1 /* Target-dependent code for OpenBSD/amd64.
3 Copyright (C) 2003-2019 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* Local non-gdb includes. */
23 #include "amd64-tdep.h"
24 #include "bsd-uthread.h"
25 #include "common/x86-xstate.h"
26 #include "frame-unwind.h"
29 #include "i387-tdep.h"
31 #include "obsd-tdep.h"
35 #include "solib-svr4.h"
38 #include "trad-frame.h"
40 /* Support for signal handlers. */
42 /* Default page size. */
43 static const int amd64obsd_page_size
= 4096;
45 /* Return whether THIS_FRAME corresponds to an OpenBSD sigtramp
49 amd64obsd_sigtramp_p (struct frame_info
*this_frame
)
51 CORE_ADDR pc
= get_frame_pc (this_frame
);
52 CORE_ADDR start_pc
= (pc
& ~(amd64obsd_page_size
- 1));
53 const gdb_byte osigreturn
[] =
56 0x67, 0x00, 0x00, 0x00, /* movq $SYS_sigreturn, %rax */
57 0xcd, 0x80 /* int $0x80 */
59 const gdb_byte sigreturn
[] =
62 0x67, 0x00, 0x00, 0x00, /* movq $SYS_sigreturn, %rax */
63 0x0f, 0x05 /* syscall */
65 size_t buflen
= (sizeof sigreturn
) + 1;
69 /* If the function has a valid symbol name, it isn't a
71 find_pc_partial_function (pc
, &name
, NULL
, NULL
);
75 /* If the function lives in a valid section (even without a starting
76 point) it isn't a trampoline. */
77 if (find_pc_section (pc
) != NULL
)
80 /* If we can't read the instructions at START_PC, return zero. */
81 buf
= (gdb_byte
*) alloca ((sizeof sigreturn
) + 1);
82 if (!safe_frame_unwind_memory (this_frame
, start_pc
+ 6, buf
, buflen
))
85 /* Check for sigreturn(2). Depending on how the assembler encoded
86 the `movq %rsp, %rdi' instruction, the code starts at offset 6 or
87 7. OpenBSD 5.0 and later use the `syscall' instruction. Older
88 versions use `int $0x80'. Check for both. */
89 if (memcmp (buf
, sigreturn
, sizeof sigreturn
)
90 && memcmp (buf
+ 1, sigreturn
, sizeof sigreturn
)
91 && memcmp (buf
, osigreturn
, sizeof osigreturn
)
92 && memcmp (buf
+ 1, osigreturn
, sizeof osigreturn
))
98 /* Assuming THIS_FRAME is for a BSD sigtramp routine, return the
99 address of the associated sigcontext structure. */
102 amd64obsd_sigcontext_addr (struct frame_info
*this_frame
)
104 CORE_ADDR pc
= get_frame_pc (this_frame
);
105 ULONGEST offset
= (pc
& (amd64obsd_page_size
- 1));
107 /* The %rsp register points at `struct sigcontext' upon entry of a
108 signal trampoline. The relevant part of the trampoline is
113 movq $SYS_sigreturn,%rax
116 (see /usr/src/sys/arch/amd64/amd64/locore.S). The `pushq'
117 instruction clobbers %rsp, but its value is saved in `%rdi'. */
120 return get_frame_register_unsigned (this_frame
, AMD64_RDI_REGNUM
);
122 return get_frame_register_unsigned (this_frame
, AMD64_RSP_REGNUM
);
125 /* OpenBSD 3.5 or later. */
127 /* Mapping between the general-purpose registers in `struct reg'
128 format and GDB's register cache layout. */
130 /* From <machine/reg.h>. */
131 int amd64obsd_r_reg_offset
[] =
148 11 * 8, /* ... %r15 */
150 17 * 8, /* %eflags */
159 /* From <machine/signal.h>. */
160 static int amd64obsd_sc_reg_offset
[] =
177 11 * 8, /* ... %r15 */
179 23 * 8, /* %eflags */
188 /* From /usr/src/lib/libpthread/arch/amd64/uthread_machdep.c. */
189 static int amd64obsd_uthread_reg_offset
[] =
199 12 * 8, /* %r8 ... */
206 5 * 8, /* ... %r15 */
217 /* Offset within the thread structure where we can find the saved
218 stack pointer (%esp). */
219 #define AMD64OBSD_UTHREAD_RSP_OFFSET 400
222 amd64obsd_supply_uthread (struct regcache
*regcache
,
223 int regnum
, CORE_ADDR addr
)
225 struct gdbarch
*gdbarch
= regcache
->arch ();
226 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
227 CORE_ADDR sp_addr
= addr
+ AMD64OBSD_UTHREAD_RSP_OFFSET
;
232 gdb_assert (regnum
>= -1);
234 if (regnum
== -1 || regnum
== AMD64_RSP_REGNUM
)
238 /* Fetch stack pointer from thread structure. */
239 sp
= read_memory_unsigned_integer (sp_addr
, 8, byte_order
);
241 /* Adjust the stack pointer such that it looks as if we just
242 returned from _thread_machdep_switch. */
243 offset
= amd64obsd_uthread_reg_offset
[AMD64_RIP_REGNUM
] + 8;
244 store_unsigned_integer (buf
, 8, byte_order
, sp
+ offset
);
245 regcache
->raw_supply (AMD64_RSP_REGNUM
, buf
);
248 for (i
= 0; i
< ARRAY_SIZE (amd64obsd_uthread_reg_offset
); i
++)
250 if (amd64obsd_uthread_reg_offset
[i
] != -1
251 && (regnum
== -1 || regnum
== i
))
253 /* Fetch stack pointer from thread structure (if we didn't
256 sp
= read_memory_unsigned_integer (sp_addr
, 8, byte_order
);
258 /* Read the saved register from the stack frame. */
259 read_memory (sp
+ amd64obsd_uthread_reg_offset
[i
], buf
, 8);
260 regcache
->raw_supply (i
, buf
);
266 amd64obsd_collect_uthread (const struct regcache
*regcache
,
267 int regnum
, CORE_ADDR addr
)
269 struct gdbarch
*gdbarch
= regcache
->arch ();
270 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
271 CORE_ADDR sp_addr
= addr
+ AMD64OBSD_UTHREAD_RSP_OFFSET
;
276 gdb_assert (regnum
>= -1);
278 if (regnum
== -1 || regnum
== AMD64_RSP_REGNUM
)
282 /* Calculate the stack pointer (frame pointer) that will be
283 stored into the thread structure. */
284 offset
= amd64obsd_uthread_reg_offset
[AMD64_RIP_REGNUM
] + 8;
285 regcache
->raw_collect (AMD64_RSP_REGNUM
, buf
);
286 sp
= extract_unsigned_integer (buf
, 8, byte_order
) - offset
;
288 /* Store the stack pointer. */
289 write_memory_unsigned_integer (sp_addr
, 8, byte_order
, sp
);
291 /* The stack pointer was (potentially) modified. Make sure we
292 build a proper stack frame. */
296 for (i
= 0; i
< ARRAY_SIZE (amd64obsd_uthread_reg_offset
); i
++)
298 if (amd64obsd_uthread_reg_offset
[i
] != -1
299 && (regnum
== -1 || regnum
== i
))
301 /* Fetch stack pointer from thread structure (if we didn't
302 calculate it already). */
304 sp
= read_memory_unsigned_integer (sp_addr
, 8, byte_order
);
306 /* Write the register into the stack frame. */
307 regcache
->raw_collect (i
, buf
);
308 write_memory (sp
+ amd64obsd_uthread_reg_offset
[i
], buf
, 8);
312 /* Kernel debugging support. */
314 /* From <machine/frame.h>. Easy since `struct trapframe' matches
315 `struct sigcontext'. */
316 #define amd64obsd_tf_reg_offset amd64obsd_sc_reg_offset
318 static struct trad_frame_cache
*
319 amd64obsd_trapframe_cache (struct frame_info
*this_frame
, void **this_cache
)
321 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
322 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
323 struct trad_frame_cache
*cache
;
324 CORE_ADDR func
, sp
, addr
;
330 return (struct trad_frame_cache
*) *this_cache
;
332 cache
= trad_frame_cache_zalloc (this_frame
);
335 func
= get_frame_func (this_frame
);
336 sp
= get_frame_register_unsigned (this_frame
, AMD64_RSP_REGNUM
);
338 find_pc_partial_function (func
, &name
, NULL
, NULL
);
339 if (name
&& startswith (name
, "Xintr"))
340 addr
= sp
+ 8; /* It's an interrupt frame. */
344 for (i
= 0; i
< ARRAY_SIZE (amd64obsd_tf_reg_offset
); i
++)
345 if (amd64obsd_tf_reg_offset
[i
] != -1)
346 trad_frame_set_reg_addr (cache
, i
, addr
+ amd64obsd_tf_reg_offset
[i
]);
348 /* Read %cs from trap frame. */
349 addr
+= amd64obsd_tf_reg_offset
[AMD64_CS_REGNUM
];
350 cs
= read_memory_unsigned_integer (addr
, 8, byte_order
);
351 if ((cs
& I386_SEL_RPL
) == I386_SEL_UPL
)
353 /* Trap from user space; terminate backtrace. */
354 trad_frame_set_id (cache
, outer_frame_id
);
358 /* Construct the frame ID using the function start. */
359 trad_frame_set_id (cache
, frame_id_build (sp
+ 16, func
));
366 amd64obsd_trapframe_this_id (struct frame_info
*this_frame
,
367 void **this_cache
, struct frame_id
*this_id
)
369 struct trad_frame_cache
*cache
=
370 amd64obsd_trapframe_cache (this_frame
, this_cache
);
372 trad_frame_get_id (cache
, this_id
);
375 static struct value
*
376 amd64obsd_trapframe_prev_register (struct frame_info
*this_frame
,
377 void **this_cache
, int regnum
)
379 struct trad_frame_cache
*cache
=
380 amd64obsd_trapframe_cache (this_frame
, this_cache
);
382 return trad_frame_get_register (cache
, this_frame
, regnum
);
386 amd64obsd_trapframe_sniffer (const struct frame_unwind
*self
,
387 struct frame_info
*this_frame
,
388 void **this_prologue_cache
)
393 /* Check Current Privilege Level and bail out if we're not executing
395 cs
= get_frame_register_unsigned (this_frame
, AMD64_CS_REGNUM
);
396 if ((cs
& I386_SEL_RPL
) == I386_SEL_UPL
)
399 find_pc_partial_function (get_frame_pc (this_frame
), &name
, NULL
, NULL
);
400 return (name
&& ((strcmp (name
, "calltrap") == 0)
401 || (strcmp (name
, "osyscall1") == 0)
402 || (strcmp (name
, "Xsyscall") == 0)
403 || (startswith (name
, "Xintr"))));
406 static const struct frame_unwind amd64obsd_trapframe_unwind
= {
407 /* FIXME: kettenis/20051219: This really is more like an interrupt
408 frame, but SIGTRAMP_FRAME would print <signal handler called>,
409 which really is not what we want here. */
411 default_frame_unwind_stop_reason
,
412 amd64obsd_trapframe_this_id
,
413 amd64obsd_trapframe_prev_register
,
415 amd64obsd_trapframe_sniffer
420 amd64obsd_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
422 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
424 amd64_init_abi (info
, gdbarch
,
425 amd64_target_description (X86_XSTATE_SSE_MASK
, true));
426 obsd_init_abi (info
, gdbarch
);
428 /* Initialize general-purpose register set details. */
429 tdep
->gregset_reg_offset
= amd64obsd_r_reg_offset
;
430 tdep
->gregset_num_regs
= ARRAY_SIZE (amd64obsd_r_reg_offset
);
431 tdep
->sizeof_gregset
= 24 * 8;
433 tdep
->jb_pc_offset
= 7 * 8;
435 tdep
->sigtramp_p
= amd64obsd_sigtramp_p
;
436 tdep
->sigcontext_addr
= amd64obsd_sigcontext_addr
;
437 tdep
->sc_reg_offset
= amd64obsd_sc_reg_offset
;
438 tdep
->sc_num_regs
= ARRAY_SIZE (amd64obsd_sc_reg_offset
);
440 /* OpenBSD provides a user-level threads implementation. */
441 bsd_uthread_set_supply_uthread (gdbarch
, amd64obsd_supply_uthread
);
442 bsd_uthread_set_collect_uthread (gdbarch
, amd64obsd_collect_uthread
);
444 /* OpenBSD uses SVR4-style shared libraries. */
445 set_solib_svr4_fetch_link_map_offsets
446 (gdbarch
, svr4_lp64_fetch_link_map_offsets
);
448 /* Unwind kernel trap frames correctly. */
449 frame_unwind_prepend_unwinder (gdbarch
, &amd64obsd_trapframe_unwind
);
453 _initialize_amd64obsd_tdep (void)
455 /* The OpenBSD/amd64 native dependent code makes this assumption. */
456 gdb_assert (ARRAY_SIZE (amd64obsd_r_reg_offset
) == AMD64_NUM_GREGS
);
458 gdbarch_register_osabi (bfd_arch_i386
, bfd_mach_x86_64
,
459 GDB_OSABI_OPENBSD
, amd64obsd_init_abi
);