1 /* Native-dependent code for GNU/Linux on MIPS processors.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
25 #include "mips-tdep.h"
28 #include "linux-nat.h"
29 #include "mips-linux-tdep.h"
31 #include "gdb_proc_service.h"
34 #include <sys/ptrace.h>
36 #ifndef PTRACE_GET_THREAD_AREA
37 #define PTRACE_GET_THREAD_AREA 25
40 /* Assume that we have PTRACE_GETREGS et al. support. If we do not,
41 we'll clear this and use PTRACE_PEEKUSER instead. */
42 static int have_ptrace_regsets = 1;
44 /* Saved function pointers to fetch and store a single register using
45 PTRACE_PEEKUSER and PTRACE_POKEUSER. */
47 void (*super_fetch_registers) (struct regcache *, int);
48 void (*super_store_registers) (struct regcache *, int);
50 /* Pseudo registers can not be read. ptrace does not provide a way to
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(). */
56 mips_linux_cannot_fetch_register (int regno)
58 if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
60 else if (regno >= mips_regnum (current_gdbarch)->fp0
61 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
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)
76 mips_linux_cannot_store_register (int regno)
78 if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
80 else if (regno >= FP0_REGNUM && regno <= FP0_REGNUM + 32)
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)
91 /* Map gdb internal register number to ptrace ``address''.
92 These ``addresses'' are normally defined in <asm/ptrace.h>. */
95 mips_linux_register_addr (int regno)
99 if (regno < 0 || regno >= NUM_REGS)
100 error (_("Bogon register number %d."), 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)
109 else if (regno == mips_regnum (current_gdbarch)->cause)
111 else if (regno == mips_regnum (current_gdbarch)->badvaddr)
113 else if (regno == mips_regnum (current_gdbarch)->lo)
115 else if (regno == mips_regnum (current_gdbarch)->hi)
117 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
119 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
122 error (_("Unknowable register number %d."), regno);
128 mips64_linux_register_addr (int regno)
132 if (regno < 0 || regno >= NUM_REGS)
133 error (_("Bogon register number %d."), 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)
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;
155 error (_("Unknowable register number %d."), regno);
160 /* Fetch the thread-local storage pointer for libthread_db. */
163 ps_get_thread_area (const struct ps_prochandle *ph,
164 lwpid_t lwpid, int idx, void **base)
166 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
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);
177 /* Wrapper functions. These are only used by libthread_db. */
180 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
182 if (mips_isa_regsize (current_gdbarch) == 4)
183 mips_supply_gregset (regcache, (const mips_elf_gregset_t *) gregsetp);
185 mips64_supply_gregset (regcache, (const mips64_elf_gregset_t *) gregsetp);
189 fill_gregset (const struct regcache *regcache,
190 gdb_gregset_t *gregsetp, int regno)
192 if (mips_isa_regsize (current_gdbarch) == 4)
193 mips_fill_gregset (regcache, (mips_elf_gregset_t *) gregsetp, regno);
195 mips64_fill_gregset (regcache, (mips64_elf_gregset_t *) gregsetp, regno);
199 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
201 if (mips_isa_regsize (current_gdbarch) == 4)
202 mips_supply_fpregset (regcache, (const mips_elf_fpregset_t *) fpregsetp);
204 mips64_supply_fpregset (regcache, (const mips64_elf_fpregset_t *) fpregsetp);
208 fill_fpregset (const struct regcache *regcache,
209 gdb_fpregset_t *fpregsetp, int regno)
211 if (mips_isa_regsize (current_gdbarch) == 4)
212 mips_fill_fpregset (regcache, (mips_elf_fpregset_t *) fpregsetp, regno);
214 mips64_fill_fpregset (regcache, (mips64_elf_fpregset_t *) fpregsetp, regno);
218 /* Fetch REGNO (or all registers if REGNO == -1) from the target
219 using PTRACE_GETREGS et al. */
222 mips64_linux_regsets_fetch_registers (struct regcache *regcache, int regno)
227 if (regno >= mips_regnum (current_gdbarch)->fp0
228 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
230 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
232 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
237 tid = ptid_get_lwp (inferior_ptid);
239 tid = ptid_get_pid (inferior_ptid);
241 if (regno == -1 || !is_fp)
243 mips64_elf_gregset_t regs;
245 if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) ®s) == -1)
249 have_ptrace_regsets = 0;
252 perror_with_name (_("Couldn't get registers"));
255 mips64_supply_gregset (regcache,
256 (const mips64_elf_gregset_t *) ®s);
259 if (regno == -1 || is_fp)
261 mips64_elf_fpregset_t fp_regs;
263 if (ptrace (PTRACE_GETFPREGS, tid, 0L,
264 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
268 have_ptrace_regsets = 0;
271 perror_with_name (_("Couldn't get FP registers"));
274 mips64_supply_fpregset (regcache,
275 (const mips64_elf_fpregset_t *) &fp_regs);
279 /* Store REGNO (or all registers if REGNO == -1) to the target
280 using PTRACE_SETREGS et al. */
283 mips64_linux_regsets_store_registers (const struct regcache *regcache, int regno)
288 if (regno >= mips_regnum (current_gdbarch)->fp0
289 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
291 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
293 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
298 tid = ptid_get_lwp (inferior_ptid);
300 tid = ptid_get_pid (inferior_ptid);
302 if (regno == -1 || !is_fp)
304 mips64_elf_gregset_t regs;
306 if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) ®s) == -1)
307 perror_with_name (_("Couldn't get registers"));
309 mips64_fill_gregset (regcache, ®s, regno);
311 if (ptrace (PTRACE_SETREGS, tid, 0L, (PTRACE_TYPE_ARG3) ®s) == -1)
312 perror_with_name (_("Couldn't set registers"));
315 if (regno == -1 || is_fp)
317 mips64_elf_fpregset_t fp_regs;
319 if (ptrace (PTRACE_GETFPREGS, tid, 0L,
320 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
321 perror_with_name (_("Couldn't get FP registers"));
323 mips64_fill_fpregset (regcache, &fp_regs, regno);
325 if (ptrace (PTRACE_SETFPREGS, tid, 0L,
326 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
327 perror_with_name (_("Couldn't set FP registers"));
331 /* Fetch REGNO (or all registers if REGNO == -1) from the target
332 using any working method. */
335 mips64_linux_fetch_registers (struct regcache *regcache, int regnum)
337 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
338 if (have_ptrace_regsets)
339 mips64_linux_regsets_fetch_registers (regcache, regnum);
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)
344 super_fetch_registers (regcache, regnum);
347 /* Store REGNO (or all registers if REGNO == -1) to the target
348 using any working method. */
351 mips64_linux_store_registers (struct regcache *regcache, int regnum)
353 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
354 if (have_ptrace_regsets)
355 mips64_linux_regsets_store_registers (regcache, regnum);
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)
360 super_store_registers (regcache, regnum);
363 /* Return the address in the core dump or inferior of register
367 mips_linux_register_u_offset (int regno)
369 if (mips_abi_regsize (current_gdbarch) == 8)
370 return mips64_linux_register_addr (regno);
372 return mips_linux_register_addr (regno);
375 void _initialize_mips_linux_nat (void);
378 _initialize_mips_linux_nat (void)
380 struct target_ops *t = linux_trad_target (mips_linux_register_u_offset);
382 super_fetch_registers = t->to_fetch_registers;
383 super_store_registers = t->to_store_registers;
385 t->to_fetch_registers = mips64_linux_fetch_registers;
386 t->to_store_registers = mips64_linux_store_registers;
388 linux_nat_add_target (t);