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