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 #include "defs.h"
22 #include "gdbcore.h"
23 #include "inferior.h"
24 #include "regcache.h"
25 #include "target.h"
26
27 #include "nbsd-nat.h"
28 #include <sys/types.h>
29 #include <sys/ptrace.h>
30 #include <sys/sysctl.h>
31 #include <machine/reg.h>
32 #include <machine/frame.h>
33 #include <arm/arm32/frame.h>
34
35 /* Support for debugging kernel virtual memory images. */
36 #include <machine/pcb.h>
37
38 #include "arm-tdep.h"
39 #include "arm-nbsd-tdep.h"
40 #include "aarch32-tdep.h"
41 #include "inf-ptrace.h"
42 #include "bsd-kvm.h"
43 #include "nbsd-nat.h"
44
45 class arm_netbsd_nat_target final : public nbsd_nat_target
46 {
47 public:
48 /* Add our register access methods. */
49 void fetch_registers (struct regcache *, int) override;
50 void store_registers (struct regcache *, int) override;
51 const struct target_desc *read_description () override;
52 };
53
54 static arm_netbsd_nat_target the_arm_netbsd_nat_target;
55
56 /* Determine if PT_GETREGS fetches REGNUM. */
57
58 static bool
getregs_supplies(int regnum)59 getregs_supplies (int regnum)
60 {
61 return ((regnum >= ARM_A1_REGNUM && regnum <= ARM_PC_REGNUM)
62 || regnum == ARM_PS_REGNUM);
63 }
64
65 /* Determine if PT_GETFPREGS fetches REGNUM. */
66
67 static bool
getfpregs_supplies(int regnum)68 getfpregs_supplies (int regnum)
69 {
70 return ((regnum >= ARM_D0_REGNUM && regnum <= ARM_D31_REGNUM)
71 || regnum == ARM_FPSCR_REGNUM);
72 }
73
74 static int
armnbsd_supply_pcb(struct regcache * regcache,struct pcb * pcb)75 armnbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
76 {
77 struct switchframe sf;
78
79 /* The following is true for NetBSD/arm32 in 5.0 and after:
80
81 The pcb contains r8-r13 (sp) at the point of context switch in
82 cpu_switchto() or call of dumpsys(). At that point we have a
83 stack frame as described by `struct switchframe', which for
84 NetBSD/arm32 has the following layout:
85
86 r4 ascending.
87 r5 |
88 r6 |
89 r7 \|/
90 old sp
91 pc
92
93 we reconstruct the register state as it would look when we just
94 returned from cpu_switchto() or dumpsys(). */
95
96 if (!arm_apcs_32)
97 return 0;
98
99 /* The stack pointer shouldn't be zero. */
100 if (pcb->pcb_sp == 0)
101 return 0;
102
103 read_memory (pcb->pcb_sp, (gdb_byte *) &sf, sizeof sf);
104
105 regcache->raw_supply (ARM_PC_REGNUM, &sf.sf_pc);
106 regcache->raw_supply (ARM_SP_REGNUM, &pcb->pcb_sp);
107 regcache->raw_supply (12, &pcb->pcb_r12);
108 regcache->raw_supply (11, &pcb->pcb_r11);
109 regcache->raw_supply (10, &pcb->pcb_r10);
110 regcache->raw_supply (9, &pcb->pcb_r9);
111 regcache->raw_supply (8, &pcb->pcb_r8);
112 regcache->raw_supply (7, &sf.sf_r7);
113 regcache->raw_supply (6, &sf.sf_r6);
114 regcache->raw_supply (5, &sf.sf_r5);
115 regcache->raw_supply (4, &sf.sf_r4);
116
117 return 1;
118 }
119
120 static void
arm_supply_vfpregset(struct regcache * regcache,struct fpreg * fpregset)121 arm_supply_vfpregset (struct regcache *regcache, struct fpreg *fpregset)
122 {
123 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
124 if (tdep->vfp_register_count == 0)
125 return;
126
127 struct vfpreg &vfp = fpregset->fpr_vfp;
128 for (int regno = 0; regno <= tdep->vfp_register_count; regno++)
129 regcache->raw_supply (regno + ARM_D0_REGNUM, (char *) &vfp.vfp_regs[regno]);
130
131 regcache->raw_supply (ARM_FPSCR_REGNUM, (char *) &vfp.vfp_fpscr);
132 }
133
134 static void
fetch_register(struct regcache * regcache,int regno)135 fetch_register (struct regcache *regcache, int regno)
136 {
137 struct reg regs;
138 ptid_t ptid = regcache->ptid ();
139 pid_t pid = ptid.pid ();
140 int lwp = ptid.lwp ();
141 int ret;
142
143 ret = ptrace (PT_GETREGS, regcache->ptid ().pid (),
144 (PTRACE_TYPE_ARG3) ®s, lwp);
145
146 if (ret < 0)
147 {
148 warning (_("unable to fetch general register"));
149 return;
150 }
151 arm_nbsd_supply_gregset (nullptr, regcache, regno, ®s, sizeof (regs));
152 }
153
154 static void
fetch_fp_register(struct regcache * regcache,int regno)155 fetch_fp_register (struct regcache *regcache, int regno)
156 {
157 struct fpreg inferior_fp_registers;
158 int lwp = regcache->ptid ().lwp ();
159
160 int ret = ptrace (PT_GETFPREGS, regcache->ptid ().pid (),
161 (PTRACE_TYPE_ARG3) &inferior_fp_registers, lwp);
162
163 struct vfpreg &vfp = inferior_fp_registers.fpr_vfp;
164
165 if (ret < 0)
166 {
167 warning (_("unable to fetch floating-point register"));
168 return;
169 }
170
171 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
172 if (regno == ARM_FPSCR_REGNUM && tdep->vfp_register_count != 0)
173 regcache->raw_supply (ARM_FPSCR_REGNUM, (char *) &vfp.vfp_fpscr);
174 else if (regno >= ARM_D0_REGNUM
175 && regno <= ARM_D0_REGNUM + tdep->vfp_register_count)
176 {
177 regcache->raw_supply (regno,
178 (char *) &vfp.vfp_regs[regno - ARM_D0_REGNUM]);
179 }
180 else
181 warning (_("Invalid register number."));
182 }
183
184 static void
fetch_fp_regs(struct regcache * regcache)185 fetch_fp_regs (struct regcache *regcache)
186 {
187 struct fpreg inferior_fp_registers;
188 int lwp = regcache->ptid ().lwp ();
189 int ret;
190
191 ret = ptrace (PT_GETFPREGS, regcache->ptid ().pid (),
192 (PTRACE_TYPE_ARG3) &inferior_fp_registers, lwp);
193
194 if (ret < 0)
195 {
196 warning (_("unable to fetch floating-point registers"));
197 return;
198 }
199
200 arm_supply_vfpregset (regcache, &inferior_fp_registers);
201 }
202
203 void
fetch_registers(struct regcache * regcache,int regno)204 arm_netbsd_nat_target::fetch_registers (struct regcache *regcache, int regno)
205 {
206 if (regno >= 0)
207 {
208 if (getregs_supplies (regno))
209 fetch_register (regcache, regno);
210 else if (getfpregs_supplies (regno))
211 fetch_fp_register (regcache, regno);
212 else
213 warning (_("unable to fetch register %d"), regno);
214 }
215 else
216 {
217 fetch_register (regcache, -1);
218 fetch_fp_regs (regcache);
219 }
220 }
221
222
223 static void
store_register(const struct regcache * regcache,int regno)224 store_register (const struct regcache *regcache, int regno)
225 {
226 struct gdbarch *gdbarch = regcache->arch ();
227 struct reg regs;
228 int ret;
229 ptid_t ptid = regcache->ptid ();
230 pid_t pid = ptid.pid ();
231 int lwp = ptid.lwp ();
232
233 ret = ptrace (PT_GETREGS, regcache->ptid ().pid (),
234 (PTRACE_TYPE_ARG3) ®s, lwp);
235
236 if (ret < 0)
237 {
238 warning (_("unable to fetch general registers"));
239 return;
240 }
241
242 switch (regno)
243 {
244 case ARM_SP_REGNUM:
245 regcache->raw_collect (ARM_SP_REGNUM, (char *) ®s.r_sp);
246 break;
247
248 case ARM_LR_REGNUM:
249 regcache->raw_collect (ARM_LR_REGNUM, (char *) ®s.r_lr);
250 break;
251
252 case ARM_PC_REGNUM:
253 if (arm_apcs_32)
254 regcache->raw_collect (ARM_PC_REGNUM, (char *) ®s.r_pc);
255 else
256 {
257 unsigned pc_val;
258
259 regcache->raw_collect (ARM_PC_REGNUM, (char *) &pc_val);
260
261 pc_val = gdbarch_addr_bits_remove (gdbarch, pc_val);
262 regs.r_pc ^= gdbarch_addr_bits_remove (gdbarch, regs.r_pc);
263 regs.r_pc |= pc_val;
264 }
265 break;
266
267 case ARM_PS_REGNUM:
268 if (arm_apcs_32)
269 regcache->raw_collect (ARM_PS_REGNUM, (char *) ®s.r_cpsr);
270 else
271 {
272 unsigned psr_val;
273
274 regcache->raw_collect (ARM_PS_REGNUM, (char *) &psr_val);
275
276 psr_val ^= gdbarch_addr_bits_remove (gdbarch, psr_val);
277 regs.r_pc = gdbarch_addr_bits_remove (gdbarch, regs.r_pc);
278 regs.r_pc |= psr_val;
279 }
280 break;
281
282 default:
283 regcache->raw_collect (regno, (char *) ®s.r[regno]);
284 break;
285 }
286
287 ret = ptrace (PT_SETREGS, regcache->ptid ().pid (),
288 (PTRACE_TYPE_ARG3) ®s, lwp);
289
290 if (ret < 0)
291 warning (_("unable to write register %d to inferior"), regno);
292 }
293
294 static void
store_regs(const struct regcache * regcache)295 store_regs (const struct regcache *regcache)
296 {
297 struct gdbarch *gdbarch = regcache->arch ();
298 struct reg regs;
299 int lwp = regcache->ptid ().lwp ();
300 int ret;
301 int regno;
302
303
304 for (regno = ARM_A1_REGNUM; regno < ARM_SP_REGNUM; regno++)
305 regcache->raw_collect (regno, (char *) ®s.r[regno]);
306
307 regcache->raw_collect (ARM_SP_REGNUM, (char *) ®s.r_sp);
308 regcache->raw_collect (ARM_LR_REGNUM, (char *) ®s.r_lr);
309
310 if (arm_apcs_32)
311 {
312 regcache->raw_collect (ARM_PC_REGNUM, (char *) ®s.r_pc);
313 regcache->raw_collect (ARM_PS_REGNUM, (char *) ®s.r_cpsr);
314 }
315 else
316 {
317 unsigned pc_val;
318 unsigned psr_val;
319
320 regcache->raw_collect (ARM_PC_REGNUM, (char *) &pc_val);
321 regcache->raw_collect (ARM_PS_REGNUM, (char *) &psr_val);
322
323 pc_val = gdbarch_addr_bits_remove (gdbarch, pc_val);
324 psr_val ^= gdbarch_addr_bits_remove (gdbarch, psr_val);
325
326 regs.r_pc = pc_val | psr_val;
327 }
328
329 ret = ptrace (PT_SETREGS, regcache->ptid ().pid (),
330 (PTRACE_TYPE_ARG3) ®s, lwp);
331
332 if (ret < 0)
333 warning (_("unable to store general registers"));
334 }
335
336 static void
store_fp_register(const struct regcache * regcache,int regno)337 store_fp_register (const struct regcache *regcache, int regno)
338 {
339 struct fpreg inferior_fp_registers;
340 int lwp = regcache->ptid ().lwp ();
341 int ret = ptrace (PT_GETFPREGS, regcache->ptid ().pid (),
342 (PTRACE_TYPE_ARG3) &inferior_fp_registers, lwp);
343 struct vfpreg &vfp = inferior_fp_registers.fpr_vfp;
344
345 if (ret < 0)
346 {
347 warning (_("unable to fetch floating-point registers"));
348 return;
349 }
350
351 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
352 if (regno == ARM_FPSCR_REGNUM && tdep->vfp_register_count != 0)
353 regcache->raw_collect (ARM_FPSCR_REGNUM, (char *) &vfp.vfp_fpscr);
354 else if (regno >= ARM_D0_REGNUM
355 && regno <= ARM_D0_REGNUM + tdep->vfp_register_count)
356 {
357 regcache->raw_collect (regno,
358 (char *) &vfp.vfp_regs[regno - ARM_D0_REGNUM]);
359 }
360 else
361 warning (_("Invalid register number."));
362
363 ret = ptrace (PT_SETFPREGS, regcache->ptid ().pid (),
364 (PTRACE_TYPE_ARG3) &inferior_fp_registers, lwp);
365
366 if (ret < 0)
367 warning (_("unable to write register %d to inferior"), regno);
368 }
369
370 static void
store_fp_regs(const struct regcache * regcache)371 store_fp_regs (const struct regcache *regcache)
372 {
373 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
374 int lwp = regcache->ptid ().lwp ();
375 if (tdep->vfp_register_count == 0)
376 return;
377
378 struct fpreg fpregs;
379 for (int regno = 0; regno <= tdep->vfp_register_count; regno++)
380 regcache->raw_collect
381 (regno + ARM_D0_REGNUM, (char *) &fpregs.fpr_vfp.vfp_regs[regno]);
382
383 regcache->raw_collect (ARM_FPSCR_REGNUM,
384 (char *) &fpregs.fpr_vfp.vfp_fpscr);
385
386 int ret = ptrace (PT_SETFPREGS, regcache->ptid ().pid (),
387 (PTRACE_TYPE_ARG3) &fpregs, lwp);
388
389 if (ret < 0)
390 warning (_("unable to store floating-point registers"));
391 }
392
393 void
store_registers(struct regcache * regcache,int regno)394 arm_netbsd_nat_target::store_registers (struct regcache *regcache, int regno)
395 {
396 if (regno >= 0)
397 {
398 if (getregs_supplies (regno))
399 store_register (regcache, regno);
400 else if (getfpregs_supplies (regno))
401 store_fp_register (regcache, regno);
402 else
403 warning (_("unable to store register %d"), regno);
404 }
405 else
406 {
407 store_regs (regcache);
408 store_fp_regs (regcache);
409 }
410 }
411
412 const struct target_desc *
read_description()413 arm_netbsd_nat_target::read_description ()
414 {
415 int flag;
416 size_t len = sizeof (flag);
417
418 if (sysctlbyname("machdep.fpu_present", &flag, &len, NULL, 0) != 0
419 || !flag)
420 return arm_read_description (ARM_FP_TYPE_NONE);
421
422 len = sizeof(flag);
423 if (sysctlbyname("machdep.neon_present", &flag, &len, NULL, 0) == 0 && flag)
424 return aarch32_read_description ();
425 return arm_read_description (ARM_FP_TYPE_VFPV3);
426 }
427
428 void _initialize_arm_netbsd_nat ();
429 void
_initialize_arm_netbsd_nat()430 _initialize_arm_netbsd_nat ()
431 {
432 /* Support debugging kernel virtual memory images. */
433 bsd_kvm_add_target (armnbsd_supply_pcb);
434
435 /*###436 [cc] note: suggested alternative: 'the_arm_nbsd_nat_target'%%%*/
436 add_inf_child_target (&the_arm_netbsd_nat_target);
437 }
438