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