Inherit alpha_netbsd_nat_target from nbsd_nat_target
[binutils-gdb.git] / gdb / alpha-bsd-nat.c
1 /* Native-dependent code for Alpha BSD's.
2
3 Copyright (C) 2000-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 "inferior.h"
24 #include "regcache.h"
25
26 #include "alpha-tdep.h"
27 #include "alpha-bsd-tdep.h"
28 #include "inf-ptrace.h"
29 #include "nbsd-nat.h"
30
31 #include <sys/types.h>
32 #include <sys/ptrace.h>
33 #include <machine/reg.h>
34
35 #ifdef HAVE_SYS_PROCFS_H
36 #include <sys/procfs.h>
37 #endif
38
39 #ifndef HAVE_GREGSET_T
40 typedef struct reg gregset_t;
41 #endif
42
43 #ifndef HAVE_FPREGSET_T
44 typedef struct fpreg fpregset_t;
45 #endif
46
47 #include "gregset.h"
48
49 struct alpha_bsd_nat_target final : public nbsd_nat_target
50 {
51 void fetch_registers (struct regcache *, int) override;
52 void store_registers (struct regcache *, int) override;
53 };
54
55 static alpha_bsd_nat_target the_alpha_bsd_nat_target;
56
57 /* Provide *regset() wrappers around the generic Alpha BSD register
58 supply/fill routines. */
59
60 void
61 supply_gregset (struct regcache *regcache, const gregset_t *gregsetp)
62 {
63 alphabsd_supply_reg (regcache, (const char *) gregsetp, -1);
64 }
65
66 void
67 fill_gregset (const struct regcache *regcache, gregset_t *gregsetp, int regno)
68 {
69 alphabsd_fill_reg (regcache, (char *) gregsetp, regno);
70 }
71
72 void
73 supply_fpregset (struct regcache *regcache, const fpregset_t *fpregsetp)
74 {
75 alphabsd_supply_fpreg (regcache, (const char *) fpregsetp, -1);
76 }
77
78 void
79 fill_fpregset (const struct regcache *regcache,
80 fpregset_t *fpregsetp, int regno)
81 {
82 alphabsd_fill_fpreg (regcache, (char *) fpregsetp, regno);
83 }
84 \f
85 /* Determine if PT_GETREGS fetches this register. */
86
87 static int
88 getregs_supplies (int regno)
89 {
90 return ((regno >= ALPHA_V0_REGNUM && regno <= ALPHA_ZERO_REGNUM)
91 || regno >= ALPHA_PC_REGNUM);
92 }
93
94 /* Fetch register REGNO from the inferior. If REGNO is -1, do this
95 for all registers (including the floating point registers). */
96
97 void
98 alpha_bsd_nat_target::fetch_registers (struct regcache *regcache, int regno)
99 {
100 if (regno == -1 || getregs_supplies (regno))
101 {
102 struct reg gregs;
103
104 if (ptrace (PT_GETREGS, regcache->ptid ().pid (),
105 (PTRACE_TYPE_ARG3) &gregs, 0) == -1)
106 perror_with_name (_("Couldn't get registers"));
107
108 alphabsd_supply_reg (regcache, (char *) &gregs, regno);
109 if (regno != -1)
110 return;
111 }
112
113 if (regno == -1
114 || regno >= gdbarch_fp0_regnum (regcache->arch ()))
115 {
116 struct fpreg fpregs;
117
118 if (ptrace (PT_GETFPREGS, regcache->ptid ().pid (),
119 (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
120 perror_with_name (_("Couldn't get floating point status"));
121
122 alphabsd_supply_fpreg (regcache, (char *) &fpregs, regno);
123 }
124 }
125
126 /* Store register REGNO back into the inferior. If REGNO is -1, do
127 this for all registers (including the floating point registers). */
128
129 void
130 alpha_bsd_nat_target::store_registers (struct regcache *regcache, int regno)
131 {
132 if (regno == -1 || getregs_supplies (regno))
133 {
134 struct reg gregs;
135 if (ptrace (PT_GETREGS, regcache->ptid ().pid (),
136 (PTRACE_TYPE_ARG3) &gregs, 0) == -1)
137 perror_with_name (_("Couldn't get registers"));
138
139 alphabsd_fill_reg (regcache, (char *) &gregs, regno);
140
141 if (ptrace (PT_SETREGS, regcache->ptid ().pid (),
142 (PTRACE_TYPE_ARG3) &gregs, 0) == -1)
143 perror_with_name (_("Couldn't write registers"));
144
145 if (regno != -1)
146 return;
147 }
148
149 if (regno == -1
150 || regno >= gdbarch_fp0_regnum (regcache->arch ()))
151 {
152 struct fpreg fpregs;
153
154 if (ptrace (PT_GETFPREGS, regcache->ptid ().pid (),
155 (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
156 perror_with_name (_("Couldn't get floating point status"));
157
158 alphabsd_fill_fpreg (regcache, (char *) &fpregs, regno);
159
160 if (ptrace (PT_SETFPREGS, regcache->ptid ().pid (),
161 (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
162 perror_with_name (_("Couldn't write floating point status"));
163 }
164 }
165 \f
166
167 /* Support for debugging kernel virtual memory images. */
168
169 #include <sys/signal.h>
170 #include <machine/pcb.h>
171
172 #include "bsd-kvm.h"
173
174 static int
175 alphabsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
176 {
177 int regnum;
178
179 /* The following is true for OpenBSD 3.9:
180
181 The pcb contains the register state at the context switch inside
182 cpu_switch(). */
183
184 /* The stack pointer shouldn't be zero. */
185 if (pcb->pcb_hw.apcb_ksp == 0)
186 return 0;
187
188 regcache->raw_supply (ALPHA_SP_REGNUM, &pcb->pcb_hw.apcb_ksp);
189
190 for (regnum = ALPHA_S0_REGNUM; regnum < ALPHA_A0_REGNUM; regnum++)
191 regcache->raw_supply (regnum, &pcb->pcb_context[regnum - ALPHA_S0_REGNUM]);
192 regcache->raw_supply (ALPHA_RA_REGNUM, &pcb->pcb_context[7]);
193
194 return 1;
195 }
196 \f
197
198 void _initialize_alphabsd_nat ();
199 void
200 _initialize_alphabsd_nat ()
201 {
202 add_inf_child_target (&the_alpha_bsd_nat_target);
203
204 /* Support debugging kernel virtual memory images. */
205 bsd_kvm_add_target (alphabsd_supply_pcb);
206 }