1 /* Target-dependent code for GNU/Linux SPARC.
3 Copyright (C) 2003, 2004, 2005, 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/>. */
22 #include "dwarf2-frame.h"
24 #include "frame-unwind.h"
31 #include "solib-svr4.h"
33 #include "trad-frame.h"
34 #include "tramp-frame.h"
35 #include "xml-syscall.h"
37 /* The syscall's XML filename for sparc 32-bit. */
38 #define XML_SYSCALL_FILENAME_SPARC32 "syscalls/sparc-linux.xml"
40 #include "sparc-tdep.h"
42 /* Signal trampoline support. */
44 static void sparc32_linux_sigframe_init (const struct tramp_frame *self,
45 struct frame_info *this_frame,
46 struct trad_frame_cache *this_cache,
49 /* GNU/Linux has two flavors of signals. Normal signal handlers, and
50 "realtime" (RT) signals. The RT signals can provide additional
51 information to the signal handler if the SA_SIGINFO flag is set
52 when establishing a signal handler using `sigaction'. It is not
53 unlikely that future versions of GNU/Linux will support SA_SIGINFO
54 for normal signals too. */
56 /* When the sparc Linux kernel calls a signal handler and the
57 SA_RESTORER flag isn't set, the return address points to a bit of
58 code on the stack. This code checks whether the PC appears to be
59 within this bit of code.
61 The instruction sequence for normal signals is encoded below.
62 Checking for the code sequence should be somewhat reliable, because
63 the effect is to call the system call sigreturn. This is unlikely
64 to occur anywhere other than a signal trampoline. */
66 static const struct tramp_frame sparc32_linux_sigframe =
71 { 0x821020d8, -1 }, /* mov __NR_sugreturn, %g1 */
72 { 0x91d02010, -1 }, /* ta 0x10 */
73 { TRAMP_SENTINEL_INSN, -1 }
75 sparc32_linux_sigframe_init
78 /* The instruction sequence for RT signals is slightly different. The
79 effect is to call the system call rt_sigreturn. */
81 static const struct tramp_frame sparc32_linux_rt_sigframe =
86 { 0x82102065, -1 }, /* mov __NR_rt_sigreturn, %g1 */
87 { 0x91d02010, -1 }, /* ta 0x10 */
88 { TRAMP_SENTINEL_INSN, -1 }
90 sparc32_linux_sigframe_init
94 sparc32_linux_sigframe_init (const struct tramp_frame *self,
95 struct frame_info *this_frame,
96 struct trad_frame_cache *this_cache,
99 CORE_ADDR base, addr, sp_addr;
102 base = get_frame_register_unsigned (this_frame, SPARC_O1_REGNUM);
103 if (self == &sparc32_linux_rt_sigframe)
106 /* Offsets from <bits/sigcontext.h>. */
108 trad_frame_set_reg_addr (this_cache, SPARC32_PSR_REGNUM, base + 0);
109 trad_frame_set_reg_addr (this_cache, SPARC32_PC_REGNUM, base + 4);
110 trad_frame_set_reg_addr (this_cache, SPARC32_NPC_REGNUM, base + 8);
111 trad_frame_set_reg_addr (this_cache, SPARC32_Y_REGNUM, base + 12);
113 /* Since %g0 is always zero, keep the identity encoding. */
115 sp_addr = base + 16 + ((SPARC_SP_REGNUM - SPARC_G0_REGNUM) * 4);
116 for (regnum = SPARC_G1_REGNUM; regnum <= SPARC_O7_REGNUM; regnum++)
118 trad_frame_set_reg_addr (this_cache, regnum, addr);
122 base = get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
123 addr = get_frame_memory_unsigned (this_frame, sp_addr, 4);
125 for (regnum = SPARC_L0_REGNUM; regnum <= SPARC_I7_REGNUM; regnum++)
127 trad_frame_set_reg_addr (this_cache, regnum, addr);
130 trad_frame_set_id (this_cache, frame_id_build (base, func));
133 /* Return the address of a system call's alternative return
137 sparc32_linux_step_trap (struct frame_info *frame, unsigned long insn)
139 if (insn == 0x91d02010)
141 ULONGEST sc_num = get_frame_register_unsigned (frame, SPARC_G1_REGNUM);
143 /* __NR_rt_sigreturn is 101 and __NR_sigreturn is 216 */
144 if (sc_num == 101 || sc_num == 216)
146 struct gdbarch *gdbarch = get_frame_arch (frame);
147 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
149 ULONGEST sp, pc_offset;
151 sp = get_frame_register_unsigned (frame, SPARC_SP_REGNUM);
153 /* The kernel puts the sigreturn registers on the stack,
154 and this is where the signal unwinding state is take from
155 when returning from a signal.
157 For __NR_sigreturn, this register area sits 96 bytes from
158 the base of the stack. The saved PC sits 4 bytes into the
159 sigreturn register save area.
161 For __NR_rt_sigreturn a siginfo_t, which is 128 bytes, sits
162 right before the sigreturn register save area. */
168 return read_memory_unsigned_integer (sp + pc_offset, 4, byte_order);
176 const struct sparc_gregset sparc32_linux_core_gregset =
191 sparc32_linux_supply_core_gregset (const struct regset *regset,
192 struct regcache *regcache,
193 int regnum, const void *gregs, size_t len)
195 sparc32_supply_gregset (&sparc32_linux_core_gregset, regcache, regnum, gregs);
199 sparc32_linux_collect_core_gregset (const struct regset *regset,
200 const struct regcache *regcache,
201 int regnum, void *gregs, size_t len)
203 sparc32_collect_gregset (&sparc32_linux_core_gregset, regcache, regnum, gregs);
207 sparc32_linux_supply_core_fpregset (const struct regset *regset,
208 struct regcache *regcache,
209 int regnum, const void *fpregs, size_t len)
211 sparc32_supply_fpregset (regcache, regnum, fpregs);
215 sparc32_linux_collect_core_fpregset (const struct regset *regset,
216 const struct regcache *regcache,
217 int regnum, void *fpregs, size_t len)
219 sparc32_collect_fpregset (regcache, regnum, fpregs);
222 /* Set the program counter for process PTID to PC. */
224 #define PSR_SYSCALL 0x00004000
227 sparc_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
229 struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
232 regcache_cooked_write_unsigned (regcache, tdep->pc_regnum, pc);
233 regcache_cooked_write_unsigned (regcache, tdep->npc_regnum, pc + 4);
235 /* Clear the "in syscall" bit to prevent the kernel from
236 messing with the PCs we just installed, if we happen to be
237 within an interrupted system call that the kernel wants to
240 Note that after we return from the dummy call, the PSR et al.
241 registers will be automatically restored, and the kernel
242 continues to restart the system call at this point. */
243 regcache_cooked_read_unsigned (regcache, SPARC32_PSR_REGNUM, &psr);
245 regcache_cooked_write_unsigned (regcache, SPARC32_PSR_REGNUM, psr);
249 sparc32_linux_get_syscall_number (struct gdbarch *gdbarch,
252 struct regcache *regcache = get_thread_regcache (ptid);
253 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
254 /* The content of a register. */
259 /* Getting the system call number from the register.
260 When dealing with the sparc architecture, this information
261 is stored at the %g1 register. */
262 regcache_cooked_read (regcache, SPARC_G1_REGNUM, buf);
264 ret = extract_signed_integer (buf, 4, byte_order);
272 sparc32_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
274 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
276 tdep->gregset = regset_alloc (gdbarch, sparc32_linux_supply_core_gregset,
277 sparc32_linux_collect_core_gregset);
278 tdep->sizeof_gregset = 152;
280 tdep->fpregset = regset_alloc (gdbarch, sparc32_linux_supply_core_fpregset,
281 sparc32_linux_collect_core_fpregset);
282 tdep->sizeof_fpregset = 396;
284 tramp_frame_prepend_unwinder (gdbarch, &sparc32_linux_sigframe);
285 tramp_frame_prepend_unwinder (gdbarch, &sparc32_linux_rt_sigframe);
287 /* GNU/Linux has SVR4-style shared libraries... */
288 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
289 set_solib_svr4_fetch_link_map_offsets
290 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
292 /* ...which means that we need some special handling when doing
293 prologue analysis. */
294 tdep->plt_entry_size = 12;
296 /* Enable TLS support. */
297 set_gdbarch_fetch_tls_load_module_address (gdbarch,
298 svr4_fetch_objfile_link_map);
300 /* Make sure we can single-step over signal return system calls. */
301 tdep->step_trap = sparc32_linux_step_trap;
303 /* Hook in the DWARF CFI frame unwinder. */
304 dwarf2_append_unwinders (gdbarch);
306 set_gdbarch_write_pc (gdbarch, sparc_linux_write_pc);
308 /* Functions for 'catch syscall'. */
309 set_xml_syscall_file_name (XML_SYSCALL_FILENAME_SPARC32);
310 set_gdbarch_get_syscall_number (gdbarch,
311 sparc32_linux_get_syscall_number);
314 /* Provide a prototype to silence -Wmissing-prototypes. */
315 extern void _initialize_sparc_linux_tdep (void);
318 _initialize_sparc_linux_tdep (void)
320 gdbarch_register_osabi (bfd_arch_sparc, 0, GDB_OSABI_LINUX,
321 sparc32_linux_init_abi);