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