1 /* Target-dependent code for the RISC-V architecture, for GDB.
3 Copyright (C) 2018 Free Software Foundation, Inc.
5 Contributed by Alessandro Forin(af@cs.cmu.edu) at CMU
6 and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin
7 and by Todd Snyder <todd@bluespec.com>
8 and by Mike Frysinger <vapier@gentoo.org>.
10 This file is part of GDB.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>. */
37 #include "arch-utils.h"
40 #include "riscv-tdep.h"
42 #include "reggroups.h"
43 #include "opcode/riscv.h"
44 #include "elf/riscv.h"
48 #include "frame-unwind.h"
49 #include "frame-base.h"
50 #include "trad-frame.h"
52 #include "floatformat.h"
54 #include "target-descriptions.h"
55 #include "dwarf2-frame.h"
56 #include "user-regs.h"
58 #include "common-defs.h"
59 #include "opcode/riscv-opc.h"
60 #include "cli/cli-decode.h"
63 /* The stack must be 16-byte aligned. */
64 #define SP_ALIGNMENT 16
66 /* Forward declarations. */
67 static bool riscv_has_feature (struct gdbarch *gdbarch, char feature);
68 struct riscv_inferior_data;
69 struct riscv_inferior_data * riscv_inferior_data (struct inferior *const inf);
71 /* Define a series of is_XXX_insn functions to check if the value INSN
72 is an instance of instruction XXX. */
73 #define DECLARE_INSN(INSN_NAME, INSN_MATCH, INSN_MASK) \
74 static inline bool is_ ## INSN_NAME ## _insn (long insn) \
76 return (insn & INSN_MASK) == INSN_MATCH; \
78 #include "opcode/riscv-opc.h"
81 /* Per inferior information for RiscV. */
83 struct riscv_inferior_data
85 /* True when MISA_VALUE is valid, otherwise false. */
88 /* If MISA_READ is true then MISA_VALUE holds the value of the MISA
89 register read from the target. */
93 /* Key created when the RiscV per-inferior data is registered. */
95 static const struct inferior_data *riscv_inferior_data_reg;
97 /* Architectural name for core registers. */
99 static const char * const riscv_gdb_reg_names[RISCV_LAST_FP_REGNUM + 1] =
101 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
102 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
103 "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
104 "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31",
106 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
107 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
108 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
109 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
112 /* Maps "pretty" register names onto their GDB register number. */
114 struct register_alias
116 /* The register alias. Usually more descriptive than the
117 architectural name of the register. */
120 /* The GDB register number. */
124 /* Table of register aliases. */
126 static const struct register_alias riscv_register_aliases[] =
194 #define DECLARE_CSR(name, num) { #name, (num) + 65 },
195 #include "opcode/riscv-opc.h"
199 /* Controls whether we place compressed breakpoints or not. When in auto
200 mode GDB tries to determine if the target supports compressed
201 breakpoints, and uses them if it does. */
203 static enum auto_boolean use_compressed_breakpoints;
205 /* The show callback for 'show riscv use-compressed-breakpoints'. */
208 show_use_compressed_breakpoints (struct ui_file *file, int from_tty,
209 struct cmd_list_element *c,
212 const char *additional_info;
213 struct gdbarch *gdbarch = target_gdbarch ();
215 if (use_compressed_breakpoints == AUTO_BOOLEAN_AUTO)
216 if (riscv_has_feature (gdbarch, 'C'))
217 additional_info = _(" (currently on)");
219 additional_info = _(" (currently off)");
221 additional_info = "";
223 fprintf_filtered (file,
224 _("Debugger's use of compressed breakpoints is set "
225 "to %s%s.\n"), value, additional_info);
228 /* The set and show lists for 'set riscv' and 'show riscv' prefixes. */
230 static struct cmd_list_element *setriscvcmdlist = NULL;
231 static struct cmd_list_element *showriscvcmdlist = NULL;
233 /* The show callback for the 'show riscv' prefix command. */
236 show_riscv_command (const char *args, int from_tty)
238 help_list (showriscvcmdlist, "show riscv ", all_commands, gdb_stdout);
241 /* The set callback for the 'set riscv' prefix command. */
244 set_riscv_command (const char *args, int from_tty)
247 (_("\"set riscv\" must be followed by an appropriate subcommand.\n"));
248 help_list (setriscvcmdlist, "set riscv ", all_commands, gdb_stdout);
251 /* The set and show lists for 'set riscv' and 'show riscv' prefixes. */
253 static struct cmd_list_element *setdebugriscvcmdlist = NULL;
254 static struct cmd_list_element *showdebugriscvcmdlist = NULL;
256 /* The show callback for the 'show debug riscv' prefix command. */
259 show_debug_riscv_command (const char *args, int from_tty)
261 help_list (showdebugriscvcmdlist, "show debug riscv ", all_commands, gdb_stdout);
264 /* The set callback for the 'set debug riscv' prefix command. */
267 set_debug_riscv_command (const char *args, int from_tty)
270 (_("\"set debug riscv\" must be followed by an appropriate subcommand.\n"));
271 help_list (setdebugriscvcmdlist, "set debug riscv ", all_commands, gdb_stdout);
274 /* The show callback for all 'show debug riscv VARNAME' variables. */
277 show_riscv_debug_variable (struct ui_file *file, int from_tty,
278 struct cmd_list_element *c,
281 fprintf_filtered (file,
282 _("RiscV debug variable `%s' is set to: %s\n"),
286 /* When this is set to non-zero debugging information about inferior calls
289 static unsigned int riscv_debug_infcall = 0;
291 /* Read the MISA register from the target. The register will only be read
292 once, and the value read will be cached. If the register can't be read
293 from the target then a default value (0) will be returned. If the
294 pointer READ_P is not null, then the bool pointed to is updated to
295 indicate if the value returned was read from the target (true) or is the
299 riscv_read_misa_reg (bool *read_p)
301 struct riscv_inferior_data *inf_data
302 = riscv_inferior_data (current_inferior ());
304 if (!inf_data->misa_read && target_has_registers)
307 struct frame_info *frame = get_current_frame ();
311 value = get_frame_register_unsigned (frame, RISCV_CSR_MISA_REGNUM);
313 CATCH (ex, RETURN_MASK_ERROR)
315 /* Old cores might have MISA located at a different offset. */
316 value = get_frame_register_unsigned (frame,
317 RISCV_CSR_LEGACY_MISA_REGNUM);
321 inf_data->misa_read = true;
322 inf_data->misa_value = value;
325 if (read_p != nullptr)
326 *read_p = inf_data->misa_read;
328 return inf_data->misa_value;
331 /* Return true if FEATURE is available for the architecture GDBARCH. The
332 FEATURE should be one of the single character feature codes described in
333 the RiscV ISA manual, these are between 'A' and 'Z'. */
336 riscv_has_feature (struct gdbarch *gdbarch, char feature)
338 bool have_read_misa = false;
341 gdb_assert (feature >= 'A' && feature <= 'Z');
343 /* It would be nice to always check with the real target where possible,
344 however, for compressed instructions this is a bad idea.
346 The call to `set_gdbarch_decr_pc_after_break' is made just once per
347 GDBARCH and we decide at that point if we should decrement by 2 or 4
348 bytes based on whether the BFD has compressed instruction support or
351 If the BFD was not compiled with compressed instruction support, but we
352 are running on a target with compressed instructions then we might
353 place a 4-byte breakpoint, then decrement the $pc by 2 bytes leading to
356 It's safer if we just make decisions about compressed instruction
357 support based on the BFD. */
359 misa = riscv_read_misa_reg (&have_read_misa);
360 if (!have_read_misa || misa == 0)
361 misa = gdbarch_tdep (gdbarch)->core_features;
363 return (misa & (1 << (feature - 'A'))) != 0;
366 /* Return the width in bytes of the general purpose registers for GDBARCH.
367 Possible return values are 4, 8, or 16 for RiscV variants RV32, RV64, or
371 riscv_isa_xlen (struct gdbarch *gdbarch)
373 switch (gdbarch_tdep (gdbarch)->abi.fields.base_len)
376 warning (_("unknown xlen size, assuming 4 bytes"));
386 /* Return the width in bytes of the floating point registers for GDBARCH.
387 If this architecture has no floating point registers, then return 0.
388 Possible values are 4, 8, or 16 for depending on which of single, double
389 or quad floating point support is available. */
392 riscv_isa_flen (struct gdbarch *gdbarch)
394 if (riscv_has_feature (gdbarch, 'Q'))
396 else if (riscv_has_feature (gdbarch, 'D'))
398 else if (riscv_has_feature (gdbarch, 'F'))
404 /* Return true if the target for GDBARCH has floating point hardware. */
407 riscv_has_fp_regs (struct gdbarch *gdbarch)
409 return (riscv_isa_flen (gdbarch) > 0);
412 /* Return true if GDBARCH is using any of the floating point hardware ABIs. */
415 riscv_has_fp_abi (struct gdbarch *gdbarch)
417 return (gdbarch_tdep (gdbarch)->abi.fields.float_abi != 0);
420 /* Implement the breakpoint_kind_from_pc gdbarch method. */
423 riscv_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
425 if (use_compressed_breakpoints == AUTO_BOOLEAN_AUTO)
427 if (riscv_has_feature (gdbarch, 'C'))
432 else if (use_compressed_breakpoints == AUTO_BOOLEAN_TRUE)
438 /* Implement the sw_breakpoint_from_kind gdbarch method. */
440 static const gdb_byte *
441 riscv_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
443 static const gdb_byte ebreak[] = { 0x73, 0x00, 0x10, 0x00, };
444 static const gdb_byte c_ebreak[] = { 0x02, 0x90 };
454 gdb_assert_not_reached ("unhandled breakpoint kind");
458 /* Callback function for user_reg_add. */
460 static struct value *
461 value_of_riscv_user_reg (struct frame_info *frame, const void *baton)
463 const int *reg_p = (const int *) baton;
464 return value_of_register (*reg_p, frame);
467 /* Implement the register_name gdbarch method. */
470 riscv_register_name (struct gdbarch *gdbarch, int regnum)
472 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
473 return tdesc_register_name (gdbarch, regnum);
475 /* Prefer to use the alias. */
476 if (regnum >= RISCV_ZERO_REGNUM && regnum <= RISCV_LAST_REGNUM)
480 for (i = 0; i < ARRAY_SIZE (riscv_register_aliases); ++i)
481 if (regnum == riscv_register_aliases[i].regnum)
482 return riscv_register_aliases[i].name;
485 if (regnum >= RISCV_ZERO_REGNUM && regnum <= RISCV_LAST_FP_REGNUM)
486 return riscv_gdb_reg_names[regnum];
488 if (regnum >= RISCV_FIRST_CSR_REGNUM && regnum <= RISCV_LAST_CSR_REGNUM)
492 sprintf (buf, "csr%d", regnum - RISCV_FIRST_CSR_REGNUM);
496 if (regnum == RISCV_PRIV_REGNUM)
502 /* Implement the pseudo_register_read gdbarch method. */
504 static enum register_status
505 riscv_pseudo_register_read (struct gdbarch *gdbarch,
506 readable_regcache *regcache,
510 return regcache->raw_read (regnum, buf);
513 /* Implement the pseudo_register_write gdbarch method. */
516 riscv_pseudo_register_write (struct gdbarch *gdbarch,
517 struct regcache *regcache,
521 regcache_raw_write (regcache, cookednum, buf);
524 /* Implement the register_type gdbarch method. */
527 riscv_register_type (struct gdbarch *gdbarch, int regnum)
531 if (regnum < RISCV_FIRST_FP_REGNUM)
533 if (regnum == gdbarch_pc_regnum (gdbarch)
534 || regnum == RISCV_RA_REGNUM)
535 return builtin_type (gdbarch)->builtin_func_ptr;
537 if (regnum == RISCV_FP_REGNUM
538 || regnum == RISCV_SP_REGNUM
539 || regnum == RISCV_GP_REGNUM
540 || regnum == RISCV_TP_REGNUM)
541 return builtin_type (gdbarch)->builtin_data_ptr;
543 /* Remaining GPRs vary in size based on the current ISA. */
544 regsize = riscv_isa_xlen (gdbarch);
548 return builtin_type (gdbarch)->builtin_uint32;
550 return builtin_type (gdbarch)->builtin_uint64;
552 return builtin_type (gdbarch)->builtin_uint128;
554 internal_error (__FILE__, __LINE__,
555 _("unknown isa regsize %i"), regsize);
558 else if (regnum <= RISCV_LAST_FP_REGNUM)
560 regsize = riscv_isa_xlen (gdbarch);
564 return builtin_type (gdbarch)->builtin_float;
566 return builtin_type (gdbarch)->builtin_double;
568 return builtin_type (gdbarch)->builtin_long_double;
570 internal_error (__FILE__, __LINE__,
571 _("unknown isa regsize %i"), regsize);
574 else if (regnum == RISCV_PRIV_REGNUM)
575 return builtin_type (gdbarch)->builtin_int8;
578 if (regnum == RISCV_CSR_FFLAGS_REGNUM
579 || regnum == RISCV_CSR_FRM_REGNUM
580 || regnum == RISCV_CSR_FCSR_REGNUM)
581 return builtin_type (gdbarch)->builtin_int32;
583 regsize = riscv_isa_xlen (gdbarch);
587 return builtin_type (gdbarch)->builtin_int32;
589 return builtin_type (gdbarch)->builtin_int64;
591 return builtin_type (gdbarch)->builtin_int128;
593 internal_error (__FILE__, __LINE__,
594 _("unknown isa regsize %i"), regsize);
599 /* Helper for riscv_print_registers_info, prints info for a single register
603 riscv_print_one_register_info (struct gdbarch *gdbarch,
604 struct ui_file *file,
605 struct frame_info *frame,
608 const char *name = gdbarch_register_name (gdbarch, regnum);
609 struct value *val = value_of_register (regnum, frame);
610 struct type *regtype = value_type (val);
611 int print_raw_format;
612 enum tab_stops { value_column_1 = 15 };
614 fputs_filtered (name, file);
615 print_spaces_filtered (value_column_1 - strlen (name), file);
617 print_raw_format = (value_entirely_available (val)
618 && !value_optimized_out (val));
620 if (TYPE_CODE (regtype) == TYPE_CODE_FLT)
622 struct value_print_options opts;
623 const gdb_byte *valaddr = value_contents_for_printing (val);
624 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (regtype));
626 get_user_print_options (&opts);
630 value_embedded_offset (val), 0,
631 file, 0, val, &opts, current_language);
633 if (print_raw_format)
635 fprintf_filtered (file, "\t(raw ");
636 print_hex_chars (file, valaddr, TYPE_LENGTH (regtype), byte_order,
638 fprintf_filtered (file, ")");
643 struct value_print_options opts;
645 /* Print the register in hex. */
646 get_formatted_print_options (&opts, 'x');
649 value_embedded_offset (val), 0,
650 file, 0, val, &opts, current_language);
652 if (print_raw_format)
654 if (regnum == RISCV_CSR_MSTATUS_REGNUM)
657 int size = register_size (gdbarch, regnum);
660 d = value_as_long (val);
662 fprintf_filtered (file,
663 "\tSD:%X VM:%02X MXR:%X PUM:%X MPRV:%X XS:%X "
664 "FS:%X MPP:%x HPP:%X SPP:%X MPIE:%X HPIE:%X "
665 "SPIE:%X UPIE:%X MIE:%X HIE:%X SIE:%X UIE:%X",
666 (int) ((d >> (xlen - 1)) & 0x1),
667 (int) ((d >> 24) & 0x1f),
668 (int) ((d >> 19) & 0x1),
669 (int) ((d >> 18) & 0x1),
670 (int) ((d >> 17) & 0x1),
671 (int) ((d >> 15) & 0x3),
672 (int) ((d >> 13) & 0x3),
673 (int) ((d >> 11) & 0x3),
674 (int) ((d >> 9) & 0x3),
675 (int) ((d >> 8) & 0x1),
676 (int) ((d >> 7) & 0x1),
677 (int) ((d >> 6) & 0x1),
678 (int) ((d >> 5) & 0x1),
679 (int) ((d >> 4) & 0x1),
680 (int) ((d >> 3) & 0x1),
681 (int) ((d >> 2) & 0x1),
682 (int) ((d >> 1) & 0x1),
683 (int) ((d >> 0) & 0x1));
685 else if (regnum == RISCV_CSR_MISA_REGNUM)
691 d = value_as_long (val);
695 for (; base > 0; base--)
697 fprintf_filtered (file, "\tRV%d", xlen);
699 for (i = 0; i < 26; i++)
702 fprintf_filtered (file, "%c", 'A' + i);
705 else if (regnum == RISCV_CSR_FCSR_REGNUM
706 || regnum == RISCV_CSR_FFLAGS_REGNUM
707 || regnum == RISCV_CSR_FRM_REGNUM)
711 d = value_as_long (val);
713 fprintf_filtered (file, "\t");
714 if (regnum != RISCV_CSR_FRM_REGNUM)
715 fprintf_filtered (file,
716 "RD:%01X NV:%d DZ:%d OF:%d UF:%d NX:%d",
717 (int) ((d >> 5) & 0x7),
718 (int) ((d >> 4) & 0x1),
719 (int) ((d >> 3) & 0x1),
720 (int) ((d >> 2) & 0x1),
721 (int) ((d >> 1) & 0x1),
722 (int) ((d >> 0) & 0x1));
724 if (regnum != RISCV_CSR_FFLAGS_REGNUM)
726 static const char * const sfrm[] =
728 "RNE (round to nearest; ties to even)",
729 "RTZ (Round towards zero)",
730 "RDN (Round down towards -INF)",
731 "RUP (Round up towards +INF)",
732 "RMM (Round to nearest; ties to max magnitude)",
735 "dynamic rounding mode",
737 int frm = ((regnum == RISCV_CSR_FCSR_REGNUM)
738 ? (d >> 5) : d) & 0x3;
740 fprintf_filtered (file, "%sFRM:%i [%s]",
741 (regnum == RISCV_CSR_FCSR_REGNUM
746 else if (regnum == RISCV_PRIV_REGNUM)
751 d = value_as_long (val);
756 static const char * const sprv[] =
763 fprintf_filtered (file, "\tprv:%d [%s]",
767 fprintf_filtered (file, "\tprv:%d [INVALID]", priv);
771 /* If not a vector register, print it also according to its
773 if (TYPE_VECTOR (regtype) == 0)
775 get_user_print_options (&opts);
777 fprintf_filtered (file, "\t");
779 value_embedded_offset (val), 0,
780 file, 0, val, &opts, current_language);
785 fprintf_filtered (file, "\n");
788 /* Implement the register_reggroup_p gdbarch method. Is REGNUM a member
792 riscv_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
793 struct reggroup *reggroup)
799 /* Used by 'info registers' and 'info registers <groupname>'. */
801 if (gdbarch_register_name (gdbarch, regnum) == NULL
802 || gdbarch_register_name (gdbarch, regnum)[0] == '\0')
805 if (reggroup == all_reggroup)
807 if (regnum < RISCV_FIRST_CSR_REGNUM || regnum == RISCV_PRIV_REGNUM)
809 /* Only include CSRs that have aliases. */
810 for (i = 0; i < ARRAY_SIZE (riscv_register_aliases); ++i)
812 if (regnum == riscv_register_aliases[i].regnum)
817 else if (reggroup == float_reggroup)
818 return ((regnum >= RISCV_FIRST_FP_REGNUM && regnum <= RISCV_LAST_FP_REGNUM)
819 || (regnum == RISCV_CSR_FCSR_REGNUM
820 || regnum == RISCV_CSR_FFLAGS_REGNUM
821 || regnum == RISCV_CSR_FRM_REGNUM));
822 else if (reggroup == general_reggroup)
823 return regnum < RISCV_FIRST_FP_REGNUM;
824 else if (reggroup == restore_reggroup || reggroup == save_reggroup)
826 if (riscv_has_fp_regs (gdbarch))
827 return regnum <= RISCV_LAST_FP_REGNUM;
829 return regnum < RISCV_FIRST_FP_REGNUM;
831 else if (reggroup == system_reggroup)
833 if (regnum == RISCV_PRIV_REGNUM)
835 if (regnum < RISCV_FIRST_CSR_REGNUM || regnum > RISCV_LAST_CSR_REGNUM)
837 /* Only include CSRs that have aliases. */
838 for (i = 0; i < ARRAY_SIZE (riscv_register_aliases); ++i)
840 if (regnum == riscv_register_aliases[i].regnum)
845 else if (reggroup == vector_reggroup)
851 /* Implement the print_registers_info gdbarch method. This is used by
852 'info registers' and 'info all-registers'. */
855 riscv_print_registers_info (struct gdbarch *gdbarch,
856 struct ui_file *file,
857 struct frame_info *frame,
858 int regnum, int print_all)
862 /* Print one specified register. */
863 gdb_assert (regnum <= RISCV_LAST_REGNUM);
864 if (gdbarch_register_name (gdbarch, regnum) == NULL
865 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
866 error (_("Not a valid register for the current processor type"));
867 riscv_print_one_register_info (gdbarch, file, frame, regnum);
871 struct reggroup *reggroup;
874 reggroup = all_reggroup;
876 reggroup = general_reggroup;
878 for (regnum = 0; regnum <= RISCV_LAST_REGNUM; ++regnum)
880 /* Zero never changes, so might as well hide by default. */
881 if (regnum == RISCV_ZERO_REGNUM && !print_all)
884 /* Registers with no name are not valid on this ISA. */
885 if (gdbarch_register_name (gdbarch, regnum) == NULL
886 || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
889 /* Is the register in the group we're interested in? */
890 if (!riscv_register_reggroup_p (gdbarch, regnum, reggroup))
893 riscv_print_one_register_info (gdbarch, file, frame, regnum);
898 /* Class that handles one decoded RiscV instruction. */
904 /* Enum of all the opcodes that GDB cares about during the prologue scan. */
907 /* Unknown value is used at initialisation time. */
910 /* These instructions are all the ones we are interested in during the
921 /* Other instructions are not interesting during the prologue scan, and
936 void decode (struct gdbarch *gdbarch, CORE_ADDR pc);
938 /* Get the length of the instruction in bytes. */
942 /* Get the opcode for this instruction. */
943 enum opcode opcode () const
946 /* Get destination register field for this instruction. This is only
947 valid if the OPCODE implies there is such a field for this
952 /* Get the RS1 register field for this instruction. This is only valid
953 if the OPCODE implies there is such a field for this instruction. */
957 /* Get the RS2 register field for this instruction. This is only valid
958 if the OPCODE implies there is such a field for this instruction. */
962 /* Get the immediate for this instruction in signed form. This is only
963 valid if the OPCODE implies there is such a field for this
965 int imm_signed () const
970 /* Extract 5 bit register field at OFFSET from instruction OPCODE. */
971 int decode_register_index (unsigned long opcode, int offset)
973 return (opcode >> offset) & 0x1F;
976 /* Helper for DECODE, decode 32-bit R-type instruction. */
977 void decode_r_type_insn (enum opcode opcode, ULONGEST ival)
980 m_rd = decode_register_index (ival, OP_SH_RD);
981 m_rs1 = decode_register_index (ival, OP_SH_RS1);
982 m_rs2 = decode_register_index (ival, OP_SH_RS2);
985 /* Helper for DECODE, decode 16-bit compressed R-type instruction. */
986 void decode_cr_type_insn (enum opcode opcode, ULONGEST ival)
989 m_rd = m_rs1 = decode_register_index (ival, OP_SH_CRS1S);
990 m_rs2 = decode_register_index (ival, OP_SH_CRS2);
993 /* Helper for DECODE, decode 32-bit I-type instruction. */
994 void decode_i_type_insn (enum opcode opcode, ULONGEST ival)
997 m_rd = decode_register_index (ival, OP_SH_RD);
998 m_rs1 = decode_register_index (ival, OP_SH_RS1);
999 m_imm.s = EXTRACT_ITYPE_IMM (ival);
1002 /* Helper for DECODE, decode 16-bit compressed I-type instruction. */
1003 void decode_ci_type_insn (enum opcode opcode, ULONGEST ival)
1006 m_rd = m_rs1 = decode_register_index (ival, OP_SH_CRS1S);
1007 m_imm.s = EXTRACT_RVC_IMM (ival);
1010 /* Helper for DECODE, decode 32-bit S-type instruction. */
1011 void decode_s_type_insn (enum opcode opcode, ULONGEST ival)
1014 m_rs1 = decode_register_index (ival, OP_SH_RS1);
1015 m_rs2 = decode_register_index (ival, OP_SH_RS2);
1016 m_imm.s = EXTRACT_STYPE_IMM (ival);
1019 /* Helper for DECODE, decode 32-bit U-type instruction. */
1020 void decode_u_type_insn (enum opcode opcode, ULONGEST ival)
1023 m_rd = decode_register_index (ival, OP_SH_RD);
1024 m_imm.s = EXTRACT_UTYPE_IMM (ival);
1027 /* Fetch instruction from target memory at ADDR, return the content of
1028 the instruction, and update LEN with the instruction length. */
1029 static ULONGEST fetch_instruction (struct gdbarch *gdbarch,
1030 CORE_ADDR addr, int *len);
1032 /* The length of the instruction in bytes. Should be 2 or 4. */
1035 /* The instruction opcode. */
1036 enum opcode m_opcode;
1038 /* The three possible registers an instruction might reference. Not
1039 every instruction fills in all of these registers. Which fields are
1040 valid depends on the opcode. The naming of these fields matches the
1041 naming in the riscv isa manual. */
1046 /* Possible instruction immediate. This is only valid if the instruction
1047 format contains an immediate, not all instruction, whether this is
1048 valid depends on the opcode. Despite only having one format for now
1049 the immediate is packed into a union, later instructions might require
1050 an unsigned formatted immediate, having the union in place now will
1051 reduce the need for code churn later. */
1052 union riscv_insn_immediate
1054 riscv_insn_immediate ()
1064 /* Fetch instruction from target memory at ADDR, return the content of the
1065 instruction, and update LEN with the instruction length. */
1068 riscv_insn::fetch_instruction (struct gdbarch *gdbarch,
1069 CORE_ADDR addr, int *len)
1071 enum bfd_endian byte_order = gdbarch_byte_order_for_code (gdbarch);
1073 int instlen, status;
1075 /* All insns are at least 16 bits. */
1076 status = target_read_memory (addr, buf, 2);
1078 memory_error (TARGET_XFER_E_IO, addr);
1080 /* If we need more, grab it now. */
1081 instlen = riscv_insn_length (buf[0]);
1083 if (instlen > sizeof (buf))
1084 internal_error (__FILE__, __LINE__,
1085 _("%s: riscv_insn_length returned %i"),
1087 else if (instlen > 2)
1089 status = target_read_memory (addr + 2, buf + 2, instlen - 2);
1091 memory_error (TARGET_XFER_E_IO, addr + 2);
1094 return extract_unsigned_integer (buf, instlen, byte_order);
1097 /* Fetch from target memory an instruction at PC and decode it. */
1100 riscv_insn::decode (struct gdbarch *gdbarch, CORE_ADDR pc)
1104 /* Fetch the instruction, and the instructions length. */
1105 ival = fetch_instruction (gdbarch, pc, &m_length);
1109 if (is_add_insn (ival))
1110 decode_r_type_insn (ADD, ival);
1111 else if (is_addw_insn (ival))
1112 decode_r_type_insn (ADDW, ival);
1113 else if (is_addi_insn (ival))
1114 decode_i_type_insn (ADDI, ival);
1115 else if (is_addiw_insn (ival))
1116 decode_i_type_insn (ADDIW, ival);
1117 else if (is_auipc_insn (ival))
1118 decode_u_type_insn (AUIPC, ival);
1119 else if (is_lui_insn (ival))
1120 decode_u_type_insn (LUI, ival);
1121 else if (is_sd_insn (ival))
1122 decode_s_type_insn (SD, ival);
1123 else if (is_sw_insn (ival))
1124 decode_s_type_insn (SW, ival);
1126 /* None of the other fields are valid in this case. */
1129 else if (m_length == 2)
1131 if (is_c_add_insn (ival))
1132 decode_cr_type_insn (ADD, ival);
1133 else if (is_c_addw_insn (ival))
1134 decode_cr_type_insn (ADDW, ival);
1135 else if (is_c_addi_insn (ival))
1136 decode_ci_type_insn (ADDI, ival);
1137 else if (is_c_addiw_insn (ival))
1138 decode_ci_type_insn (ADDIW, ival);
1139 else if (is_c_addi16sp_insn (ival))
1142 m_rd = m_rs1 = decode_register_index (ival, OP_SH_RD);
1143 m_imm.s = EXTRACT_RVC_ADDI16SP_IMM (ival);
1145 else if (is_lui_insn (ival))
1147 else if (is_c_sd_insn (ival))
1149 else if (is_sw_insn (ival))
1152 /* None of the other fields of INSN are valid in this case. */
1156 internal_error (__FILE__, __LINE__,
1157 _("unable to decode %d byte instructions in "
1158 "prologue at %s"), m_length,
1159 core_addr_to_string (pc));
1162 /* The prologue scanner. This is currently only used for skipping the
1163 prologue of a function when the DWARF information is not sufficient.
1164 However, it is written with filling of the frame cache in mind, which
1165 is why different groups of stack setup instructions are split apart
1166 during the core of the inner loop. In the future, the intention is to
1167 extend this function to fully support building up a frame cache that
1168 can unwind register values when there is no DWARF information. */
1171 riscv_scan_prologue (struct gdbarch *gdbarch,
1172 CORE_ADDR start_pc, CORE_ADDR limit_pc)
1174 CORE_ADDR cur_pc, next_pc;
1175 long frame_offset = 0;
1176 CORE_ADDR end_prologue_addr = 0;
1178 if (limit_pc > start_pc + 200)
1179 limit_pc = start_pc + 200;
1181 for (next_pc = cur_pc = start_pc; cur_pc < limit_pc; cur_pc = next_pc)
1183 struct riscv_insn insn;
1185 /* Decode the current instruction, and decide where the next
1186 instruction lives based on the size of this instruction. */
1187 insn.decode (gdbarch, cur_pc);
1188 gdb_assert (insn.length () > 0);
1189 next_pc = cur_pc + insn.length ();
1191 /* Look for common stack adjustment insns. */
1192 if ((insn.opcode () == riscv_insn::ADDI
1193 || insn.opcode () == riscv_insn::ADDIW)
1194 && insn.rd () == RISCV_SP_REGNUM
1195 && insn.rs1 () == RISCV_SP_REGNUM)
1197 /* Handle: addi sp, sp, -i
1198 or: addiw sp, sp, -i */
1199 if (insn.imm_signed () < 0)
1200 frame_offset += insn.imm_signed ();
1204 else if ((insn.opcode () == riscv_insn::SW
1205 || insn.opcode () == riscv_insn::SD)
1206 && (insn.rs1 () == RISCV_SP_REGNUM
1207 || insn.rs1 () == RISCV_FP_REGNUM))
1209 /* Handle: sw reg, offset(sp)
1210 or: sd reg, offset(sp)
1211 or: sw reg, offset(s0)
1212 or: sd reg, offset(s0) */
1213 /* Instruction storing a register onto the stack. */
1215 else if (insn.opcode () == riscv_insn::ADDI
1216 && insn.rd () == RISCV_FP_REGNUM
1217 && insn.rs1 () == RISCV_SP_REGNUM)
1219 /* Handle: addi s0, sp, size */
1220 /* Instructions setting up the frame pointer. */
1222 else if ((insn.opcode () == riscv_insn::ADD
1223 || insn.opcode () == riscv_insn::ADDW)
1224 && insn.rd () == RISCV_FP_REGNUM
1225 && insn.rs1 () == RISCV_SP_REGNUM
1226 && insn.rs2 () == RISCV_ZERO_REGNUM)
1228 /* Handle: add s0, sp, 0
1229 or: addw s0, sp, 0 */
1230 /* Instructions setting up the frame pointer. */
1232 else if ((insn.rd () == RISCV_GP_REGNUM
1233 && (insn.opcode () == riscv_insn::AUIPC
1234 || insn.opcode () == riscv_insn::LUI
1235 || (insn.opcode () == riscv_insn::ADDI
1236 && insn.rs1 () == RISCV_GP_REGNUM)
1237 || (insn.opcode () == riscv_insn::ADD
1238 && (insn.rs1 () == RISCV_GP_REGNUM
1239 || insn.rs2 () == RISCV_GP_REGNUM))))
1240 || (insn.opcode () == riscv_insn::ADDI
1241 && insn.rd () == RISCV_ZERO_REGNUM
1242 && insn.rs1 () == RISCV_ZERO_REGNUM
1243 && insn.imm_signed () == 0))
1245 /* Handle: auipc gp, n
1250 or: add x0, x0, 0 (NOP) */
1251 /* These instructions are part of the prologue, but we don't need
1252 to do anything special to handle them. */
1256 if (end_prologue_addr == 0)
1257 end_prologue_addr = cur_pc;
1261 if (end_prologue_addr == 0)
1262 end_prologue_addr = cur_pc;
1264 return end_prologue_addr;
1267 /* Implement the riscv_skip_prologue gdbarch method. */
1270 riscv_skip_prologue (struct gdbarch *gdbarch,
1274 CORE_ADDR func_addr;
1276 /* See if we can determine the end of the prologue via the symbol
1277 table. If so, then return either PC, or the PC after the
1278 prologue, whichever is greater. */
1279 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
1281 CORE_ADDR post_prologue_pc
1282 = skip_prologue_using_sal (gdbarch, func_addr);
1284 if (post_prologue_pc != 0)
1285 return std::max (pc, post_prologue_pc);
1288 /* Can't determine prologue from the symbol table, need to examine
1291 /* Find an upper limit on the function prologue using the debug
1292 information. If the debug information could not be used to provide
1293 that bound, then use an arbitrary large number as the upper bound. */
1294 limit_pc = skip_prologue_using_sal (gdbarch, pc);
1296 limit_pc = pc + 100; /* MAGIC! */
1298 return riscv_scan_prologue (gdbarch, pc, limit_pc);
1301 /* Implement the gdbarch push dummy code callback. */
1304 riscv_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
1305 CORE_ADDR funaddr, struct value **args, int nargs,
1306 struct type *value_type, CORE_ADDR *real_pc,
1307 CORE_ADDR *bp_addr, struct regcache *regcache)
1309 /* Allocate space for a breakpoint, and keep the stack correctly
1317 /* Compute the alignment of the type T. Used while setting up the
1318 arguments for a dummy call. */
1321 riscv_type_alignment (struct type *t)
1323 t = check_typedef (t);
1324 switch (TYPE_CODE (t))
1327 error (_("Could not compute alignment of type"));
1329 case TYPE_CODE_RVALUE_REF:
1331 case TYPE_CODE_ENUM:
1335 case TYPE_CODE_CHAR:
1336 case TYPE_CODE_BOOL:
1337 return TYPE_LENGTH (t);
1339 case TYPE_CODE_ARRAY:
1340 case TYPE_CODE_COMPLEX:
1341 return riscv_type_alignment (TYPE_TARGET_TYPE (t));
1343 case TYPE_CODE_STRUCT:
1344 case TYPE_CODE_UNION:
1349 for (i = 0; i < TYPE_NFIELDS (t); ++i)
1351 if (TYPE_FIELD_LOC_KIND (t, i) == FIELD_LOC_KIND_BITPOS)
1353 int a = riscv_type_alignment (TYPE_FIELD_TYPE (t, i));
1363 /* Holds information about a single argument either being passed to an
1364 inferior function, or returned from an inferior function. This includes
1365 information about the size, type, etc of the argument, and also
1366 information about how the argument will be passed (or returned). */
1368 struct riscv_arg_info
1370 /* Contents of the argument. */
1371 const gdb_byte *contents;
1373 /* Length of argument. */
1376 /* Alignment required for an argument of this type. */
1379 /* The type for this argument. */
1382 /* Each argument can have either 1 or 2 locations assigned to it. Each
1383 location describes where part of the argument will be placed. The
1384 second location is valid based on the LOC_TYPE and C_LENGTH fields
1385 of the first location (which is always valid). */
1388 /* What type of location this is. */
1391 /* Argument passed in a register. */
1394 /* Argument passed as an on stack argument. */
1397 /* Argument passed by reference. The second location is always
1398 valid for a BY_REF argument, and describes where the address
1399 of the BY_REF argument should be placed. */
1403 /* Information that depends on the location type. */
1406 /* Which register number to use. */
1409 /* The offset into the stack region. */
1413 /* The length of contents covered by this location. If this is less
1414 than the total length of the argument, then the second location
1415 will be valid, and will describe where the rest of the argument
1419 /* The offset within CONTENTS for this part of the argument. Will
1420 always be 0 for the first part. For the second part of the
1421 argument, this might be the C_LENGTH value of the first part,
1422 however, if we are passing a structure in two registers, and there's
1423 is padding between the first and second field, then this offset
1424 might be greater than the length of the first argument part. When
1425 the second argument location is not holding part of the argument
1426 value, but is instead holding the address of a reference argument,
1427 then this offset will be set to 0. */
1432 /* Information about a set of registers being used for passing arguments as
1433 part of a function call. The register set must be numerically
1434 sequential from NEXT_REGNUM to LAST_REGNUM. The register set can be
1435 disabled from use by setting NEXT_REGNUM greater than LAST_REGNUM. */
1437 struct riscv_arg_reg
1439 riscv_arg_reg (int first, int last)
1440 : next_regnum (first),
1446 /* The GDB register number to use in this set. */
1449 /* The last GDB register number to use in this set. */
1453 /* Arguments can be passed as on stack arguments, or by reference. The
1454 on stack arguments must be in a continuous region starting from $sp,
1455 while the by reference arguments can be anywhere, but we'll put them
1456 on the stack after (at higher address) the on stack arguments.
1458 This might not be the right approach to take. The ABI is clear that
1459 an argument passed by reference can be modified by the callee, which
1460 us placing the argument (temporarily) onto the stack will not achieve
1461 (changes will be lost). There's also the possibility that very large
1462 arguments could overflow the stack.
1464 This struct is used to track offset into these two areas for where
1465 arguments are to be placed. */
1466 struct riscv_memory_offsets
1468 riscv_memory_offsets ()
1475 /* Offset into on stack argument area. */
1478 /* Offset into the pass by reference area. */
1482 /* Holds information about where arguments to a call will be placed. This
1483 is updated as arguments are added onto the call, and can be used to
1484 figure out where the next argument should be placed. */
1486 struct riscv_call_info
1488 riscv_call_info (struct gdbarch *gdbarch)
1489 : int_regs (RISCV_A0_REGNUM, RISCV_A0_REGNUM + 7),
1490 float_regs (RISCV_FA0_REGNUM, RISCV_FA0_REGNUM + 7)
1492 xlen = riscv_isa_xlen (gdbarch);
1493 flen = riscv_isa_flen (gdbarch);
1495 /* Disable use of floating point registers if needed. */
1496 if (!riscv_has_fp_abi (gdbarch))
1497 float_regs.next_regnum = float_regs.last_regnum + 1;
1500 /* Track the memory areas used for holding in-memory arguments to a
1502 struct riscv_memory_offsets memory;
1504 /* Holds information about the next integer register to use for passing
1506 struct riscv_arg_reg int_regs;
1508 /* Holds information about the next floating point register to use for
1509 passing an argument. */
1510 struct riscv_arg_reg float_regs;
1512 /* The XLEN and FLEN are copied in to this structure for convenience, and
1513 are just the results of calling RISCV_ISA_XLEN and RISCV_ISA_FLEN. */
1518 /* Return the number of registers available for use as parameters in the
1519 register set REG. Returned value can be 0 or more. */
1522 riscv_arg_regs_available (struct riscv_arg_reg *reg)
1524 if (reg->next_regnum > reg->last_regnum)
1527 return (reg->last_regnum - reg->next_regnum + 1);
1530 /* If there is at least one register available in the register set REG then
1531 the next register from REG is assigned to LOC and the length field of
1532 LOC is updated to LENGTH. The register set REG is updated to indicate
1533 that the assigned register is no longer available and the function
1536 If there are no registers available in REG then the function returns
1537 false, and LOC and REG are unchanged. */
1540 riscv_assign_reg_location (struct riscv_arg_info::location *loc,
1541 struct riscv_arg_reg *reg,
1542 int length, int offset)
1544 if (reg->next_regnum <= reg->last_regnum)
1546 loc->loc_type = riscv_arg_info::location::in_reg;
1547 loc->loc_data.regno = reg->next_regnum;
1549 loc->c_length = length;
1550 loc->c_offset = offset;
1557 /* Assign LOC a location as the next stack parameter, and update MEMORY to
1558 record that an area of stack has been used to hold the parameter
1561 The length field of LOC is updated to LENGTH, the length of the
1562 parameter being stored, and ALIGN is the alignment required by the
1563 parameter, which will affect how memory is allocated out of MEMORY. */
1566 riscv_assign_stack_location (struct riscv_arg_info::location *loc,
1567 struct riscv_memory_offsets *memory,
1568 int length, int align)
1570 loc->loc_type = riscv_arg_info::location::on_stack;
1572 = align_up (memory->arg_offset, align);
1573 loc->loc_data.offset = memory->arg_offset;
1574 memory->arg_offset += length;
1575 loc->c_length = length;
1577 /* Offset is always 0, either we're the first location part, in which
1578 case we're reading content from the start of the argument, or we're
1579 passing the address of a reference argument, so 0. */
1583 /* Update AINFO, which describes an argument that should be passed or
1584 returned using the integer ABI. The argloc fields within AINFO are
1585 updated to describe the location in which the argument will be passed to
1586 a function, or returned from a function.
1588 The CINFO structure contains the ongoing call information, the holds
1589 information such as which argument registers are remaining to be
1590 assigned to parameter, and how much memory has been used by parameters
1593 By examining the state of CINFO a suitable location can be selected,
1594 and assigned to AINFO. */
1597 riscv_call_arg_scalar_int (struct riscv_arg_info *ainfo,
1598 struct riscv_call_info *cinfo)
1600 if (ainfo->length > (2 * cinfo->xlen))
1602 /* Argument is going to be passed by reference. */
1603 ainfo->argloc[0].loc_type
1604 = riscv_arg_info::location::by_ref;
1605 cinfo->memory.ref_offset
1606 = align_up (cinfo->memory.ref_offset, ainfo->align);
1607 ainfo->argloc[0].loc_data.offset = cinfo->memory.ref_offset;
1608 cinfo->memory.ref_offset += ainfo->length;
1609 ainfo->argloc[0].c_length = ainfo->length;
1611 /* The second location for this argument is given over to holding the
1612 address of the by-reference data. Pass 0 for the offset as this
1613 is not part of the actual argument value. */
1614 if (!riscv_assign_reg_location (&ainfo->argloc[1],
1617 riscv_assign_stack_location (&ainfo->argloc[1],
1618 &cinfo->memory, cinfo->xlen,
1623 int len = (ainfo->length > cinfo->xlen) ? cinfo->xlen : ainfo->length;
1625 if (!riscv_assign_reg_location (&ainfo->argloc[0],
1626 &cinfo->int_regs, len, 0))
1627 riscv_assign_stack_location (&ainfo->argloc[0],
1628 &cinfo->memory, len, ainfo->align);
1630 if (len < ainfo->length)
1632 len = ainfo->length - len;
1633 if (!riscv_assign_reg_location (&ainfo->argloc[1],
1634 &cinfo->int_regs, len,
1636 riscv_assign_stack_location (&ainfo->argloc[1],
1637 &cinfo->memory, len, cinfo->xlen);
1642 /* Like RISCV_CALL_ARG_SCALAR_INT, except the argument described by AINFO
1643 is being passed with the floating point ABI. */
1646 riscv_call_arg_scalar_float (struct riscv_arg_info *ainfo,
1647 struct riscv_call_info *cinfo)
1649 if (ainfo->length > cinfo->flen)
1650 return riscv_call_arg_scalar_int (ainfo, cinfo);
1653 if (!riscv_assign_reg_location (&ainfo->argloc[0],
1656 return riscv_call_arg_scalar_int (ainfo, cinfo);
1660 /* Like RISCV_CALL_ARG_SCALAR_INT, except the argument described by AINFO
1661 is a complex floating point argument, and is therefore handled
1662 differently to other argument types. */
1665 riscv_call_arg_complex_float (struct riscv_arg_info *ainfo,
1666 struct riscv_call_info *cinfo)
1668 if (ainfo->length <= (2 * cinfo->flen)
1669 && riscv_arg_regs_available (&cinfo->float_regs) >= 2)
1672 int len = ainfo->length / 2;
1674 result = riscv_assign_reg_location (&ainfo->argloc[0],
1675 &cinfo->float_regs, len, len);
1676 gdb_assert (result);
1678 result = riscv_assign_reg_location (&ainfo->argloc[1],
1679 &cinfo->float_regs, len, len);
1680 gdb_assert (result);
1683 return riscv_call_arg_scalar_int (ainfo, cinfo);
1686 /* A structure used for holding information about a structure type within
1687 the inferior program. The RiscV ABI has special rules for handling some
1688 structures with a single field or with two fields. The counting of
1689 fields here is done after flattening out all nested structures. */
1691 class riscv_struct_info
1694 riscv_struct_info ()
1695 : m_number_of_fields (0),
1696 m_types { nullptr, nullptr }
1701 /* Analyse TYPE descending into nested structures, count the number of
1702 scalar fields and record the types of the first two fields found. */
1703 void analyse (struct type *type);
1705 /* The number of scalar fields found in the analysed type. This is
1706 currently only accurate if the value returned is 0, 1, or 2 as the
1707 analysis stops counting when the number of fields is 3. This is
1708 because the RiscV ABI only has special cases for 1 or 2 fields,
1709 anything else we just don't care about. */
1710 int number_of_fields () const
1711 { return m_number_of_fields; }
1713 /* Return the type for scalar field INDEX within the analysed type. Will
1714 return nullptr if there is no field at that index. Only INDEX values
1715 0 and 1 can be requested as the RiscV ABI only has special cases for
1716 structures with 1 or 2 fields. */
1717 struct type *field_type (int index) const
1719 gdb_assert (index < (sizeof (m_types) / sizeof (m_types[0])));
1720 return m_types[index];
1724 /* The number of scalar fields found within the structure after recursing
1725 into nested structures. */
1726 int m_number_of_fields;
1728 /* The types of the first two scalar fields found within the structure
1729 after recursing into nested structures. */
1730 struct type *m_types[2];
1733 /* Analyse TYPE descending into nested structures, count the number of
1734 scalar fields and record the types of the first two fields found. */
1737 riscv_struct_info::analyse (struct type *type)
1739 unsigned int count = TYPE_NFIELDS (type);
1742 for (i = 0; i < count; ++i)
1744 if (TYPE_FIELD_LOC_KIND (type, i) != FIELD_LOC_KIND_BITPOS)
1747 struct type *field_type = TYPE_FIELD_TYPE (type, i);
1748 field_type = check_typedef (field_type);
1750 switch (TYPE_CODE (field_type))
1752 case TYPE_CODE_STRUCT:
1753 analyse (field_type);
1757 /* RiscV only flattens out structures. Anything else does not
1758 need to be flattened, we just record the type, and when we
1759 look at the analysis results we'll realise this is not a
1760 structure we can special case, and pass the structure in
1762 if (m_number_of_fields < 2)
1763 m_types[m_number_of_fields] = field_type;
1764 m_number_of_fields++;
1768 /* RiscV only has special handling for structures with 1 or 2 scalar
1769 fields, any more than that and the structure is just passed in
1770 memory. We can safely drop out early when we find 3 or more
1773 if (m_number_of_fields > 2)
1778 /* Like RISCV_CALL_ARG_SCALAR_INT, except the argument described by AINFO
1779 is a structure. Small structures on RiscV have some special case
1780 handling in order that the structure might be passed in register.
1781 Larger structures are passed in memory. After assigning location
1782 information to AINFO, CINFO will have been updated. */
1785 riscv_call_arg_struct (struct riscv_arg_info *ainfo,
1786 struct riscv_call_info *cinfo)
1788 if (riscv_arg_regs_available (&cinfo->float_regs) >= 1)
1790 struct riscv_struct_info sinfo;
1792 sinfo.analyse (ainfo->type);
1793 if (sinfo.number_of_fields () == 1
1794 && TYPE_CODE (sinfo.field_type (0)) == TYPE_CODE_COMPLEX)
1796 gdb_assert (TYPE_LENGTH (ainfo->type)
1797 == TYPE_LENGTH (sinfo.field_type (0)));
1798 return riscv_call_arg_complex_float (ainfo, cinfo);
1801 if (sinfo.number_of_fields () == 1
1802 && TYPE_CODE (sinfo.field_type (0)) == TYPE_CODE_FLT)
1804 gdb_assert (TYPE_LENGTH (ainfo->type)
1805 == TYPE_LENGTH (sinfo.field_type (0)));
1806 return riscv_call_arg_scalar_float (ainfo, cinfo);
1809 if (sinfo.number_of_fields () == 2
1810 && TYPE_CODE (sinfo.field_type (0)) == TYPE_CODE_FLT
1811 && TYPE_LENGTH (sinfo.field_type (0)) <= cinfo->flen
1812 && TYPE_CODE (sinfo.field_type (1)) == TYPE_CODE_FLT
1813 && TYPE_LENGTH (sinfo.field_type (1)) <= cinfo->flen
1814 && riscv_arg_regs_available (&cinfo->float_regs) >= 2)
1816 int len0, len1, offset;
1818 gdb_assert (TYPE_LENGTH (ainfo->type) <= (2 * cinfo->flen));
1820 len0 = TYPE_LENGTH (sinfo.field_type (0));
1821 if (!riscv_assign_reg_location (&ainfo->argloc[0],
1822 &cinfo->float_regs, len0, 0))
1823 error (_("failed during argument setup"));
1825 len1 = TYPE_LENGTH (sinfo.field_type (1));
1826 offset = align_up (len0, riscv_type_alignment (sinfo.field_type (1)));
1827 gdb_assert (len1 <= (TYPE_LENGTH (ainfo->type)
1828 - TYPE_LENGTH (sinfo.field_type (0))));
1830 if (!riscv_assign_reg_location (&ainfo->argloc[1],
1833 error (_("failed during argument setup"));
1837 if (sinfo.number_of_fields () == 2
1838 && riscv_arg_regs_available (&cinfo->int_regs) >= 1
1839 && (TYPE_CODE (sinfo.field_type (0)) == TYPE_CODE_FLT
1840 && TYPE_LENGTH (sinfo.field_type (0)) <= cinfo->flen
1841 && is_integral_type (sinfo.field_type (1))
1842 && TYPE_LENGTH (sinfo.field_type (1)) <= cinfo->xlen))
1844 int len0, len1, offset;
1846 gdb_assert (TYPE_LENGTH (ainfo->type)
1847 <= (cinfo->flen + cinfo->xlen));
1849 len0 = TYPE_LENGTH (sinfo.field_type (0));
1850 if (!riscv_assign_reg_location (&ainfo->argloc[0],
1851 &cinfo->float_regs, len0, 0))
1852 error (_("failed during argument setup"));
1854 len1 = TYPE_LENGTH (sinfo.field_type (1));
1855 offset = align_up (len0, riscv_type_alignment (sinfo.field_type (1)));
1856 gdb_assert (len1 <= cinfo->xlen);
1857 if (!riscv_assign_reg_location (&ainfo->argloc[1],
1858 &cinfo->int_regs, len1, offset))
1859 error (_("failed during argument setup"));
1863 if (sinfo.number_of_fields () == 2
1864 && riscv_arg_regs_available (&cinfo->int_regs) >= 1
1865 && (is_integral_type (sinfo.field_type (0))
1866 && TYPE_LENGTH (sinfo.field_type (0)) <= cinfo->xlen
1867 && TYPE_CODE (sinfo.field_type (1)) == TYPE_CODE_FLT
1868 && TYPE_LENGTH (sinfo.field_type (1)) <= cinfo->flen))
1870 int len0, len1, offset;
1872 gdb_assert (TYPE_LENGTH (ainfo->type)
1873 <= (cinfo->flen + cinfo->xlen));
1875 len0 = TYPE_LENGTH (sinfo.field_type (0));
1876 len1 = TYPE_LENGTH (sinfo.field_type (1));
1877 offset = align_up (len0, riscv_type_alignment (sinfo.field_type (1)));
1879 gdb_assert (len0 <= cinfo->xlen);
1880 gdb_assert (len1 <= cinfo->flen);
1882 if (!riscv_assign_reg_location (&ainfo->argloc[0],
1883 &cinfo->int_regs, len0, 0))
1884 error (_("failed during argument setup"));
1886 if (!riscv_assign_reg_location (&ainfo->argloc[1],
1889 error (_("failed during argument setup"));
1895 /* Non of the structure flattening cases apply, so we just pass using
1897 ainfo->length = align_up (ainfo->length, cinfo->xlen);
1898 riscv_call_arg_scalar_int (ainfo, cinfo);
1901 /* Assign a location to call (or return) argument AINFO, the location is
1902 selected from CINFO which holds information about what call argument
1903 locations are available for use next. The TYPE is the type of the
1904 argument being passed, this information is recorded into AINFO (along
1905 with some additional information derived from the type).
1907 After assigning a location to AINFO, CINFO will have been updated. */
1910 riscv_arg_location (struct gdbarch *gdbarch,
1911 struct riscv_arg_info *ainfo,
1912 struct riscv_call_info *cinfo,
1916 ainfo->length = TYPE_LENGTH (ainfo->type);
1917 ainfo->align = riscv_type_alignment (ainfo->type);
1918 ainfo->contents = nullptr;
1920 switch (TYPE_CODE (ainfo->type))
1923 case TYPE_CODE_BOOL:
1924 case TYPE_CODE_CHAR:
1925 case TYPE_CODE_RANGE:
1926 case TYPE_CODE_ENUM:
1928 if (ainfo->length <= cinfo->xlen)
1930 ainfo->type = builtin_type (gdbarch)->builtin_long;
1931 ainfo->length = cinfo->xlen;
1933 else if (ainfo->length <= (2 * cinfo->xlen))
1935 ainfo->type = builtin_type (gdbarch)->builtin_long_long;
1936 ainfo->length = 2 * cinfo->xlen;
1939 /* Recalculate the alignment requirement. */
1940 ainfo->align = riscv_type_alignment (ainfo->type);
1941 riscv_call_arg_scalar_int (ainfo, cinfo);
1945 riscv_call_arg_scalar_float (ainfo, cinfo);
1948 case TYPE_CODE_COMPLEX:
1949 riscv_call_arg_complex_float (ainfo, cinfo);
1952 case TYPE_CODE_STRUCT:
1953 riscv_call_arg_struct (ainfo, cinfo);
1957 riscv_call_arg_scalar_int (ainfo, cinfo);
1962 /* Used for printing debug information about the call argument location in
1963 INFO to STREAM. The addresses in SP_REFS and SP_ARGS are the base
1964 addresses for the location of pass-by-reference and
1965 arguments-on-the-stack memory areas. */
1968 riscv_print_arg_location (ui_file *stream, struct gdbarch *gdbarch,
1969 struct riscv_arg_info *info,
1970 CORE_ADDR sp_refs, CORE_ADDR sp_args)
1972 const char* type_name = TYPE_NAME (info->type);
1973 if (type_name == nullptr)
1976 fprintf_unfiltered (stream, "type: '%s', length: 0x%x, alignment: 0x%x",
1977 type_name, info->length, info->align);
1978 switch (info->argloc[0].loc_type)
1980 case riscv_arg_info::location::in_reg:
1982 (stream, ", register %s",
1983 gdbarch_register_name (gdbarch, info->argloc[0].loc_data.regno));
1984 if (info->argloc[0].c_length < info->length)
1986 switch (info->argloc[1].loc_type)
1988 case riscv_arg_info::location::in_reg:
1990 (stream, ", register %s",
1991 gdbarch_register_name (gdbarch,
1992 info->argloc[1].loc_data.regno));
1995 case riscv_arg_info::location::on_stack:
1996 fprintf_unfiltered (stream, ", on stack at offset 0x%x",
1997 info->argloc[1].loc_data.offset);
2000 case riscv_arg_info::location::by_ref:
2002 /* The second location should never be a reference, any
2003 argument being passed by reference just places its address
2004 in the first location and is done. */
2005 error (_("invalid argument location"));
2009 if (info->argloc[1].c_offset > info->argloc[0].c_length)
2010 fprintf_unfiltered (stream, " (offset 0x%x)",
2011 info->argloc[1].c_offset);
2015 case riscv_arg_info::location::on_stack:
2016 fprintf_unfiltered (stream, ", on stack at offset 0x%x",
2017 info->argloc[0].loc_data.offset);
2020 case riscv_arg_info::location::by_ref:
2022 (stream, ", by reference, data at offset 0x%x (%s)",
2023 info->argloc[0].loc_data.offset,
2024 core_addr_to_string (sp_refs + info->argloc[0].loc_data.offset));
2025 if (info->argloc[1].loc_type
2026 == riscv_arg_info::location::in_reg)
2028 (stream, ", address in register %s",
2029 gdbarch_register_name (gdbarch, info->argloc[1].loc_data.regno));
2032 gdb_assert (info->argloc[1].loc_type
2033 == riscv_arg_info::location::on_stack);
2035 (stream, ", address on stack at offset 0x%x (%s)",
2036 info->argloc[1].loc_data.offset,
2037 core_addr_to_string (sp_args + info->argloc[1].loc_data.offset));
2042 error ("unknown argument location type");
2046 /* Implement the push dummy call gdbarch callback. */
2049 riscv_push_dummy_call (struct gdbarch *gdbarch,
2050 struct value *function,
2051 struct regcache *regcache,
2054 struct value **args,
2057 CORE_ADDR struct_addr)
2060 CORE_ADDR sp_args, sp_refs;
2061 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2062 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2064 struct riscv_arg_info *arg_info =
2065 (struct riscv_arg_info *) alloca (nargs * sizeof (struct riscv_arg_info));
2066 struct riscv_arg_info *info;
2068 struct riscv_call_info call_info (gdbarch);
2072 /* We'll use register $a0 if we're returning a struct. */
2074 ++call_info.int_regs.next_regnum;
2076 for (i = 0, info = &arg_info[0];
2080 struct value *arg_value;
2081 struct type *arg_type;
2083 arg_value = args[i];
2084 arg_type = check_typedef (value_type (arg_value));
2086 riscv_arg_location (gdbarch, info, &call_info, arg_type);
2088 if (info->type != arg_type)
2089 arg_value = value_cast (info->type, arg_value);
2090 info->contents = value_contents (arg_value);
2093 /* Adjust the stack pointer and align it. */
2094 sp = sp_refs = align_down (sp - call_info.memory.ref_offset, SP_ALIGNMENT);
2095 sp = sp_args = align_down (sp - call_info.memory.arg_offset, SP_ALIGNMENT);
2097 if (riscv_debug_infcall > 0)
2099 fprintf_unfiltered (gdb_stdlog, "dummy call args:\n");
2100 fprintf_unfiltered (gdb_stdlog, ": floating point ABI %s in use\n",
2101 (riscv_has_fp_abi (gdbarch) ? "is" : "is not"));
2102 fprintf_unfiltered (gdb_stdlog, ": xlen: %d\n: flen: %d\n",
2103 call_info.xlen, call_info.flen);
2105 fprintf_unfiltered (gdb_stdlog,
2106 "[*] struct return pointer in register $A0\n");
2107 for (i = 0; i < nargs; ++i)
2109 struct riscv_arg_info *info = &arg_info [i];
2111 fprintf_unfiltered (gdb_stdlog, "[%2d] ", i);
2112 riscv_print_arg_location (gdb_stdlog, gdbarch, info, sp_refs, sp_args);
2113 fprintf_unfiltered (gdb_stdlog, "\n");
2115 if (call_info.memory.arg_offset > 0
2116 || call_info.memory.ref_offset > 0)
2118 fprintf_unfiltered (gdb_stdlog, " Original sp: %s\n",
2119 core_addr_to_string (osp));
2120 fprintf_unfiltered (gdb_stdlog, "Stack required (for args): 0x%x\n",
2121 call_info.memory.arg_offset);
2122 fprintf_unfiltered (gdb_stdlog, "Stack required (for refs): 0x%x\n",
2123 call_info.memory.ref_offset);
2124 fprintf_unfiltered (gdb_stdlog, " Stack allocated: %s\n",
2125 core_addr_to_string_nz (osp - sp));
2129 /* Now load the argument into registers, or onto the stack. */
2133 gdb_byte buf[sizeof (LONGEST)];
2135 store_unsigned_integer (buf, call_info.xlen, byte_order, struct_addr);
2136 regcache_cooked_write (regcache, RISCV_A0_REGNUM, buf);
2139 for (i = 0; i < nargs; ++i)
2142 int second_arg_length = 0;
2143 const gdb_byte *second_arg_data;
2144 struct riscv_arg_info *info = &arg_info [i];
2146 gdb_assert (info->length > 0);
2148 switch (info->argloc[0].loc_type)
2150 case riscv_arg_info::location::in_reg:
2152 gdb_byte tmp [sizeof (ULONGEST)];
2154 gdb_assert (info->argloc[0].c_length <= info->length);
2155 memset (tmp, 0, sizeof (tmp));
2156 memcpy (tmp, info->contents, info->argloc[0].c_length);
2157 regcache_cooked_write (regcache,
2158 info->argloc[0].loc_data.regno,
2161 ((info->argloc[0].c_length < info->length)
2162 ? info->argloc[1].c_length : 0);
2163 second_arg_data = info->contents + info->argloc[1].c_offset;
2167 case riscv_arg_info::location::on_stack:
2168 dst = sp_args + info->argloc[0].loc_data.offset;
2169 write_memory (dst, info->contents, info->length);
2170 second_arg_length = 0;
2173 case riscv_arg_info::location::by_ref:
2174 dst = sp_refs + info->argloc[0].loc_data.offset;
2175 write_memory (dst, info->contents, info->length);
2177 second_arg_length = call_info.xlen;
2178 second_arg_data = (gdb_byte *) &dst;
2182 error ("unknown argument location type");
2185 if (second_arg_length > 0)
2187 switch (info->argloc[1].loc_type)
2189 case riscv_arg_info::location::in_reg:
2191 gdb_byte tmp [sizeof (ULONGEST)];
2193 gdb_assert (second_arg_length <= call_info.xlen);
2194 memset (tmp, 0, sizeof (tmp));
2195 memcpy (tmp, second_arg_data, second_arg_length);
2196 regcache_cooked_write (regcache,
2197 info->argloc[1].loc_data.regno,
2202 case riscv_arg_info::location::on_stack:
2206 arg_addr = sp_args + info->argloc[1].loc_data.offset;
2207 write_memory (arg_addr, second_arg_data, second_arg_length);
2211 case riscv_arg_info::location::by_ref:
2213 /* The second location should never be a reference, any
2214 argument being passed by reference just places its address
2215 in the first location and is done. */
2216 error (_("invalid argument location"));
2222 /* Set the dummy return value to bp_addr.
2223 A dummy breakpoint will be setup to execute the call. */
2225 if (riscv_debug_infcall > 0)
2226 fprintf_unfiltered (gdb_stdlog, ": writing $ra = %s\n",
2227 core_addr_to_string (bp_addr));
2228 regcache_cooked_write_unsigned (regcache, RISCV_RA_REGNUM, bp_addr);
2230 /* Finally, update the stack pointer. */
2232 if (riscv_debug_infcall > 0)
2233 fprintf_unfiltered (gdb_stdlog, ": writing $sp = %s\n",
2234 core_addr_to_string (sp));
2235 regcache_cooked_write_unsigned (regcache, RISCV_SP_REGNUM, sp);
2240 /* Implement the return_value gdbarch method. */
2242 static enum return_value_convention
2243 riscv_return_value (struct gdbarch *gdbarch,
2244 struct value *function,
2246 struct regcache *regcache,
2248 const gdb_byte *writebuf)
2250 enum type_code rv_type = TYPE_CODE (type);
2251 unsigned int rv_size = TYPE_LENGTH (type);
2252 int fp, regnum, flen;
2254 struct riscv_call_info call_info (gdbarch);
2255 struct riscv_arg_info info;
2256 struct type *arg_type;
2258 arg_type = check_typedef (type);
2259 riscv_arg_location (gdbarch, &info, &call_info, arg_type);
2261 if (riscv_debug_infcall > 0)
2263 fprintf_unfiltered (gdb_stdlog, "riscv return value:\n");
2264 fprintf_unfiltered (gdb_stdlog, "[R] ");
2265 riscv_print_arg_location (gdb_stdlog, gdbarch, &info, 0, 0);
2266 fprintf_unfiltered (gdb_stdlog, "\n");
2269 if (readbuf != nullptr || writebuf != nullptr)
2273 switch (info.argloc[0].loc_type)
2275 /* Return value in register(s). */
2276 case riscv_arg_info::location::in_reg:
2278 regnum = info.argloc[0].loc_data.regno;
2281 regcache_cooked_read (regcache, regnum, readbuf);
2284 regcache_cooked_write (regcache, regnum, writebuf);
2286 /* A return value in register can have a second part in a
2288 if (info.argloc[0].c_length < info.length)
2290 switch (info.argloc[1].loc_type)
2292 case riscv_arg_info::location::in_reg:
2293 regnum = info.argloc[1].loc_data.regno;
2297 readbuf += info.argloc[1].c_offset;
2298 regcache_cooked_read (regcache, regnum, readbuf);
2303 writebuf += info.argloc[1].c_offset;
2304 regcache_cooked_write (regcache, regnum, writebuf);
2308 case riscv_arg_info::location::by_ref:
2309 case riscv_arg_info::location::on_stack:
2311 error (_("invalid argument location"));
2318 /* Return value by reference will have its address in A0. */
2319 case riscv_arg_info::location::by_ref:
2323 regcache_cooked_read_unsigned (regcache, RISCV_A0_REGNUM,
2325 if (readbuf != nullptr)
2326 read_memory (addr, readbuf, info.length);
2327 if (writebuf != nullptr)
2328 write_memory (addr, writebuf, info.length);
2332 case riscv_arg_info::location::on_stack:
2334 error (_("invalid argument location"));
2339 switch (info.argloc[0].loc_type)
2341 case riscv_arg_info::location::in_reg:
2342 return RETURN_VALUE_REGISTER_CONVENTION;
2343 case riscv_arg_info::location::by_ref:
2344 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
2345 case riscv_arg_info::location::on_stack:
2347 error (_("invalid argument location"));
2351 /* Implement the frame_align gdbarch method. */
2354 riscv_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
2356 return align_down (addr, 16);
2359 /* Implement the unwind_pc gdbarch method. */
2362 riscv_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
2364 return frame_unwind_register_unsigned (next_frame, RISCV_PC_REGNUM);
2367 /* Implement the unwind_sp gdbarch method. */
2370 riscv_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
2372 return frame_unwind_register_unsigned (next_frame, RISCV_SP_REGNUM);
2375 /* Implement the dummy_id gdbarch method. */
2377 static struct frame_id
2378 riscv_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
2380 return frame_id_build (get_frame_register_signed (this_frame, RISCV_SP_REGNUM),
2381 get_frame_pc (this_frame));
2384 /* Generate, or return the cached frame cache for the RiscV frame
2387 static struct trad_frame_cache *
2388 riscv_frame_cache (struct frame_info *this_frame, void **this_cache)
2391 CORE_ADDR start_addr;
2392 CORE_ADDR stack_addr;
2393 struct trad_frame_cache *this_trad_cache;
2394 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2396 if ((*this_cache) != NULL)
2397 return (struct trad_frame_cache *) *this_cache;
2398 this_trad_cache = trad_frame_cache_zalloc (this_frame);
2399 (*this_cache) = this_trad_cache;
2401 trad_frame_set_reg_realreg (this_trad_cache, gdbarch_pc_regnum (gdbarch),
2404 pc = get_frame_pc (this_frame);
2405 find_pc_partial_function (pc, NULL, &start_addr, NULL);
2406 stack_addr = get_frame_register_signed (this_frame, RISCV_SP_REGNUM);
2407 trad_frame_set_id (this_trad_cache, frame_id_build (stack_addr, start_addr));
2409 trad_frame_set_this_base (this_trad_cache, stack_addr);
2411 return this_trad_cache;
2414 /* Implement the this_id callback for RiscV frame unwinder. */
2417 riscv_frame_this_id (struct frame_info *this_frame,
2418 void **prologue_cache,
2419 struct frame_id *this_id)
2421 struct trad_frame_cache *info;
2423 info = riscv_frame_cache (this_frame, prologue_cache);
2424 trad_frame_get_id (info, this_id);
2427 /* Implement the prev_register callback for RiscV frame unwinder. */
2429 static struct value *
2430 riscv_frame_prev_register (struct frame_info *this_frame,
2431 void **prologue_cache,
2434 struct trad_frame_cache *info;
2436 info = riscv_frame_cache (this_frame, prologue_cache);
2437 return trad_frame_get_register (info, this_frame, regnum);
2440 /* Structure defining the RiscV normal frame unwind functions. Since we
2441 are the fallback unwinder (DWARF unwinder is used first), we use the
2442 default frame sniffer, which always accepts the frame. */
2444 static const struct frame_unwind riscv_frame_unwind =
2446 /*.type =*/ NORMAL_FRAME,
2447 /*.stop_reason =*/ default_frame_unwind_stop_reason,
2448 /*.this_id =*/ riscv_frame_this_id,
2449 /*.prev_register =*/ riscv_frame_prev_register,
2450 /*.unwind_data =*/ NULL,
2451 /*.sniffer =*/ default_frame_sniffer,
2452 /*.dealloc_cache =*/ NULL,
2453 /*.prev_arch =*/ NULL,
2456 /* Initialize the current architecture based on INFO. If possible,
2457 re-use an architecture from ARCHES, which is a list of
2458 architectures already created during this debugging session.
2460 Called e.g. at program startup, when reading a core file, and when
2461 reading a binary file. */
2463 static struct gdbarch *
2464 riscv_gdbarch_init (struct gdbarch_info info,
2465 struct gdbarch_list *arches)
2467 struct gdbarch *gdbarch;
2468 struct gdbarch_tdep *tdep;
2469 struct gdbarch_tdep tmp_tdep;
2470 bool has_compressed_isa = false;
2473 /* Ideally, we'd like to get as much information from the target for
2474 things like register size, and whether the target has floating point
2475 hardware. However, there are some things that the target can't tell
2476 us, like, what ABI is being used.
2478 So, for now, we take as much information as possible from the ELF,
2479 including things like register size, and FP hardware support, along
2480 with information about the ABI.
2482 Information about this target is built up in TMP_TDEP, and then we
2483 look for an existing gdbarch in ARCHES that matches TMP_TDEP. If no
2484 match is found we'll create a new gdbarch and copy TMP_TDEP over. */
2485 memset (&tmp_tdep, 0, sizeof (tmp_tdep));
2487 if (info.abfd != NULL
2488 && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
2490 unsigned char eclass = elf_elfheader (info.abfd)->e_ident[EI_CLASS];
2491 int e_flags = elf_elfheader (info.abfd)->e_flags;
2493 if (eclass == ELFCLASS32)
2494 tmp_tdep.abi.fields.base_len = 1;
2495 else if (eclass == ELFCLASS64)
2496 tmp_tdep.abi.fields.base_len = 2;
2498 internal_error (__FILE__, __LINE__,
2499 _("unknown ELF header class %d"), eclass);
2501 if (e_flags & EF_RISCV_RVC)
2503 has_compressed_isa = true;
2504 tmp_tdep.core_features |= (1 << ('C' - 'A'));
2507 if (e_flags & EF_RISCV_FLOAT_ABI_DOUBLE)
2509 tmp_tdep.abi.fields.float_abi = 2;
2510 tmp_tdep.core_features |= (1 << ('D' - 'A'));
2511 tmp_tdep.core_features |= (1 << ('F' - 'A'));
2513 else if (e_flags & EF_RISCV_FLOAT_ABI_SINGLE)
2515 tmp_tdep.abi.fields.float_abi = 1;
2516 tmp_tdep.core_features |= (1 << ('F' - 'A'));
2521 const struct bfd_arch_info *binfo = info.bfd_arch_info;
2523 if (binfo->bits_per_word == 32)
2524 tmp_tdep.abi.fields.base_len = 1;
2525 else if (binfo->bits_per_word == 64)
2526 tmp_tdep.abi.fields.base_len = 2;
2528 internal_error (__FILE__, __LINE__, _("unknown bits_per_word %d"),
2529 binfo->bits_per_word);
2532 /* Find a candidate among the list of pre-declared architectures. */
2533 for (arches = gdbarch_list_lookup_by_info (arches, &info);
2535 arches = gdbarch_list_lookup_by_info (arches->next, &info))
2536 if (gdbarch_tdep (arches->gdbarch)->abi.value == tmp_tdep.abi.value)
2537 return arches->gdbarch;
2539 /* None found, so create a new architecture from the information provided. */
2540 tdep = (struct gdbarch_tdep *) xmalloc (sizeof *tdep);
2541 gdbarch = gdbarch_alloc (&info, tdep);
2542 memcpy (tdep, &tmp_tdep, sizeof (tmp_tdep));
2544 /* Target data types. */
2545 set_gdbarch_short_bit (gdbarch, 16);
2546 set_gdbarch_int_bit (gdbarch, 32);
2547 set_gdbarch_long_bit (gdbarch, riscv_isa_xlen (gdbarch) * 8);
2548 set_gdbarch_long_long_bit (gdbarch, 64);
2549 set_gdbarch_float_bit (gdbarch, 32);
2550 set_gdbarch_double_bit (gdbarch, 64);
2551 set_gdbarch_long_double_bit (gdbarch, 128);
2552 set_gdbarch_long_double_format (gdbarch, floatformats_ia64_quad);
2553 set_gdbarch_ptr_bit (gdbarch, riscv_isa_xlen (gdbarch) * 8);
2554 set_gdbarch_char_signed (gdbarch, 0);
2556 /* Information about the target architecture. */
2557 set_gdbarch_return_value (gdbarch, riscv_return_value);
2558 set_gdbarch_breakpoint_kind_from_pc (gdbarch, riscv_breakpoint_kind_from_pc);
2559 set_gdbarch_sw_breakpoint_from_kind (gdbarch, riscv_sw_breakpoint_from_kind);
2561 /* Register architecture. */
2562 set_gdbarch_pseudo_register_read (gdbarch, riscv_pseudo_register_read);
2563 set_gdbarch_pseudo_register_write (gdbarch, riscv_pseudo_register_write);
2564 set_gdbarch_num_regs (gdbarch, RISCV_LAST_REGNUM + 1);
2565 set_gdbarch_num_pseudo_regs (gdbarch, RISCV_LAST_REGNUM + 1);
2566 set_gdbarch_sp_regnum (gdbarch, RISCV_SP_REGNUM);
2567 set_gdbarch_pc_regnum (gdbarch, RISCV_PC_REGNUM);
2568 set_gdbarch_ps_regnum (gdbarch, RISCV_FP_REGNUM);
2569 set_gdbarch_deprecated_fp_regnum (gdbarch, RISCV_FP_REGNUM);
2571 /* Functions to supply register information. */
2572 set_gdbarch_register_name (gdbarch, riscv_register_name);
2573 set_gdbarch_register_type (gdbarch, riscv_register_type);
2574 set_gdbarch_print_registers_info (gdbarch, riscv_print_registers_info);
2575 set_gdbarch_register_reggroup_p (gdbarch, riscv_register_reggroup_p);
2577 /* Functions to analyze frames. */
2578 set_gdbarch_decr_pc_after_break (gdbarch, (has_compressed_isa ? 2 : 4));
2579 set_gdbarch_skip_prologue (gdbarch, riscv_skip_prologue);
2580 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2581 set_gdbarch_frame_align (gdbarch, riscv_frame_align);
2583 /* Functions to access frame data. */
2584 set_gdbarch_unwind_pc (gdbarch, riscv_unwind_pc);
2585 set_gdbarch_unwind_sp (gdbarch, riscv_unwind_sp);
2587 /* Functions handling dummy frames. */
2588 set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
2589 set_gdbarch_push_dummy_code (gdbarch, riscv_push_dummy_code);
2590 set_gdbarch_push_dummy_call (gdbarch, riscv_push_dummy_call);
2591 set_gdbarch_dummy_id (gdbarch, riscv_dummy_id);
2593 /* Frame unwinders. Use DWARF debug info if available, otherwise use our own
2595 dwarf2_append_unwinders (gdbarch);
2596 frame_unwind_append_unwinder (gdbarch, &riscv_frame_unwind);
2598 /* Check any target description for validity. */
2599 if (tdesc_has_registers (info.target_desc))
2601 const struct tdesc_feature *feature;
2602 struct tdesc_arch_data *tdesc_data;
2605 feature = tdesc_find_feature (info.target_desc, "org.gnu.gdb.riscv.cpu");
2606 if (feature == NULL)
2609 tdesc_data = tdesc_data_alloc ();
2612 for (i = RISCV_ZERO_REGNUM; i <= RISCV_LAST_FP_REGNUM; ++i)
2613 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
2614 riscv_gdb_reg_names[i]);
2615 for (i = RISCV_FIRST_CSR_REGNUM; i <= RISCV_LAST_CSR_REGNUM; ++i)
2619 sprintf (buf, "csr%d", i - RISCV_FIRST_CSR_REGNUM);
2620 valid_p &= tdesc_numbered_register (feature, tdesc_data, i, buf);
2623 valid_p &= tdesc_numbered_register (feature, tdesc_data, i++, "priv");
2626 tdesc_data_cleanup (tdesc_data);
2628 tdesc_use_registers (gdbarch, info.target_desc, tdesc_data);
2632 for (i = 0; i < ARRAY_SIZE (riscv_register_aliases); ++i)
2633 user_reg_add (gdbarch, riscv_register_aliases[i].name,
2634 value_of_riscv_user_reg, &riscv_register_aliases[i].regnum);
2640 /* Allocate new riscv_inferior_data object. */
2642 static struct riscv_inferior_data *
2643 riscv_new_inferior_data (void)
2645 struct riscv_inferior_data *inf_data
2646 = new (struct riscv_inferior_data);
2647 inf_data->misa_read = false;
2651 /* Free inferior data. */
2654 riscv_inferior_data_cleanup (struct inferior *inf, void *data)
2656 struct riscv_inferior_data *inf_data =
2657 static_cast <struct riscv_inferior_data *> (data);
2661 /* Return riscv_inferior_data for the given INFERIOR. If not yet created,
2664 struct riscv_inferior_data *
2665 riscv_inferior_data (struct inferior *const inf)
2667 struct riscv_inferior_data *inf_data;
2669 gdb_assert (inf != NULL);
2672 = (struct riscv_inferior_data *) inferior_data (inf, riscv_inferior_data_reg);
2673 if (inf_data == NULL)
2675 inf_data = riscv_new_inferior_data ();
2676 set_inferior_data (inf, riscv_inferior_data_reg, inf_data);
2682 /* Free the inferior data when an inferior exits. */
2685 riscv_invalidate_inferior_data (struct inferior *inf)
2687 struct riscv_inferior_data *inf_data;
2689 gdb_assert (inf != NULL);
2691 /* Don't call RISCV_INFERIOR_DATA as we don't want to create the data if
2692 we've not already created it by this point. */
2694 = (struct riscv_inferior_data *) inferior_data (inf, riscv_inferior_data_reg);
2695 if (inf_data != NULL)
2698 set_inferior_data (inf, riscv_inferior_data_reg, NULL);
2703 _initialize_riscv_tdep (void)
2705 gdbarch_register (bfd_arch_riscv, riscv_gdbarch_init, NULL);
2707 /* Register per-inferior data. */
2708 riscv_inferior_data_reg
2709 = register_inferior_data_with_cleanup (NULL, riscv_inferior_data_cleanup);
2711 /* Observers used to invalidate the inferior data when needed. */
2712 observer_attach_inferior_exit (riscv_invalidate_inferior_data);
2713 observer_attach_inferior_appeared (riscv_invalidate_inferior_data);
2715 /* Add root prefix command for all "set debug riscv" and "show debug
2717 add_prefix_cmd ("riscv", no_class, set_debug_riscv_command,
2718 _("RISC-V specific debug commands."),
2719 &setdebugriscvcmdlist, "set debug riscv ", 0,
2722 add_prefix_cmd ("riscv", no_class, show_debug_riscv_command,
2723 _("RISC-V specific debug commands."),
2724 &showdebugriscvcmdlist, "show debug riscv ", 0,
2727 add_setshow_zuinteger_cmd ("infcall", class_maintenance,
2728 &riscv_debug_infcall, _("\
2729 Set riscv inferior call debugging."), _("\
2730 Show riscv inferior call debugging."), _("\
2731 When non-zero, print debugging information for the riscv specific parts\n\
2732 of the inferior call mechanism."),
2734 show_riscv_debug_variable,
2735 &setdebugriscvcmdlist, &showdebugriscvcmdlist);
2737 /* Add root prefix command for all "set riscv" and "show riscv" commands. */
2738 add_prefix_cmd ("riscv", no_class, set_riscv_command,
2739 _("RISC-V specific commands."),
2740 &setriscvcmdlist, "set riscv ", 0, &setlist);
2742 add_prefix_cmd ("riscv", no_class, show_riscv_command,
2743 _("RISC-V specific commands."),
2744 &showriscvcmdlist, "show riscv ", 0, &showlist);
2747 use_compressed_breakpoints = AUTO_BOOLEAN_AUTO;
2748 add_setshow_auto_boolean_cmd ("use-compressed-breakpoints", no_class,
2749 &use_compressed_breakpoints,
2751 Set debugger's use of compressed breakpoints."), _(" \
2752 Show debugger's use of compressed breakpoints."), _("\
2753 Debugging compressed code requires compressed breakpoints to be used. If\n \
2754 left to 'auto' then gdb will use them if $misa indicates the C extension\n \
2755 is supported. If that doesn't give the correct behavior, then this option\n\
2758 show_use_compressed_breakpoints,