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