Commit | Line | Data |
---|---|---|
386c036b MK |
1 | /* Target-dependent code for GNU/Linux SPARC. |
2 | ||
7b6bb8da | 3 | Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011 |
0fb0cc75 | 4 | Free Software Foundation, Inc. |
386c036b 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 |
386c036b 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/>. */ |
386c036b MK |
20 | |
21 | #include "defs.h" | |
faea95b1 | 22 | #include "dwarf2-frame.h" |
386c036b MK |
23 | #include "frame.h" |
24 | #include "frame-unwind.h" | |
e6bb342a | 25 | #include "gdbtypes.h" |
07c5f590 | 26 | #include "regset.h" |
386c036b MK |
27 | #include "gdbarch.h" |
28 | #include "gdbcore.h" | |
29 | #include "osabi.h" | |
30 | #include "regcache.h" | |
31 | #include "solib-svr4.h" | |
32 | #include "symtab.h" | |
33 | #include "trad-frame.h" | |
70f1dc74 | 34 | #include "tramp-frame.h" |
09de9781 | 35 | #include "xml-syscall.h" |
a5ee0f0c | 36 | #include "linux-tdep.h" |
09de9781 DM |
37 | |
38 | /* The syscall's XML filename for sparc 32-bit. */ | |
39 | #define XML_SYSCALL_FILENAME_SPARC32 "syscalls/sparc-linux.xml" | |
386c036b | 40 | |
386c036b MK |
41 | #include "sparc-tdep.h" |
42 | ||
81f726ab | 43 | /* Signal trampoline support. */ |
386c036b | 44 | |
70f1dc74 | 45 | static void sparc32_linux_sigframe_init (const struct tramp_frame *self, |
5366653e | 46 | struct frame_info *this_frame, |
70f1dc74 MK |
47 | struct trad_frame_cache *this_cache, |
48 | CORE_ADDR func); | |
49 | ||
386c036b MK |
50 | /* GNU/Linux has two flavors of signals. Normal signal handlers, and |
51 | "realtime" (RT) signals. The RT signals can provide additional | |
52 | information to the signal handler if the SA_SIGINFO flag is set | |
53 | when establishing a signal handler using `sigaction'. It is not | |
54 | unlikely that future versions of GNU/Linux will support SA_SIGINFO | |
55 | for normal signals too. */ | |
56 | ||
57 | /* When the sparc Linux kernel calls a signal handler and the | |
58 | SA_RESTORER flag isn't set, the return address points to a bit of | |
70f1dc74 MK |
59 | code on the stack. This code checks whether the PC appears to be |
60 | within this bit of code. | |
386c036b | 61 | |
70f1dc74 | 62 | The instruction sequence for normal signals is encoded below. |
386c036b MK |
63 | Checking for the code sequence should be somewhat reliable, because |
64 | the effect is to call the system call sigreturn. This is unlikely | |
70f1dc74 | 65 | to occur anywhere other than a signal trampoline. */ |
386c036b | 66 | |
70f1dc74 MK |
67 | static const struct tramp_frame sparc32_linux_sigframe = |
68 | { | |
81f726ab DM |
69 | SIGTRAMP_FRAME, |
70 | 4, | |
71 | { | |
70f1dc74 MK |
72 | { 0x821020d8, -1 }, /* mov __NR_sugreturn, %g1 */ |
73 | { 0x91d02010, -1 }, /* ta 0x10 */ | |
81f726ab DM |
74 | { TRAMP_SENTINEL_INSN, -1 } |
75 | }, | |
76 | sparc32_linux_sigframe_init | |
77 | }; | |
386c036b | 78 | |
70f1dc74 MK |
79 | /* The instruction sequence for RT signals is slightly different. The |
80 | effect is to call the system call rt_sigreturn. */ | |
81 | ||
82 | static const struct tramp_frame sparc32_linux_rt_sigframe = | |
83 | { | |
81f726ab DM |
84 | SIGTRAMP_FRAME, |
85 | 4, | |
86 | { | |
70f1dc74 MK |
87 | { 0x82102065, -1 }, /* mov __NR_rt_sigreturn, %g1 */ |
88 | { 0x91d02010, -1 }, /* ta 0x10 */ | |
81f726ab DM |
89 | { TRAMP_SENTINEL_INSN, -1 } |
90 | }, | |
91 | sparc32_linux_sigframe_init | |
92 | }; | |
386c036b | 93 | |
81f726ab DM |
94 | static void |
95 | sparc32_linux_sigframe_init (const struct tramp_frame *self, | |
5366653e | 96 | struct frame_info *this_frame, |
81f726ab DM |
97 | struct trad_frame_cache *this_cache, |
98 | CORE_ADDR func) | |
386c036b | 99 | { |
80f9e3aa | 100 | CORE_ADDR base, addr, sp_addr; |
386c036b MK |
101 | int regnum; |
102 | ||
5366653e | 103 | base = get_frame_register_unsigned (this_frame, SPARC_O1_REGNUM); |
81f726ab DM |
104 | if (self == &sparc32_linux_rt_sigframe) |
105 | base += 128; | |
386c036b | 106 | |
70f1dc74 | 107 | /* Offsets from <bits/sigcontext.h>. */ |
78a0fd57 | 108 | |
70f1dc74 MK |
109 | trad_frame_set_reg_addr (this_cache, SPARC32_PSR_REGNUM, base + 0); |
110 | trad_frame_set_reg_addr (this_cache, SPARC32_PC_REGNUM, base + 4); | |
111 | trad_frame_set_reg_addr (this_cache, SPARC32_NPC_REGNUM, base + 8); | |
112 | trad_frame_set_reg_addr (this_cache, SPARC32_Y_REGNUM, base + 12); | |
386c036b MK |
113 | |
114 | /* Since %g0 is always zero, keep the identity encoding. */ | |
70f1dc74 | 115 | addr = base + 20; |
80f9e3aa | 116 | sp_addr = base + 16 + ((SPARC_SP_REGNUM - SPARC_G0_REGNUM) * 4); |
81f726ab DM |
117 | for (regnum = SPARC_G1_REGNUM; regnum <= SPARC_O7_REGNUM; regnum++) |
118 | { | |
119 | trad_frame_set_reg_addr (this_cache, regnum, addr); | |
120 | addr += 4; | |
121 | } | |
386c036b | 122 | |
5366653e DJ |
123 | base = get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM); |
124 | addr = get_frame_memory_unsigned (this_frame, sp_addr, 4); | |
80f9e3aa | 125 | |
81f726ab DM |
126 | for (regnum = SPARC_L0_REGNUM; regnum <= SPARC_I7_REGNUM; regnum++) |
127 | { | |
128 | trad_frame_set_reg_addr (this_cache, regnum, addr); | |
129 | addr += 4; | |
130 | } | |
131 | trad_frame_set_id (this_cache, frame_id_build (base, func)); | |
386c036b | 132 | } |
386c036b | 133 | \f |
0b4294d3 DM |
134 | /* Return the address of a system call's alternative return |
135 | address. */ | |
136 | ||
137 | static CORE_ADDR | |
0b1b3e42 | 138 | sparc32_linux_step_trap (struct frame_info *frame, unsigned long insn) |
0b4294d3 DM |
139 | { |
140 | if (insn == 0x91d02010) | |
141 | { | |
0b1b3e42 | 142 | ULONGEST sc_num = get_frame_register_unsigned (frame, SPARC_G1_REGNUM); |
0b4294d3 | 143 | |
c378eb4e | 144 | /* __NR_rt_sigreturn is 101 and __NR_sigreturn is 216. */ |
0b4294d3 DM |
145 | if (sc_num == 101 || sc_num == 216) |
146 | { | |
e17a4113 UW |
147 | struct gdbarch *gdbarch = get_frame_arch (frame); |
148 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); | |
149 | ||
0b4294d3 DM |
150 | ULONGEST sp, pc_offset; |
151 | ||
0b1b3e42 | 152 | sp = get_frame_register_unsigned (frame, SPARC_SP_REGNUM); |
0b4294d3 DM |
153 | |
154 | /* The kernel puts the sigreturn registers on the stack, | |
155 | and this is where the signal unwinding state is take from | |
156 | when returning from a signal. | |
157 | ||
158 | For __NR_sigreturn, this register area sits 96 bytes from | |
159 | the base of the stack. The saved PC sits 4 bytes into the | |
160 | sigreturn register save area. | |
161 | ||
162 | For __NR_rt_sigreturn a siginfo_t, which is 128 bytes, sits | |
163 | right before the sigreturn register save area. */ | |
164 | ||
165 | pc_offset = 96 + 4; | |
166 | if (sc_num == 101) | |
167 | pc_offset += 128; | |
168 | ||
e17a4113 | 169 | return read_memory_unsigned_integer (sp + pc_offset, 4, byte_order); |
0b4294d3 DM |
170 | } |
171 | } | |
172 | ||
173 | return 0; | |
174 | } | |
175 | \f | |
386c036b | 176 | |
07c5f590 DM |
177 | const struct sparc_gregset sparc32_linux_core_gregset = |
178 | { | |
179 | 32 * 4, /* %psr */ | |
180 | 33 * 4, /* %pc */ | |
181 | 34 * 4, /* %npc */ | |
182 | 35 * 4, /* %y */ | |
183 | -1, /* %wim */ | |
184 | -1, /* %tbr */ | |
185 | 1 * 4, /* %g1 */ | |
186 | 16 * 4, /* %l0 */ | |
187 | 4, /* y size */ | |
188 | }; | |
189 | \f | |
190 | ||
191 | static void | |
192 | sparc32_linux_supply_core_gregset (const struct regset *regset, | |
193 | struct regcache *regcache, | |
194 | int regnum, const void *gregs, size_t len) | |
195 | { | |
c378eb4e MS |
196 | sparc32_supply_gregset (&sparc32_linux_core_gregset, |
197 | regcache, regnum, gregs); | |
07c5f590 DM |
198 | } |
199 | ||
200 | static void | |
201 | sparc32_linux_collect_core_gregset (const struct regset *regset, | |
202 | const struct regcache *regcache, | |
203 | int regnum, void *gregs, size_t len) | |
204 | { | |
c378eb4e MS |
205 | sparc32_collect_gregset (&sparc32_linux_core_gregset, |
206 | regcache, regnum, gregs); | |
07c5f590 DM |
207 | } |
208 | ||
209 | static void | |
210 | sparc32_linux_supply_core_fpregset (const struct regset *regset, | |
211 | struct regcache *regcache, | |
212 | int regnum, const void *fpregs, size_t len) | |
213 | { | |
214 | sparc32_supply_fpregset (regcache, regnum, fpregs); | |
215 | } | |
216 | ||
217 | static void | |
218 | sparc32_linux_collect_core_fpregset (const struct regset *regset, | |
219 | const struct regcache *regcache, | |
220 | int regnum, void *fpregs, size_t len) | |
221 | { | |
222 | sparc32_collect_fpregset (regcache, regnum, fpregs); | |
223 | } | |
224 | ||
e8467b5a DM |
225 | /* Set the program counter for process PTID to PC. */ |
226 | ||
227 | #define PSR_SYSCALL 0x00004000 | |
228 | ||
229 | static void | |
230 | sparc_linux_write_pc (struct regcache *regcache, CORE_ADDR pc) | |
231 | { | |
232 | struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache)); | |
233 | ULONGEST psr; | |
234 | ||
235 | regcache_cooked_write_unsigned (regcache, tdep->pc_regnum, pc); | |
236 | regcache_cooked_write_unsigned (regcache, tdep->npc_regnum, pc + 4); | |
237 | ||
238 | /* Clear the "in syscall" bit to prevent the kernel from | |
239 | messing with the PCs we just installed, if we happen to be | |
240 | within an interrupted system call that the kernel wants to | |
241 | restart. | |
242 | ||
243 | Note that after we return from the dummy call, the PSR et al. | |
244 | registers will be automatically restored, and the kernel | |
245 | continues to restart the system call at this point. */ | |
246 | regcache_cooked_read_unsigned (regcache, SPARC32_PSR_REGNUM, &psr); | |
247 | psr &= ~PSR_SYSCALL; | |
248 | regcache_cooked_write_unsigned (regcache, SPARC32_PSR_REGNUM, psr); | |
249 | } | |
250 | ||
09de9781 DM |
251 | static LONGEST |
252 | sparc32_linux_get_syscall_number (struct gdbarch *gdbarch, | |
253 | ptid_t ptid) | |
254 | { | |
255 | struct regcache *regcache = get_thread_regcache (ptid); | |
256 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); | |
257 | /* The content of a register. */ | |
258 | gdb_byte buf[4]; | |
259 | /* The result. */ | |
260 | LONGEST ret; | |
261 | ||
262 | /* Getting the system call number from the register. | |
263 | When dealing with the sparc architecture, this information | |
264 | is stored at the %g1 register. */ | |
265 | regcache_cooked_read (regcache, SPARC_G1_REGNUM, buf); | |
266 | ||
267 | ret = extract_signed_integer (buf, 4, byte_order); | |
268 | ||
269 | return ret; | |
270 | } | |
271 | ||
07c5f590 DM |
272 | \f |
273 | ||
386c036b MK |
274 | static void |
275 | sparc32_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) | |
276 | { | |
a33e488c MK |
277 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); |
278 | ||
a5ee0f0c PA |
279 | linux_init_abi (info, gdbarch); |
280 | ||
07c5f590 DM |
281 | tdep->gregset = regset_alloc (gdbarch, sparc32_linux_supply_core_gregset, |
282 | sparc32_linux_collect_core_gregset); | |
283 | tdep->sizeof_gregset = 152; | |
284 | ||
285 | tdep->fpregset = regset_alloc (gdbarch, sparc32_linux_supply_core_fpregset, | |
286 | sparc32_linux_collect_core_fpregset); | |
287 | tdep->sizeof_fpregset = 396; | |
288 | ||
81f726ab DM |
289 | tramp_frame_prepend_unwinder (gdbarch, &sparc32_linux_sigframe); |
290 | tramp_frame_prepend_unwinder (gdbarch, &sparc32_linux_rt_sigframe); | |
291 | ||
a33e488c MK |
292 | /* GNU/Linux has SVR4-style shared libraries... */ |
293 | set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target); | |
294 | set_solib_svr4_fetch_link_map_offsets | |
295 | (gdbarch, svr4_ilp32_fetch_link_map_offsets); | |
386c036b | 296 | |
a33e488c MK |
297 | /* ...which means that we need some special handling when doing |
298 | prologue analysis. */ | |
299 | tdep->plt_entry_size = 12; | |
386c036b | 300 | |
b2756930 KB |
301 | /* Enable TLS support. */ |
302 | set_gdbarch_fetch_tls_load_module_address (gdbarch, | |
303 | svr4_fetch_objfile_link_map); | |
faea95b1 | 304 | |
0b4294d3 DM |
305 | /* Make sure we can single-step over signal return system calls. */ |
306 | tdep->step_trap = sparc32_linux_step_trap; | |
307 | ||
faea95b1 | 308 | /* Hook in the DWARF CFI frame unwinder. */ |
87a7da84 | 309 | dwarf2_append_unwinders (gdbarch); |
e8467b5a DM |
310 | |
311 | set_gdbarch_write_pc (gdbarch, sparc_linux_write_pc); | |
09de9781 DM |
312 | |
313 | /* Functions for 'catch syscall'. */ | |
314 | set_xml_syscall_file_name (XML_SYSCALL_FILENAME_SPARC32); | |
315 | set_gdbarch_get_syscall_number (gdbarch, | |
316 | sparc32_linux_get_syscall_number); | |
386c036b MK |
317 | } |
318 | ||
319 | /* Provide a prototype to silence -Wmissing-prototypes. */ | |
320 | extern void _initialize_sparc_linux_tdep (void); | |
321 | ||
322 | void | |
323 | _initialize_sparc_linux_tdep (void) | |
324 | { | |
325 | gdbarch_register_osabi (bfd_arch_sparc, 0, GDB_OSABI_LINUX, | |
326 | sparc32_linux_init_abi); | |
327 | } |