* inf-ptrace.c (inf_ptrace_register_u_offset): Adapt parameter list.
[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
dda0c97e 50/* Map gdb internal register number to ptrace ``address''.
7714d83a
UW
51 These ``addresses'' are normally defined in <asm/ptrace.h>.
52
53 ptrace does not provide a way to read (or set) MIPS_PS_REGNUM,
54 and there's no point in reading or setting MIPS_ZERO_REGNUM.
55 We also can not set BADVADDR, CAUSE, or FCRIR via ptrace(). */
dda0c97e
UW
56
57static CORE_ADDR
7714d83a 58mips_linux_register_addr (struct gdbarch *gdbarch, int regno, int store)
dda0c97e 59{
7714d83a 60 CORE_ADDR regaddr;
dda0c97e
UW
61
62 if (regno < 0 || regno >= NUM_REGS)
63 error (_("Bogon register number %d."), regno);
64
7714d83a 65 if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
dda0c97e 66 regaddr = regno;
7714d83a
UW
67 else if ((regno >= mips_regnum (gdbarch)->fp0)
68 && (regno < mips_regnum (gdbarch)->fp0 + 32))
69 regaddr = FPR_BASE + (regno - mips_regnum (gdbarch)->fp0);
70 else if (regno == mips_regnum (gdbarch)->pc)
dda0c97e 71 regaddr = PC;
7714d83a
UW
72 else if (regno == mips_regnum (gdbarch)->cause)
73 regaddr = store? (CORE_ADDR) -1 : CAUSE;
74 else if (regno == mips_regnum (gdbarch)->badvaddr)
75 regaddr = store? (CORE_ADDR) -1 : BADVADDR;
76 else if (regno == mips_regnum (gdbarch)->lo)
dda0c97e 77 regaddr = MMLO;
7714d83a 78 else if (regno == mips_regnum (gdbarch)->hi)
dda0c97e 79 regaddr = MMHI;
7714d83a 80 else if (regno == mips_regnum (gdbarch)->fp_control_status)
dda0c97e 81 regaddr = FPC_CSR;
7714d83a
UW
82 else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
83 regaddr = store? (CORE_ADDR) -1 : FPC_EIR;
dda0c97e 84 else
7714d83a 85 regaddr = (CORE_ADDR) -1;
dda0c97e
UW
86
87 return regaddr;
88}
89
90static CORE_ADDR
7714d83a 91mips64_linux_register_addr (struct gdbarch *gdbarch, int regno, int store)
dda0c97e 92{
7714d83a 93 CORE_ADDR regaddr;
dda0c97e
UW
94
95 if (regno < 0 || regno >= NUM_REGS)
96 error (_("Bogon register number %d."), regno);
97
7714d83a 98 if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
dda0c97e 99 regaddr = regno;
7714d83a
UW
100 else if ((regno >= mips_regnum (gdbarch)->fp0)
101 && (regno < mips_regnum (gdbarch)->fp0 + 32))
dda0c97e 102 regaddr = MIPS64_FPR_BASE + (regno - FP0_REGNUM);
7714d83a 103 else if (regno == mips_regnum (gdbarch)->pc)
dda0c97e 104 regaddr = MIPS64_PC;
7714d83a
UW
105 else if (regno == mips_regnum (gdbarch)->cause)
106 regaddr = store? (CORE_ADDR) -1 : MIPS64_CAUSE;
107 else if (regno == mips_regnum (gdbarch)->badvaddr)
108 regaddr = store? (CORE_ADDR) -1 : MIPS64_BADVADDR;
109 else if (regno == mips_regnum (gdbarch)->lo)
dda0c97e 110 regaddr = MIPS64_MMLO;
7714d83a 111 else if (regno == mips_regnum (gdbarch)->hi)
dda0c97e 112 regaddr = MIPS64_MMHI;
7714d83a 113 else if (regno == mips_regnum (gdbarch)->fp_control_status)
dda0c97e 114 regaddr = MIPS64_FPC_CSR;
7714d83a
UW
115 else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
116 regaddr = store? (CORE_ADDR) -1 : MIPS64_FPC_EIR;
dda0c97e 117 else
7714d83a 118 regaddr = (CORE_ADDR) -1;
dda0c97e
UW
119
120 return regaddr;
121}
122
dc60ece8
DJ
123/* Fetch the thread-local storage pointer for libthread_db. */
124
125ps_err_e
126ps_get_thread_area (const struct ps_prochandle *ph,
127 lwpid_t lwpid, int idx, void **base)
128{
129 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
130 return PS_ERR;
131
132 /* IDX is the bias from the thread pointer to the beginning of the
133 thread descriptor. It has to be subtracted due to implementation
134 quirks in libthread_db. */
135 *base = (void *) ((char *)*base - idx);
136
137 return PS_OK;
138}
139
3e00823e
UW
140/* Wrapper functions. These are only used by libthread_db. */
141
142void
7f7fe91e 143supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
3e00823e
UW
144{
145 if (mips_isa_regsize (current_gdbarch) == 4)
7f7fe91e 146 mips_supply_gregset (regcache, (const mips_elf_gregset_t *) gregsetp);
3e00823e 147 else
7f7fe91e 148 mips64_supply_gregset (regcache, (const mips64_elf_gregset_t *) gregsetp);
3e00823e
UW
149}
150
151void
7f7fe91e
UW
152fill_gregset (const struct regcache *regcache,
153 gdb_gregset_t *gregsetp, int regno)
3e00823e
UW
154{
155 if (mips_isa_regsize (current_gdbarch) == 4)
7f7fe91e 156 mips_fill_gregset (regcache, (mips_elf_gregset_t *) gregsetp, regno);
3e00823e 157 else
7f7fe91e 158 mips64_fill_gregset (regcache, (mips64_elf_gregset_t *) gregsetp, regno);
3e00823e
UW
159}
160
161void
7f7fe91e 162supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
3e00823e
UW
163{
164 if (mips_isa_regsize (current_gdbarch) == 4)
7f7fe91e 165 mips_supply_fpregset (regcache, (const mips_elf_fpregset_t *) fpregsetp);
3e00823e 166 else
7f7fe91e 167 mips64_supply_fpregset (regcache, (const mips64_elf_fpregset_t *) fpregsetp);
3e00823e
UW
168}
169
170void
7f7fe91e
UW
171fill_fpregset (const struct regcache *regcache,
172 gdb_fpregset_t *fpregsetp, int regno)
3e00823e
UW
173{
174 if (mips_isa_regsize (current_gdbarch) == 4)
7f7fe91e 175 mips_fill_fpregset (regcache, (mips_elf_fpregset_t *) fpregsetp, regno);
3e00823e 176 else
7f7fe91e 177 mips64_fill_fpregset (regcache, (mips64_elf_fpregset_t *) fpregsetp, regno);
3e00823e
UW
178}
179
180
d37eb719
DJ
181/* Fetch REGNO (or all registers if REGNO == -1) from the target
182 using PTRACE_GETREGS et al. */
183
184static void
56be3814 185mips64_linux_regsets_fetch_registers (struct regcache *regcache, int regno)
d37eb719
DJ
186{
187 int is_fp;
188 int tid;
189
190 if (regno >= mips_regnum (current_gdbarch)->fp0
191 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
192 is_fp = 1;
193 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
194 is_fp = 1;
195 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
196 is_fp = 1;
197 else
198 is_fp = 0;
199
200 tid = ptid_get_lwp (inferior_ptid);
201 if (tid == 0)
202 tid = ptid_get_pid (inferior_ptid);
203
204 if (regno == -1 || !is_fp)
205 {
206 mips64_elf_gregset_t regs;
207
208 if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
209 {
210 if (errno == EIO)
211 {
212 have_ptrace_regsets = 0;
213 return;
214 }
215 perror_with_name (_("Couldn't get registers"));
216 }
217
56be3814 218 mips64_supply_gregset (regcache,
28f5035f 219 (const mips64_elf_gregset_t *) &regs);
d37eb719
DJ
220 }
221
222 if (regno == -1 || is_fp)
223 {
224 mips64_elf_fpregset_t fp_regs;
225
226 if (ptrace (PTRACE_GETFPREGS, tid, 0L,
227 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
228 {
229 if (errno == EIO)
230 {
231 have_ptrace_regsets = 0;
232 return;
233 }
234 perror_with_name (_("Couldn't get FP registers"));
235 }
236
56be3814 237 mips64_supply_fpregset (regcache,
28f5035f 238 (const mips64_elf_fpregset_t *) &fp_regs);
d37eb719
DJ
239 }
240}
241
242/* Store REGNO (or all registers if REGNO == -1) to the target
243 using PTRACE_SETREGS et al. */
244
245static void
56be3814 246mips64_linux_regsets_store_registers (const struct regcache *regcache, int regno)
d37eb719
DJ
247{
248 int is_fp;
249 int tid;
250
251 if (regno >= mips_regnum (current_gdbarch)->fp0
252 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
253 is_fp = 1;
254 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
255 is_fp = 1;
256 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
257 is_fp = 1;
258 else
259 is_fp = 0;
260
261 tid = ptid_get_lwp (inferior_ptid);
262 if (tid == 0)
263 tid = ptid_get_pid (inferior_ptid);
264
265 if (regno == -1 || !is_fp)
266 {
267 mips64_elf_gregset_t regs;
268
269 if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
270 perror_with_name (_("Couldn't get registers"));
271
56be3814 272 mips64_fill_gregset (regcache, &regs, regno);
d37eb719
DJ
273
274 if (ptrace (PTRACE_SETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
275 perror_with_name (_("Couldn't set registers"));
276 }
277
278 if (regno == -1 || is_fp)
279 {
280 mips64_elf_fpregset_t fp_regs;
281
282 if (ptrace (PTRACE_GETFPREGS, tid, 0L,
283 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
284 perror_with_name (_("Couldn't get FP registers"));
285
56be3814 286 mips64_fill_fpregset (regcache, &fp_regs, regno);
d37eb719
DJ
287
288 if (ptrace (PTRACE_SETFPREGS, tid, 0L,
289 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
290 perror_with_name (_("Couldn't set FP registers"));
291 }
292}
293
294/* Fetch REGNO (or all registers if REGNO == -1) from the target
295 using any working method. */
296
297static void
56be3814 298mips64_linux_fetch_registers (struct regcache *regcache, int regnum)
d37eb719
DJ
299{
300 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
301 if (have_ptrace_regsets)
56be3814 302 mips64_linux_regsets_fetch_registers (regcache, regnum);
d37eb719
DJ
303
304 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
305 back to PTRACE_PEEKUSER. */
306 if (!have_ptrace_regsets)
56be3814 307 super_fetch_registers (regcache, regnum);
d37eb719
DJ
308}
309
310/* Store REGNO (or all registers if REGNO == -1) to the target
311 using any working method. */
312
313static void
56be3814 314mips64_linux_store_registers (struct regcache *regcache, int regnum)
d37eb719
DJ
315{
316 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
317 if (have_ptrace_regsets)
56be3814 318 mips64_linux_regsets_store_registers (regcache, regnum);
d37eb719
DJ
319
320 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
321 back to PTRACE_PEEKUSER. */
322 if (!have_ptrace_regsets)
56be3814 323 super_store_registers (regcache, regnum);
d37eb719
DJ
324}
325
910122bf
UW
326/* Return the address in the core dump or inferior of register
327 REGNO. */
328
329static CORE_ADDR
7714d83a 330mips_linux_register_u_offset (struct gdbarch *gdbarch, int regno, int store_p)
910122bf 331{
7714d83a
UW
332 if (mips_abi_regsize (gdbarch) == 8)
333 return mips64_linux_register_addr (gdbarch, regno, store_p);
dda0c97e 334 else
7714d83a 335 return mips_linux_register_addr (gdbarch, regno, store_p);
910122bf
UW
336}
337
10d6c8cd
DJ
338void _initialize_mips_linux_nat (void);
339
340void
341_initialize_mips_linux_nat (void)
342{
910122bf 343 struct target_ops *t = linux_trad_target (mips_linux_register_u_offset);
d37eb719
DJ
344
345 super_fetch_registers = t->to_fetch_registers;
346 super_store_registers = t->to_store_registers;
347
348 t->to_fetch_registers = mips64_linux_fetch_registers;
349 t->to_store_registers = mips64_linux_store_registers;
350
f973ed9c 351 linux_nat_add_target (t);
10d6c8cd 352}
This page took 0.558408 seconds and 4 git commands to generate.