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"
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 /* Map gdb internal register number to ptrace ``address''.
91 These ``addresses'' are normally defined in <asm/ptrace.h>. */
94 mips_linux_register_addr (int regno)
98 if (regno < 0 || regno >= NUM_REGS)
99 error (_("Bogon register number %d."), regno);
103 else if ((regno >= mips_regnum (current_gdbarch)->fp0)
104 && (regno < mips_regnum (current_gdbarch)->fp0 + 32))
105 regaddr = FPR_BASE + (regno - mips_regnum (current_gdbarch)->fp0);
106 else if (regno == mips_regnum (current_gdbarch)->pc)
108 else if (regno == mips_regnum (current_gdbarch)->cause)
110 else if (regno == mips_regnum (current_gdbarch)->badvaddr)
112 else if (regno == mips_regnum (current_gdbarch)->lo)
114 else if (regno == mips_regnum (current_gdbarch)->hi)
116 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
118 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
121 error (_("Unknowable register number %d."), regno);
127 mips64_linux_register_addr (int regno)
131 if (regno < 0 || regno >= NUM_REGS)
132 error (_("Bogon register number %d."), regno);
136 else if ((regno >= mips_regnum (current_gdbarch)->fp0)
137 && (regno < mips_regnum (current_gdbarch)->fp0 + 32))
138 regaddr = MIPS64_FPR_BASE + (regno - FP0_REGNUM);
139 else if (regno == mips_regnum (current_gdbarch)->pc)
141 else if (regno == mips_regnum (current_gdbarch)->cause)
142 regaddr = MIPS64_CAUSE;
143 else if (regno == mips_regnum (current_gdbarch)->badvaddr)
144 regaddr = MIPS64_BADVADDR;
145 else if (regno == mips_regnum (current_gdbarch)->lo)
146 regaddr = MIPS64_MMLO;
147 else if (regno == mips_regnum (current_gdbarch)->hi)
148 regaddr = MIPS64_MMHI;
149 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
150 regaddr = MIPS64_FPC_CSR;
151 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
152 regaddr = MIPS64_FPC_EIR;
154 error (_("Unknowable register number %d."), regno);
159 /* Fetch the thread-local storage pointer for libthread_db. */
162 ps_get_thread_area (const struct ps_prochandle *ph,
163 lwpid_t lwpid, int idx, void **base)
165 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
168 /* IDX is the bias from the thread pointer to the beginning of the
169 thread descriptor. It has to be subtracted due to implementation
170 quirks in libthread_db. */
171 *base = (void *) ((char *)*base - idx);
176 /* Wrapper functions. These are only used by libthread_db. */
179 supply_gregset (gdb_gregset_t *gregsetp)
181 if (mips_isa_regsize (current_gdbarch) == 4)
182 mips_supply_gregset ((void *) gregsetp);
184 mips64_supply_gregset ((void *) gregsetp);
188 fill_gregset (gdb_gregset_t *gregsetp, int regno)
190 if (mips_isa_regsize (current_gdbarch) == 4)
191 mips_fill_gregset ((void *) gregsetp, regno);
193 mips64_fill_gregset ((void *) gregsetp, regno);
197 supply_fpregset (gdb_fpregset_t *fpregsetp)
199 if (mips_isa_regsize (current_gdbarch) == 4)
200 mips_supply_fpregset ((void *) fpregsetp);
202 mips64_supply_fpregset ((void *) fpregsetp);
206 fill_fpregset (gdb_fpregset_t *fpregsetp, int regno)
208 if (mips_isa_regsize (current_gdbarch) == 4)
209 mips_fill_fpregset ((void *) fpregsetp, regno);
211 mips64_fill_fpregset ((void *) fpregsetp, regno);
215 /* Fetch REGNO (or all registers if REGNO == -1) from the target
216 using PTRACE_GETREGS et al. */
219 mips64_linux_regsets_fetch_registers (int regno)
224 if (regno >= mips_regnum (current_gdbarch)->fp0
225 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
227 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
229 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
234 tid = ptid_get_lwp (inferior_ptid);
236 tid = ptid_get_pid (inferior_ptid);
238 if (regno == -1 || !is_fp)
240 mips64_elf_gregset_t regs;
242 if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) ®s) == -1)
246 have_ptrace_regsets = 0;
249 perror_with_name (_("Couldn't get registers"));
252 mips64_supply_gregset (®s);
255 if (regno == -1 || is_fp)
257 mips64_elf_fpregset_t fp_regs;
259 if (ptrace (PTRACE_GETFPREGS, tid, 0L,
260 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
264 have_ptrace_regsets = 0;
267 perror_with_name (_("Couldn't get FP registers"));
270 mips64_supply_fpregset (&fp_regs);
274 /* Store REGNO (or all registers if REGNO == -1) to the target
275 using PTRACE_SETREGS et al. */
278 mips64_linux_regsets_store_registers (int regno)
283 if (regno >= mips_regnum (current_gdbarch)->fp0
284 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
286 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
288 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
293 tid = ptid_get_lwp (inferior_ptid);
295 tid = ptid_get_pid (inferior_ptid);
297 if (regno == -1 || !is_fp)
299 mips64_elf_gregset_t regs;
301 if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) ®s) == -1)
302 perror_with_name (_("Couldn't get registers"));
304 mips64_fill_gregset (®s, regno);
306 if (ptrace (PTRACE_SETREGS, tid, 0L, (PTRACE_TYPE_ARG3) ®s) == -1)
307 perror_with_name (_("Couldn't set registers"));
310 if (regno == -1 || is_fp)
312 mips64_elf_fpregset_t fp_regs;
314 if (ptrace (PTRACE_GETFPREGS, tid, 0L,
315 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
316 perror_with_name (_("Couldn't get FP registers"));
318 mips64_fill_fpregset (&fp_regs, regno);
320 if (ptrace (PTRACE_SETFPREGS, tid, 0L,
321 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
322 perror_with_name (_("Couldn't set FP registers"));
326 /* Fetch REGNO (or all registers if REGNO == -1) from the target
327 using any working method. */
330 mips64_linux_fetch_registers (int regnum)
332 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
333 if (have_ptrace_regsets)
334 mips64_linux_regsets_fetch_registers (regnum);
336 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
337 back to PTRACE_PEEKUSER. */
338 if (!have_ptrace_regsets)
339 super_fetch_registers (regnum);
342 /* Store REGNO (or all registers if REGNO == -1) to the target
343 using any working method. */
346 mips64_linux_store_registers (int regnum)
348 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
349 if (have_ptrace_regsets)
350 mips64_linux_regsets_store_registers (regnum);
352 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
353 back to PTRACE_PEEKUSER. */
354 if (!have_ptrace_regsets)
355 super_store_registers (regnum);
358 /* Return the address in the core dump or inferior of register
362 mips_linux_register_u_offset (int regno)
364 if (mips_abi_regsize (current_gdbarch) == 8)
365 return mips64_linux_register_addr (regno);
367 return mips_linux_register_addr (regno);
370 void _initialize_mips_linux_nat (void);
373 _initialize_mips_linux_nat (void)
375 struct target_ops *t = linux_trad_target (mips_linux_register_u_offset);
377 super_fetch_registers = t->to_fetch_registers;
378 super_store_registers = t->to_store_registers;
380 t->to_fetch_registers = mips64_linux_fetch_registers;
381 t->to_store_registers = mips64_linux_store_registers;
383 linux_nat_add_target (t);