]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/amd64-fbsd-nat.c
x86/Intel: warn about undue mnemonic suffixes
[thirdparty/binutils-gdb.git] / gdb / amd64-fbsd-nat.c
1 /* Native-dependent code for FreeBSD/amd64.
2
3 Copyright (C) 2003-2024 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 "inferior.h"
21 #include "regcache.h"
22 #include "target.h"
23
24 #include <signal.h>
25 #include <sys/types.h>
26 #include <sys/ptrace.h>
27 #include <sys/sysctl.h>
28 #include <sys/user.h>
29 #include <machine/reg.h>
30
31 #include "amd64-tdep.h"
32 #include "amd64-fbsd-tdep.h"
33 #include "i387-tdep.h"
34 #include "amd64-nat.h"
35 #include "x86-nat.h"
36 #include "x86-fbsd-nat.h"
37
38 class amd64_fbsd_nat_target final : public x86_fbsd_nat_target
39 {
40 public:
41 void fetch_registers (struct regcache *, int) override;
42 void store_registers (struct regcache *, int) override;
43
44 const struct target_desc *read_description () override;
45 };
46
47 static amd64_fbsd_nat_target the_amd64_fbsd_nat_target;
48
49 /* This is a layout of the amd64 'struct reg' but with i386
50 registers. */
51
52 static const struct regcache_map_entry amd64_fbsd32_gregmap[] =
53 {
54 { 8, REGCACHE_MAP_SKIP, 8 },
55 { 1, I386_EDI_REGNUM, 8 },
56 { 1, I386_ESI_REGNUM, 8 },
57 { 1, I386_EBP_REGNUM, 8 },
58 { 1, I386_EBX_REGNUM, 8 },
59 { 1, I386_EDX_REGNUM, 8 },
60 { 1, I386_ECX_REGNUM, 8 },
61 { 1, I386_EAX_REGNUM, 8 },
62 { 1, REGCACHE_MAP_SKIP, 4 }, /* trapno */
63 { 1, I386_FS_REGNUM, 2 },
64 { 1, I386_GS_REGNUM, 2 },
65 { 1, REGCACHE_MAP_SKIP, 4 }, /* err */
66 { 1, I386_ES_REGNUM, 2 },
67 { 1, I386_DS_REGNUM, 2 },
68 { 1, I386_EIP_REGNUM, 8 },
69 { 1, I386_CS_REGNUM, 8 },
70 { 1, I386_EFLAGS_REGNUM, 8 },
71 { 1, I386_ESP_REGNUM, 0 },
72 { 1, I386_SS_REGNUM, 8 },
73 { 0 }
74 };
75
76 static const struct regset amd64_fbsd32_gregset =
77 {
78 amd64_fbsd32_gregmap, regcache_supply_regset, regcache_collect_regset
79 };
80
81 /* Return the regset to use for 'struct reg' for the GDBARCH. */
82
83 static const struct regset *
84 find_gregset (struct gdbarch *gdbarch)
85 {
86 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
87 return &amd64_fbsd32_gregset;
88 else
89 return &amd64_fbsd_gregset;
90 }
91
92 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
93 for all registers. */
94
95 void
96 amd64_fbsd_nat_target::fetch_registers (struct regcache *regcache, int regnum)
97 {
98 struct gdbarch *gdbarch = regcache->arch ();
99 #if defined(PT_GETFSBASE) || defined(PT_GETGSBASE)
100 const i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
101 #endif
102 pid_t pid = get_ptrace_pid (regcache->ptid ());
103 const struct regset *gregset = find_gregset (gdbarch);
104
105 if (fetch_register_set<struct reg> (regcache, regnum, PT_GETREGS, gregset))
106 {
107 if (regnum != -1)
108 return;
109 }
110
111 #ifdef PT_GETFSBASE
112 if (regnum == -1 || regnum == tdep->fsbase_regnum)
113 {
114 register_t base;
115
116 if (ptrace (PT_GETFSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
117 perror_with_name (_("Couldn't get segment register fs_base"));
118
119 regcache->raw_supply (tdep->fsbase_regnum, &base);
120 if (regnum != -1)
121 return;
122 }
123 #endif
124 #ifdef PT_GETGSBASE
125 if (regnum == -1 || regnum == tdep->fsbase_regnum + 1)
126 {
127 register_t base;
128
129 if (ptrace (PT_GETGSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
130 perror_with_name (_("Couldn't get segment register gs_base"));
131
132 regcache->raw_supply (tdep->fsbase_regnum + 1, &base);
133 if (regnum != -1)
134 return;
135 }
136 #endif
137
138 /* There is no amd64_fxsave_supplies or amd64_xsave_supplies.
139 Instead, the earlier register sets return early if the request
140 was for a specific register that was already satisified to avoid
141 fetching the FPU/XSAVE state unnecessarily. */
142
143 #ifdef PT_GETXSTATE_INFO
144 if (m_xsave_info.xsave_len != 0)
145 {
146 void *xstateregs = alloca (m_xsave_info.xsave_len);
147
148 if (ptrace (PT_GETXSTATE, pid, (PTRACE_TYPE_ARG3) xstateregs, 0) == -1)
149 perror_with_name (_("Couldn't get extended state status"));
150
151 amd64_supply_xsave (regcache, regnum, xstateregs);
152 return;
153 }
154 #endif
155
156 struct fpreg fpregs;
157
158 if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
159 perror_with_name (_("Couldn't get floating point status"));
160
161 amd64_supply_fxsave (regcache, regnum, &fpregs);
162 }
163
164 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
165 this for all registers. */
166
167 void
168 amd64_fbsd_nat_target::store_registers (struct regcache *regcache, int regnum)
169 {
170 struct gdbarch *gdbarch = regcache->arch ();
171 #if defined(PT_GETFSBASE) || defined(PT_GETGSBASE)
172 const i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
173 #endif
174 pid_t pid = get_ptrace_pid (regcache->ptid ());
175 const struct regset *gregset = find_gregset (gdbarch);
176
177 if (store_register_set<struct reg> (regcache, regnum, PT_GETREGS, PT_SETREGS,
178 gregset))
179 {
180 if (regnum != -1)
181 return;
182 }
183
184 #ifdef PT_SETFSBASE
185 if (regnum == -1 || regnum == tdep->fsbase_regnum)
186 {
187 register_t base;
188
189 /* Clear the full base value to support 32-bit targets. */
190 base = 0;
191 regcache->raw_collect (tdep->fsbase_regnum, &base);
192
193 if (ptrace (PT_SETFSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
194 perror_with_name (_("Couldn't write segment register fs_base"));
195 if (regnum != -1)
196 return;
197 }
198 #endif
199 #ifdef PT_SETGSBASE
200 if (regnum == -1 || regnum == tdep->fsbase_regnum + 1)
201 {
202 register_t base;
203
204 /* Clear the full base value to support 32-bit targets. */
205 base = 0;
206 regcache->raw_collect (tdep->fsbase_regnum + 1, &base);
207
208 if (ptrace (PT_SETGSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
209 perror_with_name (_("Couldn't write segment register gs_base"));
210 if (regnum != -1)
211 return;
212 }
213 #endif
214
215 /* There is no amd64_fxsave_supplies or amd64_xsave_supplies.
216 Instead, the earlier register sets return early if the request
217 was for a specific register that was already satisified to avoid
218 fetching the FPU/XSAVE state unnecessarily. */
219
220 #ifdef PT_GETXSTATE_INFO
221 if (m_xsave_info.xsave_len != 0)
222 {
223 void *xstateregs = alloca (m_xsave_info.xsave_len);
224
225 if (ptrace (PT_GETXSTATE, pid, (PTRACE_TYPE_ARG3) xstateregs, 0) == -1)
226 perror_with_name (_("Couldn't get extended state status"));
227
228 amd64_collect_xsave (regcache, regnum, xstateregs, 0);
229
230 if (ptrace (PT_SETXSTATE, pid, (PTRACE_TYPE_ARG3) xstateregs,
231 m_xsave_info.xsave_len) == -1)
232 perror_with_name (_("Couldn't write extended state status"));
233 return;
234 }
235 #endif
236
237 struct fpreg fpregs;
238
239 if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
240 perror_with_name (_("Couldn't get floating point status"));
241
242 amd64_collect_fxsave (regcache, regnum, &fpregs);
243
244 if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
245 perror_with_name (_("Couldn't write floating point status"));
246 }
247
248 /* Support for debugging kernel virtual memory images. */
249
250 #include <machine/pcb.h>
251 #include <osreldate.h>
252
253 #include "bsd-kvm.h"
254
255 static int
256 amd64fbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
257 {
258 /* The following is true for FreeBSD 5.2:
259
260 The pcb contains %rip, %rbx, %rsp, %rbp, %r12, %r13, %r14, %r15,
261 %ds, %es, %fs and %gs. This accounts for all callee-saved
262 registers specified by the psABI and then some. Here %esp
263 contains the stack pointer at the point just after the call to
264 cpu_switch(). From this information we reconstruct the register
265 state as it would like when we just returned from cpu_switch(). */
266
267 /* The stack pointer shouldn't be zero. */
268 if (pcb->pcb_rsp == 0)
269 return 0;
270
271 pcb->pcb_rsp += 8;
272 regcache->raw_supply (AMD64_RIP_REGNUM, &pcb->pcb_rip);
273 regcache->raw_supply (AMD64_RBX_REGNUM, &pcb->pcb_rbx);
274 regcache->raw_supply (AMD64_RSP_REGNUM, &pcb->pcb_rsp);
275 regcache->raw_supply (AMD64_RBP_REGNUM, &pcb->pcb_rbp);
276 regcache->raw_supply (12, &pcb->pcb_r12);
277 regcache->raw_supply (13, &pcb->pcb_r13);
278 regcache->raw_supply (14, &pcb->pcb_r14);
279 regcache->raw_supply (15, &pcb->pcb_r15);
280 #if (__FreeBSD_version < 800075) && (__FreeBSD_kernel_version < 800075)
281 /* struct pcb provides the pcb_ds/pcb_es/pcb_fs/pcb_gs fields only
282 up until __FreeBSD_version 800074: The removal of these fields
283 occurred on 2009-04-01 while the __FreeBSD_version number was
284 bumped to 800075 on 2009-04-06. So 800075 is the closest version
285 number where we should not try to access these fields. */
286 regcache->raw_supply (AMD64_DS_REGNUM, &pcb->pcb_ds);
287 regcache->raw_supply (AMD64_ES_REGNUM, &pcb->pcb_es);
288 regcache->raw_supply (AMD64_FS_REGNUM, &pcb->pcb_fs);
289 regcache->raw_supply (AMD64_GS_REGNUM, &pcb->pcb_gs);
290 #endif
291
292 return 1;
293 }
294 \f
295
296 /* Implement the read_description method. */
297
298 const struct target_desc *
299 amd64_fbsd_nat_target::read_description ()
300 {
301 struct reg regs;
302 int is64;
303
304 if (inferior_ptid == null_ptid)
305 return this->beneath ()->read_description ();
306
307 if (ptrace (PT_GETREGS, inferior_ptid.pid (),
308 (PTRACE_TYPE_ARG3) &regs, 0) == -1)
309 perror_with_name (_("Couldn't get registers"));
310 is64 = (regs.r_cs == GSEL (GUCODE_SEL, SEL_UPL));
311 #ifdef PT_GETXSTATE_INFO
312 probe_xsave_layout (inferior_ptid.pid ());
313 if (m_xsave_info.xsave_len != 0)
314 {
315 if (is64)
316 return amd64_target_description (m_xsave_info.xsave_mask, true);
317 else
318 return i386_target_description (m_xsave_info.xsave_mask, true);
319 }
320 #endif
321 if (is64)
322 return amd64_target_description (X86_XSTATE_SSE_MASK, true);
323 else
324 return i386_target_description (X86_XSTATE_SSE_MASK, true);
325 }
326
327 void _initialize_amd64fbsd_nat ();
328 void
329 _initialize_amd64fbsd_nat ()
330 {
331 add_inf_child_target (&the_amd64_fbsd_nat_target);
332
333 /* Support debugging kernel virtual memory images. */
334 bsd_kvm_add_target (amd64fbsd_supply_pcb);
335 }