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"
31 #include "gdb_proc_service.h"
33 #include <sys/ptrace.h>
35 #ifndef PTRACE_GET_THREAD_AREA
36 #define PTRACE_GET_THREAD_AREA 25
39 /* Assume that we have PTRACE_GETREGS et al. support. If we do not,
40 we'll clear this and use PTRACE_PEEKUSER instead. */
41 static int have_ptrace_regsets = 1;
43 /* Saved function pointers to fetch and store a single register using
44 PTRACE_PEEKUSER and PTRACE_POKEUSER. */
46 void (*super_fetch_registers) (int);
47 void (*super_store_registers) (int);
49 /* Pseudo registers can not be read. ptrace does not provide a way to
50 read (or set) MIPS_PS_REGNUM, and there's no point in reading or
51 setting MIPS_ZERO_REGNUM. We also can not set BADVADDR, CAUSE, or
52 FCRIR via ptrace(). */
55 mips_linux_cannot_fetch_register (int regno)
57 if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
59 else if (regno >= mips_regnum (current_gdbarch)->fp0
60 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
62 else if (regno == mips_regnum (current_gdbarch)->lo
63 || regno == mips_regnum (current_gdbarch)->hi
64 || regno == mips_regnum (current_gdbarch)->badvaddr
65 || regno == mips_regnum (current_gdbarch)->cause
66 || regno == mips_regnum (current_gdbarch)->pc
67 || regno == mips_regnum (current_gdbarch)->fp_control_status
68 || regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
75 mips_linux_cannot_store_register (int regno)
77 if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
79 else if (regno >= FP0_REGNUM && regno <= FP0_REGNUM + 32)
81 else if (regno == mips_regnum (current_gdbarch)->lo
82 || regno == mips_regnum (current_gdbarch)->hi
83 || regno == mips_regnum (current_gdbarch)->pc
84 || regno == mips_regnum (current_gdbarch)->fp_control_status)
90 /* Fetch the thread-local storage pointer for libthread_db. */
93 ps_get_thread_area (const struct ps_prochandle *ph,
94 lwpid_t lwpid, int idx, void **base)
96 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
99 /* IDX is the bias from the thread pointer to the beginning of the
100 thread descriptor. It has to be subtracted due to implementation
101 quirks in libthread_db. */
102 *base = (void *) ((char *)*base - idx);
107 /* Fetch REGNO (or all registers if REGNO == -1) from the target
108 using PTRACE_GETREGS et al. */
111 mips64_linux_regsets_fetch_registers (int regno)
116 if (regno >= mips_regnum (current_gdbarch)->fp0
117 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
119 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
121 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
126 tid = ptid_get_lwp (inferior_ptid);
128 tid = ptid_get_pid (inferior_ptid);
130 if (regno == -1 || !is_fp)
132 mips64_elf_gregset_t regs;
134 if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) ®s) == -1)
138 have_ptrace_regsets = 0;
141 perror_with_name (_("Couldn't get registers"));
144 mips64_supply_gregset (®s);
147 if (regno == -1 || is_fp)
149 mips64_elf_fpregset_t fp_regs;
151 if (ptrace (PTRACE_GETFPREGS, tid, 0L,
152 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
156 have_ptrace_regsets = 0;
159 perror_with_name (_("Couldn't get FP registers"));
162 mips64_supply_fpregset (&fp_regs);
166 /* Store REGNO (or all registers if REGNO == -1) to the target
167 using PTRACE_SETREGS et al. */
170 mips64_linux_regsets_store_registers (int regno)
175 if (regno >= mips_regnum (current_gdbarch)->fp0
176 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
178 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
180 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
185 tid = ptid_get_lwp (inferior_ptid);
187 tid = ptid_get_pid (inferior_ptid);
189 if (regno == -1 || !is_fp)
191 mips64_elf_gregset_t regs;
193 if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) ®s) == -1)
194 perror_with_name (_("Couldn't get registers"));
196 mips64_fill_gregset (®s, regno);
198 if (ptrace (PTRACE_SETREGS, tid, 0L, (PTRACE_TYPE_ARG3) ®s) == -1)
199 perror_with_name (_("Couldn't set registers"));
202 if (regno == -1 || is_fp)
204 mips64_elf_fpregset_t fp_regs;
206 if (ptrace (PTRACE_GETFPREGS, tid, 0L,
207 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
208 perror_with_name (_("Couldn't get FP registers"));
210 mips64_fill_fpregset (&fp_regs, regno);
212 if (ptrace (PTRACE_SETFPREGS, tid, 0L,
213 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
214 perror_with_name (_("Couldn't set FP registers"));
218 /* Fetch REGNO (or all registers if REGNO == -1) from the target
219 using any working method. */
222 mips64_linux_fetch_registers (int regnum)
224 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
225 if (have_ptrace_regsets)
226 mips64_linux_regsets_fetch_registers (regnum);
228 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
229 back to PTRACE_PEEKUSER. */
230 if (!have_ptrace_regsets)
231 super_fetch_registers (regnum);
234 /* Store REGNO (or all registers if REGNO == -1) to the target
235 using any working method. */
238 mips64_linux_store_registers (int regnum)
240 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
241 if (have_ptrace_regsets)
242 mips64_linux_regsets_store_registers (regnum);
244 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
245 back to PTRACE_PEEKUSER. */
246 if (!have_ptrace_regsets)
247 super_store_registers (regnum);
250 /* Return the address in the core dump or inferior of register
254 mips_linux_register_u_offset (int regno)
256 /* FIXME drow/2005-09-04: The hardcoded use of register_addr should go
257 away. This requires disentangling the various definitions of it
258 (particularly alpha-nat.c's). */
259 return register_addr (regno, 0);
262 void _initialize_mips_linux_nat (void);
265 _initialize_mips_linux_nat (void)
267 struct target_ops *t = linux_trad_target (mips_linux_register_u_offset);
269 super_fetch_registers = t->to_fetch_registers;
270 super_store_registers = t->to_store_registers;
272 t->to_fetch_registers = mips64_linux_fetch_registers;
273 t->to_store_registers = mips64_linux_store_registers;
275 linux_nat_add_target (t);