1 /* Target-dependent code for OpenBSD/sparc64.
3 Copyright (C) 2004-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/>. */
21 #include "frame-unwind.h"
28 #include "trad-frame.h"
31 #include "obsd-tdep.h"
32 #include "sparc64-tdep.h"
33 #include "solib-svr4.h"
34 #include "bsd-uthread.h"
36 /* Older OpenBSD versions used the traditional NetBSD core file
37 format, even for ports that use ELF. These core files don't use
38 multiple register sets. Instead, the general-purpose and
39 floating-point registers are lumped together in a single section.
40 Unlike on NetBSD, OpenBSD uses a different layout for its
41 general-purpose registers than the layout used for ptrace(2).
43 Newer OpenBSD versions use ELF core files. Here the register sets
44 match the ptrace(2) layout. */
46 /* From <machine/reg.h>. */
47 const struct sparc_gregmap sparc64obsd_gregmap
=
60 const struct sparc_gregmap sparc64obsd_core_gregmap
=
74 sparc64obsd_supply_gregset (const struct regset
*regset
,
75 struct regcache
*regcache
,
76 int regnum
, const void *gregs
, size_t len
)
78 const void *fpregs
= (char *)gregs
+ 288;
82 sparc64_supply_gregset (&sparc64obsd_gregmap
, regcache
, regnum
, gregs
);
86 sparc64_supply_gregset (&sparc64obsd_core_gregmap
, regcache
, regnum
, gregs
);
87 sparc64_supply_fpregset (&sparc64_bsd_fpregmap
, regcache
, regnum
, fpregs
);
91 sparc64obsd_supply_fpregset (const struct regset
*regset
,
92 struct regcache
*regcache
,
93 int regnum
, const void *fpregs
, size_t len
)
95 sparc64_supply_fpregset (&sparc64_bsd_fpregmap
, regcache
, regnum
, fpregs
);
99 /* Signal trampolines. */
101 /* Since OpenBSD 3.2, the sigtramp routine is mapped at a random page
102 in virtual memory. The randomness makes it somewhat tricky to
103 detect it, but fortunately we can rely on the fact that the start
104 of the sigtramp routine is page-aligned. We recognize the
105 trampoline by looking for the code that invokes the sigreturn
106 system call. The offset where we can find that code varies from
109 By the way, the mapping mentioned above is read-only, so you cannot
110 place a breakpoint in the signal trampoline. */
112 /* Default page size. */
113 static const int sparc64obsd_page_size
= 8192;
115 /* Offset for sigreturn(2). */
116 static const int sparc64obsd_sigreturn_offset
[] = {
117 0xf0, /* OpenBSD 3.8 */
118 0xec, /* OpenBSD 3.6 */
119 0xe8, /* OpenBSD 3.2 */
124 sparc64obsd_pc_in_sigtramp (CORE_ADDR pc
, const char *name
)
126 CORE_ADDR start_pc
= (pc
& ~(sparc64obsd_page_size
- 1));
133 for (offset
= sparc64obsd_sigreturn_offset
; *offset
!= -1; offset
++)
135 /* Check for "restore %g0, SYS_sigreturn, %g1". */
136 insn
= sparc_fetch_instruction (start_pc
+ *offset
);
137 if (insn
!= 0x83e82067)
140 /* Check for "t ST_SYSCALL". */
141 insn
= sparc_fetch_instruction (start_pc
+ *offset
+ 8);
142 if (insn
!= 0x91d02000)
151 static struct sparc_frame_cache
*
152 sparc64obsd_frame_cache (const frame_info_ptr
&this_frame
, void **this_cache
)
154 struct sparc_frame_cache
*cache
;
158 return (struct sparc_frame_cache
*) *this_cache
;
160 cache
= sparc_frame_cache (this_frame
, this_cache
);
161 gdb_assert (cache
== *this_cache
);
163 /* If we couldn't find the frame's function, we're probably dealing
164 with an on-stack signal trampoline. */
167 cache
->pc
= get_frame_pc (this_frame
);
168 cache
->pc
&= ~(sparc64obsd_page_size
- 1);
170 /* Since we couldn't find the frame's function, the cache was
171 initialized under the assumption that we're frameless. */
172 sparc_record_save_insn (cache
);
173 addr
= get_frame_register_unsigned (this_frame
, SPARC_FP_REGNUM
);
179 /* We find the appropriate instance of `struct sigcontext' at a
180 fixed offset in the signal frame. */
181 addr
= cache
->base
+ 128 + 16;
182 cache
->saved_regs
= sparc64nbsd_sigcontext_saved_regs (addr
, this_frame
);
188 sparc64obsd_frame_this_id (const frame_info_ptr
&this_frame
, void **this_cache
,
189 struct frame_id
*this_id
)
191 struct sparc_frame_cache
*cache
=
192 sparc64obsd_frame_cache (this_frame
, this_cache
);
194 (*this_id
) = frame_id_build (cache
->base
, cache
->pc
);
197 static struct value
*
198 sparc64obsd_frame_prev_register (const frame_info_ptr
&this_frame
,
199 void **this_cache
, int regnum
)
201 struct sparc_frame_cache
*cache
=
202 sparc64obsd_frame_cache (this_frame
, this_cache
);
204 return trad_frame_get_prev_register (this_frame
, cache
->saved_regs
, regnum
);
208 sparc64obsd_sigtramp_frame_sniffer (const struct frame_unwind
*self
,
209 const frame_info_ptr
&this_frame
,
212 CORE_ADDR pc
= get_frame_pc (this_frame
);
215 find_pc_partial_function (pc
, &name
, NULL
, NULL
);
216 if (sparc64obsd_pc_in_sigtramp (pc
, name
))
222 static const struct frame_unwind sparc64obsd_frame_unwind
=
224 "sparc64 openbsd sigtramp",
226 default_frame_unwind_stop_reason
,
227 sparc64obsd_frame_this_id
,
228 sparc64obsd_frame_prev_register
,
230 sparc64obsd_sigtramp_frame_sniffer
233 /* Kernel debugging support. */
235 static struct sparc_frame_cache
*
236 sparc64obsd_trapframe_cache (const frame_info_ptr
&this_frame
, void **this_cache
)
238 struct sparc_frame_cache
*cache
;
239 CORE_ADDR sp
, trapframe_addr
;
243 return (struct sparc_frame_cache
*) *this_cache
;
245 cache
= sparc_frame_cache (this_frame
, this_cache
);
246 gdb_assert (cache
== *this_cache
);
248 sp
= get_frame_register_unsigned (this_frame
, SPARC_SP_REGNUM
);
249 trapframe_addr
= sp
+ BIAS
+ 176;
251 cache
->saved_regs
= trad_frame_alloc_saved_regs (this_frame
);
253 cache
->saved_regs
[SPARC64_STATE_REGNUM
].set_addr (trapframe_addr
);
254 cache
->saved_regs
[SPARC64_PC_REGNUM
].set_addr (trapframe_addr
+ 8);
255 cache
->saved_regs
[SPARC64_NPC_REGNUM
].set_addr (trapframe_addr
+ 16);
257 for (regnum
= SPARC_G0_REGNUM
; regnum
<= SPARC_I7_REGNUM
; regnum
++)
258 cache
->saved_regs
[regnum
].set_addr (trapframe_addr
+ 48
259 + (regnum
- SPARC_G0_REGNUM
) * 8);
265 sparc64obsd_trapframe_this_id (const frame_info_ptr
&this_frame
,
266 void **this_cache
, struct frame_id
*this_id
)
268 struct sparc_frame_cache
*cache
=
269 sparc64obsd_trapframe_cache (this_frame
, this_cache
);
271 (*this_id
) = frame_id_build (cache
->base
, cache
->pc
);
274 static struct value
*
275 sparc64obsd_trapframe_prev_register (const frame_info_ptr
&this_frame
,
276 void **this_cache
, int regnum
)
278 struct sparc_frame_cache
*cache
=
279 sparc64obsd_trapframe_cache (this_frame
, this_cache
);
281 return trad_frame_get_prev_register (this_frame
, cache
->saved_regs
, regnum
);
285 sparc64obsd_trapframe_sniffer (const struct frame_unwind
*self
,
286 const frame_info_ptr
&this_frame
,
293 /* Check whether we are in privileged mode, and bail out if we're not. */
294 pstate
= get_frame_register_unsigned (this_frame
, SPARC64_PSTATE_REGNUM
);
295 if ((pstate
& SPARC64_PSTATE_PRIV
) == 0)
298 pc
= get_frame_address_in_block (this_frame
);
299 find_pc_partial_function (pc
, &name
, NULL
, NULL
);
300 if (name
&& strcmp (name
, "Lslowtrap_reenter") == 0)
306 static const struct frame_unwind sparc64obsd_trapframe_unwind
=
308 "sparc64 openbsd trap",
310 default_frame_unwind_stop_reason
,
311 sparc64obsd_trapframe_this_id
,
312 sparc64obsd_trapframe_prev_register
,
314 sparc64obsd_trapframe_sniffer
318 /* Threads support. */
320 /* Offset wthin the thread structure where we can find %fp and %i7. */
321 #define SPARC64OBSD_UTHREAD_FP_OFFSET 232
322 #define SPARC64OBSD_UTHREAD_PC_OFFSET 240
325 sparc64obsd_supply_uthread (struct regcache
*regcache
,
326 int regnum
, CORE_ADDR addr
)
328 struct gdbarch
*gdbarch
= regcache
->arch ();
329 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
330 CORE_ADDR fp
, fp_addr
= addr
+ SPARC64OBSD_UTHREAD_FP_OFFSET
;
333 /* This function calls functions that depend on the global current thread. */
334 gdb_assert (regcache
->ptid () == inferior_ptid
);
336 gdb_assert (regnum
>= -1);
338 fp
= read_memory_unsigned_integer (fp_addr
, 8, byte_order
);
339 if (regnum
== SPARC_SP_REGNUM
|| regnum
== -1)
341 store_unsigned_integer (buf
, 8, byte_order
, fp
);
342 regcache
->raw_supply (SPARC_SP_REGNUM
, buf
);
344 if (regnum
== SPARC_SP_REGNUM
)
348 if (regnum
== SPARC64_PC_REGNUM
|| regnum
== SPARC64_NPC_REGNUM
351 CORE_ADDR i7
, i7_addr
= addr
+ SPARC64OBSD_UTHREAD_PC_OFFSET
;
353 i7
= read_memory_unsigned_integer (i7_addr
, 8, byte_order
);
354 if (regnum
== SPARC64_PC_REGNUM
|| regnum
== -1)
356 store_unsigned_integer (buf
, 8, byte_order
, i7
+ 8);
357 regcache
->raw_supply (SPARC64_PC_REGNUM
, buf
);
359 if (regnum
== SPARC64_NPC_REGNUM
|| regnum
== -1)
361 store_unsigned_integer (buf
, 8, byte_order
, i7
+ 12);
362 regcache
->raw_supply (SPARC64_NPC_REGNUM
, buf
);
365 if (regnum
== SPARC64_PC_REGNUM
|| regnum
== SPARC64_NPC_REGNUM
)
369 sparc_supply_rwindow (regcache
, fp
, regnum
);
373 sparc64obsd_collect_uthread(const struct regcache
*regcache
,
374 int regnum
, CORE_ADDR addr
)
376 struct gdbarch
*gdbarch
= regcache
->arch ();
377 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
381 /* This function calls functions that depend on the global current thread. */
382 gdb_assert (regcache
->ptid () == inferior_ptid
);
384 gdb_assert (regnum
>= -1);
386 if (regnum
== SPARC_SP_REGNUM
|| regnum
== -1)
388 CORE_ADDR fp_addr
= addr
+ SPARC64OBSD_UTHREAD_FP_OFFSET
;
390 regcache
->raw_collect (SPARC_SP_REGNUM
, buf
);
391 write_memory (fp_addr
,buf
, 8);
394 if (regnum
== SPARC64_PC_REGNUM
|| regnum
== -1)
396 CORE_ADDR i7
, i7_addr
= addr
+ SPARC64OBSD_UTHREAD_PC_OFFSET
;
398 regcache
->raw_collect (SPARC64_PC_REGNUM
, buf
);
399 i7
= extract_unsigned_integer (buf
, 8, byte_order
) - 8;
400 write_memory_unsigned_integer (i7_addr
, 8, byte_order
, i7
);
402 if (regnum
== SPARC64_PC_REGNUM
)
406 regcache
->raw_collect (SPARC_SP_REGNUM
, buf
);
407 sp
= extract_unsigned_integer (buf
, 8, byte_order
);
408 sparc_collect_rwindow (regcache
, sp
, regnum
);
412 static const struct regset sparc64obsd_gregset
=
414 NULL
, sparc64obsd_supply_gregset
, NULL
417 static const struct regset sparc64obsd_fpregset
=
419 NULL
, sparc64obsd_supply_fpregset
, NULL
423 sparc64obsd_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
425 sparc_gdbarch_tdep
*tdep
= gdbarch_tdep
<sparc_gdbarch_tdep
> (gdbarch
);
427 tdep
->gregset
= &sparc64obsd_gregset
;
428 tdep
->sizeof_gregset
= 288;
429 tdep
->fpregset
= &sparc64obsd_fpregset
;
430 tdep
->sizeof_fpregset
= 272;
432 /* Make sure we can single-step "new" syscalls. */
433 tdep
->step_trap
= sparcnbsd_step_trap
;
435 frame_unwind_append_unwinder (gdbarch
, &sparc64obsd_frame_unwind
);
436 frame_unwind_append_unwinder (gdbarch
, &sparc64obsd_trapframe_unwind
);
438 sparc64_init_abi (info
, gdbarch
);
439 obsd_init_abi (info
, gdbarch
);
441 /* OpenBSD/sparc64 has SVR4-style shared libraries. */
442 set_solib_svr4_fetch_link_map_offsets
443 (gdbarch
, svr4_lp64_fetch_link_map_offsets
);
444 set_gdbarch_skip_solib_resolver (gdbarch
, obsd_skip_solib_resolver
);
446 /* OpenBSD provides a user-level threads implementation. */
447 bsd_uthread_set_supply_uthread (gdbarch
, sparc64obsd_supply_uthread
);
448 bsd_uthread_set_collect_uthread (gdbarch
, sparc64obsd_collect_uthread
);
451 void _initialize_sparc64obsd_tdep ();
453 _initialize_sparc64obsd_tdep ()
455 gdbarch_register_osabi (bfd_arch_sparc
, bfd_mach_sparc_v9
,
456 GDB_OSABI_OPENBSD
, sparc64obsd_init_abi
);