riscv-tdep.c revision 1.1.1.5 1 1.1 christos /* Target-dependent code for the RISC-V architecture, for GDB.
2 1.1 christos
3 1.1.1.4 christos Copyright (C) 2018-2024 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1.1.4 christos #include "extract-store-integer.h"
21 1.1 christos #include "frame.h"
22 1.1 christos #include "inferior.h"
23 1.1 christos #include "symtab.h"
24 1.1 christos #include "value.h"
25 1.1.1.4 christos #include "cli/cli-cmds.h"
26 1.1 christos #include "language.h"
27 1.1 christos #include "gdbcore.h"
28 1.1 christos #include "symfile.h"
29 1.1 christos #include "objfiles.h"
30 1.1 christos #include "gdbtypes.h"
31 1.1 christos #include "target.h"
32 1.1 christos #include "arch-utils.h"
33 1.1 christos #include "regcache.h"
34 1.1 christos #include "osabi.h"
35 1.1 christos #include "riscv-tdep.h"
36 1.1 christos #include "reggroups.h"
37 1.1 christos #include "opcode/riscv.h"
38 1.1 christos #include "elf/riscv.h"
39 1.1 christos #include "elf-bfd.h"
40 1.1 christos #include "symcat.h"
41 1.1 christos #include "dis-asm.h"
42 1.1 christos #include "frame-unwind.h"
43 1.1 christos #include "frame-base.h"
44 1.1 christos #include "trad-frame.h"
45 1.1 christos #include "infcall.h"
46 1.1 christos #include "floatformat.h"
47 1.1 christos #include "remote.h"
48 1.1 christos #include "target-descriptions.h"
49 1.1.1.2 christos #include "dwarf2/frame.h"
50 1.1 christos #include "user-regs.h"
51 1.1 christos #include "valprint.h"
52 1.1 christos #include "opcode/riscv-opc.h"
53 1.1 christos #include "cli/cli-decode.h"
54 1.1 christos #include "observable.h"
55 1.1 christos #include "prologue-value.h"
56 1.1 christos #include "arch/riscv.h"
57 1.1.1.2 christos #include "riscv-ravenscar-thread.h"
58 1.1.1.4 christos #include "gdbsupport/gdb-safe-ctype.h"
59 1.1 christos
60 1.1 christos /* The stack must be 16-byte aligned. */
61 1.1 christos #define SP_ALIGNMENT 16
62 1.1 christos
63 1.1 christos /* The biggest alignment that the target supports. */
64 1.1 christos #define BIGGEST_ALIGNMENT 16
65 1.1 christos
66 1.1 christos /* Define a series of is_XXX_insn functions to check if the value INSN
67 1.1 christos is an instance of instruction XXX. */
68 1.1 christos #define DECLARE_INSN(INSN_NAME, INSN_MATCH, INSN_MASK) \
69 1.1 christos static inline bool is_ ## INSN_NAME ## _insn (long insn) \
70 1.1 christos { \
71 1.1 christos return (insn & INSN_MASK) == INSN_MATCH; \
72 1.1 christos }
73 1.1 christos #include "opcode/riscv-opc.h"
74 1.1 christos #undef DECLARE_INSN
75 1.1 christos
76 1.1.1.4 christos /* When this is true debugging information about breakpoint kinds will be
77 1.1.1.4 christos printed. */
78 1.1.1.3 christos
79 1.1.1.4 christos static bool riscv_debug_breakpoints = false;
80 1.1.1.3 christos
81 1.1.1.4 christos /* Print a "riscv-breakpoints" debug statement. */
82 1.1.1.4 christos
83 1.1.1.4 christos #define riscv_breakpoints_debug_printf(fmt, ...) \
84 1.1.1.4 christos debug_prefixed_printf_cond (riscv_debug_breakpoints, \
85 1.1.1.4 christos "riscv-breakpoints", \
86 1.1.1.4 christos fmt, ##__VA_ARGS__)
87 1.1.1.4 christos
88 1.1.1.4 christos /* When this is true debugging information about inferior calls will be
89 1.1.1.4 christos printed. */
90 1.1.1.4 christos
91 1.1.1.4 christos static bool riscv_debug_infcall = false;
92 1.1.1.4 christos
93 1.1.1.4 christos /* Print a "riscv-infcall" debug statement. */
94 1.1.1.4 christos
95 1.1.1.4 christos #define riscv_infcall_debug_printf(fmt, ...) \
96 1.1.1.4 christos debug_prefixed_printf_cond (riscv_debug_infcall, "riscv-infcall", \
97 1.1.1.4 christos fmt, ##__VA_ARGS__)
98 1.1.1.4 christos
99 1.1.1.4 christos /* Print "riscv-infcall" start/end debug statements. */
100 1.1.1.4 christos
101 1.1.1.4 christos #define RISCV_INFCALL_SCOPED_DEBUG_START_END(fmt, ...) \
102 1.1.1.4 christos scoped_debug_start_end (riscv_debug_infcall, "riscv-infcall", \
103 1.1.1.4 christos fmt, ##__VA_ARGS__)
104 1.1.1.4 christos
105 1.1.1.4 christos /* When this is true debugging information about stack unwinding will be
106 1.1.1.4 christos printed. */
107 1.1.1.4 christos
108 1.1.1.4 christos static bool riscv_debug_unwinder = false;
109 1.1.1.4 christos
110 1.1.1.4 christos /* Print a "riscv-unwinder" debug statement. */
111 1.1.1.3 christos
112 1.1.1.4 christos #define riscv_unwinder_debug_printf(fmt, ...) \
113 1.1.1.4 christos debug_prefixed_printf_cond (riscv_debug_unwinder, "riscv-unwinder", \
114 1.1.1.4 christos fmt, ##__VA_ARGS__)
115 1.1.1.3 christos
116 1.1.1.4 christos /* When this is true debugging information about gdbarch initialisation
117 1.1.1.3 christos will be printed. */
118 1.1.1.3 christos
119 1.1.1.4 christos static bool riscv_debug_gdbarch = false;
120 1.1.1.3 christos
121 1.1.1.4 christos /* Print a "riscv-gdbarch" debug statement. */
122 1.1.1.3 christos
123 1.1.1.4 christos #define riscv_gdbarch_debug_printf(fmt, ...) \
124 1.1.1.4 christos debug_prefixed_printf_cond (riscv_debug_gdbarch, "riscv-gdbarch", \
125 1.1.1.4 christos fmt, ##__VA_ARGS__)
126 1.1.1.3 christos
127 1.1.1.3 christos /* The names of the RISC-V target description features. */
128 1.1.1.3 christos const char *riscv_feature_name_csr = "org.gnu.gdb.riscv.csr";
129 1.1.1.3 christos static const char *riscv_feature_name_cpu = "org.gnu.gdb.riscv.cpu";
130 1.1.1.3 christos static const char *riscv_feature_name_fpu = "org.gnu.gdb.riscv.fpu";
131 1.1.1.3 christos static const char *riscv_feature_name_virtual = "org.gnu.gdb.riscv.virtual";
132 1.1.1.3 christos static const char *riscv_feature_name_vector = "org.gnu.gdb.riscv.vector";
133 1.1.1.3 christos
134 1.1.1.3 christos /* The current set of options to be passed to the disassembler. */
135 1.1.1.4 christos static std::string riscv_disassembler_options;
136 1.1.1.3 christos
137 1.1 christos /* Cached information about a frame. */
138 1.1 christos
139 1.1 christos struct riscv_unwind_cache
140 1.1 christos {
141 1.1 christos /* The register from which we can calculate the frame base. This is
142 1.1 christos usually $sp or $fp. */
143 1.1 christos int frame_base_reg;
144 1.1 christos
145 1.1 christos /* The offset from the current value in register FRAME_BASE_REG to the
146 1.1 christos actual frame base address. */
147 1.1 christos int frame_base_offset;
148 1.1 christos
149 1.1 christos /* Information about previous register values. */
150 1.1.1.3 christos trad_frame_saved_reg *regs;
151 1.1 christos
152 1.1 christos /* The id for this frame. */
153 1.1 christos struct frame_id this_id;
154 1.1 christos
155 1.1 christos /* The base (stack) address for this frame. This is the stack pointer
156 1.1 christos value on entry to this frame before any adjustments are made. */
157 1.1 christos CORE_ADDR frame_base;
158 1.1 christos };
159 1.1 christos
160 1.1 christos /* RISC-V specific register group for CSRs. */
161 1.1 christos
162 1.1.1.3 christos static const reggroup *csr_reggroup = nullptr;
163 1.1 christos
164 1.1.1.2 christos /* Callback function for user_reg_add. */
165 1.1.1.2 christos
166 1.1.1.2 christos static struct value *
167 1.1.1.4 christos value_of_riscv_user_reg (const frame_info_ptr &frame, const void *baton)
168 1.1.1.2 christos {
169 1.1.1.2 christos const int *reg_p = (const int *) baton;
170 1.1.1.4 christos return value_of_register (*reg_p, get_next_frame_sentinel_okay (frame));
171 1.1.1.2 christos }
172 1.1.1.2 christos
173 1.1.1.2 christos /* Information about a register alias that needs to be set up for this
174 1.1.1.2 christos target. These are collected when the target's XML description is
175 1.1.1.2 christos analysed, and then processed later, once the gdbarch has been created. */
176 1.1.1.2 christos
177 1.1.1.2 christos class riscv_pending_register_alias
178 1.1.1.2 christos {
179 1.1.1.2 christos public:
180 1.1.1.2 christos /* Constructor. */
181 1.1.1.2 christos
182 1.1.1.2 christos riscv_pending_register_alias (const char *name, const void *baton)
183 1.1.1.2 christos : m_name (name),
184 1.1.1.2 christos m_baton (baton)
185 1.1.1.2 christos { /* Nothing. */ }
186 1.1.1.2 christos
187 1.1.1.2 christos /* Convert this into a user register for GDBARCH. */
188 1.1.1.2 christos
189 1.1.1.2 christos void create (struct gdbarch *gdbarch) const
190 1.1.1.2 christos {
191 1.1.1.2 christos user_reg_add (gdbarch, m_name, value_of_riscv_user_reg, m_baton);
192 1.1.1.2 christos }
193 1.1.1.2 christos
194 1.1.1.2 christos private:
195 1.1.1.2 christos /* The name for this alias. */
196 1.1.1.2 christos const char *m_name;
197 1.1.1.2 christos
198 1.1.1.2 christos /* The baton value for passing to user_reg_add. This must point to some
199 1.1.1.2 christos data that will live for at least as long as the gdbarch object to
200 1.1.1.2 christos which the user register is attached. */
201 1.1.1.2 christos const void *m_baton;
202 1.1.1.2 christos };
203 1.1.1.2 christos
204 1.1 christos /* A set of registers that we expect to find in a tdesc_feature. These
205 1.1 christos are use in RISCV_GDBARCH_INIT when processing the target description. */
206 1.1 christos
207 1.1 christos struct riscv_register_feature
208 1.1 christos {
209 1.1.1.3 christos explicit riscv_register_feature (const char *feature_name)
210 1.1.1.3 christos : m_feature_name (feature_name)
211 1.1.1.3 christos { /* Delete. */ }
212 1.1.1.3 christos
213 1.1.1.3 christos riscv_register_feature () = delete;
214 1.1.1.3 christos DISABLE_COPY_AND_ASSIGN (riscv_register_feature);
215 1.1.1.3 christos
216 1.1 christos /* Information for a single register. */
217 1.1 christos struct register_info
218 1.1 christos {
219 1.1 christos /* The GDB register number for this register. */
220 1.1 christos int regnum;
221 1.1 christos
222 1.1 christos /* List of names for this register. The first name in this list is the
223 1.1 christos preferred name, the name GDB should use when describing this
224 1.1 christos register. */
225 1.1.1.2 christos std::vector<const char *> names;
226 1.1 christos
227 1.1.1.2 christos /* Look in FEATURE for a register with a name from this classes names
228 1.1.1.2 christos list. If the register is found then register its number with
229 1.1.1.3 christos TDESC_DATA and add all its aliases to the ALIASES list.
230 1.1.1.3 christos PREFER_FIRST_NAME_P is used when deciding which aliases to create. */
231 1.1.1.2 christos bool check (struct tdesc_arch_data *tdesc_data,
232 1.1.1.2 christos const struct tdesc_feature *feature,
233 1.1.1.3 christos bool prefer_first_name_p,
234 1.1.1.2 christos std::vector<riscv_pending_register_alias> *aliases) const;
235 1.1 christos };
236 1.1 christos
237 1.1.1.3 christos /* Return the name of this feature. */
238 1.1.1.3 christos const char *name () const
239 1.1.1.3 christos { return m_feature_name; }
240 1.1.1.3 christos
241 1.1.1.3 christos protected:
242 1.1.1.3 christos
243 1.1.1.3 christos /* Return a target description feature extracted from TDESC for this
244 1.1.1.3 christos register feature. Will return nullptr if there is no feature in TDESC
245 1.1.1.3 christos with the name M_FEATURE_NAME. */
246 1.1.1.3 christos const struct tdesc_feature *tdesc_feature (const struct target_desc *tdesc) const
247 1.1.1.3 christos {
248 1.1.1.3 christos return tdesc_find_feature (tdesc, name ());
249 1.1.1.3 christos }
250 1.1.1.2 christos
251 1.1 christos /* List of all the registers that we expect that we might find in this
252 1.1 christos register set. */
253 1.1.1.3 christos std::vector<struct register_info> m_registers;
254 1.1.1.3 christos
255 1.1.1.3 christos private:
256 1.1.1.3 christos
257 1.1.1.3 christos /* The name for this feature. This is the name used to find this feature
258 1.1.1.3 christos within the target description. */
259 1.1.1.3 christos const char *m_feature_name;
260 1.1 christos };
261 1.1 christos
262 1.1.1.2 christos /* See description in the class declaration above. */
263 1.1.1.2 christos
264 1.1.1.2 christos bool
265 1.1.1.2 christos riscv_register_feature::register_info::check
266 1.1.1.2 christos (struct tdesc_arch_data *tdesc_data,
267 1.1.1.2 christos const struct tdesc_feature *feature,
268 1.1.1.3 christos bool prefer_first_name_p,
269 1.1.1.2 christos std::vector<riscv_pending_register_alias> *aliases) const
270 1.1.1.2 christos {
271 1.1.1.2 christos for (const char *name : this->names)
272 1.1.1.2 christos {
273 1.1.1.2 christos bool found = tdesc_numbered_register (feature, tdesc_data,
274 1.1.1.2 christos this->regnum, name);
275 1.1.1.2 christos if (found)
276 1.1.1.2 christos {
277 1.1.1.2 christos /* We know that the target description mentions this
278 1.1.1.2 christos register. In RISCV_REGISTER_NAME we ensure that GDB
279 1.1.1.2 christos always uses the first name for each register, so here we
280 1.1.1.2 christos add aliases for all of the remaining names. */
281 1.1.1.3 christos int start_index = prefer_first_name_p ? 1 : 0;
282 1.1.1.2 christos for (int i = start_index; i < this->names.size (); ++i)
283 1.1.1.2 christos {
284 1.1.1.2 christos const char *alias = this->names[i];
285 1.1.1.3 christos if (alias == name && !prefer_first_name_p)
286 1.1.1.2 christos continue;
287 1.1.1.2 christos aliases->emplace_back (alias, (void *) &this->regnum);
288 1.1.1.2 christos }
289 1.1.1.2 christos return true;
290 1.1.1.2 christos }
291 1.1.1.2 christos }
292 1.1.1.2 christos return false;
293 1.1.1.2 christos }
294 1.1.1.2 christos
295 1.1.1.3 christos /* Class representing the x-registers feature set. */
296 1.1 christos
297 1.1.1.3 christos struct riscv_xreg_feature : public riscv_register_feature
298 1.1 christos {
299 1.1.1.3 christos riscv_xreg_feature ()
300 1.1.1.3 christos : riscv_register_feature (riscv_feature_name_cpu)
301 1.1.1.3 christos {
302 1.1.1.3 christos m_registers = {
303 1.1.1.3 christos { RISCV_ZERO_REGNUM + 0, { "zero", "x0" } },
304 1.1.1.3 christos { RISCV_ZERO_REGNUM + 1, { "ra", "x1" } },
305 1.1.1.3 christos { RISCV_ZERO_REGNUM + 2, { "sp", "x2" } },
306 1.1.1.3 christos { RISCV_ZERO_REGNUM + 3, { "gp", "x3" } },
307 1.1.1.3 christos { RISCV_ZERO_REGNUM + 4, { "tp", "x4" } },
308 1.1.1.3 christos { RISCV_ZERO_REGNUM + 5, { "t0", "x5" } },
309 1.1.1.3 christos { RISCV_ZERO_REGNUM + 6, { "t1", "x6" } },
310 1.1.1.3 christos { RISCV_ZERO_REGNUM + 7, { "t2", "x7" } },
311 1.1.1.3 christos { RISCV_ZERO_REGNUM + 8, { "fp", "x8", "s0" } },
312 1.1.1.3 christos { RISCV_ZERO_REGNUM + 9, { "s1", "x9" } },
313 1.1.1.3 christos { RISCV_ZERO_REGNUM + 10, { "a0", "x10" } },
314 1.1.1.3 christos { RISCV_ZERO_REGNUM + 11, { "a1", "x11" } },
315 1.1.1.3 christos { RISCV_ZERO_REGNUM + 12, { "a2", "x12" } },
316 1.1.1.3 christos { RISCV_ZERO_REGNUM + 13, { "a3", "x13" } },
317 1.1.1.3 christos { RISCV_ZERO_REGNUM + 14, { "a4", "x14" } },
318 1.1.1.3 christos { RISCV_ZERO_REGNUM + 15, { "a5", "x15" } },
319 1.1.1.3 christos { RISCV_ZERO_REGNUM + 16, { "a6", "x16" } },
320 1.1.1.3 christos { RISCV_ZERO_REGNUM + 17, { "a7", "x17" } },
321 1.1.1.3 christos { RISCV_ZERO_REGNUM + 18, { "s2", "x18" } },
322 1.1.1.3 christos { RISCV_ZERO_REGNUM + 19, { "s3", "x19" } },
323 1.1.1.3 christos { RISCV_ZERO_REGNUM + 20, { "s4", "x20" } },
324 1.1.1.3 christos { RISCV_ZERO_REGNUM + 21, { "s5", "x21" } },
325 1.1.1.3 christos { RISCV_ZERO_REGNUM + 22, { "s6", "x22" } },
326 1.1.1.3 christos { RISCV_ZERO_REGNUM + 23, { "s7", "x23" } },
327 1.1.1.3 christos { RISCV_ZERO_REGNUM + 24, { "s8", "x24" } },
328 1.1.1.3 christos { RISCV_ZERO_REGNUM + 25, { "s9", "x25" } },
329 1.1.1.3 christos { RISCV_ZERO_REGNUM + 26, { "s10", "x26" } },
330 1.1.1.3 christos { RISCV_ZERO_REGNUM + 27, { "s11", "x27" } },
331 1.1.1.3 christos { RISCV_ZERO_REGNUM + 28, { "t3", "x28" } },
332 1.1.1.3 christos { RISCV_ZERO_REGNUM + 29, { "t4", "x29" } },
333 1.1.1.3 christos { RISCV_ZERO_REGNUM + 30, { "t5", "x30" } },
334 1.1.1.3 christos { RISCV_ZERO_REGNUM + 31, { "t6", "x31" } },
335 1.1.1.3 christos { RISCV_ZERO_REGNUM + 32, { "pc" } }
336 1.1.1.3 christos };
337 1.1.1.3 christos }
338 1.1.1.3 christos
339 1.1.1.3 christos /* Return the preferred name for the register with gdb register number
340 1.1.1.3 christos REGNUM, which must be in the inclusive range RISCV_ZERO_REGNUM to
341 1.1.1.3 christos RISCV_PC_REGNUM. */
342 1.1.1.3 christos const char *register_name (int regnum) const
343 1.1.1.3 christos {
344 1.1.1.3 christos gdb_assert (regnum >= RISCV_ZERO_REGNUM && regnum <= m_registers.size ());
345 1.1.1.3 christos return m_registers[regnum].names[0];
346 1.1.1.3 christos }
347 1.1.1.3 christos
348 1.1.1.3 christos /* Check this feature within TDESC, record the registers from this
349 1.1.1.3 christos feature into TDESC_DATA and update ALIASES and FEATURES. */
350 1.1.1.3 christos bool check (const struct target_desc *tdesc,
351 1.1.1.3 christos struct tdesc_arch_data *tdesc_data,
352 1.1.1.3 christos std::vector<riscv_pending_register_alias> *aliases,
353 1.1.1.3 christos struct riscv_gdbarch_features *features) const
354 1.1.1.3 christos {
355 1.1.1.3 christos const struct tdesc_feature *feature_cpu = tdesc_feature (tdesc);
356 1.1.1.3 christos
357 1.1.1.3 christos if (feature_cpu == nullptr)
358 1.1.1.3 christos return false;
359 1.1.1.3 christos
360 1.1.1.3 christos bool seen_an_optional_reg_p = false;
361 1.1.1.3 christos for (const auto ® : m_registers)
362 1.1.1.3 christos {
363 1.1.1.3 christos bool found = reg.check (tdesc_data, feature_cpu, true, aliases);
364 1.1.1.3 christos
365 1.1.1.3 christos bool is_optional_reg_p = (reg.regnum >= RISCV_ZERO_REGNUM + 16
366 1.1.1.3 christos && reg.regnum < RISCV_ZERO_REGNUM + 32);
367 1.1.1.3 christos
368 1.1.1.3 christos if (!found && (!is_optional_reg_p || seen_an_optional_reg_p))
369 1.1.1.3 christos return false;
370 1.1.1.3 christos else if (found && is_optional_reg_p)
371 1.1.1.3 christos seen_an_optional_reg_p = true;
372 1.1.1.3 christos }
373 1.1.1.3 christos
374 1.1.1.3 christos /* Check that all of the core cpu registers have the same bitsize. */
375 1.1.1.3 christos int xlen_bitsize = tdesc_register_bitsize (feature_cpu, "pc");
376 1.1.1.3 christos
377 1.1.1.3 christos bool valid_p = true;
378 1.1.1.3 christos for (auto &tdesc_reg : feature_cpu->registers)
379 1.1.1.3 christos valid_p &= (tdesc_reg->bitsize == xlen_bitsize);
380 1.1.1.3 christos
381 1.1.1.3 christos features->xlen = (xlen_bitsize / 8);
382 1.1.1.3 christos features->embedded = !seen_an_optional_reg_p;
383 1.1.1.3 christos
384 1.1.1.3 christos return valid_p;
385 1.1.1.3 christos }
386 1.1 christos };
387 1.1 christos
388 1.1.1.3 christos /* An instance of the x-register feature set. */
389 1.1.1.3 christos
390 1.1.1.3 christos static const struct riscv_xreg_feature riscv_xreg_feature;
391 1.1 christos
392 1.1.1.3 christos /* Class representing the f-registers feature set. */
393 1.1.1.3 christos
394 1.1.1.3 christos struct riscv_freg_feature : public riscv_register_feature
395 1.1 christos {
396 1.1.1.3 christos riscv_freg_feature ()
397 1.1.1.3 christos : riscv_register_feature (riscv_feature_name_fpu)
398 1.1.1.3 christos {
399 1.1.1.3 christos m_registers = {
400 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 0, { "ft0", "f0" } },
401 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 1, { "ft1", "f1" } },
402 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 2, { "ft2", "f2" } },
403 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 3, { "ft3", "f3" } },
404 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 4, { "ft4", "f4" } },
405 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 5, { "ft5", "f5" } },
406 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 6, { "ft6", "f6" } },
407 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 7, { "ft7", "f7" } },
408 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 8, { "fs0", "f8" } },
409 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 9, { "fs1", "f9" } },
410 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 10, { "fa0", "f10" } },
411 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 11, { "fa1", "f11" } },
412 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 12, { "fa2", "f12" } },
413 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 13, { "fa3", "f13" } },
414 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 14, { "fa4", "f14" } },
415 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 15, { "fa5", "f15" } },
416 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 16, { "fa6", "f16" } },
417 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 17, { "fa7", "f17" } },
418 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 18, { "fs2", "f18" } },
419 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 19, { "fs3", "f19" } },
420 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 20, { "fs4", "f20" } },
421 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 21, { "fs5", "f21" } },
422 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 22, { "fs6", "f22" } },
423 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 23, { "fs7", "f23" } },
424 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 24, { "fs8", "f24" } },
425 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 25, { "fs9", "f25" } },
426 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 26, { "fs10", "f26" } },
427 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 27, { "fs11", "f27" } },
428 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 28, { "ft8", "f28" } },
429 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 29, { "ft9", "f29" } },
430 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 30, { "ft10", "f30" } },
431 1.1.1.3 christos { RISCV_FIRST_FP_REGNUM + 31, { "ft11", "f31" } },
432 1.1.1.3 christos { RISCV_CSR_FFLAGS_REGNUM, { "fflags", "csr1" } },
433 1.1.1.3 christos { RISCV_CSR_FRM_REGNUM, { "frm", "csr2" } },
434 1.1.1.3 christos { RISCV_CSR_FCSR_REGNUM, { "fcsr", "csr3" } },
435 1.1.1.3 christos };
436 1.1.1.3 christos }
437 1.1.1.3 christos
438 1.1.1.3 christos /* Return the preferred name for the register with gdb register number
439 1.1.1.3 christos REGNUM, which must be in the inclusive range RISCV_FIRST_FP_REGNUM to
440 1.1.1.3 christos RISCV_LAST_FP_REGNUM. */
441 1.1.1.3 christos const char *register_name (int regnum) const
442 1.1.1.3 christos {
443 1.1.1.4 christos static_assert (RISCV_LAST_FP_REGNUM == RISCV_FIRST_FP_REGNUM + 31);
444 1.1.1.3 christos gdb_assert (regnum >= RISCV_FIRST_FP_REGNUM
445 1.1.1.3 christos && regnum <= RISCV_LAST_FP_REGNUM);
446 1.1.1.3 christos regnum -= RISCV_FIRST_FP_REGNUM;
447 1.1.1.3 christos return m_registers[regnum].names[0];
448 1.1.1.3 christos }
449 1.1.1.3 christos
450 1.1.1.3 christos /* Check this feature within TDESC, record the registers from this
451 1.1.1.3 christos feature into TDESC_DATA and update ALIASES and FEATURES. */
452 1.1.1.3 christos bool check (const struct target_desc *tdesc,
453 1.1.1.3 christos struct tdesc_arch_data *tdesc_data,
454 1.1.1.3 christos std::vector<riscv_pending_register_alias> *aliases,
455 1.1.1.3 christos struct riscv_gdbarch_features *features) const
456 1.1.1.3 christos {
457 1.1.1.3 christos const struct tdesc_feature *feature_fpu = tdesc_feature (tdesc);
458 1.1.1.3 christos
459 1.1.1.3 christos /* It's fine if this feature is missing. Update the architecture
460 1.1.1.3 christos feature set and return. */
461 1.1.1.3 christos if (feature_fpu == nullptr)
462 1.1.1.3 christos {
463 1.1.1.3 christos features->flen = 0;
464 1.1.1.3 christos return true;
465 1.1.1.3 christos }
466 1.1.1.3 christos
467 1.1.1.3 christos /* Check all of the floating pointer registers are present. We also
468 1.1.1.3 christos check that the floating point CSRs are present too, though if these
469 1.1.1.3 christos are missing this is not fatal. */
470 1.1.1.3 christos for (const auto ® : m_registers)
471 1.1.1.3 christos {
472 1.1.1.3 christos bool found = reg.check (tdesc_data, feature_fpu, true, aliases);
473 1.1.1.3 christos
474 1.1.1.3 christos bool is_ctrl_reg_p = reg.regnum > RISCV_LAST_FP_REGNUM;
475 1.1.1.3 christos
476 1.1.1.3 christos if (!found && !is_ctrl_reg_p)
477 1.1.1.3 christos return false;
478 1.1.1.3 christos }
479 1.1.1.3 christos
480 1.1.1.3 christos /* Look through all of the floating point registers (not the FP CSRs
481 1.1.1.3 christos though), and check they all have the same bitsize. Use this bitsize
482 1.1.1.3 christos to update the feature set for this gdbarch. */
483 1.1.1.3 christos int fp_bitsize = -1;
484 1.1.1.3 christos for (const auto ® : m_registers)
485 1.1.1.3 christos {
486 1.1.1.3 christos /* Stop once we get to the CSRs which are at the end of the
487 1.1.1.3 christos M_REGISTERS list. */
488 1.1.1.3 christos if (reg.regnum > RISCV_LAST_FP_REGNUM)
489 1.1.1.3 christos break;
490 1.1.1.3 christos
491 1.1.1.3 christos int reg_bitsize = -1;
492 1.1.1.3 christos for (const char *name : reg.names)
493 1.1.1.3 christos {
494 1.1.1.3 christos if (tdesc_unnumbered_register (feature_fpu, name))
495 1.1.1.3 christos {
496 1.1.1.3 christos reg_bitsize = tdesc_register_bitsize (feature_fpu, name);
497 1.1.1.3 christos break;
498 1.1.1.3 christos }
499 1.1.1.3 christos }
500 1.1.1.3 christos gdb_assert (reg_bitsize != -1);
501 1.1.1.3 christos if (fp_bitsize == -1)
502 1.1.1.3 christos fp_bitsize = reg_bitsize;
503 1.1.1.3 christos else if (fp_bitsize != reg_bitsize)
504 1.1.1.3 christos return false;
505 1.1.1.3 christos }
506 1.1 christos
507 1.1.1.3 christos features->flen = (fp_bitsize / 8);
508 1.1.1.3 christos return true;
509 1.1.1.3 christos }
510 1.1 christos };
511 1.1 christos
512 1.1.1.3 christos /* An instance of the f-register feature set. */
513 1.1.1.3 christos
514 1.1.1.3 christos static const struct riscv_freg_feature riscv_freg_feature;
515 1.1.1.3 christos
516 1.1.1.3 christos /* Class representing the virtual registers. These are not physical
517 1.1.1.3 christos registers on the hardware, but might be available from the target.
518 1.1.1.3 christos These are not pseudo registers, reading these really does result in a
519 1.1.1.3 christos register read from the target, it is just that there might not be a
520 1.1.1.3 christos physical register backing the result. */
521 1.1.1.3 christos
522 1.1.1.3 christos struct riscv_virtual_feature : public riscv_register_feature
523 1.1.1.3 christos {
524 1.1.1.3 christos riscv_virtual_feature ()
525 1.1.1.3 christos : riscv_register_feature (riscv_feature_name_virtual)
526 1.1.1.3 christos {
527 1.1.1.3 christos m_registers = {
528 1.1.1.3 christos { RISCV_PRIV_REGNUM, { "priv" } }
529 1.1.1.3 christos };
530 1.1.1.3 christos }
531 1.1.1.3 christos
532 1.1.1.3 christos bool check (const struct target_desc *tdesc,
533 1.1.1.3 christos struct tdesc_arch_data *tdesc_data,
534 1.1.1.3 christos std::vector<riscv_pending_register_alias> *aliases,
535 1.1.1.3 christos struct riscv_gdbarch_features *features) const
536 1.1.1.3 christos {
537 1.1.1.3 christos const struct tdesc_feature *feature_virtual = tdesc_feature (tdesc);
538 1.1.1.3 christos
539 1.1.1.3 christos /* It's fine if this feature is missing. */
540 1.1.1.3 christos if (feature_virtual == nullptr)
541 1.1.1.3 christos return true;
542 1.1.1.3 christos
543 1.1.1.3 christos /* We don't check the return value from the call to check here, all the
544 1.1.1.3 christos registers in this feature are optional. */
545 1.1.1.3 christos for (const auto ® : m_registers)
546 1.1.1.3 christos reg.check (tdesc_data, feature_virtual, true, aliases);
547 1.1.1.3 christos
548 1.1.1.3 christos return true;
549 1.1.1.3 christos }
550 1.1 christos };
551 1.1 christos
552 1.1.1.3 christos /* An instance of the virtual register feature. */
553 1.1.1.3 christos
554 1.1.1.3 christos static const struct riscv_virtual_feature riscv_virtual_feature;
555 1.1.1.3 christos
556 1.1.1.3 christos /* Class representing the CSR feature. */
557 1.1.1.3 christos
558 1.1.1.3 christos struct riscv_csr_feature : public riscv_register_feature
559 1.1.1.3 christos {
560 1.1.1.3 christos riscv_csr_feature ()
561 1.1.1.3 christos : riscv_register_feature (riscv_feature_name_csr)
562 1.1.1.3 christos {
563 1.1.1.3 christos m_registers = {
564 1.1.1.3 christos #define DECLARE_CSR(NAME,VALUE,CLASS,DEFINE_VER,ABORT_VER) \
565 1.1.1.3 christos { RISCV_ ## VALUE ## _REGNUM, { # NAME } },
566 1.1 christos #include "opcode/riscv-opc.h"
567 1.1 christos #undef DECLARE_CSR
568 1.1.1.3 christos };
569 1.1.1.3 christos riscv_create_csr_aliases ();
570 1.1.1.3 christos }
571 1.1.1.3 christos
572 1.1.1.3 christos bool check (const struct target_desc *tdesc,
573 1.1.1.3 christos struct tdesc_arch_data *tdesc_data,
574 1.1.1.3 christos std::vector<riscv_pending_register_alias> *aliases,
575 1.1.1.3 christos struct riscv_gdbarch_features *features) const
576 1.1.1.3 christos {
577 1.1.1.3 christos const struct tdesc_feature *feature_csr = tdesc_feature (tdesc);
578 1.1.1.3 christos
579 1.1.1.3 christos /* It's fine if this feature is missing. */
580 1.1.1.3 christos if (feature_csr == nullptr)
581 1.1.1.3 christos return true;
582 1.1.1.3 christos
583 1.1.1.3 christos /* We don't check the return value from the call to check here, all the
584 1.1.1.3 christos registers in this feature are optional. */
585 1.1.1.3 christos for (const auto ® : m_registers)
586 1.1.1.3 christos reg.check (tdesc_data, feature_csr, true, aliases);
587 1.1.1.3 christos
588 1.1.1.3 christos return true;
589 1.1.1.3 christos }
590 1.1.1.3 christos
591 1.1.1.3 christos private:
592 1.1.1.3 christos
593 1.1.1.3 christos /* Complete RISCV_CSR_FEATURE, building the CSR alias names and adding them
594 1.1.1.3 christos to the name list for each register. */
595 1.1.1.3 christos
596 1.1.1.3 christos void
597 1.1.1.3 christos riscv_create_csr_aliases ()
598 1.1.1.3 christos {
599 1.1.1.3 christos for (auto ® : m_registers)
600 1.1.1.3 christos {
601 1.1.1.3 christos int csr_num = reg.regnum - RISCV_FIRST_CSR_REGNUM;
602 1.1.1.3 christos gdb::unique_xmalloc_ptr<char> alias = xstrprintf ("csr%d", csr_num);
603 1.1.1.3 christos reg.names.push_back (alias.release ());
604 1.1.1.3 christos }
605 1.1.1.3 christos }
606 1.1 christos };
607 1.1 christos
608 1.1.1.3 christos /* An instance of the csr register feature. */
609 1.1 christos
610 1.1.1.3 christos static const struct riscv_csr_feature riscv_csr_feature;
611 1.1.1.3 christos
612 1.1.1.3 christos /* Class representing the v-registers feature set. */
613 1.1.1.3 christos
614 1.1.1.3 christos struct riscv_vector_feature : public riscv_register_feature
615 1.1 christos {
616 1.1.1.3 christos riscv_vector_feature ()
617 1.1.1.3 christos : riscv_register_feature (riscv_feature_name_vector)
618 1.1.1.3 christos {
619 1.1.1.3 christos m_registers = {
620 1.1.1.3 christos { RISCV_V0_REGNUM + 0, { "v0" } },
621 1.1.1.3 christos { RISCV_V0_REGNUM + 1, { "v1" } },
622 1.1.1.3 christos { RISCV_V0_REGNUM + 2, { "v2" } },
623 1.1.1.3 christos { RISCV_V0_REGNUM + 3, { "v3" } },
624 1.1.1.3 christos { RISCV_V0_REGNUM + 4, { "v4" } },
625 1.1.1.3 christos { RISCV_V0_REGNUM + 5, { "v5" } },
626 1.1.1.3 christos { RISCV_V0_REGNUM + 6, { "v6" } },
627 1.1.1.3 christos { RISCV_V0_REGNUM + 7, { "v7" } },
628 1.1.1.3 christos { RISCV_V0_REGNUM + 8, { "v8" } },
629 1.1.1.3 christos { RISCV_V0_REGNUM + 9, { "v9" } },
630 1.1.1.3 christos { RISCV_V0_REGNUM + 10, { "v10" } },
631 1.1.1.3 christos { RISCV_V0_REGNUM + 11, { "v11" } },
632 1.1.1.3 christos { RISCV_V0_REGNUM + 12, { "v12" } },
633 1.1.1.3 christos { RISCV_V0_REGNUM + 13, { "v13" } },
634 1.1.1.3 christos { RISCV_V0_REGNUM + 14, { "v14" } },
635 1.1.1.3 christos { RISCV_V0_REGNUM + 15, { "v15" } },
636 1.1.1.3 christos { RISCV_V0_REGNUM + 16, { "v16" } },
637 1.1.1.3 christos { RISCV_V0_REGNUM + 17, { "v17" } },
638 1.1.1.3 christos { RISCV_V0_REGNUM + 18, { "v18" } },
639 1.1.1.3 christos { RISCV_V0_REGNUM + 19, { "v19" } },
640 1.1.1.3 christos { RISCV_V0_REGNUM + 20, { "v20" } },
641 1.1.1.3 christos { RISCV_V0_REGNUM + 21, { "v21" } },
642 1.1.1.3 christos { RISCV_V0_REGNUM + 22, { "v22" } },
643 1.1.1.3 christos { RISCV_V0_REGNUM + 23, { "v23" } },
644 1.1.1.3 christos { RISCV_V0_REGNUM + 24, { "v24" } },
645 1.1.1.3 christos { RISCV_V0_REGNUM + 25, { "v25" } },
646 1.1.1.3 christos { RISCV_V0_REGNUM + 26, { "v26" } },
647 1.1.1.3 christos { RISCV_V0_REGNUM + 27, { "v27" } },
648 1.1.1.3 christos { RISCV_V0_REGNUM + 28, { "v28" } },
649 1.1.1.3 christos { RISCV_V0_REGNUM + 29, { "v29" } },
650 1.1.1.3 christos { RISCV_V0_REGNUM + 30, { "v30" } },
651 1.1.1.3 christos { RISCV_V0_REGNUM + 31, { "v31" } },
652 1.1.1.3 christos };
653 1.1.1.3 christos }
654 1.1.1.3 christos
655 1.1.1.3 christos /* Return the preferred name for the register with gdb register number
656 1.1.1.3 christos REGNUM, which must be in the inclusive range RISCV_V0_REGNUM to
657 1.1.1.3 christos RISCV_V0_REGNUM + 31. */
658 1.1.1.3 christos const char *register_name (int regnum) const
659 1.1.1.3 christos {
660 1.1.1.3 christos gdb_assert (regnum >= RISCV_V0_REGNUM
661 1.1.1.3 christos && regnum <= RISCV_V0_REGNUM + 31);
662 1.1.1.3 christos regnum -= RISCV_V0_REGNUM;
663 1.1.1.3 christos return m_registers[regnum].names[0];
664 1.1.1.3 christos }
665 1.1.1.3 christos
666 1.1.1.3 christos /* Check this feature within TDESC, record the registers from this
667 1.1.1.3 christos feature into TDESC_DATA and update ALIASES and FEATURES. */
668 1.1.1.3 christos bool check (const struct target_desc *tdesc,
669 1.1.1.3 christos struct tdesc_arch_data *tdesc_data,
670 1.1.1.3 christos std::vector<riscv_pending_register_alias> *aliases,
671 1.1.1.3 christos struct riscv_gdbarch_features *features) const
672 1.1.1.3 christos {
673 1.1.1.3 christos const struct tdesc_feature *feature_vector = tdesc_feature (tdesc);
674 1.1.1.3 christos
675 1.1.1.3 christos /* It's fine if this feature is missing. Update the architecture
676 1.1.1.3 christos feature set and return. */
677 1.1.1.3 christos if (feature_vector == nullptr)
678 1.1.1.3 christos {
679 1.1.1.3 christos features->vlen = 0;
680 1.1.1.3 christos return true;
681 1.1.1.3 christos }
682 1.1.1.3 christos
683 1.1.1.3 christos /* Check all of the vector registers are present. */
684 1.1.1.3 christos for (const auto ® : m_registers)
685 1.1.1.3 christos {
686 1.1.1.3 christos if (!reg.check (tdesc_data, feature_vector, true, aliases))
687 1.1.1.3 christos return false;
688 1.1.1.3 christos }
689 1.1.1.3 christos
690 1.1.1.3 christos /* Look through all of the vector registers and check they all have the
691 1.1.1.3 christos same bitsize. Use this bitsize to update the feature set for this
692 1.1.1.3 christos gdbarch. */
693 1.1.1.3 christos int vector_bitsize = -1;
694 1.1.1.3 christos for (const auto ® : m_registers)
695 1.1.1.3 christos {
696 1.1.1.3 christos int reg_bitsize = -1;
697 1.1.1.3 christos for (const char *name : reg.names)
698 1.1.1.3 christos {
699 1.1.1.3 christos if (tdesc_unnumbered_register (feature_vector, name))
700 1.1.1.3 christos {
701 1.1.1.3 christos reg_bitsize = tdesc_register_bitsize (feature_vector, name);
702 1.1.1.3 christos break;
703 1.1.1.3 christos }
704 1.1.1.3 christos }
705 1.1.1.3 christos gdb_assert (reg_bitsize != -1);
706 1.1.1.3 christos if (vector_bitsize == -1)
707 1.1.1.3 christos vector_bitsize = reg_bitsize;
708 1.1.1.3 christos else if (vector_bitsize != reg_bitsize)
709 1.1.1.3 christos return false;
710 1.1.1.3 christos }
711 1.1.1.3 christos
712 1.1.1.3 christos features->vlen = (vector_bitsize / 8);
713 1.1.1.3 christos return true;
714 1.1.1.3 christos }
715 1.1.1.3 christos };
716 1.1.1.3 christos
717 1.1.1.3 christos /* An instance of the v-register feature set. */
718 1.1.1.3 christos
719 1.1.1.3 christos static const struct riscv_vector_feature riscv_vector_feature;
720 1.1 christos
721 1.1 christos /* Controls whether we place compressed breakpoints or not. When in auto
722 1.1 christos mode GDB tries to determine if the target supports compressed
723 1.1 christos breakpoints, and uses them if it does. */
724 1.1 christos
725 1.1 christos static enum auto_boolean use_compressed_breakpoints;
726 1.1 christos
727 1.1 christos /* The show callback for 'show riscv use-compressed-breakpoints'. */
728 1.1 christos
729 1.1 christos static void
730 1.1 christos show_use_compressed_breakpoints (struct ui_file *file, int from_tty,
731 1.1 christos struct cmd_list_element *c,
732 1.1 christos const char *value)
733 1.1 christos {
734 1.1.1.3 christos gdb_printf (file,
735 1.1.1.3 christos _("Debugger's use of compressed breakpoints is set "
736 1.1.1.3 christos "to %s.\n"), value);
737 1.1 christos }
738 1.1 christos
739 1.1 christos /* The set and show lists for 'set riscv' and 'show riscv' prefixes. */
740 1.1 christos
741 1.1 christos static struct cmd_list_element *setriscvcmdlist = NULL;
742 1.1 christos static struct cmd_list_element *showriscvcmdlist = NULL;
743 1.1 christos
744 1.1 christos /* The set and show lists for 'set riscv' and 'show riscv' prefixes. */
745 1.1 christos
746 1.1 christos static struct cmd_list_element *setdebugriscvcmdlist = NULL;
747 1.1 christos static struct cmd_list_element *showdebugriscvcmdlist = NULL;
748 1.1 christos
749 1.1 christos /* The show callback for all 'show debug riscv VARNAME' variables. */
750 1.1 christos
751 1.1 christos static void
752 1.1 christos show_riscv_debug_variable (struct ui_file *file, int from_tty,
753 1.1 christos struct cmd_list_element *c,
754 1.1 christos const char *value)
755 1.1 christos {
756 1.1.1.3 christos gdb_printf (file,
757 1.1.1.3 christos _("RiscV debug variable `%s' is set to: %s\n"),
758 1.1.1.3 christos c->name, value);
759 1.1 christos }
760 1.1 christos
761 1.1 christos /* See riscv-tdep.h. */
762 1.1 christos
763 1.1 christos int
764 1.1 christos riscv_isa_xlen (struct gdbarch *gdbarch)
765 1.1 christos {
766 1.1.1.3 christos riscv_gdbarch_tdep *tdep = gdbarch_tdep<riscv_gdbarch_tdep> (gdbarch);
767 1.1.1.3 christos return tdep->isa_features.xlen;
768 1.1 christos }
769 1.1 christos
770 1.1 christos /* See riscv-tdep.h. */
771 1.1 christos
772 1.1 christos int
773 1.1 christos riscv_abi_xlen (struct gdbarch *gdbarch)
774 1.1 christos {
775 1.1.1.3 christos riscv_gdbarch_tdep *tdep = gdbarch_tdep<riscv_gdbarch_tdep> (gdbarch);
776 1.1.1.3 christos return tdep->abi_features.xlen;
777 1.1 christos }
778 1.1 christos
779 1.1 christos /* See riscv-tdep.h. */
780 1.1 christos
781 1.1 christos int
782 1.1 christos riscv_isa_flen (struct gdbarch *gdbarch)
783 1.1 christos {
784 1.1.1.3 christos riscv_gdbarch_tdep *tdep = gdbarch_tdep<riscv_gdbarch_tdep> (gdbarch);
785 1.1.1.3 christos return tdep->isa_features.flen;
786 1.1 christos }
787 1.1 christos
788 1.1 christos /* See riscv-tdep.h. */
789 1.1 christos
790 1.1 christos int
791 1.1 christos riscv_abi_flen (struct gdbarch *gdbarch)
792 1.1 christos {
793 1.1.1.3 christos riscv_gdbarch_tdep *tdep = gdbarch_tdep<riscv_gdbarch_tdep> (gdbarch);
794 1.1.1.3 christos return tdep->abi_features.flen;
795 1.1.1.3 christos }
796 1.1.1.3 christos
797 1.1.1.3 christos /* See riscv-tdep.h. */
798 1.1.1.3 christos
799 1.1.1.3 christos bool
800 1.1.1.3 christos riscv_abi_embedded (struct gdbarch *gdbarch)
801 1.1.1.3 christos {
802 1.1.1.3 christos riscv_gdbarch_tdep *tdep = gdbarch_tdep<riscv_gdbarch_tdep> (gdbarch);
803 1.1.1.3 christos return tdep->abi_features.embedded;
804 1.1 christos }
805 1.1 christos
806 1.1 christos /* Return true if the target for GDBARCH has floating point hardware. */
807 1.1 christos
808 1.1 christos static bool
809 1.1 christos riscv_has_fp_regs (struct gdbarch *gdbarch)
810 1.1 christos {
811 1.1 christos return (riscv_isa_flen (gdbarch) > 0);
812 1.1 christos }
813 1.1 christos
814 1.1 christos /* Return true if GDBARCH is using any of the floating point hardware ABIs. */
815 1.1 christos
816 1.1 christos static bool
817 1.1 christos riscv_has_fp_abi (struct gdbarch *gdbarch)
818 1.1 christos {
819 1.1.1.3 christos riscv_gdbarch_tdep *tdep = gdbarch_tdep<riscv_gdbarch_tdep> (gdbarch);
820 1.1.1.3 christos return tdep->abi_features.flen > 0;
821 1.1 christos }
822 1.1 christos
823 1.1 christos /* Return true if REGNO is a floating pointer register. */
824 1.1 christos
825 1.1 christos static bool
826 1.1 christos riscv_is_fp_regno_p (int regno)
827 1.1 christos {
828 1.1 christos return (regno >= RISCV_FIRST_FP_REGNUM
829 1.1 christos && regno <= RISCV_LAST_FP_REGNUM);
830 1.1 christos }
831 1.1 christos
832 1.1 christos /* Implement the breakpoint_kind_from_pc gdbarch method. */
833 1.1 christos
834 1.1 christos static int
835 1.1 christos riscv_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
836 1.1 christos {
837 1.1 christos if (use_compressed_breakpoints == AUTO_BOOLEAN_AUTO)
838 1.1 christos {
839 1.1 christos bool unaligned_p = false;
840 1.1 christos gdb_byte buf[1];
841 1.1 christos
842 1.1 christos /* Some targets don't support unaligned reads. The address can only
843 1.1 christos be unaligned if the C extension is supported. So it is safe to
844 1.1 christos use a compressed breakpoint in this case. */
845 1.1 christos if (*pcptr & 0x2)
846 1.1 christos unaligned_p = true;
847 1.1 christos else
848 1.1 christos {
849 1.1.1.2 christos /* Read the opcode byte to determine the instruction length. If
850 1.1.1.2 christos the read fails this may be because we tried to set the
851 1.1.1.2 christos breakpoint at an invalid address, in this case we provide a
852 1.1.1.2 christos fake result which will give a breakpoint length of 4.
853 1.1.1.2 christos Hopefully when we try to actually insert the breakpoint we
854 1.1.1.2 christos will see a failure then too which will be reported to the
855 1.1.1.2 christos user. */
856 1.1.1.2 christos if (target_read_code (*pcptr, buf, 1) == -1)
857 1.1.1.2 christos buf[0] = 0;
858 1.1 christos }
859 1.1 christos
860 1.1 christos if (riscv_debug_breakpoints)
861 1.1 christos {
862 1.1 christos const char *bp = (unaligned_p || riscv_insn_length (buf[0]) == 2
863 1.1 christos ? "C.EBREAK" : "EBREAK");
864 1.1 christos
865 1.1.1.4 christos std::string suffix;
866 1.1 christos if (unaligned_p)
867 1.1.1.4 christos suffix = "(unaligned address)";
868 1.1 christos else
869 1.1.1.4 christos suffix = string_printf ("(instruction length %d)",
870 1.1.1.4 christos riscv_insn_length (buf[0]));
871 1.1.1.4 christos riscv_breakpoints_debug_printf ("Using %s for breakpoint at %s %s",
872 1.1.1.4 christos bp, paddress (gdbarch, *pcptr),
873 1.1.1.4 christos suffix.c_str ());
874 1.1 christos }
875 1.1 christos if (unaligned_p || riscv_insn_length (buf[0]) == 2)
876 1.1 christos return 2;
877 1.1 christos else
878 1.1 christos return 4;
879 1.1 christos }
880 1.1 christos else if (use_compressed_breakpoints == AUTO_BOOLEAN_TRUE)
881 1.1 christos return 2;
882 1.1 christos else
883 1.1 christos return 4;
884 1.1 christos }
885 1.1 christos
886 1.1 christos /* Implement the sw_breakpoint_from_kind gdbarch method. */
887 1.1 christos
888 1.1 christos static const gdb_byte *
889 1.1 christos riscv_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
890 1.1 christos {
891 1.1 christos static const gdb_byte ebreak[] = { 0x73, 0x00, 0x10, 0x00, };
892 1.1 christos static const gdb_byte c_ebreak[] = { 0x02, 0x90 };
893 1.1 christos
894 1.1 christos *size = kind;
895 1.1 christos switch (kind)
896 1.1 christos {
897 1.1 christos case 2:
898 1.1 christos return c_ebreak;
899 1.1 christos case 4:
900 1.1 christos return ebreak;
901 1.1 christos default:
902 1.1.1.3 christos gdb_assert_not_reached ("unhandled breakpoint kind");
903 1.1 christos }
904 1.1 christos }
905 1.1 christos
906 1.1 christos /* Implement the register_name gdbarch method. This is used instead of
907 1.1 christos the function supplied by calling TDESC_USE_REGISTERS so that we can
908 1.1.1.2 christos ensure the preferred names are offered for x-regs and f-regs. */
909 1.1 christos
910 1.1 christos static const char *
911 1.1 christos riscv_register_name (struct gdbarch *gdbarch, int regnum)
912 1.1 christos {
913 1.1 christos /* Lookup the name through the target description. If we get back NULL
914 1.1 christos then this is an unknown register. If we do get a name back then we
915 1.1 christos look up the registers preferred name below. */
916 1.1 christos const char *name = tdesc_register_name (gdbarch, regnum);
917 1.1.1.3 christos gdb_assert (name != nullptr);
918 1.1.1.3 christos if (name[0] == '\0')
919 1.1.1.3 christos return name;
920 1.1 christos
921 1.1.1.2 christos /* We want GDB to use the ABI names for registers even if the target
922 1.1.1.2 christos gives us a target description with the architectural name. For
923 1.1.1.2 christos example we want to see 'ra' instead of 'x1' whatever the target
924 1.1.1.2 christos description called it. */
925 1.1 christos if (regnum >= RISCV_ZERO_REGNUM && regnum < RISCV_FIRST_FP_REGNUM)
926 1.1.1.3 christos return riscv_xreg_feature.register_name (regnum);
927 1.1 christos
928 1.1.1.2 christos /* Like with the x-regs we prefer the abi names for the floating point
929 1.1.1.3 christos registers. If the target doesn't have floating point registers then
930 1.1.1.3 christos the tdesc_register_name call above should have returned an empty
931 1.1.1.3 christos string. */
932 1.1 christos if (regnum >= RISCV_FIRST_FP_REGNUM && regnum <= RISCV_LAST_FP_REGNUM)
933 1.1 christos {
934 1.1.1.3 christos gdb_assert (riscv_has_fp_regs (gdbarch));
935 1.1.1.3 christos return riscv_freg_feature.register_name (regnum);
936 1.1 christos }
937 1.1 christos
938 1.1.1.2 christos /* Some targets (QEMU) are reporting these three registers twice, once
939 1.1.1.2 christos in the FPU feature, and once in the CSR feature. Both of these read
940 1.1.1.2 christos the same underlying state inside the target, but naming the register
941 1.1.1.2 christos twice in the target description results in GDB having two registers
942 1.1.1.2 christos with the same name, only one of which can ever be accessed, but both
943 1.1.1.2 christos will show up in 'info register all'. Unless, we identify the
944 1.1.1.2 christos duplicate copies of these registers (in riscv_tdesc_unknown_reg) and
945 1.1.1.2 christos then hide the registers here by giving them no name. */
946 1.1.1.3 christos riscv_gdbarch_tdep *tdep = gdbarch_tdep<riscv_gdbarch_tdep> (gdbarch);
947 1.1.1.3 christos if (tdep->duplicate_fflags_regnum == regnum
948 1.1.1.3 christos || tdep->duplicate_frm_regnum == regnum
949 1.1.1.3 christos || tdep->duplicate_fcsr_regnum == regnum)
950 1.1.1.3 christos return "";
951 1.1 christos
952 1.1.1.2 christos /* The remaining registers are different. For all other registers on the
953 1.1.1.2 christos machine we prefer to see the names that the target description
954 1.1.1.2 christos provides. This is particularly important for CSRs which might be
955 1.1.1.2 christos renamed over time. If GDB keeps track of the "latest" name, but a
956 1.1.1.2 christos particular target provides an older name then we don't want to force
957 1.1.1.2 christos users to see the newer name in register output.
958 1.1.1.2 christos
959 1.1.1.2 christos The other case that reaches here are any registers that the target
960 1.1.1.2 christos provided that GDB is completely unaware of. For these we have no
961 1.1.1.2 christos choice but to accept the target description name.
962 1.1 christos
963 1.1.1.2 christos Just accept whatever name TDESC_REGISTER_NAME returned. */
964 1.1 christos return name;
965 1.1 christos }
966 1.1 christos
967 1.1.1.3 christos /* Implement gdbarch_pseudo_register_read. Read pseudo-register REGNUM
968 1.1.1.3 christos from REGCACHE and place the register value into BUF. BUF is sized
969 1.1.1.3 christos based on the type of register REGNUM, all of BUF should be written too,
970 1.1.1.3 christos the result should be sign or zero extended as appropriate. */
971 1.1.1.3 christos
972 1.1.1.3 christos static enum register_status
973 1.1.1.3 christos riscv_pseudo_register_read (struct gdbarch *gdbarch,
974 1.1.1.3 christos readable_regcache *regcache,
975 1.1.1.3 christos int regnum, gdb_byte *buf)
976 1.1.1.3 christos {
977 1.1.1.3 christos riscv_gdbarch_tdep *tdep = gdbarch_tdep<riscv_gdbarch_tdep> (gdbarch);
978 1.1.1.3 christos
979 1.1.1.3 christos if (regnum == tdep->fflags_regnum || regnum == tdep->frm_regnum)
980 1.1.1.3 christos {
981 1.1.1.3 christos /* Clear BUF. */
982 1.1.1.3 christos memset (buf, 0, register_size (gdbarch, regnum));
983 1.1.1.3 christos
984 1.1.1.3 christos /* Read the first byte of the fcsr register, this contains both frm
985 1.1.1.3 christos and fflags. */
986 1.1.1.3 christos enum register_status status
987 1.1.1.3 christos = regcache->raw_read_part (RISCV_CSR_FCSR_REGNUM, 0, 1, buf);
988 1.1.1.3 christos
989 1.1.1.3 christos if (status != REG_VALID)
990 1.1.1.3 christos return status;
991 1.1.1.3 christos
992 1.1.1.3 christos /* Extract the appropriate parts. */
993 1.1.1.3 christos if (regnum == tdep->fflags_regnum)
994 1.1.1.3 christos buf[0] &= 0x1f;
995 1.1.1.3 christos else if (regnum == tdep->frm_regnum)
996 1.1.1.3 christos buf[0] = (buf[0] >> 5) & 0x7;
997 1.1.1.3 christos
998 1.1.1.3 christos return REG_VALID;
999 1.1.1.3 christos }
1000 1.1.1.3 christos
1001 1.1.1.3 christos return REG_UNKNOWN;
1002 1.1.1.3 christos }
1003 1.1.1.3 christos
1004 1.1.1.4 christos /* Implement gdbarch_deprecated_pseudo_register_write. Write the contents of
1005 1.1.1.4 christos BUF into pseudo-register REGNUM in REGCACHE. BUF is sized based on the type
1006 1.1.1.4 christos of register REGNUM. */
1007 1.1.1.3 christos
1008 1.1.1.3 christos static void
1009 1.1.1.3 christos riscv_pseudo_register_write (struct gdbarch *gdbarch,
1010 1.1.1.3 christos struct regcache *regcache, int regnum,
1011 1.1.1.3 christos const gdb_byte *buf)
1012 1.1.1.3 christos {
1013 1.1.1.3 christos riscv_gdbarch_tdep *tdep = gdbarch_tdep<riscv_gdbarch_tdep> (gdbarch);
1014 1.1.1.3 christos
1015 1.1.1.3 christos if (regnum == tdep->fflags_regnum || regnum == tdep->frm_regnum)
1016 1.1.1.3 christos {
1017 1.1.1.3 christos int fcsr_regnum = RISCV_CSR_FCSR_REGNUM;
1018 1.1.1.5 christos gdb::byte_vector raw_buf (register_size (gdbarch, fcsr_regnum));
1019 1.1.1.3 christos
1020 1.1.1.3 christos regcache->raw_read (fcsr_regnum, raw_buf);
1021 1.1.1.3 christos
1022 1.1.1.3 christos if (regnum == tdep->fflags_regnum)
1023 1.1.1.3 christos raw_buf[0] = (raw_buf[0] & ~0x1f) | (buf[0] & 0x1f);
1024 1.1.1.3 christos else if (regnum == tdep->frm_regnum)
1025 1.1.1.3 christos raw_buf[0] = (raw_buf[0] & ~(0x7 << 5)) | ((buf[0] & 0x7) << 5);
1026 1.1.1.3 christos
1027 1.1.1.3 christos regcache->raw_write (fcsr_regnum, raw_buf);
1028 1.1.1.3 christos }
1029 1.1.1.3 christos else
1030 1.1.1.3 christos gdb_assert_not_reached ("unknown pseudo register %d", regnum);
1031 1.1.1.3 christos }
1032 1.1.1.3 christos
1033 1.1.1.3 christos /* Implement the cannot_store_register gdbarch method. The zero register
1034 1.1.1.3 christos (x0) is read-only on RISC-V. */
1035 1.1.1.3 christos
1036 1.1.1.3 christos static int
1037 1.1.1.3 christos riscv_cannot_store_register (struct gdbarch *gdbarch, int regnum)
1038 1.1.1.3 christos {
1039 1.1.1.3 christos return regnum == RISCV_ZERO_REGNUM;
1040 1.1.1.3 christos }
1041 1.1.1.3 christos
1042 1.1 christos /* Construct a type for 64-bit FP registers. */
1043 1.1 christos
1044 1.1 christos static struct type *
1045 1.1 christos riscv_fpreg_d_type (struct gdbarch *gdbarch)
1046 1.1 christos {
1047 1.1.1.3 christos riscv_gdbarch_tdep *tdep = gdbarch_tdep<riscv_gdbarch_tdep> (gdbarch);
1048 1.1 christos
1049 1.1 christos if (tdep->riscv_fpreg_d_type == nullptr)
1050 1.1 christos {
1051 1.1 christos const struct builtin_type *bt = builtin_type (gdbarch);
1052 1.1 christos
1053 1.1 christos /* The type we're building is this: */
1054 1.1 christos #if 0
1055 1.1 christos union __gdb_builtin_type_fpreg_d
1056 1.1 christos {
1057 1.1 christos float f;
1058 1.1 christos double d;
1059 1.1 christos };
1060 1.1 christos #endif
1061 1.1 christos
1062 1.1 christos struct type *t;
1063 1.1 christos
1064 1.1 christos t = arch_composite_type (gdbarch,
1065 1.1 christos "__gdb_builtin_type_fpreg_d", TYPE_CODE_UNION);
1066 1.1 christos append_composite_type_field (t, "float", bt->builtin_float);
1067 1.1 christos append_composite_type_field (t, "double", bt->builtin_double);
1068 1.1.1.3 christos t->set_is_vector (true);
1069 1.1.1.2 christos t->set_name ("builtin_type_fpreg_d");
1070 1.1 christos tdep->riscv_fpreg_d_type = t;
1071 1.1 christos }
1072 1.1 christos
1073 1.1 christos return tdep->riscv_fpreg_d_type;
1074 1.1 christos }
1075 1.1 christos
1076 1.1 christos /* Implement the register_type gdbarch method. This is installed as an
1077 1.1 christos for the override setup by TDESC_USE_REGISTERS, for most registers we
1078 1.1 christos delegate the type choice to the target description, but for a few
1079 1.1 christos registers we try to improve the types if the target description has
1080 1.1 christos taken a simplistic approach. */
1081 1.1 christos
1082 1.1 christos static struct type *
1083 1.1 christos riscv_register_type (struct gdbarch *gdbarch, int regnum)
1084 1.1 christos {
1085 1.1 christos struct type *type = tdesc_register_type (gdbarch, regnum);
1086 1.1 christos int xlen = riscv_isa_xlen (gdbarch);
1087 1.1 christos
1088 1.1 christos /* We want to perform some specific type "fixes" in cases where we feel
1089 1.1 christos that we really can do better than the target description. For all
1090 1.1 christos other cases we just return what the target description says. */
1091 1.1 christos if (riscv_is_fp_regno_p (regnum))
1092 1.1 christos {
1093 1.1 christos /* This spots the case for RV64 where the double is defined as
1094 1.1.1.3 christos either 'ieee_double' or 'float' (which is the generic name that
1095 1.1.1.3 christos converts to 'double' on 64-bit). In these cases its better to
1096 1.1.1.3 christos present the registers using a union type. */
1097 1.1 christos int flen = riscv_isa_flen (gdbarch);
1098 1.1 christos if (flen == 8
1099 1.1.1.3 christos && type->code () == TYPE_CODE_FLT
1100 1.1.1.3 christos && type->length () == flen
1101 1.1.1.3 christos && (strcmp (type->name (), "builtin_type_ieee_double") == 0
1102 1.1.1.3 christos || strcmp (type->name (), "double") == 0))
1103 1.1.1.3 christos type = riscv_fpreg_d_type (gdbarch);
1104 1.1 christos }
1105 1.1 christos
1106 1.1 christos if ((regnum == gdbarch_pc_regnum (gdbarch)
1107 1.1 christos || regnum == RISCV_RA_REGNUM
1108 1.1 christos || regnum == RISCV_FP_REGNUM
1109 1.1 christos || regnum == RISCV_SP_REGNUM
1110 1.1 christos || regnum == RISCV_GP_REGNUM
1111 1.1 christos || regnum == RISCV_TP_REGNUM)
1112 1.1.1.2 christos && type->code () == TYPE_CODE_INT
1113 1.1.1.3 christos && type->length () == xlen)
1114 1.1 christos {
1115 1.1 christos /* This spots the case where some interesting registers are defined
1116 1.1.1.3 christos as simple integers of the expected size, we force these registers
1117 1.1.1.3 christos to be pointers as we believe that is more useful. */
1118 1.1 christos if (regnum == gdbarch_pc_regnum (gdbarch)
1119 1.1.1.3 christos || regnum == RISCV_RA_REGNUM)
1120 1.1.1.3 christos type = builtin_type (gdbarch)->builtin_func_ptr;
1121 1.1 christos else if (regnum == RISCV_FP_REGNUM
1122 1.1.1.3 christos || regnum == RISCV_SP_REGNUM
1123 1.1.1.3 christos || regnum == RISCV_GP_REGNUM
1124 1.1.1.3 christos || regnum == RISCV_TP_REGNUM)
1125 1.1 christos type = builtin_type (gdbarch)->builtin_data_ptr;
1126 1.1 christos }
1127 1.1 christos
1128 1.1 christos return type;
1129 1.1 christos }
1130 1.1 christos
1131 1.1 christos /* Helper for riscv_print_registers_info, prints info for a single register
1132 1.1 christos REGNUM. */
1133 1.1 christos
1134 1.1 christos static void
1135 1.1 christos riscv_print_one_register_info (struct gdbarch *gdbarch,
1136 1.1 christos struct ui_file *file,
1137 1.1.1.4 christos const frame_info_ptr &frame,
1138 1.1 christos int regnum)
1139 1.1 christos {
1140 1.1 christos const char *name = gdbarch_register_name (gdbarch, regnum);
1141 1.1 christos struct value *val;
1142 1.1 christos struct type *regtype;
1143 1.1 christos int print_raw_format;
1144 1.1 christos enum tab_stops { value_column_1 = 15 };
1145 1.1 christos
1146 1.1.1.3 christos gdb_puts (name, file);
1147 1.1.1.4 christos print_spaces (std::max<int> (1, value_column_1 - strlen (name)), file);
1148 1.1 christos
1149 1.1.1.2 christos try
1150 1.1 christos {
1151 1.1.1.4 christos val = value_of_register (regnum, get_next_frame_sentinel_okay (frame));
1152 1.1.1.4 christos regtype = val->type ();
1153 1.1 christos }
1154 1.1.1.2 christos catch (const gdb_exception_error &ex)
1155 1.1 christos {
1156 1.1 christos /* Handle failure to read a register without interrupting the entire
1157 1.1.1.3 christos 'info registers' flow. */
1158 1.1.1.3 christos gdb_printf (file, "%s\n", ex.what ());
1159 1.1 christos return;
1160 1.1 christos }
1161 1.1 christos
1162 1.1.1.4 christos print_raw_format = (val->entirely_available ()
1163 1.1.1.4 christos && !val->optimized_out ());
1164 1.1 christos
1165 1.1.1.2 christos if (regtype->code () == TYPE_CODE_FLT
1166 1.1.1.2 christos || (regtype->code () == TYPE_CODE_UNION
1167 1.1.1.2 christos && regtype->num_fields () == 2
1168 1.1.1.2 christos && regtype->field (0).type ()->code () == TYPE_CODE_FLT
1169 1.1.1.2 christos && regtype->field (1).type ()->code () == TYPE_CODE_FLT)
1170 1.1.1.2 christos || (regtype->code () == TYPE_CODE_UNION
1171 1.1.1.2 christos && regtype->num_fields () == 3
1172 1.1.1.2 christos && regtype->field (0).type ()->code () == TYPE_CODE_FLT
1173 1.1.1.2 christos && regtype->field (1).type ()->code () == TYPE_CODE_FLT
1174 1.1.1.2 christos && regtype->field (2).type ()->code () == TYPE_CODE_FLT))
1175 1.1 christos {
1176 1.1 christos struct value_print_options opts;
1177 1.1.1.4 christos const gdb_byte *valaddr = val->contents_for_printing ().data ();
1178 1.1.1.2 christos enum bfd_endian byte_order = type_byte_order (regtype);
1179 1.1 christos
1180 1.1 christos get_user_print_options (&opts);
1181 1.1.1.4 christos opts.deref_ref = true;
1182 1.1 christos
1183 1.1.1.2 christos common_val_print (val, file, 0, &opts, current_language);
1184 1.1 christos
1185 1.1 christos if (print_raw_format)
1186 1.1 christos {
1187 1.1.1.3 christos gdb_printf (file, "\t(raw ");
1188 1.1.1.3 christos print_hex_chars (file, valaddr, regtype->length (), byte_order,
1189 1.1 christos true);
1190 1.1.1.3 christos gdb_printf (file, ")");
1191 1.1 christos }
1192 1.1 christos }
1193 1.1 christos else
1194 1.1 christos {
1195 1.1 christos struct value_print_options opts;
1196 1.1.1.3 christos riscv_gdbarch_tdep *tdep = gdbarch_tdep<riscv_gdbarch_tdep> (gdbarch);
1197 1.1 christos
1198 1.1 christos /* Print the register in hex. */
1199 1.1 christos get_formatted_print_options (&opts, 'x');
1200 1.1.1.4 christos opts.deref_ref = true;
1201 1.1.1.2 christos common_val_print (val, file, 0, &opts, current_language);
1202 1.1 christos
1203 1.1 christos if (print_raw_format)
1204 1.1 christos {
1205 1.1 christos if (regnum == RISCV_CSR_MSTATUS_REGNUM)
1206 1.1 christos {
1207 1.1 christos LONGEST d;
1208 1.1 christos int size = register_size (gdbarch, regnum);
1209 1.1 christos unsigned xlen;
1210 1.1 christos
1211 1.1 christos /* The SD field is always in the upper bit of MSTATUS, regardless
1212 1.1 christos of the number of bits in MSTATUS. */
1213 1.1 christos d = value_as_long (val);
1214 1.1 christos xlen = size * 8;
1215 1.1.1.3 christos gdb_printf (file,
1216 1.1.1.3 christos "\tSD:%X VM:%02X MXR:%X PUM:%X MPRV:%X XS:%X "
1217 1.1.1.3 christos "FS:%X MPP:%x HPP:%X SPP:%X MPIE:%X HPIE:%X "
1218 1.1.1.3 christos "SPIE:%X UPIE:%X MIE:%X HIE:%X SIE:%X UIE:%X",
1219 1.1.1.3 christos (int) ((d >> (xlen - 1)) & 0x1),
1220 1.1.1.3 christos (int) ((d >> 24) & 0x1f),
1221 1.1.1.3 christos (int) ((d >> 19) & 0x1),
1222 1.1.1.3 christos (int) ((d >> 18) & 0x1),
1223 1.1.1.3 christos (int) ((d >> 17) & 0x1),
1224 1.1.1.3 christos (int) ((d >> 15) & 0x3),
1225 1.1.1.3 christos (int) ((d >> 13) & 0x3),
1226 1.1.1.3 christos (int) ((d >> 11) & 0x3),
1227 1.1.1.3 christos (int) ((d >> 9) & 0x3),
1228 1.1.1.3 christos (int) ((d >> 8) & 0x1),
1229 1.1.1.3 christos (int) ((d >> 7) & 0x1),
1230 1.1.1.3 christos (int) ((d >> 6) & 0x1),
1231 1.1.1.3 christos (int) ((d >> 5) & 0x1),
1232 1.1.1.3 christos (int) ((d >> 4) & 0x1),
1233 1.1.1.3 christos (int) ((d >> 3) & 0x1),
1234 1.1.1.3 christos (int) ((d >> 2) & 0x1),
1235 1.1.1.3 christos (int) ((d >> 1) & 0x1),
1236 1.1.1.3 christos (int) ((d >> 0) & 0x1));
1237 1.1 christos }
1238 1.1 christos else if (regnum == RISCV_CSR_MISA_REGNUM)
1239 1.1 christos {
1240 1.1 christos int base;
1241 1.1 christos unsigned xlen, i;
1242 1.1 christos LONGEST d;
1243 1.1 christos int size = register_size (gdbarch, regnum);
1244 1.1 christos
1245 1.1 christos /* The MXL field is always in the upper two bits of MISA,
1246 1.1 christos regardless of the number of bits in MISA. Mask out other
1247 1.1 christos bits to ensure we have a positive value. */
1248 1.1 christos d = value_as_long (val);
1249 1.1 christos base = (d >> ((size * 8) - 2)) & 0x3;
1250 1.1 christos xlen = 16;
1251 1.1 christos
1252 1.1 christos for (; base > 0; base--)
1253 1.1 christos xlen *= 2;
1254 1.1.1.3 christos gdb_printf (file, "\tRV%d", xlen);
1255 1.1 christos
1256 1.1 christos for (i = 0; i < 26; i++)
1257 1.1 christos {
1258 1.1 christos if (d & (1 << i))
1259 1.1.1.3 christos gdb_printf (file, "%c", 'A' + i);
1260 1.1 christos }
1261 1.1 christos }
1262 1.1 christos else if (regnum == RISCV_CSR_FCSR_REGNUM
1263 1.1.1.3 christos || regnum == tdep->fflags_regnum
1264 1.1.1.3 christos || regnum == tdep->frm_regnum)
1265 1.1 christos {
1266 1.1.1.3 christos LONGEST d = value_as_long (val);
1267 1.1 christos
1268 1.1.1.3 christos gdb_printf (file, "\t");
1269 1.1.1.3 christos if (regnum != tdep->frm_regnum)
1270 1.1.1.3 christos gdb_printf (file,
1271 1.1.1.3 christos "NV:%d DZ:%d OF:%d UF:%d NX:%d",
1272 1.1.1.3 christos (int) ((d >> 4) & 0x1),
1273 1.1.1.3 christos (int) ((d >> 3) & 0x1),
1274 1.1.1.3 christos (int) ((d >> 2) & 0x1),
1275 1.1.1.3 christos (int) ((d >> 1) & 0x1),
1276 1.1.1.3 christos (int) ((d >> 0) & 0x1));
1277 1.1 christos
1278 1.1.1.3 christos if (regnum != tdep->fflags_regnum)
1279 1.1 christos {
1280 1.1 christos static const char * const sfrm[] =
1281 1.1 christos {
1282 1.1.1.3 christos _("RNE (round to nearest; ties to even)"),
1283 1.1.1.3 christos _("RTZ (Round towards zero)"),
1284 1.1.1.3 christos _("RDN (Round down towards -INF)"),
1285 1.1.1.3 christos _("RUP (Round up towards +INF)"),
1286 1.1.1.3 christos _("RMM (Round to nearest; ties to max magnitude)"),
1287 1.1.1.3 christos _("INVALID[5]"),
1288 1.1.1.3 christos _("INVALID[6]"),
1289 1.1.1.3 christos /* A value of 0x7 indicates dynamic rounding mode when
1290 1.1.1.3 christos used within an instructions rounding-mode field, but
1291 1.1.1.3 christos is invalid within the FRM register. */
1292 1.1.1.3 christos _("INVALID[7] (Dynamic rounding mode)"),
1293 1.1 christos };
1294 1.1 christos int frm = ((regnum == RISCV_CSR_FCSR_REGNUM)
1295 1.1.1.3 christos ? (d >> 5) : d) & 0x7;
1296 1.1 christos
1297 1.1.1.3 christos gdb_printf (file, "%sFRM:%i [%s]",
1298 1.1.1.3 christos (regnum == RISCV_CSR_FCSR_REGNUM
1299 1.1.1.3 christos ? " " : ""),
1300 1.1.1.3 christos frm, sfrm[frm]);
1301 1.1 christos }
1302 1.1 christos }
1303 1.1 christos else if (regnum == RISCV_PRIV_REGNUM)
1304 1.1 christos {
1305 1.1 christos LONGEST d;
1306 1.1 christos uint8_t priv;
1307 1.1 christos
1308 1.1 christos d = value_as_long (val);
1309 1.1 christos priv = d & 0xff;
1310 1.1 christos
1311 1.1 christos if (priv < 4)
1312 1.1 christos {
1313 1.1 christos static const char * const sprv[] =
1314 1.1 christos {
1315 1.1 christos "User/Application",
1316 1.1 christos "Supervisor",
1317 1.1 christos "Hypervisor",
1318 1.1 christos "Machine"
1319 1.1 christos };
1320 1.1.1.3 christos gdb_printf (file, "\tprv:%d [%s]",
1321 1.1.1.3 christos priv, sprv[priv]);
1322 1.1 christos }
1323 1.1 christos else
1324 1.1.1.3 christos gdb_printf (file, "\tprv:%d [INVALID]", priv);
1325 1.1 christos }
1326 1.1 christos else
1327 1.1 christos {
1328 1.1 christos /* If not a vector register, print it also according to its
1329 1.1 christos natural format. */
1330 1.1.1.3 christos if (regtype->is_vector () == 0)
1331 1.1 christos {
1332 1.1 christos get_user_print_options (&opts);
1333 1.1.1.4 christos opts.deref_ref = true;
1334 1.1.1.3 christos gdb_printf (file, "\t");
1335 1.1.1.2 christos common_val_print (val, file, 0, &opts, current_language);
1336 1.1 christos }
1337 1.1 christos }
1338 1.1 christos }
1339 1.1 christos }
1340 1.1.1.3 christos gdb_printf (file, "\n");
1341 1.1 christos }
1342 1.1 christos
1343 1.1 christos /* Return true if REGNUM is a valid CSR register. The CSR register space
1344 1.1 christos is sparsely populated, so not every number is a named CSR. */
1345 1.1 christos
1346 1.1 christos static bool
1347 1.1 christos riscv_is_regnum_a_named_csr (int regnum)
1348 1.1 christos {
1349 1.1 christos gdb_assert (regnum >= RISCV_FIRST_CSR_REGNUM
1350 1.1 christos && regnum <= RISCV_LAST_CSR_REGNUM);
1351 1.1 christos
1352 1.1 christos switch (regnum)
1353 1.1 christos {
1354 1.1.1.2 christos #define DECLARE_CSR(name, num, class, define_ver, abort_ver) case RISCV_ ## num ## _REGNUM:
1355 1.1 christos #include "opcode/riscv-opc.h"
1356 1.1 christos #undef DECLARE_CSR
1357 1.1 christos return true;
1358 1.1 christos
1359 1.1 christos default:
1360 1.1 christos return false;
1361 1.1 christos }
1362 1.1 christos }
1363 1.1 christos
1364 1.1.1.3 christos /* Return true if REGNUM is an unknown CSR identified in
1365 1.1.1.3 christos riscv_tdesc_unknown_reg for GDBARCH. */
1366 1.1.1.3 christos
1367 1.1.1.3 christos static bool
1368 1.1.1.3 christos riscv_is_unknown_csr (struct gdbarch *gdbarch, int regnum)
1369 1.1.1.3 christos {
1370 1.1.1.3 christos riscv_gdbarch_tdep *tdep = gdbarch_tdep<riscv_gdbarch_tdep> (gdbarch);
1371 1.1.1.3 christos return (regnum >= tdep->unknown_csrs_first_regnum
1372 1.1.1.3 christos && regnum < (tdep->unknown_csrs_first_regnum
1373 1.1.1.3 christos + tdep->unknown_csrs_count));
1374 1.1.1.3 christos }
1375 1.1.1.3 christos
1376 1.1 christos /* Implement the register_reggroup_p gdbarch method. Is REGNUM a member
1377 1.1 christos of REGGROUP? */
1378 1.1 christos
1379 1.1 christos static int
1380 1.1 christos riscv_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
1381 1.1.1.3 christos const struct reggroup *reggroup)
1382 1.1 christos {
1383 1.1.1.3 christos riscv_gdbarch_tdep *tdep = gdbarch_tdep<riscv_gdbarch_tdep> (gdbarch);
1384 1.1.1.3 christos
1385 1.1 christos /* Used by 'info registers' and 'info registers <groupname>'. */
1386 1.1 christos
1387 1.1.1.3 christos if (gdbarch_register_name (gdbarch, regnum)[0] == '\0')
1388 1.1 christos return 0;
1389 1.1 christos
1390 1.1.1.3 christos if (regnum > RISCV_LAST_REGNUM && regnum < gdbarch_num_regs (gdbarch))
1391 1.1 christos {
1392 1.1.1.2 christos /* Any extra registers from the CSR tdesc_feature (identified in
1393 1.1.1.2 christos riscv_tdesc_unknown_reg) are removed from the save/restore groups
1394 1.1.1.3 christos as some targets (QEMU) report CSRs which then can't be read and
1395 1.1.1.3 christos having unreadable registers in the save/restore group breaks
1396 1.1.1.3 christos things like inferior calls.
1397 1.1.1.3 christos
1398 1.1.1.3 christos The unknown CSRs are also removed from the general group, and
1399 1.1.1.3 christos added into both the csr and system group. This is inline with the
1400 1.1.1.3 christos known CSRs (see below). */
1401 1.1.1.3 christos if (riscv_is_unknown_csr (gdbarch, regnum))
1402 1.1.1.3 christos {
1403 1.1.1.3 christos if (reggroup == restore_reggroup || reggroup == save_reggroup
1404 1.1.1.3 christos || reggroup == general_reggroup)
1405 1.1.1.3 christos return 0;
1406 1.1.1.3 christos else if (reggroup == system_reggroup || reggroup == csr_reggroup)
1407 1.1.1.3 christos return 1;
1408 1.1.1.3 christos }
1409 1.1.1.2 christos
1410 1.1.1.2 christos /* This is some other unknown register from the target description.
1411 1.1.1.2 christos In this case we trust whatever the target description says about
1412 1.1.1.2 christos which groups this register should be in. */
1413 1.1 christos int ret = tdesc_register_in_reggroup_p (gdbarch, regnum, reggroup);
1414 1.1 christos if (ret != -1)
1415 1.1.1.3 christos return ret;
1416 1.1 christos
1417 1.1 christos return default_register_reggroup_p (gdbarch, regnum, reggroup);
1418 1.1 christos }
1419 1.1 christos
1420 1.1 christos if (reggroup == all_reggroup)
1421 1.1 christos {
1422 1.1.1.3 christos if (regnum < RISCV_FIRST_CSR_REGNUM || regnum >= RISCV_PRIV_REGNUM)
1423 1.1 christos return 1;
1424 1.1 christos if (riscv_is_regnum_a_named_csr (regnum))
1425 1.1.1.3 christos return 1;
1426 1.1 christos return 0;
1427 1.1 christos }
1428 1.1 christos else if (reggroup == float_reggroup)
1429 1.1 christos return (riscv_is_fp_regno_p (regnum)
1430 1.1 christos || regnum == RISCV_CSR_FCSR_REGNUM
1431 1.1.1.3 christos || regnum == tdep->fflags_regnum
1432 1.1.1.3 christos || regnum == tdep->frm_regnum);
1433 1.1 christos else if (reggroup == general_reggroup)
1434 1.1 christos return regnum < RISCV_FIRST_FP_REGNUM;
1435 1.1 christos else if (reggroup == restore_reggroup || reggroup == save_reggroup)
1436 1.1 christos {
1437 1.1 christos if (riscv_has_fp_regs (gdbarch))
1438 1.1 christos return (regnum <= RISCV_LAST_FP_REGNUM
1439 1.1 christos || regnum == RISCV_CSR_FCSR_REGNUM
1440 1.1.1.3 christos || regnum == tdep->fflags_regnum
1441 1.1.1.3 christos || regnum == tdep->frm_regnum);
1442 1.1 christos else
1443 1.1 christos return regnum < RISCV_FIRST_FP_REGNUM;
1444 1.1 christos }
1445 1.1 christos else if (reggroup == system_reggroup || reggroup == csr_reggroup)
1446 1.1 christos {
1447 1.1 christos if (regnum == RISCV_PRIV_REGNUM)
1448 1.1 christos return 1;
1449 1.1 christos if (regnum < RISCV_FIRST_CSR_REGNUM || regnum > RISCV_LAST_CSR_REGNUM)
1450 1.1 christos return 0;
1451 1.1 christos if (riscv_is_regnum_a_named_csr (regnum))
1452 1.1.1.3 christos return 1;
1453 1.1 christos return 0;
1454 1.1 christos }
1455 1.1 christos else if (reggroup == vector_reggroup)
1456 1.1.1.3 christos return (regnum >= RISCV_V0_REGNUM && regnum <= RISCV_V31_REGNUM);
1457 1.1 christos else
1458 1.1 christos return 0;
1459 1.1 christos }
1460 1.1 christos
1461 1.1.1.3 christos /* Return the name for pseudo-register REGNUM for GDBARCH. */
1462 1.1.1.3 christos
1463 1.1.1.3 christos static const char *
1464 1.1.1.3 christos riscv_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
1465 1.1.1.3 christos {
1466 1.1.1.3 christos riscv_gdbarch_tdep *tdep = gdbarch_tdep<riscv_gdbarch_tdep> (gdbarch);
1467 1.1.1.3 christos
1468 1.1.1.3 christos if (regnum == tdep->fflags_regnum)
1469 1.1.1.3 christos return "fflags";
1470 1.1.1.3 christos else if (regnum == tdep->frm_regnum)
1471 1.1.1.3 christos return "frm";
1472 1.1.1.3 christos else
1473 1.1.1.3 christos gdb_assert_not_reached ("unknown pseudo register number %d", regnum);
1474 1.1.1.3 christos }
1475 1.1.1.3 christos
1476 1.1.1.3 christos /* Return the type for pseudo-register REGNUM for GDBARCH. */
1477 1.1.1.3 christos
1478 1.1.1.3 christos static struct type *
1479 1.1.1.3 christos riscv_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
1480 1.1.1.3 christos {
1481 1.1.1.3 christos riscv_gdbarch_tdep *tdep = gdbarch_tdep<riscv_gdbarch_tdep> (gdbarch);
1482 1.1.1.3 christos
1483 1.1.1.3 christos if (regnum == tdep->fflags_regnum || regnum == tdep->frm_regnum)
1484 1.1.1.3 christos return builtin_type (gdbarch)->builtin_int32;
1485 1.1.1.3 christos else
1486 1.1.1.3 christos gdb_assert_not_reached ("unknown pseudo register number %d", regnum);
1487 1.1.1.3 christos }
1488 1.1.1.3 christos
1489 1.1.1.3 christos /* Return true (non-zero) if pseudo-register REGNUM from GDBARCH is a
1490 1.1.1.3 christos member of REGGROUP, otherwise return false (zero). */
1491 1.1.1.3 christos
1492 1.1.1.3 christos static int
1493 1.1.1.3 christos riscv_pseudo_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
1494 1.1.1.3 christos const struct reggroup *reggroup)
1495 1.1.1.3 christos {
1496 1.1.1.3 christos /* The standard function will also work for pseudo-registers. */
1497 1.1.1.3 christos return riscv_register_reggroup_p (gdbarch, regnum, reggroup);
1498 1.1.1.3 christos }
1499 1.1.1.3 christos
1500 1.1 christos /* Implement the print_registers_info gdbarch method. This is used by
1501 1.1 christos 'info registers' and 'info all-registers'. */
1502 1.1 christos
1503 1.1 christos static void
1504 1.1 christos riscv_print_registers_info (struct gdbarch *gdbarch,
1505 1.1 christos struct ui_file *file,
1506 1.1.1.4 christos const frame_info_ptr &frame,
1507 1.1 christos int regnum, int print_all)
1508 1.1 christos {
1509 1.1 christos if (regnum != -1)
1510 1.1 christos {
1511 1.1 christos /* Print one specified register. */
1512 1.1.1.3 christos if (*(gdbarch_register_name (gdbarch, regnum)) == '\0')
1513 1.1.1.3 christos error (_("Not a valid register for the current processor type"));
1514 1.1 christos riscv_print_one_register_info (gdbarch, file, frame, regnum);
1515 1.1 christos }
1516 1.1 christos else
1517 1.1 christos {
1518 1.1.1.3 christos const struct reggroup *reggroup;
1519 1.1 christos
1520 1.1 christos if (print_all)
1521 1.1 christos reggroup = all_reggroup;
1522 1.1 christos else
1523 1.1 christos reggroup = general_reggroup;
1524 1.1 christos
1525 1.1.1.2 christos for (regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); ++regnum)
1526 1.1 christos {
1527 1.1 christos /* Zero never changes, so might as well hide by default. */
1528 1.1 christos if (regnum == RISCV_ZERO_REGNUM && !print_all)
1529 1.1 christos continue;
1530 1.1 christos
1531 1.1 christos /* Registers with no name are not valid on this ISA. */
1532 1.1.1.3 christos if (*(gdbarch_register_name (gdbarch, regnum)) == '\0')
1533 1.1 christos continue;
1534 1.1 christos
1535 1.1 christos /* Is the register in the group we're interested in? */
1536 1.1 christos if (!gdbarch_register_reggroup_p (gdbarch, regnum, reggroup))
1537 1.1 christos continue;
1538 1.1 christos
1539 1.1 christos riscv_print_one_register_info (gdbarch, file, frame, regnum);
1540 1.1 christos }
1541 1.1 christos }
1542 1.1 christos }
1543 1.1 christos
1544 1.1 christos /* Class that handles one decoded RiscV instruction. */
1545 1.1 christos
1546 1.1 christos class riscv_insn
1547 1.1 christos {
1548 1.1 christos public:
1549 1.1 christos
1550 1.1 christos /* Enum of all the opcodes that GDB cares about during the prologue scan. */
1551 1.1 christos enum opcode
1552 1.1 christos {
1553 1.1 christos /* Unknown value is used at initialisation time. */
1554 1.1 christos UNKNOWN = 0,
1555 1.1 christos
1556 1.1 christos /* These instructions are all the ones we are interested in during the
1557 1.1 christos prologue scan. */
1558 1.1 christos ADD,
1559 1.1 christos ADDI,
1560 1.1 christos ADDIW,
1561 1.1 christos ADDW,
1562 1.1 christos AUIPC,
1563 1.1 christos LUI,
1564 1.1.1.4 christos LI,
1565 1.1 christos SD,
1566 1.1 christos SW,
1567 1.1.1.3 christos LD,
1568 1.1.1.3 christos LW,
1569 1.1.1.3 christos MV,
1570 1.1.1.2 christos /* These are needed for software breakpoint support. */
1571 1.1 christos JAL,
1572 1.1 christos JALR,
1573 1.1 christos BEQ,
1574 1.1 christos BNE,
1575 1.1 christos BLT,
1576 1.1 christos BGE,
1577 1.1 christos BLTU,
1578 1.1 christos BGEU,
1579 1.1 christos /* These are needed for stepping over atomic sequences. */
1580 1.1.1.4 christos SLTI,
1581 1.1.1.4 christos SLTIU,
1582 1.1.1.4 christos XORI,
1583 1.1.1.4 christos ORI,
1584 1.1.1.4 christos ANDI,
1585 1.1.1.4 christos SLLI,
1586 1.1.1.4 christos SLLIW,
1587 1.1.1.4 christos SRLI,
1588 1.1.1.4 christos SRLIW,
1589 1.1.1.4 christos SRAI,
1590 1.1.1.4 christos SRAIW,
1591 1.1.1.4 christos SUB,
1592 1.1.1.4 christos SUBW,
1593 1.1.1.4 christos SLL,
1594 1.1.1.4 christos SLLW,
1595 1.1.1.4 christos SLT,
1596 1.1.1.4 christos SLTU,
1597 1.1.1.4 christos XOR,
1598 1.1.1.4 christos SRL,
1599 1.1.1.4 christos SRLW,
1600 1.1.1.4 christos SRA,
1601 1.1.1.4 christos SRAW,
1602 1.1.1.4 christos OR,
1603 1.1.1.4 christos AND,
1604 1.1.1.4 christos LR_W,
1605 1.1.1.4 christos LR_D,
1606 1.1.1.4 christos SC_W,
1607 1.1.1.4 christos SC_D,
1608 1.1.1.3 christos /* This instruction is used to do a syscall. */
1609 1.1.1.3 christos ECALL,
1610 1.1 christos
1611 1.1 christos /* Other instructions are not interesting during the prologue scan, and
1612 1.1 christos are ignored. */
1613 1.1 christos OTHER
1614 1.1 christos };
1615 1.1 christos
1616 1.1 christos riscv_insn ()
1617 1.1 christos : m_length (0),
1618 1.1 christos m_opcode (OTHER),
1619 1.1 christos m_rd (0),
1620 1.1 christos m_rs1 (0),
1621 1.1 christos m_rs2 (0)
1622 1.1 christos {
1623 1.1 christos /* Nothing. */
1624 1.1 christos }
1625 1.1 christos
1626 1.1 christos void decode (struct gdbarch *gdbarch, CORE_ADDR pc);
1627 1.1 christos
1628 1.1 christos /* Get the length of the instruction in bytes. */
1629 1.1 christos int length () const
1630 1.1 christos { return m_length; }
1631 1.1 christos
1632 1.1 christos /* Get the opcode for this instruction. */
1633 1.1 christos enum opcode opcode () const
1634 1.1 christos { return m_opcode; }
1635 1.1 christos
1636 1.1 christos /* Get destination register field for this instruction. This is only
1637 1.1 christos valid if the OPCODE implies there is such a field for this
1638 1.1 christos instruction. */
1639 1.1 christos int rd () const
1640 1.1 christos { return m_rd; }
1641 1.1 christos
1642 1.1 christos /* Get the RS1 register field for this instruction. This is only valid
1643 1.1 christos if the OPCODE implies there is such a field for this instruction. */
1644 1.1 christos int rs1 () const
1645 1.1 christos { return m_rs1; }
1646 1.1 christos
1647 1.1 christos /* Get the RS2 register field for this instruction. This is only valid
1648 1.1 christos if the OPCODE implies there is such a field for this instruction. */
1649 1.1 christos int rs2 () const
1650 1.1 christos { return m_rs2; }
1651 1.1 christos
1652 1.1 christos /* Get the immediate for this instruction in signed form. This is only
1653 1.1 christos valid if the OPCODE implies there is such a field for this
1654 1.1 christos instruction. */
1655 1.1 christos int imm_signed () const
1656 1.1 christos { return m_imm.s; }
1657 1.1 christos
1658 1.1 christos private:
1659 1.1 christos
1660 1.1 christos /* Extract 5 bit register field at OFFSET from instruction OPCODE. */
1661 1.1 christos int decode_register_index (unsigned long opcode, int offset)
1662 1.1 christos {
1663 1.1 christos return (opcode >> offset) & 0x1F;
1664 1.1 christos }
1665 1.1 christos
1666 1.1 christos /* Extract 5 bit register field at OFFSET from instruction OPCODE. */
1667 1.1 christos int decode_register_index_short (unsigned long opcode, int offset)
1668 1.1 christos {
1669 1.1 christos return ((opcode >> offset) & 0x7) + 8;
1670 1.1 christos }
1671 1.1 christos
1672 1.1 christos /* Helper for DECODE, decode 32-bit R-type instruction. */
1673 1.1 christos void decode_r_type_insn (enum opcode opcode, ULONGEST ival)
1674 1.1 christos {
1675 1.1 christos m_opcode = opcode;
1676 1.1 christos m_rd = decode_register_index (ival, OP_SH_RD);
1677 1.1 christos m_rs1 = decode_register_index (ival, OP_SH_RS1);
1678 1.1 christos m_rs2 = decode_register_index (ival, OP_SH_RS2);
1679 1.1 christos }
1680 1.1 christos
1681 1.1 christos /* Helper for DECODE, decode 16-bit compressed R-type instruction. */
1682 1.1 christos void decode_cr_type_insn (enum opcode opcode, ULONGEST ival)
1683 1.1 christos {
1684 1.1 christos m_opcode = opcode;
1685 1.1 christos m_rd = m_rs1 = decode_register_index (ival, OP_SH_CRS1S);
1686 1.1 christos m_rs2 = decode_register_index (ival, OP_SH_CRS2);
1687 1.1 christos }
1688 1.1 christos
1689 1.1 christos /* Helper for DECODE, decode 32-bit I-type instruction. */
1690 1.1 christos void decode_i_type_insn (enum opcode opcode, ULONGEST ival)
1691 1.1 christos {
1692 1.1 christos m_opcode = opcode;
1693 1.1 christos m_rd = decode_register_index (ival, OP_SH_RD);
1694 1.1 christos m_rs1 = decode_register_index (ival, OP_SH_RS1);
1695 1.1 christos m_imm.s = EXTRACT_ITYPE_IMM (ival);
1696 1.1 christos }
1697 1.1 christos
1698 1.1.1.4 christos /* Helper for DECODE, decode 16-bit compressed I-type instruction. Some
1699 1.1.1.4 christos of the CI instruction have a hard-coded rs1 register, while others
1700 1.1.1.4 christos just use rd for both the source and destination. RS1_REGNUM, if
1701 1.1.1.4 christos passed, is the value to place in rs1, otherwise rd is duplicated into
1702 1.1.1.4 christos rs1. */
1703 1.1.1.4 christos void decode_ci_type_insn (enum opcode opcode, ULONGEST ival,
1704 1.1.1.4 christos std::optional<int> rs1_regnum = {})
1705 1.1 christos {
1706 1.1 christos m_opcode = opcode;
1707 1.1.1.4 christos m_rd = decode_register_index (ival, OP_SH_CRS1S);
1708 1.1.1.4 christos if (rs1_regnum.has_value ())
1709 1.1.1.4 christos m_rs1 = *rs1_regnum;
1710 1.1.1.4 christos else
1711 1.1.1.4 christos m_rs1 = m_rd;
1712 1.1.1.3 christos m_imm.s = EXTRACT_CITYPE_IMM (ival);
1713 1.1.1.3 christos }
1714 1.1.1.3 christos
1715 1.1.1.3 christos /* Helper for DECODE, decode 16-bit compressed CL-type instruction. */
1716 1.1.1.3 christos void decode_cl_type_insn (enum opcode opcode, ULONGEST ival)
1717 1.1.1.3 christos {
1718 1.1.1.3 christos m_opcode = opcode;
1719 1.1.1.3 christos m_rd = decode_register_index_short (ival, OP_SH_CRS2S);
1720 1.1.1.3 christos m_rs1 = decode_register_index_short (ival, OP_SH_CRS1S);
1721 1.1.1.3 christos m_imm.s = EXTRACT_CLTYPE_IMM (ival);
1722 1.1 christos }
1723 1.1 christos
1724 1.1 christos /* Helper for DECODE, decode 32-bit S-type instruction. */
1725 1.1 christos void decode_s_type_insn (enum opcode opcode, ULONGEST ival)
1726 1.1 christos {
1727 1.1 christos m_opcode = opcode;
1728 1.1 christos m_rs1 = decode_register_index (ival, OP_SH_RS1);
1729 1.1 christos m_rs2 = decode_register_index (ival, OP_SH_RS2);
1730 1.1 christos m_imm.s = EXTRACT_STYPE_IMM (ival);
1731 1.1 christos }
1732 1.1 christos
1733 1.1 christos /* Helper for DECODE, decode 16-bit CS-type instruction. The immediate
1734 1.1 christos encoding is different for each CS format instruction, so extracting
1735 1.1 christos the immediate is left up to the caller, who should pass the extracted
1736 1.1 christos immediate value through in IMM. */
1737 1.1 christos void decode_cs_type_insn (enum opcode opcode, ULONGEST ival, int imm)
1738 1.1 christos {
1739 1.1 christos m_opcode = opcode;
1740 1.1 christos m_imm.s = imm;
1741 1.1 christos m_rs1 = decode_register_index_short (ival, OP_SH_CRS1S);
1742 1.1 christos m_rs2 = decode_register_index_short (ival, OP_SH_CRS2S);
1743 1.1 christos }
1744 1.1 christos
1745 1.1 christos /* Helper for DECODE, decode 16-bit CSS-type instruction. The immediate
1746 1.1 christos encoding is different for each CSS format instruction, so extracting
1747 1.1 christos the immediate is left up to the caller, who should pass the extracted
1748 1.1 christos immediate value through in IMM. */
1749 1.1 christos void decode_css_type_insn (enum opcode opcode, ULONGEST ival, int imm)
1750 1.1 christos {
1751 1.1 christos m_opcode = opcode;
1752 1.1 christos m_imm.s = imm;
1753 1.1 christos m_rs1 = RISCV_SP_REGNUM;
1754 1.1 christos /* Not a compressed register number in this case. */
1755 1.1 christos m_rs2 = decode_register_index (ival, OP_SH_CRS2);
1756 1.1 christos }
1757 1.1 christos
1758 1.1 christos /* Helper for DECODE, decode 32-bit U-type instruction. */
1759 1.1 christos void decode_u_type_insn (enum opcode opcode, ULONGEST ival)
1760 1.1 christos {
1761 1.1 christos m_opcode = opcode;
1762 1.1 christos m_rd = decode_register_index (ival, OP_SH_RD);
1763 1.1 christos m_imm.s = EXTRACT_UTYPE_IMM (ival);
1764 1.1 christos }
1765 1.1 christos
1766 1.1 christos /* Helper for DECODE, decode 32-bit J-type instruction. */
1767 1.1 christos void decode_j_type_insn (enum opcode opcode, ULONGEST ival)
1768 1.1 christos {
1769 1.1 christos m_opcode = opcode;
1770 1.1 christos m_rd = decode_register_index (ival, OP_SH_RD);
1771 1.1.1.3 christos m_imm.s = EXTRACT_JTYPE_IMM (ival);
1772 1.1 christos }
1773 1.1 christos
1774 1.1 christos /* Helper for DECODE, decode 32-bit J-type instruction. */
1775 1.1 christos void decode_cj_type_insn (enum opcode opcode, ULONGEST ival)
1776 1.1 christos {
1777 1.1 christos m_opcode = opcode;
1778 1.1.1.3 christos m_imm.s = EXTRACT_CJTYPE_IMM (ival);
1779 1.1 christos }
1780 1.1 christos
1781 1.1 christos void decode_b_type_insn (enum opcode opcode, ULONGEST ival)
1782 1.1 christos {
1783 1.1 christos m_opcode = opcode;
1784 1.1 christos m_rs1 = decode_register_index (ival, OP_SH_RS1);
1785 1.1 christos m_rs2 = decode_register_index (ival, OP_SH_RS2);
1786 1.1.1.3 christos m_imm.s = EXTRACT_BTYPE_IMM (ival);
1787 1.1 christos }
1788 1.1 christos
1789 1.1 christos void decode_cb_type_insn (enum opcode opcode, ULONGEST ival)
1790 1.1 christos {
1791 1.1 christos m_opcode = opcode;
1792 1.1 christos m_rs1 = decode_register_index_short (ival, OP_SH_CRS1S);
1793 1.1.1.3 christos m_imm.s = EXTRACT_CBTYPE_IMM (ival);
1794 1.1 christos }
1795 1.1 christos
1796 1.1.1.4 christos void decode_ca_type_insn (enum opcode opcode, ULONGEST ival)
1797 1.1.1.4 christos {
1798 1.1.1.4 christos m_opcode = opcode;
1799 1.1.1.4 christos m_rs1 = decode_register_index_short (ival, OP_SH_CRS1S);
1800 1.1.1.4 christos m_rs2 = decode_register_index_short (ival, OP_SH_CRS2S);
1801 1.1.1.4 christos }
1802 1.1.1.4 christos
1803 1.1 christos /* Fetch instruction from target memory at ADDR, return the content of
1804 1.1 christos the instruction, and update LEN with the instruction length. */
1805 1.1 christos static ULONGEST fetch_instruction (struct gdbarch *gdbarch,
1806 1.1 christos CORE_ADDR addr, int *len);
1807 1.1 christos
1808 1.1 christos /* The length of the instruction in bytes. Should be 2 or 4. */
1809 1.1 christos int m_length;
1810 1.1 christos
1811 1.1 christos /* The instruction opcode. */
1812 1.1 christos enum opcode m_opcode;
1813 1.1 christos
1814 1.1 christos /* The three possible registers an instruction might reference. Not
1815 1.1 christos every instruction fills in all of these registers. Which fields are
1816 1.1 christos valid depends on the opcode. The naming of these fields matches the
1817 1.1 christos naming in the riscv isa manual. */
1818 1.1 christos int m_rd;
1819 1.1 christos int m_rs1;
1820 1.1 christos int m_rs2;
1821 1.1 christos
1822 1.1 christos /* Possible instruction immediate. This is only valid if the instruction
1823 1.1 christos format contains an immediate, not all instruction, whether this is
1824 1.1 christos valid depends on the opcode. Despite only having one format for now
1825 1.1 christos the immediate is packed into a union, later instructions might require
1826 1.1 christos an unsigned formatted immediate, having the union in place now will
1827 1.1 christos reduce the need for code churn later. */
1828 1.1 christos union riscv_insn_immediate
1829 1.1 christos {
1830 1.1 christos riscv_insn_immediate ()
1831 1.1 christos : s (0)
1832 1.1 christos {
1833 1.1 christos /* Nothing. */
1834 1.1 christos }
1835 1.1 christos
1836 1.1 christos int s;
1837 1.1 christos } m_imm;
1838 1.1 christos };
1839 1.1 christos
1840 1.1 christos /* Fetch instruction from target memory at ADDR, return the content of the
1841 1.1 christos instruction, and update LEN with the instruction length. */
1842 1.1 christos
1843 1.1 christos ULONGEST
1844 1.1 christos riscv_insn::fetch_instruction (struct gdbarch *gdbarch,
1845 1.1 christos CORE_ADDR addr, int *len)
1846 1.1 christos {
1847 1.1.1.3 christos gdb_byte buf[RISCV_MAX_INSN_LEN];
1848 1.1 christos int instlen, status;
1849 1.1 christos
1850 1.1 christos /* All insns are at least 16 bits. */
1851 1.1 christos status = target_read_memory (addr, buf, 2);
1852 1.1 christos if (status)
1853 1.1 christos memory_error (TARGET_XFER_E_IO, addr);
1854 1.1 christos
1855 1.1 christos /* If we need more, grab it now. */
1856 1.1 christos instlen = riscv_insn_length (buf[0]);
1857 1.1 christos gdb_assert (instlen <= sizeof (buf));
1858 1.1 christos *len = instlen;
1859 1.1 christos
1860 1.1 christos if (instlen > 2)
1861 1.1 christos {
1862 1.1 christos status = target_read_memory (addr + 2, buf + 2, instlen - 2);
1863 1.1 christos if (status)
1864 1.1 christos memory_error (TARGET_XFER_E_IO, addr + 2);
1865 1.1 christos }
1866 1.1 christos
1867 1.1.1.4 christos /* RISC-V Specification states instructions are always little endian */
1868 1.1.1.4 christos return extract_unsigned_integer (buf, instlen, BFD_ENDIAN_LITTLE);
1869 1.1 christos }
1870 1.1 christos
1871 1.1 christos /* Fetch from target memory an instruction at PC and decode it. This can
1872 1.1 christos throw an error if the memory access fails, callers are responsible for
1873 1.1 christos handling this error if that is appropriate. */
1874 1.1 christos
1875 1.1 christos void
1876 1.1 christos riscv_insn::decode (struct gdbarch *gdbarch, CORE_ADDR pc)
1877 1.1 christos {
1878 1.1 christos ULONGEST ival;
1879 1.1 christos
1880 1.1 christos /* Fetch the instruction, and the instructions length. */
1881 1.1 christos ival = fetch_instruction (gdbarch, pc, &m_length);
1882 1.1 christos
1883 1.1 christos if (m_length == 4)
1884 1.1 christos {
1885 1.1 christos if (is_add_insn (ival))
1886 1.1 christos decode_r_type_insn (ADD, ival);
1887 1.1 christos else if (is_addw_insn (ival))
1888 1.1 christos decode_r_type_insn (ADDW, ival);
1889 1.1 christos else if (is_addi_insn (ival))
1890 1.1 christos decode_i_type_insn (ADDI, ival);
1891 1.1 christos else if (is_addiw_insn (ival))
1892 1.1 christos decode_i_type_insn (ADDIW, ival);
1893 1.1 christos else if (is_auipc_insn (ival))
1894 1.1 christos decode_u_type_insn (AUIPC, ival);
1895 1.1 christos else if (is_lui_insn (ival))
1896 1.1 christos decode_u_type_insn (LUI, ival);
1897 1.1 christos else if (is_sd_insn (ival))
1898 1.1 christos decode_s_type_insn (SD, ival);
1899 1.1 christos else if (is_sw_insn (ival))
1900 1.1 christos decode_s_type_insn (SW, ival);
1901 1.1 christos else if (is_jal_insn (ival))
1902 1.1 christos decode_j_type_insn (JAL, ival);
1903 1.1 christos else if (is_jalr_insn (ival))
1904 1.1 christos decode_i_type_insn (JALR, ival);
1905 1.1 christos else if (is_beq_insn (ival))
1906 1.1 christos decode_b_type_insn (BEQ, ival);
1907 1.1 christos else if (is_bne_insn (ival))
1908 1.1 christos decode_b_type_insn (BNE, ival);
1909 1.1 christos else if (is_blt_insn (ival))
1910 1.1 christos decode_b_type_insn (BLT, ival);
1911 1.1 christos else if (is_bge_insn (ival))
1912 1.1 christos decode_b_type_insn (BGE, ival);
1913 1.1 christos else if (is_bltu_insn (ival))
1914 1.1 christos decode_b_type_insn (BLTU, ival);
1915 1.1 christos else if (is_bgeu_insn (ival))
1916 1.1 christos decode_b_type_insn (BGEU, ival);
1917 1.1.1.4 christos else if (is_slti_insn(ival))
1918 1.1.1.4 christos decode_i_type_insn (SLTI, ival);
1919 1.1.1.4 christos else if (is_sltiu_insn(ival))
1920 1.1.1.4 christos decode_i_type_insn (SLTIU, ival);
1921 1.1.1.4 christos else if (is_xori_insn(ival))
1922 1.1.1.4 christos decode_i_type_insn (XORI, ival);
1923 1.1.1.4 christos else if (is_ori_insn(ival))
1924 1.1.1.4 christos decode_i_type_insn (ORI, ival);
1925 1.1.1.4 christos else if (is_andi_insn(ival))
1926 1.1.1.4 christos decode_i_type_insn (ANDI, ival);
1927 1.1.1.4 christos else if (is_slli_insn(ival))
1928 1.1.1.4 christos decode_i_type_insn (SLLI, ival);
1929 1.1.1.4 christos else if (is_slliw_insn(ival))
1930 1.1.1.4 christos decode_i_type_insn (SLLIW, ival);
1931 1.1.1.4 christos else if (is_srli_insn(ival))
1932 1.1.1.4 christos decode_i_type_insn (SRLI, ival);
1933 1.1.1.4 christos else if (is_srliw_insn(ival))
1934 1.1.1.4 christos decode_i_type_insn (SRLIW, ival);
1935 1.1.1.4 christos else if (is_srai_insn(ival))
1936 1.1.1.4 christos decode_i_type_insn (SRAI, ival);
1937 1.1.1.4 christos else if (is_sraiw_insn(ival))
1938 1.1.1.4 christos decode_i_type_insn (SRAIW, ival);
1939 1.1.1.4 christos else if (is_sub_insn(ival))
1940 1.1.1.4 christos decode_r_type_insn (SUB, ival);
1941 1.1.1.4 christos else if (is_subw_insn(ival))
1942 1.1.1.4 christos decode_r_type_insn (SUBW, ival);
1943 1.1.1.4 christos else if (is_sll_insn(ival))
1944 1.1.1.4 christos decode_r_type_insn (SLL, ival);
1945 1.1.1.4 christos else if (is_sllw_insn(ival))
1946 1.1.1.4 christos decode_r_type_insn (SLLW, ival);
1947 1.1.1.4 christos else if (is_slt_insn(ival))
1948 1.1.1.4 christos decode_r_type_insn (SLT, ival);
1949 1.1.1.4 christos else if (is_sltu_insn(ival))
1950 1.1.1.4 christos decode_r_type_insn (SLTU, ival);
1951 1.1.1.4 christos else if (is_xor_insn(ival))
1952 1.1.1.4 christos decode_r_type_insn (XOR, ival);
1953 1.1.1.4 christos else if (is_srl_insn(ival))
1954 1.1.1.4 christos decode_r_type_insn (SRL, ival);
1955 1.1.1.4 christos else if (is_srlw_insn(ival))
1956 1.1.1.4 christos decode_r_type_insn (SRLW, ival);
1957 1.1.1.4 christos else if (is_sra_insn(ival))
1958 1.1.1.4 christos decode_r_type_insn (SRA, ival);
1959 1.1.1.4 christos else if (is_sraw_insn(ival))
1960 1.1.1.4 christos decode_r_type_insn (SRAW, ival);
1961 1.1.1.4 christos else if (is_or_insn(ival))
1962 1.1.1.4 christos decode_r_type_insn (OR, ival);
1963 1.1.1.4 christos else if (is_and_insn(ival))
1964 1.1.1.4 christos decode_r_type_insn (AND, ival);
1965 1.1 christos else if (is_lr_w_insn (ival))
1966 1.1.1.4 christos decode_r_type_insn (LR_W, ival);
1967 1.1 christos else if (is_lr_d_insn (ival))
1968 1.1.1.4 christos decode_r_type_insn (LR_D, ival);
1969 1.1 christos else if (is_sc_w_insn (ival))
1970 1.1.1.4 christos decode_r_type_insn (SC_W, ival);
1971 1.1 christos else if (is_sc_d_insn (ival))
1972 1.1.1.4 christos decode_r_type_insn (SC_D, ival);
1973 1.1.1.3 christos else if (is_ecall_insn (ival))
1974 1.1.1.3 christos decode_i_type_insn (ECALL, ival);
1975 1.1.1.3 christos else if (is_ld_insn (ival))
1976 1.1.1.3 christos decode_i_type_insn (LD, ival);
1977 1.1.1.3 christos else if (is_lw_insn (ival))
1978 1.1.1.3 christos decode_i_type_insn (LW, ival);
1979 1.1 christos else
1980 1.1 christos /* None of the other fields are valid in this case. */
1981 1.1 christos m_opcode = OTHER;
1982 1.1 christos }
1983 1.1 christos else if (m_length == 2)
1984 1.1 christos {
1985 1.1 christos int xlen = riscv_isa_xlen (gdbarch);
1986 1.1 christos
1987 1.1 christos /* C_ADD and C_JALR have the same opcode. If RS2 is 0, then this is a
1988 1.1 christos C_JALR. So must try to match C_JALR first as it has more bits in
1989 1.1 christos mask. */
1990 1.1 christos if (is_c_jalr_insn (ival))
1991 1.1 christos decode_cr_type_insn (JALR, ival);
1992 1.1 christos else if (is_c_add_insn (ival))
1993 1.1 christos decode_cr_type_insn (ADD, ival);
1994 1.1 christos /* C_ADDW is RV64 and RV128 only. */
1995 1.1 christos else if (xlen != 4 && is_c_addw_insn (ival))
1996 1.1 christos decode_cr_type_insn (ADDW, ival);
1997 1.1 christos else if (is_c_addi_insn (ival))
1998 1.1 christos decode_ci_type_insn (ADDI, ival);
1999 1.1 christos /* C_ADDIW and C_JAL have the same opcode. C_ADDIW is RV64 and RV128
2000 1.1 christos only and C_JAL is RV32 only. */
2001 1.1 christos else if (xlen != 4 && is_c_addiw_insn (ival))
2002 1.1 christos decode_ci_type_insn (ADDIW, ival);
2003 1.1 christos else if (xlen == 4 && is_c_jal_insn (ival))
2004 1.1 christos decode_cj_type_insn (JAL, ival);
2005 1.1 christos /* C_ADDI16SP and C_LUI have the same opcode. If RD is 2, then this is a
2006 1.1 christos C_ADDI16SP. So must try to match C_ADDI16SP first as it has more bits
2007 1.1 christos in mask. */
2008 1.1 christos else if (is_c_addi16sp_insn (ival))
2009 1.1 christos {
2010 1.1 christos m_opcode = ADDI;
2011 1.1 christos m_rd = m_rs1 = decode_register_index (ival, OP_SH_RD);
2012 1.1.1.3 christos m_imm.s = EXTRACT_CITYPE_ADDI16SP_IMM (ival);
2013 1.1 christos }
2014 1.1 christos else if (is_c_addi4spn_insn (ival))
2015 1.1 christos {
2016 1.1 christos m_opcode = ADDI;
2017 1.1 christos m_rd = decode_register_index_short (ival, OP_SH_CRS2S);
2018 1.1 christos m_rs1 = RISCV_SP_REGNUM;
2019 1.1.1.3 christos m_imm.s = EXTRACT_CIWTYPE_ADDI4SPN_IMM (ival);
2020 1.1 christos }
2021 1.1 christos else if (is_c_lui_insn (ival))
2022 1.1.1.3 christos {
2023 1.1.1.3 christos m_opcode = LUI;
2024 1.1.1.3 christos m_rd = decode_register_index (ival, OP_SH_CRS1S);
2025 1.1.1.3 christos m_imm.s = EXTRACT_CITYPE_LUI_IMM (ival);
2026 1.1.1.3 christos }
2027 1.1.1.4 christos else if (is_c_srli_insn (ival))
2028 1.1.1.4 christos decode_cb_type_insn (SRLI, ival);
2029 1.1.1.4 christos else if (is_c_srai_insn (ival))
2030 1.1.1.4 christos decode_cb_type_insn (SRAI, ival);
2031 1.1.1.4 christos else if (is_c_andi_insn (ival))
2032 1.1.1.4 christos decode_cb_type_insn (ANDI, ival);
2033 1.1.1.4 christos else if (is_c_sub_insn (ival))
2034 1.1.1.4 christos decode_ca_type_insn (SUB, ival);
2035 1.1.1.4 christos else if (is_c_xor_insn (ival))
2036 1.1.1.4 christos decode_ca_type_insn (XOR, ival);
2037 1.1.1.4 christos else if (is_c_or_insn (ival))
2038 1.1.1.4 christos decode_ca_type_insn (OR, ival);
2039 1.1.1.4 christos else if (is_c_and_insn (ival))
2040 1.1.1.4 christos decode_ca_type_insn (AND, ival);
2041 1.1.1.4 christos else if (is_c_subw_insn (ival))
2042 1.1.1.4 christos decode_ca_type_insn (SUBW, ival);
2043 1.1.1.4 christos else if (is_c_addw_insn (ival))
2044 1.1.1.4 christos decode_ca_type_insn (ADDW, ival);
2045 1.1.1.4 christos else if (is_c_li_insn (ival))
2046 1.1.1.4 christos decode_ci_type_insn (LI, ival);
2047 1.1 christos /* C_SD and C_FSW have the same opcode. C_SD is RV64 and RV128 only,
2048 1.1 christos and C_FSW is RV32 only. */
2049 1.1 christos else if (xlen != 4 && is_c_sd_insn (ival))
2050 1.1.1.3 christos decode_cs_type_insn (SD, ival, EXTRACT_CLTYPE_LD_IMM (ival));
2051 1.1 christos else if (is_c_sw_insn (ival))
2052 1.1.1.3 christos decode_cs_type_insn (SW, ival, EXTRACT_CLTYPE_LW_IMM (ival));
2053 1.1 christos else if (is_c_swsp_insn (ival))
2054 1.1.1.3 christos decode_css_type_insn (SW, ival, EXTRACT_CSSTYPE_SWSP_IMM (ival));
2055 1.1 christos else if (xlen != 4 && is_c_sdsp_insn (ival))
2056 1.1.1.3 christos decode_css_type_insn (SD, ival, EXTRACT_CSSTYPE_SDSP_IMM (ival));
2057 1.1 christos /* C_JR and C_MV have the same opcode. If RS2 is 0, then this is a C_JR.
2058 1.1.1.3 christos So must try to match C_JR first as it has more bits in mask. */
2059 1.1 christos else if (is_c_jr_insn (ival))
2060 1.1 christos decode_cr_type_insn (JALR, ival);
2061 1.1.1.3 christos else if (is_c_mv_insn (ival))
2062 1.1.1.3 christos decode_cr_type_insn (MV, ival);
2063 1.1 christos else if (is_c_j_insn (ival))
2064 1.1 christos decode_cj_type_insn (JAL, ival);
2065 1.1 christos else if (is_c_beqz_insn (ival))
2066 1.1 christos decode_cb_type_insn (BEQ, ival);
2067 1.1 christos else if (is_c_bnez_insn (ival))
2068 1.1 christos decode_cb_type_insn (BNE, ival);
2069 1.1.1.3 christos else if (is_c_ld_insn (ival))
2070 1.1.1.3 christos decode_cl_type_insn (LD, ival);
2071 1.1.1.3 christos else if (is_c_lw_insn (ival))
2072 1.1.1.3 christos decode_cl_type_insn (LW, ival);
2073 1.1.1.4 christos else if (is_c_ldsp_insn (ival))
2074 1.1.1.4 christos decode_ci_type_insn (LD, ival, RISCV_SP_REGNUM);
2075 1.1.1.4 christos else if (is_c_lwsp_insn (ival))
2076 1.1.1.4 christos decode_ci_type_insn (LW, ival, RISCV_SP_REGNUM);
2077 1.1 christos else
2078 1.1 christos /* None of the other fields of INSN are valid in this case. */
2079 1.1 christos m_opcode = OTHER;
2080 1.1 christos }
2081 1.1 christos else
2082 1.1.1.2 christos {
2083 1.1.1.3 christos /* 6 bytes or more. If the instruction is longer than 8 bytes, we don't
2084 1.1.1.3 christos have full instruction bits in ival. At least, such long instructions
2085 1.1.1.3 christos are not defined yet, so just ignore it. */
2086 1.1.1.3 christos gdb_assert (m_length > 0 && m_length % 2 == 0);
2087 1.1.1.2 christos m_opcode = OTHER;
2088 1.1.1.2 christos }
2089 1.1 christos }
2090 1.1 christos
2091 1.1.1.4 christos /* Return true if INSN represents an instruction something like:
2092 1.1.1.4 christos
2093 1.1.1.4 christos ld fp,IMMEDIATE(sp)
2094 1.1.1.4 christos
2095 1.1.1.4 christos That is, a load from stack-pointer plus some immediate offset, with the
2096 1.1.1.4 christos result stored into the frame pointer. We also accept 'lw' as well as
2097 1.1.1.4 christos 'ld'. */
2098 1.1.1.4 christos
2099 1.1.1.4 christos static bool
2100 1.1.1.4 christos is_insn_load_of_fp_from_sp (const struct riscv_insn &insn)
2101 1.1.1.4 christos {
2102 1.1.1.4 christos return ((insn.opcode () == riscv_insn::LD
2103 1.1.1.4 christos || insn.opcode () == riscv_insn::LW)
2104 1.1.1.4 christos && insn.rd () == RISCV_FP_REGNUM
2105 1.1.1.4 christos && insn.rs1 () == RISCV_SP_REGNUM);
2106 1.1.1.4 christos }
2107 1.1.1.4 christos
2108 1.1.1.4 christos /* Return true if INSN represents an instruction something like:
2109 1.1.1.4 christos
2110 1.1.1.4 christos add sp,sp,IMMEDIATE
2111 1.1.1.4 christos
2112 1.1.1.4 christos That is, an add of an immediate to the value in the stack pointer
2113 1.1.1.4 christos register, with the result stored back to the stack pointer register. */
2114 1.1.1.4 christos
2115 1.1.1.4 christos static bool
2116 1.1.1.4 christos is_insn_addi_of_sp_to_sp (const struct riscv_insn &insn)
2117 1.1.1.4 christos {
2118 1.1.1.4 christos return ((insn.opcode () == riscv_insn::ADDI
2119 1.1.1.4 christos || insn.opcode () == riscv_insn::ADDIW)
2120 1.1.1.4 christos && insn.rd () == RISCV_SP_REGNUM
2121 1.1.1.4 christos && insn.rs1 () == RISCV_SP_REGNUM);
2122 1.1.1.4 christos }
2123 1.1.1.4 christos
2124 1.1.1.4 christos /* Is the instruction in code memory prior to address PC a load from stack
2125 1.1.1.4 christos instruction? Return true if it is, otherwise, return false.
2126 1.1.1.4 christos
2127 1.1.1.4 christos This is a best effort that is used as part of the function prologue
2128 1.1.1.4 christos scanning logic. With compressed instructions and arbitrary control
2129 1.1.1.4 christos flow in the inferior, we can never be certain what the instruction
2130 1.1.1.4 christos prior to PC is.
2131 1.1.1.4 christos
2132 1.1.1.4 christos This function first looks for a compressed instruction, then looks for
2133 1.1.1.4 christos a 32-bit non-compressed instruction. */
2134 1.1.1.4 christos
2135 1.1.1.4 christos static bool
2136 1.1.1.4 christos previous_insn_is_load_fp_from_stack (struct gdbarch *gdbarch, CORE_ADDR pc)
2137 1.1.1.4 christos {
2138 1.1.1.4 christos struct riscv_insn insn;
2139 1.1.1.4 christos insn.decode (gdbarch, pc - 2);
2140 1.1.1.4 christos gdb_assert (insn.length () > 0);
2141 1.1.1.4 christos
2142 1.1.1.4 christos if (insn.length () != 2 || !is_insn_load_of_fp_from_sp (insn))
2143 1.1.1.4 christos {
2144 1.1.1.4 christos insn.decode (gdbarch, pc - 4);
2145 1.1.1.4 christos gdb_assert (insn.length () > 0);
2146 1.1.1.4 christos
2147 1.1.1.4 christos if (insn.length () != 4 || !is_insn_load_of_fp_from_sp (insn))
2148 1.1.1.4 christos return false;
2149 1.1.1.4 christos }
2150 1.1.1.4 christos
2151 1.1.1.4 christos riscv_unwinder_debug_printf
2152 1.1.1.4 christos ("previous instruction at %s (length %d) was 'ld'",
2153 1.1.1.4 christos core_addr_to_string (pc - insn.length ()), insn.length ());
2154 1.1.1.4 christos return true;
2155 1.1.1.4 christos }
2156 1.1.1.4 christos
2157 1.1.1.4 christos /* Is the instruction in code memory prior to address PC an add of an
2158 1.1.1.4 christos immediate to the stack pointer, with the result being written back into
2159 1.1.1.4 christos the stack pointer? Return true and set *PREV_PC to the address of the
2160 1.1.1.4 christos previous instruction if we believe the previous instruction is such an
2161 1.1.1.4 christos add, otherwise return false and *PREV_PC is undefined.
2162 1.1.1.4 christos
2163 1.1.1.4 christos This is a best effort that is used as part of the function prologue
2164 1.1.1.4 christos scanning logic. With compressed instructions and arbitrary control
2165 1.1.1.4 christos flow in the inferior, we can never be certain what the instruction
2166 1.1.1.4 christos prior to PC is.
2167 1.1.1.4 christos
2168 1.1.1.4 christos This function first looks for a compressed instruction, then looks for
2169 1.1.1.4 christos a 32-bit non-compressed instruction. */
2170 1.1.1.4 christos
2171 1.1.1.4 christos static bool
2172 1.1.1.4 christos previous_insn_is_add_imm_to_sp (struct gdbarch *gdbarch, CORE_ADDR pc,
2173 1.1.1.4 christos CORE_ADDR *prev_pc)
2174 1.1.1.4 christos {
2175 1.1.1.4 christos struct riscv_insn insn;
2176 1.1.1.4 christos insn.decode (gdbarch, pc - 2);
2177 1.1.1.4 christos gdb_assert (insn.length () > 0);
2178 1.1.1.4 christos
2179 1.1.1.4 christos if (insn.length () != 2 || !is_insn_addi_of_sp_to_sp (insn))
2180 1.1.1.4 christos {
2181 1.1.1.4 christos insn.decode (gdbarch, pc - 4);
2182 1.1.1.4 christos gdb_assert (insn.length () > 0);
2183 1.1.1.4 christos
2184 1.1.1.4 christos if (insn.length () != 4 || !is_insn_addi_of_sp_to_sp (insn))
2185 1.1.1.4 christos return false;
2186 1.1.1.4 christos }
2187 1.1.1.4 christos
2188 1.1.1.4 christos riscv_unwinder_debug_printf
2189 1.1.1.4 christos ("previous instruction at %s (length %d) was 'add'",
2190 1.1.1.4 christos core_addr_to_string (pc - insn.length ()), insn.length ());
2191 1.1.1.4 christos *prev_pc = pc - insn.length ();
2192 1.1.1.4 christos return true;
2193 1.1.1.4 christos }
2194 1.1.1.4 christos
2195 1.1.1.4 christos /* Try to spot when PC is located in an exit sequence for a particular
2196 1.1.1.4 christos function. Detecting an exit sequence involves a limited amount of
2197 1.1.1.4 christos scanning backwards through the disassembly, and so, when considering
2198 1.1.1.4 christos compressed instructions, we can never be certain that we have
2199 1.1.1.4 christos disassembled the preceding instructions correctly. On top of that, we
2200 1.1.1.4 christos can't be certain that the inferior arrived at PC by passing through the
2201 1.1.1.4 christos preceding instructions.
2202 1.1.1.4 christos
2203 1.1.1.4 christos With all that said, we know that using prologue scanning to figure a
2204 1.1.1.4 christos functions unwind information starts to fail when we consider returns
2205 1.1.1.4 christos from an instruction -- we must pass through some instructions that
2206 1.1.1.4 christos restore the previous state prior to the final return instruction, and
2207 1.1.1.4 christos with state partially restored, our prologue derived unwind information
2208 1.1.1.4 christos is no longer valid.
2209 1.1.1.4 christos
2210 1.1.1.4 christos This function then, aims to spot instruction sequences like this:
2211 1.1.1.4 christos
2212 1.1.1.4 christos ld fp, IMM_1(sp)
2213 1.1.1.4 christos add sp, sp, IMM_2
2214 1.1.1.4 christos ret
2215 1.1.1.4 christos
2216 1.1.1.4 christos The first instruction restores the previous frame-pointer value, the
2217 1.1.1.4 christos second restores the previous stack pointer value, and the final
2218 1.1.1.4 christos instruction is the actual return.
2219 1.1.1.4 christos
2220 1.1.1.4 christos We need to consider that some or all of these instructions might be
2221 1.1.1.4 christos compressed.
2222 1.1.1.4 christos
2223 1.1.1.4 christos This function makes the assumption that, when the inferior reaches the
2224 1.1.1.4 christos 'ret' instruction the stack pointer will have been restored to its value
2225 1.1.1.4 christos on entry to this function. This assumption will be true in most well
2226 1.1.1.4 christos formed programs.
2227 1.1.1.4 christos
2228 1.1.1.4 christos Return true if we detect that we are in such an instruction sequence,
2229 1.1.1.4 christos that is PC points at one of the three instructions given above. In this
2230 1.1.1.4 christos case, set *OFFSET to IMM_2 if PC points to either of the first
2231 1.1.1.4 christos two instructions (the 'ld' or 'add'), otherwise set *OFFSET to 0.
2232 1.1.1.4 christos
2233 1.1.1.4 christos Otherwise, this function returns false, and the contents of *OFFSET are
2234 1.1.1.4 christos undefined. */
2235 1.1.1.4 christos
2236 1.1.1.4 christos static bool
2237 1.1.1.4 christos riscv_detect_end_of_function (struct gdbarch *gdbarch, CORE_ADDR pc,
2238 1.1.1.4 christos int *offset)
2239 1.1.1.4 christos {
2240 1.1.1.4 christos *offset = 0;
2241 1.1.1.4 christos
2242 1.1.1.4 christos /* We only want to scan a maximum of 3 instructions. */
2243 1.1.1.4 christos for (int i = 0; i < 3; ++i)
2244 1.1.1.4 christos {
2245 1.1.1.4 christos struct riscv_insn insn;
2246 1.1.1.4 christos insn.decode (gdbarch, pc);
2247 1.1.1.4 christos gdb_assert (insn.length () > 0);
2248 1.1.1.4 christos
2249 1.1.1.4 christos if (is_insn_load_of_fp_from_sp (insn))
2250 1.1.1.4 christos {
2251 1.1.1.4 christos riscv_unwinder_debug_printf ("found 'ld' instruction at %s",
2252 1.1.1.4 christos core_addr_to_string (pc));
2253 1.1.1.4 christos if (i > 0)
2254 1.1.1.4 christos return false;
2255 1.1.1.4 christos pc += insn.length ();
2256 1.1.1.4 christos }
2257 1.1.1.4 christos else if (is_insn_addi_of_sp_to_sp (insn))
2258 1.1.1.4 christos {
2259 1.1.1.4 christos riscv_unwinder_debug_printf ("found 'add' instruction at %s",
2260 1.1.1.4 christos core_addr_to_string (pc));
2261 1.1.1.4 christos if (i > 1)
2262 1.1.1.4 christos return false;
2263 1.1.1.4 christos if (i == 0)
2264 1.1.1.4 christos {
2265 1.1.1.4 christos if (!previous_insn_is_load_fp_from_stack (gdbarch, pc))
2266 1.1.1.4 christos return false;
2267 1.1.1.4 christos
2268 1.1.1.4 christos i = 1;
2269 1.1.1.4 christos }
2270 1.1.1.4 christos *offset = insn.imm_signed ();
2271 1.1.1.4 christos pc += insn.length ();
2272 1.1.1.4 christos }
2273 1.1.1.4 christos else if (insn.opcode () == riscv_insn::JALR
2274 1.1.1.4 christos && insn.rs1 () == RISCV_RA_REGNUM
2275 1.1.1.4 christos && insn.rs2 () == RISCV_ZERO_REGNUM)
2276 1.1.1.4 christos {
2277 1.1.1.4 christos riscv_unwinder_debug_printf ("found 'ret' instruction at %s",
2278 1.1.1.4 christos core_addr_to_string (pc));
2279 1.1.1.4 christos gdb_assert (i != 1);
2280 1.1.1.4 christos if (i == 0)
2281 1.1.1.4 christos {
2282 1.1.1.4 christos CORE_ADDR prev_pc;
2283 1.1.1.4 christos if (!previous_insn_is_add_imm_to_sp (gdbarch, pc, &prev_pc))
2284 1.1.1.4 christos return false;
2285 1.1.1.4 christos if (!previous_insn_is_load_fp_from_stack (gdbarch, prev_pc))
2286 1.1.1.4 christos return false;
2287 1.1.1.4 christos i = 2;
2288 1.1.1.4 christos }
2289 1.1.1.4 christos
2290 1.1.1.4 christos pc += insn.length ();
2291 1.1.1.4 christos }
2292 1.1.1.4 christos else
2293 1.1.1.4 christos return false;
2294 1.1.1.4 christos }
2295 1.1.1.4 christos
2296 1.1.1.4 christos return true;
2297 1.1.1.4 christos }
2298 1.1.1.4 christos
2299 1.1 christos /* The prologue scanner. This is currently only used for skipping the
2300 1.1 christos prologue of a function when the DWARF information is not sufficient.
2301 1.1 christos However, it is written with filling of the frame cache in mind, which
2302 1.1 christos is why different groups of stack setup instructions are split apart
2303 1.1 christos during the core of the inner loop. In the future, the intention is to
2304 1.1 christos extend this function to fully support building up a frame cache that
2305 1.1 christos can unwind register values when there is no DWARF information. */
2306 1.1 christos
2307 1.1 christos static CORE_ADDR
2308 1.1 christos riscv_scan_prologue (struct gdbarch *gdbarch,
2309 1.1 christos CORE_ADDR start_pc, CORE_ADDR end_pc,
2310 1.1 christos struct riscv_unwind_cache *cache)
2311 1.1 christos {
2312 1.1 christos CORE_ADDR cur_pc, next_pc, after_prologue_pc;
2313 1.1.1.4 christos CORE_ADDR original_end_pc = end_pc;
2314 1.1 christos CORE_ADDR end_prologue_addr = 0;
2315 1.1 christos
2316 1.1 christos /* Find an upper limit on the function prologue using the debug
2317 1.1 christos information. If the debug information could not be used to provide
2318 1.1 christos that bound, then use an arbitrary large number as the upper bound. */
2319 1.1 christos after_prologue_pc = skip_prologue_using_sal (gdbarch, start_pc);
2320 1.1 christos if (after_prologue_pc == 0)
2321 1.1 christos after_prologue_pc = start_pc + 100; /* Arbitrary large number. */
2322 1.1 christos if (after_prologue_pc < end_pc)
2323 1.1 christos end_pc = after_prologue_pc;
2324 1.1 christos
2325 1.1 christos pv_t regs[RISCV_NUM_INTEGER_REGS]; /* Number of GPR. */
2326 1.1 christos for (int regno = 0; regno < RISCV_NUM_INTEGER_REGS; regno++)
2327 1.1 christos regs[regno] = pv_register (regno, 0);
2328 1.1 christos pv_area stack (RISCV_SP_REGNUM, gdbarch_addr_bit (gdbarch));
2329 1.1 christos
2330 1.1.1.4 christos riscv_unwinder_debug_printf ("function starting at %s (limit %s)",
2331 1.1.1.4 christos core_addr_to_string (start_pc),
2332 1.1.1.4 christos core_addr_to_string (end_pc));
2333 1.1 christos
2334 1.1 christos for (next_pc = cur_pc = start_pc; cur_pc < end_pc; cur_pc = next_pc)
2335 1.1 christos {
2336 1.1 christos struct riscv_insn insn;
2337 1.1 christos
2338 1.1 christos /* Decode the current instruction, and decide where the next
2339 1.1 christos instruction lives based on the size of this instruction. */
2340 1.1 christos insn.decode (gdbarch, cur_pc);
2341 1.1 christos gdb_assert (insn.length () > 0);
2342 1.1 christos next_pc = cur_pc + insn.length ();
2343 1.1 christos
2344 1.1 christos /* Look for common stack adjustment insns. */
2345 1.1.1.4 christos if (is_insn_addi_of_sp_to_sp (insn))
2346 1.1 christos {
2347 1.1 christos /* Handle: addi sp, sp, -i
2348 1.1 christos or: addiw sp, sp, -i */
2349 1.1.1.3 christos gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
2350 1.1.1.3 christos gdb_assert (insn.rs1 () < RISCV_NUM_INTEGER_REGS);
2351 1.1.1.3 christos regs[insn.rd ()]
2352 1.1.1.3 christos = pv_add_constant (regs[insn.rs1 ()], insn.imm_signed ());
2353 1.1 christos }
2354 1.1 christos else if ((insn.opcode () == riscv_insn::SW
2355 1.1 christos || insn.opcode () == riscv_insn::SD)
2356 1.1 christos && (insn.rs1 () == RISCV_SP_REGNUM
2357 1.1 christos || insn.rs1 () == RISCV_FP_REGNUM))
2358 1.1 christos {
2359 1.1 christos /* Handle: sw reg, offset(sp)
2360 1.1 christos or: sd reg, offset(sp)
2361 1.1 christos or: sw reg, offset(s0)
2362 1.1 christos or: sd reg, offset(s0) */
2363 1.1 christos /* Instruction storing a register onto the stack. */
2364 1.1.1.3 christos gdb_assert (insn.rs1 () < RISCV_NUM_INTEGER_REGS);
2365 1.1.1.3 christos gdb_assert (insn.rs2 () < RISCV_NUM_INTEGER_REGS);
2366 1.1.1.3 christos stack.store (pv_add_constant (regs[insn.rs1 ()], insn.imm_signed ()),
2367 1.1.1.3 christos (insn.opcode () == riscv_insn::SW ? 4 : 8),
2368 1.1.1.3 christos regs[insn.rs2 ()]);
2369 1.1 christos }
2370 1.1 christos else if (insn.opcode () == riscv_insn::ADDI
2371 1.1 christos && insn.rd () == RISCV_FP_REGNUM
2372 1.1 christos && insn.rs1 () == RISCV_SP_REGNUM)
2373 1.1 christos {
2374 1.1 christos /* Handle: addi s0, sp, size */
2375 1.1 christos /* Instructions setting up the frame pointer. */
2376 1.1.1.3 christos gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
2377 1.1.1.3 christos gdb_assert (insn.rs1 () < RISCV_NUM_INTEGER_REGS);
2378 1.1.1.3 christos regs[insn.rd ()]
2379 1.1.1.3 christos = pv_add_constant (regs[insn.rs1 ()], insn.imm_signed ());
2380 1.1 christos }
2381 1.1 christos else if ((insn.opcode () == riscv_insn::ADD
2382 1.1 christos || insn.opcode () == riscv_insn::ADDW)
2383 1.1 christos && insn.rd () == RISCV_FP_REGNUM
2384 1.1 christos && insn.rs1 () == RISCV_SP_REGNUM
2385 1.1 christos && insn.rs2 () == RISCV_ZERO_REGNUM)
2386 1.1 christos {
2387 1.1 christos /* Handle: add s0, sp, 0
2388 1.1 christos or: addw s0, sp, 0 */
2389 1.1 christos /* Instructions setting up the frame pointer. */
2390 1.1.1.3 christos gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
2391 1.1.1.3 christos gdb_assert (insn.rs1 () < RISCV_NUM_INTEGER_REGS);
2392 1.1.1.3 christos regs[insn.rd ()] = pv_add_constant (regs[insn.rs1 ()], 0);
2393 1.1 christos }
2394 1.1 christos else if ((insn.opcode () == riscv_insn::ADDI
2395 1.1.1.3 christos && insn.rd () == RISCV_ZERO_REGNUM
2396 1.1.1.3 christos && insn.rs1 () == RISCV_ZERO_REGNUM
2397 1.1.1.3 christos && insn.imm_signed () == 0))
2398 1.1 christos {
2399 1.1 christos /* Handle: add x0, x0, 0 (NOP) */
2400 1.1 christos }
2401 1.1 christos else if (insn.opcode () == riscv_insn::AUIPC)
2402 1.1.1.3 christos {
2403 1.1.1.3 christos gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
2404 1.1.1.3 christos regs[insn.rd ()] = pv_constant (cur_pc + insn.imm_signed ());
2405 1.1.1.3 christos }
2406 1.1.1.4 christos else if (insn.opcode () == riscv_insn::LUI
2407 1.1.1.4 christos || insn.opcode () == riscv_insn::LI)
2408 1.1.1.3 christos {
2409 1.1 christos /* Handle: lui REG, n
2410 1.1.1.4 christos or: li REG, n */
2411 1.1.1.3 christos gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
2412 1.1.1.3 christos regs[insn.rd ()] = pv_constant (insn.imm_signed ());
2413 1.1.1.3 christos }
2414 1.1 christos else if (insn.opcode () == riscv_insn::ADDI)
2415 1.1.1.3 christos {
2416 1.1.1.3 christos /* Handle: addi REG1, REG2, IMM */
2417 1.1.1.3 christos gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
2418 1.1.1.3 christos gdb_assert (insn.rs1 () < RISCV_NUM_INTEGER_REGS);
2419 1.1.1.3 christos regs[insn.rd ()]
2420 1.1.1.3 christos = pv_add_constant (regs[insn.rs1 ()], insn.imm_signed ());
2421 1.1.1.3 christos }
2422 1.1 christos else if (insn.opcode () == riscv_insn::ADD)
2423 1.1.1.3 christos {
2424 1.1.1.3 christos /* Handle: add REG1, REG2, REG3 */
2425 1.1.1.3 christos gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
2426 1.1.1.3 christos gdb_assert (insn.rs1 () < RISCV_NUM_INTEGER_REGS);
2427 1.1.1.3 christos gdb_assert (insn.rs2 () < RISCV_NUM_INTEGER_REGS);
2428 1.1.1.3 christos regs[insn.rd ()] = pv_add (regs[insn.rs1 ()], regs[insn.rs2 ()]);
2429 1.1.1.3 christos }
2430 1.1.1.3 christos else if (insn.opcode () == riscv_insn::LD
2431 1.1.1.3 christos || insn.opcode () == riscv_insn::LW)
2432 1.1.1.3 christos {
2433 1.1.1.3 christos /* Handle: ld reg, offset(rs1)
2434 1.1.1.3 christos or: c.ld reg, offset(rs1)
2435 1.1.1.3 christos or: lw reg, offset(rs1)
2436 1.1.1.3 christos or: c.lw reg, offset(rs1) */
2437 1.1.1.3 christos gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
2438 1.1.1.3 christos gdb_assert (insn.rs1 () < RISCV_NUM_INTEGER_REGS);
2439 1.1.1.3 christos regs[insn.rd ()]
2440 1.1.1.3 christos = stack.fetch (pv_add_constant (regs[insn.rs1 ()],
2441 1.1.1.3 christos insn.imm_signed ()),
2442 1.1.1.3 christos (insn.opcode () == riscv_insn::LW ? 4 : 8));
2443 1.1.1.3 christos }
2444 1.1.1.3 christos else if (insn.opcode () == riscv_insn::MV)
2445 1.1.1.3 christos {
2446 1.1.1.3 christos /* Handle: c.mv RD, RS2 */
2447 1.1.1.3 christos gdb_assert (insn.rd () < RISCV_NUM_INTEGER_REGS);
2448 1.1.1.3 christos gdb_assert (insn.rs2 () < RISCV_NUM_INTEGER_REGS);
2449 1.1.1.3 christos gdb_assert (insn.rs2 () > 0);
2450 1.1.1.3 christos regs[insn.rd ()] = regs[insn.rs2 ()];
2451 1.1.1.3 christos }
2452 1.1 christos else
2453 1.1 christos {
2454 1.1 christos end_prologue_addr = cur_pc;
2455 1.1 christos break;
2456 1.1 christos }
2457 1.1 christos }
2458 1.1 christos
2459 1.1 christos if (end_prologue_addr == 0)
2460 1.1 christos end_prologue_addr = cur_pc;
2461 1.1 christos
2462 1.1.1.4 christos riscv_unwinder_debug_printf ("end of prologue at %s",
2463 1.1.1.4 christos core_addr_to_string (end_prologue_addr));
2464 1.1 christos
2465 1.1 christos if (cache != NULL)
2466 1.1 christos {
2467 1.1 christos /* Figure out if it is a frame pointer or just a stack pointer. Also
2468 1.1.1.3 christos the offset held in the pv_t is from the original register value to
2469 1.1.1.3 christos the current value, which for a grows down stack means a negative
2470 1.1.1.3 christos value. The FRAME_BASE_OFFSET is the negation of this, how to get
2471 1.1.1.3 christos from the current value to the original value. */
2472 1.1 christos if (pv_is_register (regs[RISCV_FP_REGNUM], RISCV_SP_REGNUM))
2473 1.1 christos {
2474 1.1.1.3 christos cache->frame_base_reg = RISCV_FP_REGNUM;
2475 1.1.1.3 christos cache->frame_base_offset = -regs[RISCV_FP_REGNUM].k;
2476 1.1 christos }
2477 1.1 christos else
2478 1.1 christos {
2479 1.1.1.3 christos cache->frame_base_reg = RISCV_SP_REGNUM;
2480 1.1.1.3 christos cache->frame_base_offset = -regs[RISCV_SP_REGNUM].k;
2481 1.1 christos }
2482 1.1 christos
2483 1.1.1.4 christos /* Check to see if we are located near to a return instruction in
2484 1.1.1.4 christos this function. If we are then the one or both of the stack
2485 1.1.1.4 christos pointer and frame pointer may have been restored to their previous
2486 1.1.1.4 christos value. If we can spot this situation then we can adjust which
2487 1.1.1.4 christos register and offset we use for the frame base. */
2488 1.1.1.4 christos if (cache->frame_base_reg != RISCV_SP_REGNUM
2489 1.1.1.4 christos || cache->frame_base_offset != 0)
2490 1.1.1.4 christos {
2491 1.1.1.4 christos int sp_offset;
2492 1.1.1.4 christos
2493 1.1.1.4 christos if (riscv_detect_end_of_function (gdbarch, original_end_pc,
2494 1.1.1.4 christos &sp_offset))
2495 1.1.1.4 christos {
2496 1.1.1.4 christos riscv_unwinder_debug_printf
2497 1.1.1.4 christos ("in function epilogue at %s, stack offset is %d",
2498 1.1.1.4 christos core_addr_to_string (original_end_pc), sp_offset);
2499 1.1.1.4 christos cache->frame_base_reg= RISCV_SP_REGNUM;
2500 1.1.1.4 christos cache->frame_base_offset = sp_offset;
2501 1.1.1.4 christos }
2502 1.1.1.4 christos }
2503 1.1.1.4 christos
2504 1.1 christos /* Assign offset from old SP to all saved registers. As we don't
2505 1.1.1.3 christos have the previous value for the frame base register at this
2506 1.1.1.3 christos point, we store the offset as the address in the trad_frame, and
2507 1.1.1.3 christos then convert this to an actual address later. */
2508 1.1 christos for (int i = 0; i <= RISCV_NUM_INTEGER_REGS; i++)
2509 1.1 christos {
2510 1.1 christos CORE_ADDR offset;
2511 1.1 christos if (stack.find_reg (gdbarch, i, &offset))
2512 1.1.1.3 christos {
2513 1.1.1.4 christos /* Display OFFSET as a signed value, the offsets are from the
2514 1.1.1.4 christos frame base address to the registers location on the stack,
2515 1.1.1.4 christos with a descending stack this means the offsets are always
2516 1.1.1.4 christos negative. */
2517 1.1.1.4 christos riscv_unwinder_debug_printf ("register $%s at stack offset %s",
2518 1.1.1.4 christos gdbarch_register_name (gdbarch, i),
2519 1.1.1.4 christos plongest ((LONGEST) offset));
2520 1.1.1.3 christos cache->regs[i].set_addr (offset);
2521 1.1.1.3 christos }
2522 1.1 christos }
2523 1.1 christos }
2524 1.1 christos
2525 1.1 christos return end_prologue_addr;
2526 1.1 christos }
2527 1.1 christos
2528 1.1 christos /* Implement the riscv_skip_prologue gdbarch method. */
2529 1.1 christos
2530 1.1 christos static CORE_ADDR
2531 1.1 christos riscv_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
2532 1.1 christos {
2533 1.1 christos CORE_ADDR func_addr;
2534 1.1 christos
2535 1.1 christos /* See if we can determine the end of the prologue via the symbol
2536 1.1 christos table. If so, then return either PC, or the PC after the
2537 1.1 christos prologue, whichever is greater. */
2538 1.1 christos if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
2539 1.1 christos {
2540 1.1 christos CORE_ADDR post_prologue_pc
2541 1.1 christos = skip_prologue_using_sal (gdbarch, func_addr);
2542 1.1 christos
2543 1.1 christos if (post_prologue_pc != 0)
2544 1.1 christos return std::max (pc, post_prologue_pc);
2545 1.1 christos }
2546 1.1 christos
2547 1.1 christos /* Can't determine prologue from the symbol table, need to examine
2548 1.1 christos instructions. Pass -1 for the end address to indicate the prologue
2549 1.1 christos scanner can scan as far as it needs to find the end of the prologue. */
2550 1.1 christos return riscv_scan_prologue (gdbarch, pc, ((CORE_ADDR) -1), NULL);
2551 1.1 christos }
2552 1.1 christos
2553 1.1 christos /* Implement the gdbarch push dummy code callback. */
2554 1.1 christos
2555 1.1 christos static CORE_ADDR
2556 1.1 christos riscv_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
2557 1.1 christos CORE_ADDR funaddr, struct value **args, int nargs,
2558 1.1 christos struct type *value_type, CORE_ADDR *real_pc,
2559 1.1 christos CORE_ADDR *bp_addr, struct regcache *regcache)
2560 1.1 christos {
2561 1.1.1.2 christos /* A nop instruction is 'add x0, x0, 0'. */
2562 1.1.1.2 christos static const gdb_byte nop_insn[] = { 0x13, 0x00, 0x00, 0x00 };
2563 1.1.1.2 christos
2564 1.1 christos /* Allocate space for a breakpoint, and keep the stack correctly
2565 1.1.1.2 christos aligned. The space allocated here must be at least big enough to
2566 1.1.1.2 christos accommodate the NOP_INSN defined above. */
2567 1.1 christos sp -= 16;
2568 1.1 christos *bp_addr = sp;
2569 1.1 christos *real_pc = funaddr;
2570 1.1 christos
2571 1.1.1.2 christos /* When we insert a breakpoint we select whether to use a compressed
2572 1.1.1.2 christos breakpoint or not based on the existing contents of the memory.
2573 1.1 christos
2574 1.1.1.2 christos If the breakpoint is being placed onto the stack as part of setting up
2575 1.1.1.2 christos for an inferior call from GDB, then the existing stack contents may
2576 1.1.1.2 christos randomly appear to be a compressed instruction, causing GDB to insert
2577 1.1.1.2 christos a compressed breakpoint. If this happens on a target that does not
2578 1.1.1.2 christos support compressed instructions then this could cause problems.
2579 1.1.1.2 christos
2580 1.1.1.2 christos To prevent this issue we write an uncompressed nop onto the stack at
2581 1.1.1.2 christos the location where the breakpoint will be inserted. In this way we
2582 1.1.1.2 christos ensure that we always use an uncompressed breakpoint, which should
2583 1.1.1.2 christos work on all targets.
2584 1.1.1.2 christos
2585 1.1.1.2 christos We call TARGET_WRITE_MEMORY here so that if the write fails we don't
2586 1.1.1.2 christos throw an exception. Instead we ignore the error and move on. The
2587 1.1.1.2 christos assumption is that either GDB will error later when actually trying to
2588 1.1.1.2 christos insert a software breakpoint, or GDB will use hardware breakpoints and
2589 1.1.1.2 christos there will be no need to write to memory later. */
2590 1.1.1.2 christos int status = target_write_memory (*bp_addr, nop_insn, sizeof (nop_insn));
2591 1.1.1.2 christos
2592 1.1.1.4 christos riscv_infcall_debug_printf ("writing %s-byte nop instruction to %s: %s",
2593 1.1.1.4 christos plongest (sizeof (nop_insn)),
2594 1.1.1.4 christos paddress (gdbarch, *bp_addr),
2595 1.1.1.4 christos (status == 0 ? "success" : "failed"));
2596 1.1 christos
2597 1.1.1.2 christos return sp;
2598 1.1.1.2 christos }
2599 1.1 christos
2600 1.1.1.2 christos /* Implement the gdbarch type alignment method, overrides the generic
2601 1.1.1.2 christos alignment algorithm for anything that is RISC-V specific. */
2602 1.1 christos
2603 1.1.1.2 christos static ULONGEST
2604 1.1.1.2 christos riscv_type_align (gdbarch *gdbarch, type *type)
2605 1.1.1.2 christos {
2606 1.1.1.2 christos type = check_typedef (type);
2607 1.1.1.3 christos if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
2608 1.1.1.3 christos return std::min (type->length (), (ULONGEST) BIGGEST_ALIGNMENT);
2609 1.1 christos
2610 1.1.1.2 christos /* Anything else will be aligned by the generic code. */
2611 1.1.1.2 christos return 0;
2612 1.1 christos }
2613 1.1 christos
2614 1.1 christos /* Holds information about a single argument either being passed to an
2615 1.1 christos inferior function, or returned from an inferior function. This includes
2616 1.1 christos information about the size, type, etc of the argument, and also
2617 1.1 christos information about how the argument will be passed (or returned). */
2618 1.1 christos
2619 1.1 christos struct riscv_arg_info
2620 1.1 christos {
2621 1.1 christos /* Contents of the argument. */
2622 1.1 christos const gdb_byte *contents;
2623 1.1 christos
2624 1.1 christos /* Length of argument. */
2625 1.1 christos int length;
2626 1.1 christos
2627 1.1 christos /* Alignment required for an argument of this type. */
2628 1.1 christos int align;
2629 1.1 christos
2630 1.1 christos /* The type for this argument. */
2631 1.1 christos struct type *type;
2632 1.1 christos
2633 1.1 christos /* Each argument can have either 1 or 2 locations assigned to it. Each
2634 1.1 christos location describes where part of the argument will be placed. The
2635 1.1 christos second location is valid based on the LOC_TYPE and C_LENGTH fields
2636 1.1 christos of the first location (which is always valid). */
2637 1.1 christos struct location
2638 1.1 christos {
2639 1.1 christos /* What type of location this is. */
2640 1.1 christos enum location_type
2641 1.1 christos {
2642 1.1 christos /* Argument passed in a register. */
2643 1.1 christos in_reg,
2644 1.1 christos
2645 1.1 christos /* Argument passed as an on stack argument. */
2646 1.1 christos on_stack,
2647 1.1 christos
2648 1.1 christos /* Argument passed by reference. The second location is always
2649 1.1 christos valid for a BY_REF argument, and describes where the address
2650 1.1 christos of the BY_REF argument should be placed. */
2651 1.1 christos by_ref
2652 1.1 christos } loc_type;
2653 1.1 christos
2654 1.1 christos /* Information that depends on the location type. */
2655 1.1 christos union
2656 1.1 christos {
2657 1.1 christos /* Which register number to use. */
2658 1.1 christos int regno;
2659 1.1 christos
2660 1.1 christos /* The offset into the stack region. */
2661 1.1 christos int offset;
2662 1.1 christos } loc_data;
2663 1.1 christos
2664 1.1 christos /* The length of contents covered by this location. If this is less
2665 1.1 christos than the total length of the argument, then the second location
2666 1.1 christos will be valid, and will describe where the rest of the argument
2667 1.1 christos will go. */
2668 1.1 christos int c_length;
2669 1.1 christos
2670 1.1.1.2 christos /* The offset within CONTENTS for this part of the argument. This can
2671 1.1.1.2 christos be non-zero even for the first part (the first field of a struct can
2672 1.1.1.2 christos have a non-zero offset due to padding). For the second part of the
2673 1.1 christos argument, this might be the C_LENGTH value of the first part,
2674 1.1 christos however, if we are passing a structure in two registers, and there's
2675 1.1 christos is padding between the first and second field, then this offset
2676 1.1 christos might be greater than the length of the first argument part. When
2677 1.1 christos the second argument location is not holding part of the argument
2678 1.1 christos value, but is instead holding the address of a reference argument,
2679 1.1 christos then this offset will be set to 0. */
2680 1.1 christos int c_offset;
2681 1.1 christos } argloc[2];
2682 1.1 christos
2683 1.1 christos /* TRUE if this is an unnamed argument. */
2684 1.1 christos bool is_unnamed;
2685 1.1 christos };
2686 1.1 christos
2687 1.1 christos /* Information about a set of registers being used for passing arguments as
2688 1.1 christos part of a function call. The register set must be numerically
2689 1.1 christos sequential from NEXT_REGNUM to LAST_REGNUM. The register set can be
2690 1.1 christos disabled from use by setting NEXT_REGNUM greater than LAST_REGNUM. */
2691 1.1 christos
2692 1.1 christos struct riscv_arg_reg
2693 1.1 christos {
2694 1.1 christos riscv_arg_reg (int first, int last)
2695 1.1 christos : next_regnum (first),
2696 1.1 christos last_regnum (last)
2697 1.1 christos {
2698 1.1 christos /* Nothing. */
2699 1.1 christos }
2700 1.1 christos
2701 1.1 christos /* The GDB register number to use in this set. */
2702 1.1 christos int next_regnum;
2703 1.1 christos
2704 1.1 christos /* The last GDB register number to use in this set. */
2705 1.1 christos int last_regnum;
2706 1.1 christos };
2707 1.1 christos
2708 1.1 christos /* Arguments can be passed as on stack arguments, or by reference. The
2709 1.1 christos on stack arguments must be in a continuous region starting from $sp,
2710 1.1 christos while the by reference arguments can be anywhere, but we'll put them
2711 1.1 christos on the stack after (at higher address) the on stack arguments.
2712 1.1 christos
2713 1.1 christos This might not be the right approach to take. The ABI is clear that
2714 1.1 christos an argument passed by reference can be modified by the callee, which
2715 1.1 christos us placing the argument (temporarily) onto the stack will not achieve
2716 1.1 christos (changes will be lost). There's also the possibility that very large
2717 1.1 christos arguments could overflow the stack.
2718 1.1 christos
2719 1.1 christos This struct is used to track offset into these two areas for where
2720 1.1 christos arguments are to be placed. */
2721 1.1 christos struct riscv_memory_offsets
2722 1.1 christos {
2723 1.1 christos riscv_memory_offsets ()
2724 1.1 christos : arg_offset (0),
2725 1.1 christos ref_offset (0)
2726 1.1 christos {
2727 1.1 christos /* Nothing. */
2728 1.1 christos }
2729 1.1 christos
2730 1.1 christos /* Offset into on stack argument area. */
2731 1.1 christos int arg_offset;
2732 1.1 christos
2733 1.1 christos /* Offset into the pass by reference area. */
2734 1.1 christos int ref_offset;
2735 1.1 christos };
2736 1.1 christos
2737 1.1 christos /* Holds information about where arguments to a call will be placed. This
2738 1.1 christos is updated as arguments are added onto the call, and can be used to
2739 1.1 christos figure out where the next argument should be placed. */
2740 1.1 christos
2741 1.1 christos struct riscv_call_info
2742 1.1 christos {
2743 1.1 christos riscv_call_info (struct gdbarch *gdbarch)
2744 1.1 christos : int_regs (RISCV_A0_REGNUM, RISCV_A0_REGNUM + 7),
2745 1.1 christos float_regs (RISCV_FA0_REGNUM, RISCV_FA0_REGNUM + 7)
2746 1.1 christos {
2747 1.1 christos xlen = riscv_abi_xlen (gdbarch);
2748 1.1 christos flen = riscv_abi_flen (gdbarch);
2749 1.1 christos
2750 1.1.1.3 christos /* Reduce the number of integer argument registers when using the
2751 1.1.1.3 christos embedded abi (i.e. rv32e). */
2752 1.1.1.3 christos if (riscv_abi_embedded (gdbarch))
2753 1.1.1.3 christos int_regs.last_regnum = RISCV_A0_REGNUM + 5;
2754 1.1.1.3 christos
2755 1.1 christos /* Disable use of floating point registers if needed. */
2756 1.1 christos if (!riscv_has_fp_abi (gdbarch))
2757 1.1 christos float_regs.next_regnum = float_regs.last_regnum + 1;
2758 1.1 christos }
2759 1.1 christos
2760 1.1 christos /* Track the memory areas used for holding in-memory arguments to a
2761 1.1 christos call. */
2762 1.1 christos struct riscv_memory_offsets memory;
2763 1.1 christos
2764 1.1 christos /* Holds information about the next integer register to use for passing
2765 1.1 christos an argument. */
2766 1.1 christos struct riscv_arg_reg int_regs;
2767 1.1 christos
2768 1.1 christos /* Holds information about the next floating point register to use for
2769 1.1 christos passing an argument. */
2770 1.1 christos struct riscv_arg_reg float_regs;
2771 1.1 christos
2772 1.1 christos /* The XLEN and FLEN are copied in to this structure for convenience, and
2773 1.1 christos are just the results of calling RISCV_ABI_XLEN and RISCV_ABI_FLEN. */
2774 1.1 christos int xlen;
2775 1.1 christos int flen;
2776 1.1 christos };
2777 1.1 christos
2778 1.1 christos /* Return the number of registers available for use as parameters in the
2779 1.1 christos register set REG. Returned value can be 0 or more. */
2780 1.1 christos
2781 1.1 christos static int
2782 1.1 christos riscv_arg_regs_available (struct riscv_arg_reg *reg)
2783 1.1 christos {
2784 1.1 christos if (reg->next_regnum > reg->last_regnum)
2785 1.1 christos return 0;
2786 1.1 christos
2787 1.1 christos return (reg->last_regnum - reg->next_regnum + 1);
2788 1.1 christos }
2789 1.1 christos
2790 1.1 christos /* If there is at least one register available in the register set REG then
2791 1.1 christos the next register from REG is assigned to LOC and the length field of
2792 1.1 christos LOC is updated to LENGTH. The register set REG is updated to indicate
2793 1.1 christos that the assigned register is no longer available and the function
2794 1.1 christos returns true.
2795 1.1 christos
2796 1.1 christos If there are no registers available in REG then the function returns
2797 1.1 christos false, and LOC and REG are unchanged. */
2798 1.1 christos
2799 1.1 christos static bool
2800 1.1 christos riscv_assign_reg_location (struct riscv_arg_info::location *loc,
2801 1.1 christos struct riscv_arg_reg *reg,
2802 1.1 christos int length, int offset)
2803 1.1 christos {
2804 1.1 christos if (reg->next_regnum <= reg->last_regnum)
2805 1.1 christos {
2806 1.1 christos loc->loc_type = riscv_arg_info::location::in_reg;
2807 1.1 christos loc->loc_data.regno = reg->next_regnum;
2808 1.1 christos reg->next_regnum++;
2809 1.1 christos loc->c_length = length;
2810 1.1 christos loc->c_offset = offset;
2811 1.1 christos return true;
2812 1.1 christos }
2813 1.1 christos
2814 1.1 christos return false;
2815 1.1 christos }
2816 1.1 christos
2817 1.1 christos /* Assign LOC a location as the next stack parameter, and update MEMORY to
2818 1.1 christos record that an area of stack has been used to hold the parameter
2819 1.1 christos described by LOC.
2820 1.1 christos
2821 1.1 christos The length field of LOC is updated to LENGTH, the length of the
2822 1.1 christos parameter being stored, and ALIGN is the alignment required by the
2823 1.1 christos parameter, which will affect how memory is allocated out of MEMORY. */
2824 1.1 christos
2825 1.1 christos static void
2826 1.1 christos riscv_assign_stack_location (struct riscv_arg_info::location *loc,
2827 1.1 christos struct riscv_memory_offsets *memory,
2828 1.1 christos int length, int align)
2829 1.1 christos {
2830 1.1 christos loc->loc_type = riscv_arg_info::location::on_stack;
2831 1.1 christos memory->arg_offset
2832 1.1 christos = align_up (memory->arg_offset, align);
2833 1.1 christos loc->loc_data.offset = memory->arg_offset;
2834 1.1 christos memory->arg_offset += length;
2835 1.1 christos loc->c_length = length;
2836 1.1 christos
2837 1.1 christos /* Offset is always 0, either we're the first location part, in which
2838 1.1 christos case we're reading content from the start of the argument, or we're
2839 1.1 christos passing the address of a reference argument, so 0. */
2840 1.1 christos loc->c_offset = 0;
2841 1.1 christos }
2842 1.1 christos
2843 1.1 christos /* Update AINFO, which describes an argument that should be passed or
2844 1.1 christos returned using the integer ABI. The argloc fields within AINFO are
2845 1.1 christos updated to describe the location in which the argument will be passed to
2846 1.1 christos a function, or returned from a function.
2847 1.1 christos
2848 1.1 christos The CINFO structure contains the ongoing call information, the holds
2849 1.1 christos information such as which argument registers are remaining to be
2850 1.1 christos assigned to parameter, and how much memory has been used by parameters
2851 1.1 christos so far.
2852 1.1 christos
2853 1.1 christos By examining the state of CINFO a suitable location can be selected,
2854 1.1 christos and assigned to AINFO. */
2855 1.1 christos
2856 1.1 christos static void
2857 1.1 christos riscv_call_arg_scalar_int (struct riscv_arg_info *ainfo,
2858 1.1 christos struct riscv_call_info *cinfo)
2859 1.1 christos {
2860 1.1.1.4 christos if (TYPE_HAS_DYNAMIC_LENGTH (ainfo->type)
2861 1.1.1.4 christos || ainfo->length > (2 * cinfo->xlen))
2862 1.1 christos {
2863 1.1 christos /* Argument is going to be passed by reference. */
2864 1.1 christos ainfo->argloc[0].loc_type
2865 1.1 christos = riscv_arg_info::location::by_ref;
2866 1.1 christos cinfo->memory.ref_offset
2867 1.1 christos = align_up (cinfo->memory.ref_offset, ainfo->align);
2868 1.1 christos ainfo->argloc[0].loc_data.offset = cinfo->memory.ref_offset;
2869 1.1 christos cinfo->memory.ref_offset += ainfo->length;
2870 1.1 christos ainfo->argloc[0].c_length = ainfo->length;
2871 1.1 christos
2872 1.1 christos /* The second location for this argument is given over to holding the
2873 1.1 christos address of the by-reference data. Pass 0 for the offset as this
2874 1.1 christos is not part of the actual argument value. */
2875 1.1 christos if (!riscv_assign_reg_location (&ainfo->argloc[1],
2876 1.1 christos &cinfo->int_regs,
2877 1.1 christos cinfo->xlen, 0))
2878 1.1 christos riscv_assign_stack_location (&ainfo->argloc[1],
2879 1.1 christos &cinfo->memory, cinfo->xlen,
2880 1.1 christos cinfo->xlen);
2881 1.1 christos }
2882 1.1 christos else
2883 1.1 christos {
2884 1.1 christos int len = std::min (ainfo->length, cinfo->xlen);
2885 1.1 christos int align = std::max (ainfo->align, cinfo->xlen);
2886 1.1 christos
2887 1.1 christos /* Unnamed arguments in registers that require 2*XLEN alignment are
2888 1.1 christos passed in an aligned register pair. */
2889 1.1 christos if (ainfo->is_unnamed && (align == cinfo->xlen * 2)
2890 1.1 christos && cinfo->int_regs.next_regnum & 1)
2891 1.1 christos cinfo->int_regs.next_regnum++;
2892 1.1 christos
2893 1.1 christos if (!riscv_assign_reg_location (&ainfo->argloc[0],
2894 1.1 christos &cinfo->int_regs, len, 0))
2895 1.1 christos riscv_assign_stack_location (&ainfo->argloc[0],
2896 1.1 christos &cinfo->memory, len, align);
2897 1.1 christos
2898 1.1 christos if (len < ainfo->length)
2899 1.1 christos {
2900 1.1 christos len = ainfo->length - len;
2901 1.1 christos if (!riscv_assign_reg_location (&ainfo->argloc[1],
2902 1.1 christos &cinfo->int_regs, len,
2903 1.1 christos cinfo->xlen))
2904 1.1 christos riscv_assign_stack_location (&ainfo->argloc[1],
2905 1.1 christos &cinfo->memory, len, cinfo->xlen);
2906 1.1 christos }
2907 1.1 christos }
2908 1.1 christos }
2909 1.1 christos
2910 1.1 christos /* Like RISCV_CALL_ARG_SCALAR_INT, except the argument described by AINFO
2911 1.1 christos is being passed with the floating point ABI. */
2912 1.1 christos
2913 1.1 christos static void
2914 1.1 christos riscv_call_arg_scalar_float (struct riscv_arg_info *ainfo,
2915 1.1 christos struct riscv_call_info *cinfo)
2916 1.1 christos {
2917 1.1 christos if (ainfo->length > cinfo->flen || ainfo->is_unnamed)
2918 1.1 christos return riscv_call_arg_scalar_int (ainfo, cinfo);
2919 1.1 christos else
2920 1.1 christos {
2921 1.1 christos if (!riscv_assign_reg_location (&ainfo->argloc[0],
2922 1.1 christos &cinfo->float_regs,
2923 1.1 christos ainfo->length, 0))
2924 1.1 christos return riscv_call_arg_scalar_int (ainfo, cinfo);
2925 1.1 christos }
2926 1.1 christos }
2927 1.1 christos
2928 1.1 christos /* Like RISCV_CALL_ARG_SCALAR_INT, except the argument described by AINFO
2929 1.1 christos is a complex floating point argument, and is therefore handled
2930 1.1 christos differently to other argument types. */
2931 1.1 christos
2932 1.1 christos static void
2933 1.1 christos riscv_call_arg_complex_float (struct riscv_arg_info *ainfo,
2934 1.1 christos struct riscv_call_info *cinfo)
2935 1.1 christos {
2936 1.1 christos if (ainfo->length <= (2 * cinfo->flen)
2937 1.1 christos && riscv_arg_regs_available (&cinfo->float_regs) >= 2
2938 1.1 christos && !ainfo->is_unnamed)
2939 1.1 christos {
2940 1.1 christos bool result;
2941 1.1 christos int len = ainfo->length / 2;
2942 1.1 christos
2943 1.1 christos result = riscv_assign_reg_location (&ainfo->argloc[0],
2944 1.1.1.2 christos &cinfo->float_regs, len, 0);
2945 1.1 christos gdb_assert (result);
2946 1.1 christos
2947 1.1 christos result = riscv_assign_reg_location (&ainfo->argloc[1],
2948 1.1 christos &cinfo->float_regs, len, len);
2949 1.1 christos gdb_assert (result);
2950 1.1 christos }
2951 1.1 christos else
2952 1.1 christos return riscv_call_arg_scalar_int (ainfo, cinfo);
2953 1.1 christos }
2954 1.1 christos
2955 1.1 christos /* A structure used for holding information about a structure type within
2956 1.1 christos the inferior program. The RiscV ABI has special rules for handling some
2957 1.1 christos structures with a single field or with two fields. The counting of
2958 1.1 christos fields here is done after flattening out all nested structures. */
2959 1.1 christos
2960 1.1 christos class riscv_struct_info
2961 1.1 christos {
2962 1.1 christos public:
2963 1.1 christos riscv_struct_info ()
2964 1.1 christos : m_number_of_fields (0),
2965 1.1.1.2 christos m_types { nullptr, nullptr },
2966 1.1.1.2 christos m_offsets { 0, 0 }
2967 1.1 christos {
2968 1.1 christos /* Nothing. */
2969 1.1 christos }
2970 1.1 christos
2971 1.1 christos /* Analyse TYPE descending into nested structures, count the number of
2972 1.1 christos scalar fields and record the types of the first two fields found. */
2973 1.1.1.2 christos void analyse (struct type *type)
2974 1.1.1.2 christos {
2975 1.1.1.2 christos analyse_inner (type, 0);
2976 1.1.1.2 christos }
2977 1.1 christos
2978 1.1 christos /* The number of scalar fields found in the analysed type. This is
2979 1.1 christos currently only accurate if the value returned is 0, 1, or 2 as the
2980 1.1 christos analysis stops counting when the number of fields is 3. This is
2981 1.1 christos because the RiscV ABI only has special cases for 1 or 2 fields,
2982 1.1 christos anything else we just don't care about. */
2983 1.1 christos int number_of_fields () const
2984 1.1 christos { return m_number_of_fields; }
2985 1.1 christos
2986 1.1 christos /* Return the type for scalar field INDEX within the analysed type. Will
2987 1.1 christos return nullptr if there is no field at that index. Only INDEX values
2988 1.1 christos 0 and 1 can be requested as the RiscV ABI only has special cases for
2989 1.1 christos structures with 1 or 2 fields. */
2990 1.1 christos struct type *field_type (int index) const
2991 1.1 christos {
2992 1.1 christos gdb_assert (index < (sizeof (m_types) / sizeof (m_types[0])));
2993 1.1 christos return m_types[index];
2994 1.1 christos }
2995 1.1 christos
2996 1.1.1.2 christos /* Return the offset of scalar field INDEX within the analysed type. Will
2997 1.1.1.2 christos return 0 if there is no field at that index. Only INDEX values 0 and
2998 1.1.1.2 christos 1 can be requested as the RiscV ABI only has special cases for
2999 1.1.1.2 christos structures with 1 or 2 fields. */
3000 1.1.1.2 christos int field_offset (int index) const
3001 1.1.1.2 christos {
3002 1.1.1.2 christos gdb_assert (index < (sizeof (m_offsets) / sizeof (m_offsets[0])));
3003 1.1.1.2 christos return m_offsets[index];
3004 1.1.1.2 christos }
3005 1.1.1.2 christos
3006 1.1 christos private:
3007 1.1 christos /* The number of scalar fields found within the structure after recursing
3008 1.1 christos into nested structures. */
3009 1.1 christos int m_number_of_fields;
3010 1.1 christos
3011 1.1 christos /* The types of the first two scalar fields found within the structure
3012 1.1 christos after recursing into nested structures. */
3013 1.1 christos struct type *m_types[2];
3014 1.1.1.2 christos
3015 1.1.1.2 christos /* The offsets of the first two scalar fields found within the structure
3016 1.1.1.2 christos after recursing into nested structures. */
3017 1.1.1.2 christos int m_offsets[2];
3018 1.1.1.2 christos
3019 1.1.1.2 christos /* Recursive core for ANALYSE, the OFFSET parameter tracks the byte
3020 1.1.1.2 christos offset from the start of the top level structure being analysed. */
3021 1.1.1.2 christos void analyse_inner (struct type *type, int offset);
3022 1.1 christos };
3023 1.1 christos
3024 1.1.1.2 christos /* See description in class declaration. */
3025 1.1 christos
3026 1.1 christos void
3027 1.1.1.2 christos riscv_struct_info::analyse_inner (struct type *type, int offset)
3028 1.1 christos {
3029 1.1.1.2 christos unsigned int count = type->num_fields ();
3030 1.1 christos unsigned int i;
3031 1.1 christos
3032 1.1 christos for (i = 0; i < count; ++i)
3033 1.1 christos {
3034 1.1.1.3 christos if (type->field (i).loc_kind () != FIELD_LOC_KIND_BITPOS)
3035 1.1 christos continue;
3036 1.1 christos
3037 1.1.1.2 christos struct type *field_type = type->field (i).type ();
3038 1.1 christos field_type = check_typedef (field_type);
3039 1.1.1.2 christos int field_offset
3040 1.1.1.3 christos = offset + type->field (i).loc_bitpos () / TARGET_CHAR_BIT;
3041 1.1 christos
3042 1.1.1.2 christos switch (field_type->code ())
3043 1.1 christos {
3044 1.1 christos case TYPE_CODE_STRUCT:
3045 1.1.1.2 christos analyse_inner (field_type, field_offset);
3046 1.1 christos break;
3047 1.1 christos
3048 1.1 christos default:
3049 1.1 christos /* RiscV only flattens out structures. Anything else does not
3050 1.1 christos need to be flattened, we just record the type, and when we
3051 1.1 christos look at the analysis results we'll realise this is not a
3052 1.1 christos structure we can special case, and pass the structure in
3053 1.1 christos memory. */
3054 1.1 christos if (m_number_of_fields < 2)
3055 1.1.1.2 christos {
3056 1.1.1.2 christos m_types[m_number_of_fields] = field_type;
3057 1.1.1.2 christos m_offsets[m_number_of_fields] = field_offset;
3058 1.1.1.2 christos }
3059 1.1 christos m_number_of_fields++;
3060 1.1 christos break;
3061 1.1 christos }
3062 1.1 christos
3063 1.1 christos /* RiscV only has special handling for structures with 1 or 2 scalar
3064 1.1 christos fields, any more than that and the structure is just passed in
3065 1.1 christos memory. We can safely drop out early when we find 3 or more
3066 1.1 christos fields then. */
3067 1.1 christos
3068 1.1 christos if (m_number_of_fields > 2)
3069 1.1 christos return;
3070 1.1 christos }
3071 1.1 christos }
3072 1.1 christos
3073 1.1 christos /* Like RISCV_CALL_ARG_SCALAR_INT, except the argument described by AINFO
3074 1.1 christos is a structure. Small structures on RiscV have some special case
3075 1.1 christos handling in order that the structure might be passed in register.
3076 1.1 christos Larger structures are passed in memory. After assigning location
3077 1.1 christos information to AINFO, CINFO will have been updated. */
3078 1.1 christos
3079 1.1 christos static void
3080 1.1 christos riscv_call_arg_struct (struct riscv_arg_info *ainfo,
3081 1.1 christos struct riscv_call_info *cinfo)
3082 1.1 christos {
3083 1.1 christos if (riscv_arg_regs_available (&cinfo->float_regs) >= 1)
3084 1.1 christos {
3085 1.1 christos struct riscv_struct_info sinfo;
3086 1.1 christos
3087 1.1 christos sinfo.analyse (ainfo->type);
3088 1.1 christos if (sinfo.number_of_fields () == 1
3089 1.1.1.2 christos && sinfo.field_type(0)->code () == TYPE_CODE_COMPLEX)
3090 1.1 christos {
3091 1.1.1.2 christos /* The following is similar to RISCV_CALL_ARG_COMPLEX_FLOAT,
3092 1.1.1.2 christos except we use the type of the complex field instead of the
3093 1.1.1.2 christos type from AINFO, and the first location might be at a non-zero
3094 1.1.1.2 christos offset. */
3095 1.1.1.3 christos if (sinfo.field_type (0)->length () <= (2 * cinfo->flen)
3096 1.1.1.2 christos && riscv_arg_regs_available (&cinfo->float_regs) >= 2
3097 1.1.1.2 christos && !ainfo->is_unnamed)
3098 1.1.1.2 christos {
3099 1.1.1.2 christos bool result;
3100 1.1.1.3 christos int len = sinfo.field_type (0)->length () / 2;
3101 1.1.1.2 christos int offset = sinfo.field_offset (0);
3102 1.1.1.2 christos
3103 1.1.1.2 christos result = riscv_assign_reg_location (&ainfo->argloc[0],
3104 1.1.1.2 christos &cinfo->float_regs, len,
3105 1.1.1.2 christos offset);
3106 1.1.1.2 christos gdb_assert (result);
3107 1.1.1.2 christos
3108 1.1.1.2 christos result = riscv_assign_reg_location (&ainfo->argloc[1],
3109 1.1.1.2 christos &cinfo->float_regs, len,
3110 1.1.1.2 christos (offset + len));
3111 1.1.1.2 christos gdb_assert (result);
3112 1.1.1.2 christos }
3113 1.1.1.2 christos else
3114 1.1.1.2 christos riscv_call_arg_scalar_int (ainfo, cinfo);
3115 1.1.1.2 christos return;
3116 1.1 christos }
3117 1.1 christos
3118 1.1 christos if (sinfo.number_of_fields () == 1
3119 1.1.1.2 christos && sinfo.field_type(0)->code () == TYPE_CODE_FLT)
3120 1.1 christos {
3121 1.1.1.2 christos /* The following is similar to RISCV_CALL_ARG_SCALAR_FLOAT,
3122 1.1.1.2 christos except we use the type of the first scalar field instead of
3123 1.1.1.2 christos the type from AINFO. Also the location might be at a non-zero
3124 1.1.1.2 christos offset. */
3125 1.1.1.3 christos if (sinfo.field_type (0)->length () > cinfo->flen
3126 1.1.1.2 christos || ainfo->is_unnamed)
3127 1.1.1.2 christos riscv_call_arg_scalar_int (ainfo, cinfo);
3128 1.1.1.2 christos else
3129 1.1.1.2 christos {
3130 1.1.1.2 christos int offset = sinfo.field_offset (0);
3131 1.1.1.3 christos int len = sinfo.field_type (0)->length ();
3132 1.1.1.2 christos
3133 1.1.1.2 christos if (!riscv_assign_reg_location (&ainfo->argloc[0],
3134 1.1.1.2 christos &cinfo->float_regs,
3135 1.1.1.2 christos len, offset))
3136 1.1.1.2 christos riscv_call_arg_scalar_int (ainfo, cinfo);
3137 1.1.1.2 christos }
3138 1.1.1.2 christos return;
3139 1.1 christos }
3140 1.1 christos
3141 1.1 christos if (sinfo.number_of_fields () == 2
3142 1.1.1.2 christos && sinfo.field_type(0)->code () == TYPE_CODE_FLT
3143 1.1.1.3 christos && sinfo.field_type (0)->length () <= cinfo->flen
3144 1.1.1.2 christos && sinfo.field_type(1)->code () == TYPE_CODE_FLT
3145 1.1.1.3 christos && sinfo.field_type (1)->length () <= cinfo->flen
3146 1.1 christos && riscv_arg_regs_available (&cinfo->float_regs) >= 2)
3147 1.1 christos {
3148 1.1.1.3 christos int len0 = sinfo.field_type (0)->length ();
3149 1.1.1.2 christos int offset = sinfo.field_offset (0);
3150 1.1 christos if (!riscv_assign_reg_location (&ainfo->argloc[0],
3151 1.1.1.2 christos &cinfo->float_regs, len0, offset))
3152 1.1 christos error (_("failed during argument setup"));
3153 1.1 christos
3154 1.1.1.3 christos int len1 = sinfo.field_type (1)->length ();
3155 1.1.1.2 christos offset = sinfo.field_offset (1);
3156 1.1.1.3 christos gdb_assert (len1 <= (ainfo->type->length ()
3157 1.1.1.3 christos - sinfo.field_type (0)->length ()));
3158 1.1 christos
3159 1.1 christos if (!riscv_assign_reg_location (&ainfo->argloc[1],
3160 1.1 christos &cinfo->float_regs,
3161 1.1 christos len1, offset))
3162 1.1 christos error (_("failed during argument setup"));
3163 1.1 christos return;
3164 1.1 christos }
3165 1.1 christos
3166 1.1 christos if (sinfo.number_of_fields () == 2
3167 1.1 christos && riscv_arg_regs_available (&cinfo->int_regs) >= 1
3168 1.1.1.2 christos && (sinfo.field_type(0)->code () == TYPE_CODE_FLT
3169 1.1.1.3 christos && sinfo.field_type (0)->length () <= cinfo->flen
3170 1.1 christos && is_integral_type (sinfo.field_type (1))
3171 1.1.1.3 christos && sinfo.field_type (1)->length () <= cinfo->xlen))
3172 1.1 christos {
3173 1.1.1.3 christos int len0 = sinfo.field_type (0)->length ();
3174 1.1.1.2 christos int offset = sinfo.field_offset (0);
3175 1.1 christos if (!riscv_assign_reg_location (&ainfo->argloc[0],
3176 1.1.1.2 christos &cinfo->float_regs, len0, offset))
3177 1.1 christos error (_("failed during argument setup"));
3178 1.1 christos
3179 1.1.1.3 christos int len1 = sinfo.field_type (1)->length ();
3180 1.1.1.2 christos offset = sinfo.field_offset (1);
3181 1.1 christos gdb_assert (len1 <= cinfo->xlen);
3182 1.1 christos if (!riscv_assign_reg_location (&ainfo->argloc[1],
3183 1.1 christos &cinfo->int_regs, len1, offset))
3184 1.1 christos error (_("failed during argument setup"));
3185 1.1 christos return;
3186 1.1 christos }
3187 1.1 christos
3188 1.1 christos if (sinfo.number_of_fields () == 2
3189 1.1 christos && riscv_arg_regs_available (&cinfo->int_regs) >= 1
3190 1.1 christos && (is_integral_type (sinfo.field_type (0))
3191 1.1.1.3 christos && sinfo.field_type (0)->length () <= cinfo->xlen
3192 1.1.1.2 christos && sinfo.field_type(1)->code () == TYPE_CODE_FLT
3193 1.1.1.3 christos && sinfo.field_type (1)->length () <= cinfo->flen))
3194 1.1 christos {
3195 1.1.1.3 christos int len0 = sinfo.field_type (0)->length ();
3196 1.1.1.3 christos int len1 = sinfo.field_type (1)->length ();
3197 1.1 christos
3198 1.1 christos gdb_assert (len0 <= cinfo->xlen);
3199 1.1 christos gdb_assert (len1 <= cinfo->flen);
3200 1.1 christos
3201 1.1.1.2 christos int offset = sinfo.field_offset (0);
3202 1.1 christos if (!riscv_assign_reg_location (&ainfo->argloc[0],
3203 1.1.1.2 christos &cinfo->int_regs, len0, offset))
3204 1.1 christos error (_("failed during argument setup"));
3205 1.1 christos
3206 1.1.1.2 christos offset = sinfo.field_offset (1);
3207 1.1 christos if (!riscv_assign_reg_location (&ainfo->argloc[1],
3208 1.1 christos &cinfo->float_regs,
3209 1.1 christos len1, offset))
3210 1.1 christos error (_("failed during argument setup"));
3211 1.1 christos
3212 1.1 christos return;
3213 1.1 christos }
3214 1.1 christos }
3215 1.1 christos
3216 1.1 christos /* Non of the structure flattening cases apply, so we just pass using
3217 1.1 christos the integer ABI. */
3218 1.1 christos riscv_call_arg_scalar_int (ainfo, cinfo);
3219 1.1 christos }
3220 1.1 christos
3221 1.1 christos /* Assign a location to call (or return) argument AINFO, the location is
3222 1.1 christos selected from CINFO which holds information about what call argument
3223 1.1 christos locations are available for use next. The TYPE is the type of the
3224 1.1 christos argument being passed, this information is recorded into AINFO (along
3225 1.1 christos with some additional information derived from the type). IS_UNNAMED
3226 1.1 christos is true if this is an unnamed (stdarg) argument, this info is also
3227 1.1 christos recorded into AINFO.
3228 1.1 christos
3229 1.1 christos After assigning a location to AINFO, CINFO will have been updated. */
3230 1.1 christos
3231 1.1 christos static void
3232 1.1 christos riscv_arg_location (struct gdbarch *gdbarch,
3233 1.1 christos struct riscv_arg_info *ainfo,
3234 1.1 christos struct riscv_call_info *cinfo,
3235 1.1 christos struct type *type, bool is_unnamed)
3236 1.1 christos {
3237 1.1 christos ainfo->type = type;
3238 1.1.1.3 christos ainfo->length = ainfo->type->length ();
3239 1.1.1.2 christos ainfo->align = type_align (ainfo->type);
3240 1.1 christos ainfo->is_unnamed = is_unnamed;
3241 1.1 christos ainfo->contents = nullptr;
3242 1.1.1.2 christos ainfo->argloc[0].c_length = 0;
3243 1.1.1.2 christos ainfo->argloc[1].c_length = 0;
3244 1.1 christos
3245 1.1.1.2 christos switch (ainfo->type->code ())
3246 1.1 christos {
3247 1.1 christos case TYPE_CODE_INT:
3248 1.1 christos case TYPE_CODE_BOOL:
3249 1.1 christos case TYPE_CODE_CHAR:
3250 1.1 christos case TYPE_CODE_RANGE:
3251 1.1 christos case TYPE_CODE_ENUM:
3252 1.1 christos case TYPE_CODE_PTR:
3253 1.1.1.3 christos case TYPE_CODE_FIXED_POINT:
3254 1.1 christos if (ainfo->length <= cinfo->xlen)
3255 1.1 christos {
3256 1.1 christos ainfo->type = builtin_type (gdbarch)->builtin_long;
3257 1.1 christos ainfo->length = cinfo->xlen;
3258 1.1 christos }
3259 1.1 christos else if (ainfo->length <= (2 * cinfo->xlen))
3260 1.1 christos {
3261 1.1 christos ainfo->type = builtin_type (gdbarch)->builtin_long_long;
3262 1.1 christos ainfo->length = 2 * cinfo->xlen;
3263 1.1 christos }
3264 1.1 christos
3265 1.1 christos /* Recalculate the alignment requirement. */
3266 1.1.1.2 christos ainfo->align = type_align (ainfo->type);
3267 1.1 christos riscv_call_arg_scalar_int (ainfo, cinfo);
3268 1.1 christos break;
3269 1.1 christos
3270 1.1 christos case TYPE_CODE_FLT:
3271 1.1 christos riscv_call_arg_scalar_float (ainfo, cinfo);
3272 1.1 christos break;
3273 1.1 christos
3274 1.1 christos case TYPE_CODE_COMPLEX:
3275 1.1 christos riscv_call_arg_complex_float (ainfo, cinfo);
3276 1.1 christos break;
3277 1.1 christos
3278 1.1 christos case TYPE_CODE_STRUCT:
3279 1.1.1.4 christos if (!TYPE_HAS_DYNAMIC_LENGTH (ainfo->type))
3280 1.1.1.4 christos {
3281 1.1.1.4 christos riscv_call_arg_struct (ainfo, cinfo);
3282 1.1.1.4 christos break;
3283 1.1.1.4 christos }
3284 1.1.1.4 christos [[fallthrough]];
3285 1.1 christos
3286 1.1 christos default:
3287 1.1 christos riscv_call_arg_scalar_int (ainfo, cinfo);
3288 1.1 christos break;
3289 1.1 christos }
3290 1.1 christos }
3291 1.1 christos
3292 1.1 christos /* Used for printing debug information about the call argument location in
3293 1.1 christos INFO to STREAM. The addresses in SP_REFS and SP_ARGS are the base
3294 1.1 christos addresses for the location of pass-by-reference and
3295 1.1 christos arguments-on-the-stack memory areas. */
3296 1.1 christos
3297 1.1 christos static void
3298 1.1 christos riscv_print_arg_location (ui_file *stream, struct gdbarch *gdbarch,
3299 1.1 christos struct riscv_arg_info *info,
3300 1.1 christos CORE_ADDR sp_refs, CORE_ADDR sp_args)
3301 1.1 christos {
3302 1.1.1.3 christos gdb_printf (stream, "type: '%s', length: 0x%x, alignment: 0x%x",
3303 1.1.1.3 christos TYPE_SAFE_NAME (info->type), info->length, info->align);
3304 1.1 christos switch (info->argloc[0].loc_type)
3305 1.1 christos {
3306 1.1 christos case riscv_arg_info::location::in_reg:
3307 1.1.1.3 christos gdb_printf
3308 1.1 christos (stream, ", register %s",
3309 1.1 christos gdbarch_register_name (gdbarch, info->argloc[0].loc_data.regno));
3310 1.1 christos if (info->argloc[0].c_length < info->length)
3311 1.1 christos {
3312 1.1 christos switch (info->argloc[1].loc_type)
3313 1.1 christos {
3314 1.1 christos case riscv_arg_info::location::in_reg:
3315 1.1.1.3 christos gdb_printf
3316 1.1 christos (stream, ", register %s",
3317 1.1 christos gdbarch_register_name (gdbarch,
3318 1.1 christos info->argloc[1].loc_data.regno));
3319 1.1 christos break;
3320 1.1 christos
3321 1.1 christos case riscv_arg_info::location::on_stack:
3322 1.1.1.3 christos gdb_printf (stream, ", on stack at offset 0x%x",
3323 1.1.1.3 christos info->argloc[1].loc_data.offset);
3324 1.1 christos break;
3325 1.1 christos
3326 1.1 christos case riscv_arg_info::location::by_ref:
3327 1.1 christos default:
3328 1.1 christos /* The second location should never be a reference, any
3329 1.1 christos argument being passed by reference just places its address
3330 1.1 christos in the first location and is done. */
3331 1.1 christos error (_("invalid argument location"));
3332 1.1 christos break;
3333 1.1 christos }
3334 1.1 christos
3335 1.1 christos if (info->argloc[1].c_offset > info->argloc[0].c_length)
3336 1.1.1.3 christos gdb_printf (stream, " (offset 0x%x)",
3337 1.1.1.3 christos info->argloc[1].c_offset);
3338 1.1 christos }
3339 1.1 christos break;
3340 1.1 christos
3341 1.1 christos case riscv_arg_info::location::on_stack:
3342 1.1.1.3 christos gdb_printf (stream, ", on stack at offset 0x%x",
3343 1.1.1.3 christos info->argloc[0].loc_data.offset);
3344 1.1 christos break;
3345 1.1 christos
3346 1.1 christos case riscv_arg_info::location::by_ref:
3347 1.1.1.3 christos gdb_printf
3348 1.1 christos (stream, ", by reference, data at offset 0x%x (%s)",
3349 1.1 christos info->argloc[0].loc_data.offset,
3350 1.1 christos core_addr_to_string (sp_refs + info->argloc[0].loc_data.offset));
3351 1.1 christos if (info->argloc[1].loc_type
3352 1.1 christos == riscv_arg_info::location::in_reg)
3353 1.1.1.3 christos gdb_printf
3354 1.1 christos (stream, ", address in register %s",
3355 1.1 christos gdbarch_register_name (gdbarch, info->argloc[1].loc_data.regno));
3356 1.1 christos else
3357 1.1 christos {
3358 1.1 christos gdb_assert (info->argloc[1].loc_type
3359 1.1 christos == riscv_arg_info::location::on_stack);
3360 1.1.1.3 christos gdb_printf
3361 1.1 christos (stream, ", address on stack at offset 0x%x (%s)",
3362 1.1 christos info->argloc[1].loc_data.offset,
3363 1.1 christos core_addr_to_string (sp_args + info->argloc[1].loc_data.offset));
3364 1.1 christos }
3365 1.1 christos break;
3366 1.1 christos
3367 1.1 christos default:
3368 1.1.1.3 christos gdb_assert_not_reached ("unknown argument location type");
3369 1.1 christos }
3370 1.1 christos }
3371 1.1 christos
3372 1.1.1.2 christos /* Wrapper around REGCACHE->cooked_write. Places the LEN bytes of DATA
3373 1.1.1.2 christos into a buffer that is at least as big as the register REGNUM, padding
3374 1.1.1.2 christos out the DATA with either 0x00, or 0xff. For floating point registers
3375 1.1.1.2 christos 0xff is used, for everyone else 0x00 is used. */
3376 1.1.1.2 christos
3377 1.1.1.2 christos static void
3378 1.1.1.2 christos riscv_regcache_cooked_write (int regnum, const gdb_byte *data, int len,
3379 1.1.1.2 christos struct regcache *regcache, int flen)
3380 1.1.1.2 christos {
3381 1.1.1.2 christos gdb_byte tmp [sizeof (ULONGEST)];
3382 1.1.1.2 christos
3383 1.1.1.2 christos /* FP values in FP registers must be NaN-boxed. */
3384 1.1.1.2 christos if (riscv_is_fp_regno_p (regnum) && len < flen)
3385 1.1.1.2 christos memset (tmp, -1, sizeof (tmp));
3386 1.1.1.2 christos else
3387 1.1.1.2 christos memset (tmp, 0, sizeof (tmp));
3388 1.1.1.2 christos memcpy (tmp, data, len);
3389 1.1.1.2 christos regcache->cooked_write (regnum, tmp);
3390 1.1.1.2 christos }
3391 1.1.1.2 christos
3392 1.1 christos /* Implement the push dummy call gdbarch callback. */
3393 1.1 christos
3394 1.1 christos static CORE_ADDR
3395 1.1 christos riscv_push_dummy_call (struct gdbarch *gdbarch,
3396 1.1 christos struct value *function,
3397 1.1 christos struct regcache *regcache,
3398 1.1 christos CORE_ADDR bp_addr,
3399 1.1 christos int nargs,
3400 1.1 christos struct value **args,
3401 1.1 christos CORE_ADDR sp,
3402 1.1 christos function_call_return_method return_method,
3403 1.1 christos CORE_ADDR struct_addr)
3404 1.1 christos {
3405 1.1 christos int i;
3406 1.1 christos CORE_ADDR sp_args, sp_refs;
3407 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3408 1.1 christos
3409 1.1 christos struct riscv_arg_info *arg_info =
3410 1.1 christos (struct riscv_arg_info *) alloca (nargs * sizeof (struct riscv_arg_info));
3411 1.1 christos
3412 1.1 christos struct riscv_call_info call_info (gdbarch);
3413 1.1 christos
3414 1.1 christos CORE_ADDR osp = sp;
3415 1.1 christos
3416 1.1.1.4 christos struct type *ftype = check_typedef (function->type ());
3417 1.1 christos
3418 1.1.1.2 christos if (ftype->code () == TYPE_CODE_PTR)
3419 1.1.1.3 christos ftype = check_typedef (ftype->target_type ());
3420 1.1 christos
3421 1.1 christos /* We'll use register $a0 if we're returning a struct. */
3422 1.1 christos if (return_method == return_method_struct)
3423 1.1 christos ++call_info.int_regs.next_regnum;
3424 1.1 christos
3425 1.1 christos for (i = 0; i < nargs; ++i)
3426 1.1 christos {
3427 1.1 christos struct value *arg_value;
3428 1.1 christos struct type *arg_type;
3429 1.1 christos struct riscv_arg_info *info = &arg_info[i];
3430 1.1 christos
3431 1.1 christos arg_value = args[i];
3432 1.1.1.4 christos arg_type = check_typedef (arg_value->type ());
3433 1.1 christos
3434 1.1 christos riscv_arg_location (gdbarch, info, &call_info, arg_type,
3435 1.1.1.3 christos ftype->has_varargs () && i >= ftype->num_fields ());
3436 1.1 christos
3437 1.1 christos if (info->type != arg_type)
3438 1.1 christos arg_value = value_cast (info->type, arg_value);
3439 1.1.1.4 christos info->contents = arg_value->contents ().data ();
3440 1.1 christos }
3441 1.1 christos
3442 1.1 christos /* Adjust the stack pointer and align it. */
3443 1.1 christos sp = sp_refs = align_down (sp - call_info.memory.ref_offset, SP_ALIGNMENT);
3444 1.1 christos sp = sp_args = align_down (sp - call_info.memory.arg_offset, SP_ALIGNMENT);
3445 1.1 christos
3446 1.1.1.4 christos if (riscv_debug_infcall)
3447 1.1 christos {
3448 1.1.1.4 christos RISCV_INFCALL_SCOPED_DEBUG_START_END ("dummy call args");
3449 1.1.1.4 christos riscv_infcall_debug_printf ("floating point ABI %s in use",
3450 1.1.1.4 christos (riscv_has_fp_abi (gdbarch)
3451 1.1.1.4 christos ? "is" : "is not"));
3452 1.1.1.4 christos riscv_infcall_debug_printf ("xlen: %d", call_info.xlen);
3453 1.1.1.4 christos riscv_infcall_debug_printf ("flen: %d", call_info.flen);
3454 1.1 christos if (return_method == return_method_struct)
3455 1.1.1.4 christos riscv_infcall_debug_printf
3456 1.1.1.4 christos ("[**] struct return pointer in register $A0");
3457 1.1 christos for (i = 0; i < nargs; ++i)
3458 1.1 christos {
3459 1.1 christos struct riscv_arg_info *info = &arg_info [i];
3460 1.1.1.4 christos string_file tmp;
3461 1.1 christos
3462 1.1.1.4 christos riscv_print_arg_location (&tmp, gdbarch, info, sp_refs, sp_args);
3463 1.1.1.4 christos riscv_infcall_debug_printf ("[%2d] %s", i, tmp.string ().c_str ());
3464 1.1 christos }
3465 1.1 christos if (call_info.memory.arg_offset > 0
3466 1.1 christos || call_info.memory.ref_offset > 0)
3467 1.1 christos {
3468 1.1.1.4 christos riscv_infcall_debug_printf (" Original sp: %s",
3469 1.1.1.4 christos core_addr_to_string (osp));
3470 1.1.1.4 christos riscv_infcall_debug_printf ("Stack required (for args): 0x%x",
3471 1.1.1.4 christos call_info.memory.arg_offset);
3472 1.1.1.4 christos riscv_infcall_debug_printf ("Stack required (for refs): 0x%x",
3473 1.1.1.4 christos call_info.memory.ref_offset);
3474 1.1.1.4 christos riscv_infcall_debug_printf (" Stack allocated: %s",
3475 1.1.1.4 christos core_addr_to_string_nz (osp - sp));
3476 1.1 christos }
3477 1.1 christos }
3478 1.1 christos
3479 1.1 christos /* Now load the argument into registers, or onto the stack. */
3480 1.1 christos
3481 1.1 christos if (return_method == return_method_struct)
3482 1.1 christos {
3483 1.1 christos gdb_byte buf[sizeof (LONGEST)];
3484 1.1 christos
3485 1.1 christos store_unsigned_integer (buf, call_info.xlen, byte_order, struct_addr);
3486 1.1 christos regcache->cooked_write (RISCV_A0_REGNUM, buf);
3487 1.1 christos }
3488 1.1 christos
3489 1.1 christos for (i = 0; i < nargs; ++i)
3490 1.1 christos {
3491 1.1 christos CORE_ADDR dst;
3492 1.1 christos int second_arg_length = 0;
3493 1.1 christos const gdb_byte *second_arg_data;
3494 1.1 christos struct riscv_arg_info *info = &arg_info [i];
3495 1.1 christos
3496 1.1 christos gdb_assert (info->length > 0);
3497 1.1 christos
3498 1.1 christos switch (info->argloc[0].loc_type)
3499 1.1 christos {
3500 1.1 christos case riscv_arg_info::location::in_reg:
3501 1.1 christos {
3502 1.1 christos gdb_assert (info->argloc[0].c_length <= info->length);
3503 1.1.1.2 christos
3504 1.1.1.2 christos riscv_regcache_cooked_write (info->argloc[0].loc_data.regno,
3505 1.1.1.2 christos (info->contents
3506 1.1.1.2 christos + info->argloc[0].c_offset),
3507 1.1.1.2 christos info->argloc[0].c_length,
3508 1.1.1.2 christos regcache, call_info.flen);
3509 1.1 christos second_arg_length =
3510 1.1.1.2 christos (((info->argloc[0].c_length + info->argloc[0].c_offset) < info->length)
3511 1.1 christos ? info->argloc[1].c_length : 0);
3512 1.1 christos second_arg_data = info->contents + info->argloc[1].c_offset;
3513 1.1 christos }
3514 1.1 christos break;
3515 1.1 christos
3516 1.1 christos case riscv_arg_info::location::on_stack:
3517 1.1 christos dst = sp_args + info->argloc[0].loc_data.offset;
3518 1.1 christos write_memory (dst, info->contents, info->length);
3519 1.1 christos second_arg_length = 0;
3520 1.1 christos break;
3521 1.1 christos
3522 1.1 christos case riscv_arg_info::location::by_ref:
3523 1.1 christos dst = sp_refs + info->argloc[0].loc_data.offset;
3524 1.1 christos write_memory (dst, info->contents, info->length);
3525 1.1 christos
3526 1.1 christos second_arg_length = call_info.xlen;
3527 1.1 christos second_arg_data = (gdb_byte *) &dst;
3528 1.1 christos break;
3529 1.1 christos
3530 1.1 christos default:
3531 1.1.1.3 christos gdb_assert_not_reached ("unknown argument location type");
3532 1.1 christos }
3533 1.1 christos
3534 1.1 christos if (second_arg_length > 0)
3535 1.1 christos {
3536 1.1 christos switch (info->argloc[1].loc_type)
3537 1.1 christos {
3538 1.1 christos case riscv_arg_info::location::in_reg:
3539 1.1 christos {
3540 1.1 christos gdb_assert ((riscv_is_fp_regno_p (info->argloc[1].loc_data.regno)
3541 1.1 christos && second_arg_length <= call_info.flen)
3542 1.1 christos || second_arg_length <= call_info.xlen);
3543 1.1.1.2 christos riscv_regcache_cooked_write (info->argloc[1].loc_data.regno,
3544 1.1.1.2 christos second_arg_data,
3545 1.1.1.2 christos second_arg_length,
3546 1.1.1.2 christos regcache, call_info.flen);
3547 1.1 christos }
3548 1.1 christos break;
3549 1.1 christos
3550 1.1 christos case riscv_arg_info::location::on_stack:
3551 1.1 christos {
3552 1.1 christos CORE_ADDR arg_addr;
3553 1.1 christos
3554 1.1 christos arg_addr = sp_args + info->argloc[1].loc_data.offset;
3555 1.1 christos write_memory (arg_addr, second_arg_data, second_arg_length);
3556 1.1 christos break;
3557 1.1 christos }
3558 1.1 christos
3559 1.1 christos case riscv_arg_info::location::by_ref:
3560 1.1 christos default:
3561 1.1 christos /* The second location should never be a reference, any
3562 1.1 christos argument being passed by reference just places its address
3563 1.1 christos in the first location and is done. */
3564 1.1 christos error (_("invalid argument location"));
3565 1.1 christos break;
3566 1.1 christos }
3567 1.1 christos }
3568 1.1 christos }
3569 1.1 christos
3570 1.1 christos /* Set the dummy return value to bp_addr.
3571 1.1 christos A dummy breakpoint will be setup to execute the call. */
3572 1.1 christos
3573 1.1.1.4 christos riscv_infcall_debug_printf ("writing $ra = %s",
3574 1.1.1.4 christos core_addr_to_string (bp_addr));
3575 1.1 christos regcache_cooked_write_unsigned (regcache, RISCV_RA_REGNUM, bp_addr);
3576 1.1 christos
3577 1.1 christos /* Finally, update the stack pointer. */
3578 1.1 christos
3579 1.1.1.4 christos riscv_infcall_debug_printf ("writing $sp = %s", core_addr_to_string (sp));
3580 1.1 christos regcache_cooked_write_unsigned (regcache, RISCV_SP_REGNUM, sp);
3581 1.1 christos
3582 1.1 christos return sp;
3583 1.1 christos }
3584 1.1 christos
3585 1.1 christos /* Implement the return_value gdbarch method. */
3586 1.1 christos
3587 1.1 christos static enum return_value_convention
3588 1.1 christos riscv_return_value (struct gdbarch *gdbarch,
3589 1.1 christos struct value *function,
3590 1.1 christos struct type *type,
3591 1.1 christos struct regcache *regcache,
3592 1.1.1.4 christos struct value **read_value,
3593 1.1 christos const gdb_byte *writebuf)
3594 1.1 christos {
3595 1.1 christos struct riscv_call_info call_info (gdbarch);
3596 1.1 christos struct riscv_arg_info info;
3597 1.1 christos struct type *arg_type;
3598 1.1 christos
3599 1.1 christos arg_type = check_typedef (type);
3600 1.1 christos riscv_arg_location (gdbarch, &info, &call_info, arg_type, false);
3601 1.1 christos
3602 1.1.1.4 christos if (riscv_debug_infcall)
3603 1.1 christos {
3604 1.1.1.4 christos string_file tmp;
3605 1.1.1.4 christos riscv_print_arg_location (&tmp, gdbarch, &info, 0, 0);
3606 1.1.1.4 christos riscv_infcall_debug_printf ("[R] %s", tmp.string ().c_str ());
3607 1.1 christos }
3608 1.1 christos
3609 1.1.1.4 christos if (read_value != nullptr || writebuf != nullptr)
3610 1.1 christos {
3611 1.1.1.3 christos unsigned int arg_len;
3612 1.1.1.3 christos struct value *abi_val;
3613 1.1.1.4 christos gdb_byte *readbuf = nullptr;
3614 1.1.1.3 christos int regnum;
3615 1.1.1.3 christos
3616 1.1.1.3 christos /* We only do one thing at a time. */
3617 1.1.1.4 christos gdb_assert (read_value == nullptr || writebuf == nullptr);
3618 1.1.1.3 christos
3619 1.1.1.3 christos /* In some cases the argument is not returned as the declared type,
3620 1.1.1.3 christos and we need to cast to or from the ABI type in order to
3621 1.1.1.3 christos correctly access the argument. When writing to the machine we
3622 1.1.1.3 christos do the cast here, when reading from the machine the cast occurs
3623 1.1.1.3 christos later, after extracting the value. As the ABI type can be
3624 1.1.1.3 christos larger than the declared type, then the read or write buffers
3625 1.1.1.3 christos passed in might be too small. Here we ensure that we are using
3626 1.1.1.3 christos buffers of sufficient size. */
3627 1.1.1.3 christos if (writebuf != nullptr)
3628 1.1.1.3 christos {
3629 1.1.1.3 christos struct value *arg_val;
3630 1.1 christos
3631 1.1.1.3 christos if (is_fixed_point_type (arg_type))
3632 1.1 christos {
3633 1.1.1.3 christos /* Convert the argument to the type used to pass
3634 1.1.1.3 christos the return value, but being careful to preserve
3635 1.1.1.3 christos the fact that the value needs to be returned
3636 1.1.1.3 christos unscaled. */
3637 1.1.1.3 christos gdb_mpz unscaled;
3638 1.1.1.3 christos
3639 1.1.1.3 christos unscaled.read (gdb::make_array_view (writebuf,
3640 1.1.1.3 christos arg_type->length ()),
3641 1.1.1.3 christos type_byte_order (arg_type),
3642 1.1.1.3 christos arg_type->is_unsigned ());
3643 1.1.1.4 christos abi_val = value::allocate (info.type);
3644 1.1.1.4 christos unscaled.write (abi_val->contents_raw (),
3645 1.1.1.3 christos type_byte_order (info.type),
3646 1.1.1.3 christos info.type->is_unsigned ());
3647 1.1 christos }
3648 1.1.1.3 christos else
3649 1.1 christos {
3650 1.1.1.3 christos arg_val = value_from_contents (arg_type, writebuf);
3651 1.1.1.3 christos abi_val = value_cast (info.type, arg_val);
3652 1.1 christos }
3653 1.1.1.4 christos writebuf = abi_val->contents_raw ().data ();
3654 1.1.1.3 christos }
3655 1.1.1.3 christos else
3656 1.1.1.3 christos {
3657 1.1.1.4 christos abi_val = value::allocate (info.type);
3658 1.1.1.4 christos readbuf = abi_val->contents_raw ().data ();
3659 1.1.1.3 christos }
3660 1.1.1.3 christos arg_len = info.type->length ();
3661 1.1 christos
3662 1.1.1.3 christos switch (info.argloc[0].loc_type)
3663 1.1.1.3 christos {
3664 1.1.1.3 christos /* Return value in register(s). */
3665 1.1.1.3 christos case riscv_arg_info::location::in_reg:
3666 1.1.1.3 christos {
3667 1.1.1.3 christos regnum = info.argloc[0].loc_data.regno;
3668 1.1.1.3 christos gdb_assert (info.argloc[0].c_length <= arg_len);
3669 1.1.1.3 christos gdb_assert (info.argloc[0].c_length
3670 1.1.1.3 christos <= register_size (gdbarch, regnum));
3671 1.1.1.3 christos
3672 1.1.1.3 christos if (readbuf)
3673 1.1.1.3 christos {
3674 1.1.1.3 christos gdb_byte *ptr = readbuf + info.argloc[0].c_offset;
3675 1.1.1.3 christos regcache->cooked_read_part (regnum, 0,
3676 1.1.1.3 christos info.argloc[0].c_length,
3677 1.1.1.3 christos ptr);
3678 1.1.1.3 christos }
3679 1.1.1.3 christos
3680 1.1.1.3 christos if (writebuf)
3681 1.1.1.3 christos {
3682 1.1.1.3 christos const gdb_byte *ptr = writebuf + info.argloc[0].c_offset;
3683 1.1.1.3 christos riscv_regcache_cooked_write (regnum, ptr,
3684 1.1.1.3 christos info.argloc[0].c_length,
3685 1.1.1.3 christos regcache, call_info.flen);
3686 1.1.1.3 christos }
3687 1.1.1.3 christos
3688 1.1.1.3 christos /* A return value in register can have a second part in a
3689 1.1.1.3 christos second register. */
3690 1.1.1.3 christos if (info.argloc[1].c_length > 0)
3691 1.1.1.3 christos {
3692 1.1.1.3 christos switch (info.argloc[1].loc_type)
3693 1.1.1.3 christos {
3694 1.1.1.3 christos case riscv_arg_info::location::in_reg:
3695 1.1.1.3 christos regnum = info.argloc[1].loc_data.regno;
3696 1.1.1.3 christos
3697 1.1.1.3 christos gdb_assert ((info.argloc[0].c_length
3698 1.1.1.3 christos + info.argloc[1].c_length) <= arg_len);
3699 1.1.1.3 christos gdb_assert (info.argloc[1].c_length
3700 1.1.1.3 christos <= register_size (gdbarch, regnum));
3701 1.1.1.3 christos
3702 1.1.1.3 christos if (readbuf)
3703 1.1.1.3 christos {
3704 1.1.1.3 christos readbuf += info.argloc[1].c_offset;
3705 1.1.1.3 christos regcache->cooked_read_part (regnum, 0,
3706 1.1.1.3 christos info.argloc[1].c_length,
3707 1.1.1.3 christos readbuf);
3708 1.1.1.3 christos }
3709 1.1.1.3 christos
3710 1.1.1.3 christos if (writebuf)
3711 1.1.1.3 christos {
3712 1.1.1.3 christos const gdb_byte *ptr
3713 1.1.1.3 christos = writebuf + info.argloc[1].c_offset;
3714 1.1.1.3 christos riscv_regcache_cooked_write
3715 1.1.1.3 christos (regnum, ptr, info.argloc[1].c_length,
3716 1.1.1.3 christos regcache, call_info.flen);
3717 1.1.1.3 christos }
3718 1.1.1.3 christos break;
3719 1.1.1.3 christos
3720 1.1.1.3 christos case riscv_arg_info::location::by_ref:
3721 1.1.1.3 christos case riscv_arg_info::location::on_stack:
3722 1.1.1.3 christos default:
3723 1.1.1.3 christos error (_("invalid argument location"));
3724 1.1.1.3 christos break;
3725 1.1.1.3 christos }
3726 1.1.1.3 christos }
3727 1.1 christos }
3728 1.1.1.3 christos break;
3729 1.1 christos
3730 1.1.1.3 christos /* Return value by reference will have its address in A0. */
3731 1.1.1.3 christos case riscv_arg_info::location::by_ref:
3732 1.1 christos {
3733 1.1.1.3 christos ULONGEST addr;
3734 1.1.1.3 christos
3735 1.1.1.3 christos regcache_cooked_read_unsigned (regcache, RISCV_A0_REGNUM,
3736 1.1.1.3 christos &addr);
3737 1.1.1.4 christos if (read_value != nullptr)
3738 1.1.1.4 christos {
3739 1.1.1.4 christos abi_val = value_at_non_lval (type, addr);
3740 1.1.1.4 christos /* Also reset the expected type, so that the cast
3741 1.1.1.4 christos later on is a no-op. If the cast is not a no-op,
3742 1.1.1.4 christos and if the return type is variably-sized, then the
3743 1.1.1.4 christos type of ABI_VAL will differ from ARG_TYPE due to
3744 1.1.1.4 christos dynamic type resolution, and so will most likely
3745 1.1.1.4 christos fail. */
3746 1.1.1.4 christos arg_type = abi_val->type ();
3747 1.1.1.4 christos }
3748 1.1.1.3 christos if (writebuf != nullptr)
3749 1.1.1.3 christos write_memory (addr, writebuf, info.length);
3750 1.1 christos }
3751 1.1.1.3 christos break;
3752 1.1.1.3 christos
3753 1.1.1.3 christos case riscv_arg_info::location::on_stack:
3754 1.1.1.3 christos default:
3755 1.1.1.3 christos error (_("invalid argument location"));
3756 1.1.1.3 christos break;
3757 1.1.1.3 christos }
3758 1.1.1.3 christos
3759 1.1.1.3 christos /* This completes the cast from abi type back to the declared type
3760 1.1.1.3 christos in the case that we are reading from the machine. See the
3761 1.1.1.3 christos comment at the head of this block for more details. */
3762 1.1.1.4 christos if (read_value != nullptr)
3763 1.1.1.3 christos {
3764 1.1.1.3 christos if (is_fixed_point_type (arg_type))
3765 1.1.1.3 christos {
3766 1.1.1.3 christos /* Convert abi_val to the actual return type, but
3767 1.1.1.3 christos being careful to preserve the fact that abi_val
3768 1.1.1.3 christos is unscaled. */
3769 1.1.1.3 christos gdb_mpz unscaled;
3770 1.1.1.3 christos
3771 1.1.1.4 christos unscaled.read (abi_val->contents (),
3772 1.1.1.3 christos type_byte_order (info.type),
3773 1.1.1.3 christos info.type->is_unsigned ());
3774 1.1.1.4 christos *read_value = value::allocate (arg_type);
3775 1.1.1.4 christos unscaled.write ((*read_value)->contents_raw (),
3776 1.1.1.3 christos type_byte_order (arg_type),
3777 1.1.1.3 christos arg_type->is_unsigned ());
3778 1.1.1.3 christos }
3779 1.1.1.3 christos else
3780 1.1.1.4 christos *read_value = value_cast (arg_type, abi_val);
3781 1.1.1.3 christos }
3782 1.1 christos }
3783 1.1 christos
3784 1.1 christos switch (info.argloc[0].loc_type)
3785 1.1 christos {
3786 1.1 christos case riscv_arg_info::location::in_reg:
3787 1.1 christos return RETURN_VALUE_REGISTER_CONVENTION;
3788 1.1 christos case riscv_arg_info::location::by_ref:
3789 1.1.1.3 christos return RETURN_VALUE_ABI_PRESERVES_ADDRESS;
3790 1.1 christos case riscv_arg_info::location::on_stack:
3791 1.1 christos default:
3792 1.1 christos error (_("invalid argument location"));
3793 1.1 christos }
3794 1.1 christos }
3795 1.1 christos
3796 1.1 christos /* Implement the frame_align gdbarch method. */
3797 1.1 christos
3798 1.1 christos static CORE_ADDR
3799 1.1 christos riscv_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
3800 1.1 christos {
3801 1.1 christos return align_down (addr, 16);
3802 1.1 christos }
3803 1.1 christos
3804 1.1 christos /* Generate, or return the cached frame cache for the RiscV frame
3805 1.1 christos unwinder. */
3806 1.1 christos
3807 1.1 christos static struct riscv_unwind_cache *
3808 1.1.1.4 christos riscv_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
3809 1.1 christos {
3810 1.1 christos CORE_ADDR pc, start_addr;
3811 1.1 christos struct riscv_unwind_cache *cache;
3812 1.1 christos struct gdbarch *gdbarch = get_frame_arch (this_frame);
3813 1.1 christos int numregs, regno;
3814 1.1 christos
3815 1.1 christos if ((*this_cache) != NULL)
3816 1.1 christos return (struct riscv_unwind_cache *) *this_cache;
3817 1.1 christos
3818 1.1 christos cache = FRAME_OBSTACK_ZALLOC (struct riscv_unwind_cache);
3819 1.1 christos cache->regs = trad_frame_alloc_saved_regs (this_frame);
3820 1.1 christos (*this_cache) = cache;
3821 1.1 christos
3822 1.1 christos /* Scan the prologue, filling in the cache. */
3823 1.1 christos start_addr = get_frame_func (this_frame);
3824 1.1 christos pc = get_frame_pc (this_frame);
3825 1.1 christos riscv_scan_prologue (gdbarch, start_addr, pc, cache);
3826 1.1 christos
3827 1.1 christos /* We can now calculate the frame base address. */
3828 1.1 christos cache->frame_base
3829 1.1.1.3 christos = (get_frame_register_unsigned (this_frame, cache->frame_base_reg)
3830 1.1 christos + cache->frame_base_offset);
3831 1.1.1.4 christos riscv_unwinder_debug_printf ("frame base is %s ($%s + 0x%x)",
3832 1.1.1.4 christos core_addr_to_string (cache->frame_base),
3833 1.1.1.4 christos gdbarch_register_name (gdbarch,
3834 1.1.1.4 christos cache->frame_base_reg),
3835 1.1.1.4 christos cache->frame_base_offset);
3836 1.1 christos
3837 1.1 christos /* The prologue scanner sets the address of registers stored to the stack
3838 1.1 christos as the offset of that register from the frame base. The prologue
3839 1.1 christos scanner doesn't know the actual frame base value, and so is unable to
3840 1.1 christos compute the exact address. We do now know the frame base value, so
3841 1.1 christos update the address of registers stored to the stack. */
3842 1.1 christos numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
3843 1.1 christos for (regno = 0; regno < numregs; ++regno)
3844 1.1 christos {
3845 1.1.1.3 christos if (cache->regs[regno].is_addr ())
3846 1.1.1.3 christos cache->regs[regno].set_addr (cache->regs[regno].addr ()
3847 1.1.1.3 christos + cache->frame_base);
3848 1.1 christos }
3849 1.1 christos
3850 1.1 christos /* The previous $pc can be found wherever the $ra value can be found.
3851 1.1 christos The previous $ra value is gone, this would have been stored be the
3852 1.1 christos previous frame if required. */
3853 1.1 christos cache->regs[gdbarch_pc_regnum (gdbarch)] = cache->regs[RISCV_RA_REGNUM];
3854 1.1.1.3 christos cache->regs[RISCV_RA_REGNUM].set_unknown ();
3855 1.1 christos
3856 1.1 christos /* Build the frame id. */
3857 1.1 christos cache->this_id = frame_id_build (cache->frame_base, start_addr);
3858 1.1 christos
3859 1.1 christos /* The previous $sp value is the frame base value. */
3860 1.1.1.3 christos cache->regs[gdbarch_sp_regnum (gdbarch)].set_value (cache->frame_base);
3861 1.1 christos
3862 1.1 christos return cache;
3863 1.1 christos }
3864 1.1 christos
3865 1.1 christos /* Implement the this_id callback for RiscV frame unwinder. */
3866 1.1 christos
3867 1.1 christos static void
3868 1.1.1.4 christos riscv_frame_this_id (const frame_info_ptr &this_frame,
3869 1.1 christos void **prologue_cache,
3870 1.1 christos struct frame_id *this_id)
3871 1.1 christos {
3872 1.1 christos struct riscv_unwind_cache *cache;
3873 1.1 christos
3874 1.1.1.2 christos try
3875 1.1 christos {
3876 1.1 christos cache = riscv_frame_cache (this_frame, prologue_cache);
3877 1.1 christos *this_id = cache->this_id;
3878 1.1 christos }
3879 1.1.1.2 christos catch (const gdb_exception_error &ex)
3880 1.1 christos {
3881 1.1 christos /* Ignore errors, this leaves the frame id as the predefined outer
3882 1.1.1.3 christos frame id which terminates the backtrace at this point. */
3883 1.1 christos }
3884 1.1 christos }
3885 1.1 christos
3886 1.1 christos /* Implement the prev_register callback for RiscV frame unwinder. */
3887 1.1 christos
3888 1.1 christos static struct value *
3889 1.1.1.4 christos riscv_frame_prev_register (const frame_info_ptr &this_frame,
3890 1.1 christos void **prologue_cache,
3891 1.1 christos int regnum)
3892 1.1 christos {
3893 1.1 christos struct riscv_unwind_cache *cache;
3894 1.1 christos
3895 1.1 christos cache = riscv_frame_cache (this_frame, prologue_cache);
3896 1.1 christos return trad_frame_get_prev_register (this_frame, cache->regs, regnum);
3897 1.1 christos }
3898 1.1 christos
3899 1.1 christos /* Structure defining the RiscV normal frame unwind functions. Since we
3900 1.1 christos are the fallback unwinder (DWARF unwinder is used first), we use the
3901 1.1 christos default frame sniffer, which always accepts the frame. */
3902 1.1 christos
3903 1.1 christos static const struct frame_unwind riscv_frame_unwind =
3904 1.1 christos {
3905 1.1.1.3 christos /*.name =*/ "riscv prologue",
3906 1.1 christos /*.type =*/ NORMAL_FRAME,
3907 1.1 christos /*.stop_reason =*/ default_frame_unwind_stop_reason,
3908 1.1 christos /*.this_id =*/ riscv_frame_this_id,
3909 1.1 christos /*.prev_register =*/ riscv_frame_prev_register,
3910 1.1 christos /*.unwind_data =*/ NULL,
3911 1.1 christos /*.sniffer =*/ default_frame_sniffer,
3912 1.1 christos /*.dealloc_cache =*/ NULL,
3913 1.1 christos /*.prev_arch =*/ NULL,
3914 1.1 christos };
3915 1.1 christos
3916 1.1.1.3 christos /* Extract a set of required target features out of ABFD. If ABFD is
3917 1.1.1.3 christos nullptr then a RISCV_GDBARCH_FEATURES is returned in its default state. */
3918 1.1 christos
3919 1.1 christos static struct riscv_gdbarch_features
3920 1.1.1.3 christos riscv_features_from_bfd (const bfd *abfd)
3921 1.1 christos {
3922 1.1 christos struct riscv_gdbarch_features features;
3923 1.1 christos
3924 1.1 christos /* Now try to improve on the defaults by looking at the binary we are
3925 1.1 christos going to execute. We assume the user knows what they are doing and
3926 1.1 christos that the target will match the binary. Remember, this code path is
3927 1.1 christos only used at all if the target hasn't given us a description, so this
3928 1.1 christos is really a last ditched effort to do something sane before giving
3929 1.1 christos up. */
3930 1.1.1.3 christos if (abfd != nullptr && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
3931 1.1 christos {
3932 1.1.1.3 christos unsigned char eclass = elf_elfheader (abfd)->e_ident[EI_CLASS];
3933 1.1.1.3 christos int e_flags = elf_elfheader (abfd)->e_flags;
3934 1.1 christos
3935 1.1 christos if (eclass == ELFCLASS32)
3936 1.1 christos features.xlen = 4;
3937 1.1 christos else if (eclass == ELFCLASS64)
3938 1.1 christos features.xlen = 8;
3939 1.1 christos else
3940 1.1.1.3 christos internal_error (_("unknown ELF header class %d"), eclass);
3941 1.1 christos
3942 1.1 christos if (e_flags & EF_RISCV_FLOAT_ABI_DOUBLE)
3943 1.1 christos features.flen = 8;
3944 1.1 christos else if (e_flags & EF_RISCV_FLOAT_ABI_SINGLE)
3945 1.1 christos features.flen = 4;
3946 1.1.1.3 christos
3947 1.1.1.3 christos if (e_flags & EF_RISCV_RVE)
3948 1.1.1.3 christos {
3949 1.1.1.3 christos if (features.xlen == 8)
3950 1.1.1.3 christos {
3951 1.1.1.3 christos warning (_("64-bit ELF with RV32E flag set! Assuming 32-bit"));
3952 1.1.1.3 christos features.xlen = 4;
3953 1.1.1.3 christos }
3954 1.1.1.3 christos features.embedded = true;
3955 1.1.1.3 christos }
3956 1.1 christos }
3957 1.1 christos
3958 1.1 christos return features;
3959 1.1 christos }
3960 1.1 christos
3961 1.1 christos /* Find a suitable default target description. Use the contents of INFO,
3962 1.1 christos specifically the bfd object being executed, to guide the selection of a
3963 1.1 christos suitable default target description. */
3964 1.1 christos
3965 1.1 christos static const struct target_desc *
3966 1.1 christos riscv_find_default_target_description (const struct gdbarch_info info)
3967 1.1 christos {
3968 1.1 christos /* Extract desired feature set from INFO. */
3969 1.1 christos struct riscv_gdbarch_features features
3970 1.1.1.3 christos = riscv_features_from_bfd (info.abfd);
3971 1.1 christos
3972 1.1.1.3 christos /* If the XLEN field is still 0 then we got nothing useful from INFO.BFD,
3973 1.1.1.3 christos maybe there was no bfd object. In this case we fall back to a minimal
3974 1.1.1.3 christos useful target with no floating point, the x-register size is selected
3975 1.1.1.3 christos based on the architecture from INFO. */
3976 1.1 christos if (features.xlen == 0)
3977 1.1.1.3 christos features.xlen = info.bfd_arch_info->bits_per_word == 32 ? 4 : 8;
3978 1.1 christos
3979 1.1 christos /* Now build a target description based on the feature set. */
3980 1.1.1.2 christos return riscv_lookup_target_description (features);
3981 1.1 christos }
3982 1.1 christos
3983 1.1.1.3 christos /* Add all the RISC-V specific register groups into GDBARCH. */
3984 1.1 christos
3985 1.1 christos static void
3986 1.1 christos riscv_add_reggroups (struct gdbarch *gdbarch)
3987 1.1 christos {
3988 1.1 christos reggroup_add (gdbarch, csr_reggroup);
3989 1.1 christos }
3990 1.1 christos
3991 1.1 christos /* Implement the "dwarf2_reg_to_regnum" gdbarch method. */
3992 1.1 christos
3993 1.1 christos static int
3994 1.1 christos riscv_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
3995 1.1 christos {
3996 1.1.1.3 christos if (reg <= RISCV_DWARF_REGNUM_X31)
3997 1.1 christos return RISCV_ZERO_REGNUM + (reg - RISCV_DWARF_REGNUM_X0);
3998 1.1 christos
3999 1.1.1.3 christos else if (reg <= RISCV_DWARF_REGNUM_F31)
4000 1.1 christos return RISCV_FIRST_FP_REGNUM + (reg - RISCV_DWARF_REGNUM_F0);
4001 1.1 christos
4002 1.1.1.3 christos else if (reg >= RISCV_DWARF_FIRST_CSR && reg <= RISCV_DWARF_LAST_CSR)
4003 1.1.1.3 christos return RISCV_FIRST_CSR_REGNUM + (reg - RISCV_DWARF_FIRST_CSR);
4004 1.1.1.3 christos
4005 1.1.1.3 christos else if (reg >= RISCV_DWARF_REGNUM_V0 && reg <= RISCV_DWARF_REGNUM_V31)
4006 1.1.1.3 christos return RISCV_V0_REGNUM + (reg - RISCV_DWARF_REGNUM_V0);
4007 1.1.1.3 christos
4008 1.1 christos return -1;
4009 1.1 christos }
4010 1.1 christos
4011 1.1.1.2 christos /* Implement the gcc_target_options method. We have to select the arch and abi
4012 1.1.1.2 christos from the feature info. We have enough feature info to select the abi, but
4013 1.1.1.2 christos not enough info for the arch given all of the possible architecture
4014 1.1.1.2 christos extensions. So choose reasonable defaults for now. */
4015 1.1.1.2 christos
4016 1.1.1.2 christos static std::string
4017 1.1.1.2 christos riscv_gcc_target_options (struct gdbarch *gdbarch)
4018 1.1.1.2 christos {
4019 1.1.1.2 christos int isa_xlen = riscv_isa_xlen (gdbarch);
4020 1.1.1.2 christos int isa_flen = riscv_isa_flen (gdbarch);
4021 1.1.1.2 christos int abi_xlen = riscv_abi_xlen (gdbarch);
4022 1.1.1.2 christos int abi_flen = riscv_abi_flen (gdbarch);
4023 1.1.1.2 christos std::string target_options;
4024 1.1.1.2 christos
4025 1.1.1.2 christos target_options = "-march=rv";
4026 1.1.1.2 christos if (isa_xlen == 8)
4027 1.1.1.2 christos target_options += "64";
4028 1.1.1.2 christos else
4029 1.1.1.2 christos target_options += "32";
4030 1.1.1.2 christos if (isa_flen == 8)
4031 1.1.1.2 christos target_options += "gc";
4032 1.1.1.2 christos else if (isa_flen == 4)
4033 1.1.1.2 christos target_options += "imafc";
4034 1.1.1.2 christos else
4035 1.1.1.2 christos target_options += "imac";
4036 1.1.1.2 christos
4037 1.1.1.2 christos target_options += " -mabi=";
4038 1.1.1.2 christos if (abi_xlen == 8)
4039 1.1.1.2 christos target_options += "lp64";
4040 1.1.1.2 christos else
4041 1.1.1.2 christos target_options += "ilp32";
4042 1.1.1.2 christos if (abi_flen == 8)
4043 1.1.1.2 christos target_options += "d";
4044 1.1.1.2 christos else if (abi_flen == 4)
4045 1.1.1.2 christos target_options += "f";
4046 1.1.1.2 christos
4047 1.1.1.2 christos /* The gdb loader doesn't handle link-time relaxation relocations. */
4048 1.1.1.2 christos target_options += " -mno-relax";
4049 1.1.1.2 christos
4050 1.1.1.2 christos return target_options;
4051 1.1.1.2 christos }
4052 1.1.1.2 christos
4053 1.1.1.2 christos /* Call back from tdesc_use_registers, called for each unknown register
4054 1.1.1.2 christos found in the target description.
4055 1.1.1.2 christos
4056 1.1.1.2 christos See target-description.h (typedef tdesc_unknown_register_ftype) for a
4057 1.1.1.2 christos discussion of the arguments and return values. */
4058 1.1.1.2 christos
4059 1.1.1.2 christos static int
4060 1.1.1.2 christos riscv_tdesc_unknown_reg (struct gdbarch *gdbarch, tdesc_feature *feature,
4061 1.1.1.2 christos const char *reg_name, int possible_regnum)
4062 1.1.1.2 christos {
4063 1.1.1.2 christos /* At one point in time GDB had an incorrect default target description
4064 1.1.1.2 christos that duplicated the fflags, frm, and fcsr registers in both the FPU
4065 1.1.1.2 christos and CSR register sets.
4066 1.1.1.2 christos
4067 1.1.1.2 christos Some targets (QEMU) copied these target descriptions into their source
4068 1.1.1.3 christos tree, and so we're now stuck working with some versions of QEMU that
4069 1.1.1.2 christos declare the same registers twice.
4070 1.1.1.2 christos
4071 1.1.1.3 christos To make matters worse, if GDB tries to read or write to these
4072 1.1.1.3 christos registers using the register number assigned in the FPU feature set,
4073 1.1.1.3 christos then QEMU will fail to read the register, so we must use the register
4074 1.1.1.3 christos number declared in the CSR feature set.
4075 1.1.1.3 christos
4076 1.1.1.3 christos Luckily, GDB scans the FPU feature first, and then the CSR feature,
4077 1.1.1.3 christos which means that the CSR feature will be the one we end up using, the
4078 1.1.1.3 christos versions of these registers in the FPU feature will appear as unknown
4079 1.1.1.3 christos registers and will be passed through to this code.
4080 1.1.1.3 christos
4081 1.1.1.3 christos To prevent these duplicate registers showing up in any of the register
4082 1.1.1.3 christos lists, and to prevent GDB every trying to access the FPU feature copies,
4083 1.1.1.3 christos we spot the three problematic registers here, and record the register
4084 1.1.1.3 christos number that GDB has assigned them. Then in riscv_register_name we will
4085 1.1.1.3 christos return no name for the three duplicates, this hides the duplicates from
4086 1.1.1.3 christos the user. */
4087 1.1.1.3 christos if (strcmp (tdesc_feature_name (feature), riscv_freg_feature.name ()) == 0)
4088 1.1.1.2 christos {
4089 1.1.1.3 christos riscv_gdbarch_tdep *tdep = gdbarch_tdep<riscv_gdbarch_tdep> (gdbarch);
4090 1.1.1.2 christos int *regnum_ptr = nullptr;
4091 1.1.1.2 christos
4092 1.1.1.2 christos if (strcmp (reg_name, "fflags") == 0)
4093 1.1.1.2 christos regnum_ptr = &tdep->duplicate_fflags_regnum;
4094 1.1.1.2 christos else if (strcmp (reg_name, "frm") == 0)
4095 1.1.1.2 christos regnum_ptr = &tdep->duplicate_frm_regnum;
4096 1.1.1.2 christos else if (strcmp (reg_name, "fcsr") == 0)
4097 1.1.1.2 christos regnum_ptr = &tdep->duplicate_fcsr_regnum;
4098 1.1.1.2 christos
4099 1.1.1.2 christos if (regnum_ptr != nullptr)
4100 1.1.1.2 christos {
4101 1.1.1.2 christos /* This means the register appears more than twice in the target
4102 1.1.1.2 christos description. Just let GDB add this as another register.
4103 1.1.1.2 christos We'll have duplicates in the register name list, but there's
4104 1.1.1.2 christos not much more we can do. */
4105 1.1.1.2 christos if (*regnum_ptr != -1)
4106 1.1.1.2 christos return -1;
4107 1.1.1.2 christos
4108 1.1.1.2 christos /* Record the number assigned to this register, then return the
4109 1.1.1.2 christos number (so it actually gets assigned to this register). */
4110 1.1.1.2 christos *regnum_ptr = possible_regnum;
4111 1.1.1.2 christos return possible_regnum;
4112 1.1.1.2 christos }
4113 1.1.1.2 christos }
4114 1.1.1.2 christos
4115 1.1.1.2 christos /* Any unknown registers in the CSR feature are recorded within a single
4116 1.1.1.2 christos block so we can easily identify these registers when making choices
4117 1.1.1.2 christos about register groups in riscv_register_reggroup_p. */
4118 1.1.1.3 christos if (strcmp (tdesc_feature_name (feature), riscv_csr_feature.name ()) == 0)
4119 1.1.1.2 christos {
4120 1.1.1.3 christos riscv_gdbarch_tdep *tdep = gdbarch_tdep<riscv_gdbarch_tdep> (gdbarch);
4121 1.1.1.2 christos if (tdep->unknown_csrs_first_regnum == -1)
4122 1.1.1.2 christos tdep->unknown_csrs_first_regnum = possible_regnum;
4123 1.1.1.2 christos gdb_assert (tdep->unknown_csrs_first_regnum
4124 1.1.1.2 christos + tdep->unknown_csrs_count == possible_regnum);
4125 1.1.1.2 christos tdep->unknown_csrs_count++;
4126 1.1.1.2 christos return possible_regnum;
4127 1.1.1.2 christos }
4128 1.1.1.2 christos
4129 1.1.1.2 christos /* Some other unknown register. Don't assign this a number now, it will
4130 1.1.1.2 christos be assigned a number automatically later by the target description
4131 1.1.1.2 christos handling code. */
4132 1.1.1.2 christos return -1;
4133 1.1.1.2 christos }
4134 1.1.1.2 christos
4135 1.1.1.2 christos /* Implement the gnu_triplet_regexp method. A single compiler supports both
4136 1.1.1.2 christos 32-bit and 64-bit code, and may be named riscv32 or riscv64 or (not
4137 1.1.1.2 christos recommended) riscv. */
4138 1.1.1.2 christos
4139 1.1.1.2 christos static const char *
4140 1.1.1.2 christos riscv_gnu_triplet_regexp (struct gdbarch *gdbarch)
4141 1.1.1.2 christos {
4142 1.1.1.2 christos return "riscv(32|64)?";
4143 1.1.1.2 christos }
4144 1.1.1.2 christos
4145 1.1.1.4 christos /* Implementation of `gdbarch_stap_is_single_operand', as defined in
4146 1.1.1.4 christos gdbarch.h. */
4147 1.1.1.4 christos
4148 1.1.1.4 christos static int
4149 1.1.1.4 christos riscv_stap_is_single_operand (struct gdbarch *gdbarch, const char *s)
4150 1.1.1.4 christos {
4151 1.1.1.4 christos return (ISDIGIT (*s) /* Literal number. */
4152 1.1.1.4 christos || *s == '(' /* Register indirection. */
4153 1.1.1.4 christos || ISALPHA (*s)); /* Register value. */
4154 1.1.1.4 christos }
4155 1.1.1.4 christos
4156 1.1.1.4 christos /* String that appears before a register name in a SystemTap register
4157 1.1.1.4 christos indirect expression. */
4158 1.1.1.4 christos
4159 1.1.1.4 christos static const char *const stap_register_indirection_prefixes[] =
4160 1.1.1.4 christos {
4161 1.1.1.4 christos "(", nullptr
4162 1.1.1.4 christos };
4163 1.1.1.4 christos
4164 1.1.1.4 christos /* String that appears after a register name in a SystemTap register
4165 1.1.1.4 christos indirect expression. */
4166 1.1.1.4 christos
4167 1.1.1.4 christos static const char *const stap_register_indirection_suffixes[] =
4168 1.1.1.4 christos {
4169 1.1.1.4 christos ")", nullptr
4170 1.1.1.4 christos };
4171 1.1.1.4 christos
4172 1.1 christos /* Initialize the current architecture based on INFO. If possible,
4173 1.1 christos re-use an architecture from ARCHES, which is a list of
4174 1.1 christos architectures already created during this debugging session.
4175 1.1 christos
4176 1.1 christos Called e.g. at program startup, when reading a core file, and when
4177 1.1 christos reading a binary file. */
4178 1.1 christos
4179 1.1 christos static struct gdbarch *
4180 1.1 christos riscv_gdbarch_init (struct gdbarch_info info,
4181 1.1 christos struct gdbarch_list *arches)
4182 1.1 christos {
4183 1.1 christos struct riscv_gdbarch_features features;
4184 1.1 christos const struct target_desc *tdesc = info.target_desc;
4185 1.1 christos
4186 1.1 christos /* Ensure we always have a target description. */
4187 1.1 christos if (!tdesc_has_registers (tdesc))
4188 1.1 christos tdesc = riscv_find_default_target_description (info);
4189 1.1.1.3 christos gdb_assert (tdesc != nullptr);
4190 1.1 christos
4191 1.1.1.4 christos riscv_gdbarch_debug_printf ("have got a target description");
4192 1.1 christos
4193 1.1.1.3 christos tdesc_arch_data_up tdesc_data = tdesc_data_alloc ();
4194 1.1.1.2 christos std::vector<riscv_pending_register_alias> pending_aliases;
4195 1.1 christos
4196 1.1.1.3 christos bool valid_p = (riscv_xreg_feature.check (tdesc, tdesc_data.get (),
4197 1.1.1.3 christos &pending_aliases, &features)
4198 1.1.1.3 christos && riscv_freg_feature.check (tdesc, tdesc_data.get (),
4199 1.1.1.3 christos &pending_aliases, &features)
4200 1.1.1.3 christos && riscv_virtual_feature.check (tdesc, tdesc_data.get (),
4201 1.1.1.3 christos &pending_aliases, &features)
4202 1.1.1.3 christos && riscv_csr_feature.check (tdesc, tdesc_data.get (),
4203 1.1.1.3 christos &pending_aliases, &features)
4204 1.1.1.3 christos && riscv_vector_feature.check (tdesc, tdesc_data.get (),
4205 1.1.1.3 christos &pending_aliases, &features));
4206 1.1 christos if (!valid_p)
4207 1.1 christos {
4208 1.1.1.4 christos riscv_gdbarch_debug_printf ("target description is not valid");
4209 1.1 christos return NULL;
4210 1.1 christos }
4211 1.1 christos
4212 1.1.1.3 christos if (tdesc_found_register (tdesc_data.get (), RISCV_CSR_FFLAGS_REGNUM))
4213 1.1.1.3 christos features.has_fflags_reg = true;
4214 1.1.1.3 christos if (tdesc_found_register (tdesc_data.get (), RISCV_CSR_FRM_REGNUM))
4215 1.1.1.3 christos features.has_frm_reg = true;
4216 1.1.1.3 christos if (tdesc_found_register (tdesc_data.get (), RISCV_CSR_FCSR_REGNUM))
4217 1.1.1.3 christos features.has_fcsr_reg = true;
4218 1.1.1.3 christos
4219 1.1 christos /* Have a look at what the supplied (if any) bfd object requires of the
4220 1.1 christos target, then check that this matches with what the target is
4221 1.1 christos providing. */
4222 1.1 christos struct riscv_gdbarch_features abi_features
4223 1.1.1.3 christos = riscv_features_from_bfd (info.abfd);
4224 1.1.1.3 christos
4225 1.1.1.3 christos /* If the ABI_FEATURES xlen is 0 then this indicates we got no useful abi
4226 1.1.1.3 christos features from the INFO object. In this case we just treat the
4227 1.1.1.3 christos hardware features as defining the abi. */
4228 1.1.1.3 christos if (abi_features.xlen == 0)
4229 1.1.1.3 christos abi_features = features;
4230 1.1.1.3 christos
4231 1.1 christos /* In theory a binary compiled for RV32 could run on an RV64 target,
4232 1.1 christos however, this has not been tested in GDB yet, so for now we require
4233 1.1 christos that the requested xlen match the targets xlen. */
4234 1.1.1.3 christos if (abi_features.xlen != features.xlen)
4235 1.1 christos error (_("bfd requires xlen %d, but target has xlen %d"),
4236 1.1.1.3 christos abi_features.xlen, features.xlen);
4237 1.1 christos /* We do support running binaries compiled for 32-bit float on targets
4238 1.1 christos with 64-bit float, so we only complain if the binary requires more
4239 1.1 christos than the target has available. */
4240 1.1 christos if (abi_features.flen > features.flen)
4241 1.1 christos error (_("bfd requires flen %d, but target has flen %d"),
4242 1.1.1.3 christos abi_features.flen, features.flen);
4243 1.1 christos
4244 1.1 christos /* Find a candidate among the list of pre-declared architectures. */
4245 1.1 christos for (arches = gdbarch_list_lookup_by_info (arches, &info);
4246 1.1 christos arches != NULL;
4247 1.1 christos arches = gdbarch_list_lookup_by_info (arches->next, &info))
4248 1.1 christos {
4249 1.1 christos /* Check that the feature set of the ARCHES matches the feature set
4250 1.1.1.3 christos we are looking for. If it doesn't then we can't reuse this
4251 1.1.1.3 christos gdbarch. */
4252 1.1.1.3 christos riscv_gdbarch_tdep *other_tdep
4253 1.1.1.3 christos = gdbarch_tdep<riscv_gdbarch_tdep> (arches->gdbarch);
4254 1.1 christos
4255 1.1 christos if (other_tdep->isa_features != features
4256 1.1 christos || other_tdep->abi_features != abi_features)
4257 1.1.1.3 christos continue;
4258 1.1 christos
4259 1.1 christos break;
4260 1.1 christos }
4261 1.1 christos
4262 1.1 christos if (arches != NULL)
4263 1.1.1.3 christos return arches->gdbarch;
4264 1.1 christos
4265 1.1 christos /* None found, so create a new architecture from the information provided. */
4266 1.1.1.4 christos gdbarch *gdbarch
4267 1.1.1.4 christos = gdbarch_alloc (&info, gdbarch_tdep_up (new riscv_gdbarch_tdep));
4268 1.1.1.4 christos riscv_gdbarch_tdep *tdep = gdbarch_tdep<riscv_gdbarch_tdep> (gdbarch);
4269 1.1.1.4 christos
4270 1.1 christos tdep->isa_features = features;
4271 1.1 christos tdep->abi_features = abi_features;
4272 1.1 christos
4273 1.1 christos /* Target data types. */
4274 1.1 christos set_gdbarch_short_bit (gdbarch, 16);
4275 1.1 christos set_gdbarch_int_bit (gdbarch, 32);
4276 1.1 christos set_gdbarch_long_bit (gdbarch, riscv_isa_xlen (gdbarch) * 8);
4277 1.1 christos set_gdbarch_long_long_bit (gdbarch, 64);
4278 1.1 christos set_gdbarch_float_bit (gdbarch, 32);
4279 1.1 christos set_gdbarch_double_bit (gdbarch, 64);
4280 1.1 christos set_gdbarch_long_double_bit (gdbarch, 128);
4281 1.1.1.3 christos set_gdbarch_long_double_format (gdbarch, floatformats_ieee_quad);
4282 1.1 christos set_gdbarch_ptr_bit (gdbarch, riscv_isa_xlen (gdbarch) * 8);
4283 1.1 christos set_gdbarch_char_signed (gdbarch, 0);
4284 1.1.1.2 christos set_gdbarch_type_align (gdbarch, riscv_type_align);
4285 1.1 christos
4286 1.1 christos /* Information about the target architecture. */
4287 1.1.1.4 christos set_gdbarch_return_value_as_value (gdbarch, riscv_return_value);
4288 1.1 christos set_gdbarch_breakpoint_kind_from_pc (gdbarch, riscv_breakpoint_kind_from_pc);
4289 1.1 christos set_gdbarch_sw_breakpoint_from_kind (gdbarch, riscv_sw_breakpoint_from_kind);
4290 1.1 christos set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
4291 1.1 christos
4292 1.1 christos /* Functions to analyze frames. */
4293 1.1 christos set_gdbarch_skip_prologue (gdbarch, riscv_skip_prologue);
4294 1.1 christos set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
4295 1.1 christos set_gdbarch_frame_align (gdbarch, riscv_frame_align);
4296 1.1 christos
4297 1.1 christos /* Functions handling dummy frames. */
4298 1.1 christos set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
4299 1.1 christos set_gdbarch_push_dummy_code (gdbarch, riscv_push_dummy_code);
4300 1.1 christos set_gdbarch_push_dummy_call (gdbarch, riscv_push_dummy_call);
4301 1.1 christos
4302 1.1 christos /* Frame unwinders. Use DWARF debug info if available, otherwise use our own
4303 1.1 christos unwinder. */
4304 1.1 christos dwarf2_append_unwinders (gdbarch);
4305 1.1 christos frame_unwind_append_unwinder (gdbarch, &riscv_frame_unwind);
4306 1.1 christos
4307 1.1 christos /* Register architecture. */
4308 1.1 christos riscv_add_reggroups (gdbarch);
4309 1.1 christos
4310 1.1 christos /* Internal <-> external register number maps. */
4311 1.1 christos set_gdbarch_dwarf2_reg_to_regnum (gdbarch, riscv_dwarf_reg_to_regnum);
4312 1.1 christos
4313 1.1 christos /* We reserve all possible register numbers for the known registers.
4314 1.1 christos This means the target description mechanism will add any target
4315 1.1 christos specific registers after this number. This helps make debugging GDB
4316 1.1 christos just a little easier. */
4317 1.1 christos set_gdbarch_num_regs (gdbarch, RISCV_LAST_REGNUM + 1);
4318 1.1 christos
4319 1.1 christos /* Some specific register numbers GDB likes to know about. */
4320 1.1 christos set_gdbarch_sp_regnum (gdbarch, RISCV_SP_REGNUM);
4321 1.1 christos set_gdbarch_pc_regnum (gdbarch, RISCV_PC_REGNUM);
4322 1.1 christos
4323 1.1 christos set_gdbarch_print_registers_info (gdbarch, riscv_print_registers_info);
4324 1.1 christos
4325 1.1.1.3 christos set_tdesc_pseudo_register_name (gdbarch, riscv_pseudo_register_name);
4326 1.1.1.3 christos set_tdesc_pseudo_register_type (gdbarch, riscv_pseudo_register_type);
4327 1.1.1.3 christos set_tdesc_pseudo_register_reggroup_p (gdbarch,
4328 1.1.1.3 christos riscv_pseudo_register_reggroup_p);
4329 1.1.1.3 christos set_gdbarch_pseudo_register_read (gdbarch, riscv_pseudo_register_read);
4330 1.1.1.4 christos set_gdbarch_deprecated_pseudo_register_write (gdbarch,
4331 1.1.1.4 christos riscv_pseudo_register_write);
4332 1.1.1.3 christos
4333 1.1 christos /* Finalise the target description registers. */
4334 1.1.1.3 christos tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data),
4335 1.1.1.3 christos riscv_tdesc_unknown_reg);
4336 1.1.1.3 christos
4337 1.1.1.3 christos /* Calculate the number of pseudo registers we need. The fflags and frm
4338 1.1.1.3 christos registers are sub-fields of the fcsr CSR register (csr3). However,
4339 1.1.1.3 christos these registers can also be accessed directly as separate CSR
4340 1.1.1.3 christos registers (fflags is csr1, and frm is csr2). And so, some targets
4341 1.1.1.3 christos might choose to offer direct access to all three registers in the
4342 1.1.1.3 christos target description, while other targets might choose to only offer
4343 1.1.1.3 christos access to fcsr.
4344 1.1.1.3 christos
4345 1.1.1.3 christos As we scan the target description we spot which of fcsr, fflags, and
4346 1.1.1.3 christos frm are available. If fcsr is available but either of fflags and/or
4347 1.1.1.3 christos frm are not available, then we add pseudo-registers to provide the
4348 1.1.1.3 christos missing functionality.
4349 1.1.1.3 christos
4350 1.1.1.3 christos This has to be done after the call to tdesc_use_registers as we don't
4351 1.1.1.3 christos know the final register number until after that call, and the pseudo
4352 1.1.1.3 christos register numbers need to be after the physical registers. */
4353 1.1.1.3 christos int num_pseudo_regs = 0;
4354 1.1.1.3 christos int next_pseudo_regnum = gdbarch_num_regs (gdbarch);
4355 1.1.1.3 christos
4356 1.1.1.3 christos if (features.has_fflags_reg)
4357 1.1.1.3 christos tdep->fflags_regnum = RISCV_CSR_FFLAGS_REGNUM;
4358 1.1.1.3 christos else if (features.has_fcsr_reg)
4359 1.1.1.3 christos {
4360 1.1.1.3 christos tdep->fflags_regnum = next_pseudo_regnum;
4361 1.1.1.3 christos pending_aliases.emplace_back ("csr1", (void *) &tdep->fflags_regnum);
4362 1.1.1.3 christos next_pseudo_regnum++;
4363 1.1.1.3 christos num_pseudo_regs++;
4364 1.1.1.3 christos }
4365 1.1.1.3 christos
4366 1.1.1.3 christos if (features.has_frm_reg)
4367 1.1.1.3 christos tdep->frm_regnum = RISCV_CSR_FRM_REGNUM;
4368 1.1.1.3 christos else if (features.has_fcsr_reg)
4369 1.1.1.3 christos {
4370 1.1.1.3 christos tdep->frm_regnum = next_pseudo_regnum;
4371 1.1.1.3 christos pending_aliases.emplace_back ("csr2", (void *) &tdep->frm_regnum);
4372 1.1.1.3 christos next_pseudo_regnum++;
4373 1.1.1.3 christos num_pseudo_regs++;
4374 1.1.1.3 christos }
4375 1.1.1.3 christos
4376 1.1.1.3 christos set_gdbarch_num_pseudo_regs (gdbarch, num_pseudo_regs);
4377 1.1 christos
4378 1.1 christos /* Override the register type callback setup by the target description
4379 1.1 christos mechanism. This allows us to provide special type for floating point
4380 1.1 christos registers. */
4381 1.1 christos set_gdbarch_register_type (gdbarch, riscv_register_type);
4382 1.1 christos
4383 1.1 christos /* Override the register name callback setup by the target description
4384 1.1 christos mechanism. This allows us to force our preferred names for the
4385 1.1 christos registers, no matter what the target description called them. */
4386 1.1 christos set_gdbarch_register_name (gdbarch, riscv_register_name);
4387 1.1 christos
4388 1.1.1.3 christos /* Tell GDB which RISC-V registers are read-only. */
4389 1.1.1.3 christos set_gdbarch_cannot_store_register (gdbarch, riscv_cannot_store_register);
4390 1.1.1.3 christos
4391 1.1 christos /* Override the register group callback setup by the target description
4392 1.1 christos mechanism. This allows us to force registers into the groups we
4393 1.1 christos want, ignoring what the target tells us. */
4394 1.1 christos set_gdbarch_register_reggroup_p (gdbarch, riscv_register_reggroup_p);
4395 1.1 christos
4396 1.1.1.2 christos /* Create register aliases for alternative register names. We only
4397 1.1.1.2 christos create aliases for registers which were mentioned in the target
4398 1.1.1.2 christos description. */
4399 1.1.1.2 christos for (const auto &alias : pending_aliases)
4400 1.1.1.2 christos alias.create (gdbarch);
4401 1.1.1.2 christos
4402 1.1.1.2 christos /* Compile command hooks. */
4403 1.1.1.2 christos set_gdbarch_gcc_target_options (gdbarch, riscv_gcc_target_options);
4404 1.1.1.2 christos set_gdbarch_gnu_triplet_regexp (gdbarch, riscv_gnu_triplet_regexp);
4405 1.1 christos
4406 1.1.1.3 christos /* Disassembler options support. */
4407 1.1.1.3 christos set_gdbarch_valid_disassembler_options (gdbarch,
4408 1.1.1.3 christos disassembler_options_riscv ());
4409 1.1.1.3 christos set_gdbarch_disassembler_options (gdbarch, &riscv_disassembler_options);
4410 1.1.1.3 christos
4411 1.1.1.4 christos /* SystemTap Support. */
4412 1.1.1.4 christos set_gdbarch_stap_is_single_operand (gdbarch, riscv_stap_is_single_operand);
4413 1.1.1.4 christos set_gdbarch_stap_register_indirection_prefixes
4414 1.1.1.4 christos (gdbarch, stap_register_indirection_prefixes);
4415 1.1.1.4 christos set_gdbarch_stap_register_indirection_suffixes
4416 1.1.1.4 christos (gdbarch, stap_register_indirection_suffixes);
4417 1.1.1.4 christos
4418 1.1 christos /* Hook in OS ABI-specific overrides, if they have been registered. */
4419 1.1 christos gdbarch_init_osabi (info, gdbarch);
4420 1.1 christos
4421 1.1.1.2 christos register_riscv_ravenscar_ops (gdbarch);
4422 1.1.1.2 christos
4423 1.1 christos return gdbarch;
4424 1.1 christos }
4425 1.1 christos
4426 1.1 christos /* This decodes the current instruction and determines the address of the
4427 1.1 christos next instruction. */
4428 1.1 christos
4429 1.1 christos static CORE_ADDR
4430 1.1 christos riscv_next_pc (struct regcache *regcache, CORE_ADDR pc)
4431 1.1 christos {
4432 1.1 christos struct gdbarch *gdbarch = regcache->arch ();
4433 1.1.1.3 christos const riscv_gdbarch_tdep *tdep
4434 1.1.1.3 christos = gdbarch_tdep<riscv_gdbarch_tdep> (gdbarch);
4435 1.1 christos struct riscv_insn insn;
4436 1.1 christos CORE_ADDR next_pc;
4437 1.1 christos
4438 1.1 christos insn.decode (gdbarch, pc);
4439 1.1 christos next_pc = pc + insn.length ();
4440 1.1 christos
4441 1.1 christos if (insn.opcode () == riscv_insn::JAL)
4442 1.1 christos next_pc = pc + insn.imm_signed ();
4443 1.1 christos else if (insn.opcode () == riscv_insn::JALR)
4444 1.1 christos {
4445 1.1 christos LONGEST source;
4446 1.1 christos regcache->cooked_read (insn.rs1 (), &source);
4447 1.1 christos next_pc = (source + insn.imm_signed ()) & ~(CORE_ADDR) 0x1;
4448 1.1 christos }
4449 1.1 christos else if (insn.opcode () == riscv_insn::BEQ)
4450 1.1 christos {
4451 1.1 christos LONGEST src1, src2;
4452 1.1 christos regcache->cooked_read (insn.rs1 (), &src1);
4453 1.1 christos regcache->cooked_read (insn.rs2 (), &src2);
4454 1.1 christos if (src1 == src2)
4455 1.1 christos next_pc = pc + insn.imm_signed ();
4456 1.1 christos }
4457 1.1 christos else if (insn.opcode () == riscv_insn::BNE)
4458 1.1 christos {
4459 1.1 christos LONGEST src1, src2;
4460 1.1 christos regcache->cooked_read (insn.rs1 (), &src1);
4461 1.1 christos regcache->cooked_read (insn.rs2 (), &src2);
4462 1.1 christos if (src1 != src2)
4463 1.1 christos next_pc = pc + insn.imm_signed ();
4464 1.1 christos }
4465 1.1 christos else if (insn.opcode () == riscv_insn::BLT)
4466 1.1 christos {
4467 1.1 christos LONGEST src1, src2;
4468 1.1 christos regcache->cooked_read (insn.rs1 (), &src1);
4469 1.1 christos regcache->cooked_read (insn.rs2 (), &src2);
4470 1.1 christos if (src1 < src2)
4471 1.1 christos next_pc = pc + insn.imm_signed ();
4472 1.1 christos }
4473 1.1 christos else if (insn.opcode () == riscv_insn::BGE)
4474 1.1 christos {
4475 1.1 christos LONGEST src1, src2;
4476 1.1 christos regcache->cooked_read (insn.rs1 (), &src1);
4477 1.1 christos regcache->cooked_read (insn.rs2 (), &src2);
4478 1.1 christos if (src1 >= src2)
4479 1.1 christos next_pc = pc + insn.imm_signed ();
4480 1.1 christos }
4481 1.1 christos else if (insn.opcode () == riscv_insn::BLTU)
4482 1.1 christos {
4483 1.1 christos ULONGEST src1, src2;
4484 1.1 christos regcache->cooked_read (insn.rs1 (), &src1);
4485 1.1 christos regcache->cooked_read (insn.rs2 (), &src2);
4486 1.1 christos if (src1 < src2)
4487 1.1 christos next_pc = pc + insn.imm_signed ();
4488 1.1 christos }
4489 1.1 christos else if (insn.opcode () == riscv_insn::BGEU)
4490 1.1 christos {
4491 1.1 christos ULONGEST src1, src2;
4492 1.1 christos regcache->cooked_read (insn.rs1 (), &src1);
4493 1.1 christos regcache->cooked_read (insn.rs2 (), &src2);
4494 1.1 christos if (src1 >= src2)
4495 1.1 christos next_pc = pc + insn.imm_signed ();
4496 1.1 christos }
4497 1.1.1.3 christos else if (insn.opcode () == riscv_insn::ECALL)
4498 1.1.1.3 christos {
4499 1.1.1.3 christos if (tdep->syscall_next_pc != nullptr)
4500 1.1.1.3 christos next_pc = tdep->syscall_next_pc (get_current_frame ());
4501 1.1.1.3 christos }
4502 1.1 christos
4503 1.1 christos return next_pc;
4504 1.1 christos }
4505 1.1 christos
4506 1.1.1.4 christos /* Return true if INSN is not a control transfer instruction and is allowed to
4507 1.1.1.4 christos appear in the middle of the lr/sc sequence. */
4508 1.1.1.4 christos
4509 1.1.1.4 christos static bool
4510 1.1.1.4 christos riscv_insn_is_non_cti_and_allowed_in_atomic_sequence
4511 1.1.1.4 christos (const struct riscv_insn &insn)
4512 1.1.1.4 christos {
4513 1.1.1.4 christos switch (insn.opcode ())
4514 1.1.1.4 christos {
4515 1.1.1.4 christos case riscv_insn::LUI:
4516 1.1.1.4 christos case riscv_insn::AUIPC:
4517 1.1.1.4 christos case riscv_insn::ADDI:
4518 1.1.1.4 christos case riscv_insn::ADDIW:
4519 1.1.1.4 christos case riscv_insn::SLTI:
4520 1.1.1.4 christos case riscv_insn::SLTIU:
4521 1.1.1.4 christos case riscv_insn::XORI:
4522 1.1.1.4 christos case riscv_insn::ORI:
4523 1.1.1.4 christos case riscv_insn::ANDI:
4524 1.1.1.4 christos case riscv_insn::SLLI:
4525 1.1.1.4 christos case riscv_insn::SLLIW:
4526 1.1.1.4 christos case riscv_insn::SRLI:
4527 1.1.1.4 christos case riscv_insn::SRLIW:
4528 1.1.1.4 christos case riscv_insn::SRAI:
4529 1.1.1.4 christos case riscv_insn::ADD:
4530 1.1.1.4 christos case riscv_insn::ADDW:
4531 1.1.1.4 christos case riscv_insn::SRAIW:
4532 1.1.1.4 christos case riscv_insn::SUB:
4533 1.1.1.4 christos case riscv_insn::SUBW:
4534 1.1.1.4 christos case riscv_insn::SLL:
4535 1.1.1.4 christos case riscv_insn::SLLW:
4536 1.1.1.4 christos case riscv_insn::SLT:
4537 1.1.1.4 christos case riscv_insn::SLTU:
4538 1.1.1.4 christos case riscv_insn::XOR:
4539 1.1.1.4 christos case riscv_insn::SRL:
4540 1.1.1.4 christos case riscv_insn::SRLW:
4541 1.1.1.4 christos case riscv_insn::SRA:
4542 1.1.1.4 christos case riscv_insn::SRAW:
4543 1.1.1.4 christos case riscv_insn::OR:
4544 1.1.1.4 christos case riscv_insn::AND:
4545 1.1.1.4 christos return true;
4546 1.1.1.4 christos }
4547 1.1.1.4 christos
4548 1.1.1.4 christos return false;
4549 1.1.1.4 christos }
4550 1.1.1.4 christos
4551 1.1.1.4 christos /* Return true if INSN is a direct branch instruction. */
4552 1.1.1.4 christos
4553 1.1.1.4 christos static bool
4554 1.1.1.4 christos riscv_insn_is_direct_branch (const struct riscv_insn &insn)
4555 1.1.1.4 christos {
4556 1.1.1.4 christos switch (insn.opcode ())
4557 1.1.1.4 christos {
4558 1.1.1.4 christos case riscv_insn::BEQ:
4559 1.1.1.4 christos case riscv_insn::BNE:
4560 1.1.1.4 christos case riscv_insn::BLT:
4561 1.1.1.4 christos case riscv_insn::BGE:
4562 1.1.1.4 christos case riscv_insn::BLTU:
4563 1.1.1.4 christos case riscv_insn::BGEU:
4564 1.1.1.4 christos case riscv_insn::JAL:
4565 1.1.1.4 christos return true;
4566 1.1.1.4 christos }
4567 1.1.1.4 christos
4568 1.1.1.4 christos return false;
4569 1.1.1.4 christos }
4570 1.1.1.4 christos
4571 1.1 christos /* We can't put a breakpoint in the middle of a lr/sc atomic sequence, so look
4572 1.1 christos for the end of the sequence and put the breakpoint there. */
4573 1.1 christos
4574 1.1.1.4 christos static std::vector<CORE_ADDR>
4575 1.1.1.4 christos riscv_deal_with_atomic_sequence (struct regcache *regcache, CORE_ADDR pc)
4576 1.1 christos {
4577 1.1 christos struct gdbarch *gdbarch = regcache->arch ();
4578 1.1 christos struct riscv_insn insn;
4579 1.1.1.4 christos CORE_ADDR cur_step_pc = pc, next_pc;
4580 1.1.1.4 christos std::vector<CORE_ADDR> next_pcs;
4581 1.1.1.4 christos bool found_valid_atomic_sequence = false;
4582 1.1.1.4 christos enum riscv_insn::opcode lr_opcode;
4583 1.1 christos
4584 1.1 christos /* First instruction has to be a load reserved. */
4585 1.1 christos insn.decode (gdbarch, cur_step_pc);
4586 1.1.1.4 christos lr_opcode = insn.opcode ();
4587 1.1.1.4 christos if (lr_opcode != riscv_insn::LR_D && lr_opcode != riscv_insn::LR_W)
4588 1.1.1.4 christos return {};
4589 1.1.1.4 christos
4590 1.1.1.4 christos /* The loop comprises only an LR/SC sequence and code to retry the sequence in
4591 1.1.1.4 christos the case of failure, and must comprise at most 16 instructions placed
4592 1.1.1.4 christos sequentially in memory. While our code tries to follow these restrictions,
4593 1.1.1.4 christos it has the following limitations:
4594 1.1.1.4 christos
4595 1.1.1.4 christos (a) We expect the loop to start with an LR and end with a BNE.
4596 1.1.1.4 christos Apparently this does not cover all cases for a valid sequence.
4597 1.1.1.4 christos (b) The atomic limitations only apply to the code that is actually
4598 1.1.1.4 christos executed, so here again it's overly restrictive.
4599 1.1.1.4 christos (c) The lr/sc are required to be for the same target address, but this
4600 1.1.1.4 christos information is only known at runtime. Same as (b), in order to check
4601 1.1.1.4 christos this we will end up needing to simulate the sequence, which is more
4602 1.1.1.4 christos complicated than what we're doing right now.
4603 1.1.1.4 christos
4604 1.1.1.4 christos Also note that we only expect a maximum of (16-2) instructions in the for
4605 1.1.1.4 christos loop as we have assumed the presence of LR and BNE at the beginning and end
4606 1.1.1.4 christos respectively. */
4607 1.1.1.4 christos for (int insn_count = 0; insn_count < 16 - 2; ++insn_count)
4608 1.1.1.4 christos {
4609 1.1.1.4 christos cur_step_pc += insn.length ();
4610 1.1.1.4 christos insn.decode (gdbarch, cur_step_pc);
4611 1.1.1.4 christos
4612 1.1.1.4 christos /* The dynamic code executed between lr/sc can only contain instructions
4613 1.1.1.4 christos from the base I instruction set, excluding loads, stores, backward
4614 1.1.1.4 christos jumps, taken backward branches, JALR, FENCE, FENCE.I, and SYSTEM
4615 1.1.1.4 christos instructions. If the C extension is supported, then compressed forms
4616 1.1.1.4 christos of the aforementioned I instructions are also permitted. */
4617 1.1 christos
4618 1.1.1.4 christos if (riscv_insn_is_non_cti_and_allowed_in_atomic_sequence (insn))
4619 1.1.1.4 christos continue;
4620 1.1.1.4 christos /* Look for a conditional branch instruction, check if it's taken forward
4621 1.1.1.4 christos or not. */
4622 1.1.1.4 christos else if (riscv_insn_is_direct_branch (insn))
4623 1.1.1.4 christos {
4624 1.1.1.4 christos if (insn.imm_signed () > 0)
4625 1.1.1.4 christos {
4626 1.1.1.4 christos next_pc = cur_step_pc + insn.imm_signed ();
4627 1.1.1.4 christos next_pcs.push_back (next_pc);
4628 1.1.1.4 christos }
4629 1.1.1.4 christos else
4630 1.1.1.4 christos break;
4631 1.1.1.4 christos }
4632 1.1.1.4 christos /* Look for a paired SC instruction which closes the atomic sequence. */
4633 1.1.1.4 christos else if ((insn.opcode () == riscv_insn::SC_D
4634 1.1.1.4 christos && lr_opcode == riscv_insn::LR_D)
4635 1.1.1.4 christos || (insn.opcode () == riscv_insn::SC_W
4636 1.1.1.4 christos && lr_opcode == riscv_insn::LR_W))
4637 1.1.1.4 christos found_valid_atomic_sequence = true;
4638 1.1.1.4 christos else
4639 1.1.1.4 christos break;
4640 1.1.1.4 christos }
4641 1.1 christos
4642 1.1.1.4 christos if (!found_valid_atomic_sequence)
4643 1.1.1.4 christos return {};
4644 1.1 christos
4645 1.1 christos /* Next instruction should be branch to start. */
4646 1.1 christos insn.decode (gdbarch, cur_step_pc);
4647 1.1 christos if (insn.opcode () != riscv_insn::BNE)
4648 1.1.1.4 christos return {};
4649 1.1 christos if (pc != (cur_step_pc + insn.imm_signed ()))
4650 1.1.1.4 christos return {};
4651 1.1.1.4 christos cur_step_pc += insn.length ();
4652 1.1 christos
4653 1.1.1.4 christos /* Remove all PCs that jump within the sequence. */
4654 1.1.1.4 christos auto matcher = [cur_step_pc] (const CORE_ADDR addr) -> bool
4655 1.1.1.4 christos {
4656 1.1.1.4 christos return addr < cur_step_pc;
4657 1.1.1.4 christos };
4658 1.1.1.4 christos auto it = std::remove_if (next_pcs.begin (), next_pcs.end (), matcher);
4659 1.1.1.4 christos next_pcs.erase (it, next_pcs.end ());
4660 1.1 christos
4661 1.1.1.4 christos next_pc = cur_step_pc;
4662 1.1.1.4 christos next_pcs.push_back (next_pc);
4663 1.1.1.4 christos return next_pcs;
4664 1.1 christos }
4665 1.1 christos
4666 1.1 christos /* This is called just before we want to resume the inferior, if we want to
4667 1.1 christos single-step it but there is no hardware or kernel single-step support. We
4668 1.1 christos find the target of the coming instruction and breakpoint it. */
4669 1.1 christos
4670 1.1 christos std::vector<CORE_ADDR>
4671 1.1 christos riscv_software_single_step (struct regcache *regcache)
4672 1.1 christos {
4673 1.1.1.4 christos CORE_ADDR cur_pc = regcache_read_pc (regcache), next_pc;
4674 1.1.1.4 christos std::vector<CORE_ADDR> next_pcs
4675 1.1.1.4 christos = riscv_deal_with_atomic_sequence (regcache, cur_pc);
4676 1.1 christos
4677 1.1.1.4 christos if (!next_pcs.empty ())
4678 1.1.1.4 christos return next_pcs;
4679 1.1 christos
4680 1.1.1.4 christos next_pc = riscv_next_pc (regcache, cur_pc);
4681 1.1 christos
4682 1.1 christos return {next_pc};
4683 1.1 christos }
4684 1.1 christos
4685 1.1 christos /* Create RISC-V specific reggroups. */
4686 1.1 christos
4687 1.1 christos static void
4688 1.1 christos riscv_init_reggroups ()
4689 1.1 christos {
4690 1.1 christos csr_reggroup = reggroup_new ("csr", USER_REGGROUP);
4691 1.1 christos }
4692 1.1 christos
4693 1.1.1.3 christos /* See riscv-tdep.h. */
4694 1.1.1.3 christos
4695 1.1.1.3 christos void
4696 1.1.1.3 christos riscv_supply_regset (const struct regset *regset,
4697 1.1.1.3 christos struct regcache *regcache, int regnum,
4698 1.1.1.3 christos const void *regs, size_t len)
4699 1.1.1.3 christos {
4700 1.1.1.3 christos regcache->supply_regset (regset, regnum, regs, len);
4701 1.1.1.3 christos
4702 1.1.1.3 christos if (regnum == -1 || regnum == RISCV_ZERO_REGNUM)
4703 1.1.1.3 christos regcache->raw_supply_zeroed (RISCV_ZERO_REGNUM);
4704 1.1.1.3 christos
4705 1.1.1.3 christos struct gdbarch *gdbarch = regcache->arch ();
4706 1.1.1.3 christos riscv_gdbarch_tdep *tdep = gdbarch_tdep<riscv_gdbarch_tdep> (gdbarch);
4707 1.1.1.3 christos
4708 1.1.1.3 christos if (regnum == -1
4709 1.1.1.3 christos || regnum == tdep->fflags_regnum
4710 1.1.1.3 christos || regnum == tdep->frm_regnum)
4711 1.1.1.3 christos {
4712 1.1.1.3 christos int fcsr_regnum = RISCV_CSR_FCSR_REGNUM;
4713 1.1.1.3 christos
4714 1.1.1.3 christos /* Ensure that FCSR has been read into REGCACHE. */
4715 1.1.1.3 christos if (regnum != -1)
4716 1.1.1.3 christos regcache->supply_regset (regset, fcsr_regnum, regs, len);
4717 1.1.1.3 christos
4718 1.1.1.3 christos /* Grab the FCSR value if it is now in the regcache. We must check
4719 1.1.1.3 christos the status first as, if the register was not supplied by REGSET,
4720 1.1.1.3 christos this call will trigger a recursive attempt to fetch the
4721 1.1.1.3 christos registers. */
4722 1.1.1.3 christos if (regcache->get_register_status (fcsr_regnum) == REG_VALID)
4723 1.1.1.3 christos {
4724 1.1.1.3 christos /* If we have an fcsr register then we should have fflags and frm
4725 1.1.1.3 christos too, either provided by the target, or provided as a pseudo
4726 1.1.1.3 christos register by GDB. */
4727 1.1.1.3 christos gdb_assert (tdep->fflags_regnum >= 0);
4728 1.1.1.3 christos gdb_assert (tdep->frm_regnum >= 0);
4729 1.1.1.3 christos
4730 1.1.1.3 christos ULONGEST fcsr_val;
4731 1.1.1.3 christos regcache->raw_read (fcsr_regnum, &fcsr_val);
4732 1.1.1.3 christos
4733 1.1.1.3 christos /* Extract the fflags and frm values. */
4734 1.1.1.3 christos ULONGEST fflags_val = fcsr_val & 0x1f;
4735 1.1.1.3 christos ULONGEST frm_val = (fcsr_val >> 5) & 0x7;
4736 1.1.1.3 christos
4737 1.1.1.3 christos /* And supply these if needed. We can only supply real
4738 1.1.1.3 christos registers, so don't try to supply fflags or frm if they are
4739 1.1.1.3 christos implemented as pseudo-registers. */
4740 1.1.1.3 christos if ((regnum == -1 || regnum == tdep->fflags_regnum)
4741 1.1.1.3 christos && tdep->fflags_regnum < gdbarch_num_regs (gdbarch))
4742 1.1.1.3 christos regcache->raw_supply_integer (tdep->fflags_regnum,
4743 1.1.1.3 christos (gdb_byte *) &fflags_val,
4744 1.1.1.3 christos sizeof (fflags_val),
4745 1.1.1.3 christos /* is_signed */ false);
4746 1.1.1.3 christos
4747 1.1.1.3 christos if ((regnum == -1 || regnum == tdep->frm_regnum)
4748 1.1.1.3 christos && tdep->frm_regnum < gdbarch_num_regs (gdbarch))
4749 1.1.1.3 christos regcache->raw_supply_integer (tdep->frm_regnum,
4750 1.1.1.3 christos (gdb_byte *)&frm_val,
4751 1.1.1.3 christos sizeof (fflags_val),
4752 1.1.1.3 christos /* is_signed */ false);
4753 1.1.1.3 christos }
4754 1.1.1.3 christos }
4755 1.1.1.3 christos }
4756 1.1.1.3 christos
4757 1.1.1.2 christos void _initialize_riscv_tdep ();
4758 1.1 christos void
4759 1.1.1.2 christos _initialize_riscv_tdep ()
4760 1.1 christos {
4761 1.1 christos riscv_init_reggroups ();
4762 1.1 christos
4763 1.1 christos gdbarch_register (bfd_arch_riscv, riscv_gdbarch_init, NULL);
4764 1.1 christos
4765 1.1 christos /* Add root prefix command for all "set debug riscv" and "show debug
4766 1.1 christos riscv" commands. */
4767 1.1.1.3 christos add_setshow_prefix_cmd ("riscv", no_class,
4768 1.1.1.3 christos _("RISC-V specific debug commands."),
4769 1.1.1.3 christos _("RISC-V specific debug commands."),
4770 1.1.1.3 christos &setdebugriscvcmdlist, &showdebugriscvcmdlist,
4771 1.1.1.3 christos &setdebuglist, &showdebuglist);
4772 1.1 christos
4773 1.1.1.4 christos add_setshow_boolean_cmd ("breakpoints", class_maintenance,
4774 1.1.1.4 christos &riscv_debug_breakpoints, _("\
4775 1.1 christos Set riscv breakpoint debugging."), _("\
4776 1.1 christos Show riscv breakpoint debugging."), _("\
4777 1.1 christos When non-zero, print debugging information for the riscv specific parts\n\
4778 1.1 christos of the breakpoint mechanism."),
4779 1.1.1.4 christos nullptr,
4780 1.1.1.4 christos show_riscv_debug_variable,
4781 1.1.1.4 christos &setdebugriscvcmdlist, &showdebugriscvcmdlist);
4782 1.1 christos
4783 1.1.1.4 christos add_setshow_boolean_cmd ("infcall", class_maintenance,
4784 1.1.1.4 christos &riscv_debug_infcall, _("\
4785 1.1 christos Set riscv inferior call debugging."), _("\
4786 1.1 christos Show riscv inferior call debugging."), _("\
4787 1.1 christos When non-zero, print debugging information for the riscv specific parts\n\
4788 1.1 christos of the inferior call mechanism."),
4789 1.1.1.4 christos nullptr,
4790 1.1.1.4 christos show_riscv_debug_variable,
4791 1.1.1.4 christos &setdebugriscvcmdlist, &showdebugriscvcmdlist);
4792 1.1 christos
4793 1.1.1.4 christos add_setshow_boolean_cmd ("unwinder", class_maintenance,
4794 1.1.1.4 christos &riscv_debug_unwinder, _("\
4795 1.1 christos Set riscv stack unwinding debugging."), _("\
4796 1.1 christos Show riscv stack unwinding debugging."), _("\
4797 1.1.1.4 christos When on, print debugging information for the riscv specific parts\n\
4798 1.1 christos of the stack unwinding mechanism."),
4799 1.1.1.4 christos nullptr,
4800 1.1.1.4 christos show_riscv_debug_variable,
4801 1.1.1.4 christos &setdebugriscvcmdlist, &showdebugriscvcmdlist);
4802 1.1 christos
4803 1.1.1.4 christos add_setshow_boolean_cmd ("gdbarch", class_maintenance,
4804 1.1.1.4 christos &riscv_debug_gdbarch, _("\
4805 1.1 christos Set riscv gdbarch initialisation debugging."), _("\
4806 1.1 christos Show riscv gdbarch initialisation debugging."), _("\
4807 1.1 christos When non-zero, print debugging information for the riscv gdbarch\n\
4808 1.1 christos initialisation process."),
4809 1.1.1.4 christos nullptr,
4810 1.1.1.4 christos show_riscv_debug_variable,
4811 1.1.1.4 christos &setdebugriscvcmdlist, &showdebugriscvcmdlist);
4812 1.1 christos
4813 1.1 christos /* Add root prefix command for all "set riscv" and "show riscv" commands. */
4814 1.1.1.3 christos add_setshow_prefix_cmd ("riscv", no_class,
4815 1.1.1.3 christos _("RISC-V specific commands."),
4816 1.1.1.3 christos _("RISC-V specific commands."),
4817 1.1.1.3 christos &setriscvcmdlist, &showriscvcmdlist,
4818 1.1.1.3 christos &setlist, &showlist);
4819 1.1 christos
4820 1.1 christos
4821 1.1 christos use_compressed_breakpoints = AUTO_BOOLEAN_AUTO;
4822 1.1 christos add_setshow_auto_boolean_cmd ("use-compressed-breakpoints", no_class,
4823 1.1 christos &use_compressed_breakpoints,
4824 1.1 christos _("\
4825 1.1.1.4 christos Set debugger's use of compressed breakpoints."), _("\
4826 1.1 christos Show debugger's use of compressed breakpoints."), _("\
4827 1.1 christos Debugging compressed code requires compressed breakpoints to be used. If\n\
4828 1.1 christos left to 'auto' then gdb will use them if the existing instruction is a\n\
4829 1.1 christos compressed instruction. If that doesn't give the correct behavior, then\n\
4830 1.1 christos this option can be used."),
4831 1.1 christos NULL,
4832 1.1 christos show_use_compressed_breakpoints,
4833 1.1 christos &setriscvcmdlist,
4834 1.1 christos &showriscvcmdlist);
4835 1.1 christos }
4836