1 /* Xtensa GNU/Linux native support.
3 Copyright (C) 2007-2014 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 "gdb_assert.h"
28 #include "linux-nat.h"
31 #include <sys/types.h>
34 #include <sys/ioctl.h>
37 #include <sys/procfs.h>
38 #include <sys/ptrace.h>
39 #include <asm/ptrace.h>
42 #include "xtensa-tdep.h"
44 /* Extended register set depends on hardware configs.
45 Keeping these definitions separately allows to introduce
46 hardware-specific overlays. */
47 #include "xtensa-xtregs.c"
50 get_thread_id (ptid_t ptid)
52 int tid = ptid_get_lwp (ptid);
54 tid = ptid_get_pid (ptid);
57 #define GET_THREAD_ID(PTID) get_thread_id (PTID)
60 fill_gregset (const struct regcache *regcache,
61 gdb_gregset_t *gregsetp, int regnum)
64 xtensa_elf_gregset_t *regs = (xtensa_elf_gregset_t *) gregsetp;
65 struct gdbarch *gdbarch = get_regcache_arch (regcache);
67 if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
68 regcache_raw_collect (regcache, gdbarch_pc_regnum (gdbarch), ®s->pc);
69 if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
70 regcache_raw_collect (regcache, gdbarch_ps_regnum (gdbarch), ®s->ps);
72 if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
73 regcache_raw_collect (regcache,
74 gdbarch_tdep (gdbarch)->wb_regnum,
76 if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
77 regcache_raw_collect (regcache,
78 gdbarch_tdep (gdbarch)->ws_regnum,
80 if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
81 regcache_raw_collect (regcache,
82 gdbarch_tdep (gdbarch)->lbeg_regnum,
84 if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
85 regcache_raw_collect (regcache,
86 gdbarch_tdep (gdbarch)->lend_regnum,
88 if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
89 regcache_raw_collect (regcache,
90 gdbarch_tdep (gdbarch)->lcount_regnum,
92 if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
93 regcache_raw_collect (regcache,
94 gdbarch_tdep (gdbarch)->sar_regnum,
96 if (regnum >=gdbarch_tdep (gdbarch)->ar_base
97 && regnum < gdbarch_tdep (gdbarch)->ar_base
98 + gdbarch_tdep (gdbarch)->num_aregs)
99 regcache_raw_collect (regcache,regnum,
100 ®s->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]);
101 else if (regnum == -1)
103 for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
104 regcache_raw_collect (regcache,
105 gdbarch_tdep (gdbarch)->ar_base + i,
111 supply_gregset_reg (struct regcache *regcache,
112 const gdb_gregset_t *gregsetp, int regnum)
115 xtensa_elf_gregset_t *regs = (xtensa_elf_gregset_t *) gregsetp;
117 struct gdbarch *gdbarch = get_regcache_arch (regcache);
119 if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
120 regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch), ®s->pc);
121 if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
122 regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch), ®s->ps);
124 if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
125 regcache_raw_supply (regcache,
126 gdbarch_tdep (gdbarch)->wb_regnum,
128 if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
129 regcache_raw_supply (regcache,
130 gdbarch_tdep (gdbarch)->ws_regnum,
132 if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
133 regcache_raw_supply (regcache,
134 gdbarch_tdep (gdbarch)->lbeg_regnum,
136 if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
137 regcache_raw_supply (regcache,
138 gdbarch_tdep (gdbarch)->lend_regnum,
140 if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
141 regcache_raw_supply (regcache,
142 gdbarch_tdep (gdbarch)->lcount_regnum,
144 if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
145 regcache_raw_supply (regcache,
146 gdbarch_tdep (gdbarch)->sar_regnum,
148 if (regnum >=gdbarch_tdep (gdbarch)->ar_base
149 && regnum < gdbarch_tdep (gdbarch)->ar_base
150 + gdbarch_tdep (gdbarch)->num_aregs)
151 regcache_raw_supply (regcache,regnum,
152 ®s->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]);
153 else if (regnum == -1)
155 for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
156 regcache_raw_supply (regcache,
157 gdbarch_tdep (gdbarch)->ar_base + i,
163 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
165 supply_gregset_reg (regcache, gregsetp, -1);
169 fill_fpregset (const struct regcache *regcache,
170 gdb_fpregset_t *fpregsetp, int regnum)
176 supply_fpregset (struct regcache *regcache,
177 const gdb_fpregset_t *fpregsetp)
182 /* Fetch greg-register(s) from process/thread TID
183 and store value(s) in GDB's register array. */
186 fetch_gregs (struct regcache *regcache, int regnum)
188 int tid = GET_THREAD_ID (inferior_ptid);
189 const gdb_gregset_t regs;
192 if (ptrace (PTRACE_GETREGS, tid, 0, (long) ®s) < 0)
194 perror_with_name (_("Couldn't get registers"));
198 supply_gregset_reg (regcache, ®s, regnum);
201 /* Store greg-register(s) in GDB's register
202 array into the process/thread specified by TID. */
205 store_gregs (struct regcache *regcache, int regnum)
207 int tid = GET_THREAD_ID (inferior_ptid);
211 if (ptrace (PTRACE_GETREGS, tid, 0, (long) ®s) < 0)
213 perror_with_name (_("Couldn't get registers"));
217 fill_gregset (regcache, ®s, regnum);
219 if (ptrace (PTRACE_SETREGS, tid, 0, (long) ®s) < 0)
221 perror_with_name (_("Couldn't write registers"));
227 static int xtreg_high;
229 /* Fetch/Store Xtensa TIE registers. Xtensa GNU/Linux PTRACE
230 interface provides special requests for this. */
233 fetch_xtregs (struct regcache *regcache, int regnum)
235 int tid = GET_THREAD_ID (inferior_ptid);
236 const xtensa_regtable_t *ptr;
237 char xtregs [XTENSA_ELF_XTREG_SIZE];
239 if (ptrace (PTRACE_GETXTREGS, tid, 0, (long)&xtregs) < 0)
240 perror_with_name (_("Couldn't get extended registers"));
242 for (ptr = xtensa_regmap_table; ptr->name; ptr++)
243 if (regnum == ptr->gdb_regnum || regnum == -1)
244 regcache_raw_supply (regcache, ptr->gdb_regnum,
245 xtregs + ptr->ptrace_offset);
249 store_xtregs (struct regcache *regcache, int regnum)
251 int tid = GET_THREAD_ID (inferior_ptid);
252 const xtensa_regtable_t *ptr;
253 char xtregs [XTENSA_ELF_XTREG_SIZE];
255 if (ptrace (PTRACE_GETXTREGS, tid, 0, (long)&xtregs) < 0)
256 perror_with_name (_("Couldn't get extended registers"));
258 for (ptr = xtensa_regmap_table; ptr->name; ptr++)
259 if (regnum == ptr->gdb_regnum || regnum == -1)
260 regcache_raw_collect (regcache, ptr->gdb_regnum,
261 xtregs + ptr->ptrace_offset);
263 if (ptrace (PTRACE_SETXTREGS, tid, 0, (long)&xtregs) < 0)
264 perror_with_name (_("Couldn't write extended registers"));
268 xtensa_linux_fetch_inferior_registers (struct target_ops *ops,
269 struct regcache *regcache, int regnum)
273 fetch_gregs (regcache, regnum);
274 fetch_xtregs (regcache, regnum);
276 else if ((regnum < xtreg_lo) || (regnum > xtreg_high))
277 fetch_gregs (regcache, regnum);
279 fetch_xtregs (regcache, regnum);
283 xtensa_linux_store_inferior_registers (struct target_ops *ops,
284 struct regcache *regcache, int regnum)
288 store_gregs (regcache, regnum);
289 store_xtregs (regcache, regnum);
291 else if ((regnum < xtreg_lo) || (regnum > xtreg_high))
292 store_gregs (regcache, regnum);
294 store_xtregs (regcache, regnum);
297 void _initialize_xtensa_linux_nat (void);
300 _initialize_xtensa_linux_nat (void)
302 struct target_ops *t;
303 const xtensa_regtable_t *ptr;
305 /* Calculate the number range for extended registers. */
306 xtreg_lo = 1000000000;
308 for (ptr = xtensa_regmap_table; ptr->name; ptr++)
310 if (ptr->gdb_regnum < xtreg_lo)
311 xtreg_lo = ptr->gdb_regnum;
312 if (ptr->gdb_regnum > xtreg_high)
313 xtreg_high = ptr->gdb_regnum;
316 /* Fill in the generic GNU/Linux methods. */
319 /* Add our register access methods. */
320 t->to_fetch_registers = xtensa_linux_fetch_inferior_registers;
321 t->to_store_registers = xtensa_linux_store_inferior_registers;
323 linux_nat_add_target (t);