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