1 /* GNU/Linux/x86-64 specific low level interface, for the remote server
3 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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/>. */
23 #include "linux-low.h"
26 #include "gdb_proc_service.h"
28 /* NOTE: gdb_proc_service.h may include linux/elf.h.
29 We need Elf32_Phdr. If we don't get linux/elf.h we could include
30 elf.h like linux-ppc-low.c does. */
32 /* Defined in auto-generated file reg-i386-linux.c. */
33 void init_registers_i386_linux (void);
34 /* Defined in auto-generated file reg-x86-64-linux.c. */
35 void init_registers_x86_64_linux (void);
38 #include <sys/procfs.h>
39 #include <sys/ptrace.h>
41 #ifndef PTRACE_GET_THREAD_AREA
42 #define PTRACE_GET_THREAD_AREA 25
45 /* This definition comes from prctl.h, but some kernels may not have it. */
46 #ifndef PTRACE_ARCH_PRCTL
47 #define PTRACE_ARCH_PRCTL 30
50 /* The following definitions come from prctl.h, but may be absent
51 for certain configurations. */
53 #define ARCH_SET_GS 0x1001
54 #define ARCH_SET_FS 0x1002
55 #define ARCH_GET_FS 0x1003
56 #define ARCH_GET_GS 0x1004
61 /* Mapping between the general-purpose registers in `struct user'
62 format and GDB's register array layout.
63 Note that the transfer layout uses 64-bit regs. */
64 static /*const*/ int i386_regmap[] =
66 RAX * 8, RCX * 8, RDX * 8, RBX * 8,
67 RSP * 8, RBP * 8, RSI * 8, RDI * 8,
68 RIP * 8, EFLAGS * 8, CS * 8, SS * 8,
69 DS * 8, ES * 8, FS * 8, GS * 8
72 #define I386_NUM_REGS (sizeof (i386_regmap) / sizeof (i386_regmap[0]))
74 /* So code below doesn't have to care, i386 or amd64. */
75 #define ORIG_EAX ORIG_RAX
77 static const int x86_64_regmap[] =
79 RAX * 8, RBX * 8, RCX * 8, RDX * 8,
80 RSI * 8, RDI * 8, RBP * 8, RSP * 8,
81 R8 * 8, R9 * 8, R10 * 8, R11 * 8,
82 R12 * 8, R13 * 8, R14 * 8, R15 * 8,
83 RIP * 8, EFLAGS * 8, CS * 8, SS * 8,
84 DS * 8, ES * 8, FS * 8, GS * 8,
85 -1, -1, -1, -1, -1, -1, -1, -1,
86 -1, -1, -1, -1, -1, -1, -1, -1,
87 -1, -1, -1, -1, -1, -1, -1, -1,
88 -1, -1, -1, -1, -1, -1, -1, -1, -1,
92 #define X86_64_NUM_REGS (sizeof (x86_64_regmap) / sizeof (x86_64_regmap[0]))
94 #else /* ! __x86_64__ */
96 /* Mapping between the general-purpose registers in `struct user'
97 format and GDB's register array layout. */
98 static /*const*/ int i386_regmap[] =
100 EAX * 4, ECX * 4, EDX * 4, EBX * 4,
101 UESP * 4, EBP * 4, ESI * 4, EDI * 4,
102 EIP * 4, EFL * 4, CS * 4, SS * 4,
103 DS * 4, ES * 4, FS * 4, GS * 4
106 #define I386_NUM_REGS (sizeof (i386_regmap) / sizeof (i386_regmap[0]))
110 /* Called by libthread_db. */
113 ps_get_thread_area (const struct ps_prochandle *ph,
114 lwpid_t lwpid, int idx, void **base)
117 int use_64bit = register_size (0) == 8;
124 if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_FS) == 0)
128 if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_GS) == 0)
139 unsigned int desc[4];
141 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid,
142 (void *) (intptr_t) idx, (unsigned long) &desc) < 0)
145 *(int *)base = desc[1];
151 i386_cannot_store_register (int regno)
153 return regno >= I386_NUM_REGS;
157 i386_cannot_fetch_register (int regno)
159 return regno >= I386_NUM_REGS;
163 x86_fill_gregset (void *buf)
168 if (register_size (0) == 8)
170 for (i = 0; i < X86_64_NUM_REGS; i++)
171 if (x86_64_regmap[i] != -1)
172 collect_register (i, ((char *) buf) + x86_64_regmap[i]);
177 for (i = 0; i < I386_NUM_REGS; i++)
178 collect_register (i, ((char *) buf) + i386_regmap[i]);
180 collect_register_by_name ("orig_eax", ((char *) buf) + ORIG_EAX * 4);
184 x86_store_gregset (const void *buf)
189 if (register_size (0) == 8)
191 for (i = 0; i < X86_64_NUM_REGS; i++)
192 if (x86_64_regmap[i] != -1)
193 supply_register (i, ((char *) buf) + x86_64_regmap[i]);
198 for (i = 0; i < I386_NUM_REGS; i++)
199 supply_register (i, ((char *) buf) + i386_regmap[i]);
201 supply_register_by_name ("orig_eax", ((char *) buf) + ORIG_EAX * 4);
205 x86_fill_fpregset (void *buf)
208 i387_cache_to_fxsave (buf);
210 i387_cache_to_fsave (buf);
215 x86_store_fpregset (const void *buf)
218 i387_fxsave_to_cache (buf);
220 i387_fsave_to_cache (buf);
227 x86_fill_fpxregset (void *buf)
229 i387_cache_to_fxsave (buf);
233 x86_store_fpxregset (const void *buf)
235 i387_fxsave_to_cache (buf);
240 /* ??? The non-biarch i386 case stores all the i387 regs twice.
241 Once in i387_.*fsave.* and once in i387_.*fxsave.*.
242 This is, presumably, to handle the case where PTRACE_[GS]ETFPXREGS
243 doesn't work. IWBN to avoid the duplication in the case where it
244 does work. Maybe the arch_setup routine could check whether it works
245 and update target_regsets accordingly, maybe by moving target_regsets
246 to linux_target_ops and set the right one there, rather than having to
247 modify the target_regsets global. */
249 struct regset_info target_regsets[] =
251 #ifdef HAVE_PTRACE_GETREGS
252 { PTRACE_GETREGS, PTRACE_SETREGS, sizeof (elf_gregset_t),
254 x86_fill_gregset, x86_store_gregset },
256 # ifdef HAVE_PTRACE_GETFPXREGS
257 { PTRACE_GETFPXREGS, PTRACE_SETFPXREGS, sizeof (elf_fpxregset_t),
259 x86_fill_fpxregset, x86_store_fpxregset },
262 { PTRACE_GETFPREGS, PTRACE_SETFPREGS, sizeof (elf_fpregset_t),
264 x86_fill_fpregset, x86_store_fpregset },
265 #endif /* HAVE_PTRACE_GETREGS */
266 { 0, 0, -1, -1, NULL, NULL }
272 int use_64bit = register_size (0) == 8;
277 collect_register_by_name ("rip", &pc);
278 return (CORE_ADDR) pc;
283 collect_register_by_name ("eip", &pc);
284 return (CORE_ADDR) pc;
289 x86_set_pc (CORE_ADDR pc)
291 int use_64bit = register_size (0) == 8;
295 unsigned long newpc = pc;
296 supply_register_by_name ("rip", &newpc);
300 unsigned int newpc = pc;
301 supply_register_by_name ("eip", &newpc);
305 static const unsigned char x86_breakpoint[] = { 0xCC };
306 #define x86_breakpoint_len 1
309 x86_breakpoint_at (CORE_ADDR pc)
313 read_inferior_memory (pc, &c, 1);
320 /* When GDBSERVER is built as a 64-bit application on linux, the
321 PTRACE_GETSIGINFO data is always presented in 64-bit layout. Since
322 debugging a 32-bit inferior with a 64-bit GDBSERVER should look the same
323 as debugging it with a 32-bit GDBSERVER, we do the 32-bit <-> 64-bit
324 conversion in-place ourselves. */
326 /* These types below (compat_*) define a siginfo type that is layout
327 compatible with the siginfo type exported by the 32-bit userspace
332 typedef int compat_int_t;
333 typedef unsigned int compat_uptr_t;
335 typedef int compat_time_t;
336 typedef int compat_timer_t;
337 typedef int compat_clock_t;
339 struct compat_timeval
341 compat_time_t tv_sec;
345 typedef union compat_sigval
347 compat_int_t sival_int;
348 compat_uptr_t sival_ptr;
351 typedef struct compat_siginfo
359 int _pad[((128 / sizeof (int)) - 3)];
368 /* POSIX.1b timers */
373 compat_sigval_t _sigval;
376 /* POSIX.1b signals */
381 compat_sigval_t _sigval;
390 compat_clock_t _utime;
391 compat_clock_t _stime;
394 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
409 #define cpt_si_pid _sifields._kill._pid
410 #define cpt_si_uid _sifields._kill._uid
411 #define cpt_si_timerid _sifields._timer._tid
412 #define cpt_si_overrun _sifields._timer._overrun
413 #define cpt_si_status _sifields._sigchld._status
414 #define cpt_si_utime _sifields._sigchld._utime
415 #define cpt_si_stime _sifields._sigchld._stime
416 #define cpt_si_ptr _sifields._rt._sigval.sival_ptr
417 #define cpt_si_addr _sifields._sigfault._addr
418 #define cpt_si_band _sifields._sigpoll._band
419 #define cpt_si_fd _sifields._sigpoll._fd
421 /* glibc at least up to 2.3.2 doesn't have si_timerid, si_overrun.
422 In their place is si_timer1,si_timer2. */
424 #define si_timerid si_timer1
427 #define si_overrun si_timer2
431 compat_siginfo_from_siginfo (compat_siginfo_t *to, siginfo_t *from)
433 memset (to, 0, sizeof (*to));
435 to->si_signo = from->si_signo;
436 to->si_errno = from->si_errno;
437 to->si_code = from->si_code;
441 to->cpt_si_ptr = (intptr_t) from->si_ptr;
443 else if (to->si_code == SI_USER)
445 to->cpt_si_pid = from->si_pid;
446 to->cpt_si_uid = from->si_uid;
448 else if (to->si_code == SI_TIMER)
450 to->cpt_si_timerid = from->si_timerid;
451 to->cpt_si_overrun = from->si_overrun;
452 to->cpt_si_ptr = (intptr_t) from->si_ptr;
456 switch (to->si_signo)
459 to->cpt_si_pid = from->si_pid;
460 to->cpt_si_uid = from->si_uid;
461 to->cpt_si_status = from->si_status;
462 to->cpt_si_utime = from->si_utime;
463 to->cpt_si_stime = from->si_stime;
469 to->cpt_si_addr = (intptr_t) from->si_addr;
472 to->cpt_si_band = from->si_band;
473 to->cpt_si_fd = from->si_fd;
476 to->cpt_si_pid = from->si_pid;
477 to->cpt_si_uid = from->si_uid;
478 to->cpt_si_ptr = (intptr_t) from->si_ptr;
485 siginfo_from_compat_siginfo (siginfo_t *to, compat_siginfo_t *from)
487 memset (to, 0, sizeof (*to));
489 to->si_signo = from->si_signo;
490 to->si_errno = from->si_errno;
491 to->si_code = from->si_code;
495 to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
497 else if (to->si_code == SI_USER)
499 to->si_pid = from->cpt_si_pid;
500 to->si_uid = from->cpt_si_uid;
502 else if (to->si_code == SI_TIMER)
504 to->si_timerid = from->cpt_si_timerid;
505 to->si_overrun = from->cpt_si_overrun;
506 to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
510 switch (to->si_signo)
513 to->si_pid = from->cpt_si_pid;
514 to->si_uid = from->cpt_si_uid;
515 to->si_status = from->cpt_si_status;
516 to->si_utime = from->cpt_si_utime;
517 to->si_stime = from->cpt_si_stime;
523 to->si_addr = (void *) (intptr_t) from->cpt_si_addr;
526 to->si_band = from->cpt_si_band;
527 to->si_fd = from->cpt_si_fd;
530 to->si_pid = from->cpt_si_pid;
531 to->si_uid = from->cpt_si_uid;
532 to->si_ptr = (void* ) (intptr_t) from->cpt_si_ptr;
538 #endif /* __x86_64__ */
540 /* Convert a native/host siginfo object, into/from the siginfo in the
541 layout of the inferiors' architecture. Returns true if any
542 conversion was done; false otherwise. If DIRECTION is 1, then copy
543 from INF to NATIVE. If DIRECTION is 0, copy from NATIVE to
547 x86_siginfo_fixup (struct siginfo *native, void *inf, int direction)
550 /* Is the inferior 32-bit? If so, then fixup the siginfo object. */
551 if (register_size (0) == 4)
553 if (sizeof (struct siginfo) != sizeof (compat_siginfo_t))
554 fatal ("unexpected difference in siginfo");
557 compat_siginfo_from_siginfo ((struct compat_siginfo *) inf, native);
559 siginfo_from_compat_siginfo (native, (struct compat_siginfo *) inf);
568 /* Initialize gdbserver for the architecture of the inferior. */
571 x86_arch_setup (void)
574 int pid = pid_of (get_thread_lwp (current_inferior));
575 char *file = linux_child_pid_to_exec_file (pid);
576 int use_64bit = elf_64_file_p (file);
582 /* This can only happen if /proc/<pid>/exe is unreadable,
583 but "that can't happen" if we've gotten this far.
584 Fall through and assume this is a 32-bit program. */
588 init_registers_x86_64_linux ();
590 /* Amd64 doesn't have HAVE_LINUX_USRREGS. */
591 the_low_target.num_regs = -1;
592 the_low_target.regmap = NULL;
593 the_low_target.cannot_fetch_register = NULL;
594 the_low_target.cannot_store_register = NULL;
596 /* Amd64 has 16 xmm regs. */
597 num_xmm_registers = 16;
603 /* Ok we have a 32-bit inferior. */
605 init_registers_i386_linux ();
607 the_low_target.num_regs = I386_NUM_REGS;
608 the_low_target.regmap = i386_regmap;
609 the_low_target.cannot_fetch_register = i386_cannot_fetch_register;
610 the_low_target.cannot_store_register = i386_cannot_store_register;
612 /* I386 has 8 xmm regs. */
613 num_xmm_registers = 8;
616 /* This is initialized assuming an amd64 target.
617 x86_arch_setup will correct it for i386 or amd64 targets. */
619 struct linux_target_ops the_low_target =
637 /* collect_ptrace_register/supply_ptrace_register are not needed in the
638 native i386 case (no registers smaller than an xfer unit), and are not
639 used in the biarch case (HAVE_LINUX_USRREGS is not defined). */
642 /* need to fix up i386 siginfo if host is amd64 */