gdb-if.c 12 KB

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