1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002-2021 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"
28 #include "libiberty.h"
36 /* "core" module install handler.
38 This is called via sim_module_install to install the "core"
39 subsystem into the simulator. */
42 static MODULE_INIT_FN sim_core_init
;
43 static MODULE_UNINSTALL_FN sim_core_uninstall
;
48 sim_core_install (SIM_DESC sd
)
50 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
52 /* establish the other handlers */
53 sim_module_add_uninstall_fn (sd
, sim_core_uninstall
);
54 sim_module_add_init_fn (sd
, sim_core_init
);
56 /* establish any initial data structures - none */
62 /* Uninstall the "core" subsystem from the simulator. */
66 sim_core_uninstall (SIM_DESC sd
)
68 sim_core
*core
= STATE_CORE (sd
);
70 /* blow away any mappings */
71 for (map
= 0; map
< nr_maps
; map
++) {
72 sim_core_mapping
*curr
= core
->common
.map
[map
].first
;
73 while (curr
!= NULL
) {
74 sim_core_mapping
*tbd
= curr
;
76 if (tbd
->free_buffer
!= NULL
) {
77 SIM_ASSERT (tbd
->buffer
!= NULL
);
78 free (tbd
->free_buffer
);
82 core
->common
.map
[map
].first
= NULL
;
90 sim_core_init (SIM_DESC sd
)
99 #ifndef SIM_CORE_SIGNAL
100 #define SIM_CORE_SIGNAL(SD,CPU,CIA,MAP,NR_BYTES,ADDR,TRANSFER,ERROR) \
101 sim_core_signal ((SD), (CPU), (CIA), (MAP), (NR_BYTES), (ADDR), (TRANSFER), (ERROR))
104 #if EXTERN_SIM_CORE_P
106 sim_core_signal (SIM_DESC sd
,
112 transfer_type transfer
,
113 sim_core_signals sig
)
115 const char *copy
= (transfer
== read_transfer
? "read" : "write");
116 address_word ip
= CIA_ADDR (cia
);
119 case sim_core_unmapped_signal
:
120 sim_io_eprintf (sd
, "core: %d byte %s to unmapped address 0x%lx at 0x%lx\n",
121 nr_bytes
, copy
, (unsigned long) addr
, (unsigned long) ip
);
122 sim_engine_halt (sd
, cpu
, NULL
, cia
, sim_stopped
, SIM_SIGSEGV
);
124 case sim_core_unaligned_signal
:
125 sim_io_eprintf (sd
, "core: %d byte misaligned %s to address 0x%lx at 0x%lx\n",
126 nr_bytes
, copy
, (unsigned long) addr
, (unsigned long) ip
);
127 sim_engine_halt (sd
, cpu
, NULL
, cia
, sim_stopped
, SIM_SIGBUS
);
130 sim_engine_abort (sd
, cpu
, cia
,
131 "sim_core_signal - internal error - bad switch");
137 #if EXTERN_SIM_CORE_P
138 static sim_core_mapping
*
139 new_sim_core_mapping (SIM_DESC sd
,
143 address_word nr_bytes
,
149 sim_core_mapping
*new_mapping
= ZALLOC (sim_core_mapping
);
151 new_mapping
->level
= level
;
152 new_mapping
->space
= space
;
153 new_mapping
->base
= addr
;
154 new_mapping
->nr_bytes
= nr_bytes
;
155 new_mapping
->bound
= addr
+ (nr_bytes
- 1);
156 new_mapping
->mask
= modulo
- 1;
157 new_mapping
->buffer
= buffer
;
158 new_mapping
->free_buffer
= free_buffer
;
159 new_mapping
->device
= device
;
165 #if EXTERN_SIM_CORE_P
167 sim_core_map_attach (SIM_DESC sd
,
168 sim_core_map
*access_map
,
172 address_word nr_bytes
,
174 struct hw
*client
, /*callback/default*/
175 void *buffer
, /*raw_memory*/
176 void *free_buffer
) /*raw_memory*/
178 /* find the insertion point for this additional mapping and then
180 sim_core_mapping
*next_mapping
;
181 sim_core_mapping
**last_mapping
;
183 SIM_ASSERT ((client
== NULL
) != (buffer
== NULL
));
184 SIM_ASSERT ((client
== NULL
) >= (free_buffer
!= NULL
));
186 /* actually do occasionally get a zero size map */
190 sim_hw_abort (sd
, client
, "called on sim_core_map_attach with size zero");
192 sim_io_error (sd
, "called on sim_core_map_attach with size zero");
195 /* find the insertion point (between last/next) */
196 next_mapping
= access_map
->first
;
197 last_mapping
= &access_map
->first
;
198 while (next_mapping
!= NULL
199 && (next_mapping
->level
< level
200 || (next_mapping
->level
== level
201 && next_mapping
->bound
< addr
)))
203 /* provided levels are the same */
204 /* assert: next_mapping->base > all bases before next_mapping */
205 /* assert: next_mapping->bound >= all bounds before next_mapping */
206 last_mapping
= &next_mapping
->next
;
207 next_mapping
= next_mapping
->next
;
210 /* check insertion point correct */
211 SIM_ASSERT (next_mapping
== NULL
|| next_mapping
->level
>= level
);
212 if (next_mapping
!= NULL
&& next_mapping
->level
== level
213 && next_mapping
->base
< (addr
+ (nr_bytes
- 1)))
216 sim_hw_abort (sd
, client
, "memory map %d:0x%lx..0x%lx (%ld bytes) overlaps %d:0x%lx..0x%lx (%ld bytes)",
219 (long) (addr
+ (nr_bytes
- 1)),
222 (long) next_mapping
->base
,
223 (long) next_mapping
->bound
,
224 (long) next_mapping
->nr_bytes
);
226 sim_io_error (sd
, "memory map %d:0x%lx..0x%lx (%ld bytes) overlaps %d:0x%lx..0x%lx (%ld bytes)",
229 (long) (addr
+ (nr_bytes
- 1)),
232 (long) next_mapping
->base
,
233 (long) next_mapping
->bound
,
234 (long) next_mapping
->nr_bytes
);
237 /* create/insert the new mapping */
238 *last_mapping
= new_sim_core_mapping (sd
,
240 space
, addr
, nr_bytes
, modulo
,
241 client
, buffer
, free_buffer
);
242 (*last_mapping
)->next
= next_mapping
;
247 /* Attach memory or a memory mapped device to the simulator.
248 See sim-core.h for a full description. */
250 #if EXTERN_SIM_CORE_P
252 sim_core_attach (SIM_DESC sd
,
258 address_word nr_bytes
,
261 void *optional_buffer
)
263 sim_core
*memory
= STATE_CORE (sd
);
268 /* check for for attempt to use unimplemented per-processor core map */
270 sim_io_error (sd
, "sim_core_map_attach - processor specific memory map not yet supported");
272 if (client
!= NULL
&& modulo
!= 0)
275 sim_hw_abort (sd
, client
, "sim_core_attach - internal error - modulo and callback memory conflict");
277 sim_io_error (sd
, "sim_core_attach - internal error - modulo and callback memory conflict");
281 unsigned mask
= modulo
- 1;
283 while (mask
>= sizeof (unsigned64
)) /* minimum modulo */
290 if (mask
!= sizeof (unsigned64
) - 1)
293 sim_hw_abort (sd
, client
, "sim_core_attach - internal error - modulo %lx not power of two", (long) modulo
);
295 sim_io_error (sd
, "sim_core_attach - internal error - modulo %lx not power of two", (long) modulo
);
299 /* verify consistency between device and buffer */
300 if (client
!= NULL
&& optional_buffer
!= NULL
)
303 sim_hw_abort (sd
, client
, "sim_core_attach - internal error - conflicting buffer and attach arguments");
305 sim_io_error (sd
, "sim_core_attach - internal error - conflicting buffer and attach arguments");
309 if (optional_buffer
== NULL
)
311 int padding
= (addr
% sizeof (unsigned64
));
312 unsigned long bytes
= (modulo
== 0 ? nr_bytes
: modulo
) + padding
;
313 free_buffer
= zalloc (bytes
);
314 buffer
= (char*) free_buffer
+ padding
;
318 buffer
= optional_buffer
;
329 /* attach the region to all applicable access maps */
334 if (mapmask
& (1 << map
))
336 sim_core_map_attach (sd
, &memory
->common
.map
[map
],
337 level
, space
, addr
, nr_bytes
, modulo
,
338 client
, buffer
, free_buffer
);
343 /* Just copy this map to each of the processor specific data structures.
344 FIXME - later this will be replaced by true processor specific
348 for (i
= 0; i
< MAX_NR_PROCESSORS
; i
++)
350 CPU_CORE (STATE_CPU (sd
, i
))->common
= STATE_CORE (sd
)->common
;
357 /* Remove any memory reference related to this address */
358 #if EXTERN_SIM_CORE_P
360 sim_core_map_detach (SIM_DESC sd
,
361 sim_core_map
*access_map
,
366 sim_core_mapping
**entry
;
367 for (entry
= &access_map
->first
;
369 entry
= &(*entry
)->next
)
371 if ((*entry
)->base
== addr
372 && (*entry
)->level
== level
373 && (*entry
)->space
== space
)
375 sim_core_mapping
*dead
= (*entry
);
376 (*entry
) = dead
->next
;
377 if (dead
->free_buffer
!= NULL
)
378 free (dead
->free_buffer
);
386 #if EXTERN_SIM_CORE_P
388 sim_core_detach (SIM_DESC sd
,
394 sim_core
*memory
= STATE_CORE (sd
);
396 for (map
= 0; map
< nr_maps
; map
++)
398 sim_core_map_detach (sd
, &memory
->common
.map
[map
],
399 level
, address_space
, addr
);
401 /* Just copy this update to each of the processor specific data
402 structures. FIXME - later this will be replaced by true
403 processor specific maps. */
406 for (i
= 0; i
< MAX_NR_PROCESSORS
; i
++)
408 CPU_CORE (STATE_CPU (sd
, i
))->common
= STATE_CORE (sd
)->common
;
415 STATIC_INLINE_SIM_CORE\
417 sim_core_find_mapping (sim_core_common
*core
,
421 transfer_type transfer
,
422 int abort
, /*either 0 or 1 - hint to inline/-O */
423 sim_cpu
*cpu
, /* abort => cpu != NULL */
426 sim_core_mapping
*mapping
= core
->map
[map
].first
;
427 ASSERT ((addr
& (nr_bytes
- 1)) == 0); /* must be aligned */
428 ASSERT ((addr
+ (nr_bytes
- 1)) >= addr
); /* must not wrap */
429 ASSERT (!abort
|| cpu
!= NULL
); /* abort needs a non null CPU */
430 while (mapping
!= NULL
)
432 if (addr
>= mapping
->base
433 && (addr
+ (nr_bytes
- 1)) <= mapping
->bound
)
435 mapping
= mapping
->next
;
439 SIM_CORE_SIGNAL (CPU_STATE (cpu
), cpu
, cia
, map
, nr_bytes
, addr
, transfer
,
440 sim_core_unmapped_signal
);
446 STATIC_INLINE_SIM_CORE\
448 sim_core_translate (sim_core_mapping
*mapping
,
451 return (void *)((unsigned8
*) mapping
->buffer
452 + ((addr
- mapping
->base
) & mapping
->mask
));
456 #if EXTERN_SIM_CORE_P
457 /* See include/gdb/remote-sim.h. */
459 sim_memory_map (SIM_DESC sd
)
461 sim_core
*core
= STATE_CORE (sd
);
463 char *s1
, *s2
, *entry
;
466 "<?xml version='1.0'?>\n"
467 "<!DOCTYPE memory-map PUBLIC '+//IDN gnu.org//DTD GDB Memory Map V1.0//EN'"
468 " 'http://sourceware.org/gdb/gdb-memory-map.dtd'>\n"
471 for (map
= 0; map
< nr_maps
; ++map
)
473 sim_core_mapping
*mapping
;
475 for (mapping
= core
->common
.map
[map
].first
;
477 mapping
= mapping
->next
)
479 /* GDB can only handle a single address space. */
480 if (mapping
->level
!= 0)
483 entry
= xasprintf ("<memory type='ram' start='%#" PRIxTW
"' "
484 "length='%#" PRIxTW
"'/>\n",
485 mapping
->base
, mapping
->nr_bytes
);
486 /* The sim memory map is organized by access, not by addresses.
487 So a RWX memory map will have three independent mappings.
488 GDB's format cannot support overlapping regions, so we have
491 Further, GDB can only handle RX ("rom") or RWX ("ram") mappings.
492 We just emit "ram" everywhere to keep it simple. If GDB ever
493 gains support for more stuff, we can expand this.
495 Using strstr is kind of hacky, but as long as the map is not huge
496 (we're talking <10K), should be fine. */
497 if (strstr (s1
, entry
) == NULL
)
499 s2
= concat (s1
, entry
, NULL
);
507 s2
= concat (s1
, "</memory-map>", NULL
);
514 #if EXTERN_SIM_CORE_P
516 sim_core_read_buffer (SIM_DESC sd
,
523 sim_core_common
*core
= (cpu
== NULL
? &STATE_CORE (sd
)->common
: &CPU_CORE (cpu
)->common
);
527 address_word raddr
= addr
+ count
;
528 sim_core_mapping
*mapping
=
529 sim_core_find_mapping (core
, map
,
530 raddr
, /*nr-bytes*/1,
532 0 /*dont-abort*/, NULL
, NULL_CIA
);
536 if (mapping
->device
!= NULL
)
538 int nr_bytes
= len
- count
;
539 if (raddr
+ nr_bytes
- 1> mapping
->bound
)
540 nr_bytes
= mapping
->bound
- raddr
+ 1;
541 /* If the access was initiated by a cpu, pass it down so errors can
542 be propagated properly. For other sources (e.g. GDB or DMA), we
543 can only signal errors via the return value. */
546 sim_cia cia
= cpu
? CPU_PC_GET (cpu
) : NULL_CIA
;
547 sim_cpu_hw_io_read_buffer (cpu
, cia
, mapping
->device
,
548 (unsigned_1
*)buffer
+ count
,
553 else 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 address_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 (mapping
->device
!= NULL
)
596 int nr_bytes
= len
- count
;
597 if (raddr
+ nr_bytes
- 1 > mapping
->bound
)
598 nr_bytes
= mapping
->bound
- raddr
+ 1;
599 /* If the access was initiated by a cpu, pass it down so errors can
600 be propagated properly. For other sources (e.g. GDB or DMA), we
601 can only signal errors via the return value. */
604 sim_cia cia
= cpu
? CPU_PC_GET (cpu
) : NULL_CIA
;
605 sim_cpu_hw_io_write_buffer (cpu
, cia
, mapping
->device
,
606 (unsigned_1
*)buffer
+ count
,
611 else if (sim_hw_io_write_buffer (sd
, mapping
->device
,
612 (unsigned_1
*)buffer
+ count
,
615 nr_bytes
) != nr_bytes
)
621 *(unsigned_1
*)sim_core_translate (mapping
, raddr
) =
622 ((unsigned_1
*)buffer
)[count
];
630 #if EXTERN_SIM_CORE_P
632 sim_core_set_xor (SIM_DESC sd
,
636 /* set up the XOR map if required. */
637 if (WITH_XOR_ENDIAN
) {
639 sim_core
*core
= STATE_CORE (sd
);
640 sim_cpu_core
*cpu_core
= (cpu
!= NULL
? CPU_CORE (cpu
) : NULL
);
641 if (cpu_core
!= NULL
)
646 mask
= WITH_XOR_ENDIAN
- 1;
649 while (i
- 1 < WITH_XOR_ENDIAN
)
651 cpu_core
->byte_xor
[i
-1] = mask
;
652 mask
= (mask
<< 1) & (WITH_XOR_ENDIAN
- 1);
659 core
->byte_xor
= WITH_XOR_ENDIAN
- 1;
667 sim_engine_abort (sd
, NULL
, NULL_CIA
,
668 "Attempted to enable xor-endian mode when permenantly disabled.");
674 #if EXTERN_SIM_CORE_P
676 reverse_n (unsigned_1
*dest
,
677 const unsigned_1
*src
,
681 for (i
= 0; i
< nr_bytes
; i
++)
683 dest
[nr_bytes
- i
- 1] = src
[i
];
689 #if EXTERN_SIM_CORE_P
691 sim_core_xor_read_buffer (SIM_DESC sd
,
698 address_word byte_xor
699 = (cpu
== NULL
? STATE_CORE (sd
)->byte_xor
: CPU_CORE (cpu
)->byte_xor
[0]);
700 if (!WITH_XOR_ENDIAN
|| !byte_xor
)
701 return sim_core_read_buffer (sd
, cpu
, map
, buffer
, addr
, nr_bytes
);
703 /* only break up transfers when xor-endian is both selected and enabled */
705 unsigned_1 x
[WITH_XOR_ENDIAN
+ 1]; /* +1 to avoid zero-sized array */
706 unsigned nr_transfered
= 0;
707 address_word start
= addr
;
708 unsigned nr_this_transfer
= (WITH_XOR_ENDIAN
- (addr
& ~(WITH_XOR_ENDIAN
- 1)));
710 /* initial and intermediate transfers are broken when they cross
711 an XOR endian boundary */
712 while (nr_transfered
+ nr_this_transfer
< nr_bytes
)
713 /* initial/intermediate transfers */
715 /* since xor-endian is enabled stop^xor defines the start
716 address of the transfer */
717 stop
= start
+ nr_this_transfer
- 1;
718 SIM_ASSERT (start
<= stop
);
719 SIM_ASSERT ((stop
^ byte_xor
) <= (start
^ byte_xor
));
720 if (sim_core_read_buffer (sd
, cpu
, map
, x
, stop
^ byte_xor
, nr_this_transfer
)
722 return nr_transfered
;
723 reverse_n (&((unsigned_1
*)buffer
)[nr_transfered
], x
, nr_this_transfer
);
724 nr_transfered
+= nr_this_transfer
;
725 nr_this_transfer
= WITH_XOR_ENDIAN
;
729 nr_this_transfer
= nr_bytes
- nr_transfered
;
730 stop
= start
+ nr_this_transfer
- 1;
731 SIM_ASSERT (stop
== (addr
+ nr_bytes
- 1));
732 if (sim_core_read_buffer (sd
, cpu
, map
, x
, stop
^ byte_xor
, nr_this_transfer
)
734 return nr_transfered
;
735 reverse_n (&((unsigned_1
*)buffer
)[nr_transfered
], x
, nr_this_transfer
);
742 #if EXTERN_SIM_CORE_P
744 sim_core_xor_write_buffer (SIM_DESC sd
,
751 address_word byte_xor
752 = (cpu
== NULL
? STATE_CORE (sd
)->byte_xor
: CPU_CORE (cpu
)->byte_xor
[0]);
753 if (!WITH_XOR_ENDIAN
|| !byte_xor
)
754 return sim_core_write_buffer (sd
, cpu
, map
, buffer
, addr
, nr_bytes
);
756 /* only break up transfers when xor-endian is both selected and enabled */
758 unsigned_1 x
[WITH_XOR_ENDIAN
+ 1]; /* +1 to avoid zero sized array */
759 unsigned nr_transfered
= 0;
760 address_word start
= addr
;
761 unsigned nr_this_transfer
= (WITH_XOR_ENDIAN
- (addr
& ~(WITH_XOR_ENDIAN
- 1)));
763 /* initial and intermediate transfers are broken when they cross
764 an XOR endian boundary */
765 while (nr_transfered
+ nr_this_transfer
< nr_bytes
)
766 /* initial/intermediate transfers */
768 /* since xor-endian is enabled stop^xor defines the start
769 address of the transfer */
770 stop
= start
+ nr_this_transfer
- 1;
771 SIM_ASSERT (start
<= stop
);
772 SIM_ASSERT ((stop
^ byte_xor
) <= (start
^ byte_xor
));
773 reverse_n (x
, &((unsigned_1
*)buffer
)[nr_transfered
], nr_this_transfer
);
774 if (sim_core_read_buffer (sd
, cpu
, map
, x
, stop
^ byte_xor
, nr_this_transfer
)
776 return nr_transfered
;
777 nr_transfered
+= nr_this_transfer
;
778 nr_this_transfer
= WITH_XOR_ENDIAN
;
782 nr_this_transfer
= nr_bytes
- nr_transfered
;
783 stop
= start
+ nr_this_transfer
- 1;
784 SIM_ASSERT (stop
== (addr
+ nr_bytes
- 1));
785 reverse_n (x
, &((unsigned_1
*)buffer
)[nr_transfered
], nr_this_transfer
);
786 if (sim_core_read_buffer (sd
, cpu
, map
, x
, stop
^ byte_xor
, nr_this_transfer
)
788 return nr_transfered
;
794 #if EXTERN_SIM_CORE_P
796 sim_core_trans_addr (SIM_DESC sd
,
801 sim_core_common
*core
= (cpu
== NULL
? &STATE_CORE (sd
)->common
: &CPU_CORE (cpu
)->common
);
802 sim_core_mapping
*mapping
=
803 sim_core_find_mapping (core
, map
,
806 0 /*dont-abort*/, NULL
, NULL_CIA
);
809 return sim_core_translate (mapping
, addr
);
815 /* define the read/write 1/2/4/8/16/word functions */
818 #include "sim-n-core.h"
821 #include "sim-n-core.h"
825 #include "sim-n-core.h"
829 #include "sim-n-core.h"
833 #include "sim-n-core.h"
836 #include "sim-n-core.h"
840 #include "sim-n-core.h"
843 #include "sim-n-core.h"
846 #include "sim-n-core.h"