]>
Commit | Line | Data |
---|---|---|
9058f767 KB |
1 | /* gdb-if.c -- sim interface to GDB. |
2 | ||
ecd75fc8 | 3 | Copyright (C) 2011-2014 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, | |
67 | struct bfd *abfd, char **argv) | |
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; | |
89 | return &the_minisim; | |
90 | } | |
91 | ||
92 | /* Verify the sim descriptor. Just print a message if the descriptor | |
93 | doesn't match. Nothing bad will happen if the descriptor doesn't | |
94 | match because all of the state is global. But if it doesn't | |
95 | match, that means there's a problem with the caller. */ | |
96 | ||
97 | static void | |
98 | check_desc (SIM_DESC sd) | |
99 | { | |
100 | if (sd != &the_minisim) | |
101 | fprintf (stderr, "rl78 minisim: desc != &the_minisim\n"); | |
102 | } | |
103 | ||
104 | /* Close the sim. */ | |
105 | ||
106 | void | |
107 | sim_close (SIM_DESC sd, int quitting) | |
108 | { | |
109 | check_desc (sd); | |
110 | ||
111 | /* Not much to do. At least free up our memory. */ | |
112 | init_mem (); | |
113 | ||
114 | open = 0; | |
115 | } | |
116 | ||
117 | /* Open the program to run; print a message if the program cannot | |
118 | be opened. */ | |
119 | ||
120 | static bfd * | |
121 | open_objfile (const char *filename) | |
122 | { | |
123 | bfd *prog = bfd_openr (filename, 0); | |
124 | ||
125 | if (!prog) | |
126 | { | |
127 | fprintf (stderr, "Can't read %s\n", filename); | |
128 | return 0; | |
129 | } | |
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 | ||
137 | return prog; | |
138 | } | |
139 | ||
140 | /* Load a program. */ | |
141 | ||
142 | SIM_RC | |
b2b255bd | 143 | sim_load (SIM_DESC sd, const char *prog, struct bfd *abfd, int from_tty) |
9058f767 KB |
144 | { |
145 | check_desc (sd); | |
146 | ||
147 | if (!abfd) | |
148 | abfd = open_objfile (prog); | |
149 | if (!abfd) | |
150 | return SIM_RC_FAIL; | |
151 | ||
152 | rl78_load (abfd, host_callbacks, "sim"); | |
153 | ||
154 | return SIM_RC_OK; | |
155 | } | |
156 | ||
157 | /* Create inferior. */ | |
158 | ||
159 | SIM_RC | |
160 | sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char **argv, char **env) | |
161 | { | |
162 | check_desc (sd); | |
163 | ||
164 | if (abfd) | |
165 | rl78_load (abfd, 0, "sim"); | |
166 | ||
167 | return SIM_RC_OK; | |
168 | } | |
169 | ||
170 | /* Read memory. */ | |
171 | ||
172 | int | |
173 | sim_read (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length) | |
174 | { | |
175 | check_desc (sd); | |
176 | ||
177 | if (mem >= MEM_SIZE) | |
178 | return 0; | |
179 | else if (mem + length > MEM_SIZE) | |
180 | length = MEM_SIZE - mem; | |
181 | ||
182 | mem_get_blk (mem, buf, length); | |
183 | return length; | |
184 | } | |
185 | ||
186 | /* Write memory. */ | |
187 | ||
188 | int | |
189 | sim_write (SIM_DESC sd, SIM_ADDR mem, const unsigned char *buf, int length) | |
190 | { | |
191 | check_desc (sd); | |
192 | ||
193 | if (mem >= MEM_SIZE) | |
194 | return 0; | |
195 | else if (mem + length > MEM_SIZE) | |
196 | length = MEM_SIZE - mem; | |
197 | ||
198 | mem_put_blk (mem, buf, length); | |
199 | return length; | |
200 | } | |
201 | ||
202 | /* Read the LENGTH bytes at BUF as an little-endian value. */ | |
203 | ||
204 | static SI | |
205 | get_le (unsigned char *buf, int length) | |
206 | { | |
207 | SI acc = 0; | |
208 | ||
209 | while (--length >= 0) | |
210 | acc = (acc << 8) + buf[length]; | |
211 | ||
212 | return acc; | |
213 | } | |
214 | ||
215 | /* Store VAL as a little-endian value in the LENGTH bytes at BUF. */ | |
216 | ||
217 | static void | |
218 | put_le (unsigned char *buf, int length, SI val) | |
219 | { | |
220 | int i; | |
221 | ||
222 | for (i = 0; i < length; i++) | |
223 | { | |
224 | buf[i] = val & 0xff; | |
225 | val >>= 8; | |
226 | } | |
227 | } | |
228 | ||
229 | /* Verify that REGNO is in the proper range. Return 0 if not and | |
230 | something non-zero if so. */ | |
231 | ||
232 | static int | |
233 | check_regno (enum sim_rl78_regnum regno) | |
234 | { | |
235 | return 0 <= regno && regno < sim_rl78_num_regs; | |
236 | } | |
237 | ||
238 | /* Return the size of the register REGNO. */ | |
239 | ||
240 | static size_t | |
241 | reg_size (enum sim_rl78_regnum regno) | |
242 | { | |
243 | size_t size; | |
244 | ||
245 | if (regno == sim_rl78_pc_regnum) | |
246 | size = 4; | |
247 | else | |
248 | size = 1; | |
249 | ||
250 | return size; | |
251 | } | |
252 | ||
253 | /* Return the register address associated with the register specified by | |
254 | REGNO. */ | |
255 | ||
256 | static unsigned long | |
257 | reg_addr (enum sim_rl78_regnum regno) | |
258 | { | |
259 | if (sim_rl78_bank0_r0_regnum <= regno | |
260 | && regno <= sim_rl78_bank0_r7_regnum) | |
261 | return 0xffef8 + (regno - sim_rl78_bank0_r0_regnum); | |
262 | else if (sim_rl78_bank1_r0_regnum <= regno | |
263 | && regno <= sim_rl78_bank1_r7_regnum) | |
264 | return 0xffef0 + (regno - sim_rl78_bank1_r0_regnum); | |
265 | else if (sim_rl78_bank2_r0_regnum <= regno | |
266 | && regno <= sim_rl78_bank2_r7_regnum) | |
267 | return 0xffee8 + (regno - sim_rl78_bank2_r0_regnum); | |
268 | else if (sim_rl78_bank3_r0_regnum <= regno | |
269 | && regno <= sim_rl78_bank3_r7_regnum) | |
270 | return 0xffee0 + (regno - sim_rl78_bank3_r0_regnum); | |
271 | else if (regno == sim_rl78_psw_regnum) | |
272 | return 0xffffa; | |
273 | else if (regno == sim_rl78_es_regnum) | |
274 | return 0xffffd; | |
275 | else if (regno == sim_rl78_cs_regnum) | |
276 | return 0xffffc; | |
277 | /* Note: We can't handle PC here because it's not memory mapped. */ | |
278 | else if (regno == sim_rl78_spl_regnum) | |
279 | return 0xffff8; | |
280 | else if (regno == sim_rl78_sph_regnum) | |
281 | return 0xffff9; | |
282 | else if (regno == sim_rl78_pmc_regnum) | |
283 | return 0xffffe; | |
284 | else if (regno == sim_rl78_mem_regnum) | |
285 | return 0xfffff; | |
286 | ||
287 | return 0; | |
288 | } | |
289 | ||
290 | /* Fetch the contents of the register specified by REGNO, placing the | |
291 | contents in BUF. The length LENGTH must match the sim's internal | |
292 | notion of the register's size. */ | |
293 | ||
294 | int | |
295 | sim_fetch_register (SIM_DESC sd, int regno, unsigned char *buf, int length) | |
296 | { | |
297 | size_t size; | |
298 | SI val; | |
299 | ||
300 | check_desc (sd); | |
301 | ||
302 | if (!check_regno (regno)) | |
303 | return 0; | |
304 | ||
305 | size = reg_size (regno); | |
306 | ||
307 | if (length != size) | |
308 | return 0; | |
309 | ||
310 | if (regno == sim_rl78_pc_regnum) | |
311 | val = pc; | |
312 | else | |
313 | val = memory[reg_addr (regno)]; | |
314 | ||
315 | put_le (buf, length, val); | |
316 | ||
317 | return size; | |
318 | } | |
319 | ||
320 | /* Store the value stored in BUF to the register REGNO. The length | |
321 | LENGTH must match the sim's internal notion of the register size. */ | |
322 | ||
323 | int | |
324 | sim_store_register (SIM_DESC sd, int regno, unsigned char *buf, int length) | |
325 | { | |
326 | size_t size; | |
327 | SI val; | |
328 | ||
329 | check_desc (sd); | |
330 | ||
331 | if (!check_regno (regno)) | |
332 | return -1; | |
333 | ||
334 | size = reg_size (regno); | |
335 | ||
336 | if (length != size) | |
337 | return -1; | |
338 | ||
339 | val = get_le (buf, length); | |
340 | ||
341 | if (regno == sim_rl78_pc_regnum) | |
317cc67d KB |
342 | { |
343 | pc = val; | |
344 | ||
345 | /* The rl78 program counter is 20 bits wide. Ensure that GDB | |
346 | hasn't picked up any stray bits. This has occurred when performing | |
347 | a GDB "return" command in which the return address is obtained | |
348 | from a 32-bit container on the stack. */ | |
349 | assert ((pc & ~0x0fffff) == 0); | |
350 | } | |
9058f767 KB |
351 | else |
352 | memory[reg_addr (regno)] = val; | |
353 | return size; | |
354 | } | |
355 | ||
356 | /* Print out message associated with "info target". */ | |
357 | ||
358 | void | |
359 | sim_info (SIM_DESC sd, int verbose) | |
360 | { | |
361 | check_desc (sd); | |
362 | ||
363 | printf ("The rl78 minisim doesn't collect any statistics.\n"); | |
364 | } | |
365 | ||
366 | static volatile int stop; | |
367 | static enum sim_stop reason; | |
368 | int siggnal; | |
369 | ||
370 | ||
371 | /* Given a signal number used by the rl78 bsp (that is, newlib), | |
372 | return the corresponding signal numbers. */ | |
373 | ||
374 | int | |
375 | rl78_signal_to_target (int sig) | |
376 | { | |
377 | switch (sig) | |
378 | { | |
379 | case 4: | |
a493e3e2 | 380 | return GDB_SIGNAL_ILL; |
9058f767 KB |
381 | |
382 | case 5: | |
a493e3e2 | 383 | return GDB_SIGNAL_TRAP; |
9058f767 KB |
384 | |
385 | case 10: | |
a493e3e2 | 386 | return GDB_SIGNAL_BUS; |
9058f767 KB |
387 | |
388 | case 11: | |
a493e3e2 | 389 | return GDB_SIGNAL_SEGV; |
9058f767 KB |
390 | |
391 | case 24: | |
a493e3e2 | 392 | return GDB_SIGNAL_XCPU; |
9058f767 KB |
393 | break; |
394 | ||
395 | case 2: | |
a493e3e2 | 396 | return GDB_SIGNAL_INT; |
9058f767 KB |
397 | |
398 | case 8: | |
a493e3e2 | 399 | return GDB_SIGNAL_FPE; |
9058f767 KB |
400 | break; |
401 | ||
402 | case 6: | |
a493e3e2 | 403 | return GDB_SIGNAL_ABRT; |
9058f767 KB |
404 | } |
405 | ||
406 | return 0; | |
407 | } | |
408 | ||
409 | ||
410 | /* Take a step return code RC and set up the variables consulted by | |
411 | sim_stop_reason appropriately. */ | |
412 | ||
413 | void | |
414 | handle_step (int rc) | |
415 | { | |
416 | if (RL78_STEPPED (rc) || RL78_HIT_BREAK (rc)) | |
417 | { | |
418 | reason = sim_stopped; | |
a493e3e2 | 419 | siggnal = GDB_SIGNAL_TRAP; |
9058f767 KB |
420 | } |
421 | else if (RL78_STOPPED (rc)) | |
422 | { | |
423 | reason = sim_stopped; | |
424 | siggnal = rl78_signal_to_target (RL78_STOP_SIG (rc)); | |
425 | } | |
426 | else | |
427 | { | |
428 | assert (RL78_EXITED (rc)); | |
429 | reason = sim_exited; | |
430 | siggnal = RL78_EXIT_STATUS (rc); | |
431 | } | |
432 | } | |
433 | ||
434 | ||
435 | /* Resume execution after a stop. */ | |
436 | ||
437 | void | |
438 | sim_resume (SIM_DESC sd, int step, int sig_to_deliver) | |
439 | { | |
440 | int rc; | |
441 | ||
442 | check_desc (sd); | |
443 | ||
444 | if (sig_to_deliver != 0) | |
445 | { | |
446 | fprintf (stderr, | |
447 | "Warning: the rl78 minisim does not implement " | |
448 | "signal delivery yet.\n" "Resuming with no signal.\n"); | |
449 | } | |
450 | ||
451 | /* We don't clear 'stop' here, because then we would miss | |
452 | interrupts that arrived on the way here. Instead, we clear | |
453 | the flag in sim_stop_reason, after GDB has disabled the | |
454 | interrupt signal handler. */ | |
455 | for (;;) | |
456 | { | |
457 | if (stop) | |
458 | { | |
459 | stop = 0; | |
460 | reason = sim_stopped; | |
a493e3e2 | 461 | siggnal = GDB_SIGNAL_INT; |
9058f767 KB |
462 | break; |
463 | } | |
464 | ||
9058f767 KB |
465 | rc = setjmp (decode_jmp_buf); |
466 | if (rc == 0) | |
467 | rc = decode_opcode (); | |
468 | ||
469 | if (!RL78_STEPPED (rc) || step) | |
470 | { | |
471 | handle_step (rc); | |
472 | break; | |
473 | } | |
474 | } | |
475 | } | |
476 | ||
477 | /* Stop the sim. */ | |
478 | ||
479 | int | |
480 | sim_stop (SIM_DESC sd) | |
481 | { | |
482 | stop = 1; | |
483 | ||
484 | return 1; | |
485 | } | |
486 | ||
487 | /* Fetch the stop reason and signal. */ | |
488 | ||
489 | void | |
490 | sim_stop_reason (SIM_DESC sd, enum sim_stop *reason_p, int *sigrc_p) | |
491 | { | |
492 | check_desc (sd); | |
493 | ||
494 | *reason_p = reason; | |
495 | *sigrc_p = siggnal; | |
496 | } | |
497 | ||
498 | /* Execute the sim-specific command associated with GDB's "sim ..." | |
499 | command. */ | |
500 | ||
501 | void | |
502 | sim_do_command (SIM_DESC sd, char *cmd) | |
503 | { | |
504 | char *args; | |
505 | ||
506 | check_desc (sd); | |
507 | ||
508 | if (cmd == NULL) | |
509 | { | |
510 | cmd = ""; | |
511 | args = ""; | |
512 | } | |
513 | else | |
514 | { | |
515 | char *p = cmd; | |
516 | ||
517 | /* Skip leading whitespace. */ | |
518 | while (isspace (*p)) | |
519 | p++; | |
520 | ||
521 | /* Find the extent of the command word. */ | |
522 | for (p = cmd; *p; p++) | |
523 | if (isspace (*p)) | |
524 | break; | |
525 | ||
526 | /* Null-terminate the command word, and record the start of any | |
527 | further arguments. */ | |
528 | if (*p) | |
529 | { | |
530 | *p = '\0'; | |
531 | args = p + 1; | |
532 | while (isspace (*args)) | |
533 | args++; | |
534 | } | |
535 | else | |
536 | args = p; | |
537 | } | |
538 | ||
539 | if (strcmp (cmd, "trace") == 0) | |
540 | { | |
541 | if (strcmp (args, "on") == 0) | |
542 | trace = 1; | |
543 | else if (strcmp (args, "off") == 0) | |
544 | trace = 0; | |
545 | else | |
546 | printf ("The 'sim trace' command expects 'on' or 'off' " | |
547 | "as an argument.\n"); | |
548 | } | |
549 | else if (strcmp (cmd, "verbose") == 0) | |
550 | { | |
551 | if (strcmp (args, "on") == 0) | |
552 | verbose = 1; | |
553 | else if (strcmp (args, "noisy") == 0) | |
554 | verbose = 2; | |
555 | else if (strcmp (args, "off") == 0) | |
556 | verbose = 0; | |
557 | else | |
558 | printf ("The 'sim verbose' command expects 'on', 'noisy', or 'off'" | |
559 | " as an argument.\n"); | |
560 | } | |
561 | else | |
562 | printf ("The 'sim' command expects either 'trace' or 'verbose'" | |
563 | " as a subcommand.\n"); | |
564 | } | |
565 | ||
566 | /* Stub for command completion. */ | |
567 | ||
568 | char ** | |
3cb2ab1a | 569 | sim_complete_command (SIM_DESC sd, const char *text, const char *word) |
9058f767 KB |
570 | { |
571 | return NULL; | |
572 | } |