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