1 /* GNU/Linux/RISC-V specific low level interface, for the remote server
3 Copyright (C) 2020 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "linux-low.h"
24 #include "elf/common.h"
25 #include "nat/riscv-linux-tdesc.h"
26 #include "opcode/riscv.h"
28 /* Work around glibc header breakage causing ELF_NFPREG not to be usable. */
33 /* Linux target op definitions for the RISC-V architecture. */
35 class riscv_target
: public linux_process_target
41 void low_arch_setup () override
;
44 /* The singleton target ops object. */
46 static riscv_target the_riscv_target
;
48 /* Implementation of linux target ops method "low_arch_setup". */
51 riscv_target::low_arch_setup ()
53 static const char *expedite_regs
[] = { "sp", "pc", NULL
};
55 const riscv_gdbarch_features features
56 = riscv_linux_read_features (lwpid_of (current_thread
));
57 target_desc
*tdesc
= riscv_create_target_description (features
);
59 if (!tdesc
->expedite_regs
)
60 init_target_desc (tdesc
, expedite_regs
);
61 current_process ()->tdesc
= tdesc
;
64 /* Collect GPRs from REGCACHE into BUF. */
67 riscv_fill_gregset (struct regcache
*regcache
, void *buf
)
69 const struct target_desc
*tdesc
= regcache
->tdesc
;
70 elf_gregset_t
*regset
= (elf_gregset_t
*) buf
;
71 int regno
= find_regno (tdesc
, "zero");
74 collect_register_by_name (regcache
, "pc", *regset
);
75 for (i
= 1; i
< ARRAY_SIZE (*regset
); i
++)
76 collect_register (regcache
, regno
+ i
, *regset
+ i
);
79 /* Supply GPRs from BUF into REGCACHE. */
82 riscv_store_gregset (struct regcache
*regcache
, const void *buf
)
84 const elf_gregset_t
*regset
= (const elf_gregset_t
*) buf
;
85 const struct target_desc
*tdesc
= regcache
->tdesc
;
86 int regno
= find_regno (tdesc
, "zero");
89 supply_register_by_name (regcache
, "pc", *regset
);
90 supply_register_zeroed (regcache
, regno
);
91 for (i
= 1; i
< ARRAY_SIZE (*regset
); i
++)
92 supply_register (regcache
, regno
+ i
, *regset
+ i
);
95 /* Collect FPRs from REGCACHE into BUF. */
98 riscv_fill_fpregset (struct regcache
*regcache
, void *buf
)
100 const struct target_desc
*tdesc
= regcache
->tdesc
;
101 int regno
= find_regno (tdesc
, "ft0");
102 int flen
= register_size (regcache
->tdesc
, regno
);
103 gdb_byte
*regbuf
= (gdb_byte
*) buf
;
106 for (i
= 0; i
< ELF_NFPREG
- 1; i
++, regbuf
+= flen
)
107 collect_register (regcache
, regno
+ i
, regbuf
);
108 collect_register_by_name (regcache
, "fcsr", regbuf
);
111 /* Supply FPRs from BUF into REGCACHE. */
114 riscv_store_fpregset (struct regcache
*regcache
, const void *buf
)
116 const struct target_desc
*tdesc
= regcache
->tdesc
;
117 int regno
= find_regno (tdesc
, "ft0");
118 int flen
= register_size (regcache
->tdesc
, regno
);
119 const gdb_byte
*regbuf
= (const gdb_byte
*) buf
;
122 for (i
= 0; i
< ELF_NFPREG
- 1; i
++, regbuf
+= flen
)
123 supply_register (regcache
, regno
+ i
, regbuf
);
124 supply_register_by_name (regcache
, "fcsr", regbuf
);
127 /* RISC-V/Linux regsets. FPRs are optional and come in different sizes,
128 so define multiple regsets for them marking them all as OPTIONAL_REGS
129 rather than FP_REGS, so that "regsets_fetch_inferior_registers" picks
130 the right one according to size. */
131 static struct regset_info riscv_regsets
[] = {
132 { PTRACE_GETREGSET
, PTRACE_SETREGSET
, NT_PRSTATUS
,
133 sizeof (elf_gregset_t
), GENERAL_REGS
,
134 riscv_fill_gregset
, riscv_store_gregset
},
135 { PTRACE_GETREGSET
, PTRACE_SETREGSET
, NT_FPREGSET
,
136 sizeof (struct __riscv_mc_q_ext_state
), OPTIONAL_REGS
,
137 riscv_fill_fpregset
, riscv_store_fpregset
},
138 { PTRACE_GETREGSET
, PTRACE_SETREGSET
, NT_FPREGSET
,
139 sizeof (struct __riscv_mc_d_ext_state
), OPTIONAL_REGS
,
140 riscv_fill_fpregset
, riscv_store_fpregset
},
141 { PTRACE_GETREGSET
, PTRACE_SETREGSET
, NT_FPREGSET
,
142 sizeof (struct __riscv_mc_f_ext_state
), OPTIONAL_REGS
,
143 riscv_fill_fpregset
, riscv_store_fpregset
},
147 /* RISC-V/Linux regset information. */
148 static struct regsets_info riscv_regsets_info
=
150 riscv_regsets
, /* regsets */
152 NULL
, /* disabled_regsets */
155 /* Definition of linux_target_ops data member "regs_info". */
156 static struct regs_info riscv_regs
=
158 NULL
, /* regset_bitmap */
163 /* Implementation of linux_target_ops method "regs_info". */
165 static const struct regs_info
*
171 /* Implementation of linux_target_ops method "fetch_register". */
174 riscv_fetch_register (struct regcache
*regcache
, int regno
)
176 const struct target_desc
*tdesc
= regcache
->tdesc
;
178 if (regno
!= find_regno (tdesc
, "zero"))
180 supply_register_zeroed (regcache
, regno
);
184 /* Implementation of linux_target_ops method "get_pc". */
187 riscv_get_pc (struct regcache
*regcache
)
189 elf_gregset_t regset
;
191 if (sizeof (regset
[0]) == 8)
192 return linux_get_pc_64bit (regcache
);
194 return linux_get_pc_32bit (regcache
);
197 /* Implementation of linux_target_ops method "set_pc". */
200 riscv_set_pc (struct regcache
*regcache
, CORE_ADDR newpc
)
202 elf_gregset_t regset
;
204 if (sizeof (regset
[0]) == 8)
205 linux_set_pc_64bit (regcache
, newpc
);
207 linux_set_pc_32bit (regcache
, newpc
);
210 /* Correct in either endianness. */
211 static const uint16_t riscv_ibreakpoint
[] = { 0x0073, 0x0010 };
212 static const uint16_t riscv_cbreakpoint
= 0x9002;
214 /* Implementation of linux_target_ops method "breakpoint_kind_from_pc". */
217 riscv_breakpoint_kind_from_pc (CORE_ADDR
*pcptr
)
226 if (target_read_memory (*pcptr
, buf
.bytes
, sizeof (buf
.insn
)) == 0
227 && riscv_insn_length (buf
.insn
== sizeof (riscv_ibreakpoint
)))
228 return sizeof (riscv_ibreakpoint
);
230 return sizeof (riscv_cbreakpoint
);
233 /* Implementation of linux_target_ops method "sw_breakpoint_from_kind". */
235 static const gdb_byte
*
236 riscv_sw_breakpoint_from_kind (int kind
, int *size
)
241 case sizeof (riscv_ibreakpoint
):
242 return (const gdb_byte
*) &riscv_ibreakpoint
;
244 return (const gdb_byte
*) &riscv_cbreakpoint
;
248 /* Implementation of linux_target_ops method "breakpoint_at". */
251 riscv_breakpoint_at (CORE_ADDR pc
)
260 if (target_read_memory (pc
, buf
.bytes
, sizeof (buf
.insn
)) == 0
261 && (buf
.insn
== riscv_cbreakpoint
262 || (buf
.insn
== riscv_ibreakpoint
[0]
263 && target_read_memory (pc
+ sizeof (buf
.insn
), buf
.bytes
,
264 sizeof (buf
.insn
)) == 0
265 && buf
.insn
== riscv_ibreakpoint
[1])))
271 /* RISC-V/Linux target operations. */
272 struct linux_target_ops the_low_target
=
275 NULL
, /* cannot_fetch_register */
276 NULL
, /* cannot_store_register */
277 riscv_fetch_register
,
280 riscv_breakpoint_kind_from_pc
,
281 riscv_sw_breakpoint_from_kind
,
282 NULL
, /* get_next_pcs */
283 0, /* decr_pc_after_break */
287 /* The linux target ops object. */
289 linux_process_target
*the_linux_target
= &the_riscv_target
;
291 /* Initialize the RISC-V/Linux target. */
294 initialize_low_arch ()
296 initialize_regsets_info (&riscv_regsets_info
);