xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/hppa-netbsd-nat.c (revision f8cf1a9151c7af1cb0bd8b09c13c66bca599c027)
1 /* Native-dependent code for NetBSD/hppa.
2 
3    Copyright (C) 2008-2023 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 #include "defs.h"
21 #include "inferior.h"
22 #include "regcache.h"
23 
24 #include <sys/types.h>
25 #include <sys/ptrace.h>
26 #include <machine/reg.h>
27 
28 #include "hppa-tdep.h"
29 #include "inf-ptrace.h"
30 
31 #include "netbsd-nat.h"
32 
33 class hppa_nbsd_nat_target final : public nbsd_nat_target
34 {
35   void fetch_registers (struct regcache *, int) override;
36   void store_registers (struct regcache *, int) override;
37 };
38 
39 static hppa_nbsd_nat_target the_hppa_nbsd_nat_target;
40 
41 static int
42 hppanbsd_gregset_supplies_p (int regnum)
43 {
44   return ((regnum >= HPPA_R0_REGNUM && regnum <= HPPA_R31_REGNUM) ||
45 	  (regnum >= HPPA_SAR_REGNUM && regnum <= HPPA_PCSQ_TAIL_REGNUM) ||
46 	  regnum == HPPA_IPSW_REGNUM ||
47 	  (regnum >= HPPA_SR4_REGNUM && regnum <= HPPA_SR4_REGNUM + 5));
48 }
49 
50 static int
51 hppanbsd_fpregset_supplies_p (int regnum)
52 {
53   return (regnum >= HPPA_FP0_REGNUM && regnum <= HPPA_FP31R_REGNUM);
54 }
55 
56 /* Supply the general-purpose registers stored in GREGS to REGCACHE.  */
57 
58 static void
59 hppanbsd_supply_gregset (struct regcache *regcache, const void *gregs)
60 {
61   const char *regs = (const char *)gregs;
62   int regnum;
63 
64   for (regnum = HPPA_R1_REGNUM; regnum <= HPPA_R31_REGNUM; regnum++)
65     regcache->raw_supply (regnum, regs + regnum * 4);
66 
67   regcache->raw_supply (HPPA_SAR_REGNUM, regs + 32 * 4);
68   regcache->raw_supply (HPPA_PCSQ_HEAD_REGNUM, regs + 33 * 4);
69   regcache->raw_supply (HPPA_PCSQ_TAIL_REGNUM, regs + 34 * 4);
70   regcache->raw_supply (HPPA_PCOQ_HEAD_REGNUM, regs + 35 * 4);
71   regcache->raw_supply (HPPA_PCOQ_TAIL_REGNUM, regs + 36 * 4);
72   regcache->raw_supply (HPPA_IPSW_REGNUM, regs);
73   regcache->raw_supply (HPPA_SR4_REGNUM, regs + 41 * 4);
74   regcache->raw_supply (HPPA_SR4_REGNUM + 1, regs + 37 * 4);
75   regcache->raw_supply (HPPA_SR4_REGNUM + 2, regs + 38 * 4);
76   regcache->raw_supply (HPPA_SR4_REGNUM + 3, regs + 39 * 4);
77   regcache->raw_supply (HPPA_SR4_REGNUM + 4, regs + 40 * 4);
78 }
79 
80 /* Supply the floating-point registers stored in FPREGS to REGCACHE.  */
81 
82 static void
83 hppanbsd_supply_fpregset (struct regcache *regcache, const void *fpregs)
84 {
85   const char *regs = (const char *)fpregs;
86   int regnum;
87 
88   for (regnum = HPPA_FP0_REGNUM; regnum <= HPPA_FP31R_REGNUM;
89        regnum += 2, regs += 8)
90     {
91       regcache->raw_supply (regnum, regs);
92       regcache->raw_supply (regnum + 1, regs + 4);
93     }
94 }
95 
96 /* Collect the general-purpose registers from REGCACHE and store them
97    in GREGS.  */
98 
99 static void
100 hppanbsd_collect_gregset (const struct regcache *regcache,
101 			  void *gregs, int regnum)
102 {
103   char *regs = (char *)gregs;
104   int i;
105 
106   for (i = HPPA_R1_REGNUM; i <= HPPA_R31_REGNUM; i++)
107     {
108       if (regnum == -1 || regnum == i)
109 	regcache->raw_collect (i, regs + i * 4);
110     }
111 
112   if (regnum == -1 || regnum == HPPA_IPSW_REGNUM)
113     regcache->raw_collect (HPPA_IPSW_REGNUM, regs);
114   if (regnum == -1 || regnum == HPPA_PCOQ_HEAD_REGNUM)
115     regcache->raw_collect (HPPA_PCOQ_HEAD_REGNUM, regs + 35 * 4);
116   if (regnum == -1 || regnum == HPPA_PCOQ_TAIL_REGNUM)
117     regcache->raw_collect (HPPA_PCOQ_TAIL_REGNUM, regs + 36 * 4);
118 
119   if (regnum == -1 || regnum == HPPA_SAR_REGNUM)
120     regcache->raw_collect (HPPA_SAR_REGNUM, regs + 32 * 4);
121   if (regnum == -1 || regnum == HPPA_PCSQ_HEAD_REGNUM)
122     regcache->raw_collect (HPPA_PCSQ_HEAD_REGNUM, regs + 33 * 4);
123   if (regnum == -1 || regnum == HPPA_PCSQ_TAIL_REGNUM)
124     regcache->raw_collect (HPPA_PCSQ_TAIL_REGNUM, regs + 34 * 4);
125   if (regnum == -1 || regnum == HPPA_PCOQ_HEAD_REGNUM)
126     regcache->raw_collect (HPPA_PCOQ_HEAD_REGNUM, regs + 35 * 4);
127   if (regnum == -1 || regnum == HPPA_PCOQ_TAIL_REGNUM)
128     regcache->raw_collect (HPPA_PCOQ_TAIL_REGNUM, regs + 36 * 4);
129   if (regnum == -1 || regnum == HPPA_IPSW_REGNUM)
130     regcache->raw_collect (HPPA_IPSW_REGNUM, regs);
131   if (regnum == -1 || regnum == HPPA_SR4_REGNUM)
132     regcache->raw_collect (HPPA_SR4_REGNUM, regs + 41 * 4);
133   if (regnum == -1 || regnum == HPPA_SR4_REGNUM + 1)
134     regcache->raw_collect (HPPA_SR4_REGNUM + 1, regs + 37 * 4);
135   if (regnum == -1 || regnum == HPPA_SR4_REGNUM + 2)
136     regcache->raw_collect (HPPA_SR4_REGNUM + 2, regs + 38 * 4);
137   if (regnum == -1 || regnum == HPPA_SR4_REGNUM + 3)
138     regcache->raw_collect (HPPA_SR4_REGNUM + 3, regs + 39 * 4);
139   if (regnum == -1 || regnum == HPPA_SR4_REGNUM + 4)
140     regcache->raw_collect (HPPA_SR4_REGNUM + 4, regs + 40 * 4);
141 }
142 
143 /* Collect the floating-point registers from REGCACHE and store them
144    in FPREGS.  */
145 
146 static void
147 hppanbsd_collect_fpregset (const struct regcache *regcache,
148 			  void *fpregs, int regnum)
149 {
150   char *regs = (char *)fpregs;
151   int i;
152 
153   for (i = HPPA_FP0_REGNUM; i <= HPPA_FP31R_REGNUM; i += 2, regs += 8)
154     {
155       if (regnum == -1 || regnum == i || regnum == i + 1)
156 	{
157 	  regcache->raw_collect (i, regs);
158 	  regcache->raw_collect (i + 1, regs + 4);
159 	}
160     }
161 }
162 
163 
164 /* Fetch register REGNUM from the inferior.  If REGNUM is -1, do this
165    for all registers (including the floating-point registers).  */
166 
167 void
168 hppa_nbsd_nat_target::fetch_registers (struct regcache *regcache, int regnum)
169 
170 {
171   pid_t pid = regcache->ptid ().pid ();
172   int lwp = regcache->ptid ().lwp ();
173 
174   if (regnum == -1 || hppanbsd_gregset_supplies_p (regnum))
175     {
176       struct reg regs;
177 
178       if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
179 	perror_with_name (_("Couldn't get registers"));
180 
181       hppanbsd_supply_gregset (regcache, &regs);
182     }
183 
184   if (regnum == -1 || hppanbsd_fpregset_supplies_p (regnum))
185     {
186       struct fpreg fpregs;
187 
188       if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1)
189 	perror_with_name (_("Couldn't get floating point status"));
190 
191       hppanbsd_supply_fpregset (regcache, &fpregs);
192     }
193 }
194 
195 /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
196    this for all registers (including the floating-point registers).  */
197 
198 void
199 hppa_nbsd_nat_target::store_registers (struct regcache *regcache, int regnum)
200 {
201   pid_t pid = regcache->ptid ().pid ();
202   int lwp = regcache->ptid ().lwp ();
203 
204   if (regnum == -1 || hppanbsd_gregset_supplies_p (regnum))
205     {
206       struct reg regs;
207 
208       if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
209 	perror_with_name (_("Couldn't get registers"));
210 
211       hppanbsd_collect_gregset (regcache, &regs, regnum);
212 
213       if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
214 	perror_with_name (_("Couldn't write registers"));
215     }
216 
217   if (regnum == -1 || hppanbsd_fpregset_supplies_p (regnum))
218     {
219       struct fpreg fpregs;
220 
221       if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1)
222 	perror_with_name (_("Couldn't get floating point status"));
223 
224       hppanbsd_collect_fpregset (regcache, &fpregs, regnum);
225 
226       if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1)
227 	perror_with_name (_("Couldn't write floating point status"));
228     }
229 }
230 
231 void _initialize_hppanbsd_nat ();
232 void
233 _initialize_hppanbsd_nat ()
234 {
235   add_inf_child_target (&the_hppa_nbsd_nat_target);
236 }
237