1 /* Target-dependent code for GNU/Linux on Nios II.
2 Copyright (C) 2012-2017 Free Software Foundation, Inc.
3 Contributed by Mentor Graphics, 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 3 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, see <http://www.gnu.org/licenses/>. */
23 #include "solib-svr4.h"
24 #include "trad-frame.h"
25 #include "tramp-frame.h"
29 #include "linux-tdep.h"
30 #include "glibc-tdep.h"
31 #include "nios2-tdep.h"
33 #include "features/nios2-linux.c"
35 /* Core file and register set support. */
37 /* Map from the normal register enumeration order to the order that
38 registers appear in core files, which corresponds to the order
39 of the register slots in the kernel's struct pt_regs. */
41 static const int reg_offsets[NIOS2_NUM_REGS] =
43 -1, 8, 9, 10, 11, 12, 13, 14, /* r0 - r7 */
44 0, 1, 2, 3, 4, 5, 6, 7, /* r8 - r15 */
45 23, 24, 25, 26, 27, 28, 29, 30, /* r16 - r23 */
46 -1, -1, 19, 18, 17, 21, -1, 16, /* et bt gp sp fp ea sstatus ra */
48 -1, 20, -1, -1, -1, -1, -1, -1, /* status estatus ... */
49 -1, -1, -1, -1, -1, -1, -1, -1
52 /* General register set size. Should match sizeof (struct pt_regs) +
53 sizeof (struct switch_stack) from the NIOS2 Linux kernel patch. */
55 #define NIOS2_GREGS_SIZE (4 * 34)
57 /* Implement the supply_regset hook for core files. */
60 nios2_supply_gregset (const struct regset *regset,
61 struct regcache *regcache,
62 int regnum, const void *gregs_buf, size_t len)
64 const gdb_byte *gregs = (const gdb_byte *) gregs_buf;
66 static const gdb_byte zero_buf[4] = {0, 0, 0, 0};
68 for (regno = NIOS2_Z_REGNUM; regno <= NIOS2_MPUACC_REGNUM; regno++)
69 if (regnum == -1 || regnum == regno)
71 if (reg_offsets[regno] != -1)
72 regcache_raw_supply (regcache, regno,
73 gregs + 4 * reg_offsets[regno]);
75 regcache_raw_supply (regcache, regno, zero_buf);
79 /* Implement the collect_regset hook for core files. */
82 nios2_collect_gregset (const struct regset *regset,
83 const struct regcache *regcache,
84 int regnum, void *gregs_buf, size_t len)
86 gdb_byte *gregs = (gdb_byte *) gregs_buf;
89 for (regno = NIOS2_Z_REGNUM; regno <= NIOS2_MPUACC_REGNUM; regno++)
90 if (regnum == -1 || regnum == regno)
92 if (reg_offsets[regno] != -1)
93 regcache_raw_collect (regcache, regno,
94 gregs + 4 * reg_offsets[regno]);
98 static const struct regset nios2_core_regset =
101 nios2_supply_gregset,
102 nios2_collect_gregset
105 /* Iterate over core file register note sections. */
108 nios2_iterate_over_regset_sections (struct gdbarch *gdbarch,
109 iterate_over_regset_sections_cb *cb,
111 const struct regcache *regcache)
113 cb (".reg", NIOS2_GREGS_SIZE, &nios2_core_regset, NULL, cb_data);
116 /* Initialize a trad-frame cache corresponding to the tramp-frame.
117 FUNC is the address of the instruction TRAMP[0] in memory.
119 This ABI is not documented. It corresponds to rt_setup_ucontext in
120 the kernel arch/nios2/kernel/signal.c file.
123 - The kernel creates a trampoline at the hard-wired address 0x1044.
124 - The stack pointer points to an object of type struct rt_sigframe.
125 The definition of this structure is not exported from the kernel.
126 The register save area is located at offset 152 bytes (as determined
127 by inspection of the stack contents in the debugger), and the
128 registers are saved as r1-r23, ra, fp, gp, ea, sp.
130 This interface was implemented with kernel version 3.19 (the first
131 official mainline kernel). Older unofficial kernel versions used
132 incompatible conventions; we do not support those here. */
134 #define NIOS2_SIGRETURN_TRAMP_ADDR 0x1044
135 #define NIOS2_SIGRETURN_REGSAVE_OFFSET 152
138 nios2_linux_rt_sigreturn_init (const struct tramp_frame *self,
139 struct frame_info *next_frame,
140 struct trad_frame_cache *this_cache,
143 CORE_ADDR sp = get_frame_register_unsigned (next_frame, NIOS2_SP_REGNUM);
144 CORE_ADDR base = sp + NIOS2_SIGRETURN_REGSAVE_OFFSET;
147 for (i = 0; i < 23; i++)
148 trad_frame_set_reg_addr (this_cache, i + 1, base + i * 4);
149 trad_frame_set_reg_addr (this_cache, NIOS2_RA_REGNUM, base + 23 * 4);
150 trad_frame_set_reg_addr (this_cache, NIOS2_FP_REGNUM, base + 24 * 4);
151 trad_frame_set_reg_addr (this_cache, NIOS2_GP_REGNUM, base + 25 * 4);
152 trad_frame_set_reg_addr (this_cache, NIOS2_PC_REGNUM, base + 27 * 4);
153 trad_frame_set_reg_addr (this_cache, NIOS2_SP_REGNUM, base + 28 * 4);
155 /* Save a frame ID. */
156 trad_frame_set_id (this_cache, frame_id_build (base, func));
159 /* Trampoline for sigreturn. This has the form
160 movi r2, __NR_rt_sigreturn
162 appropriately encoded for R1 or R2. */
164 static struct tramp_frame nios2_r1_linux_rt_sigreturn_tramp_frame =
169 { MATCH_R1_MOVI | SET_IW_I_B (2) | SET_IW_I_IMM16 (139), -1 },
170 { MATCH_R1_TRAP | SET_IW_R_IMM5 (0), -1},
171 { TRAMP_SENTINEL_INSN }
173 nios2_linux_rt_sigreturn_init
176 static struct tramp_frame nios2_r2_linux_rt_sigreturn_tramp_frame =
181 { MATCH_R2_MOVI | SET_IW_F2I16_B (2) | SET_IW_F2I16_IMM16 (139), -1 },
182 { MATCH_R2_TRAP | SET_IW_X2L5_IMM5 (0), -1},
183 { TRAMP_SENTINEL_INSN }
185 nios2_linux_rt_sigreturn_init
188 /* When FRAME is at a syscall instruction, return the PC of the next
189 instruction to be executed. */
192 nios2_linux_syscall_next_pc (struct frame_info *frame,
193 const struct nios2_opcode *op)
195 CORE_ADDR pc = get_frame_pc (frame);
196 ULONGEST syscall_nr = get_frame_register_unsigned (frame, NIOS2_R2_REGNUM);
198 /* If we are about to make a sigreturn syscall, use the unwinder to
199 decode the signal frame. */
200 if (syscall_nr == 139 /* rt_sigreturn */)
201 return frame_unwind_caller_pc (frame);
203 return pc + op->size;
206 /* Hook function for gdbarch_register_osabi. */
209 nios2_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
211 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
213 linux_init_abi (info, gdbarch);
215 /* Shared library handling. */
216 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
217 set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
219 set_solib_svr4_fetch_link_map_offsets (gdbarch,
220 svr4_ilp32_fetch_link_map_offsets);
221 /* Enable TLS support. */
222 set_gdbarch_fetch_tls_load_module_address (gdbarch,
223 svr4_fetch_objfile_link_map);
224 /* Core file support. */
225 set_gdbarch_iterate_over_regset_sections
226 (gdbarch, nios2_iterate_over_regset_sections);
227 /* Linux signal frame unwinders. */
228 if (gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_nios2r2)
229 tramp_frame_prepend_unwinder (gdbarch,
230 &nios2_r2_linux_rt_sigreturn_tramp_frame);
232 tramp_frame_prepend_unwinder (gdbarch,
233 &nios2_r1_linux_rt_sigreturn_tramp_frame);
235 tdep->syscall_next_pc = nios2_linux_syscall_next_pc;
237 /* Index of target address word in glibc jmp_buf. */
241 /* Provide a prototype to silence -Wmissing-prototypes. */
243 extern initialize_file_ftype _initialize_nios2_linux_tdep;
246 _initialize_nios2_linux_tdep (void)
249 const struct bfd_arch_info *arch_info;
251 for (arch_info = bfd_lookup_arch (bfd_arch_nios2, 0);
253 arch_info = arch_info->next)
254 gdbarch_register_osabi (bfd_arch_nios2, arch_info->mach,
255 GDB_OSABI_LINUX, nios2_linux_init_abi);
257 initialize_tdesc_nios2_linux ();