Change Ada catchpoints to be bp_catchpoint
[deliverable/binutils-gdb.git] / gdb / nios2-linux-tdep.c
CommitLineData
a1217d97 1/* Target-dependent code for GNU/Linux on Nios II.
42a4f53d 2 Copyright (C) 2012-2019 Free Software Foundation, Inc.
a1217d97
SL
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"
a1217d97
SL
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
a1217d97
SL
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
39static 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 */
0b76b0ce 44 -1, -1, 19, 18, 17, 21, -1, 16, /* et bt gp sp fp ea sstatus ra */
a1217d97
SL
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
c5b8d704
AA
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
a1217d97
SL
55/* Implement the supply_regset hook for core files. */
56
57static void
58nios2_supply_gregset (const struct regset *regset,
59 struct regcache *regcache,
60 int regnum, const void *gregs_buf, size_t len)
61{
19ba03f4 62 const gdb_byte *gregs = (const gdb_byte *) gregs_buf;
a1217d97
SL
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)
73e1c03f 70 regcache->raw_supply (regno, gregs + 4 * reg_offsets[regno]);
a1217d97 71 else
73e1c03f 72 regcache->raw_supply (regno, zero_buf);
a1217d97
SL
73 }
74}
75
81580573
AA
76/* Implement the collect_regset hook for core files. */
77
78static void
79nios2_collect_gregset (const struct regset *regset,
80 const struct regcache *regcache,
81 int regnum, void *gregs_buf, size_t len)
82{
19ba03f4 83 gdb_byte *gregs = (gdb_byte *) gregs_buf;
81580573
AA
84 int regno;
85
86 for (regno = NIOS2_Z_REGNUM; regno <= NIOS2_MPUACC_REGNUM; regno++)
87 if (regnum == -1 || regnum == regno)
88 {
89 if (reg_offsets[regno] != -1)
34a79281 90 regcache->raw_collect (regno, gregs + 4 * reg_offsets[regno]);
81580573
AA
91 }
92}
93
3ca7dae4 94static const struct regset nios2_core_regset =
a1217d97
SL
95{
96 NULL,
97 nios2_supply_gregset,
81580573 98 nios2_collect_gregset
a1217d97
SL
99};
100
c5b8d704 101/* Iterate over core file register note sections. */
a1217d97 102
c5b8d704
AA
103static void
104nios2_iterate_over_regset_sections (struct gdbarch *gdbarch,
105 iterate_over_regset_sections_cb *cb,
106 void *cb_data,
107 const struct regcache *regcache)
a1217d97 108{
a616bb94
AH
109 cb (".reg", NIOS2_GREGS_SIZE, NIOS2_GREGS_SIZE, &nios2_core_regset, NULL,
110 cb_data);
a1217d97
SL
111}
112
a1217d97 113/* Initialize a trad-frame cache corresponding to the tramp-frame.
35d54293
SL
114 FUNC is the address of the instruction TRAMP[0] in memory.
115
116 This ABI is not documented. It corresponds to rt_setup_ucontext in
117 the kernel arch/nios2/kernel/signal.c file.
118
119 The key points are:
120 - The kernel creates a trampoline at the hard-wired address 0x1044.
121 - The stack pointer points to an object of type struct rt_sigframe.
122 The definition of this structure is not exported from the kernel.
123 The register save area is located at offset 152 bytes (as determined
124 by inspection of the stack contents in the debugger), and the
125 registers are saved as r1-r23, ra, fp, gp, ea, sp.
126
127 This interface was implemented with kernel version 3.19 (the first
128 official mainline kernel). Older unofficial kernel versions used
129 incompatible conventions; we do not support those here. */
130
131#define NIOS2_SIGRETURN_TRAMP_ADDR 0x1044
132#define NIOS2_SIGRETURN_REGSAVE_OFFSET 152
a1217d97
SL
133
134static void
135nios2_linux_rt_sigreturn_init (const struct tramp_frame *self,
136 struct frame_info *next_frame,
137 struct trad_frame_cache *this_cache,
138 CORE_ADDR func)
139{
35d54293
SL
140 CORE_ADDR sp = get_frame_register_unsigned (next_frame, NIOS2_SP_REGNUM);
141 CORE_ADDR base = sp + NIOS2_SIGRETURN_REGSAVE_OFFSET;
a1217d97
SL
142 int i;
143
144 for (i = 0; i < 23; i++)
145 trad_frame_set_reg_addr (this_cache, i + 1, base + i * 4);
146 trad_frame_set_reg_addr (this_cache, NIOS2_RA_REGNUM, base + 23 * 4);
147 trad_frame_set_reg_addr (this_cache, NIOS2_FP_REGNUM, base + 24 * 4);
148 trad_frame_set_reg_addr (this_cache, NIOS2_GP_REGNUM, base + 25 * 4);
149 trad_frame_set_reg_addr (this_cache, NIOS2_PC_REGNUM, base + 27 * 4);
150 trad_frame_set_reg_addr (this_cache, NIOS2_SP_REGNUM, base + 28 * 4);
151
152 /* Save a frame ID. */
153 trad_frame_set_id (this_cache, frame_id_build (base, func));
154}
155
af60a1ef
SL
156/* Trampoline for sigreturn. This has the form
157 movi r2, __NR_rt_sigreturn
158 trap 0
159 appropriately encoded for R1 or R2. */
160
161static struct tramp_frame nios2_r1_linux_rt_sigreturn_tramp_frame =
162{
163 SIGTRAMP_FRAME,
164 4,
165 {
7bc02706
TT
166 { MATCH_R1_MOVI | SET_IW_I_B (2) | SET_IW_I_IMM16 (139), ULONGEST_MAX },
167 { MATCH_R1_TRAP | SET_IW_R_IMM5 (0), ULONGEST_MAX},
af60a1ef
SL
168 { TRAMP_SENTINEL_INSN }
169 },
170 nios2_linux_rt_sigreturn_init
171};
172
173static struct tramp_frame nios2_r2_linux_rt_sigreturn_tramp_frame =
a1217d97
SL
174{
175 SIGTRAMP_FRAME,
176 4,
177 {
7bc02706
TT
178 { MATCH_R2_MOVI | SET_IW_F2I16_B (2) | SET_IW_F2I16_IMM16 (139), ULONGEST_MAX },
179 { MATCH_R2_TRAP | SET_IW_X2L5_IMM5 (0), ULONGEST_MAX},
a1217d97
SL
180 { TRAMP_SENTINEL_INSN }
181 },
182 nios2_linux_rt_sigreturn_init
183};
184
185/* When FRAME is at a syscall instruction, return the PC of the next
186 instruction to be executed. */
187
188static CORE_ADDR
af60a1ef
SL
189nios2_linux_syscall_next_pc (struct frame_info *frame,
190 const struct nios2_opcode *op)
a1217d97
SL
191{
192 CORE_ADDR pc = get_frame_pc (frame);
193 ULONGEST syscall_nr = get_frame_register_unsigned (frame, NIOS2_R2_REGNUM);
194
195 /* If we are about to make a sigreturn syscall, use the unwinder to
196 decode the signal frame. */
21986715 197 if (syscall_nr == 139 /* rt_sigreturn */)
a1217d97
SL
198 return frame_unwind_caller_pc (frame);
199
af60a1ef 200 return pc + op->size;
a1217d97
SL
201}
202
f489207e
SL
203/* Return true if PC is a kernel helper, a function mapped by the kernel
204 into user space on an unwritable page. Currently the only such function
205 is __kuser_cmpxchg at 0x1004. See arch/nios2/kernel/entry.S in the Linux
206 kernel sources and sysdeps/unix/sysv/linux/nios2/atomic-machine.h in
207 GLIBC. */
208static bool
209nios2_linux_is_kernel_helper (CORE_ADDR pc)
210{
211 return pc == 0x1004;
212}
213
a1217d97
SL
214/* Hook function for gdbarch_register_osabi. */
215
216static void
217nios2_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
218{
219 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
220
221 linux_init_abi (info, gdbarch);
222
223 /* Shared library handling. */
224 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
225 set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
226
227 set_solib_svr4_fetch_link_map_offsets (gdbarch,
228 svr4_ilp32_fetch_link_map_offsets);
229 /* Enable TLS support. */
230 set_gdbarch_fetch_tls_load_module_address (gdbarch,
231 svr4_fetch_objfile_link_map);
232 /* Core file support. */
c5b8d704
AA
233 set_gdbarch_iterate_over_regset_sections
234 (gdbarch, nios2_iterate_over_regset_sections);
a1217d97 235 /* Linux signal frame unwinders. */
af60a1ef
SL
236 if (gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_nios2r2)
237 tramp_frame_prepend_unwinder (gdbarch,
238 &nios2_r2_linux_rt_sigreturn_tramp_frame);
239 else
240 tramp_frame_prepend_unwinder (gdbarch,
241 &nios2_r1_linux_rt_sigreturn_tramp_frame);
a1217d97
SL
242
243 tdep->syscall_next_pc = nios2_linux_syscall_next_pc;
f489207e 244 tdep->is_kernel_helper = nios2_linux_is_kernel_helper;
a1217d97
SL
245
246 /* Index of target address word in glibc jmp_buf. */
247 tdep->jb_pc = 10;
248}
249
a1217d97
SL
250void
251_initialize_nios2_linux_tdep (void)
252{
8d6dbeb4
SL
253
254 const struct bfd_arch_info *arch_info;
255
256 for (arch_info = bfd_lookup_arch (bfd_arch_nios2, 0);
257 arch_info != NULL;
258 arch_info = arch_info->next)
259 gdbarch_register_osabi (bfd_arch_nios2, arch_info->mach,
260 GDB_OSABI_LINUX, nios2_linux_init_abi);
a1217d97 261}
This page took 0.643933 seconds and 4 git commands to generate.