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.
25 #include "libiberty.h"
27 #include "sim-assert.h"
29 /* "core" module install handler.
31 This is called via sim_module_install to install the "core" subsystem
32 into the simulator. */
34 static MODULE_INIT_FN sim_core_init;
35 static MODULE_UNINSTALL_FN sim_core_uninstall;
38 /* TODO: create sim/common/device.h */
39 void device_error (device *me, char* message, ...);
40 int device_io_read_buffer(device *me, void *dest, int space, address_word addr, unsigned nr_bytes, sim_cpu *processor, sim_cia cia);
41 int device_io_write_buffer(device *me, const void *source, int space, address_word addr, unsigned nr_bytes, sim_cpu *processor, sim_cia cia);
46 sim_core_install (SIM_DESC sd)
48 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
50 /* establish the other handlers */
51 sim_module_add_uninstall_fn (sd, sim_core_uninstall);
52 sim_module_add_init_fn (sd, sim_core_init);
54 /* establish any initial data structures - none */
59 /* Uninstall the "core" subsystem from the simulator. */
63 sim_core_uninstall (SIM_DESC sd)
65 sim_core *core = STATE_CORE(sd);
67 /* blow away any mappings */
68 for (map = 0; map < nr_sim_core_maps; map++) {
69 sim_core_mapping *curr = core->common.map[map].first;
70 while (curr != NULL) {
71 sim_core_mapping *tbd = curr;
73 if (tbd->free_buffer != NULL) {
74 SIM_ASSERT(tbd->buffer != NULL);
75 zfree(tbd->free_buffer);
79 core->common.map[map].first = NULL;
86 sim_core_init (SIM_DESC sd)
94 #ifndef SIM_CORE_SIGNAL
95 #define SIM_CORE_SIGNAL(SD,CPU,CIA,MAP,NR_BYTES,ADDR,TRANSFER,ERROR) \
96 sim_core_signal ((SD), (CPU), (CIA), (MAP), (NR_BYTES), (ADDR), (TRANSFER), (ERROR))
100 sim_core_signal (SIM_DESC sd,
106 transfer_type transfer,
107 sim_core_signals sig)
109 const char *copy = (transfer == read_transfer ? "read" : "write");
110 address_word ip = CIA_ADDR (cia);
113 case sim_core_unmapped_signal:
114 sim_io_eprintf (sd, "core: %d byte %s to unmapped address 0x%lx at 0x%lx\n",
115 nr_bytes, copy, (unsigned long) addr, (unsigned long) ip);
116 sim_engine_halt (sd, cpu, NULL, cia, sim_stopped, SIM_SIGSEGV);
118 case sim_core_unaligned_signal:
119 sim_io_eprintf (sd, "core: %d byte misaligned %s to 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_SIGBUS);
124 sim_engine_abort (sd, cpu, cia,
125 "sim_core_signal - internal error - bad switch");
133 sim_core_map_to_str (sim_core_maps map)
137 case sim_core_read_map: return "read";
138 case sim_core_write_map: return "write";
139 case sim_core_execute_map: return "exec";
140 default: return "(invalid-map)";
147 new_sim_core_mapping (SIM_DESC sd,
151 address_word nr_bytes,
157 sim_core_mapping *new_mapping = ZALLOC(sim_core_mapping);
159 new_mapping->level = level;
160 new_mapping->space = space;
161 new_mapping->base = addr;
162 new_mapping->nr_bytes = nr_bytes;
163 new_mapping->bound = addr + (nr_bytes - 1);
165 new_mapping->mask = (unsigned) 0 - 1;
167 new_mapping->mask = modulo - 1;
168 new_mapping->buffer = buffer;
169 new_mapping->free_buffer = free_buffer;
170 new_mapping->device = device;
177 sim_core_map_attach (SIM_DESC sd,
178 sim_core_map *access_map,
182 address_word nr_bytes,
184 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");
202 sim_io_error (sd, "called on sim_core_map_attach with size zero");
206 /* find the insertion point (between last/next) */
207 next_mapping = access_map->first;
208 last_mapping = &access_map->first;
209 while(next_mapping != NULL
210 && (next_mapping->level < level
211 || (next_mapping->level == level
212 && next_mapping->bound < addr)))
214 /* provided levels are the same */
215 /* assert: next_mapping->base > all bases before next_mapping */
216 /* assert: next_mapping->bound >= all bounds before next_mapping */
217 last_mapping = &next_mapping->next;
218 next_mapping = next_mapping->next;
221 /* check insertion point correct */
222 SIM_ASSERT (next_mapping == NULL || next_mapping->level >= level);
223 if (next_mapping != NULL && next_mapping->level == level
224 && next_mapping->base < (addr + (nr_bytes - 1)))
227 device_error (client, "memory map %d:0x%lx..0x%lx (%ld bytes) overlaps %d:0x%lx..0x%lx (%ld bytes)",
231 (long) (addr + (nr_bytes - 1)),
233 (long) next_mapping->base,
234 (long) next_mapping->bound,
235 (long) next_mapping->nr_bytes);
237 sim_io_error (sd, "memory map %d:0x%lx..0x%lx (%ld bytes) overlaps %d:0x%lx..0x%lx (%ld bytes)",
241 (long) (addr + (nr_bytes - 1)),
243 (long) next_mapping->base,
244 (long) next_mapping->bound,
245 (long) next_mapping->nr_bytes);
249 /* create/insert the new mapping */
250 *last_mapping = new_sim_core_mapping(sd,
252 space, addr, nr_bytes, modulo,
253 client, buffer, free_buffer);
254 (*last_mapping)->next = next_mapping;
260 sim_core_attach (SIM_DESC sd,
266 address_word nr_bytes,
269 void *optional_buffer)
271 sim_core *memory = STATE_CORE(sd);
276 /* check for for attempt to use unimplemented per-processor core map */
278 sim_io_error (sd, "sim_core_map_attach - processor specific memory map not yet supported");
280 if ((access & access_read_write_exec) == 0
281 || (access & ~access_read_write_exec) != 0)
284 device_error(client, "invalid access for core attach");
286 sim_io_error (sd, "invalid access for core attach");
290 /* verify modulo memory */
291 if (!WITH_MODULO_MEMORY && modulo != 0)
294 device_error (client, "sim_core_attach - internal error - modulo memory disabled");
296 sim_io_error (sd, "sim_core_attach - internal error - modulo memory disabled");
299 if (client != NULL && modulo != 0)
302 device_error (client, "sim_core_attach - internal error - modulo and callback memory conflict");
304 sim_io_error (sd, "sim_core_attach - internal error - modulo and callback memory conflict");
309 unsigned mask = modulo - 1;
311 while (mask >= sizeof (unsigned64)) /* minimum modulo */
318 if (mask != sizeof (unsigned64) - 1)
321 device_error (client, "sim_core_attach - internal error - modulo %lx not power of two", (long) modulo);
323 sim_io_error (sd, "sim_core_attach - internal error - modulo %lx not power of two", (long) modulo);
328 /* verify consistency between device and buffer */
329 if (client != NULL && optional_buffer != NULL)
332 device_error (client, "sim_core_attach - internal error - conflicting buffer and attach arguments");
334 sim_io_error (sd, "sim_core_attach - internal error - conflicting buffer and attach arguments");
339 if (optional_buffer == NULL)
341 int padding = (addr % sizeof (unsigned64));
342 free_buffer = xmalloc ((modulo == 0 ? nr_bytes : modulo) + padding);
343 buffer = (char*) free_buffer + padding;
347 buffer = optional_buffer;
358 /* attach the region to all applicable access maps */
360 map < nr_sim_core_maps;
365 case sim_core_read_map:
366 if (access & access_read)
367 sim_core_map_attach (sd, &memory->common.map[map],
368 level, space, addr, nr_bytes, modulo,
369 client, buffer, free_buffer);
372 case sim_core_write_map:
373 if (access & access_write)
374 sim_core_map_attach (sd, &memory->common.map[map],
375 level, space, addr, nr_bytes, modulo,
376 client, buffer, free_buffer);
379 case sim_core_execute_map:
380 if (access & access_exec)
381 sim_core_map_attach (sd, &memory->common.map[map],
382 level, space, addr, nr_bytes, modulo,
383 client, buffer, free_buffer);
386 case nr_sim_core_maps:
387 sim_io_error (sd, "sim_core_attach - internal error - bad switch");
392 /* Just copy this map to each of the processor specific data structures.
393 FIXME - later this will be replaced by true processor specific
397 for (i = 0; i < MAX_NR_PROCESSORS; i++)
399 CPU_CORE (STATE_CPU (sd, i))->common = STATE_CORE (sd)->common;
405 /* Remove any memory reference related to this address */
406 STATIC_INLINE_SIM_CORE\
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 free (dead->free_buffer);
435 sim_core_detach (SIM_DESC sd,
441 sim_core *memory = STATE_CORE (sd);
443 for (map = 0; map < nr_sim_core_maps; map++)
445 sim_core_map_detach (sd, &memory->common.map[map],
446 level, address_space, addr);
448 /* Just copy this update to each of the processor specific data
449 structures. FIXME - later this will be replaced by true
450 processor specific maps. */
453 for (i = 0; i < MAX_NR_PROCESSORS; i++)
455 CPU_CORE (STATE_CPU (sd, i))->common = STATE_CORE (sd)->common;
461 STATIC_INLINE_SIM_CORE\
463 sim_core_find_mapping(sim_core_common *core,
467 transfer_type transfer,
468 int abort, /*either 0 or 1 - hint to inline/-O */
469 sim_cpu *cpu, /* abort => cpu != NULL */
472 sim_core_mapping *mapping = core->map[map].first;
473 ASSERT ((addr & (nr_bytes - 1)) == 0); /* must be aligned */
474 ASSERT ((addr + (nr_bytes - 1)) >= addr); /* must not wrap */
475 ASSERT (!abort || cpu != NULL); /* abort needs a non null CPU */
476 while (mapping != NULL)
478 if (addr >= mapping->base
479 && (addr + (nr_bytes - 1)) <= mapping->bound)
481 mapping = mapping->next;
485 SIM_CORE_SIGNAL (CPU_STATE (cpu), cpu, cia, map, nr_bytes, addr, transfer,
486 sim_core_unmapped_signal);
492 STATIC_INLINE_SIM_CORE\
494 sim_core_translate (sim_core_mapping *mapping,
497 if (WITH_MODULO_MEMORY)
498 return (void *)((unsigned8 *) mapping->buffer
499 + ((addr - mapping->base) & mapping->mask));
501 return (void *)((unsigned8 *) mapping->buffer
502 + addr - mapping->base);
508 sim_core_read_buffer (SIM_DESC sd,
515 sim_core_common *core = (cpu == NULL ? &STATE_CORE (sd)->common : &CPU_CORE (cpu)->common);
517 while (count < len) {
518 unsigned_word raddr = addr + count;
519 sim_core_mapping *mapping =
520 sim_core_find_mapping(core, map,
521 raddr, /*nr-bytes*/1,
523 0 /*dont-abort*/, NULL, NULL_CIA);
527 if (mapping->device != NULL) {
528 int nr_bytes = len - count;
529 if (raddr + nr_bytes - 1> mapping->bound)
530 nr_bytes = mapping->bound - raddr + 1;
531 if (device_io_read_buffer(mapping->device,
532 (unsigned_1*)buffer + count,
537 CIA_GET(cpu)) != nr_bytes)
544 ((unsigned_1*)buffer)[count] =
545 *(unsigned_1*)sim_core_translate(mapping, raddr);
555 sim_core_write_buffer (SIM_DESC sd,
562 sim_core_common *core = (cpu == NULL ? &STATE_CORE (sd)->common : &CPU_CORE (cpu)->common);
564 while (count < len) {
565 unsigned_word raddr = addr + count;
566 sim_core_mapping *mapping =
567 sim_core_find_mapping(core, map,
568 raddr, /*nr-bytes*/1,
570 0 /*dont-abort*/, NULL, NULL_CIA);
574 if (WITH_CALLBACK_MEMORY
575 && mapping->device != NULL) {
576 int nr_bytes = len - count;
577 if (raddr + nr_bytes - 1 > mapping->bound)
578 nr_bytes = mapping->bound - raddr + 1;
579 if (device_io_write_buffer(mapping->device,
580 (unsigned_1*)buffer + count,
585 CIA_GET(cpu)) != nr_bytes)
592 *(unsigned_1*)sim_core_translate(mapping, raddr) =
593 ((unsigned_1*)buffer)[count];
603 sim_core_set_xor (SIM_DESC sd,
607 /* set up the XOR map if required. */
608 if (WITH_XOR_ENDIAN) {
610 sim_core *core = STATE_CORE (sd);
611 sim_cpu_core *cpu_core = (cpu != NULL ? CPU_CORE (cpu) : NULL);
612 if (cpu_core != NULL)
617 mask = WITH_XOR_ENDIAN - 1;
620 while (i - 1 < WITH_XOR_ENDIAN)
622 cpu_core->xor[i-1] = mask;
623 mask = (mask << 1) & (WITH_XOR_ENDIAN - 1);
630 core->byte_xor = WITH_XOR_ENDIAN - 1;
638 sim_engine_abort (sd, cpu, NULL_CIA,
639 "Attempted to enable xor-endian mode when permenantly disabled.");
643 STATIC_INLINE_SIM_CORE\
645 reverse_n (unsigned_1 *dest,
646 const unsigned_1 *src,
650 for (i = 0; i < nr_bytes; i++)
652 dest [nr_bytes - i - 1] = src [i];
659 sim_core_xor_read_buffer (SIM_DESC sd,
666 address_word byte_xor = (cpu == NULL ? STATE_CORE (sd)->byte_xor : CPU_CORE (cpu)->xor[0]);
667 if (!WITH_XOR_ENDIAN || !byte_xor)
668 return sim_core_read_buffer (sd, cpu, map, buffer, addr, nr_bytes);
670 /* only break up transfers when xor-endian is both selected and enabled */
672 unsigned_1 x[WITH_XOR_ENDIAN + 1]; /* +1 to avoid zero-sized array */
673 unsigned nr_transfered = 0;
674 address_word start = addr;
675 unsigned nr_this_transfer = (WITH_XOR_ENDIAN - (addr & ~(WITH_XOR_ENDIAN - 1)));
677 /* initial and intermediate transfers are broken when they cross
678 an XOR endian boundary */
679 while (nr_transfered + nr_this_transfer < nr_bytes)
680 /* initial/intermediate transfers */
682 /* since xor-endian is enabled stop^xor defines the start
683 address of the transfer */
684 stop = start + nr_this_transfer - 1;
685 SIM_ASSERT (start <= stop);
686 SIM_ASSERT ((stop ^ byte_xor) <= (start ^ byte_xor));
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);
691 nr_transfered += nr_this_transfer;
692 nr_this_transfer = WITH_XOR_ENDIAN;
696 nr_this_transfer = nr_bytes - nr_transfered;
697 stop = start + nr_this_transfer - 1;
698 SIM_ASSERT (stop == (addr + nr_bytes - 1));
699 if (sim_core_read_buffer (sd, cpu, map, x, stop ^ byte_xor, nr_this_transfer)
701 return nr_transfered;
702 reverse_n (&((unsigned_1*)buffer)[nr_transfered], x, nr_this_transfer);
710 sim_core_xor_write_buffer (SIM_DESC sd,
717 address_word byte_xor = (cpu == NULL ? STATE_CORE (sd)->byte_xor : CPU_CORE (cpu)->xor[0]);
718 if (!WITH_XOR_ENDIAN || !byte_xor)
719 return sim_core_write_buffer (sd, cpu, map, buffer, addr, nr_bytes);
721 /* only break up transfers when xor-endian is both selected and enabled */
723 unsigned_1 x[WITH_XOR_ENDIAN + 1]; /* +1 to avoid zero sized array */
724 unsigned nr_transfered = 0;
725 address_word start = addr;
726 unsigned nr_this_transfer = (WITH_XOR_ENDIAN - (addr & ~(WITH_XOR_ENDIAN - 1)));
728 /* initial and intermediate transfers are broken when they cross
729 an XOR endian boundary */
730 while (nr_transfered + nr_this_transfer < nr_bytes)
731 /* initial/intermediate transfers */
733 /* since xor-endian is enabled stop^xor defines the start
734 address of the transfer */
735 stop = start + nr_this_transfer - 1;
736 SIM_ASSERT (start <= stop);
737 SIM_ASSERT ((stop ^ byte_xor) <= (start ^ byte_xor));
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;
742 nr_transfered += nr_this_transfer;
743 nr_this_transfer = WITH_XOR_ENDIAN;
747 nr_this_transfer = nr_bytes - nr_transfered;
748 stop = start + nr_this_transfer - 1;
749 SIM_ASSERT (stop == (addr + nr_bytes - 1));
750 reverse_n (x, &((unsigned_1*)buffer)[nr_transfered], nr_this_transfer);
751 if (sim_core_read_buffer (sd, cpu, map, x, stop ^ byte_xor, nr_this_transfer)
753 return nr_transfered;
760 /* define the read/write 1/2/4/8/16/word functions */
763 #include "sim-n-core.h"
766 #include "sim-n-core.h"
770 #include "sim-n-core.h"
774 #include "sim-n-core.h"
778 #include "sim-n-core.h"
781 #include "sim-n-core.h"
785 #include "sim-n-core.h"
788 #include "sim-n-core.h"
791 #include "sim-n-core.h"