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