GDB (xrefs)

/tmp/gdb-8.1/gdb/nios2-linux-tdep.c
Go to the documentation of this file.
1 /* Target-dependent code for GNU/Linux on Nios II.
2 Copyright (C) 2012-2018 Free Software Foundation, Inc.
3 Contributed by Mentor Graphics, 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 "osabi.h"
23 #include "solib-svr4.h"
24 #include "trad-frame.h"
25 #include "tramp-frame.h"
26 #include "symtab.h"
27 #include "regset.h"
28 #include "regcache.h"
29 #include "linux-tdep.h"
30 #include "glibc-tdep.h"
31 #include "nios2-tdep.h"
32
33 /* Core file and register set support. */
34
35 /* Map from the normal register enumeration order to the order that
36 registers appear in core files, which corresponds to the order
37 of the register slots in the kernel's struct pt_regs. */
38
39 static const int reg_offsets[NIOS2_NUM_REGS] =
40 {
41 -1, 8, 9, 10, 11, 12, 13, 14, /* r0 - r7 */
42 0, 1, 2, 3, 4, 5, 6, 7, /* r8 - r15 */
43 23, 24, 25, 26, 27, 28, 29, 30, /* r16 - r23 */
44 -1, -1, 19, 18, 17, 21, -1, 16, /* et bt gp sp fp ea sstatus ra */
45 21, /* pc */
46 -1, 20, -1, -1, -1, -1, -1, -1, /* status estatus ... */
47 -1, -1, -1, -1, -1, -1, -1, -1
48 };
49
50 /* General register set size. Should match sizeof (struct pt_regs) +
51 sizeof (struct switch_stack) from the NIOS2 Linux kernel patch. */
52
53 #define NIOS2_GREGS_SIZE (4 * 34)
54
55 /* Implement the supply_regset hook for core files. */
56
57 static void
58 nios2_supply_gregset (const struct regset *regset,
59 struct regcache *regcache,
60 int regnum, const void *gregs_buf, size_t len)
61 {
62 const gdb_byte *gregs = (const gdb_byte *) gregs_buf;
63 int regno;
64 static const gdb_byte zero_buf[4] = {0, 0, 0, 0};
65
66 for (regno = NIOS2_Z_REGNUM; regno <= NIOS2_MPUACC_REGNUM; regno++)
67 if (regnum == -1 || regnum == regno)
68 {
69 if (reg_offsets[regno] != -1)
70 regcache_raw_supply (regcache, regno,
71 gregs + 4 * reg_offsets[regno]);
72 else
73 regcache_raw_supply (regcache, regno, zero_buf);
74 }
75 }
76
77 /* Implement the collect_regset hook for core files. */
78
79 static void
80 nios2_collect_gregset (const struct regset *regset,
81 const struct regcache *regcache,
82 int regnum, void *gregs_buf, size_t len)
83 {
84 gdb_byte *gregs = (gdb_byte *) gregs_buf;
85 int regno;
86
87 for (regno = NIOS2_Z_REGNUM; regno <= NIOS2_MPUACC_REGNUM; regno++)
88 if (regnum == -1 || regnum == regno)
89 {
90 if (reg_offsets[regno] != -1)
91 regcache_raw_collect (regcache, regno,
92 gregs + 4 * reg_offsets[regno]);
93 }
94 }
95
96 static const struct regset nios2_core_regset =
97 {
98 NULL,
99 nios2_supply_gregset,
100 nios2_collect_gregset
101 };
102
103 /* Iterate over core file register note sections. */
104
105 static void
106 nios2_iterate_over_regset_sections (struct gdbarch *gdbarch,
107 iterate_over_regset_sections_cb *cb,
108 void *cb_data,
109 const struct regcache *regcache)
110 {
111 cb (".reg", NIOS2_GREGS_SIZE, &nios2_core_regset, NULL, cb_data);
112 }
113
114 /* Initialize a trad-frame cache corresponding to the tramp-frame.
115 FUNC is the address of the instruction TRAMP[0] in memory.
116
117 This ABI is not documented. It corresponds to rt_setup_ucontext in
118 the kernel arch/nios2/kernel/signal.c file.
119
120 The key points are:
121 - The kernel creates a trampoline at the hard-wired address 0x1044.
122 - The stack pointer points to an object of type struct rt_sigframe.
123 The definition of this structure is not exported from the kernel.
124 The register save area is located at offset 152 bytes (as determined
125 by inspection of the stack contents in the debugger), and the
126 registers are saved as r1-r23, ra, fp, gp, ea, sp.
127
128 This interface was implemented with kernel version 3.19 (the first
129 official mainline kernel). Older unofficial kernel versions used
130 incompatible conventions; we do not support those here. */
131
132 #define NIOS2_SIGRETURN_TRAMP_ADDR 0x1044
133 #define NIOS2_SIGRETURN_REGSAVE_OFFSET 152
134
135 static void
136 nios2_linux_rt_sigreturn_init (const struct tramp_frame *self,
137 struct frame_info *next_frame,
138 struct trad_frame_cache *this_cache,
139 CORE_ADDR func)
140 {
141 CORE_ADDR sp = get_frame_register_unsigned (next_frame, NIOS2_SP_REGNUM);
142 CORE_ADDR base = sp + NIOS2_SIGRETURN_REGSAVE_OFFSET;
143 int i;
144
145 for (i = 0; i < 23; i++)
146 trad_frame_set_reg_addr (this_cache, i + 1, base + i * 4);
147 trad_frame_set_reg_addr (this_cache, NIOS2_RA_REGNUM, base + 23 * 4);
148 trad_frame_set_reg_addr (this_cache, NIOS2_FP_REGNUM, base + 24 * 4);
149 trad_frame_set_reg_addr (this_cache, NIOS2_GP_REGNUM, base + 25 * 4);
150 trad_frame_set_reg_addr (this_cache, NIOS2_PC_REGNUM, base + 27 * 4);
151 trad_frame_set_reg_addr (this_cache, NIOS2_SP_REGNUM, base + 28 * 4);
152
153 /* Save a frame ID. */
154 trad_frame_set_id (this_cache, frame_id_build (base, func));
155 }
156
157 /* Trampoline for sigreturn. This has the form
158 movi r2, __NR_rt_sigreturn
159 trap 0
160 appropriately encoded for R1 or R2. */
161
162 static struct tramp_frame nios2_r1_linux_rt_sigreturn_tramp_frame =
163 {
164 SIGTRAMP_FRAME,
165 4,
166 {
167 { MATCH_R1_MOVI | SET_IW_I_B (2) | SET_IW_I_IMM16 (139), -1 },
168 { MATCH_R1_TRAP | SET_IW_R_IMM5 (0), -1},
169 { TRAMP_SENTINEL_INSN }
170 },
171 nios2_linux_rt_sigreturn_init
172 };
173
174 static struct tramp_frame nios2_r2_linux_rt_sigreturn_tramp_frame =
175 {
176 SIGTRAMP_FRAME,
177 4,
178 {
179 { MATCH_R2_MOVI | SET_IW_F2I16_B (2) | SET_IW_F2I16_IMM16 (139), -1 },
180 { MATCH_R2_TRAP | SET_IW_X2L5_IMM5 (0), -1},
181 { TRAMP_SENTINEL_INSN }
182 },
183 nios2_linux_rt_sigreturn_init
184 };
185
186 /* When FRAME is at a syscall instruction, return the PC of the next
187 instruction to be executed. */
188
189 static CORE_ADDR
190 nios2_linux_syscall_next_pc (struct frame_info *frame,
191 const struct nios2_opcode *op)
192 {
193 CORE_ADDR pc = get_frame_pc (frame);
194 ULONGEST syscall_nr = get_frame_register_unsigned (frame, NIOS2_R2_REGNUM);
195
196 /* If we are about to make a sigreturn syscall, use the unwinder to
197 decode the signal frame. */
198 if (syscall_nr == 139 /* rt_sigreturn */)
199 return frame_unwind_caller_pc (frame);
200
201 return pc + op->size;
202 }
203
204 /* Hook function for gdbarch_register_osabi. */
205
206 static void
207 nios2_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
208 {
209 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
210
211 linux_init_abi (info, gdbarch);
212
213 /* Shared library handling. */
214 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
215 set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
216
217 set_solib_svr4_fetch_link_map_offsets (gdbarch,
218 svr4_ilp32_fetch_link_map_offsets);
219 /* Enable TLS support. */
220 set_gdbarch_fetch_tls_load_module_address (gdbarch,
221 svr4_fetch_objfile_link_map);
222 /* Core file support. */
223 set_gdbarch_iterate_over_regset_sections
224 (gdbarch, nios2_iterate_over_regset_sections);
225 /* Linux signal frame unwinders. */
226 if (gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_nios2r2)
227 tramp_frame_prepend_unwinder (gdbarch,
228 &nios2_r2_linux_rt_sigreturn_tramp_frame);
229 else
230 tramp_frame_prepend_unwinder (gdbarch,
231 &nios2_r1_linux_rt_sigreturn_tramp_frame);
232
233 tdep->syscall_next_pc = nios2_linux_syscall_next_pc;
234
235 /* Index of target address word in glibc jmp_buf. */
236 tdep->jb_pc = 10;
237 }
238
239 void
240 _initialize_nios2_linux_tdep (void)
241 {
242
243 const struct bfd_arch_info *arch_info;
244
245 for (arch_info = bfd_lookup_arch (bfd_arch_nios2, 0);
246 arch_info != NULL;
247 arch_info = arch_info->next)
248 gdbarch_register_osabi (bfd_arch_nios2, arch_info->mach,
249 GDB_OSABI_LINUX, nios2_linux_init_abi);
250 }
nios2_r1_linux_rt_sigreturn_tramp_frame
static struct tramp_frame nios2_r1_linux_rt_sigreturn_tramp_frame
Definition: nios2-linux-tdep.c:162
gdbarch
Definition: gdbarch.c:127
gdbarch_tdep::syscall_next_pc
CORE_ADDR(* syscall_next_pc)(struct frame_info *frame)
Definition: mips-tdep.h:118
trad_frame_set_reg_addr
void trad_frame_set_reg_addr(struct trad_frame_cache *this_trad_cache, int regnum, CORE_ADDR addr)
Definition: trad-frame.c:126
frame_id_build
struct frame_id frame_id_build(CORE_ADDR stack_addr, CORE_ADDR code_addr)
Definition: frame.c:624
nios2_iterate_over_regset_sections
static void nios2_iterate_over_regset_sections(struct gdbarch *gdbarch, iterate_over_regset_sections_cb *cb, void *cb_data, const struct regcache *regcache)
Definition: nios2-linux-tdep.c:106
get_frame_pc
CORE_ADDR get_frame_pc(struct frame_info *frame)
Definition: frame.c:2376
CORE_ADDR
bfd_vma CORE_ADDR
Definition: common-types.h:41
set_gdbarch_fetch_tls_load_module_address
void set_gdbarch_fetch_tls_load_module_address(struct gdbarch *gdbarch, gdbarch_fetch_tls_load_module_address_ftype fetch_tls_load_module_address)
Definition: gdbarch.c:3038
TRAMP_SENTINEL_INSN
#define TRAMP_SENTINEL_INSN
Definition: tramp-frame.h:44
reg_offsets
static const int reg_offsets[NIOS2_NUM_REGS]
Definition: nios2-linux-tdep.c:39
nios2_core_regset
static const struct regset nios2_core_regset
Definition: nios2-linux-tdep.c:96
frame_unwind_caller_pc
CORE_ADDR frame_unwind_caller_pc(struct frame_info *this_frame)
Definition: frame.c:944
func
void(* func)(char *)
Definition: remote-fileio.c:1104
iterate_over_regset_sections_cb
void() iterate_over_regset_sections_cb(const char *sect_name, int size, const struct regset *regset, const char *human_name, void *cb_data)
Definition: gdbarch.h:99
set_gdbarch_skip_trampoline_code
void set_gdbarch_skip_trampoline_code(struct gdbarch *gdbarch, gdbarch_skip_trampoline_code_ftype skip_trampoline_code)
Definition: gdbarch.c:3316
glibc_skip_solib_resolver
CORE_ADDR glibc_skip_solib_resolver(struct gdbarch *gdbarch, CORE_ADDR pc)
Definition: glibc-tdep.c:38
nios2-tdep.h
trad_frame_set_id
void trad_frame_set_id(struct trad_frame_cache *this_trad_cache, struct frame_id this_id)
Definition: trad-frame.c:171
GDB_OSABI_LINUX
Definition: defs.h:517
gdbarch_tdep
Definition: aarch64-tdep.h:55
nios2_r2_linux_rt_sigreturn_tramp_frame
static struct tramp_frame nios2_r2_linux_rt_sigreturn_tramp_frame
Definition: nios2-linux-tdep.c:174
linux_init_abi
void linux_init_abi(struct gdbarch_info info, struct gdbarch *gdbarch)
Definition: linux-tdep.c:2492
SIGTRAMP_FRAME
Definition: frame.h:255
frame_info
Definition: frame.c:85
svr4_ilp32_fetch_link_map_offsets
struct link_map_offsets * svr4_ilp32_fetch_link_map_offsets(void)
Definition: solib-svr4.c:3183
defs.h
_initialize_nios2_linux_tdep
void _initialize_nios2_linux_tdep(void)
Definition: nios2-linux-tdep.c:240
gdbarch_tdep
struct gdbarch_tdep * gdbarch_tdep(struct gdbarch *gdbarch)
Definition: gdbarch.c:1491
svr4_fetch_objfile_link_map
CORE_ADDR svr4_fetch_objfile_link_map(struct objfile *objfile)
Definition: solib-svr4.c:1579
regset
Definition: regset.h:34
symtab.h
nios2_linux_init_abi
static void nios2_linux_init_abi(struct gdbarch_info info, struct gdbarch *gdbarch)
Definition: nios2-linux-tdep.c:207
trad-frame.h
find_solib_trampoline_target
CORE_ADDR find_solib_trampoline_target(struct frame_info *frame, CORE_ADDR pc)
Definition: minsyms.c:1492
solib-svr4.h
osabi.h
trad_frame_cache
Definition: trad-frame.c:27
regcache.h
NIOS2_NUM_REGS
#define NIOS2_NUM_REGS
Definition: nios2-tdep.h:63
NIOS2_R2_REGNUM
#define NIOS2_R2_REGNUM
Definition: nios2-tdep.h:28
frame.h
gdbarch_tdep::jb_pc
int jb_pc
Definition: aarch64-tdep.h:62
set_solib_svr4_fetch_link_map_offsets
void set_solib_svr4_fetch_link_map_offsets(struct gdbarch *gdbarch, struct link_map_offsets *(*flmo)(void))
Definition: solib-svr4.c:3137
NIOS2_SP_REGNUM
#define NIOS2_SP_REGNUM
Definition: nios2-tdep.h:34
regnum
int regnum
Definition: aarch64-tdep.c:77
get_frame_register_unsigned
ULONGEST get_frame_register_unsigned(struct frame_info *frame, int regnum)
Definition: frame.c:1308
gdbarch_register_osabi
void gdbarch_register_osabi(enum bfd_architecture arch, unsigned long machine, enum gdb_osabi osabi, void(*init_osabi)(struct gdbarch_info, struct gdbarch *))
Definition: osabi.c:143
glibc-tdep.h
NIOS2_Z_REGNUM
#define NIOS2_Z_REGNUM
Definition: nios2-tdep.h:27
NIOS2_GREGS_SIZE
#define NIOS2_GREGS_SIZE
Definition: nios2-linux-tdep.c:53
tramp-frame.h
gdb_byte
bfd_byte gdb_byte
Definition: common-types.h:38
nios2_linux_rt_sigreturn_init
static void nios2_linux_rt_sigreturn_init(const struct tramp_frame *self, struct frame_info *next_frame, struct trad_frame_cache *this_cache, CORE_ADDR func)
Definition: nios2-linux-tdep.c:136
NIOS2_GP_REGNUM
#define NIOS2_GP_REGNUM
Definition: nios2-tdep.h:33
tramp_frame_prepend_unwinder
void tramp_frame_prepend_unwinder(struct gdbarch *gdbarch, const struct tramp_frame *tramp_frame)
Definition: tramp-frame.c:146
linux-tdep.h
nios2_collect_gregset
static void nios2_collect_gregset(const struct regset *regset, const struct regcache *regcache, int regnum, void *gregs_buf, size_t len)
Definition: nios2-linux-tdep.c:80
NIOS2_RA_REGNUM
#define NIOS2_RA_REGNUM
Definition: nios2-tdep.h:38
regcache_raw_supply
void regcache_raw_supply(struct regcache *regcache, int regnum, const void *buf)
Definition: regcache.c:1004
ULONGEST
unsigned long long ULONGEST
Definition: common-types.h:53
regcache
Definition: regcache.h:232
gdbarch_info
Definition: gdbarch.h:1626
nios2_linux_syscall_next_pc
static CORE_ADDR nios2_linux_syscall_next_pc(struct frame_info *frame, const struct nios2_opcode *op)
Definition: nios2-linux-tdep.c:190
gdbarch_bfd_arch_info
const struct bfd_arch_info * gdbarch_bfd_arch_info(struct gdbarch *gdbarch)
Definition: gdbarch.c:1500
regcache_raw_collect
void regcache_raw_collect(const struct regcache *regcache, int regnum, void *buf)
Definition: regcache.c:1085
regset.h
tramp_frame
Definition: tramp-frame.h:46
nios2_supply_gregset
static void nios2_supply_gregset(const struct regset *regset, struct regcache *regcache, int regnum, const void *gregs_buf, size_t len)
Definition: nios2-linux-tdep.c:58
set_gdbarch_iterate_over_regset_sections
void set_gdbarch_iterate_over_regset_sections(struct gdbarch *gdbarch, gdbarch_iterate_over_regset_sections_ftype iterate_over_regset_sections)
Definition: gdbarch.c:3647
NIOS2_PC_REGNUM
#define NIOS2_PC_REGNUM
Definition: nios2-tdep.h:39
set_gdbarch_skip_solib_resolver
void set_gdbarch_skip_solib_resolver(struct gdbarch *gdbarch, gdbarch_skip_solib_resolver_ftype skip_solib_resolver)
Definition: gdbarch.c:3333
NIOS2_FP_REGNUM
#define NIOS2_FP_REGNUM
Definition: nios2-tdep.h:35
NIOS2_SIGRETURN_REGSAVE_OFFSET
#define NIOS2_SIGRETURN_REGSAVE_OFFSET
Definition: nios2-linux-tdep.c:133
NIOS2_MPUACC_REGNUM
#define NIOS2_MPUACC_REGNUM
Definition: nios2-tdep.h:56