gdb-if.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /* gdb-if.c -- sim interface to GDB.
  2. Copyright (C) 2011-2022 Free Software Foundation, Inc.
  3. Contributed by Red Hat, Inc.
  4. This file is part of the GNU simulators.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* This must come before any other includes. */
  16. #include "defs.h"
  17. #include <stdio.h>
  18. #include <assert.h>
  19. #include <signal.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include <stdlib.h>
  23. #include "ansidecl.h"
  24. #include "libiberty.h"
  25. #include "sim/callback.h"
  26. #include "sim/sim.h"
  27. #include "gdb/signals.h"
  28. #include "gdb/sim-rl78.h"
  29. #include "cpu.h"
  30. #include "mem.h"
  31. #include "load.h"
  32. #include "trace.h"
  33. /* Ideally, we'd wrap up all the minisim's data structures in an
  34. object and pass that around. However, neither GDB nor run needs
  35. that ability.
  36. So we just have one instance, that lives in global variables, and
  37. each time we open it, we re-initialize it. */
  38. struct sim_state
  39. {
  40. const char *message;
  41. };
  42. static struct sim_state the_minisim = {
  43. "This is the sole rl78 minisim instance."
  44. };
  45. static int is_open;
  46. static struct host_callback_struct *host_callbacks;
  47. /* Open an instance of the sim. For this sim, only one instance
  48. is permitted. If sim_open() is called multiple times, the sim
  49. will be reset. */
  50. SIM_DESC
  51. sim_open (SIM_OPEN_KIND kind,
  52. struct host_callback_struct *callback,
  53. struct bfd *abfd, char * const *argv)
  54. {
  55. if (is_open)
  56. fprintf (stderr, "rl78 minisim: re-opened sim\n");
  57. /* The 'run' interface doesn't use this function, so we don't care
  58. about KIND; it's always SIM_OPEN_DEBUG. */
  59. if (kind != SIM_OPEN_DEBUG)
  60. fprintf (stderr, "rl78 minisim: sim_open KIND != SIM_OPEN_DEBUG: %d\n",
  61. kind);
  62. /* We use this for the load command. Perhaps someday, it'll be used
  63. for syscalls too. */
  64. host_callbacks = callback;
  65. /* We don't expect any command-line arguments. */
  66. init_cpu ();
  67. trace = 0;
  68. sim_disasm_init (abfd);
  69. is_open = 1;
  70. while (argv != NULL && *argv != NULL)
  71. {
  72. if (strcmp (*argv, "g10") == 0 || strcmp (*argv, "-Mg10") == 0)
  73. {
  74. fprintf (stderr, "rl78 g10 support enabled.\n");
  75. rl78_g10_mode = 1;
  76. g13_multiply = 0;
  77. g14_multiply = 0;
  78. mem_set_mirror (0, 0xf8000, 4096);
  79. break;
  80. }
  81. if (strcmp (*argv, "g13") == 0 || strcmp (*argv, "-Mg13") == 0)
  82. {
  83. fprintf (stderr, "rl78 g13 support enabled.\n");
  84. rl78_g10_mode = 0;
  85. g13_multiply = 1;
  86. g14_multiply = 0;
  87. break;
  88. }
  89. if (strcmp (*argv, "g14") == 0 || strcmp (*argv, "-Mg14") == 0)
  90. {
  91. fprintf (stderr, "rl78 g14 support enabled.\n");
  92. rl78_g10_mode = 0;
  93. g13_multiply = 0;
  94. g14_multiply = 1;
  95. break;
  96. }
  97. argv++;
  98. }
  99. return &the_minisim;
  100. }
  101. /* Verify the sim descriptor. Just print a message if the descriptor
  102. doesn't match. Nothing bad will happen if the descriptor doesn't
  103. match because all of the state is global. But if it doesn't
  104. match, that means there's a problem with the caller. */
  105. static void
  106. check_desc (SIM_DESC sd)
  107. {
  108. if (sd != &the_minisim)
  109. fprintf (stderr, "rl78 minisim: desc != &the_minisim\n");
  110. }
  111. /* Close the sim. */
  112. void
  113. sim_close (SIM_DESC sd, int quitting)
  114. {
  115. check_desc (sd);
  116. /* Not much to do. At least free up our memory. */
  117. init_mem ();
  118. is_open = 0;
  119. }
  120. /* Open the program to run; print a message if the program cannot
  121. be opened. */
  122. static bfd *
  123. open_objfile (const char *filename)
  124. {
  125. bfd *prog = bfd_openr (filename, 0);
  126. if (!prog)
  127. {
  128. fprintf (stderr, "Can't read %s\n", filename);
  129. return 0;
  130. }
  131. if (!bfd_check_format (prog, bfd_object))
  132. {
  133. fprintf (stderr, "%s not a rl78 program\n", filename);
  134. return 0;
  135. }
  136. return prog;
  137. }
  138. /* Load a program. */
  139. SIM_RC
  140. sim_load (SIM_DESC sd, const char *prog, struct bfd *abfd, int from_tty)
  141. {
  142. check_desc (sd);
  143. if (!abfd)
  144. abfd = open_objfile (prog);
  145. if (!abfd)
  146. return SIM_RC_FAIL;
  147. rl78_load (abfd, host_callbacks, "sim");
  148. return SIM_RC_OK;
  149. }
  150. /* Create inferior. */
  151. SIM_RC
  152. sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
  153. char * const *argv, char * const *env)
  154. {
  155. check_desc (sd);
  156. if (abfd)
  157. rl78_load (abfd, 0, "sim");
  158. return SIM_RC_OK;
  159. }
  160. /* Read memory. */
  161. int
  162. sim_read (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
  163. {
  164. check_desc (sd);
  165. if (mem >= MEM_SIZE)
  166. return 0;
  167. else if (mem + length > MEM_SIZE)
  168. length = MEM_SIZE - mem;
  169. mem_get_blk (mem, buf, length);
  170. return length;
  171. }
  172. /* Write memory. */
  173. int
  174. sim_write (SIM_DESC sd, SIM_ADDR mem, const unsigned char *buf, int length)
  175. {
  176. check_desc (sd);
  177. if (mem >= MEM_SIZE)
  178. return 0;
  179. else if (mem + length > MEM_SIZE)
  180. length = MEM_SIZE - mem;
  181. mem_put_blk (mem, buf, length);
  182. return length;
  183. }
  184. /* Read the LENGTH bytes at BUF as an little-endian value. */
  185. static SI
  186. get_le (unsigned char *buf, int length)
  187. {
  188. SI acc = 0;
  189. while (--length >= 0)
  190. acc = (acc << 8) + buf[length];
  191. return acc;
  192. }
  193. /* Store VAL as a little-endian value in the LENGTH bytes at BUF. */
  194. static void
  195. put_le (unsigned char *buf, int length, SI val)
  196. {
  197. int i;
  198. for (i = 0; i < length; i++)
  199. {
  200. buf[i] = val & 0xff;
  201. val >>= 8;
  202. }
  203. }
  204. /* Verify that REGNO is in the proper range. Return 0 if not and
  205. something non-zero if so. */
  206. static int
  207. check_regno (enum sim_rl78_regnum regno)
  208. {
  209. return 0 <= regno && regno < sim_rl78_num_regs;
  210. }
  211. /* Return the size of the register REGNO. */
  212. static size_t
  213. reg_size (enum sim_rl78_regnum regno)
  214. {
  215. size_t size;
  216. if (regno == sim_rl78_pc_regnum)
  217. size = 4;
  218. else
  219. size = 1;
  220. return size;
  221. }
  222. /* Return the register address associated with the register specified by
  223. REGNO. */
  224. static unsigned long
  225. reg_addr (enum sim_rl78_regnum regno)
  226. {
  227. if (sim_rl78_bank0_r0_regnum <= regno
  228. && regno <= sim_rl78_bank0_r7_regnum)
  229. return 0xffef8 + (regno - sim_rl78_bank0_r0_regnum);
  230. else if (sim_rl78_bank1_r0_regnum <= regno
  231. && regno <= sim_rl78_bank1_r7_regnum)
  232. return 0xffef0 + (regno - sim_rl78_bank1_r0_regnum);
  233. else if (sim_rl78_bank2_r0_regnum <= regno
  234. && regno <= sim_rl78_bank2_r7_regnum)
  235. return 0xffee8 + (regno - sim_rl78_bank2_r0_regnum);
  236. else if (sim_rl78_bank3_r0_regnum <= regno
  237. && regno <= sim_rl78_bank3_r7_regnum)
  238. return 0xffee0 + (regno - sim_rl78_bank3_r0_regnum);
  239. else if (regno == sim_rl78_psw_regnum)
  240. return 0xffffa;
  241. else if (regno == sim_rl78_es_regnum)
  242. return 0xffffd;
  243. else if (regno == sim_rl78_cs_regnum)
  244. return 0xffffc;
  245. /* Note: We can't handle PC here because it's not memory mapped. */
  246. else if (regno == sim_rl78_spl_regnum)
  247. return 0xffff8;
  248. else if (regno == sim_rl78_sph_regnum)
  249. return 0xffff9;
  250. else if (regno == sim_rl78_pmc_regnum)
  251. return 0xffffe;
  252. else if (regno == sim_rl78_mem_regnum)
  253. return 0xfffff;
  254. return 0;
  255. }
  256. /* Fetch the contents of the register specified by REGNO, placing the
  257. contents in BUF. The length LENGTH must match the sim's internal
  258. notion of the register's size. */
  259. int
  260. sim_fetch_register (SIM_DESC sd, int regno, unsigned char *buf, int length)
  261. {
  262. size_t size;
  263. SI val;
  264. check_desc (sd);
  265. if (!check_regno (regno))
  266. return 0;
  267. size = reg_size (regno);
  268. if (length != size)
  269. return 0;
  270. if (regno == sim_rl78_pc_regnum)
  271. val = pc;
  272. else
  273. val = memory[reg_addr (regno)];
  274. put_le (buf, length, val);
  275. return size;
  276. }
  277. /* Store the value stored in BUF to the register REGNO. The length
  278. LENGTH must match the sim's internal notion of the register size. */
  279. int
  280. sim_store_register (SIM_DESC sd, int regno, unsigned char *buf, int length)
  281. {
  282. size_t size;
  283. SI val;
  284. check_desc (sd);
  285. if (!check_regno (regno))
  286. return -1;
  287. size = reg_size (regno);
  288. if (length != size)
  289. return -1;
  290. val = get_le (buf, length);
  291. if (regno == sim_rl78_pc_regnum)
  292. {
  293. pc = val;
  294. /* The rl78 program counter is 20 bits wide. Ensure that GDB
  295. hasn't picked up any stray bits. This has occurred when performing
  296. a GDB "return" command in which the return address is obtained
  297. from a 32-bit container on the stack. */
  298. assert ((pc & ~0x0fffff) == 0);
  299. }
  300. else
  301. memory[reg_addr (regno)] = val;
  302. return size;
  303. }
  304. /* Print out message associated with "info target". */
  305. void
  306. sim_info (SIM_DESC sd, int verbose)
  307. {
  308. check_desc (sd);
  309. printf ("The rl78 minisim doesn't collect any statistics.\n");
  310. }
  311. static volatile int stop;
  312. static enum sim_stop reason;
  313. int siggnal;
  314. /* Given a signal number used by the rl78 bsp (that is, newlib),
  315. return the corresponding signal numbers. */
  316. static int
  317. rl78_signal_to_target (int sig)
  318. {
  319. switch (sig)
  320. {
  321. case 4:
  322. return GDB_SIGNAL_ILL;
  323. case 5:
  324. return GDB_SIGNAL_TRAP;
  325. case 10:
  326. return GDB_SIGNAL_BUS;
  327. case 11:
  328. return GDB_SIGNAL_SEGV;
  329. case 24:
  330. return GDB_SIGNAL_XCPU;
  331. break;
  332. case 2:
  333. return GDB_SIGNAL_INT;
  334. case 8:
  335. return GDB_SIGNAL_FPE;
  336. break;
  337. case 6:
  338. return GDB_SIGNAL_ABRT;
  339. }
  340. return 0;
  341. }
  342. /* Take a step return code RC and set up the variables consulted by
  343. sim_stop_reason appropriately. */
  344. static void
  345. handle_step (int rc)
  346. {
  347. if (RL78_STEPPED (rc) || RL78_HIT_BREAK (rc))
  348. {
  349. reason = sim_stopped;
  350. siggnal = GDB_SIGNAL_TRAP;
  351. }
  352. else if (RL78_STOPPED (rc))
  353. {
  354. reason = sim_stopped;
  355. siggnal = rl78_signal_to_target (RL78_STOP_SIG (rc));
  356. }
  357. else
  358. {
  359. assert (RL78_EXITED (rc));
  360. reason = sim_exited;
  361. siggnal = RL78_EXIT_STATUS (rc);
  362. }
  363. }
  364. /* Resume execution after a stop. */
  365. void
  366. sim_resume (SIM_DESC sd, int step, int sig_to_deliver)
  367. {
  368. int rc;
  369. check_desc (sd);
  370. if (sig_to_deliver != 0)
  371. {
  372. fprintf (stderr,
  373. "Warning: the rl78 minisim does not implement "
  374. "signal delivery yet.\n" "Resuming with no signal.\n");
  375. }
  376. /* We don't clear 'stop' here, because then we would miss
  377. interrupts that arrived on the way here. Instead, we clear
  378. the flag in sim_stop_reason, after GDB has disabled the
  379. interrupt signal handler. */
  380. for (;;)
  381. {
  382. if (stop)
  383. {
  384. stop = 0;
  385. reason = sim_stopped;
  386. siggnal = GDB_SIGNAL_INT;
  387. break;
  388. }
  389. rc = setjmp (decode_jmp_buf);
  390. if (rc == 0)
  391. rc = decode_opcode ();
  392. if (!RL78_STEPPED (rc) || step)
  393. {
  394. handle_step (rc);
  395. break;
  396. }
  397. }
  398. }
  399. /* Stop the sim. */
  400. int
  401. sim_stop (SIM_DESC sd)
  402. {
  403. stop = 1;
  404. return 1;
  405. }
  406. /* Fetch the stop reason and signal. */
  407. void
  408. sim_stop_reason (SIM_DESC sd, enum sim_stop *reason_p, int *sigrc_p)
  409. {
  410. check_desc (sd);
  411. *reason_p = reason;
  412. *sigrc_p = siggnal;
  413. }
  414. /* Execute the sim-specific command associated with GDB's "sim ..."
  415. command. */
  416. void
  417. sim_do_command (SIM_DESC sd, const char *cmd)
  418. {
  419. const char *arg;
  420. char **argv = buildargv (cmd);
  421. check_desc (sd);
  422. cmd = arg = "";
  423. if (argv != NULL)
  424. {
  425. if (argv[0] != NULL)
  426. cmd = argv[0];
  427. if (argv[1] != NULL)
  428. arg = argv[1];
  429. }
  430. if (strcmp (cmd, "trace") == 0)
  431. {
  432. if (strcmp (arg, "on") == 0)
  433. trace = 1;
  434. else if (strcmp (arg, "off") == 0)
  435. trace = 0;
  436. else
  437. printf ("The 'sim trace' command expects 'on' or 'off' "
  438. "as an argument.\n");
  439. }
  440. else if (strcmp (cmd, "verbose") == 0)
  441. {
  442. if (strcmp (arg, "on") == 0)
  443. verbose = 1;
  444. else if (strcmp (arg, "noisy") == 0)
  445. verbose = 2;
  446. else if (strcmp (arg, "off") == 0)
  447. verbose = 0;
  448. else
  449. printf ("The 'sim verbose' command expects 'on', 'noisy', or 'off'"
  450. " as an argument.\n");
  451. }
  452. else
  453. printf ("The 'sim' command expects either 'trace' or 'verbose'"
  454. " as a subcommand.\n");
  455. freeargv (argv);
  456. }
  457. /* Stub for command completion. */
  458. char **
  459. sim_complete_command (SIM_DESC sd, const char *text, const char *word)
  460. {
  461. return NULL;
  462. }
  463. char *
  464. sim_memory_map (SIM_DESC sd)
  465. {
  466. return NULL;
  467. }