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/>. */
26 /* This must come before any other includes. */
30 #include "sim-assert.h"
31 #include "sim-signal.h"
32 #include "libiberty.h"
40 /* "core" module install handler.
42 This is called via sim_module_install to install the "core"
43 subsystem into the simulator. */
46 static MODULE_INIT_FN sim_core_init
;
47 static MODULE_UNINSTALL_FN sim_core_uninstall
;
52 sim_core_install (SIM_DESC sd
)
54 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
56 /* establish the other handlers */
57 sim_module_add_uninstall_fn (sd
, sim_core_uninstall
);
58 sim_module_add_init_fn (sd
, sim_core_init
);
60 /* establish any initial data structures - none */
66 /* Uninstall the "core" subsystem from the simulator. */
70 sim_core_uninstall (SIM_DESC sd
)
72 sim_core
*core
= STATE_CORE (sd
);
74 /* blow away any mappings */
75 for (map
= 0; map
< nr_maps
; map
++) {
76 sim_core_mapping
*curr
= core
->common
.map
[map
].first
;
77 while (curr
!= NULL
) {
78 sim_core_mapping
*tbd
= curr
;
80 if (tbd
->free_buffer
!= NULL
) {
81 SIM_ASSERT (tbd
->buffer
!= NULL
);
82 free (tbd
->free_buffer
);
86 core
->common
.map
[map
].first
= NULL
;
94 sim_core_init (SIM_DESC sd
)
103 #ifndef SIM_CORE_SIGNAL
104 #define SIM_CORE_SIGNAL(SD,CPU,CIA,MAP,NR_BYTES,ADDR,TRANSFER,ERROR) \
105 sim_core_signal ((SD), (CPU), (CIA), (MAP), (NR_BYTES), (ADDR), (TRANSFER), (ERROR))
108 #if EXTERN_SIM_CORE_P
110 sim_core_signal (SIM_DESC sd
,
116 transfer_type transfer
,
117 sim_core_signals sig
)
119 const char *copy
= (transfer
== read_transfer
? "read" : "write");
120 address_word ip
= CIA_ADDR (cia
);
123 case sim_core_unmapped_signal
:
124 sim_io_eprintf (sd
, "core: %d byte %s to unmapped 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_SIGSEGV
);
128 case sim_core_unaligned_signal
:
129 sim_io_eprintf (sd
, "core: %d byte misaligned %s to address 0x%lx at 0x%lx\n",
130 nr_bytes
, copy
, (unsigned long) addr
, (unsigned long) ip
);
131 sim_engine_halt (sd
, cpu
, NULL
, cia
, sim_stopped
, SIM_SIGBUS
);
134 sim_engine_abort (sd
, cpu
, cia
,
135 "sim_core_signal - internal error - bad switch");
141 #if EXTERN_SIM_CORE_P
142 static sim_core_mapping
*
143 new_sim_core_mapping (SIM_DESC sd
,
147 address_word nr_bytes
,
153 sim_core_mapping
*new_mapping
= ZALLOC (sim_core_mapping
);
155 new_mapping
->level
= level
;
156 new_mapping
->space
= space
;
157 new_mapping
->base
= addr
;
158 new_mapping
->nr_bytes
= nr_bytes
;
159 new_mapping
->bound
= addr
+ (nr_bytes
- 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
,
178 struct hw
*client
, /*callback/default*/
179 void *buffer
, /*raw_memory*/
180 void *free_buffer
) /*raw_memory*/
182 /* find the insertion point for this additional mapping and then
184 sim_core_mapping
*next_mapping
;
185 sim_core_mapping
**last_mapping
;
187 SIM_ASSERT ((client
== NULL
) != (buffer
== NULL
));
188 SIM_ASSERT ((client
== NULL
) >= (free_buffer
!= NULL
));
190 /* actually do occasionally get a zero size map */
194 sim_hw_abort (sd
, client
, "called on sim_core_map_attach with size zero");
196 sim_io_error (sd
, "called on sim_core_map_attach with size zero");
199 /* find the insertion point (between last/next) */
200 next_mapping
= access_map
->first
;
201 last_mapping
= &access_map
->first
;
202 while (next_mapping
!= NULL
203 && (next_mapping
->level
< level
204 || (next_mapping
->level
== level
205 && next_mapping
->bound
< addr
)))
207 /* provided levels are the same */
208 /* assert: next_mapping->base > all bases before next_mapping */
209 /* assert: next_mapping->bound >= all bounds before next_mapping */
210 last_mapping
= &next_mapping
->next
;
211 next_mapping
= next_mapping
->next
;
214 /* check insertion point correct */
215 SIM_ASSERT (next_mapping
== NULL
|| next_mapping
->level
>= level
);
216 if (next_mapping
!= NULL
&& next_mapping
->level
== level
217 && next_mapping
->base
< (addr
+ (nr_bytes
- 1)))
220 sim_hw_abort (sd
, client
, "memory map %d:0x%lx..0x%lx (%ld bytes) overlaps %d:0x%lx..0x%lx (%ld bytes)",
223 (long) (addr
+ (nr_bytes
- 1)),
226 (long) next_mapping
->base
,
227 (long) next_mapping
->bound
,
228 (long) next_mapping
->nr_bytes
);
230 sim_io_error (sd
, "memory map %d:0x%lx..0x%lx (%ld bytes) overlaps %d:0x%lx..0x%lx (%ld bytes)",
233 (long) (addr
+ (nr_bytes
- 1)),
236 (long) next_mapping
->base
,
237 (long) next_mapping
->bound
,
238 (long) next_mapping
->nr_bytes
);
241 /* create/insert the new mapping */
242 *last_mapping
= new_sim_core_mapping (sd
,
244 space
, addr
, nr_bytes
, modulo
,
245 client
, buffer
, free_buffer
);
246 (*last_mapping
)->next
= next_mapping
;
251 /* Attach memory or a memory mapped device to the simulator.
252 See sim-core.h for a full description. */
254 #if EXTERN_SIM_CORE_P
256 sim_core_attach (SIM_DESC sd
,
262 address_word nr_bytes
,
265 void *optional_buffer
)
267 sim_core
*memory
= STATE_CORE (sd
);
272 /* check for for attempt to use unimplemented per-processor core map */
274 sim_io_error (sd
, "sim_core_map_attach - processor specific memory map not yet supported");
276 if (client
!= NULL
&& modulo
!= 0)
279 sim_hw_abort (sd
, client
, "sim_core_attach - internal error - modulo and callback memory conflict");
281 sim_io_error (sd
, "sim_core_attach - internal error - modulo and callback memory conflict");
285 unsigned mask
= modulo
- 1;
287 while (mask
>= sizeof (unsigned64
)) /* minimum modulo */
294 if (mask
!= sizeof (unsigned64
) - 1)
297 sim_hw_abort (sd
, client
, "sim_core_attach - internal error - modulo %lx not power of two", (long) modulo
);
299 sim_io_error (sd
, "sim_core_attach - internal error - modulo %lx not power of two", (long) modulo
);
303 /* verify consistency between device and buffer */
304 if (client
!= NULL
&& optional_buffer
!= NULL
)
307 sim_hw_abort (sd
, client
, "sim_core_attach - internal error - conflicting buffer and attach arguments");
309 sim_io_error (sd
, "sim_core_attach - internal error - conflicting buffer and attach arguments");
313 if (optional_buffer
== NULL
)
315 int padding
= (addr
% sizeof (unsigned64
));
316 unsigned long bytes
= (modulo
== 0 ? nr_bytes
: modulo
) + padding
;
317 free_buffer
= zalloc (bytes
);
318 buffer
= (char*) free_buffer
+ padding
;
322 buffer
= optional_buffer
;
333 /* attach the region to all applicable access maps */
338 if (mapmask
& (1 << map
))
340 sim_core_map_attach (sd
, &memory
->common
.map
[map
],
341 level
, space
, addr
, nr_bytes
, modulo
,
342 client
, buffer
, free_buffer
);
347 /* Just copy this map to each of the processor specific data structures.
348 FIXME - later this will be replaced by true processor specific
352 for (i
= 0; i
< MAX_NR_PROCESSORS
; i
++)
354 CPU_CORE (STATE_CPU (sd
, i
))->common
= STATE_CORE (sd
)->common
;
361 /* Remove any memory reference related to this address */
362 #if EXTERN_SIM_CORE_P
364 sim_core_map_detach (SIM_DESC sd
,
365 sim_core_map
*access_map
,
370 sim_core_mapping
**entry
;
371 for (entry
= &access_map
->first
;
373 entry
= &(*entry
)->next
)
375 if ((*entry
)->base
== addr
376 && (*entry
)->level
== level
377 && (*entry
)->space
== space
)
379 sim_core_mapping
*dead
= (*entry
);
380 (*entry
) = dead
->next
;
381 if (dead
->free_buffer
!= NULL
)
382 free (dead
->free_buffer
);
390 #if EXTERN_SIM_CORE_P
392 sim_core_detach (SIM_DESC sd
,
398 sim_core
*memory
= STATE_CORE (sd
);
400 for (map
= 0; map
< nr_maps
; map
++)
402 sim_core_map_detach (sd
, &memory
->common
.map
[map
],
403 level
, address_space
, addr
);
405 /* Just copy this update to each of the processor specific data
406 structures. FIXME - later this will be replaced by true
407 processor specific maps. */
410 for (i
= 0; i
< MAX_NR_PROCESSORS
; i
++)
412 CPU_CORE (STATE_CPU (sd
, i
))->common
= STATE_CORE (sd
)->common
;
419 STATIC_INLINE_SIM_CORE\
421 sim_core_find_mapping (sim_core_common
*core
,
425 transfer_type transfer
,
426 int abort
, /*either 0 or 1 - hint to inline/-O */
427 sim_cpu
*cpu
, /* abort => cpu != NULL */
430 sim_core_mapping
*mapping
= core
->map
[map
].first
;
431 ASSERT ((addr
& (nr_bytes
- 1)) == 0); /* must be aligned */
432 ASSERT ((addr
+ (nr_bytes
- 1)) >= addr
); /* must not wrap */
433 ASSERT (!abort
|| cpu
!= NULL
); /* abort needs a non null CPU */
434 while (mapping
!= NULL
)
436 if (addr
>= mapping
->base
437 && (addr
+ (nr_bytes
- 1)) <= mapping
->bound
)
439 mapping
= mapping
->next
;
443 SIM_CORE_SIGNAL (CPU_STATE (cpu
), cpu
, cia
, map
, nr_bytes
, addr
, transfer
,
444 sim_core_unmapped_signal
);
450 STATIC_INLINE_SIM_CORE\
452 sim_core_translate (sim_core_mapping
*mapping
,
455 return (void *)((unsigned8
*) mapping
->buffer
456 + ((addr
- mapping
->base
) & mapping
->mask
));
460 #if EXTERN_SIM_CORE_P
461 /* See include/sim/sim.h. */
463 sim_memory_map (SIM_DESC sd
)
465 sim_core
*core
= STATE_CORE (sd
);
467 char *s1
, *s2
, *entry
;
470 "<?xml version='1.0'?>\n"
471 "<!DOCTYPE memory-map PUBLIC '+//IDN gnu.org//DTD GDB Memory Map V1.0//EN'"
472 " 'http://sourceware.org/gdb/gdb-memory-map.dtd'>\n"
475 for (map
= 0; map
< nr_maps
; ++map
)
477 sim_core_mapping
*mapping
;
479 for (mapping
= core
->common
.map
[map
].first
;
481 mapping
= mapping
->next
)
483 /* GDB can only handle a single address space. */
484 if (mapping
->level
!= 0)
487 entry
= xasprintf ("<memory type='ram' start='%#" PRIxTW
"' "
488 "length='%#" PRIxTW
"'/>\n",
489 mapping
->base
, mapping
->nr_bytes
);
490 /* The sim memory map is organized by access, not by addresses.
491 So a RWX memory map will have three independent mappings.
492 GDB's format cannot support overlapping regions, so we have
495 Further, GDB can only handle RX ("rom") or RWX ("ram") mappings.
496 We just emit "ram" everywhere to keep it simple. If GDB ever
497 gains support for more stuff, we can expand this.
499 Using strstr is kind of hacky, but as long as the map is not huge
500 (we're talking <10K), should be fine. */
501 if (strstr (s1
, entry
) == NULL
)
503 s2
= concat (s1
, entry
, NULL
);
511 s2
= concat (s1
, "</memory-map>", NULL
);
518 #if EXTERN_SIM_CORE_P
520 sim_core_read_buffer (SIM_DESC sd
,
527 sim_core_common
*core
= (cpu
== NULL
? &STATE_CORE (sd
)->common
: &CPU_CORE (cpu
)->common
);
531 address_word raddr
= addr
+ count
;
532 sim_core_mapping
*mapping
=
533 sim_core_find_mapping (core
, map
,
534 raddr
, /*nr-bytes*/1,
536 0 /*dont-abort*/, NULL
, NULL_CIA
);
540 if (mapping
->device
!= NULL
)
542 int nr_bytes
= len
- count
;
543 if (raddr
+ nr_bytes
- 1> mapping
->bound
)
544 nr_bytes
= mapping
->bound
- raddr
+ 1;
545 /* If the access was initiated by a cpu, pass it down so errors can
546 be propagated properly. For other sources (e.g. GDB or DMA), we
547 can only signal errors via the return value. */
550 sim_cia cia
= cpu
? CPU_PC_GET (cpu
) : NULL_CIA
;
551 sim_cpu_hw_io_read_buffer (cpu
, cia
, mapping
->device
,
552 (unsigned_1
*)buffer
+ count
,
557 else if (sim_hw_io_read_buffer (sd
, mapping
->device
,
558 (unsigned_1
*)buffer
+ count
,
561 nr_bytes
) != nr_bytes
)
567 ((unsigned_1
*)buffer
)[count
] =
568 *(unsigned_1
*)sim_core_translate (mapping
, raddr
);
576 #if EXTERN_SIM_CORE_P
578 sim_core_write_buffer (SIM_DESC sd
,
585 sim_core_common
*core
= (cpu
== NULL
? &STATE_CORE (sd
)->common
: &CPU_CORE (cpu
)->common
);
589 address_word raddr
= addr
+ count
;
590 sim_core_mapping
*mapping
=
591 sim_core_find_mapping (core
, map
,
592 raddr
, /*nr-bytes*/1,
594 0 /*dont-abort*/, NULL
, NULL_CIA
);
598 if (mapping
->device
!= NULL
)
600 int nr_bytes
= len
- count
;
601 if (raddr
+ nr_bytes
- 1 > mapping
->bound
)
602 nr_bytes
= mapping
->bound
- raddr
+ 1;
603 /* If the access was initiated by a cpu, pass it down so errors can
604 be propagated properly. For other sources (e.g. GDB or DMA), we
605 can only signal errors via the return value. */
608 sim_cia cia
= cpu
? CPU_PC_GET (cpu
) : NULL_CIA
;
609 sim_cpu_hw_io_write_buffer (cpu
, cia
, mapping
->device
,
610 (unsigned_1
*)buffer
+ count
,
615 else if (sim_hw_io_write_buffer (sd
, mapping
->device
,
616 (unsigned_1
*)buffer
+ count
,
619 nr_bytes
) != nr_bytes
)
625 *(unsigned_1
*)sim_core_translate (mapping
, raddr
) =
626 ((unsigned_1
*)buffer
)[count
];
634 #if EXTERN_SIM_CORE_P
636 sim_core_set_xor (SIM_DESC sd
,
640 /* set up the XOR map if required. */
641 if (WITH_XOR_ENDIAN
) {
643 sim_core
*core
= STATE_CORE (sd
);
644 sim_cpu_core
*cpu_core
= (cpu
!= NULL
? CPU_CORE (cpu
) : NULL
);
645 if (cpu_core
!= NULL
)
650 mask
= WITH_XOR_ENDIAN
- 1;
653 while (i
- 1 < WITH_XOR_ENDIAN
)
655 cpu_core
->byte_xor
[i
-1] = mask
;
656 mask
= (mask
<< 1) & (WITH_XOR_ENDIAN
- 1);
663 core
->byte_xor
= WITH_XOR_ENDIAN
- 1;
671 sim_engine_abort (sd
, NULL
, NULL_CIA
,
672 "Attempted to enable xor-endian mode when permenantly disabled.");
678 #if EXTERN_SIM_CORE_P
680 reverse_n (unsigned_1
*dest
,
681 const unsigned_1
*src
,
685 for (i
= 0; i
< nr_bytes
; i
++)
687 dest
[nr_bytes
- i
- 1] = src
[i
];
693 #if EXTERN_SIM_CORE_P
695 sim_core_xor_read_buffer (SIM_DESC sd
,
702 address_word byte_xor
703 = (cpu
== NULL
? STATE_CORE (sd
)->byte_xor
: CPU_CORE (cpu
)->byte_xor
[0]);
704 if (!WITH_XOR_ENDIAN
|| !byte_xor
)
705 return sim_core_read_buffer (sd
, cpu
, map
, buffer
, addr
, nr_bytes
);
707 /* only break up transfers when xor-endian is both selected and enabled */
709 unsigned_1 x
[WITH_XOR_ENDIAN
+ 1]; /* +1 to avoid zero-sized array */
710 unsigned nr_transfered
= 0;
711 address_word start
= addr
;
712 unsigned nr_this_transfer
= (WITH_XOR_ENDIAN
- (addr
& ~(WITH_XOR_ENDIAN
- 1)));
714 /* initial and intermediate transfers are broken when they cross
715 an XOR endian boundary */
716 while (nr_transfered
+ nr_this_transfer
< nr_bytes
)
717 /* initial/intermediate transfers */
719 /* since xor-endian is enabled stop^xor defines the start
720 address of the transfer */
721 stop
= start
+ nr_this_transfer
- 1;
722 SIM_ASSERT (start
<= stop
);
723 SIM_ASSERT ((stop
^ byte_xor
) <= (start
^ byte_xor
));
724 if (sim_core_read_buffer (sd
, cpu
, map
, x
, stop
^ byte_xor
, nr_this_transfer
)
726 return nr_transfered
;
727 reverse_n (&((unsigned_1
*)buffer
)[nr_transfered
], x
, nr_this_transfer
);
728 nr_transfered
+= nr_this_transfer
;
729 nr_this_transfer
= WITH_XOR_ENDIAN
;
733 nr_this_transfer
= nr_bytes
- nr_transfered
;
734 stop
= start
+ nr_this_transfer
- 1;
735 SIM_ASSERT (stop
== (addr
+ nr_bytes
- 1));
736 if (sim_core_read_buffer (sd
, cpu
, map
, x
, stop
^ byte_xor
, nr_this_transfer
)
738 return nr_transfered
;
739 reverse_n (&((unsigned_1
*)buffer
)[nr_transfered
], x
, nr_this_transfer
);
746 #if EXTERN_SIM_CORE_P
748 sim_core_xor_write_buffer (SIM_DESC sd
,
755 address_word byte_xor
756 = (cpu
== NULL
? STATE_CORE (sd
)->byte_xor
: CPU_CORE (cpu
)->byte_xor
[0]);
757 if (!WITH_XOR_ENDIAN
|| !byte_xor
)
758 return sim_core_write_buffer (sd
, cpu
, map
, buffer
, addr
, nr_bytes
);
760 /* only break up transfers when xor-endian is both selected and enabled */
762 unsigned_1 x
[WITH_XOR_ENDIAN
+ 1]; /* +1 to avoid zero sized array */
763 unsigned nr_transfered
= 0;
764 address_word start
= addr
;
765 unsigned nr_this_transfer
= (WITH_XOR_ENDIAN
- (addr
& ~(WITH_XOR_ENDIAN
- 1)));
767 /* initial and intermediate transfers are broken when they cross
768 an XOR endian boundary */
769 while (nr_transfered
+ nr_this_transfer
< nr_bytes
)
770 /* initial/intermediate transfers */
772 /* since xor-endian is enabled stop^xor defines the start
773 address of the transfer */
774 stop
= start
+ nr_this_transfer
- 1;
775 SIM_ASSERT (start
<= stop
);
776 SIM_ASSERT ((stop
^ byte_xor
) <= (start
^ byte_xor
));
777 reverse_n (x
, &((unsigned_1
*)buffer
)[nr_transfered
], nr_this_transfer
);
778 if (sim_core_read_buffer (sd
, cpu
, map
, x
, stop
^ byte_xor
, nr_this_transfer
)
780 return nr_transfered
;
781 nr_transfered
+= nr_this_transfer
;
782 nr_this_transfer
= WITH_XOR_ENDIAN
;
786 nr_this_transfer
= nr_bytes
- nr_transfered
;
787 stop
= start
+ nr_this_transfer
- 1;
788 SIM_ASSERT (stop
== (addr
+ nr_bytes
- 1));
789 reverse_n (x
, &((unsigned_1
*)buffer
)[nr_transfered
], nr_this_transfer
);
790 if (sim_core_read_buffer (sd
, cpu
, map
, x
, stop
^ byte_xor
, nr_this_transfer
)
792 return nr_transfered
;
798 #if EXTERN_SIM_CORE_P
800 sim_core_trans_addr (SIM_DESC sd
,
805 sim_core_common
*core
= (cpu
== NULL
? &STATE_CORE (sd
)->common
: &CPU_CORE (cpu
)->common
);
806 sim_core_mapping
*mapping
=
807 sim_core_find_mapping (core
, map
,
810 0 /*dont-abort*/, NULL
, NULL_CIA
);
813 return sim_core_translate (mapping
, addr
);
819 /* define the read/write 1/2/4/8/16/word functions */
822 #include "sim-n-core.h"
825 #include "sim-n-core.h"
829 #include "sim-n-core.h"
833 #include "sim-n-core.h"
837 #include "sim-n-core.h"
840 #include "sim-n-core.h"
844 #include "sim-n-core.h"
847 #include "sim-n-core.h"
850 #include "sim-n-core.h"