* target.h (struct regcache): Add forward declaration.
[deliverable/binutils-gdb.git] / gdb / mips-linux-nat.c
CommitLineData
75c9abc6 1/* Native-dependent code for GNU/Linux on MIPS processors.
a094c6fb 2
6aba47ca 3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
dc60ece8 4 Free Software Foundation, Inc.
2aa830e4
DJ
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
197e01b6
EZ
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
2aa830e4
DJ
22
23#include "defs.h"
d37eb719 24#include "inferior.h"
6b753f60 25#include "mips-tdep.h"
10d6c8cd 26#include "target.h"
28f5035f 27#include "regcache.h"
10d6c8cd 28#include "linux-nat.h"
d37eb719 29#include "mips-linux-tdep.h"
2aa830e4 30
dc60ece8 31#include "gdb_proc_service.h"
3e00823e 32#include "gregset.h"
dc60ece8 33
d37eb719
DJ
34#include <sys/ptrace.h>
35
dc60ece8
DJ
36#ifndef PTRACE_GET_THREAD_AREA
37#define PTRACE_GET_THREAD_AREA 25
38#endif
39
d37eb719
DJ
40/* Assume that we have PTRACE_GETREGS et al. support. If we do not,
41 we'll clear this and use PTRACE_PEEKUSER instead. */
42static int have_ptrace_regsets = 1;
43
44/* Saved function pointers to fetch and store a single register using
45 PTRACE_PEEKUSER and PTRACE_POKEUSER. */
46
56be3814
UW
47void (*super_fetch_registers) (struct regcache *, int);
48void (*super_store_registers) (struct regcache *, int);
d37eb719 49
2aa830e4 50/* Pseudo registers can not be read. ptrace does not provide a way to
24e05951
AC
51 read (or set) MIPS_PS_REGNUM, and there's no point in reading or
52 setting MIPS_ZERO_REGNUM. We also can not set BADVADDR, CAUSE, or
53 FCRIR via ptrace(). */
2aa830e4
DJ
54
55int
56mips_linux_cannot_fetch_register (int regno)
57{
613e114f 58 if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
2aa830e4 59 return 0;
56cea623
AC
60 else if (regno >= mips_regnum (current_gdbarch)->fp0
61 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
fd8f87c5 62 return 0;
56cea623
AC
63 else if (regno == mips_regnum (current_gdbarch)->lo
64 || regno == mips_regnum (current_gdbarch)->hi
65 || regno == mips_regnum (current_gdbarch)->badvaddr
66 || regno == mips_regnum (current_gdbarch)->cause
67 || regno == mips_regnum (current_gdbarch)->pc
68 || regno == mips_regnum (current_gdbarch)->fp_control_status
69 || regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
70 return 0;
71 else
72 return 1;
2aa830e4
DJ
73}
74
75int
76mips_linux_cannot_store_register (int regno)
77{
613e114f 78 if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
2aa830e4 79 return 0;
fd8f87c5
DJ
80 else if (regno >= FP0_REGNUM && regno <= FP0_REGNUM + 32)
81 return 0;
56cea623
AC
82 else if (regno == mips_regnum (current_gdbarch)->lo
83 || regno == mips_regnum (current_gdbarch)->hi
84 || regno == mips_regnum (current_gdbarch)->pc
85 || regno == mips_regnum (current_gdbarch)->fp_control_status)
86 return 0;
87 else
88 return 1;
2aa830e4 89}
10d6c8cd 90
dda0c97e
UW
91/* Map gdb internal register number to ptrace ``address''.
92 These ``addresses'' are normally defined in <asm/ptrace.h>. */
93
94static CORE_ADDR
95mips_linux_register_addr (int regno)
96{
97 int regaddr;
98
99 if (regno < 0 || regno >= NUM_REGS)
100 error (_("Bogon register number %d."), regno);
101
102 if (regno < 32)
103 regaddr = regno;
104 else if ((regno >= mips_regnum (current_gdbarch)->fp0)
105 && (regno < mips_regnum (current_gdbarch)->fp0 + 32))
106 regaddr = FPR_BASE + (regno - mips_regnum (current_gdbarch)->fp0);
107 else if (regno == mips_regnum (current_gdbarch)->pc)
108 regaddr = PC;
109 else if (regno == mips_regnum (current_gdbarch)->cause)
110 regaddr = CAUSE;
111 else if (regno == mips_regnum (current_gdbarch)->badvaddr)
112 regaddr = BADVADDR;
113 else if (regno == mips_regnum (current_gdbarch)->lo)
114 regaddr = MMLO;
115 else if (regno == mips_regnum (current_gdbarch)->hi)
116 regaddr = MMHI;
117 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
118 regaddr = FPC_CSR;
119 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
120 regaddr = FPC_EIR;
121 else
122 error (_("Unknowable register number %d."), regno);
123
124 return regaddr;
125}
126
127static CORE_ADDR
128mips64_linux_register_addr (int regno)
129{
130 int regaddr;
131
132 if (regno < 0 || regno >= NUM_REGS)
133 error (_("Bogon register number %d."), regno);
134
135 if (regno < 32)
136 regaddr = regno;
137 else if ((regno >= mips_regnum (current_gdbarch)->fp0)
138 && (regno < mips_regnum (current_gdbarch)->fp0 + 32))
139 regaddr = MIPS64_FPR_BASE + (regno - FP0_REGNUM);
140 else if (regno == mips_regnum (current_gdbarch)->pc)
141 regaddr = MIPS64_PC;
142 else if (regno == mips_regnum (current_gdbarch)->cause)
143 regaddr = MIPS64_CAUSE;
144 else if (regno == mips_regnum (current_gdbarch)->badvaddr)
145 regaddr = MIPS64_BADVADDR;
146 else if (regno == mips_regnum (current_gdbarch)->lo)
147 regaddr = MIPS64_MMLO;
148 else if (regno == mips_regnum (current_gdbarch)->hi)
149 regaddr = MIPS64_MMHI;
150 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
151 regaddr = MIPS64_FPC_CSR;
152 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
153 regaddr = MIPS64_FPC_EIR;
154 else
155 error (_("Unknowable register number %d."), regno);
156
157 return regaddr;
158}
159
dc60ece8
DJ
160/* Fetch the thread-local storage pointer for libthread_db. */
161
162ps_err_e
163ps_get_thread_area (const struct ps_prochandle *ph,
164 lwpid_t lwpid, int idx, void **base)
165{
166 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
167 return PS_ERR;
168
169 /* IDX is the bias from the thread pointer to the beginning of the
170 thread descriptor. It has to be subtracted due to implementation
171 quirks in libthread_db. */
172 *base = (void *) ((char *)*base - idx);
173
174 return PS_OK;
175}
176
3e00823e
UW
177/* Wrapper functions. These are only used by libthread_db. */
178
179void
7f7fe91e 180supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
3e00823e
UW
181{
182 if (mips_isa_regsize (current_gdbarch) == 4)
7f7fe91e 183 mips_supply_gregset (regcache, (const mips_elf_gregset_t *) gregsetp);
3e00823e 184 else
7f7fe91e 185 mips64_supply_gregset (regcache, (const mips64_elf_gregset_t *) gregsetp);
3e00823e
UW
186}
187
188void
7f7fe91e
UW
189fill_gregset (const struct regcache *regcache,
190 gdb_gregset_t *gregsetp, int regno)
3e00823e
UW
191{
192 if (mips_isa_regsize (current_gdbarch) == 4)
7f7fe91e 193 mips_fill_gregset (regcache, (mips_elf_gregset_t *) gregsetp, regno);
3e00823e 194 else
7f7fe91e 195 mips64_fill_gregset (regcache, (mips64_elf_gregset_t *) gregsetp, regno);
3e00823e
UW
196}
197
198void
7f7fe91e 199supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
3e00823e
UW
200{
201 if (mips_isa_regsize (current_gdbarch) == 4)
7f7fe91e 202 mips_supply_fpregset (regcache, (const mips_elf_fpregset_t *) fpregsetp);
3e00823e 203 else
7f7fe91e 204 mips64_supply_fpregset (regcache, (const mips64_elf_fpregset_t *) fpregsetp);
3e00823e
UW
205}
206
207void
7f7fe91e
UW
208fill_fpregset (const struct regcache *regcache,
209 gdb_fpregset_t *fpregsetp, int regno)
3e00823e
UW
210{
211 if (mips_isa_regsize (current_gdbarch) == 4)
7f7fe91e 212 mips_fill_fpregset (regcache, (mips_elf_fpregset_t *) fpregsetp, regno);
3e00823e 213 else
7f7fe91e 214 mips64_fill_fpregset (regcache, (mips64_elf_fpregset_t *) fpregsetp, regno);
3e00823e
UW
215}
216
217
d37eb719
DJ
218/* Fetch REGNO (or all registers if REGNO == -1) from the target
219 using PTRACE_GETREGS et al. */
220
221static void
56be3814 222mips64_linux_regsets_fetch_registers (struct regcache *regcache, int regno)
d37eb719
DJ
223{
224 int is_fp;
225 int tid;
226
227 if (regno >= mips_regnum (current_gdbarch)->fp0
228 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
229 is_fp = 1;
230 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
231 is_fp = 1;
232 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
233 is_fp = 1;
234 else
235 is_fp = 0;
236
237 tid = ptid_get_lwp (inferior_ptid);
238 if (tid == 0)
239 tid = ptid_get_pid (inferior_ptid);
240
241 if (regno == -1 || !is_fp)
242 {
243 mips64_elf_gregset_t regs;
244
245 if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
246 {
247 if (errno == EIO)
248 {
249 have_ptrace_regsets = 0;
250 return;
251 }
252 perror_with_name (_("Couldn't get registers"));
253 }
254
56be3814 255 mips64_supply_gregset (regcache,
28f5035f 256 (const mips64_elf_gregset_t *) &regs);
d37eb719
DJ
257 }
258
259 if (regno == -1 || is_fp)
260 {
261 mips64_elf_fpregset_t fp_regs;
262
263 if (ptrace (PTRACE_GETFPREGS, tid, 0L,
264 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
265 {
266 if (errno == EIO)
267 {
268 have_ptrace_regsets = 0;
269 return;
270 }
271 perror_with_name (_("Couldn't get FP registers"));
272 }
273
56be3814 274 mips64_supply_fpregset (regcache,
28f5035f 275 (const mips64_elf_fpregset_t *) &fp_regs);
d37eb719
DJ
276 }
277}
278
279/* Store REGNO (or all registers if REGNO == -1) to the target
280 using PTRACE_SETREGS et al. */
281
282static void
56be3814 283mips64_linux_regsets_store_registers (const struct regcache *regcache, int regno)
d37eb719
DJ
284{
285 int is_fp;
286 int tid;
287
288 if (regno >= mips_regnum (current_gdbarch)->fp0
289 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
290 is_fp = 1;
291 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
292 is_fp = 1;
293 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
294 is_fp = 1;
295 else
296 is_fp = 0;
297
298 tid = ptid_get_lwp (inferior_ptid);
299 if (tid == 0)
300 tid = ptid_get_pid (inferior_ptid);
301
302 if (regno == -1 || !is_fp)
303 {
304 mips64_elf_gregset_t regs;
305
306 if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
307 perror_with_name (_("Couldn't get registers"));
308
56be3814 309 mips64_fill_gregset (regcache, &regs, regno);
d37eb719
DJ
310
311 if (ptrace (PTRACE_SETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
312 perror_with_name (_("Couldn't set registers"));
313 }
314
315 if (regno == -1 || is_fp)
316 {
317 mips64_elf_fpregset_t fp_regs;
318
319 if (ptrace (PTRACE_GETFPREGS, tid, 0L,
320 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
321 perror_with_name (_("Couldn't get FP registers"));
322
56be3814 323 mips64_fill_fpregset (regcache, &fp_regs, regno);
d37eb719
DJ
324
325 if (ptrace (PTRACE_SETFPREGS, tid, 0L,
326 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
327 perror_with_name (_("Couldn't set FP registers"));
328 }
329}
330
331/* Fetch REGNO (or all registers if REGNO == -1) from the target
332 using any working method. */
333
334static void
56be3814 335mips64_linux_fetch_registers (struct regcache *regcache, int regnum)
d37eb719
DJ
336{
337 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
338 if (have_ptrace_regsets)
56be3814 339 mips64_linux_regsets_fetch_registers (regcache, regnum);
d37eb719
DJ
340
341 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
342 back to PTRACE_PEEKUSER. */
343 if (!have_ptrace_regsets)
56be3814 344 super_fetch_registers (regcache, regnum);
d37eb719
DJ
345}
346
347/* Store REGNO (or all registers if REGNO == -1) to the target
348 using any working method. */
349
350static void
56be3814 351mips64_linux_store_registers (struct regcache *regcache, int regnum)
d37eb719
DJ
352{
353 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
354 if (have_ptrace_regsets)
56be3814 355 mips64_linux_regsets_store_registers (regcache, regnum);
d37eb719
DJ
356
357 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
358 back to PTRACE_PEEKUSER. */
359 if (!have_ptrace_regsets)
56be3814 360 super_store_registers (regcache, regnum);
d37eb719
DJ
361}
362
910122bf
UW
363/* Return the address in the core dump or inferior of register
364 REGNO. */
365
366static CORE_ADDR
367mips_linux_register_u_offset (int regno)
368{
dda0c97e
UW
369 if (mips_abi_regsize (current_gdbarch) == 8)
370 return mips64_linux_register_addr (regno);
371 else
372 return mips_linux_register_addr (regno);
910122bf
UW
373}
374
10d6c8cd
DJ
375void _initialize_mips_linux_nat (void);
376
377void
378_initialize_mips_linux_nat (void)
379{
910122bf 380 struct target_ops *t = linux_trad_target (mips_linux_register_u_offset);
d37eb719
DJ
381
382 super_fetch_registers = t->to_fetch_registers;
383 super_store_registers = t->to_store_registers;
384
385 t->to_fetch_registers = mips64_linux_fetch_registers;
386 t->to_store_registers = mips64_linux_store_registers;
387
f973ed9c 388 linux_nat_add_target (t);
10d6c8cd 389}
This page took 0.607311 seconds and 4 git commands to generate.