f254f3838981b795845a5b5bbef8240633829e3f
[external/binutils.git] / gdb / gdbserver / linux-x86-low.c
1 /* GNU/Linux/x86-64 specific low level interface, for the remote server
2    for GDB.
3    Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
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 <stddef.h>
22 #include <signal.h>
23 #include "server.h"
24 #include "linux-low.h"
25 #include "i387-fp.h"
26 #include "i386-low.h"
27 #include "i386-xstate.h"
28 #include "elf/common.h"
29
30 #include "gdb_proc_service.h"
31
32 /* Defined in auto-generated file i386-linux.c.  */
33 void init_registers_i386_linux (void);
34 /* Defined in auto-generated file amd64-linux.c.  */
35 void init_registers_amd64_linux (void);
36 /* Defined in auto-generated file i386-avx-linux.c.  */
37 void init_registers_i386_avx_linux (void);
38 /* Defined in auto-generated file amd64-avx-linux.c.  */
39 void init_registers_amd64_avx_linux (void);
40 /* Defined in auto-generated file i386-mmx-linux.c.  */
41 void init_registers_i386_mmx_linux (void);
42
43 /* Backward compatibility for gdb without XML support.  */
44
45 static const char *xmltarget_i386_linux_no_xml = "@<target>\
46 <architecture>i386</architecture>\
47 <osabi>GNU/Linux</osabi>\
48 </target>";
49
50 #ifdef __x86_64__
51 static const char *xmltarget_amd64_linux_no_xml = "@<target>\
52 <architecture>i386:x86-64</architecture>\
53 <osabi>GNU/Linux</osabi>\
54 </target>";
55 #endif
56
57 #include <sys/reg.h>
58 #include <sys/procfs.h>
59 #include <sys/ptrace.h>
60 #include <sys/uio.h>
61
62 #ifndef PTRACE_GETREGSET
63 #define PTRACE_GETREGSET        0x4204
64 #endif
65
66 #ifndef PTRACE_SETREGSET
67 #define PTRACE_SETREGSET        0x4205
68 #endif
69
70
71 #ifndef PTRACE_GET_THREAD_AREA
72 #define PTRACE_GET_THREAD_AREA 25
73 #endif
74
75 /* This definition comes from prctl.h, but some kernels may not have it.  */
76 #ifndef PTRACE_ARCH_PRCTL
77 #define PTRACE_ARCH_PRCTL      30
78 #endif
79
80 /* The following definitions come from prctl.h, but may be absent
81    for certain configurations.  */
82 #ifndef ARCH_GET_FS
83 #define ARCH_SET_GS 0x1001
84 #define ARCH_SET_FS 0x1002
85 #define ARCH_GET_FS 0x1003
86 #define ARCH_GET_GS 0x1004
87 #endif
88
89 /* Per-process arch-specific data we want to keep.  */
90
91 struct arch_process_info
92 {
93   struct i386_debug_reg_state debug_reg_state;
94 };
95
96 /* Per-thread arch-specific data we want to keep.  */
97
98 struct arch_lwp_info
99 {
100   /* Non-zero if our copy differs from what's recorded in the thread.  */
101   int debug_registers_changed;
102 };
103
104 #ifdef __x86_64__
105
106 /* Mapping between the general-purpose registers in `struct user'
107    format and GDB's register array layout.
108    Note that the transfer layout uses 64-bit regs.  */
109 static /*const*/ int i386_regmap[] = 
110 {
111   RAX * 8, RCX * 8, RDX * 8, RBX * 8,
112   RSP * 8, RBP * 8, RSI * 8, RDI * 8,
113   RIP * 8, EFLAGS * 8, CS * 8, SS * 8,
114   DS * 8, ES * 8, FS * 8, GS * 8
115 };
116
117 #define I386_NUM_REGS (sizeof (i386_regmap) / sizeof (i386_regmap[0]))
118
119 /* So code below doesn't have to care, i386 or amd64.  */
120 #define ORIG_EAX ORIG_RAX
121
122 static const int x86_64_regmap[] =
123 {
124   RAX * 8, RBX * 8, RCX * 8, RDX * 8,
125   RSI * 8, RDI * 8, RBP * 8, RSP * 8,
126   R8 * 8, R9 * 8, R10 * 8, R11 * 8,
127   R12 * 8, R13 * 8, R14 * 8, R15 * 8,
128   RIP * 8, EFLAGS * 8, CS * 8, SS * 8,
129   DS * 8, ES * 8, FS * 8, GS * 8,
130   -1, -1, -1, -1, -1, -1, -1, -1,
131   -1, -1, -1, -1, -1, -1, -1, -1,
132   -1, -1, -1, -1, -1, -1, -1, -1,
133   -1, -1, -1, -1, -1, -1, -1, -1, -1,
134   ORIG_RAX * 8
135 };
136
137 #define X86_64_NUM_REGS (sizeof (x86_64_regmap) / sizeof (x86_64_regmap[0]))
138
139 #else /* ! __x86_64__ */
140
141 /* Mapping between the general-purpose registers in `struct user'
142    format and GDB's register array layout.  */
143 static /*const*/ int i386_regmap[] = 
144 {
145   EAX * 4, ECX * 4, EDX * 4, EBX * 4,
146   UESP * 4, EBP * 4, ESI * 4, EDI * 4,
147   EIP * 4, EFL * 4, CS * 4, SS * 4,
148   DS * 4, ES * 4, FS * 4, GS * 4
149 };
150
151 #define I386_NUM_REGS (sizeof (i386_regmap) / sizeof (i386_regmap[0]))
152
153 #endif
154 \f
155 /* Called by libthread_db.  */
156
157 ps_err_e
158 ps_get_thread_area (const struct ps_prochandle *ph,
159                     lwpid_t lwpid, int idx, void **base)
160 {
161 #ifdef __x86_64__
162   int use_64bit = register_size (0) == 8;
163
164   if (use_64bit)
165     {
166       switch (idx)
167         {
168         case FS:
169           if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_FS) == 0)
170             return PS_OK;
171           break;
172         case GS:
173           if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_GS) == 0)
174             return PS_OK;
175           break;
176         default:
177           return PS_BADADDR;
178         }
179       return PS_ERR;
180     }
181 #endif
182
183   {
184     unsigned int desc[4];
185
186     if (ptrace (PTRACE_GET_THREAD_AREA, lwpid,
187                 (void *) (intptr_t) idx, (unsigned long) &desc) < 0)
188       return PS_ERR;
189
190     *(int *)base = desc[1];
191     return PS_OK;
192   }
193 }
194 \f
195 static int
196 i386_cannot_store_register (int regno)
197 {
198   return regno >= I386_NUM_REGS;
199 }
200
201 static int
202 i386_cannot_fetch_register (int regno)
203 {
204   return regno >= I386_NUM_REGS;
205 }
206
207 static void
208 x86_fill_gregset (struct regcache *regcache, void *buf)
209 {
210   int i;
211
212 #ifdef __x86_64__
213   if (register_size (0) == 8)
214     {
215       for (i = 0; i < X86_64_NUM_REGS; i++)
216         if (x86_64_regmap[i] != -1)
217           collect_register (regcache, i, ((char *) buf) + x86_64_regmap[i]);
218       return;
219     }
220 #endif
221
222   for (i = 0; i < I386_NUM_REGS; i++)
223     collect_register (regcache, i, ((char *) buf) + i386_regmap[i]);
224
225   collect_register_by_name (regcache, "orig_eax",
226                             ((char *) buf) + ORIG_EAX * 4);
227 }
228
229 static void
230 x86_store_gregset (struct regcache *regcache, const void *buf)
231 {
232   int i;
233
234 #ifdef __x86_64__
235   if (register_size (0) == 8)
236     {
237       for (i = 0; i < X86_64_NUM_REGS; i++)
238         if (x86_64_regmap[i] != -1)
239           supply_register (regcache, i, ((char *) buf) + x86_64_regmap[i]);
240       return;
241     }
242 #endif
243
244   for (i = 0; i < I386_NUM_REGS; i++)
245     supply_register (regcache, i, ((char *) buf) + i386_regmap[i]);
246
247   supply_register_by_name (regcache, "orig_eax",
248                            ((char *) buf) + ORIG_EAX * 4);
249 }
250
251 static void
252 x86_fill_fpregset (struct regcache *regcache, void *buf)
253 {
254 #ifdef __x86_64__
255   i387_cache_to_fxsave (regcache, buf);
256 #else
257   i387_cache_to_fsave (regcache, buf);
258 #endif
259 }
260
261 static void
262 x86_store_fpregset (struct regcache *regcache, const void *buf)
263 {
264 #ifdef __x86_64__
265   i387_fxsave_to_cache (regcache, buf);
266 #else
267   i387_fsave_to_cache (regcache, buf);
268 #endif
269 }
270
271 #ifndef __x86_64__
272
273 static void
274 x86_fill_fpxregset (struct regcache *regcache, void *buf)
275 {
276   i387_cache_to_fxsave (regcache, buf);
277 }
278
279 static void
280 x86_store_fpxregset (struct regcache *regcache, const void *buf)
281 {
282   i387_fxsave_to_cache (regcache, buf);
283 }
284
285 #endif
286
287 static void
288 x86_fill_xstateregset (struct regcache *regcache, void *buf)
289 {
290   i387_cache_to_xsave (regcache, buf);
291 }
292
293 static void
294 x86_store_xstateregset (struct regcache *regcache, const void *buf)
295 {
296   i387_xsave_to_cache (regcache, buf);
297 }
298
299 /* ??? The non-biarch i386 case stores all the i387 regs twice.
300    Once in i387_.*fsave.* and once in i387_.*fxsave.*.
301    This is, presumably, to handle the case where PTRACE_[GS]ETFPXREGS
302    doesn't work.  IWBN to avoid the duplication in the case where it
303    does work.  Maybe the arch_setup routine could check whether it works
304    and update target_regsets accordingly, maybe by moving target_regsets
305    to linux_target_ops and set the right one there, rather than having to
306    modify the target_regsets global.  */
307
308 struct regset_info target_regsets[] =
309 {
310 #ifdef HAVE_PTRACE_GETREGS
311   { PTRACE_GETREGS, PTRACE_SETREGS, 0, sizeof (elf_gregset_t),
312     GENERAL_REGS,
313     x86_fill_gregset, x86_store_gregset },
314   { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_X86_XSTATE, 0,
315     EXTENDED_REGS, x86_fill_xstateregset, x86_store_xstateregset },
316 # ifndef __x86_64__
317 #  ifdef HAVE_PTRACE_GETFPXREGS
318   { PTRACE_GETFPXREGS, PTRACE_SETFPXREGS, 0, sizeof (elf_fpxregset_t),
319     EXTENDED_REGS,
320     x86_fill_fpxregset, x86_store_fpxregset },
321 #  endif
322 # endif
323   { PTRACE_GETFPREGS, PTRACE_SETFPREGS, 0, sizeof (elf_fpregset_t),
324     FP_REGS,
325     x86_fill_fpregset, x86_store_fpregset },
326 #endif /* HAVE_PTRACE_GETREGS */
327   { 0, 0, 0, -1, -1, NULL, NULL }
328 };
329
330 static CORE_ADDR
331 x86_get_pc (struct regcache *regcache)
332 {
333   int use_64bit = register_size (0) == 8;
334
335   if (use_64bit)
336     {
337       unsigned long pc;
338       collect_register_by_name (regcache, "rip", &pc);
339       return (CORE_ADDR) pc;
340     }
341   else
342     {
343       unsigned int pc;
344       collect_register_by_name (regcache, "eip", &pc);
345       return (CORE_ADDR) pc;
346     }
347 }
348
349 static void
350 x86_set_pc (struct regcache *regcache, CORE_ADDR pc)
351 {
352   int use_64bit = register_size (0) == 8;
353
354   if (use_64bit)
355     {
356       unsigned long newpc = pc;
357       supply_register_by_name (regcache, "rip", &newpc);
358     }
359   else
360     {
361       unsigned int newpc = pc;
362       supply_register_by_name (regcache, "eip", &newpc);
363     }
364 }
365 \f
366 static const unsigned char x86_breakpoint[] = { 0xCC };
367 #define x86_breakpoint_len 1
368
369 static int
370 x86_breakpoint_at (CORE_ADDR pc)
371 {
372   unsigned char c;
373
374   (*the_target->read_memory) (pc, &c, 1);
375   if (c == 0xCC)
376     return 1;
377
378   return 0;
379 }
380 \f
381 /* Support for debug registers.  */
382
383 static unsigned long
384 x86_linux_dr_get (ptid_t ptid, int regnum)
385 {
386   int tid;
387   unsigned long value;
388
389   tid = ptid_get_lwp (ptid);
390
391   errno = 0;
392   value = ptrace (PTRACE_PEEKUSER, tid,
393                   offsetof (struct user, u_debugreg[regnum]), 0);
394   if (errno != 0)
395     error ("Couldn't read debug register");
396
397   return value;
398 }
399
400 static void
401 x86_linux_dr_set (ptid_t ptid, int regnum, unsigned long value)
402 {
403   int tid;
404
405   tid = ptid_get_lwp (ptid);
406
407   errno = 0;
408   ptrace (PTRACE_POKEUSER, tid,
409           offsetof (struct user, u_debugreg[regnum]), value);
410   if (errno != 0)
411     error ("Couldn't write debug register");
412 }
413
414 /* Update the inferior's debug register REGNUM from STATE.  */
415
416 void
417 i386_dr_low_set_addr (const struct i386_debug_reg_state *state, int regnum)
418 {
419   struct inferior_list_entry *lp;
420   CORE_ADDR addr;
421   /* Only need to update the threads of this process.  */
422   int pid = pid_of (get_thread_lwp (current_inferior));
423
424   if (! (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR))
425     fatal ("Invalid debug register %d", regnum);
426
427   addr = state->dr_mirror[regnum];
428
429   for (lp = all_lwps.head; lp; lp = lp->next)
430     {
431       struct lwp_info *lwp = (struct lwp_info *) lp;
432
433       /* The actual update is done later, we just mark that the register
434          needs updating.  */
435       if (pid_of (lwp) == pid)
436         lwp->arch_private->debug_registers_changed = 1;
437     }
438 }
439
440 /* Update the inferior's DR7 debug control register from STATE.  */
441
442 void
443 i386_dr_low_set_control (const struct i386_debug_reg_state *state)
444 {
445   struct inferior_list_entry *lp;
446   /* Only need to update the threads of this process.  */
447   int pid = pid_of (get_thread_lwp (current_inferior));
448
449   for (lp = all_lwps.head; lp; lp = lp->next)
450     {
451       struct lwp_info *lwp = (struct lwp_info *) lp;
452
453       /* The actual update is done later, we just mark that the register
454          needs updating.  */
455       if (pid_of (lwp) == pid)
456         lwp->arch_private->debug_registers_changed = 1;
457     }
458 }
459
460 /* Get the value of the DR6 debug status register from the inferior
461    and record it in STATE.  */
462
463 void
464 i386_dr_low_get_status (struct i386_debug_reg_state *state)
465 {
466   struct lwp_info *lwp = get_thread_lwp (current_inferior);
467   ptid_t ptid = ptid_of (lwp);
468
469   state->dr_status_mirror = x86_linux_dr_get (ptid, DR_STATUS);
470 }
471 \f
472 /* Watchpoint support.  */
473
474 static int
475 x86_insert_point (char type, CORE_ADDR addr, int len)
476 {
477   struct process_info *proc = current_process ();
478   switch (type)
479     {
480     case '0':
481       return set_gdb_breakpoint_at (addr);
482     case '2':
483     case '3':
484     case '4':
485       return i386_low_insert_watchpoint (&proc->private->arch_private->debug_reg_state,
486                                          type, addr, len);
487     default:
488       /* Unsupported.  */
489       return 1;
490     }
491 }
492
493 static int
494 x86_remove_point (char type, CORE_ADDR addr, int len)
495 {
496   struct process_info *proc = current_process ();
497   switch (type)
498     {
499     case '0':
500       return delete_gdb_breakpoint_at (addr);
501     case '2':
502     case '3':
503     case '4':
504       return i386_low_remove_watchpoint (&proc->private->arch_private->debug_reg_state,
505                                          type, addr, len);
506     default:
507       /* Unsupported.  */
508       return 1;
509     }
510 }
511
512 static int
513 x86_stopped_by_watchpoint (void)
514 {
515   struct process_info *proc = current_process ();
516   return i386_low_stopped_by_watchpoint (&proc->private->arch_private->debug_reg_state);
517 }
518
519 static CORE_ADDR
520 x86_stopped_data_address (void)
521 {
522   struct process_info *proc = current_process ();
523   CORE_ADDR addr;
524   if (i386_low_stopped_data_address (&proc->private->arch_private->debug_reg_state,
525                                      &addr))
526     return addr;
527   return 0;
528 }
529 \f
530 /* Called when a new process is created.  */
531
532 static struct arch_process_info *
533 x86_linux_new_process (void)
534 {
535   struct arch_process_info *info = xcalloc (1, sizeof (*info));
536
537   i386_low_init_dregs (&info->debug_reg_state);
538
539   return info;
540 }
541
542 /* Called when a new thread is detected.  */
543
544 static struct arch_lwp_info *
545 x86_linux_new_thread (void)
546 {
547   struct arch_lwp_info *info = xcalloc (1, sizeof (*info));
548
549   info->debug_registers_changed = 1;
550
551   return info;
552 }
553
554 /* Called when resuming a thread.
555    If the debug regs have changed, update the thread's copies.  */
556
557 static void
558 x86_linux_prepare_to_resume (struct lwp_info *lwp)
559 {
560   ptid_t ptid = ptid_of (lwp);
561
562   if (lwp->arch_private->debug_registers_changed)
563     {
564       int i;
565       int pid = ptid_get_pid (ptid);
566       struct process_info *proc = find_process_pid (pid);
567       struct i386_debug_reg_state *state = &proc->private->arch_private->debug_reg_state;
568
569       for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
570         x86_linux_dr_set (ptid, i, state->dr_mirror[i]);
571
572       x86_linux_dr_set (ptid, DR_CONTROL, state->dr_control_mirror);
573
574       lwp->arch_private->debug_registers_changed = 0;
575     }
576
577   if (lwp->stopped_by_watchpoint)
578     x86_linux_dr_set (ptid, DR_STATUS, 0);
579 }
580 \f
581 /* When GDBSERVER 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 GDBSERVER should look the same
584    as debugging it with a 32-bit GDBSERVER, 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 #ifdef __x86_64__
592
593 typedef int compat_int_t;
594 typedef unsigned int compat_uptr_t;
595
596 typedef int compat_time_t;
597 typedef int compat_timer_t;
598 typedef int compat_clock_t;
599
600 struct compat_timeval
601 {
602   compat_time_t tv_sec;
603   int tv_usec;
604 };
605
606 typedef union compat_sigval
607 {
608   compat_int_t sival_int;
609   compat_uptr_t sival_ptr;
610 } compat_sigval_t;
611
612 typedef struct compat_siginfo
613 {
614   int si_signo;
615   int si_errno;
616   int si_code;
617
618   union
619   {
620     int _pad[((128 / sizeof (int)) - 3)];
621
622     /* kill() */
623     struct
624     {
625       unsigned int _pid;
626       unsigned int _uid;
627     } _kill;
628
629     /* POSIX.1b timers */
630     struct
631     {
632       compat_timer_t _tid;
633       int _overrun;
634       compat_sigval_t _sigval;
635     } _timer;
636
637     /* POSIX.1b signals */
638     struct
639     {
640       unsigned int _pid;
641       unsigned int _uid;
642       compat_sigval_t _sigval;
643     } _rt;
644
645     /* SIGCHLD */
646     struct
647     {
648       unsigned int _pid;
649       unsigned int _uid;
650       int _status;
651       compat_clock_t _utime;
652       compat_clock_t _stime;
653     } _sigchld;
654
655     /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
656     struct
657     {
658       unsigned int _addr;
659     } _sigfault;
660
661     /* SIGPOLL */
662     struct
663     {
664       int _band;
665       int _fd;
666     } _sigpoll;
667   } _sifields;
668 } compat_siginfo_t;
669
670 #define cpt_si_pid _sifields._kill._pid
671 #define cpt_si_uid _sifields._kill._uid
672 #define cpt_si_timerid _sifields._timer._tid
673 #define cpt_si_overrun _sifields._timer._overrun
674 #define cpt_si_status _sifields._sigchld._status
675 #define cpt_si_utime _sifields._sigchld._utime
676 #define cpt_si_stime _sifields._sigchld._stime
677 #define cpt_si_ptr _sifields._rt._sigval.sival_ptr
678 #define cpt_si_addr _sifields._sigfault._addr
679 #define cpt_si_band _sifields._sigpoll._band
680 #define cpt_si_fd _sifields._sigpoll._fd
681
682 /* glibc at least up to 2.3.2 doesn't have si_timerid, si_overrun.
683    In their place is si_timer1,si_timer2.  */
684 #ifndef si_timerid
685 #define si_timerid si_timer1
686 #endif
687 #ifndef si_overrun
688 #define si_overrun si_timer2
689 #endif
690
691 static void
692 compat_siginfo_from_siginfo (compat_siginfo_t *to, siginfo_t *from)
693 {
694   memset (to, 0, sizeof (*to));
695
696   to->si_signo = from->si_signo;
697   to->si_errno = from->si_errno;
698   to->si_code = from->si_code;
699
700   if (to->si_code < 0)
701     {
702       to->cpt_si_ptr = (intptr_t) from->si_ptr;
703     }
704   else if (to->si_code == SI_USER)
705     {
706       to->cpt_si_pid = from->si_pid;
707       to->cpt_si_uid = from->si_uid;
708     }
709   else if (to->si_code == SI_TIMER)
710     {
711       to->cpt_si_timerid = from->si_timerid;
712       to->cpt_si_overrun = from->si_overrun;
713       to->cpt_si_ptr = (intptr_t) from->si_ptr;
714     }
715   else
716     {
717       switch (to->si_signo)
718         {
719         case SIGCHLD:
720           to->cpt_si_pid = from->si_pid;
721           to->cpt_si_uid = from->si_uid;
722           to->cpt_si_status = from->si_status;
723           to->cpt_si_utime = from->si_utime;
724           to->cpt_si_stime = from->si_stime;
725           break;
726         case SIGILL:
727         case SIGFPE:
728         case SIGSEGV:
729         case SIGBUS:
730           to->cpt_si_addr = (intptr_t) from->si_addr;
731           break;
732         case SIGPOLL:
733           to->cpt_si_band = from->si_band;
734           to->cpt_si_fd = from->si_fd;
735           break;
736         default:
737           to->cpt_si_pid = from->si_pid;
738           to->cpt_si_uid = from->si_uid;
739           to->cpt_si_ptr = (intptr_t) from->si_ptr;
740           break;
741         }
742     }
743 }
744
745 static void
746 siginfo_from_compat_siginfo (siginfo_t *to, compat_siginfo_t *from)
747 {
748   memset (to, 0, sizeof (*to));
749
750   to->si_signo = from->si_signo;
751   to->si_errno = from->si_errno;
752   to->si_code = from->si_code;
753
754   if (to->si_code < 0)
755     {
756       to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
757     }
758   else if (to->si_code == SI_USER)
759     {
760       to->si_pid = from->cpt_si_pid;
761       to->si_uid = from->cpt_si_uid;
762     }
763   else if (to->si_code == SI_TIMER)
764     {
765       to->si_timerid = from->cpt_si_timerid;
766       to->si_overrun = from->cpt_si_overrun;
767       to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
768     }
769   else
770     {
771       switch (to->si_signo)
772         {
773         case SIGCHLD:
774           to->si_pid = from->cpt_si_pid;
775           to->si_uid = from->cpt_si_uid;
776           to->si_status = from->cpt_si_status;
777           to->si_utime = from->cpt_si_utime;
778           to->si_stime = from->cpt_si_stime;
779           break;
780         case SIGILL:
781         case SIGFPE:
782         case SIGSEGV:
783         case SIGBUS:
784           to->si_addr = (void *) (intptr_t) from->cpt_si_addr;
785           break;
786         case SIGPOLL:
787           to->si_band = from->cpt_si_band;
788           to->si_fd = from->cpt_si_fd;
789           break;
790         default:
791           to->si_pid = from->cpt_si_pid;
792           to->si_uid = from->cpt_si_uid;
793           to->si_ptr = (void* ) (intptr_t) from->cpt_si_ptr;
794           break;
795         }
796     }
797 }
798
799 #endif /* __x86_64__ */
800
801 /* Convert a native/host siginfo object, into/from the siginfo in the
802    layout of the inferiors' architecture.  Returns true if any
803    conversion was done; false otherwise.  If DIRECTION is 1, then copy
804    from INF to NATIVE.  If DIRECTION is 0, copy from NATIVE to
805    INF.  */
806
807 static int
808 x86_siginfo_fixup (struct siginfo *native, void *inf, int direction)
809 {
810 #ifdef __x86_64__
811   /* Is the inferior 32-bit?  If so, then fixup the siginfo object.  */
812   if (register_size (0) == 4)
813     {
814       if (sizeof (struct siginfo) != sizeof (compat_siginfo_t))
815         fatal ("unexpected difference in siginfo");
816
817       if (direction == 0)
818         compat_siginfo_from_siginfo ((struct compat_siginfo *) inf, native);
819       else
820         siginfo_from_compat_siginfo (native, (struct compat_siginfo *) inf);
821
822       return 1;
823     }
824 #endif
825
826   return 0;
827 }
828 \f
829 static int use_xml;
830
831 /* Update gdbserver_xmltarget.  */
832
833 static void
834 x86_linux_update_xmltarget (void)
835 {
836   int pid;
837   struct regset_info *regset;
838   static unsigned long long xcr0;
839   static int have_ptrace_getregset = -1;
840 #if !defined(__x86_64__) && defined(HAVE_PTRACE_GETFPXREGS)
841   static int have_ptrace_getfpxregs = -1;
842 #endif
843
844   if (!current_inferior)
845     return;
846
847   /* Before changing the register cache internal layout or the target
848      regsets, flush the contents of the current valid caches back to
849      the threads.  */
850   regcache_invalidate ();
851
852   pid = pid_of (get_thread_lwp (current_inferior));
853 #ifdef __x86_64__
854   if (num_xmm_registers == 8)
855     init_registers_i386_linux ();
856   else
857     init_registers_amd64_linux ();
858 #else
859     {
860 # ifdef HAVE_PTRACE_GETFPXREGS
861       if (have_ptrace_getfpxregs == -1)
862         {
863           elf_fpxregset_t fpxregs;
864
865           if (ptrace (PTRACE_GETFPXREGS, pid, 0, (int) &fpxregs) < 0)
866             {
867               have_ptrace_getfpxregs = 0;
868               x86_xcr0 = I386_XSTATE_X87_MASK;
869
870               /* Disable PTRACE_GETFPXREGS.  */
871               for (regset = target_regsets;
872                    regset->fill_function != NULL; regset++)
873                 if (regset->get_request == PTRACE_GETFPXREGS)
874                   {
875                     regset->size = 0;
876                     break;
877                   }
878             }
879           else
880             have_ptrace_getfpxregs = 1;
881         }
882
883       if (!have_ptrace_getfpxregs)
884         {
885           init_registers_i386_mmx_linux ();
886           return;
887         }
888 # endif
889       init_registers_i386_linux ();
890     }
891 #endif
892
893   if (!use_xml)
894     {
895       /* Don't use XML.  */
896 #ifdef __x86_64__
897       if (num_xmm_registers == 8)
898         gdbserver_xmltarget = xmltarget_i386_linux_no_xml;
899       else
900         gdbserver_xmltarget = xmltarget_amd64_linux_no_xml;
901 #else
902       gdbserver_xmltarget = xmltarget_i386_linux_no_xml;
903 #endif
904
905       x86_xcr0 = I386_XSTATE_SSE_MASK;
906
907       return;
908     }
909
910   /* Check if XSAVE extended state is supported.  */
911   if (have_ptrace_getregset == -1)
912     {
913       unsigned long long xstateregs[I386_XSTATE_SSE_SIZE / sizeof (long long)];
914       struct iovec iov;
915
916       iov.iov_base = xstateregs;
917       iov.iov_len = sizeof (xstateregs);
918
919       /* Check if PTRACE_GETREGSET works.  */
920       if (ptrace (PTRACE_GETREGSET, pid, (unsigned int) NT_X86_XSTATE,
921                   &iov) < 0)
922         {
923           have_ptrace_getregset = 0;
924           return;
925         }
926       else
927         have_ptrace_getregset = 1;
928
929       /* Get XCR0 from XSAVE extended state at byte 464.  */
930       xcr0 = xstateregs[464 / sizeof (long long)];
931
932       /* Use PTRACE_GETREGSET if it is available.  */
933       for (regset = target_regsets;
934            regset->fill_function != NULL; regset++)
935         if (regset->get_request == PTRACE_GETREGSET)
936           regset->size = I386_XSTATE_SIZE (xcr0);
937         else if (regset->type != GENERAL_REGS)
938           regset->size = 0;
939     }
940
941   if (have_ptrace_getregset)
942     {
943       /* AVX is the highest feature we support.  */
944       if ((xcr0 & I386_XSTATE_AVX_MASK) == I386_XSTATE_AVX_MASK)
945         {
946           x86_xcr0 = xcr0;
947
948 #ifdef __x86_64__
949           /* I386 has 8 xmm regs.  */
950           if (num_xmm_registers == 8)
951             init_registers_i386_avx_linux ();
952           else
953             init_registers_amd64_avx_linux ();
954 #else
955           init_registers_i386_avx_linux ();
956 #endif
957         }
958     }
959 }
960
961 /* Process qSupported query, "xmlRegisters=".  Update the buffer size for
962    PTRACE_GETREGSET.  */
963
964 static void
965 x86_linux_process_qsupported (const char *query)
966 {
967   /* Return if gdb doesn't support XML.  If gdb sends "xmlRegisters="
968      with "i386" in qSupported query, it supports x86 XML target
969      descriptions.  */
970   use_xml = 0;
971   if (query != NULL && strncmp (query, "xmlRegisters=", 13) == 0)
972     {
973       char *copy = xstrdup (query + 13);
974       char *p;
975
976       for (p = strtok (copy, ","); p != NULL; p = strtok (NULL, ","))
977         {
978           if (strcmp (p, "i386") == 0)
979             {
980               use_xml = 1;
981               break;
982             }
983         } 
984
985       free (copy);
986     }
987
988   x86_linux_update_xmltarget ();
989 }
990
991 /* Initialize gdbserver for the architecture of the inferior.  */
992
993 static void
994 x86_arch_setup (void)
995 {
996 #ifdef __x86_64__
997   int pid = pid_of (get_thread_lwp (current_inferior));
998   char *file = linux_child_pid_to_exec_file (pid);
999   int use_64bit = elf_64_file_p (file);
1000
1001   free (file);
1002
1003   if (use_64bit < 0)
1004     {
1005       /* This can only happen if /proc/<pid>/exe is unreadable,
1006          but "that can't happen" if we've gotten this far.
1007          Fall through and assume this is a 32-bit program.  */
1008     }
1009   else if (use_64bit)
1010     {
1011       /* Amd64 doesn't have HAVE_LINUX_USRREGS.  */
1012       the_low_target.num_regs = -1;
1013       the_low_target.regmap = NULL;
1014       the_low_target.cannot_fetch_register = NULL;
1015       the_low_target.cannot_store_register = NULL;
1016
1017       /* Amd64 has 16 xmm regs.  */
1018       num_xmm_registers = 16;
1019
1020       x86_linux_update_xmltarget ();
1021       return;
1022     }
1023 #endif
1024
1025   /* Ok we have a 32-bit inferior.  */
1026
1027   the_low_target.num_regs = I386_NUM_REGS;
1028   the_low_target.regmap = i386_regmap;
1029   the_low_target.cannot_fetch_register = i386_cannot_fetch_register;
1030   the_low_target.cannot_store_register = i386_cannot_store_register;
1031
1032   /* I386 has 8 xmm regs.  */
1033   num_xmm_registers = 8;
1034
1035   x86_linux_update_xmltarget ();
1036 }
1037
1038 static int
1039 x86_supports_tracepoints (void)
1040 {
1041   return 1;
1042 }
1043
1044 /* This is initialized assuming an amd64 target.
1045    x86_arch_setup will correct it for i386 or amd64 targets.  */
1046
1047 struct linux_target_ops the_low_target =
1048 {
1049   x86_arch_setup,
1050   -1,
1051   NULL,
1052   NULL,
1053   NULL,
1054   x86_get_pc,
1055   x86_set_pc,
1056   x86_breakpoint,
1057   x86_breakpoint_len,
1058   NULL,
1059   1,
1060   x86_breakpoint_at,
1061   x86_insert_point,
1062   x86_remove_point,
1063   x86_stopped_by_watchpoint,
1064   x86_stopped_data_address,
1065   /* collect_ptrace_register/supply_ptrace_register are not needed in the
1066      native i386 case (no registers smaller than an xfer unit), and are not
1067      used in the biarch case (HAVE_LINUX_USRREGS is not defined).  */
1068   NULL,
1069   NULL,
1070   /* need to fix up i386 siginfo if host is amd64 */
1071   x86_siginfo_fixup,
1072   x86_linux_new_process,
1073   x86_linux_new_thread,
1074   x86_linux_prepare_to_resume,
1075   x86_linux_process_qsupported,
1076   x86_supports_tracepoints
1077 };