8737a9a4acfdc14cafa98aabb3c08ff9de81f8d4
[deliverable/binutils-gdb.git] / gdb / xtensa-linux-nat.c
1 /* Xtensa GNU/Linux native support.
2
3 Copyright (C) 2007-2018 Free Software Foundation, Inc.
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"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "gdbcore.h"
24 #include "regcache.h"
25 #include "target.h"
26 #include "linux-nat.h"
27 #include <sys/types.h>
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>
34 #include "nat/gdb_ptrace.h"
35 #include <asm/ptrace.h>
36
37 #include "gregset.h"
38 #include "xtensa-tdep.h"
39
40 /* Defines ps_err_e, struct ps_prochandle. */
41 #include "gdb_proc_service.h"
42
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
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
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;
64 struct gdbarch *gdbarch = regcache->arch ();
65
66 if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
67 regcache_raw_collect (regcache, gdbarch_pc_regnum (gdbarch), &regs->pc);
68 if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
69 regcache_raw_collect (regcache, gdbarch_ps_regnum (gdbarch), &regs->ps);
70
71 if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
72 regcache_raw_collect (regcache,
73 gdbarch_tdep (gdbarch)->wb_regnum,
74 &regs->windowbase);
75 if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
76 regcache_raw_collect (regcache,
77 gdbarch_tdep (gdbarch)->ws_regnum,
78 &regs->windowstart);
79 if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
80 regcache_raw_collect (regcache,
81 gdbarch_tdep (gdbarch)->lbeg_regnum,
82 &regs->lbeg);
83 if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
84 regcache_raw_collect (regcache,
85 gdbarch_tdep (gdbarch)->lend_regnum,
86 &regs->lend);
87 if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
88 regcache_raw_collect (regcache,
89 gdbarch_tdep (gdbarch)->lcount_regnum,
90 &regs->lcount);
91 if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
92 regcache_raw_collect (regcache,
93 gdbarch_tdep (gdbarch)->sar_regnum,
94 &regs->sar);
95 if (regnum == gdbarch_tdep (gdbarch)->threadptr_regnum || regnum == -1)
96 regcache_raw_collect (regcache,
97 gdbarch_tdep (gdbarch)->threadptr_regnum,
98 &regs->threadptr);
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 &regs->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 &regs->ar[i]);
110 }
111 if (regnum >= gdbarch_tdep (gdbarch)->a0_base
112 && regnum < gdbarch_tdep (gdbarch)->a0_base + C0_NREGS)
113 regcache_raw_collect (regcache, regnum,
114 &regs->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 &regs->ar[(4 * regs->windowbase + i)
123 % gdbarch_tdep (gdbarch)->num_aregs]);
124 }
125 }
126
127 static void
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
134 struct gdbarch *gdbarch = regcache->arch ();
135
136 if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
137 regcache->raw_supply (gdbarch_pc_regnum (gdbarch), &regs->pc);
138 if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
139 regcache->raw_supply (gdbarch_ps_regnum (gdbarch), &regs->ps);
140
141 if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
142 regcache->raw_supply (gdbarch_tdep (gdbarch)->wb_regnum,
143 &regs->windowbase);
144 if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
145 regcache->raw_supply (gdbarch_tdep (gdbarch)->ws_regnum,
146 &regs->windowstart);
147 if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
148 regcache->raw_supply (gdbarch_tdep (gdbarch)->lbeg_regnum,
149 &regs->lbeg);
150 if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
151 regcache->raw_supply (gdbarch_tdep (gdbarch)->lend_regnum,
152 &regs->lend);
153 if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
154 regcache->raw_supply (gdbarch_tdep (gdbarch)->lcount_regnum,
155 &regs->lcount);
156 if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
157 regcache->raw_supply (gdbarch_tdep (gdbarch)->sar_regnum,
158 &regs->sar);
159 if (regnum == gdbarch_tdep (gdbarch)->threadptr_regnum || regnum == -1)
160 regcache->raw_supply (gdbarch_tdep (gdbarch)->threadptr_regnum,
161 &regs->threadptr);
162 if (regnum >=gdbarch_tdep (gdbarch)->ar_base
163 && regnum < gdbarch_tdep (gdbarch)->ar_base
164 + gdbarch_tdep (gdbarch)->num_aregs)
165 regcache->raw_supply (regnum,
166 &regs->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]);
167 else if (regnum == -1)
168 {
169 for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
170 regcache->raw_supply (gdbarch_tdep (gdbarch)->ar_base + i,
171 &regs->ar[i]);
172 }
173 if (regnum >= gdbarch_tdep (gdbarch)->a0_base
174 && regnum < gdbarch_tdep (gdbarch)->a0_base + C0_NREGS)
175 regcache->raw_supply (regnum,
176 &regs->ar[(4 * regs->windowbase + regnum
177 - gdbarch_tdep (gdbarch)->a0_base)
178 % gdbarch_tdep (gdbarch)->num_aregs]);
179 else if (regnum == -1)
180 {
181 for (i = 0; i < C0_NREGS; ++i)
182 regcache->raw_supply (gdbarch_tdep (gdbarch)->a0_base + i,
183 &regs->ar[(4 * regs->windowbase + i)
184 % gdbarch_tdep (gdbarch)->num_aregs]);
185 }
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 {
214 int tid = ptid_get_lwp (regcache->ptid ());
215 gdb_gregset_t regs;
216 int areg;
217
218 if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
219 {
220 perror_with_name (_("Couldn't get registers"));
221 return;
222 }
223
224 supply_gregset_reg (regcache, &regs, 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 {
233 int tid = ptid_get_lwp (regcache->ptid ());
234 gdb_gregset_t regs;
235 int areg;
236
237 if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
238 {
239 perror_with_name (_("Couldn't get registers"));
240 return;
241 }
242
243 fill_gregset (regcache, &regs, regnum);
244
245 if (ptrace (PTRACE_SETREGS, tid, 0, (long) &regs) < 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 {
261 int tid = ptid_get_lwp (regcache->ptid ());
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)
270 regcache->raw_supply (ptr->gdb_regnum, xtregs + ptr->ptrace_offset);
271 }
272
273 static void
274 store_xtregs (struct regcache *regcache, int regnum)
275 {
276 int tid = ptid_get_lwp (regcache->ptid ());
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
292 void
293 xtensa_linux_nat_target::fetch_registers (struct regcache *regcache,
294 int regnum)
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
307 void
308 xtensa_linux_nat_target::store_registers (struct regcache *regcache,
309 int regnum)
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
322 /* Called by libthread_db. */
323
324 ps_err_e
325 ps_get_thread_area (struct ps_prochandle *ph,
326 lwpid_t lwpid, int idx, void **base)
327 {
328 xtensa_elf_gregset_t regs;
329
330 if (ptrace (PTRACE_GETREGS, lwpid, NULL, &regs) != 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
341 void
342 _initialize_xtensa_linux_nat (void)
343 {
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
357 linux_target = &the_xtensa_linux_nat_target;
358 add_inf_child_target (&the_xtensa_linux_nat_target);
359 }
This page took 0.043143 seconds and 3 git commands to generate.