1 /* Target-dependent code for OpenBSD/sparc64.
3 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 "frame-unwind.h"
30 #include "trad-frame.h"
32 #include "gdb_assert.h"
34 #include "obsd-tdep.h"
35 #include "sparc64-tdep.h"
36 #include "solib-svr4.h"
37 #include "bsd-uthread.h"
39 /* OpenBSD uses the traditional NetBSD core file format, even for
40 ports that use ELF. The core files don't use multiple register
41 sets. Instead, the general-purpose and floating-point registers
42 are lumped together in a single section. Unlike on NetBSD, OpenBSD
43 uses a different layout for its general-purpose registers than the
44 layout used for ptrace(2). */
46 /* From <machine/reg.h>. */
47 const struct sparc_gregset sparc64obsd_core_gregset =
61 sparc64obsd_supply_gregset (const struct regset *regset,
62 struct regcache *regcache,
63 int regnum, const void *gregs, size_t len)
65 const char *regs = gregs;
67 sparc64_supply_gregset (&sparc64obsd_core_gregset, regcache, regnum, regs);
68 sparc64_supply_fpregset (regcache, regnum, regs + 288);
72 /* Signal trampolines. */
74 /* Since OpenBSD 3.2, the sigtramp routine is mapped at a random page
75 in virtual memory. The randomness makes it somewhat tricky to
76 detect it, but fortunately we can rely on the fact that the start
77 of the sigtramp routine is page-aligned. We recognize the
78 trampoline by looking for the code that invokes the sigreturn
79 system call. The offset where we can find that code varies from
82 By the way, the mapping mentioned above is read-only, so you cannot
83 place a breakpoint in the signal trampoline. */
85 /* Default page size. */
86 static const int sparc64obsd_page_size = 8192;
88 /* Offset for sigreturn(2). */
89 static const int sparc64obsd_sigreturn_offset[] = {
90 0xf0, /* OpenBSD 3.8 */
91 0xec, /* OpenBSD 3.6 */
92 0xe8, /* OpenBSD 3.2 */
97 sparc64obsd_pc_in_sigtramp (CORE_ADDR pc, char *name)
99 CORE_ADDR start_pc = (pc & ~(sparc64obsd_page_size - 1));
106 for (offset = sparc64obsd_sigreturn_offset; *offset != -1; offset++)
108 /* Check for "restore %g0, SYS_sigreturn, %g1". */
109 insn = sparc_fetch_instruction (start_pc + *offset);
110 if (insn != 0x83e82067)
113 /* Check for "t ST_SYSCALL". */
114 insn = sparc_fetch_instruction (start_pc + *offset + 8);
115 if (insn != 0x91d02000)
124 static struct sparc_frame_cache *
125 sparc64obsd_frame_cache (struct frame_info *this_frame, void **this_cache)
127 struct sparc_frame_cache *cache;
133 cache = sparc_frame_cache (this_frame, this_cache);
134 gdb_assert (cache == *this_cache);
136 /* If we couldn't find the frame's function, we're probably dealing
137 with an on-stack signal trampoline. */
140 cache->pc = get_frame_pc (this_frame);
141 cache->pc &= ~(sparc64obsd_page_size - 1);
143 /* Since we couldn't find the frame's function, the cache was
144 initialized under the assumption that we're frameless. */
145 cache->frameless_p = 0;
146 addr = get_frame_register_unsigned (this_frame, SPARC_FP_REGNUM);
152 /* We find the appropriate instance of `struct sigcontext' at a
153 fixed offset in the signal frame. */
154 addr = cache->base + 128 + 16;
155 cache->saved_regs = sparc64nbsd_sigcontext_saved_regs (addr, this_frame);
161 sparc64obsd_frame_this_id (struct frame_info *this_frame, void **this_cache,
162 struct frame_id *this_id)
164 struct sparc_frame_cache *cache =
165 sparc64obsd_frame_cache (this_frame, this_cache);
167 (*this_id) = frame_id_build (cache->base, cache->pc);
170 static struct value *
171 sparc64obsd_frame_prev_register (struct frame_info *this_frame,
172 void **this_cache, int regnum)
174 struct sparc_frame_cache *cache =
175 sparc64obsd_frame_cache (this_frame, this_cache);
177 return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
181 sparc64obsd_sigtramp_frame_sniffer (const struct frame_unwind *self,
182 struct frame_info *this_frame,
185 CORE_ADDR pc = get_frame_pc (this_frame);
188 find_pc_partial_function (pc, &name, NULL, NULL);
189 if (sparc64obsd_pc_in_sigtramp (pc, name))
195 static const struct frame_unwind sparc64obsd_frame_unwind =
198 sparc64obsd_frame_this_id,
199 sparc64obsd_frame_prev_register,
201 sparc64obsd_sigtramp_frame_sniffer
204 /* Kernel debugging support. */
206 static struct sparc_frame_cache *
207 sparc64obsd_trapframe_cache (struct frame_info *this_frame, void **this_cache)
209 struct sparc_frame_cache *cache;
210 CORE_ADDR sp, trapframe_addr;
216 cache = sparc_frame_cache (this_frame, this_cache);
217 gdb_assert (cache == *this_cache);
219 sp = get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
220 trapframe_addr = sp + BIAS + 176;
222 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
224 cache->saved_regs[SPARC64_STATE_REGNUM].addr = trapframe_addr;
225 cache->saved_regs[SPARC64_PC_REGNUM].addr = trapframe_addr + 8;
226 cache->saved_regs[SPARC64_NPC_REGNUM].addr = trapframe_addr + 16;
228 for (regnum = SPARC_G0_REGNUM; regnum <= SPARC_I7_REGNUM; regnum++)
229 cache->saved_regs[regnum].addr =
230 trapframe_addr + 48 + (regnum - SPARC_G0_REGNUM) * 8;
236 sparc64obsd_trapframe_this_id (struct frame_info *this_frame,
237 void **this_cache, struct frame_id *this_id)
239 struct sparc_frame_cache *cache =
240 sparc64obsd_trapframe_cache (this_frame, this_cache);
242 (*this_id) = frame_id_build (cache->base, cache->pc);
245 static struct value *
246 sparc64obsd_trapframe_prev_register (struct frame_info *this_frame,
247 void **this_cache, int regnum)
249 struct sparc_frame_cache *cache =
250 sparc64obsd_trapframe_cache (this_frame, this_cache);
252 return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
256 sparc64obsd_trapframe_sniffer (const struct frame_unwind *self,
257 struct frame_info *this_frame,
264 /* Check whether we are in privileged mode, and bail out if we're not. */
265 pstate = get_frame_register_unsigned (this_frame, SPARC64_PSTATE_REGNUM);
266 if ((pstate & SPARC64_PSTATE_PRIV) == 0)
269 pc = get_frame_address_in_block (this_frame);
270 find_pc_partial_function (pc, &name, NULL, NULL);
271 if (name && strcmp (name, "Lslowtrap_reenter") == 0)
277 static const struct frame_unwind sparc64obsd_trapframe_unwind =
280 sparc64obsd_trapframe_this_id,
281 sparc64obsd_trapframe_prev_register,
283 sparc64obsd_trapframe_sniffer
287 /* Threads support. */
289 /* Offset wthin the thread structure where we can find %fp and %i7. */
290 #define SPARC64OBSD_UTHREAD_FP_OFFSET 232
291 #define SPARC64OBSD_UTHREAD_PC_OFFSET 240
294 sparc64obsd_supply_uthread (struct regcache *regcache,
295 int regnum, CORE_ADDR addr)
297 struct gdbarch *gdbarch = get_regcache_arch (regcache);
298 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
299 CORE_ADDR fp, fp_addr = addr + SPARC64OBSD_UTHREAD_FP_OFFSET;
302 gdb_assert (regnum >= -1);
304 fp = read_memory_unsigned_integer (fp_addr, 8, byte_order);
305 if (regnum == SPARC_SP_REGNUM || regnum == -1)
307 store_unsigned_integer (buf, 8, byte_order, fp);
308 regcache_raw_supply (regcache, SPARC_SP_REGNUM, buf);
310 if (regnum == SPARC_SP_REGNUM)
314 if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM
317 CORE_ADDR i7, i7_addr = addr + SPARC64OBSD_UTHREAD_PC_OFFSET;
319 i7 = read_memory_unsigned_integer (i7_addr, 8, byte_order);
320 if (regnum == SPARC64_PC_REGNUM || regnum == -1)
322 store_unsigned_integer (buf, 8, byte_order, i7 + 8);
323 regcache_raw_supply (regcache, SPARC64_PC_REGNUM, buf);
325 if (regnum == SPARC64_NPC_REGNUM || regnum == -1)
327 store_unsigned_integer (buf, 8, byte_order, i7 + 12);
328 regcache_raw_supply (regcache, SPARC64_NPC_REGNUM, buf);
331 if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM)
335 sparc_supply_rwindow (regcache, fp, regnum);
339 sparc64obsd_collect_uthread(const struct regcache *regcache,
340 int regnum, CORE_ADDR addr)
342 struct gdbarch *gdbarch = get_regcache_arch (regcache);
343 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
347 gdb_assert (regnum >= -1);
349 if (regnum == SPARC_SP_REGNUM || regnum == -1)
351 CORE_ADDR fp_addr = addr + SPARC64OBSD_UTHREAD_FP_OFFSET;
353 regcache_raw_collect (regcache, SPARC_SP_REGNUM, buf);
354 write_memory (fp_addr,buf, 8);
357 if (regnum == SPARC64_PC_REGNUM || regnum == -1)
359 CORE_ADDR i7, i7_addr = addr + SPARC64OBSD_UTHREAD_PC_OFFSET;
361 regcache_raw_collect (regcache, SPARC64_PC_REGNUM, buf);
362 i7 = extract_unsigned_integer (buf, 8, byte_order) - 8;
363 write_memory_unsigned_integer (i7_addr, 8, byte_order, i7);
365 if (regnum == SPARC64_PC_REGNUM)
369 regcache_raw_collect (regcache, SPARC_SP_REGNUM, buf);
370 sp = extract_unsigned_integer (buf, 8, byte_order);
371 sparc_collect_rwindow (regcache, sp, regnum);
376 sparc64obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
378 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
380 tdep->gregset = regset_alloc (gdbarch, sparc64obsd_supply_gregset, NULL);
381 tdep->sizeof_gregset = 832;
383 /* Make sure we can single-step "new" syscalls. */
384 tdep->step_trap = sparcnbsd_step_trap;
386 frame_unwind_append_unwinder (gdbarch, &sparc64obsd_frame_unwind);
387 frame_unwind_append_unwinder (gdbarch, &sparc64obsd_trapframe_unwind);
389 sparc64_init_abi (info, gdbarch);
391 /* OpenBSD/sparc64 has SVR4-style shared libraries. */
392 set_solib_svr4_fetch_link_map_offsets
393 (gdbarch, svr4_lp64_fetch_link_map_offsets);
394 set_gdbarch_skip_solib_resolver (gdbarch, obsd_skip_solib_resolver);
396 /* OpenBSD provides a user-level threads implementation. */
397 bsd_uthread_set_supply_uthread (gdbarch, sparc64obsd_supply_uthread);
398 bsd_uthread_set_collect_uthread (gdbarch, sparc64obsd_collect_uthread);
402 /* Provide a prototype to silence -Wmissing-prototypes. */
403 void _initialize_sparc64obsd_tdep (void);
406 _initialize_sparc64obsd_tdep (void)
408 gdbarch_register_osabi (bfd_arch_sparc, bfd_mach_sparc_v9,
409 GDB_OSABI_OPENBSD_ELF, sparc64obsd_init_abi);