1*6881a400Schristos /* Native-dependent code for NetBSD/sparc64. 2*6881a400Schristos 3*6881a400Schristos Copyright (C) 2003-2023 Free Software Foundation, Inc. 4*6881a400Schristos 5*6881a400Schristos This file is part of GDB. 6*6881a400Schristos 7*6881a400Schristos This program is free software; you can redistribute it and/or modify 8*6881a400Schristos it under the terms of the GNU General Public License as published by 9*6881a400Schristos the Free Software Foundation; either version 3 of the License, or 10*6881a400Schristos (at your option) any later version. 11*6881a400Schristos 12*6881a400Schristos This program is distributed in the hope that it will be useful, 13*6881a400Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 14*6881a400Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*6881a400Schristos GNU General Public License for more details. 16*6881a400Schristos 17*6881a400Schristos You should have received a copy of the GNU General Public License 18*6881a400Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19*6881a400Schristos 20*6881a400Schristos #include "defs.h" 21*6881a400Schristos #include "gdbcore.h" 22*6881a400Schristos #include "regcache.h" 23*6881a400Schristos #include "target.h" 24*6881a400Schristos 25*6881a400Schristos #include "sparc64-tdep.h" 26*6881a400Schristos #include "sparc-nat.h" 27*6881a400Schristos #include "netbsd-nat.h" 28*6881a400Schristos 29*6881a400Schristos /* NetBSD is different from the other OSes that support both SPARC and 30*6881a400Schristos UltraSPARC in that the result of ptrace(2) depends on whether the 31*6881a400Schristos traced process is 32-bit or 64-bit. */ 32*6881a400Schristos 33*6881a400Schristos static void 34*6881a400Schristos sparc64nbsd_supply_gregset (const struct sparc_gregmap *gregmap, 35*6881a400Schristos struct regcache *regcache, 36*6881a400Schristos int regnum, const void *gregs) 37*6881a400Schristos { 38*6881a400Schristos int sparc32 = (gdbarch_ptr_bit (regcache->arch ()) == 32); 39*6881a400Schristos 40*6881a400Schristos if (sparc32) 41*6881a400Schristos sparc32_supply_gregset (&sparc32nbsd_gregmap, regcache, regnum, gregs); 42*6881a400Schristos else 43*6881a400Schristos sparc64_supply_gregset (&sparc64nbsd_gregmap, regcache, regnum, gregs); 44*6881a400Schristos } 45*6881a400Schristos 46*6881a400Schristos static void 47*6881a400Schristos sparc64nbsd_collect_gregset (const struct sparc_gregmap *gregmap, 48*6881a400Schristos const struct regcache *regcache, 49*6881a400Schristos int regnum, void *gregs) 50*6881a400Schristos { 51*6881a400Schristos int sparc32 = (gdbarch_ptr_bit (regcache->arch ()) == 32); 52*6881a400Schristos 53*6881a400Schristos if (sparc32) 54*6881a400Schristos sparc32_collect_gregset (&sparc32nbsd_gregmap, regcache, regnum, gregs); 55*6881a400Schristos else 56*6881a400Schristos sparc64_collect_gregset (&sparc64nbsd_gregmap, regcache, regnum, gregs); 57*6881a400Schristos } 58*6881a400Schristos 59*6881a400Schristos static void 60*6881a400Schristos sparc64nbsd_supply_fpregset (const struct sparc_fpregmap *fpregmap, 61*6881a400Schristos struct regcache *regcache, 62*6881a400Schristos int regnum, const void *fpregs) 63*6881a400Schristos { 64*6881a400Schristos int sparc32 = (gdbarch_ptr_bit (regcache->arch ()) == 32); 65*6881a400Schristos 66*6881a400Schristos if (sparc32) 67*6881a400Schristos sparc32_supply_fpregset (&sparc32_bsd_fpregmap, regcache, regnum, fpregs); 68*6881a400Schristos else 69*6881a400Schristos sparc64_supply_fpregset (&sparc64_bsd_fpregmap, regcache, regnum, fpregs); 70*6881a400Schristos } 71*6881a400Schristos 72*6881a400Schristos static void 73*6881a400Schristos sparc64nbsd_collect_fpregset (const struct sparc_fpregmap *fpregmap, 74*6881a400Schristos const struct regcache *regcache, 75*6881a400Schristos int regnum, void *fpregs) 76*6881a400Schristos { 77*6881a400Schristos int sparc32 = (gdbarch_ptr_bit (regcache->arch ()) == 32); 78*6881a400Schristos 79*6881a400Schristos if (sparc32) 80*6881a400Schristos sparc32_collect_fpregset (&sparc32_bsd_fpregmap, regcache, regnum, fpregs); 81*6881a400Schristos else 82*6881a400Schristos sparc64_collect_fpregset (&sparc64_bsd_fpregmap, regcache, regnum, fpregs); 83*6881a400Schristos } 84*6881a400Schristos 85*6881a400Schristos /* Determine whether `gregset_t' contains register REGNUM. */ 86*6881a400Schristos 87*6881a400Schristos static int 88*6881a400Schristos sparc64nbsd_gregset_supplies_p (struct gdbarch *gdbarch, int regnum) 89*6881a400Schristos { 90*6881a400Schristos if (gdbarch_ptr_bit (gdbarch) == 32) 91*6881a400Schristos return sparc32_gregset_supplies_p (gdbarch, regnum); 92*6881a400Schristos 93*6881a400Schristos /* Integer registers. */ 94*6881a400Schristos if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_G7_REGNUM) 95*6881a400Schristos || (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O7_REGNUM) 96*6881a400Schristos || (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_L7_REGNUM) 97*6881a400Schristos || (regnum >= SPARC_I0_REGNUM && regnum <= SPARC_I7_REGNUM)) 98*6881a400Schristos return 1; 99*6881a400Schristos 100*6881a400Schristos /* Control registers. */ 101*6881a400Schristos if (regnum == SPARC64_PC_REGNUM 102*6881a400Schristos || regnum == SPARC64_NPC_REGNUM 103*6881a400Schristos || regnum == SPARC64_STATE_REGNUM 104*6881a400Schristos || regnum == SPARC64_Y_REGNUM) 105*6881a400Schristos return 1; 106*6881a400Schristos 107*6881a400Schristos return 0; 108*6881a400Schristos } 109*6881a400Schristos 110*6881a400Schristos /* Determine whether `fpregset_t' contains register REGNUM. */ 111*6881a400Schristos 112*6881a400Schristos static int 113*6881a400Schristos sparc64nbsd_fpregset_supplies_p (struct gdbarch *gdbarch, int regnum) 114*6881a400Schristos { 115*6881a400Schristos if (gdbarch_ptr_bit (gdbarch) == 32) 116*6881a400Schristos return sparc32_fpregset_supplies_p (gdbarch, regnum); 117*6881a400Schristos 118*6881a400Schristos /* Floating-point registers. */ 119*6881a400Schristos if ((regnum >= SPARC_F0_REGNUM && regnum <= SPARC_F31_REGNUM) 120*6881a400Schristos || (regnum >= SPARC64_F32_REGNUM && regnum <= SPARC64_F62_REGNUM)) 121*6881a400Schristos return 1; 122*6881a400Schristos 123*6881a400Schristos /* Control registers. */ 124*6881a400Schristos if (regnum == SPARC64_FSR_REGNUM) 125*6881a400Schristos return 1; 126*6881a400Schristos 127*6881a400Schristos return 0; 128*6881a400Schristos } 129*6881a400Schristos 130*6881a400Schristos 131*6881a400Schristos /* Support for debugging kernel virtual memory images. */ 132*6881a400Schristos 133*6881a400Schristos #include <sys/types.h> 134*6881a400Schristos #include <machine/pcb.h> 135*6881a400Schristos 136*6881a400Schristos #include "bsd-kvm.h" 137*6881a400Schristos 138*6881a400Schristos static int 139*6881a400Schristos sparc64nbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb) 140*6881a400Schristos { 141*6881a400Schristos u_int64_t state; 142*6881a400Schristos int regnum; 143*6881a400Schristos 144*6881a400Schristos /* The following is true for NetBSD 1.6.2: 145*6881a400Schristos 146*6881a400Schristos The pcb contains %sp and %pc, %pstate and %cwp. From this 147*6881a400Schristos information we reconstruct the register state as it would look 148*6881a400Schristos when we just returned from cpu_switch(). */ 149*6881a400Schristos 150*6881a400Schristos /* The stack pointer shouldn't be zero. */ 151*6881a400Schristos if (pcb->pcb_sp == 0) 152*6881a400Schristos return 0; 153*6881a400Schristos 154*6881a400Schristos /* If the program counter is zero, this is probably a core dump, and 155*6881a400Schristos we can get %pc from the stack. */ 156*6881a400Schristos if (pcb->pcb_pc == 0) 157*6881a400Schristos read_memory(pcb->pcb_sp + BIAS - 176 + (11 * 8), 158*6881a400Schristos (gdb_byte *)&pcb->pcb_pc, sizeof pcb->pcb_pc); 159*6881a400Schristos 160*6881a400Schristos regcache->raw_supply (SPARC_SP_REGNUM, &pcb->pcb_sp); 161*6881a400Schristos regcache->raw_supply (SPARC64_PC_REGNUM, &pcb->pcb_pc); 162*6881a400Schristos 163*6881a400Schristos state = pcb->pcb_pstate << 8 | pcb->pcb_cwp; 164*6881a400Schristos regcache->raw_supply (SPARC64_STATE_REGNUM, &state); 165*6881a400Schristos 166*6881a400Schristos sparc_supply_rwindow (regcache, pcb->pcb_sp, -1); 167*6881a400Schristos 168*6881a400Schristos return 1; 169*6881a400Schristos } 170*6881a400Schristos 171*6881a400Schristos /* We've got nothing to add to the generic SPARC target. */ 172*6881a400Schristos static sparc_target<nbsd_nat_target> the_sparc64_nbsd_nat_target; 173*6881a400Schristos 174*6881a400Schristos void _initialize_sparc64nbsd_nat (); 175*6881a400Schristos void 176*6881a400Schristos _initialize_sparc64nbsd_nat () 177*6881a400Schristos { 178*6881a400Schristos sparc_supply_gregset = sparc64nbsd_supply_gregset; 179*6881a400Schristos sparc_collect_gregset = sparc64nbsd_collect_gregset; 180*6881a400Schristos sparc_supply_fpregset = sparc64nbsd_supply_fpregset; 181*6881a400Schristos sparc_collect_fpregset = sparc64nbsd_collect_fpregset; 182*6881a400Schristos sparc_gregset_supplies_p = sparc64nbsd_gregset_supplies_p; 183*6881a400Schristos sparc_fpregset_supplies_p = sparc64nbsd_fpregset_supplies_p; 184*6881a400Schristos 185*6881a400Schristos add_inf_child_target (&the_sparc64_nbsd_nat_target); 186*6881a400Schristos 187*6881a400Schristos /* Support debugging kernel virtual memory images. */ 188*6881a400Schristos bsd_kvm_add_target (sparc64nbsd_supply_pcb); 189*6881a400Schristos } 190