]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdbserver/linux-nios2-low.cc
gdb+gdbserver/Linux: Remove USE_SIGTRAP_SIGINFO fallback
[thirdparty/binutils-gdb.git] / gdbserver / linux-nios2-low.cc
CommitLineData
68f5f838
SL
1/* GNU/Linux/Nios II specific low level interface, for the remote server for
2 GDB.
1d506c26 3 Copyright (C) 2008-2024 Free Software Foundation, Inc.
68f5f838
SL
4
5 Contributed by Mentor Graphics, Inc.
6
7 This file is part of GDB.
8
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.
13
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.
18
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/>. */
21
68f5f838 22#include "linux-low.h"
21e94bd9 23#include "elf/common.h"
5826e159 24#include "nat/gdb_ptrace.h"
68f5f838
SL
25#include <endian.h>
26#include "gdb_proc_service.h"
27#include <asm/ptrace.h>
28
29#ifndef PTRACE_GET_THREAD_AREA
30#define PTRACE_GET_THREAD_AREA 25
31#endif
32
ef0478f6
TBA
33/* Linux target op definitions for the NIOS II architecture. */
34
35class nios2_target : public linux_process_target
36{
37public:
38
aa8d21c9
TBA
39 const regs_info *get_regs_info () override;
40
3ca4edb6
TBA
41 const gdb_byte *sw_breakpoint_from_kind (int kind, int *size) override;
42
797bcff5
TBA
43protected:
44
45 void low_arch_setup () override;
daca57a7
TBA
46
47 bool low_cannot_fetch_register (int regno) override;
48
49 bool low_cannot_store_register (int regno) override;
bf9ae9d8
TBA
50
51 bool low_supports_breakpoints () override;
52
53 CORE_ADDR low_get_pc (regcache *regcache) override;
54
55 void low_set_pc (regcache *regcache, CORE_ADDR newpc) override;
d7146cda
TBA
56
57 bool low_breakpoint_at (CORE_ADDR pc) override;
ef0478f6
TBA
58};
59
60/* The singleton target ops object. */
61
62static nios2_target the_nios2_target;
63
bf9ae9d8
TBA
64bool
65nios2_target::low_supports_breakpoints ()
66{
67 return true;
68}
69
70CORE_ADDR
71nios2_target::low_get_pc (regcache *regcache)
72{
73 return linux_get_pc_32bit (regcache);
74}
75
76void
77nios2_target::low_set_pc (regcache *regcache, CORE_ADDR pc)
78{
79 linux_set_pc_32bit (regcache, pc);
80}
81
68f5f838
SL
82/* The following definition must agree with the number of registers
83 defined in "struct user_regs" in GLIBC
21e94bd9 84 (sysdeps/unix/sysv/linux/nios2/sys/user.h), and also with
68f5f838
SL
85 NIOS2_NUM_REGS in GDB proper. */
86
87#define nios2_num_regs 49
88
89/* Defined in auto-generated file nios2-linux.c. */
90
91void init_registers_nios2_linux (void);
3aee8918 92extern const struct target_desc *tdesc_nios2_linux;
68f5f838
SL
93
94/* This union is used to convert between int and byte buffer
95 representations of register contents. */
96
97union nios2_register
98{
99 unsigned char buf[4];
100 int reg32;
101};
102
103/* Return the ptrace ``address'' of register REGNO. */
104
105static int nios2_regmap[] = {
106 -1, 1, 2, 3, 4, 5, 6, 7,
107 8, 9, 10, 11, 12, 13, 14, 15,
108 16, 17, 18, 19, 20, 21, 22, 23,
109 24, 25, 26, 27, 28, 29, 30, 31,
110 32, 33, 34, 35, 36, 37, 38, 39,
111 40, 41, 42, 43, 44, 45, 46, 47,
112 48,
113 0
114};
115
797bcff5 116/* Implement the low_arch_setup linux target ops method. */
68f5f838 117
797bcff5
TBA
118void
119nios2_target::low_arch_setup ()
68f5f838 120{
3aee8918 121 current_process ()->tdesc = tdesc_nios2_linux;
68f5f838
SL
122}
123
daca57a7 124/* Implement the low_cannot_fetch_register linux target ops method. */
68f5f838 125
daca57a7
TBA
126bool
127nios2_target::low_cannot_fetch_register (int regno)
68f5f838 128{
daca57a7 129 return (nios2_regmap[regno] == -1);
68f5f838
SL
130}
131
daca57a7 132/* Implement the low_cannot_store_register linux target ops method. */
68f5f838 133
daca57a7
TBA
134bool
135nios2_target::low_cannot_store_register (int regno)
68f5f838 136{
daca57a7 137 return (nios2_regmap[regno] == -1);
68f5f838
SL
138}
139
af60a1ef
SL
140/* Breakpoint support. Also see comments on nios2_breakpoint_from_pc
141 in nios2-tdep.c. */
142
143#if defined(__nios2_arch__) && __nios2_arch__ == 2
144#define NIOS2_BREAKPOINT 0xb7fd0020
145#define CDX_BREAKPOINT 0xd7c9
146#else
147#define NIOS2_BREAKPOINT 0x003b6ffa
148#endif
68f5f838 149
dd373349
AT
150/* We only register the 4-byte breakpoint, even on R2 targets which also
151 support 2-byte breakpoints. Since there is no supports_z_point_type
152 function provided, gdbserver never inserts software breakpoints itself
153 and instead relies on GDB to insert the breakpoint of the correct length
154 via a memory write. */
af60a1ef 155static const unsigned int nios2_breakpoint = NIOS2_BREAKPOINT;
68f5f838
SL
156#define nios2_breakpoint_len 4
157
3ca4edb6 158/* Implementation of target ops method "sw_breakpoint_from_kind". */
dd373349 159
3ca4edb6
TBA
160const gdb_byte *
161nios2_target::sw_breakpoint_from_kind (int kind, int *size)
dd373349
AT
162{
163 *size = nios2_breakpoint_len;
164 return (const gdb_byte *) &nios2_breakpoint;
165}
166
d7146cda 167/* Implement the low_breakpoint_at linux target ops method. */
68f5f838 168
d7146cda
TBA
169bool
170nios2_target::low_breakpoint_at (CORE_ADDR where)
68f5f838
SL
171{
172 unsigned int insn;
173
af60a1ef
SL
174 /* For R2, first check for the 2-byte CDX trap.n breakpoint encoding. */
175#if defined(__nios2_arch__) && __nios2_arch__ == 2
d7146cda 176 read_memory (where, (unsigned char *) &insn, 2);
af60a1ef 177 if (insn == CDX_BREAKPOINT)
d7146cda 178 return true;
af60a1ef
SL
179#endif
180
d7146cda 181 read_memory (where, (unsigned char *) &insn, 4);
68f5f838 182 if (insn == nios2_breakpoint)
d7146cda
TBA
183 return true;
184 return false;
68f5f838
SL
185}
186
187/* Fetch the thread-local storage pointer for libthread_db. */
188
189ps_err_e
754653a7 190ps_get_thread_area (struct ps_prochandle *ph,
dda83cd7 191 lwpid_t lwpid, int idx, void **base)
68f5f838
SL
192{
193 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
194 return PS_ERR;
195
196 /* IDX is the bias from the thread pointer to the beginning of the
197 thread descriptor. It has to be subtracted due to implementation
198 quirks in libthread_db. */
199 *base = (void *) ((char *) *base - idx);
200
201 return PS_OK;
202}
203
68f5f838
SL
204/* Helper functions to collect/supply a single register REGNO. */
205
206static void
207nios2_collect_register (struct regcache *regcache, int regno,
208 union nios2_register *reg)
209{
210 union nios2_register tmp_reg;
211
212 collect_register (regcache, regno, &tmp_reg.reg32);
213 reg->reg32 = tmp_reg.reg32;
214}
215
216static void
217nios2_supply_register (struct regcache *regcache, int regno,
218 const union nios2_register *reg)
219{
220 supply_register (regcache, regno, reg->buf);
221}
222
223/* We have only a single register set on Nios II. */
224
225static void
226nios2_fill_gregset (struct regcache *regcache, void *buf)
227{
b1c51e36 228 union nios2_register *regset = (union nios2_register *) buf;
68f5f838
SL
229 int i;
230
231 for (i = 1; i < nios2_num_regs; i++)
232 nios2_collect_register (regcache, i, regset + i);
233}
234
235static void
236nios2_store_gregset (struct regcache *regcache, const void *buf)
237{
b1c51e36 238 const union nios2_register *regset = (union nios2_register *) buf;
68f5f838
SL
239 int i;
240
241 for (i = 0; i < nios2_num_regs; i++)
242 nios2_supply_register (regcache, i, regset + i);
243}
68f5f838 244
3aee8918 245static struct regset_info nios2_regsets[] =
68f5f838 246{
21e94bd9
SL
247 { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PRSTATUS,
248 nios2_num_regs * 4, GENERAL_REGS,
68f5f838 249 nios2_fill_gregset, nios2_store_gregset },
50bc912a 250 NULL_REGSET
68f5f838
SL
251};
252
3aee8918
PA
253static struct regsets_info nios2_regsets_info =
254 {
255 nios2_regsets, /* regsets */
256 0, /* num_regsets */
257 NULL, /* disabled_regsets */
258 };
259
260static struct usrregs_info nios2_usrregs_info =
261 {
262 nios2_num_regs,
263 nios2_regmap,
264 };
265
aa8d21c9 266static struct regs_info myregs_info =
3aee8918
PA
267 {
268 NULL, /* regset_bitmap */
269 &nios2_usrregs_info,
270 &nios2_regsets_info
271 };
272
aa8d21c9
TBA
273const regs_info *
274nios2_target::get_regs_info ()
3aee8918 275{
aa8d21c9 276 return &myregs_info;
3aee8918
PA
277}
278
ef0478f6
TBA
279/* The linux target ops object. */
280
281linux_process_target *the_linux_target = &the_nios2_target;
282
3aee8918
PA
283void
284initialize_low_arch (void)
285{
286 init_registers_nios2_linux ();
287
288 initialize_regsets_info (&nios2_regsets_info);
289}