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 /* Map gdb internal register number to ptrace ``address''.
51 These ``addresses'' are normally defined in <asm/ptrace.h>.
53 ptrace does not provide a way to read (or set) MIPS_PS_REGNUM,
54 and there's no point in reading or setting MIPS_ZERO_REGNUM.
55 We also can not set BADVADDR, CAUSE, or FCRIR via ptrace(). */
58 mips_linux_register_addr (struct gdbarch
*gdbarch
, int regno
, int store
)
62 if (regno
< 0 || regno
>= gdbarch_num_regs (current_gdbarch
))
63 error (_("Bogon register number %d."), regno
);
65 if (regno
> MIPS_ZERO_REGNUM
&& regno
< MIPS_ZERO_REGNUM
+ 32)
67 else if ((regno
>= mips_regnum (gdbarch
)->fp0
)
68 && (regno
< mips_regnum (gdbarch
)->fp0
+ 32))
69 regaddr
= FPR_BASE
+ (regno
- mips_regnum (gdbarch
)->fp0
);
70 else if (regno
== mips_regnum (gdbarch
)->pc
)
72 else if (regno
== mips_regnum (gdbarch
)->cause
)
73 regaddr
= store
? (CORE_ADDR
) -1 : CAUSE
;
74 else if (regno
== mips_regnum (gdbarch
)->badvaddr
)
75 regaddr
= store
? (CORE_ADDR
) -1 : BADVADDR
;
76 else if (regno
== mips_regnum (gdbarch
)->lo
)
78 else if (regno
== mips_regnum (gdbarch
)->hi
)
80 else if (regno
== mips_regnum (gdbarch
)->fp_control_status
)
82 else if (regno
== mips_regnum (gdbarch
)->fp_implementation_revision
)
83 regaddr
= store
? (CORE_ADDR
) -1 : FPC_EIR
;
85 regaddr
= (CORE_ADDR
) -1;
91 mips64_linux_register_addr (struct gdbarch
*gdbarch
, int regno
, int store
)
95 if (regno
< 0 || regno
>= gdbarch_num_regs (current_gdbarch
))
96 error (_("Bogon register number %d."), regno
);
98 if (regno
> MIPS_ZERO_REGNUM
&& regno
< MIPS_ZERO_REGNUM
+ 32)
100 else if ((regno
>= mips_regnum (gdbarch
)->fp0
)
101 && (regno
< mips_regnum (gdbarch
)->fp0
+ 32))
102 regaddr
= MIPS64_FPR_BASE
+ (regno
- FP0_REGNUM
);
103 else if (regno
== mips_regnum (gdbarch
)->pc
)
105 else if (regno
== mips_regnum (gdbarch
)->cause
)
106 regaddr
= store
? (CORE_ADDR
) -1 : MIPS64_CAUSE
;
107 else if (regno
== mips_regnum (gdbarch
)->badvaddr
)
108 regaddr
= store
? (CORE_ADDR
) -1 : MIPS64_BADVADDR
;
109 else if (regno
== mips_regnum (gdbarch
)->lo
)
110 regaddr
= MIPS64_MMLO
;
111 else if (regno
== mips_regnum (gdbarch
)->hi
)
112 regaddr
= MIPS64_MMHI
;
113 else if (regno
== mips_regnum (gdbarch
)->fp_control_status
)
114 regaddr
= MIPS64_FPC_CSR
;
115 else if (regno
== mips_regnum (gdbarch
)->fp_implementation_revision
)
116 regaddr
= store
? (CORE_ADDR
) -1 : MIPS64_FPC_EIR
;
118 regaddr
= (CORE_ADDR
) -1;
123 /* Fetch the thread-local storage pointer for libthread_db. */
126 ps_get_thread_area (const struct ps_prochandle
*ph
,
127 lwpid_t lwpid
, int idx
, void **base
)
129 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
, NULL
, base
) != 0)
132 /* IDX is the bias from the thread pointer to the beginning of the
133 thread descriptor. It has to be subtracted due to implementation
134 quirks in libthread_db. */
135 *base
= (void *) ((char *)*base
- idx
);
140 /* Wrapper functions. These are only used by libthread_db. */
143 supply_gregset (struct regcache
*regcache
, const gdb_gregset_t
*gregsetp
)
145 if (mips_isa_regsize (current_gdbarch
) == 4)
146 mips_supply_gregset (regcache
, (const mips_elf_gregset_t
*) gregsetp
);
148 mips64_supply_gregset (regcache
, (const mips64_elf_gregset_t
*) gregsetp
);
152 fill_gregset (const struct regcache
*regcache
,
153 gdb_gregset_t
*gregsetp
, int regno
)
155 if (mips_isa_regsize (current_gdbarch
) == 4)
156 mips_fill_gregset (regcache
, (mips_elf_gregset_t
*) gregsetp
, regno
);
158 mips64_fill_gregset (regcache
, (mips64_elf_gregset_t
*) gregsetp
, regno
);
162 supply_fpregset (struct regcache
*regcache
, const gdb_fpregset_t
*fpregsetp
)
164 if (mips_isa_regsize (current_gdbarch
) == 4)
165 mips_supply_fpregset (regcache
, (const mips_elf_fpregset_t
*) fpregsetp
);
167 mips64_supply_fpregset (regcache
, (const mips64_elf_fpregset_t
*) fpregsetp
);
171 fill_fpregset (const struct regcache
*regcache
,
172 gdb_fpregset_t
*fpregsetp
, int regno
)
174 if (mips_isa_regsize (current_gdbarch
) == 4)
175 mips_fill_fpregset (regcache
, (mips_elf_fpregset_t
*) fpregsetp
, regno
);
177 mips64_fill_fpregset (regcache
, (mips64_elf_fpregset_t
*) fpregsetp
, regno
);
181 /* Fetch REGNO (or all registers if REGNO == -1) from the target
182 using PTRACE_GETREGS et al. */
185 mips64_linux_regsets_fetch_registers (struct regcache
*regcache
, int regno
)
190 if (regno
>= mips_regnum (current_gdbarch
)->fp0
191 && regno
<= mips_regnum (current_gdbarch
)->fp0
+ 32)
193 else if (regno
== mips_regnum (current_gdbarch
)->fp_control_status
)
195 else if (regno
== mips_regnum (current_gdbarch
)->fp_implementation_revision
)
200 tid
= ptid_get_lwp (inferior_ptid
);
202 tid
= ptid_get_pid (inferior_ptid
);
204 if (regno
== -1 || !is_fp
)
206 mips64_elf_gregset_t regs
;
208 if (ptrace (PTRACE_GETREGS
, tid
, 0L, (PTRACE_TYPE_ARG3
) ®s
) == -1)
212 have_ptrace_regsets
= 0;
215 perror_with_name (_("Couldn't get registers"));
218 mips64_supply_gregset (regcache
,
219 (const mips64_elf_gregset_t
*) ®s
);
222 if (regno
== -1 || is_fp
)
224 mips64_elf_fpregset_t fp_regs
;
226 if (ptrace (PTRACE_GETFPREGS
, tid
, 0L,
227 (PTRACE_TYPE_ARG3
) &fp_regs
) == -1)
231 have_ptrace_regsets
= 0;
234 perror_with_name (_("Couldn't get FP registers"));
237 mips64_supply_fpregset (regcache
,
238 (const mips64_elf_fpregset_t
*) &fp_regs
);
242 /* Store REGNO (or all registers if REGNO == -1) to the target
243 using PTRACE_SETREGS et al. */
246 mips64_linux_regsets_store_registers (const struct regcache
*regcache
, int regno
)
251 if (regno
>= mips_regnum (current_gdbarch
)->fp0
252 && regno
<= mips_regnum (current_gdbarch
)->fp0
+ 32)
254 else if (regno
== mips_regnum (current_gdbarch
)->fp_control_status
)
256 else if (regno
== mips_regnum (current_gdbarch
)->fp_implementation_revision
)
261 tid
= ptid_get_lwp (inferior_ptid
);
263 tid
= ptid_get_pid (inferior_ptid
);
265 if (regno
== -1 || !is_fp
)
267 mips64_elf_gregset_t regs
;
269 if (ptrace (PTRACE_GETREGS
, tid
, 0L, (PTRACE_TYPE_ARG3
) ®s
) == -1)
270 perror_with_name (_("Couldn't get registers"));
272 mips64_fill_gregset (regcache
, ®s
, regno
);
274 if (ptrace (PTRACE_SETREGS
, tid
, 0L, (PTRACE_TYPE_ARG3
) ®s
) == -1)
275 perror_with_name (_("Couldn't set registers"));
278 if (regno
== -1 || is_fp
)
280 mips64_elf_fpregset_t fp_regs
;
282 if (ptrace (PTRACE_GETFPREGS
, tid
, 0L,
283 (PTRACE_TYPE_ARG3
) &fp_regs
) == -1)
284 perror_with_name (_("Couldn't get FP registers"));
286 mips64_fill_fpregset (regcache
, &fp_regs
, regno
);
288 if (ptrace (PTRACE_SETFPREGS
, tid
, 0L,
289 (PTRACE_TYPE_ARG3
) &fp_regs
) == -1)
290 perror_with_name (_("Couldn't set FP registers"));
294 /* Fetch REGNO (or all registers if REGNO == -1) from the target
295 using any working method. */
298 mips64_linux_fetch_registers (struct regcache
*regcache
, int regnum
)
300 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
301 if (have_ptrace_regsets
)
302 mips64_linux_regsets_fetch_registers (regcache
, regnum
);
304 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
305 back to PTRACE_PEEKUSER. */
306 if (!have_ptrace_regsets
)
307 super_fetch_registers (regcache
, regnum
);
310 /* Store REGNO (or all registers if REGNO == -1) to the target
311 using any working method. */
314 mips64_linux_store_registers (struct regcache
*regcache
, int regnum
)
316 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
317 if (have_ptrace_regsets
)
318 mips64_linux_regsets_store_registers (regcache
, regnum
);
320 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
321 back to PTRACE_PEEKUSER. */
322 if (!have_ptrace_regsets
)
323 super_store_registers (regcache
, regnum
);
326 /* Return the address in the core dump or inferior of register
330 mips_linux_register_u_offset (struct gdbarch
*gdbarch
, int regno
, int store_p
)
332 if (mips_abi_regsize (gdbarch
) == 8)
333 return mips64_linux_register_addr (gdbarch
, regno
, store_p
);
335 return mips_linux_register_addr (gdbarch
, regno
, store_p
);
338 void _initialize_mips_linux_nat (void);
341 _initialize_mips_linux_nat (void)
343 struct target_ops
*t
= linux_trad_target (mips_linux_register_u_offset
);
345 super_fetch_registers
= t
->to_fetch_registers
;
346 super_store_registers
= t
->to_store_registers
;
348 t
->to_fetch_registers
= mips64_linux_fetch_registers
;
349 t
->to_store_registers
= mips64_linux_store_registers
;
351 linux_nat_add_target (t
);