gdbserver/linux-low: turn 'get_pc' and 'set_pc' into methods
[deliverable/binutils-gdb.git] / gdbserver / linux-riscv-low.cc
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
33 /* Linux target op definitions for the RISC-V architecture. */
34
35 class riscv_target : public linux_process_target
36 {
37 public:
38
39 const regs_info *get_regs_info () override;
40
41 protected:
42
43 void low_arch_setup () override;
44
45 bool low_cannot_fetch_register (int regno) override;
46
47 bool low_cannot_store_register (int regno) override;
48
49 bool low_fetch_register (regcache *regcache, int regno) override;
50
51 bool low_supports_breakpoints () override;
52
53 CORE_ADDR low_get_pc (regcache *regcache) override;
54
55 void low_set_pc (regcache *regcache, CORE_ADDR newpc) override;
56 };
57
58 /* The singleton target ops object. */
59
60 static riscv_target the_riscv_target;
61
62 bool
63 riscv_target::low_cannot_fetch_register (int regno)
64 {
65 gdb_assert_not_reached ("linux target op low_cannot_fetch_register "
66 "is not implemented by the target");
67 }
68
69 bool
70 riscv_target::low_cannot_store_register (int regno)
71 {
72 gdb_assert_not_reached ("linux target op low_cannot_store_register "
73 "is not implemented by the target");
74 }
75
76 /* Implementation of linux target ops method "low_arch_setup". */
77
78 void
79 riscv_target::low_arch_setup ()
80 {
81 static const char *expedite_regs[] = { "sp", "pc", NULL };
82
83 const riscv_gdbarch_features features
84 = riscv_linux_read_features (lwpid_of (current_thread));
85 target_desc *tdesc = riscv_create_target_description (features);
86
87 if (!tdesc->expedite_regs)
88 init_target_desc (tdesc, expedite_regs);
89 current_process ()->tdesc = tdesc;
90 }
91
92 /* Collect GPRs from REGCACHE into BUF. */
93
94 static void
95 riscv_fill_gregset (struct regcache *regcache, void *buf)
96 {
97 const struct target_desc *tdesc = regcache->tdesc;
98 elf_gregset_t *regset = (elf_gregset_t *) buf;
99 int regno = find_regno (tdesc, "zero");
100 int i;
101
102 collect_register_by_name (regcache, "pc", *regset);
103 for (i = 1; i < ARRAY_SIZE (*regset); i++)
104 collect_register (regcache, regno + i, *regset + i);
105 }
106
107 /* Supply GPRs from BUF into REGCACHE. */
108
109 static void
110 riscv_store_gregset (struct regcache *regcache, const void *buf)
111 {
112 const elf_gregset_t *regset = (const elf_gregset_t *) buf;
113 const struct target_desc *tdesc = regcache->tdesc;
114 int regno = find_regno (tdesc, "zero");
115 int i;
116
117 supply_register_by_name (regcache, "pc", *regset);
118 supply_register_zeroed (regcache, regno);
119 for (i = 1; i < ARRAY_SIZE (*regset); i++)
120 supply_register (regcache, regno + i, *regset + i);
121 }
122
123 /* Collect FPRs from REGCACHE into BUF. */
124
125 static void
126 riscv_fill_fpregset (struct regcache *regcache, void *buf)
127 {
128 const struct target_desc *tdesc = regcache->tdesc;
129 int regno = find_regno (tdesc, "ft0");
130 int flen = register_size (regcache->tdesc, regno);
131 gdb_byte *regbuf = (gdb_byte *) buf;
132 int i;
133
134 for (i = 0; i < ELF_NFPREG - 1; i++, regbuf += flen)
135 collect_register (regcache, regno + i, regbuf);
136 collect_register_by_name (regcache, "fcsr", regbuf);
137 }
138
139 /* Supply FPRs from BUF into REGCACHE. */
140
141 static void
142 riscv_store_fpregset (struct regcache *regcache, const void *buf)
143 {
144 const struct target_desc *tdesc = regcache->tdesc;
145 int regno = find_regno (tdesc, "ft0");
146 int flen = register_size (regcache->tdesc, regno);
147 const gdb_byte *regbuf = (const gdb_byte *) buf;
148 int i;
149
150 for (i = 0; i < ELF_NFPREG - 1; i++, regbuf += flen)
151 supply_register (regcache, regno + i, regbuf);
152 supply_register_by_name (regcache, "fcsr", regbuf);
153 }
154
155 /* RISC-V/Linux regsets. FPRs are optional and come in different sizes,
156 so define multiple regsets for them marking them all as OPTIONAL_REGS
157 rather than FP_REGS, so that "regsets_fetch_inferior_registers" picks
158 the right one according to size. */
159 static struct regset_info riscv_regsets[] = {
160 { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PRSTATUS,
161 sizeof (elf_gregset_t), GENERAL_REGS,
162 riscv_fill_gregset, riscv_store_gregset },
163 { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_FPREGSET,
164 sizeof (struct __riscv_mc_q_ext_state), OPTIONAL_REGS,
165 riscv_fill_fpregset, riscv_store_fpregset },
166 { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_FPREGSET,
167 sizeof (struct __riscv_mc_d_ext_state), OPTIONAL_REGS,
168 riscv_fill_fpregset, riscv_store_fpregset },
169 { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_FPREGSET,
170 sizeof (struct __riscv_mc_f_ext_state), OPTIONAL_REGS,
171 riscv_fill_fpregset, riscv_store_fpregset },
172 NULL_REGSET
173 };
174
175 /* RISC-V/Linux regset information. */
176 static struct regsets_info riscv_regsets_info =
177 {
178 riscv_regsets, /* regsets */
179 0, /* num_regsets */
180 NULL, /* disabled_regsets */
181 };
182
183 /* Definition of linux_target_ops data member "regs_info". */
184 static struct regs_info riscv_regs =
185 {
186 NULL, /* regset_bitmap */
187 NULL, /* usrregs */
188 &riscv_regsets_info,
189 };
190
191 /* Implementation of linux target ops method "get_regs_info". */
192
193 const regs_info *
194 riscv_target::get_regs_info ()
195 {
196 return &riscv_regs;
197 }
198
199 /* Implementation of linux target ops method "low_fetch_register". */
200
201 bool
202 riscv_target::low_fetch_register (regcache *regcache, int regno)
203 {
204 const struct target_desc *tdesc = regcache->tdesc;
205
206 if (regno != find_regno (tdesc, "zero"))
207 return false;
208 supply_register_zeroed (regcache, regno);
209 return true;
210 }
211
212 bool
213 riscv_target::low_supports_breakpoints ()
214 {
215 return true;
216 }
217
218 /* Implementation of linux target ops method "low_get_pc". */
219
220 CORE_ADDR
221 riscv_target::low_get_pc (regcache *regcache)
222 {
223 elf_gregset_t regset;
224
225 if (sizeof (regset[0]) == 8)
226 return linux_get_pc_64bit (regcache);
227 else
228 return linux_get_pc_32bit (regcache);
229 }
230
231 /* Implementation of linux target ops method "low_set_pc". */
232
233 void
234 riscv_target::low_set_pc (regcache *regcache, CORE_ADDR newpc)
235 {
236 elf_gregset_t regset;
237
238 if (sizeof (regset[0]) == 8)
239 linux_set_pc_64bit (regcache, newpc);
240 else
241 linux_set_pc_32bit (regcache, newpc);
242 }
243
244 /* Correct in either endianness. */
245 static const uint16_t riscv_ibreakpoint[] = { 0x0073, 0x0010 };
246 static const uint16_t riscv_cbreakpoint = 0x9002;
247
248 /* Implementation of linux_target_ops method "breakpoint_kind_from_pc". */
249
250 static int
251 riscv_breakpoint_kind_from_pc (CORE_ADDR *pcptr)
252 {
253 union
254 {
255 gdb_byte bytes[2];
256 uint16_t insn;
257 }
258 buf;
259
260 if (target_read_memory (*pcptr, buf.bytes, sizeof (buf.insn)) == 0
261 && riscv_insn_length (buf.insn == sizeof (riscv_ibreakpoint)))
262 return sizeof (riscv_ibreakpoint);
263 else
264 return sizeof (riscv_cbreakpoint);
265 }
266
267 /* Implementation of linux_target_ops method "sw_breakpoint_from_kind". */
268
269 static const gdb_byte *
270 riscv_sw_breakpoint_from_kind (int kind, int *size)
271 {
272 *size = kind;
273 switch (kind)
274 {
275 case sizeof (riscv_ibreakpoint):
276 return (const gdb_byte *) &riscv_ibreakpoint;
277 default:
278 return (const gdb_byte *) &riscv_cbreakpoint;
279 }
280 }
281
282 /* Implementation of linux_target_ops method "breakpoint_at". */
283
284 static int
285 riscv_breakpoint_at (CORE_ADDR pc)
286 {
287 union
288 {
289 gdb_byte bytes[2];
290 uint16_t insn;
291 }
292 buf;
293
294 if (target_read_memory (pc, buf.bytes, sizeof (buf.insn)) == 0
295 && (buf.insn == riscv_cbreakpoint
296 || (buf.insn == riscv_ibreakpoint[0]
297 && target_read_memory (pc + sizeof (buf.insn), buf.bytes,
298 sizeof (buf.insn)) == 0
299 && buf.insn == riscv_ibreakpoint[1])))
300 return 1;
301 else
302 return 0;
303 }
304
305 /* RISC-V/Linux target operations. */
306 struct linux_target_ops the_low_target =
307 {
308 riscv_breakpoint_kind_from_pc,
309 riscv_sw_breakpoint_from_kind,
310 NULL, /* get_next_pcs */
311 0, /* decr_pc_after_break */
312 riscv_breakpoint_at,
313 };
314
315 /* The linux target ops object. */
316
317 linux_process_target *the_linux_target = &the_riscv_target;
318
319 /* Initialize the RISC-V/Linux target. */
320
321 void
322 initialize_low_arch ()
323 {
324 initialize_regsets_info (&riscv_regsets_info);
325 }
This page took 0.038531 seconds and 5 git commands to generate.