1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002, 2007, 2008 Free Software Foundation, Inc.
5 Contributed by Andrew Cagney and Red Hat.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "sim-assert.h"
33 /* "core" module install handler.
35 This is called via sim_module_install to install the "core"
36 subsystem into the simulator. */
39 static MODULE_INIT_FN sim_core_init;
40 static MODULE_UNINSTALL_FN sim_core_uninstall;
45 sim_core_install (SIM_DESC sd)
47 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
49 /* establish the other handlers */
50 sim_module_add_uninstall_fn (sd, sim_core_uninstall);
51 sim_module_add_init_fn (sd, sim_core_init);
53 /* establish any initial data structures - none */
59 /* Uninstall the "core" subsystem from the simulator. */
63 sim_core_uninstall (SIM_DESC sd)
65 sim_core *core = STATE_CORE(sd);
67 /* blow away any mappings */
68 for (map = 0; map < nr_maps; map++) {
69 sim_core_mapping *curr = core->common.map[map].first;
70 while (curr != NULL) {
71 sim_core_mapping *tbd = curr;
73 if (tbd->free_buffer != NULL) {
74 SIM_ASSERT(tbd->buffer != NULL);
75 zfree(tbd->free_buffer);
79 core->common.map[map].first = NULL;
87 sim_core_init (SIM_DESC sd)
96 #ifndef SIM_CORE_SIGNAL
97 #define SIM_CORE_SIGNAL(SD,CPU,CIA,MAP,NR_BYTES,ADDR,TRANSFER,ERROR) \
98 sim_core_signal ((SD), (CPU), (CIA), (MAP), (NR_BYTES), (ADDR), (TRANSFER), (ERROR))
101 #if EXTERN_SIM_CORE_P
103 sim_core_signal (SIM_DESC sd,
109 transfer_type transfer,
110 sim_core_signals sig)
112 const char *copy = (transfer == read_transfer ? "read" : "write");
113 address_word ip = CIA_ADDR (cia);
116 case sim_core_unmapped_signal:
117 sim_io_eprintf (sd, "core: %d byte %s to unmapped address 0x%lx at 0x%lx\n",
118 nr_bytes, copy, (unsigned long) addr, (unsigned long) ip);
119 sim_engine_halt (sd, cpu, NULL, cia, sim_stopped, SIM_SIGSEGV);
121 case sim_core_unaligned_signal:
122 sim_io_eprintf (sd, "core: %d byte misaligned %s to address 0x%lx at 0x%lx\n",
123 nr_bytes, copy, (unsigned long) addr, (unsigned long) ip);
124 sim_engine_halt (sd, cpu, NULL, cia, sim_stopped, SIM_SIGBUS);
127 sim_engine_abort (sd, cpu, cia,
128 "sim_core_signal - internal error - bad switch");
134 #if EXTERN_SIM_CORE_P
135 static sim_core_mapping *
136 new_sim_core_mapping (SIM_DESC sd,
140 address_word nr_bytes,
150 sim_core_mapping *new_mapping = ZALLOC(sim_core_mapping);
152 new_mapping->level = level;
153 new_mapping->space = space;
154 new_mapping->base = addr;
155 new_mapping->nr_bytes = nr_bytes;
156 new_mapping->bound = addr + (nr_bytes - 1);
158 new_mapping->mask = (unsigned) 0 - 1;
160 new_mapping->mask = modulo - 1;
161 new_mapping->buffer = buffer;
162 new_mapping->free_buffer = free_buffer;
163 new_mapping->device = device;
169 #if EXTERN_SIM_CORE_P
171 sim_core_map_attach (SIM_DESC sd,
172 sim_core_map *access_map,
176 address_word nr_bytes,
179 struct hw *client, /*callback/default*/
181 device *client, /*callback/default*/
183 void *buffer, /*raw_memory*/
184 void *free_buffer) /*raw_memory*/
186 /* find the insertion point for this additional mapping and then
188 sim_core_mapping *next_mapping;
189 sim_core_mapping **last_mapping;
191 SIM_ASSERT ((client == NULL) != (buffer == NULL));
192 SIM_ASSERT ((client == NULL) >= (free_buffer != NULL));
194 /* actually do occasionally get a zero size map */
198 device_error(client, "called on sim_core_map_attach with size zero");
201 sim_hw_abort (sd, client, "called on sim_core_map_attach with size zero");
203 sim_io_error (sd, "called on sim_core_map_attach with size zero");
206 /* find the insertion point (between last/next) */
207 next_mapping = access_map->first;
208 last_mapping = &access_map->first;
209 while(next_mapping != NULL
210 && (next_mapping->level < level
211 || (next_mapping->level == level
212 && next_mapping->bound < addr)))
214 /* provided levels are the same */
215 /* assert: next_mapping->base > all bases before next_mapping */
216 /* assert: next_mapping->bound >= all bounds before next_mapping */
217 last_mapping = &next_mapping->next;
218 next_mapping = next_mapping->next;
221 /* check insertion point correct */
222 SIM_ASSERT (next_mapping == NULL || next_mapping->level >= level);
223 if (next_mapping != NULL && next_mapping->level == level
224 && next_mapping->base < (addr + (nr_bytes - 1)))
227 device_error (client, "memory map %d:0x%lx..0x%lx (%ld bytes) overlaps %d:0x%lx..0x%lx (%ld bytes)",
230 (long) (addr + nr_bytes - 1),
233 (long) next_mapping->base,
234 (long) next_mapping->bound,
235 (long) next_mapping->nr_bytes);
238 sim_hw_abort (sd, client, "memory map %d:0x%lx..0x%lx (%ld bytes) overlaps %d:0x%lx..0x%lx (%ld bytes)",
241 (long) (addr + (nr_bytes - 1)),
244 (long) next_mapping->base,
245 (long) next_mapping->bound,
246 (long) next_mapping->nr_bytes);
248 sim_io_error (sd, "memory map %d:0x%lx..0x%lx (%ld bytes) overlaps %d:0x%lx..0x%lx (%ld bytes)",
251 (long) (addr + (nr_bytes - 1)),
254 (long) next_mapping->base,
255 (long) next_mapping->bound,
256 (long) next_mapping->nr_bytes);
259 /* create/insert the new mapping */
260 *last_mapping = new_sim_core_mapping(sd,
262 space, addr, nr_bytes, modulo,
263 client, buffer, free_buffer);
264 (*last_mapping)->next = next_mapping;
269 /* Attach memory or a memory mapped device to the simulator.
270 See sim-core.h for a full description. */
272 #if EXTERN_SIM_CORE_P
274 sim_core_attach (SIM_DESC sd,
280 address_word nr_bytes,
287 void *optional_buffer)
289 sim_core *memory = STATE_CORE(sd);
294 /* check for for attempt to use unimplemented per-processor core map */
296 sim_io_error (sd, "sim_core_map_attach - processor specific memory map not yet supported");
298 /* verify modulo memory */
299 if (!WITH_MODULO_MEMORY && modulo != 0)
302 device_error (client, "sim_core_attach - internal error - modulo memory disabled");
305 sim_hw_abort (sd, client, "sim_core_attach - internal error - modulo memory disabled");
307 sim_io_error (sd, "sim_core_attach - internal error - modulo memory disabled");
309 if (client != NULL && modulo != 0)
312 device_error (client, "sim_core_attach - internal error - modulo and callback memory conflict");
315 sim_hw_abort (sd, client, "sim_core_attach - internal error - modulo and callback memory conflict");
317 sim_io_error (sd, "sim_core_attach - internal error - modulo and callback memory conflict");
321 unsigned mask = modulo - 1;
323 while (mask >= sizeof (unsigned64)) /* minimum modulo */
330 if (mask != sizeof (unsigned64) - 1)
333 device_error (client, "sim_core_attach - internal error - modulo %lx not power of two", (long) modulo);
336 sim_hw_abort (sd, client, "sim_core_attach - internal error - modulo %lx not power of two", (long) modulo);
338 sim_io_error (sd, "sim_core_attach - internal error - modulo %lx not power of two", (long) modulo);
342 /* verify consistency between device and buffer */
343 if (client != NULL && optional_buffer != NULL)
346 device_error (client, "sim_core_attach - internal error - conflicting buffer and attach arguments");
349 sim_hw_abort (sd, client, "sim_core_attach - internal error - conflicting buffer and attach arguments");
351 sim_io_error (sd, "sim_core_attach - internal error - conflicting buffer and attach arguments");
355 if (optional_buffer == NULL)
357 int padding = (addr % sizeof (unsigned64));
358 unsigned long bytes = (modulo == 0 ? nr_bytes : modulo) + padding;
359 free_buffer = zalloc (bytes);
360 buffer = (char*) free_buffer + padding;
364 buffer = optional_buffer;
375 /* attach the region to all applicable access maps */
380 if (mapmask & (1 << map))
382 sim_core_map_attach (sd, &memory->common.map[map],
383 level, space, addr, nr_bytes, modulo,
384 client, buffer, free_buffer);
389 /* Just copy this map to each of the processor specific data structures.
390 FIXME - later this will be replaced by true processor specific
394 for (i = 0; i < MAX_NR_PROCESSORS; i++)
396 CPU_CORE (STATE_CPU (sd, i))->common = STATE_CORE (sd)->common;
403 /* Remove any memory reference related to this address */
404 #if EXTERN_SIM_CORE_P
406 sim_core_map_detach (SIM_DESC sd,
407 sim_core_map *access_map,
412 sim_core_mapping **entry;
413 for (entry = &access_map->first;
415 entry = &(*entry)->next)
417 if ((*entry)->base == addr
418 && (*entry)->level == level
419 && (*entry)->space == space)
421 sim_core_mapping *dead = (*entry);
422 (*entry) = dead->next;
423 if (dead->free_buffer != NULL)
424 zfree (dead->free_buffer);
432 #if EXTERN_SIM_CORE_P
434 sim_core_detach (SIM_DESC sd,
440 sim_core *memory = STATE_CORE (sd);
442 for (map = 0; map < nr_maps; map++)
444 sim_core_map_detach (sd, &memory->common.map[map],
445 level, address_space, addr);
447 /* Just copy this update to each of the processor specific data
448 structures. FIXME - later this will be replaced by true
449 processor specific maps. */
452 for (i = 0; i < MAX_NR_PROCESSORS; i++)
454 CPU_CORE (STATE_CPU (sd, i))->common = STATE_CORE (sd)->common;
461 STATIC_INLINE_SIM_CORE\
463 sim_core_find_mapping(sim_core_common *core,
467 transfer_type transfer,
468 int abort, /*either 0 or 1 - hint to inline/-O */
469 sim_cpu *cpu, /* abort => cpu != NULL */
472 sim_core_mapping *mapping = core->map[map].first;
473 ASSERT ((addr & (nr_bytes - 1)) == 0); /* must be aligned */
474 ASSERT ((addr + (nr_bytes - 1)) >= addr); /* must not wrap */
475 ASSERT (!abort || cpu != NULL); /* abort needs a non null CPU */
476 while (mapping != NULL)
478 if (addr >= mapping->base
479 && (addr + (nr_bytes - 1)) <= mapping->bound)
481 mapping = mapping->next;
485 SIM_CORE_SIGNAL (CPU_STATE (cpu), cpu, cia, map, nr_bytes, addr, transfer,
486 sim_core_unmapped_signal);
492 STATIC_INLINE_SIM_CORE\
494 sim_core_translate (sim_core_mapping *mapping,
497 if (WITH_MODULO_MEMORY)
498 return (void *)((unsigned8 *) mapping->buffer
499 + ((addr - mapping->base) & mapping->mask));
501 return (void *)((unsigned8 *) mapping->buffer
502 + addr - mapping->base);
506 #if EXTERN_SIM_CORE_P
508 sim_core_read_buffer (SIM_DESC sd,
515 sim_core_common *core = (cpu == NULL ? &STATE_CORE (sd)->common : &CPU_CORE (cpu)->common);
519 unsigned_word raddr = addr + count;
520 sim_core_mapping *mapping =
521 sim_core_find_mapping (core, map,
522 raddr, /*nr-bytes*/1,
524 0 /*dont-abort*/, NULL, NULL_CIA);
528 if (mapping->device != NULL)
530 int nr_bytes = len - count;
531 sim_cia cia = cpu ? CIA_GET (cpu) : NULL_CIA;
532 if (raddr + nr_bytes - 1> mapping->bound)
533 nr_bytes = mapping->bound - raddr + 1;
534 if (device_io_read_buffer (mapping->device,
535 (unsigned_1*)buffer + count,
548 if (mapping->device != NULL)
550 int nr_bytes = len - count;
551 if (raddr + nr_bytes - 1> mapping->bound)
552 nr_bytes = mapping->bound - raddr + 1;
553 if (sim_hw_io_read_buffer (sd, mapping->device,
554 (unsigned_1*)buffer + count,
557 nr_bytes) != nr_bytes)
563 ((unsigned_1*)buffer)[count] =
564 *(unsigned_1*)sim_core_translate(mapping, raddr);
572 #if EXTERN_SIM_CORE_P
574 sim_core_write_buffer (SIM_DESC sd,
581 sim_core_common *core = (cpu == NULL ? &STATE_CORE (sd)->common : &CPU_CORE (cpu)->common);
585 unsigned_word raddr = addr + count;
586 sim_core_mapping *mapping =
587 sim_core_find_mapping (core, map,
588 raddr, /*nr-bytes*/1,
590 0 /*dont-abort*/, NULL, NULL_CIA);
594 if (WITH_CALLBACK_MEMORY
595 && mapping->device != NULL)
597 int nr_bytes = len - count;
598 sim_cia cia = cpu ? CIA_GET (cpu) : NULL_CIA;
599 if (raddr + nr_bytes - 1 > mapping->bound)
600 nr_bytes = mapping->bound - raddr + 1;
601 if (device_io_write_buffer (mapping->device,
602 (unsigned_1*)buffer + count,
615 if (WITH_CALLBACK_MEMORY
616 && mapping->device != NULL)
618 int nr_bytes = len - count;
619 if (raddr + nr_bytes - 1 > mapping->bound)
620 nr_bytes = mapping->bound - raddr + 1;
621 if (sim_hw_io_write_buffer (sd, mapping->device,
622 (unsigned_1*)buffer + count,
625 nr_bytes) != nr_bytes)
631 *(unsigned_1*)sim_core_translate(mapping, raddr) =
632 ((unsigned_1*)buffer)[count];
640 #if EXTERN_SIM_CORE_P
642 sim_core_set_xor (SIM_DESC sd,
646 /* set up the XOR map if required. */
647 if (WITH_XOR_ENDIAN) {
649 sim_core *core = STATE_CORE (sd);
650 sim_cpu_core *cpu_core = (cpu != NULL ? CPU_CORE (cpu) : NULL);
651 if (cpu_core != NULL)
656 mask = WITH_XOR_ENDIAN - 1;
659 while (i - 1 < WITH_XOR_ENDIAN)
661 cpu_core->xor[i-1] = mask;
662 mask = (mask << 1) & (WITH_XOR_ENDIAN - 1);
669 core->byte_xor = WITH_XOR_ENDIAN - 1;
677 sim_engine_abort (sd, NULL, NULL_CIA,
678 "Attempted to enable xor-endian mode when permenantly disabled.");
684 #if EXTERN_SIM_CORE_P
686 reverse_n (unsigned_1 *dest,
687 const unsigned_1 *src,
691 for (i = 0; i < nr_bytes; i++)
693 dest [nr_bytes - i - 1] = src [i];
699 #if EXTERN_SIM_CORE_P
701 sim_core_xor_read_buffer (SIM_DESC sd,
708 address_word byte_xor = (cpu == NULL ? STATE_CORE (sd)->byte_xor : CPU_CORE (cpu)->xor[0]);
709 if (!WITH_XOR_ENDIAN || !byte_xor)
710 return sim_core_read_buffer (sd, cpu, map, buffer, addr, nr_bytes);
712 /* only break up transfers when xor-endian is both selected and enabled */
714 unsigned_1 x[WITH_XOR_ENDIAN + 1]; /* +1 to avoid zero-sized array */
715 unsigned nr_transfered = 0;
716 address_word start = addr;
717 unsigned nr_this_transfer = (WITH_XOR_ENDIAN - (addr & ~(WITH_XOR_ENDIAN - 1)));
719 /* initial and intermediate transfers are broken when they cross
720 an XOR endian boundary */
721 while (nr_transfered + nr_this_transfer < nr_bytes)
722 /* initial/intermediate transfers */
724 /* since xor-endian is enabled stop^xor defines the start
725 address of the transfer */
726 stop = start + nr_this_transfer - 1;
727 SIM_ASSERT (start <= stop);
728 SIM_ASSERT ((stop ^ byte_xor) <= (start ^ byte_xor));
729 if (sim_core_read_buffer (sd, cpu, map, x, stop ^ byte_xor, nr_this_transfer)
731 return nr_transfered;
732 reverse_n (&((unsigned_1*)buffer)[nr_transfered], x, nr_this_transfer);
733 nr_transfered += nr_this_transfer;
734 nr_this_transfer = WITH_XOR_ENDIAN;
738 nr_this_transfer = nr_bytes - nr_transfered;
739 stop = start + nr_this_transfer - 1;
740 SIM_ASSERT (stop == (addr + nr_bytes - 1));
741 if (sim_core_read_buffer (sd, cpu, map, x, stop ^ byte_xor, nr_this_transfer)
743 return nr_transfered;
744 reverse_n (&((unsigned_1*)buffer)[nr_transfered], x, nr_this_transfer);
751 #if EXTERN_SIM_CORE_P
753 sim_core_xor_write_buffer (SIM_DESC sd,
760 address_word byte_xor = (cpu == NULL ? STATE_CORE (sd)->byte_xor : CPU_CORE (cpu)->xor[0]);
761 if (!WITH_XOR_ENDIAN || !byte_xor)
762 return sim_core_write_buffer (sd, cpu, map, buffer, addr, nr_bytes);
764 /* only break up transfers when xor-endian is both selected and enabled */
766 unsigned_1 x[WITH_XOR_ENDIAN + 1]; /* +1 to avoid zero sized array */
767 unsigned nr_transfered = 0;
768 address_word start = addr;
769 unsigned nr_this_transfer = (WITH_XOR_ENDIAN - (addr & ~(WITH_XOR_ENDIAN - 1)));
771 /* initial and intermediate transfers are broken when they cross
772 an XOR endian boundary */
773 while (nr_transfered + nr_this_transfer < nr_bytes)
774 /* initial/intermediate transfers */
776 /* since xor-endian is enabled stop^xor defines the start
777 address of the transfer */
778 stop = start + nr_this_transfer - 1;
779 SIM_ASSERT (start <= stop);
780 SIM_ASSERT ((stop ^ byte_xor) <= (start ^ byte_xor));
781 reverse_n (x, &((unsigned_1*)buffer)[nr_transfered], nr_this_transfer);
782 if (sim_core_read_buffer (sd, cpu, map, x, stop ^ byte_xor, nr_this_transfer)
784 return nr_transfered;
785 nr_transfered += nr_this_transfer;
786 nr_this_transfer = WITH_XOR_ENDIAN;
790 nr_this_transfer = nr_bytes - nr_transfered;
791 stop = start + nr_this_transfer - 1;
792 SIM_ASSERT (stop == (addr + nr_bytes - 1));
793 reverse_n (x, &((unsigned_1*)buffer)[nr_transfered], nr_this_transfer);
794 if (sim_core_read_buffer (sd, cpu, map, x, stop ^ byte_xor, nr_this_transfer)
796 return nr_transfered;
802 #if EXTERN_SIM_CORE_P
804 sim_core_trans_addr (SIM_DESC sd,
809 sim_core_common *core = (cpu == NULL ? &STATE_CORE (sd)->common : &CPU_CORE (cpu)->common);
810 sim_core_mapping *mapping =
811 sim_core_find_mapping (core, map,
814 0 /*dont-abort*/, NULL, NULL_CIA);
817 return sim_core_translate(mapping, addr);
823 /* define the read/write 1/2/4/8/16/word functions */
826 #include "sim-n-core.h"
829 #include "sim-n-core.h"
833 #include "sim-n-core.h"
837 #include "sim-n-core.h"
841 #include "sim-n-core.h"
844 #include "sim-n-core.h"
848 #include "sim-n-core.h"
851 #include "sim-n-core.h"
854 #include "sim-n-core.h"