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