wrapper.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. /* run front end support for arm
  2. Copyright (C) 1995-2022 Free Software Foundation, Inc.
  3. This file is part of ARM SIM.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* This file provides the interface between the simulator and
  15. run.c and gdb (when the simulator is linked with gdb).
  16. All simulator interaction should go through this file. */
  17. /* This must come before any other includes. */
  18. #include "defs.h"
  19. #include <stdio.h>
  20. #include <stdarg.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <bfd.h>
  24. #include <signal.h>
  25. #include "sim/callback.h"
  26. #include "sim/sim.h"
  27. #include "sim-main.h"
  28. #include "sim-options.h"
  29. #include "armemu.h"
  30. #include "dbg_rdi.h"
  31. #include "ansidecl.h"
  32. #include "gdb/sim-arm.h"
  33. #include "gdb/signals.h"
  34. #include "libiberty.h"
  35. #include "iwmmxt.h"
  36. #include "maverick.h"
  37. /* TODO: This should get pulled from the SIM_DESC. */
  38. host_callback *sim_callback;
  39. /* TODO: This should get merged into sim_cpu. */
  40. struct ARMul_State *state;
  41. /* Memory size in bytes. */
  42. /* TODO: Memory should be converted to the common memory module. */
  43. static int mem_size = (1 << 21);
  44. int stop_simulator;
  45. #include "dis-asm.h"
  46. /* TODO: Tracing should be converted to common tracing module. */
  47. int trace = 0;
  48. int disas = 0;
  49. int trace_funcs = 0;
  50. static struct disassemble_info info;
  51. static char opbuf[1000];
  52. static int ATTRIBUTE_PRINTF (2, 3)
  53. op_printf (char *buf, const char *fmt, ...)
  54. {
  55. int ret;
  56. va_list ap;
  57. va_start (ap, fmt);
  58. ret = vsprintf (opbuf + strlen (opbuf), fmt, ap);
  59. va_end (ap);
  60. return ret;
  61. }
  62. static int ATTRIBUTE_PRINTF (3, 4)
  63. op_styled_printf (char *buf, enum disassembler_style style,
  64. const char *fmt, ...)
  65. {
  66. int ret;
  67. va_list ap;
  68. va_start (ap, fmt);
  69. ret = vsprintf (opbuf + strlen (opbuf), fmt, ap);
  70. va_end (ap);
  71. return ret;
  72. }
  73. static int
  74. sim_dis_read (bfd_vma memaddr ATTRIBUTE_UNUSED,
  75. bfd_byte * ptr,
  76. unsigned int length,
  77. struct disassemble_info * info)
  78. {
  79. ARMword val = (ARMword) *((ARMword *) info->application_data);
  80. while (length--)
  81. {
  82. * ptr ++ = val & 0xFF;
  83. val >>= 8;
  84. }
  85. return 0;
  86. }
  87. void
  88. print_insn (ARMword instr)
  89. {
  90. int size;
  91. disassembler_ftype disassemble_fn;
  92. opbuf[0] = 0;
  93. info.application_data = & instr;
  94. disassemble_fn = disassembler (bfd_arch_arm, 0, 0, NULL);
  95. size = disassemble_fn (0, & info);
  96. fprintf (stderr, " %*s\n", size, opbuf);
  97. }
  98. static void
  99. init (void)
  100. {
  101. static int done;
  102. if (!done)
  103. {
  104. ARMul_EmulateInit ();
  105. state = ARMul_NewState ();
  106. state->bigendSig = (CURRENT_TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? HIGH : LOW);
  107. ARMul_MemoryInit (state, mem_size);
  108. ARMul_OSInit (state);
  109. state->verbose = 0;
  110. done = 1;
  111. }
  112. }
  113. void
  114. ARMul_ConsolePrint (ARMul_State * state,
  115. const char * format,
  116. ...)
  117. {
  118. va_list ap;
  119. if (state->verbose)
  120. {
  121. va_start (ap, format);
  122. vprintf (format, ap);
  123. va_end (ap);
  124. }
  125. }
  126. int
  127. sim_write (SIM_DESC sd ATTRIBUTE_UNUSED,
  128. SIM_ADDR addr,
  129. const unsigned char * buffer,
  130. int size)
  131. {
  132. int i;
  133. init ();
  134. for (i = 0; i < size; i++)
  135. ARMul_SafeWriteByte (state, addr + i, buffer[i]);
  136. return size;
  137. }
  138. int
  139. sim_read (SIM_DESC sd ATTRIBUTE_UNUSED,
  140. SIM_ADDR addr,
  141. unsigned char * buffer,
  142. int size)
  143. {
  144. int i;
  145. init ();
  146. for (i = 0; i < size; i++)
  147. buffer[i] = ARMul_SafeReadByte (state, addr + i);
  148. return size;
  149. }
  150. int
  151. sim_stop (SIM_DESC sd ATTRIBUTE_UNUSED)
  152. {
  153. state->Emulate = STOP;
  154. stop_simulator = 1;
  155. return 1;
  156. }
  157. void
  158. sim_resume (SIM_DESC sd ATTRIBUTE_UNUSED,
  159. int step,
  160. int siggnal ATTRIBUTE_UNUSED)
  161. {
  162. state->EndCondition = 0;
  163. stop_simulator = 0;
  164. if (step)
  165. {
  166. state->Reg[15] = ARMul_DoInstr (state);
  167. if (state->EndCondition == 0)
  168. state->EndCondition = RDIError_BreakpointReached;
  169. }
  170. else
  171. {
  172. state->NextInstr = RESUME; /* treat as PC change */
  173. state->Reg[15] = ARMul_DoProg (state);
  174. }
  175. FLUSHPIPE;
  176. }
  177. SIM_RC
  178. sim_create_inferior (SIM_DESC sd ATTRIBUTE_UNUSED,
  179. struct bfd * abfd,
  180. char * const *argv,
  181. char * const *env)
  182. {
  183. int argvlen = 0;
  184. int mach;
  185. char * const *arg;
  186. init ();
  187. if (abfd != NULL)
  188. {
  189. ARMul_SetPC (state, bfd_get_start_address (abfd));
  190. mach = bfd_get_mach (abfd);
  191. }
  192. else
  193. {
  194. ARMul_SetPC (state, 0); /* ??? */
  195. mach = 0;
  196. }
  197. #ifdef MODET
  198. if (abfd != NULL && (bfd_get_start_address (abfd) & 1))
  199. SETT;
  200. #endif
  201. switch (mach)
  202. {
  203. default:
  204. (*sim_callback->printf_filtered)
  205. (sim_callback,
  206. "Unknown machine type '%d'; please update sim_create_inferior.\n",
  207. mach);
  208. /* fall through */
  209. case 0:
  210. /* We wouldn't set the machine type with earlier toolchains, so we
  211. explicitly select a processor capable of supporting all ARMs in
  212. 32bit mode. */
  213. ARMul_SelectProcessor (state, ARM_v5_Prop | ARM_v5e_Prop | ARM_v6_Prop);
  214. break;
  215. #if 1
  216. case bfd_mach_arm_6T2:
  217. case bfd_mach_arm_7:
  218. case bfd_mach_arm_7EM:
  219. ARMul_SelectProcessor (state, ARM_v5_Prop | ARM_v5e_Prop | ARM_v6_Prop);
  220. break;
  221. #endif
  222. case bfd_mach_arm_XScale:
  223. ARMul_SelectProcessor (state, ARM_v5_Prop | ARM_v5e_Prop | ARM_XScale_Prop | ARM_v6_Prop);
  224. break;
  225. case bfd_mach_arm_iWMMXt2:
  226. case bfd_mach_arm_iWMMXt:
  227. {
  228. extern int SWI_vector_installed;
  229. ARMword i;
  230. if (! SWI_vector_installed)
  231. {
  232. /* Intialise the hardware vectors to zero. */
  233. if (! SWI_vector_installed)
  234. for (i = ARMul_ResetV; i <= ARMFIQV; i += 4)
  235. ARMul_WriteWord (state, i, 0);
  236. /* ARM_WriteWord will have detected the write to the SWI vector,
  237. but we want SWI_vector_installed to remain at 0 so that thumb
  238. mode breakpoints will work. */
  239. SWI_vector_installed = 0;
  240. }
  241. }
  242. ARMul_SelectProcessor (state, ARM_v5_Prop | ARM_v5e_Prop | ARM_XScale_Prop | ARM_iWMMXt_Prop);
  243. break;
  244. case bfd_mach_arm_ep9312:
  245. ARMul_SelectProcessor (state, ARM_v4_Prop | ARM_ep9312_Prop);
  246. break;
  247. case bfd_mach_arm_5:
  248. if (bfd_family_coff (abfd))
  249. {
  250. /* This is a special case in order to support COFF based ARM toolchains.
  251. The COFF header does not have enough room to store all the different
  252. kinds of ARM cpu, so the XScale, v5T and v5TE architectures all default
  253. to v5. (See coff_set_flags() in bdf/coffcode.h). So if we see a v5
  254. machine type here, we assume it could be any of the above architectures
  255. and so select the most feature-full. */
  256. ARMul_SelectProcessor (state, ARM_v5_Prop | ARM_v5e_Prop | ARM_XScale_Prop);
  257. break;
  258. }
  259. /* Otherwise drop through. */
  260. case bfd_mach_arm_5T:
  261. ARMul_SelectProcessor (state, ARM_v5_Prop);
  262. break;
  263. case bfd_mach_arm_5TE:
  264. ARMul_SelectProcessor (state, ARM_v5_Prop | ARM_v5e_Prop);
  265. break;
  266. case bfd_mach_arm_4:
  267. case bfd_mach_arm_4T:
  268. ARMul_SelectProcessor (state, ARM_v4_Prop);
  269. break;
  270. case bfd_mach_arm_3:
  271. case bfd_mach_arm_3M:
  272. ARMul_SelectProcessor (state, ARM_Lock_Prop);
  273. break;
  274. case bfd_mach_arm_2:
  275. case bfd_mach_arm_2a:
  276. ARMul_SelectProcessor (state, ARM_Fix26_Prop);
  277. break;
  278. }
  279. memset (& info, 0, sizeof (info));
  280. INIT_DISASSEMBLE_INFO (info, stdout, op_printf, op_styled_printf);
  281. info.read_memory_func = sim_dis_read;
  282. info.arch = bfd_get_arch (abfd);
  283. info.mach = bfd_get_mach (abfd);
  284. info.endian_code = BFD_ENDIAN_LITTLE;
  285. if (info.mach == 0)
  286. info.arch = bfd_arch_arm;
  287. disassemble_init_for_target (& info);
  288. if (argv != NULL)
  289. {
  290. /* Set up the command line by laboriously stringing together
  291. the environment carefully picked apart by our caller. */
  292. /* Free any old stuff. */
  293. if (state->CommandLine != NULL)
  294. {
  295. free (state->CommandLine);
  296. state->CommandLine = NULL;
  297. }
  298. /* See how much we need. */
  299. for (arg = argv; *arg != NULL; arg++)
  300. argvlen += strlen (*arg) + 1;
  301. /* Allocate it. */
  302. state->CommandLine = malloc (argvlen + 1);
  303. if (state->CommandLine != NULL)
  304. {
  305. arg = argv;
  306. state->CommandLine[0] = '\0';
  307. for (arg = argv; *arg != NULL; arg++)
  308. {
  309. strcat (state->CommandLine, *arg);
  310. strcat (state->CommandLine, " ");
  311. }
  312. }
  313. }
  314. if (env != NULL)
  315. {
  316. /* Now see if there's a MEMSIZE spec in the environment. */
  317. while (*env)
  318. {
  319. if (strncmp (*env, "MEMSIZE=", sizeof ("MEMSIZE=") - 1) == 0)
  320. {
  321. char *end_of_num;
  322. /* Set up memory limit. */
  323. state->MemSize =
  324. strtoul (*env + sizeof ("MEMSIZE=") - 1, &end_of_num, 0);
  325. }
  326. env++;
  327. }
  328. }
  329. return SIM_RC_OK;
  330. }
  331. static int
  332. frommem (struct ARMul_State *state, unsigned char *memory)
  333. {
  334. if (state->bigendSig == HIGH)
  335. return (memory[0] << 24) | (memory[1] << 16)
  336. | (memory[2] << 8) | (memory[3] << 0);
  337. else
  338. return (memory[3] << 24) | (memory[2] << 16)
  339. | (memory[1] << 8) | (memory[0] << 0);
  340. }
  341. static void
  342. tomem (struct ARMul_State *state,
  343. unsigned char *memory,
  344. int val)
  345. {
  346. if (state->bigendSig == HIGH)
  347. {
  348. memory[0] = val >> 24;
  349. memory[1] = val >> 16;
  350. memory[2] = val >> 8;
  351. memory[3] = val >> 0;
  352. }
  353. else
  354. {
  355. memory[3] = val >> 24;
  356. memory[2] = val >> 16;
  357. memory[1] = val >> 8;
  358. memory[0] = val >> 0;
  359. }
  360. }
  361. static int
  362. arm_reg_store (SIM_CPU *cpu, int rn, unsigned char *memory, int length)
  363. {
  364. init ();
  365. switch ((enum sim_arm_regs) rn)
  366. {
  367. case SIM_ARM_R0_REGNUM:
  368. case SIM_ARM_R1_REGNUM:
  369. case SIM_ARM_R2_REGNUM:
  370. case SIM_ARM_R3_REGNUM:
  371. case SIM_ARM_R4_REGNUM:
  372. case SIM_ARM_R5_REGNUM:
  373. case SIM_ARM_R6_REGNUM:
  374. case SIM_ARM_R7_REGNUM:
  375. case SIM_ARM_R8_REGNUM:
  376. case SIM_ARM_R9_REGNUM:
  377. case SIM_ARM_R10_REGNUM:
  378. case SIM_ARM_R11_REGNUM:
  379. case SIM_ARM_R12_REGNUM:
  380. case SIM_ARM_R13_REGNUM:
  381. case SIM_ARM_R14_REGNUM:
  382. case SIM_ARM_R15_REGNUM: /* PC */
  383. case SIM_ARM_FP0_REGNUM:
  384. case SIM_ARM_FP1_REGNUM:
  385. case SIM_ARM_FP2_REGNUM:
  386. case SIM_ARM_FP3_REGNUM:
  387. case SIM_ARM_FP4_REGNUM:
  388. case SIM_ARM_FP5_REGNUM:
  389. case SIM_ARM_FP6_REGNUM:
  390. case SIM_ARM_FP7_REGNUM:
  391. case SIM_ARM_FPS_REGNUM:
  392. ARMul_SetReg (state, state->Mode, rn, frommem (state, memory));
  393. break;
  394. case SIM_ARM_PS_REGNUM:
  395. state->Cpsr = frommem (state, memory);
  396. ARMul_CPSRAltered (state);
  397. break;
  398. case SIM_ARM_MAVERIC_COP0R0_REGNUM:
  399. case SIM_ARM_MAVERIC_COP0R1_REGNUM:
  400. case SIM_ARM_MAVERIC_COP0R2_REGNUM:
  401. case SIM_ARM_MAVERIC_COP0R3_REGNUM:
  402. case SIM_ARM_MAVERIC_COP0R4_REGNUM:
  403. case SIM_ARM_MAVERIC_COP0R5_REGNUM:
  404. case SIM_ARM_MAVERIC_COP0R6_REGNUM:
  405. case SIM_ARM_MAVERIC_COP0R7_REGNUM:
  406. case SIM_ARM_MAVERIC_COP0R8_REGNUM:
  407. case SIM_ARM_MAVERIC_COP0R9_REGNUM:
  408. case SIM_ARM_MAVERIC_COP0R10_REGNUM:
  409. case SIM_ARM_MAVERIC_COP0R11_REGNUM:
  410. case SIM_ARM_MAVERIC_COP0R12_REGNUM:
  411. case SIM_ARM_MAVERIC_COP0R13_REGNUM:
  412. case SIM_ARM_MAVERIC_COP0R14_REGNUM:
  413. case SIM_ARM_MAVERIC_COP0R15_REGNUM:
  414. memcpy (& DSPregs [rn - SIM_ARM_MAVERIC_COP0R0_REGNUM],
  415. memory, sizeof (struct maverick_regs));
  416. return sizeof (struct maverick_regs);
  417. case SIM_ARM_MAVERIC_DSPSC_REGNUM:
  418. memcpy (&DSPsc, memory, sizeof DSPsc);
  419. return sizeof DSPsc;
  420. case SIM_ARM_IWMMXT_COP0R0_REGNUM:
  421. case SIM_ARM_IWMMXT_COP0R1_REGNUM:
  422. case SIM_ARM_IWMMXT_COP0R2_REGNUM:
  423. case SIM_ARM_IWMMXT_COP0R3_REGNUM:
  424. case SIM_ARM_IWMMXT_COP0R4_REGNUM:
  425. case SIM_ARM_IWMMXT_COP0R5_REGNUM:
  426. case SIM_ARM_IWMMXT_COP0R6_REGNUM:
  427. case SIM_ARM_IWMMXT_COP0R7_REGNUM:
  428. case SIM_ARM_IWMMXT_COP0R8_REGNUM:
  429. case SIM_ARM_IWMMXT_COP0R9_REGNUM:
  430. case SIM_ARM_IWMMXT_COP0R10_REGNUM:
  431. case SIM_ARM_IWMMXT_COP0R11_REGNUM:
  432. case SIM_ARM_IWMMXT_COP0R12_REGNUM:
  433. case SIM_ARM_IWMMXT_COP0R13_REGNUM:
  434. case SIM_ARM_IWMMXT_COP0R14_REGNUM:
  435. case SIM_ARM_IWMMXT_COP0R15_REGNUM:
  436. case SIM_ARM_IWMMXT_COP1R0_REGNUM:
  437. case SIM_ARM_IWMMXT_COP1R1_REGNUM:
  438. case SIM_ARM_IWMMXT_COP1R2_REGNUM:
  439. case SIM_ARM_IWMMXT_COP1R3_REGNUM:
  440. case SIM_ARM_IWMMXT_COP1R4_REGNUM:
  441. case SIM_ARM_IWMMXT_COP1R5_REGNUM:
  442. case SIM_ARM_IWMMXT_COP1R6_REGNUM:
  443. case SIM_ARM_IWMMXT_COP1R7_REGNUM:
  444. case SIM_ARM_IWMMXT_COP1R8_REGNUM:
  445. case SIM_ARM_IWMMXT_COP1R9_REGNUM:
  446. case SIM_ARM_IWMMXT_COP1R10_REGNUM:
  447. case SIM_ARM_IWMMXT_COP1R11_REGNUM:
  448. case SIM_ARM_IWMMXT_COP1R12_REGNUM:
  449. case SIM_ARM_IWMMXT_COP1R13_REGNUM:
  450. case SIM_ARM_IWMMXT_COP1R14_REGNUM:
  451. case SIM_ARM_IWMMXT_COP1R15_REGNUM:
  452. return Store_Iwmmxt_Register (rn - SIM_ARM_IWMMXT_COP0R0_REGNUM, memory);
  453. default:
  454. return 0;
  455. }
  456. return length;
  457. }
  458. static int
  459. arm_reg_fetch (SIM_CPU *cpu, int rn, unsigned char *memory, int length)
  460. {
  461. ARMword regval;
  462. int len = length;
  463. init ();
  464. switch ((enum sim_arm_regs) rn)
  465. {
  466. case SIM_ARM_R0_REGNUM:
  467. case SIM_ARM_R1_REGNUM:
  468. case SIM_ARM_R2_REGNUM:
  469. case SIM_ARM_R3_REGNUM:
  470. case SIM_ARM_R4_REGNUM:
  471. case SIM_ARM_R5_REGNUM:
  472. case SIM_ARM_R6_REGNUM:
  473. case SIM_ARM_R7_REGNUM:
  474. case SIM_ARM_R8_REGNUM:
  475. case SIM_ARM_R9_REGNUM:
  476. case SIM_ARM_R10_REGNUM:
  477. case SIM_ARM_R11_REGNUM:
  478. case SIM_ARM_R12_REGNUM:
  479. case SIM_ARM_R13_REGNUM:
  480. case SIM_ARM_R14_REGNUM:
  481. case SIM_ARM_R15_REGNUM: /* PC */
  482. regval = ARMul_GetReg (state, state->Mode, rn);
  483. break;
  484. case SIM_ARM_FP0_REGNUM:
  485. case SIM_ARM_FP1_REGNUM:
  486. case SIM_ARM_FP2_REGNUM:
  487. case SIM_ARM_FP3_REGNUM:
  488. case SIM_ARM_FP4_REGNUM:
  489. case SIM_ARM_FP5_REGNUM:
  490. case SIM_ARM_FP6_REGNUM:
  491. case SIM_ARM_FP7_REGNUM:
  492. case SIM_ARM_FPS_REGNUM:
  493. memset (memory, 0, length);
  494. return 0;
  495. case SIM_ARM_PS_REGNUM:
  496. regval = ARMul_GetCPSR (state);
  497. break;
  498. case SIM_ARM_MAVERIC_COP0R0_REGNUM:
  499. case SIM_ARM_MAVERIC_COP0R1_REGNUM:
  500. case SIM_ARM_MAVERIC_COP0R2_REGNUM:
  501. case SIM_ARM_MAVERIC_COP0R3_REGNUM:
  502. case SIM_ARM_MAVERIC_COP0R4_REGNUM:
  503. case SIM_ARM_MAVERIC_COP0R5_REGNUM:
  504. case SIM_ARM_MAVERIC_COP0R6_REGNUM:
  505. case SIM_ARM_MAVERIC_COP0R7_REGNUM:
  506. case SIM_ARM_MAVERIC_COP0R8_REGNUM:
  507. case SIM_ARM_MAVERIC_COP0R9_REGNUM:
  508. case SIM_ARM_MAVERIC_COP0R10_REGNUM:
  509. case SIM_ARM_MAVERIC_COP0R11_REGNUM:
  510. case SIM_ARM_MAVERIC_COP0R12_REGNUM:
  511. case SIM_ARM_MAVERIC_COP0R13_REGNUM:
  512. case SIM_ARM_MAVERIC_COP0R14_REGNUM:
  513. case SIM_ARM_MAVERIC_COP0R15_REGNUM:
  514. memcpy (memory, & DSPregs [rn - SIM_ARM_MAVERIC_COP0R0_REGNUM],
  515. sizeof (struct maverick_regs));
  516. return sizeof (struct maverick_regs);
  517. case SIM_ARM_MAVERIC_DSPSC_REGNUM:
  518. memcpy (memory, & DSPsc, sizeof DSPsc);
  519. return sizeof DSPsc;
  520. case SIM_ARM_IWMMXT_COP0R0_REGNUM:
  521. case SIM_ARM_IWMMXT_COP0R1_REGNUM:
  522. case SIM_ARM_IWMMXT_COP0R2_REGNUM:
  523. case SIM_ARM_IWMMXT_COP0R3_REGNUM:
  524. case SIM_ARM_IWMMXT_COP0R4_REGNUM:
  525. case SIM_ARM_IWMMXT_COP0R5_REGNUM:
  526. case SIM_ARM_IWMMXT_COP0R6_REGNUM:
  527. case SIM_ARM_IWMMXT_COP0R7_REGNUM:
  528. case SIM_ARM_IWMMXT_COP0R8_REGNUM:
  529. case SIM_ARM_IWMMXT_COP0R9_REGNUM:
  530. case SIM_ARM_IWMMXT_COP0R10_REGNUM:
  531. case SIM_ARM_IWMMXT_COP0R11_REGNUM:
  532. case SIM_ARM_IWMMXT_COP0R12_REGNUM:
  533. case SIM_ARM_IWMMXT_COP0R13_REGNUM:
  534. case SIM_ARM_IWMMXT_COP0R14_REGNUM:
  535. case SIM_ARM_IWMMXT_COP0R15_REGNUM:
  536. case SIM_ARM_IWMMXT_COP1R0_REGNUM:
  537. case SIM_ARM_IWMMXT_COP1R1_REGNUM:
  538. case SIM_ARM_IWMMXT_COP1R2_REGNUM:
  539. case SIM_ARM_IWMMXT_COP1R3_REGNUM:
  540. case SIM_ARM_IWMMXT_COP1R4_REGNUM:
  541. case SIM_ARM_IWMMXT_COP1R5_REGNUM:
  542. case SIM_ARM_IWMMXT_COP1R6_REGNUM:
  543. case SIM_ARM_IWMMXT_COP1R7_REGNUM:
  544. case SIM_ARM_IWMMXT_COP1R8_REGNUM:
  545. case SIM_ARM_IWMMXT_COP1R9_REGNUM:
  546. case SIM_ARM_IWMMXT_COP1R10_REGNUM:
  547. case SIM_ARM_IWMMXT_COP1R11_REGNUM:
  548. case SIM_ARM_IWMMXT_COP1R12_REGNUM:
  549. case SIM_ARM_IWMMXT_COP1R13_REGNUM:
  550. case SIM_ARM_IWMMXT_COP1R14_REGNUM:
  551. case SIM_ARM_IWMMXT_COP1R15_REGNUM:
  552. return Fetch_Iwmmxt_Register (rn - SIM_ARM_IWMMXT_COP0R0_REGNUM, memory);
  553. default:
  554. return 0;
  555. }
  556. while (len)
  557. {
  558. tomem (state, memory, regval);
  559. len -= 4;
  560. memory += 4;
  561. regval = 0;
  562. }
  563. return length;
  564. }
  565. typedef struct
  566. {
  567. char * swi_option;
  568. unsigned int swi_mask;
  569. } swi_options;
  570. #define SWI_SWITCH "--swi-support"
  571. static swi_options options[] =
  572. {
  573. { "none", 0 },
  574. { "demon", SWI_MASK_DEMON },
  575. { "angel", SWI_MASK_ANGEL },
  576. { "redboot", SWI_MASK_REDBOOT },
  577. { "all", -1 },
  578. { "NONE", 0 },
  579. { "DEMON", SWI_MASK_DEMON },
  580. { "ANGEL", SWI_MASK_ANGEL },
  581. { "REDBOOT", SWI_MASK_REDBOOT },
  582. { "ALL", -1 }
  583. };
  584. static int
  585. sim_target_parse_command_line (int argc, char ** argv)
  586. {
  587. int i;
  588. for (i = 1; i < argc; i++)
  589. {
  590. char * ptr = argv[i];
  591. int arg;
  592. if ((ptr == NULL) || (* ptr != '-'))
  593. break;
  594. if (strcmp (ptr, "-t") == 0)
  595. {
  596. trace = 1;
  597. continue;
  598. }
  599. if (strcmp (ptr, "-z") == 0)
  600. {
  601. /* Remove this option from the argv array. */
  602. for (arg = i; arg < argc; arg ++)
  603. {
  604. free (argv[arg]);
  605. argv[arg] = argv[arg + 1];
  606. }
  607. argc --;
  608. i --;
  609. trace_funcs = 1;
  610. continue;
  611. }
  612. if (strcmp (ptr, "-d") == 0)
  613. {
  614. /* Remove this option from the argv array. */
  615. for (arg = i; arg < argc; arg ++)
  616. {
  617. free (argv[arg]);
  618. argv[arg] = argv[arg + 1];
  619. }
  620. argc --;
  621. i --;
  622. disas = 1;
  623. continue;
  624. }
  625. if (strncmp (ptr, SWI_SWITCH, sizeof SWI_SWITCH - 1) != 0)
  626. continue;
  627. if (ptr[sizeof SWI_SWITCH - 1] == 0)
  628. {
  629. /* Remove this option from the argv array. */
  630. for (arg = i; arg < argc; arg ++)
  631. {
  632. free (argv[arg]);
  633. argv[arg] = argv[arg + 1];
  634. }
  635. argc --;
  636. ptr = argv[i];
  637. }
  638. else
  639. ptr += sizeof SWI_SWITCH;
  640. swi_mask = 0;
  641. while (* ptr)
  642. {
  643. int i;
  644. for (i = ARRAY_SIZE (options); i--;)
  645. if (strncmp (ptr, options[i].swi_option,
  646. strlen (options[i].swi_option)) == 0)
  647. {
  648. swi_mask |= options[i].swi_mask;
  649. ptr += strlen (options[i].swi_option);
  650. if (* ptr == ',')
  651. ++ ptr;
  652. break;
  653. }
  654. if (i < 0)
  655. break;
  656. }
  657. if (* ptr != 0)
  658. fprintf (stderr, "Ignoring swi options: %s\n", ptr);
  659. /* Remove this option from the argv array. */
  660. for (arg = i; arg < argc; arg ++)
  661. {
  662. free (argv[arg]);
  663. argv[arg] = argv[arg + 1];
  664. }
  665. argc --;
  666. i --;
  667. }
  668. return argc;
  669. }
  670. static void
  671. sim_target_parse_arg_array (char ** argv)
  672. {
  673. sim_target_parse_command_line (countargv (argv), argv);
  674. }
  675. static sim_cia
  676. arm_pc_get (sim_cpu *cpu)
  677. {
  678. return PC;
  679. }
  680. static void
  681. arm_pc_set (sim_cpu *cpu, sim_cia pc)
  682. {
  683. ARMul_SetPC (state, pc);
  684. }
  685. static void
  686. free_state (SIM_DESC sd)
  687. {
  688. if (STATE_MODULES (sd) != NULL)
  689. sim_module_uninstall (sd);
  690. sim_cpu_free_all (sd);
  691. sim_state_free (sd);
  692. }
  693. SIM_DESC
  694. sim_open (SIM_OPEN_KIND kind,
  695. host_callback *cb,
  696. struct bfd *abfd,
  697. char * const *argv)
  698. {
  699. int i;
  700. char **argv_copy;
  701. SIM_DESC sd = sim_state_alloc (kind, cb);
  702. SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  703. /* Set default options before parsing user options. */
  704. current_alignment = STRICT_ALIGNMENT;
  705. /* The cpu data is kept in a separately allocated chunk of memory. */
  706. if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
  707. {
  708. free_state (sd);
  709. return 0;
  710. }
  711. if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
  712. {
  713. free_state (sd);
  714. return 0;
  715. }
  716. /* The parser will print an error message for us, so we silently return. */
  717. if (sim_parse_args (sd, argv) != SIM_RC_OK)
  718. {
  719. free_state (sd);
  720. return 0;
  721. }
  722. /* Check for/establish the a reference program image. */
  723. if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
  724. {
  725. free_state (sd);
  726. return 0;
  727. }
  728. /* Configure/verify the target byte order and other runtime
  729. configuration options. */
  730. if (sim_config (sd) != SIM_RC_OK)
  731. {
  732. sim_module_uninstall (sd);
  733. return 0;
  734. }
  735. if (sim_post_argv_init (sd) != SIM_RC_OK)
  736. {
  737. /* Uninstall the modules to avoid memory leaks,
  738. file descriptor leaks, etc. */
  739. sim_module_uninstall (sd);
  740. return 0;
  741. }
  742. /* CPU specific initialization. */
  743. for (i = 0; i < MAX_NR_PROCESSORS; ++i)
  744. {
  745. SIM_CPU *cpu = STATE_CPU (sd, i);
  746. CPU_REG_FETCH (cpu) = arm_reg_fetch;
  747. CPU_REG_STORE (cpu) = arm_reg_store;
  748. CPU_PC_FETCH (cpu) = arm_pc_get;
  749. CPU_PC_STORE (cpu) = arm_pc_set;
  750. }
  751. sim_callback = cb;
  752. /* Copy over the argv contents so we can modify them. */
  753. argv_copy = dupargv (argv);
  754. sim_target_parse_arg_array (argv_copy);
  755. if (argv_copy[1] != NULL)
  756. {
  757. int i;
  758. /* Scan for memory-size switches. */
  759. for (i = 0; (argv_copy[i] != NULL) && (argv_copy[i][0] != 0); i++)
  760. if (argv_copy[i][0] == '-' && argv_copy[i][1] == 'm')
  761. {
  762. if (argv_copy[i][2] != '\0')
  763. mem_size = atoi (&argv_copy[i][2]);
  764. else if (argv_copy[i + 1] != NULL)
  765. {
  766. mem_size = atoi (argv_copy[i + 1]);
  767. i++;
  768. }
  769. else
  770. {
  771. sim_callback->printf_filtered (sim_callback,
  772. "Missing argument to -m option\n");
  773. return NULL;
  774. }
  775. }
  776. }
  777. freeargv (argv_copy);
  778. return sd;
  779. }
  780. void
  781. sim_stop_reason (SIM_DESC sd ATTRIBUTE_UNUSED,
  782. enum sim_stop *reason,
  783. int *sigrc)
  784. {
  785. if (stop_simulator)
  786. {
  787. *reason = sim_stopped;
  788. *sigrc = GDB_SIGNAL_INT;
  789. }
  790. else if (state->EndCondition == 0)
  791. {
  792. *reason = sim_exited;
  793. *sigrc = state->Reg[0] & 255;
  794. }
  795. else
  796. {
  797. *reason = sim_stopped;
  798. if (state->EndCondition == RDIError_BreakpointReached)
  799. *sigrc = GDB_SIGNAL_TRAP;
  800. else if ( state->EndCondition == RDIError_DataAbort
  801. || state->EndCondition == RDIError_AddressException)
  802. *sigrc = GDB_SIGNAL_BUS;
  803. else
  804. *sigrc = 0;
  805. }
  806. }