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