interp.c 72 KB

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