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