]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/mips/interp.c
sim: split sim-signal.h include out
[thirdparty/binutils-gdb.git] / sim / mips / interp.c
CommitLineData
c906108c
SS
1/*> interp.c <*/
2/* Simulator for the MIPS architecture.
3
4 This file is part of the MIPS sim
5
6 THIS SOFTWARE IS NOT COPYRIGHTED
7
8 Cygnus offers the following for use in the public domain. Cygnus
9 makes no warranty with regard to the software or it's performance
10 and the user accepts the software "AS IS" with all faults.
11
12 CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO
13 THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
15
c906108c
SS
16NOTEs:
17
18The IDT monitor (found on the VR4300 board), seems to lie about
19register contents. It seems to treat the registers as sign-extended
2032-bit values. This cause *REAL* problems when single-stepping 64-bit
21code on the hardware.
22
23*/
24
6df01ab8
MF
25/* This must come before any other includes. */
26#include "defs.h"
27
c906108c
SS
28#include "bfd.h"
29#include "sim-main.h"
30#include "sim-utils.h"
31#include "sim-options.h"
32#include "sim-assert.h"
33#include "sim-hw.h"
1fef66b0 34#include "sim-signal.h"
c906108c
SS
35
36#include "itable.h"
37
c906108c
SS
38#include <stdio.h>
39#include <stdarg.h>
40#include <ansidecl.h>
41#include <ctype.h>
42#include <limits.h>
43#include <math.h>
c906108c 44#include <stdlib.h>
c906108c 45#include <string.h>
c906108c
SS
46
47#include "getopt.h"
48#include "libiberty.h"
49#include "bfd.h"
b36d953b 50#include "elf-bfd.h"
df68e12b
MF
51#include "sim/callback.h" /* GDB simulator callback interface */
52#include "sim/sim.h" /* GDB simulator interface */
5c6f091a 53#include "sim-syscall.h" /* Simulator system call support */
c906108c 54
bdca5ee4
TT
55char* pr_addr (SIM_ADDR addr);
56char* pr_uword64 (uword64 addr);
c906108c
SS
57
58
59/* Within interp.c we refer to the sim_state and sim_cpu directly. */
60#define CPU cpu
61#define SD sd
62
63
64/* The following reserved instruction value is used when a simulator
65 trap is required. NOTE: Care must be taken, since this value may be
66 used in later revisions of the MIPS ISA. */
67
8e394ffc 68#define RSVD_INSTRUCTION (0x00000039)
c906108c
SS
69#define RSVD_INSTRUCTION_MASK (0xFC00003F)
70
71#define RSVD_INSTRUCTION_ARG_SHIFT 6
72#define RSVD_INSTRUCTION_ARG_MASK 0xFFFFF
73
74
75/* Bits in the Debug register */
76#define Debug_DBD 0x80000000 /* Debug Branch Delay */
77#define Debug_DM 0x40000000 /* Debug Mode */
78#define Debug_DBp 0x00000002 /* Debug Breakpoint indicator */
79
80/*---------------------------------------------------------------------------*/
81/*-- GDB simulator interface ------------------------------------------------*/
82/*---------------------------------------------------------------------------*/
83
bdca5ee4 84static void ColdReset (SIM_DESC sd);
c906108c
SS
85
86/*---------------------------------------------------------------------------*/
87
88
89
90#define DELAYSLOT() {\
91 if (STATE & simDELAYSLOT)\
92 sim_io_eprintf(sd,"Delay slot already activated (branch in delay slot?)\n");\
93 STATE |= simDELAYSLOT;\
94 }
95
96#define JALDELAYSLOT() {\
97 DELAYSLOT ();\
98 STATE |= simJALDELAYSLOT;\
99 }
100
101#define NULLIFY() {\
102 STATE &= ~simDELAYSLOT;\
103 STATE |= simSKIPNEXT;\
104 }
105
106#define CANCELDELAYSLOT() {\
107 DSSTATE = 0;\
108 STATE &= ~(simDELAYSLOT | simJALDELAYSLOT);\
109 }
110
111#define INDELAYSLOT() ((STATE & simDELAYSLOT) != 0)
112#define INJALDELAYSLOT() ((STATE & simJALDELAYSLOT) != 0)
113
adf40b2e
JM
114/* Note that the monitor code essentially assumes this layout of memory.
115 If you change these, change the monitor code, too. */
14fb6c5a
TS
116/* FIXME Currently addresses are truncated to 32-bits, see
117 mips/sim-main.c:address_translation(). If that changes, then these
118 values will need to be extended, and tested for more carefully. */
c906108c
SS
119#define K0BASE (0x80000000)
120#define K0SIZE (0x20000000)
121#define K1BASE (0xA0000000)
122#define K1SIZE (0x20000000)
adf40b2e
JM
123
124/* Simple run-time monitor support.
125
126 We emulate the monitor by placing magic reserved instructions at
127 the monitor's entry points; when we hit these instructions, instead
128 of raising an exception (as we would normally), we look at the
129 instruction and perform the appropriate monitory operation.
130
131 `*_monitor_base' are the physical addresses at which the corresponding
132 monitor vectors are located. `0' means none. By default,
133 install all three.
134 The RSVD_INSTRUCTION... macros specify the magic instructions we
135 use at the monitor entry points. */
136static int firmware_option_p = 0;
137static SIM_ADDR idt_monitor_base = 0xBFC00000;
138static SIM_ADDR pmon_monitor_base = 0xBFC00500;
139static SIM_ADDR lsipmon_monitor_base = 0xBFC00200;
140
141static SIM_RC sim_firmware_command (SIM_DESC sd, char* arg);
142
c8847145 143#define MEM_SIZE (8 << 20) /* 8 MBytes */
c906108c
SS
144
145
29bc024d 146#if WITH_TRACE_ANY_P
c906108c
SS
147static char *tracefile = "trace.din"; /* default filename for trace log */
148FILE *tracefh = NULL;
bdca5ee4 149static void open_trace (SIM_DESC sd);
29bc024d
MF
150#else
151#define open_trace(sd)
152#endif
c906108c
SS
153
154static const char * get_insn_name (sim_cpu *, int);
155
156/* simulation target board. NULL=canonical */
157static char* board = NULL;
158
159
160static DECLARE_OPTION_HANDLER (mips_option_handler);
161
162enum {
163 OPTION_DINERO_TRACE = OPTION_START,
164 OPTION_DINERO_FILE,
adf40b2e 165 OPTION_FIRMWARE,
2525df03 166 OPTION_INFO_MEMORY,
c906108c
SS
167 OPTION_BOARD
168};
169
2525df03 170static int display_mem_info = 0;
c906108c
SS
171
172static SIM_RC
8ac57fbd
MF
173mips_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt, char *arg,
174 int is_command)
c906108c
SS
175{
176 int cpu_nr;
177 switch (opt)
178 {
179 case OPTION_DINERO_TRACE: /* ??? */
29bc024d 180#if WITH_TRACE_ANY_P
c906108c
SS
181 /* Eventually the simTRACE flag could be treated as a toggle, to
182 allow external control of the program points being traced
183 (i.e. only from main onwards, excluding the run-time setup,
184 etc.). */
185 for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
186 {
187 sim_cpu *cpu = STATE_CPU (sd, cpu_nr);
188 if (arg == NULL)
189 STATE |= simTRACE;
190 else if (strcmp (arg, "yes") == 0)
191 STATE |= simTRACE;
192 else if (strcmp (arg, "no") == 0)
193 STATE &= ~simTRACE;
194 else if (strcmp (arg, "on") == 0)
195 STATE |= simTRACE;
196 else if (strcmp (arg, "off") == 0)
197 STATE &= ~simTRACE;
198 else
199 {
200 fprintf (stderr, "Unrecognized dinero-trace option `%s'\n", arg);
201 return SIM_RC_FAIL;
202 }
203 }
204 return SIM_RC_OK;
29bc024d 205#else /* !WITH_TRACE_ANY_P */
c906108c
SS
206 fprintf(stderr,"\
207Simulator constructed without dinero tracing support (for performance).\n\
29bc024d 208Re-compile simulator with \"-DWITH_TRACE_ANY_P\" to enable this option.\n");
c906108c 209 return SIM_RC_FAIL;
29bc024d 210#endif /* !WITH_TRACE_ANY_P */
c906108c
SS
211
212 case OPTION_DINERO_FILE:
29bc024d 213#if WITH_TRACE_ANY_P
c906108c
SS
214 if (optarg != NULL) {
215 char *tmp;
216 tmp = (char *)malloc(strlen(optarg) + 1);
217 if (tmp == NULL)
218 {
219 sim_io_printf(sd,"Failed to allocate buffer for tracefile name \"%s\"\n",optarg);
220 return SIM_RC_FAIL;
221 }
222 else {
223 strcpy(tmp,optarg);
224 tracefile = tmp;
225 sim_io_printf(sd,"Placing trace information into file \"%s\"\n",tracefile);
226 }
227 }
29bc024d 228#endif /* WITH_TRACE_ANY_P */
c906108c
SS
229 return SIM_RC_OK;
230
adf40b2e
JM
231 case OPTION_FIRMWARE:
232 return sim_firmware_command (sd, arg);
233
c906108c
SS
234 case OPTION_BOARD:
235 {
236 if (arg)
237 {
238 board = zalloc(strlen(arg) + 1);
239 strcpy(board, arg);
240 }
241 return SIM_RC_OK;
242 }
2525df03
NC
243
244 case OPTION_INFO_MEMORY:
245 display_mem_info = 1;
246 break;
c906108c
SS
247 }
248
249 return SIM_RC_OK;
250}
251
252
253static const OPTION mips_options[] =
254{
255 { {"dinero-trace", optional_argument, NULL, OPTION_DINERO_TRACE},
256 '\0', "on|off", "Enable dinero tracing",
257 mips_option_handler },
258 { {"dinero-file", required_argument, NULL, OPTION_DINERO_FILE},
259 '\0', "FILE", "Write dinero trace to FILE",
260 mips_option_handler },
adf40b2e
JM
261 { {"firmware", required_argument, NULL, OPTION_FIRMWARE},
262 '\0', "[idt|pmon|lsipmon|none][@ADDRESS]", "Emulate ROM monitor",
263 mips_option_handler },
c906108c
SS
264 { {"board", required_argument, NULL, OPTION_BOARD},
265 '\0', "none" /* rely on compile-time string concatenation for other options */
266
267#define BOARD_JMR3904 "jmr3904"
268 "|" BOARD_JMR3904
269#define BOARD_JMR3904_PAL "jmr3904pal"
270 "|" BOARD_JMR3904_PAL
271#define BOARD_JMR3904_DEBUG "jmr3904debug"
272 "|" BOARD_JMR3904_DEBUG
43e526b9
JM
273#define BOARD_BSP "bsp"
274 "|" BOARD_BSP
c906108c
SS
275
276 , "Customize simulation for a particular board.", mips_option_handler },
277
2525df03
NC
278 /* These next two options have the same names as ones found in the
279 memory_options[] array in common/sim-memopt.c. This is because
280 the intention is to provide an alternative handler for those two
281 options. We need an alternative handler because the memory
282 regions are not set up until after the command line arguments
283 have been parsed, and so we cannot display the memory info whilst
284 processing the command line. There is a hack in sim_open to
285 remove these handlers when we want the real --memory-info option
286 to work. */
287 { { "info-memory", no_argument, NULL, OPTION_INFO_MEMORY },
288 '\0', NULL, "List configured memory regions", mips_option_handler },
289 { { "memory-info", no_argument, NULL, OPTION_INFO_MEMORY },
290 '\0', NULL, NULL, mips_option_handler },
291
c906108c
SS
292 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
293};
294
295
296int interrupt_pending;
297
298void
299interrupt_event (SIM_DESC sd, void *data)
300{
301 sim_cpu *cpu = STATE_CPU (sd, 0); /* FIXME */
034685f9 302 address_word cia = CPU_PC_GET (cpu);
c906108c
SS
303 if (SR & status_IE)
304 {
305 interrupt_pending = 0;
306 SignalExceptionInterrupt (1); /* interrupt "1" */
307 }
308 else if (!interrupt_pending)
309 sim_events_schedule (sd, 1, interrupt_event, data);
310}
311
312
313/*---------------------------------------------------------------------------*/
314/*-- Device registration hook -----------------------------------------------*/
315/*---------------------------------------------------------------------------*/
316static void device_init(SIM_DESC sd) {
317#ifdef DEVICE_INIT
318 extern void register_devices(SIM_DESC);
319 register_devices(sd);
320#endif
321}
322
323/*---------------------------------------------------------------------------*/
324/*-- GDB simulator interface ------------------------------------------------*/
325/*---------------------------------------------------------------------------*/
326
7bebb329
MF
327static sim_cia
328mips_pc_get (sim_cpu *cpu)
329{
330 return PC;
331}
332
333static void
334mips_pc_set (sim_cpu *cpu, sim_cia pc)
335{
336 PC = pc;
337}
338
e1211e55
MF
339static int mips_reg_fetch (SIM_CPU *, int, unsigned char *, int);
340static int mips_reg_store (SIM_CPU *, int, unsigned char *, int);
341
c906108c 342SIM_DESC
2e3d4f4d
MF
343sim_open (SIM_OPEN_KIND kind, host_callback *cb,
344 struct bfd *abfd, char * const *argv)
c906108c 345{
7bebb329 346 int i;
8ea7241c
MF
347 SIM_DESC sd = sim_state_alloc_extra (kind, cb,
348 sizeof (struct mips_sim_state));
7bebb329 349 sim_cpu *cpu;
c906108c
SS
350
351 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
352
7bebb329 353 /* The cpu data is kept in a separately allocated chunk of memory. */
d5a71b11 354 if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
7bebb329
MF
355 return 0;
356
357 cpu = STATE_CPU (sd, 0); /* FIXME */
358
c906108c 359 /* FIXME: watchpoints code shouldn't need this */
c906108c
SS
360 STATE_WATCHPOINTS (sd)->interrupt_handler = interrupt_event;
361
362 /* Initialize the mechanism for doing insn profiling. */
363 CPU_INSN_NAME (cpu) = get_insn_name;
364 CPU_MAX_INSNS (cpu) = nr_itable_entries;
365
366 STATE = 0;
367
368 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
369 return 0;
370 sim_add_option_table (sd, NULL, mips_options);
371
372
77cf2ef5 373 /* The parser will print an error message for us, so we silently return. */
c906108c
SS
374 if (sim_parse_args (sd, argv) != SIM_RC_OK)
375 {
376 /* Uninstall the modules to avoid memory leaks,
377 file descriptor leaks, etc. */
378 sim_module_uninstall (sd);
379 return 0;
380 }
381
382 /* handle board-specific memory maps */
383 if (board == NULL)
384 {
385 /* Allocate core managed memory */
14fb6c5a
TS
386 sim_memopt *entry, *match = NULL;
387 address_word mem_size = 0;
388 int mapped = 0;
adf40b2e 389
c906108c
SS
390 /* For compatibility with the old code - under this (at level one)
391 are the kernel spaces K0 & K1. Both of these map to a single
392 smaller sub region */
393 sim_do_command(sd," memory region 0x7fff8000,0x8000") ; /* MTZ- 32 k stack */
14fb6c5a
TS
394
395 /* Look for largest memory region defined on command-line at
396 phys address 0. */
14fb6c5a
TS
397 for (entry = STATE_MEMOPT (sd); entry != NULL; entry = entry->next)
398 {
399 /* If we find an entry at address 0, then we will end up
400 allocating a new buffer in the "memory alias" command
401 below. The region at address 0 will be deleted. */
402 address_word size = (entry->modulo != 0
403 ? entry->modulo : entry->nr_bytes);
404 if (entry->addr == 0
405 && (!match || entry->level < match->level))
406 match = entry;
407 else if (entry->addr == K0BASE || entry->addr == K1BASE)
408 mapped = 1;
409 else
410 {
411 sim_memopt *alias;
412 for (alias = entry->alias; alias != NULL; alias = alias->next)
413 {
414 if (alias->addr == 0
415 && (!match || entry->level < match->level))
416 match = entry;
417 else if (alias->addr == K0BASE || alias->addr == K1BASE)
418 mapped = 1;
419 }
420 }
421 }
422
423 if (!mapped)
424 {
425 if (match)
426 {
427 /* Get existing memory region size. */
428 mem_size = (match->modulo != 0
429 ? match->modulo : match->nr_bytes);
430 /* Delete old region. */
dae666c9 431 sim_do_commandf (sd, "memory delete %d:0x%" PRIxTW "@%d",
14fb6c5a
TS
432 match->space, match->addr, match->level);
433 }
434 else if (mem_size == 0)
435 mem_size = MEM_SIZE;
436 /* Limit to KSEG1 size (512MB) */
437 if (mem_size > K1SIZE)
438 mem_size = K1SIZE;
439 /* memory alias K1BASE@1,K1SIZE%MEMSIZE,K0BASE */
dae666c9 440 sim_do_commandf (sd, "memory alias 0x%x@1,0x%x%%0x%lx,0x%0x",
14fb6c5a 441 K1BASE, K1SIZE, (long)mem_size, K0BASE);
168671c1
FS
442 if (WITH_TARGET_WORD_BITSIZE == 64)
443 sim_do_commandf (sd, "memory alias 0x%x,0x%" PRIxTW ",0x%" PRIxTA,
444 (K0BASE), mem_size, EXTENDED(K0BASE));
14fb6c5a
TS
445 }
446
c906108c
SS
447 device_init(sd);
448 }
43e526b9
JM
449 else if (board != NULL
450 && (strcmp(board, BOARD_BSP) == 0))
451 {
452 int i;
453
454 STATE_ENVIRONMENT (sd) = OPERATING_ENVIRONMENT;
455
456 /* ROM: 0x9FC0_0000 - 0x9FFF_FFFF and 0xBFC0_0000 - 0xBFFF_FFFF */
dae666c9 457 sim_do_commandf (sd, "memory alias 0x%x@1,0x%x,0x%0x",
43e526b9
JM
458 0x9FC00000,
459 4 * 1024 * 1024, /* 4 MB */
460 0xBFC00000);
461
462 /* SRAM: 0x8000_0000 - 0x803F_FFFF and 0xA000_0000 - 0xA03F_FFFF */
dae666c9 463 sim_do_commandf (sd, "memory alias 0x%x@1,0x%x,0x%0x",
43e526b9
JM
464 0x80000000,
465 4 * 1024 * 1024, /* 4 MB */
466 0xA0000000);
467
468 /* DRAM: 0x8800_0000 - 0x89FF_FFFF and 0xA800_0000 - 0xA9FF_FFFF */
469 for (i=0; i<8; i++) /* 32 MB total */
470 {
471 unsigned size = 4 * 1024 * 1024; /* 4 MB */
dae666c9 472 sim_do_commandf (sd, "memory alias 0x%x@1,0x%x,0x%0x",
43e526b9
JM
473 0x88000000 + (i * size),
474 size,
475 0xA8000000 + (i * size));
476 }
477 }
c906108c 478#if (WITH_HW)
43e526b9
JM
479 else if (board != NULL
480 && (strcmp(board, BOARD_JMR3904) == 0 ||
481 strcmp(board, BOARD_JMR3904_PAL) == 0 ||
482 strcmp(board, BOARD_JMR3904_DEBUG) == 0))
c906108c
SS
483 {
484 /* match VIRTUAL memory layout of JMR-TX3904 board */
485 int i;
486
adf40b2e
JM
487 /* --- disable monitor unless forced on by user --- */
488
489 if (! firmware_option_p)
490 {
491 idt_monitor_base = 0;
492 pmon_monitor_base = 0;
493 lsipmon_monitor_base = 0;
494 }
495
c906108c
SS
496 /* --- environment --- */
497
498 STATE_ENVIRONMENT (sd) = OPERATING_ENVIRONMENT;
499
500 /* --- memory --- */
501
502 /* ROM: 0x9FC0_0000 - 0x9FFF_FFFF and 0xBFC0_0000 - 0xBFFF_FFFF */
dae666c9 503 sim_do_commandf (sd, "memory alias 0x%x@1,0x%x,0x%0x",
c906108c
SS
504 0x9FC00000,
505 4 * 1024 * 1024, /* 4 MB */
506 0xBFC00000);
507
508 /* SRAM: 0x8000_0000 - 0x803F_FFFF and 0xA000_0000 - 0xA03F_FFFF */
dae666c9 509 sim_do_commandf (sd, "memory alias 0x%x@1,0x%x,0x%0x",
c906108c
SS
510 0x80000000,
511 4 * 1024 * 1024, /* 4 MB */
512 0xA0000000);
513
514 /* DRAM: 0x8800_0000 - 0x89FF_FFFF and 0xA800_0000 - 0xA9FF_FFFF */
515 for (i=0; i<8; i++) /* 32 MB total */
516 {
517 unsigned size = 4 * 1024 * 1024; /* 4 MB */
dae666c9 518 sim_do_commandf (sd, "memory alias 0x%x@1,0x%x,0x%0x",
c906108c
SS
519 0x88000000 + (i * size),
520 size,
521 0xA8000000 + (i * size));
522 }
523
cb7450ea 524 /* Dummy memory regions for unsimulated devices - sorted by address */
c906108c 525
dae666c9
MF
526 sim_do_commandf (sd, "memory alias 0x%x@1,0x%x", 0xB1000000, 0x400); /* ISA I/O */
527 sim_do_commandf (sd, "memory alias 0x%x@1,0x%x", 0xB2100000, 0x004); /* ISA ctl */
528 sim_do_commandf (sd, "memory alias 0x%x@1,0x%x", 0xB2500000, 0x004); /* LED/switch */
529 sim_do_commandf (sd, "memory alias 0x%x@1,0x%x", 0xB2700000, 0x004); /* RTC */
530 sim_do_commandf (sd, "memory alias 0x%x@1,0x%x", 0xB3C00000, 0x004); /* RTC */
531 sim_do_commandf (sd, "memory alias 0x%x@1,0x%x", 0xFFFF8000, 0x900); /* DRAMC */
532 sim_do_commandf (sd, "memory alias 0x%x@1,0x%x", 0xFFFF9000, 0x200); /* EBIF */
533 sim_do_commandf (sd, "memory alias 0x%x@1,0x%x", 0xFFFFE000, 0x01c); /* EBIF */
534 sim_do_commandf (sd, "memory alias 0x%x@1,0x%x", 0xFFFFF500, 0x300); /* PIO */
cb7450ea 535
c906108c
SS
536
537 /* --- simulated devices --- */
538 sim_hw_parse (sd, "/tx3904irc@0xffffc000/reg 0xffffc000 0x20");
539 sim_hw_parse (sd, "/tx3904cpu");
540 sim_hw_parse (sd, "/tx3904tmr@0xfffff000/reg 0xfffff000 0x100");
541 sim_hw_parse (sd, "/tx3904tmr@0xfffff100/reg 0xfffff100 0x100");
542 sim_hw_parse (sd, "/tx3904tmr@0xfffff200/reg 0xfffff200 0x100");
543 sim_hw_parse (sd, "/tx3904sio@0xfffff300/reg 0xfffff300 0x100");
544 {
545 /* FIXME: poking at dv-sockser internals, use tcp backend if
546 --sockser_addr option was given.*/
8ea881d9 547#ifdef HAVE_DV_SOCKSER
c906108c 548 extern char* sockser_addr;
8ea881d9
MF
549#else
550# define sockser_addr NULL
551#endif
552 if (sockser_addr == NULL)
c906108c
SS
553 sim_hw_parse (sd, "/tx3904sio@0xfffff300/backend stdio");
554 else
555 sim_hw_parse (sd, "/tx3904sio@0xfffff300/backend tcp");
556 }
557 sim_hw_parse (sd, "/tx3904sio@0xfffff400/reg 0xfffff400 0x100");
558 sim_hw_parse (sd, "/tx3904sio@0xfffff400/backend stdio");
559
560 /* -- device connections --- */
561 sim_hw_parse (sd, "/tx3904irc > ip level /tx3904cpu");
562 sim_hw_parse (sd, "/tx3904tmr@0xfffff000 > int tmr0 /tx3904irc");
563 sim_hw_parse (sd, "/tx3904tmr@0xfffff100 > int tmr1 /tx3904irc");
564 sim_hw_parse (sd, "/tx3904tmr@0xfffff200 > int tmr2 /tx3904irc");
565 sim_hw_parse (sd, "/tx3904sio@0xfffff300 > int sio0 /tx3904irc");
566 sim_hw_parse (sd, "/tx3904sio@0xfffff400 > int sio1 /tx3904irc");
567
568 /* add PAL timer & I/O module */
569 if(! strcmp(board, BOARD_JMR3904_PAL))
570 {
571 /* the device */
572 sim_hw_parse (sd, "/pal@0xffff0000");
573 sim_hw_parse (sd, "/pal@0xffff0000/reg 0xffff0000 64");
574
575 /* wire up interrupt ports to irc */
576 sim_hw_parse (sd, "/pal@0x31000000 > countdown tmr0 /tx3904irc");
577 sim_hw_parse (sd, "/pal@0x31000000 > timer tmr1 /tx3904irc");
578 sim_hw_parse (sd, "/pal@0x31000000 > int int0 /tx3904irc");
579 }
580
581 if(! strcmp(board, BOARD_JMR3904_DEBUG))
582 {
583 /* -- DEBUG: glue interrupt generators --- */
584 sim_hw_parse (sd, "/glue@0xffff0000/reg 0xffff0000 0x50");
585 sim_hw_parse (sd, "/glue@0xffff0000 > int0 int0 /tx3904irc");
586 sim_hw_parse (sd, "/glue@0xffff0000 > int1 int1 /tx3904irc");
587 sim_hw_parse (sd, "/glue@0xffff0000 > int2 int2 /tx3904irc");
588 sim_hw_parse (sd, "/glue@0xffff0000 > int3 int3 /tx3904irc");
589 sim_hw_parse (sd, "/glue@0xffff0000 > int4 int4 /tx3904irc");
590 sim_hw_parse (sd, "/glue@0xffff0000 > int5 int5 /tx3904irc");
591 sim_hw_parse (sd, "/glue@0xffff0000 > int6 int6 /tx3904irc");
592 sim_hw_parse (sd, "/glue@0xffff0000 > int7 int7 /tx3904irc");
593 sim_hw_parse (sd, "/glue@0xffff0000 > int8 dmac0 /tx3904irc");
594 sim_hw_parse (sd, "/glue@0xffff0000 > int9 dmac1 /tx3904irc");
595 sim_hw_parse (sd, "/glue@0xffff0000 > int10 dmac2 /tx3904irc");
596 sim_hw_parse (sd, "/glue@0xffff0000 > int11 dmac3 /tx3904irc");
597 sim_hw_parse (sd, "/glue@0xffff0000 > int12 sio0 /tx3904irc");
598 sim_hw_parse (sd, "/glue@0xffff0000 > int13 sio1 /tx3904irc");
599 sim_hw_parse (sd, "/glue@0xffff0000 > int14 tmr0 /tx3904irc");
600 sim_hw_parse (sd, "/glue@0xffff0000 > int15 tmr1 /tx3904irc");
601 sim_hw_parse (sd, "/glue@0xffff0000 > int16 tmr2 /tx3904irc");
602 sim_hw_parse (sd, "/glue@0xffff0000 > int17 nmi /tx3904cpu");
603 }
604
605 device_init(sd);
606 }
607#endif
608
2525df03
NC
609 if (display_mem_info)
610 {
611 struct option_list * ol;
612 struct option_list * prev;
613
614 /* This is a hack. We want to execute the real --memory-info command
615 line switch which is handled in common/sim-memopts.c, not the
616 override we have defined in this file. So we remove the
617 mips_options array from the state options list. This is safe
618 because we have now processed all of the command line. */
619 for (ol = STATE_OPTIONS (sd), prev = NULL;
620 ol != NULL;
621 prev = ol, ol = ol->next)
622 if (ol->options == mips_options)
623 break;
624
625 SIM_ASSERT (ol != NULL);
626
627 if (prev == NULL)
628 STATE_OPTIONS (sd) = ol->next;
629 else
630 prev->next = ol->next;
631
632 sim_do_commandf (sd, "memory-info");
633 }
c906108c
SS
634
635 /* check for/establish the a reference program image */
636 if (sim_analyze_program (sd,
637 (STATE_PROG_ARGV (sd) != NULL
638 ? *STATE_PROG_ARGV (sd)
639 : NULL),
640 abfd) != SIM_RC_OK)
641 {
642 sim_module_uninstall (sd);
643 return 0;
644 }
645
646 /* Configure/verify the target byte order and other runtime
647 configuration options */
648 if (sim_config (sd) != SIM_RC_OK)
649 {
650 sim_module_uninstall (sd);
651 return 0;
652 }
653
654 if (sim_post_argv_init (sd) != SIM_RC_OK)
655 {
656 /* Uninstall the modules to avoid memory leaks,
657 file descriptor leaks, etc. */
658 sim_module_uninstall (sd);
659 return 0;
660 }
661
662 /* verify assumptions the simulator made about the host type system.
663 This macro does not return if there is a problem */
664 SIM_ASSERT (sizeof(int) == (4 * sizeof(char)));
665 SIM_ASSERT (sizeof(word64) == (8 * sizeof(char)));
666
667 /* This is NASTY, in that we are assuming the size of specific
668 registers: */
669 {
670 int rn;
671 for (rn = 0; (rn < (LAST_EMBED_REGNUM + 1)); rn++)
672 {
673 if (rn < 32)
674 cpu->register_widths[rn] = WITH_TARGET_WORD_BITSIZE;
ee7254b0 675 else if ((rn >= FGR_BASE) && (rn < (FGR_BASE + NR_FGR)))
c906108c
SS
676 cpu->register_widths[rn] = WITH_TARGET_FLOATING_POINT_BITSIZE;
677 else if ((rn >= 33) && (rn <= 37))
678 cpu->register_widths[rn] = WITH_TARGET_WORD_BITSIZE;
679 else if ((rn == SRIDX)
680 || (rn == FCR0IDX)
681 || (rn == FCR31IDX)
682 || ((rn >= 72) && (rn <= 89)))
683 cpu->register_widths[rn] = 32;
684 else
685 cpu->register_widths[rn] = 0;
686 }
687
688
689 }
690
c906108c
SS
691 if (STATE & simTRACE)
692 open_trace(sd);
c906108c 693
adf40b2e
JM
694 /*
695 sim_io_eprintf (sd, "idt@%x pmon@%x lsipmon@%x\n",
696 idt_monitor_base,
697 pmon_monitor_base,
698 lsipmon_monitor_base);
699 */
c906108c
SS
700
701 /* Write the monitor trap address handlers into the monitor (eeprom)
702 address space. This can only be done once the target endianness
703 has been determined. */
adf40b2e
JM
704 if (idt_monitor_base != 0)
705 {
706 unsigned loop;
168671c1 707 address_word idt_monitor_size = 1 << 11;
adf40b2e
JM
708
709 /* the default monitor region */
168671c1
FS
710 if (WITH_TARGET_WORD_BITSIZE == 64)
711 sim_do_commandf (sd, "memory alias 0x%x,0x%" PRIxTW ",0x%" PRIxTA,
712 idt_monitor_base, idt_monitor_size,
713 EXTENDED (idt_monitor_base));
714 else
dae666c9 715 sim_do_commandf (sd, "memory region 0x%x,0x%" PRIxTA,
168671c1 716 idt_monitor_base, idt_monitor_size);
adf40b2e
JM
717
718 /* Entry into the IDT monitor is via fixed address vectors, and
719 not using machine instructions. To avoid clashing with use of
720 the MIPS TRAP system, we place our own (simulator specific)
721 "undefined" instructions into the relevant vector slots. */
722 for (loop = 0; (loop < idt_monitor_size); loop += 4)
723 {
724 address_word vaddr = (idt_monitor_base + loop);
725 unsigned32 insn = (RSVD_INSTRUCTION |
726 (((loop >> 2) & RSVD_INSTRUCTION_ARG_MASK)
727 << RSVD_INSTRUCTION_ARG_SHIFT));
728 H2T (insn);
8ac57fbd 729 sim_write (sd, vaddr, (unsigned char *)&insn, sizeof (insn));
adf40b2e
JM
730 }
731 }
732
733 if ((pmon_monitor_base != 0) || (lsipmon_monitor_base != 0))
734 {
c906108c
SS
735 /* The PMON monitor uses the same address space, but rather than
736 branching into it the address of a routine is loaded. We can
737 cheat for the moment, and direct the PMON routine to IDT style
738 instructions within the monitor space. This relies on the IDT
739 monitor not using the locations from 0xBFC00500 onwards as its
740 entry points.*/
adf40b2e
JM
741 unsigned loop;
742 for (loop = 0; (loop < 24); loop++)
743 {
744 unsigned32 value = ((0x500 - 8) / 8); /* default UNDEFINED reason code */
745 switch (loop)
746 {
c906108c
SS
747 case 0: /* read */
748 value = 7;
749 break;
750 case 1: /* write */
751 value = 8;
752 break;
753 case 2: /* open */
754 value = 6;
755 break;
756 case 3: /* close */
757 value = 10;
758 break;
759 case 5: /* printf */
760 value = ((0x500 - 16) / 8); /* not an IDT reason code */
761 break;
762 case 8: /* cliexit */
763 value = 17;
764 break;
765 case 11: /* flush_cache */
766 value = 28;
767 break;
768 }
adf40b2e
JM
769
770 SIM_ASSERT (idt_monitor_base != 0);
771 value = ((unsigned int) idt_monitor_base + (value * 8));
c906108c 772 H2T (value);
c906108c 773
adf40b2e
JM
774 if (pmon_monitor_base != 0)
775 {
776 address_word vaddr = (pmon_monitor_base + (loop * 4));
8ac57fbd 777 sim_write (sd, vaddr, (unsigned char *)&value, sizeof (value));
adf40b2e
JM
778 }
779
780 if (lsipmon_monitor_base != 0)
781 {
782 address_word vaddr = (lsipmon_monitor_base + (loop * 4));
8ac57fbd 783 sim_write (sd, vaddr, (unsigned char *)&value, sizeof (value));
adf40b2e 784 }
c906108c 785 }
adf40b2e
JM
786
787 /* Write an abort sequence into the TRAP (common) exception vector
788 addresses. This is to catch code executing a TRAP (et.al.)
789 instruction without installing a trap handler. */
790 if ((idt_monitor_base != 0) ||
791 (pmon_monitor_base != 0) ||
792 (lsipmon_monitor_base != 0))
793 {
794 unsigned32 halt[2] = { 0x2404002f /* addiu r4, r0, 47 */,
795 HALT_INSTRUCTION /* BREAK */ };
796 H2T (halt[0]);
797 H2T (halt[1]);
8ac57fbd
MF
798 sim_write (sd, 0x80000000, (unsigned char *) halt, sizeof (halt));
799 sim_write (sd, 0x80000180, (unsigned char *) halt, sizeof (halt));
800 sim_write (sd, 0x80000200, (unsigned char *) halt, sizeof (halt));
adf40b2e 801 /* XXX: Write here unconditionally? */
8ac57fbd
MF
802 sim_write (sd, 0xBFC00200, (unsigned char *) halt, sizeof (halt));
803 sim_write (sd, 0xBFC00380, (unsigned char *) halt, sizeof (halt));
804 sim_write (sd, 0xBFC00400, (unsigned char *) halt, sizeof (halt));
adf40b2e 805 }
c906108c
SS
806 }
807
7bebb329
MF
808 /* CPU specific initialization. */
809 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
810 {
811 SIM_CPU *cpu = STATE_CPU (sd, i);
c906108c 812
e1211e55
MF
813 CPU_REG_FETCH (cpu) = mips_reg_fetch;
814 CPU_REG_STORE (cpu) = mips_reg_store;
7bebb329
MF
815 CPU_PC_FETCH (cpu) = mips_pc_get;
816 CPU_PC_STORE (cpu) = mips_pc_set;
817 }
c906108c
SS
818
819 return sd;
820}
821
29bc024d 822#if WITH_TRACE_ANY_P
c906108c 823static void
8ac57fbd 824open_trace (SIM_DESC sd)
c906108c
SS
825{
826 tracefh = fopen(tracefile,"wb+");
827 if (tracefh == NULL)
828 {
829 sim_io_eprintf(sd,"Failed to create file \"%s\", writing trace information to stderr.\n",tracefile);
830 tracefh = stderr;
831 }
832}
29bc024d 833#endif
c906108c
SS
834
835/* Return name of an insn, used by insn profiling. */
836static const char *
837get_insn_name (sim_cpu *cpu, int i)
838{
839 return itable[i].name;
840}
841
842void
6e4f085c 843mips_sim_close (SIM_DESC sd, int quitting)
c906108c 844{
29bc024d 845#if WITH_TRACE_ANY_P
c906108c
SS
846 if (tracefh != NULL && tracefh != stderr)
847 fclose(tracefh);
848 tracefh = NULL;
29bc024d 849#endif
c906108c
SS
850}
851
e1211e55
MF
852static int
853mips_reg_store (SIM_CPU *cpu, int rn, unsigned char *memory, int length)
c906108c 854{
c906108c
SS
855 /* NOTE: gdb (the client) stores registers in target byte order
856 while the simulator uses host byte order */
c906108c
SS
857
858 /* Unfortunately this suffers from the same problem as the register
859 numbering one. We need to know what the width of each logical
860 register number is for the architecture being simulated. */
861
862 if (cpu->register_widths[rn] == 0)
863 {
e1211e55 864 sim_io_eprintf (CPU_STATE (cpu), "Invalid register width for %d (register store ignored)\n", rn);
c906108c
SS
865 return 0;
866 }
867
ee7254b0 868 if (rn >= FGR_BASE && rn < FGR_BASE + NR_FGR)
c906108c 869 {
ee7254b0 870 cpu->fpr_state[rn - FGR_BASE] = fmt_uninterpreted;
c906108c
SS
871 if (cpu->register_widths[rn] == 32)
872 {
a0b3c4fd
JM
873 if (length == 8)
874 {
ee7254b0 875 cpu->fgr[rn - FGR_BASE] =
a0b3c4fd
JM
876 (unsigned32) T2H_8 (*(unsigned64*)memory);
877 return 8;
878 }
879 else
880 {
ee7254b0 881 cpu->fgr[rn - FGR_BASE] = T2H_4 (*(unsigned32*)memory);
a0b3c4fd
JM
882 return 4;
883 }
c906108c
SS
884 }
885 else
886 {
14fb6c5a
TS
887 if (length == 8)
888 {
889 cpu->fgr[rn - FGR_BASE] = T2H_8 (*(unsigned64*)memory);
890 return 8;
891 }
892 else
893 {
894 cpu->fgr[rn - FGR_BASE] = T2H_4 (*(unsigned32*)memory);
895 return 4;
896 }
c906108c
SS
897 }
898 }
899
900 if (cpu->register_widths[rn] == 32)
901 {
a0b3c4fd
JM
902 if (length == 8)
903 {
904 cpu->registers[rn] =
905 (unsigned32) T2H_8 (*(unsigned64*)memory);
906 return 8;
907 }
908 else
909 {
910 cpu->registers[rn] = T2H_4 (*(unsigned32*)memory);
911 return 4;
912 }
c906108c
SS
913 }
914 else
915 {
14fb6c5a
TS
916 if (length == 8)
917 {
918 cpu->registers[rn] = T2H_8 (*(unsigned64*)memory);
919 return 8;
920 }
921 else
922 {
923 cpu->registers[rn] = (signed32) T2H_4(*(unsigned32*)memory);
924 return 4;
925 }
c906108c
SS
926 }
927
928 return 0;
929}
930
e1211e55
MF
931static int
932mips_reg_fetch (SIM_CPU *cpu, int rn, unsigned char *memory, int length)
c906108c 933{
c906108c
SS
934 /* NOTE: gdb (the client) stores registers in target byte order
935 while the simulator uses host byte order */
c906108c
SS
936
937 if (cpu->register_widths[rn] == 0)
938 {
e1211e55 939 sim_io_eprintf (CPU_STATE (cpu), "Invalid register width for %d (register fetch ignored)\n", rn);
c906108c
SS
940 return 0;
941 }
942
c906108c 943 /* Any floating point register */
ee7254b0 944 if (rn >= FGR_BASE && rn < FGR_BASE + NR_FGR)
c906108c
SS
945 {
946 if (cpu->register_widths[rn] == 32)
947 {
a0b3c4fd
JM
948 if (length == 8)
949 {
950 *(unsigned64*)memory =
ee7254b0 951 H2T_8 ((unsigned32) (cpu->fgr[rn - FGR_BASE]));
a0b3c4fd
JM
952 return 8;
953 }
954 else
955 {
ee7254b0 956 *(unsigned32*)memory = H2T_4 (cpu->fgr[rn - FGR_BASE]);
a0b3c4fd
JM
957 return 4;
958 }
c906108c
SS
959 }
960 else
961 {
14fb6c5a
TS
962 if (length == 8)
963 {
964 *(unsigned64*)memory = H2T_8 (cpu->fgr[rn - FGR_BASE]);
965 return 8;
966 }
967 else
968 {
969 *(unsigned32*)memory = H2T_4 ((unsigned32)(cpu->fgr[rn - FGR_BASE]));
970 return 4;
971 }
c906108c
SS
972 }
973 }
974
975 if (cpu->register_widths[rn] == 32)
976 {
a0b3c4fd
JM
977 if (length == 8)
978 {
979 *(unsigned64*)memory =
980 H2T_8 ((unsigned32) (cpu->registers[rn]));
981 return 8;
982 }
983 else
984 {
985 *(unsigned32*)memory = H2T_4 ((unsigned32)(cpu->registers[rn]));
986 return 4;
987 }
c906108c
SS
988 }
989 else
990 {
14fb6c5a
TS
991 if (length == 8)
992 {
993 *(unsigned64*)memory =
994 H2T_8 ((unsigned64) (cpu->registers[rn]));
995 return 8;
996 }
997 else
998 {
999 *(unsigned32*)memory = H2T_4 ((unsigned32)(cpu->registers[rn]));
1000 return 4;
1001 }
c906108c
SS
1002 }
1003
1004 return 0;
1005}
1006
c906108c 1007SIM_RC
2e3d4f4d
MF
1008sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
1009 char * const *argv, char * const *env)
c906108c
SS
1010{
1011
1012#ifdef DEBUG
1013#if 0 /* FIXME: doesn't compile */
1014 printf("DBG: sim_create_inferior entered: start_address = 0x%s\n",
1015 pr_addr(PC));
1016#endif
1017#endif /* DEBUG */
1018
1019 ColdReset(sd);
1020
1021 if (abfd != NULL)
1022 {
1023 /* override PC value set by ColdReset () */
1024 int cpu_nr;
1025 for (cpu_nr = 0; cpu_nr < sim_engine_nr_cpus (sd); cpu_nr++)
1026 {
1027 sim_cpu *cpu = STATE_CPU (sd, cpu_nr);
b36d953b
MF
1028 sim_cia pc = bfd_get_start_address (abfd);
1029
b312488f
FS
1030 /* The 64-bit BFD sign-extends MIPS addresses to model
1031 32-bit compatibility segments with 64-bit addressing.
1032 These addresses work as is on 64-bit targets but
1033 can be truncated for 32-bit targets. */
1034 if (WITH_TARGET_WORD_BITSIZE == 32)
b36d953b
MF
1035 pc = (unsigned32) pc;
1036
1037 CPU_PC_SET (cpu, pc);
c906108c
SS
1038 }
1039 }
1040
1041#if 0 /* def DEBUG */
1042 if (argv || env)
1043 {
1044 /* We should really place the argv slot values into the argument
1045 registers, and onto the stack as required. However, this
1046 assumes that we have a stack defined, which is not
1047 necessarily true at the moment. */
1048 char **cptr;
1049 sim_io_printf(sd,"sim_create_inferior() : passed arguments ignored\n");
1050 for (cptr = argv; (cptr && *cptr); cptr++)
1051 printf("DBG: arg \"%s\"\n",*cptr);
1052 }
1053#endif /* DEBUG */
1054
1055 return SIM_RC_OK;
1056}
1057
c906108c
SS
1058/*---------------------------------------------------------------------------*/
1059/*-- Private simulator support interface ------------------------------------*/
1060/*---------------------------------------------------------------------------*/
1061
1062/* Read a null terminated string from memory, return in a buffer */
1063static char *
1064fetch_str (SIM_DESC sd,
1065 address_word addr)
1066{
1067 char *buf;
1068 int nr = 0;
8ac57fbd 1069 unsigned char null;
c906108c
SS
1070 while (sim_read (sd, addr + nr, &null, 1) == 1 && null != 0)
1071 nr++;
1072 buf = NZALLOC (char, nr + 1);
8ac57fbd 1073 sim_read (sd, addr, (unsigned char *)buf, nr);
c906108c
SS
1074 return buf;
1075}
1076
adf40b2e
JM
1077
1078/* Implements the "sim firmware" command:
1079 sim firmware NAME[@ADDRESS] --- emulate ROM monitor named NAME.
1080 NAME can be idt, pmon, or lsipmon. If omitted, ADDRESS
1081 defaults to the normal address for that monitor.
1082 sim firmware none --- don't emulate any ROM monitor. Useful
1083 if you need a clean address space. */
1084static SIM_RC
1085sim_firmware_command (SIM_DESC sd, char *arg)
1086{
1087 int address_present = 0;
1088 SIM_ADDR address;
1089
1090 /* Signal occurrence of this option. */
1091 firmware_option_p = 1;
1092
1093 /* Parse out the address, if present. */
1094 {
1095 char *p = strchr (arg, '@');
1096 if (p)
1097 {
1098 char *q;
1099 address_present = 1;
1100 p ++; /* skip over @ */
1101
1102 address = strtoul (p, &q, 0);
1103 if (*q != '\0')
1104 {
1105 sim_io_printf (sd, "Invalid address given to the"
1106 "`sim firmware NAME@ADDRESS' command: %s\n",
1107 p);
1108 return SIM_RC_FAIL;
1109 }
1110 }
1111 else
b4b6c939
AC
1112 {
1113 address_present = 0;
1114 address = -1; /* Dummy value. */
1115 }
adf40b2e
JM
1116 }
1117
1118 if (! strncmp (arg, "idt", 3))
1119 {
1120 idt_monitor_base = address_present ? address : 0xBFC00000;
1121 pmon_monitor_base = 0;
1122 lsipmon_monitor_base = 0;
1123 }
1124 else if (! strncmp (arg, "pmon", 4))
1125 {
1126 /* pmon uses indirect calls. Hook into implied idt. */
1127 pmon_monitor_base = address_present ? address : 0xBFC00500;
1128 idt_monitor_base = pmon_monitor_base - 0x500;
1129 lsipmon_monitor_base = 0;
1130 }
1131 else if (! strncmp (arg, "lsipmon", 7))
1132 {
1133 /* lsipmon uses indirect calls. Hook into implied idt. */
1134 pmon_monitor_base = 0;
1135 lsipmon_monitor_base = address_present ? address : 0xBFC00200;
1136 idt_monitor_base = lsipmon_monitor_base - 0x200;
1137 }
1138 else if (! strncmp (arg, "none", 4))
1139 {
1140 if (address_present)
1141 {
1142 sim_io_printf (sd,
1143 "The `sim firmware none' command does "
1144 "not take an `ADDRESS' argument.\n");
1145 return SIM_RC_FAIL;
1146 }
1147 idt_monitor_base = 0;
1148 pmon_monitor_base = 0;
1149 lsipmon_monitor_base = 0;
1150 }
1151 else
1152 {
1153 sim_io_printf (sd, "\
1154Unrecognized name given to the `sim firmware NAME' command: %s\n\
1155Recognized firmware names are: `idt', `pmon', `lsipmon', and `none'.\n",
1156 arg);
1157 return SIM_RC_FAIL;
1158 }
1159
1160 return SIM_RC_OK;
1161}
1162
5c6f091a
FS
1163/* stat structures from MIPS32/64. */
1164static const char stat32_map[] =
1165"st_dev,2:st_ino,2:st_mode,4:st_nlink,2:st_uid,2:st_gid,2"
1166":st_rdev,2:st_size,4:st_atime,4:st_spare1,4:st_mtime,4:st_spare2,4"
1167":st_ctime,4:st_spare3,4:st_blksize,4:st_blocks,4:st_spare4,8";
1168
1169static const char stat64_map[] =
1170"st_dev,2:st_ino,2:st_mode,4:st_nlink,2:st_uid,2:st_gid,2"
1171":st_rdev,2:st_size,8:st_atime,8:st_spare1,8:st_mtime,8:st_spare2,8"
1172":st_ctime,8:st_spare3,8:st_blksize,8:st_blocks,8:st_spare4,16";
1173
1174/* Map for calls using the host struct stat. */
1175static const CB_TARGET_DEFS_MAP CB_stat_map[] =
1176{
1177 { "stat", CB_SYS_stat, 15 },
1178 { 0, -1, -1 }
1179};
adf40b2e
JM
1180
1181
c906108c 1182/* Simple monitor interface (currently setup for the IDT and PMON monitors) */
8030f857 1183int
c906108c
SS
1184sim_monitor (SIM_DESC sd,
1185 sim_cpu *cpu,
1186 address_word cia,
1187 unsigned int reason)
1188{
1189#ifdef DEBUG
1190 printf("DBG: sim_monitor: entered (reason = %d)\n",reason);
1191#endif /* DEBUG */
1192
1193 /* The IDT monitor actually allows two instructions per vector
1194 slot. However, the simulator currently causes a trap on each
1195 individual instruction. We cheat, and lose the bottom bit. */
1196 reason >>= 1;
1197
1198 /* The following callback functions are available, however the
1199 monitor we are simulating does not make use of them: get_errno,
5c6f091a 1200 isatty, rename, system and time. */
c906108c
SS
1201 switch (reason)
1202 {
1203
1204 case 6: /* int open(char *path,int flags) */
1205 {
1206 char *path = fetch_str (sd, A0);
1207 V0 = sim_io_open (sd, path, (int)A1);
d79fe0d6 1208 free (path);
c906108c
SS
1209 break;
1210 }
1211
1212 case 7: /* int read(int file,char *ptr,int len) */
1213 {
1214 int fd = A0;
1215 int nr = A2;
1216 char *buf = zalloc (nr);
1217 V0 = sim_io_read (sd, fd, buf, nr);
8ac57fbd 1218 sim_write (sd, A1, (unsigned char *)buf, nr);
d79fe0d6 1219 free (buf);
c906108c
SS
1220 }
1221 break;
1222
1223 case 8: /* int write(int file,char *ptr,int len) */
1224 {
1225 int fd = A0;
1226 int nr = A2;
1227 char *buf = zalloc (nr);
8ac57fbd 1228 sim_read (sd, A1, (unsigned char *)buf, nr);
c906108c 1229 V0 = sim_io_write (sd, fd, buf, nr);
f8df4c77
TS
1230 if (fd == 1)
1231 sim_io_flush_stdout (sd);
1232 else if (fd == 2)
1233 sim_io_flush_stderr (sd);
d79fe0d6 1234 free (buf);
c906108c
SS
1235 break;
1236 }
1237
1238 case 10: /* int close(int file) */
1239 {
1240 V0 = sim_io_close (sd, (int)A0);
1241 break;
1242 }
1243
1244 case 2: /* Densan monitor: char inbyte(int waitflag) */
1245 {
1246 if (A0 == 0) /* waitflag == NOWAIT */
1247 V0 = (unsigned_word)-1;
1248 }
1249 /* Drop through to case 11 */
1250
1251 case 11: /* char inbyte(void) */
1252 {
1253 char tmp;
43e526b9
JM
1254 /* ensure that all output has gone... */
1255 sim_io_flush_stdout (sd);
c906108c
SS
1256 if (sim_io_read_stdin (sd, &tmp, sizeof(char)) != sizeof(char))
1257 {
1258 sim_io_error(sd,"Invalid return from character read");
1259 V0 = (unsigned_word)-1;
1260 }
1261 else
1262 V0 = (unsigned_word)tmp;
1263 break;
1264 }
1265
1266 case 3: /* Densan monitor: void co(char chr) */
1267 case 12: /* void outbyte(char chr) : write a byte to "stdout" */
1268 {
1269 char tmp = (char)(A0 & 0xFF);
1270 sim_io_write_stdout (sd, &tmp, sizeof(char));
1271 break;
1272 }
1273
5c6f091a
FS
1274 case 13: /* int unlink(const char *path) */
1275 {
1276 char *path = fetch_str (sd, A0);
1277 V0 = sim_io_unlink (sd, path);
1278 free (path);
1279 break;
1280 }
1281
1282 case 14: /* int lseek(int fd, int offset, int whence) */
1283 {
1284 V0 = sim_io_lseek (sd, A0, A1, A2);
1285 break;
1286 }
1287
1288 case 15: /* int stat(const char *path, struct stat *buf); */
1289 {
1290 /* As long as the infrastructure doesn't cache anything
1291 related to the stat mapping, this trick gets us a dual
1292 "struct stat"-type mapping in the least error-prone way. */
1293 host_callback *cb = STATE_CALLBACK (sd);
1294 const char *saved_map = cb->stat_map;
1295 CB_TARGET_DEFS_MAP *saved_syscall_map = cb->syscall_map;
1296 bfd *prog_bfd = STATE_PROG_BFD (sd);
1297 int is_elf32bit = (elf_elfheader(prog_bfd)->e_ident[EI_CLASS] ==
1298 ELFCLASS32);
1299 static CB_SYSCALL s;
1300 CB_SYSCALL_INIT (&s);
1301 s.func = 15;
1302 /* Mask out the sign extension part for 64-bit targets because the
1303 MIPS simulator's memory model is still 32-bit. */
1304 s.arg1 = A0 & 0xFFFFFFFF;
1305 s.arg2 = A1 & 0xFFFFFFFF;
1306 s.p1 = (PTR) sd;
1307 s.p2 = (PTR) cpu;
1308 s.read_mem = sim_syscall_read_mem;
1309 s.write_mem = sim_syscall_write_mem;
1310
1311 cb->syscall_map = (CB_TARGET_DEFS_MAP *) CB_stat_map;
1312 cb->stat_map = is_elf32bit ? stat32_map : stat64_map;
1313
1314 if (cb_syscall (cb, &s) != CB_RC_OK)
1315 sim_engine_halt (sd, cpu, NULL, mips_pc_get (cpu),
1316 sim_stopped, SIM_SIGILL);
1317
1318 V0 = s.result;
1319 cb->stat_map = saved_map;
1320 cb->syscall_map = saved_syscall_map;
1321 break;
1322 }
1323
c906108c
SS
1324 case 17: /* void _exit() */
1325 {
1326 sim_io_eprintf (sd, "sim_monitor(17): _exit(int reason) to be coded\n");
1327 sim_engine_halt (SD, CPU, NULL, NULL_CIA, sim_exited,
1328 (unsigned int)(A0 & 0xFFFFFFFF));
1329 break;
1330 }
1331
e80fc152 1332 case 28: /* PMON flush_cache */
c906108c
SS
1333 break;
1334
1335 case 55: /* void get_mem_info(unsigned int *ptr) */
1336 /* in: A0 = pointer to three word memory location */
1337 /* out: [A0 + 0] = size */
1338 /* [A0 + 4] = instruction cache size */
1339 /* [A0 + 8] = data cache size */
1340 {
14fb6c5a 1341 unsigned_4 value;
c906108c 1342 unsigned_4 zero = 0;
14fb6c5a
TS
1343 address_word mem_size;
1344 sim_memopt *entry, *match = NULL;
1345
1346 /* Search for memory region mapped to KSEG0 or KSEG1. */
1347 for (entry = STATE_MEMOPT (sd);
1348 entry != NULL;
1349 entry = entry->next)
1350 {
1351 if ((entry->addr == K0BASE || entry->addr == K1BASE)
1352 && (!match || entry->level < match->level))
1353 match = entry;
1354 else
1355 {
1356 sim_memopt *alias;
1357 for (alias = entry->alias;
1358 alias != NULL;
1359 alias = alias->next)
1360 if ((alias->addr == K0BASE || alias->addr == K1BASE)
1361 && (!match || entry->level < match->level))
1362 match = entry;
1363 }
1364 }
1365
1366 /* Get region size, limit to KSEG1 size (512MB). */
1367 SIM_ASSERT (match != NULL);
1368 mem_size = (match->modulo != 0
1369 ? match->modulo : match->nr_bytes);
1370 if (mem_size > K1SIZE)
1371 mem_size = K1SIZE;
1372
1373 value = mem_size;
c906108c 1374 H2T (value);
8ac57fbd
MF
1375 sim_write (sd, A0 + 0, (unsigned char *)&value, 4);
1376 sim_write (sd, A0 + 4, (unsigned char *)&zero, 4);
1377 sim_write (sd, A0 + 8, (unsigned char *)&zero, 4);
5accf1ff 1378 /* sim_io_eprintf (sd, "sim: get_mem_info() deprecated\n"); */
c906108c
SS
1379 break;
1380 }
1381
e80fc152 1382 case 158: /* PMON printf */
c906108c
SS
1383 /* in: A0 = pointer to format string */
1384 /* A1 = optional argument 1 */
1385 /* A2 = optional argument 2 */
1386 /* A3 = optional argument 3 */
1387 /* out: void */
1388 /* The following is based on the PMON printf source */
1389 {
1390 address_word s = A0;
8ac57fbd 1391 unsigned char c;
6828a302 1392 address_word *ap = &A1; /* 1st argument */
c906108c
SS
1393 /* This isn't the quickest way, since we call the host print
1394 routine for every character almost. But it does avoid
1395 having to allocate and manage a temporary string buffer. */
1396 /* TODO: Include check that we only use three arguments (A1,
1397 A2 and A3) */
1398 while (sim_read (sd, s++, &c, 1) && c != '\0')
1399 {
1400 if (c == '%')
1401 {
1402 char tmp[40];
1403 enum {FMT_RJUST, FMT_LJUST, FMT_RJUST0, FMT_CENTER} fmt = FMT_RJUST;
1404 int width = 0, trunc = 0, haddot = 0, longlong = 0;
1405 while (sim_read (sd, s++, &c, 1) && c != '\0')
1406 {
1407 if (strchr ("dobxXulscefg%", c))
1408 break;
1409 else if (c == '-')
1410 fmt = FMT_LJUST;
1411 else if (c == '0')
1412 fmt = FMT_RJUST0;
1413 else if (c == '~')
1414 fmt = FMT_CENTER;
1415 else if (c == '*')
1416 {
1417 if (haddot)
1418 trunc = (int)*ap++;
1419 else
1420 width = (int)*ap++;
1421 }
1422 else if (c >= '1' && c <= '9')
1423 {
1424 address_word t = s;
1425 unsigned int n;
1426 while (sim_read (sd, s++, &c, 1) == 1 && isdigit (c))
1427 tmp[s - t] = c;
1428 tmp[s - t] = '\0';
1429 n = (unsigned int)strtol(tmp,NULL,10);
1430 if (haddot)
1431 trunc = n;
1432 else
1433 width = n;
1434 s--;
1435 }
1436 else if (c == '.')
1437 haddot = 1;
1438 }
1439 switch (c)
1440 {
1441 case '%':
1442 sim_io_printf (sd, "%%");
1443 break;
1444 case 's':
1445 if ((int)*ap != 0)
1446 {
1447 address_word p = *ap++;
8ac57fbd 1448 unsigned char ch;
c906108c
SS
1449 while (sim_read (sd, p++, &ch, 1) == 1 && ch != '\0')
1450 sim_io_printf(sd, "%c", ch);
1451 }
1452 else
1453 sim_io_printf(sd,"(null)");
1454 break;
1455 case 'c':
1456 sim_io_printf (sd, "%c", (int)*ap++);
1457 break;
1458 default:
1459 if (c == 'l')
1460 {
1461 sim_read (sd, s++, &c, 1);
1462 if (c == 'l')
1463 {
1464 longlong = 1;
1465 sim_read (sd, s++, &c, 1);
1466 }
1467 }
1468 if (strchr ("dobxXu", c))
1469 {
1470 word64 lv = (word64) *ap++;
1471 if (c == 'b')
1472 sim_io_printf(sd,"<binary not supported>");
1473 else
1474 {
6828a302
MF
1475#define _P(c, fmt64, fmt32) \
1476 case c: \
1477 if (longlong) \
1478 sim_io_printf (sd, "%" fmt64, lv); \
1479 else \
1480 sim_io_printf (sd, "%" fmt32, (int)lv); \
1481 break;
1482#define P(c, fmtc) _P(c, PRI##fmtc##64, PRI##fmtc##32)
1483 switch (c)
1484 {
1485 P('d', d)
1486 P('o', o)
1487 P('x', x)
1488 P('X', X)
1489 P('u', u)
1490 }
c906108c 1491 }
6828a302
MF
1492#undef P
1493#undef _P
c906108c
SS
1494 }
1495 else if (strchr ("eEfgG", c))
1496 {
1497 double dbl = *(double*)(ap++);
6828a302
MF
1498
1499#define P(c, fmtc) \
1500 case c: \
1501 sim_io_printf (sd, "%*.*" #fmtc, width, trunc, dbl); \
1502 break;
1503 switch (c)
1504 {
1505 P('e', e)
1506 P('E', E)
1507 P('f', f)
1508 P('g', g)
1509 P('G', G)
1510 }
1511#undef P
c906108c
SS
1512 trunc = 0;
1513 }
1514 }
1515 }
1516 else
1517 sim_io_printf(sd, "%c", c);
1518 }
1519 break;
1520 }
1521
1522 default:
8030f857
BE
1523 /* Unknown reason. */
1524 return 0;
c906108c 1525 }
8030f857 1526 return 1;
c906108c
SS
1527}
1528
1529/* Store a word into memory. */
1530
1531static void
1532store_word (SIM_DESC sd,
1533 sim_cpu *cpu,
1534 address_word cia,
1535 uword64 vaddr,
1536 signed_word val)
1537{
26f8bf63 1538 address_word paddr = vaddr;
c906108c
SS
1539
1540 if ((vaddr & 3) != 0)
1541 SignalExceptionAddressStore ();
1542 else
1543 {
26f8bf63
MF
1544 const uword64 mask = 7;
1545 uword64 memval;
1546 unsigned int byte;
1547
1548 paddr = (paddr & ~mask) | ((paddr & mask) ^ (ReverseEndian << 2));
1549 byte = (vaddr & mask) ^ (BigEndianCPU << 2);
1550 memval = ((uword64) val) << (8 * byte);
1551 StoreMemory (AccessLength_WORD, memval, 0, paddr, vaddr,
1552 isREAL);
c906108c
SS
1553 }
1554}
1555
1556/* Load a word from memory. */
1557
1558static signed_word
1559load_word (SIM_DESC sd,
1560 sim_cpu *cpu,
1561 address_word cia,
1562 uword64 vaddr)
1563{
1564 if ((vaddr & 3) != 0)
1565 {
1566 SIM_CORE_SIGNAL (SD, cpu, cia, read_map, AccessLength_WORD+1, vaddr, read_transfer, sim_core_unaligned_signal);
1567 }
1568 else
1569 {
26f8bf63
MF
1570 address_word paddr = vaddr;
1571 const uword64 mask = 0x7;
1572 const unsigned int reverse = ReverseEndian ? 1 : 0;
1573 const unsigned int bigend = BigEndianCPU ? 1 : 0;
1574 uword64 memval;
1575 unsigned int byte;
1576
1577 paddr = (paddr & ~mask) | ((paddr & mask) ^ (reverse << 2));
1578 LoadMemory (&memval, NULL, AccessLength_WORD, paddr, vaddr, isDATA,
1579 isREAL);
1580 byte = (vaddr & mask) ^ (bigend << 2);
1581 return EXTEND32 (memval >> (8 * byte));
c906108c
SS
1582 }
1583
1584 return 0;
1585}
1586
1587/* Simulate the mips16 entry and exit pseudo-instructions. These
1588 would normally be handled by the reserved instruction exception
1589 code, but for ease of simulation we just handle them directly. */
1590
1591static void
1592mips16_entry (SIM_DESC sd,
1593 sim_cpu *cpu,
1594 address_word cia,
1595 unsigned int insn)
1596{
1597 int aregs, sregs, rreg;
1598
1599#ifdef DEBUG
1600 printf("DBG: mips16_entry: entered (insn = 0x%08X)\n",insn);
1601#endif /* DEBUG */
1602
1603 aregs = (insn & 0x700) >> 8;
1604 sregs = (insn & 0x0c0) >> 6;
1605 rreg = (insn & 0x020) >> 5;
1606
1607 /* This should be checked by the caller. */
1608 if (sregs == 3)
1609 abort ();
1610
1611 if (aregs < 5)
1612 {
1613 int i;
1614 signed_word tsp;
1615
1616 /* This is the entry pseudo-instruction. */
1617
1618 for (i = 0; i < aregs; i++)
1619 store_word (SD, CPU, cia, (uword64) (SP + 4 * i), GPR[i + 4]);
1620
1621 tsp = SP;
1622 SP -= 32;
1623
1624 if (rreg)
1625 {
1626 tsp -= 4;
1627 store_word (SD, CPU, cia, (uword64) tsp, RA);
1628 }
1629
1630 for (i = 0; i < sregs; i++)
1631 {
1632 tsp -= 4;
1633 store_word (SD, CPU, cia, (uword64) tsp, GPR[16 + i]);
1634 }
1635 }
1636 else
1637 {
1638 int i;
1639 signed_word tsp;
1640
1641 /* This is the exit pseudo-instruction. */
1642
1643 tsp = SP + 32;
1644
1645 if (rreg)
1646 {
1647 tsp -= 4;
1648 RA = load_word (SD, CPU, cia, (uword64) tsp);
1649 }
1650
1651 for (i = 0; i < sregs; i++)
1652 {
1653 tsp -= 4;
1654 GPR[i + 16] = load_word (SD, CPU, cia, (uword64) tsp);
1655 }
1656
1657 SP += 32;
1658
1659 if (CURRENT_FLOATING_POINT == HARD_FLOATING_POINT)
1660 {
1661 if (aregs == 5)
1662 {
1663 FGR[0] = WORD64LO (GPR[4]);
1664 FPR_STATE[0] = fmt_uninterpreted;
1665 }
1666 else if (aregs == 6)
1667 {
1668 FGR[0] = WORD64LO (GPR[5]);
1669 FGR[1] = WORD64LO (GPR[4]);
1670 FPR_STATE[0] = fmt_uninterpreted;
1671 FPR_STATE[1] = fmt_uninterpreted;
1672 }
1673 }
1674
1675 PC = RA;
1676 }
1677
1678}
1679
1680/*-- trace support ----------------------------------------------------------*/
1681
29bc024d 1682/* The trace support is provided (if required) in the memory accessing
c906108c
SS
1683 routines. Since we are also providing the architecture specific
1684 features, the architecture simulation code can also deal with
29bc024d 1685 notifying the trace world of cache flushes, etc. Similarly we do
c906108c
SS
1686 not need to provide profiling support in the simulator engine,
1687 since we can sample in the instruction fetch control loop. By
29bc024d 1688 defining the trace manifest, we add tracing as a run-time
c906108c
SS
1689 option. */
1690
29bc024d 1691#if WITH_TRACE_ANY_P
c906108c
SS
1692/* Tracing by default produces "din" format (as required by
1693 dineroIII). Each line of such a trace file *MUST* have a din label
1694 and address field. The rest of the line is ignored, so comments can
1695 be included if desired. The first field is the label which must be
1696 one of the following values:
1697
1698 0 read data
1699 1 write data
1700 2 instruction fetch
1701 3 escape record (treated as unknown access type)
1702 4 escape record (causes cache flush)
1703
1704 The address field is a 32bit (lower-case) hexadecimal address
1705 value. The address should *NOT* be preceded by "0x".
1706
1707 The size of the memory transfer is not important when dealing with
1708 cache lines (as long as no more than a cache line can be
1709 transferred in a single operation :-), however more information
1710 could be given following the dineroIII requirement to allow more
1711 complete memory and cache simulators to provide better
1712 results. i.e. the University of Pisa has a cache simulator that can
1713 also take bus size and speed as (variable) inputs to calculate
1714 complete system performance (a much more useful ability when trying
1715 to construct an end product, rather than a processor). They
1716 currently have an ARM version of their tool called ChARM. */
1717
1718
1719void
1720dotrace (SIM_DESC sd,
1721 sim_cpu *cpu,
1722 FILE *tracefh,
1723 int type,
1724 SIM_ADDR address,
1725 int width,
b80d4475 1726 const char *comment, ...)
c906108c
SS
1727{
1728 if (STATE & simTRACE) {
1729 va_list ap;
1730 fprintf(tracefh,"%d %s ; width %d ; ",
1731 type,
1732 pr_addr(address),
1733 width);
1734 va_start(ap,comment);
1735 vfprintf(tracefh,comment,ap);
1736 va_end(ap);
1737 fprintf(tracefh,"\n");
1738 }
1739 /* NOTE: Since the "din" format will only accept 32bit addresses, and
1740 we may be generating 64bit ones, we should put the hi-32bits of the
1741 address into the comment field. */
1742
1743 /* TODO: Provide a buffer for the trace lines. We can then avoid
1744 performing writes until the buffer is filled, or the file is
1745 being closed. */
1746
1747 /* NOTE: We could consider adding a comment field to the "din" file
1748 produced using type 3 markers (unknown access). This would then
1749 allow information about the program that the "din" is for, and
1750 the MIPs world that was being simulated, to be placed into the
1751 trace file. */
1752
1753 return;
1754}
29bc024d 1755#endif /* WITH_TRACE_ANY_P */
c906108c
SS
1756
1757/*---------------------------------------------------------------------------*/
1758/*-- simulator engine -------------------------------------------------------*/
1759/*---------------------------------------------------------------------------*/
1760
1761static void
1762ColdReset (SIM_DESC sd)
1763{
1764 int cpu_nr;
1765 for (cpu_nr = 0; cpu_nr < sim_engine_nr_cpus (sd); cpu_nr++)
1766 {
1767 sim_cpu *cpu = STATE_CPU (sd, cpu_nr);
1768 /* RESET: Fixed PC address: */
1769 PC = (unsigned_word) UNSIGNED64 (0xFFFFFFFFBFC00000);
1770 /* The reset vector address is in the unmapped, uncached memory space. */
1771
1772 SR &= ~(status_SR | status_TS | status_RP);
1773 SR |= (status_ERL | status_BEV);
1774
1775 /* Cheat and allow access to the complete register set immediately */
1776 if (CURRENT_FLOATING_POINT == HARD_FLOATING_POINT
1777 && WITH_TARGET_WORD_BITSIZE == 64)
1778 SR |= status_FR; /* 64bit registers */
1779
1780 /* Ensure that any instructions with pending register updates are
1781 cleared: */
1782 PENDING_INVALIDATE();
1783
1784 /* Initialise the FPU registers to the unknown state */
1785 if (CURRENT_FLOATING_POINT == HARD_FLOATING_POINT)
1786 {
1787 int rn;
1788 for (rn = 0; (rn < 32); rn++)
1789 FPR_STATE[rn] = fmt_uninterpreted;
1790 }
1791
07802d98
TS
1792 /* Initialise the Config0 register. */
1793 C0_CONFIG = 0x80000000 /* Config1 present */
1794 | 2; /* KSEG0 uncached */
1795 if (WITH_TARGET_WORD_BITSIZE == 64)
1796 {
1797 /* FIXME Currently mips/sim-main.c:address_translation()
1798 truncates all addresses to 32-bits. */
1799 if (0 && WITH_TARGET_ADDRESS_BITSIZE == 64)
1800 C0_CONFIG |= (2 << 13); /* MIPS64, 64-bit addresses */
1801 else
1802 C0_CONFIG |= (1 << 13); /* MIPS64, 32-bit addresses */
1803 }
1804 if (BigEndianMem)
1805 C0_CONFIG |= 0x00008000; /* Big Endian */
c906108c
SS
1806 }
1807}
1808
1809
1810
1811
1812/* Description from page A-26 of the "MIPS IV Instruction Set" manual (revision 3.1) */
1813/* Signal an exception condition. This will result in an exception
1814 that aborts the instruction. The instruction operation pseudocode
1815 will never see a return from this function call. */
1816
1817void
1818signal_exception (SIM_DESC sd,
1819 sim_cpu *cpu,
1820 address_word cia,
1821 int exception,...)
1822{
1823 /* int vector; */
1824
1825#ifdef DEBUG
1826 sim_io_printf(sd,"DBG: SignalException(%d) PC = 0x%s\n",exception,pr_addr(cia));
1827#endif /* DEBUG */
1828
1829 /* Ensure that any active atomic read/modify/write operation will fail: */
1830 LLBIT = 0;
1831
1832 /* Save registers before interrupt dispatching */
1833#ifdef SIM_CPU_EXCEPTION_TRIGGER
1834 SIM_CPU_EXCEPTION_TRIGGER(sd, cpu, cia);
1835#endif
1836
1837 switch (exception) {
1838
e80fc152 1839 case DebugBreakPoint:
c906108c
SS
1840 if (! (Debug & Debug_DM))
1841 {
1842 if (INDELAYSLOT())
1843 {
1844 CANCELDELAYSLOT();
1845
1846 Debug |= Debug_DBD; /* signaled from within in delay slot */
1847 DEPC = cia - 4; /* reference the branch instruction */
1848 }
1849 else
1850 {
1851 Debug &= ~Debug_DBD; /* not signaled from within a delay slot */
1852 DEPC = cia;
1853 }
1854
1855 Debug |= Debug_DM; /* in debugging mode */
1856 Debug |= Debug_DBp; /* raising a DBp exception */
1857 PC = 0xBFC00200;
1858 sim_engine_restart (SD, CPU, NULL, NULL_CIA);
1859 }
1860 break;
1861
e80fc152 1862 case ReservedInstruction:
c906108c
SS
1863 {
1864 va_list ap;
1865 unsigned int instruction;
1866 va_start(ap,exception);
1867 instruction = va_arg(ap,unsigned int);
1868 va_end(ap);
1869 /* Provide simple monitor support using ReservedInstruction
1870 exceptions. The following code simulates the fixed vector
1871 entry points into the IDT monitor by causing a simulator
1872 trap, performing the monitor operation, and returning to
1873 the address held in the $ra register (standard PCS return
1874 address). This means we only need to pre-load the vector
1875 space with suitable instruction values. For systems were
1876 actual trap instructions are used, we would not need to
1877 perform this magic. */
1878 if ((instruction & RSVD_INSTRUCTION_MASK) == RSVD_INSTRUCTION)
1879 {
8030f857
BE
1880 int reason = (instruction >> RSVD_INSTRUCTION_ARG_SHIFT) & RSVD_INSTRUCTION_ARG_MASK;
1881 if (!sim_monitor (SD, CPU, cia, reason))
1882 sim_io_error (sd, "sim_monitor: unhandled reason = %d, pc = 0x%s\n", reason, pr_addr (cia));
1883
c906108c
SS
1884 /* NOTE: This assumes that a branch-and-link style
1885 instruction was used to enter the vector (which is the
1886 case with the current IDT monitor). */
1887 sim_engine_restart (SD, CPU, NULL, RA);
1888 }
1889 /* Look for the mips16 entry and exit instructions, and
1890 simulate a handler for them. */
1891 else if ((cia & 1) != 0
1892 && (instruction & 0xf81f) == 0xe809
1893 && (instruction & 0x0c0) != 0x0c0)
1894 {
1895 mips16_entry (SD, CPU, cia, instruction);
1896 sim_engine_restart (sd, NULL, NULL, NULL_CIA);
1897 }
1898 /* else fall through to normal exception processing */
1899 sim_io_eprintf(sd,"ReservedInstruction at PC = 0x%s\n", pr_addr (cia));
1900 }
1901
1902 default:
1903 /* Store exception code into current exception id variable (used
1904 by exit code): */
1905
1906 /* TODO: If not simulating exceptions then stop the simulator
1907 execution. At the moment we always stop the simulation. */
1908
1909#ifdef SUBTARGET_R3900
1910 /* update interrupt-related registers */
1911
1912 /* insert exception code in bits 6:2 */
1913 CAUSE = LSMASKED32(CAUSE, 31, 7) | LSINSERTED32(exception, 6, 2);
1914 /* shift IE/KU history bits left */
1915 SR = LSMASKED32(SR, 31, 4) | LSINSERTED32(LSEXTRACTED32(SR, 3, 0), 5, 2);
1916
1917 if (STATE & simDELAYSLOT)
1918 {
1919 STATE &= ~simDELAYSLOT;
1920 CAUSE |= cause_BD;
1921 EPC = (cia - 4); /* reference the branch instruction */
1922 }
1923 else
1924 EPC = cia;
1925
1926 if (SR & status_BEV)
1927 PC = (signed)0xBFC00000 + 0x180;
1928 else
1929 PC = (signed)0x80000000 + 0x080;
1930#else
1931 /* See figure 5-17 for an outline of the code below */
1932 if (! (SR & status_EXL))
1933 {
1934 CAUSE = (exception << 2);
1935 if (STATE & simDELAYSLOT)
1936 {
1937 STATE &= ~simDELAYSLOT;
1938 CAUSE |= cause_BD;
1939 EPC = (cia - 4); /* reference the branch instruction */
1940 }
1941 else
1942 EPC = cia;
1943 /* FIXME: TLB et.al. */
1944 /* vector = 0x180; */
1945 }
1946 else
1947 {
1948 CAUSE = (exception << 2);
1949 /* vector = 0x180; */
1950 }
1951 SR |= status_EXL;
1952 /* Store exception code into current exception id variable (used
1953 by exit code): */
1954
1955 if (SR & status_BEV)
1956 PC = (signed)0xBFC00200 + 0x180;
1957 else
1958 PC = (signed)0x80000000 + 0x180;
1959#endif
1960
1961 switch ((CAUSE >> 2) & 0x1F)
1962 {
1963 case Interrupt:
1964 /* Interrupts arrive during event processing, no need to
1965 restart */
1966 return;
1967
1968 case NMIReset:
1969 /* Ditto */
1970#ifdef SUBTARGET_3900
1971 /* Exception vector: BEV=0 BFC00000 / BEF=1 BFC00000 */
1972 PC = (signed)0xBFC00000;
0d3e762b 1973#endif /* SUBTARGET_3900 */
c906108c
SS
1974 return;
1975
1976 case TLBModification:
1977 case TLBLoad:
1978 case TLBStore:
1979 case AddressLoad:
1980 case AddressStore:
1981 case InstructionFetch:
1982 case DataReference:
1983 /* The following is so that the simulator will continue from the
1984 exception handler address. */
1985 sim_engine_halt (SD, CPU, NULL, PC,
1986 sim_stopped, SIM_SIGBUS);
1987
1988 case ReservedInstruction:
1989 case CoProcessorUnusable:
1990 PC = EPC;
1991 sim_engine_halt (SD, CPU, NULL, PC,
1992 sim_stopped, SIM_SIGILL);
1993
1994 case IntegerOverflow:
1995 case FPE:
1996 sim_engine_halt (SD, CPU, NULL, PC,
1997 sim_stopped, SIM_SIGFPE);
1998
1999 case BreakPoint:
2000 sim_engine_halt (SD, CPU, NULL, PC, sim_stopped, SIM_SIGTRAP);
2001 break;
2002
2003 case SystemCall:
2004 case Trap:
2005 sim_engine_restart (SD, CPU, NULL, PC);
2006 break;
2007
2008 case Watch:
2009 PC = EPC;
2010 sim_engine_halt (SD, CPU, NULL, PC,
2011 sim_stopped, SIM_SIGTRAP);
2012
e80fc152 2013 default: /* Unknown internal exception */
c906108c
SS
2014 PC = EPC;
2015 sim_engine_halt (SD, CPU, NULL, PC,
2016 sim_stopped, SIM_SIGABRT);
2017
2018 }
2019
2020 case SimulatorFault:
2021 {
2022 va_list ap;
2023 char *msg;
2024 va_start(ap,exception);
2025 msg = va_arg(ap,char *);
2026 va_end(ap);
2027 sim_engine_abort (SD, CPU, NULL_CIA,
2028 "FATAL: Simulator error \"%s\"\n",msg);
2029 }
2030 }
2031
2032 return;
2033}
2034
2035
2036
402586aa
CD
2037/* This function implements what the MIPS32 and MIPS64 ISAs define as
2038 "UNPREDICTABLE" behaviour.
2039
2040 About UNPREDICTABLE behaviour they say: "UNPREDICTABLE results
2041 may vary from processor implementation to processor implementation,
2042 instruction to instruction, or as a function of time on the same
2043 implementation or instruction. Software can never depend on results
2044 that are UNPREDICTABLE. ..." (MIPS64 Architecture for Programmers
2045 Volume II, The MIPS64 Instruction Set. MIPS Document MD00087 revision
2046 0.95, page 2.)
2047
2048 For UNPREDICTABLE behaviour, we print a message, if possible print
2049 the offending instructions mips.igen instruction name (provided by
2050 the caller), and stop the simulator.
2051
2052 XXX FIXME: eventually, stopping the simulator should be made conditional
2053 on a command-line option. */
2054void
2055unpredictable_action(sim_cpu *cpu, address_word cia)
c906108c 2056{
402586aa
CD
2057 SIM_DESC sd = CPU_STATE(cpu);
2058
2059 sim_io_eprintf(sd, "UNPREDICTABLE: PC = 0x%s\n", pr_addr (cia));
2060 sim_engine_halt (SD, CPU, NULL, cia, sim_stopped, SIM_SIGABRT);
c906108c 2061}
c906108c 2062
c906108c
SS
2063
2064/*-- co-processor support routines ------------------------------------------*/
2065
2066static int UNUSED
2067CoProcPresent(unsigned int coproc_number)
2068{
2069 /* Return TRUE if simulator provides a model for the given co-processor number */
2070 return(0);
2071}
2072
2073void
2074cop_lw (SIM_DESC sd,
2075 sim_cpu *cpu,
2076 address_word cia,
2077 int coproc_num,
2078 int coproc_reg,
2079 unsigned int memword)
2080{
2081 switch (coproc_num)
2082 {
2083 case 1:
2084 if (CURRENT_FLOATING_POINT == HARD_FLOATING_POINT)
2085 {
2086#ifdef DEBUG
2087 printf("DBG: COP_LW: memword = 0x%08X (uword64)memword = 0x%s\n",memword,pr_addr(memword));
2088#endif
14fb6c5a 2089 StoreFPR(coproc_reg,fmt_uninterpreted_32,(uword64)memword);
c906108c
SS
2090 break;
2091 }
2092
2093 default:
2094#if 0 /* this should be controlled by a configuration option */
2095 sim_io_printf(sd,"COP_LW(%d,%d,0x%08X) at PC = 0x%s : TODO (architecture specific)\n",coproc_num,coproc_reg,memword,pr_addr(cia));
2096#endif
2097 break;
2098 }
2099
2100 return;
2101}
2102
2103void
2104cop_ld (SIM_DESC sd,
2105 sim_cpu *cpu,
2106 address_word cia,
2107 int coproc_num,
2108 int coproc_reg,
2109 uword64 memword)
2110{
2111
2112#ifdef DEBUG
2113 printf("DBG: COP_LD: coproc_num = %d, coproc_reg = %d, value = 0x%s : PC = 0x%s\n", coproc_num, coproc_reg, pr_uword64(memword), pr_addr(cia) );
2114#endif
2115
2116 switch (coproc_num) {
2117 case 1:
2118 if (CURRENT_FLOATING_POINT == HARD_FLOATING_POINT)
2119 {
14fb6c5a 2120 StoreFPR(coproc_reg,fmt_uninterpreted_64,memword);
c906108c
SS
2121 break;
2122 }
2123
2124 default:
2125#if 0 /* this message should be controlled by a configuration option */
2126 sim_io_printf(sd,"COP_LD(%d,%d,0x%s) at PC = 0x%s : TODO (architecture specific)\n",coproc_num,coproc_reg,pr_addr(memword),pr_addr(cia));
2127#endif
2128 break;
2129 }
2130
2131 return;
2132}
2133
2134
2135
2136
2137unsigned int
2138cop_sw (SIM_DESC sd,
2139 sim_cpu *cpu,
2140 address_word cia,
2141 int coproc_num,
2142 int coproc_reg)
2143{
2144 unsigned int value = 0;
2145
2146 switch (coproc_num)
2147 {
2148 case 1:
2149 if (CURRENT_FLOATING_POINT == HARD_FLOATING_POINT)
2150 {
14fb6c5a 2151 value = (unsigned int)ValueFPR(coproc_reg,fmt_uninterpreted_32);
c906108c
SS
2152 break;
2153 }
2154
2155 default:
2156#if 0 /* should be controlled by configuration option */
2157 sim_io_printf(sd,"COP_SW(%d,%d) at PC = 0x%s : TODO (architecture specific)\n",coproc_num,coproc_reg,pr_addr(cia));
2158#endif
2159 break;
2160 }
2161
2162 return(value);
2163}
2164
2165uword64
2166cop_sd (SIM_DESC sd,
2167 sim_cpu *cpu,
2168 address_word cia,
2169 int coproc_num,
2170 int coproc_reg)
2171{
2172 uword64 value = 0;
2173 switch (coproc_num)
2174 {
2175 case 1:
2176 if (CURRENT_FLOATING_POINT == HARD_FLOATING_POINT)
2177 {
14fb6c5a 2178 value = ValueFPR(coproc_reg,fmt_uninterpreted_64);
c906108c
SS
2179 break;
2180 }
2181
2182 default:
2183#if 0 /* should be controlled by configuration option */
2184 sim_io_printf(sd,"COP_SD(%d,%d) at PC = 0x%s : TODO (architecture specific)\n",coproc_num,coproc_reg,pr_addr(cia));
2185#endif
2186 break;
2187 }
2188
2189 return(value);
2190}
2191
2192
2193
2194
2195void
2196decode_coproc (SIM_DESC sd,
2197 sim_cpu *cpu,
2198 address_word cia,
8e394ffc
AB
2199 unsigned int instruction,
2200 int coprocnum,
2201 CP0_operation op,
2202 int rt,
2203 int rd,
2204 int sel)
c906108c 2205{
c906108c
SS
2206 switch (coprocnum)
2207 {
2208 case 0: /* standard CPU control and cache registers */
2209 {
c906108c
SS
2210 /* R4000 Users Manual (second edition) lists the following CP0
2211 instructions:
2212 CODE><-RT><RD-><--TAIL--->
2213 DMFC0 Doubleword Move From CP0 (VR4100 = 01000000001tttttddddd00000000000)
2214 DMTC0 Doubleword Move To CP0 (VR4100 = 01000000101tttttddddd00000000000)
2215 MFC0 word Move From CP0 (VR4100 = 01000000000tttttddddd00000000000)
2216 MTC0 word Move To CP0 (VR4100 = 01000000100tttttddddd00000000000)
2217 TLBR Read Indexed TLB Entry (VR4100 = 01000010000000000000000000000001)
2218 TLBWI Write Indexed TLB Entry (VR4100 = 01000010000000000000000000000010)
2219 TLBWR Write Random TLB Entry (VR4100 = 01000010000000000000000000000110)
2220 TLBP Probe TLB for Matching Entry (VR4100 = 01000010000000000000000000001000)
2221 CACHE Cache operation (VR4100 = 101111bbbbbpppppiiiiiiiiiiiiiiii)
2222 ERET Exception return (VR4100 = 01000010000000000000000000011000)
2223 */
8e394ffc
AB
2224 if (((op == cp0_mfc0) || (op == cp0_mtc0) /* MFC0 / MTC0 */
2225 || (op == cp0_dmfc0) || (op == cp0_dmtc0)) /* DMFC0 / DMTC0 */
2226 && sel == 0)
c906108c 2227 {
c906108c
SS
2228 switch (rd) /* NOTEs: Standard CP0 registers */
2229 {
2230 /* 0 = Index R4000 VR4100 VR4300 */
2231 /* 1 = Random R4000 VR4100 VR4300 */
2232 /* 2 = EntryLo0 R4000 VR4100 VR4300 */
2233 /* 3 = EntryLo1 R4000 VR4100 VR4300 */
2234 /* 4 = Context R4000 VR4100 VR4300 */
2235 /* 5 = PageMask R4000 VR4100 VR4300 */
2236 /* 6 = Wired R4000 VR4100 VR4300 */
2237 /* 8 = BadVAddr R4000 VR4100 VR4300 */
2238 /* 9 = Count R4000 VR4100 VR4300 */
2239 /* 10 = EntryHi R4000 VR4100 VR4300 */
2240 /* 11 = Compare R4000 VR4100 VR4300 */
2241 /* 12 = SR R4000 VR4100 VR4300 */
2242#ifdef SUBTARGET_R3900
2243 case 3:
2244 /* 3 = Config R3900 */
2245 case 7:
2246 /* 7 = Cache R3900 */
2247 case 15:
2248 /* 15 = PRID R3900 */
2249
2250 /* ignore */
2251 break;
2252
2253 case 8:
2254 /* 8 = BadVAddr R4000 VR4100 VR4300 */
8e394ffc 2255 if (op == cp0_mfc0 || op == cp0_dmfc0)
1a27f959 2256 GPR[rt] = (signed_word) (signed_address) COP0_BADVADDR;
c906108c
SS
2257 else
2258 COP0_BADVADDR = GPR[rt];
2259 break;
2260
2261#endif /* SUBTARGET_R3900 */
2262 case 12:
8e394ffc 2263 if (op == cp0_mfc0 || op == cp0_dmfc0)
c906108c
SS
2264 GPR[rt] = SR;
2265 else
2266 SR = GPR[rt];
2267 break;
2268 /* 13 = Cause R4000 VR4100 VR4300 */
2269 case 13:
8e394ffc 2270 if (op == cp0_mfc0 || op == cp0_dmfc0)
c906108c
SS
2271 GPR[rt] = CAUSE;
2272 else
2273 CAUSE = GPR[rt];
2274 break;
2275 /* 14 = EPC R4000 VR4100 VR4300 */
2276 case 14:
8e394ffc 2277 if (op == cp0_mfc0 || op == cp0_dmfc0)
c906108c
SS
2278 GPR[rt] = (signed_word) (signed_address) EPC;
2279 else
2280 EPC = GPR[rt];
2281 break;
2282 /* 15 = PRId R4000 VR4100 VR4300 */
2283#ifdef SUBTARGET_R3900
2284 /* 16 = Debug */
2285 case 16:
8e394ffc 2286 if (op == cp0_mfc0 || op == cp0_dmfc0)
c906108c
SS
2287 GPR[rt] = Debug;
2288 else
2289 Debug = GPR[rt];
2290 break;
2291#else
2292 /* 16 = Config R4000 VR4100 VR4300 */
2293 case 16:
8e394ffc 2294 if (op == cp0_mfc0 || op == cp0_dmfc0)
07802d98
TS
2295 GPR[rt] = C0_CONFIG;
2296 else
2297 /* only bottom three bits are writable */
2298 C0_CONFIG = (C0_CONFIG & ~0x7) | (GPR[rt] & 0x7);
c906108c
SS
2299 break;
2300#endif
2301#ifdef SUBTARGET_R3900
2302 /* 17 = Debug */
2303 case 17:
8e394ffc 2304 if (op == cp0_mfc0 || op == cp0_dmfc0)
c906108c
SS
2305 GPR[rt] = DEPC;
2306 else
2307 DEPC = GPR[rt];
2308 break;
2309#else
2310 /* 17 = LLAddr R4000 VR4100 VR4300 */
2311#endif
2312 /* 18 = WatchLo R4000 VR4100 VR4300 */
2313 /* 19 = WatchHi R4000 VR4100 VR4300 */
2314 /* 20 = XContext R4000 VR4100 VR4300 */
2315 /* 26 = PErr or ECC R4000 VR4100 VR4300 */
2316 /* 27 = CacheErr R4000 VR4100 */
2317 /* 28 = TagLo R4000 VR4100 VR4300 */
2318 /* 29 = TagHi R4000 VR4100 VR4300 */
2319 /* 30 = ErrorEPC R4000 VR4100 VR4300 */
a3027dd7
FCE
2320 if (STATE_VERBOSE_P(SD))
2321 sim_io_eprintf (SD,
e30db738
AC
2322 "Warning: PC 0x%lx:interp.c decode_coproc DEADC0DE\n",
2323 (unsigned long)cia);
c906108c
SS
2324 GPR[rt] = 0xDEADC0DE; /* CPR[0,rd] */
2325 /* CPR[0,rd] = GPR[rt]; */
2326 default:
8e394ffc 2327 if (op == cp0_mfc0 || op == cp0_dmfc0)
c906108c
SS
2328 GPR[rt] = (signed_word) (signed32) COP0_GPR[rd];
2329 else
2330 COP0_GPR[rd] = GPR[rt];
2331#if 0
2332 if (code == 0x00)
2333 sim_io_printf(sd,"Warning: MFC0 %d,%d ignored, PC=%08x (architecture specific)\n",rt,rd, (unsigned)cia);
2334 else
2335 sim_io_printf(sd,"Warning: MTC0 %d,%d ignored, PC=%08x (architecture specific)\n",rt,rd, (unsigned)cia);
2336#endif
2337 }
2338 }
8e394ffc 2339 else if ((op == cp0_mfc0 || op == cp0_dmfc0)
07802d98
TS
2340 && rd == 16)
2341 {
2342 /* [D]MFC0 RT,C0_CONFIG,SEL */
2343 signed32 cfg = 0;
8e394ffc 2344 switch (sel)
07802d98
TS
2345 {
2346 case 0:
2347 cfg = C0_CONFIG;
2348 break;
2349 case 1:
2350 /* MIPS32 r/o Config1:
2351 Config2 present */
2352 cfg = 0x80000000;
2353 /* MIPS16 implemented.
2354 XXX How to check configuration? */
2355 cfg |= 0x0000004;
2356 if (CURRENT_FLOATING_POINT == HARD_FLOATING_POINT)
2357 /* MDMX & FPU implemented */
2358 cfg |= 0x00000021;
2359 break;
2360 case 2:
2361 /* MIPS32 r/o Config2:
2362 Config3 present. */
2363 cfg = 0x80000000;
2364 break;
2365 case 3:
2366 /* MIPS32 r/o Config3:
2367 SmartMIPS implemented. */
2368 cfg = 0x00000002;
2369 break;
2370 }
2371 GPR[rt] = cfg;
2372 }
8e394ffc 2373 else if (op == cp0_eret && sel == 0x18)
c906108c
SS
2374 {
2375 /* ERET */
2376 if (SR & status_ERL)
2377 {
2378 /* Oops, not yet available */
2379 sim_io_printf(sd,"Warning: ERET when SR[ERL] set not handled yet");
2380 PC = EPC;
2381 SR &= ~status_ERL;
2382 }
2383 else
2384 {
2385 PC = EPC;
2386 SR &= ~status_EXL;
2387 }
2388 }
8e394ffc 2389 else if (op == cp0_rfe && sel == 0x10)
c906108c
SS
2390 {
2391 /* RFE */
2392#ifdef SUBTARGET_R3900
2393 /* TX39: Copy IEp/KUp -> IEc/KUc, and IEo/KUo -> IEp/KUp */
2394
2395 /* shift IE/KU history bits right */
2396 SR = LSMASKED32(SR, 31, 4) | LSINSERTED32(LSEXTRACTED32(SR, 5, 2), 3, 0);
2397
2398 /* TODO: CACHE register */
2399#endif /* SUBTARGET_R3900 */
2400 }
8e394ffc 2401 else if (op == cp0_deret && sel == 0x1F)
c906108c
SS
2402 {
2403 /* DERET */
2404 Debug &= ~Debug_DM;
2405 DELAYSLOT();
2406 DSPC = DEPC;
2407 }
2408 else
2409 sim_io_eprintf(sd,"Unrecognised COP0 instruction 0x%08X at PC = 0x%s : No handler present\n",instruction,pr_addr(cia));
2410 /* TODO: When executing an ERET or RFE instruction we should
2411 clear LLBIT, to ensure that any out-standing atomic
2412 read/modify/write sequence fails. */
2413 }
2414 break;
2415
2416 case 2: /* co-processor 2 */
2417 {
2418 int handle = 0;
2419
2420
2421 if(! handle)
2422 {
2423 sim_io_eprintf(sd, "COP2 instruction 0x%08X at PC = 0x%s : No handler present\n",
2424 instruction,pr_addr(cia));
2425 }
2426 }
2427 break;
2428
2429 case 1: /* should not occur (FPU co-processor) */
2430 case 3: /* should not occur (FPU co-processor) */
2431 SignalException(ReservedInstruction,instruction);
2432 break;
2433 }
2434
2435 return;
2436}
2437
2438
2439/* This code copied from gdb's utils.c. Would like to share this code,
2440 but don't know of a common place where both could get to it. */
2441
2442/* Temporary storage using circular buffer */
2443#define NUMCELLS 16
2444#define CELLSIZE 32
2445static char*
2446get_cell (void)
2447{
2448 static char buf[NUMCELLS][CELLSIZE];
2449 static int cell=0;
2450 if (++cell>=NUMCELLS) cell=0;
2451 return buf[cell];
2452}
2453
2454/* Print routines to handle variable size regs, etc */
2455
2456/* Eliminate warning from compiler on 32-bit systems */
2457static int thirty_two = 32;
2458
2459char*
8ac57fbd 2460pr_addr (SIM_ADDR addr)
c906108c
SS
2461{
2462 char *paddr_str=get_cell();
2463 switch (sizeof(addr))
2464 {
2465 case 8:
2466 sprintf(paddr_str,"%08lx%08lx",
2467 (unsigned long)(addr>>thirty_two),(unsigned long)(addr&0xffffffff));
2468 break;
2469 case 4:
2470 sprintf(paddr_str,"%08lx",(unsigned long)addr);
2471 break;
2472 case 2:
2473 sprintf(paddr_str,"%04x",(unsigned short)(addr&0xffff));
2474 break;
2475 default:
2476 sprintf(paddr_str,"%x",addr);
2477 }
2478 return paddr_str;
2479}
2480
2481char*
8ac57fbd 2482pr_uword64 (uword64 addr)
c906108c
SS
2483{
2484 char *paddr_str=get_cell();
2485 sprintf(paddr_str,"%08lx%08lx",
2486 (unsigned long)(addr>>thirty_two),(unsigned long)(addr&0xffffffff));
2487 return paddr_str;
2488}
2489
2490
2491void
2492mips_core_signal (SIM_DESC sd,
2493 sim_cpu *cpu,
2494 sim_cia cia,
2495 unsigned map,
2496 int nr_bytes,
2497 address_word addr,
2498 transfer_type transfer,
2499 sim_core_signals sig)
2500{
2501 const char *copy = (transfer == read_transfer ? "read" : "write");
2502 address_word ip = CIA_ADDR (cia);
2503
2504 switch (sig)
2505 {
2506 case sim_core_unmapped_signal:
2507 sim_io_eprintf (sd, "mips-core: %d byte %s to unmapped address 0x%lx at 0x%lx\n",
2508 nr_bytes, copy,
2509 (unsigned long) addr, (unsigned long) ip);
2510 COP0_BADVADDR = addr;
2511 SignalExceptionDataReference();
2512 break;
2513
2514 case sim_core_unaligned_signal:
2515 sim_io_eprintf (sd, "mips-core: %d byte %s to unaligned address 0x%lx at 0x%lx\n",
2516 nr_bytes, copy,
2517 (unsigned long) addr, (unsigned long) ip);
2518 COP0_BADVADDR = addr;
2519 if(transfer == read_transfer)
2520 SignalExceptionAddressLoad();
2521 else
2522 SignalExceptionAddressStore();
2523 break;
2524
2525 default:
2526 sim_engine_abort (sd, cpu, cia,
2527 "mips_core_signal - internal error - bad switch");
2528 }
2529}
2530
2531
2532void
2533mips_cpu_exception_trigger(SIM_DESC sd, sim_cpu* cpu, address_word cia)
2534{
2535 ASSERT(cpu != NULL);
2536
2537 if(cpu->exc_suspended > 0)
2538 sim_io_eprintf(sd, "Warning, nested exception triggered (%d)\n", cpu->exc_suspended);
2539
2540 PC = cia;
2541 memcpy(cpu->exc_trigger_registers, cpu->registers, sizeof(cpu->exc_trigger_registers));
2542 cpu->exc_suspended = 0;
2543}
2544
2545void
2546mips_cpu_exception_suspend(SIM_DESC sd, sim_cpu* cpu, int exception)
2547{
2548 ASSERT(cpu != NULL);
2549
2550 if(cpu->exc_suspended > 0)
2551 sim_io_eprintf(sd, "Warning, nested exception signal (%d then %d)\n",
2552 cpu->exc_suspended, exception);
2553
2554 memcpy(cpu->exc_suspend_registers, cpu->registers, sizeof(cpu->exc_suspend_registers));
2555 memcpy(cpu->registers, cpu->exc_trigger_registers, sizeof(cpu->registers));
2556 cpu->exc_suspended = exception;
2557}
2558
2559void
2560mips_cpu_exception_resume(SIM_DESC sd, sim_cpu* cpu, int exception)
2561{
2562 ASSERT(cpu != NULL);
2563
2564 if(exception == 0 && cpu->exc_suspended > 0)
2565 {
2566 /* warn not for breakpoints */
2567 if(cpu->exc_suspended != sim_signal_to_host(sd, SIM_SIGTRAP))
2568 sim_io_eprintf(sd, "Warning, resuming but ignoring pending exception signal (%d)\n",
2569 cpu->exc_suspended);
2570 }
2571 else if(exception != 0 && cpu->exc_suspended > 0)
2572 {
2573 if(exception != cpu->exc_suspended)
2574 sim_io_eprintf(sd, "Warning, resuming with mismatched exception signal (%d vs %d)\n",
2575 cpu->exc_suspended, exception);
2576
2577 memcpy(cpu->registers, cpu->exc_suspend_registers, sizeof(cpu->registers));
2578 }
2579 else if(exception != 0 && cpu->exc_suspended == 0)
2580 {
2581 sim_io_eprintf(sd, "Warning, ignoring spontanous exception signal (%d)\n", exception);
2582 }
2583 cpu->exc_suspended = 0;
2584}
2585
2586
2587/*---------------------------------------------------------------------------*/
2588/*> EOF interp.c <*/