Commit | Line | Data |
---|---|---|
94a0e877 MG |
1 | /* Xtensa GNU/Linux native support. |
2 | ||
e2882c85 | 3 | Copyright (C) 2007-2018 Free Software Foundation, Inc. |
94a0e877 MG |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "defs.h" | |
94a0e877 MG |
21 | #include "frame.h" |
22 | #include "inferior.h" | |
23 | #include "gdbcore.h" | |
24 | #include "regcache.h" | |
94a0e877 MG |
25 | #include "target.h" |
26 | #include "linux-nat.h" | |
94a0e877 | 27 | #include <sys/types.h> |
94a0e877 MG |
28 | #include <signal.h> |
29 | #include <sys/user.h> | |
30 | #include <sys/ioctl.h> | |
31 | #include "gdb_wait.h" | |
32 | #include <fcntl.h> | |
33 | #include <sys/procfs.h> | |
5826e159 | 34 | #include "nat/gdb_ptrace.h" |
e671835b | 35 | #include <asm/ptrace.h> |
94a0e877 MG |
36 | |
37 | #include "gregset.h" | |
38 | #include "xtensa-tdep.h" | |
39 | ||
40045d91 MF |
40 | /* Defines ps_err_e, struct ps_prochandle. */ |
41 | #include "gdb_proc_service.h" | |
42 | ||
94a0e877 MG |
43 | /* Extended register set depends on hardware configs. |
44 | Keeping these definitions separately allows to introduce | |
45 | hardware-specific overlays. */ | |
46 | #include "xtensa-xtregs.c" | |
47 | ||
f6ac5f3d PA |
48 | class xtensa_linux_nat_target final : public linux_nat_target |
49 | { | |
50 | public: | |
51 | /* Add our register access methods. */ | |
52 | void fetch_registers (struct regcache *, int) override; | |
53 | void store_registers (struct regcache *, int) override; | |
54 | }; | |
55 | ||
56 | static xtensa_linux_nat_target the_xtensa_linux_nat_target; | |
57 | ||
94a0e877 MG |
58 | void |
59 | fill_gregset (const struct regcache *regcache, | |
60 | gdb_gregset_t *gregsetp, int regnum) | |
61 | { | |
62 | int i; | |
63 | xtensa_elf_gregset_t *regs = (xtensa_elf_gregset_t *) gregsetp; | |
ac7936df | 64 | struct gdbarch *gdbarch = regcache->arch (); |
94a0e877 MG |
65 | |
66 | if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1) | |
67 | regcache_raw_collect (regcache, gdbarch_pc_regnum (gdbarch), ®s->pc); | |
68 | if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1) | |
69 | regcache_raw_collect (regcache, gdbarch_ps_regnum (gdbarch), ®s->ps); | |
70 | ||
71 | if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1) | |
72 | regcache_raw_collect (regcache, | |
73 | gdbarch_tdep (gdbarch)->wb_regnum, | |
74 | ®s->windowbase); | |
75 | if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1) | |
76 | regcache_raw_collect (regcache, | |
77 | gdbarch_tdep (gdbarch)->ws_regnum, | |
78 | ®s->windowstart); | |
79 | if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1) | |
80 | regcache_raw_collect (regcache, | |
81 | gdbarch_tdep (gdbarch)->lbeg_regnum, | |
82 | ®s->lbeg); | |
83 | if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1) | |
84 | regcache_raw_collect (regcache, | |
85 | gdbarch_tdep (gdbarch)->lend_regnum, | |
86 | ®s->lend); | |
87 | if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1) | |
88 | regcache_raw_collect (regcache, | |
89 | gdbarch_tdep (gdbarch)->lcount_regnum, | |
90 | ®s->lcount); | |
91 | if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1) | |
92 | regcache_raw_collect (regcache, | |
93 | gdbarch_tdep (gdbarch)->sar_regnum, | |
94 | ®s->sar); | |
f74f865e MF |
95 | if (regnum == gdbarch_tdep (gdbarch)->threadptr_regnum || regnum == -1) |
96 | regcache_raw_collect (regcache, | |
97 | gdbarch_tdep (gdbarch)->threadptr_regnum, | |
98 | ®s->threadptr); | |
94a0e877 MG |
99 | if (regnum >=gdbarch_tdep (gdbarch)->ar_base |
100 | && regnum < gdbarch_tdep (gdbarch)->ar_base | |
101 | + gdbarch_tdep (gdbarch)->num_aregs) | |
102 | regcache_raw_collect (regcache,regnum, | |
103 | ®s->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]); | |
104 | else if (regnum == -1) | |
105 | { | |
106 | for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i) | |
107 | regcache_raw_collect (regcache, | |
108 | gdbarch_tdep (gdbarch)->ar_base + i, | |
109 | ®s->ar[i]); | |
110 | } | |
0ce4291e MF |
111 | if (regnum >= gdbarch_tdep (gdbarch)->a0_base |
112 | && regnum < gdbarch_tdep (gdbarch)->a0_base + C0_NREGS) | |
113 | regcache_raw_collect (regcache, regnum, | |
114 | ®s->ar[(4 * regs->windowbase + regnum | |
115 | - gdbarch_tdep (gdbarch)->a0_base) | |
116 | % gdbarch_tdep (gdbarch)->num_aregs]); | |
117 | else if (regnum == -1) | |
118 | { | |
119 | for (i = 0; i < C0_NREGS; ++i) | |
120 | regcache_raw_collect (regcache, | |
121 | gdbarch_tdep (gdbarch)->a0_base + i, | |
122 | ®s->ar[(4 * regs->windowbase + i) | |
123 | % gdbarch_tdep (gdbarch)->num_aregs]); | |
124 | } | |
94a0e877 MG |
125 | } |
126 | ||
1c215b97 | 127 | static void |
94a0e877 MG |
128 | supply_gregset_reg (struct regcache *regcache, |
129 | const gdb_gregset_t *gregsetp, int regnum) | |
130 | { | |
131 | int i; | |
132 | xtensa_elf_gregset_t *regs = (xtensa_elf_gregset_t *) gregsetp; | |
133 | ||
ac7936df | 134 | struct gdbarch *gdbarch = regcache->arch (); |
94a0e877 MG |
135 | |
136 | if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1) | |
73e1c03f | 137 | regcache->raw_supply (gdbarch_pc_regnum (gdbarch), ®s->pc); |
94a0e877 | 138 | if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1) |
73e1c03f | 139 | regcache->raw_supply (gdbarch_ps_regnum (gdbarch), ®s->ps); |
94a0e877 MG |
140 | |
141 | if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1) | |
73e1c03f | 142 | regcache->raw_supply (gdbarch_tdep (gdbarch)->wb_regnum, |
94a0e877 MG |
143 | ®s->windowbase); |
144 | if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1) | |
73e1c03f | 145 | regcache->raw_supply (gdbarch_tdep (gdbarch)->ws_regnum, |
94a0e877 MG |
146 | ®s->windowstart); |
147 | if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1) | |
73e1c03f | 148 | regcache->raw_supply (gdbarch_tdep (gdbarch)->lbeg_regnum, |
94a0e877 MG |
149 | ®s->lbeg); |
150 | if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1) | |
73e1c03f | 151 | regcache->raw_supply (gdbarch_tdep (gdbarch)->lend_regnum, |
94a0e877 MG |
152 | ®s->lend); |
153 | if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1) | |
73e1c03f | 154 | regcache->raw_supply (gdbarch_tdep (gdbarch)->lcount_regnum, |
94a0e877 MG |
155 | ®s->lcount); |
156 | if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1) | |
73e1c03f | 157 | regcache->raw_supply (gdbarch_tdep (gdbarch)->sar_regnum, |
94a0e877 | 158 | ®s->sar); |
f74f865e | 159 | if (regnum == gdbarch_tdep (gdbarch)->threadptr_regnum || regnum == -1) |
73e1c03f | 160 | regcache->raw_supply (gdbarch_tdep (gdbarch)->threadptr_regnum, |
f74f865e | 161 | ®s->threadptr); |
94a0e877 MG |
162 | if (regnum >=gdbarch_tdep (gdbarch)->ar_base |
163 | && regnum < gdbarch_tdep (gdbarch)->ar_base | |
164 | + gdbarch_tdep (gdbarch)->num_aregs) | |
73e1c03f | 165 | regcache->raw_supply (regnum, |
94a0e877 MG |
166 | ®s->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]); |
167 | else if (regnum == -1) | |
168 | { | |
169 | for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i) | |
73e1c03f | 170 | regcache->raw_supply (gdbarch_tdep (gdbarch)->ar_base + i, |
94a0e877 MG |
171 | ®s->ar[i]); |
172 | } | |
0ce4291e MF |
173 | if (regnum >= gdbarch_tdep (gdbarch)->a0_base |
174 | && regnum < gdbarch_tdep (gdbarch)->a0_base + C0_NREGS) | |
73e1c03f SM |
175 | regcache->raw_supply (regnum, |
176 | ®s->ar[(4 * regs->windowbase + regnum | |
177 | - gdbarch_tdep (gdbarch)->a0_base) | |
0ce4291e MF |
178 | % gdbarch_tdep (gdbarch)->num_aregs]); |
179 | else if (regnum == -1) | |
180 | { | |
181 | for (i = 0; i < C0_NREGS; ++i) | |
73e1c03f SM |
182 | regcache->raw_supply (gdbarch_tdep (gdbarch)->a0_base + i, |
183 | ®s->ar[(4 * regs->windowbase + i) | |
184 | % gdbarch_tdep (gdbarch)->num_aregs]); | |
0ce4291e | 185 | } |
94a0e877 MG |
186 | } |
187 | ||
188 | void | |
189 | supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp) | |
190 | { | |
191 | supply_gregset_reg (regcache, gregsetp, -1); | |
192 | } | |
193 | ||
194 | void | |
195 | fill_fpregset (const struct regcache *regcache, | |
196 | gdb_fpregset_t *fpregsetp, int regnum) | |
197 | { | |
198 | return; | |
199 | } | |
200 | ||
201 | void | |
202 | supply_fpregset (struct regcache *regcache, | |
203 | const gdb_fpregset_t *fpregsetp) | |
204 | { | |
205 | return; | |
206 | } | |
207 | ||
208 | /* Fetch greg-register(s) from process/thread TID | |
209 | and store value(s) in GDB's register array. */ | |
210 | ||
211 | static void | |
212 | fetch_gregs (struct regcache *regcache, int regnum) | |
213 | { | |
222312d3 | 214 | int tid = ptid_get_lwp (regcache->ptid ()); |
d274ecf4 | 215 | gdb_gregset_t regs; |
94a0e877 MG |
216 | int areg; |
217 | ||
218 | if (ptrace (PTRACE_GETREGS, tid, 0, (long) ®s) < 0) | |
219 | { | |
220 | perror_with_name (_("Couldn't get registers")); | |
221 | return; | |
222 | } | |
223 | ||
224 | supply_gregset_reg (regcache, ®s, regnum); | |
225 | } | |
226 | ||
227 | /* Store greg-register(s) in GDB's register | |
228 | array into the process/thread specified by TID. */ | |
229 | ||
230 | static void | |
231 | store_gregs (struct regcache *regcache, int regnum) | |
232 | { | |
222312d3 | 233 | int tid = ptid_get_lwp (regcache->ptid ()); |
94a0e877 MG |
234 | gdb_gregset_t regs; |
235 | int areg; | |
236 | ||
237 | if (ptrace (PTRACE_GETREGS, tid, 0, (long) ®s) < 0) | |
238 | { | |
239 | perror_with_name (_("Couldn't get registers")); | |
240 | return; | |
241 | } | |
242 | ||
243 | fill_gregset (regcache, ®s, regnum); | |
244 | ||
245 | if (ptrace (PTRACE_SETREGS, tid, 0, (long) ®s) < 0) | |
246 | { | |
247 | perror_with_name (_("Couldn't write registers")); | |
248 | return; | |
249 | } | |
250 | } | |
251 | ||
252 | static int xtreg_lo; | |
253 | static int xtreg_high; | |
254 | ||
255 | /* Fetch/Store Xtensa TIE registers. Xtensa GNU/Linux PTRACE | |
256 | interface provides special requests for this. */ | |
257 | ||
258 | static void | |
259 | fetch_xtregs (struct regcache *regcache, int regnum) | |
260 | { | |
222312d3 | 261 | int tid = ptid_get_lwp (regcache->ptid ()); |
94a0e877 MG |
262 | const xtensa_regtable_t *ptr; |
263 | char xtregs [XTENSA_ELF_XTREG_SIZE]; | |
264 | ||
265 | if (ptrace (PTRACE_GETXTREGS, tid, 0, (long)&xtregs) < 0) | |
266 | perror_with_name (_("Couldn't get extended registers")); | |
267 | ||
268 | for (ptr = xtensa_regmap_table; ptr->name; ptr++) | |
269 | if (regnum == ptr->gdb_regnum || regnum == -1) | |
73e1c03f | 270 | regcache->raw_supply (ptr->gdb_regnum, xtregs + ptr->ptrace_offset); |
94a0e877 MG |
271 | } |
272 | ||
273 | static void | |
274 | store_xtregs (struct regcache *regcache, int regnum) | |
275 | { | |
222312d3 | 276 | int tid = ptid_get_lwp (regcache->ptid ()); |
94a0e877 MG |
277 | const xtensa_regtable_t *ptr; |
278 | char xtregs [XTENSA_ELF_XTREG_SIZE]; | |
279 | ||
280 | if (ptrace (PTRACE_GETXTREGS, tid, 0, (long)&xtregs) < 0) | |
281 | perror_with_name (_("Couldn't get extended registers")); | |
282 | ||
283 | for (ptr = xtensa_regmap_table; ptr->name; ptr++) | |
284 | if (regnum == ptr->gdb_regnum || regnum == -1) | |
285 | regcache_raw_collect (regcache, ptr->gdb_regnum, | |
286 | xtregs + ptr->ptrace_offset); | |
287 | ||
288 | if (ptrace (PTRACE_SETXTREGS, tid, 0, (long)&xtregs) < 0) | |
289 | perror_with_name (_("Couldn't write extended registers")); | |
290 | } | |
291 | ||
f6ac5f3d PA |
292 | void |
293 | xtensa_linux_nat_target::fetch_registers (struct regcache *regcache, | |
294 | int regnum) | |
94a0e877 MG |
295 | { |
296 | if (regnum == -1) | |
297 | { | |
298 | fetch_gregs (regcache, regnum); | |
299 | fetch_xtregs (regcache, regnum); | |
300 | } | |
301 | else if ((regnum < xtreg_lo) || (regnum > xtreg_high)) | |
302 | fetch_gregs (regcache, regnum); | |
303 | else | |
304 | fetch_xtregs (regcache, regnum); | |
305 | } | |
306 | ||
f6ac5f3d PA |
307 | void |
308 | xtensa_linux_nat_target::store_registers (struct regcache *regcache, | |
309 | int regnum) | |
94a0e877 MG |
310 | { |
311 | if (regnum == -1) | |
312 | { | |
313 | store_gregs (regcache, regnum); | |
314 | store_xtregs (regcache, regnum); | |
315 | } | |
316 | else if ((regnum < xtreg_lo) || (regnum > xtreg_high)) | |
317 | store_gregs (regcache, regnum); | |
318 | else | |
319 | store_xtregs (regcache, regnum); | |
320 | } | |
321 | ||
40045d91 MF |
322 | /* Called by libthread_db. */ |
323 | ||
324 | ps_err_e | |
754653a7 | 325 | ps_get_thread_area (struct ps_prochandle *ph, |
40045d91 MF |
326 | lwpid_t lwpid, int idx, void **base) |
327 | { | |
328 | xtensa_elf_gregset_t regs; | |
329 | ||
330 | if (ptrace (PTRACE_GETREGS, lwpid, NULL, ®s) != 0) | |
331 | return PS_ERR; | |
332 | ||
333 | /* IDX is the bias from the thread pointer to the beginning of the | |
334 | thread descriptor. It has to be subtracted due to implementation | |
335 | quirks in libthread_db. */ | |
336 | *base = (void *) ((char *) regs.threadptr - idx); | |
337 | ||
338 | return PS_OK; | |
339 | } | |
340 | ||
94a0e877 MG |
341 | void |
342 | _initialize_xtensa_linux_nat (void) | |
343 | { | |
94a0e877 MG |
344 | const xtensa_regtable_t *ptr; |
345 | ||
346 | /* Calculate the number range for extended registers. */ | |
347 | xtreg_lo = 1000000000; | |
348 | xtreg_high = -1; | |
349 | for (ptr = xtensa_regmap_table; ptr->name; ptr++) | |
350 | { | |
351 | if (ptr->gdb_regnum < xtreg_lo) | |
352 | xtreg_lo = ptr->gdb_regnum; | |
353 | if (ptr->gdb_regnum > xtreg_high) | |
354 | xtreg_high = ptr->gdb_regnum; | |
355 | } | |
356 | ||
f6ac5f3d | 357 | linux_target = &the_xtensa_linux_nat_target; |
d9f719f1 | 358 | add_inf_child_target (&the_xtensa_linux_nat_target); |
94a0e877 | 359 | } |