]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/arm-nbsd-nat.c
Remove use of deprecated core functions (in NetBSD/ARM)
[thirdparty/binutils-gdb.git] / gdb / arm-nbsd-nat.c
1 /* Native-dependent code for BSD Unix running on ARM's, for GDB.
2
3 Copyright (C) 1988-2020 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 /* We define this to get types like register_t. */
21 #define _KERNTYPES
22 #include "defs.h"
23 #include "gdbcore.h"
24 #include "inferior.h"
25 #include "regcache.h"
26 #include "target.h"
27 #include <sys/types.h>
28 #include <sys/ptrace.h>
29 #include <sys/sysctl.h>
30 #include <machine/reg.h>
31 #include <machine/frame.h>
32
33 #include "arm-tdep.h"
34 #include "arm-nbsd-tdep.h"
35 #include "aarch32-tdep.h"
36 #include "inf-ptrace.h"
37
38 class arm_netbsd_nat_target final : public inf_ptrace_target
39 {
40 public:
41 /* Add our register access methods. */
42 void fetch_registers (struct regcache *, int) override;
43 void store_registers (struct regcache *, int) override;
44 const struct target_desc *read_description () override;
45 };
46
47 static arm_netbsd_nat_target the_arm_netbsd_nat_target;
48
49 static void
50 arm_supply_vfpregset (struct regcache *regcache, struct fpreg *fpregset)
51 {
52 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
53 if (tdep->vfp_register_count == 0)
54 return;
55
56 struct vfpreg &vfp = fpregset->fpr_vfp;
57 for (int regno = 0; regno <= tdep->vfp_register_count; regno++)
58 regcache->raw_supply (regno + ARM_D0_REGNUM, (char *) &vfp.vfp_regs[regno]);
59
60 regcache->raw_supply (ARM_FPSCR_REGNUM, (char *) &vfp.vfp_fpscr);
61 }
62
63 static void
64 fetch_register (struct regcache *regcache, int regno)
65 {
66 struct reg inferior_registers;
67 int ret;
68
69 ret = ptrace (PT_GETREGS, regcache->ptid ().pid (),
70 (PTRACE_TYPE_ARG3) &inferior_registers, 0);
71
72 if (ret < 0)
73 {
74 warning (_("unable to fetch general register"));
75 return;
76 }
77 arm_nbsd_supply_gregset (nullptr, regcache, regno, &inferior_registers,
78 sizeof (inferior_registers));
79 }
80
81 static void
82 fetch_fp_register (struct regcache *regcache, int regno)
83 {
84 struct fpreg inferior_fp_registers;
85 int ret = ptrace (PT_GETFPREGS, regcache->ptid ().pid (),
86 (PTRACE_TYPE_ARG3) &inferior_fp_registers, 0);
87
88 struct vfpreg &vfp = inferior_fp_registers.fpr_vfp;
89
90 if (ret < 0)
91 {
92 warning (_("unable to fetch floating-point register"));
93 return;
94 }
95
96 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
97 if (regno == ARM_FPSCR_REGNUM && tdep->vfp_register_count != 0)
98 regcache->raw_supply (ARM_FPSCR_REGNUM, (char *) &vfp.vfp_fpscr);
99 else if (regno >= ARM_D0_REGNUM
100 && regno <= ARM_D0_REGNUM + tdep->vfp_register_count)
101 {
102 regcache->raw_supply (regno,
103 (char *) &vfp.vfp_regs[regno - ARM_D0_REGNUM]);
104 }
105 else
106 warning (_("Invalid register number."));
107 }
108
109 static void
110 fetch_fp_regs (struct regcache *regcache)
111 {
112 struct fpreg inferior_fp_registers;
113 int ret;
114 int regno;
115
116 ret = ptrace (PT_GETFPREGS, regcache->ptid ().pid (),
117 (PTRACE_TYPE_ARG3) &inferior_fp_registers, 0);
118
119 if (ret < 0)
120 {
121 warning (_("unable to fetch general registers"));
122 return;
123 }
124
125 arm_supply_vfpregset (regcache, &inferior_fp_registers);
126 }
127
128 void
129 arm_netbsd_nat_target::fetch_registers (struct regcache *regcache, int regno)
130 {
131 if (regno >= 0)
132 {
133 if (regno < ARM_F0_REGNUM || regno > ARM_FPS_REGNUM)
134 fetch_register (regcache, regno);
135 else
136 fetch_fp_register (regcache, regno);
137 }
138 else
139 {
140 fetch_register (regcache, -1);
141 fetch_fp_regs (regcache);
142 }
143 }
144
145
146 static void
147 store_register (const struct regcache *regcache, int regno)
148 {
149 struct gdbarch *gdbarch = regcache->arch ();
150 struct reg inferior_registers;
151 int ret;
152
153 ret = ptrace (PT_GETREGS, regcache->ptid ().pid (),
154 (PTRACE_TYPE_ARG3) &inferior_registers, 0);
155
156 if (ret < 0)
157 {
158 warning (_("unable to fetch general registers"));
159 return;
160 }
161
162 switch (regno)
163 {
164 case ARM_SP_REGNUM:
165 regcache->raw_collect (ARM_SP_REGNUM, (char *) &inferior_registers.r_sp);
166 break;
167
168 case ARM_LR_REGNUM:
169 regcache->raw_collect (ARM_LR_REGNUM, (char *) &inferior_registers.r_lr);
170 break;
171
172 case ARM_PC_REGNUM:
173 if (arm_apcs_32)
174 regcache->raw_collect (ARM_PC_REGNUM,
175 (char *) &inferior_registers.r_pc);
176 else
177 {
178 unsigned pc_val;
179
180 regcache->raw_collect (ARM_PC_REGNUM, (char *) &pc_val);
181
182 pc_val = gdbarch_addr_bits_remove (gdbarch, pc_val);
183 inferior_registers.r_pc ^= gdbarch_addr_bits_remove
184 (gdbarch, inferior_registers.r_pc);
185 inferior_registers.r_pc |= pc_val;
186 }
187 break;
188
189 case ARM_PS_REGNUM:
190 if (arm_apcs_32)
191 regcache->raw_collect (ARM_PS_REGNUM,
192 (char *) &inferior_registers.r_cpsr);
193 else
194 {
195 unsigned psr_val;
196
197 regcache->raw_collect (ARM_PS_REGNUM, (char *) &psr_val);
198
199 psr_val ^= gdbarch_addr_bits_remove (gdbarch, psr_val);
200 inferior_registers.r_pc = gdbarch_addr_bits_remove
201 (gdbarch, inferior_registers.r_pc);
202 inferior_registers.r_pc |= psr_val;
203 }
204 break;
205
206 default:
207 regcache->raw_collect (regno, (char *) &inferior_registers.r[regno]);
208 break;
209 }
210
211 ret = ptrace (PT_SETREGS, regcache->ptid ().pid (),
212 (PTRACE_TYPE_ARG3) &inferior_registers, 0);
213
214 if (ret < 0)
215 warning (_("unable to write register %d to inferior"), regno);
216 }
217
218 static void
219 store_regs (const struct regcache *regcache)
220 {
221 struct gdbarch *gdbarch = regcache->arch ();
222 struct reg inferior_registers;
223 int ret;
224 int regno;
225
226
227 for (regno = ARM_A1_REGNUM; regno < ARM_SP_REGNUM; regno++)
228 regcache->raw_collect (regno, (char *) &inferior_registers.r[regno]);
229
230 regcache->raw_collect (ARM_SP_REGNUM, (char *) &inferior_registers.r_sp);
231 regcache->raw_collect (ARM_LR_REGNUM, (char *) &inferior_registers.r_lr);
232
233 if (arm_apcs_32)
234 {
235 regcache->raw_collect (ARM_PC_REGNUM, (char *) &inferior_registers.r_pc);
236 regcache->raw_collect (ARM_PS_REGNUM,
237 (char *) &inferior_registers.r_cpsr);
238 }
239 else
240 {
241 unsigned pc_val;
242 unsigned psr_val;
243
244 regcache->raw_collect (ARM_PC_REGNUM, (char *) &pc_val);
245 regcache->raw_collect (ARM_PS_REGNUM, (char *) &psr_val);
246
247 pc_val = gdbarch_addr_bits_remove (gdbarch, pc_val);
248 psr_val ^= gdbarch_addr_bits_remove (gdbarch, psr_val);
249
250 inferior_registers.r_pc = pc_val | psr_val;
251 }
252
253 ret = ptrace (PT_SETREGS, regcache->ptid ().pid (),
254 (PTRACE_TYPE_ARG3) &inferior_registers, 0);
255
256 if (ret < 0)
257 warning (_("unable to store general registers"));
258 }
259
260 static void
261 store_fp_register (const struct regcache *regcache, int regno)
262 {
263 struct fpreg inferior_fp_registers;
264 int ret = ptrace (PT_GETFPREGS, regcache->ptid ().pid (),
265 (PTRACE_TYPE_ARG3) &inferior_fp_registers, 0);
266 struct vfpreg &vfp = inferior_fp_registers.fpr_vfp;
267
268 if (ret < 0)
269 {
270 warning (_("unable to fetch floating-point registers"));
271 return;
272 }
273
274 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
275 if (regno == ARM_FPSCR_REGNUM && tdep->vfp_register_count != 0)
276 regcache->raw_collect (ARM_FPSCR_REGNUM, (char *) &vfp.vfp_fpscr);
277 else if (regno >= ARM_D0_REGNUM
278 && regno <= ARM_D0_REGNUM + tdep->vfp_register_count)
279 {
280 regcache->raw_collect (regno,
281 (char *) &vfp.vfp_regs[regno - ARM_D0_REGNUM]);
282 }
283 else
284 warning (_("Invalid register number."));
285
286 ret = ptrace (PT_SETFPREGS, regcache->ptid ().pid (),
287 (PTRACE_TYPE_ARG3) &inferior_fp_registers, 0);
288
289 if (ret < 0)
290 warning (_("unable to write register %d to inferior"), regno);
291 }
292
293 static void
294 store_fp_regs (const struct regcache *regcache)
295 {
296 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
297 if (tdep->vfp_register_count == 0)
298 return;
299
300 struct fpreg fpregs;
301 for (int regno = 0; regno <= tdep->vfp_register_count; regno++)
302 regcache->raw_collect
303 (regno + ARM_D0_REGNUM, (char *) &fpregs.fpr_vfp.vfp_regs[regno]);
304
305 regcache->raw_collect (ARM_FPSCR_REGNUM,
306 (char *) &fpregs.fpr_vfp.vfp_fpscr);
307
308 int ret = ptrace (PT_SETFPREGS, regcache->ptid ().pid (),
309 (PTRACE_TYPE_ARG3) &fpregs, 0);
310
311 if (ret < 0)
312 warning (_("unable to store floating-point registers"));
313 }
314
315 void
316 arm_netbsd_nat_target::store_registers (struct regcache *regcache, int regno)
317 {
318 if (regno >= 0)
319 {
320 if (regno < ARM_F0_REGNUM || regno > ARM_FPS_REGNUM)
321 store_register (regcache, regno);
322 else
323 store_fp_register (regcache, regno);
324 }
325 else
326 {
327 store_regs (regcache);
328 store_fp_regs (regcache);
329 }
330 }
331
332 const struct target_desc *
333 arm_netbsd_nat_target::read_description ()
334 {
335 int flag;
336 size_t len = sizeof (flag);
337
338 if (sysctlbyname("machdep.fpu_present", &flag, &len, NULL, 0) != 0
339 || !flag)
340 return arm_read_description (ARM_FP_TYPE_NONE);
341
342 len = sizeof(flag);
343 if (sysctlbyname("machdep.neon_present", &flag, &len, NULL, 0) == 0 && flag)
344 return aarch32_read_description ();
345
346 return arm_read_description (ARM_FP_TYPE_VFPV3);
347 }
348
349 void _initialize_arm_netbsd_nat ();
350 void
351 _initialize_arm_netbsd_nat ()
352 {
353 add_inf_child_target (&the_arm_netbsd_nat_target);
354 }