1 /* Target-dependent code for OpenBSD/amd64.
3 Copyright (C) 2003-2024 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/>. */
20 #include "extract-store-integer.h"
22 #include "frame-unwind.h"
30 #include "trad-frame.h"
32 #include "obsd-tdep.h"
33 #include "amd64-tdep.h"
34 #include "i387-tdep.h"
35 #include "gdbsupport/x86-xstate.h"
36 #include "solib-svr4.h"
37 #include "bsd-uthread.h"
39 /* Support for signal handlers. */
41 /* Default page size. */
42 static const int amd64obsd_page_size
= 4096;
44 /* Return whether THIS_FRAME corresponds to an OpenBSD sigtramp
48 amd64obsd_sigtramp_p (const frame_info_ptr
&this_frame
)
50 CORE_ADDR pc
= get_frame_pc (this_frame
);
51 CORE_ADDR start_pc
= (pc
& ~(amd64obsd_page_size
- 1));
52 const gdb_byte osigreturn
[] =
55 0x67, 0x00, 0x00, 0x00, /* movq $SYS_sigreturn, %rax */
56 0xcd, 0x80 /* int $0x80 */
58 const gdb_byte sigreturn
[] =
61 0x67, 0x00, 0x00, 0x00, /* movq $SYS_sigreturn, %rax */
62 0x0f, 0x05 /* syscall */
64 size_t buflen
= (sizeof sigreturn
) + 1;
68 /* If the function has a valid symbol name, it isn't a
70 find_pc_partial_function (pc
, &name
, NULL
, NULL
);
74 /* If the function lives in a valid section (even without a starting
75 point) it isn't a trampoline. */
76 if (find_pc_section (pc
) != NULL
)
79 /* If we can't read the instructions at START_PC, return zero. */
80 buf
= (gdb_byte
*) alloca ((sizeof sigreturn
) + 1);
81 if (!safe_frame_unwind_memory (this_frame
, start_pc
+ 6, {buf
, buflen
}))
84 /* Check for sigreturn(2). Depending on how the assembler encoded
85 the `movq %rsp, %rdi' instruction, the code starts at offset 6 or
86 7. OpenBSD 5.0 and later use the `syscall' instruction. Older
87 versions use `int $0x80'. Check for both. */
88 if (memcmp (buf
, sigreturn
, sizeof sigreturn
)
89 && memcmp (buf
+ 1, sigreturn
, sizeof sigreturn
)
90 && memcmp (buf
, osigreturn
, sizeof osigreturn
)
91 && memcmp (buf
+ 1, osigreturn
, sizeof osigreturn
))
97 /* Assuming THIS_FRAME is for a BSD sigtramp routine, return the
98 address of the associated sigcontext structure. */
101 amd64obsd_sigcontext_addr (const frame_info_ptr
&this_frame
)
103 CORE_ADDR pc
= get_frame_pc (this_frame
);
104 ULONGEST offset
= (pc
& (amd64obsd_page_size
- 1));
106 /* The %rsp register points at `struct sigcontext' upon entry of a
107 signal trampoline. The relevant part of the trampoline is
112 movq $SYS_sigreturn,%rax
115 (see /usr/src/sys/arch/amd64/amd64/locore.S). The `pushq'
116 instruction clobbers %rsp, but its value is saved in `%rdi'. */
119 return get_frame_register_unsigned (this_frame
, AMD64_RDI_REGNUM
);
121 return get_frame_register_unsigned (this_frame
, AMD64_RSP_REGNUM
);
124 /* OpenBSD 3.5 or later. */
126 /* Mapping between the general-purpose registers in `struct reg'
127 format and GDB's register cache layout. */
129 /* From <machine/reg.h>. */
130 int amd64obsd_r_reg_offset
[] =
147 11 * 8, /* ... %r15 */
149 17 * 8, /* %eflags */
158 /* From <machine/signal.h>. */
159 static int amd64obsd_sc_reg_offset
[] =
176 11 * 8, /* ... %r15 */
178 23 * 8, /* %eflags */
187 /* From /usr/src/lib/libpthread/arch/amd64/uthread_machdep.c. */
188 static int amd64obsd_uthread_reg_offset
[] =
198 12 * 8, /* %r8 ... */
205 5 * 8, /* ... %r15 */
216 /* Offset within the thread structure where we can find the saved
217 stack pointer (%esp). */
218 #define AMD64OBSD_UTHREAD_RSP_OFFSET 400
221 amd64obsd_supply_uthread (struct regcache
*regcache
,
222 int regnum
, CORE_ADDR addr
)
224 struct gdbarch
*gdbarch
= regcache
->arch ();
225 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
226 CORE_ADDR sp_addr
= addr
+ AMD64OBSD_UTHREAD_RSP_OFFSET
;
231 gdb_assert (regnum
>= -1);
233 if (regnum
== -1 || regnum
== AMD64_RSP_REGNUM
)
237 /* Fetch stack pointer from thread structure. */
238 sp
= read_memory_unsigned_integer (sp_addr
, 8, byte_order
);
240 /* Adjust the stack pointer such that it looks as if we just
241 returned from _thread_machdep_switch. */
242 offset
= amd64obsd_uthread_reg_offset
[AMD64_RIP_REGNUM
] + 8;
243 store_unsigned_integer (buf
, 8, byte_order
, sp
+ offset
);
244 regcache
->raw_supply (AMD64_RSP_REGNUM
, buf
);
247 for (i
= 0; i
< ARRAY_SIZE (amd64obsd_uthread_reg_offset
); i
++)
249 if (amd64obsd_uthread_reg_offset
[i
] != -1
250 && (regnum
== -1 || regnum
== i
))
252 /* Fetch stack pointer from thread structure (if we didn't
255 sp
= read_memory_unsigned_integer (sp_addr
, 8, byte_order
);
257 /* Read the saved register from the stack frame. */
258 read_memory (sp
+ amd64obsd_uthread_reg_offset
[i
], buf
, 8);
259 regcache
->raw_supply (i
, buf
);
265 amd64obsd_collect_uthread (const struct regcache
*regcache
,
266 int regnum
, CORE_ADDR addr
)
268 struct gdbarch
*gdbarch
= regcache
->arch ();
269 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
270 CORE_ADDR sp_addr
= addr
+ AMD64OBSD_UTHREAD_RSP_OFFSET
;
275 gdb_assert (regnum
>= -1);
277 if (regnum
== -1 || regnum
== AMD64_RSP_REGNUM
)
281 /* Calculate the stack pointer (frame pointer) that will be
282 stored into the thread structure. */
283 offset
= amd64obsd_uthread_reg_offset
[AMD64_RIP_REGNUM
] + 8;
284 regcache
->raw_collect (AMD64_RSP_REGNUM
, buf
);
285 sp
= extract_unsigned_integer (buf
, 8, byte_order
) - offset
;
287 /* Store the stack pointer. */
288 write_memory_unsigned_integer (sp_addr
, 8, byte_order
, sp
);
290 /* The stack pointer was (potentially) modified. Make sure we
291 build a proper stack frame. */
295 for (i
= 0; i
< ARRAY_SIZE (amd64obsd_uthread_reg_offset
); i
++)
297 if (amd64obsd_uthread_reg_offset
[i
] != -1
298 && (regnum
== -1 || regnum
== i
))
300 /* Fetch stack pointer from thread structure (if we didn't
301 calculate it already). */
303 sp
= read_memory_unsigned_integer (sp_addr
, 8, byte_order
);
305 /* Write the register into the stack frame. */
306 regcache
->raw_collect (i
, buf
);
307 write_memory (sp
+ amd64obsd_uthread_reg_offset
[i
], buf
, 8);
311 /* Kernel debugging support. */
313 /* From <machine/frame.h>. Easy since `struct trapframe' matches
314 `struct sigcontext'. */
315 #define amd64obsd_tf_reg_offset amd64obsd_sc_reg_offset
317 static struct trad_frame_cache
*
318 amd64obsd_trapframe_cache (const frame_info_ptr
&this_frame
, void **this_cache
)
320 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
321 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
322 struct trad_frame_cache
*cache
;
323 CORE_ADDR func
, sp
, addr
;
329 return (struct trad_frame_cache
*) *this_cache
;
331 cache
= trad_frame_cache_zalloc (this_frame
);
334 func
= get_frame_func (this_frame
);
335 sp
= get_frame_register_unsigned (this_frame
, AMD64_RSP_REGNUM
);
337 find_pc_partial_function (func
, &name
, NULL
, NULL
);
338 if (name
&& startswith (name
, "Xintr"))
339 addr
= sp
+ 8; /* It's an interrupt frame. */
343 for (i
= 0; i
< ARRAY_SIZE (amd64obsd_tf_reg_offset
); i
++)
344 if (amd64obsd_tf_reg_offset
[i
] != -1)
345 trad_frame_set_reg_addr (cache
, i
, addr
+ amd64obsd_tf_reg_offset
[i
]);
347 /* Read %cs from trap frame. */
348 addr
+= amd64obsd_tf_reg_offset
[AMD64_CS_REGNUM
];
349 cs
= read_memory_unsigned_integer (addr
, 8, byte_order
);
350 if ((cs
& I386_SEL_RPL
) == I386_SEL_UPL
)
352 /* Trap from user space; terminate backtrace. */
353 trad_frame_set_id (cache
, outer_frame_id
);
357 /* Construct the frame ID using the function start. */
358 trad_frame_set_id (cache
, frame_id_build (sp
+ 16, func
));
365 amd64obsd_trapframe_this_id (const frame_info_ptr
&this_frame
,
366 void **this_cache
, struct frame_id
*this_id
)
368 struct trad_frame_cache
*cache
=
369 amd64obsd_trapframe_cache (this_frame
, this_cache
);
371 trad_frame_get_id (cache
, this_id
);
374 static struct value
*
375 amd64obsd_trapframe_prev_register (const frame_info_ptr
&this_frame
,
376 void **this_cache
, int regnum
)
378 struct trad_frame_cache
*cache
=
379 amd64obsd_trapframe_cache (this_frame
, this_cache
);
381 return trad_frame_get_register (cache
, this_frame
, regnum
);
385 amd64obsd_trapframe_sniffer (const struct frame_unwind
*self
,
386 const frame_info_ptr
&this_frame
,
387 void **this_prologue_cache
)
392 /* Check Current Privilege Level and bail out if we're not executing
394 cs
= get_frame_register_unsigned (this_frame
, AMD64_CS_REGNUM
);
395 if ((cs
& I386_SEL_RPL
) == I386_SEL_UPL
)
398 find_pc_partial_function (get_frame_pc (this_frame
), &name
, NULL
, NULL
);
399 return (name
&& ((strcmp (name
, "calltrap") == 0)
400 || (strcmp (name
, "osyscall1") == 0)
401 || (strcmp (name
, "Xsyscall") == 0)
402 || (startswith (name
, "Xintr"))));
405 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. */
410 "amd64 openbsd trap",
412 default_frame_unwind_stop_reason
,
413 amd64obsd_trapframe_this_id
,
414 amd64obsd_trapframe_prev_register
,
416 amd64obsd_trapframe_sniffer
421 amd64obsd_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
423 i386_gdbarch_tdep
*tdep
= gdbarch_tdep
<i386_gdbarch_tdep
> (gdbarch
);
425 amd64_init_abi (info
, gdbarch
,
426 amd64_target_description (X86_XSTATE_SSE_MASK
, true));
427 obsd_init_abi (info
, gdbarch
);
429 /* Initialize general-purpose register set details. */
430 tdep
->gregset_reg_offset
= amd64obsd_r_reg_offset
;
431 tdep
->gregset_num_regs
= ARRAY_SIZE (amd64obsd_r_reg_offset
);
432 tdep
->sizeof_gregset
= 24 * 8;
434 tdep
->jb_pc_offset
= 7 * 8;
436 tdep
->sigtramp_p
= amd64obsd_sigtramp_p
;
437 tdep
->sigcontext_addr
= amd64obsd_sigcontext_addr
;
438 tdep
->sc_reg_offset
= amd64obsd_sc_reg_offset
;
439 tdep
->sc_num_regs
= ARRAY_SIZE (amd64obsd_sc_reg_offset
);
441 /* OpenBSD provides a user-level threads implementation. */
442 bsd_uthread_set_supply_uthread (gdbarch
, amd64obsd_supply_uthread
);
443 bsd_uthread_set_collect_uthread (gdbarch
, amd64obsd_collect_uthread
);
445 /* OpenBSD uses SVR4-style shared libraries. */
446 set_solib_svr4_fetch_link_map_offsets
447 (gdbarch
, svr4_lp64_fetch_link_map_offsets
);
449 /* Unwind kernel trap frames correctly. */
450 frame_unwind_prepend_unwinder (gdbarch
, &amd64obsd_trapframe_unwind
);
453 void _initialize_amd64obsd_tdep ();
455 _initialize_amd64obsd_tdep ()
457 /* The OpenBSD/amd64 native dependent code makes this assumption. */
458 gdb_assert (ARRAY_SIZE (amd64obsd_r_reg_offset
) == AMD64_NUM_GREGS
);
460 gdbarch_register_osabi (bfd_arch_i386
, bfd_mach_x86_64
,
461 GDB_OSABI_OPENBSD
, amd64obsd_init_abi
);