1*6881a400Schristos /* Native-dependent code for NetBSD/powerpc. 2*6881a400Schristos 3*6881a400Schristos Copyright (C) 2002-2023 Free Software Foundation, Inc. 4*6881a400Schristos 5*6881a400Schristos Contributed by Wasabi Systems, Inc. 6*6881a400Schristos 7*6881a400Schristos This file is part of GDB. 8*6881a400Schristos 9*6881a400Schristos This program is free software; you can redistribute it and/or modify 10*6881a400Schristos it under the terms of the GNU General Public License as published by 11*6881a400Schristos the Free Software Foundation; either version 3 of the License, or 12*6881a400Schristos (at your option) any later version. 13*6881a400Schristos 14*6881a400Schristos This program is distributed in the hope that it will be useful, 15*6881a400Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 16*6881a400Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17*6881a400Schristos GNU General Public License for more details. 18*6881a400Schristos 19*6881a400Schristos You should have received a copy of the GNU General Public License 20*6881a400Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 21*6881a400Schristos 22*6881a400Schristos /* We define this to get types like register_t. */ 23*6881a400Schristos #include "defs.h" 24*6881a400Schristos 25*6881a400Schristos #include <sys/types.h> 26*6881a400Schristos #include <sys/ptrace.h> 27*6881a400Schristos #include <machine/reg.h> 28*6881a400Schristos #include <machine/frame.h> 29*6881a400Schristos #include <machine/pcb.h> 30*6881a400Schristos 31*6881a400Schristos #include "gdbcore.h" 32*6881a400Schristos #include "inferior.h" 33*6881a400Schristos #include "regcache.h" 34*6881a400Schristos 35*6881a400Schristos #include "ppc-tdep.h" 36*6881a400Schristos #include "ppc-netbsd-tdep.h" 37*6881a400Schristos #include "bsd-kvm.h" 38*6881a400Schristos #include "inf-ptrace.h" 39*6881a400Schristos #include "netbsd-nat.h" 40*6881a400Schristos 41*6881a400Schristos struct ppc_nbsd_nat_target final : public nbsd_nat_target 42*6881a400Schristos { 43*6881a400Schristos void fetch_registers (struct regcache *, int) override; 44*6881a400Schristos void store_registers (struct regcache *, int) override; 45*6881a400Schristos }; 46*6881a400Schristos 47*6881a400Schristos static ppc_nbsd_nat_target the_ppc_nbsd_nat_target; 48*6881a400Schristos 49*6881a400Schristos /* Returns true if PT_GETREGS fetches this register. */ 50*6881a400Schristos 51*6881a400Schristos static int 52*6881a400Schristos getregs_supplies (struct gdbarch *gdbarch, int regnum) 53*6881a400Schristos { 54*6881a400Schristos ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch); 55*6881a400Schristos 56*6881a400Schristos return ((regnum >= tdep->ppc_gp0_regnum 57*6881a400Schristos && regnum < tdep->ppc_gp0_regnum + ppc_num_gprs) 58*6881a400Schristos || regnum == tdep->ppc_lr_regnum 59*6881a400Schristos || regnum == tdep->ppc_cr_regnum 60*6881a400Schristos || regnum == tdep->ppc_xer_regnum 61*6881a400Schristos || regnum == tdep->ppc_ctr_regnum 62*6881a400Schristos || regnum == gdbarch_pc_regnum (gdbarch)); 63*6881a400Schristos } 64*6881a400Schristos 65*6881a400Schristos /* Like above, but for PT_GETFPREGS. */ 66*6881a400Schristos 67*6881a400Schristos static int 68*6881a400Schristos getfpregs_supplies (struct gdbarch *gdbarch, int regnum) 69*6881a400Schristos { 70*6881a400Schristos ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch); 71*6881a400Schristos 72*6881a400Schristos /* FIXME: jimb/2004-05-05: Some PPC variants don't have floating 73*6881a400Schristos point registers. Traditionally, GDB's register set has still 74*6881a400Schristos listed the floating point registers for such machines, so this 75*6881a400Schristos code is harmless. However, the new E500 port actually omits the 76*6881a400Schristos floating point registers entirely from the register set --- they 77*6881a400Schristos don't even have register numbers assigned to them. 78*6881a400Schristos 79*6881a400Schristos It's not clear to me how best to update this code, so this assert 80*6881a400Schristos will alert the first person to encounter the NetBSD/E500 81*6881a400Schristos combination to the problem. */ 82*6881a400Schristos gdb_assert (ppc_floating_point_unit_p (gdbarch)); 83*6881a400Schristos 84*6881a400Schristos return ((regnum >= tdep->ppc_fp0_regnum 85*6881a400Schristos && regnum < tdep->ppc_fp0_regnum + ppc_num_fprs) 86*6881a400Schristos || regnum == tdep->ppc_fpscr_regnum); 87*6881a400Schristos } 88*6881a400Schristos 89*6881a400Schristos void 90*6881a400Schristos ppc_nbsd_nat_target::fetch_registers (struct regcache *regcache, int regnum) 91*6881a400Schristos { 92*6881a400Schristos struct gdbarch *gdbarch = regcache->arch (); 93*6881a400Schristos pid_t pid = regcache->ptid ().pid (); 94*6881a400Schristos int lwp = regcache->ptid ().lwp (); 95*6881a400Schristos 96*6881a400Schristos if (regnum == -1 || getregs_supplies (gdbarch, regnum)) 97*6881a400Schristos { 98*6881a400Schristos struct reg regs; 99*6881a400Schristos 100*6881a400Schristos if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) ®s, lwp) == -1) 101*6881a400Schristos perror_with_name (_("Couldn't get registers")); 102*6881a400Schristos 103*6881a400Schristos ppc_supply_gregset (&ppcnbsd_gregset, regcache, 104*6881a400Schristos regnum, ®s, sizeof regs); 105*6881a400Schristos } 106*6881a400Schristos 107*6881a400Schristos if (regnum == -1 || getfpregs_supplies (gdbarch, regnum)) 108*6881a400Schristos { 109*6881a400Schristos struct fpreg fpregs; 110*6881a400Schristos 111*6881a400Schristos if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1) 112*6881a400Schristos perror_with_name (_("Couldn't get FP registers")); 113*6881a400Schristos 114*6881a400Schristos ppc_supply_fpregset (&ppcnbsd_fpregset, regcache, 115*6881a400Schristos regnum, &fpregs, sizeof fpregs); 116*6881a400Schristos } 117*6881a400Schristos } 118*6881a400Schristos 119*6881a400Schristos void 120*6881a400Schristos ppc_nbsd_nat_target::store_registers (struct regcache *regcache, int regnum) 121*6881a400Schristos { 122*6881a400Schristos struct gdbarch *gdbarch = regcache->arch (); 123*6881a400Schristos pid_t pid = regcache->ptid ().pid (); 124*6881a400Schristos int lwp = regcache->ptid ().lwp (); 125*6881a400Schristos 126*6881a400Schristos if (regnum == -1 || getregs_supplies (gdbarch, regnum)) 127*6881a400Schristos { 128*6881a400Schristos struct reg regs; 129*6881a400Schristos 130*6881a400Schristos if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) ®s, lwp) == -1) 131*6881a400Schristos perror_with_name (_("Couldn't get registers")); 132*6881a400Schristos 133*6881a400Schristos ppc_collect_gregset (&ppcnbsd_gregset, regcache, 134*6881a400Schristos regnum, ®s, sizeof regs); 135*6881a400Schristos 136*6881a400Schristos if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) ®s, lwp) == -1) 137*6881a400Schristos perror_with_name (_("Couldn't write registers")); 138*6881a400Schristos } 139*6881a400Schristos 140*6881a400Schristos if (regnum == -1 || getfpregs_supplies (gdbarch, regnum)) 141*6881a400Schristos { 142*6881a400Schristos struct fpreg fpregs; 143*6881a400Schristos 144*6881a400Schristos if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1) 145*6881a400Schristos perror_with_name (_("Couldn't get FP registers")); 146*6881a400Schristos 147*6881a400Schristos ppc_collect_fpregset (&ppcnbsd_fpregset, regcache, 148*6881a400Schristos regnum, &fpregs, sizeof fpregs); 149*6881a400Schristos 150*6881a400Schristos if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1) 151*6881a400Schristos perror_with_name (_("Couldn't set FP registers")); 152*6881a400Schristos } 153*6881a400Schristos } 154*6881a400Schristos 155*6881a400Schristos static int 156*6881a400Schristos ppcnbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb) 157*6881a400Schristos { 158*6881a400Schristos struct switchframe sf; 159*6881a400Schristos struct callframe cf; 160*6881a400Schristos struct gdbarch *gdbarch = regcache->arch (); 161*6881a400Schristos ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch); 162*6881a400Schristos int i; 163*6881a400Schristos 164*6881a400Schristos /* The stack pointer shouldn't be zero. */ 165*6881a400Schristos if (pcb->pcb_sp == 0) 166*6881a400Schristos return 0; 167*6881a400Schristos 168*6881a400Schristos read_memory (pcb->pcb_sp, (gdb_byte *)&sf, sizeof sf); 169*6881a400Schristos regcache->raw_supply (tdep->ppc_cr_regnum, &sf.sf_cr); 170*6881a400Schristos regcache->raw_supply (tdep->ppc_gp0_regnum + 2, &sf.sf_fixreg2); 171*6881a400Schristos for (i = 0 ; i < 19 ; i++) 172*6881a400Schristos regcache->raw_supply (tdep->ppc_gp0_regnum + 13 + i, &sf.sf_fixreg[i]); 173*6881a400Schristos 174*6881a400Schristos read_memory(sf.sf_sp, (gdb_byte *)&cf, sizeof(cf)); 175*6881a400Schristos regcache->raw_supply (tdep->ppc_gp0_regnum + 30, &cf.cf_r30); 176*6881a400Schristos regcache->raw_supply (tdep->ppc_gp0_regnum + 31, &cf.cf_r31); 177*6881a400Schristos regcache->raw_supply (tdep->ppc_gp0_regnum + 1, &cf.cf_sp); 178*6881a400Schristos 179*6881a400Schristos read_memory(cf.cf_sp, (gdb_byte *)&cf, sizeof(cf)); 180*6881a400Schristos regcache->raw_supply (tdep->ppc_lr_regnum, &cf.cf_lr); 181*6881a400Schristos regcache->raw_supply (gdbarch_pc_regnum (gdbarch), &cf.cf_lr); 182*6881a400Schristos 183*6881a400Schristos return 1; 184*6881a400Schristos } 185*6881a400Schristos 186*6881a400Schristos void _initialize_ppcnbsd_nat (); 187*6881a400Schristos void 188*6881a400Schristos _initialize_ppcnbsd_nat () 189*6881a400Schristos { 190*6881a400Schristos /* Support debugging kernel virtual memory images. */ 191*6881a400Schristos bsd_kvm_add_target (ppcnbsd_supply_pcb); 192*6881a400Schristos 193*6881a400Schristos add_inf_child_target (&the_ppc_nbsd_nat_target); 194*6881a400Schristos } 195