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