xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/hppa-netbsd-nat.c (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
1*6881a400Schristos /* Native-dependent code for NetBSD/hppa.
2*6881a400Schristos 
3*6881a400Schristos    Copyright (C) 2008-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 "inferior.h"
22*6881a400Schristos #include "regcache.h"
23*6881a400Schristos 
24*6881a400Schristos #include <sys/types.h>
25*6881a400Schristos #include <sys/ptrace.h>
26*6881a400Schristos #include <machine/reg.h>
27*6881a400Schristos 
28*6881a400Schristos #include "hppa-tdep.h"
29*6881a400Schristos #include "inf-ptrace.h"
30*6881a400Schristos 
31*6881a400Schristos #include "netbsd-nat.h"
32*6881a400Schristos 
33*6881a400Schristos class hppa_nbsd_nat_target final : public nbsd_nat_target
34*6881a400Schristos {
35*6881a400Schristos   void fetch_registers (struct regcache *, int) override;
36*6881a400Schristos   void store_registers (struct regcache *, int) override;
37*6881a400Schristos };
38*6881a400Schristos 
39*6881a400Schristos static hppa_nbsd_nat_target the_hppa_nbsd_nat_target;
40*6881a400Schristos 
41*6881a400Schristos static int
42*6881a400Schristos hppanbsd_gregset_supplies_p (int regnum)
43*6881a400Schristos {
44*6881a400Schristos   return ((regnum >= HPPA_R0_REGNUM && regnum <= HPPA_R31_REGNUM) ||
45*6881a400Schristos 	  (regnum >= HPPA_SAR_REGNUM && regnum <= HPPA_PCSQ_TAIL_REGNUM) ||
46*6881a400Schristos 	  regnum == HPPA_IPSW_REGNUM ||
47*6881a400Schristos 	  (regnum >= HPPA_SR4_REGNUM && regnum <= HPPA_SR4_REGNUM + 5));
48*6881a400Schristos }
49*6881a400Schristos 
50*6881a400Schristos static int
51*6881a400Schristos hppanbsd_fpregset_supplies_p (int regnum)
52*6881a400Schristos {
53*6881a400Schristos   return (regnum >= HPPA_FP0_REGNUM && regnum <= HPPA_FP31R_REGNUM);
54*6881a400Schristos }
55*6881a400Schristos 
56*6881a400Schristos /* Supply the general-purpose registers stored in GREGS to REGCACHE.  */
57*6881a400Schristos 
58*6881a400Schristos static void
59*6881a400Schristos hppanbsd_supply_gregset (struct regcache *regcache, const void *gregs)
60*6881a400Schristos {
61*6881a400Schristos   const char *regs = (const char *)gregs;
62*6881a400Schristos   int regnum;
63*6881a400Schristos 
64*6881a400Schristos   for (regnum = HPPA_R1_REGNUM; regnum <= HPPA_R31_REGNUM; regnum++)
65*6881a400Schristos     regcache->raw_supply (regnum, regs + regnum * 4);
66*6881a400Schristos 
67*6881a400Schristos   regcache->raw_supply (HPPA_SAR_REGNUM, regs + 32 * 4);
68*6881a400Schristos   regcache->raw_supply (HPPA_PCSQ_HEAD_REGNUM, regs + 33 * 4);
69*6881a400Schristos   regcache->raw_supply (HPPA_PCSQ_TAIL_REGNUM, regs + 34 * 4);
70*6881a400Schristos   regcache->raw_supply (HPPA_PCOQ_HEAD_REGNUM, regs + 35 * 4);
71*6881a400Schristos   regcache->raw_supply (HPPA_PCOQ_TAIL_REGNUM, regs + 36 * 4);
72*6881a400Schristos   regcache->raw_supply (HPPA_IPSW_REGNUM, regs);
73*6881a400Schristos   regcache->raw_supply (HPPA_SR4_REGNUM, regs + 41 * 4);
74*6881a400Schristos   regcache->raw_supply (HPPA_SR4_REGNUM + 1, regs + 37 * 4);
75*6881a400Schristos   regcache->raw_supply (HPPA_SR4_REGNUM + 2, regs + 38 * 4);
76*6881a400Schristos   regcache->raw_supply (HPPA_SR4_REGNUM + 3, regs + 39 * 4);
77*6881a400Schristos   regcache->raw_supply (HPPA_SR4_REGNUM + 4, regs + 40 * 4);
78*6881a400Schristos }
79*6881a400Schristos 
80*6881a400Schristos /* Supply the floating-point registers stored in FPREGS to REGCACHE.  */
81*6881a400Schristos 
82*6881a400Schristos static void
83*6881a400Schristos hppanbsd_supply_fpregset (struct regcache *regcache, const void *fpregs)
84*6881a400Schristos {
85*6881a400Schristos   const char *regs = (const char *)fpregs;
86*6881a400Schristos   int regnum;
87*6881a400Schristos 
88*6881a400Schristos   for (regnum = HPPA_FP0_REGNUM; regnum <= HPPA_FP31R_REGNUM;
89*6881a400Schristos        regnum += 2, regs += 8)
90*6881a400Schristos     {
91*6881a400Schristos       regcache->raw_supply (regnum, regs);
92*6881a400Schristos       regcache->raw_supply (regnum + 1, regs + 4);
93*6881a400Schristos     }
94*6881a400Schristos }
95*6881a400Schristos 
96*6881a400Schristos /* Collect the general-purpose registers from REGCACHE and store them
97*6881a400Schristos    in GREGS.  */
98*6881a400Schristos 
99*6881a400Schristos static void
100*6881a400Schristos hppanbsd_collect_gregset (const struct regcache *regcache,
101*6881a400Schristos 			  void *gregs, int regnum)
102*6881a400Schristos {
103*6881a400Schristos   char *regs = (char *)gregs;
104*6881a400Schristos   int i;
105*6881a400Schristos 
106*6881a400Schristos   for (i = HPPA_R1_REGNUM; i <= HPPA_R31_REGNUM; i++)
107*6881a400Schristos     {
108*6881a400Schristos       if (regnum == -1 || regnum == i)
109*6881a400Schristos 	regcache->raw_collect (i, regs + i * 4);
110*6881a400Schristos     }
111*6881a400Schristos 
112*6881a400Schristos   if (regnum == -1 || regnum == HPPA_IPSW_REGNUM)
113*6881a400Schristos     regcache->raw_collect (HPPA_IPSW_REGNUM, regs);
114*6881a400Schristos   if (regnum == -1 || regnum == HPPA_PCOQ_HEAD_REGNUM)
115*6881a400Schristos     regcache->raw_collect (HPPA_PCOQ_HEAD_REGNUM, regs + 35 * 4);
116*6881a400Schristos   if (regnum == -1 || regnum == HPPA_PCOQ_TAIL_REGNUM)
117*6881a400Schristos     regcache->raw_collect (HPPA_PCOQ_TAIL_REGNUM, regs + 36 * 4);
118*6881a400Schristos 
119*6881a400Schristos   if (regnum == -1 || regnum == HPPA_SAR_REGNUM)
120*6881a400Schristos     regcache->raw_collect (HPPA_SAR_REGNUM, regs + 32 * 4);
121*6881a400Schristos   if (regnum == -1 || regnum == HPPA_PCSQ_HEAD_REGNUM)
122*6881a400Schristos     regcache->raw_collect (HPPA_PCSQ_HEAD_REGNUM, regs + 33 * 4);
123*6881a400Schristos   if (regnum == -1 || regnum == HPPA_PCSQ_TAIL_REGNUM)
124*6881a400Schristos     regcache->raw_collect (HPPA_PCSQ_TAIL_REGNUM, regs + 34 * 4);
125*6881a400Schristos   if (regnum == -1 || regnum == HPPA_PCOQ_HEAD_REGNUM)
126*6881a400Schristos     regcache->raw_collect (HPPA_PCOQ_HEAD_REGNUM, regs + 35 * 4);
127*6881a400Schristos   if (regnum == -1 || regnum == HPPA_PCOQ_TAIL_REGNUM)
128*6881a400Schristos     regcache->raw_collect (HPPA_PCOQ_TAIL_REGNUM, regs + 36 * 4);
129*6881a400Schristos   if (regnum == -1 || regnum == HPPA_IPSW_REGNUM)
130*6881a400Schristos     regcache->raw_collect (HPPA_IPSW_REGNUM, regs);
131*6881a400Schristos   if (regnum == -1 || regnum == HPPA_SR4_REGNUM)
132*6881a400Schristos     regcache->raw_collect (HPPA_SR4_REGNUM, regs + 41 * 4);
133*6881a400Schristos   if (regnum == -1 || regnum == HPPA_SR4_REGNUM + 1)
134*6881a400Schristos     regcache->raw_collect (HPPA_SR4_REGNUM + 1, regs + 37 * 4);
135*6881a400Schristos   if (regnum == -1 || regnum == HPPA_SR4_REGNUM + 2)
136*6881a400Schristos     regcache->raw_collect (HPPA_SR4_REGNUM + 2, regs + 38 * 4);
137*6881a400Schristos   if (regnum == -1 || regnum == HPPA_SR4_REGNUM + 3)
138*6881a400Schristos     regcache->raw_collect (HPPA_SR4_REGNUM + 3, regs + 39 * 4);
139*6881a400Schristos   if (regnum == -1 || regnum == HPPA_SR4_REGNUM + 4)
140*6881a400Schristos     regcache->raw_collect (HPPA_SR4_REGNUM + 4, regs + 40 * 4);
141*6881a400Schristos }
142*6881a400Schristos 
143*6881a400Schristos /* Collect the floating-point registers from REGCACHE and store them
144*6881a400Schristos    in FPREGS.  */
145*6881a400Schristos 
146*6881a400Schristos static void
147*6881a400Schristos hppanbsd_collect_fpregset (const struct regcache *regcache,
148*6881a400Schristos 			  void *fpregs, int regnum)
149*6881a400Schristos {
150*6881a400Schristos   char *regs = (char *)fpregs;
151*6881a400Schristos   int i;
152*6881a400Schristos 
153*6881a400Schristos   for (i = HPPA_FP0_REGNUM; i <= HPPA_FP31R_REGNUM; i += 2, regs += 8)
154*6881a400Schristos     {
155*6881a400Schristos       if (regnum == -1 || regnum == i || regnum == i + 1)
156*6881a400Schristos 	{
157*6881a400Schristos 	  regcache->raw_collect (i, regs);
158*6881a400Schristos 	  regcache->raw_collect (i + 1, regs + 4);
159*6881a400Schristos 	}
160*6881a400Schristos     }
161*6881a400Schristos }
162*6881a400Schristos 
163*6881a400Schristos 
164*6881a400Schristos /* Fetch register REGNUM from the inferior.  If REGNUM is -1, do this
165*6881a400Schristos    for all registers (including the floating-point registers).  */
166*6881a400Schristos 
167*6881a400Schristos void
168*6881a400Schristos hppa_nbsd_nat_target::fetch_registers (struct regcache *regcache, int regnum)
169*6881a400Schristos 
170*6881a400Schristos {
171*6881a400Schristos   pid_t pid = regcache->ptid ().pid ();
172*6881a400Schristos   int lwp = regcache->ptid ().lwp ();
173*6881a400Schristos 
174*6881a400Schristos   if (regnum == -1 || hppanbsd_gregset_supplies_p (regnum))
175*6881a400Schristos     {
176*6881a400Schristos       struct reg regs;
177*6881a400Schristos 
178*6881a400Schristos       if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
179*6881a400Schristos 	perror_with_name (_("Couldn't get registers"));
180*6881a400Schristos 
181*6881a400Schristos       hppanbsd_supply_gregset (regcache, &regs);
182*6881a400Schristos     }
183*6881a400Schristos 
184*6881a400Schristos   if (regnum == -1 || hppanbsd_fpregset_supplies_p (regnum))
185*6881a400Schristos     {
186*6881a400Schristos       struct fpreg fpregs;
187*6881a400Schristos 
188*6881a400Schristos       if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1)
189*6881a400Schristos 	perror_with_name (_("Couldn't get floating point status"));
190*6881a400Schristos 
191*6881a400Schristos       hppanbsd_supply_fpregset (regcache, &fpregs);
192*6881a400Schristos     }
193*6881a400Schristos }
194*6881a400Schristos 
195*6881a400Schristos /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
196*6881a400Schristos    this for all registers (including the floating-point registers).  */
197*6881a400Schristos 
198*6881a400Schristos void
199*6881a400Schristos hppa_nbsd_nat_target::store_registers (struct regcache *regcache, int regnum)
200*6881a400Schristos {
201*6881a400Schristos   pid_t pid = regcache->ptid ().pid ();
202*6881a400Schristos   int lwp = regcache->ptid ().lwp ();
203*6881a400Schristos 
204*6881a400Schristos   if (regnum == -1 || hppanbsd_gregset_supplies_p (regnum))
205*6881a400Schristos     {
206*6881a400Schristos       struct reg regs;
207*6881a400Schristos 
208*6881a400Schristos       if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
209*6881a400Schristos 	perror_with_name (_("Couldn't get registers"));
210*6881a400Schristos 
211*6881a400Schristos       hppanbsd_collect_gregset (regcache, &regs, regnum);
212*6881a400Schristos 
213*6881a400Schristos       if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
214*6881a400Schristos 	perror_with_name (_("Couldn't write registers"));
215*6881a400Schristos     }
216*6881a400Schristos 
217*6881a400Schristos   if (regnum == -1 || hppanbsd_fpregset_supplies_p (regnum))
218*6881a400Schristos     {
219*6881a400Schristos       struct fpreg fpregs;
220*6881a400Schristos 
221*6881a400Schristos       if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1)
222*6881a400Schristos 	perror_with_name (_("Couldn't get floating point status"));
223*6881a400Schristos 
224*6881a400Schristos       hppanbsd_collect_fpregset (regcache, &fpregs, regnum);
225*6881a400Schristos 
226*6881a400Schristos       if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1)
227*6881a400Schristos 	perror_with_name (_("Couldn't write floating point status"));
228*6881a400Schristos     }
229*6881a400Schristos }
230*6881a400Schristos 
231*6881a400Schristos void _initialize_hppanbsd_nat ();
232*6881a400Schristos void
233*6881a400Schristos _initialize_hppanbsd_nat ()
234*6881a400Schristos {
235*6881a400Schristos   add_inf_child_target (&the_hppa_nbsd_nat_target);
236*6881a400Schristos }
237