Move low-level Linux x86 debug register code to a shared file
[deliverable/binutils-gdb.git] / gdb / x86-linux-nat.c
1 /* Native-dependent code for GNU/Linux x86 (i386 and x86-64).
2
3 Copyright (C) 1999-2015 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 "elf/common.h"
23 #include "gdb_proc_service.h"
24 #include <sys/ptrace.h>
25 #include <sys/user.h>
26 #include <sys/procfs.h>
27 #include <sys/uio.h>
28
29 #include "x86-nat.h"
30 #include "linux-nat.h"
31 #ifndef __x86_64__
32 #include "i386-linux-nat.h"
33 #endif
34 #include "x86-linux-nat.h"
35 #include "i386-linux-tdep.h"
36 #ifdef __x86_64__
37 #include "amd64-linux-tdep.h"
38 #endif
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
45 /* Per-thread arch-specific data we want to keep. */
46
47 struct arch_lwp_info
48 {
49 /* Non-zero if our copy differs from what's recorded in the thread. */
50 int debug_registers_changed;
51 };
52
53 /* Does the current host support PTRACE_GETREGSET? */
54 int have_ptrace_getregset = -1;
55
56 /* Called prior to resuming a thread. */
57
58 static void
59 x86_linux_prepare_to_resume (struct lwp_info *lwp)
60 {
61 x86_linux_update_debug_registers (lwp);
62 }
63
64 /* Called when a new thread is detected. */
65
66 static void
67 x86_linux_new_thread (struct lwp_info *lwp)
68 {
69 lwp_set_debug_registers_changed (lwp, 1);
70 }
71 \f
72
73 /* linux_nat_new_fork hook. */
74
75 static void
76 x86_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
77 {
78 pid_t parent_pid;
79 struct x86_debug_reg_state *parent_state;
80 struct x86_debug_reg_state *child_state;
81
82 /* NULL means no watchpoint has ever been set in the parent. In
83 that case, there's nothing to do. */
84 if (parent->arch_private == NULL)
85 return;
86
87 /* Linux kernel before 2.6.33 commit
88 72f674d203cd230426437cdcf7dd6f681dad8b0d
89 will inherit hardware debug registers from parent
90 on fork/vfork/clone. Newer Linux kernels create such tasks with
91 zeroed debug registers.
92
93 GDB core assumes the child inherits the watchpoints/hw
94 breakpoints of the parent, and will remove them all from the
95 forked off process. Copy the debug registers mirrors into the
96 new process so that all breakpoints and watchpoints can be
97 removed together. The debug registers mirror will become zeroed
98 in the end before detaching the forked off process, thus making
99 this compatible with older Linux kernels too. */
100
101 parent_pid = ptid_get_pid (parent->ptid);
102 parent_state = x86_debug_reg_state (parent_pid);
103 child_state = x86_debug_reg_state (child_pid);
104 *child_state = *parent_state;
105 }
106 \f
107
108 static void (*super_post_startup_inferior) (struct target_ops *self,
109 ptid_t ptid);
110
111 static void
112 x86_linux_child_post_startup_inferior (struct target_ops *self, ptid_t ptid)
113 {
114 x86_cleanup_dregs ();
115 super_post_startup_inferior (self, ptid);
116 }
117
118 #ifdef __x86_64__
119 /* Value of CS segment register:
120 64bit process: 0x33
121 32bit process: 0x23 */
122 #define AMD64_LINUX_USER64_CS 0x33
123
124 /* Value of DS segment register:
125 LP64 process: 0x0
126 X32 process: 0x2b */
127 #define AMD64_LINUX_X32_DS 0x2b
128 #endif
129
130 /* Get Linux/x86 target description from running target. */
131
132 static const struct target_desc *
133 x86_linux_read_description (struct target_ops *ops)
134 {
135 int tid;
136 int is_64bit = 0;
137 #ifdef __x86_64__
138 int is_x32;
139 #endif
140 static uint64_t xcr0;
141 uint64_t xcr0_features_bits;
142
143 /* GNU/Linux LWP ID's are process ID's. */
144 tid = ptid_get_lwp (inferior_ptid);
145 if (tid == 0)
146 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
147
148 #ifdef __x86_64__
149 {
150 unsigned long cs;
151 unsigned long ds;
152
153 /* Get CS register. */
154 errno = 0;
155 cs = ptrace (PTRACE_PEEKUSER, tid,
156 offsetof (struct user_regs_struct, cs), 0);
157 if (errno != 0)
158 perror_with_name (_("Couldn't get CS register"));
159
160 is_64bit = cs == AMD64_LINUX_USER64_CS;
161
162 /* Get DS register. */
163 errno = 0;
164 ds = ptrace (PTRACE_PEEKUSER, tid,
165 offsetof (struct user_regs_struct, ds), 0);
166 if (errno != 0)
167 perror_with_name (_("Couldn't get DS register"));
168
169 is_x32 = ds == AMD64_LINUX_X32_DS;
170
171 if (sizeof (void *) == 4 && is_64bit && !is_x32)
172 error (_("Can't debug 64-bit process with 32-bit GDB"));
173 }
174 #elif HAVE_PTRACE_GETFPXREGS
175 if (have_ptrace_getfpxregs == -1)
176 {
177 elf_fpxregset_t fpxregs;
178
179 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
180 {
181 have_ptrace_getfpxregs = 0;
182 have_ptrace_getregset = 0;
183 return tdesc_i386_mmx_linux;
184 }
185 }
186 #endif
187
188 if (have_ptrace_getregset == -1)
189 {
190 uint64_t xstateregs[(X86_XSTATE_SSE_SIZE / sizeof (uint64_t))];
191 struct iovec iov;
192
193 iov.iov_base = xstateregs;
194 iov.iov_len = sizeof (xstateregs);
195
196 /* Check if PTRACE_GETREGSET works. */
197 if (ptrace (PTRACE_GETREGSET, tid,
198 (unsigned int) NT_X86_XSTATE, &iov) < 0)
199 have_ptrace_getregset = 0;
200 else
201 {
202 have_ptrace_getregset = 1;
203
204 /* Get XCR0 from XSAVE extended state. */
205 xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET
206 / sizeof (uint64_t))];
207 }
208 }
209
210 /* Check the native XCR0 only if PTRACE_GETREGSET is available. If
211 PTRACE_GETREGSET is not available then set xcr0_features_bits to
212 zero so that the "no-features" descriptions are returned by the
213 switches below. */
214 if (have_ptrace_getregset)
215 xcr0_features_bits = xcr0 & X86_XSTATE_ALL_MASK;
216 else
217 xcr0_features_bits = 0;
218
219 if (is_64bit)
220 {
221 #ifdef __x86_64__
222 switch (xcr0_features_bits)
223 {
224 case X86_XSTATE_MPX_AVX512_MASK:
225 case X86_XSTATE_AVX512_MASK:
226 if (is_x32)
227 return tdesc_x32_avx512_linux;
228 else
229 return tdesc_amd64_avx512_linux;
230 case X86_XSTATE_MPX_MASK:
231 if (is_x32)
232 return tdesc_x32_avx_linux; /* No MPX on x32 using AVX. */
233 else
234 return tdesc_amd64_mpx_linux;
235 case X86_XSTATE_AVX_MASK:
236 if (is_x32)
237 return tdesc_x32_avx_linux;
238 else
239 return tdesc_amd64_avx_linux;
240 default:
241 if (is_x32)
242 return tdesc_x32_linux;
243 else
244 return tdesc_amd64_linux;
245 }
246 #endif
247 }
248 else
249 {
250 switch (xcr0_features_bits)
251 {
252 case X86_XSTATE_MPX_AVX512_MASK:
253 case X86_XSTATE_AVX512_MASK:
254 return tdesc_i386_avx512_linux;
255 case X86_XSTATE_MPX_MASK:
256 return tdesc_i386_mpx_linux;
257 case X86_XSTATE_AVX_MASK:
258 return tdesc_i386_avx_linux;
259 default:
260 return tdesc_i386_linux;
261 }
262 }
263
264 gdb_assert_not_reached ("failed to return tdesc");
265 }
266 \f
267
268 /* Enable branch tracing. */
269
270 static struct btrace_target_info *
271 x86_linux_enable_btrace (struct target_ops *self, ptid_t ptid,
272 const struct btrace_config *conf)
273 {
274 struct btrace_target_info *tinfo;
275 struct gdbarch *gdbarch;
276
277 errno = 0;
278 tinfo = linux_enable_btrace (ptid, conf);
279
280 if (tinfo == NULL)
281 error (_("Could not enable branch tracing for %s: %s."),
282 target_pid_to_str (ptid), safe_strerror (errno));
283
284 /* Fill in the size of a pointer in bits. */
285 if (tinfo->ptr_bits == 0)
286 {
287 gdbarch = target_thread_architecture (ptid);
288 tinfo->ptr_bits = gdbarch_ptr_bit (gdbarch);
289 }
290 return tinfo;
291 }
292
293 /* Disable branch tracing. */
294
295 static void
296 x86_linux_disable_btrace (struct target_ops *self,
297 struct btrace_target_info *tinfo)
298 {
299 enum btrace_error errcode = linux_disable_btrace (tinfo);
300
301 if (errcode != BTRACE_ERR_NONE)
302 error (_("Could not disable branch tracing."));
303 }
304
305 /* Teardown branch tracing. */
306
307 static void
308 x86_linux_teardown_btrace (struct target_ops *self,
309 struct btrace_target_info *tinfo)
310 {
311 /* Ignore errors. */
312 linux_disable_btrace (tinfo);
313 }
314
315 static enum btrace_error
316 x86_linux_read_btrace (struct target_ops *self,
317 struct btrace_data *data,
318 struct btrace_target_info *btinfo,
319 enum btrace_read_type type)
320 {
321 return linux_read_btrace (data, btinfo, type);
322 }
323
324 /* See to_btrace_conf in target.h. */
325
326 static const struct btrace_config *
327 x86_linux_btrace_conf (struct target_ops *self,
328 const struct btrace_target_info *btinfo)
329 {
330 return linux_btrace_conf (btinfo);
331 }
332
333 \f
334
335 /* Helper for ps_get_thread_area. Sets BASE_ADDR to a pointer to
336 the thread local storage (or its descriptor) and returns PS_OK
337 on success. Returns PS_ERR on failure. */
338
339 ps_err_e
340 x86_linux_get_thread_area (pid_t pid, void *addr, unsigned int *base_addr)
341 {
342 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
343 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
344 4 byte integers in size: `entry_number', `base_addr', `limit',
345 and a bunch of status bits.
346
347 The values returned by this ptrace call should be part of the
348 regcache buffer, and ps_get_thread_area should channel its
349 request through the regcache. That way remote targets could
350 provide the value using the remote protocol and not this direct
351 call.
352
353 Is this function needed? I'm guessing that the `base' is the
354 address of a descriptor that libthread_db uses to find the
355 thread local address base that GDB needs. Perhaps that
356 descriptor is defined by the ABI. Anyway, given that
357 libthread_db calls this function without prompting (gdb
358 requesting tls base) I guess it needs info in there anyway. */
359 unsigned int desc[4];
360
361 /* This code assumes that "int" is 32 bits and that
362 GET_THREAD_AREA returns no more than 4 int values. */
363 gdb_assert (sizeof (int) == 4);
364
365 #ifndef PTRACE_GET_THREAD_AREA
366 #define PTRACE_GET_THREAD_AREA 25
367 #endif
368
369 if (ptrace (PTRACE_GET_THREAD_AREA, pid, addr, &desc) < 0)
370 return PS_ERR;
371
372 *base_addr = desc[1];
373 return PS_OK;
374 }
375 \f
376
377 /* Create an x86 GNU/Linux target. */
378
379 struct target_ops *
380 x86_linux_create_target (void)
381 {
382 /* Fill in the generic GNU/Linux methods. */
383 struct target_ops *t = linux_target ();
384
385 /* Initialize the debug register function vectors. */
386 x86_use_watchpoints (t);
387 x86_dr_low.set_control = x86_linux_dr_set_control;
388 x86_dr_low.set_addr = x86_linux_dr_set_addr;
389 x86_dr_low.get_addr = x86_linux_dr_get_addr;
390 x86_dr_low.get_status = x86_linux_dr_get_status;
391 x86_dr_low.get_control = x86_linux_dr_get_control;
392 x86_set_debug_register_length (sizeof (void *));
393
394 /* Override the GNU/Linux inferior startup hook. */
395 super_post_startup_inferior = t->to_post_startup_inferior;
396 t->to_post_startup_inferior = x86_linux_child_post_startup_inferior;
397
398 /* Add the description reader. */
399 t->to_read_description = x86_linux_read_description;
400
401 /* Add btrace methods. */
402 t->to_supports_btrace = linux_supports_btrace;
403 t->to_enable_btrace = x86_linux_enable_btrace;
404 t->to_disable_btrace = x86_linux_disable_btrace;
405 t->to_teardown_btrace = x86_linux_teardown_btrace;
406 t->to_read_btrace = x86_linux_read_btrace;
407 t->to_btrace_conf = x86_linux_btrace_conf;
408
409 return t;
410 }
411
412 /* Add an x86 GNU/Linux target. */
413
414 void
415 x86_linux_add_target (struct target_ops *t)
416 {
417 linux_nat_add_target (t);
418 linux_nat_set_new_thread (t, x86_linux_new_thread);
419 linux_nat_set_new_fork (t, x86_linux_new_fork);
420 linux_nat_set_forget_process (t, x86_forget_process);
421 linux_nat_set_prepare_to_resume (t, x86_linux_prepare_to_resume);
422 }
This page took 0.037371 seconds and 4 git commands to generate.