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