Remove regcache_raw_supply
[deliverable/binutils-gdb.git] / gdb / xtensa-linux-nat.c
CommitLineData
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
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)
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);
f74f865e
MF
95 if (regnum == gdbarch_tdep (gdbarch)->threadptr_regnum || regnum == -1)
96 regcache_raw_collect (regcache,
97 gdbarch_tdep (gdbarch)->threadptr_regnum,
98 &regs->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 &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 }
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 &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 }
94a0e877
MG
125}
126
1c215b97 127static void
94a0e877
MG
128supply_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), &regs->pc);
94a0e877 138 if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
73e1c03f 139 regcache->raw_supply (gdbarch_ps_regnum (gdbarch), &regs->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 &regs->windowbase);
144 if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
73e1c03f 145 regcache->raw_supply (gdbarch_tdep (gdbarch)->ws_regnum,
94a0e877
MG
146 &regs->windowstart);
147 if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
73e1c03f 148 regcache->raw_supply (gdbarch_tdep (gdbarch)->lbeg_regnum,
94a0e877
MG
149 &regs->lbeg);
150 if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
73e1c03f 151 regcache->raw_supply (gdbarch_tdep (gdbarch)->lend_regnum,
94a0e877
MG
152 &regs->lend);
153 if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
73e1c03f 154 regcache->raw_supply (gdbarch_tdep (gdbarch)->lcount_regnum,
94a0e877
MG
155 &regs->lcount);
156 if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
73e1c03f 157 regcache->raw_supply (gdbarch_tdep (gdbarch)->sar_regnum,
94a0e877 158 &regs->sar);
f74f865e 159 if (regnum == gdbarch_tdep (gdbarch)->threadptr_regnum || regnum == -1)
73e1c03f 160 regcache->raw_supply (gdbarch_tdep (gdbarch)->threadptr_regnum,
f74f865e 161 &regs->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 &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)
73e1c03f 170 regcache->raw_supply (gdbarch_tdep (gdbarch)->ar_base + i,
94a0e877
MG
171 &regs->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 &regs->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 &regs->ar[(4 * regs->windowbase + i)
184 % gdbarch_tdep (gdbarch)->num_aregs]);
0ce4291e 185 }
94a0e877
MG
186}
187
188void
189supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
190{
191 supply_gregset_reg (regcache, gregsetp, -1);
192}
193
194void
195fill_fpregset (const struct regcache *regcache,
196 gdb_fpregset_t *fpregsetp, int regnum)
197{
198 return;
199}
200
201void
202supply_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
211static void
212fetch_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) &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
230static void
231store_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) &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
252static int xtreg_lo;
253static int xtreg_high;
254
255/* Fetch/Store Xtensa TIE registers. Xtensa GNU/Linux PTRACE
256 interface provides special requests for this. */
257
258static void
259fetch_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
273static void
274store_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
292void
293xtensa_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
307void
308xtensa_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
324ps_err_e
754653a7 325ps_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, &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
94a0e877
MG
341void
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}
This page took 1.1205 seconds and 4 git commands to generate.