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"
27 #include "linux-nat.h"
28 #include "mips-linux-tdep.h"
30 #include "gdb_proc_service.h"
32 #include <sys/ptrace.h>
34 #ifndef PTRACE_GET_THREAD_AREA
35 #define PTRACE_GET_THREAD_AREA 25
38 /* Assume that we have PTRACE_GETREGS et al. support. If we do not,
39 we'll clear this and use PTRACE_PEEKUSER instead. */
40 static int have_ptrace_regsets = 1;
42 /* Saved function pointers to fetch and store a single register using
43 PTRACE_PEEKUSER and PTRACE_POKEUSER. */
45 void (*super_fetch_registers) (int);
46 void (*super_store_registers) (int);
48 /* Pseudo registers can not be read. ptrace does not provide a way to
49 read (or set) MIPS_PS_REGNUM, and there's no point in reading or
50 setting MIPS_ZERO_REGNUM. We also can not set BADVADDR, CAUSE, or
51 FCRIR via ptrace(). */
54 mips_linux_cannot_fetch_register (int regno)
56 if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
58 else if (regno >= mips_regnum (current_gdbarch)->fp0
59 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
61 else if (regno == mips_regnum (current_gdbarch)->lo
62 || regno == mips_regnum (current_gdbarch)->hi
63 || regno == mips_regnum (current_gdbarch)->badvaddr
64 || regno == mips_regnum (current_gdbarch)->cause
65 || regno == mips_regnum (current_gdbarch)->pc
66 || regno == mips_regnum (current_gdbarch)->fp_control_status
67 || regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
74 mips_linux_cannot_store_register (int regno)
76 if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
78 else if (regno >= FP0_REGNUM && regno <= FP0_REGNUM + 32)
80 else if (regno == mips_regnum (current_gdbarch)->lo
81 || regno == mips_regnum (current_gdbarch)->hi
82 || regno == mips_regnum (current_gdbarch)->pc
83 || regno == mips_regnum (current_gdbarch)->fp_control_status)
89 /* Map gdb internal register number to ptrace ``address''.
90 These ``addresses'' are normally defined in <asm/ptrace.h>. */
93 mips_linux_register_addr (int regno)
97 if (regno < 0 || regno >= NUM_REGS)
98 error (_("Bogon register number %d."), regno);
102 else if ((regno >= mips_regnum (current_gdbarch)->fp0)
103 && (regno < mips_regnum (current_gdbarch)->fp0 + 32))
104 regaddr = FPR_BASE + (regno - mips_regnum (current_gdbarch)->fp0);
105 else if (regno == mips_regnum (current_gdbarch)->pc)
107 else if (regno == mips_regnum (current_gdbarch)->cause)
109 else if (regno == mips_regnum (current_gdbarch)->badvaddr)
111 else if (regno == mips_regnum (current_gdbarch)->lo)
113 else if (regno == mips_regnum (current_gdbarch)->hi)
115 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
117 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
120 error (_("Unknowable register number %d."), regno);
126 mips64_linux_register_addr (int regno)
130 if (regno < 0 || regno >= NUM_REGS)
131 error (_("Bogon register number %d."), regno);
135 else if ((regno >= mips_regnum (current_gdbarch)->fp0)
136 && (regno < mips_regnum (current_gdbarch)->fp0 + 32))
137 regaddr = MIPS64_FPR_BASE + (regno - FP0_REGNUM);
138 else if (regno == mips_regnum (current_gdbarch)->pc)
140 else if (regno == mips_regnum (current_gdbarch)->cause)
141 regaddr = MIPS64_CAUSE;
142 else if (regno == mips_regnum (current_gdbarch)->badvaddr)
143 regaddr = MIPS64_BADVADDR;
144 else if (regno == mips_regnum (current_gdbarch)->lo)
145 regaddr = MIPS64_MMLO;
146 else if (regno == mips_regnum (current_gdbarch)->hi)
147 regaddr = MIPS64_MMHI;
148 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
149 regaddr = MIPS64_FPC_CSR;
150 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
151 regaddr = MIPS64_FPC_EIR;
153 error (_("Unknowable register number %d."), regno);
158 /* Fetch the thread-local storage pointer for libthread_db. */
161 ps_get_thread_area (const struct ps_prochandle *ph,
162 lwpid_t lwpid, int idx, void **base)
164 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
167 /* IDX is the bias from the thread pointer to the beginning of the
168 thread descriptor. It has to be subtracted due to implementation
169 quirks in libthread_db. */
170 *base = (void *) ((char *)*base - idx);
175 /* Fetch REGNO (or all registers if REGNO == -1) from the target
176 using PTRACE_GETREGS et al. */
179 mips64_linux_regsets_fetch_registers (int regno)
184 if (regno >= mips_regnum (current_gdbarch)->fp0
185 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
187 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
189 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
194 tid = ptid_get_lwp (inferior_ptid);
196 tid = ptid_get_pid (inferior_ptid);
198 if (regno == -1 || !is_fp)
200 mips64_elf_gregset_t regs;
202 if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) ®s) == -1)
206 have_ptrace_regsets = 0;
209 perror_with_name (_("Couldn't get registers"));
212 mips64_supply_gregset (®s);
215 if (regno == -1 || is_fp)
217 mips64_elf_fpregset_t fp_regs;
219 if (ptrace (PTRACE_GETFPREGS, tid, 0L,
220 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
224 have_ptrace_regsets = 0;
227 perror_with_name (_("Couldn't get FP registers"));
230 mips64_supply_fpregset (&fp_regs);
234 /* Store REGNO (or all registers if REGNO == -1) to the target
235 using PTRACE_SETREGS et al. */
238 mips64_linux_regsets_store_registers (int regno)
243 if (regno >= mips_regnum (current_gdbarch)->fp0
244 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
246 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
248 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
253 tid = ptid_get_lwp (inferior_ptid);
255 tid = ptid_get_pid (inferior_ptid);
257 if (regno == -1 || !is_fp)
259 mips64_elf_gregset_t regs;
261 if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) ®s) == -1)
262 perror_with_name (_("Couldn't get registers"));
264 mips64_fill_gregset (®s, regno);
266 if (ptrace (PTRACE_SETREGS, tid, 0L, (PTRACE_TYPE_ARG3) ®s) == -1)
267 perror_with_name (_("Couldn't set registers"));
270 if (regno == -1 || is_fp)
272 mips64_elf_fpregset_t fp_regs;
274 if (ptrace (PTRACE_GETFPREGS, tid, 0L,
275 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
276 perror_with_name (_("Couldn't get FP registers"));
278 mips64_fill_fpregset (&fp_regs, regno);
280 if (ptrace (PTRACE_SETFPREGS, tid, 0L,
281 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
282 perror_with_name (_("Couldn't set FP registers"));
286 /* Fetch REGNO (or all registers if REGNO == -1) from the target
287 using any working method. */
290 mips64_linux_fetch_registers (int regnum)
292 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
293 if (have_ptrace_regsets)
294 mips64_linux_regsets_fetch_registers (regnum);
296 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
297 back to PTRACE_PEEKUSER. */
298 if (!have_ptrace_regsets)
299 super_fetch_registers (regnum);
302 /* Store REGNO (or all registers if REGNO == -1) to the target
303 using any working method. */
306 mips64_linux_store_registers (int regnum)
308 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
309 if (have_ptrace_regsets)
310 mips64_linux_regsets_store_registers (regnum);
312 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
313 back to PTRACE_PEEKUSER. */
314 if (!have_ptrace_regsets)
315 super_store_registers (regnum);
318 /* Return the address in the core dump or inferior of register
322 mips_linux_register_u_offset (int regno)
324 if (mips_abi_regsize (current_gdbarch) == 8)
325 return mips64_linux_register_addr (regno);
327 return mips_linux_register_addr (regno);
330 void _initialize_mips_linux_nat (void);
333 _initialize_mips_linux_nat (void)
335 struct target_ops *t = linux_trad_target (mips_linux_register_u_offset);
337 super_fetch_registers = t->to_fetch_registers;
338 super_store_registers = t->to_store_registers;
340 t->to_fetch_registers = mips64_linux_fetch_registers;
341 t->to_store_registers = mips64_linux_store_registers;
343 linux_nat_add_target (t);