1 /* Xtensa GNU/Linux native support.
3 Copyright (C) 2007-2015 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 /* Extended register set depends on hardware configs.
41 Keeping these definitions separately allows to introduce
42 hardware-specific overlays. */
43 #include "xtensa-xtregs.c"
46 fill_gregset (const struct regcache *regcache,
47 gdb_gregset_t *gregsetp, int regnum)
50 xtensa_elf_gregset_t *regs = (xtensa_elf_gregset_t *) gregsetp;
51 struct gdbarch *gdbarch = get_regcache_arch (regcache);
53 if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
54 regcache_raw_collect (regcache, gdbarch_pc_regnum (gdbarch), ®s->pc);
55 if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
56 regcache_raw_collect (regcache, gdbarch_ps_regnum (gdbarch), ®s->ps);
58 if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
59 regcache_raw_collect (regcache,
60 gdbarch_tdep (gdbarch)->wb_regnum,
62 if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
63 regcache_raw_collect (regcache,
64 gdbarch_tdep (gdbarch)->ws_regnum,
66 if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
67 regcache_raw_collect (regcache,
68 gdbarch_tdep (gdbarch)->lbeg_regnum,
70 if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
71 regcache_raw_collect (regcache,
72 gdbarch_tdep (gdbarch)->lend_regnum,
74 if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
75 regcache_raw_collect (regcache,
76 gdbarch_tdep (gdbarch)->lcount_regnum,
78 if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
79 regcache_raw_collect (regcache,
80 gdbarch_tdep (gdbarch)->sar_regnum,
82 if (regnum >=gdbarch_tdep (gdbarch)->ar_base
83 && regnum < gdbarch_tdep (gdbarch)->ar_base
84 + gdbarch_tdep (gdbarch)->num_aregs)
85 regcache_raw_collect (regcache,regnum,
86 ®s->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]);
87 else if (regnum == -1)
89 for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
90 regcache_raw_collect (regcache,
91 gdbarch_tdep (gdbarch)->ar_base + i,
97 supply_gregset_reg (struct regcache *regcache,
98 const gdb_gregset_t *gregsetp, int regnum)
101 xtensa_elf_gregset_t *regs = (xtensa_elf_gregset_t *) gregsetp;
103 struct gdbarch *gdbarch = get_regcache_arch (regcache);
105 if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
106 regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch), ®s->pc);
107 if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
108 regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch), ®s->ps);
110 if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
111 regcache_raw_supply (regcache,
112 gdbarch_tdep (gdbarch)->wb_regnum,
114 if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
115 regcache_raw_supply (regcache,
116 gdbarch_tdep (gdbarch)->ws_regnum,
118 if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
119 regcache_raw_supply (regcache,
120 gdbarch_tdep (gdbarch)->lbeg_regnum,
122 if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
123 regcache_raw_supply (regcache,
124 gdbarch_tdep (gdbarch)->lend_regnum,
126 if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
127 regcache_raw_supply (regcache,
128 gdbarch_tdep (gdbarch)->lcount_regnum,
130 if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
131 regcache_raw_supply (regcache,
132 gdbarch_tdep (gdbarch)->sar_regnum,
134 if (regnum >=gdbarch_tdep (gdbarch)->ar_base
135 && regnum < gdbarch_tdep (gdbarch)->ar_base
136 + gdbarch_tdep (gdbarch)->num_aregs)
137 regcache_raw_supply (regcache,regnum,
138 ®s->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]);
139 else if (regnum == -1)
141 for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
142 regcache_raw_supply (regcache,
143 gdbarch_tdep (gdbarch)->ar_base + i,
149 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
151 supply_gregset_reg (regcache, gregsetp, -1);
155 fill_fpregset (const struct regcache *regcache,
156 gdb_fpregset_t *fpregsetp, int regnum)
162 supply_fpregset (struct regcache *regcache,
163 const gdb_fpregset_t *fpregsetp)
168 /* Fetch greg-register(s) from process/thread TID
169 and store value(s) in GDB's register array. */
172 fetch_gregs (struct regcache *regcache, int regnum)
174 int tid = ptid_get_lwp (inferior_ptid);
175 const gdb_gregset_t regs;
178 if (ptrace (PTRACE_GETREGS, tid, 0, (long) ®s) < 0)
180 perror_with_name (_("Couldn't get registers"));
184 supply_gregset_reg (regcache, ®s, regnum);
187 /* Store greg-register(s) in GDB's register
188 array into the process/thread specified by TID. */
191 store_gregs (struct regcache *regcache, int regnum)
193 int tid = ptid_get_lwp (inferior_ptid);
197 if (ptrace (PTRACE_GETREGS, tid, 0, (long) ®s) < 0)
199 perror_with_name (_("Couldn't get registers"));
203 fill_gregset (regcache, ®s, regnum);
205 if (ptrace (PTRACE_SETREGS, tid, 0, (long) ®s) < 0)
207 perror_with_name (_("Couldn't write registers"));
213 static int xtreg_high;
215 /* Fetch/Store Xtensa TIE registers. Xtensa GNU/Linux PTRACE
216 interface provides special requests for this. */
219 fetch_xtregs (struct regcache *regcache, int regnum)
221 int tid = ptid_get_lwp (inferior_ptid);
222 const xtensa_regtable_t *ptr;
223 char xtregs [XTENSA_ELF_XTREG_SIZE];
225 if (ptrace (PTRACE_GETXTREGS, tid, 0, (long)&xtregs) < 0)
226 perror_with_name (_("Couldn't get extended registers"));
228 for (ptr = xtensa_regmap_table; ptr->name; ptr++)
229 if (regnum == ptr->gdb_regnum || regnum == -1)
230 regcache_raw_supply (regcache, ptr->gdb_regnum,
231 xtregs + ptr->ptrace_offset);
235 store_xtregs (struct regcache *regcache, int regnum)
237 int tid = ptid_get_lwp (inferior_ptid);
238 const xtensa_regtable_t *ptr;
239 char xtregs [XTENSA_ELF_XTREG_SIZE];
241 if (ptrace (PTRACE_GETXTREGS, tid, 0, (long)&xtregs) < 0)
242 perror_with_name (_("Couldn't get extended registers"));
244 for (ptr = xtensa_regmap_table; ptr->name; ptr++)
245 if (regnum == ptr->gdb_regnum || regnum == -1)
246 regcache_raw_collect (regcache, ptr->gdb_regnum,
247 xtregs + ptr->ptrace_offset);
249 if (ptrace (PTRACE_SETXTREGS, tid, 0, (long)&xtregs) < 0)
250 perror_with_name (_("Couldn't write extended registers"));
254 xtensa_linux_fetch_inferior_registers (struct target_ops *ops,
255 struct regcache *regcache, int regnum)
259 fetch_gregs (regcache, regnum);
260 fetch_xtregs (regcache, regnum);
262 else if ((regnum < xtreg_lo) || (regnum > xtreg_high))
263 fetch_gregs (regcache, regnum);
265 fetch_xtregs (regcache, regnum);
269 xtensa_linux_store_inferior_registers (struct target_ops *ops,
270 struct regcache *regcache, int regnum)
274 store_gregs (regcache, regnum);
275 store_xtregs (regcache, regnum);
277 else if ((regnum < xtreg_lo) || (regnum > xtreg_high))
278 store_gregs (regcache, regnum);
280 store_xtregs (regcache, regnum);
283 void _initialize_xtensa_linux_nat (void);
286 _initialize_xtensa_linux_nat (void)
288 struct target_ops *t;
289 const xtensa_regtable_t *ptr;
291 /* Calculate the number range for extended registers. */
292 xtreg_lo = 1000000000;
294 for (ptr = xtensa_regmap_table; ptr->name; ptr++)
296 if (ptr->gdb_regnum < xtreg_lo)
297 xtreg_lo = ptr->gdb_regnum;
298 if (ptr->gdb_regnum > xtreg_high)
299 xtreg_high = ptr->gdb_regnum;
302 /* Fill in the generic GNU/Linux methods. */
305 /* Add our register access methods. */
306 t->to_fetch_registers = xtensa_linux_fetch_inferior_registers;
307 t->to_store_registers = xtensa_linux_store_inferior_registers;
309 linux_nat_add_target (t);