Commit | Line | Data |
---|---|---|
bf84f706 MR |
1 | /* GNU/Linux/RISC-V specific low level interface, for the remote server |
2 | for GDB. | |
3 | Copyright (C) 2020 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of GDB. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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/>. */ | |
19 | ||
20 | #include "server.h" | |
21 | ||
22 | #include "linux-low.h" | |
23 | #include "tdesc.h" | |
24 | #include "elf/common.h" | |
25 | #include "nat/riscv-linux-tdesc.h" | |
26 | #include "opcode/riscv.h" | |
27 | ||
28 | /* Work around glibc header breakage causing ELF_NFPREG not to be usable. */ | |
29 | #ifndef NFPREG | |
30 | # define NFPREG 33 | |
31 | #endif | |
32 | ||
ef0478f6 TBA |
33 | /* Linux target op definitions for the RISC-V architecture. */ |
34 | ||
35 | class riscv_target : public linux_process_target | |
36 | { | |
37 | public: | |
38 | ||
797bcff5 TBA |
39 | protected: |
40 | ||
41 | void low_arch_setup () override; | |
ef0478f6 TBA |
42 | }; |
43 | ||
44 | /* The singleton target ops object. */ | |
45 | ||
46 | static riscv_target the_riscv_target; | |
47 | ||
797bcff5 | 48 | /* Implementation of linux target ops method "low_arch_setup". */ |
bf84f706 | 49 | |
797bcff5 TBA |
50 | void |
51 | riscv_target::low_arch_setup () | |
bf84f706 MR |
52 | { |
53 | static const char *expedite_regs[] = { "sp", "pc", NULL }; | |
54 | ||
55 | const riscv_gdbarch_features features | |
56 | = riscv_linux_read_features (lwpid_of (current_thread)); | |
57 | target_desc *tdesc = riscv_create_target_description (features); | |
58 | ||
59 | if (!tdesc->expedite_regs) | |
60 | init_target_desc (tdesc, expedite_regs); | |
61 | current_process ()->tdesc = tdesc; | |
62 | } | |
63 | ||
64 | /* Collect GPRs from REGCACHE into BUF. */ | |
65 | ||
66 | static void | |
67 | riscv_fill_gregset (struct regcache *regcache, void *buf) | |
68 | { | |
69 | const struct target_desc *tdesc = regcache->tdesc; | |
70 | elf_gregset_t *regset = (elf_gregset_t *) buf; | |
71 | int regno = find_regno (tdesc, "zero"); | |
72 | int i; | |
73 | ||
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); | |
77 | } | |
78 | ||
79 | /* Supply GPRs from BUF into REGCACHE. */ | |
80 | ||
81 | static void | |
82 | riscv_store_gregset (struct regcache *regcache, const void *buf) | |
83 | { | |
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"); | |
87 | int i; | |
88 | ||
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); | |
93 | } | |
94 | ||
95 | /* Collect FPRs from REGCACHE into BUF. */ | |
96 | ||
97 | static void | |
98 | riscv_fill_fpregset (struct regcache *regcache, void *buf) | |
99 | { | |
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; | |
104 | int i; | |
105 | ||
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); | |
109 | } | |
110 | ||
111 | /* Supply FPRs from BUF into REGCACHE. */ | |
112 | ||
113 | static void | |
114 | riscv_store_fpregset (struct regcache *regcache, const void *buf) | |
115 | { | |
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; | |
120 | int i; | |
121 | ||
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); | |
125 | } | |
126 | ||
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 }, | |
144 | NULL_REGSET | |
145 | }; | |
146 | ||
147 | /* RISC-V/Linux regset information. */ | |
148 | static struct regsets_info riscv_regsets_info = | |
149 | { | |
150 | riscv_regsets, /* regsets */ | |
151 | 0, /* num_regsets */ | |
152 | NULL, /* disabled_regsets */ | |
153 | }; | |
154 | ||
155 | /* Definition of linux_target_ops data member "regs_info". */ | |
156 | static struct regs_info riscv_regs = | |
157 | { | |
158 | NULL, /* regset_bitmap */ | |
159 | NULL, /* usrregs */ | |
160 | &riscv_regsets_info, | |
161 | }; | |
162 | ||
163 | /* Implementation of linux_target_ops method "regs_info". */ | |
164 | ||
165 | static const struct regs_info * | |
166 | riscv_regs_info () | |
167 | { | |
168 | return &riscv_regs; | |
169 | } | |
170 | ||
171 | /* Implementation of linux_target_ops method "fetch_register". */ | |
172 | ||
173 | static int | |
174 | riscv_fetch_register (struct regcache *regcache, int regno) | |
175 | { | |
176 | const struct target_desc *tdesc = regcache->tdesc; | |
177 | ||
178 | if (regno != find_regno (tdesc, "zero")) | |
179 | return 0; | |
180 | supply_register_zeroed (regcache, regno); | |
181 | return 1; | |
182 | } | |
183 | ||
184 | /* Implementation of linux_target_ops method "get_pc". */ | |
185 | ||
186 | static CORE_ADDR | |
187 | riscv_get_pc (struct regcache *regcache) | |
188 | { | |
189 | elf_gregset_t regset; | |
190 | ||
191 | if (sizeof (regset[0]) == 8) | |
192 | return linux_get_pc_64bit (regcache); | |
193 | else | |
194 | return linux_get_pc_32bit (regcache); | |
195 | } | |
196 | ||
197 | /* Implementation of linux_target_ops method "set_pc". */ | |
198 | ||
199 | static void | |
200 | riscv_set_pc (struct regcache *regcache, CORE_ADDR newpc) | |
201 | { | |
202 | elf_gregset_t regset; | |
203 | ||
204 | if (sizeof (regset[0]) == 8) | |
205 | linux_set_pc_64bit (regcache, newpc); | |
206 | else | |
207 | linux_set_pc_32bit (regcache, newpc); | |
208 | } | |
209 | ||
210 | /* Correct in either endianness. */ | |
211 | static const uint16_t riscv_ibreakpoint[] = { 0x0073, 0x0010 }; | |
212 | static const uint16_t riscv_cbreakpoint = 0x9002; | |
213 | ||
214 | /* Implementation of linux_target_ops method "breakpoint_kind_from_pc". */ | |
215 | ||
216 | static int | |
217 | riscv_breakpoint_kind_from_pc (CORE_ADDR *pcptr) | |
218 | { | |
219 | union | |
220 | { | |
221 | gdb_byte bytes[2]; | |
222 | uint16_t insn; | |
223 | } | |
224 | buf; | |
225 | ||
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); | |
229 | else | |
230 | return sizeof (riscv_cbreakpoint); | |
231 | } | |
232 | ||
233 | /* Implementation of linux_target_ops method "sw_breakpoint_from_kind". */ | |
234 | ||
235 | static const gdb_byte * | |
236 | riscv_sw_breakpoint_from_kind (int kind, int *size) | |
237 | { | |
238 | *size = kind; | |
239 | switch (kind) | |
240 | { | |
241 | case sizeof (riscv_ibreakpoint): | |
242 | return (const gdb_byte *) &riscv_ibreakpoint; | |
243 | default: | |
244 | return (const gdb_byte *) &riscv_cbreakpoint; | |
245 | } | |
246 | } | |
247 | ||
248 | /* Implementation of linux_target_ops method "breakpoint_at". */ | |
249 | ||
250 | static int | |
251 | riscv_breakpoint_at (CORE_ADDR pc) | |
252 | { | |
253 | union | |
254 | { | |
255 | gdb_byte bytes[2]; | |
256 | uint16_t insn; | |
257 | } | |
258 | buf; | |
259 | ||
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]))) | |
266 | return 1; | |
267 | else | |
268 | return 0; | |
269 | } | |
270 | ||
271 | /* RISC-V/Linux target operations. */ | |
272 | struct linux_target_ops the_low_target = | |
273 | { | |
bf84f706 MR |
274 | riscv_regs_info, |
275 | NULL, /* cannot_fetch_register */ | |
276 | NULL, /* cannot_store_register */ | |
277 | riscv_fetch_register, | |
278 | riscv_get_pc, | |
279 | riscv_set_pc, | |
280 | riscv_breakpoint_kind_from_pc, | |
281 | riscv_sw_breakpoint_from_kind, | |
282 | NULL, /* get_next_pcs */ | |
283 | 0, /* decr_pc_after_break */ | |
284 | riscv_breakpoint_at, | |
285 | }; | |
286 | ||
ef0478f6 TBA |
287 | /* The linux target ops object. */ |
288 | ||
289 | linux_process_target *the_linux_target = &the_riscv_target; | |
290 | ||
bf84f706 MR |
291 | /* Initialize the RISC-V/Linux target. */ |
292 | ||
293 | void | |
294 | initialize_low_arch () | |
295 | { | |
296 | initialize_regsets_info (&riscv_regsets_info); | |
297 | } |