]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/frv-linux-tdep.c
e6472014df6efdc6c670b75a881357b79c50adcf
[thirdparty/binutils-gdb.git] / gdb / frv-linux-tdep.c
1 /* Target-dependent code for GNU/Linux running on the Fujitsu FR-V,
2 for GDB.
3
4 Copyright (C) 2004-2016 Free Software Foundation, Inc.
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
10 the Free Software Foundation; either version 3 of the License, or
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
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "gdbcore.h"
23 #include "target.h"
24 #include "frame.h"
25 #include "osabi.h"
26 #include "regcache.h"
27 #include "elf-bfd.h"
28 #include "elf/frv.h"
29 #include "frv-tdep.h"
30 #include "trad-frame.h"
31 #include "frame-unwind.h"
32 #include "regset.h"
33 #include "linux-tdep.h"
34
35 /* Define the size (in bytes) of an FR-V instruction. */
36 static const int frv_instr_size = 4;
37
38 enum {
39 NORMAL_SIGTRAMP = 1,
40 RT_SIGTRAMP = 2
41 };
42
43 static int
44 frv_linux_pc_in_sigtramp (struct gdbarch *gdbarch, CORE_ADDR pc,
45 const char *name)
46 {
47 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
48 gdb_byte buf[frv_instr_size];
49 LONGEST instr;
50 int retval = 0;
51
52 if (target_read_memory (pc, buf, sizeof buf) != 0)
53 return 0;
54
55 instr = extract_unsigned_integer (buf, sizeof buf, byte_order);
56
57 if (instr == 0x8efc0077) /* setlos #__NR_sigreturn, gr7 */
58 retval = NORMAL_SIGTRAMP;
59 else if (instr == 0x8efc00ad) /* setlos #__NR_rt_sigreturn, gr7 */
60 retval = RT_SIGTRAMP;
61 else
62 return 0;
63
64 if (target_read_memory (pc + frv_instr_size, buf, sizeof buf) != 0)
65 return 0;
66 instr = extract_unsigned_integer (buf, sizeof buf, byte_order);
67 if (instr != 0xc0700000) /* tira gr0, 0 */
68 return 0;
69
70 /* If we get this far, we'll return a non-zero value, either
71 NORMAL_SIGTRAMP (1) or RT_SIGTRAMP (2). */
72 return retval;
73 }
74
75 /* Given NEXT_FRAME, the "callee" frame of the sigtramp frame that we
76 wish to decode, and REGNO, one of the frv register numbers defined
77 in frv-tdep.h, return the address of the saved register (corresponding
78 to REGNO) in the sigtramp frame. Return -1 if the register is not
79 found in the sigtramp frame. The magic numbers in the code below
80 were computed by examining the following kernel structs:
81
82 From arch/frv/kernel/signal.c:
83
84 struct sigframe
85 {
86 void (*pretcode)(void);
87 int sig;
88 struct sigcontext sc;
89 unsigned long extramask[_NSIG_WORDS-1];
90 uint32_t retcode[2];
91 };
92
93 struct rt_sigframe
94 {
95 void (*pretcode)(void);
96 int sig;
97 struct siginfo *pinfo;
98 void *puc;
99 struct siginfo info;
100 struct ucontext uc;
101 uint32_t retcode[2];
102 };
103
104 From include/asm-frv/ucontext.h:
105
106 struct ucontext {
107 unsigned long uc_flags;
108 struct ucontext *uc_link;
109 stack_t uc_stack;
110 struct sigcontext uc_mcontext;
111 sigset_t uc_sigmask;
112 };
113
114 From include/asm-frv/signal.h:
115
116 typedef struct sigaltstack {
117 void *ss_sp;
118 int ss_flags;
119 size_t ss_size;
120 } stack_t;
121
122 From include/asm-frv/sigcontext.h:
123
124 struct sigcontext {
125 struct user_context sc_context;
126 unsigned long sc_oldmask;
127 } __attribute__((aligned(8)));
128
129 From include/asm-frv/registers.h:
130 struct user_int_regs
131 {
132 unsigned long psr;
133 unsigned long isr;
134 unsigned long ccr;
135 unsigned long cccr;
136 unsigned long lr;
137 unsigned long lcr;
138 unsigned long pc;
139 unsigned long __status;
140 unsigned long syscallno;
141 unsigned long orig_gr8;
142 unsigned long gner[2];
143 unsigned long long iacc[1];
144
145 union {
146 unsigned long tbr;
147 unsigned long gr[64];
148 };
149 };
150
151 struct user_fpmedia_regs
152 {
153 unsigned long fr[64];
154 unsigned long fner[2];
155 unsigned long msr[2];
156 unsigned long acc[8];
157 unsigned char accg[8];
158 unsigned long fsr[1];
159 };
160
161 struct user_context
162 {
163 struct user_int_regs i;
164 struct user_fpmedia_regs f;
165
166 void *extension;
167 } __attribute__((aligned(8))); */
168
169 static LONGEST
170 frv_linux_sigcontext_reg_addr (struct frame_info *this_frame, int regno,
171 CORE_ADDR *sc_addr_cache_ptr)
172 {
173 struct gdbarch *gdbarch = get_frame_arch (this_frame);
174 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
175 CORE_ADDR sc_addr;
176
177 if (sc_addr_cache_ptr && *sc_addr_cache_ptr)
178 {
179 sc_addr = *sc_addr_cache_ptr;
180 }
181 else
182 {
183 CORE_ADDR pc, sp;
184 gdb_byte buf[4];
185 int tramp_type;
186
187 pc = get_frame_pc (this_frame);
188 tramp_type = frv_linux_pc_in_sigtramp (gdbarch, pc, 0);
189
190 get_frame_register (this_frame, sp_regnum, buf);
191 sp = extract_unsigned_integer (buf, sizeof buf, byte_order);
192
193 if (tramp_type == NORMAL_SIGTRAMP)
194 {
195 /* For a normal sigtramp frame, the sigcontext struct starts
196 at SP + 8. */
197 sc_addr = sp + 8;
198 }
199 else if (tramp_type == RT_SIGTRAMP)
200 {
201 /* For a realtime sigtramp frame, SP + 12 contains a pointer
202 to a ucontext struct. The ucontext struct contains a
203 sigcontext struct starting 24 bytes in. (The offset of
204 uc_mcontext within struct ucontext is derived as follows:
205 stack_t is a 12-byte struct and struct sigcontext is
206 8-byte aligned. This gives an offset of 8 + 12 + 4 (for
207 padding) = 24.) */
208 if (target_read_memory (sp + 12, buf, sizeof buf) != 0)
209 {
210 warning (_("Can't read realtime sigtramp frame."));
211 return 0;
212 }
213 sc_addr = extract_unsigned_integer (buf, sizeof buf, byte_order);
214 sc_addr += 24;
215 }
216 else
217 internal_error (__FILE__, __LINE__, _("not a signal trampoline"));
218
219 if (sc_addr_cache_ptr)
220 *sc_addr_cache_ptr = sc_addr;
221 }
222
223 switch (regno)
224 {
225 case psr_regnum :
226 return sc_addr + 0;
227 /* sc_addr + 4 has "isr", the Integer Status Register. */
228 case ccr_regnum :
229 return sc_addr + 8;
230 case cccr_regnum :
231 return sc_addr + 12;
232 case lr_regnum :
233 return sc_addr + 16;
234 case lcr_regnum :
235 return sc_addr + 20;
236 case pc_regnum :
237 return sc_addr + 24;
238 /* sc_addr + 28 is __status, the exception status.
239 sc_addr + 32 is syscallno, the syscall number or -1.
240 sc_addr + 36 is orig_gr8, the original syscall arg #1.
241 sc_addr + 40 is gner[0].
242 sc_addr + 44 is gner[1]. */
243 case iacc0h_regnum :
244 return sc_addr + 48;
245 case iacc0l_regnum :
246 return sc_addr + 52;
247 default :
248 if (first_gpr_regnum <= regno && regno <= last_gpr_regnum)
249 return sc_addr + 56 + 4 * (regno - first_gpr_regnum);
250 else if (first_fpr_regnum <= regno && regno <= last_fpr_regnum)
251 return sc_addr + 312 + 4 * (regno - first_fpr_regnum);
252 else
253 return -1; /* not saved. */
254 }
255 }
256
257 /* Signal trampolines. */
258
259 static struct trad_frame_cache *
260 frv_linux_sigtramp_frame_cache (struct frame_info *this_frame,
261 void **this_cache)
262 {
263 struct gdbarch *gdbarch = get_frame_arch (this_frame);
264 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
265 struct trad_frame_cache *cache;
266 CORE_ADDR addr;
267 gdb_byte buf[4];
268 int regnum;
269 CORE_ADDR sc_addr_cache_val = 0;
270 struct frame_id this_id;
271
272 if (*this_cache)
273 return (struct trad_frame_cache *) *this_cache;
274
275 cache = trad_frame_cache_zalloc (this_frame);
276
277 /* FIXME: cagney/2004-05-01: This is is long standing broken code.
278 The frame ID's code address should be the start-address of the
279 signal trampoline and not the current PC within that
280 trampoline. */
281 get_frame_register (this_frame, sp_regnum, buf);
282 addr = extract_unsigned_integer (buf, sizeof buf, byte_order);
283 this_id = frame_id_build (addr, get_frame_pc (this_frame));
284 trad_frame_set_id (cache, this_id);
285
286 for (regnum = 0; regnum < frv_num_regs; regnum++)
287 {
288 LONGEST reg_addr = frv_linux_sigcontext_reg_addr (this_frame, regnum,
289 &sc_addr_cache_val);
290 if (reg_addr != -1)
291 trad_frame_set_reg_addr (cache, regnum, reg_addr);
292 }
293
294 *this_cache = cache;
295 return cache;
296 }
297
298 static void
299 frv_linux_sigtramp_frame_this_id (struct frame_info *this_frame,
300 void **this_cache,
301 struct frame_id *this_id)
302 {
303 struct trad_frame_cache *cache
304 = frv_linux_sigtramp_frame_cache (this_frame, this_cache);
305 trad_frame_get_id (cache, this_id);
306 }
307
308 static struct value *
309 frv_linux_sigtramp_frame_prev_register (struct frame_info *this_frame,
310 void **this_cache, int regnum)
311 {
312 /* Make sure we've initialized the cache. */
313 struct trad_frame_cache *cache
314 = frv_linux_sigtramp_frame_cache (this_frame, this_cache);
315 return trad_frame_get_register (cache, this_frame, regnum);
316 }
317
318 static int
319 frv_linux_sigtramp_frame_sniffer (const struct frame_unwind *self,
320 struct frame_info *this_frame,
321 void **this_cache)
322 {
323 struct gdbarch *gdbarch = get_frame_arch (this_frame);
324 CORE_ADDR pc = get_frame_pc (this_frame);
325 const char *name;
326
327 find_pc_partial_function (pc, &name, NULL, NULL);
328 if (frv_linux_pc_in_sigtramp (gdbarch, pc, name))
329 return 1;
330
331 return 0;
332 }
333
334 static const struct frame_unwind frv_linux_sigtramp_frame_unwind =
335 {
336 SIGTRAMP_FRAME,
337 default_frame_unwind_stop_reason,
338 frv_linux_sigtramp_frame_this_id,
339 frv_linux_sigtramp_frame_prev_register,
340 NULL,
341 frv_linux_sigtramp_frame_sniffer
342 };
343 \f
344 /* The FRV kernel defines ELF_NGREG as 46. We add 2 in order to include
345 the loadmap addresses in the register set. (See below for more info.) */
346 #define FRV_ELF_NGREG (46 + 2)
347 typedef unsigned char frv_elf_greg_t[4];
348 typedef struct { frv_elf_greg_t reg[FRV_ELF_NGREG]; } frv_elf_gregset_t;
349
350 typedef unsigned char frv_elf_fpreg_t[4];
351 typedef struct
352 {
353 frv_elf_fpreg_t fr[64];
354 frv_elf_fpreg_t fner[2];
355 frv_elf_fpreg_t msr[2];
356 frv_elf_fpreg_t acc[8];
357 unsigned char accg[8];
358 frv_elf_fpreg_t fsr[1];
359 } frv_elf_fpregset_t;
360
361 /* Register maps. */
362
363 static const struct regcache_map_entry frv_linux_gregmap[] =
364 {
365 { 1, psr_regnum, 4 },
366 { 1, REGCACHE_MAP_SKIP, 4 }, /* isr */
367 { 1, ccr_regnum, 4 },
368 { 1, cccr_regnum, 4 },
369 { 1, lr_regnum, 4 },
370 { 1, lcr_regnum, 4 },
371 { 1, pc_regnum, 4 },
372 { 1, REGCACHE_MAP_SKIP, 4 }, /* __status */
373 { 1, REGCACHE_MAP_SKIP, 4 }, /* syscallno */
374 { 1, REGCACHE_MAP_SKIP, 4 }, /* orig_gr8 */
375 { 1, gner0_regnum, 4 },
376 { 1, gner1_regnum, 4 },
377 { 1, REGCACHE_MAP_SKIP, 8 }, /* iacc0 */
378 { 1, tbr_regnum, 4 },
379 { 31, first_gpr_regnum + 1, 4 }, /* gr1 ... gr31 */
380
381 /* Technically, the loadmap addresses are not part of `pr_reg' as
382 found in the elf_prstatus struct. The fields which communicate
383 the loadmap address appear (by design) immediately after
384 `pr_reg' though, and the BFD function elf32_frv_grok_prstatus()
385 has been implemented to include these fields in the register
386 section that it extracts from the core file. So, for our
387 purposes, they may be viewed as registers. */
388
389 { 1, fdpic_loadmap_exec_regnum, 4 },
390 { 1, fdpic_loadmap_interp_regnum, 4 },
391 { 0 }
392 };
393
394 static const struct regcache_map_entry frv_linux_fpregmap[] =
395 {
396 { 64, first_fpr_regnum, 4 }, /* fr0 ... fr63 */
397 { 1, fner0_regnum, 4 },
398 { 1, fner1_regnum, 4 },
399 { 1, msr0_regnum, 4 },
400 { 1, msr1_regnum, 4 },
401 { 8, acc0_regnum, 4 }, /* acc0 ... acc7 */
402 { 1, accg0123_regnum, 4 },
403 { 1, accg4567_regnum, 4 },
404 { 1, fsr0_regnum, 4 },
405 { 0 }
406 };
407
408 /* Unpack an frv_elf_gregset_t into GDB's register cache. */
409
410 static void
411 frv_linux_supply_gregset (const struct regset *regset,
412 struct regcache *regcache,
413 int regnum, const void *gregs, size_t len)
414 {
415 int regi;
416 char zerobuf[MAX_REGISTER_SIZE];
417
418 memset (zerobuf, 0, MAX_REGISTER_SIZE);
419
420 /* gr0 always contains 0. Also, the kernel passes the TBR value in
421 this slot. */
422 regcache_raw_supply (regcache, first_gpr_regnum, zerobuf);
423
424 /* Fill gr32, ..., gr63 with zeros. */
425 for (regi = first_gpr_regnum + 32; regi <= last_gpr_regnum; regi++)
426 regcache_raw_supply (regcache, regi, zerobuf);
427
428 regcache_supply_regset (regset, regcache, regnum, gregs, len);
429 }
430
431 /* FRV Linux kernel register sets. */
432
433 static const struct regset frv_linux_gregset =
434 {
435 frv_linux_gregmap,
436 frv_linux_supply_gregset, regcache_collect_regset
437 };
438
439 static const struct regset frv_linux_fpregset =
440 {
441 frv_linux_fpregmap,
442 regcache_supply_regset, regcache_collect_regset
443 };
444
445 static void
446 frv_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
447 iterate_over_regset_sections_cb *cb,
448 void *cb_data,
449 const struct regcache *regcache)
450 {
451 cb (".reg", sizeof (frv_elf_gregset_t), &frv_linux_gregset,
452 NULL, cb_data);
453 cb (".reg2", sizeof (frv_elf_fpregset_t), &frv_linux_fpregset,
454 NULL, cb_data);
455 }
456
457 \f
458 static void
459 frv_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
460 {
461 linux_init_abi (info, gdbarch);
462
463 /* Set the sigtramp frame sniffer. */
464 frame_unwind_append_unwinder (gdbarch, &frv_linux_sigtramp_frame_unwind);
465
466 set_gdbarch_iterate_over_regset_sections
467 (gdbarch, frv_linux_iterate_over_regset_sections);
468 }
469
470 static enum gdb_osabi
471 frv_linux_elf_osabi_sniffer (bfd *abfd)
472 {
473 int elf_flags;
474
475 elf_flags = elf_elfheader (abfd)->e_flags;
476
477 /* Assume GNU/Linux if using the FDPIC ABI. If/when another OS shows
478 up that uses this ABI, we'll need to start using .note sections
479 or some such. */
480 if (elf_flags & EF_FRV_FDPIC)
481 return GDB_OSABI_LINUX;
482 else
483 return GDB_OSABI_UNKNOWN;
484 }
485
486 /* Provide a prototype to silence -Wmissing-prototypes. */
487 void _initialize_frv_linux_tdep (void);
488
489 void
490 _initialize_frv_linux_tdep (void)
491 {
492 gdbarch_register_osabi (bfd_arch_frv, 0, GDB_OSABI_LINUX,
493 frv_linux_init_abi);
494 gdbarch_register_osabi_sniffer (bfd_arch_frv,
495 bfd_target_elf_flavour,
496 frv_linux_elf_osabi_sniffer);
497 }