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"
30 /* for Windows builds. signal numbers used by MSVC are mostly
31 the same as non-linux unixen. */
37 /* "core" module install handler.
39 This is called via sim_module_install to install the "core" subsystem
40 into the simulator. */
42 static MODULE_INIT_FN sim_core_init;
43 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 */
60 /* Uninstall the "core" subsystem from the simulator. */
64 sim_core_uninstall (SIM_DESC sd)
66 sim_core *core = STATE_CORE(sd);
68 /* blow away any mappings */
69 for (map = 0; map < nr_sim_core_maps; map++) {
70 sim_core_mapping *curr = core->common.map[map].first;
71 while (curr != NULL) {
72 sim_core_mapping *tbd = curr;
74 if (tbd->free_buffer != NULL) {
75 SIM_ASSERT(tbd->buffer != NULL);
76 zfree(tbd->free_buffer);
80 core->common.map[map].first = NULL;
87 sim_core_init (SIM_DESC sd)
95 #ifndef SIM_CORE_SIGNAL
96 #define SIM_CORE_SIGNAL(SD,CPU,CIA,MAP,NR_BYTES,ADDR,TRANSFER,ERROR) \
97 sim_core_signal ((SD), (CPU), (CIA), (MAP), (NR_BYTES), (ADDR), (TRANSFER), (ERROR))
101 sim_core_signal (SIM_DESC sd,
107 transfer_type transfer,
108 sim_core_signals sig)
110 const char *copy = (transfer == read_transfer ? "read" : "write");
111 address_word ip = CIA_ADDR (cia);
114 case sim_core_unmapped_signal:
115 sim_io_eprintf (sd, "core: %d byte %s to unmaped address 0x%lx at 0x%lx\n",
116 nr_bytes, copy, (unsigned long) addr, (unsigned long) ip);
117 sim_engine_halt (sd, cpu, NULL, cia, sim_signalled, SIGSEGV);
119 case sim_core_unaligned_signal:
120 sim_io_eprintf (sd, "core: %d byte misaligned %s to 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_signalled, SIGBUS);
125 sim_engine_abort (sd, cpu, cia,
126 "sim_core_signal - internal error - bad switch");
132 STATIC_INLINE_SIM_CORE\
134 sim_core_map_to_str (sim_core_maps map)
138 case sim_core_read_map: return "read";
139 case sim_core_write_map: return "write";
140 case sim_core_execute_map: return "exec";
141 default: return "(invalid-map)";
148 new_sim_core_mapping (SIM_DESC sd,
152 address_word nr_bytes,
158 sim_core_mapping *new_mapping = ZALLOC(sim_core_mapping);
160 new_mapping->level = level;
161 new_mapping->space = space;
162 new_mapping->base = addr;
163 new_mapping->nr_bytes = nr_bytes;
164 new_mapping->bound = addr + (nr_bytes - 1);
166 new_mapping->mask = (unsigned) 0 - 1;
168 new_mapping->mask = modulo - 1;
169 new_mapping->buffer = buffer;
170 new_mapping->free_buffer = free_buffer;
171 new_mapping->device = device;
178 sim_core_map_attach (SIM_DESC sd,
179 sim_core_map *access_map,
183 address_word nr_bytes,
185 device *client, /*callback/default*/
186 void *buffer, /*raw_memory*/
187 void *free_buffer) /*raw_memory*/
189 /* find the insertion point for this additional mapping and then
191 sim_core_mapping *next_mapping;
192 sim_core_mapping **last_mapping;
194 SIM_ASSERT ((client == NULL) != (buffer == NULL));
195 SIM_ASSERT ((client == NULL) >= (free_buffer != NULL));
197 /* actually do occasionally get a zero size map */
201 device_error(client, "called on sim_core_map_attach with size zero");
203 sim_io_error (sd, "called on sim_core_map_attach with size zero");
207 /* find the insertion point (between last/next) */
208 next_mapping = access_map->first;
209 last_mapping = &access_map->first;
210 while(next_mapping != NULL
211 && (next_mapping->level < level
212 || (next_mapping->level == level
213 && next_mapping->bound < addr)))
215 /* provided levels are the same */
216 /* assert: next_mapping->base > all bases before next_mapping */
217 /* assert: next_mapping->bound >= all bounds before next_mapping */
218 last_mapping = &next_mapping->next;
219 next_mapping = next_mapping->next;
222 /* check insertion point correct */
223 SIM_ASSERT (next_mapping == NULL || next_mapping->level >= level);
224 if (next_mapping != NULL && next_mapping->level == level
225 && next_mapping->base < (addr + (nr_bytes - 1)))
228 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)),
234 (long) next_mapping->base,
235 (long) next_mapping->bound,
236 (long) next_mapping->nr_bytes);
238 sim_io_error (sd, "memory map %d:0x%lx..0x%lx (%ld bytes) overlaps %d:0x%lx..0x%lx (%ld bytes)",
242 (long) (addr + (nr_bytes - 1)),
244 (long) next_mapping->base,
245 (long) next_mapping->bound,
246 (long) next_mapping->nr_bytes);
250 /* create/insert the new mapping */
251 *last_mapping = new_sim_core_mapping(sd,
253 space, addr, nr_bytes, modulo,
254 client, buffer, free_buffer);
255 (*last_mapping)->next = next_mapping;
261 sim_core_attach (SIM_DESC sd,
267 address_word nr_bytes,
270 void *optional_buffer)
272 sim_core *memory = STATE_CORE(sd);
277 /* check for for attempt to use unimplemented per-processor core map */
279 sim_io_error (sd, "sim_core_map_attach - processor specific memory map not yet supported");
281 if ((access & access_read_write_exec) == 0
282 || (access & ~access_read_write_exec) != 0)
285 device_error(client, "invalid access for core attach");
287 sim_io_error (sd, "invalid access for core attach");
291 /* verify modulo memory */
292 if (!WITH_MODULO_MEMORY && modulo != 0)
295 device_error (client, "sim_core_attach - internal error - modulo memory disabled");
297 sim_io_error (sd, "sim_core_attach - internal error - modulo memory disabled");
300 if (client != NULL && modulo != 0)
303 device_error (client, "sim_core_attach - internal error - modulo and callback memory conflict");
305 sim_io_error (sd, "sim_core_attach - internal error - modulo and callback memory conflict");
310 unsigned mask = modulo - 1;
312 while (mask >= sizeof (unsigned64)) /* minimum modulo */
319 if (mask != sizeof (unsigned64) - 1)
322 device_error (client, "sim_core_attach - internal error - modulo %lx not power of two", (long) modulo);
324 sim_io_error (sd, "sim_core_attach - internal error - modulo %lx not power of two", (long) modulo);
329 /* verify consistency between device and buffer */
330 if (client != NULL && optional_buffer != NULL)
333 device_error (client, "sim_core_attach - internal error - conflicting buffer and attach arguments");
335 sim_io_error (sd, "sim_core_attach - internal error - conflicting buffer and attach arguments");
340 if (optional_buffer == NULL)
342 int padding = (addr % sizeof (unsigned64));
343 free_buffer = zalloc ((modulo == 0 ? nr_bytes : modulo) + padding);
344 buffer = (char*) free_buffer + padding;
348 buffer = optional_buffer;
359 /* attach the region to all applicable access maps */
361 map < nr_sim_core_maps;
366 case sim_core_read_map:
367 if (access & access_read)
368 sim_core_map_attach (sd, &memory->common.map[map],
369 level, space, addr, nr_bytes, modulo,
370 client, buffer, free_buffer);
373 case sim_core_write_map:
374 if (access & access_write)
375 sim_core_map_attach (sd, &memory->common.map[map],
376 level, space, addr, nr_bytes, modulo,
377 client, buffer, free_buffer);
380 case sim_core_execute_map:
381 if (access & access_exec)
382 sim_core_map_attach (sd, &memory->common.map[map],
383 level, space, addr, nr_bytes, modulo,
384 client, buffer, free_buffer);
387 case nr_sim_core_maps:
388 sim_io_error (sd, "sim_core_attach - internal error - bad switch");
393 /* Just copy this map to each of the processor specific data structures.
394 FIXME - later this will be replaced by true processor specific
398 for (i = 0; i < MAX_NR_PROCESSORS; i++)
400 CPU_CORE (STATE_CPU (sd, i))->common = STATE_CORE (sd)->common;
406 /* Remove any memory reference related to this address */
407 STATIC_INLINE_SIM_CORE\
409 sim_core_map_detach (SIM_DESC sd,
410 sim_core_map *access_map,
415 sim_core_mapping **entry;
416 for (entry = &access_map->first;
418 entry = &(*entry)->next)
420 if ((*entry)->base == addr
421 && (*entry)->level == level
422 && (*entry)->space == space)
424 sim_core_mapping *dead = (*entry);
425 (*entry) = dead->next;
426 if (dead->free_buffer != NULL)
427 zfree (dead->free_buffer);
436 sim_core_detach (SIM_DESC sd,
442 sim_core *memory = STATE_CORE (sd);
444 for (map = 0; map < nr_sim_core_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;
462 STATIC_INLINE_SIM_CORE\
464 sim_core_find_mapping(sim_core_common *core,
468 transfer_type transfer,
469 int abort, /*either 0 or 1 - hint to inline/-O */
470 sim_cpu *cpu, /* abort => cpu != NULL */
473 sim_core_mapping *mapping = core->map[map].first;
474 ASSERT ((addr & (nr_bytes - 1)) == 0); /* must be aligned */
475 ASSERT ((addr + (nr_bytes - 1)) >= addr); /* must not wrap */
476 ASSERT (!abort || cpu != NULL); /* abort needs a non null CPU */
477 while (mapping != NULL)
479 if (addr >= mapping->base
480 && (addr + (nr_bytes - 1)) <= mapping->bound)
482 mapping = mapping->next;
486 SIM_CORE_SIGNAL (CPU_STATE (cpu), cpu, cia, map, nr_bytes, addr, transfer,
487 sim_core_unmapped_signal);
493 STATIC_INLINE_SIM_CORE\
495 sim_core_translate (sim_core_mapping *mapping,
498 if (WITH_MODULO_MEMORY)
499 return (void *)((unsigned8 *) mapping->buffer
500 + ((addr - mapping->base) & mapping->mask));
502 return (void *)((unsigned8 *) mapping->buffer
503 + addr - mapping->base);
509 sim_core_read_buffer (SIM_DESC sd,
516 sim_core_common *core = (cpu == NULL ? &STATE_CORE (sd)->common : &CPU_CORE (cpu)->common);
518 while (count < len) {
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) {
529 int nr_bytes = len - count;
530 if (raddr + nr_bytes - 1> mapping->bound)
531 nr_bytes = mapping->bound - raddr + 1;
532 if (device_io_read_buffer(mapping->device,
533 (unsigned_1*)buffer + count,
536 nr_bytes) != nr_bytes)
543 ((unsigned_1*)buffer)[count] =
544 *(unsigned_1*)sim_core_translate(mapping, raddr);
554 sim_core_write_buffer (SIM_DESC sd,
561 sim_core_common *core = (cpu == NULL ? &STATE_CORE (sd)->common : &CPU_CORE (cpu)->common);
563 while (count < len) {
564 unsigned_word raddr = addr + count;
565 sim_core_mapping *mapping =
566 sim_core_find_mapping(core, map,
567 raddr, /*nr-bytes*/1,
569 0 /*dont-abort*/, NULL, NULL_CIA);
573 if (WITH_CALLBACK_MEMORY
574 && mapping->device != NULL) {
575 int nr_bytes = len - count;
576 if (raddr + nr_bytes - 1 > mapping->bound)
577 nr_bytes = mapping->bound - raddr + 1;
578 if (device_io_write_buffer(mapping->device,
579 (unsigned_1*)buffer + count,
582 nr_bytes) != nr_bytes)
589 *(unsigned_1*)sim_core_translate(mapping, raddr) =
590 ((unsigned_1*)buffer)[count];
600 sim_core_set_xor (SIM_DESC sd,
604 /* set up the XOR map if required. */
605 if (WITH_XOR_ENDIAN) {
607 sim_core *core = STATE_CORE (sd);
608 sim_cpu_core *cpu_core = (cpu != NULL ? CPU_CORE (cpu) : NULL);
609 if (cpu_core != NULL)
614 mask = WITH_XOR_ENDIAN - 1;
617 while (i - 1 < WITH_XOR_ENDIAN)
619 cpu_core->xor[i-1] = mask;
620 mask = (mask << 1) & (WITH_XOR_ENDIAN - 1);
627 core->byte_xor = WITH_XOR_ENDIAN - 1;
635 sim_engine_abort (sd, cpu, NULL_CIA,
636 "Attempted to enable xor-endian mode when permenantly disabled.");
640 STATIC_INLINE_SIM_CORE\
642 reverse_n (unsigned_1 *dest,
643 const unsigned_1 *src,
647 for (i = 0; i < nr_bytes; i++)
649 dest [nr_bytes - i - 1] = src [i];
656 sim_core_xor_read_buffer (SIM_DESC sd,
663 address_word byte_xor = (cpu == NULL ? STATE_CORE (sd)->byte_xor : CPU_CORE (cpu)->xor[0]);
664 if (!WITH_XOR_ENDIAN || !byte_xor)
665 return sim_core_read_buffer (sd, cpu, map, buffer, addr, nr_bytes);
667 /* only break up transfers when xor-endian is both selected and enabled */
669 unsigned_1 x[WITH_XOR_ENDIAN + 1]; /* +1 to avoid zero-sized array */
670 unsigned nr_transfered = 0;
671 address_word start = addr;
672 unsigned nr_this_transfer = (WITH_XOR_ENDIAN - (addr & ~(WITH_XOR_ENDIAN - 1)));
674 /* initial and intermediate transfers are broken when they cross
675 an XOR endian boundary */
676 while (nr_transfered + nr_this_transfer < nr_bytes)
677 /* initial/intermediate transfers */
679 /* since xor-endian is enabled stop^xor defines the start
680 address of the transfer */
681 stop = start + nr_this_transfer - 1;
682 SIM_ASSERT (start <= stop);
683 SIM_ASSERT ((stop ^ byte_xor) <= (start ^ byte_xor));
684 if (sim_core_read_buffer (sd, cpu, map, x, stop ^ byte_xor, nr_this_transfer)
686 return nr_transfered;
687 reverse_n (&((unsigned_1*)buffer)[nr_transfered], x, nr_this_transfer);
688 nr_transfered += nr_this_transfer;
689 nr_this_transfer = WITH_XOR_ENDIAN;
693 nr_this_transfer = nr_bytes - nr_transfered;
694 stop = start + nr_this_transfer - 1;
695 SIM_ASSERT (stop == (addr + nr_bytes - 1));
696 if (sim_core_read_buffer (sd, cpu, map, x, stop ^ byte_xor, nr_this_transfer)
698 return nr_transfered;
699 reverse_n (&((unsigned_1*)buffer)[nr_transfered], x, nr_this_transfer);
707 sim_core_xor_write_buffer (SIM_DESC sd,
714 address_word byte_xor = (cpu == NULL ? STATE_CORE (sd)->byte_xor : CPU_CORE (cpu)->xor[0]);
715 if (!WITH_XOR_ENDIAN || !byte_xor)
716 return sim_core_write_buffer (sd, cpu, map, buffer, addr, nr_bytes);
718 /* only break up transfers when xor-endian is both selected and enabled */
720 unsigned_1 x[WITH_XOR_ENDIAN + 1]; /* +1 to avoid zero sized array */
721 unsigned nr_transfered = 0;
722 address_word start = addr;
723 unsigned nr_this_transfer = (WITH_XOR_ENDIAN - (addr & ~(WITH_XOR_ENDIAN - 1)));
725 /* initial and intermediate transfers are broken when they cross
726 an XOR endian boundary */
727 while (nr_transfered + nr_this_transfer < nr_bytes)
728 /* initial/intermediate transfers */
730 /* since xor-endian is enabled stop^xor defines the start
731 address of the transfer */
732 stop = start + nr_this_transfer - 1;
733 SIM_ASSERT (start <= stop);
734 SIM_ASSERT ((stop ^ byte_xor) <= (start ^ byte_xor));
735 reverse_n (x, &((unsigned_1*)buffer)[nr_transfered], nr_this_transfer);
736 if (sim_core_read_buffer (sd, cpu, map, x, stop ^ byte_xor, nr_this_transfer)
738 return nr_transfered;
739 nr_transfered += nr_this_transfer;
740 nr_this_transfer = WITH_XOR_ENDIAN;
744 nr_this_transfer = nr_bytes - nr_transfered;
745 stop = start + nr_this_transfer - 1;
746 SIM_ASSERT (stop == (addr + nr_bytes - 1));
747 reverse_n (x, &((unsigned_1*)buffer)[nr_transfered], nr_this_transfer);
748 if (sim_core_read_buffer (sd, cpu, map, x, stop ^ byte_xor, nr_this_transfer)
750 return nr_transfered;
757 /* define the read/write 1/2/4/8/16/word functions */
760 #include "sim-n-core.h"
763 #include "sim-n-core.h"
767 #include "sim-n-core.h"
771 #include "sim-n-core.h"
775 #include "sim-n-core.h"
778 #include "sim-n-core.h"
782 #include "sim-n-core.h"
785 #include "sim-n-core.h"
788 #include "sim-n-core.h"