1 /* gdb-if.c -- sim interface to GDB.
3 Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 Contributed by Red Hat, Inc.
6 This file is part of the GNU simulators.
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.
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.
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/>. */
30 #include "gdb/callback.h"
31 #include "gdb/remote-sim.h"
32 #include "gdb/signals.h"
33 #include "gdb/sim-rx.h"
42 /* Ideally, we'd wrap up all the minisim's data structures in an
43 object and pass that around. However, neither GDB nor run needs
46 So we just have one instance, that lives in global variables, and
47 each time we open it, we re-initialize it. */
53 static struct sim_state the_minisim = {
54 "This is the sole rx minisim instance. See libsim.a's global variables."
60 sim_open (SIM_OPEN_KIND kind,
61 struct host_callback_struct *callback,
62 struct bfd *abfd, char **argv)
65 fprintf (stderr, "rx minisim: re-opened sim\n");
67 /* The 'run' interface doesn't use this function, so we don't care
68 about KIND; it's always SIM_OPEN_DEBUG. */
69 if (kind != SIM_OPEN_DEBUG)
70 fprintf (stderr, "rx minisim: sim_open KIND != SIM_OPEN_DEBUG: %d\n",
73 set_callbacks (callback);
75 /* We don't expect any command-line arguments. */
79 execution_error_init_debugger ();
81 sim_disasm_init (abfd);
87 check_desc (SIM_DESC sd)
89 if (sd != &the_minisim)
90 fprintf (stderr, "rx minisim: desc != &the_minisim\n");
94 sim_close (SIM_DESC sd, int quitting)
98 /* Not much to do. At least free up our memory. */
105 open_objfile (const char *filename)
107 bfd *prog = bfd_openr (filename, 0);
111 fprintf (stderr, "Can't read %s\n", filename);
115 if (!bfd_check_format (prog, bfd_object))
117 fprintf (stderr, "%s not a rx program\n", filename);
124 static struct swap_list
127 struct swap_list *next;
131 free_swap_list (void)
135 struct swap_list *next = swap_list->next;
141 /* When running in big endian mode, we must do an additional
142 byte swap of memory areas used to hold instructions. See
143 the comment preceding rx_load in load.c to see why this is
146 Construct a list of memory areas that must be byte swapped.
147 This list will be consulted when either reading or writing
151 build_swap_list (struct bfd *abfd)
156 /* Nothing to do when in little endian mode. */
160 for (s = abfd->sections; s; s = s->next)
162 if ((s->flags & SEC_LOAD) && (s->flags & SEC_CODE))
164 struct swap_list *sl;
167 size = bfd_get_section_size (s);
171 sl = malloc (sizeof (struct swap_list));
173 sl->next = swap_list;
174 sl->start = bfd_section_lma (abfd, s);
175 sl->end = sl->start + size;
182 addr_in_swap_list (bfd_vma addr)
186 for (s = swap_list; s; s = s->next)
188 if (s->start <= addr && addr < s->end)
195 sim_load (SIM_DESC sd, char *prog, struct bfd *abfd, int from_tty)
200 abfd = open_objfile (prog);
205 build_swap_list (abfd);
211 sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char **argv, char **env)
218 build_swap_list (abfd);
225 sim_read (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
234 execution_error_clear_last_error ();
236 for (i = 0; i < length; i++)
238 bfd_vma addr = mem + i;
239 int do_swap = addr_in_swap_list (addr);
240 buf[i] = mem_get_qi (addr ^ (do_swap ? 3 : 0));
242 if (execution_error_get_last_error () != SIM_ERR_NONE)
250 sim_write (SIM_DESC sd, SIM_ADDR mem, const unsigned char *buf, int length)
256 execution_error_clear_last_error ();
258 for (i = 0; i < length; i++)
260 bfd_vma addr = mem + i;
261 int do_swap = addr_in_swap_list (addr);
262 mem_put_qi (addr ^ (do_swap ? 3 : 0), buf[i]);
264 if (execution_error_get_last_error () != SIM_ERR_NONE)
271 /* Read the LENGTH bytes at BUF as an little-endian value. */
273 get_le (unsigned char *buf, int length)
276 while (--length >= 0)
277 acc = (acc << 8) + buf[length];
282 /* Read the LENGTH bytes at BUF as a big-endian value. */
284 get_be (unsigned char *buf, int length)
288 acc = (acc << 8) + *buf++;
293 /* Store VAL as a little-endian value in the LENGTH bytes at BUF. */
295 put_le (unsigned char *buf, int length, DI val)
299 for (i = 0; i < length; i++)
306 /* Store VAL as a big-endian value in the LENGTH bytes at BUF. */
308 put_be (unsigned char *buf, int length, DI val)
312 for (i = length-1; i >= 0; i--)
321 check_regno (enum sim_rx_regnum regno)
323 return 0 <= regno && regno < sim_rx_num_regs;
327 reg_size (enum sim_rx_regnum regno)
333 case sim_rx_r0_regnum:
334 size = sizeof (regs.r[0]);
336 case sim_rx_r1_regnum:
337 size = sizeof (regs.r[1]);
339 case sim_rx_r2_regnum:
340 size = sizeof (regs.r[2]);
342 case sim_rx_r3_regnum:
343 size = sizeof (regs.r[3]);
345 case sim_rx_r4_regnum:
346 size = sizeof (regs.r[4]);
348 case sim_rx_r5_regnum:
349 size = sizeof (regs.r[5]);
351 case sim_rx_r6_regnum:
352 size = sizeof (regs.r[6]);
354 case sim_rx_r7_regnum:
355 size = sizeof (regs.r[7]);
357 case sim_rx_r8_regnum:
358 size = sizeof (regs.r[8]);
360 case sim_rx_r9_regnum:
361 size = sizeof (regs.r[9]);
363 case sim_rx_r10_regnum:
364 size = sizeof (regs.r[10]);
366 case sim_rx_r11_regnum:
367 size = sizeof (regs.r[11]);
369 case sim_rx_r12_regnum:
370 size = sizeof (regs.r[12]);
372 case sim_rx_r13_regnum:
373 size = sizeof (regs.r[13]);
375 case sim_rx_r14_regnum:
376 size = sizeof (regs.r[14]);
378 case sim_rx_r15_regnum:
379 size = sizeof (regs.r[15]);
381 case sim_rx_isp_regnum:
382 size = sizeof (regs.r_isp);
384 case sim_rx_usp_regnum:
385 size = sizeof (regs.r_usp);
387 case sim_rx_intb_regnum:
388 size = sizeof (regs.r_intb);
390 case sim_rx_pc_regnum:
391 size = sizeof (regs.r_pc);
393 case sim_rx_ps_regnum:
394 size = sizeof (regs.r_psw);
396 case sim_rx_bpc_regnum:
397 size = sizeof (regs.r_bpc);
399 case sim_rx_bpsw_regnum:
400 size = sizeof (regs.r_bpsw);
402 case sim_rx_fintv_regnum:
403 size = sizeof (regs.r_fintv);
405 case sim_rx_fpsw_regnum:
406 size = sizeof (regs.r_fpsw);
408 case sim_rx_acc_regnum:
409 size = sizeof (regs.r_acc);
419 sim_fetch_register (SIM_DESC sd, int regno, unsigned char *buf, int length)
426 if (!check_regno (regno))
429 size = reg_size (regno);
436 case sim_rx_r0_regnum:
439 case sim_rx_r1_regnum:
442 case sim_rx_r2_regnum:
445 case sim_rx_r3_regnum:
448 case sim_rx_r4_regnum:
451 case sim_rx_r5_regnum:
454 case sim_rx_r6_regnum:
457 case sim_rx_r7_regnum:
460 case sim_rx_r8_regnum:
463 case sim_rx_r9_regnum:
466 case sim_rx_r10_regnum:
469 case sim_rx_r11_regnum:
472 case sim_rx_r12_regnum:
475 case sim_rx_r13_regnum:
478 case sim_rx_r14_regnum:
481 case sim_rx_r15_regnum:
484 case sim_rx_isp_regnum:
487 case sim_rx_usp_regnum:
490 case sim_rx_intb_regnum:
491 val = get_reg (intb);
493 case sim_rx_pc_regnum:
496 case sim_rx_ps_regnum:
499 case sim_rx_bpc_regnum:
502 case sim_rx_bpsw_regnum:
503 val = get_reg (bpsw);
505 case sim_rx_fintv_regnum:
506 val = get_reg (fintv);
508 case sim_rx_fpsw_regnum:
509 val = get_reg (fpsw);
511 case sim_rx_acc_regnum:
512 val = ((DI) get_reg (acchi) << 32) | get_reg (acclo);
515 fprintf (stderr, "rx minisim: unrecognized register number: %d\n",
521 put_be (buf, length, val);
523 put_le (buf, length, val);
529 sim_store_register (SIM_DESC sd, int regno, unsigned char *buf, int length)
536 if (!check_regno (regno))
539 size = reg_size (regno);
545 val = get_be (buf, length);
547 val = get_le (buf, length);
551 case sim_rx_r0_regnum:
554 case sim_rx_r1_regnum:
557 case sim_rx_r2_regnum:
560 case sim_rx_r3_regnum:
563 case sim_rx_r4_regnum:
566 case sim_rx_r5_regnum:
569 case sim_rx_r6_regnum:
572 case sim_rx_r7_regnum:
575 case sim_rx_r8_regnum:
578 case sim_rx_r9_regnum:
581 case sim_rx_r10_regnum:
584 case sim_rx_r11_regnum:
587 case sim_rx_r12_regnum:
590 case sim_rx_r13_regnum:
593 case sim_rx_r14_regnum:
596 case sim_rx_r15_regnum:
599 case sim_rx_isp_regnum:
602 case sim_rx_usp_regnum:
605 case sim_rx_intb_regnum:
608 case sim_rx_pc_regnum:
611 case sim_rx_ps_regnum:
614 case sim_rx_bpc_regnum:
617 case sim_rx_bpsw_regnum:
620 case sim_rx_fintv_regnum:
621 put_reg (fintv, val);
623 case sim_rx_fpsw_regnum:
626 case sim_rx_acc_regnum:
627 put_reg (acclo, val & 0xffffffff);
628 put_reg (acchi, (val >> 32) & 0xffffffff);
631 fprintf (stderr, "rx minisim: unrecognized register number: %d\n",
640 sim_info (SIM_DESC sd, int verbose)
644 printf ("The rx minisim doesn't collect any statistics.\n");
647 static volatile int stop;
648 static enum sim_stop reason;
652 /* Given a signal number used by the RX bsp (that is, newlib),
653 return a host signal number. (Oddly, the gdb/sim interface uses
654 host signal numbers...) */
656 rx_signal_to_host (int rx)
705 /* Take a step return code RC and set up the variables consulted by
706 sim_stop_reason appropriately. */
710 if (execution_error_get_last_error () != SIM_ERR_NONE)
712 reason = sim_stopped;
713 siggnal = TARGET_SIGNAL_SEGV;
715 if (RX_STEPPED (rc) || RX_HIT_BREAK (rc))
717 reason = sim_stopped;
718 siggnal = TARGET_SIGNAL_TRAP;
720 else if (RX_STOPPED (rc))
722 reason = sim_stopped;
723 siggnal = rx_signal_to_host (RX_STOP_SIG (rc));
727 assert (RX_EXITED (rc));
729 siggnal = RX_EXIT_STATUS (rc);
735 sim_resume (SIM_DESC sd, int step, int sig_to_deliver)
741 if (sig_to_deliver != 0)
744 "Warning: the rx minisim does not implement "
745 "signal delivery yet.\n" "Resuming with no signal.\n");
748 execution_error_clear_last_error ();
752 rc = setjmp (decode_jmp_buf);
754 rc = decode_opcode ();
759 /* We don't clear 'stop' here, because then we would miss
760 interrupts that arrived on the way here. Instead, we clear
761 the flag in sim_stop_reason, after GDB has disabled the
762 interrupt signal handler. */
768 reason = sim_stopped;
769 siggnal = TARGET_SIGNAL_INT;
773 rc = setjmp (decode_jmp_buf);
775 rc = decode_opcode ();
777 if (execution_error_get_last_error () != SIM_ERR_NONE)
779 reason = sim_stopped;
780 siggnal = TARGET_SIGNAL_SEGV;
784 if (!RX_STEPPED (rc))
794 sim_stop (SIM_DESC sd)
802 sim_stop_reason (SIM_DESC sd, enum sim_stop *reason_p, int *sigrc_p)
811 sim_do_command (SIM_DESC sd, char *cmd)
817 /* Skip leading whitespace. */
821 /* Find the extent of the command word. */
822 for (p = cmd; *p; p++)
826 /* Null-terminate the command word, and record the start of any
827 further arguments. */
833 while (isspace (*args))
839 if (strcmp (cmd, "trace") == 0)
841 if (strcmp (args, "on") == 0)
843 else if (strcmp (args, "off") == 0)
846 printf ("The 'sim trace' command expects 'on' or 'off' "
847 "as an argument.\n");
849 else if (strcmp (cmd, "verbose") == 0)
851 if (strcmp (args, "on") == 0)
853 else if (strcmp (args, "noisy") == 0)
855 else if (strcmp (args, "off") == 0)
858 printf ("The 'sim verbose' command expects 'on', 'noisy', or 'off'"
859 " as an argument.\n");
862 printf ("The 'sim' command expects either 'trace' or 'verbose'"
863 " as a subcommand.\n");