1*6881a400Schristos /* Target-dependent code for NetBSD on RISC-V processors. 2*6881a400Schristos Copyright (C) 2018-2020 Free Software Foundation, Inc. 3*6881a400Schristos 4*6881a400Schristos This file is part of GDB. 5*6881a400Schristos 6*6881a400Schristos This program is free software; you can redistribute it and/or modify 7*6881a400Schristos it under the terms of the GNU General Public License as published by 8*6881a400Schristos the Free Software Foundation; either version 3 of the License, or 9*6881a400Schristos (at your option) any later version. 10*6881a400Schristos 11*6881a400Schristos This program is distributed in the hope that it will be useful, 12*6881a400Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 13*6881a400Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*6881a400Schristos GNU General Public License for more details. 15*6881a400Schristos 16*6881a400Schristos You should have received a copy of the GNU General Public License 17*6881a400Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 18*6881a400Schristos 19*6881a400Schristos #include "defs.h" 20*6881a400Schristos #include "netbsd-tdep.h" 21*6881a400Schristos #include "osabi.h" 22*6881a400Schristos #include "riscv-tdep.h" 23*6881a400Schristos #include "riscv-netbsd-tdep.h" 24*6881a400Schristos #include "solib-svr4.h" 25*6881a400Schristos #include "target.h" 26*6881a400Schristos #include "trad-frame.h" 27*6881a400Schristos #include "tramp-frame.h" 28*6881a400Schristos #include "gdbarch.h" 29*6881a400Schristos #include "inferior.h" 30*6881a400Schristos 31*6881a400Schristos /* Register maps. */ 32*6881a400Schristos 33*6881a400Schristos static const struct regcache_map_entry riscv_nbsd_gregmap[] = 34*6881a400Schristos { 35*6881a400Schristos { 31, RISCV_RA_REGNUM, 0 }, /* x1 to x31 */ 36*6881a400Schristos { 1, RISCV_PC_REGNUM, 0 }, 37*6881a400Schristos { 0 } 38*6881a400Schristos }; 39*6881a400Schristos 40*6881a400Schristos static const struct regcache_map_entry riscv_nbsd_fpregmap[] = 41*6881a400Schristos { 42*6881a400Schristos { 32, RISCV_FIRST_FP_REGNUM, 16 }, 43*6881a400Schristos { 1, RISCV_CSR_FCSR_REGNUM, 8 }, 44*6881a400Schristos { 0 } 45*6881a400Schristos }; 46*6881a400Schristos 47*6881a400Schristos /* Supply the general-purpose registers stored in GREGS to REGCACHE. 48*6881a400Schristos This function only exists to supply the always-zero x0 in addition 49*6881a400Schristos to the registers in GREGS. */ 50*6881a400Schristos 51*6881a400Schristos static void 52*6881a400Schristos riscv_nbsd_supply_gregset (const struct regset *regset, 53*6881a400Schristos struct regcache *regcache, int regnum, 54*6881a400Schristos const void *gregs, size_t len) 55*6881a400Schristos { 56*6881a400Schristos regcache->supply_regset (&riscv_nbsd_gregset, regnum, gregs, len); 57*6881a400Schristos if (regnum == -1 || regnum == RISCV_ZERO_REGNUM) 58*6881a400Schristos regcache->raw_supply_zeroed (RISCV_ZERO_REGNUM); 59*6881a400Schristos } 60*6881a400Schristos 61*6881a400Schristos /* Register set definitions. */ 62*6881a400Schristos 63*6881a400Schristos const struct regset riscv_nbsd_gregset = 64*6881a400Schristos { 65*6881a400Schristos riscv_nbsd_gregmap, 66*6881a400Schristos riscv_nbsd_supply_gregset, regcache_collect_regset 67*6881a400Schristos }; 68*6881a400Schristos 69*6881a400Schristos const struct regset riscv_nbsd_fpregset = 70*6881a400Schristos { 71*6881a400Schristos riscv_nbsd_fpregmap, 72*6881a400Schristos regcache_supply_regset, regcache_collect_regset 73*6881a400Schristos }; 74*6881a400Schristos 75*6881a400Schristos /* Implement the "iterate_over_regset_sections" gdbarch method. */ 76*6881a400Schristos 77*6881a400Schristos static void 78*6881a400Schristos riscv_nbsd_iterate_over_regset_sections (struct gdbarch *gdbarch, 79*6881a400Schristos iterate_over_regset_sections_cb *cb, 80*6881a400Schristos void *cb_data, 81*6881a400Schristos const struct regcache *regcache) 82*6881a400Schristos { 83*6881a400Schristos cb (".reg", RISCV_NBSD_NUM_GREGS * riscv_isa_xlen (gdbarch), 84*6881a400Schristos RISCV_NBSD_NUM_GREGS * riscv_isa_xlen (gdbarch), 85*6881a400Schristos &riscv_nbsd_gregset, NULL, cb_data); 86*6881a400Schristos cb (".reg2", RISCV_NBSD_SIZEOF_FPREGSET, RISCV_NBSD_SIZEOF_FPREGSET, 87*6881a400Schristos &riscv_nbsd_fpregset, NULL, cb_data); 88*6881a400Schristos } 89*6881a400Schristos 90*6881a400Schristos /* In a signal frame, sp points to a 'struct sigframe' which is 91*6881a400Schristos defined as: 92*6881a400Schristos 93*6881a400Schristos struct sigframe { 94*6881a400Schristos siginfo_t sf_si; 95*6881a400Schristos ucontext_t sf_uc; 96*6881a400Schristos }; 97*6881a400Schristos 98*6881a400Schristos ucontext_t is defined as: 99*6881a400Schristos 100*6881a400Schristos struct __ucontext { 101*6881a400Schristos sigset_t uc_sigmask; 102*6881a400Schristos mcontext_t uc_mcontext; 103*6881a400Schristos ... 104*6881a400Schristos }; 105*6881a400Schristos 106*6881a400Schristos The mcontext_t contains the general purpose register set followed 107*6881a400Schristos by the floating point register set. The floating point register 108*6881a400Schristos set is only valid if the _MC_FP_VALID flag is set in mc_flags. */ 109*6881a400Schristos 110*6881a400Schristos #define RISCV_SIGFRAME_UCONTEXT_OFFSET 80 111*6881a400Schristos #define RISCV_UCONTEXT_MCONTEXT_OFFSET 16 112*6881a400Schristos #define RISCV_MCONTEXT_FLAG_FP_VALID 0x1 113*6881a400Schristos 114*6881a400Schristos /* Implement the "init" method of struct tramp_frame. */ 115*6881a400Schristos 116*6881a400Schristos static void 117*6881a400Schristos riscv_nbsd_sigframe_init (const struct tramp_frame *self, 118*6881a400Schristos frame_info_ptr this_frame, 119*6881a400Schristos struct trad_frame_cache *this_cache, 120*6881a400Schristos CORE_ADDR func) 121*6881a400Schristos { 122*6881a400Schristos struct gdbarch *gdbarch = get_frame_arch (this_frame); 123*6881a400Schristos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 124*6881a400Schristos CORE_ADDR sp = get_frame_register_unsigned (this_frame, RISCV_SP_REGNUM); 125*6881a400Schristos CORE_ADDR mcontext_addr 126*6881a400Schristos = (sp 127*6881a400Schristos + RISCV_SIGFRAME_UCONTEXT_OFFSET 128*6881a400Schristos + RISCV_UCONTEXT_MCONTEXT_OFFSET); 129*6881a400Schristos gdb_byte buf[4]; 130*6881a400Schristos 131*6881a400Schristos trad_frame_set_reg_regmap (this_cache, riscv_nbsd_gregmap, mcontext_addr, 132*6881a400Schristos RISCV_NBSD_NUM_GREGS * riscv_isa_xlen (gdbarch)); 133*6881a400Schristos 134*6881a400Schristos CORE_ADDR fpregs_addr 135*6881a400Schristos = mcontext_addr + RISCV_NBSD_NUM_GREGS * riscv_isa_xlen (gdbarch); 136*6881a400Schristos CORE_ADDR fp_flags_addr 137*6881a400Schristos = fpregs_addr + RISCV_NBSD_SIZEOF_FPREGSET; 138*6881a400Schristos if (target_read_memory (fp_flags_addr, buf, 4) == 0 139*6881a400Schristos && (extract_unsigned_integer (buf, 4, byte_order) 140*6881a400Schristos & RISCV_MCONTEXT_FLAG_FP_VALID)) 141*6881a400Schristos trad_frame_set_reg_regmap (this_cache, riscv_nbsd_fpregmap, fpregs_addr, 142*6881a400Schristos RISCV_NBSD_SIZEOF_FPREGSET); 143*6881a400Schristos 144*6881a400Schristos trad_frame_set_id (this_cache, frame_id_build (sp, func)); 145*6881a400Schristos } 146*6881a400Schristos 147*6881a400Schristos /* RISC-V supports 16-bit instructions ("C") as well as 32-bit 148*6881a400Schristos instructions. The signal trampoline on NetBSD uses a mix of 149*6881a400Schristos these, but tramp_frame assumes a fixed instruction size. To cope, 150*6881a400Schristos claim that all instructions are 16 bits and use two "slots" for 151*6881a400Schristos 32-bit instructions. */ 152*6881a400Schristos 153*6881a400Schristos static const struct tramp_frame riscv_nbsd_sigframe = 154*6881a400Schristos { 155*6881a400Schristos SIGTRAMP_FRAME, 156*6881a400Schristos 2, 157*6881a400Schristos { 158*6881a400Schristos {0x850a, ULONGEST_MAX}, /* mov a0, sp */ 159*6881a400Schristos {0x0513, ULONGEST_MAX}, /* addi a0, a0, #SF_UC */ 160*6881a400Schristos {0x0505, ULONGEST_MAX}, 161*6881a400Schristos {0x0293, ULONGEST_MAX}, /* li t0, #SYS_sigreturn */ 162*6881a400Schristos {0x1a10, ULONGEST_MAX}, 163*6881a400Schristos {0x0073, ULONGEST_MAX}, /* ecall */ 164*6881a400Schristos {0x0000, ULONGEST_MAX}, 165*6881a400Schristos {TRAMP_SENTINEL_INSN, ULONGEST_MAX} 166*6881a400Schristos }, 167*6881a400Schristos riscv_nbsd_sigframe_init 168*6881a400Schristos }; 169*6881a400Schristos 170*6881a400Schristos 171*6881a400Schristos #if 0 172*6881a400Schristos /* Implement the "get_thread_local_address" gdbarch method. */ 173*6881a400Schristos 174*6881a400Schristos static CORE_ADDR 175*6881a400Schristos riscv_nbsd_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid, 176*6881a400Schristos CORE_ADDR lm_addr, CORE_ADDR offset) 177*6881a400Schristos { 178*6881a400Schristos struct regcache *regcache; 179*6881a400Schristos 180*6881a400Schristos regcache = get_thread_arch_regcache (current_inferior ()->process_target (), 181*6881a400Schristos ptid, gdbarch); 182*6881a400Schristos 183*6881a400Schristos target_fetch_registers (regcache, RISCV_TP_REGNUM); 184*6881a400Schristos 185*6881a400Schristos ULONGEST tp; 186*6881a400Schristos if (regcache->cooked_read (RISCV_TP_REGNUM, &tp) != REG_VALID) 187*6881a400Schristos error (_("Unable to fetch %%tp")); 188*6881a400Schristos 189*6881a400Schristos /* %tp points to the end of the TCB which contains two pointers. 190*6881a400Schristos The first pointer in the TCB points to the DTV array. */ 191*6881a400Schristos CORE_ADDR dtv_addr = tp - (gdbarch_ptr_bit (gdbarch) / 8) * 2; 192*6881a400Schristos return nbsd_get_thread_local_address (gdbarch, dtv_addr, lm_addr, offset); 193*6881a400Schristos } 194*6881a400Schristos #endif 195*6881a400Schristos 196*6881a400Schristos 197*6881a400Schristos /* Implement the 'init_osabi' method of struct gdb_osabi_handler. */ 198*6881a400Schristos 199*6881a400Schristos static void 200*6881a400Schristos riscv_nbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) 201*6881a400Schristos { 202*6881a400Schristos /* Generic NetBSD support. */ 203*6881a400Schristos nbsd_init_abi (info, gdbarch); 204*6881a400Schristos 205*6881a400Schristos set_gdbarch_software_single_step (gdbarch, riscv_software_single_step); 206*6881a400Schristos 207*6881a400Schristos set_solib_svr4_fetch_link_map_offsets (gdbarch, 208*6881a400Schristos (riscv_isa_xlen (gdbarch) == 4 209*6881a400Schristos ? svr4_ilp32_fetch_link_map_offsets 210*6881a400Schristos : svr4_lp64_fetch_link_map_offsets)); 211*6881a400Schristos 212*6881a400Schristos tramp_frame_prepend_unwinder (gdbarch, &riscv_nbsd_sigframe); 213*6881a400Schristos 214*6881a400Schristos set_gdbarch_iterate_over_regset_sections 215*6881a400Schristos (gdbarch, riscv_nbsd_iterate_over_regset_sections); 216*6881a400Schristos 217*6881a400Schristos set_gdbarch_fetch_tls_load_module_address (gdbarch, 218*6881a400Schristos svr4_fetch_objfile_link_map); 219*6881a400Schristos #if 0 220*6881a400Schristos set_gdbarch_get_thread_local_address (gdbarch, 221*6881a400Schristos riscv_nbsd_get_thread_local_address); 222*6881a400Schristos #endif 223*6881a400Schristos } 224*6881a400Schristos 225*6881a400Schristos void _initialize_riscv_nbsd_tdep (); 226*6881a400Schristos void 227*6881a400Schristos _initialize_riscv_nbsd_tdep () 228*6881a400Schristos { 229*6881a400Schristos gdbarch_register_osabi (bfd_arch_riscv, 0, GDB_OSABI_NETBSD, 230*6881a400Schristos riscv_nbsd_init_abi); 231*6881a400Schristos } 232