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