1 /* gdb-if.c -- sim interface to GDB.
3 Copyright (C) 2008-2024 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/>. */
21 /* This must come before any other includes. */
33 #include "libiberty.h"
34 #include "sim/callback.h"
36 #include "gdb/signals.h"
37 #include "sim/sim-rx.h"
46 /* Ideally, we'd wrap up all the minisim's data structures in an
47 object and pass that around. However, neither GDB nor run needs
50 So we just have one instance, that lives in global variables, and
51 each time we open it, we re-initialize it. */
57 static struct sim_state the_minisim
= {
58 "This is the sole rx minisim instance. See libsim.a's global variables."
61 static int rx_sim_is_open
;
64 sim_open (SIM_OPEN_KIND kind
,
65 struct host_callback_struct
*callback
,
66 struct bfd
*abfd
, char * const *argv
)
69 fprintf (stderr
, "rx minisim: re-opened sim\n");
71 /* The 'run' interface doesn't use this function, so we don't care
72 about KIND; it's always SIM_OPEN_DEBUG. */
73 if (kind
!= SIM_OPEN_DEBUG
)
74 fprintf (stderr
, "rx minisim: sim_open KIND != SIM_OPEN_DEBUG: %d\n",
77 set_callbacks (callback
);
79 /* We don't expect any command-line arguments. */
83 execution_error_init_debugger ();
85 sim_disasm_init (abfd
);
91 check_desc (SIM_DESC sd
)
93 if (sd
!= &the_minisim
)
94 fprintf (stderr
, "rx minisim: desc != &the_minisim\n");
98 sim_close (SIM_DESC sd
, int quitting
)
102 /* Not much to do. At least free up our memory. */
109 open_objfile (const char *filename
)
111 bfd
*prog
= bfd_openr (filename
, 0);
115 fprintf (stderr
, "Can't read %s\n", filename
);
119 if (!bfd_check_format (prog
, bfd_object
))
121 fprintf (stderr
, "%s not a rx program\n", filename
);
128 static struct swap_list
131 struct swap_list
*next
;
135 free_swap_list (void)
139 struct swap_list
*next
= swap_list
->next
;
145 /* When running in big endian mode, we must do an additional
146 byte swap of memory areas used to hold instructions. See
147 the comment preceding rx_load in load.c to see why this is
150 Construct a list of memory areas that must be byte swapped.
151 This list will be consulted when either reading or writing
155 build_swap_list (struct bfd
*abfd
)
160 /* Nothing to do when in little endian mode. */
164 for (s
= abfd
->sections
; s
; s
= s
->next
)
166 if ((s
->flags
& SEC_LOAD
) && (s
->flags
& SEC_CODE
))
168 struct swap_list
*sl
;
171 size
= bfd_section_size (s
);
175 sl
= malloc (sizeof (struct swap_list
));
177 sl
->next
= swap_list
;
178 sl
->start
= bfd_section_lma (s
);
179 sl
->end
= sl
->start
+ size
;
186 addr_in_swap_list (bfd_vma addr
)
190 for (s
= swap_list
; s
; s
= s
->next
)
192 if (s
->start
<= addr
&& addr
< s
->end
)
199 sim_load (SIM_DESC sd
, const char *prog
, struct bfd
*abfd
, int from_tty
)
204 abfd
= open_objfile (prog
);
208 rx_load (abfd
, get_callbacks ());
209 build_swap_list (abfd
);
215 sim_create_inferior (SIM_DESC sd
, struct bfd
*abfd
,
216 char * const *argv
, char * const *env
)
222 rx_load (abfd
, NULL
);
223 build_swap_list (abfd
);
230 sim_read (SIM_DESC sd
, uint64_t addr
, void *buffer
, uint64_t length
)
233 unsigned char *data
= buffer
;
240 execution_error_clear_last_error ();
242 for (i
= 0; i
< length
; i
++)
244 bfd_vma vma
= addr
+ i
;
245 int do_swap
= addr_in_swap_list (vma
);
246 data
[i
] = mem_get_qi (vma
^ (do_swap
? 3 : 0));
248 if (execution_error_get_last_error () != SIM_ERR_NONE
)
256 sim_write (SIM_DESC sd
, uint64_t addr
, const void *buffer
, uint64_t length
)
259 const unsigned char *data
= buffer
;
263 execution_error_clear_last_error ();
265 for (i
= 0; i
< length
; i
++)
267 bfd_vma vma
= addr
+ i
;
268 int do_swap
= addr_in_swap_list (vma
);
269 mem_put_qi (vma
^ (do_swap
? 3 : 0), data
[i
]);
271 if (execution_error_get_last_error () != SIM_ERR_NONE
)
278 /* Read the LENGTH bytes at BUF as an little-endian value. */
280 get_le (const unsigned char *buf
, int length
)
283 while (--length
>= 0)
284 acc
= (acc
<< 8) + buf
[length
];
289 /* Read the LENGTH bytes at BUF as a big-endian value. */
291 get_be (const unsigned char *buf
, int length
)
295 acc
= (acc
<< 8) + *buf
++;
300 /* Store VAL as a little-endian value in the LENGTH bytes at BUF. */
302 put_le (unsigned char *buf
, int length
, DI val
)
306 for (i
= 0; i
< length
; i
++)
313 /* Store VAL as a big-endian value in the LENGTH bytes at BUF. */
315 put_be (unsigned char *buf
, int length
, DI val
)
319 for (i
= length
-1; i
>= 0; i
--)
328 check_regno (enum sim_rx_regnum regno
)
330 return 0 <= regno
&& regno
< sim_rx_num_regs
;
334 reg_size (enum sim_rx_regnum regno
)
340 case sim_rx_r0_regnum
:
341 size
= sizeof (regs
.r
[0]);
343 case sim_rx_r1_regnum
:
344 size
= sizeof (regs
.r
[1]);
346 case sim_rx_r2_regnum
:
347 size
= sizeof (regs
.r
[2]);
349 case sim_rx_r3_regnum
:
350 size
= sizeof (regs
.r
[3]);
352 case sim_rx_r4_regnum
:
353 size
= sizeof (regs
.r
[4]);
355 case sim_rx_r5_regnum
:
356 size
= sizeof (regs
.r
[5]);
358 case sim_rx_r6_regnum
:
359 size
= sizeof (regs
.r
[6]);
361 case sim_rx_r7_regnum
:
362 size
= sizeof (regs
.r
[7]);
364 case sim_rx_r8_regnum
:
365 size
= sizeof (regs
.r
[8]);
367 case sim_rx_r9_regnum
:
368 size
= sizeof (regs
.r
[9]);
370 case sim_rx_r10_regnum
:
371 size
= sizeof (regs
.r
[10]);
373 case sim_rx_r11_regnum
:
374 size
= sizeof (regs
.r
[11]);
376 case sim_rx_r12_regnum
:
377 size
= sizeof (regs
.r
[12]);
379 case sim_rx_r13_regnum
:
380 size
= sizeof (regs
.r
[13]);
382 case sim_rx_r14_regnum
:
383 size
= sizeof (regs
.r
[14]);
385 case sim_rx_r15_regnum
:
386 size
= sizeof (regs
.r
[15]);
388 case sim_rx_isp_regnum
:
389 size
= sizeof (regs
.r_isp
);
391 case sim_rx_usp_regnum
:
392 size
= sizeof (regs
.r_usp
);
394 case sim_rx_intb_regnum
:
395 size
= sizeof (regs
.r_intb
);
397 case sim_rx_pc_regnum
:
398 size
= sizeof (regs
.r_pc
);
400 case sim_rx_ps_regnum
:
401 size
= sizeof (regs
.r_psw
);
403 case sim_rx_bpc_regnum
:
404 size
= sizeof (regs
.r_bpc
);
406 case sim_rx_bpsw_regnum
:
407 size
= sizeof (regs
.r_bpsw
);
409 case sim_rx_fintv_regnum
:
410 size
= sizeof (regs
.r_fintv
);
412 case sim_rx_fpsw_regnum
:
413 size
= sizeof (regs
.r_fpsw
);
415 case sim_rx_acc_regnum
:
416 size
= sizeof (regs
.r_acc
);
426 sim_fetch_register (SIM_DESC sd
, int regno
, void *buf
, int length
)
433 if (!check_regno (regno
))
436 size
= reg_size (regno
);
443 case sim_rx_r0_regnum
:
446 case sim_rx_r1_regnum
:
449 case sim_rx_r2_regnum
:
452 case sim_rx_r3_regnum
:
455 case sim_rx_r4_regnum
:
458 case sim_rx_r5_regnum
:
461 case sim_rx_r6_regnum
:
464 case sim_rx_r7_regnum
:
467 case sim_rx_r8_regnum
:
470 case sim_rx_r9_regnum
:
473 case sim_rx_r10_regnum
:
476 case sim_rx_r11_regnum
:
479 case sim_rx_r12_regnum
:
482 case sim_rx_r13_regnum
:
485 case sim_rx_r14_regnum
:
488 case sim_rx_r15_regnum
:
491 case sim_rx_isp_regnum
:
494 case sim_rx_usp_regnum
:
497 case sim_rx_intb_regnum
:
498 val
= get_reg (intb
);
500 case sim_rx_pc_regnum
:
503 case sim_rx_ps_regnum
:
506 case sim_rx_bpc_regnum
:
509 case sim_rx_bpsw_regnum
:
510 val
= get_reg (bpsw
);
512 case sim_rx_fintv_regnum
:
513 val
= get_reg (fintv
);
515 case sim_rx_fpsw_regnum
:
516 val
= get_reg (fpsw
);
518 case sim_rx_acc_regnum
:
519 val
= ((DI
) get_reg (acchi
) << 32) | get_reg (acclo
);
522 fprintf (stderr
, "rx minisim: unrecognized register number: %d\n",
528 put_be (buf
, length
, val
);
530 put_le (buf
, length
, val
);
536 sim_store_register (SIM_DESC sd
, int regno
, const void *buf
, int length
)
543 if (!check_regno (regno
))
546 size
= reg_size (regno
);
552 val
= get_be (buf
, length
);
554 val
= get_le (buf
, length
);
558 case sim_rx_r0_regnum
:
561 case sim_rx_r1_regnum
:
564 case sim_rx_r2_regnum
:
567 case sim_rx_r3_regnum
:
570 case sim_rx_r4_regnum
:
573 case sim_rx_r5_regnum
:
576 case sim_rx_r6_regnum
:
579 case sim_rx_r7_regnum
:
582 case sim_rx_r8_regnum
:
585 case sim_rx_r9_regnum
:
588 case sim_rx_r10_regnum
:
591 case sim_rx_r11_regnum
:
594 case sim_rx_r12_regnum
:
597 case sim_rx_r13_regnum
:
600 case sim_rx_r14_regnum
:
603 case sim_rx_r15_regnum
:
606 case sim_rx_isp_regnum
:
609 case sim_rx_usp_regnum
:
612 case sim_rx_intb_regnum
:
615 case sim_rx_pc_regnum
:
618 case sim_rx_ps_regnum
:
621 case sim_rx_bpc_regnum
:
624 case sim_rx_bpsw_regnum
:
627 case sim_rx_fintv_regnum
:
628 put_reg (fintv
, val
);
630 case sim_rx_fpsw_regnum
:
633 case sim_rx_acc_regnum
:
634 put_reg (acclo
, val
& 0xffffffff);
635 put_reg (acchi
, (val
>> 32) & 0xffffffff);
638 fprintf (stderr
, "rx minisim: unrecognized register number: %d\n",
647 sim_info (SIM_DESC sd
, bool verbose
)
651 printf ("The rx minisim doesn't collect any statistics.\n");
654 static volatile int stop
;
655 static enum sim_stop reason
;
659 /* Given a signal number used by the RX bsp (that is, newlib),
660 return a target signal number used by GDB. */
662 rx_signal_to_gdb_signal (int rx
)
667 return GDB_SIGNAL_ILL
;
670 return GDB_SIGNAL_TRAP
;
673 return GDB_SIGNAL_BUS
;
676 return GDB_SIGNAL_SEGV
;
679 return GDB_SIGNAL_XCPU
;
682 return GDB_SIGNAL_INT
;
685 return GDB_SIGNAL_FPE
;
688 return GDB_SIGNAL_ABRT
;
695 /* Take a step return code RC and set up the variables consulted by
696 sim_stop_reason appropriately. */
700 if (execution_error_get_last_error () != SIM_ERR_NONE
)
702 reason
= sim_stopped
;
703 siggnal
= GDB_SIGNAL_SEGV
;
705 if (RX_STEPPED (rc
) || RX_HIT_BREAK (rc
))
707 reason
= sim_stopped
;
708 siggnal
= GDB_SIGNAL_TRAP
;
710 else if (RX_STOPPED (rc
))
712 reason
= sim_stopped
;
713 siggnal
= rx_signal_to_gdb_signal (RX_STOP_SIG (rc
));
717 assert (RX_EXITED (rc
));
719 siggnal
= RX_EXIT_STATUS (rc
);
725 sim_resume (SIM_DESC sd
, int step
, int sig_to_deliver
)
731 if (sig_to_deliver
!= 0)
734 "Warning: the rx minisim does not implement "
735 "signal delivery yet.\n" "Resuming with no signal.\n");
738 execution_error_clear_last_error ();
742 rc
= setjmp (decode_jmp_buf
);
744 rc
= decode_opcode ();
749 /* We don't clear 'stop' here, because then we would miss
750 interrupts that arrived on the way here. Instead, we clear
751 the flag in sim_stop_reason, after GDB has disabled the
752 interrupt signal handler. */
758 reason
= sim_stopped
;
759 siggnal
= GDB_SIGNAL_INT
;
763 rc
= setjmp (decode_jmp_buf
);
765 rc
= decode_opcode ();
767 if (execution_error_get_last_error () != SIM_ERR_NONE
)
769 reason
= sim_stopped
;
770 siggnal
= GDB_SIGNAL_SEGV
;
774 if (!RX_STEPPED (rc
))
784 sim_stop (SIM_DESC sd
)
792 sim_stop_reason (SIM_DESC sd
, enum sim_stop
*reason_p
, int *sigrc_p
)
801 sim_do_command (SIM_DESC sd
, const char *cmd
)
804 char **argv
= buildargv (cmd
);
817 if (strcmp (cmd
, "trace") == 0)
819 if (strcmp (arg
, "on") == 0)
821 else if (strcmp (arg
, "off") == 0)
824 printf ("The 'sim trace' command expects 'on' or 'off' "
825 "as an argument.\n");
827 else if (strcmp (cmd
, "verbose") == 0)
829 if (strcmp (arg
, "on") == 0)
831 else if (strcmp (arg
, "noisy") == 0)
833 else if (strcmp (arg
, "off") == 0)
836 printf ("The 'sim verbose' command expects 'on', 'noisy', or 'off'"
837 " as an argument.\n");
840 printf ("The 'sim' command expects either 'trace' or 'verbose'"
841 " as a subcommand.\n");
847 sim_complete_command (SIM_DESC sd
, const char *text
, const char *word
)
852 /* Stub this out for now. */
855 sim_memory_map (SIM_DESC sd
)