1 /* This file is part of the program psim.
3 Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include "sim-assert.h"
28 /* "core" module install handler.
30 This is called via sim_module_install to install the "core" subsystem
31 into the simulator. */
33 static MODULE_INIT_FN sim_core_init
;
34 static MODULE_UNINSTALL_FN sim_core_uninstall
;
38 sim_core_install (SIM_DESC sd
)
40 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
42 /* establish the other handlers */
43 sim_module_add_uninstall_fn (sd
, sim_core_uninstall
);
44 sim_module_add_init_fn (sd
, sim_core_init
);
46 /* establish any initial data structures - none */
51 /* Uninstall the "core" subsystem from the simulator. */
55 sim_core_uninstall (SIM_DESC sd
)
57 sim_core
*core
= STATE_CORE(sd
);
59 /* blow away any mappings */
60 for (map
= 0; map
< nr_sim_core_maps
; map
++) {
61 sim_core_mapping
*curr
= core
->common
.map
[map
].first
;
62 while (curr
!= NULL
) {
63 sim_core_mapping
*tbd
= curr
;
65 if (tbd
->free_buffer
!= NULL
) {
66 SIM_ASSERT(tbd
->buffer
!= NULL
);
67 zfree(tbd
->free_buffer
);
71 core
->common
.map
[map
].first
= NULL
;
78 sim_core_init (SIM_DESC sd
)
86 #ifndef SIM_CORE_SIGNAL
87 #define SIM_CORE_SIGNAL(SD,CPU,CIA,MAP,NR_BYTES,ADDR,TRANSFER,ERROR) \
88 sim_core_signal ((SD), (CPU), (CIA), (MAP), (NR_BYTES), (ADDR), (TRANSFER), (ERROR))
92 sim_core_signal (SIM_DESC sd
,
98 transfer_type transfer
,
101 const char *copy
= (transfer
== read_transfer
? "read" : "write");
102 address_word ip
= CIA_ADDR (cia
);
105 case sim_core_unmapped_signal
:
106 sim_io_eprintf (sd
, "core: %d byte %s to unmaped address 0x%lx at 0x%lx\n",
107 nr_bytes
, copy
, (unsigned long) addr
, (unsigned long) ip
);
108 sim_engine_halt (sd
, cpu
, NULL
, cia
, sim_signalled
, SIM_SIGSEGV
);
110 case sim_core_unaligned_signal
:
111 sim_io_eprintf (sd
, "core: %d byte misaligned %s to address 0x%lx at 0x%lx\n",
112 nr_bytes
, copy
, (unsigned long) addr
, (unsigned long) ip
);
113 sim_engine_halt (sd
, cpu
, NULL
, cia
, sim_signalled
, SIM_SIGBUS
);
116 sim_engine_abort (sd
, cpu
, cia
,
117 "sim_core_signal - internal error - bad switch");
123 STATIC_INLINE_SIM_CORE\
125 sim_core_map_to_str (sim_core_maps map
)
129 case sim_core_read_map
: return "read";
130 case sim_core_write_map
: return "write";
131 case sim_core_execute_map
: return "exec";
132 default: return "(invalid-map)";
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);
157 new_mapping
->mask
= (unsigned) 0 - 1;
159 new_mapping
->mask
= modulo
- 1;
160 new_mapping
->buffer
= buffer
;
161 new_mapping
->free_buffer
= free_buffer
;
162 new_mapping
->device
= device
;
169 sim_core_map_attach (SIM_DESC sd
,
170 sim_core_map
*access_map
,
174 address_word nr_bytes
,
176 device
*client
, /*callback/default*/
177 void *buffer
, /*raw_memory*/
178 void *free_buffer
) /*raw_memory*/
180 /* find the insertion point for this additional mapping and then
182 sim_core_mapping
*next_mapping
;
183 sim_core_mapping
**last_mapping
;
185 SIM_ASSERT ((client
== NULL
) != (buffer
== NULL
));
186 SIM_ASSERT ((client
== NULL
) >= (free_buffer
!= NULL
));
188 /* actually do occasionally get a zero size map */
192 device_error(client
, "called on sim_core_map_attach with size zero");
194 sim_io_error (sd
, "called on sim_core_map_attach with size zero");
198 /* find the insertion point (between last/next) */
199 next_mapping
= access_map
->first
;
200 last_mapping
= &access_map
->first
;
201 while(next_mapping
!= NULL
202 && (next_mapping
->level
< level
203 || (next_mapping
->level
== level
204 && next_mapping
->bound
< addr
)))
206 /* provided levels are the same */
207 /* assert: next_mapping->base > all bases before next_mapping */
208 /* assert: next_mapping->bound >= all bounds before next_mapping */
209 last_mapping
= &next_mapping
->next
;
210 next_mapping
= next_mapping
->next
;
213 /* check insertion point correct */
214 SIM_ASSERT (next_mapping
== NULL
|| next_mapping
->level
>= level
);
215 if (next_mapping
!= NULL
&& next_mapping
->level
== level
216 && next_mapping
->base
< (addr
+ (nr_bytes
- 1)))
219 device_error (client
, "memory map %d:0x%lx..0x%lx (%ld bytes) overlaps %d:0x%lx..0x%lx (%ld bytes)",
223 (long) (addr
+ (nr_bytes
- 1)),
225 (long) next_mapping
->base
,
226 (long) next_mapping
->bound
,
227 (long) next_mapping
->nr_bytes
);
229 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)),
235 (long) next_mapping
->base
,
236 (long) next_mapping
->bound
,
237 (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
;
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 ((access
& access_read_write_exec
) == 0
273 || (access
& ~access_read_write_exec
) != 0)
276 device_error(client
, "invalid access for core attach");
278 sim_io_error (sd
, "invalid access for core attach");
282 /* verify modulo memory */
283 if (!WITH_MODULO_MEMORY
&& modulo
!= 0)
286 device_error (client
, "sim_core_attach - internal error - modulo memory disabled");
288 sim_io_error (sd
, "sim_core_attach - internal error - modulo memory disabled");
291 if (client
!= NULL
&& modulo
!= 0)
294 device_error (client
, "sim_core_attach - internal error - modulo and callback memory conflict");
296 sim_io_error (sd
, "sim_core_attach - internal error - modulo and callback memory conflict");
301 unsigned mask
= modulo
- 1;
303 while (mask
>= sizeof (unsigned64
)) /* minimum modulo */
310 if (mask
!= sizeof (unsigned64
) - 1)
313 device_error (client
, "sim_core_attach - internal error - modulo %lx not power of two", (long) modulo
);
315 sim_io_error (sd
, "sim_core_attach - internal error - modulo %lx not power of two", (long) modulo
);
320 /* verify consistency between device and buffer */
321 if (client
!= NULL
&& optional_buffer
!= NULL
)
324 device_error (client
, "sim_core_attach - internal error - conflicting buffer and attach arguments");
326 sim_io_error (sd
, "sim_core_attach - internal error - conflicting buffer and attach arguments");
331 if (optional_buffer
== NULL
)
333 int padding
= (addr
% sizeof (unsigned64
));
334 free_buffer
= zalloc ((modulo
== 0 ? nr_bytes
: modulo
) + padding
);
335 buffer
= (char*) free_buffer
+ padding
;
339 buffer
= optional_buffer
;
350 /* attach the region to all applicable access maps */
352 map
< nr_sim_core_maps
;
357 case sim_core_read_map
:
358 if (access
& access_read
)
359 sim_core_map_attach (sd
, &memory
->common
.map
[map
],
360 level
, space
, addr
, nr_bytes
, modulo
,
361 client
, buffer
, free_buffer
);
364 case sim_core_write_map
:
365 if (access
& access_write
)
366 sim_core_map_attach (sd
, &memory
->common
.map
[map
],
367 level
, space
, addr
, nr_bytes
, modulo
,
368 client
, buffer
, free_buffer
);
371 case sim_core_execute_map
:
372 if (access
& access_exec
)
373 sim_core_map_attach (sd
, &memory
->common
.map
[map
],
374 level
, space
, addr
, nr_bytes
, modulo
,
375 client
, buffer
, free_buffer
);
378 case nr_sim_core_maps
:
379 sim_io_error (sd
, "sim_core_attach - internal error - bad switch");
384 /* Just copy this map to each of the processor specific data structures.
385 FIXME - later this will be replaced by true processor specific
389 for (i
= 0; i
< MAX_NR_PROCESSORS
; i
++)
391 CPU_CORE (STATE_CPU (sd
, i
))->common
= STATE_CORE (sd
)->common
;
397 /* Remove any memory reference related to this address */
398 STATIC_INLINE_SIM_CORE\
400 sim_core_map_detach (SIM_DESC sd
,
401 sim_core_map
*access_map
,
406 sim_core_mapping
**entry
;
407 for (entry
= &access_map
->first
;
409 entry
= &(*entry
)->next
)
411 if ((*entry
)->base
== addr
412 && (*entry
)->level
== level
413 && (*entry
)->space
== space
)
415 sim_core_mapping
*dead
= (*entry
);
416 (*entry
) = dead
->next
;
417 if (dead
->free_buffer
!= NULL
)
418 zfree (dead
->free_buffer
);
427 sim_core_detach (SIM_DESC sd
,
433 sim_core
*memory
= STATE_CORE (sd
);
435 for (map
= 0; map
< nr_sim_core_maps
; map
++)
437 sim_core_map_detach (sd
, &memory
->common
.map
[map
],
438 level
, address_space
, addr
);
440 /* Just copy this update to each of the processor specific data
441 structures. FIXME - later this will be replaced by true
442 processor specific maps. */
445 for (i
= 0; i
< MAX_NR_PROCESSORS
; i
++)
447 CPU_CORE (STATE_CPU (sd
, i
))->common
= STATE_CORE (sd
)->common
;
453 STATIC_INLINE_SIM_CORE\
455 sim_core_find_mapping(sim_core_common
*core
,
459 transfer_type transfer
,
460 int abort
, /*either 0 or 1 - hint to inline/-O */
461 sim_cpu
*cpu
, /* abort => cpu != NULL */
464 sim_core_mapping
*mapping
= core
->map
[map
].first
;
465 ASSERT ((addr
& (nr_bytes
- 1)) == 0); /* must be aligned */
466 ASSERT ((addr
+ (nr_bytes
- 1)) >= addr
); /* must not wrap */
467 ASSERT (!abort
|| cpu
!= NULL
); /* abort needs a non null CPU */
468 while (mapping
!= NULL
)
470 if (addr
>= mapping
->base
471 && (addr
+ (nr_bytes
- 1)) <= mapping
->bound
)
473 mapping
= mapping
->next
;
477 SIM_CORE_SIGNAL (CPU_STATE (cpu
), cpu
, cia
, map
, nr_bytes
, addr
, transfer
,
478 sim_core_unmapped_signal
);
484 STATIC_INLINE_SIM_CORE\
486 sim_core_translate (sim_core_mapping
*mapping
,
489 if (WITH_MODULO_MEMORY
)
490 return (void *)((unsigned8
*) mapping
->buffer
491 + ((addr
- mapping
->base
) & mapping
->mask
));
493 return (void *)((unsigned8
*) mapping
->buffer
494 + addr
- mapping
->base
);
500 sim_core_read_buffer (SIM_DESC sd
,
507 sim_core_common
*core
= (cpu
== NULL
? &STATE_CORE (sd
)->common
: &CPU_CORE (cpu
)->common
);
509 while (count
< len
) {
510 unsigned_word raddr
= addr
+ count
;
511 sim_core_mapping
*mapping
=
512 sim_core_find_mapping(core
, map
,
513 raddr
, /*nr-bytes*/1,
515 0 /*dont-abort*/, NULL
, NULL_CIA
);
519 if (mapping
->device
!= NULL
) {
520 int nr_bytes
= len
- count
;
521 if (raddr
+ nr_bytes
- 1> mapping
->bound
)
522 nr_bytes
= mapping
->bound
- raddr
+ 1;
523 if (device_io_read_buffer(mapping
->device
,
524 (unsigned_1
*)buffer
+ count
,
527 nr_bytes
) != nr_bytes
)
534 ((unsigned_1
*)buffer
)[count
] =
535 *(unsigned_1
*)sim_core_translate(mapping
, raddr
);
545 sim_core_write_buffer (SIM_DESC sd
,
552 sim_core_common
*core
= (cpu
== NULL
? &STATE_CORE (sd
)->common
: &CPU_CORE (cpu
)->common
);
554 while (count
< len
) {
555 unsigned_word raddr
= addr
+ count
;
556 sim_core_mapping
*mapping
=
557 sim_core_find_mapping(core
, map
,
558 raddr
, /*nr-bytes*/1,
560 0 /*dont-abort*/, NULL
, NULL_CIA
);
564 if (WITH_CALLBACK_MEMORY
565 && mapping
->device
!= NULL
) {
566 int nr_bytes
= len
- count
;
567 if (raddr
+ nr_bytes
- 1 > mapping
->bound
)
568 nr_bytes
= mapping
->bound
- raddr
+ 1;
569 if (device_io_write_buffer(mapping
->device
,
570 (unsigned_1
*)buffer
+ count
,
573 nr_bytes
) != nr_bytes
)
580 *(unsigned_1
*)sim_core_translate(mapping
, raddr
) =
581 ((unsigned_1
*)buffer
)[count
];
591 sim_core_set_xor (SIM_DESC sd
,
595 /* set up the XOR map if required. */
596 if (WITH_XOR_ENDIAN
) {
598 sim_core
*core
= STATE_CORE (sd
);
599 sim_cpu_core
*cpu_core
= (cpu
!= NULL
? CPU_CORE (cpu
) : NULL
);
600 if (cpu_core
!= NULL
)
605 mask
= WITH_XOR_ENDIAN
- 1;
608 while (i
- 1 < WITH_XOR_ENDIAN
)
610 cpu_core
->xor[i
-1] = mask
;
611 mask
= (mask
<< 1) & (WITH_XOR_ENDIAN
- 1);
618 core
->byte_xor
= WITH_XOR_ENDIAN
- 1;
626 sim_engine_abort (sd
, cpu
, NULL_CIA
,
627 "Attempted to enable xor-endian mode when permenantly disabled.");
631 STATIC_INLINE_SIM_CORE\
633 reverse_n (unsigned_1
*dest
,
634 const unsigned_1
*src
,
638 for (i
= 0; i
< nr_bytes
; i
++)
640 dest
[nr_bytes
- i
- 1] = src
[i
];
647 sim_core_xor_read_buffer (SIM_DESC sd
,
654 address_word byte_xor
= (cpu
== NULL
? STATE_CORE (sd
)->byte_xor
: CPU_CORE (cpu
)->xor[0]);
655 if (!WITH_XOR_ENDIAN
|| !byte_xor
)
656 return sim_core_read_buffer (sd
, cpu
, map
, buffer
, addr
, nr_bytes
);
658 /* only break up transfers when xor-endian is both selected and enabled */
660 unsigned_1 x
[WITH_XOR_ENDIAN
+ 1]; /* +1 to avoid zero-sized array */
661 unsigned nr_transfered
= 0;
662 address_word start
= addr
;
663 unsigned nr_this_transfer
= (WITH_XOR_ENDIAN
- (addr
& ~(WITH_XOR_ENDIAN
- 1)));
665 /* initial and intermediate transfers are broken when they cross
666 an XOR endian boundary */
667 while (nr_transfered
+ nr_this_transfer
< nr_bytes
)
668 /* initial/intermediate transfers */
670 /* since xor-endian is enabled stop^xor defines the start
671 address of the transfer */
672 stop
= start
+ nr_this_transfer
- 1;
673 SIM_ASSERT (start
<= stop
);
674 SIM_ASSERT ((stop
^ byte_xor
) <= (start
^ byte_xor
));
675 if (sim_core_read_buffer (sd
, cpu
, map
, x
, stop
^ byte_xor
, nr_this_transfer
)
677 return nr_transfered
;
678 reverse_n (&((unsigned_1
*)buffer
)[nr_transfered
], x
, nr_this_transfer
);
679 nr_transfered
+= nr_this_transfer
;
680 nr_this_transfer
= WITH_XOR_ENDIAN
;
684 nr_this_transfer
= nr_bytes
- nr_transfered
;
685 stop
= start
+ nr_this_transfer
- 1;
686 SIM_ASSERT (stop
== (addr
+ nr_bytes
- 1));
687 if (sim_core_read_buffer (sd
, cpu
, map
, x
, stop
^ byte_xor
, nr_this_transfer
)
689 return nr_transfered
;
690 reverse_n (&((unsigned_1
*)buffer
)[nr_transfered
], x
, nr_this_transfer
);
698 sim_core_xor_write_buffer (SIM_DESC sd
,
705 address_word byte_xor
= (cpu
== NULL
? STATE_CORE (sd
)->byte_xor
: CPU_CORE (cpu
)->xor[0]);
706 if (!WITH_XOR_ENDIAN
|| !byte_xor
)
707 return sim_core_write_buffer (sd
, cpu
, map
, buffer
, addr
, nr_bytes
);
709 /* only break up transfers when xor-endian is both selected and enabled */
711 unsigned_1 x
[WITH_XOR_ENDIAN
+ 1]; /* +1 to avoid zero sized array */
712 unsigned nr_transfered
= 0;
713 address_word start
= addr
;
714 unsigned nr_this_transfer
= (WITH_XOR_ENDIAN
- (addr
& ~(WITH_XOR_ENDIAN
- 1)));
716 /* initial and intermediate transfers are broken when they cross
717 an XOR endian boundary */
718 while (nr_transfered
+ nr_this_transfer
< nr_bytes
)
719 /* initial/intermediate transfers */
721 /* since xor-endian is enabled stop^xor defines the start
722 address of the transfer */
723 stop
= start
+ nr_this_transfer
- 1;
724 SIM_ASSERT (start
<= stop
);
725 SIM_ASSERT ((stop
^ byte_xor
) <= (start
^ byte_xor
));
726 reverse_n (x
, &((unsigned_1
*)buffer
)[nr_transfered
], nr_this_transfer
);
727 if (sim_core_read_buffer (sd
, cpu
, map
, x
, stop
^ byte_xor
, nr_this_transfer
)
729 return nr_transfered
;
730 nr_transfered
+= nr_this_transfer
;
731 nr_this_transfer
= WITH_XOR_ENDIAN
;
735 nr_this_transfer
= nr_bytes
- nr_transfered
;
736 stop
= start
+ nr_this_transfer
- 1;
737 SIM_ASSERT (stop
== (addr
+ nr_bytes
- 1));
738 reverse_n (x
, &((unsigned_1
*)buffer
)[nr_transfered
], nr_this_transfer
);
739 if (sim_core_read_buffer (sd
, cpu
, map
, x
, stop
^ byte_xor
, nr_this_transfer
)
741 return nr_transfered
;
748 /* define the read/write 1/2/4/8/16/word functions */
751 #include "sim-n-core.h"
754 #include "sim-n-core.h"
758 #include "sim-n-core.h"
762 #include "sim-n-core.h"
766 #include "sim-n-core.h"
769 #include "sim-n-core.h"
773 #include "sim-n-core.h"
776 #include "sim-n-core.h"
779 #include "sim-n-core.h"