1 /* Target-dependent code for OpenBSD/sparc.
3 Copyright (C) 2004-2013 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 "floatformat.h"
23 #include "frame-unwind.h"
28 #include "trad-frame.h"
30 #include "gdb_assert.h"
32 #include "obsd-tdep.h"
33 #include "sparc-tdep.h"
34 #include "solib-svr4.h"
35 #include "bsd-uthread.h"
37 /* Signal trampolines. */
39 /* The OpenBSD kernel maps the signal trampoline at some random
40 location in user space, which means that the traditional BSD way of
41 detecting it won't work.
43 The signal trampoline will be mapped at an address that is page
44 aligned. We recognize the signal trampoline by looking for the
45 sigreturn system call. */
47 static const int sparc32obsd_page_size = 4096;
50 sparc32obsd_pc_in_sigtramp (CORE_ADDR pc, const char *name)
52 CORE_ADDR start_pc = (pc & ~(sparc32obsd_page_size - 1));
58 /* Check for "restore %g0, SYS_sigreturn, %g1". */
59 insn = sparc_fetch_instruction (start_pc + 0xec);
60 if (insn != 0x83e82067)
63 /* Check for "t ST_SYSCALL". */
64 insn = sparc_fetch_instruction (start_pc + 0xf4);
65 if (insn != 0x91d02000)
71 static struct sparc_frame_cache *
72 sparc32obsd_sigtramp_frame_cache (struct frame_info *this_frame,
75 struct sparc_frame_cache *cache;
81 cache = sparc_frame_cache (this_frame, this_cache);
82 gdb_assert (cache == *this_cache);
84 /* If we couldn't find the frame's function, we're probably dealing
85 with an on-stack signal trampoline. */
88 cache->pc = get_frame_pc (this_frame);
89 cache->pc &= ~(sparc32obsd_page_size - 1);
91 /* Since we couldn't find the frame's function, the cache was
92 initialized under the assumption that we're frameless. */
93 sparc_record_save_insn (cache);
94 addr = get_frame_register_unsigned (this_frame, SPARC_FP_REGNUM);
98 cache->saved_regs = sparc32nbsd_sigcontext_saved_regs (this_frame);
104 sparc32obsd_sigtramp_frame_this_id (struct frame_info *this_frame,
106 struct frame_id *this_id)
108 struct sparc_frame_cache *cache =
109 sparc32obsd_sigtramp_frame_cache (this_frame, this_cache);
111 (*this_id) = frame_id_build (cache->base, cache->pc);
114 static struct value *
115 sparc32obsd_sigtramp_frame_prev_register (struct frame_info *this_frame,
116 void **this_cache, int regnum)
118 struct sparc_frame_cache *cache =
119 sparc32obsd_sigtramp_frame_cache (this_frame, this_cache);
121 return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
125 sparc32obsd_sigtramp_frame_sniffer (const struct frame_unwind *self,
126 struct frame_info *this_frame,
129 CORE_ADDR pc = get_frame_pc (this_frame);
132 find_pc_partial_function (pc, &name, NULL, NULL);
133 if (sparc32obsd_pc_in_sigtramp (pc, name))
138 static const struct frame_unwind sparc32obsd_sigtramp_frame_unwind =
141 default_frame_unwind_stop_reason,
142 sparc32obsd_sigtramp_frame_this_id,
143 sparc32obsd_sigtramp_frame_prev_register,
145 sparc32obsd_sigtramp_frame_sniffer
150 /* Offset wthin the thread structure where we can find %fp and %i7. */
151 #define SPARC32OBSD_UTHREAD_FP_OFFSET 128
152 #define SPARC32OBSD_UTHREAD_PC_OFFSET 132
155 sparc32obsd_supply_uthread (struct regcache *regcache,
156 int regnum, CORE_ADDR addr)
158 struct gdbarch *gdbarch = get_regcache_arch (regcache);
159 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
160 CORE_ADDR fp, fp_addr = addr + SPARC32OBSD_UTHREAD_FP_OFFSET;
163 gdb_assert (regnum >= -1);
165 fp = read_memory_unsigned_integer (fp_addr, 4, byte_order);
166 if (regnum == SPARC_SP_REGNUM || regnum == -1)
168 store_unsigned_integer (buf, 4, byte_order, fp);
169 regcache_raw_supply (regcache, SPARC_SP_REGNUM, buf);
171 if (regnum == SPARC_SP_REGNUM)
175 if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM
178 CORE_ADDR i7, i7_addr = addr + SPARC32OBSD_UTHREAD_PC_OFFSET;
180 i7 = read_memory_unsigned_integer (i7_addr, 4, byte_order);
181 if (regnum == SPARC32_PC_REGNUM || regnum == -1)
183 store_unsigned_integer (buf, 4, byte_order, i7 + 8);
184 regcache_raw_supply (regcache, SPARC32_PC_REGNUM, buf);
186 if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
188 store_unsigned_integer (buf, 4, byte_order, i7 + 12);
189 regcache_raw_supply (regcache, SPARC32_NPC_REGNUM, buf);
192 if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
196 sparc_supply_rwindow (regcache, fp, regnum);
200 sparc32obsd_collect_uthread(const struct regcache *regcache,
201 int regnum, CORE_ADDR addr)
203 struct gdbarch *gdbarch = get_regcache_arch (regcache);
204 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
208 gdb_assert (regnum >= -1);
210 if (regnum == SPARC_SP_REGNUM || regnum == -1)
212 CORE_ADDR fp_addr = addr + SPARC32OBSD_UTHREAD_FP_OFFSET;
214 regcache_raw_collect (regcache, SPARC_SP_REGNUM, buf);
215 write_memory (fp_addr,buf, 4);
218 if (regnum == SPARC32_PC_REGNUM || regnum == -1)
220 CORE_ADDR i7, i7_addr = addr + SPARC32OBSD_UTHREAD_PC_OFFSET;
222 regcache_raw_collect (regcache, SPARC32_PC_REGNUM, buf);
223 i7 = extract_unsigned_integer (buf, 4, byte_order) - 8;
224 write_memory_unsigned_integer (i7_addr, 4, byte_order, i7);
226 if (regnum == SPARC32_PC_REGNUM)
230 regcache_raw_collect (regcache, SPARC_SP_REGNUM, buf);
231 sp = extract_unsigned_integer (buf, 4, byte_order);
232 sparc_collect_rwindow (regcache, sp, regnum);
237 sparc32obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
239 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
241 /* OpenBSD/sparc is very similar to NetBSD/sparc ELF. */
242 sparc32nbsd_elf_init_abi (info, gdbarch);
244 set_gdbarch_skip_solib_resolver (gdbarch, obsd_skip_solib_resolver);
246 frame_unwind_append_unwinder (gdbarch, &sparc32obsd_sigtramp_frame_unwind);
248 /* OpenBSD provides a user-level threads implementation. */
249 bsd_uthread_set_supply_uthread (gdbarch, sparc32obsd_supply_uthread);
250 bsd_uthread_set_collect_uthread (gdbarch, sparc32obsd_collect_uthread);
254 /* Provide a prototype to silence -Wmissing-prototypes. */
255 void _initialize_sparc32obsd_tdep (void);
258 _initialize_sparc32obsd_tdep (void)
260 gdbarch_register_osabi (bfd_arch_sparc, 0, GDB_OSABI_OPENBSD_ELF,
261 sparc32obsd_init_abi);