1 /* Target-dependent code for GNU/Linux running on the Fujitsu FR-V,
3 Copyright (C) 2004 Free Software Foundation, Inc.
5 This file is part of GDB.
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 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
31 #include "trad-frame.h"
32 #include "frame-unwind.h"
34 #include "gdb_string.h"
36 /* Define the size (in bytes) of an FR-V instruction. */
37 static const int frv_instr_size
= 4;
45 frv_linux_pc_in_sigtramp (CORE_ADDR pc
, char *name
)
47 char buf
[frv_instr_size
];
51 if (target_read_memory (pc
, buf
, sizeof buf
) != 0)
54 instr
= extract_unsigned_integer (buf
, sizeof buf
);
56 if (instr
== 0x8efc0077) /* setlos #__NR_sigreturn, gr7 */
57 retval
= NORMAL_SIGTRAMP
;
58 else if (instr
-= 0x8efc00ad) /* setlos #__NR_rt_sigreturn, gr7 */
63 if (target_read_memory (pc
+ frv_instr_size
, buf
, sizeof buf
) != 0)
65 instr
= extract_unsigned_integer (buf
, sizeof buf
);
66 if (instr
!= 0xc0700000) /* tira gr0, 0 */
69 /* If we get this far, we'll return a non-zero value, either
70 NORMAL_SIGTRAMP (1) or RT_SIGTRAMP (2). */
74 /* Given NEXT_FRAME, the "callee" frame of the sigtramp frame that we
75 wish to decode, and REGNO, one of the frv register numbers defined
76 in frv-tdep.h, return the address of the saved register (corresponding
77 to REGNO) in the sigtramp frame. Return -1 if the register is not
78 found in the sigtramp frame. The magic numbers in the code below
79 were computed by examining the following kernel structs:
81 From arch/frv/kernel/signal.c:
85 void (*pretcode)(void);
88 unsigned long extramask[_NSIG_WORDS-1];
94 void (*pretcode)(void);
96 struct siginfo *pinfo;
103 From include/asm-frv/ucontext.h:
106 unsigned long uc_flags;
107 struct ucontext *uc_link;
109 struct sigcontext uc_mcontext;
113 From include/asm-frv/signal.h:
115 typedef struct sigaltstack {
121 From include/asm-frv/sigcontext.h:
124 struct user_context sc_context;
125 unsigned long sc_oldmask;
126 } __attribute__((aligned(8)));
128 From include/asm-frv/registers.h:
138 unsigned long __status;
139 unsigned long syscallno;
140 unsigned long orig_gr8;
141 unsigned long gner[2];
142 unsigned long long iacc[1];
146 unsigned long gr[64];
150 struct user_fpmedia_regs
152 unsigned long fr[64];
153 unsigned long fner[2];
154 unsigned long msr[2];
155 unsigned long acc[8];
156 unsigned char accg[8];
157 unsigned long fsr[1];
162 struct user_int_regs i;
163 struct user_fpmedia_regs f;
166 } __attribute__((aligned(8))); */
169 frv_linux_sigcontext_reg_addr (struct frame_info
*next_frame
, int regno
,
170 CORE_ADDR
*sc_addr_cache_ptr
)
174 if (sc_addr_cache_ptr
&& *sc_addr_cache_ptr
)
176 sc_addr
= *sc_addr_cache_ptr
;
184 pc
= frame_pc_unwind (next_frame
);
185 tramp_type
= frv_linux_pc_in_sigtramp (pc
, 0);
187 frame_unwind_register (next_frame
, sp_regnum
, buf
);
188 sp
= extract_unsigned_integer (buf
, sizeof buf
);
190 if (tramp_type
== NORMAL_SIGTRAMP
)
192 /* For a normal sigtramp frame, the sigcontext struct starts
196 else if (tramp_type
== RT_SIGTRAMP
)
198 /* For a realtime sigtramp frame, SP + 12 contains a pointer
199 to a ucontext struct. The ucontext struct contains a
200 sigcontext struct starting 24 bytes in. (The offset of
201 uc_mcontext within struct ucontext is derived as follows:
202 stack_t is a 12-byte struct and struct sigcontext is
203 8-byte aligned. This gives an offset of 8 + 12 + 4 (for
205 if (target_read_memory (sp
+ 12, buf
, sizeof buf
) != 0)
207 warning (_("Can't read realtime sigtramp frame."));
210 sc_addr
= extract_unsigned_integer (buf
, sizeof buf
);
214 internal_error (__FILE__
, __LINE__
, _("not a signal trampoline"));
216 if (sc_addr_cache_ptr
)
217 *sc_addr_cache_ptr
= sc_addr
;
224 /* sc_addr + 4 has "isr", the Integer Status Register. */
235 /* sc_addr + 28 is __status, the exception status.
236 sc_addr + 32 is syscallno, the syscall number or -1.
237 sc_addr + 36 is orig_gr8, the original syscall arg #1.
238 sc_addr + 40 is gner[0].
239 sc_addr + 44 is gner[1]. */
245 if (first_gpr_regnum
<= regno
&& regno
<= last_gpr_regnum
)
246 return sc_addr
+ 56 + 4 * (regno
- first_gpr_regnum
);
247 else if (first_fpr_regnum
<= regno
&& regno
<= last_fpr_regnum
)
248 return sc_addr
+ 312 + 4 * (regno
- first_fpr_regnum
);
250 return -1; /* not saved. */
254 /* Signal trampolines. */
256 static struct trad_frame_cache
*
257 frv_linux_sigtramp_frame_cache (struct frame_info
*next_frame
, void **this_cache
)
259 struct trad_frame_cache
*cache
;
260 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
264 CORE_ADDR sc_addr_cache_val
= 0;
265 struct frame_id this_id
;
270 cache
= trad_frame_cache_zalloc (next_frame
);
272 /* FIXME: cagney/2004-05-01: This is is long standing broken code.
273 The frame ID's code address should be the start-address of the
274 signal trampoline and not the current PC within that
276 frame_unwind_register (next_frame
, sp_regnum
, buf
);
277 this_id
= frame_id_build (extract_unsigned_integer (buf
, sizeof buf
),
278 frame_pc_unwind (next_frame
));
279 trad_frame_set_id (cache
, this_id
);
281 for (regnum
= 0; regnum
< frv_num_regs
; regnum
++)
283 LONGEST reg_addr
= frv_linux_sigcontext_reg_addr (next_frame
, regnum
,
286 trad_frame_set_reg_addr (cache
, regnum
, reg_addr
);
294 frv_linux_sigtramp_frame_this_id (struct frame_info
*next_frame
, void **this_cache
,
295 struct frame_id
*this_id
)
297 struct trad_frame_cache
*cache
=
298 frv_linux_sigtramp_frame_cache (next_frame
, this_cache
);
299 trad_frame_get_id (cache
, this_id
);
303 frv_linux_sigtramp_frame_prev_register (struct frame_info
*next_frame
,
305 int regnum
, int *optimizedp
,
306 enum lval_type
*lvalp
, CORE_ADDR
*addrp
,
307 int *realnump
, gdb_byte
*valuep
)
309 /* Make sure we've initialized the cache. */
310 struct trad_frame_cache
*cache
=
311 frv_linux_sigtramp_frame_cache (next_frame
, this_cache
);
312 trad_frame_get_register (cache
, next_frame
, regnum
, optimizedp
, lvalp
,
313 addrp
, realnump
, valuep
);
316 static const struct frame_unwind frv_linux_sigtramp_frame_unwind
=
319 frv_linux_sigtramp_frame_this_id
,
320 frv_linux_sigtramp_frame_prev_register
323 static const struct frame_unwind
*
324 frv_linux_sigtramp_frame_sniffer (struct frame_info
*next_frame
)
326 CORE_ADDR pc
= frame_pc_unwind (next_frame
);
329 find_pc_partial_function (pc
, &name
, NULL
, NULL
);
330 if (frv_linux_pc_in_sigtramp (pc
, name
))
331 return &frv_linux_sigtramp_frame_unwind
;
337 /* The FRV kernel defines ELF_NGREG as 46. We add 2 in order to include
338 the loadmap addresses in the register set. (See below for more info.) */
339 #define FRV_ELF_NGREG (46 + 2)
340 typedef unsigned char frv_elf_greg_t
[4];
341 typedef struct { frv_elf_greg_t reg
[FRV_ELF_NGREG
]; } frv_elf_gregset_t
;
343 typedef unsigned char frv_elf_fpreg_t
[4];
346 frv_elf_fpreg_t fr
[64];
347 frv_elf_fpreg_t fner
[2];
348 frv_elf_fpreg_t msr
[2];
349 frv_elf_fpreg_t acc
[8];
350 unsigned char accg
[8];
351 frv_elf_fpreg_t fsr
[1];
352 } frv_elf_fpregset_t
;
354 /* Constants for accessing elements of frv_elf_gregset_t. */
359 #define FRV_PT_CCCR 3
363 #define FRV_PT_GNER0 10
364 #define FRV_PT_GNER1 11
365 #define FRV_PT_IACC0H 12
366 #define FRV_PT_IACC0L 13
368 /* Note: Only 32 of the GRs will be found in the corefile. */
369 #define FRV_PT_GR(j) ( 14 + (j)) /* GRj for 0<=j<=63. */
371 #define FRV_PT_TBR FRV_PT_GR(0) /* gr0 is always 0, so TBR is stuffed
374 /* Technically, the loadmap addresses are not part of `pr_reg' as
375 found in the elf_prstatus struct. The fields which communicate the
376 loadmap address appear (by design) immediately after `pr_reg'
377 though, and the BFD function elf32_frv_grok_prstatus() has been
378 implemented to include these fields in the register section that it
379 extracts from the core file. So, for our purposes, they may be
380 viewed as registers. */
382 #define FRV_PT_EXEC_FDPIC_LOADMAP 46
383 #define FRV_PT_INTERP_FDPIC_LOADMAP 47
386 /* Unpack an frv_elf_gregset_t into GDB's register cache. */
389 frv_linux_supply_gregset (const struct regset
*regset
,
390 struct regcache
*regcache
,
391 int regnum
, const void *gregs
, size_t len
)
394 char zerobuf
[MAX_REGISTER_SIZE
];
395 const frv_elf_gregset_t
*gregsetp
= gregs
;
397 memset (zerobuf
, 0, MAX_REGISTER_SIZE
);
399 /* gr0 always contains 0. Also, the kernel passes the TBR value in
401 regcache_raw_supply (regcache
, first_gpr_regnum
, zerobuf
);
403 for (regi
= first_gpr_regnum
+ 1; regi
<= last_gpr_regnum
; regi
++)
405 if (regi
>= first_gpr_regnum
+ 32)
406 regcache_raw_supply (regcache
, regi
, zerobuf
);
408 regcache_raw_supply (regcache
, regi
,
409 gregsetp
->reg
[FRV_PT_GR (regi
- first_gpr_regnum
)]);
412 regcache_raw_supply (regcache
, pc_regnum
, gregsetp
->reg
[FRV_PT_PC
]);
413 regcache_raw_supply (regcache
, psr_regnum
, gregsetp
->reg
[FRV_PT_PSR
]);
414 regcache_raw_supply (regcache
, ccr_regnum
, gregsetp
->reg
[FRV_PT_CCR
]);
415 regcache_raw_supply (regcache
, cccr_regnum
, gregsetp
->reg
[FRV_PT_CCCR
]);
416 regcache_raw_supply (regcache
, lr_regnum
, gregsetp
->reg
[FRV_PT_LR
]);
417 regcache_raw_supply (regcache
, lcr_regnum
, gregsetp
->reg
[FRV_PT_LCR
]);
418 regcache_raw_supply (regcache
, gner0_regnum
, gregsetp
->reg
[FRV_PT_GNER0
]);
419 regcache_raw_supply (regcache
, gner1_regnum
, gregsetp
->reg
[FRV_PT_GNER1
]);
420 regcache_raw_supply (regcache
, tbr_regnum
, gregsetp
->reg
[FRV_PT_TBR
]);
421 regcache_raw_supply (regcache
, fdpic_loadmap_exec_regnum
,
422 gregsetp
->reg
[FRV_PT_EXEC_FDPIC_LOADMAP
]);
423 regcache_raw_supply (regcache
, fdpic_loadmap_interp_regnum
,
424 gregsetp
->reg
[FRV_PT_INTERP_FDPIC_LOADMAP
]);
427 /* Unpack an frv_elf_fpregset_t into GDB's register cache. */
430 frv_linux_supply_fpregset (const struct regset
*regset
,
431 struct regcache
*regcache
,
432 int regnum
, const void *gregs
, size_t len
)
435 const frv_elf_fpregset_t
*fpregsetp
= gregs
;
437 for (regi
= first_fpr_regnum
; regi
<= last_fpr_regnum
; regi
++)
438 regcache_raw_supply (regcache
, regi
, fpregsetp
->fr
[regi
- first_fpr_regnum
]);
440 regcache_raw_supply (regcache
, fner0_regnum
, fpregsetp
->fner
[0]);
441 regcache_raw_supply (regcache
, fner1_regnum
, fpregsetp
->fner
[1]);
443 regcache_raw_supply (regcache
, msr0_regnum
, fpregsetp
->msr
[0]);
444 regcache_raw_supply (regcache
, msr1_regnum
, fpregsetp
->msr
[1]);
446 for (regi
= acc0_regnum
; regi
<= acc7_regnum
; regi
++)
447 regcache_raw_supply (regcache
, regi
, fpregsetp
->acc
[regi
- acc0_regnum
]);
449 regcache_raw_supply (regcache
, accg0123_regnum
, fpregsetp
->accg
);
450 regcache_raw_supply (regcache
, accg4567_regnum
, fpregsetp
->accg
+ 4);
452 regcache_raw_supply (regcache
, fsr0_regnum
, fpregsetp
->fsr
[0]);
455 /* FRV Linux register sets. */
457 static struct regset frv_linux_gregset
=
460 frv_linux_supply_gregset
463 static struct regset frv_linux_fpregset
=
466 frv_linux_supply_fpregset
469 static const struct regset
*
470 frv_linux_regset_from_core_section (struct gdbarch
*gdbarch
,
471 const char *sect_name
, size_t sect_size
)
473 if (strcmp (sect_name
, ".reg") == 0
474 && sect_size
>= sizeof (frv_elf_gregset_t
))
475 return &frv_linux_gregset
;
477 if (strcmp (sect_name
, ".reg2") == 0
478 && sect_size
>= sizeof (frv_elf_fpregset_t
))
479 return &frv_linux_fpregset
;
486 frv_linux_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
488 /* Set the sigtramp frame sniffer. */
489 frame_unwind_append_sniffer (gdbarch
, frv_linux_sigtramp_frame_sniffer
);
490 set_gdbarch_regset_from_core_section (gdbarch
,
491 frv_linux_regset_from_core_section
);
494 static enum gdb_osabi
495 frv_linux_elf_osabi_sniffer (bfd
*abfd
)
499 elf_flags
= elf_elfheader (abfd
)->e_flags
;
501 /* Assume GNU/Linux if using the FDPIC ABI. If/when another OS shows
502 up that uses this ABI, we'll need to start using .note sections
504 if (elf_flags
& EF_FRV_FDPIC
)
505 return GDB_OSABI_LINUX
;
507 return GDB_OSABI_UNKNOWN
;
510 /* Provide a prototype to silence -Wmissing-prototypes. */
511 void _initialize_frv_linux_tdep (void);
514 _initialize_frv_linux_tdep (void)
516 gdbarch_register_osabi (bfd_arch_frv
, 0, GDB_OSABI_LINUX
, frv_linux_init_abi
);
517 gdbarch_register_osabi_sniffer (bfd_arch_frv
,
518 bfd_target_elf_flavour
,
519 frv_linux_elf_osabi_sniffer
);