Commit | Line | Data |
---|---|---|
a4b6fc86 | 1 | /* Native-dependent code for GNU/Linux x86-64. |
0a65a603 | 2 | |
ecd75fc8 | 3 | Copyright (C) 2001-2014 Free Software Foundation, Inc. |
53e95fcf JS |
4 | Contributed by Jiri Smid, SuSE Labs. |
5 | ||
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 10 | the Free Software Foundation; either version 3 of the License, or |
53e95fcf JS |
11 | (at your option) any later version. |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
53e95fcf JS |
20 | |
21 | #include "defs.h" | |
22 | #include "inferior.h" | |
23 | #include "gdbcore.h" | |
24 | #include "regcache.h" | |
a055a187 | 25 | #include "regset.h" |
4056d258 | 26 | #include "linux-nat.h" |
8695c747 | 27 | #include "amd64-linux-tdep.h" |
3e3aea48 MM |
28 | #include "linux-btrace.h" |
29 | #include "btrace.h" | |
c4f35dd8 | 30 | |
53e95fcf | 31 | #include "gdb_assert.h" |
0e9f083f | 32 | #include <string.h> |
a055a187 L |
33 | #include "elf/common.h" |
34 | #include <sys/uio.h> | |
53e95fcf JS |
35 | #include <sys/ptrace.h> |
36 | #include <sys/debugreg.h> | |
37 | #include <sys/syscall.h> | |
38 | #include <sys/procfs.h> | |
6fea9e18 | 39 | #include <sys/user.h> |
c43af07c EZ |
40 | #include <asm/prctl.h> |
41 | /* FIXME ezannoni-2003-07-09: we need <sys/reg.h> to be included after | |
42 | <asm/ptrace.h> because the latter redefines FS and GS for no apparent | |
43 | reason, and those definitions don't match the ones that libpthread_db | |
44 | uses, which come from <sys/reg.h>. */ | |
0963b4bd | 45 | /* ezannoni-2003-07-09: I think this is fixed. The extraneous defs have |
c43af07c EZ |
46 | been removed from ptrace.h in the kernel. However, better safe than |
47 | sorry. */ | |
48 | #include <asm/ptrace.h> | |
33a0a2ac | 49 | #include <sys/reg.h> |
c43af07c | 50 | #include "gdb_proc_service.h" |
33a0a2ac | 51 | |
c4f35dd8 MK |
52 | /* Prototypes for supply_gregset etc. */ |
53 | #include "gregset.h" | |
54 | ||
9c1488cb | 55 | #include "amd64-tdep.h" |
60fac5b8 MK |
56 | #include "i386-linux-tdep.h" |
57 | #include "amd64-nat.h" | |
9bb9e8ad | 58 | #include "i386-nat.h" |
a055a187 L |
59 | #include "i386-xstate.h" |
60 | ||
61 | #ifndef PTRACE_GETREGSET | |
62 | #define PTRACE_GETREGSET 0x4204 | |
63 | #endif | |
64 | ||
65 | #ifndef PTRACE_SETREGSET | |
66 | #define PTRACE_SETREGSET 0x4205 | |
67 | #endif | |
68 | ||
7b50312a PA |
69 | /* Per-thread arch-specific data we want to keep. */ |
70 | ||
71 | struct arch_lwp_info | |
72 | { | |
73 | /* Non-zero if our copy differs from what's recorded in the thread. */ | |
74 | int debug_registers_changed; | |
75 | }; | |
76 | ||
a055a187 L |
77 | /* Does the current host support PTRACE_GETREGSET? */ |
78 | static int have_ptrace_getregset = -1; | |
60fac5b8 | 79 | |
60fac5b8 MK |
80 | /* Mapping between the general-purpose registers in GNU/Linux x86-64 |
81 | `struct user' format and GDB's register cache layout for GNU/Linux | |
82 | i386. | |
83 | ||
84 | Note that most GNU/Linux x86-64 registers are 64-bit, while the | |
85 | GNU/Linux i386 registers are all 32-bit, but since we're | |
86 | little-endian we get away with that. */ | |
87 | ||
88 | /* From <sys/reg.h> on GNU/Linux i386. */ | |
430eaf2e | 89 | static int amd64_linux_gregset32_reg_offset[] = |
60fac5b8 | 90 | { |
f5859b4d MK |
91 | RAX * 8, RCX * 8, /* %eax, %ecx */ |
92 | RDX * 8, RBX * 8, /* %edx, %ebx */ | |
93 | RSP * 8, RBP * 8, /* %esp, %ebp */ | |
94 | RSI * 8, RDI * 8, /* %esi, %edi */ | |
95 | RIP * 8, EFLAGS * 8, /* %eip, %eflags */ | |
96 | CS * 8, SS * 8, /* %cs, %ss */ | |
97 | DS * 8, ES * 8, /* %ds, %es */ | |
98 | FS * 8, GS * 8, /* %fs, %gs */ | |
60fac5b8 MK |
99 | -1, -1, -1, -1, -1, -1, -1, -1, |
100 | -1, -1, -1, -1, -1, -1, -1, -1, | |
101 | -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
a055a187 | 102 | -1, -1, -1, -1, -1, -1, -1, -1, |
01f9f808 MS |
103 | -1, -1, -1, -1, /* MPX registers BND0 ... BND3. */ |
104 | -1, -1, /* MPX registers BNDCFGU, BNDSTATUS. */ | |
105 | -1, -1, -1, -1, -1, -1, -1, -1, /* k0 ... k7 (AVX512) */ | |
106 | -1, -1, -1, -1, -1, -1, -1, -1, /* zmm0 ... zmm7 (AVX512) */ | |
107 | ORIG_RAX * 8 /* "orig_eax" */ | |
60fac5b8 | 108 | }; |
53e95fcf JS |
109 | \f |
110 | ||
111 | /* Transfering the general-purpose registers between GDB, inferiors | |
112 | and core files. */ | |
113 | ||
60fac5b8 | 114 | /* Fill GDB's register cache with the general-purpose register values |
53e95fcf JS |
115 | in *GREGSETP. */ |
116 | ||
117 | void | |
7f7fe91e | 118 | supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp) |
53e95fcf | 119 | { |
7f7fe91e | 120 | amd64_supply_native_gregset (regcache, gregsetp, -1); |
53e95fcf JS |
121 | } |
122 | ||
60fac5b8 MK |
123 | /* Fill register REGNUM (if it is a general-purpose register) in |
124 | *GREGSETP with the value in GDB's register cache. If REGNUM is -1, | |
53e95fcf JS |
125 | do this for all registers. */ |
126 | ||
127 | void | |
7f7fe91e UW |
128 | fill_gregset (const struct regcache *regcache, |
129 | elf_gregset_t *gregsetp, int regnum) | |
53e95fcf | 130 | { |
7f7fe91e | 131 | amd64_collect_native_gregset (regcache, gregsetp, regnum); |
53e95fcf JS |
132 | } |
133 | ||
53e95fcf JS |
134 | /* Transfering floating-point registers between GDB, inferiors and cores. */ |
135 | ||
60fac5b8 | 136 | /* Fill GDB's register cache with the floating-point and SSE register |
c4f35dd8 | 137 | values in *FPREGSETP. */ |
53e95fcf JS |
138 | |
139 | void | |
7f7fe91e | 140 | supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp) |
53e95fcf | 141 | { |
7f7fe91e | 142 | amd64_supply_fxsave (regcache, -1, fpregsetp); |
53e95fcf JS |
143 | } |
144 | ||
8dda9770 | 145 | /* Fill register REGNUM (if it is a floating-point or SSE register) in |
60fac5b8 | 146 | *FPREGSETP with the value in GDB's register cache. If REGNUM is |
c4f35dd8 | 147 | -1, do this for all registers. */ |
53e95fcf JS |
148 | |
149 | void | |
7f7fe91e UW |
150 | fill_fpregset (const struct regcache *regcache, |
151 | elf_fpregset_t *fpregsetp, int regnum) | |
53e95fcf | 152 | { |
7f7fe91e | 153 | amd64_collect_fxsave (regcache, regnum, fpregsetp); |
53e95fcf | 154 | } |
53e95fcf JS |
155 | \f |
156 | ||
157 | /* Transferring arbitrary registers between GDB and inferior. */ | |
158 | ||
60fac5b8 | 159 | /* Fetch register REGNUM from the child process. If REGNUM is -1, do |
53e95fcf JS |
160 | this for all registers (including the floating point and SSE |
161 | registers). */ | |
162 | ||
10d6c8cd | 163 | static void |
28439f5e PA |
164 | amd64_linux_fetch_inferior_registers (struct target_ops *ops, |
165 | struct regcache *regcache, int regnum) | |
53e95fcf | 166 | { |
f8028488 | 167 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
53e95fcf JS |
168 | int tid; |
169 | ||
a4b6fc86 | 170 | /* GNU/Linux LWP ID's are process ID's. */ |
dfd4cc63 | 171 | tid = ptid_get_lwp (inferior_ptid); |
c4f35dd8 | 172 | if (tid == 0) |
dfd4cc63 | 173 | tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */ |
53e95fcf | 174 | |
f8028488 | 175 | if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum)) |
53e95fcf | 176 | { |
99679982 MK |
177 | elf_gregset_t regs; |
178 | ||
179 | if (ptrace (PTRACE_GETREGS, tid, 0, (long) ®s) < 0) | |
edefbb7c | 180 | perror_with_name (_("Couldn't get registers")); |
99679982 | 181 | |
56be3814 | 182 | amd64_supply_native_gregset (regcache, ®s, -1); |
60fac5b8 MK |
183 | if (regnum != -1) |
184 | return; | |
53e95fcf JS |
185 | } |
186 | ||
f8028488 | 187 | if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum)) |
53e95fcf | 188 | { |
99679982 | 189 | elf_fpregset_t fpregs; |
53e95fcf | 190 | |
a055a187 L |
191 | if (have_ptrace_getregset) |
192 | { | |
193 | char xstateregs[I386_XSTATE_MAX_SIZE]; | |
194 | struct iovec iov; | |
195 | ||
196 | iov.iov_base = xstateregs; | |
197 | iov.iov_len = sizeof (xstateregs); | |
198 | if (ptrace (PTRACE_GETREGSET, tid, | |
199 | (unsigned int) NT_X86_XSTATE, (long) &iov) < 0) | |
200 | perror_with_name (_("Couldn't get extended state status")); | |
99679982 | 201 | |
a055a187 L |
202 | amd64_supply_xsave (regcache, -1, xstateregs); |
203 | } | |
204 | else | |
205 | { | |
206 | if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0) | |
207 | perror_with_name (_("Couldn't get floating point status")); | |
208 | ||
209 | amd64_supply_fxsave (regcache, -1, &fpregs); | |
210 | } | |
99679982 | 211 | } |
53e95fcf JS |
212 | } |
213 | ||
60fac5b8 MK |
214 | /* Store register REGNUM back into the child process. If REGNUM is |
215 | -1, do this for all registers (including the floating-point and SSE | |
53e95fcf | 216 | registers). */ |
c4f35dd8 | 217 | |
10d6c8cd | 218 | static void |
28439f5e PA |
219 | amd64_linux_store_inferior_registers (struct target_ops *ops, |
220 | struct regcache *regcache, int regnum) | |
53e95fcf | 221 | { |
f8028488 | 222 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
53e95fcf JS |
223 | int tid; |
224 | ||
a4b6fc86 | 225 | /* GNU/Linux LWP ID's are process ID's. */ |
dfd4cc63 | 226 | tid = ptid_get_lwp (inferior_ptid); |
c4f35dd8 | 227 | if (tid == 0) |
dfd4cc63 | 228 | tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */ |
53e95fcf | 229 | |
f8028488 | 230 | if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum)) |
53e95fcf | 231 | { |
99679982 MK |
232 | elf_gregset_t regs; |
233 | ||
234 | if (ptrace (PTRACE_GETREGS, tid, 0, (long) ®s) < 0) | |
edefbb7c | 235 | perror_with_name (_("Couldn't get registers")); |
99679982 | 236 | |
56be3814 | 237 | amd64_collect_native_gregset (regcache, ®s, regnum); |
99679982 MK |
238 | |
239 | if (ptrace (PTRACE_SETREGS, tid, 0, (long) ®s) < 0) | |
edefbb7c | 240 | perror_with_name (_("Couldn't write registers")); |
99679982 | 241 | |
60fac5b8 MK |
242 | if (regnum != -1) |
243 | return; | |
53e95fcf JS |
244 | } |
245 | ||
f8028488 | 246 | if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum)) |
53e95fcf | 247 | { |
99679982 MK |
248 | elf_fpregset_t fpregs; |
249 | ||
a055a187 L |
250 | if (have_ptrace_getregset) |
251 | { | |
252 | char xstateregs[I386_XSTATE_MAX_SIZE]; | |
253 | struct iovec iov; | |
254 | ||
255 | iov.iov_base = xstateregs; | |
256 | iov.iov_len = sizeof (xstateregs); | |
257 | if (ptrace (PTRACE_GETREGSET, tid, | |
258 | (unsigned int) NT_X86_XSTATE, (long) &iov) < 0) | |
259 | perror_with_name (_("Couldn't get extended state status")); | |
99679982 | 260 | |
a055a187 L |
261 | amd64_collect_xsave (regcache, regnum, xstateregs, 0); |
262 | ||
263 | if (ptrace (PTRACE_SETREGSET, tid, | |
264 | (unsigned int) NT_X86_XSTATE, (long) &iov) < 0) | |
265 | perror_with_name (_("Couldn't write extended state status")); | |
266 | } | |
267 | else | |
268 | { | |
269 | if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0) | |
270 | perror_with_name (_("Couldn't get floating point status")); | |
99679982 | 271 | |
a055a187 | 272 | amd64_collect_fxsave (regcache, regnum, &fpregs); |
99679982 | 273 | |
a055a187 L |
274 | if (ptrace (PTRACE_SETFPREGS, tid, 0, (long) &fpregs) < 0) |
275 | perror_with_name (_("Couldn't write floating point status")); | |
276 | } | |
53e95fcf | 277 | } |
53e95fcf JS |
278 | } |
279 | \f | |
9f0bdab8 DJ |
280 | /* Support for debug registers. */ |
281 | ||
c4f35dd8 | 282 | static unsigned long |
9f0bdab8 | 283 | amd64_linux_dr_get (ptid_t ptid, int regnum) |
c4f35dd8 MK |
284 | { |
285 | int tid; | |
286 | unsigned long value; | |
53e95fcf | 287 | |
dfd4cc63 | 288 | tid = ptid_get_lwp (ptid); |
9f0bdab8 | 289 | if (tid == 0) |
dfd4cc63 | 290 | tid = ptid_get_pid (ptid); |
53e95fcf | 291 | |
c4f35dd8 | 292 | errno = 0; |
9f0bdab8 | 293 | value = ptrace (PTRACE_PEEKUSER, tid, |
c4f35dd8 MK |
294 | offsetof (struct user, u_debugreg[regnum]), 0); |
295 | if (errno != 0) | |
edefbb7c | 296 | perror_with_name (_("Couldn't read debug register")); |
53e95fcf | 297 | |
c4f35dd8 MK |
298 | return value; |
299 | } | |
53e95fcf | 300 | |
a79d3c27 JK |
301 | /* Set debug register REGNUM to VALUE in only the one LWP of PTID. */ |
302 | ||
53e95fcf | 303 | static void |
9f0bdab8 | 304 | amd64_linux_dr_set (ptid_t ptid, int regnum, unsigned long value) |
53e95fcf | 305 | { |
c4f35dd8 | 306 | int tid; |
53e95fcf | 307 | |
dfd4cc63 | 308 | tid = ptid_get_lwp (ptid); |
9f0bdab8 | 309 | if (tid == 0) |
dfd4cc63 | 310 | tid = ptid_get_pid (ptid); |
53e95fcf | 311 | |
c4f35dd8 | 312 | errno = 0; |
9f0bdab8 DJ |
313 | ptrace (PTRACE_POKEUSER, tid, |
314 | offsetof (struct user, u_debugreg[regnum]), value); | |
c4f35dd8 | 315 | if (errno != 0) |
edefbb7c | 316 | perror_with_name (_("Couldn't write debug register")); |
c4f35dd8 | 317 | } |
53e95fcf | 318 | |
7b50312a | 319 | /* Return the inferior's debug register REGNUM. */ |
a79d3c27 | 320 | |
7b50312a PA |
321 | static CORE_ADDR |
322 | amd64_linux_dr_get_addr (int regnum) | |
c4f35dd8 | 323 | { |
7b50312a PA |
324 | /* DR6 and DR7 are retrieved with some other way. */ |
325 | gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR); | |
9f0bdab8 | 326 | |
7b50312a | 327 | return amd64_linux_dr_get (inferior_ptid, regnum); |
c4f35dd8 | 328 | } |
53e95fcf | 329 | |
7b50312a | 330 | /* Return the inferior's DR7 debug control register. */ |
a79d3c27 | 331 | |
7b50312a PA |
332 | static unsigned long |
333 | amd64_linux_dr_get_control (void) | |
b7c4cbf8 | 334 | { |
7b50312a PA |
335 | return amd64_linux_dr_get (inferior_ptid, DR_CONTROL); |
336 | } | |
9f0bdab8 | 337 | |
7b50312a PA |
338 | /* Get DR_STATUS from only the one LWP of INFERIOR_PTID. */ |
339 | ||
340 | static unsigned long | |
341 | amd64_linux_dr_get_status (void) | |
342 | { | |
343 | return amd64_linux_dr_get (inferior_ptid, DR_STATUS); | |
344 | } | |
345 | ||
26cb8b7c PA |
346 | /* Callback for iterate_over_lwps. Update the debug registers of |
347 | LWP. */ | |
7b50312a PA |
348 | |
349 | static int | |
350 | update_debug_registers_callback (struct lwp_info *lwp, void *arg) | |
351 | { | |
6e012a6c PA |
352 | if (lwp->arch_private == NULL) |
353 | lwp->arch_private = XCNEW (struct arch_lwp_info); | |
354 | ||
7b50312a PA |
355 | /* The actual update is done later just before resuming the lwp, we |
356 | just mark that the registers need updating. */ | |
357 | lwp->arch_private->debug_registers_changed = 1; | |
358 | ||
359 | /* If the lwp isn't stopped, force it to momentarily pause, so we | |
360 | can update its debug registers. */ | |
361 | if (!lwp->stopped) | |
362 | linux_stop_lwp (lwp); | |
c4f35dd8 | 363 | |
8da828f7 | 364 | /* Continue the iteration. */ |
7b50312a | 365 | return 0; |
b7c4cbf8 AJ |
366 | } |
367 | ||
7b50312a | 368 | /* Set DR_CONTROL to CONTROL in all LWPs of the current inferior. */ |
a79d3c27 | 369 | |
9bb9e8ad | 370 | static void |
7b50312a | 371 | amd64_linux_dr_set_control (unsigned long control) |
53e95fcf | 372 | { |
26cb8b7c PA |
373 | ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid)); |
374 | ||
375 | iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL); | |
53e95fcf | 376 | } |
8cfda98c | 377 | |
7b50312a PA |
378 | /* Set address REGNUM (zero based) to ADDR in all LWPs of the current |
379 | inferior. */ | |
a79d3c27 | 380 | |
7b50312a PA |
381 | static void |
382 | amd64_linux_dr_set_addr (int regnum, CORE_ADDR addr) | |
8cfda98c | 383 | { |
26cb8b7c PA |
384 | ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid)); |
385 | ||
7b50312a PA |
386 | gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR); |
387 | ||
26cb8b7c | 388 | iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL); |
9f0bdab8 DJ |
389 | } |
390 | ||
7b50312a PA |
391 | /* Called when resuming a thread. |
392 | If the debug regs have changed, update the thread's copies. */ | |
a79d3c27 JK |
393 | |
394 | static void | |
7b50312a | 395 | amd64_linux_prepare_to_resume (struct lwp_info *lwp) |
a79d3c27 | 396 | { |
7b50312a | 397 | int clear_status = 0; |
a79d3c27 | 398 | |
6e012a6c PA |
399 | /* NULL means this is the main thread still going through the shell, |
400 | or, no watchpoint has been set yet. In that case, there's | |
401 | nothing to do. */ | |
402 | if (lwp->arch_private == NULL) | |
403 | return; | |
404 | ||
7b50312a | 405 | if (lwp->arch_private->debug_registers_changed) |
a79d3c27 | 406 | { |
26cb8b7c PA |
407 | struct i386_debug_reg_state *state |
408 | = i386_debug_reg_state (ptid_get_pid (lwp->ptid)); | |
7b50312a PA |
409 | int i; |
410 | ||
4403d8e9 JK |
411 | /* On Linux kernel before 2.6.33 commit |
412 | 72f674d203cd230426437cdcf7dd6f681dad8b0d | |
413 | if you enable a breakpoint by the DR_CONTROL bits you need to have | |
414 | already written the corresponding DR_FIRSTADDR...DR_LASTADDR registers. | |
415 | ||
416 | Ensure DR_CONTROL gets written as the very last register here. */ | |
417 | ||
7b50312a PA |
418 | for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++) |
419 | if (state->dr_ref_count[i] > 0) | |
420 | { | |
421 | amd64_linux_dr_set (lwp->ptid, i, state->dr_mirror[i]); | |
422 | ||
423 | /* If we're setting a watchpoint, any change the inferior | |
424 | had done itself to the debug registers needs to be | |
425 | discarded, otherwise, i386_stopped_data_address can get | |
426 | confused. */ | |
427 | clear_status = 1; | |
428 | } | |
429 | ||
430 | amd64_linux_dr_set (lwp->ptid, DR_CONTROL, state->dr_control_mirror); | |
431 | ||
432 | lwp->arch_private->debug_registers_changed = 0; | |
a79d3c27 | 433 | } |
a79d3c27 | 434 | |
7b50312a PA |
435 | if (clear_status || lwp->stopped_by_watchpoint) |
436 | amd64_linux_dr_set (lwp->ptid, DR_STATUS, 0); | |
437 | } | |
a79d3c27 | 438 | |
9f0bdab8 | 439 | static void |
7b50312a | 440 | amd64_linux_new_thread (struct lwp_info *lp) |
9f0bdab8 | 441 | { |
7b50312a | 442 | struct arch_lwp_info *info = XCNEW (struct arch_lwp_info); |
9f0bdab8 | 443 | |
7b50312a | 444 | info->debug_registers_changed = 1; |
9f0bdab8 | 445 | |
7b50312a | 446 | lp->arch_private = info; |
8cfda98c | 447 | } |
26cb8b7c PA |
448 | |
449 | /* linux_nat_new_fork hook. */ | |
450 | ||
451 | static void | |
452 | amd64_linux_new_fork (struct lwp_info *parent, pid_t child_pid) | |
453 | { | |
454 | pid_t parent_pid; | |
455 | struct i386_debug_reg_state *parent_state; | |
456 | struct i386_debug_reg_state *child_state; | |
457 | ||
458 | /* NULL means no watchpoint has ever been set in the parent. In | |
459 | that case, there's nothing to do. */ | |
460 | if (parent->arch_private == NULL) | |
461 | return; | |
462 | ||
463 | /* Linux kernel before 2.6.33 commit | |
464 | 72f674d203cd230426437cdcf7dd6f681dad8b0d | |
465 | will inherit hardware debug registers from parent | |
466 | on fork/vfork/clone. Newer Linux kernels create such tasks with | |
467 | zeroed debug registers. | |
468 | ||
469 | GDB core assumes the child inherits the watchpoints/hw | |
470 | breakpoints of the parent, and will remove them all from the | |
471 | forked off process. Copy the debug registers mirrors into the | |
472 | new process so that all breakpoints and watchpoints can be | |
473 | removed together. The debug registers mirror will become zeroed | |
474 | in the end before detaching the forked off process, thus making | |
475 | this compatible with older Linux kernels too. */ | |
476 | ||
477 | parent_pid = ptid_get_pid (parent->ptid); | |
478 | parent_state = i386_debug_reg_state (parent_pid); | |
479 | child_state = i386_debug_reg_state (child_pid); | |
480 | *child_state = *parent_state; | |
481 | } | |
482 | ||
5bca7895 | 483 | \f |
c43af07c | 484 | |
50d71875 AC |
485 | /* This function is called by libthread_db as part of its handling of |
486 | a request for a thread's local storage address. */ | |
487 | ||
5bca7895 | 488 | ps_err_e |
c43af07c EZ |
489 | ps_get_thread_area (const struct ps_prochandle *ph, |
490 | lwpid_t lwpid, int idx, void **base) | |
491 | { | |
f5656ead | 492 | if (gdbarch_bfd_arch_info (target_gdbarch ())->bits_per_word == 32) |
50d71875 AC |
493 | { |
494 | /* The full structure is found in <asm-i386/ldt.h>. The second | |
495 | integer is the LDT's base_address and that is used to locate | |
496 | the thread's local storage. See i386-linux-nat.c more | |
497 | info. */ | |
498 | unsigned int desc[4]; | |
499 | ||
500 | /* This code assumes that "int" is 32 bits and that | |
501 | GET_THREAD_AREA returns no more than 4 int values. */ | |
502 | gdb_assert (sizeof (int) == 4); | |
503 | #ifndef PTRACE_GET_THREAD_AREA | |
504 | #define PTRACE_GET_THREAD_AREA 25 | |
505 | #endif | |
506 | if (ptrace (PTRACE_GET_THREAD_AREA, | |
507 | lwpid, (void *) (long) idx, (unsigned long) &desc) < 0) | |
508 | return PS_ERR; | |
509 | ||
510 | /* Extend the value to 64 bits. Here it's assumed that a "long" | |
511 | and a "void *" are the same. */ | |
512 | (*base) = (void *) (long) desc[1]; | |
513 | return PS_OK; | |
514 | } | |
515 | else | |
516 | { | |
517 | /* This definition comes from prctl.h, but some kernels may not | |
518 | have it. */ | |
c43af07c EZ |
519 | #ifndef PTRACE_ARCH_PRCTL |
520 | #define PTRACE_ARCH_PRCTL 30 | |
521 | #endif | |
50d71875 AC |
522 | /* FIXME: ezannoni-2003-07-09 see comment above about include |
523 | file order. We could be getting bogus values for these two. */ | |
524 | gdb_assert (FS < ELF_NGREG); | |
525 | gdb_assert (GS < ELF_NGREG); | |
526 | switch (idx) | |
527 | { | |
528 | case FS: | |
6fea9e18 L |
529 | #ifdef HAVE_STRUCT_USER_REGS_STRUCT_FS_BASE |
530 | { | |
531 | /* PTRACE_ARCH_PRCTL is obsolete since 2.6.25, where the | |
532 | fs_base and gs_base fields of user_regs_struct can be | |
533 | used directly. */ | |
534 | unsigned long fs; | |
535 | errno = 0; | |
536 | fs = ptrace (PTRACE_PEEKUSER, lwpid, | |
537 | offsetof (struct user_regs_struct, fs_base), 0); | |
538 | if (errno == 0) | |
539 | { | |
540 | *base = (void *) fs; | |
541 | return PS_OK; | |
542 | } | |
543 | } | |
544 | #endif | |
50d71875 AC |
545 | if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_FS) == 0) |
546 | return PS_OK; | |
547 | break; | |
548 | case GS: | |
6fea9e18 L |
549 | #ifdef HAVE_STRUCT_USER_REGS_STRUCT_GS_BASE |
550 | { | |
551 | unsigned long gs; | |
552 | errno = 0; | |
553 | gs = ptrace (PTRACE_PEEKUSER, lwpid, | |
554 | offsetof (struct user_regs_struct, gs_base), 0); | |
555 | if (errno == 0) | |
556 | { | |
557 | *base = (void *) gs; | |
558 | return PS_OK; | |
559 | } | |
560 | } | |
561 | #endif | |
50d71875 AC |
562 | if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_GS) == 0) |
563 | return PS_OK; | |
564 | break; | |
565 | default: /* Should not happen. */ | |
566 | return PS_BADADDR; | |
567 | } | |
c43af07c | 568 | } |
b6d42148 | 569 | return PS_ERR; /* ptrace failed. */ |
c43af07c | 570 | } |
5bca7895 | 571 | \f |
c43af07c | 572 | |
2e97a79e TT |
573 | static void (*super_post_startup_inferior) (struct target_ops *self, |
574 | ptid_t ptid); | |
10d6c8cd DJ |
575 | |
576 | static void | |
2e97a79e | 577 | amd64_linux_child_post_startup_inferior (struct target_ops *self, ptid_t ptid) |
4056d258 ML |
578 | { |
579 | i386_cleanup_dregs (); | |
2e97a79e | 580 | super_post_startup_inferior (self, ptid); |
4056d258 | 581 | } |
60fac5b8 MK |
582 | \f |
583 | ||
5b009018 PA |
584 | /* When GDB is built as a 64-bit application on linux, the |
585 | PTRACE_GETSIGINFO data is always presented in 64-bit layout. Since | |
586 | debugging a 32-bit inferior with a 64-bit GDB should look the same | |
587 | as debugging it with a 32-bit GDB, we do the 32-bit <-> 64-bit | |
588 | conversion in-place ourselves. */ | |
589 | ||
590 | /* These types below (compat_*) define a siginfo type that is layout | |
591 | compatible with the siginfo type exported by the 32-bit userspace | |
592 | support. */ | |
593 | ||
594 | typedef int compat_int_t; | |
595 | typedef unsigned int compat_uptr_t; | |
596 | ||
597 | typedef int compat_time_t; | |
598 | typedef int compat_timer_t; | |
599 | typedef int compat_clock_t; | |
600 | ||
601 | struct compat_timeval | |
602 | { | |
603 | compat_time_t tv_sec; | |
604 | int tv_usec; | |
605 | }; | |
606 | ||
607 | typedef union compat_sigval | |
608 | { | |
609 | compat_int_t sival_int; | |
610 | compat_uptr_t sival_ptr; | |
611 | } compat_sigval_t; | |
612 | ||
613 | typedef struct compat_siginfo | |
614 | { | |
615 | int si_signo; | |
616 | int si_errno; | |
617 | int si_code; | |
618 | ||
619 | union | |
620 | { | |
621 | int _pad[((128 / sizeof (int)) - 3)]; | |
622 | ||
623 | /* kill() */ | |
624 | struct | |
625 | { | |
626 | unsigned int _pid; | |
627 | unsigned int _uid; | |
628 | } _kill; | |
629 | ||
630 | /* POSIX.1b timers */ | |
631 | struct | |
632 | { | |
633 | compat_timer_t _tid; | |
634 | int _overrun; | |
635 | compat_sigval_t _sigval; | |
636 | } _timer; | |
637 | ||
638 | /* POSIX.1b signals */ | |
639 | struct | |
640 | { | |
641 | unsigned int _pid; | |
642 | unsigned int _uid; | |
643 | compat_sigval_t _sigval; | |
644 | } _rt; | |
645 | ||
646 | /* SIGCHLD */ | |
647 | struct | |
648 | { | |
649 | unsigned int _pid; | |
650 | unsigned int _uid; | |
651 | int _status; | |
652 | compat_clock_t _utime; | |
653 | compat_clock_t _stime; | |
654 | } _sigchld; | |
655 | ||
656 | /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */ | |
657 | struct | |
658 | { | |
659 | unsigned int _addr; | |
660 | } _sigfault; | |
661 | ||
662 | /* SIGPOLL */ | |
663 | struct | |
664 | { | |
665 | int _band; | |
666 | int _fd; | |
667 | } _sigpoll; | |
668 | } _sifields; | |
669 | } compat_siginfo_t; | |
670 | ||
ba224c70 L |
671 | /* For x32, clock_t in _sigchld is 64bit aligned at 4 bytes. */ |
672 | typedef struct compat_x32_clock | |
673 | { | |
674 | int lower; | |
675 | int upper; | |
676 | } compat_x32_clock_t; | |
677 | ||
678 | typedef struct compat_x32_siginfo | |
679 | { | |
680 | int si_signo; | |
681 | int si_errno; | |
682 | int si_code; | |
683 | ||
684 | union | |
685 | { | |
686 | int _pad[((128 / sizeof (int)) - 3)]; | |
687 | ||
688 | /* kill() */ | |
689 | struct | |
690 | { | |
691 | unsigned int _pid; | |
692 | unsigned int _uid; | |
693 | } _kill; | |
694 | ||
695 | /* POSIX.1b timers */ | |
696 | struct | |
697 | { | |
698 | compat_timer_t _tid; | |
699 | int _overrun; | |
700 | compat_sigval_t _sigval; | |
701 | } _timer; | |
702 | ||
703 | /* POSIX.1b signals */ | |
704 | struct | |
705 | { | |
706 | unsigned int _pid; | |
707 | unsigned int _uid; | |
708 | compat_sigval_t _sigval; | |
709 | } _rt; | |
710 | ||
711 | /* SIGCHLD */ | |
712 | struct | |
713 | { | |
714 | unsigned int _pid; | |
715 | unsigned int _uid; | |
716 | int _status; | |
717 | compat_x32_clock_t _utime; | |
718 | compat_x32_clock_t _stime; | |
719 | } _sigchld; | |
720 | ||
721 | /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */ | |
722 | struct | |
723 | { | |
724 | unsigned int _addr; | |
725 | } _sigfault; | |
726 | ||
727 | /* SIGPOLL */ | |
728 | struct | |
729 | { | |
730 | int _band; | |
731 | int _fd; | |
732 | } _sigpoll; | |
733 | } _sifields; | |
734 | } compat_x32_siginfo_t; | |
735 | ||
5b009018 PA |
736 | #define cpt_si_pid _sifields._kill._pid |
737 | #define cpt_si_uid _sifields._kill._uid | |
738 | #define cpt_si_timerid _sifields._timer._tid | |
739 | #define cpt_si_overrun _sifields._timer._overrun | |
740 | #define cpt_si_status _sifields._sigchld._status | |
741 | #define cpt_si_utime _sifields._sigchld._utime | |
742 | #define cpt_si_stime _sifields._sigchld._stime | |
743 | #define cpt_si_ptr _sifields._rt._sigval.sival_ptr | |
744 | #define cpt_si_addr _sifields._sigfault._addr | |
745 | #define cpt_si_band _sifields._sigpoll._band | |
746 | #define cpt_si_fd _sifields._sigpoll._fd | |
747 | ||
14064aa2 DE |
748 | /* glibc at least up to 2.3.2 doesn't have si_timerid, si_overrun. |
749 | In their place is si_timer1,si_timer2. */ | |
750 | #ifndef si_timerid | |
751 | #define si_timerid si_timer1 | |
752 | #endif | |
753 | #ifndef si_overrun | |
754 | #define si_overrun si_timer2 | |
755 | #endif | |
756 | ||
5b009018 PA |
757 | static void |
758 | compat_siginfo_from_siginfo (compat_siginfo_t *to, siginfo_t *from) | |
759 | { | |
760 | memset (to, 0, sizeof (*to)); | |
761 | ||
762 | to->si_signo = from->si_signo; | |
763 | to->si_errno = from->si_errno; | |
764 | to->si_code = from->si_code; | |
765 | ||
b53a1623 | 766 | if (to->si_code == SI_TIMER) |
5b009018 | 767 | { |
b53a1623 PA |
768 | to->cpt_si_timerid = from->si_timerid; |
769 | to->cpt_si_overrun = from->si_overrun; | |
5b009018 PA |
770 | to->cpt_si_ptr = (intptr_t) from->si_ptr; |
771 | } | |
772 | else if (to->si_code == SI_USER) | |
773 | { | |
774 | to->cpt_si_pid = from->si_pid; | |
775 | to->cpt_si_uid = from->si_uid; | |
776 | } | |
b53a1623 | 777 | else if (to->si_code < 0) |
5b009018 | 778 | { |
b53a1623 PA |
779 | to->cpt_si_pid = from->si_pid; |
780 | to->cpt_si_uid = from->si_uid; | |
5b009018 PA |
781 | to->cpt_si_ptr = (intptr_t) from->si_ptr; |
782 | } | |
783 | else | |
784 | { | |
785 | switch (to->si_signo) | |
786 | { | |
787 | case SIGCHLD: | |
788 | to->cpt_si_pid = from->si_pid; | |
789 | to->cpt_si_uid = from->si_uid; | |
790 | to->cpt_si_status = from->si_status; | |
791 | to->cpt_si_utime = from->si_utime; | |
792 | to->cpt_si_stime = from->si_stime; | |
793 | break; | |
794 | case SIGILL: | |
795 | case SIGFPE: | |
796 | case SIGSEGV: | |
797 | case SIGBUS: | |
798 | to->cpt_si_addr = (intptr_t) from->si_addr; | |
799 | break; | |
800 | case SIGPOLL: | |
801 | to->cpt_si_band = from->si_band; | |
802 | to->cpt_si_fd = from->si_fd; | |
803 | break; | |
804 | default: | |
805 | to->cpt_si_pid = from->si_pid; | |
806 | to->cpt_si_uid = from->si_uid; | |
807 | to->cpt_si_ptr = (intptr_t) from->si_ptr; | |
808 | break; | |
809 | } | |
810 | } | |
811 | } | |
812 | ||
813 | static void | |
814 | siginfo_from_compat_siginfo (siginfo_t *to, compat_siginfo_t *from) | |
815 | { | |
816 | memset (to, 0, sizeof (*to)); | |
817 | ||
818 | to->si_signo = from->si_signo; | |
819 | to->si_errno = from->si_errno; | |
820 | to->si_code = from->si_code; | |
821 | ||
b53a1623 | 822 | if (to->si_code == SI_TIMER) |
5b009018 | 823 | { |
b53a1623 PA |
824 | to->si_timerid = from->cpt_si_timerid; |
825 | to->si_overrun = from->cpt_si_overrun; | |
5b009018 PA |
826 | to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr; |
827 | } | |
828 | else if (to->si_code == SI_USER) | |
829 | { | |
830 | to->si_pid = from->cpt_si_pid; | |
831 | to->si_uid = from->cpt_si_uid; | |
832 | } | |
b53a1623 | 833 | if (to->si_code < 0) |
5b009018 | 834 | { |
b53a1623 PA |
835 | to->si_pid = from->cpt_si_pid; |
836 | to->si_uid = from->cpt_si_uid; | |
5b009018 PA |
837 | to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr; |
838 | } | |
839 | else | |
840 | { | |
841 | switch (to->si_signo) | |
842 | { | |
843 | case SIGCHLD: | |
844 | to->si_pid = from->cpt_si_pid; | |
845 | to->si_uid = from->cpt_si_uid; | |
846 | to->si_status = from->cpt_si_status; | |
847 | to->si_utime = from->cpt_si_utime; | |
848 | to->si_stime = from->cpt_si_stime; | |
849 | break; | |
850 | case SIGILL: | |
851 | case SIGFPE: | |
852 | case SIGSEGV: | |
853 | case SIGBUS: | |
854 | to->si_addr = (void *) (intptr_t) from->cpt_si_addr; | |
855 | break; | |
856 | case SIGPOLL: | |
857 | to->si_band = from->cpt_si_band; | |
858 | to->si_fd = from->cpt_si_fd; | |
859 | break; | |
860 | default: | |
861 | to->si_pid = from->cpt_si_pid; | |
862 | to->si_uid = from->cpt_si_uid; | |
863 | to->si_ptr = (void* ) (intptr_t) from->cpt_si_ptr; | |
864 | break; | |
865 | } | |
866 | } | |
867 | } | |
868 | ||
ba224c70 L |
869 | static void |
870 | compat_x32_siginfo_from_siginfo (compat_x32_siginfo_t *to, | |
871 | siginfo_t *from) | |
872 | { | |
873 | memset (to, 0, sizeof (*to)); | |
874 | ||
875 | to->si_signo = from->si_signo; | |
876 | to->si_errno = from->si_errno; | |
877 | to->si_code = from->si_code; | |
878 | ||
879 | if (to->si_code == SI_TIMER) | |
880 | { | |
881 | to->cpt_si_timerid = from->si_timerid; | |
882 | to->cpt_si_overrun = from->si_overrun; | |
883 | to->cpt_si_ptr = (intptr_t) from->si_ptr; | |
884 | } | |
885 | else if (to->si_code == SI_USER) | |
886 | { | |
887 | to->cpt_si_pid = from->si_pid; | |
888 | to->cpt_si_uid = from->si_uid; | |
889 | } | |
890 | else if (to->si_code < 0) | |
891 | { | |
892 | to->cpt_si_pid = from->si_pid; | |
893 | to->cpt_si_uid = from->si_uid; | |
894 | to->cpt_si_ptr = (intptr_t) from->si_ptr; | |
895 | } | |
896 | else | |
897 | { | |
898 | switch (to->si_signo) | |
899 | { | |
900 | case SIGCHLD: | |
901 | to->cpt_si_pid = from->si_pid; | |
902 | to->cpt_si_uid = from->si_uid; | |
903 | to->cpt_si_status = from->si_status; | |
904 | memcpy (&to->cpt_si_utime, &from->si_utime, | |
905 | sizeof (to->cpt_si_utime)); | |
906 | memcpy (&to->cpt_si_stime, &from->si_stime, | |
907 | sizeof (to->cpt_si_stime)); | |
908 | break; | |
909 | case SIGILL: | |
910 | case SIGFPE: | |
911 | case SIGSEGV: | |
912 | case SIGBUS: | |
913 | to->cpt_si_addr = (intptr_t) from->si_addr; | |
914 | break; | |
915 | case SIGPOLL: | |
916 | to->cpt_si_band = from->si_band; | |
917 | to->cpt_si_fd = from->si_fd; | |
918 | break; | |
919 | default: | |
920 | to->cpt_si_pid = from->si_pid; | |
921 | to->cpt_si_uid = from->si_uid; | |
922 | to->cpt_si_ptr = (intptr_t) from->si_ptr; | |
923 | break; | |
924 | } | |
925 | } | |
926 | } | |
927 | ||
928 | static void | |
929 | siginfo_from_compat_x32_siginfo (siginfo_t *to, | |
930 | compat_x32_siginfo_t *from) | |
931 | { | |
932 | memset (to, 0, sizeof (*to)); | |
933 | ||
934 | to->si_signo = from->si_signo; | |
935 | to->si_errno = from->si_errno; | |
936 | to->si_code = from->si_code; | |
937 | ||
938 | if (to->si_code == SI_TIMER) | |
939 | { | |
940 | to->si_timerid = from->cpt_si_timerid; | |
941 | to->si_overrun = from->cpt_si_overrun; | |
942 | to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr; | |
943 | } | |
944 | else if (to->si_code == SI_USER) | |
945 | { | |
946 | to->si_pid = from->cpt_si_pid; | |
947 | to->si_uid = from->cpt_si_uid; | |
948 | } | |
949 | if (to->si_code < 0) | |
950 | { | |
951 | to->si_pid = from->cpt_si_pid; | |
952 | to->si_uid = from->cpt_si_uid; | |
953 | to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr; | |
954 | } | |
955 | else | |
956 | { | |
957 | switch (to->si_signo) | |
958 | { | |
959 | case SIGCHLD: | |
960 | to->si_pid = from->cpt_si_pid; | |
961 | to->si_uid = from->cpt_si_uid; | |
962 | to->si_status = from->cpt_si_status; | |
963 | memcpy (&to->si_utime, &from->cpt_si_utime, | |
964 | sizeof (to->si_utime)); | |
965 | memcpy (&to->si_stime, &from->cpt_si_stime, | |
966 | sizeof (to->si_stime)); | |
967 | break; | |
968 | case SIGILL: | |
969 | case SIGFPE: | |
970 | case SIGSEGV: | |
971 | case SIGBUS: | |
972 | to->si_addr = (void *) (intptr_t) from->cpt_si_addr; | |
973 | break; | |
974 | case SIGPOLL: | |
975 | to->si_band = from->cpt_si_band; | |
976 | to->si_fd = from->cpt_si_fd; | |
977 | break; | |
978 | default: | |
979 | to->si_pid = from->cpt_si_pid; | |
980 | to->si_uid = from->cpt_si_uid; | |
981 | to->si_ptr = (void* ) (intptr_t) from->cpt_si_ptr; | |
982 | break; | |
983 | } | |
984 | } | |
985 | } | |
986 | ||
5b009018 PA |
987 | /* Convert a native/host siginfo object, into/from the siginfo in the |
988 | layout of the inferiors' architecture. Returns true if any | |
989 | conversion was done; false otherwise. If DIRECTION is 1, then copy | |
990 | from INF to NATIVE. If DIRECTION is 0, copy from NATIVE to | |
991 | INF. */ | |
992 | ||
993 | static int | |
a5362b9a | 994 | amd64_linux_siginfo_fixup (siginfo_t *native, gdb_byte *inf, int direction) |
5b009018 | 995 | { |
ba224c70 L |
996 | struct gdbarch *gdbarch = get_frame_arch (get_current_frame ()); |
997 | ||
5b009018 PA |
998 | /* Is the inferior 32-bit? If so, then do fixup the siginfo |
999 | object. */ | |
ba224c70 | 1000 | if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32) |
5b009018 | 1001 | { |
a5362b9a | 1002 | gdb_assert (sizeof (siginfo_t) == sizeof (compat_siginfo_t)); |
5b009018 PA |
1003 | |
1004 | if (direction == 0) | |
1005 | compat_siginfo_from_siginfo ((struct compat_siginfo *) inf, native); | |
1006 | else | |
1007 | siginfo_from_compat_siginfo (native, (struct compat_siginfo *) inf); | |
1008 | ||
ba224c70 L |
1009 | return 1; |
1010 | } | |
1011 | /* No fixup for native x32 GDB. */ | |
1012 | else if (gdbarch_addr_bit (gdbarch) == 32 && sizeof (void *) == 8) | |
1013 | { | |
1014 | gdb_assert (sizeof (siginfo_t) == sizeof (compat_x32_siginfo_t)); | |
1015 | ||
1016 | if (direction == 0) | |
1017 | compat_x32_siginfo_from_siginfo ((struct compat_x32_siginfo *) inf, | |
1018 | native); | |
1019 | else | |
1020 | siginfo_from_compat_x32_siginfo (native, | |
1021 | (struct compat_x32_siginfo *) inf); | |
1022 | ||
5b009018 PA |
1023 | return 1; |
1024 | } | |
1025 | else | |
1026 | return 0; | |
1027 | } | |
1028 | ||
90884b2b L |
1029 | /* Get Linux/x86 target description from running target. |
1030 | ||
1031 | Value of CS segment register: | |
1032 | 1. 64bit process: 0x33. | |
1033 | 2. 32bit process: 0x23. | |
6da38f42 L |
1034 | |
1035 | Value of DS segment register: | |
1036 | 1. LP64 process: 0x0. | |
1037 | 2. X32 process: 0x2b. | |
90884b2b L |
1038 | */ |
1039 | ||
1040 | #define AMD64_LINUX_USER64_CS 0x33 | |
6da38f42 | 1041 | #define AMD64_LINUX_X32_DS 0x2b |
90884b2b L |
1042 | |
1043 | static const struct target_desc * | |
1044 | amd64_linux_read_description (struct target_ops *ops) | |
1045 | { | |
1046 | unsigned long cs; | |
6da38f42 | 1047 | unsigned long ds; |
90884b2b | 1048 | int tid; |
a055a187 | 1049 | int is_64bit; |
6da38f42 | 1050 | int is_x32; |
a055a187 | 1051 | static uint64_t xcr0; |
90884b2b L |
1052 | |
1053 | /* GNU/Linux LWP ID's are process ID's. */ | |
dfd4cc63 | 1054 | tid = ptid_get_lwp (inferior_ptid); |
90884b2b | 1055 | if (tid == 0) |
dfd4cc63 | 1056 | tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */ |
90884b2b L |
1057 | |
1058 | /* Get CS register. */ | |
1059 | errno = 0; | |
1060 | cs = ptrace (PTRACE_PEEKUSER, tid, | |
1061 | offsetof (struct user_regs_struct, cs), 0); | |
1062 | if (errno != 0) | |
1063 | perror_with_name (_("Couldn't get CS register")); | |
1064 | ||
a055a187 L |
1065 | is_64bit = cs == AMD64_LINUX_USER64_CS; |
1066 | ||
6da38f42 L |
1067 | /* Get DS register. */ |
1068 | errno = 0; | |
1069 | ds = ptrace (PTRACE_PEEKUSER, tid, | |
1070 | offsetof (struct user_regs_struct, ds), 0); | |
1071 | if (errno != 0) | |
1072 | perror_with_name (_("Couldn't get DS register")); | |
1073 | ||
1074 | is_x32 = ds == AMD64_LINUX_X32_DS; | |
1075 | ||
1076 | if (sizeof (void *) == 4 && is_64bit && !is_x32) | |
1077 | error (_("Can't debug 64-bit process with 32-bit GDB")); | |
1078 | ||
a055a187 L |
1079 | if (have_ptrace_getregset == -1) |
1080 | { | |
1081 | uint64_t xstateregs[(I386_XSTATE_SSE_SIZE / sizeof (uint64_t))]; | |
1082 | struct iovec iov; | |
1083 | ||
1084 | iov.iov_base = xstateregs; | |
1085 | iov.iov_len = sizeof (xstateregs); | |
1086 | ||
1087 | /* Check if PTRACE_GETREGSET works. */ | |
1088 | if (ptrace (PTRACE_GETREGSET, tid, | |
1089 | (unsigned int) NT_X86_XSTATE, (long) &iov) < 0) | |
1090 | have_ptrace_getregset = 0; | |
1091 | else | |
1092 | { | |
1093 | have_ptrace_getregset = 1; | |
1094 | ||
1095 | /* Get XCR0 from XSAVE extended state. */ | |
1096 | xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET | |
1097 | / sizeof (uint64_t))]; | |
1098 | } | |
1099 | } | |
1100 | ||
1101 | /* Check the native XCR0 only if PTRACE_GETREGSET is available. */ | |
e43e105e | 1102 | if (have_ptrace_getregset && (xcr0 & I386_XSTATE_ALL_MASK)) |
a055a187 | 1103 | { |
e43e105e | 1104 | switch (xcr0 & I386_XSTATE_ALL_MASK) |
6da38f42 | 1105 | { |
01f9f808 MS |
1106 | case I386_XSTATE_MPX_AVX512_MASK: |
1107 | case I386_XSTATE_AVX512_MASK: | |
1108 | if (is_64bit) | |
1109 | { | |
1110 | if (is_x32) | |
1111 | return tdesc_x32_avx512_linux; | |
1112 | else | |
1113 | return tdesc_amd64_avx512_linux; | |
1114 | } | |
1115 | else | |
1116 | return tdesc_i386_avx512_linux; | |
e43e105e WT |
1117 | case I386_XSTATE_MPX_MASK: |
1118 | if (is_64bit) | |
1119 | { | |
1120 | if (is_x32) | |
1121 | return tdesc_x32_avx_linux; /* No MPX on x32 using AVX. */ | |
1122 | else | |
1123 | return tdesc_amd64_mpx_linux; | |
1124 | } | |
1125 | else | |
1126 | return tdesc_i386_mpx_linux; | |
1127 | case I386_XSTATE_AVX_MASK: | |
1128 | if (is_64bit) | |
1129 | { | |
1130 | if (is_x32) | |
1131 | return tdesc_x32_avx_linux; | |
1132 | else | |
1133 | return tdesc_amd64_avx_linux; | |
1134 | } | |
1135 | else | |
1136 | return tdesc_i386_avx_linux; | |
1137 | default: | |
1138 | if (is_64bit) | |
1139 | { | |
1140 | if (is_x32) | |
1141 | return tdesc_x32_linux; | |
1142 | else | |
1143 | return tdesc_amd64_linux; | |
1144 | } | |
6da38f42 | 1145 | else |
e43e105e | 1146 | return tdesc_i386_linux; |
6da38f42 | 1147 | } |
a055a187 | 1148 | } |
90884b2b | 1149 | else |
a055a187 L |
1150 | { |
1151 | if (is_64bit) | |
6da38f42 L |
1152 | { |
1153 | if (is_x32) | |
1154 | return tdesc_x32_linux; | |
1155 | else | |
1156 | return tdesc_amd64_linux; | |
1157 | } | |
a055a187 L |
1158 | else |
1159 | return tdesc_i386_linux; | |
1160 | } | |
90884b2b L |
1161 | } |
1162 | ||
3e3aea48 MM |
1163 | /* Enable branch tracing. */ |
1164 | ||
1165 | static struct btrace_target_info * | |
e3c49f88 | 1166 | amd64_linux_enable_btrace (struct target_ops *self, ptid_t ptid) |
3e3aea48 MM |
1167 | { |
1168 | struct btrace_target_info *tinfo; | |
1169 | struct gdbarch *gdbarch; | |
1170 | ||
1171 | errno = 0; | |
1172 | tinfo = linux_enable_btrace (ptid); | |
1173 | ||
1174 | if (tinfo == NULL) | |
1175 | error (_("Could not enable branch tracing for %s: %s."), | |
1176 | target_pid_to_str (ptid), safe_strerror (errno)); | |
1177 | ||
1178 | /* Fill in the size of a pointer in bits. */ | |
1179 | gdbarch = target_thread_architecture (ptid); | |
1180 | tinfo->ptr_bits = gdbarch_ptr_bit (gdbarch); | |
1181 | ||
1182 | return tinfo; | |
1183 | } | |
1184 | ||
1185 | /* Disable branch tracing. */ | |
1186 | ||
1187 | static void | |
25e95349 TT |
1188 | amd64_linux_disable_btrace (struct target_ops *self, |
1189 | struct btrace_target_info *tinfo) | |
3e3aea48 | 1190 | { |
969c39fb | 1191 | enum btrace_error errcode = linux_disable_btrace (tinfo); |
3e3aea48 | 1192 | |
969c39fb MM |
1193 | if (errcode != BTRACE_ERR_NONE) |
1194 | error (_("Could not disable branch tracing.")); | |
3e3aea48 MM |
1195 | } |
1196 | ||
1197 | /* Teardown branch tracing. */ | |
1198 | ||
1199 | static void | |
1777056d TT |
1200 | amd64_linux_teardown_btrace (struct target_ops *self, |
1201 | struct btrace_target_info *tinfo) | |
3e3aea48 MM |
1202 | { |
1203 | /* Ignore errors. */ | |
1204 | linux_disable_btrace (tinfo); | |
1205 | } | |
1206 | ||
39c49f83 TT |
1207 | static enum btrace_error |
1208 | amd64_linux_read_btrace (struct target_ops *self, | |
1209 | VEC (btrace_block_s) **data, | |
1210 | struct btrace_target_info *btinfo, | |
1211 | enum btrace_read_type type) | |
1212 | { | |
1213 | return linux_read_btrace (data, btinfo, type); | |
1214 | } | |
1215 | ||
60fac5b8 | 1216 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
430eaf2e | 1217 | void _initialize_amd64_linux_nat (void); |
60fac5b8 MK |
1218 | |
1219 | void | |
430eaf2e | 1220 | _initialize_amd64_linux_nat (void) |
60fac5b8 | 1221 | { |
10d6c8cd DJ |
1222 | struct target_ops *t; |
1223 | ||
430eaf2e | 1224 | amd64_native_gregset32_reg_offset = amd64_linux_gregset32_reg_offset; |
60fac5b8 | 1225 | amd64_native_gregset32_num_regs = I386_LINUX_NUM_REGS; |
6cd6a2ae | 1226 | amd64_native_gregset64_reg_offset = amd64_linux_gregset_reg_offset; |
8695c747 | 1227 | amd64_native_gregset64_num_regs = AMD64_LINUX_NUM_REGS; |
60fac5b8 | 1228 | |
430eaf2e | 1229 | gdb_assert (ARRAY_SIZE (amd64_linux_gregset32_reg_offset) |
60fac5b8 | 1230 | == amd64_native_gregset32_num_regs); |
10d6c8cd DJ |
1231 | |
1232 | /* Fill in the generic GNU/Linux methods. */ | |
1233 | t = linux_target (); | |
1234 | ||
c03374d5 DJ |
1235 | i386_use_watchpoints (t); |
1236 | ||
9bb9e8ad PM |
1237 | i386_dr_low.set_control = amd64_linux_dr_set_control; |
1238 | i386_dr_low.set_addr = amd64_linux_dr_set_addr; | |
7b50312a | 1239 | i386_dr_low.get_addr = amd64_linux_dr_get_addr; |
9bb9e8ad | 1240 | i386_dr_low.get_status = amd64_linux_dr_get_status; |
7b50312a | 1241 | i386_dr_low.get_control = amd64_linux_dr_get_control; |
9bb9e8ad PM |
1242 | i386_set_debug_register_length (8); |
1243 | ||
10d6c8cd DJ |
1244 | /* Override the GNU/Linux inferior startup hook. */ |
1245 | super_post_startup_inferior = t->to_post_startup_inferior; | |
1246 | t->to_post_startup_inferior = amd64_linux_child_post_startup_inferior; | |
1247 | ||
1248 | /* Add our register access methods. */ | |
1249 | t->to_fetch_registers = amd64_linux_fetch_inferior_registers; | |
1250 | t->to_store_registers = amd64_linux_store_inferior_registers; | |
1251 | ||
90884b2b L |
1252 | t->to_read_description = amd64_linux_read_description; |
1253 | ||
3e3aea48 MM |
1254 | /* Add btrace methods. */ |
1255 | t->to_supports_btrace = linux_supports_btrace; | |
1256 | t->to_enable_btrace = amd64_linux_enable_btrace; | |
1257 | t->to_disable_btrace = amd64_linux_disable_btrace; | |
1258 | t->to_teardown_btrace = amd64_linux_teardown_btrace; | |
39c49f83 | 1259 | t->to_read_btrace = amd64_linux_read_btrace; |
3e3aea48 | 1260 | |
10d6c8cd | 1261 | /* Register the target. */ |
f973ed9c | 1262 | linux_nat_add_target (t); |
9f0bdab8 | 1263 | linux_nat_set_new_thread (t, amd64_linux_new_thread); |
26cb8b7c PA |
1264 | linux_nat_set_new_fork (t, amd64_linux_new_fork); |
1265 | linux_nat_set_forget_process (t, i386_forget_process); | |
5b009018 | 1266 | linux_nat_set_siginfo_fixup (t, amd64_linux_siginfo_fixup); |
7b50312a | 1267 | linux_nat_set_prepare_to_resume (t, amd64_linux_prepare_to_resume); |
60fac5b8 | 1268 | } |