1*6881a400Schristos /* Target-dependent code for NetBSD/sparc. 2*6881a400Schristos 3*6881a400Schristos Copyright (C) 2002-2023 Free Software Foundation, Inc. 4*6881a400Schristos Contributed by Wasabi Systems, Inc. 5*6881a400Schristos 6*6881a400Schristos This file is part of GDB. 7*6881a400Schristos 8*6881a400Schristos This program is free software; you can redistribute it and/or modify 9*6881a400Schristos it under the terms of the GNU General Public License as published by 10*6881a400Schristos the Free Software Foundation; either version 3 of the License, or 11*6881a400Schristos (at your option) any later version. 12*6881a400Schristos 13*6881a400Schristos This program is distributed in the hope that it will be useful, 14*6881a400Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 15*6881a400Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16*6881a400Schristos GNU General Public License for more details. 17*6881a400Schristos 18*6881a400Schristos You should have received a copy of the GNU General Public License 19*6881a400Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 20*6881a400Schristos 21*6881a400Schristos #include "defs.h" 22*6881a400Schristos #include "frame.h" 23*6881a400Schristos #include "frame-unwind.h" 24*6881a400Schristos #include "gdbcore.h" 25*6881a400Schristos #include "gdbtypes.h" 26*6881a400Schristos #include "osabi.h" 27*6881a400Schristos #include "regcache.h" 28*6881a400Schristos #include "regset.h" 29*6881a400Schristos #include "solib-svr4.h" 30*6881a400Schristos #include "symtab.h" 31*6881a400Schristos #include "trad-frame.h" 32*6881a400Schristos #include "gdbarch.h" 33*6881a400Schristos 34*6881a400Schristos #include "sparc-tdep.h" 35*6881a400Schristos #include "netbsd-tdep.h" 36*6881a400Schristos 37*6881a400Schristos /* Macros to extract fields from SPARC instructions. */ 38*6881a400Schristos #define X_RS1(i) (((i) >> 14) & 0x1f) 39*6881a400Schristos #define X_RS2(i) ((i) & 0x1f) 40*6881a400Schristos #define X_I(i) (((i) >> 13) & 1) 41*6881a400Schristos 42*6881a400Schristos const struct sparc_gregmap sparc32nbsd_gregmap = 43*6881a400Schristos { 44*6881a400Schristos 0 * 4, /* %psr */ 45*6881a400Schristos 1 * 4, /* %pc */ 46*6881a400Schristos 2 * 4, /* %npc */ 47*6881a400Schristos 3 * 4, /* %y */ 48*6881a400Schristos -1, /* %wim */ 49*6881a400Schristos -1, /* %tbr */ 50*6881a400Schristos 5 * 4, /* %g1 */ 51*6881a400Schristos -1 /* %l0 */ 52*6881a400Schristos }; 53*6881a400Schristos 54*6881a400Schristos static void 55*6881a400Schristos sparc32nbsd_supply_gregset (const struct regset *regset, 56*6881a400Schristos struct regcache *regcache, 57*6881a400Schristos int regnum, const void *gregs, size_t len) 58*6881a400Schristos { 59*6881a400Schristos sparc32_supply_gregset (&sparc32nbsd_gregmap, regcache, regnum, gregs); 60*6881a400Schristos 61*6881a400Schristos /* Traditional NetBSD core files don't use multiple register sets. 62*6881a400Schristos Instead, the general-purpose and floating-point registers are 63*6881a400Schristos lumped together in a single section. */ 64*6881a400Schristos if (len >= 212) 65*6881a400Schristos sparc32_supply_fpregset (&sparc32_bsd_fpregmap, regcache, regnum, 66*6881a400Schristos (const char *) gregs + 80); 67*6881a400Schristos } 68*6881a400Schristos 69*6881a400Schristos static void 70*6881a400Schristos sparc32nbsd_supply_fpregset (const struct regset *regset, 71*6881a400Schristos struct regcache *regcache, 72*6881a400Schristos int regnum, const void *fpregs, size_t len) 73*6881a400Schristos { 74*6881a400Schristos sparc32_supply_fpregset (&sparc32_bsd_fpregmap, regcache, regnum, fpregs); 75*6881a400Schristos } 76*6881a400Schristos 77*6881a400Schristos 78*6881a400Schristos /* Signal trampolines. */ 79*6881a400Schristos 80*6881a400Schristos /* The following variables describe the location of an on-stack signal 81*6881a400Schristos trampoline. The current values correspond to the memory layout for 82*6881a400Schristos NetBSD 1.3 and up. These shouldn't be necessary for NetBSD 2.0 and 83*6881a400Schristos up, since NetBSD uses signal trampolines provided by libc now. */ 84*6881a400Schristos 85*6881a400Schristos static const CORE_ADDR sparc32nbsd_sigtramp_start = 0xeffffef0; 86*6881a400Schristos static const CORE_ADDR sparc32nbsd_sigtramp_end = 0xeffffff0; 87*6881a400Schristos 88*6881a400Schristos static int 89*6881a400Schristos sparc32nbsd_pc_in_sigtramp (CORE_ADDR pc, const char *name) 90*6881a400Schristos { 91*6881a400Schristos if (pc >= sparc32nbsd_sigtramp_start && pc < sparc32nbsd_sigtramp_end) 92*6881a400Schristos return 1; 93*6881a400Schristos 94*6881a400Schristos return nbsd_pc_in_sigtramp (pc, name); 95*6881a400Schristos } 96*6881a400Schristos 97*6881a400Schristos trad_frame_saved_reg * 98*6881a400Schristos sparc32nbsd_sigcontext_saved_regs (frame_info_ptr this_frame) 99*6881a400Schristos { 100*6881a400Schristos struct gdbarch *gdbarch = get_frame_arch (this_frame); 101*6881a400Schristos trad_frame_saved_reg *saved_regs; 102*6881a400Schristos CORE_ADDR addr, sigcontext_addr; 103*6881a400Schristos int regnum, delta; 104*6881a400Schristos ULONGEST psr; 105*6881a400Schristos 106*6881a400Schristos saved_regs = trad_frame_alloc_saved_regs (this_frame); 107*6881a400Schristos 108*6881a400Schristos /* We find the appropriate instance of `struct sigcontext' at a 109*6881a400Schristos fixed offset in the signal frame. */ 110*6881a400Schristos addr = get_frame_register_unsigned (this_frame, SPARC_FP_REGNUM); 111*6881a400Schristos sigcontext_addr = addr + 64 + 16; 112*6881a400Schristos 113*6881a400Schristos /* The registers are saved in bits and pieces scattered all over the 114*6881a400Schristos place. The code below records their location on the assumption 115*6881a400Schristos that the part of the signal trampoline that saves the state has 116*6881a400Schristos been executed. */ 117*6881a400Schristos 118*6881a400Schristos saved_regs[SPARC_SP_REGNUM].set_addr (sigcontext_addr + 8); 119*6881a400Schristos saved_regs[SPARC32_PC_REGNUM].set_addr (sigcontext_addr + 12); 120*6881a400Schristos saved_regs[SPARC32_NPC_REGNUM].set_addr (sigcontext_addr + 16); 121*6881a400Schristos saved_regs[SPARC32_PSR_REGNUM].set_addr (sigcontext_addr + 20); 122*6881a400Schristos saved_regs[SPARC_G1_REGNUM].set_addr (sigcontext_addr + 24); 123*6881a400Schristos saved_regs[SPARC_O0_REGNUM].set_addr (sigcontext_addr + 28); 124*6881a400Schristos 125*6881a400Schristos /* The remaining `global' registers and %y are saved in the `local' 126*6881a400Schristos registers. */ 127*6881a400Schristos delta = SPARC_L0_REGNUM - SPARC_G0_REGNUM; 128*6881a400Schristos for (regnum = SPARC_G2_REGNUM; regnum <= SPARC_G7_REGNUM; regnum++) 129*6881a400Schristos saved_regs[regnum].set_realreg (regnum + delta); 130*6881a400Schristos saved_regs[SPARC32_Y_REGNUM].set_realreg (SPARC_L1_REGNUM); 131*6881a400Schristos 132*6881a400Schristos /* The remaining `out' registers can be found in the current frame's 133*6881a400Schristos `in' registers. */ 134*6881a400Schristos delta = SPARC_I0_REGNUM - SPARC_O0_REGNUM; 135*6881a400Schristos for (regnum = SPARC_O1_REGNUM; regnum <= SPARC_O5_REGNUM; regnum++) 136*6881a400Schristos saved_regs[regnum].set_realreg (regnum + delta); 137*6881a400Schristos saved_regs[SPARC_O7_REGNUM].set_realreg (SPARC_I7_REGNUM); 138*6881a400Schristos 139*6881a400Schristos /* The `local' and `in' registers have been saved in the register 140*6881a400Schristos save area. */ 141*6881a400Schristos addr = saved_regs[SPARC_SP_REGNUM].addr (); 142*6881a400Schristos addr = get_frame_memory_unsigned (this_frame, addr, 4); 143*6881a400Schristos for (regnum = SPARC_L0_REGNUM; 144*6881a400Schristos regnum <= SPARC_I7_REGNUM; regnum++, addr += 4) 145*6881a400Schristos saved_regs[regnum].set_addr (addr); 146*6881a400Schristos 147*6881a400Schristos /* Handle StackGhost. */ 148*6881a400Schristos { 149*6881a400Schristos ULONGEST wcookie = sparc_fetch_wcookie (gdbarch); 150*6881a400Schristos 151*6881a400Schristos if (wcookie != 0) 152*6881a400Schristos { 153*6881a400Schristos ULONGEST i7; 154*6881a400Schristos 155*6881a400Schristos addr = saved_regs[SPARC_I7_REGNUM].addr (); 156*6881a400Schristos i7 = get_frame_memory_unsigned (this_frame, addr, 4); 157*6881a400Schristos saved_regs[SPARC_I7_REGNUM].set_value (i7 ^ wcookie); 158*6881a400Schristos } 159*6881a400Schristos } 160*6881a400Schristos 161*6881a400Schristos /* The floating-point registers are only saved if the EF bit in %prs 162*6881a400Schristos has been set. */ 163*6881a400Schristos 164*6881a400Schristos #define PSR_EF 0x00001000 165*6881a400Schristos 166*6881a400Schristos addr = saved_regs[SPARC32_PSR_REGNUM].addr (); 167*6881a400Schristos psr = get_frame_memory_unsigned (this_frame, addr, 4); 168*6881a400Schristos if (psr & PSR_EF) 169*6881a400Schristos { 170*6881a400Schristos CORE_ADDR sp; 171*6881a400Schristos 172*6881a400Schristos sp = get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM); 173*6881a400Schristos saved_regs[SPARC32_FSR_REGNUM].set_addr (sp + 96); 174*6881a400Schristos for (regnum = SPARC_F0_REGNUM, addr = sp + 96 + 8; 175*6881a400Schristos regnum <= SPARC_F31_REGNUM; regnum++, addr += 4) 176*6881a400Schristos saved_regs[regnum].set_addr (addr); 177*6881a400Schristos } 178*6881a400Schristos 179*6881a400Schristos return saved_regs; 180*6881a400Schristos } 181*6881a400Schristos 182*6881a400Schristos static struct sparc_frame_cache * 183*6881a400Schristos sparc32nbsd_sigcontext_frame_cache (frame_info_ptr this_frame, 184*6881a400Schristos void **this_cache) 185*6881a400Schristos { 186*6881a400Schristos struct sparc_frame_cache *cache; 187*6881a400Schristos CORE_ADDR addr; 188*6881a400Schristos 189*6881a400Schristos if (*this_cache) 190*6881a400Schristos return (struct sparc_frame_cache *) *this_cache; 191*6881a400Schristos 192*6881a400Schristos cache = sparc_frame_cache (this_frame, this_cache); 193*6881a400Schristos gdb_assert (cache == *this_cache); 194*6881a400Schristos 195*6881a400Schristos /* If we couldn't find the frame's function, we're probably dealing 196*6881a400Schristos with an on-stack signal trampoline. */ 197*6881a400Schristos if (cache->pc == 0) 198*6881a400Schristos { 199*6881a400Schristos cache->pc = sparc32nbsd_sigtramp_start; 200*6881a400Schristos 201*6881a400Schristos /* Since we couldn't find the frame's function, the cache was 202*6881a400Schristos initialized under the assumption that we're frameless. */ 203*6881a400Schristos sparc_record_save_insn (cache); 204*6881a400Schristos addr = get_frame_register_unsigned (this_frame, SPARC_FP_REGNUM); 205*6881a400Schristos cache->base = addr; 206*6881a400Schristos } 207*6881a400Schristos 208*6881a400Schristos cache->saved_regs = sparc32nbsd_sigcontext_saved_regs (this_frame); 209*6881a400Schristos 210*6881a400Schristos return cache; 211*6881a400Schristos } 212*6881a400Schristos 213*6881a400Schristos static void 214*6881a400Schristos sparc32nbsd_sigcontext_frame_this_id (frame_info_ptr this_frame, 215*6881a400Schristos void **this_cache, 216*6881a400Schristos struct frame_id *this_id) 217*6881a400Schristos { 218*6881a400Schristos struct sparc_frame_cache *cache = 219*6881a400Schristos sparc32nbsd_sigcontext_frame_cache (this_frame, this_cache); 220*6881a400Schristos 221*6881a400Schristos (*this_id) = frame_id_build (cache->base, cache->pc); 222*6881a400Schristos } 223*6881a400Schristos 224*6881a400Schristos static struct value * 225*6881a400Schristos sparc32nbsd_sigcontext_frame_prev_register (frame_info_ptr this_frame, 226*6881a400Schristos void **this_cache, int regnum) 227*6881a400Schristos { 228*6881a400Schristos struct sparc_frame_cache *cache = 229*6881a400Schristos sparc32nbsd_sigcontext_frame_cache (this_frame, this_cache); 230*6881a400Schristos 231*6881a400Schristos return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum); 232*6881a400Schristos } 233*6881a400Schristos 234*6881a400Schristos static int 235*6881a400Schristos sparc32nbsd_sigcontext_frame_sniffer (const struct frame_unwind *self, 236*6881a400Schristos frame_info_ptr this_frame, 237*6881a400Schristos void **this_cache) 238*6881a400Schristos { 239*6881a400Schristos CORE_ADDR pc = get_frame_pc (this_frame); 240*6881a400Schristos const char *name; 241*6881a400Schristos 242*6881a400Schristos find_pc_partial_function (pc, &name, NULL, NULL); 243*6881a400Schristos if (sparc32nbsd_pc_in_sigtramp (pc, name)) 244*6881a400Schristos { 245*6881a400Schristos if (name == NULL || !startswith (name, "__sigtramp_sigcontext")) 246*6881a400Schristos return 1; 247*6881a400Schristos } 248*6881a400Schristos 249*6881a400Schristos return 0; 250*6881a400Schristos } 251*6881a400Schristos 252*6881a400Schristos static const struct frame_unwind sparc32nbsd_sigcontext_frame_unwind = 253*6881a400Schristos { 254*6881a400Schristos "sparc32 netbsd sigcontext", 255*6881a400Schristos SIGTRAMP_FRAME, 256*6881a400Schristos default_frame_unwind_stop_reason, 257*6881a400Schristos sparc32nbsd_sigcontext_frame_this_id, 258*6881a400Schristos sparc32nbsd_sigcontext_frame_prev_register, 259*6881a400Schristos NULL, 260*6881a400Schristos sparc32nbsd_sigcontext_frame_sniffer 261*6881a400Schristos }; 262*6881a400Schristos 263*6881a400Schristos /* Return the address of a system call's alternative return 264*6881a400Schristos address. */ 265*6881a400Schristos 266*6881a400Schristos CORE_ADDR 267*6881a400Schristos sparcnbsd_step_trap (frame_info_ptr frame, unsigned long insn) 268*6881a400Schristos { 269*6881a400Schristos if ((X_I (insn) == 0 && X_RS1 (insn) == 0 && X_RS2 (insn) == 0) 270*6881a400Schristos || (X_I (insn) == 1 && X_RS1 (insn) == 0 && (insn & 0x7f) == 0)) 271*6881a400Schristos { 272*6881a400Schristos /* "New" system call. */ 273*6881a400Schristos ULONGEST number = get_frame_register_unsigned (frame, SPARC_G1_REGNUM); 274*6881a400Schristos 275*6881a400Schristos if (number & 0x400) 276*6881a400Schristos return get_frame_register_unsigned (frame, SPARC_G2_REGNUM); 277*6881a400Schristos if (number & 0x800) 278*6881a400Schristos return get_frame_register_unsigned (frame, SPARC_G7_REGNUM); 279*6881a400Schristos } 280*6881a400Schristos 281*6881a400Schristos return 0; 282*6881a400Schristos } 283*6881a400Schristos 284*6881a400Schristos 285*6881a400Schristos static const struct regset sparc32nbsd_gregset = 286*6881a400Schristos { 287*6881a400Schristos NULL, sparc32nbsd_supply_gregset, NULL 288*6881a400Schristos }; 289*6881a400Schristos 290*6881a400Schristos static const struct regset sparc32nbsd_fpregset = 291*6881a400Schristos { 292*6881a400Schristos NULL, sparc32nbsd_supply_fpregset, NULL 293*6881a400Schristos }; 294*6881a400Schristos 295*6881a400Schristos static void 296*6881a400Schristos sparc32nbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) 297*6881a400Schristos { 298*6881a400Schristos sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (gdbarch); 299*6881a400Schristos 300*6881a400Schristos nbsd_init_abi (info, gdbarch); 301*6881a400Schristos 302*6881a400Schristos /* NetBSD doesn't support the 128-bit `long double' from the psABI. */ 303*6881a400Schristos set_gdbarch_long_double_bit (gdbarch, 64); 304*6881a400Schristos set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double); 305*6881a400Schristos 306*6881a400Schristos tdep->gregset = &sparc32nbsd_gregset; 307*6881a400Schristos tdep->sizeof_gregset = 20 * 4; 308*6881a400Schristos 309*6881a400Schristos tdep->fpregset = &sparc32nbsd_fpregset; 310*6881a400Schristos tdep->sizeof_fpregset = 33 * 4; 311*6881a400Schristos 312*6881a400Schristos /* Make sure we can single-step "new" syscalls. */ 313*6881a400Schristos tdep->step_trap = sparcnbsd_step_trap; 314*6881a400Schristos 315*6881a400Schristos frame_unwind_append_unwinder (gdbarch, &sparc32nbsd_sigcontext_frame_unwind); 316*6881a400Schristos 317*6881a400Schristos set_solib_svr4_fetch_link_map_offsets 318*6881a400Schristos (gdbarch, svr4_ilp32_fetch_link_map_offsets); 319*6881a400Schristos } 320*6881a400Schristos 321*6881a400Schristos void _initialize_sparcnbsd_tdep (); 322*6881a400Schristos void 323*6881a400Schristos _initialize_sparcnbsd_tdep () 324*6881a400Schristos { 325*6881a400Schristos gdbarch_register_osabi (bfd_arch_sparc, 0, GDB_OSABI_NETBSD, 326*6881a400Schristos sparc32nbsd_init_abi); 327*6881a400Schristos } 328