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