1 /* Xtensa GNU/Linux native support.
3 Copyright (C) 2007-2017 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
26 #include "linux-nat.h"
27 #include <sys/types.h>
30 #include <sys/ioctl.h>
33 #include <sys/procfs.h>
34 #include "nat/gdb_ptrace.h"
35 #include <asm/ptrace.h>
38 #include "xtensa-tdep.h"
40 /* Defines ps_err_e, struct ps_prochandle. */
41 #include "gdb_proc_service.h"
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"
49 fill_gregset (const struct regcache *regcache,
50 gdb_gregset_t *gregsetp, int regnum)
53 xtensa_elf_gregset_t *regs = (xtensa_elf_gregset_t *) gregsetp;
54 struct gdbarch *gdbarch = get_regcache_arch (regcache);
56 if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
57 regcache_raw_collect (regcache, gdbarch_pc_regnum (gdbarch), ®s->pc);
58 if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
59 regcache_raw_collect (regcache, gdbarch_ps_regnum (gdbarch), ®s->ps);
61 if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
62 regcache_raw_collect (regcache,
63 gdbarch_tdep (gdbarch)->wb_regnum,
65 if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
66 regcache_raw_collect (regcache,
67 gdbarch_tdep (gdbarch)->ws_regnum,
69 if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
70 regcache_raw_collect (regcache,
71 gdbarch_tdep (gdbarch)->lbeg_regnum,
73 if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
74 regcache_raw_collect (regcache,
75 gdbarch_tdep (gdbarch)->lend_regnum,
77 if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
78 regcache_raw_collect (regcache,
79 gdbarch_tdep (gdbarch)->lcount_regnum,
81 if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
82 regcache_raw_collect (regcache,
83 gdbarch_tdep (gdbarch)->sar_regnum,
85 if (regnum == gdbarch_tdep (gdbarch)->threadptr_regnum || regnum == -1)
86 regcache_raw_collect (regcache,
87 gdbarch_tdep (gdbarch)->threadptr_regnum,
89 if (regnum >=gdbarch_tdep (gdbarch)->ar_base
90 && regnum < gdbarch_tdep (gdbarch)->ar_base
91 + gdbarch_tdep (gdbarch)->num_aregs)
92 regcache_raw_collect (regcache,regnum,
93 ®s->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]);
94 else if (regnum == -1)
96 for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
97 regcache_raw_collect (regcache,
98 gdbarch_tdep (gdbarch)->ar_base + i,
101 if (regnum >= gdbarch_tdep (gdbarch)->a0_base
102 && regnum < gdbarch_tdep (gdbarch)->a0_base + C0_NREGS)
103 regcache_raw_collect (regcache, regnum,
104 ®s->ar[(4 * regs->windowbase + regnum
105 - gdbarch_tdep (gdbarch)->a0_base)
106 % gdbarch_tdep (gdbarch)->num_aregs]);
107 else if (regnum == -1)
109 for (i = 0; i < C0_NREGS; ++i)
110 regcache_raw_collect (regcache,
111 gdbarch_tdep (gdbarch)->a0_base + i,
112 ®s->ar[(4 * regs->windowbase + i)
113 % gdbarch_tdep (gdbarch)->num_aregs]);
118 supply_gregset_reg (struct regcache *regcache,
119 const gdb_gregset_t *gregsetp, int regnum)
122 xtensa_elf_gregset_t *regs = (xtensa_elf_gregset_t *) gregsetp;
124 struct gdbarch *gdbarch = get_regcache_arch (regcache);
126 if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
127 regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch), ®s->pc);
128 if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
129 regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch), ®s->ps);
131 if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
132 regcache_raw_supply (regcache,
133 gdbarch_tdep (gdbarch)->wb_regnum,
135 if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
136 regcache_raw_supply (regcache,
137 gdbarch_tdep (gdbarch)->ws_regnum,
139 if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
140 regcache_raw_supply (regcache,
141 gdbarch_tdep (gdbarch)->lbeg_regnum,
143 if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
144 regcache_raw_supply (regcache,
145 gdbarch_tdep (gdbarch)->lend_regnum,
147 if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
148 regcache_raw_supply (regcache,
149 gdbarch_tdep (gdbarch)->lcount_regnum,
151 if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
152 regcache_raw_supply (regcache,
153 gdbarch_tdep (gdbarch)->sar_regnum,
155 if (regnum == gdbarch_tdep (gdbarch)->threadptr_regnum || regnum == -1)
156 regcache_raw_supply (regcache,
157 gdbarch_tdep (gdbarch)->threadptr_regnum,
159 if (regnum >=gdbarch_tdep (gdbarch)->ar_base
160 && regnum < gdbarch_tdep (gdbarch)->ar_base
161 + gdbarch_tdep (gdbarch)->num_aregs)
162 regcache_raw_supply (regcache,regnum,
163 ®s->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]);
164 else if (regnum == -1)
166 for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
167 regcache_raw_supply (regcache,
168 gdbarch_tdep (gdbarch)->ar_base + i,
171 if (regnum >= gdbarch_tdep (gdbarch)->a0_base
172 && regnum < gdbarch_tdep (gdbarch)->a0_base + C0_NREGS)
173 regcache_raw_supply (regcache, regnum,
174 ®s->ar[(4 * regs->windowbase + regnum
175 - gdbarch_tdep (gdbarch)->a0_base)
176 % gdbarch_tdep (gdbarch)->num_aregs]);
177 else if (regnum == -1)
179 for (i = 0; i < C0_NREGS; ++i)
180 regcache_raw_supply (regcache,
181 gdbarch_tdep (gdbarch)->a0_base + i,
182 ®s->ar[(4 * regs->windowbase + i)
183 % gdbarch_tdep (gdbarch)->num_aregs]);
188 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
190 supply_gregset_reg (regcache, gregsetp, -1);
194 fill_fpregset (const struct regcache *regcache,
195 gdb_fpregset_t *fpregsetp, int regnum)
201 supply_fpregset (struct regcache *regcache,
202 const gdb_fpregset_t *fpregsetp)
207 /* Fetch greg-register(s) from process/thread TID
208 and store value(s) in GDB's register array. */
211 fetch_gregs (struct regcache *regcache, int regnum)
213 int tid = ptid_get_lwp (regcache_get_ptid (regcache));
217 if (ptrace (PTRACE_GETREGS, tid, 0, (long) ®s) < 0)
219 perror_with_name (_("Couldn't get registers"));
223 supply_gregset_reg (regcache, ®s, regnum);
226 /* Store greg-register(s) in GDB's register
227 array into the process/thread specified by TID. */
230 store_gregs (struct regcache *regcache, int regnum)
232 int tid = ptid_get_lwp (regcache_get_ptid (regcache));
236 if (ptrace (PTRACE_GETREGS, tid, 0, (long) ®s) < 0)
238 perror_with_name (_("Couldn't get registers"));
242 fill_gregset (regcache, ®s, regnum);
244 if (ptrace (PTRACE_SETREGS, tid, 0, (long) ®s) < 0)
246 perror_with_name (_("Couldn't write registers"));
252 static int xtreg_high;
254 /* Fetch/Store Xtensa TIE registers. Xtensa GNU/Linux PTRACE
255 interface provides special requests for this. */
258 fetch_xtregs (struct regcache *regcache, int regnum)
260 int tid = ptid_get_lwp (regcache_get_ptid (regcache));
261 const xtensa_regtable_t *ptr;
262 char xtregs [XTENSA_ELF_XTREG_SIZE];
264 if (ptrace (PTRACE_GETXTREGS, tid, 0, (long)&xtregs) < 0)
265 perror_with_name (_("Couldn't get extended registers"));
267 for (ptr = xtensa_regmap_table; ptr->name; ptr++)
268 if (regnum == ptr->gdb_regnum || regnum == -1)
269 regcache_raw_supply (regcache, ptr->gdb_regnum,
270 xtregs + ptr->ptrace_offset);
274 store_xtregs (struct regcache *regcache, int regnum)
276 int tid = ptid_get_lwp (regcache_get_ptid (regcache));
277 const xtensa_regtable_t *ptr;
278 char xtregs [XTENSA_ELF_XTREG_SIZE];
280 if (ptrace (PTRACE_GETXTREGS, tid, 0, (long)&xtregs) < 0)
281 perror_with_name (_("Couldn't get extended registers"));
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);
288 if (ptrace (PTRACE_SETXTREGS, tid, 0, (long)&xtregs) < 0)
289 perror_with_name (_("Couldn't write extended registers"));
293 xtensa_linux_fetch_inferior_registers (struct target_ops *ops,
294 struct regcache *regcache, int regnum)
298 fetch_gregs (regcache, regnum);
299 fetch_xtregs (regcache, regnum);
301 else if ((regnum < xtreg_lo) || (regnum > xtreg_high))
302 fetch_gregs (regcache, regnum);
304 fetch_xtregs (regcache, regnum);
308 xtensa_linux_store_inferior_registers (struct target_ops *ops,
309 struct regcache *regcache, int regnum)
313 store_gregs (regcache, regnum);
314 store_xtregs (regcache, regnum);
316 else if ((regnum < xtreg_lo) || (regnum > xtreg_high))
317 store_gregs (regcache, regnum);
319 store_xtregs (regcache, regnum);
322 /* Called by libthread_db. */
325 ps_get_thread_area (struct ps_prochandle *ph,
326 lwpid_t lwpid, int idx, void **base)
328 xtensa_elf_gregset_t regs;
330 if (ptrace (PTRACE_GETREGS, lwpid, NULL, ®s) != 0)
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);
341 void _initialize_xtensa_linux_nat (void);
344 _initialize_xtensa_linux_nat (void)
346 struct target_ops *t;
347 const xtensa_regtable_t *ptr;
349 /* Calculate the number range for extended registers. */
350 xtreg_lo = 1000000000;
352 for (ptr = xtensa_regmap_table; ptr->name; ptr++)
354 if (ptr->gdb_regnum < xtreg_lo)
355 xtreg_lo = ptr->gdb_regnum;
356 if (ptr->gdb_regnum > xtreg_high)
357 xtreg_high = ptr->gdb_regnum;
360 /* Fill in the generic GNU/Linux methods. */
363 /* Add our register access methods. */
364 t->to_fetch_registers = xtensa_linux_fetch_inferior_registers;
365 t->to_store_registers = xtensa_linux_store_inferior_registers;
367 linux_nat_add_target (t);