1 /* Native-dependent code for GNU/Linux x86 (i386 and x86-64).
3 Copyright (C) 1999-2017 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
22 #include "elf/common.h"
23 #include "gdb_proc_service.h"
24 #include "nat/gdb_ptrace.h"
26 #include <sys/procfs.h>
30 #include "linux-nat.h"
32 #include "i386-linux-nat.h"
34 #include "x86-linux-nat.h"
35 #include "i386-linux-tdep.h"
37 #include "amd64-linux-tdep.h"
39 #include "x86-xstate.h"
40 #include "nat/linux-btrace.h"
41 #include "nat/linux-nat.h"
42 #include "nat/x86-linux.h"
43 #include "nat/x86-linux-dregs.h"
44 #include "nat/linux-ptrace.h"
46 /* Per-thread arch-specific data we want to keep. */
50 /* Non-zero if our copy differs from what's recorded in the thread. */
51 int debug_registers_changed
;
56 /* linux_nat_new_fork hook. */
59 x86_linux_new_fork (struct lwp_info
*parent
, pid_t child_pid
)
62 struct x86_debug_reg_state
*parent_state
;
63 struct x86_debug_reg_state
*child_state
;
65 /* NULL means no watchpoint has ever been set in the parent. In
66 that case, there's nothing to do. */
67 if (parent
->arch_private
== NULL
)
70 /* Linux kernel before 2.6.33 commit
71 72f674d203cd230426437cdcf7dd6f681dad8b0d
72 will inherit hardware debug registers from parent
73 on fork/vfork/clone. Newer Linux kernels create such tasks with
74 zeroed debug registers.
76 GDB core assumes the child inherits the watchpoints/hw
77 breakpoints of the parent, and will remove them all from the
78 forked off process. Copy the debug registers mirrors into the
79 new process so that all breakpoints and watchpoints can be
80 removed together. The debug registers mirror will become zeroed
81 in the end before detaching the forked off process, thus making
82 this compatible with older Linux kernels too. */
84 parent_pid
= ptid_get_pid (parent
->ptid
);
85 parent_state
= x86_debug_reg_state (parent_pid
);
86 child_state
= x86_debug_reg_state (child_pid
);
87 *child_state
= *parent_state
;
91 static void (*super_post_startup_inferior
) (struct target_ops
*self
,
95 x86_linux_child_post_startup_inferior (struct target_ops
*self
, ptid_t ptid
)
98 super_post_startup_inferior (self
, ptid
);
102 /* Value of CS segment register:
104 32bit process: 0x23 */
105 #define AMD64_LINUX_USER64_CS 0x33
107 /* Value of DS segment register:
110 #define AMD64_LINUX_X32_DS 0x2b
113 /* Get Linux/x86 target description from running target. */
115 static const struct target_desc
*
116 x86_linux_read_description (struct target_ops
*ops
)
123 static uint64_t xcr0
;
124 uint64_t xcr0_features_bits
;
126 /* GNU/Linux LWP ID's are process ID's. */
127 tid
= ptid_get_lwp (inferior_ptid
);
129 tid
= ptid_get_pid (inferior_ptid
); /* Not a threaded program. */
136 /* Get CS register. */
138 cs
= ptrace (PTRACE_PEEKUSER
, tid
,
139 offsetof (struct user_regs_struct
, cs
), 0);
141 perror_with_name (_("Couldn't get CS register"));
143 is_64bit
= cs
== AMD64_LINUX_USER64_CS
;
145 /* Get DS register. */
147 ds
= ptrace (PTRACE_PEEKUSER
, tid
,
148 offsetof (struct user_regs_struct
, ds
), 0);
150 perror_with_name (_("Couldn't get DS register"));
152 is_x32
= ds
== AMD64_LINUX_X32_DS
;
154 if (sizeof (void *) == 4 && is_64bit
&& !is_x32
)
155 error (_("Can't debug 64-bit process with 32-bit GDB"));
157 #elif HAVE_PTRACE_GETFPXREGS
158 if (have_ptrace_getfpxregs
== -1)
160 elf_fpxregset_t fpxregs
;
162 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, (int) &fpxregs
) < 0)
164 have_ptrace_getfpxregs
= 0;
165 have_ptrace_getregset
= TRIBOOL_FALSE
;
166 return tdesc_i386_mmx_linux
;
171 if (have_ptrace_getregset
== TRIBOOL_UNKNOWN
)
173 uint64_t xstateregs
[(X86_XSTATE_SSE_SIZE
/ sizeof (uint64_t))];
176 iov
.iov_base
= xstateregs
;
177 iov
.iov_len
= sizeof (xstateregs
);
179 /* Check if PTRACE_GETREGSET works. */
180 if (ptrace (PTRACE_GETREGSET
, tid
,
181 (unsigned int) NT_X86_XSTATE
, &iov
) < 0)
182 have_ptrace_getregset
= TRIBOOL_FALSE
;
185 have_ptrace_getregset
= TRIBOOL_TRUE
;
187 /* Get XCR0 from XSAVE extended state. */
188 xcr0
= xstateregs
[(I386_LINUX_XSAVE_XCR0_OFFSET
189 / sizeof (uint64_t))];
193 /* Check the native XCR0 only if PTRACE_GETREGSET is available. If
194 PTRACE_GETREGSET is not available then set xcr0_features_bits to
195 zero so that the "no-features" descriptions are returned by the
197 if (have_ptrace_getregset
== TRIBOOL_TRUE
)
198 xcr0_features_bits
= xcr0
& X86_XSTATE_ALL_MASK
;
200 xcr0_features_bits
= 0;
205 switch (xcr0_features_bits
)
207 case X86_XSTATE_AVX_MPX_AVX512_PKU_MASK
:
209 /* No MPX, PKU on x32, fall back to AVX-AVX512. */
210 return tdesc_x32_avx_avx512_linux
;
212 return tdesc_amd64_avx_mpx_avx512_pku_linux
;
213 case X86_XSTATE_AVX_AVX512_MASK
:
215 return tdesc_x32_avx_avx512_linux
;
217 return tdesc_amd64_avx_avx512_linux
;
218 case X86_XSTATE_MPX_MASK
:
220 return tdesc_x32_avx_linux
; /* No MPX on x32 using AVX. */
222 return tdesc_amd64_mpx_linux
;
223 case X86_XSTATE_AVX_MPX_MASK
:
225 return tdesc_x32_avx_linux
; /* No MPX on x32 using AVX. */
227 return tdesc_amd64_avx_mpx_linux
;
228 case X86_XSTATE_AVX_MASK
:
230 return tdesc_x32_avx_linux
;
232 return tdesc_amd64_avx_linux
;
235 return tdesc_x32_linux
;
237 return tdesc_amd64_linux
;
243 switch (xcr0_features_bits
)
245 case X86_XSTATE_AVX_MPX_AVX512_PKU_MASK
:
246 return tdesc_i386_avx_mpx_avx512_pku_linux
;
247 case X86_XSTATE_AVX_AVX512_MASK
:
248 return tdesc_i386_avx_avx512_linux
;
249 case X86_XSTATE_MPX_MASK
:
250 return tdesc_i386_mpx_linux
;
251 case X86_XSTATE_AVX_MPX_MASK
:
252 return tdesc_i386_avx_mpx_linux
;
253 case X86_XSTATE_AVX_MASK
:
254 return tdesc_i386_avx_linux
;
256 return tdesc_i386_linux
;
260 gdb_assert_not_reached ("failed to return tdesc");
264 /* Enable branch tracing. */
266 static struct btrace_target_info
*
267 x86_linux_enable_btrace (struct target_ops
*self
, ptid_t ptid
,
268 const struct btrace_config
*conf
)
270 struct btrace_target_info
*tinfo
;
273 tinfo
= linux_enable_btrace (ptid
, conf
);
276 error (_("Could not enable branch tracing for %s: %s."),
277 target_pid_to_str (ptid
), safe_strerror (errno
));
282 /* Disable branch tracing. */
285 x86_linux_disable_btrace (struct target_ops
*self
,
286 struct btrace_target_info
*tinfo
)
288 enum btrace_error errcode
= linux_disable_btrace (tinfo
);
290 if (errcode
!= BTRACE_ERR_NONE
)
291 error (_("Could not disable branch tracing."));
294 /* Teardown branch tracing. */
297 x86_linux_teardown_btrace (struct target_ops
*self
,
298 struct btrace_target_info
*tinfo
)
301 linux_disable_btrace (tinfo
);
304 static enum btrace_error
305 x86_linux_read_btrace (struct target_ops
*self
,
306 struct btrace_data
*data
,
307 struct btrace_target_info
*btinfo
,
308 enum btrace_read_type type
)
310 return linux_read_btrace (data
, btinfo
, type
);
313 /* See to_btrace_conf in target.h. */
315 static const struct btrace_config
*
316 x86_linux_btrace_conf (struct target_ops
*self
,
317 const struct btrace_target_info
*btinfo
)
319 return linux_btrace_conf (btinfo
);
324 /* Helper for ps_get_thread_area. Sets BASE_ADDR to a pointer to
325 the thread local storage (or its descriptor) and returns PS_OK
326 on success. Returns PS_ERR on failure. */
329 x86_linux_get_thread_area (pid_t pid
, void *addr
, unsigned int *base_addr
)
331 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
332 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
333 4 byte integers in size: `entry_number', `base_addr', `limit',
334 and a bunch of status bits.
336 The values returned by this ptrace call should be part of the
337 regcache buffer, and ps_get_thread_area should channel its
338 request through the regcache. That way remote targets could
339 provide the value using the remote protocol and not this direct
342 Is this function needed? I'm guessing that the `base' is the
343 address of a descriptor that libthread_db uses to find the
344 thread local address base that GDB needs. Perhaps that
345 descriptor is defined by the ABI. Anyway, given that
346 libthread_db calls this function without prompting (gdb
347 requesting tls base) I guess it needs info in there anyway. */
348 unsigned int desc
[4];
350 /* This code assumes that "int" is 32 bits and that
351 GET_THREAD_AREA returns no more than 4 int values. */
352 gdb_assert (sizeof (int) == 4);
354 #ifndef PTRACE_GET_THREAD_AREA
355 #define PTRACE_GET_THREAD_AREA 25
358 if (ptrace (PTRACE_GET_THREAD_AREA
, pid
, addr
, &desc
) < 0)
361 *base_addr
= desc
[1];
366 /* Create an x86 GNU/Linux target. */
369 x86_linux_create_target (void)
371 /* Fill in the generic GNU/Linux methods. */
372 struct target_ops
*t
= linux_target ();
374 /* Initialize the debug register function vectors. */
375 x86_use_watchpoints (t
);
376 x86_dr_low
.set_control
= x86_linux_dr_set_control
;
377 x86_dr_low
.set_addr
= x86_linux_dr_set_addr
;
378 x86_dr_low
.get_addr
= x86_linux_dr_get_addr
;
379 x86_dr_low
.get_status
= x86_linux_dr_get_status
;
380 x86_dr_low
.get_control
= x86_linux_dr_get_control
;
381 x86_set_debug_register_length (sizeof (void *));
383 /* Override the GNU/Linux inferior startup hook. */
384 super_post_startup_inferior
= t
->to_post_startup_inferior
;
385 t
->to_post_startup_inferior
= x86_linux_child_post_startup_inferior
;
387 /* Add the description reader. */
388 t
->to_read_description
= x86_linux_read_description
;
390 /* Add btrace methods. */
391 t
->to_supports_btrace
= linux_supports_btrace
;
392 t
->to_enable_btrace
= x86_linux_enable_btrace
;
393 t
->to_disable_btrace
= x86_linux_disable_btrace
;
394 t
->to_teardown_btrace
= x86_linux_teardown_btrace
;
395 t
->to_read_btrace
= x86_linux_read_btrace
;
396 t
->to_btrace_conf
= x86_linux_btrace_conf
;
401 /* Add an x86 GNU/Linux target. */
404 x86_linux_add_target (struct target_ops
*t
)
406 linux_nat_add_target (t
);
407 linux_nat_set_new_thread (t
, x86_linux_new_thread
);
408 linux_nat_set_new_fork (t
, x86_linux_new_fork
);
409 linux_nat_set_forget_process (t
, x86_forget_process
);
410 linux_nat_set_prepare_to_resume (t
, x86_linux_prepare_to_resume
);