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