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