1 /* Target-dependent code for GNU/Linux on RISC-V processors.
2 Copyright (C) 2018-2024 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "riscv-tdep.h"
21 #include "glibc-tdep.h"
22 #include "linux-tdep.h"
23 #include "solib-svr4.h"
25 #include "tramp-frame.h"
26 #include "trad-frame.h"
29 /* The following value is derived from __NR_rt_sigreturn in
30 <include/uapi/asm-generic/unistd.h> from the Linux source tree. */
32 #define RISCV_NR_rt_sigreturn 139
34 /* Define the general register mapping. The kernel puts the PC at offset 0,
35 gdb puts it at offset 32. Register x0 is always 0 and can be ignored.
36 Registers x1 to x31 are in the same place. */
38 static const struct regcache_map_entry riscv_linux_gregmap
[] =
40 { 1, RISCV_PC_REGNUM
, 0 },
41 { 31, RISCV_RA_REGNUM
, 0 }, /* x1 to x31 */
45 /* Define the FP register mapping. The kernel puts the 32 FP regs first, and
48 static const struct regcache_map_entry riscv_linux_fregmap
[] =
50 { 32, RISCV_FIRST_FP_REGNUM
, 0 },
51 { 1, RISCV_CSR_FCSR_REGNUM
, 0 },
55 /* Define the general register regset. */
57 static const struct regset riscv_linux_gregset
=
59 riscv_linux_gregmap
, riscv_supply_regset
, regcache_collect_regset
62 /* Define the FP register regset. */
64 static const struct regset riscv_linux_fregset
=
66 riscv_linux_fregmap
, riscv_supply_regset
, regcache_collect_regset
69 /* Define hook for core file support. */
72 riscv_linux_iterate_over_regset_sections (struct gdbarch
*gdbarch
,
73 iterate_over_regset_sections_cb
*cb
,
75 const struct regcache
*regcache
)
77 cb (".reg", (32 * riscv_isa_xlen (gdbarch
)), (32 * riscv_isa_xlen (gdbarch
)),
78 &riscv_linux_gregset
, NULL
, cb_data
);
79 /* The kernel is adding 8 bytes for FCSR. */
80 cb (".reg2", (32 * riscv_isa_flen (gdbarch
)) + 8,
81 (32 * riscv_isa_flen (gdbarch
)) + 8,
82 &riscv_linux_fregset
, NULL
, cb_data
);
85 /* Signal trampoline support. */
87 static void riscv_linux_sigframe_init (const struct tramp_frame
*self
,
88 const frame_info_ptr
&this_frame
,
89 struct trad_frame_cache
*this_cache
,
92 #define RISCV_INST_LI_A7_SIGRETURN 0x08b00893
93 #define RISCV_INST_ECALL 0x00000073
95 static const struct tramp_frame riscv_linux_sigframe
= {
99 { RISCV_INST_LI_A7_SIGRETURN
, ULONGEST_MAX
},
100 { RISCV_INST_ECALL
, ULONGEST_MAX
},
101 { TRAMP_SENTINEL_INSN
}
103 riscv_linux_sigframe_init
,
107 /* Runtime signal frames look like this:
114 unsigned long __uc_flags;
115 struct ucontext *uclink;
118 char __glibc_reserved[1024 / 8 - sizeof (sigset_t)];
119 mcontext_t uc_mcontext;
122 #define SIGFRAME_SIGINFO_SIZE 128
123 #define UCONTEXT_MCONTEXT_OFFSET 176
126 riscv_linux_sigframe_init (const struct tramp_frame
*self
,
127 const frame_info_ptr
&this_frame
,
128 struct trad_frame_cache
*this_cache
,
131 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
132 int xlen
= riscv_isa_xlen (gdbarch
);
133 int flen
= riscv_isa_flen (gdbarch
);
134 CORE_ADDR frame_sp
= get_frame_sp (this_frame
);
135 CORE_ADDR mcontext_base
;
138 mcontext_base
= frame_sp
+ SIGFRAME_SIGINFO_SIZE
+ UCONTEXT_MCONTEXT_OFFSET
;
140 /* Handle the integer registers. The first one is PC, followed by x1
142 regs_base
= mcontext_base
;
143 trad_frame_set_reg_addr (this_cache
, RISCV_PC_REGNUM
, regs_base
);
144 for (int i
= 1; i
< 32; i
++)
145 trad_frame_set_reg_addr (this_cache
, RISCV_ZERO_REGNUM
+ i
,
146 regs_base
+ (i
* xlen
));
148 /* Handle the FP registers. First comes the 32 FP registers, followed by
150 regs_base
+= 32 * xlen
;
151 for (int i
= 0; i
< 32; i
++)
152 trad_frame_set_reg_addr (this_cache
, RISCV_FIRST_FP_REGNUM
+ i
,
153 regs_base
+ (i
* flen
));
154 regs_base
+= 32 * flen
;
155 trad_frame_set_reg_addr (this_cache
, RISCV_CSR_FCSR_REGNUM
, regs_base
);
157 /* Choice of the bottom of the sigframe is somewhat arbitrary. */
158 trad_frame_set_id (this_cache
, frame_id_build (frame_sp
, func
));
161 /* When FRAME is at a syscall instruction (ECALL), return the PC of the next
162 instruction to be executed. */
165 riscv_linux_syscall_next_pc (const frame_info_ptr
&frame
)
167 const CORE_ADDR pc
= get_frame_pc (frame
);
168 const ULONGEST a7
= get_frame_register_unsigned (frame
, RISCV_A7_REGNUM
);
170 if (a7
== RISCV_NR_rt_sigreturn
)
171 return frame_unwind_caller_pc (frame
);
173 return pc
+ 4 /* Length of the ECALL insn. */;
176 /* Initialize RISC-V Linux ABI info. */
179 riscv_linux_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
181 riscv_gdbarch_tdep
*tdep
= gdbarch_tdep
<riscv_gdbarch_tdep
> (gdbarch
);
183 linux_init_abi (info
, gdbarch
, 0);
185 set_gdbarch_software_single_step (gdbarch
, riscv_software_single_step
);
187 set_solib_svr4_fetch_link_map_offsets (gdbarch
,
188 (riscv_isa_xlen (gdbarch
) == 4
189 ? linux_ilp32_fetch_link_map_offsets
190 : linux_lp64_fetch_link_map_offsets
));
192 /* GNU/Linux uses SVR4-style shared libraries. */
193 set_gdbarch_skip_trampoline_code (gdbarch
, find_solib_trampoline_target
);
195 /* GNU/Linux uses the dynamic linker included in the GNU C Library. */
196 set_gdbarch_skip_solib_resolver (gdbarch
, glibc_skip_solib_resolver
);
198 /* Enable TLS support. */
199 set_gdbarch_fetch_tls_load_module_address (gdbarch
,
200 svr4_fetch_objfile_link_map
);
202 set_gdbarch_iterate_over_regset_sections
203 (gdbarch
, riscv_linux_iterate_over_regset_sections
);
205 tramp_frame_prepend_unwinder (gdbarch
, &riscv_linux_sigframe
);
207 tdep
->syscall_next_pc
= riscv_linux_syscall_next_pc
;
210 /* Initialize RISC-V Linux target support. */
212 void _initialize_riscv_linux_tdep ();
214 _initialize_riscv_linux_tdep ()
216 gdbarch_register_osabi (bfd_arch_riscv
, 0, GDB_OSABI_LINUX
,
217 riscv_linux_init_abi
);