merge from gcc
[gdb/gnu.git] / gdb / xtensa-linux-nat.c
blob3934165a743464fa190fafa1361fc6fce04fae64
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/>. */
20 #include "defs.h"
21 #include "gdb_string.h"
22 #include "frame.h"
23 #include "inferior.h"
24 #include "gdbcore.h"
25 #include "regcache.h"
26 #include "gdb_assert.h"
27 #include "target.h"
28 #include "linux-nat.h"
30 #include <stdint.h>
31 #include <sys/types.h>
32 #include <signal.h>
33 #include <sys/user.h>
34 #include <sys/ioctl.h>
35 #include "gdb_wait.h"
36 #include <fcntl.h>
37 #include <sys/procfs.h>
38 #include <sys/ptrace.h>
40 #include "gregset.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"
48 static int
49 get_thread_id (ptid_t ptid)
51 int tid = ptid_get_lwp (ptid);
52 if (0 == tid)
53 tid = ptid_get_pid (ptid);
54 return tid;
56 #define GET_THREAD_ID(PTID) get_thread_id (PTID)
58 void
59 fill_gregset (const struct regcache *regcache,
60 gdb_gregset_t *gregsetp, int regnum)
62 int i;
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), &regs->pc);
68 if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
69 regcache_raw_collect (regcache, gdbarch_ps_regnum (gdbarch), &regs->ps);
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);
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 &regs->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,
105 &regs->ar[i]);
109 void
110 supply_gregset_reg (struct regcache *regcache,
111 const gdb_gregset_t *gregsetp, int regnum)
113 int i;
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), &regs->pc);
120 if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
121 regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch), &regs->ps);
123 if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
124 regcache_raw_supply (regcache,
125 gdbarch_tdep (gdbarch)->wb_regnum,
126 &regs->windowbase);
127 if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
128 regcache_raw_supply (regcache,
129 gdbarch_tdep (gdbarch)->ws_regnum,
130 &regs->windowstart);
131 if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
132 regcache_raw_supply (regcache,
133 gdbarch_tdep (gdbarch)->lbeg_regnum,
134 &regs->lbeg);
135 if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
136 regcache_raw_supply (regcache,
137 gdbarch_tdep (gdbarch)->lend_regnum,
138 &regs->lend);
139 if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
140 regcache_raw_supply (regcache,
141 gdbarch_tdep (gdbarch)->lcount_regnum,
142 &regs->lcount);
143 if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
144 regcache_raw_supply (regcache,
145 gdbarch_tdep (gdbarch)->sar_regnum,
146 &regs->sar);
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 &regs->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,
157 &regs->ar[i]);
161 void
162 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
164 supply_gregset_reg (regcache, gregsetp, -1);
167 void
168 fill_fpregset (const struct regcache *regcache,
169 gdb_fpregset_t *fpregsetp, int regnum)
171 return;
174 void
175 supply_fpregset (struct regcache *regcache,
176 const gdb_fpregset_t *fpregsetp)
178 return;
181 /* Fetch greg-register(s) from process/thread TID
182 and store value(s) in GDB's register array. */
184 static void
185 fetch_gregs (struct regcache *regcache, int regnum)
187 int tid = GET_THREAD_ID (inferior_ptid);
188 const gdb_gregset_t regs;
189 int areg;
191 if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
193 perror_with_name (_("Couldn't get registers"));
194 return;
197 supply_gregset_reg (regcache, &regs, regnum);
200 /* Store greg-register(s) in GDB's register
201 array into the process/thread specified by TID. */
203 static void
204 store_gregs (struct regcache *regcache, int regnum)
206 int tid = GET_THREAD_ID (inferior_ptid);
207 gdb_gregset_t regs;
208 int areg;
210 if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
212 perror_with_name (_("Couldn't get registers"));
213 return;
216 fill_gregset (regcache, &regs, regnum);
218 if (ptrace (PTRACE_SETREGS, tid, 0, (long) &regs) < 0)
220 perror_with_name (_("Couldn't write registers"));
221 return;
225 static int xtreg_lo;
226 static int xtreg_high;
228 /* Fetch/Store Xtensa TIE registers. Xtensa GNU/Linux PTRACE
229 interface provides special requests for this. */
231 static void
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);
247 static void
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"));
266 void
267 xtensa_linux_fetch_inferior_registers (struct target_ops *ops,
268 struct regcache *regcache, int regnum)
270 if (regnum == -1)
272 fetch_gregs (regcache, regnum);
273 fetch_xtregs (regcache, regnum);
275 else if ((regnum < xtreg_lo) || (regnum > xtreg_high))
276 fetch_gregs (regcache, regnum);
277 else
278 fetch_xtregs (regcache, regnum);
281 void
282 xtensa_linux_store_inferior_registers (struct target_ops *ops,
283 struct regcache *regcache, int regnum)
285 if (regnum == -1)
287 store_gregs (regcache, regnum);
288 store_xtregs (regcache, regnum);
290 else if ((regnum < xtreg_lo) || (regnum > xtreg_high))
291 store_gregs (regcache, regnum);
292 else
293 store_xtregs (regcache, regnum);
296 void _initialize_xtensa_linux_nat (void);
298 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;
306 xtreg_high = -1;
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. */
316 t = linux_target ();
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);