[gdb/testsuite] Fix gdb.threads/watchpoint-fork.exp race
[deliverable/binutils-gdb.git] / gdb / xtensa-linux-nat.c
CommitLineData
94a0e877
MG
1/* Xtensa GNU/Linux native support.
2
b811d2c2 3 Copyright (C) 2007-2020 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>
268a13a5 31#include "gdbsupport/gdb_wait.h"
94a0e877
MG
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
48class xtensa_linux_nat_target final : public linux_nat_target
49{
50public:
51 /* Add our register access methods. */
52 void fetch_registers (struct regcache *, int) override;
53 void store_registers (struct regcache *, int) override;
54};
55
56static xtensa_linux_nat_target the_xtensa_linux_nat_target;
57
94a0e877
MG
58void
59fill_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)
34a79281 67 regcache->raw_collect (gdbarch_pc_regnum (gdbarch), &regs->pc);
94a0e877 68 if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
34a79281 69 regcache->raw_collect (gdbarch_ps_regnum (gdbarch), &regs->ps);
94a0e877
MG
70
71 if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
34a79281
SM
72 regcache->raw_collect (gdbarch_tdep (gdbarch)->wb_regnum,
73 &regs->windowbase);
94a0e877 74 if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
34a79281
SM
75 regcache->raw_collect (gdbarch_tdep (gdbarch)->ws_regnum,
76 &regs->windowstart);
94a0e877 77 if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
34a79281
SM
78 regcache->raw_collect (gdbarch_tdep (gdbarch)->lbeg_regnum,
79 &regs->lbeg);
94a0e877 80 if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
34a79281
SM
81 regcache->raw_collect (gdbarch_tdep (gdbarch)->lend_regnum,
82 &regs->lend);
94a0e877 83 if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
34a79281
SM
84 regcache->raw_collect (gdbarch_tdep (gdbarch)->lcount_regnum,
85 &regs->lcount);
94a0e877 86 if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
34a79281
SM
87 regcache->raw_collect (gdbarch_tdep (gdbarch)->sar_regnum,
88 &regs->sar);
f74f865e 89 if (regnum == gdbarch_tdep (gdbarch)->threadptr_regnum || regnum == -1)
34a79281
SM
90 regcache->raw_collect (gdbarch_tdep (gdbarch)->threadptr_regnum,
91 &regs->threadptr);
94a0e877
MG
92 if (regnum >=gdbarch_tdep (gdbarch)->ar_base
93 && regnum < gdbarch_tdep (gdbarch)->ar_base
94 + gdbarch_tdep (gdbarch)->num_aregs)
34a79281
SM
95 regcache->raw_collect (regnum,
96 &regs->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]);
94a0e877
MG
97 else if (regnum == -1)
98 {
99 for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
34a79281
SM
100 regcache->raw_collect (gdbarch_tdep (gdbarch)->ar_base + i,
101 &regs->ar[i]);
94a0e877 102 }
0ce4291e
MF
103 if (regnum >= gdbarch_tdep (gdbarch)->a0_base
104 && regnum < gdbarch_tdep (gdbarch)->a0_base + C0_NREGS)
34a79281
SM
105 regcache->raw_collect (regnum,
106 &regs->ar[(4 * regs->windowbase + regnum
107 - gdbarch_tdep (gdbarch)->a0_base)
0ce4291e
MF
108 % gdbarch_tdep (gdbarch)->num_aregs]);
109 else if (regnum == -1)
110 {
111 for (i = 0; i < C0_NREGS; ++i)
34a79281
SM
112 regcache->raw_collect (gdbarch_tdep (gdbarch)->a0_base + i,
113 (&regs->ar[(4 * regs->windowbase + i)
114 % gdbarch_tdep (gdbarch)->num_aregs]));
0ce4291e 115 }
94a0e877
MG
116}
117
1c215b97 118static void
94a0e877
MG
119supply_gregset_reg (struct regcache *regcache,
120 const gdb_gregset_t *gregsetp, int regnum)
121{
122 int i;
123 xtensa_elf_gregset_t *regs = (xtensa_elf_gregset_t *) gregsetp;
124
ac7936df 125 struct gdbarch *gdbarch = regcache->arch ();
94a0e877
MG
126
127 if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
73e1c03f 128 regcache->raw_supply (gdbarch_pc_regnum (gdbarch), &regs->pc);
94a0e877 129 if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
73e1c03f 130 regcache->raw_supply (gdbarch_ps_regnum (gdbarch), &regs->ps);
94a0e877
MG
131
132 if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
73e1c03f 133 regcache->raw_supply (gdbarch_tdep (gdbarch)->wb_regnum,
94a0e877
MG
134 &regs->windowbase);
135 if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
73e1c03f 136 regcache->raw_supply (gdbarch_tdep (gdbarch)->ws_regnum,
94a0e877
MG
137 &regs->windowstart);
138 if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
73e1c03f 139 regcache->raw_supply (gdbarch_tdep (gdbarch)->lbeg_regnum,
94a0e877
MG
140 &regs->lbeg);
141 if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
73e1c03f 142 regcache->raw_supply (gdbarch_tdep (gdbarch)->lend_regnum,
94a0e877
MG
143 &regs->lend);
144 if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
73e1c03f 145 regcache->raw_supply (gdbarch_tdep (gdbarch)->lcount_regnum,
94a0e877
MG
146 &regs->lcount);
147 if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
73e1c03f 148 regcache->raw_supply (gdbarch_tdep (gdbarch)->sar_regnum,
94a0e877 149 &regs->sar);
f74f865e 150 if (regnum == gdbarch_tdep (gdbarch)->threadptr_regnum || regnum == -1)
73e1c03f 151 regcache->raw_supply (gdbarch_tdep (gdbarch)->threadptr_regnum,
f74f865e 152 &regs->threadptr);
94a0e877
MG
153 if (regnum >=gdbarch_tdep (gdbarch)->ar_base
154 && regnum < gdbarch_tdep (gdbarch)->ar_base
155 + gdbarch_tdep (gdbarch)->num_aregs)
73e1c03f 156 regcache->raw_supply (regnum,
94a0e877
MG
157 &regs->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]);
158 else if (regnum == -1)
159 {
160 for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
73e1c03f 161 regcache->raw_supply (gdbarch_tdep (gdbarch)->ar_base + i,
94a0e877
MG
162 &regs->ar[i]);
163 }
0ce4291e
MF
164 if (regnum >= gdbarch_tdep (gdbarch)->a0_base
165 && regnum < gdbarch_tdep (gdbarch)->a0_base + C0_NREGS)
73e1c03f
SM
166 regcache->raw_supply (regnum,
167 &regs->ar[(4 * regs->windowbase + regnum
168 - gdbarch_tdep (gdbarch)->a0_base)
0ce4291e
MF
169 % gdbarch_tdep (gdbarch)->num_aregs]);
170 else if (regnum == -1)
171 {
172 for (i = 0; i < C0_NREGS; ++i)
73e1c03f
SM
173 regcache->raw_supply (gdbarch_tdep (gdbarch)->a0_base + i,
174 &regs->ar[(4 * regs->windowbase + i)
175 % gdbarch_tdep (gdbarch)->num_aregs]);
0ce4291e 176 }
94a0e877
MG
177}
178
179void
180supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
181{
182 supply_gregset_reg (regcache, gregsetp, -1);
183}
184
185void
186fill_fpregset (const struct regcache *regcache,
187 gdb_fpregset_t *fpregsetp, int regnum)
188{
189 return;
190}
191
192void
193supply_fpregset (struct regcache *regcache,
194 const gdb_fpregset_t *fpregsetp)
195{
196 return;
197}
198
199/* Fetch greg-register(s) from process/thread TID
200 and store value(s) in GDB's register array. */
201
202static void
203fetch_gregs (struct regcache *regcache, int regnum)
204{
e38504b3 205 int tid = regcache->ptid ().lwp ();
d274ecf4 206 gdb_gregset_t regs;
94a0e877
MG
207 int areg;
208
209 if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
210 {
211 perror_with_name (_("Couldn't get registers"));
212 return;
213 }
214
215 supply_gregset_reg (regcache, &regs, regnum);
216}
217
218/* Store greg-register(s) in GDB's register
219 array into the process/thread specified by TID. */
220
221static void
222store_gregs (struct regcache *regcache, int regnum)
223{
e38504b3 224 int tid = regcache->ptid ().lwp ();
94a0e877
MG
225 gdb_gregset_t regs;
226 int areg;
227
228 if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
229 {
230 perror_with_name (_("Couldn't get registers"));
231 return;
232 }
233
234 fill_gregset (regcache, &regs, regnum);
235
236 if (ptrace (PTRACE_SETREGS, tid, 0, (long) &regs) < 0)
237 {
238 perror_with_name (_("Couldn't write registers"));
239 return;
240 }
241}
242
243static int xtreg_lo;
244static int xtreg_high;
245
246/* Fetch/Store Xtensa TIE registers. Xtensa GNU/Linux PTRACE
247 interface provides special requests for this. */
248
249static void
250fetch_xtregs (struct regcache *regcache, int regnum)
251{
e38504b3 252 int tid = regcache->ptid ().lwp ();
94a0e877
MG
253 const xtensa_regtable_t *ptr;
254 char xtregs [XTENSA_ELF_XTREG_SIZE];
255
256 if (ptrace (PTRACE_GETXTREGS, tid, 0, (long)&xtregs) < 0)
257 perror_with_name (_("Couldn't get extended registers"));
258
259 for (ptr = xtensa_regmap_table; ptr->name; ptr++)
260 if (regnum == ptr->gdb_regnum || regnum == -1)
73e1c03f 261 regcache->raw_supply (ptr->gdb_regnum, xtregs + ptr->ptrace_offset);
94a0e877
MG
262}
263
264static void
265store_xtregs (struct regcache *regcache, int regnum)
266{
e38504b3 267 int tid = regcache->ptid ().lwp ();
94a0e877
MG
268 const xtensa_regtable_t *ptr;
269 char xtregs [XTENSA_ELF_XTREG_SIZE];
270
271 if (ptrace (PTRACE_GETXTREGS, tid, 0, (long)&xtregs) < 0)
272 perror_with_name (_("Couldn't get extended registers"));
273
274 for (ptr = xtensa_regmap_table; ptr->name; ptr++)
275 if (regnum == ptr->gdb_regnum || regnum == -1)
34a79281 276 regcache->raw_collect (ptr->gdb_regnum, xtregs + ptr->ptrace_offset);
94a0e877
MG
277
278 if (ptrace (PTRACE_SETXTREGS, tid, 0, (long)&xtregs) < 0)
279 perror_with_name (_("Couldn't write extended registers"));
280}
281
f6ac5f3d
PA
282void
283xtensa_linux_nat_target::fetch_registers (struct regcache *regcache,
284 int regnum)
94a0e877
MG
285{
286 if (regnum == -1)
287 {
288 fetch_gregs (regcache, regnum);
289 fetch_xtregs (regcache, regnum);
290 }
291 else if ((regnum < xtreg_lo) || (regnum > xtreg_high))
292 fetch_gregs (regcache, regnum);
293 else
294 fetch_xtregs (regcache, regnum);
295}
296
f6ac5f3d
PA
297void
298xtensa_linux_nat_target::store_registers (struct regcache *regcache,
299 int regnum)
94a0e877
MG
300{
301 if (regnum == -1)
302 {
303 store_gregs (regcache, regnum);
304 store_xtregs (regcache, regnum);
305 }
306 else if ((regnum < xtreg_lo) || (regnum > xtreg_high))
307 store_gregs (regcache, regnum);
308 else
309 store_xtregs (regcache, regnum);
310}
311
40045d91
MF
312/* Called by libthread_db. */
313
314ps_err_e
754653a7 315ps_get_thread_area (struct ps_prochandle *ph,
40045d91
MF
316 lwpid_t lwpid, int idx, void **base)
317{
318 xtensa_elf_gregset_t regs;
319
320 if (ptrace (PTRACE_GETREGS, lwpid, NULL, &regs) != 0)
321 return PS_ERR;
322
323 /* IDX is the bias from the thread pointer to the beginning of the
324 thread descriptor. It has to be subtracted due to implementation
325 quirks in libthread_db. */
326 *base = (void *) ((char *) regs.threadptr - idx);
327
328 return PS_OK;
329}
330
6c265988 331void _initialize_xtensa_linux_nat ();
94a0e877 332void
6c265988 333_initialize_xtensa_linux_nat ()
94a0e877 334{
94a0e877
MG
335 const xtensa_regtable_t *ptr;
336
337 /* Calculate the number range for extended registers. */
338 xtreg_lo = 1000000000;
339 xtreg_high = -1;
340 for (ptr = xtensa_regmap_table; ptr->name; ptr++)
341 {
342 if (ptr->gdb_regnum < xtreg_lo)
343 xtreg_lo = ptr->gdb_regnum;
344 if (ptr->gdb_regnum > xtreg_high)
345 xtreg_high = ptr->gdb_regnum;
346 }
347
f6ac5f3d 348 linux_target = &the_xtensa_linux_nat_target;
d9f719f1 349 add_inf_child_target (&the_xtensa_linux_nat_target);
94a0e877 350}
This page took 1.314834 seconds and 4 git commands to generate.