Add support for single register window model on SPARC
[deliverable/binutils-gdb.git] / gdb / sparcobsd-tdep.c
CommitLineData
566626fa
MK
1/* Target-dependent code for OpenBSD/sparc.
2
7b6bb8da 3 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
0fb0cc75 4 Free Software Foundation, Inc.
566626fa
MK
5
6 This file is part of GDB.
7
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
566626fa
MK
11 (at your option) any later version.
12
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.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
566626fa
MK
20
21#include "defs.h"
22#include "floatformat.h"
23#include "frame.h"
24#include "frame-unwind.h"
92c2d36a 25#include "gdbcore.h"
566626fa 26#include "osabi.h"
92c2d36a 27#include "regcache.h"
566626fa
MK
28#include "symtab.h"
29#include "trad-frame.h"
30
31#include "gdb_assert.h"
32
dfe1ff2e 33#include "obsd-tdep.h"
566626fa 34#include "sparc-tdep.h"
92c2d36a
MK
35#include "solib-svr4.h"
36#include "bsd-uthread.h"
566626fa
MK
37
38/* Signal trampolines. */
39
40/* The OpenBSD kernel maps the signal trampoline at some random
41 location in user space, which means that the traditional BSD way of
42 detecting it won't work.
43
44 The signal trampoline will be mapped at an address that is page
37e28b92 45 aligned. We recognize the signal trampoline by looking for the
566626fa
MK
46 sigreturn system call. */
47
48static const int sparc32obsd_page_size = 4096;
49
50static int
51sparc32obsd_pc_in_sigtramp (CORE_ADDR pc, char *name)
52{
53 CORE_ADDR start_pc = (pc & ~(sparc32obsd_page_size - 1));
54 unsigned long insn;
55
56 if (name)
57 return 0;
58
59 /* Check for "restore %g0, SYS_sigreturn, %g1". */
60 insn = sparc_fetch_instruction (start_pc + 0xec);
61 if (insn != 0x83e82067)
62 return 0;
63
64 /* Check for "t ST_SYSCALL". */
65 insn = sparc_fetch_instruction (start_pc + 0xf4);
66 if (insn != 0x91d02000)
67 return 0;
68
69 return 1;
70}
71
72static struct sparc_frame_cache *
7ea566be
MK
73sparc32obsd_sigtramp_frame_cache (struct frame_info *this_frame,
74 void **this_cache)
566626fa
MK
75{
76 struct sparc_frame_cache *cache;
77 CORE_ADDR addr;
78
79 if (*this_cache)
80 return *this_cache;
81
7ea566be 82 cache = sparc_frame_cache (this_frame, this_cache);
566626fa
MK
83 gdb_assert (cache == *this_cache);
84
85 /* If we couldn't find the frame's function, we're probably dealing
86 with an on-stack signal trampoline. */
87 if (cache->pc == 0)
88 {
7ea566be 89 cache->pc = get_frame_pc (this_frame);
566626fa
MK
90 cache->pc &= ~(sparc32obsd_page_size - 1);
91
92 /* Since we couldn't find the frame's function, the cache was
93 initialized under the assumption that we're frameless. */
369c397b 94 sparc_record_save_insn (cache);
7ea566be 95 addr = get_frame_register_unsigned (this_frame, SPARC_FP_REGNUM);
566626fa
MK
96 cache->base = addr;
97 }
98
7ea566be 99 cache->saved_regs = sparc32nbsd_sigcontext_saved_regs (this_frame);
566626fa
MK
100
101 return cache;
102}
103
104static void
7ea566be
MK
105sparc32obsd_sigtramp_frame_this_id (struct frame_info *this_frame,
106 void **this_cache,
107 struct frame_id *this_id)
566626fa
MK
108{
109 struct sparc_frame_cache *cache =
7ea566be 110 sparc32obsd_sigtramp_frame_cache (this_frame, this_cache);
566626fa
MK
111
112 (*this_id) = frame_id_build (cache->base, cache->pc);
113}
114
7ea566be
MK
115static struct value *
116sparc32obsd_sigtramp_frame_prev_register (struct frame_info *this_frame,
117 void **this_cache, int regnum)
566626fa
MK
118{
119 struct sparc_frame_cache *cache =
7ea566be 120 sparc32obsd_sigtramp_frame_cache (this_frame, this_cache);
566626fa 121
7ea566be 122 return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
566626fa
MK
123}
124
7ea566be
MK
125static int
126sparc32obsd_sigtramp_frame_sniffer (const struct frame_unwind *self,
127 struct frame_info *this_frame,
128 void **this_cache)
566626fa 129{
7ea566be 130 CORE_ADDR pc = get_frame_pc (this_frame);
566626fa
MK
131 char *name;
132
133 find_pc_partial_function (pc, &name, NULL, NULL);
134 if (sparc32obsd_pc_in_sigtramp (pc, name))
7ea566be 135 return 1;
566626fa 136
7ea566be 137 return 0;
566626fa 138}
7ea566be
MK
139static const struct frame_unwind sparc32obsd_sigtramp_frame_unwind =
140{
141 SIGTRAMP_FRAME,
8fbca658 142 default_frame_unwind_stop_reason,
7ea566be
MK
143 sparc32obsd_sigtramp_frame_this_id,
144 sparc32obsd_sigtramp_frame_prev_register,
145 NULL,
146 sparc32obsd_sigtramp_frame_sniffer
147};
148
566626fa
MK
149\f
150
92c2d36a
MK
151/* Offset wthin the thread structure where we can find %fp and %i7. */
152#define SPARC32OBSD_UTHREAD_FP_OFFSET 128
153#define SPARC32OBSD_UTHREAD_PC_OFFSET 132
154
155static void
156sparc32obsd_supply_uthread (struct regcache *regcache,
157 int regnum, CORE_ADDR addr)
158{
e17a4113
UW
159 struct gdbarch *gdbarch = get_regcache_arch (regcache);
160 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
92c2d36a
MK
161 CORE_ADDR fp, fp_addr = addr + SPARC32OBSD_UTHREAD_FP_OFFSET;
162 gdb_byte buf[4];
163
164 gdb_assert (regnum >= -1);
165
e17a4113 166 fp = read_memory_unsigned_integer (fp_addr, 4, byte_order);
92c2d36a
MK
167 if (regnum == SPARC_SP_REGNUM || regnum == -1)
168 {
e17a4113 169 store_unsigned_integer (buf, 4, byte_order, fp);
92c2d36a
MK
170 regcache_raw_supply (regcache, SPARC_SP_REGNUM, buf);
171
172 if (regnum == SPARC_SP_REGNUM)
173 return;
174 }
175
176 if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM
177 || regnum == -1)
178 {
179 CORE_ADDR i7, i7_addr = addr + SPARC32OBSD_UTHREAD_PC_OFFSET;
180
e17a4113 181 i7 = read_memory_unsigned_integer (i7_addr, 4, byte_order);
92c2d36a
MK
182 if (regnum == SPARC32_PC_REGNUM || regnum == -1)
183 {
e17a4113 184 store_unsigned_integer (buf, 4, byte_order, i7 + 8);
92c2d36a
MK
185 regcache_raw_supply (regcache, SPARC32_PC_REGNUM, buf);
186 }
187 if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
188 {
e17a4113 189 store_unsigned_integer (buf, 4, byte_order, i7 + 12);
92c2d36a
MK
190 regcache_raw_supply (regcache, SPARC32_NPC_REGNUM, buf);
191 }
192
193 if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
194 return;
195 }
196
197 sparc_supply_rwindow (regcache, fp, regnum);
198}
199
200static void
201sparc32obsd_collect_uthread(const struct regcache *regcache,
202 int regnum, CORE_ADDR addr)
203{
e17a4113
UW
204 struct gdbarch *gdbarch = get_regcache_arch (regcache);
205 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
92c2d36a
MK
206 CORE_ADDR sp;
207 gdb_byte buf[4];
208
209 gdb_assert (regnum >= -1);
210
211 if (regnum == SPARC_SP_REGNUM || regnum == -1)
212 {
213 CORE_ADDR fp_addr = addr + SPARC32OBSD_UTHREAD_FP_OFFSET;
214
215 regcache_raw_collect (regcache, SPARC_SP_REGNUM, buf);
216 write_memory (fp_addr,buf, 4);
217 }
218
219 if (regnum == SPARC32_PC_REGNUM || regnum == -1)
220 {
221 CORE_ADDR i7, i7_addr = addr + SPARC32OBSD_UTHREAD_PC_OFFSET;
222
223 regcache_raw_collect (regcache, SPARC32_PC_REGNUM, buf);
e17a4113
UW
224 i7 = extract_unsigned_integer (buf, 4, byte_order) - 8;
225 write_memory_unsigned_integer (i7_addr, 4, byte_order, i7);
92c2d36a
MK
226
227 if (regnum == SPARC32_PC_REGNUM)
228 return;
229 }
230
231 regcache_raw_collect (regcache, SPARC_SP_REGNUM, buf);
e17a4113 232 sp = extract_unsigned_integer (buf, 4, byte_order);
92c2d36a
MK
233 sparc_collect_rwindow (regcache, sp, regnum);
234}
235\f
236
566626fa
MK
237static void
238sparc32obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
239{
240 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
241
19671c2b
MK
242 /* OpenBSD/sparc is very similar to NetBSD/sparc ELF. */
243 sparc32nbsd_elf_init_abi (info, gdbarch);
566626fa 244
dfe1ff2e
MK
245 set_gdbarch_skip_solib_resolver (gdbarch, obsd_skip_solib_resolver);
246
7ea566be 247 frame_unwind_append_unwinder (gdbarch, &sparc32obsd_sigtramp_frame_unwind);
92c2d36a
MK
248
249 /* OpenBSD provides a user-level threads implementation. */
250 bsd_uthread_set_supply_uthread (gdbarch, sparc32obsd_supply_uthread);
251 bsd_uthread_set_collect_uthread (gdbarch, sparc32obsd_collect_uthread);
566626fa
MK
252}
253
254\f
255/* Provide a prototype to silence -Wmissing-prototypes. */
256void _initialize_sparc32obsd_tdep (void);
257
258void
259_initialize_sparc32obsd_tdep (void)
260{
261 gdbarch_register_osabi (bfd_arch_sparc, 0, GDB_OSABI_OPENBSD_ELF,
262 sparc32obsd_init_abi);
263}
This page took 0.76298 seconds and 4 git commands to generate.