1 /* Xtensa GNU/Linux native support.
3 Copyright (C) 2007-2013 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/>. */
21 #include "gdb_string.h"
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>
41 #include "xtensa-tdep.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 get_thread_id (ptid_t ptid)
51 int tid = ptid_get_lwp (ptid);
53 tid = ptid_get_pid (ptid);
56 #define GET_THREAD_ID(PTID) get_thread_id (PTID)
59 fill_gregset (const struct regcache *regcache,
60 gdb_gregset_t *gregsetp, int regnum)
63 xtensa_elf_gregset_t *regs = (xtensa_elf_gregset_t *) gregsetp;
64 struct gdbarch *gdbarch = get_regcache_arch (regcache);
66 if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
67 regcache_raw_collect (regcache, gdbarch_pc_regnum (gdbarch), ®s->pc);
68 if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
69 regcache_raw_collect (regcache, gdbarch_ps_regnum (gdbarch), ®s->ps);
71 if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
72 regcache_raw_collect (regcache,
73 gdbarch_tdep (gdbarch)->wb_regnum,
75 if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
76 regcache_raw_collect (regcache,
77 gdbarch_tdep (gdbarch)->ws_regnum,
79 if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
80 regcache_raw_collect (regcache,
81 gdbarch_tdep (gdbarch)->lbeg_regnum,
83 if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
84 regcache_raw_collect (regcache,
85 gdbarch_tdep (gdbarch)->lend_regnum,
87 if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
88 regcache_raw_collect (regcache,
89 gdbarch_tdep (gdbarch)->lcount_regnum,
91 if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
92 regcache_raw_collect (regcache,
93 gdbarch_tdep (gdbarch)->sar_regnum,
95 if (regnum >=gdbarch_tdep (gdbarch)->ar_base
96 && regnum < gdbarch_tdep (gdbarch)->ar_base
97 + gdbarch_tdep (gdbarch)->num_aregs)
98 regcache_raw_collect (regcache,regnum,
99 ®s->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]);
100 else if (regnum == -1)
102 for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
103 regcache_raw_collect (regcache,
104 gdbarch_tdep (gdbarch)->ar_base + i,
110 supply_gregset_reg (struct regcache *regcache,
111 const gdb_gregset_t *gregsetp, int regnum)
114 xtensa_elf_gregset_t *regs = (xtensa_elf_gregset_t *) gregsetp;
116 struct gdbarch *gdbarch = get_regcache_arch (regcache);
118 if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
119 regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch), ®s->pc);
120 if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
121 regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch), ®s->ps);
123 if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
124 regcache_raw_supply (regcache,
125 gdbarch_tdep (gdbarch)->wb_regnum,
127 if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
128 regcache_raw_supply (regcache,
129 gdbarch_tdep (gdbarch)->ws_regnum,
131 if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
132 regcache_raw_supply (regcache,
133 gdbarch_tdep (gdbarch)->lbeg_regnum,
135 if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
136 regcache_raw_supply (regcache,
137 gdbarch_tdep (gdbarch)->lend_regnum,
139 if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
140 regcache_raw_supply (regcache,
141 gdbarch_tdep (gdbarch)->lcount_regnum,
143 if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
144 regcache_raw_supply (regcache,
145 gdbarch_tdep (gdbarch)->sar_regnum,
147 if (regnum >=gdbarch_tdep (gdbarch)->ar_base
148 && regnum < gdbarch_tdep (gdbarch)->ar_base
149 + gdbarch_tdep (gdbarch)->num_aregs)
150 regcache_raw_supply (regcache,regnum,
151 ®s->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]);
152 else if (regnum == -1)
154 for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
155 regcache_raw_supply (regcache,
156 gdbarch_tdep (gdbarch)->ar_base + i,
162 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
164 supply_gregset_reg (regcache, gregsetp, -1);
168 fill_fpregset (const struct regcache *regcache,
169 gdb_fpregset_t *fpregsetp, int regnum)
175 supply_fpregset (struct regcache *regcache,
176 const gdb_fpregset_t *fpregsetp)
181 /* Fetch greg-register(s) from process/thread TID
182 and store value(s) in GDB's register array. */
185 fetch_gregs (struct regcache *regcache, int regnum)
187 int tid = GET_THREAD_ID (inferior_ptid);
188 const gdb_gregset_t regs;
191 if (ptrace (PTRACE_GETREGS, tid, 0, (long) ®s) < 0)
193 perror_with_name (_("Couldn't get registers"));
197 supply_gregset_reg (regcache, ®s, regnum);
200 /* Store greg-register(s) in GDB's register
201 array into the process/thread specified by TID. */
204 store_gregs (struct regcache *regcache, int regnum)
206 int tid = GET_THREAD_ID (inferior_ptid);
210 if (ptrace (PTRACE_GETREGS, tid, 0, (long) ®s) < 0)
212 perror_with_name (_("Couldn't get registers"));
216 fill_gregset (regcache, ®s, regnum);
218 if (ptrace (PTRACE_SETREGS, tid, 0, (long) ®s) < 0)
220 perror_with_name (_("Couldn't write registers"));
226 static int xtreg_high;
228 /* Fetch/Store Xtensa TIE registers. Xtensa GNU/Linux PTRACE
229 interface provides special requests for this. */
232 fetch_xtregs (struct regcache *regcache, int regnum)
234 int tid = GET_THREAD_ID (inferior_ptid);
235 const xtensa_regtable_t *ptr;
236 char xtregs [XTENSA_ELF_XTREG_SIZE];
238 if (ptrace (PTRACE_GETXTREGS, tid, 0, (long)&xtregs) < 0)
239 perror_with_name (_("Couldn't get extended registers"));
241 for (ptr = xtensa_regmap_table; ptr->name; ptr++)
242 if (regnum == ptr->gdb_regnum || regnum == -1)
243 regcache_raw_supply (regcache, ptr->gdb_regnum,
244 xtregs + ptr->ptrace_offset);
248 store_xtregs (struct regcache *regcache, int regnum)
250 int tid = GET_THREAD_ID (inferior_ptid);
251 const xtensa_regtable_t *ptr;
252 char xtregs [XTENSA_ELF_XTREG_SIZE];
254 if (ptrace (PTRACE_GETXTREGS, tid, 0, (long)&xtregs) < 0)
255 perror_with_name (_("Couldn't get extended registers"));
257 for (ptr = xtensa_regmap_table; ptr->name; ptr++)
258 if (regnum == ptr->gdb_regnum || regnum == -1)
259 regcache_raw_collect (regcache, ptr->gdb_regnum,
260 xtregs + ptr->ptrace_offset);
262 if (ptrace (PTRACE_SETXTREGS, tid, 0, (long)&xtregs) < 0)
263 perror_with_name (_("Couldn't write extended registers"));
267 xtensa_linux_fetch_inferior_registers (struct target_ops *ops,
268 struct regcache *regcache, int regnum)
272 fetch_gregs (regcache, regnum);
273 fetch_xtregs (regcache, regnum);
275 else if ((regnum < xtreg_lo) || (regnum > xtreg_high))
276 fetch_gregs (regcache, regnum);
278 fetch_xtregs (regcache, regnum);
282 xtensa_linux_store_inferior_registers (struct target_ops *ops,
283 struct regcache *regcache, int regnum)
287 store_gregs (regcache, regnum);
288 store_xtregs (regcache, regnum);
290 else if ((regnum < xtreg_lo) || (regnum > xtreg_high))
291 store_gregs (regcache, regnum);
293 store_xtregs (regcache, regnum);
296 void _initialize_xtensa_linux_nat (void);
299 _initialize_xtensa_linux_nat (void)
301 struct target_ops *t;
302 const xtensa_regtable_t *ptr;
304 /* Calculate the number range for extended registers. */
305 xtreg_lo = 1000000000;
307 for (ptr = xtensa_regmap_table; ptr->name; ptr++)
309 if (ptr->gdb_regnum < xtreg_lo)
310 xtreg_lo = ptr->gdb_regnum;
311 if (ptr->gdb_regnum > xtreg_high)
312 xtreg_high = ptr->gdb_regnum;
315 /* Fill in the generic GNU/Linux methods. */
318 /* Add our register access methods. */
319 t->to_fetch_registers = xtensa_linux_fetch_inferior_registers;
320 t->to_store_registers = xtensa_linux_store_inferior_registers;
322 linux_nat_add_target (t);