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