1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002, 2007 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 2 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, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
29 #include "sim-assert.h"
35 /* "core" module install handler.
37 This is called via sim_module_install to install the "core"
38 subsystem into the simulator. */
41 static MODULE_INIT_FN sim_core_init;
42 static MODULE_UNINSTALL_FN sim_core_uninstall;
47 sim_core_install (SIM_DESC sd)
49 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
51 /* establish the other handlers */
52 sim_module_add_uninstall_fn (sd, sim_core_uninstall);
53 sim_module_add_init_fn (sd, sim_core_init);
55 /* establish any initial data structures - none */
61 /* Uninstall the "core" subsystem from the simulator. */
65 sim_core_uninstall (SIM_DESC sd)
67 sim_core *core = STATE_CORE(sd);
69 /* blow away any mappings */
70 for (map = 0; map < nr_maps; map++) {
71 sim_core_mapping *curr = core->common.map[map].first;
72 while (curr != NULL) {
73 sim_core_mapping *tbd = curr;
75 if (tbd->free_buffer != NULL) {
76 SIM_ASSERT(tbd->buffer != NULL);
77 zfree(tbd->free_buffer);
81 core->common.map[map].first = NULL;
89 sim_core_init (SIM_DESC sd)
98 #ifndef SIM_CORE_SIGNAL
99 #define SIM_CORE_SIGNAL(SD,CPU,CIA,MAP,NR_BYTES,ADDR,TRANSFER,ERROR) \
100 sim_core_signal ((SD), (CPU), (CIA), (MAP), (NR_BYTES), (ADDR), (TRANSFER), (ERROR))
103 #if EXTERN_SIM_CORE_P
105 sim_core_signal (SIM_DESC sd,
111 transfer_type transfer,
112 sim_core_signals sig)
114 const char *copy = (transfer == read_transfer ? "read" : "write");
115 address_word ip = CIA_ADDR (cia);
118 case sim_core_unmapped_signal:
119 sim_io_eprintf (sd, "core: %d byte %s to unmapped address 0x%lx at 0x%lx\n",
120 nr_bytes, copy, (unsigned long) addr, (unsigned long) ip);
121 sim_engine_halt (sd, cpu, NULL, cia, sim_stopped, SIM_SIGSEGV);
123 case sim_core_unaligned_signal:
124 sim_io_eprintf (sd, "core: %d byte misaligned %s to address 0x%lx at 0x%lx\n",
125 nr_bytes, copy, (unsigned long) addr, (unsigned long) ip);
126 sim_engine_halt (sd, cpu, NULL, cia, sim_stopped, SIM_SIGBUS);
129 sim_engine_abort (sd, cpu, cia,
130 "sim_core_signal - internal error - bad switch");
136 #if EXTERN_SIM_CORE_P
137 static sim_core_mapping *
138 new_sim_core_mapping (SIM_DESC sd,
142 address_word nr_bytes,
152 sim_core_mapping *new_mapping = ZALLOC(sim_core_mapping);
154 new_mapping->level = level;
155 new_mapping->space = space;
156 new_mapping->base = addr;
157 new_mapping->nr_bytes = nr_bytes;
158 new_mapping->bound = addr + (nr_bytes - 1);
160 new_mapping->mask = (unsigned) 0 - 1;
162 new_mapping->mask = modulo - 1;
163 new_mapping->buffer = buffer;
164 new_mapping->free_buffer = free_buffer;
165 new_mapping->device = device;
171 #if EXTERN_SIM_CORE_P
173 sim_core_map_attach (SIM_DESC sd,
174 sim_core_map *access_map,
178 address_word nr_bytes,
181 struct hw *client, /*callback/default*/
183 device *client, /*callback/default*/
185 void *buffer, /*raw_memory*/
186 void *free_buffer) /*raw_memory*/
188 /* find the insertion point for this additional mapping and then
190 sim_core_mapping *next_mapping;
191 sim_core_mapping **last_mapping;
193 SIM_ASSERT ((client == NULL) != (buffer == NULL));
194 SIM_ASSERT ((client == NULL) >= (free_buffer != NULL));
196 /* actually do occasionally get a zero size map */
200 device_error(client, "called on sim_core_map_attach with size zero");
203 sim_hw_abort (sd, client, "called on sim_core_map_attach with size zero");
205 sim_io_error (sd, "called on sim_core_map_attach with size zero");
208 /* find the insertion point (between last/next) */
209 next_mapping = access_map->first;
210 last_mapping = &access_map->first;
211 while(next_mapping != NULL
212 && (next_mapping->level < level
213 || (next_mapping->level == level
214 && next_mapping->bound < addr)))
216 /* provided levels are the same */
217 /* assert: next_mapping->base > all bases before next_mapping */
218 /* assert: next_mapping->bound >= all bounds before next_mapping */
219 last_mapping = &next_mapping->next;
220 next_mapping = next_mapping->next;
223 /* check insertion point correct */
224 SIM_ASSERT (next_mapping == NULL || next_mapping->level >= level);
225 if (next_mapping != NULL && next_mapping->level == level
226 && next_mapping->base < (addr + (nr_bytes - 1)))
229 device_error (client, "memory map %d:0x%lx..0x%lx (%ld bytes) overlaps %d:0x%lx..0x%lx (%ld bytes)",
232 (long) (addr + nr_bytes - 1),
235 (long) next_mapping->base,
236 (long) next_mapping->bound,
237 (long) next_mapping->nr_bytes);
240 sim_hw_abort (sd, client, "memory map %d:0x%lx..0x%lx (%ld bytes) overlaps %d:0x%lx..0x%lx (%ld bytes)",
243 (long) (addr + (nr_bytes - 1)),
246 (long) next_mapping->base,
247 (long) next_mapping->bound,
248 (long) next_mapping->nr_bytes);
250 sim_io_error (sd, "memory map %d:0x%lx..0x%lx (%ld bytes) overlaps %d:0x%lx..0x%lx (%ld bytes)",
253 (long) (addr + (nr_bytes - 1)),
256 (long) next_mapping->base,
257 (long) next_mapping->bound,
258 (long) next_mapping->nr_bytes);
261 /* create/insert the new mapping */
262 *last_mapping = new_sim_core_mapping(sd,
264 space, addr, nr_bytes, modulo,
265 client, buffer, free_buffer);
266 (*last_mapping)->next = next_mapping;
271 /* Attach memory or a memory mapped device to the simulator.
272 See sim-core.h for a full description. */
274 #if EXTERN_SIM_CORE_P
276 sim_core_attach (SIM_DESC sd,
282 address_word nr_bytes,
289 void *optional_buffer)
291 sim_core *memory = STATE_CORE(sd);
296 /* check for for attempt to use unimplemented per-processor core map */
298 sim_io_error (sd, "sim_core_map_attach - processor specific memory map not yet supported");
300 /* verify modulo memory */
301 if (!WITH_MODULO_MEMORY && modulo != 0)
304 device_error (client, "sim_core_attach - internal error - modulo memory disabled");
307 sim_hw_abort (sd, client, "sim_core_attach - internal error - modulo memory disabled");
309 sim_io_error (sd, "sim_core_attach - internal error - modulo memory disabled");
311 if (client != NULL && modulo != 0)
314 device_error (client, "sim_core_attach - internal error - modulo and callback memory conflict");
317 sim_hw_abort (sd, client, "sim_core_attach - internal error - modulo and callback memory conflict");
319 sim_io_error (sd, "sim_core_attach - internal error - modulo and callback memory conflict");
323 unsigned mask = modulo - 1;
325 while (mask >= sizeof (unsigned64)) /* minimum modulo */
332 if (mask != sizeof (unsigned64) - 1)
335 device_error (client, "sim_core_attach - internal error - modulo %lx not power of two", (long) modulo);
338 sim_hw_abort (sd, client, "sim_core_attach - internal error - modulo %lx not power of two", (long) modulo);
340 sim_io_error (sd, "sim_core_attach - internal error - modulo %lx not power of two", (long) modulo);
344 /* verify consistency between device and buffer */
345 if (client != NULL && optional_buffer != NULL)
348 device_error (client, "sim_core_attach - internal error - conflicting buffer and attach arguments");
351 sim_hw_abort (sd, client, "sim_core_attach - internal error - conflicting buffer and attach arguments");
353 sim_io_error (sd, "sim_core_attach - internal error - conflicting buffer and attach arguments");
357 if (optional_buffer == NULL)
359 int padding = (addr % sizeof (unsigned64));
360 unsigned long bytes = (modulo == 0 ? nr_bytes : modulo) + padding;
361 free_buffer = zalloc (bytes);
362 buffer = (char*) free_buffer + padding;
366 buffer = optional_buffer;
377 /* attach the region to all applicable access maps */
382 if (mapmask & (1 << map))
384 sim_core_map_attach (sd, &memory->common.map[map],
385 level, space, addr, nr_bytes, modulo,
386 client, buffer, free_buffer);
391 /* Just copy this map to each of the processor specific data structures.
392 FIXME - later this will be replaced by true processor specific
396 for (i = 0; i < MAX_NR_PROCESSORS; i++)
398 CPU_CORE (STATE_CPU (sd, i))->common = STATE_CORE (sd)->common;
405 /* Remove any memory reference related to this address */
406 #if EXTERN_SIM_CORE_P
408 sim_core_map_detach (SIM_DESC sd,
409 sim_core_map *access_map,
414 sim_core_mapping **entry;
415 for (entry = &access_map->first;
417 entry = &(*entry)->next)
419 if ((*entry)->base == addr
420 && (*entry)->level == level
421 && (*entry)->space == space)
423 sim_core_mapping *dead = (*entry);
424 (*entry) = dead->next;
425 if (dead->free_buffer != NULL)
426 zfree (dead->free_buffer);
434 #if EXTERN_SIM_CORE_P
436 sim_core_detach (SIM_DESC sd,
442 sim_core *memory = STATE_CORE (sd);
444 for (map = 0; map < nr_maps; map++)
446 sim_core_map_detach (sd, &memory->common.map[map],
447 level, address_space, addr);
449 /* Just copy this update to each of the processor specific data
450 structures. FIXME - later this will be replaced by true
451 processor specific maps. */
454 for (i = 0; i < MAX_NR_PROCESSORS; i++)
456 CPU_CORE (STATE_CPU (sd, i))->common = STATE_CORE (sd)->common;
463 STATIC_INLINE_SIM_CORE\
465 sim_core_find_mapping(sim_core_common *core,
469 transfer_type transfer,
470 int abort, /*either 0 or 1 - hint to inline/-O */
471 sim_cpu *cpu, /* abort => cpu != NULL */
474 sim_core_mapping *mapping = core->map[map].first;
475 ASSERT ((addr & (nr_bytes - 1)) == 0); /* must be aligned */
476 ASSERT ((addr + (nr_bytes - 1)) >= addr); /* must not wrap */
477 ASSERT (!abort || cpu != NULL); /* abort needs a non null CPU */
478 while (mapping != NULL)
480 if (addr >= mapping->base
481 && (addr + (nr_bytes - 1)) <= mapping->bound)
483 mapping = mapping->next;
487 SIM_CORE_SIGNAL (CPU_STATE (cpu), cpu, cia, map, nr_bytes, addr, transfer,
488 sim_core_unmapped_signal);
494 STATIC_INLINE_SIM_CORE\
496 sim_core_translate (sim_core_mapping *mapping,
499 if (WITH_MODULO_MEMORY)
500 return (void *)((unsigned8 *) mapping->buffer
501 + ((addr - mapping->base) & mapping->mask));
503 return (void *)((unsigned8 *) mapping->buffer
504 + addr - mapping->base);
508 #if EXTERN_SIM_CORE_P
510 sim_core_read_buffer (SIM_DESC sd,
517 sim_core_common *core = (cpu == NULL ? &STATE_CORE (sd)->common : &CPU_CORE (cpu)->common);
521 unsigned_word raddr = addr + count;
522 sim_core_mapping *mapping =
523 sim_core_find_mapping (core, map,
524 raddr, /*nr-bytes*/1,
526 0 /*dont-abort*/, NULL, NULL_CIA);
530 if (mapping->device != NULL)
532 int nr_bytes = len - count;
533 sim_cia cia = cpu ? CIA_GET (cpu) : NULL_CIA;
534 if (raddr + nr_bytes - 1> mapping->bound)
535 nr_bytes = mapping->bound - raddr + 1;
536 if (device_io_read_buffer (mapping->device,
537 (unsigned_1*)buffer + count,
550 if (mapping->device != NULL)
552 int nr_bytes = len - count;
553 if (raddr + nr_bytes - 1> mapping->bound)
554 nr_bytes = mapping->bound - raddr + 1;
555 if (sim_hw_io_read_buffer (sd, mapping->device,
556 (unsigned_1*)buffer + count,
559 nr_bytes) != nr_bytes)
565 ((unsigned_1*)buffer)[count] =
566 *(unsigned_1*)sim_core_translate(mapping, raddr);
574 #if EXTERN_SIM_CORE_P
576 sim_core_write_buffer (SIM_DESC sd,
583 sim_core_common *core = (cpu == NULL ? &STATE_CORE (sd)->common : &CPU_CORE (cpu)->common);
587 unsigned_word raddr = addr + count;
588 sim_core_mapping *mapping =
589 sim_core_find_mapping (core, map,
590 raddr, /*nr-bytes*/1,
592 0 /*dont-abort*/, NULL, NULL_CIA);
596 if (WITH_CALLBACK_MEMORY
597 && mapping->device != NULL)
599 int nr_bytes = len - count;
600 sim_cia cia = cpu ? CIA_GET (cpu) : NULL_CIA;
601 if (raddr + nr_bytes - 1 > mapping->bound)
602 nr_bytes = mapping->bound - raddr + 1;
603 if (device_io_write_buffer (mapping->device,
604 (unsigned_1*)buffer + count,
617 if (WITH_CALLBACK_MEMORY
618 && mapping->device != NULL)
620 int nr_bytes = len - count;
621 if (raddr + nr_bytes - 1 > mapping->bound)
622 nr_bytes = mapping->bound - raddr + 1;
623 if (sim_hw_io_write_buffer (sd, mapping->device,
624 (unsigned_1*)buffer + count,
627 nr_bytes) != nr_bytes)
633 *(unsigned_1*)sim_core_translate(mapping, raddr) =
634 ((unsigned_1*)buffer)[count];
642 #if EXTERN_SIM_CORE_P
644 sim_core_set_xor (SIM_DESC sd,
648 /* set up the XOR map if required. */
649 if (WITH_XOR_ENDIAN) {
651 sim_core *core = STATE_CORE (sd);
652 sim_cpu_core *cpu_core = (cpu != NULL ? CPU_CORE (cpu) : NULL);
653 if (cpu_core != NULL)
658 mask = WITH_XOR_ENDIAN - 1;
661 while (i - 1 < WITH_XOR_ENDIAN)
663 cpu_core->xor[i-1] = mask;
664 mask = (mask << 1) & (WITH_XOR_ENDIAN - 1);
671 core->byte_xor = WITH_XOR_ENDIAN - 1;
679 sim_engine_abort (sd, NULL, NULL_CIA,
680 "Attempted to enable xor-endian mode when permenantly disabled.");
686 #if EXTERN_SIM_CORE_P
688 reverse_n (unsigned_1 *dest,
689 const unsigned_1 *src,
693 for (i = 0; i < nr_bytes; i++)
695 dest [nr_bytes - i - 1] = src [i];
701 #if EXTERN_SIM_CORE_P
703 sim_core_xor_read_buffer (SIM_DESC sd,
710 address_word byte_xor = (cpu == NULL ? STATE_CORE (sd)->byte_xor : CPU_CORE (cpu)->xor[0]);
711 if (!WITH_XOR_ENDIAN || !byte_xor)
712 return sim_core_read_buffer (sd, cpu, map, buffer, addr, nr_bytes);
714 /* only break up transfers when xor-endian is both selected and enabled */
716 unsigned_1 x[WITH_XOR_ENDIAN + 1]; /* +1 to avoid zero-sized array */
717 unsigned nr_transfered = 0;
718 address_word start = addr;
719 unsigned nr_this_transfer = (WITH_XOR_ENDIAN - (addr & ~(WITH_XOR_ENDIAN - 1)));
721 /* initial and intermediate transfers are broken when they cross
722 an XOR endian boundary */
723 while (nr_transfered + nr_this_transfer < nr_bytes)
724 /* initial/intermediate transfers */
726 /* since xor-endian is enabled stop^xor defines the start
727 address of the transfer */
728 stop = start + nr_this_transfer - 1;
729 SIM_ASSERT (start <= stop);
730 SIM_ASSERT ((stop ^ byte_xor) <= (start ^ byte_xor));
731 if (sim_core_read_buffer (sd, cpu, map, x, stop ^ byte_xor, nr_this_transfer)
733 return nr_transfered;
734 reverse_n (&((unsigned_1*)buffer)[nr_transfered], x, nr_this_transfer);
735 nr_transfered += nr_this_transfer;
736 nr_this_transfer = WITH_XOR_ENDIAN;
740 nr_this_transfer = nr_bytes - nr_transfered;
741 stop = start + nr_this_transfer - 1;
742 SIM_ASSERT (stop == (addr + nr_bytes - 1));
743 if (sim_core_read_buffer (sd, cpu, map, x, stop ^ byte_xor, nr_this_transfer)
745 return nr_transfered;
746 reverse_n (&((unsigned_1*)buffer)[nr_transfered], x, nr_this_transfer);
753 #if EXTERN_SIM_CORE_P
755 sim_core_xor_write_buffer (SIM_DESC sd,
762 address_word byte_xor = (cpu == NULL ? STATE_CORE (sd)->byte_xor : CPU_CORE (cpu)->xor[0]);
763 if (!WITH_XOR_ENDIAN || !byte_xor)
764 return sim_core_write_buffer (sd, cpu, map, buffer, addr, nr_bytes);
766 /* only break up transfers when xor-endian is both selected and enabled */
768 unsigned_1 x[WITH_XOR_ENDIAN + 1]; /* +1 to avoid zero sized array */
769 unsigned nr_transfered = 0;
770 address_word start = addr;
771 unsigned nr_this_transfer = (WITH_XOR_ENDIAN - (addr & ~(WITH_XOR_ENDIAN - 1)));
773 /* initial and intermediate transfers are broken when they cross
774 an XOR endian boundary */
775 while (nr_transfered + nr_this_transfer < nr_bytes)
776 /* initial/intermediate transfers */
778 /* since xor-endian is enabled stop^xor defines the start
779 address of the transfer */
780 stop = start + nr_this_transfer - 1;
781 SIM_ASSERT (start <= stop);
782 SIM_ASSERT ((stop ^ byte_xor) <= (start ^ byte_xor));
783 reverse_n (x, &((unsigned_1*)buffer)[nr_transfered], nr_this_transfer);
784 if (sim_core_read_buffer (sd, cpu, map, x, stop ^ byte_xor, nr_this_transfer)
786 return nr_transfered;
787 nr_transfered += nr_this_transfer;
788 nr_this_transfer = WITH_XOR_ENDIAN;
792 nr_this_transfer = nr_bytes - nr_transfered;
793 stop = start + nr_this_transfer - 1;
794 SIM_ASSERT (stop == (addr + nr_bytes - 1));
795 reverse_n (x, &((unsigned_1*)buffer)[nr_transfered], nr_this_transfer);
796 if (sim_core_read_buffer (sd, cpu, map, x, stop ^ byte_xor, nr_this_transfer)
798 return nr_transfered;
804 #if EXTERN_SIM_CORE_P
806 sim_core_trans_addr (SIM_DESC sd,
811 sim_core_common *core = (cpu == NULL ? &STATE_CORE (sd)->common : &CPU_CORE (cpu)->common);
812 sim_core_mapping *mapping =
813 sim_core_find_mapping (core, map,
816 0 /*dont-abort*/, NULL, NULL_CIA);
819 return sim_core_translate(mapping, addr);
825 /* define the read/write 1/2/4/8/16/word functions */
828 #include "sim-n-core.h"
831 #include "sim-n-core.h"
835 #include "sim-n-core.h"
839 #include "sim-n-core.h"
843 #include "sim-n-core.h"
846 #include "sim-n-core.h"
850 #include "sim-n-core.h"
853 #include "sim-n-core.h"
856 #include "sim-n-core.h"