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