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/>. */
24 #include "linux-low.h"
27 #include "gdb_proc_service.h"
29 /* NOTE: gdb_proc_service.h may include linux/elf.h.
30 We need Elf32_Phdr. If we don't get linux/elf.h we could include
31 elf.h like linux-ppc-low.c does. */
33 /* Defined in auto-generated file reg-i386-linux.c. */
34 void init_registers_i386_linux (void);
35 /* Defined in auto-generated file reg-x86-64-linux.c. */
36 void init_registers_x86_64_linux (void);
39 #include <sys/procfs.h>
40 #include <sys/ptrace.h>
42 #ifndef PTRACE_GET_THREAD_AREA
43 #define PTRACE_GET_THREAD_AREA 25
46 /* This definition comes from prctl.h, but some kernels may not have it. */
47 #ifndef PTRACE_ARCH_PRCTL
48 #define PTRACE_ARCH_PRCTL 30
51 /* The following definitions come from prctl.h, but may be absent
52 for certain configurations. */
54 #define ARCH_SET_GS 0x1001
55 #define ARCH_SET_FS 0x1002
56 #define ARCH_GET_FS 0x1003
57 #define ARCH_GET_GS 0x1004
62 /* Mapping between the general-purpose registers in `struct user'
63 format and GDB's register array layout.
64 Note that the transfer layout uses 64-bit regs. */
65 static /*const*/ int i386_regmap
[] =
67 RAX
* 8, RCX
* 8, RDX
* 8, RBX
* 8,
68 RSP
* 8, RBP
* 8, RSI
* 8, RDI
* 8,
69 RIP
* 8, EFLAGS
* 8, CS
* 8, SS
* 8,
70 DS
* 8, ES
* 8, FS
* 8, GS
* 8
73 #define I386_NUM_REGS (sizeof (i386_regmap) / sizeof (i386_regmap[0]))
75 /* So code below doesn't have to care, i386 or amd64. */
76 #define ORIG_EAX ORIG_RAX
78 static const int x86_64_regmap
[] =
80 RAX
* 8, RBX
* 8, RCX
* 8, RDX
* 8,
81 RSI
* 8, RDI
* 8, RBP
* 8, RSP
* 8,
82 R8
* 8, R9
* 8, R10
* 8, R11
* 8,
83 R12
* 8, R13
* 8, R14
* 8, R15
* 8,
84 RIP
* 8, EFLAGS
* 8, CS
* 8, SS
* 8,
85 DS
* 8, ES
* 8, FS
* 8, GS
* 8,
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,
89 -1, -1, -1, -1, -1, -1, -1, -1, -1,
93 #define X86_64_NUM_REGS (sizeof (x86_64_regmap) / sizeof (x86_64_regmap[0]))
95 #else /* ! __x86_64__ */
97 /* Mapping between the general-purpose registers in `struct user'
98 format and GDB's register array layout. */
99 static /*const*/ int i386_regmap
[] =
101 EAX
* 4, ECX
* 4, EDX
* 4, EBX
* 4,
102 UESP
* 4, EBP
* 4, ESI
* 4, EDI
* 4,
103 EIP
* 4, EFL
* 4, CS
* 4, SS
* 4,
104 DS
* 4, ES
* 4, FS
* 4, GS
* 4
107 #define I386_NUM_REGS (sizeof (i386_regmap) / sizeof (i386_regmap[0]))
111 /* Called by libthread_db. */
114 ps_get_thread_area (const struct ps_prochandle
*ph
,
115 lwpid_t lwpid
, int idx
, void **base
)
118 int use_64bit
= register_size (0) == 8;
125 if (ptrace (PTRACE_ARCH_PRCTL
, lwpid
, base
, ARCH_GET_FS
) == 0)
129 if (ptrace (PTRACE_ARCH_PRCTL
, lwpid
, base
, ARCH_GET_GS
) == 0)
140 unsigned int desc
[4];
142 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
,
143 (void *) (intptr_t) idx
, (unsigned long) &desc
) < 0)
146 *(int *)base
= desc
[1];
152 i386_cannot_store_register (int regno
)
154 return regno
>= I386_NUM_REGS
;
158 i386_cannot_fetch_register (int regno
)
160 return regno
>= I386_NUM_REGS
;
164 x86_fill_gregset (void *buf
)
169 if (register_size (0) == 8)
171 for (i
= 0; i
< X86_64_NUM_REGS
; i
++)
172 if (x86_64_regmap
[i
] != -1)
173 collect_register (i
, ((char *) buf
) + x86_64_regmap
[i
]);
178 for (i
= 0; i
< I386_NUM_REGS
; i
++)
179 collect_register (i
, ((char *) buf
) + i386_regmap
[i
]);
181 collect_register_by_name ("orig_eax", ((char *) buf
) + ORIG_EAX
* 4);
185 x86_store_gregset (const void *buf
)
190 if (register_size (0) == 8)
192 for (i
= 0; i
< X86_64_NUM_REGS
; i
++)
193 if (x86_64_regmap
[i
] != -1)
194 supply_register (i
, ((char *) buf
) + x86_64_regmap
[i
]);
199 for (i
= 0; i
< I386_NUM_REGS
; i
++)
200 supply_register (i
, ((char *) buf
) + i386_regmap
[i
]);
202 supply_register_by_name ("orig_eax", ((char *) buf
) + ORIG_EAX
* 4);
206 x86_fill_fpregset (void *buf
)
209 i387_cache_to_fxsave (buf
);
211 i387_cache_to_fsave (buf
);
216 x86_store_fpregset (const void *buf
)
219 i387_fxsave_to_cache (buf
);
221 i387_fsave_to_cache (buf
);
228 x86_fill_fpxregset (void *buf
)
230 i387_cache_to_fxsave (buf
);
234 x86_store_fpxregset (const void *buf
)
236 i387_fxsave_to_cache (buf
);
241 /* ??? The non-biarch i386 case stores all the i387 regs twice.
242 Once in i387_.*fsave.* and once in i387_.*fxsave.*.
243 This is, presumably, to handle the case where PTRACE_[GS]ETFPXREGS
244 doesn't work. IWBN to avoid the duplication in the case where it
245 does work. Maybe the arch_setup routine could check whether it works
246 and update target_regsets accordingly, maybe by moving target_regsets
247 to linux_target_ops and set the right one there, rather than having to
248 modify the target_regsets global. */
250 struct regset_info target_regsets
[] =
252 #ifdef HAVE_PTRACE_GETREGS
253 { PTRACE_GETREGS
, PTRACE_SETREGS
, sizeof (elf_gregset_t
),
255 x86_fill_gregset
, x86_store_gregset
},
257 # ifdef HAVE_PTRACE_GETFPXREGS
258 { PTRACE_GETFPXREGS
, PTRACE_SETFPXREGS
, sizeof (elf_fpxregset_t
),
260 x86_fill_fpxregset
, x86_store_fpxregset
},
263 { PTRACE_GETFPREGS
, PTRACE_SETFPREGS
, sizeof (elf_fpregset_t
),
265 x86_fill_fpregset
, x86_store_fpregset
},
266 #endif /* HAVE_PTRACE_GETREGS */
267 { 0, 0, -1, -1, NULL
, NULL
}
273 int use_64bit
= register_size (0) == 8;
278 collect_register_by_name ("rip", &pc
);
279 return (CORE_ADDR
) pc
;
284 collect_register_by_name ("eip", &pc
);
285 return (CORE_ADDR
) pc
;
290 x86_set_pc (CORE_ADDR pc
)
292 int use_64bit
= register_size (0) == 8;
296 unsigned long newpc
= pc
;
297 supply_register_by_name ("rip", &newpc
);
301 unsigned int newpc
= pc
;
302 supply_register_by_name ("eip", &newpc
);
306 static const unsigned char x86_breakpoint
[] = { 0xCC };
307 #define x86_breakpoint_len 1
310 x86_breakpoint_at (CORE_ADDR pc
)
314 read_inferior_memory (pc
, &c
, 1);
321 /* When GDBSERVER is built as a 64-bit application on linux, the
322 PTRACE_GETSIGINFO data is always presented in 64-bit layout. Since
323 debugging a 32-bit inferior with a 64-bit GDBSERVER should look the same
324 as debugging it with a 32-bit GDBSERVER, we do the 32-bit <-> 64-bit
325 conversion in-place ourselves. */
327 /* These types below (compat_*) define a siginfo type that is layout
328 compatible with the siginfo type exported by the 32-bit userspace
333 typedef int compat_int_t
;
334 typedef unsigned int compat_uptr_t
;
336 typedef int compat_time_t
;
337 typedef int compat_timer_t
;
338 typedef int compat_clock_t
;
340 struct compat_timeval
342 compat_time_t tv_sec
;
346 typedef union compat_sigval
348 compat_int_t sival_int
;
349 compat_uptr_t sival_ptr
;
352 typedef struct compat_siginfo
360 int _pad
[((128 / sizeof (int)) - 3)];
369 /* POSIX.1b timers */
374 compat_sigval_t _sigval
;
377 /* POSIX.1b signals */
382 compat_sigval_t _sigval
;
391 compat_clock_t _utime
;
392 compat_clock_t _stime
;
395 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
410 #define cpt_si_pid _sifields._kill._pid
411 #define cpt_si_uid _sifields._kill._uid
412 #define cpt_si_timerid _sifields._timer._tid
413 #define cpt_si_overrun _sifields._timer._overrun
414 #define cpt_si_status _sifields._sigchld._status
415 #define cpt_si_utime _sifields._sigchld._utime
416 #define cpt_si_stime _sifields._sigchld._stime
417 #define cpt_si_ptr _sifields._rt._sigval.sival_ptr
418 #define cpt_si_addr _sifields._sigfault._addr
419 #define cpt_si_band _sifields._sigpoll._band
420 #define cpt_si_fd _sifields._sigpoll._fd
422 /* glibc at least up to 2.3.2 doesn't have si_timerid, si_overrun.
423 In their place is si_timer1,si_timer2. */
425 #define si_timerid si_timer1
428 #define si_overrun si_timer2
432 compat_siginfo_from_siginfo (compat_siginfo_t
*to
, siginfo_t
*from
)
434 memset (to
, 0, sizeof (*to
));
436 to
->si_signo
= from
->si_signo
;
437 to
->si_errno
= from
->si_errno
;
438 to
->si_code
= from
->si_code
;
442 to
->cpt_si_ptr
= (intptr_t) from
->si_ptr
;
444 else if (to
->si_code
== SI_USER
)
446 to
->cpt_si_pid
= from
->si_pid
;
447 to
->cpt_si_uid
= from
->si_uid
;
449 else if (to
->si_code
== SI_TIMER
)
451 to
->cpt_si_timerid
= from
->si_timerid
;
452 to
->cpt_si_overrun
= from
->si_overrun
;
453 to
->cpt_si_ptr
= (intptr_t) from
->si_ptr
;
457 switch (to
->si_signo
)
460 to
->cpt_si_pid
= from
->si_pid
;
461 to
->cpt_si_uid
= from
->si_uid
;
462 to
->cpt_si_status
= from
->si_status
;
463 to
->cpt_si_utime
= from
->si_utime
;
464 to
->cpt_si_stime
= from
->si_stime
;
470 to
->cpt_si_addr
= (intptr_t) from
->si_addr
;
473 to
->cpt_si_band
= from
->si_band
;
474 to
->cpt_si_fd
= from
->si_fd
;
477 to
->cpt_si_pid
= from
->si_pid
;
478 to
->cpt_si_uid
= from
->si_uid
;
479 to
->cpt_si_ptr
= (intptr_t) from
->si_ptr
;
486 siginfo_from_compat_siginfo (siginfo_t
*to
, compat_siginfo_t
*from
)
488 memset (to
, 0, sizeof (*to
));
490 to
->si_signo
= from
->si_signo
;
491 to
->si_errno
= from
->si_errno
;
492 to
->si_code
= from
->si_code
;
496 to
->si_ptr
= (void *) (intptr_t) from
->cpt_si_ptr
;
498 else if (to
->si_code
== SI_USER
)
500 to
->si_pid
= from
->cpt_si_pid
;
501 to
->si_uid
= from
->cpt_si_uid
;
503 else if (to
->si_code
== SI_TIMER
)
505 to
->si_timerid
= from
->cpt_si_timerid
;
506 to
->si_overrun
= from
->cpt_si_overrun
;
507 to
->si_ptr
= (void *) (intptr_t) from
->cpt_si_ptr
;
511 switch (to
->si_signo
)
514 to
->si_pid
= from
->cpt_si_pid
;
515 to
->si_uid
= from
->cpt_si_uid
;
516 to
->si_status
= from
->cpt_si_status
;
517 to
->si_utime
= from
->cpt_si_utime
;
518 to
->si_stime
= from
->cpt_si_stime
;
524 to
->si_addr
= (void *) (intptr_t) from
->cpt_si_addr
;
527 to
->si_band
= from
->cpt_si_band
;
528 to
->si_fd
= from
->cpt_si_fd
;
531 to
->si_pid
= from
->cpt_si_pid
;
532 to
->si_uid
= from
->cpt_si_uid
;
533 to
->si_ptr
= (void* ) (intptr_t) from
->cpt_si_ptr
;
539 #endif /* __x86_64__ */
541 /* Convert a native/host siginfo object, into/from the siginfo in the
542 layout of the inferiors' architecture. Returns true if any
543 conversion was done; false otherwise. If DIRECTION is 1, then copy
544 from INF to NATIVE. If DIRECTION is 0, copy from NATIVE to
548 x86_siginfo_fixup (struct siginfo
*native
, void *inf
, int direction
)
551 /* Is the inferior 32-bit? If so, then fixup the siginfo object. */
552 if (register_size (0) == 4)
554 assert (sizeof (struct siginfo
) == sizeof (compat_siginfo_t
));
557 compat_siginfo_from_siginfo ((struct compat_siginfo
*) inf
, native
);
559 siginfo_from_compat_siginfo (native
, (struct compat_siginfo
*) inf
);
568 /* Return non-zero if the target is 64-bit. */
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 */