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