1 /* Target-dependent code for the RISC-V architecture, for GDB.
3 Copyright (C) 2018-2020 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
32 #include "arch-utils.h"
35 #include "riscv-tdep.h"
37 #include "reggroups.h"
38 #include "opcode/riscv.h"
39 #include "elf/riscv.h"
43 #include "frame-unwind.h"
44 #include "frame-base.h"
45 #include "trad-frame.h"
47 #include "floatformat.h"
49 #include "target-descriptions.h"
50 #include "dwarf2/frame.h"
51 #include "user-regs.h"
53 #include "gdbsupport/common-defs.h"
54 #include "opcode/riscv-opc.h"
55 #include "cli/cli-decode.h"
56 #include "observable.h"
57 #include "prologue-value.h"
58 #include "arch/riscv.h"
59 #include "riscv-ravenscar-thread.h"
61 /* The stack must be 16-byte aligned. */
62 #define SP_ALIGNMENT 16
64 /* The biggest alignment that the target supports. */
65 #define BIGGEST_ALIGNMENT 16
67 /* Define a series of is_XXX_insn functions to check if the value INSN
68 is an instance of instruction XXX. */
69 #define DECLARE_INSN(INSN_NAME, INSN_MATCH, INSN_MASK) \
70 static inline bool is_ ## INSN_NAME ## _insn (long insn) \
72 return (insn & INSN_MASK) == INSN_MATCH; \
74 #include "opcode/riscv-opc.h"
77 /* Cached information about a frame. */
79 struct riscv_unwind_cache
81 /* The register from which we can calculate the frame base. This is
82 usually $sp or $fp. */
85 /* The offset from the current value in register FRAME_BASE_REG to the
86 actual frame base address. */
87 int frame_base_offset
;
89 /* Information about previous register values. */
90 struct trad_frame_saved_reg
*regs
;
92 /* The id for this frame. */
93 struct frame_id this_id
;
95 /* The base (stack) address for this frame. This is the stack pointer
96 value on entry to this frame before any adjustments are made. */
100 /* RISC-V specific register group for CSRs. */
102 static reggroup
*csr_reggroup
= NULL
;
104 /* Callback function for user_reg_add. */
106 static struct value
*
107 value_of_riscv_user_reg (struct frame_info
*frame
, const void *baton
)
109 const int *reg_p
= (const int *) baton
;
110 return value_of_register (*reg_p
, frame
);
113 /* Information about a register alias that needs to be set up for this
114 target. These are collected when the target's XML description is
115 analysed, and then processed later, once the gdbarch has been created. */
117 class riscv_pending_register_alias
122 riscv_pending_register_alias (const char *name
, const void *baton
)
127 /* Convert this into a user register for GDBARCH. */
129 void create (struct gdbarch
*gdbarch
) const
131 user_reg_add (gdbarch
, m_name
, value_of_riscv_user_reg
, m_baton
);
135 /* The name for this alias. */
138 /* The baton value for passing to user_reg_add. This must point to some
139 data that will live for at least as long as the gdbarch object to
140 which the user register is attached. */
144 /* Registers in the RISCV_REGISTER_FEATURE lists below are either optional,
145 or required. For example the $pc register is always going to be a
146 required register, you can't do much debugging without that. In
147 contrast, most of the CSRs are optional, GDB doesn't require them in
148 order to have a useful debug session. This enum models the difference
149 between these register types. */
151 enum riscv_register_required_status
153 /* This register is optional within this feature. */
156 /* This register is required within this feature. */
159 /* This register is required, the register must either be in this
160 feature, or it could appear within the CSR feature. */
161 RISCV_REG_REQUIRED_MAYBE_CSR
164 /* A set of registers that we expect to find in a tdesc_feature. These
165 are use in RISCV_GDBARCH_INIT when processing the target description. */
167 struct riscv_register_feature
169 /* Information for a single register. */
172 /* The GDB register number for this register. */
175 /* List of names for this register. The first name in this list is the
176 preferred name, the name GDB should use when describing this
178 std::vector
<const char *> names
;
180 /* Is this register required within this feature? In some cases the
181 register could be required, but might also be in the CSR feature. */
182 riscv_register_required_status required
;
184 /* Look in FEATURE for a register with a name from this classes names
185 list. If the register is found then register its number with
186 TDESC_DATA and add all its aliases to the ALIASES list. REG_SET is
187 used to help create the aliases. */
188 bool check (struct tdesc_arch_data
*tdesc_data
,
189 const struct tdesc_feature
*feature
,
190 const struct riscv_register_feature
*reg_set
,
191 std::vector
<riscv_pending_register_alias
> *aliases
) const;
194 /* The name for this feature. This is the name used to find this feature
195 within the target description. */
198 /* For x-regs and f-regs we always force GDB to use the first name from
199 the REGISTERS.NAMES vector, it is therefore important that we create
200 user-register aliases for all of the remaining names at indexes 1+ in
203 For CSRs we take a different approach, we prefer whatever name the
204 target description uses, in this case we want to create user-register
205 aliases for any other names that aren't the target description
208 When this flag is true we are dealing with the first case, and when
209 this is false we are dealing with the latter. */
210 bool prefer_first_name
;
212 /* List of all the registers that we expect that we might find in this
214 std::vector
<struct register_info
> registers
;
217 /* See description in the class declaration above. */
220 riscv_register_feature::register_info::check
221 (struct tdesc_arch_data
*tdesc_data
,
222 const struct tdesc_feature
*feature
,
223 const struct riscv_register_feature
*reg_set
,
224 std::vector
<riscv_pending_register_alias
> *aliases
) const
226 for (const char *name
: this->names
)
228 bool found
= tdesc_numbered_register (feature
, tdesc_data
,
232 /* We know that the target description mentions this
233 register. In RISCV_REGISTER_NAME we ensure that GDB
234 always uses the first name for each register, so here we
235 add aliases for all of the remaining names. */
236 bool prefer_first_name
= reg_set
->prefer_first_name
;
237 int start_index
= prefer_first_name
? 1 : 0;
238 for (int i
= start_index
; i
< this->names
.size (); ++i
)
240 const char *alias
= this->names
[i
];
241 if (alias
== name
&& !prefer_first_name
)
243 aliases
->emplace_back (alias
, (void *) &this->regnum
);
251 /* The general x-registers feature set. */
253 static const struct riscv_register_feature riscv_xreg_feature
=
255 "org.gnu.gdb.riscv.cpu", true,
257 { RISCV_ZERO_REGNUM
+ 0, { "zero", "x0" }, RISCV_REG_REQUIRED
},
258 { RISCV_ZERO_REGNUM
+ 1, { "ra", "x1" }, RISCV_REG_REQUIRED
},
259 { RISCV_ZERO_REGNUM
+ 2, { "sp", "x2" }, RISCV_REG_REQUIRED
},
260 { RISCV_ZERO_REGNUM
+ 3, { "gp", "x3" }, RISCV_REG_REQUIRED
},
261 { RISCV_ZERO_REGNUM
+ 4, { "tp", "x4" }, RISCV_REG_REQUIRED
},
262 { RISCV_ZERO_REGNUM
+ 5, { "t0", "x5" }, RISCV_REG_REQUIRED
},
263 { RISCV_ZERO_REGNUM
+ 6, { "t1", "x6" }, RISCV_REG_REQUIRED
},
264 { RISCV_ZERO_REGNUM
+ 7, { "t2", "x7" }, RISCV_REG_REQUIRED
},
265 { RISCV_ZERO_REGNUM
+ 8, { "fp", "x8", "s0" }, RISCV_REG_REQUIRED
},
266 { RISCV_ZERO_REGNUM
+ 9, { "s1", "x9" }, RISCV_REG_REQUIRED
},
267 { RISCV_ZERO_REGNUM
+ 10, { "a0", "x10" }, RISCV_REG_REQUIRED
},
268 { RISCV_ZERO_REGNUM
+ 11, { "a1", "x11" }, RISCV_REG_REQUIRED
},
269 { RISCV_ZERO_REGNUM
+ 12, { "a2", "x12" }, RISCV_REG_REQUIRED
},
270 { RISCV_ZERO_REGNUM
+ 13, { "a3", "x13" }, RISCV_REG_REQUIRED
},
271 { RISCV_ZERO_REGNUM
+ 14, { "a4", "x14" }, RISCV_REG_REQUIRED
},
272 { RISCV_ZERO_REGNUM
+ 15, { "a5", "x15" }, RISCV_REG_REQUIRED
},
273 { RISCV_ZERO_REGNUM
+ 16, { "a6", "x16" }, RISCV_REG_REQUIRED
},
274 { RISCV_ZERO_REGNUM
+ 17, { "a7", "x17" }, RISCV_REG_REQUIRED
},
275 { RISCV_ZERO_REGNUM
+ 18, { "s2", "x18" }, RISCV_REG_REQUIRED
},
276 { RISCV_ZERO_REGNUM
+ 19, { "s3", "x19" }, RISCV_REG_REQUIRED
},
277 { RISCV_ZERO_REGNUM
+ 20, { "s4", "x20" }, RISCV_REG_REQUIRED
},
278 { RISCV_ZERO_REGNUM
+ 21, { "s5", "x21" }, RISCV_REG_REQUIRED
},
279 { RISCV_ZERO_REGNUM
+ 22, { "s6", "x22" }, RISCV_REG_REQUIRED
},
280 { RISCV_ZERO_REGNUM
+ 23, { "s7", "x23" }, RISCV_REG_REQUIRED
},
281 { RISCV_ZERO_REGNUM
+ 24, { "s8", "x24" }, RISCV_REG_REQUIRED
},
282 { RISCV_ZERO_REGNUM
+ 25, { "s9", "x25" }, RISCV_REG_REQUIRED
},
283 { RISCV_ZERO_REGNUM
+ 26, { "s10", "x26" }, RISCV_REG_REQUIRED
},
284 { RISCV_ZERO_REGNUM
+ 27, { "s11", "x27" }, RISCV_REG_REQUIRED
},
285 { RISCV_ZERO_REGNUM
+ 28, { "t3", "x28" }, RISCV_REG_REQUIRED
},
286 { RISCV_ZERO_REGNUM
+ 29, { "t4", "x29" }, RISCV_REG_REQUIRED
},
287 { RISCV_ZERO_REGNUM
+ 30, { "t5", "x30" }, RISCV_REG_REQUIRED
},
288 { RISCV_ZERO_REGNUM
+ 31, { "t6", "x31" }, RISCV_REG_REQUIRED
},
289 { RISCV_ZERO_REGNUM
+ 32, { "pc" }, RISCV_REG_REQUIRED
}
293 /* The f-registers feature set. */
295 static const struct riscv_register_feature riscv_freg_feature
=
297 "org.gnu.gdb.riscv.fpu", true,
299 { RISCV_FIRST_FP_REGNUM
+ 0, { "ft0", "f0" }, RISCV_REG_REQUIRED
},
300 { RISCV_FIRST_FP_REGNUM
+ 1, { "ft1", "f1" }, RISCV_REG_REQUIRED
},
301 { RISCV_FIRST_FP_REGNUM
+ 2, { "ft2", "f2" }, RISCV_REG_REQUIRED
},
302 { RISCV_FIRST_FP_REGNUM
+ 3, { "ft3", "f3" }, RISCV_REG_REQUIRED
},
303 { RISCV_FIRST_FP_REGNUM
+ 4, { "ft4", "f4" }, RISCV_REG_REQUIRED
},
304 { RISCV_FIRST_FP_REGNUM
+ 5, { "ft5", "f5" }, RISCV_REG_REQUIRED
},
305 { RISCV_FIRST_FP_REGNUM
+ 6, { "ft6", "f6" }, RISCV_REG_REQUIRED
},
306 { RISCV_FIRST_FP_REGNUM
+ 7, { "ft7", "f7" }, RISCV_REG_REQUIRED
},
307 { RISCV_FIRST_FP_REGNUM
+ 8, { "fs0", "f8" }, RISCV_REG_REQUIRED
},
308 { RISCV_FIRST_FP_REGNUM
+ 9, { "fs1", "f9" }, RISCV_REG_REQUIRED
},
309 { RISCV_FIRST_FP_REGNUM
+ 10, { "fa0", "f10" }, RISCV_REG_REQUIRED
},
310 { RISCV_FIRST_FP_REGNUM
+ 11, { "fa1", "f11" }, RISCV_REG_REQUIRED
},
311 { RISCV_FIRST_FP_REGNUM
+ 12, { "fa2", "f12" }, RISCV_REG_REQUIRED
},
312 { RISCV_FIRST_FP_REGNUM
+ 13, { "fa3", "f13" }, RISCV_REG_REQUIRED
},
313 { RISCV_FIRST_FP_REGNUM
+ 14, { "fa4", "f14" }, RISCV_REG_REQUIRED
},
314 { RISCV_FIRST_FP_REGNUM
+ 15, { "fa5", "f15" }, RISCV_REG_REQUIRED
},
315 { RISCV_FIRST_FP_REGNUM
+ 16, { "fa6", "f16" }, RISCV_REG_REQUIRED
},
316 { RISCV_FIRST_FP_REGNUM
+ 17, { "fa7", "f17" }, RISCV_REG_REQUIRED
},
317 { RISCV_FIRST_FP_REGNUM
+ 18, { "fs2", "f18" }, RISCV_REG_REQUIRED
},
318 { RISCV_FIRST_FP_REGNUM
+ 19, { "fs3", "f19" }, RISCV_REG_REQUIRED
},
319 { RISCV_FIRST_FP_REGNUM
+ 20, { "fs4", "f20" }, RISCV_REG_REQUIRED
},
320 { RISCV_FIRST_FP_REGNUM
+ 21, { "fs5", "f21" }, RISCV_REG_REQUIRED
},
321 { RISCV_FIRST_FP_REGNUM
+ 22, { "fs6", "f22" }, RISCV_REG_REQUIRED
},
322 { RISCV_FIRST_FP_REGNUM
+ 23, { "fs7", "f23" }, RISCV_REG_REQUIRED
},
323 { RISCV_FIRST_FP_REGNUM
+ 24, { "fs8", "f24" }, RISCV_REG_REQUIRED
},
324 { RISCV_FIRST_FP_REGNUM
+ 25, { "fs9", "f25" }, RISCV_REG_REQUIRED
},
325 { RISCV_FIRST_FP_REGNUM
+ 26, { "fs10", "f26" }, RISCV_REG_REQUIRED
},
326 { RISCV_FIRST_FP_REGNUM
+ 27, { "fs11", "f27" }, RISCV_REG_REQUIRED
},
327 { RISCV_FIRST_FP_REGNUM
+ 28, { "ft8", "f28" }, RISCV_REG_REQUIRED
},
328 { RISCV_FIRST_FP_REGNUM
+ 29, { "ft9", "f29" }, RISCV_REG_REQUIRED
},
329 { RISCV_FIRST_FP_REGNUM
+ 30, { "ft10", "f30" }, RISCV_REG_REQUIRED
},
330 { RISCV_FIRST_FP_REGNUM
+ 31, { "ft11", "f31" }, RISCV_REG_REQUIRED
},
332 { RISCV_CSR_FFLAGS_REGNUM
, { "fflags", "csr1" }, RISCV_REG_REQUIRED_MAYBE_CSR
},
333 { RISCV_CSR_FRM_REGNUM
, { "frm", "csr2" }, RISCV_REG_REQUIRED_MAYBE_CSR
},
334 { RISCV_CSR_FCSR_REGNUM
, { "fcsr", "csr3" }, RISCV_REG_REQUIRED_MAYBE_CSR
},
339 /* Set of virtual registers. These are not physical registers on the
340 hardware, but might be available from the target. These are not pseudo
341 registers, reading these really does result in a register read from the
342 target, it is just that there might not be a physical register backing
345 static const struct riscv_register_feature riscv_virtual_feature
=
347 "org.gnu.gdb.riscv.virtual", false,
349 { RISCV_PRIV_REGNUM
, { "priv" }, RISCV_REG_OPTIONAL
}
353 /* Feature set for CSRs. This set is NOT constant as the register names
354 list for each register is not complete. The aliases are computed
355 during RISCV_CREATE_CSR_ALIASES. */
357 static struct riscv_register_feature riscv_csr_feature
=
359 "org.gnu.gdb.riscv.csr", false,
361 #define DECLARE_CSR(NAME,VALUE,CLASS,DEFINE_VER,ABORT_VER) \
362 { RISCV_ ## VALUE ## _REGNUM, { # NAME }, RISCV_REG_OPTIONAL },
363 #include "opcode/riscv-opc.h"
368 /* Complete RISCV_CSR_FEATURE, building the CSR alias names and adding them
369 to the name list for each register. */
372 riscv_create_csr_aliases ()
374 for (auto ®
: riscv_csr_feature
.registers
)
376 int csr_num
= reg
.regnum
- RISCV_FIRST_CSR_REGNUM
;
377 const char *alias
= xstrprintf ("csr%d", csr_num
);
378 reg
.names
.push_back (alias
);
380 /* Setup the other csr aliases. We don't use a switch table here in
381 case there are multiple aliases with the same value. Also filter
382 based on ABRT_VER in order to avoid a very old alias for misa that
383 duplicates the name "misa" but at a different CSR address. */
384 #define DECLARE_CSR_ALIAS(NAME,VALUE,CLASS,DEF_VER,ABRT_VER) \
385 if (csr_num == VALUE && ABRT_VER >= PRIV_SPEC_CLASS_1P11) \
386 reg.names.push_back ( # NAME );
387 #include "opcode/riscv-opc.h"
388 #undef DECLARE_CSR_ALIAS
392 /* Controls whether we place compressed breakpoints or not. When in auto
393 mode GDB tries to determine if the target supports compressed
394 breakpoints, and uses them if it does. */
396 static enum auto_boolean use_compressed_breakpoints
;
398 /* The show callback for 'show riscv use-compressed-breakpoints'. */
401 show_use_compressed_breakpoints (struct ui_file
*file
, int from_tty
,
402 struct cmd_list_element
*c
,
405 fprintf_filtered (file
,
406 _("Debugger's use of compressed breakpoints is set "
410 /* The set and show lists for 'set riscv' and 'show riscv' prefixes. */
412 static struct cmd_list_element
*setriscvcmdlist
= NULL
;
413 static struct cmd_list_element
*showriscvcmdlist
= NULL
;
415 /* The set and show lists for 'set riscv' and 'show riscv' prefixes. */
417 static struct cmd_list_element
*setdebugriscvcmdlist
= NULL
;
418 static struct cmd_list_element
*showdebugriscvcmdlist
= NULL
;
420 /* The show callback for all 'show debug riscv VARNAME' variables. */
423 show_riscv_debug_variable (struct ui_file
*file
, int from_tty
,
424 struct cmd_list_element
*c
,
427 fprintf_filtered (file
,
428 _("RiscV debug variable `%s' is set to: %s\n"),
432 /* When this is set to non-zero debugging information about breakpoint
433 kinds will be printed. */
435 static unsigned int riscv_debug_breakpoints
= 0;
437 /* When this is set to non-zero debugging information about inferior calls
440 static unsigned int riscv_debug_infcall
= 0;
442 /* When this is set to non-zero debugging information about stack unwinding
445 static unsigned int riscv_debug_unwinder
= 0;
447 /* When this is set to non-zero debugging information about gdbarch
448 initialisation will be printed. */
450 static unsigned int riscv_debug_gdbarch
= 0;
452 /* See riscv-tdep.h. */
455 riscv_isa_xlen (struct gdbarch
*gdbarch
)
457 return gdbarch_tdep (gdbarch
)->isa_features
.xlen
;
460 /* See riscv-tdep.h. */
463 riscv_abi_xlen (struct gdbarch
*gdbarch
)
465 return gdbarch_tdep (gdbarch
)->abi_features
.xlen
;
468 /* See riscv-tdep.h. */
471 riscv_isa_flen (struct gdbarch
*gdbarch
)
473 return gdbarch_tdep (gdbarch
)->isa_features
.flen
;
476 /* See riscv-tdep.h. */
479 riscv_abi_flen (struct gdbarch
*gdbarch
)
481 return gdbarch_tdep (gdbarch
)->abi_features
.flen
;
484 /* Return true if the target for GDBARCH has floating point hardware. */
487 riscv_has_fp_regs (struct gdbarch
*gdbarch
)
489 return (riscv_isa_flen (gdbarch
) > 0);
492 /* Return true if GDBARCH is using any of the floating point hardware ABIs. */
495 riscv_has_fp_abi (struct gdbarch
*gdbarch
)
497 return gdbarch_tdep (gdbarch
)->abi_features
.flen
> 0;
500 /* Return true if REGNO is a floating pointer register. */
503 riscv_is_fp_regno_p (int regno
)
505 return (regno
>= RISCV_FIRST_FP_REGNUM
506 && regno
<= RISCV_LAST_FP_REGNUM
);
509 /* Implement the breakpoint_kind_from_pc gdbarch method. */
512 riscv_breakpoint_kind_from_pc (struct gdbarch
*gdbarch
, CORE_ADDR
*pcptr
)
514 if (use_compressed_breakpoints
== AUTO_BOOLEAN_AUTO
)
516 bool unaligned_p
= false;
519 /* Some targets don't support unaligned reads. The address can only
520 be unaligned if the C extension is supported. So it is safe to
521 use a compressed breakpoint in this case. */
526 /* Read the opcode byte to determine the instruction length. If
527 the read fails this may be because we tried to set the
528 breakpoint at an invalid address, in this case we provide a
529 fake result which will give a breakpoint length of 4.
530 Hopefully when we try to actually insert the breakpoint we
531 will see a failure then too which will be reported to the
533 if (target_read_code (*pcptr
, buf
, 1) == -1)
535 read_code (*pcptr
, buf
, 1);
538 if (riscv_debug_breakpoints
)
540 const char *bp
= (unaligned_p
|| riscv_insn_length (buf
[0]) == 2
541 ? "C.EBREAK" : "EBREAK");
543 fprintf_unfiltered (gdb_stdlog
, "Using %s for breakpoint at %s ",
544 bp
, paddress (gdbarch
, *pcptr
));
546 fprintf_unfiltered (gdb_stdlog
, "(unaligned address)\n");
548 fprintf_unfiltered (gdb_stdlog
, "(instruction length %d)\n",
549 riscv_insn_length (buf
[0]));
551 if (unaligned_p
|| riscv_insn_length (buf
[0]) == 2)
556 else if (use_compressed_breakpoints
== AUTO_BOOLEAN_TRUE
)
562 /* Implement the sw_breakpoint_from_kind gdbarch method. */
564 static const gdb_byte
*
565 riscv_sw_breakpoint_from_kind (struct gdbarch
*gdbarch
, int kind
, int *size
)
567 static const gdb_byte ebreak
[] = { 0x73, 0x00, 0x10, 0x00, };
568 static const gdb_byte c_ebreak
[] = { 0x02, 0x90 };
578 gdb_assert_not_reached (_("unhandled breakpoint kind"));
582 /* Implement the register_name gdbarch method. This is used instead of
583 the function supplied by calling TDESC_USE_REGISTERS so that we can
584 ensure the preferred names are offered for x-regs and f-regs. */
587 riscv_register_name (struct gdbarch
*gdbarch
, int regnum
)
589 /* Lookup the name through the target description. If we get back NULL
590 then this is an unknown register. If we do get a name back then we
591 look up the registers preferred name below. */
592 const char *name
= tdesc_register_name (gdbarch
, regnum
);
593 if (name
== NULL
|| name
[0] == '\0')
596 /* We want GDB to use the ABI names for registers even if the target
597 gives us a target description with the architectural name. For
598 example we want to see 'ra' instead of 'x1' whatever the target
599 description called it. */
600 if (regnum
>= RISCV_ZERO_REGNUM
&& regnum
< RISCV_FIRST_FP_REGNUM
)
602 gdb_assert (regnum
< riscv_xreg_feature
.registers
.size ());
603 return riscv_xreg_feature
.registers
[regnum
].names
[0];
606 /* Like with the x-regs we prefer the abi names for the floating point
608 if (regnum
>= RISCV_FIRST_FP_REGNUM
&& regnum
<= RISCV_LAST_FP_REGNUM
)
610 if (riscv_has_fp_regs (gdbarch
))
612 regnum
-= RISCV_FIRST_FP_REGNUM
;
613 gdb_assert (regnum
< riscv_freg_feature
.registers
.size ());
614 return riscv_freg_feature
.registers
[regnum
].names
[0];
620 /* Some targets (QEMU) are reporting these three registers twice, once
621 in the FPU feature, and once in the CSR feature. Both of these read
622 the same underlying state inside the target, but naming the register
623 twice in the target description results in GDB having two registers
624 with the same name, only one of which can ever be accessed, but both
625 will show up in 'info register all'. Unless, we identify the
626 duplicate copies of these registers (in riscv_tdesc_unknown_reg) and
627 then hide the registers here by giving them no name. */
628 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
629 if (tdep
->duplicate_fflags_regnum
== regnum
)
631 if (tdep
->duplicate_frm_regnum
== regnum
)
633 if (tdep
->duplicate_fcsr_regnum
== regnum
)
636 /* The remaining registers are different. For all other registers on the
637 machine we prefer to see the names that the target description
638 provides. This is particularly important for CSRs which might be
639 renamed over time. If GDB keeps track of the "latest" name, but a
640 particular target provides an older name then we don't want to force
641 users to see the newer name in register output.
643 The other case that reaches here are any registers that the target
644 provided that GDB is completely unaware of. For these we have no
645 choice but to accept the target description name.
647 Just accept whatever name TDESC_REGISTER_NAME returned. */
651 /* Construct a type for 64-bit FP registers. */
654 riscv_fpreg_d_type (struct gdbarch
*gdbarch
)
656 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
658 if (tdep
->riscv_fpreg_d_type
== nullptr)
660 const struct builtin_type
*bt
= builtin_type (gdbarch
);
662 /* The type we're building is this: */
664 union __gdb_builtin_type_fpreg_d
673 t
= arch_composite_type (gdbarch
,
674 "__gdb_builtin_type_fpreg_d", TYPE_CODE_UNION
);
675 append_composite_type_field (t
, "float", bt
->builtin_float
);
676 append_composite_type_field (t
, "double", bt
->builtin_double
);
677 t
->set_is_vector (true);
678 t
->set_name ("builtin_type_fpreg_d");
679 tdep
->riscv_fpreg_d_type
= t
;
682 return tdep
->riscv_fpreg_d_type
;
685 /* Implement the register_type gdbarch method. This is installed as an
686 for the override setup by TDESC_USE_REGISTERS, for most registers we
687 delegate the type choice to the target description, but for a few
688 registers we try to improve the types if the target description has
689 taken a simplistic approach. */
692 riscv_register_type (struct gdbarch
*gdbarch
, int regnum
)
694 struct type
*type
= tdesc_register_type (gdbarch
, regnum
);
695 int xlen
= riscv_isa_xlen (gdbarch
);
697 /* We want to perform some specific type "fixes" in cases where we feel
698 that we really can do better than the target description. For all
699 other cases we just return what the target description says. */
700 if (riscv_is_fp_regno_p (regnum
))
702 /* This spots the case for RV64 where the double is defined as
703 either 'ieee_double' or 'float' (which is the generic name that
704 converts to 'double' on 64-bit). In these cases its better to
705 present the registers using a union type. */
706 int flen
= riscv_isa_flen (gdbarch
);
708 && type
->code () == TYPE_CODE_FLT
709 && TYPE_LENGTH (type
) == flen
710 && (strcmp (type
->name (), "builtin_type_ieee_double") == 0
711 || strcmp (type
->name (), "double") == 0))
712 type
= riscv_fpreg_d_type (gdbarch
);
715 if ((regnum
== gdbarch_pc_regnum (gdbarch
)
716 || regnum
== RISCV_RA_REGNUM
717 || regnum
== RISCV_FP_REGNUM
718 || regnum
== RISCV_SP_REGNUM
719 || regnum
== RISCV_GP_REGNUM
720 || regnum
== RISCV_TP_REGNUM
)
721 && type
->code () == TYPE_CODE_INT
722 && TYPE_LENGTH (type
) == xlen
)
724 /* This spots the case where some interesting registers are defined
725 as simple integers of the expected size, we force these registers
726 to be pointers as we believe that is more useful. */
727 if (regnum
== gdbarch_pc_regnum (gdbarch
)
728 || regnum
== RISCV_RA_REGNUM
)
729 type
= builtin_type (gdbarch
)->builtin_func_ptr
;
730 else if (regnum
== RISCV_FP_REGNUM
731 || regnum
== RISCV_SP_REGNUM
732 || regnum
== RISCV_GP_REGNUM
733 || regnum
== RISCV_TP_REGNUM
)
734 type
= builtin_type (gdbarch
)->builtin_data_ptr
;
740 /* Helper for riscv_print_registers_info, prints info for a single register
744 riscv_print_one_register_info (struct gdbarch
*gdbarch
,
745 struct ui_file
*file
,
746 struct frame_info
*frame
,
749 const char *name
= gdbarch_register_name (gdbarch
, regnum
);
751 struct type
*regtype
;
752 int print_raw_format
;
753 enum tab_stops
{ value_column_1
= 15 };
755 fputs_filtered (name
, file
);
756 print_spaces_filtered (value_column_1
- strlen (name
), file
);
760 val
= value_of_register (regnum
, frame
);
761 regtype
= value_type (val
);
763 catch (const gdb_exception_error
&ex
)
765 /* Handle failure to read a register without interrupting the entire
766 'info registers' flow. */
767 fprintf_filtered (file
, "%s\n", ex
.what ());
771 print_raw_format
= (value_entirely_available (val
)
772 && !value_optimized_out (val
));
774 if (regtype
->code () == TYPE_CODE_FLT
775 || (regtype
->code () == TYPE_CODE_UNION
776 && regtype
->num_fields () == 2
777 && regtype
->field (0).type ()->code () == TYPE_CODE_FLT
778 && regtype
->field (1).type ()->code () == TYPE_CODE_FLT
)
779 || (regtype
->code () == TYPE_CODE_UNION
780 && regtype
->num_fields () == 3
781 && regtype
->field (0).type ()->code () == TYPE_CODE_FLT
782 && regtype
->field (1).type ()->code () == TYPE_CODE_FLT
783 && regtype
->field (2).type ()->code () == TYPE_CODE_FLT
))
785 struct value_print_options opts
;
786 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
787 enum bfd_endian byte_order
= type_byte_order (regtype
);
789 get_user_print_options (&opts
);
792 common_val_print (val
, file
, 0, &opts
, current_language
);
794 if (print_raw_format
)
796 fprintf_filtered (file
, "\t(raw ");
797 print_hex_chars (file
, valaddr
, TYPE_LENGTH (regtype
), byte_order
,
799 fprintf_filtered (file
, ")");
804 struct value_print_options opts
;
806 /* Print the register in hex. */
807 get_formatted_print_options (&opts
, 'x');
809 common_val_print (val
, file
, 0, &opts
, current_language
);
811 if (print_raw_format
)
813 if (regnum
== RISCV_CSR_MSTATUS_REGNUM
)
816 int size
= register_size (gdbarch
, regnum
);
819 /* The SD field is always in the upper bit of MSTATUS, regardless
820 of the number of bits in MSTATUS. */
821 d
= value_as_long (val
);
823 fprintf_filtered (file
,
824 "\tSD:%X VM:%02X MXR:%X PUM:%X MPRV:%X XS:%X "
825 "FS:%X MPP:%x HPP:%X SPP:%X MPIE:%X HPIE:%X "
826 "SPIE:%X UPIE:%X MIE:%X HIE:%X SIE:%X UIE:%X",
827 (int) ((d
>> (xlen
- 1)) & 0x1),
828 (int) ((d
>> 24) & 0x1f),
829 (int) ((d
>> 19) & 0x1),
830 (int) ((d
>> 18) & 0x1),
831 (int) ((d
>> 17) & 0x1),
832 (int) ((d
>> 15) & 0x3),
833 (int) ((d
>> 13) & 0x3),
834 (int) ((d
>> 11) & 0x3),
835 (int) ((d
>> 9) & 0x3),
836 (int) ((d
>> 8) & 0x1),
837 (int) ((d
>> 7) & 0x1),
838 (int) ((d
>> 6) & 0x1),
839 (int) ((d
>> 5) & 0x1),
840 (int) ((d
>> 4) & 0x1),
841 (int) ((d
>> 3) & 0x1),
842 (int) ((d
>> 2) & 0x1),
843 (int) ((d
>> 1) & 0x1),
844 (int) ((d
>> 0) & 0x1));
846 else if (regnum
== RISCV_CSR_MISA_REGNUM
)
851 int size
= register_size (gdbarch
, regnum
);
853 /* The MXL field is always in the upper two bits of MISA,
854 regardless of the number of bits in MISA. Mask out other
855 bits to ensure we have a positive value. */
856 d
= value_as_long (val
);
857 base
= (d
>> ((size
* 8) - 2)) & 0x3;
860 for (; base
> 0; base
--)
862 fprintf_filtered (file
, "\tRV%d", xlen
);
864 for (i
= 0; i
< 26; i
++)
867 fprintf_filtered (file
, "%c", 'A' + i
);
870 else if (regnum
== RISCV_CSR_FCSR_REGNUM
871 || regnum
== RISCV_CSR_FFLAGS_REGNUM
872 || regnum
== RISCV_CSR_FRM_REGNUM
)
876 d
= value_as_long (val
);
878 fprintf_filtered (file
, "\t");
879 if (regnum
!= RISCV_CSR_FRM_REGNUM
)
880 fprintf_filtered (file
,
881 "RD:%01X NV:%d DZ:%d OF:%d UF:%d NX:%d",
882 (int) ((d
>> 5) & 0x7),
883 (int) ((d
>> 4) & 0x1),
884 (int) ((d
>> 3) & 0x1),
885 (int) ((d
>> 2) & 0x1),
886 (int) ((d
>> 1) & 0x1),
887 (int) ((d
>> 0) & 0x1));
889 if (regnum
!= RISCV_CSR_FFLAGS_REGNUM
)
891 static const char * const sfrm
[] =
893 "RNE (round to nearest; ties to even)",
894 "RTZ (Round towards zero)",
895 "RDN (Round down towards -INF)",
896 "RUP (Round up towards +INF)",
897 "RMM (Round to nearest; ties to max magnitude)",
900 "dynamic rounding mode",
902 int frm
= ((regnum
== RISCV_CSR_FCSR_REGNUM
)
903 ? (d
>> 5) : d
) & 0x3;
905 fprintf_filtered (file
, "%sFRM:%i [%s]",
906 (regnum
== RISCV_CSR_FCSR_REGNUM
911 else if (regnum
== RISCV_PRIV_REGNUM
)
916 d
= value_as_long (val
);
921 static const char * const sprv
[] =
928 fprintf_filtered (file
, "\tprv:%d [%s]",
932 fprintf_filtered (file
, "\tprv:%d [INVALID]", priv
);
936 /* If not a vector register, print it also according to its
938 if (regtype
->is_vector () == 0)
940 get_user_print_options (&opts
);
942 fprintf_filtered (file
, "\t");
943 common_val_print (val
, file
, 0, &opts
, current_language
);
948 fprintf_filtered (file
, "\n");
951 /* Return true if REGNUM is a valid CSR register. The CSR register space
952 is sparsely populated, so not every number is a named CSR. */
955 riscv_is_regnum_a_named_csr (int regnum
)
957 gdb_assert (regnum
>= RISCV_FIRST_CSR_REGNUM
958 && regnum
<= RISCV_LAST_CSR_REGNUM
);
962 #define DECLARE_CSR(name, num, class, define_ver, abort_ver) case RISCV_ ## num ## _REGNUM:
963 #include "opcode/riscv-opc.h"
972 /* Implement the register_reggroup_p gdbarch method. Is REGNUM a member
976 riscv_register_reggroup_p (struct gdbarch
*gdbarch
, int regnum
,
977 struct reggroup
*reggroup
)
979 /* Used by 'info registers' and 'info registers <groupname>'. */
981 if (gdbarch_register_name (gdbarch
, regnum
) == NULL
982 || gdbarch_register_name (gdbarch
, regnum
)[0] == '\0')
985 if (regnum
> RISCV_LAST_REGNUM
)
987 /* Any extra registers from the CSR tdesc_feature (identified in
988 riscv_tdesc_unknown_reg) are removed from the save/restore groups
989 as some targets (QEMU) report CSRs which then can't be read. */
990 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
991 if ((reggroup
== restore_reggroup
|| reggroup
== save_reggroup
)
992 && regnum
>= tdep
->unknown_csrs_first_regnum
993 && regnum
< (tdep
->unknown_csrs_first_regnum
994 + tdep
->unknown_csrs_count
))
997 /* This is some other unknown register from the target description.
998 In this case we trust whatever the target description says about
999 which groups this register should be in. */
1000 int ret
= tdesc_register_in_reggroup_p (gdbarch
, regnum
, reggroup
);
1004 return default_register_reggroup_p (gdbarch
, regnum
, reggroup
);
1007 if (reggroup
== all_reggroup
)
1009 if (regnum
< RISCV_FIRST_CSR_REGNUM
|| regnum
== RISCV_PRIV_REGNUM
)
1011 if (riscv_is_regnum_a_named_csr (regnum
))
1015 else if (reggroup
== float_reggroup
)
1016 return (riscv_is_fp_regno_p (regnum
)
1017 || regnum
== RISCV_CSR_FCSR_REGNUM
1018 || regnum
== RISCV_CSR_FFLAGS_REGNUM
1019 || regnum
== RISCV_CSR_FRM_REGNUM
);
1020 else if (reggroup
== general_reggroup
)
1021 return regnum
< RISCV_FIRST_FP_REGNUM
;
1022 else if (reggroup
== restore_reggroup
|| reggroup
== save_reggroup
)
1024 if (riscv_has_fp_regs (gdbarch
))
1025 return (regnum
<= RISCV_LAST_FP_REGNUM
1026 || regnum
== RISCV_CSR_FCSR_REGNUM
1027 || regnum
== RISCV_CSR_FFLAGS_REGNUM
1028 || regnum
== RISCV_CSR_FRM_REGNUM
);
1030 return regnum
< RISCV_FIRST_FP_REGNUM
;
1032 else if (reggroup
== system_reggroup
|| reggroup
== csr_reggroup
)
1034 if (regnum
== RISCV_PRIV_REGNUM
)
1036 if (regnum
< RISCV_FIRST_CSR_REGNUM
|| regnum
> RISCV_LAST_CSR_REGNUM
)
1038 if (riscv_is_regnum_a_named_csr (regnum
))
1042 else if (reggroup
== vector_reggroup
)
1048 /* Implement the print_registers_info gdbarch method. This is used by
1049 'info registers' and 'info all-registers'. */
1052 riscv_print_registers_info (struct gdbarch
*gdbarch
,
1053 struct ui_file
*file
,
1054 struct frame_info
*frame
,
1055 int regnum
, int print_all
)
1059 /* Print one specified register. */
1060 if (gdbarch_register_name (gdbarch
, regnum
) == NULL
1061 || *(gdbarch_register_name (gdbarch
, regnum
)) == '\0')
1062 error (_("Not a valid register for the current processor type"));
1063 riscv_print_one_register_info (gdbarch
, file
, frame
, regnum
);
1067 struct reggroup
*reggroup
;
1070 reggroup
= all_reggroup
;
1072 reggroup
= general_reggroup
;
1074 for (regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); ++regnum
)
1076 /* Zero never changes, so might as well hide by default. */
1077 if (regnum
== RISCV_ZERO_REGNUM
&& !print_all
)
1080 /* Registers with no name are not valid on this ISA. */
1081 if (gdbarch_register_name (gdbarch
, regnum
) == NULL
1082 || *(gdbarch_register_name (gdbarch
, regnum
)) == '\0')
1085 /* Is the register in the group we're interested in? */
1086 if (!gdbarch_register_reggroup_p (gdbarch
, regnum
, reggroup
))
1089 riscv_print_one_register_info (gdbarch
, file
, frame
, regnum
);
1094 /* Class that handles one decoded RiscV instruction. */
1100 /* Enum of all the opcodes that GDB cares about during the prologue scan. */
1103 /* Unknown value is used at initialisation time. */
1106 /* These instructions are all the ones we are interested in during the
1116 /* These are needed for software breakpoint support. */
1125 /* These are needed for stepping over atomic sequences. */
1129 /* Other instructions are not interesting during the prologue scan, and
1144 void decode (struct gdbarch
*gdbarch
, CORE_ADDR pc
);
1146 /* Get the length of the instruction in bytes. */
1148 { return m_length
; }
1150 /* Get the opcode for this instruction. */
1151 enum opcode
opcode () const
1152 { return m_opcode
; }
1154 /* Get destination register field for this instruction. This is only
1155 valid if the OPCODE implies there is such a field for this
1160 /* Get the RS1 register field for this instruction. This is only valid
1161 if the OPCODE implies there is such a field for this instruction. */
1165 /* Get the RS2 register field for this instruction. This is only valid
1166 if the OPCODE implies there is such a field for this instruction. */
1170 /* Get the immediate for this instruction in signed form. This is only
1171 valid if the OPCODE implies there is such a field for this
1173 int imm_signed () const
1178 /* Extract 5 bit register field at OFFSET from instruction OPCODE. */
1179 int decode_register_index (unsigned long opcode
, int offset
)
1181 return (opcode
>> offset
) & 0x1F;
1184 /* Extract 5 bit register field at OFFSET from instruction OPCODE. */
1185 int decode_register_index_short (unsigned long opcode
, int offset
)
1187 return ((opcode
>> offset
) & 0x7) + 8;
1190 /* Helper for DECODE, decode 32-bit R-type instruction. */
1191 void decode_r_type_insn (enum opcode opcode
, ULONGEST ival
)
1194 m_rd
= decode_register_index (ival
, OP_SH_RD
);
1195 m_rs1
= decode_register_index (ival
, OP_SH_RS1
);
1196 m_rs2
= decode_register_index (ival
, OP_SH_RS2
);
1199 /* Helper for DECODE, decode 16-bit compressed R-type instruction. */
1200 void decode_cr_type_insn (enum opcode opcode
, ULONGEST ival
)
1203 m_rd
= m_rs1
= decode_register_index (ival
, OP_SH_CRS1S
);
1204 m_rs2
= decode_register_index (ival
, OP_SH_CRS2
);
1207 /* Helper for DECODE, decode 32-bit I-type instruction. */
1208 void decode_i_type_insn (enum opcode opcode
, ULONGEST ival
)
1211 m_rd
= decode_register_index (ival
, OP_SH_RD
);
1212 m_rs1
= decode_register_index (ival
, OP_SH_RS1
);
1213 m_imm
.s
= EXTRACT_ITYPE_IMM (ival
);
1216 /* Helper for DECODE, decode 16-bit compressed I-type instruction. */
1217 void decode_ci_type_insn (enum opcode opcode
, ULONGEST ival
)
1220 m_rd
= m_rs1
= decode_register_index (ival
, OP_SH_CRS1S
);
1221 m_imm
.s
= EXTRACT_RVC_IMM (ival
);
1224 /* Helper for DECODE, decode 32-bit S-type instruction. */
1225 void decode_s_type_insn (enum opcode opcode
, ULONGEST ival
)
1228 m_rs1
= decode_register_index (ival
, OP_SH_RS1
);
1229 m_rs2
= decode_register_index (ival
, OP_SH_RS2
);
1230 m_imm
.s
= EXTRACT_STYPE_IMM (ival
);
1233 /* Helper for DECODE, decode 16-bit CS-type instruction. The immediate
1234 encoding is different for each CS format instruction, so extracting
1235 the immediate is left up to the caller, who should pass the extracted
1236 immediate value through in IMM. */
1237 void decode_cs_type_insn (enum opcode opcode
, ULONGEST ival
, int imm
)
1241 m_rs1
= decode_register_index_short (ival
, OP_SH_CRS1S
);
1242 m_rs2
= decode_register_index_short (ival
, OP_SH_CRS2S
);
1245 /* Helper for DECODE, decode 16-bit CSS-type instruction. The immediate
1246 encoding is different for each CSS format instruction, so extracting
1247 the immediate is left up to the caller, who should pass the extracted
1248 immediate value through in IMM. */
1249 void decode_css_type_insn (enum opcode opcode
, ULONGEST ival
, int imm
)
1253 m_rs1
= RISCV_SP_REGNUM
;
1254 /* Not a compressed register number in this case. */
1255 m_rs2
= decode_register_index (ival
, OP_SH_CRS2
);
1258 /* Helper for DECODE, decode 32-bit U-type instruction. */
1259 void decode_u_type_insn (enum opcode opcode
, ULONGEST ival
)
1262 m_rd
= decode_register_index (ival
, OP_SH_RD
);
1263 m_imm
.s
= EXTRACT_UTYPE_IMM (ival
);
1266 /* Helper for DECODE, decode 32-bit J-type instruction. */
1267 void decode_j_type_insn (enum opcode opcode
, ULONGEST ival
)
1270 m_rd
= decode_register_index (ival
, OP_SH_RD
);
1271 m_imm
.s
= EXTRACT_UJTYPE_IMM (ival
);
1274 /* Helper for DECODE, decode 32-bit J-type instruction. */
1275 void decode_cj_type_insn (enum opcode opcode
, ULONGEST ival
)
1278 m_imm
.s
= EXTRACT_RVC_J_IMM (ival
);
1281 void decode_b_type_insn (enum opcode opcode
, ULONGEST ival
)
1284 m_rs1
= decode_register_index (ival
, OP_SH_RS1
);
1285 m_rs2
= decode_register_index (ival
, OP_SH_RS2
);
1286 m_imm
.s
= EXTRACT_SBTYPE_IMM (ival
);
1289 void decode_cb_type_insn (enum opcode opcode
, ULONGEST ival
)
1292 m_rs1
= decode_register_index_short (ival
, OP_SH_CRS1S
);
1293 m_imm
.s
= EXTRACT_RVC_B_IMM (ival
);
1296 /* Fetch instruction from target memory at ADDR, return the content of
1297 the instruction, and update LEN with the instruction length. */
1298 static ULONGEST
fetch_instruction (struct gdbarch
*gdbarch
,
1299 CORE_ADDR addr
, int *len
);
1301 /* The length of the instruction in bytes. Should be 2 or 4. */
1304 /* The instruction opcode. */
1305 enum opcode m_opcode
;
1307 /* The three possible registers an instruction might reference. Not
1308 every instruction fills in all of these registers. Which fields are
1309 valid depends on the opcode. The naming of these fields matches the
1310 naming in the riscv isa manual. */
1315 /* Possible instruction immediate. This is only valid if the instruction
1316 format contains an immediate, not all instruction, whether this is
1317 valid depends on the opcode. Despite only having one format for now
1318 the immediate is packed into a union, later instructions might require
1319 an unsigned formatted immediate, having the union in place now will
1320 reduce the need for code churn later. */
1321 union riscv_insn_immediate
1323 riscv_insn_immediate ()
1333 /* Fetch instruction from target memory at ADDR, return the content of the
1334 instruction, and update LEN with the instruction length. */
1337 riscv_insn::fetch_instruction (struct gdbarch
*gdbarch
,
1338 CORE_ADDR addr
, int *len
)
1340 enum bfd_endian byte_order
= gdbarch_byte_order_for_code (gdbarch
);
1342 int instlen
, status
;
1344 /* All insns are at least 16 bits. */
1345 status
= target_read_memory (addr
, buf
, 2);
1347 memory_error (TARGET_XFER_E_IO
, addr
);
1349 /* If we need more, grab it now. */
1350 instlen
= riscv_insn_length (buf
[0]);
1351 gdb_assert (instlen
<= sizeof (buf
));
1356 status
= target_read_memory (addr
+ 2, buf
+ 2, instlen
- 2);
1358 memory_error (TARGET_XFER_E_IO
, addr
+ 2);
1361 return extract_unsigned_integer (buf
, instlen
, byte_order
);
1364 /* Fetch from target memory an instruction at PC and decode it. This can
1365 throw an error if the memory access fails, callers are responsible for
1366 handling this error if that is appropriate. */
1369 riscv_insn::decode (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
1373 /* Fetch the instruction, and the instructions length. */
1374 ival
= fetch_instruction (gdbarch
, pc
, &m_length
);
1378 if (is_add_insn (ival
))
1379 decode_r_type_insn (ADD
, ival
);
1380 else if (is_addw_insn (ival
))
1381 decode_r_type_insn (ADDW
, ival
);
1382 else if (is_addi_insn (ival
))
1383 decode_i_type_insn (ADDI
, ival
);
1384 else if (is_addiw_insn (ival
))
1385 decode_i_type_insn (ADDIW
, ival
);
1386 else if (is_auipc_insn (ival
))
1387 decode_u_type_insn (AUIPC
, ival
);
1388 else if (is_lui_insn (ival
))
1389 decode_u_type_insn (LUI
, ival
);
1390 else if (is_sd_insn (ival
))
1391 decode_s_type_insn (SD
, ival
);
1392 else if (is_sw_insn (ival
))
1393 decode_s_type_insn (SW
, ival
);
1394 else if (is_jal_insn (ival
))
1395 decode_j_type_insn (JAL
, ival
);
1396 else if (is_jalr_insn (ival
))
1397 decode_i_type_insn (JALR
, ival
);
1398 else if (is_beq_insn (ival
))
1399 decode_b_type_insn (BEQ
, ival
);
1400 else if (is_bne_insn (ival
))
1401 decode_b_type_insn (BNE
, ival
);
1402 else if (is_blt_insn (ival
))
1403 decode_b_type_insn (BLT
, ival
);
1404 else if (is_bge_insn (ival
))
1405 decode_b_type_insn (BGE
, ival
);
1406 else if (is_bltu_insn (ival
))
1407 decode_b_type_insn (BLTU
, ival
);
1408 else if (is_bgeu_insn (ival
))
1409 decode_b_type_insn (BGEU
, ival
);
1410 else if (is_lr_w_insn (ival
))
1411 decode_r_type_insn (LR
, ival
);
1412 else if (is_lr_d_insn (ival
))
1413 decode_r_type_insn (LR
, ival
);
1414 else if (is_sc_w_insn (ival
))
1415 decode_r_type_insn (SC
, ival
);
1416 else if (is_sc_d_insn (ival
))
1417 decode_r_type_insn (SC
, ival
);
1419 /* None of the other fields are valid in this case. */
1422 else if (m_length
== 2)
1424 int xlen
= riscv_isa_xlen (gdbarch
);
1426 /* C_ADD and C_JALR have the same opcode. If RS2 is 0, then this is a
1427 C_JALR. So must try to match C_JALR first as it has more bits in
1429 if (is_c_jalr_insn (ival
))
1430 decode_cr_type_insn (JALR
, ival
);
1431 else if (is_c_add_insn (ival
))
1432 decode_cr_type_insn (ADD
, ival
);
1433 /* C_ADDW is RV64 and RV128 only. */
1434 else if (xlen
!= 4 && is_c_addw_insn (ival
))
1435 decode_cr_type_insn (ADDW
, ival
);
1436 else if (is_c_addi_insn (ival
))
1437 decode_ci_type_insn (ADDI
, ival
);
1438 /* C_ADDIW and C_JAL have the same opcode. C_ADDIW is RV64 and RV128
1439 only and C_JAL is RV32 only. */
1440 else if (xlen
!= 4 && is_c_addiw_insn (ival
))
1441 decode_ci_type_insn (ADDIW
, ival
);
1442 else if (xlen
== 4 && is_c_jal_insn (ival
))
1443 decode_cj_type_insn (JAL
, ival
);
1444 /* C_ADDI16SP and C_LUI have the same opcode. If RD is 2, then this is a
1445 C_ADDI16SP. So must try to match C_ADDI16SP first as it has more bits
1447 else if (is_c_addi16sp_insn (ival
))
1450 m_rd
= m_rs1
= decode_register_index (ival
, OP_SH_RD
);
1451 m_imm
.s
= EXTRACT_RVC_ADDI16SP_IMM (ival
);
1453 else if (is_c_addi4spn_insn (ival
))
1456 m_rd
= decode_register_index_short (ival
, OP_SH_CRS2S
);
1457 m_rs1
= RISCV_SP_REGNUM
;
1458 m_imm
.s
= EXTRACT_RVC_ADDI4SPN_IMM (ival
);
1460 else if (is_c_lui_insn (ival
))
1463 m_rd
= decode_register_index (ival
, OP_SH_CRS1S
);
1464 m_imm
.s
= EXTRACT_RVC_LUI_IMM (ival
);
1466 /* C_SD and C_FSW have the same opcode. C_SD is RV64 and RV128 only,
1467 and C_FSW is RV32 only. */
1468 else if (xlen
!= 4 && is_c_sd_insn (ival
))
1469 decode_cs_type_insn (SD
, ival
, EXTRACT_RVC_LD_IMM (ival
));
1470 else if (is_c_sw_insn (ival
))
1471 decode_cs_type_insn (SW
, ival
, EXTRACT_RVC_LW_IMM (ival
));
1472 else if (is_c_swsp_insn (ival
))
1473 decode_css_type_insn (SW
, ival
, EXTRACT_RVC_SWSP_IMM (ival
));
1474 else if (xlen
!= 4 && is_c_sdsp_insn (ival
))
1475 decode_css_type_insn (SD
, ival
, EXTRACT_RVC_SDSP_IMM (ival
));
1476 /* C_JR and C_MV have the same opcode. If RS2 is 0, then this is a C_JR.
1477 So must try to match C_JR first as it ahs more bits in mask. */
1478 else if (is_c_jr_insn (ival
))
1479 decode_cr_type_insn (JALR
, ival
);
1480 else if (is_c_j_insn (ival
))
1481 decode_cj_type_insn (JAL
, ival
);
1482 else if (is_c_beqz_insn (ival
))
1483 decode_cb_type_insn (BEQ
, ival
);
1484 else if (is_c_bnez_insn (ival
))
1485 decode_cb_type_insn (BNE
, ival
);
1487 /* None of the other fields of INSN are valid in this case. */
1492 /* This must be a 6 or 8 byte instruction, we don't currently decode
1493 any of these, so just ignore it. */
1494 gdb_assert (m_length
== 6 || m_length
== 8);
1499 /* The prologue scanner. This is currently only used for skipping the
1500 prologue of a function when the DWARF information is not sufficient.
1501 However, it is written with filling of the frame cache in mind, which
1502 is why different groups of stack setup instructions are split apart
1503 during the core of the inner loop. In the future, the intention is to
1504 extend this function to fully support building up a frame cache that
1505 can unwind register values when there is no DWARF information. */
1508 riscv_scan_prologue (struct gdbarch
*gdbarch
,
1509 CORE_ADDR start_pc
, CORE_ADDR end_pc
,
1510 struct riscv_unwind_cache
*cache
)
1512 CORE_ADDR cur_pc
, next_pc
, after_prologue_pc
;
1513 CORE_ADDR end_prologue_addr
= 0;
1515 /* Find an upper limit on the function prologue using the debug
1516 information. If the debug information could not be used to provide
1517 that bound, then use an arbitrary large number as the upper bound. */
1518 after_prologue_pc
= skip_prologue_using_sal (gdbarch
, start_pc
);
1519 if (after_prologue_pc
== 0)
1520 after_prologue_pc
= start_pc
+ 100; /* Arbitrary large number. */
1521 if (after_prologue_pc
< end_pc
)
1522 end_pc
= after_prologue_pc
;
1524 pv_t regs
[RISCV_NUM_INTEGER_REGS
]; /* Number of GPR. */
1525 for (int regno
= 0; regno
< RISCV_NUM_INTEGER_REGS
; regno
++)
1526 regs
[regno
] = pv_register (regno
, 0);
1527 pv_area
stack (RISCV_SP_REGNUM
, gdbarch_addr_bit (gdbarch
));
1529 if (riscv_debug_unwinder
)
1532 "Prologue scan for function starting at %s (limit %s)\n",
1533 core_addr_to_string (start_pc
),
1534 core_addr_to_string (end_pc
));
1536 for (next_pc
= cur_pc
= start_pc
; cur_pc
< end_pc
; cur_pc
= next_pc
)
1538 struct riscv_insn insn
;
1540 /* Decode the current instruction, and decide where the next
1541 instruction lives based on the size of this instruction. */
1542 insn
.decode (gdbarch
, cur_pc
);
1543 gdb_assert (insn
.length () > 0);
1544 next_pc
= cur_pc
+ insn
.length ();
1546 /* Look for common stack adjustment insns. */
1547 if ((insn
.opcode () == riscv_insn::ADDI
1548 || insn
.opcode () == riscv_insn::ADDIW
)
1549 && insn
.rd () == RISCV_SP_REGNUM
1550 && insn
.rs1 () == RISCV_SP_REGNUM
)
1552 /* Handle: addi sp, sp, -i
1553 or: addiw sp, sp, -i */
1554 gdb_assert (insn
.rd () < RISCV_NUM_INTEGER_REGS
);
1555 gdb_assert (insn
.rs1 () < RISCV_NUM_INTEGER_REGS
);
1557 = pv_add_constant (regs
[insn
.rs1 ()], insn
.imm_signed ());
1559 else if ((insn
.opcode () == riscv_insn::SW
1560 || insn
.opcode () == riscv_insn::SD
)
1561 && (insn
.rs1 () == RISCV_SP_REGNUM
1562 || insn
.rs1 () == RISCV_FP_REGNUM
))
1564 /* Handle: sw reg, offset(sp)
1565 or: sd reg, offset(sp)
1566 or: sw reg, offset(s0)
1567 or: sd reg, offset(s0) */
1568 /* Instruction storing a register onto the stack. */
1569 gdb_assert (insn
.rs1 () < RISCV_NUM_INTEGER_REGS
);
1570 gdb_assert (insn
.rs2 () < RISCV_NUM_INTEGER_REGS
);
1571 stack
.store (pv_add_constant (regs
[insn
.rs1 ()], insn
.imm_signed ()),
1572 (insn
.opcode () == riscv_insn::SW
? 4 : 8),
1575 else if (insn
.opcode () == riscv_insn::ADDI
1576 && insn
.rd () == RISCV_FP_REGNUM
1577 && insn
.rs1 () == RISCV_SP_REGNUM
)
1579 /* Handle: addi s0, sp, size */
1580 /* Instructions setting up the frame pointer. */
1581 gdb_assert (insn
.rd () < RISCV_NUM_INTEGER_REGS
);
1582 gdb_assert (insn
.rs1 () < RISCV_NUM_INTEGER_REGS
);
1584 = pv_add_constant (regs
[insn
.rs1 ()], insn
.imm_signed ());
1586 else if ((insn
.opcode () == riscv_insn::ADD
1587 || insn
.opcode () == riscv_insn::ADDW
)
1588 && insn
.rd () == RISCV_FP_REGNUM
1589 && insn
.rs1 () == RISCV_SP_REGNUM
1590 && insn
.rs2 () == RISCV_ZERO_REGNUM
)
1592 /* Handle: add s0, sp, 0
1593 or: addw s0, sp, 0 */
1594 /* Instructions setting up the frame pointer. */
1595 gdb_assert (insn
.rd () < RISCV_NUM_INTEGER_REGS
);
1596 gdb_assert (insn
.rs1 () < RISCV_NUM_INTEGER_REGS
);
1597 regs
[insn
.rd ()] = pv_add_constant (regs
[insn
.rs1 ()], 0);
1599 else if ((insn
.opcode () == riscv_insn::ADDI
1600 && insn
.rd () == RISCV_ZERO_REGNUM
1601 && insn
.rs1 () == RISCV_ZERO_REGNUM
1602 && insn
.imm_signed () == 0))
1604 /* Handle: add x0, x0, 0 (NOP) */
1606 else if (insn
.opcode () == riscv_insn::AUIPC
)
1608 gdb_assert (insn
.rd () < RISCV_NUM_INTEGER_REGS
);
1609 regs
[insn
.rd ()] = pv_constant (cur_pc
+ insn
.imm_signed ());
1611 else if (insn
.opcode () == riscv_insn::LUI
)
1613 /* Handle: lui REG, n
1614 Where REG is not gp register. */
1615 gdb_assert (insn
.rd () < RISCV_NUM_INTEGER_REGS
);
1616 regs
[insn
.rd ()] = pv_constant (insn
.imm_signed ());
1618 else if (insn
.opcode () == riscv_insn::ADDI
)
1620 /* Handle: addi REG1, REG2, IMM */
1621 gdb_assert (insn
.rd () < RISCV_NUM_INTEGER_REGS
);
1622 gdb_assert (insn
.rs1 () < RISCV_NUM_INTEGER_REGS
);
1624 = pv_add_constant (regs
[insn
.rs1 ()], insn
.imm_signed ());
1626 else if (insn
.opcode () == riscv_insn::ADD
)
1628 /* Handle: addi REG1, REG2, IMM */
1629 gdb_assert (insn
.rd () < RISCV_NUM_INTEGER_REGS
);
1630 gdb_assert (insn
.rs1 () < RISCV_NUM_INTEGER_REGS
);
1631 gdb_assert (insn
.rs2 () < RISCV_NUM_INTEGER_REGS
);
1632 regs
[insn
.rd ()] = pv_add (regs
[insn
.rs1 ()], regs
[insn
.rs2 ()]);
1636 end_prologue_addr
= cur_pc
;
1641 if (end_prologue_addr
== 0)
1642 end_prologue_addr
= cur_pc
;
1644 if (riscv_debug_unwinder
)
1645 fprintf_unfiltered (gdb_stdlog
, "End of prologue at %s\n",
1646 core_addr_to_string (end_prologue_addr
));
1650 /* Figure out if it is a frame pointer or just a stack pointer. Also
1651 the offset held in the pv_t is from the original register value to
1652 the current value, which for a grows down stack means a negative
1653 value. The FRAME_BASE_OFFSET is the negation of this, how to get
1654 from the current value to the original value. */
1655 if (pv_is_register (regs
[RISCV_FP_REGNUM
], RISCV_SP_REGNUM
))
1657 cache
->frame_base_reg
= RISCV_FP_REGNUM
;
1658 cache
->frame_base_offset
= -regs
[RISCV_FP_REGNUM
].k
;
1662 cache
->frame_base_reg
= RISCV_SP_REGNUM
;
1663 cache
->frame_base_offset
= -regs
[RISCV_SP_REGNUM
].k
;
1666 /* Assign offset from old SP to all saved registers. As we don't
1667 have the previous value for the frame base register at this
1668 point, we store the offset as the address in the trad_frame, and
1669 then convert this to an actual address later. */
1670 for (int i
= 0; i
<= RISCV_NUM_INTEGER_REGS
; i
++)
1673 if (stack
.find_reg (gdbarch
, i
, &offset
))
1675 if (riscv_debug_unwinder
)
1677 /* Display OFFSET as a signed value, the offsets are from
1678 the frame base address to the registers location on
1679 the stack, with a descending stack this means the
1680 offsets are always negative. */
1681 fprintf_unfiltered (gdb_stdlog
,
1682 "Register $%s at stack offset %s\n",
1683 gdbarch_register_name (gdbarch
, i
),
1684 plongest ((LONGEST
) offset
));
1686 trad_frame_set_addr (cache
->regs
, i
, offset
);
1691 return end_prologue_addr
;
1694 /* Implement the riscv_skip_prologue gdbarch method. */
1697 riscv_skip_prologue (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
1699 CORE_ADDR func_addr
;
1701 /* See if we can determine the end of the prologue via the symbol
1702 table. If so, then return either PC, or the PC after the
1703 prologue, whichever is greater. */
1704 if (find_pc_partial_function (pc
, NULL
, &func_addr
, NULL
))
1706 CORE_ADDR post_prologue_pc
1707 = skip_prologue_using_sal (gdbarch
, func_addr
);
1709 if (post_prologue_pc
!= 0)
1710 return std::max (pc
, post_prologue_pc
);
1713 /* Can't determine prologue from the symbol table, need to examine
1714 instructions. Pass -1 for the end address to indicate the prologue
1715 scanner can scan as far as it needs to find the end of the prologue. */
1716 return riscv_scan_prologue (gdbarch
, pc
, ((CORE_ADDR
) -1), NULL
);
1719 /* Implement the gdbarch push dummy code callback. */
1722 riscv_push_dummy_code (struct gdbarch
*gdbarch
, CORE_ADDR sp
,
1723 CORE_ADDR funaddr
, struct value
**args
, int nargs
,
1724 struct type
*value_type
, CORE_ADDR
*real_pc
,
1725 CORE_ADDR
*bp_addr
, struct regcache
*regcache
)
1727 /* A nop instruction is 'add x0, x0, 0'. */
1728 static const gdb_byte nop_insn
[] = { 0x13, 0x00, 0x00, 0x00 };
1730 /* Allocate space for a breakpoint, and keep the stack correctly
1731 aligned. The space allocated here must be at least big enough to
1732 accommodate the NOP_INSN defined above. */
1737 /* When we insert a breakpoint we select whether to use a compressed
1738 breakpoint or not based on the existing contents of the memory.
1740 If the breakpoint is being placed onto the stack as part of setting up
1741 for an inferior call from GDB, then the existing stack contents may
1742 randomly appear to be a compressed instruction, causing GDB to insert
1743 a compressed breakpoint. If this happens on a target that does not
1744 support compressed instructions then this could cause problems.
1746 To prevent this issue we write an uncompressed nop onto the stack at
1747 the location where the breakpoint will be inserted. In this way we
1748 ensure that we always use an uncompressed breakpoint, which should
1749 work on all targets.
1751 We call TARGET_WRITE_MEMORY here so that if the write fails we don't
1752 throw an exception. Instead we ignore the error and move on. The
1753 assumption is that either GDB will error later when actually trying to
1754 insert a software breakpoint, or GDB will use hardware breakpoints and
1755 there will be no need to write to memory later. */
1756 int status
= target_write_memory (*bp_addr
, nop_insn
, sizeof (nop_insn
));
1758 if (riscv_debug_breakpoints
|| riscv_debug_infcall
)
1759 fprintf_unfiltered (gdb_stdlog
,
1760 "Writing %s-byte nop instruction to %s: %s\n",
1761 plongest (sizeof (nop_insn
)),
1762 paddress (gdbarch
, *bp_addr
),
1763 (status
== 0 ? "success" : "failed"));
1768 /* Implement the gdbarch type alignment method, overrides the generic
1769 alignment algorithm for anything that is RISC-V specific. */
1772 riscv_type_align (gdbarch
*gdbarch
, type
*type
)
1774 type
= check_typedef (type
);
1775 if (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ())
1776 return std::min (TYPE_LENGTH (type
), (ULONGEST
) BIGGEST_ALIGNMENT
);
1778 /* Anything else will be aligned by the generic code. */
1782 /* Holds information about a single argument either being passed to an
1783 inferior function, or returned from an inferior function. This includes
1784 information about the size, type, etc of the argument, and also
1785 information about how the argument will be passed (or returned). */
1787 struct riscv_arg_info
1789 /* Contents of the argument. */
1790 const gdb_byte
*contents
;
1792 /* Length of argument. */
1795 /* Alignment required for an argument of this type. */
1798 /* The type for this argument. */
1801 /* Each argument can have either 1 or 2 locations assigned to it. Each
1802 location describes where part of the argument will be placed. The
1803 second location is valid based on the LOC_TYPE and C_LENGTH fields
1804 of the first location (which is always valid). */
1807 /* What type of location this is. */
1810 /* Argument passed in a register. */
1813 /* Argument passed as an on stack argument. */
1816 /* Argument passed by reference. The second location is always
1817 valid for a BY_REF argument, and describes where the address
1818 of the BY_REF argument should be placed. */
1822 /* Information that depends on the location type. */
1825 /* Which register number to use. */
1828 /* The offset into the stack region. */
1832 /* The length of contents covered by this location. If this is less
1833 than the total length of the argument, then the second location
1834 will be valid, and will describe where the rest of the argument
1838 /* The offset within CONTENTS for this part of the argument. This can
1839 be non-zero even for the first part (the first field of a struct can
1840 have a non-zero offset due to padding). For the second part of the
1841 argument, this might be the C_LENGTH value of the first part,
1842 however, if we are passing a structure in two registers, and there's
1843 is padding between the first and second field, then this offset
1844 might be greater than the length of the first argument part. When
1845 the second argument location is not holding part of the argument
1846 value, but is instead holding the address of a reference argument,
1847 then this offset will be set to 0. */
1851 /* TRUE if this is an unnamed argument. */
1855 /* Information about a set of registers being used for passing arguments as
1856 part of a function call. The register set must be numerically
1857 sequential from NEXT_REGNUM to LAST_REGNUM. The register set can be
1858 disabled from use by setting NEXT_REGNUM greater than LAST_REGNUM. */
1860 struct riscv_arg_reg
1862 riscv_arg_reg (int first
, int last
)
1863 : next_regnum (first
),
1869 /* The GDB register number to use in this set. */
1872 /* The last GDB register number to use in this set. */
1876 /* Arguments can be passed as on stack arguments, or by reference. The
1877 on stack arguments must be in a continuous region starting from $sp,
1878 while the by reference arguments can be anywhere, but we'll put them
1879 on the stack after (at higher address) the on stack arguments.
1881 This might not be the right approach to take. The ABI is clear that
1882 an argument passed by reference can be modified by the callee, which
1883 us placing the argument (temporarily) onto the stack will not achieve
1884 (changes will be lost). There's also the possibility that very large
1885 arguments could overflow the stack.
1887 This struct is used to track offset into these two areas for where
1888 arguments are to be placed. */
1889 struct riscv_memory_offsets
1891 riscv_memory_offsets ()
1898 /* Offset into on stack argument area. */
1901 /* Offset into the pass by reference area. */
1905 /* Holds information about where arguments to a call will be placed. This
1906 is updated as arguments are added onto the call, and can be used to
1907 figure out where the next argument should be placed. */
1909 struct riscv_call_info
1911 riscv_call_info (struct gdbarch
*gdbarch
)
1912 : int_regs (RISCV_A0_REGNUM
, RISCV_A0_REGNUM
+ 7),
1913 float_regs (RISCV_FA0_REGNUM
, RISCV_FA0_REGNUM
+ 7)
1915 xlen
= riscv_abi_xlen (gdbarch
);
1916 flen
= riscv_abi_flen (gdbarch
);
1918 /* Disable use of floating point registers if needed. */
1919 if (!riscv_has_fp_abi (gdbarch
))
1920 float_regs
.next_regnum
= float_regs
.last_regnum
+ 1;
1923 /* Track the memory areas used for holding in-memory arguments to a
1925 struct riscv_memory_offsets memory
;
1927 /* Holds information about the next integer register to use for passing
1929 struct riscv_arg_reg int_regs
;
1931 /* Holds information about the next floating point register to use for
1932 passing an argument. */
1933 struct riscv_arg_reg float_regs
;
1935 /* The XLEN and FLEN are copied in to this structure for convenience, and
1936 are just the results of calling RISCV_ABI_XLEN and RISCV_ABI_FLEN. */
1941 /* Return the number of registers available for use as parameters in the
1942 register set REG. Returned value can be 0 or more. */
1945 riscv_arg_regs_available (struct riscv_arg_reg
*reg
)
1947 if (reg
->next_regnum
> reg
->last_regnum
)
1950 return (reg
->last_regnum
- reg
->next_regnum
+ 1);
1953 /* If there is at least one register available in the register set REG then
1954 the next register from REG is assigned to LOC and the length field of
1955 LOC is updated to LENGTH. The register set REG is updated to indicate
1956 that the assigned register is no longer available and the function
1959 If there are no registers available in REG then the function returns
1960 false, and LOC and REG are unchanged. */
1963 riscv_assign_reg_location (struct riscv_arg_info::location
*loc
,
1964 struct riscv_arg_reg
*reg
,
1965 int length
, int offset
)
1967 if (reg
->next_regnum
<= reg
->last_regnum
)
1969 loc
->loc_type
= riscv_arg_info::location::in_reg
;
1970 loc
->loc_data
.regno
= reg
->next_regnum
;
1972 loc
->c_length
= length
;
1973 loc
->c_offset
= offset
;
1980 /* Assign LOC a location as the next stack parameter, and update MEMORY to
1981 record that an area of stack has been used to hold the parameter
1984 The length field of LOC is updated to LENGTH, the length of the
1985 parameter being stored, and ALIGN is the alignment required by the
1986 parameter, which will affect how memory is allocated out of MEMORY. */
1989 riscv_assign_stack_location (struct riscv_arg_info::location
*loc
,
1990 struct riscv_memory_offsets
*memory
,
1991 int length
, int align
)
1993 loc
->loc_type
= riscv_arg_info::location::on_stack
;
1995 = align_up (memory
->arg_offset
, align
);
1996 loc
->loc_data
.offset
= memory
->arg_offset
;
1997 memory
->arg_offset
+= length
;
1998 loc
->c_length
= length
;
2000 /* Offset is always 0, either we're the first location part, in which
2001 case we're reading content from the start of the argument, or we're
2002 passing the address of a reference argument, so 0. */
2006 /* Update AINFO, which describes an argument that should be passed or
2007 returned using the integer ABI. The argloc fields within AINFO are
2008 updated to describe the location in which the argument will be passed to
2009 a function, or returned from a function.
2011 The CINFO structure contains the ongoing call information, the holds
2012 information such as which argument registers are remaining to be
2013 assigned to parameter, and how much memory has been used by parameters
2016 By examining the state of CINFO a suitable location can be selected,
2017 and assigned to AINFO. */
2020 riscv_call_arg_scalar_int (struct riscv_arg_info
*ainfo
,
2021 struct riscv_call_info
*cinfo
)
2023 if (ainfo
->length
> (2 * cinfo
->xlen
))
2025 /* Argument is going to be passed by reference. */
2026 ainfo
->argloc
[0].loc_type
2027 = riscv_arg_info::location::by_ref
;
2028 cinfo
->memory
.ref_offset
2029 = align_up (cinfo
->memory
.ref_offset
, ainfo
->align
);
2030 ainfo
->argloc
[0].loc_data
.offset
= cinfo
->memory
.ref_offset
;
2031 cinfo
->memory
.ref_offset
+= ainfo
->length
;
2032 ainfo
->argloc
[0].c_length
= ainfo
->length
;
2034 /* The second location for this argument is given over to holding the
2035 address of the by-reference data. Pass 0 for the offset as this
2036 is not part of the actual argument value. */
2037 if (!riscv_assign_reg_location (&ainfo
->argloc
[1],
2040 riscv_assign_stack_location (&ainfo
->argloc
[1],
2041 &cinfo
->memory
, cinfo
->xlen
,
2046 int len
= std::min (ainfo
->length
, cinfo
->xlen
);
2047 int align
= std::max (ainfo
->align
, cinfo
->xlen
);
2049 /* Unnamed arguments in registers that require 2*XLEN alignment are
2050 passed in an aligned register pair. */
2051 if (ainfo
->is_unnamed
&& (align
== cinfo
->xlen
* 2)
2052 && cinfo
->int_regs
.next_regnum
& 1)
2053 cinfo
->int_regs
.next_regnum
++;
2055 if (!riscv_assign_reg_location (&ainfo
->argloc
[0],
2056 &cinfo
->int_regs
, len
, 0))
2057 riscv_assign_stack_location (&ainfo
->argloc
[0],
2058 &cinfo
->memory
, len
, align
);
2060 if (len
< ainfo
->length
)
2062 len
= ainfo
->length
- len
;
2063 if (!riscv_assign_reg_location (&ainfo
->argloc
[1],
2064 &cinfo
->int_regs
, len
,
2066 riscv_assign_stack_location (&ainfo
->argloc
[1],
2067 &cinfo
->memory
, len
, cinfo
->xlen
);
2072 /* Like RISCV_CALL_ARG_SCALAR_INT, except the argument described by AINFO
2073 is being passed with the floating point ABI. */
2076 riscv_call_arg_scalar_float (struct riscv_arg_info
*ainfo
,
2077 struct riscv_call_info
*cinfo
)
2079 if (ainfo
->length
> cinfo
->flen
|| ainfo
->is_unnamed
)
2080 return riscv_call_arg_scalar_int (ainfo
, cinfo
);
2083 if (!riscv_assign_reg_location (&ainfo
->argloc
[0],
2086 return riscv_call_arg_scalar_int (ainfo
, cinfo
);
2090 /* Like RISCV_CALL_ARG_SCALAR_INT, except the argument described by AINFO
2091 is a complex floating point argument, and is therefore handled
2092 differently to other argument types. */
2095 riscv_call_arg_complex_float (struct riscv_arg_info
*ainfo
,
2096 struct riscv_call_info
*cinfo
)
2098 if (ainfo
->length
<= (2 * cinfo
->flen
)
2099 && riscv_arg_regs_available (&cinfo
->float_regs
) >= 2
2100 && !ainfo
->is_unnamed
)
2103 int len
= ainfo
->length
/ 2;
2105 result
= riscv_assign_reg_location (&ainfo
->argloc
[0],
2106 &cinfo
->float_regs
, len
, 0);
2107 gdb_assert (result
);
2109 result
= riscv_assign_reg_location (&ainfo
->argloc
[1],
2110 &cinfo
->float_regs
, len
, len
);
2111 gdb_assert (result
);
2114 return riscv_call_arg_scalar_int (ainfo
, cinfo
);
2117 /* A structure used for holding information about a structure type within
2118 the inferior program. The RiscV ABI has special rules for handling some
2119 structures with a single field or with two fields. The counting of
2120 fields here is done after flattening out all nested structures. */
2122 class riscv_struct_info
2125 riscv_struct_info ()
2126 : m_number_of_fields (0),
2127 m_types
{ nullptr, nullptr },
2133 /* Analyse TYPE descending into nested structures, count the number of
2134 scalar fields and record the types of the first two fields found. */
2135 void analyse (struct type
*type
)
2137 analyse_inner (type
, 0);
2140 /* The number of scalar fields found in the analysed type. This is
2141 currently only accurate if the value returned is 0, 1, or 2 as the
2142 analysis stops counting when the number of fields is 3. This is
2143 because the RiscV ABI only has special cases for 1 or 2 fields,
2144 anything else we just don't care about. */
2145 int number_of_fields () const
2146 { return m_number_of_fields
; }
2148 /* Return the type for scalar field INDEX within the analysed type. Will
2149 return nullptr if there is no field at that index. Only INDEX values
2150 0 and 1 can be requested as the RiscV ABI only has special cases for
2151 structures with 1 or 2 fields. */
2152 struct type
*field_type (int index
) const
2154 gdb_assert (index
< (sizeof (m_types
) / sizeof (m_types
[0])));
2155 return m_types
[index
];
2158 /* Return the offset of scalar field INDEX within the analysed type. Will
2159 return 0 if there is no field at that index. Only INDEX values 0 and
2160 1 can be requested as the RiscV ABI only has special cases for
2161 structures with 1 or 2 fields. */
2162 int field_offset (int index
) const
2164 gdb_assert (index
< (sizeof (m_offsets
) / sizeof (m_offsets
[0])));
2165 return m_offsets
[index
];
2169 /* The number of scalar fields found within the structure after recursing
2170 into nested structures. */
2171 int m_number_of_fields
;
2173 /* The types of the first two scalar fields found within the structure
2174 after recursing into nested structures. */
2175 struct type
*m_types
[2];
2177 /* The offsets of the first two scalar fields found within the structure
2178 after recursing into nested structures. */
2181 /* Recursive core for ANALYSE, the OFFSET parameter tracks the byte
2182 offset from the start of the top level structure being analysed. */
2183 void analyse_inner (struct type
*type
, int offset
);
2186 /* See description in class declaration. */
2189 riscv_struct_info::analyse_inner (struct type
*type
, int offset
)
2191 unsigned int count
= type
->num_fields ();
2194 for (i
= 0; i
< count
; ++i
)
2196 if (TYPE_FIELD_LOC_KIND (type
, i
) != FIELD_LOC_KIND_BITPOS
)
2199 struct type
*field_type
= type
->field (i
).type ();
2200 field_type
= check_typedef (field_type
);
2202 = offset
+ TYPE_FIELD_BITPOS (type
, i
) / TARGET_CHAR_BIT
;
2204 switch (field_type
->code ())
2206 case TYPE_CODE_STRUCT
:
2207 analyse_inner (field_type
, field_offset
);
2211 /* RiscV only flattens out structures. Anything else does not
2212 need to be flattened, we just record the type, and when we
2213 look at the analysis results we'll realise this is not a
2214 structure we can special case, and pass the structure in
2216 if (m_number_of_fields
< 2)
2218 m_types
[m_number_of_fields
] = field_type
;
2219 m_offsets
[m_number_of_fields
] = field_offset
;
2221 m_number_of_fields
++;
2225 /* RiscV only has special handling for structures with 1 or 2 scalar
2226 fields, any more than that and the structure is just passed in
2227 memory. We can safely drop out early when we find 3 or more
2230 if (m_number_of_fields
> 2)
2235 /* Like RISCV_CALL_ARG_SCALAR_INT, except the argument described by AINFO
2236 is a structure. Small structures on RiscV have some special case
2237 handling in order that the structure might be passed in register.
2238 Larger structures are passed in memory. After assigning location
2239 information to AINFO, CINFO will have been updated. */
2242 riscv_call_arg_struct (struct riscv_arg_info
*ainfo
,
2243 struct riscv_call_info
*cinfo
)
2245 if (riscv_arg_regs_available (&cinfo
->float_regs
) >= 1)
2247 struct riscv_struct_info sinfo
;
2249 sinfo
.analyse (ainfo
->type
);
2250 if (sinfo
.number_of_fields () == 1
2251 && sinfo
.field_type(0)->code () == TYPE_CODE_COMPLEX
)
2253 /* The following is similar to RISCV_CALL_ARG_COMPLEX_FLOAT,
2254 except we use the type of the complex field instead of the
2255 type from AINFO, and the first location might be at a non-zero
2257 if (TYPE_LENGTH (sinfo
.field_type (0)) <= (2 * cinfo
->flen
)
2258 && riscv_arg_regs_available (&cinfo
->float_regs
) >= 2
2259 && !ainfo
->is_unnamed
)
2262 int len
= TYPE_LENGTH (sinfo
.field_type (0)) / 2;
2263 int offset
= sinfo
.field_offset (0);
2265 result
= riscv_assign_reg_location (&ainfo
->argloc
[0],
2266 &cinfo
->float_regs
, len
,
2268 gdb_assert (result
);
2270 result
= riscv_assign_reg_location (&ainfo
->argloc
[1],
2271 &cinfo
->float_regs
, len
,
2273 gdb_assert (result
);
2276 riscv_call_arg_scalar_int (ainfo
, cinfo
);
2280 if (sinfo
.number_of_fields () == 1
2281 && sinfo
.field_type(0)->code () == TYPE_CODE_FLT
)
2283 /* The following is similar to RISCV_CALL_ARG_SCALAR_FLOAT,
2284 except we use the type of the first scalar field instead of
2285 the type from AINFO. Also the location might be at a non-zero
2287 if (TYPE_LENGTH (sinfo
.field_type (0)) > cinfo
->flen
2288 || ainfo
->is_unnamed
)
2289 riscv_call_arg_scalar_int (ainfo
, cinfo
);
2292 int offset
= sinfo
.field_offset (0);
2293 int len
= TYPE_LENGTH (sinfo
.field_type (0));
2295 if (!riscv_assign_reg_location (&ainfo
->argloc
[0],
2298 riscv_call_arg_scalar_int (ainfo
, cinfo
);
2303 if (sinfo
.number_of_fields () == 2
2304 && sinfo
.field_type(0)->code () == TYPE_CODE_FLT
2305 && TYPE_LENGTH (sinfo
.field_type (0)) <= cinfo
->flen
2306 && sinfo
.field_type(1)->code () == TYPE_CODE_FLT
2307 && TYPE_LENGTH (sinfo
.field_type (1)) <= cinfo
->flen
2308 && riscv_arg_regs_available (&cinfo
->float_regs
) >= 2)
2310 int len0
= TYPE_LENGTH (sinfo
.field_type (0));
2311 int offset
= sinfo
.field_offset (0);
2312 if (!riscv_assign_reg_location (&ainfo
->argloc
[0],
2313 &cinfo
->float_regs
, len0
, offset
))
2314 error (_("failed during argument setup"));
2316 int len1
= TYPE_LENGTH (sinfo
.field_type (1));
2317 offset
= sinfo
.field_offset (1);
2318 gdb_assert (len1
<= (TYPE_LENGTH (ainfo
->type
)
2319 - TYPE_LENGTH (sinfo
.field_type (0))));
2321 if (!riscv_assign_reg_location (&ainfo
->argloc
[1],
2324 error (_("failed during argument setup"));
2328 if (sinfo
.number_of_fields () == 2
2329 && riscv_arg_regs_available (&cinfo
->int_regs
) >= 1
2330 && (sinfo
.field_type(0)->code () == TYPE_CODE_FLT
2331 && TYPE_LENGTH (sinfo
.field_type (0)) <= cinfo
->flen
2332 && is_integral_type (sinfo
.field_type (1))
2333 && TYPE_LENGTH (sinfo
.field_type (1)) <= cinfo
->xlen
))
2335 int len0
= TYPE_LENGTH (sinfo
.field_type (0));
2336 int offset
= sinfo
.field_offset (0);
2337 if (!riscv_assign_reg_location (&ainfo
->argloc
[0],
2338 &cinfo
->float_regs
, len0
, offset
))
2339 error (_("failed during argument setup"));
2341 int len1
= TYPE_LENGTH (sinfo
.field_type (1));
2342 offset
= sinfo
.field_offset (1);
2343 gdb_assert (len1
<= cinfo
->xlen
);
2344 if (!riscv_assign_reg_location (&ainfo
->argloc
[1],
2345 &cinfo
->int_regs
, len1
, offset
))
2346 error (_("failed during argument setup"));
2350 if (sinfo
.number_of_fields () == 2
2351 && riscv_arg_regs_available (&cinfo
->int_regs
) >= 1
2352 && (is_integral_type (sinfo
.field_type (0))
2353 && TYPE_LENGTH (sinfo
.field_type (0)) <= cinfo
->xlen
2354 && sinfo
.field_type(1)->code () == TYPE_CODE_FLT
2355 && TYPE_LENGTH (sinfo
.field_type (1)) <= cinfo
->flen
))
2357 int len0
= TYPE_LENGTH (sinfo
.field_type (0));
2358 int len1
= TYPE_LENGTH (sinfo
.field_type (1));
2360 gdb_assert (len0
<= cinfo
->xlen
);
2361 gdb_assert (len1
<= cinfo
->flen
);
2363 int offset
= sinfo
.field_offset (0);
2364 if (!riscv_assign_reg_location (&ainfo
->argloc
[0],
2365 &cinfo
->int_regs
, len0
, offset
))
2366 error (_("failed during argument setup"));
2368 offset
= sinfo
.field_offset (1);
2369 if (!riscv_assign_reg_location (&ainfo
->argloc
[1],
2372 error (_("failed during argument setup"));
2378 /* Non of the structure flattening cases apply, so we just pass using
2380 riscv_call_arg_scalar_int (ainfo
, cinfo
);
2383 /* Assign a location to call (or return) argument AINFO, the location is
2384 selected from CINFO which holds information about what call argument
2385 locations are available for use next. The TYPE is the type of the
2386 argument being passed, this information is recorded into AINFO (along
2387 with some additional information derived from the type). IS_UNNAMED
2388 is true if this is an unnamed (stdarg) argument, this info is also
2389 recorded into AINFO.
2391 After assigning a location to AINFO, CINFO will have been updated. */
2394 riscv_arg_location (struct gdbarch
*gdbarch
,
2395 struct riscv_arg_info
*ainfo
,
2396 struct riscv_call_info
*cinfo
,
2397 struct type
*type
, bool is_unnamed
)
2400 ainfo
->length
= TYPE_LENGTH (ainfo
->type
);
2401 ainfo
->align
= type_align (ainfo
->type
);
2402 ainfo
->is_unnamed
= is_unnamed
;
2403 ainfo
->contents
= nullptr;
2404 ainfo
->argloc
[0].c_length
= 0;
2405 ainfo
->argloc
[1].c_length
= 0;
2407 switch (ainfo
->type
->code ())
2410 case TYPE_CODE_BOOL
:
2411 case TYPE_CODE_CHAR
:
2412 case TYPE_CODE_RANGE
:
2413 case TYPE_CODE_ENUM
:
2415 if (ainfo
->length
<= cinfo
->xlen
)
2417 ainfo
->type
= builtin_type (gdbarch
)->builtin_long
;
2418 ainfo
->length
= cinfo
->xlen
;
2420 else if (ainfo
->length
<= (2 * cinfo
->xlen
))
2422 ainfo
->type
= builtin_type (gdbarch
)->builtin_long_long
;
2423 ainfo
->length
= 2 * cinfo
->xlen
;
2426 /* Recalculate the alignment requirement. */
2427 ainfo
->align
= type_align (ainfo
->type
);
2428 riscv_call_arg_scalar_int (ainfo
, cinfo
);
2432 riscv_call_arg_scalar_float (ainfo
, cinfo
);
2435 case TYPE_CODE_COMPLEX
:
2436 riscv_call_arg_complex_float (ainfo
, cinfo
);
2439 case TYPE_CODE_STRUCT
:
2440 riscv_call_arg_struct (ainfo
, cinfo
);
2444 riscv_call_arg_scalar_int (ainfo
, cinfo
);
2449 /* Used for printing debug information about the call argument location in
2450 INFO to STREAM. The addresses in SP_REFS and SP_ARGS are the base
2451 addresses for the location of pass-by-reference and
2452 arguments-on-the-stack memory areas. */
2455 riscv_print_arg_location (ui_file
*stream
, struct gdbarch
*gdbarch
,
2456 struct riscv_arg_info
*info
,
2457 CORE_ADDR sp_refs
, CORE_ADDR sp_args
)
2459 fprintf_unfiltered (stream
, "type: '%s', length: 0x%x, alignment: 0x%x",
2460 TYPE_SAFE_NAME (info
->type
), info
->length
, info
->align
);
2461 switch (info
->argloc
[0].loc_type
)
2463 case riscv_arg_info::location::in_reg
:
2465 (stream
, ", register %s",
2466 gdbarch_register_name (gdbarch
, info
->argloc
[0].loc_data
.regno
));
2467 if (info
->argloc
[0].c_length
< info
->length
)
2469 switch (info
->argloc
[1].loc_type
)
2471 case riscv_arg_info::location::in_reg
:
2473 (stream
, ", register %s",
2474 gdbarch_register_name (gdbarch
,
2475 info
->argloc
[1].loc_data
.regno
));
2478 case riscv_arg_info::location::on_stack
:
2479 fprintf_unfiltered (stream
, ", on stack at offset 0x%x",
2480 info
->argloc
[1].loc_data
.offset
);
2483 case riscv_arg_info::location::by_ref
:
2485 /* The second location should never be a reference, any
2486 argument being passed by reference just places its address
2487 in the first location and is done. */
2488 error (_("invalid argument location"));
2492 if (info
->argloc
[1].c_offset
> info
->argloc
[0].c_length
)
2493 fprintf_unfiltered (stream
, " (offset 0x%x)",
2494 info
->argloc
[1].c_offset
);
2498 case riscv_arg_info::location::on_stack
:
2499 fprintf_unfiltered (stream
, ", on stack at offset 0x%x",
2500 info
->argloc
[0].loc_data
.offset
);
2503 case riscv_arg_info::location::by_ref
:
2505 (stream
, ", by reference, data at offset 0x%x (%s)",
2506 info
->argloc
[0].loc_data
.offset
,
2507 core_addr_to_string (sp_refs
+ info
->argloc
[0].loc_data
.offset
));
2508 if (info
->argloc
[1].loc_type
2509 == riscv_arg_info::location::in_reg
)
2511 (stream
, ", address in register %s",
2512 gdbarch_register_name (gdbarch
, info
->argloc
[1].loc_data
.regno
));
2515 gdb_assert (info
->argloc
[1].loc_type
2516 == riscv_arg_info::location::on_stack
);
2518 (stream
, ", address on stack at offset 0x%x (%s)",
2519 info
->argloc
[1].loc_data
.offset
,
2520 core_addr_to_string (sp_args
+ info
->argloc
[1].loc_data
.offset
));
2525 gdb_assert_not_reached (_("unknown argument location type"));
2529 /* Wrapper around REGCACHE->cooked_write. Places the LEN bytes of DATA
2530 into a buffer that is at least as big as the register REGNUM, padding
2531 out the DATA with either 0x00, or 0xff. For floating point registers
2532 0xff is used, for everyone else 0x00 is used. */
2535 riscv_regcache_cooked_write (int regnum
, const gdb_byte
*data
, int len
,
2536 struct regcache
*regcache
, int flen
)
2538 gdb_byte tmp
[sizeof (ULONGEST
)];
2540 /* FP values in FP registers must be NaN-boxed. */
2541 if (riscv_is_fp_regno_p (regnum
) && len
< flen
)
2542 memset (tmp
, -1, sizeof (tmp
));
2544 memset (tmp
, 0, sizeof (tmp
));
2545 memcpy (tmp
, data
, len
);
2546 regcache
->cooked_write (regnum
, tmp
);
2549 /* Implement the push dummy call gdbarch callback. */
2552 riscv_push_dummy_call (struct gdbarch
*gdbarch
,
2553 struct value
*function
,
2554 struct regcache
*regcache
,
2557 struct value
**args
,
2559 function_call_return_method return_method
,
2560 CORE_ADDR struct_addr
)
2563 CORE_ADDR sp_args
, sp_refs
;
2564 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2566 struct riscv_arg_info
*arg_info
=
2567 (struct riscv_arg_info
*) alloca (nargs
* sizeof (struct riscv_arg_info
));
2569 struct riscv_call_info
call_info (gdbarch
);
2573 struct type
*ftype
= check_typedef (value_type (function
));
2575 if (ftype
->code () == TYPE_CODE_PTR
)
2576 ftype
= check_typedef (TYPE_TARGET_TYPE (ftype
));
2578 /* We'll use register $a0 if we're returning a struct. */
2579 if (return_method
== return_method_struct
)
2580 ++call_info
.int_regs
.next_regnum
;
2582 for (i
= 0; i
< nargs
; ++i
)
2584 struct value
*arg_value
;
2585 struct type
*arg_type
;
2586 struct riscv_arg_info
*info
= &arg_info
[i
];
2588 arg_value
= args
[i
];
2589 arg_type
= check_typedef (value_type (arg_value
));
2591 riscv_arg_location (gdbarch
, info
, &call_info
, arg_type
,
2592 ftype
->has_varargs () && i
>= ftype
->num_fields ());
2594 if (info
->type
!= arg_type
)
2595 arg_value
= value_cast (info
->type
, arg_value
);
2596 info
->contents
= value_contents (arg_value
);
2599 /* Adjust the stack pointer and align it. */
2600 sp
= sp_refs
= align_down (sp
- call_info
.memory
.ref_offset
, SP_ALIGNMENT
);
2601 sp
= sp_args
= align_down (sp
- call_info
.memory
.arg_offset
, SP_ALIGNMENT
);
2603 if (riscv_debug_infcall
> 0)
2605 fprintf_unfiltered (gdb_stdlog
, "dummy call args:\n");
2606 fprintf_unfiltered (gdb_stdlog
, ": floating point ABI %s in use\n",
2607 (riscv_has_fp_abi (gdbarch
) ? "is" : "is not"));
2608 fprintf_unfiltered (gdb_stdlog
, ": xlen: %d\n: flen: %d\n",
2609 call_info
.xlen
, call_info
.flen
);
2610 if (return_method
== return_method_struct
)
2611 fprintf_unfiltered (gdb_stdlog
,
2612 "[*] struct return pointer in register $A0\n");
2613 for (i
= 0; i
< nargs
; ++i
)
2615 struct riscv_arg_info
*info
= &arg_info
[i
];
2617 fprintf_unfiltered (gdb_stdlog
, "[%2d] ", i
);
2618 riscv_print_arg_location (gdb_stdlog
, gdbarch
, info
, sp_refs
, sp_args
);
2619 fprintf_unfiltered (gdb_stdlog
, "\n");
2621 if (call_info
.memory
.arg_offset
> 0
2622 || call_info
.memory
.ref_offset
> 0)
2624 fprintf_unfiltered (gdb_stdlog
, " Original sp: %s\n",
2625 core_addr_to_string (osp
));
2626 fprintf_unfiltered (gdb_stdlog
, "Stack required (for args): 0x%x\n",
2627 call_info
.memory
.arg_offset
);
2628 fprintf_unfiltered (gdb_stdlog
, "Stack required (for refs): 0x%x\n",
2629 call_info
.memory
.ref_offset
);
2630 fprintf_unfiltered (gdb_stdlog
, " Stack allocated: %s\n",
2631 core_addr_to_string_nz (osp
- sp
));
2635 /* Now load the argument into registers, or onto the stack. */
2637 if (return_method
== return_method_struct
)
2639 gdb_byte buf
[sizeof (LONGEST
)];
2641 store_unsigned_integer (buf
, call_info
.xlen
, byte_order
, struct_addr
);
2642 regcache
->cooked_write (RISCV_A0_REGNUM
, buf
);
2645 for (i
= 0; i
< nargs
; ++i
)
2648 int second_arg_length
= 0;
2649 const gdb_byte
*second_arg_data
;
2650 struct riscv_arg_info
*info
= &arg_info
[i
];
2652 gdb_assert (info
->length
> 0);
2654 switch (info
->argloc
[0].loc_type
)
2656 case riscv_arg_info::location::in_reg
:
2658 gdb_assert (info
->argloc
[0].c_length
<= info
->length
);
2660 riscv_regcache_cooked_write (info
->argloc
[0].loc_data
.regno
,
2662 + info
->argloc
[0].c_offset
),
2663 info
->argloc
[0].c_length
,
2664 regcache
, call_info
.flen
);
2666 (((info
->argloc
[0].c_length
+ info
->argloc
[0].c_offset
) < info
->length
)
2667 ? info
->argloc
[1].c_length
: 0);
2668 second_arg_data
= info
->contents
+ info
->argloc
[1].c_offset
;
2672 case riscv_arg_info::location::on_stack
:
2673 dst
= sp_args
+ info
->argloc
[0].loc_data
.offset
;
2674 write_memory (dst
, info
->contents
, info
->length
);
2675 second_arg_length
= 0;
2678 case riscv_arg_info::location::by_ref
:
2679 dst
= sp_refs
+ info
->argloc
[0].loc_data
.offset
;
2680 write_memory (dst
, info
->contents
, info
->length
);
2682 second_arg_length
= call_info
.xlen
;
2683 second_arg_data
= (gdb_byte
*) &dst
;
2687 gdb_assert_not_reached (_("unknown argument location type"));
2690 if (second_arg_length
> 0)
2692 switch (info
->argloc
[1].loc_type
)
2694 case riscv_arg_info::location::in_reg
:
2696 gdb_assert ((riscv_is_fp_regno_p (info
->argloc
[1].loc_data
.regno
)
2697 && second_arg_length
<= call_info
.flen
)
2698 || second_arg_length
<= call_info
.xlen
);
2699 riscv_regcache_cooked_write (info
->argloc
[1].loc_data
.regno
,
2702 regcache
, call_info
.flen
);
2706 case riscv_arg_info::location::on_stack
:
2710 arg_addr
= sp_args
+ info
->argloc
[1].loc_data
.offset
;
2711 write_memory (arg_addr
, second_arg_data
, second_arg_length
);
2715 case riscv_arg_info::location::by_ref
:
2717 /* The second location should never be a reference, any
2718 argument being passed by reference just places its address
2719 in the first location and is done. */
2720 error (_("invalid argument location"));
2726 /* Set the dummy return value to bp_addr.
2727 A dummy breakpoint will be setup to execute the call. */
2729 if (riscv_debug_infcall
> 0)
2730 fprintf_unfiltered (gdb_stdlog
, ": writing $ra = %s\n",
2731 core_addr_to_string (bp_addr
));
2732 regcache_cooked_write_unsigned (regcache
, RISCV_RA_REGNUM
, bp_addr
);
2734 /* Finally, update the stack pointer. */
2736 if (riscv_debug_infcall
> 0)
2737 fprintf_unfiltered (gdb_stdlog
, ": writing $sp = %s\n",
2738 core_addr_to_string (sp
));
2739 regcache_cooked_write_unsigned (regcache
, RISCV_SP_REGNUM
, sp
);
2744 /* Implement the return_value gdbarch method. */
2746 static enum return_value_convention
2747 riscv_return_value (struct gdbarch
*gdbarch
,
2748 struct value
*function
,
2750 struct regcache
*regcache
,
2752 const gdb_byte
*writebuf
)
2754 struct riscv_call_info
call_info (gdbarch
);
2755 struct riscv_arg_info info
;
2756 struct type
*arg_type
;
2758 arg_type
= check_typedef (type
);
2759 riscv_arg_location (gdbarch
, &info
, &call_info
, arg_type
, false);
2761 if (riscv_debug_infcall
> 0)
2763 fprintf_unfiltered (gdb_stdlog
, "riscv return value:\n");
2764 fprintf_unfiltered (gdb_stdlog
, "[R] ");
2765 riscv_print_arg_location (gdb_stdlog
, gdbarch
, &info
, 0, 0);
2766 fprintf_unfiltered (gdb_stdlog
, "\n");
2769 if (readbuf
!= nullptr || writebuf
!= nullptr)
2771 unsigned int arg_len
;
2772 struct value
*abi_val
;
2773 gdb_byte
*old_readbuf
= nullptr;
2776 /* We only do one thing at a time. */
2777 gdb_assert (readbuf
== nullptr || writebuf
== nullptr);
2779 /* In some cases the argument is not returned as the declared type,
2780 and we need to cast to or from the ABI type in order to
2781 correctly access the argument. When writing to the machine we
2782 do the cast here, when reading from the machine the cast occurs
2783 later, after extracting the value. As the ABI type can be
2784 larger than the declared type, then the read or write buffers
2785 passed in might be too small. Here we ensure that we are using
2786 buffers of sufficient size. */
2787 if (writebuf
!= nullptr)
2789 struct value
*arg_val
= value_from_contents (arg_type
, writebuf
);
2790 abi_val
= value_cast (info
.type
, arg_val
);
2791 writebuf
= value_contents_raw (abi_val
);
2795 abi_val
= allocate_value (info
.type
);
2796 old_readbuf
= readbuf
;
2797 readbuf
= value_contents_raw (abi_val
);
2799 arg_len
= TYPE_LENGTH (info
.type
);
2801 switch (info
.argloc
[0].loc_type
)
2803 /* Return value in register(s). */
2804 case riscv_arg_info::location::in_reg
:
2806 regnum
= info
.argloc
[0].loc_data
.regno
;
2807 gdb_assert (info
.argloc
[0].c_length
<= arg_len
);
2808 gdb_assert (info
.argloc
[0].c_length
2809 <= register_size (gdbarch
, regnum
));
2813 gdb_byte
*ptr
= readbuf
+ info
.argloc
[0].c_offset
;
2814 regcache
->cooked_read_part (regnum
, 0,
2815 info
.argloc
[0].c_length
,
2821 const gdb_byte
*ptr
= writebuf
+ info
.argloc
[0].c_offset
;
2822 riscv_regcache_cooked_write (regnum
, ptr
,
2823 info
.argloc
[0].c_length
,
2824 regcache
, call_info
.flen
);
2827 /* A return value in register can have a second part in a
2829 if (info
.argloc
[1].c_length
> 0)
2831 switch (info
.argloc
[1].loc_type
)
2833 case riscv_arg_info::location::in_reg
:
2834 regnum
= info
.argloc
[1].loc_data
.regno
;
2836 gdb_assert ((info
.argloc
[0].c_length
2837 + info
.argloc
[1].c_length
) <= arg_len
);
2838 gdb_assert (info
.argloc
[1].c_length
2839 <= register_size (gdbarch
, regnum
));
2843 readbuf
+= info
.argloc
[1].c_offset
;
2844 regcache
->cooked_read_part (regnum
, 0,
2845 info
.argloc
[1].c_length
,
2852 = writebuf
+ info
.argloc
[1].c_offset
;
2853 riscv_regcache_cooked_write
2854 (regnum
, ptr
, info
.argloc
[1].c_length
,
2855 regcache
, call_info
.flen
);
2859 case riscv_arg_info::location::by_ref
:
2860 case riscv_arg_info::location::on_stack
:
2862 error (_("invalid argument location"));
2869 /* Return value by reference will have its address in A0. */
2870 case riscv_arg_info::location::by_ref
:
2874 regcache_cooked_read_unsigned (regcache
, RISCV_A0_REGNUM
,
2876 if (readbuf
!= nullptr)
2877 read_memory (addr
, readbuf
, info
.length
);
2878 if (writebuf
!= nullptr)
2879 write_memory (addr
, writebuf
, info
.length
);
2883 case riscv_arg_info::location::on_stack
:
2885 error (_("invalid argument location"));
2889 /* This completes the cast from abi type back to the declared type
2890 in the case that we are reading from the machine. See the
2891 comment at the head of this block for more details. */
2892 if (readbuf
!= nullptr)
2894 struct value
*arg_val
= value_cast (arg_type
, abi_val
);
2895 memcpy (old_readbuf
, value_contents_raw (arg_val
),
2896 TYPE_LENGTH (arg_type
));
2900 switch (info
.argloc
[0].loc_type
)
2902 case riscv_arg_info::location::in_reg
:
2903 return RETURN_VALUE_REGISTER_CONVENTION
;
2904 case riscv_arg_info::location::by_ref
:
2905 return RETURN_VALUE_ABI_RETURNS_ADDRESS
;
2906 case riscv_arg_info::location::on_stack
:
2908 error (_("invalid argument location"));
2912 /* Implement the frame_align gdbarch method. */
2915 riscv_frame_align (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
2917 return align_down (addr
, 16);
2920 /* Generate, or return the cached frame cache for the RiscV frame
2923 static struct riscv_unwind_cache
*
2924 riscv_frame_cache (struct frame_info
*this_frame
, void **this_cache
)
2926 CORE_ADDR pc
, start_addr
;
2927 struct riscv_unwind_cache
*cache
;
2928 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
2931 if ((*this_cache
) != NULL
)
2932 return (struct riscv_unwind_cache
*) *this_cache
;
2934 cache
= FRAME_OBSTACK_ZALLOC (struct riscv_unwind_cache
);
2935 cache
->regs
= trad_frame_alloc_saved_regs (this_frame
);
2936 (*this_cache
) = cache
;
2938 /* Scan the prologue, filling in the cache. */
2939 start_addr
= get_frame_func (this_frame
);
2940 pc
= get_frame_pc (this_frame
);
2941 riscv_scan_prologue (gdbarch
, start_addr
, pc
, cache
);
2943 /* We can now calculate the frame base address. */
2945 = (get_frame_register_unsigned (this_frame
, cache
->frame_base_reg
)
2946 + cache
->frame_base_offset
);
2947 if (riscv_debug_unwinder
)
2948 fprintf_unfiltered (gdb_stdlog
, "Frame base is %s ($%s + 0x%x)\n",
2949 core_addr_to_string (cache
->frame_base
),
2950 gdbarch_register_name (gdbarch
,
2951 cache
->frame_base_reg
),
2952 cache
->frame_base_offset
);
2954 /* The prologue scanner sets the address of registers stored to the stack
2955 as the offset of that register from the frame base. The prologue
2956 scanner doesn't know the actual frame base value, and so is unable to
2957 compute the exact address. We do now know the frame base value, so
2958 update the address of registers stored to the stack. */
2959 numregs
= gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
2960 for (regno
= 0; regno
< numregs
; ++regno
)
2962 if (trad_frame_addr_p (cache
->regs
, regno
))
2963 cache
->regs
[regno
].addr
+= cache
->frame_base
;
2966 /* The previous $pc can be found wherever the $ra value can be found.
2967 The previous $ra value is gone, this would have been stored be the
2968 previous frame if required. */
2969 cache
->regs
[gdbarch_pc_regnum (gdbarch
)] = cache
->regs
[RISCV_RA_REGNUM
];
2970 trad_frame_set_unknown (cache
->regs
, RISCV_RA_REGNUM
);
2972 /* Build the frame id. */
2973 cache
->this_id
= frame_id_build (cache
->frame_base
, start_addr
);
2975 /* The previous $sp value is the frame base value. */
2976 trad_frame_set_value (cache
->regs
, gdbarch_sp_regnum (gdbarch
),
2982 /* Implement the this_id callback for RiscV frame unwinder. */
2985 riscv_frame_this_id (struct frame_info
*this_frame
,
2986 void **prologue_cache
,
2987 struct frame_id
*this_id
)
2989 struct riscv_unwind_cache
*cache
;
2993 cache
= riscv_frame_cache (this_frame
, prologue_cache
);
2994 *this_id
= cache
->this_id
;
2996 catch (const gdb_exception_error
&ex
)
2998 /* Ignore errors, this leaves the frame id as the predefined outer
2999 frame id which terminates the backtrace at this point. */
3003 /* Implement the prev_register callback for RiscV frame unwinder. */
3005 static struct value
*
3006 riscv_frame_prev_register (struct frame_info
*this_frame
,
3007 void **prologue_cache
,
3010 struct riscv_unwind_cache
*cache
;
3012 cache
= riscv_frame_cache (this_frame
, prologue_cache
);
3013 return trad_frame_get_prev_register (this_frame
, cache
->regs
, regnum
);
3016 /* Structure defining the RiscV normal frame unwind functions. Since we
3017 are the fallback unwinder (DWARF unwinder is used first), we use the
3018 default frame sniffer, which always accepts the frame. */
3020 static const struct frame_unwind riscv_frame_unwind
=
3022 /*.type =*/ NORMAL_FRAME
,
3023 /*.stop_reason =*/ default_frame_unwind_stop_reason
,
3024 /*.this_id =*/ riscv_frame_this_id
,
3025 /*.prev_register =*/ riscv_frame_prev_register
,
3026 /*.unwind_data =*/ NULL
,
3027 /*.sniffer =*/ default_frame_sniffer
,
3028 /*.dealloc_cache =*/ NULL
,
3029 /*.prev_arch =*/ NULL
,
3032 /* Extract a set of required target features out of INFO, specifically the
3033 bfd being executed is examined to see what target features it requires.
3034 IF there is no current bfd, or the bfd doesn't indicate any useful
3035 features then a RISCV_GDBARCH_FEATURES is returned in its default state. */
3037 static struct riscv_gdbarch_features
3038 riscv_features_from_gdbarch_info (const struct gdbarch_info info
)
3040 struct riscv_gdbarch_features features
;
3042 /* Now try to improve on the defaults by looking at the binary we are
3043 going to execute. We assume the user knows what they are doing and
3044 that the target will match the binary. Remember, this code path is
3045 only used at all if the target hasn't given us a description, so this
3046 is really a last ditched effort to do something sane before giving
3048 if (info
.abfd
!= NULL
3049 && bfd_get_flavour (info
.abfd
) == bfd_target_elf_flavour
)
3051 unsigned char eclass
= elf_elfheader (info
.abfd
)->e_ident
[EI_CLASS
];
3052 int e_flags
= elf_elfheader (info
.abfd
)->e_flags
;
3054 if (eclass
== ELFCLASS32
)
3056 else if (eclass
== ELFCLASS64
)
3059 internal_error (__FILE__
, __LINE__
,
3060 _("unknown ELF header class %d"), eclass
);
3062 if (e_flags
& EF_RISCV_FLOAT_ABI_DOUBLE
)
3064 else if (e_flags
& EF_RISCV_FLOAT_ABI_SINGLE
)
3071 /* Find a suitable default target description. Use the contents of INFO,
3072 specifically the bfd object being executed, to guide the selection of a
3073 suitable default target description. */
3075 static const struct target_desc
*
3076 riscv_find_default_target_description (const struct gdbarch_info info
)
3078 /* Extract desired feature set from INFO. */
3079 struct riscv_gdbarch_features features
3080 = riscv_features_from_gdbarch_info (info
);
3082 /* If the XLEN field is still 0 then we got nothing useful from INFO. In
3083 this case we fall back to a minimal useful target, 8-byte x-registers,
3084 with no floating point. */
3085 if (features
.xlen
== 0)
3088 /* Now build a target description based on the feature set. */
3089 return riscv_lookup_target_description (features
);
3092 /* All of the registers in REG_SET are checked for in FEATURE, TDESC_DATA
3093 is updated with the register numbers for each register as listed in
3094 REG_SET. If any register marked as required in REG_SET is not found in
3095 FEATURE then this function returns false, otherwise, it returns true. */
3098 riscv_check_tdesc_feature (struct tdesc_arch_data
*tdesc_data
,
3099 const struct tdesc_feature
*main_feature
,
3100 const struct tdesc_feature
*csr_feature
,
3101 const struct riscv_register_feature
*reg_set
,
3102 std::vector
<riscv_pending_register_alias
> *aliases
)
3104 for (const auto ®
: reg_set
->registers
)
3106 bool found
= reg
.check (tdesc_data
, main_feature
, reg_set
, aliases
);
3108 if (!found
&& reg
.required
!= RISCV_REG_OPTIONAL
)
3110 if (reg
.required
== RISCV_REG_REQUIRED_MAYBE_CSR
3111 && csr_feature
!= nullptr)
3113 gdb_assert (main_feature
!= csr_feature
);
3114 found
= reg
.check (tdesc_data
, csr_feature
, reg_set
, aliases
);
3124 /* Add all the expected register sets into GDBARCH. */
3127 riscv_add_reggroups (struct gdbarch
*gdbarch
)
3129 /* Add predefined register groups. */
3130 reggroup_add (gdbarch
, all_reggroup
);
3131 reggroup_add (gdbarch
, save_reggroup
);
3132 reggroup_add (gdbarch
, restore_reggroup
);
3133 reggroup_add (gdbarch
, system_reggroup
);
3134 reggroup_add (gdbarch
, vector_reggroup
);
3135 reggroup_add (gdbarch
, general_reggroup
);
3136 reggroup_add (gdbarch
, float_reggroup
);
3138 /* Add RISC-V specific register groups. */
3139 reggroup_add (gdbarch
, csr_reggroup
);
3142 /* Implement the "dwarf2_reg_to_regnum" gdbarch method. */
3145 riscv_dwarf_reg_to_regnum (struct gdbarch
*gdbarch
, int reg
)
3147 if (reg
< RISCV_DWARF_REGNUM_X31
)
3148 return RISCV_ZERO_REGNUM
+ (reg
- RISCV_DWARF_REGNUM_X0
);
3150 else if (reg
< RISCV_DWARF_REGNUM_F31
)
3151 return RISCV_FIRST_FP_REGNUM
+ (reg
- RISCV_DWARF_REGNUM_F0
);
3153 else if (reg
>= RISCV_DWARF_FIRST_CSR
&& reg
<= RISCV_DWARF_LAST_CSR
)
3154 return RISCV_FIRST_CSR_REGNUM
+ (reg
- RISCV_DWARF_FIRST_CSR
);
3159 /* Implement the gcc_target_options method. We have to select the arch and abi
3160 from the feature info. We have enough feature info to select the abi, but
3161 not enough info for the arch given all of the possible architecture
3162 extensions. So choose reasonable defaults for now. */
3165 riscv_gcc_target_options (struct gdbarch
*gdbarch
)
3167 int isa_xlen
= riscv_isa_xlen (gdbarch
);
3168 int isa_flen
= riscv_isa_flen (gdbarch
);
3169 int abi_xlen
= riscv_abi_xlen (gdbarch
);
3170 int abi_flen
= riscv_abi_flen (gdbarch
);
3171 std::string target_options
;
3173 target_options
= "-march=rv";
3175 target_options
+= "64";
3177 target_options
+= "32";
3179 target_options
+= "gc";
3180 else if (isa_flen
== 4)
3181 target_options
+= "imafc";
3183 target_options
+= "imac";
3185 target_options
+= " -mabi=";
3187 target_options
+= "lp64";
3189 target_options
+= "ilp32";
3191 target_options
+= "d";
3192 else if (abi_flen
== 4)
3193 target_options
+= "f";
3195 /* The gdb loader doesn't handle link-time relaxation relocations. */
3196 target_options
+= " -mno-relax";
3198 return target_options
;
3201 /* Call back from tdesc_use_registers, called for each unknown register
3202 found in the target description.
3204 See target-description.h (typedef tdesc_unknown_register_ftype) for a
3205 discussion of the arguments and return values. */
3208 riscv_tdesc_unknown_reg (struct gdbarch
*gdbarch
, tdesc_feature
*feature
,
3209 const char *reg_name
, int possible_regnum
)
3211 /* At one point in time GDB had an incorrect default target description
3212 that duplicated the fflags, frm, and fcsr registers in both the FPU
3213 and CSR register sets.
3215 Some targets (QEMU) copied these target descriptions into their source
3216 tree, and so we're currently stuck working with some targets that
3217 declare the same registers twice.
3219 There's not much we can do about this any more. Assuming the target
3220 will direct a request for either register number to the correct
3221 underlying hardware register then it doesn't matter which one GDB
3222 uses, so long as we (GDB) are consistent (so that we don't end up with
3223 invalid cache misses).
3225 As we always scan the FPU registers first, then the CSRs, if the
3226 target has included the offending registers in both sets then we will
3227 always see the FPU copies here, as the CSR versions will replace them
3228 in the register list.
3230 To prevent these duplicates showing up in any of the register list,
3231 record their register numbers here. */
3232 if (strcmp (tdesc_feature_name (feature
), riscv_freg_feature
.name
) == 0)
3234 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
3235 int *regnum_ptr
= nullptr;
3237 if (strcmp (reg_name
, "fflags") == 0)
3238 regnum_ptr
= &tdep
->duplicate_fflags_regnum
;
3239 else if (strcmp (reg_name
, "frm") == 0)
3240 regnum_ptr
= &tdep
->duplicate_frm_regnum
;
3241 else if (strcmp (reg_name
, "fcsr") == 0)
3242 regnum_ptr
= &tdep
->duplicate_fcsr_regnum
;
3244 if (regnum_ptr
!= nullptr)
3246 /* This means the register appears more than twice in the target
3247 description. Just let GDB add this as another register.
3248 We'll have duplicates in the register name list, but there's
3249 not much more we can do. */
3250 if (*regnum_ptr
!= -1)
3253 /* Record the number assigned to this register, then return the
3254 number (so it actually gets assigned to this register). */
3255 *regnum_ptr
= possible_regnum
;
3256 return possible_regnum
;
3260 /* Any unknown registers in the CSR feature are recorded within a single
3261 block so we can easily identify these registers when making choices
3262 about register groups in riscv_register_reggroup_p. */
3263 if (strcmp (tdesc_feature_name (feature
), riscv_csr_feature
.name
) == 0)
3265 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
3266 if (tdep
->unknown_csrs_first_regnum
== -1)
3267 tdep
->unknown_csrs_first_regnum
= possible_regnum
;
3268 gdb_assert (tdep
->unknown_csrs_first_regnum
3269 + tdep
->unknown_csrs_count
== possible_regnum
);
3270 tdep
->unknown_csrs_count
++;
3271 return possible_regnum
;
3274 /* Some other unknown register. Don't assign this a number now, it will
3275 be assigned a number automatically later by the target description
3280 /* Implement the gnu_triplet_regexp method. A single compiler supports both
3281 32-bit and 64-bit code, and may be named riscv32 or riscv64 or (not
3282 recommended) riscv. */
3285 riscv_gnu_triplet_regexp (struct gdbarch
*gdbarch
)
3287 return "riscv(32|64)?";
3290 /* Initialize the current architecture based on INFO. If possible,
3291 re-use an architecture from ARCHES, which is a list of
3292 architectures already created during this debugging session.
3294 Called e.g. at program startup, when reading a core file, and when
3295 reading a binary file. */
3297 static struct gdbarch
*
3298 riscv_gdbarch_init (struct gdbarch_info info
,
3299 struct gdbarch_list
*arches
)
3301 struct gdbarch
*gdbarch
;
3302 struct gdbarch_tdep
*tdep
;
3303 struct riscv_gdbarch_features features
;
3304 const struct target_desc
*tdesc
= info
.target_desc
;
3306 /* Ensure we always have a target description. */
3307 if (!tdesc_has_registers (tdesc
))
3308 tdesc
= riscv_find_default_target_description (info
);
3311 if (riscv_debug_gdbarch
)
3312 fprintf_unfiltered (gdb_stdlog
, "Have got a target description\n");
3314 const struct tdesc_feature
*feature_cpu
3315 = tdesc_find_feature (tdesc
, riscv_xreg_feature
.name
);
3316 const struct tdesc_feature
*feature_fpu
3317 = tdesc_find_feature (tdesc
, riscv_freg_feature
.name
);
3318 const struct tdesc_feature
*feature_virtual
3319 = tdesc_find_feature (tdesc
, riscv_virtual_feature
.name
);
3320 const struct tdesc_feature
*feature_csr
3321 = tdesc_find_feature (tdesc
, riscv_csr_feature
.name
);
3323 if (feature_cpu
== NULL
)
3326 tdesc_arch_data_up tdesc_data
= tdesc_data_alloc ();
3327 std::vector
<riscv_pending_register_alias
> pending_aliases
;
3329 bool valid_p
= riscv_check_tdesc_feature (tdesc_data
.get (),
3330 feature_cpu
, feature_csr
,
3331 &riscv_xreg_feature
,
3335 /* Check that all of the core cpu registers have the same bitsize. */
3336 int xlen_bitsize
= tdesc_register_bitsize (feature_cpu
, "pc");
3338 for (auto &tdesc_reg
: feature_cpu
->registers
)
3339 valid_p
&= (tdesc_reg
->bitsize
== xlen_bitsize
);
3341 if (riscv_debug_gdbarch
)
3344 "From target-description, xlen = %d\n", xlen_bitsize
);
3346 features
.xlen
= (xlen_bitsize
/ 8);
3349 if (feature_fpu
!= NULL
)
3351 valid_p
&= riscv_check_tdesc_feature (tdesc_data
.get (), feature_fpu
,
3353 &riscv_freg_feature
,
3356 /* Search for the first floating point register (by any alias), to
3357 determine the bitsize. */
3359 const auto &fp0
= riscv_freg_feature
.registers
[0];
3361 for (const char *name
: fp0
.names
)
3363 if (tdesc_unnumbered_register (feature_fpu
, name
))
3365 bitsize
= tdesc_register_bitsize (feature_fpu
, name
);
3370 gdb_assert (bitsize
!= -1);
3371 features
.flen
= (bitsize
/ 8);
3373 if (riscv_debug_gdbarch
)
3376 "From target-description, flen = %d\n", bitsize
);
3382 if (riscv_debug_gdbarch
)
3385 "No FPU in target-description, assume soft-float ABI\n");
3388 if (feature_virtual
)
3389 riscv_check_tdesc_feature (tdesc_data
.get (), feature_virtual
, feature_csr
,
3390 &riscv_virtual_feature
,
3394 riscv_check_tdesc_feature (tdesc_data
.get (), feature_csr
, nullptr,
3400 if (riscv_debug_gdbarch
)
3401 fprintf_unfiltered (gdb_stdlog
, "Target description is not valid\n");
3405 /* Have a look at what the supplied (if any) bfd object requires of the
3406 target, then check that this matches with what the target is
3408 struct riscv_gdbarch_features abi_features
3409 = riscv_features_from_gdbarch_info (info
);
3410 /* In theory a binary compiled for RV32 could run on an RV64 target,
3411 however, this has not been tested in GDB yet, so for now we require
3412 that the requested xlen match the targets xlen. */
3413 if (abi_features
.xlen
!= 0 && abi_features
.xlen
!= features
.xlen
)
3414 error (_("bfd requires xlen %d, but target has xlen %d"),
3415 abi_features
.xlen
, features
.xlen
);
3416 /* We do support running binaries compiled for 32-bit float on targets
3417 with 64-bit float, so we only complain if the binary requires more
3418 than the target has available. */
3419 if (abi_features
.flen
> features
.flen
)
3420 error (_("bfd requires flen %d, but target has flen %d"),
3421 abi_features
.flen
, features
.flen
);
3423 /* If the ABI_FEATURES xlen is 0 then this indicates we got no useful abi
3424 features from the INFO object. In this case we assume that the xlen
3425 abi matches the hardware. */
3426 if (abi_features
.xlen
== 0)
3427 abi_features
.xlen
= features
.xlen
;
3429 /* Find a candidate among the list of pre-declared architectures. */
3430 for (arches
= gdbarch_list_lookup_by_info (arches
, &info
);
3432 arches
= gdbarch_list_lookup_by_info (arches
->next
, &info
))
3434 /* Check that the feature set of the ARCHES matches the feature set
3435 we are looking for. If it doesn't then we can't reuse this
3437 struct gdbarch_tdep
*other_tdep
= gdbarch_tdep (arches
->gdbarch
);
3439 if (other_tdep
->isa_features
!= features
3440 || other_tdep
->abi_features
!= abi_features
)
3447 return arches
->gdbarch
;
3449 /* None found, so create a new architecture from the information provided. */
3450 tdep
= new (struct gdbarch_tdep
);
3451 gdbarch
= gdbarch_alloc (&info
, tdep
);
3452 tdep
->isa_features
= features
;
3453 tdep
->abi_features
= abi_features
;
3455 /* Target data types. */
3456 set_gdbarch_short_bit (gdbarch
, 16);
3457 set_gdbarch_int_bit (gdbarch
, 32);
3458 set_gdbarch_long_bit (gdbarch
, riscv_isa_xlen (gdbarch
) * 8);
3459 set_gdbarch_long_long_bit (gdbarch
, 64);
3460 set_gdbarch_float_bit (gdbarch
, 32);
3461 set_gdbarch_double_bit (gdbarch
, 64);
3462 set_gdbarch_long_double_bit (gdbarch
, 128);
3463 set_gdbarch_long_double_format (gdbarch
, floatformats_ia64_quad
);
3464 set_gdbarch_ptr_bit (gdbarch
, riscv_isa_xlen (gdbarch
) * 8);
3465 set_gdbarch_char_signed (gdbarch
, 0);
3466 set_gdbarch_type_align (gdbarch
, riscv_type_align
);
3468 /* Information about the target architecture. */
3469 set_gdbarch_return_value (gdbarch
, riscv_return_value
);
3470 set_gdbarch_breakpoint_kind_from_pc (gdbarch
, riscv_breakpoint_kind_from_pc
);
3471 set_gdbarch_sw_breakpoint_from_kind (gdbarch
, riscv_sw_breakpoint_from_kind
);
3472 set_gdbarch_have_nonsteppable_watchpoint (gdbarch
, 1);
3474 /* Functions to analyze frames. */
3475 set_gdbarch_skip_prologue (gdbarch
, riscv_skip_prologue
);
3476 set_gdbarch_inner_than (gdbarch
, core_addr_lessthan
);
3477 set_gdbarch_frame_align (gdbarch
, riscv_frame_align
);
3479 /* Functions handling dummy frames. */
3480 set_gdbarch_call_dummy_location (gdbarch
, ON_STACK
);
3481 set_gdbarch_push_dummy_code (gdbarch
, riscv_push_dummy_code
);
3482 set_gdbarch_push_dummy_call (gdbarch
, riscv_push_dummy_call
);
3484 /* Frame unwinders. Use DWARF debug info if available, otherwise use our own
3486 dwarf2_append_unwinders (gdbarch
);
3487 frame_unwind_append_unwinder (gdbarch
, &riscv_frame_unwind
);
3489 /* Register architecture. */
3490 riscv_add_reggroups (gdbarch
);
3492 /* Internal <-> external register number maps. */
3493 set_gdbarch_dwarf2_reg_to_regnum (gdbarch
, riscv_dwarf_reg_to_regnum
);
3495 /* We reserve all possible register numbers for the known registers.
3496 This means the target description mechanism will add any target
3497 specific registers after this number. This helps make debugging GDB
3498 just a little easier. */
3499 set_gdbarch_num_regs (gdbarch
, RISCV_LAST_REGNUM
+ 1);
3501 /* We don't have to provide the count of 0 here (its the default) but
3502 include this line to make it explicit that, right now, we don't have
3503 any pseudo registers on RISC-V. */
3504 set_gdbarch_num_pseudo_regs (gdbarch
, 0);
3506 /* Some specific register numbers GDB likes to know about. */
3507 set_gdbarch_sp_regnum (gdbarch
, RISCV_SP_REGNUM
);
3508 set_gdbarch_pc_regnum (gdbarch
, RISCV_PC_REGNUM
);
3510 set_gdbarch_print_registers_info (gdbarch
, riscv_print_registers_info
);
3512 /* Finalise the target description registers. */
3513 tdesc_use_registers (gdbarch
, tdesc
, std::move (tdesc_data
),
3514 riscv_tdesc_unknown_reg
);
3516 /* Override the register type callback setup by the target description
3517 mechanism. This allows us to provide special type for floating point
3519 set_gdbarch_register_type (gdbarch
, riscv_register_type
);
3521 /* Override the register name callback setup by the target description
3522 mechanism. This allows us to force our preferred names for the
3523 registers, no matter what the target description called them. */
3524 set_gdbarch_register_name (gdbarch
, riscv_register_name
);
3526 /* Override the register group callback setup by the target description
3527 mechanism. This allows us to force registers into the groups we
3528 want, ignoring what the target tells us. */
3529 set_gdbarch_register_reggroup_p (gdbarch
, riscv_register_reggroup_p
);
3531 /* Create register aliases for alternative register names. We only
3532 create aliases for registers which were mentioned in the target
3534 for (const auto &alias
: pending_aliases
)
3535 alias
.create (gdbarch
);
3537 /* Compile command hooks. */
3538 set_gdbarch_gcc_target_options (gdbarch
, riscv_gcc_target_options
);
3539 set_gdbarch_gnu_triplet_regexp (gdbarch
, riscv_gnu_triplet_regexp
);
3541 /* Hook in OS ABI-specific overrides, if they have been registered. */
3542 gdbarch_init_osabi (info
, gdbarch
);
3544 register_riscv_ravenscar_ops (gdbarch
);
3549 /* This decodes the current instruction and determines the address of the
3550 next instruction. */
3553 riscv_next_pc (struct regcache
*regcache
, CORE_ADDR pc
)
3555 struct gdbarch
*gdbarch
= regcache
->arch ();
3556 struct riscv_insn insn
;
3559 insn
.decode (gdbarch
, pc
);
3560 next_pc
= pc
+ insn
.length ();
3562 if (insn
.opcode () == riscv_insn::JAL
)
3563 next_pc
= pc
+ insn
.imm_signed ();
3564 else if (insn
.opcode () == riscv_insn::JALR
)
3567 regcache
->cooked_read (insn
.rs1 (), &source
);
3568 next_pc
= (source
+ insn
.imm_signed ()) & ~(CORE_ADDR
) 0x1;
3570 else if (insn
.opcode () == riscv_insn::BEQ
)
3573 regcache
->cooked_read (insn
.rs1 (), &src1
);
3574 regcache
->cooked_read (insn
.rs2 (), &src2
);
3576 next_pc
= pc
+ insn
.imm_signed ();
3578 else if (insn
.opcode () == riscv_insn::BNE
)
3581 regcache
->cooked_read (insn
.rs1 (), &src1
);
3582 regcache
->cooked_read (insn
.rs2 (), &src2
);
3584 next_pc
= pc
+ insn
.imm_signed ();
3586 else if (insn
.opcode () == riscv_insn::BLT
)
3589 regcache
->cooked_read (insn
.rs1 (), &src1
);
3590 regcache
->cooked_read (insn
.rs2 (), &src2
);
3592 next_pc
= pc
+ insn
.imm_signed ();
3594 else if (insn
.opcode () == riscv_insn::BGE
)
3597 regcache
->cooked_read (insn
.rs1 (), &src1
);
3598 regcache
->cooked_read (insn
.rs2 (), &src2
);
3600 next_pc
= pc
+ insn
.imm_signed ();
3602 else if (insn
.opcode () == riscv_insn::BLTU
)
3604 ULONGEST src1
, src2
;
3605 regcache
->cooked_read (insn
.rs1 (), &src1
);
3606 regcache
->cooked_read (insn
.rs2 (), &src2
);
3608 next_pc
= pc
+ insn
.imm_signed ();
3610 else if (insn
.opcode () == riscv_insn::BGEU
)
3612 ULONGEST src1
, src2
;
3613 regcache
->cooked_read (insn
.rs1 (), &src1
);
3614 regcache
->cooked_read (insn
.rs2 (), &src2
);
3616 next_pc
= pc
+ insn
.imm_signed ();
3622 /* We can't put a breakpoint in the middle of a lr/sc atomic sequence, so look
3623 for the end of the sequence and put the breakpoint there. */
3626 riscv_next_pc_atomic_sequence (struct regcache
*regcache
, CORE_ADDR pc
,
3629 struct gdbarch
*gdbarch
= regcache
->arch ();
3630 struct riscv_insn insn
;
3631 CORE_ADDR cur_step_pc
= pc
;
3632 CORE_ADDR last_addr
= 0;
3634 /* First instruction has to be a load reserved. */
3635 insn
.decode (gdbarch
, cur_step_pc
);
3636 if (insn
.opcode () != riscv_insn::LR
)
3638 cur_step_pc
= cur_step_pc
+ insn
.length ();
3640 /* Next instruction should be branch to exit. */
3641 insn
.decode (gdbarch
, cur_step_pc
);
3642 if (insn
.opcode () != riscv_insn::BNE
)
3644 last_addr
= cur_step_pc
+ insn
.imm_signed ();
3645 cur_step_pc
= cur_step_pc
+ insn
.length ();
3647 /* Next instruction should be store conditional. */
3648 insn
.decode (gdbarch
, cur_step_pc
);
3649 if (insn
.opcode () != riscv_insn::SC
)
3651 cur_step_pc
= cur_step_pc
+ insn
.length ();
3653 /* Next instruction should be branch to start. */
3654 insn
.decode (gdbarch
, cur_step_pc
);
3655 if (insn
.opcode () != riscv_insn::BNE
)
3657 if (pc
!= (cur_step_pc
+ insn
.imm_signed ()))
3659 cur_step_pc
= cur_step_pc
+ insn
.length ();
3661 /* We should now be at the end of the sequence. */
3662 if (cur_step_pc
!= last_addr
)
3665 *next_pc
= cur_step_pc
;
3669 /* This is called just before we want to resume the inferior, if we want to
3670 single-step it but there is no hardware or kernel single-step support. We
3671 find the target of the coming instruction and breakpoint it. */
3673 std::vector
<CORE_ADDR
>
3674 riscv_software_single_step (struct regcache
*regcache
)
3676 CORE_ADDR pc
, next_pc
;
3678 pc
= regcache_read_pc (regcache
);
3680 if (riscv_next_pc_atomic_sequence (regcache
, pc
, &next_pc
))
3683 next_pc
= riscv_next_pc (regcache
, pc
);
3688 /* Create RISC-V specific reggroups. */
3691 riscv_init_reggroups ()
3693 csr_reggroup
= reggroup_new ("csr", USER_REGGROUP
);
3696 void _initialize_riscv_tdep ();
3698 _initialize_riscv_tdep ()
3700 riscv_create_csr_aliases ();
3701 riscv_init_reggroups ();
3703 gdbarch_register (bfd_arch_riscv
, riscv_gdbarch_init
, NULL
);
3705 /* Add root prefix command for all "set debug riscv" and "show debug
3707 add_basic_prefix_cmd ("riscv", no_class
,
3708 _("RISC-V specific debug commands."),
3709 &setdebugriscvcmdlist
, "set debug riscv ", 0,
3712 add_show_prefix_cmd ("riscv", no_class
,
3713 _("RISC-V specific debug commands."),
3714 &showdebugriscvcmdlist
, "show debug riscv ", 0,
3717 add_setshow_zuinteger_cmd ("breakpoints", class_maintenance
,
3718 &riscv_debug_breakpoints
, _("\
3719 Set riscv breakpoint debugging."), _("\
3720 Show riscv breakpoint debugging."), _("\
3721 When non-zero, print debugging information for the riscv specific parts\n\
3722 of the breakpoint mechanism."),
3724 show_riscv_debug_variable
,
3725 &setdebugriscvcmdlist
, &showdebugriscvcmdlist
);
3727 add_setshow_zuinteger_cmd ("infcall", class_maintenance
,
3728 &riscv_debug_infcall
, _("\
3729 Set riscv inferior call debugging."), _("\
3730 Show riscv inferior call debugging."), _("\
3731 When non-zero, print debugging information for the riscv specific parts\n\
3732 of the inferior call mechanism."),
3734 show_riscv_debug_variable
,
3735 &setdebugriscvcmdlist
, &showdebugriscvcmdlist
);
3737 add_setshow_zuinteger_cmd ("unwinder", class_maintenance
,
3738 &riscv_debug_unwinder
, _("\
3739 Set riscv stack unwinding debugging."), _("\
3740 Show riscv stack unwinding debugging."), _("\
3741 When non-zero, print debugging information for the riscv specific parts\n\
3742 of the stack unwinding mechanism."),
3744 show_riscv_debug_variable
,
3745 &setdebugriscvcmdlist
, &showdebugriscvcmdlist
);
3747 add_setshow_zuinteger_cmd ("gdbarch", class_maintenance
,
3748 &riscv_debug_gdbarch
, _("\
3749 Set riscv gdbarch initialisation debugging."), _("\
3750 Show riscv gdbarch initialisation debugging."), _("\
3751 When non-zero, print debugging information for the riscv gdbarch\n\
3752 initialisation process."),
3754 show_riscv_debug_variable
,
3755 &setdebugriscvcmdlist
, &showdebugriscvcmdlist
);
3757 /* Add root prefix command for all "set riscv" and "show riscv" commands. */
3758 add_basic_prefix_cmd ("riscv", no_class
,
3759 _("RISC-V specific commands."),
3760 &setriscvcmdlist
, "set riscv ", 0, &setlist
);
3762 add_show_prefix_cmd ("riscv", no_class
,
3763 _("RISC-V specific commands."),
3764 &showriscvcmdlist
, "show riscv ", 0, &showlist
);
3767 use_compressed_breakpoints
= AUTO_BOOLEAN_AUTO
;
3768 add_setshow_auto_boolean_cmd ("use-compressed-breakpoints", no_class
,
3769 &use_compressed_breakpoints
,
3771 Set debugger's use of compressed breakpoints."), _(" \
3772 Show debugger's use of compressed breakpoints."), _("\
3773 Debugging compressed code requires compressed breakpoints to be used. If\n\
3774 left to 'auto' then gdb will use them if the existing instruction is a\n\
3775 compressed instruction. If that doesn't give the correct behavior, then\n\
3776 this option can be used."),
3778 show_use_compressed_breakpoints
,