1 /* Target-dependent code for GNU/Linux running on the Fujitsu FR-V,
4 Copyright (C) 2004, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
7 This file is part of GDB.
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
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
31 #include "trad-frame.h"
32 #include "frame-unwind.h"
34 #include "gdb_string.h"
35 #include "linux-tdep.h"
37 /* Define the size (in bytes) of an FR-V instruction. */
38 static const int frv_instr_size
= 4;
46 frv_linux_pc_in_sigtramp (struct gdbarch
*gdbarch
, CORE_ADDR pc
, char *name
)
48 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
49 char buf
[frv_instr_size
];
53 if (target_read_memory (pc
, buf
, sizeof buf
) != 0)
56 instr
= extract_unsigned_integer (buf
, sizeof buf
, byte_order
);
58 if (instr
== 0x8efc0077) /* setlos #__NR_sigreturn, gr7 */
59 retval
= NORMAL_SIGTRAMP
;
60 else if (instr
-= 0x8efc00ad) /* setlos #__NR_rt_sigreturn, gr7 */
65 if (target_read_memory (pc
+ frv_instr_size
, buf
, sizeof buf
) != 0)
67 instr
= extract_unsigned_integer (buf
, sizeof buf
, byte_order
);
68 if (instr
!= 0xc0700000) /* tira gr0, 0 */
71 /* If we get this far, we'll return a non-zero value, either
72 NORMAL_SIGTRAMP (1) or RT_SIGTRAMP (2). */
76 /* Given NEXT_FRAME, the "callee" frame of the sigtramp frame that we
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:
83 From arch/frv/kernel/signal.c:
87 void (*pretcode)(void);
90 unsigned long extramask[_NSIG_WORDS-1];
96 void (*pretcode)(void);
98 struct siginfo *pinfo;
105 From include/asm-frv/ucontext.h:
108 unsigned long uc_flags;
109 struct ucontext *uc_link;
111 struct sigcontext uc_mcontext;
115 From include/asm-frv/signal.h:
117 typedef struct sigaltstack {
123 From include/asm-frv/sigcontext.h:
126 struct user_context sc_context;
127 unsigned long sc_oldmask;
128 } __attribute__((aligned(8)));
130 From include/asm-frv/registers.h:
140 unsigned long __status;
141 unsigned long syscallno;
142 unsigned long orig_gr8;
143 unsigned long gner[2];
144 unsigned long long iacc[1];
148 unsigned long gr[64];
152 struct user_fpmedia_regs
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];
164 struct user_int_regs i;
165 struct user_fpmedia_regs f;
168 } __attribute__((aligned(8))); */
171 frv_linux_sigcontext_reg_addr (struct frame_info
*this_frame
, int regno
,
172 CORE_ADDR
*sc_addr_cache_ptr
)
174 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
175 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
178 if (sc_addr_cache_ptr
&& *sc_addr_cache_ptr
)
180 sc_addr
= *sc_addr_cache_ptr
;
188 pc
= get_frame_pc (this_frame
);
189 tramp_type
= frv_linux_pc_in_sigtramp (gdbarch
, pc
, 0);
191 get_frame_register (this_frame
, sp_regnum
, buf
);
192 sp
= extract_unsigned_integer (buf
, sizeof buf
, byte_order
);
194 if (tramp_type
== NORMAL_SIGTRAMP
)
196 /* For a normal sigtramp frame, the sigcontext struct starts
200 else if (tramp_type
== RT_SIGTRAMP
)
202 /* For a realtime sigtramp frame, SP + 12 contains a pointer
203 to a ucontext struct. The ucontext struct contains a
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
209 if (target_read_memory (sp
+ 12, buf
, sizeof buf
) != 0)
211 warning (_("Can't read realtime sigtramp frame."));
214 sc_addr
= extract_unsigned_integer (buf
, sizeof buf
, byte_order
);
218 internal_error (__FILE__
, __LINE__
, _("not a signal trampoline"));
220 if (sc_addr_cache_ptr
)
221 *sc_addr_cache_ptr
= sc_addr
;
228 /* sc_addr + 4 has "isr", the Integer Status Register. */
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].
243 sc_addr + 44 is gner[1]. */
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
);
254 return -1; /* not saved. */
258 /* Signal trampolines. */
260 static struct trad_frame_cache
*
261 frv_linux_sigtramp_frame_cache (struct frame_info
*this_frame
, void **this_cache
)
263 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
264 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
265 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
266 struct trad_frame_cache
*cache
;
270 CORE_ADDR sc_addr_cache_val
= 0;
271 struct frame_id this_id
;
276 cache
= trad_frame_cache_zalloc (this_frame
);
278 /* FIXME: cagney/2004-05-01: This is is long standing broken code.
279 The frame ID's code address should be the start-address of the
280 signal trampoline and not the current PC within that
282 get_frame_register (this_frame
, sp_regnum
, buf
);
283 addr
= extract_unsigned_integer (buf
, sizeof buf
, byte_order
);
284 this_id
= frame_id_build (addr
, get_frame_pc (this_frame
));
285 trad_frame_set_id (cache
, this_id
);
287 for (regnum
= 0; regnum
< frv_num_regs
; regnum
++)
289 LONGEST reg_addr
= frv_linux_sigcontext_reg_addr (this_frame
, regnum
,
292 trad_frame_set_reg_addr (cache
, regnum
, reg_addr
);
300 frv_linux_sigtramp_frame_this_id (struct frame_info
*this_frame
, void **this_cache
,
301 struct frame_id
*this_id
)
303 struct trad_frame_cache
*cache
=
304 frv_linux_sigtramp_frame_cache (this_frame
, this_cache
);
305 trad_frame_get_id (cache
, this_id
);
308 static struct value
*
309 frv_linux_sigtramp_frame_prev_register (struct frame_info
*this_frame
,
310 void **this_cache
, int regnum
)
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
);
319 frv_linux_sigtramp_frame_sniffer (const struct frame_unwind
*self
,
320 struct frame_info
*this_frame
,
323 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
324 CORE_ADDR pc
= get_frame_pc (this_frame
);
327 find_pc_partial_function (pc
, &name
, NULL
, NULL
);
328 if (frv_linux_pc_in_sigtramp (gdbarch
, pc
, name
))
334 static const struct frame_unwind frv_linux_sigtramp_frame_unwind
=
337 frv_linux_sigtramp_frame_this_id
,
338 frv_linux_sigtramp_frame_prev_register
,
340 frv_linux_sigtramp_frame_sniffer
343 /* The FRV kernel defines ELF_NGREG as 46. We add 2 in order to include
344 the loadmap addresses in the register set. (See below for more info.) */
345 #define FRV_ELF_NGREG (46 + 2)
346 typedef unsigned char frv_elf_greg_t
[4];
347 typedef struct { frv_elf_greg_t reg
[FRV_ELF_NGREG
]; } frv_elf_gregset_t
;
349 typedef unsigned char frv_elf_fpreg_t
[4];
352 frv_elf_fpreg_t fr
[64];
353 frv_elf_fpreg_t fner
[2];
354 frv_elf_fpreg_t msr
[2];
355 frv_elf_fpreg_t acc
[8];
356 unsigned char accg
[8];
357 frv_elf_fpreg_t fsr
[1];
358 } frv_elf_fpregset_t
;
360 /* Constants for accessing elements of frv_elf_gregset_t. */
365 #define FRV_PT_CCCR 3
369 #define FRV_PT_GNER0 10
370 #define FRV_PT_GNER1 11
371 #define FRV_PT_IACC0H 12
372 #define FRV_PT_IACC0L 13
374 /* Note: Only 32 of the GRs will be found in the corefile. */
375 #define FRV_PT_GR(j) ( 14 + (j)) /* GRj for 0<=j<=63. */
377 #define FRV_PT_TBR FRV_PT_GR(0) /* gr0 is always 0, so TBR is stuffed
380 /* Technically, the loadmap addresses are not part of `pr_reg' as
381 found in the elf_prstatus struct. The fields which communicate the
382 loadmap address appear (by design) immediately after `pr_reg'
383 though, and the BFD function elf32_frv_grok_prstatus() has been
384 implemented to include these fields in the register section that it
385 extracts from the core file. So, for our purposes, they may be
386 viewed as registers. */
388 #define FRV_PT_EXEC_FDPIC_LOADMAP 46
389 #define FRV_PT_INTERP_FDPIC_LOADMAP 47
392 /* Unpack an frv_elf_gregset_t into GDB's register cache. */
395 frv_linux_supply_gregset (const struct regset
*regset
,
396 struct regcache
*regcache
,
397 int regnum
, const void *gregs
, size_t len
)
400 char zerobuf
[MAX_REGISTER_SIZE
];
401 const frv_elf_gregset_t
*gregsetp
= gregs
;
403 memset (zerobuf
, 0, MAX_REGISTER_SIZE
);
405 /* gr0 always contains 0. Also, the kernel passes the TBR value in
407 regcache_raw_supply (regcache
, first_gpr_regnum
, zerobuf
);
409 for (regi
= first_gpr_regnum
+ 1; regi
<= last_gpr_regnum
; regi
++)
411 if (regi
>= first_gpr_regnum
+ 32)
412 regcache_raw_supply (regcache
, regi
, zerobuf
);
414 regcache_raw_supply (regcache
, regi
,
415 gregsetp
->reg
[FRV_PT_GR (regi
- first_gpr_regnum
)]);
418 regcache_raw_supply (regcache
, pc_regnum
, gregsetp
->reg
[FRV_PT_PC
]);
419 regcache_raw_supply (regcache
, psr_regnum
, gregsetp
->reg
[FRV_PT_PSR
]);
420 regcache_raw_supply (regcache
, ccr_regnum
, gregsetp
->reg
[FRV_PT_CCR
]);
421 regcache_raw_supply (regcache
, cccr_regnum
, gregsetp
->reg
[FRV_PT_CCCR
]);
422 regcache_raw_supply (regcache
, lr_regnum
, gregsetp
->reg
[FRV_PT_LR
]);
423 regcache_raw_supply (regcache
, lcr_regnum
, gregsetp
->reg
[FRV_PT_LCR
]);
424 regcache_raw_supply (regcache
, gner0_regnum
, gregsetp
->reg
[FRV_PT_GNER0
]);
425 regcache_raw_supply (regcache
, gner1_regnum
, gregsetp
->reg
[FRV_PT_GNER1
]);
426 regcache_raw_supply (regcache
, tbr_regnum
, gregsetp
->reg
[FRV_PT_TBR
]);
427 regcache_raw_supply (regcache
, fdpic_loadmap_exec_regnum
,
428 gregsetp
->reg
[FRV_PT_EXEC_FDPIC_LOADMAP
]);
429 regcache_raw_supply (regcache
, fdpic_loadmap_interp_regnum
,
430 gregsetp
->reg
[FRV_PT_INTERP_FDPIC_LOADMAP
]);
433 /* Unpack an frv_elf_fpregset_t into GDB's register cache. */
436 frv_linux_supply_fpregset (const struct regset
*regset
,
437 struct regcache
*regcache
,
438 int regnum
, const void *gregs
, size_t len
)
441 const frv_elf_fpregset_t
*fpregsetp
= gregs
;
443 for (regi
= first_fpr_regnum
; regi
<= last_fpr_regnum
; regi
++)
444 regcache_raw_supply (regcache
, regi
, fpregsetp
->fr
[regi
- first_fpr_regnum
]);
446 regcache_raw_supply (regcache
, fner0_regnum
, fpregsetp
->fner
[0]);
447 regcache_raw_supply (regcache
, fner1_regnum
, fpregsetp
->fner
[1]);
449 regcache_raw_supply (regcache
, msr0_regnum
, fpregsetp
->msr
[0]);
450 regcache_raw_supply (regcache
, msr1_regnum
, fpregsetp
->msr
[1]);
452 for (regi
= acc0_regnum
; regi
<= acc7_regnum
; regi
++)
453 regcache_raw_supply (regcache
, regi
, fpregsetp
->acc
[regi
- acc0_regnum
]);
455 regcache_raw_supply (regcache
, accg0123_regnum
, fpregsetp
->accg
);
456 regcache_raw_supply (regcache
, accg4567_regnum
, fpregsetp
->accg
+ 4);
458 regcache_raw_supply (regcache
, fsr0_regnum
, fpregsetp
->fsr
[0]);
461 /* FRV Linux kernel register sets. */
463 static struct regset frv_linux_gregset
=
466 frv_linux_supply_gregset
469 static struct regset frv_linux_fpregset
=
472 frv_linux_supply_fpregset
475 static const struct regset
*
476 frv_linux_regset_from_core_section (struct gdbarch
*gdbarch
,
477 const char *sect_name
, size_t sect_size
)
479 if (strcmp (sect_name
, ".reg") == 0
480 && sect_size
>= sizeof (frv_elf_gregset_t
))
481 return &frv_linux_gregset
;
483 if (strcmp (sect_name
, ".reg2") == 0
484 && sect_size
>= sizeof (frv_elf_fpregset_t
))
485 return &frv_linux_fpregset
;
492 frv_linux_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
494 linux_init_abi (info
, gdbarch
);
496 /* Set the sigtramp frame sniffer. */
497 frame_unwind_append_unwinder (gdbarch
, &frv_linux_sigtramp_frame_unwind
);
499 set_gdbarch_regset_from_core_section (gdbarch
,
500 frv_linux_regset_from_core_section
);
503 static enum gdb_osabi
504 frv_linux_elf_osabi_sniffer (bfd
*abfd
)
508 elf_flags
= elf_elfheader (abfd
)->e_flags
;
510 /* Assume GNU/Linux if using the FDPIC ABI. If/when another OS shows
511 up that uses this ABI, we'll need to start using .note sections
513 if (elf_flags
& EF_FRV_FDPIC
)
514 return GDB_OSABI_LINUX
;
516 return GDB_OSABI_UNKNOWN
;
519 /* Provide a prototype to silence -Wmissing-prototypes. */
520 void _initialize_frv_linux_tdep (void);
523 _initialize_frv_linux_tdep (void)
525 gdbarch_register_osabi (bfd_arch_frv
, 0, GDB_OSABI_LINUX
, frv_linux_init_abi
);
526 gdbarch_register_osabi_sniffer (bfd_arch_frv
,
527 bfd_target_elf_flavour
,
528 frv_linux_elf_osabi_sniffer
);