s390-vregs.exp: Avoid compile errors with older GCCs and on 31-bit targets
[binutils-gdb.git] / gdb / xtensa-linux-nat.c
blob921bc99644bb68faff0b03b8e76b401cb7a24f92
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/>. */
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "gdbcore.h"
24 #include "regcache.h"
25 #include "target.h"
26 #include "linux-nat.h"
28 #include <stdint.h>
29 #include <sys/types.h>
30 #include <signal.h>
31 #include <sys/user.h>
32 #include <sys/ioctl.h>
33 #include "gdb_wait.h"
34 #include <fcntl.h>
35 #include <sys/procfs.h>
36 #include <sys/ptrace.h>
37 #include <asm/ptrace.h>
39 #include "gregset.h"
40 #include "xtensa-tdep.h"
42 /* Extended register set depends on hardware configs.
43 Keeping these definitions separately allows to introduce
44 hardware-specific overlays. */
45 #include "xtensa-xtregs.c"
47 static int
48 get_thread_id (ptid_t ptid)
50 int tid = ptid_get_lwp (ptid);
51 if (0 == tid)
52 tid = ptid_get_pid (ptid);
53 return tid;
55 #define GET_THREAD_ID(PTID) get_thread_id (PTID)
57 void
58 fill_gregset (const struct regcache *regcache,
59 gdb_gregset_t *gregsetp, int regnum)
61 int i;
62 xtensa_elf_gregset_t *regs = (xtensa_elf_gregset_t *) gregsetp;
63 struct gdbarch *gdbarch = get_regcache_arch (regcache);
65 if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
66 regcache_raw_collect (regcache, gdbarch_pc_regnum (gdbarch), &regs->pc);
67 if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
68 regcache_raw_collect (regcache, gdbarch_ps_regnum (gdbarch), &regs->ps);
70 if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
71 regcache_raw_collect (regcache,
72 gdbarch_tdep (gdbarch)->wb_regnum,
73 &regs->windowbase);
74 if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
75 regcache_raw_collect (regcache,
76 gdbarch_tdep (gdbarch)->ws_regnum,
77 &regs->windowstart);
78 if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
79 regcache_raw_collect (regcache,
80 gdbarch_tdep (gdbarch)->lbeg_regnum,
81 &regs->lbeg);
82 if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
83 regcache_raw_collect (regcache,
84 gdbarch_tdep (gdbarch)->lend_regnum,
85 &regs->lend);
86 if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
87 regcache_raw_collect (regcache,
88 gdbarch_tdep (gdbarch)->lcount_regnum,
89 &regs->lcount);
90 if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
91 regcache_raw_collect (regcache,
92 gdbarch_tdep (gdbarch)->sar_regnum,
93 &regs->sar);
94 if (regnum >=gdbarch_tdep (gdbarch)->ar_base
95 && regnum < gdbarch_tdep (gdbarch)->ar_base
96 + gdbarch_tdep (gdbarch)->num_aregs)
97 regcache_raw_collect (regcache,regnum,
98 &regs->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]);
99 else if (regnum == -1)
101 for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
102 regcache_raw_collect (regcache,
103 gdbarch_tdep (gdbarch)->ar_base + i,
104 &regs->ar[i]);
108 void
109 supply_gregset_reg (struct regcache *regcache,
110 const gdb_gregset_t *gregsetp, int regnum)
112 int i;
113 xtensa_elf_gregset_t *regs = (xtensa_elf_gregset_t *) gregsetp;
115 struct gdbarch *gdbarch = get_regcache_arch (regcache);
117 if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
118 regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch), &regs->pc);
119 if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
120 regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch), &regs->ps);
122 if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
123 regcache_raw_supply (regcache,
124 gdbarch_tdep (gdbarch)->wb_regnum,
125 &regs->windowbase);
126 if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
127 regcache_raw_supply (regcache,
128 gdbarch_tdep (gdbarch)->ws_regnum,
129 &regs->windowstart);
130 if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
131 regcache_raw_supply (regcache,
132 gdbarch_tdep (gdbarch)->lbeg_regnum,
133 &regs->lbeg);
134 if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
135 regcache_raw_supply (regcache,
136 gdbarch_tdep (gdbarch)->lend_regnum,
137 &regs->lend);
138 if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
139 regcache_raw_supply (regcache,
140 gdbarch_tdep (gdbarch)->lcount_regnum,
141 &regs->lcount);
142 if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
143 regcache_raw_supply (regcache,
144 gdbarch_tdep (gdbarch)->sar_regnum,
145 &regs->sar);
146 if (regnum >=gdbarch_tdep (gdbarch)->ar_base
147 && regnum < gdbarch_tdep (gdbarch)->ar_base
148 + gdbarch_tdep (gdbarch)->num_aregs)
149 regcache_raw_supply (regcache,regnum,
150 &regs->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]);
151 else if (regnum == -1)
153 for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
154 regcache_raw_supply (regcache,
155 gdbarch_tdep (gdbarch)->ar_base + i,
156 &regs->ar[i]);
160 void
161 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
163 supply_gregset_reg (regcache, gregsetp, -1);
166 void
167 fill_fpregset (const struct regcache *regcache,
168 gdb_fpregset_t *fpregsetp, int regnum)
170 return;
173 void
174 supply_fpregset (struct regcache *regcache,
175 const gdb_fpregset_t *fpregsetp)
177 return;
180 /* Fetch greg-register(s) from process/thread TID
181 and store value(s) in GDB's register array. */
183 static void
184 fetch_gregs (struct regcache *regcache, int regnum)
186 int tid = GET_THREAD_ID (inferior_ptid);
187 const gdb_gregset_t regs;
188 int areg;
190 if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
192 perror_with_name (_("Couldn't get registers"));
193 return;
196 supply_gregset_reg (regcache, &regs, regnum);
199 /* Store greg-register(s) in GDB's register
200 array into the process/thread specified by TID. */
202 static void
203 store_gregs (struct regcache *regcache, int regnum)
205 int tid = GET_THREAD_ID (inferior_ptid);
206 gdb_gregset_t regs;
207 int areg;
209 if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
211 perror_with_name (_("Couldn't get registers"));
212 return;
215 fill_gregset (regcache, &regs, regnum);
217 if (ptrace (PTRACE_SETREGS, tid, 0, (long) &regs) < 0)
219 perror_with_name (_("Couldn't write registers"));
220 return;
224 static int xtreg_lo;
225 static int xtreg_high;
227 /* Fetch/Store Xtensa TIE registers. Xtensa GNU/Linux PTRACE
228 interface provides special requests for this. */
230 static void
231 fetch_xtregs (struct regcache *regcache, int regnum)
233 int tid = GET_THREAD_ID (inferior_ptid);
234 const xtensa_regtable_t *ptr;
235 char xtregs [XTENSA_ELF_XTREG_SIZE];
237 if (ptrace (PTRACE_GETXTREGS, tid, 0, (long)&xtregs) < 0)
238 perror_with_name (_("Couldn't get extended registers"));
240 for (ptr = xtensa_regmap_table; ptr->name; ptr++)
241 if (regnum == ptr->gdb_regnum || regnum == -1)
242 regcache_raw_supply (regcache, ptr->gdb_regnum,
243 xtregs + ptr->ptrace_offset);
246 static void
247 store_xtregs (struct regcache *regcache, int regnum)
249 int tid = GET_THREAD_ID (inferior_ptid);
250 const xtensa_regtable_t *ptr;
251 char xtregs [XTENSA_ELF_XTREG_SIZE];
253 if (ptrace (PTRACE_GETXTREGS, tid, 0, (long)&xtregs) < 0)
254 perror_with_name (_("Couldn't get extended registers"));
256 for (ptr = xtensa_regmap_table; ptr->name; ptr++)
257 if (regnum == ptr->gdb_regnum || regnum == -1)
258 regcache_raw_collect (regcache, ptr->gdb_regnum,
259 xtregs + ptr->ptrace_offset);
261 if (ptrace (PTRACE_SETXTREGS, tid, 0, (long)&xtregs) < 0)
262 perror_with_name (_("Couldn't write extended registers"));
265 void
266 xtensa_linux_fetch_inferior_registers (struct target_ops *ops,
267 struct regcache *regcache, int regnum)
269 if (regnum == -1)
271 fetch_gregs (regcache, regnum);
272 fetch_xtregs (regcache, regnum);
274 else if ((regnum < xtreg_lo) || (regnum > xtreg_high))
275 fetch_gregs (regcache, regnum);
276 else
277 fetch_xtregs (regcache, regnum);
280 void
281 xtensa_linux_store_inferior_registers (struct target_ops *ops,
282 struct regcache *regcache, int regnum)
284 if (regnum == -1)
286 store_gregs (regcache, regnum);
287 store_xtregs (regcache, regnum);
289 else if ((regnum < xtreg_lo) || (regnum > xtreg_high))
290 store_gregs (regcache, regnum);
291 else
292 store_xtregs (regcache, regnum);
295 void _initialize_xtensa_linux_nat (void);
297 void
298 _initialize_xtensa_linux_nat (void)
300 struct target_ops *t;
301 const xtensa_regtable_t *ptr;
303 /* Calculate the number range for extended registers. */
304 xtreg_lo = 1000000000;
305 xtreg_high = -1;
306 for (ptr = xtensa_regmap_table; ptr->name; ptr++)
308 if (ptr->gdb_regnum < xtreg_lo)
309 xtreg_lo = ptr->gdb_regnum;
310 if (ptr->gdb_regnum > xtreg_high)
311 xtreg_high = ptr->gdb_regnum;
314 /* Fill in the generic GNU/Linux methods. */
315 t = linux_target ();
317 /* Add our register access methods. */
318 t->to_fetch_registers = xtensa_linux_fetch_inferior_registers;
319 t->to_store_registers = xtensa_linux_store_inferior_registers;
321 linux_nat_add_target (t);