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) (int);
48 void (*super_store_registers) (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 (gdb_gregset_t *gregsetp)
182 if (mips_isa_regsize (current_gdbarch) == 4)
183 mips_supply_gregset (current_regcache, (void *) gregsetp);
185 mips64_supply_gregset (current_regcache, (void *) gregsetp);
189 fill_gregset (gdb_gregset_t *gregsetp, int regno)
191 if (mips_isa_regsize (current_gdbarch) == 4)
192 mips_fill_gregset (current_regcache, (void *) gregsetp, regno);
194 mips64_fill_gregset (current_regcache, (void *) gregsetp, regno);
198 supply_fpregset (gdb_fpregset_t *fpregsetp)
200 if (mips_isa_regsize (current_gdbarch) == 4)
201 mips_supply_fpregset (current_regcache, (void *) fpregsetp);
203 mips64_supply_fpregset (current_regcache, (void *) fpregsetp);
207 fill_fpregset (gdb_fpregset_t *fpregsetp, int regno)
209 if (mips_isa_regsize (current_gdbarch) == 4)
210 mips_fill_fpregset (current_regcache, (void *) fpregsetp, regno);
212 mips64_fill_fpregset (current_regcache, (void *) fpregsetp, regno);
216 /* Fetch REGNO (or all registers if REGNO == -1) from the target
217 using PTRACE_GETREGS et al. */
220 mips64_linux_regsets_fetch_registers (int regno)
225 if (regno >= mips_regnum (current_gdbarch)->fp0
226 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
228 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
230 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
235 tid = ptid_get_lwp (inferior_ptid);
237 tid = ptid_get_pid (inferior_ptid);
239 if (regno == -1 || !is_fp)
241 mips64_elf_gregset_t regs;
243 if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) ®s) == -1)
247 have_ptrace_regsets = 0;
250 perror_with_name (_("Couldn't get registers"));
253 mips64_supply_gregset (current_regcache,
254 (const mips64_elf_gregset_t *) ®s);
257 if (regno == -1 || is_fp)
259 mips64_elf_fpregset_t fp_regs;
261 if (ptrace (PTRACE_GETFPREGS, tid, 0L,
262 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
266 have_ptrace_regsets = 0;
269 perror_with_name (_("Couldn't get FP registers"));
272 mips64_supply_fpregset (current_regcache,
273 (const mips64_elf_fpregset_t *) &fp_regs);
277 /* Store REGNO (or all registers if REGNO == -1) to the target
278 using PTRACE_SETREGS et al. */
281 mips64_linux_regsets_store_registers (int regno)
286 if (regno >= mips_regnum (current_gdbarch)->fp0
287 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
289 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
291 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
296 tid = ptid_get_lwp (inferior_ptid);
298 tid = ptid_get_pid (inferior_ptid);
300 if (regno == -1 || !is_fp)
302 mips64_elf_gregset_t regs;
304 if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) ®s) == -1)
305 perror_with_name (_("Couldn't get registers"));
307 mips64_fill_gregset (current_regcache, ®s, regno);
309 if (ptrace (PTRACE_SETREGS, tid, 0L, (PTRACE_TYPE_ARG3) ®s) == -1)
310 perror_with_name (_("Couldn't set registers"));
313 if (regno == -1 || is_fp)
315 mips64_elf_fpregset_t fp_regs;
317 if (ptrace (PTRACE_GETFPREGS, tid, 0L,
318 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
319 perror_with_name (_("Couldn't get FP registers"));
321 mips64_fill_fpregset (current_regcache, &fp_regs, regno);
323 if (ptrace (PTRACE_SETFPREGS, tid, 0L,
324 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
325 perror_with_name (_("Couldn't set FP registers"));
329 /* Fetch REGNO (or all registers if REGNO == -1) from the target
330 using any working method. */
333 mips64_linux_fetch_registers (int regnum)
335 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
336 if (have_ptrace_regsets)
337 mips64_linux_regsets_fetch_registers (regnum);
339 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
340 back to PTRACE_PEEKUSER. */
341 if (!have_ptrace_regsets)
342 super_fetch_registers (regnum);
345 /* Store REGNO (or all registers if REGNO == -1) to the target
346 using any working method. */
349 mips64_linux_store_registers (int regnum)
351 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
352 if (have_ptrace_regsets)
353 mips64_linux_regsets_store_registers (regnum);
355 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
356 back to PTRACE_PEEKUSER. */
357 if (!have_ptrace_regsets)
358 super_store_registers (regnum);
361 /* Return the address in the core dump or inferior of register
365 mips_linux_register_u_offset (int regno)
367 if (mips_abi_regsize (current_gdbarch) == 8)
368 return mips64_linux_register_addr (regno);
370 return mips_linux_register_addr (regno);
373 void _initialize_mips_linux_nat (void);
376 _initialize_mips_linux_nat (void)
378 struct target_ops *t = linux_trad_target (mips_linux_register_u_offset);
380 super_fetch_registers = t->to_fetch_registers;
381 super_store_registers = t->to_store_registers;
383 t->to_fetch_registers = mips64_linux_fetch_registers;
384 t->to_store_registers = mips64_linux_store_registers;
386 linux_nat_add_target (t);