1 /* Native-dependent code for GNU/Linux on MIPS processors.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 3 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, see <http://www.gnu.org/licenses/>. */
23 #include "mips-tdep.h"
26 #include "linux-nat.h"
27 #include "mips-linux-tdep.h"
28 #include "target-descriptions.h"
30 #include "gdb_proc_service.h"
34 #include <sys/ptrace.h>
36 #include "features/mips-linux.c"
37 #include "features/mips64-linux.c"
39 #ifndef PTRACE_GET_THREAD_AREA
40 #define PTRACE_GET_THREAD_AREA 25
43 /* Assume that we have PTRACE_GETREGS et al. support. If we do not,
44 we'll clear this and use PTRACE_PEEKUSER instead. */
45 static int have_ptrace_regsets
= 1;
47 /* Saved function pointers to fetch and store a single register using
48 PTRACE_PEEKUSER and PTRACE_POKEUSER. */
50 void (*super_fetch_registers
) (struct target_ops
*, struct regcache
*, int);
51 void (*super_store_registers
) (struct target_ops
*, struct regcache
*, int);
53 /* Map gdb internal register number to ptrace ``address''.
54 These ``addresses'' are normally defined in <asm/ptrace.h>.
56 ptrace does not provide a way to read (or set) MIPS_PS_REGNUM,
57 and there's no point in reading or setting MIPS_ZERO_REGNUM.
58 We also can not set BADVADDR, CAUSE, or FCRIR via ptrace(). */
61 mips_linux_register_addr (struct gdbarch
*gdbarch
, int regno
, int store
)
65 if (regno
< 0 || regno
>= gdbarch_num_regs (gdbarch
))
66 error (_("Bogon register number %d."), regno
);
68 if (regno
> MIPS_ZERO_REGNUM
&& regno
< MIPS_ZERO_REGNUM
+ 32)
70 else if ((regno
>= mips_regnum (gdbarch
)->fp0
)
71 && (regno
< mips_regnum (gdbarch
)->fp0
+ 32))
72 regaddr
= FPR_BASE
+ (regno
- mips_regnum (gdbarch
)->fp0
);
73 else if (regno
== mips_regnum (gdbarch
)->pc
)
75 else if (regno
== mips_regnum (gdbarch
)->cause
)
76 regaddr
= store
? (CORE_ADDR
) -1 : CAUSE
;
77 else if (regno
== mips_regnum (gdbarch
)->badvaddr
)
78 regaddr
= store
? (CORE_ADDR
) -1 : BADVADDR
;
79 else if (regno
== mips_regnum (gdbarch
)->lo
)
81 else if (regno
== mips_regnum (gdbarch
)->hi
)
83 else if (regno
== mips_regnum (gdbarch
)->fp_control_status
)
85 else if (regno
== mips_regnum (gdbarch
)->fp_implementation_revision
)
86 regaddr
= store
? (CORE_ADDR
) -1 : FPC_EIR
;
87 else if (mips_linux_restart_reg_p (gdbarch
) && regno
== MIPS_RESTART_REGNUM
)
90 regaddr
= (CORE_ADDR
) -1;
96 mips64_linux_register_addr (struct gdbarch
*gdbarch
, int regno
, int store
)
100 if (regno
< 0 || regno
>= gdbarch_num_regs (gdbarch
))
101 error (_("Bogon register number %d."), regno
);
103 if (regno
> MIPS_ZERO_REGNUM
&& regno
< MIPS_ZERO_REGNUM
+ 32)
105 else if ((regno
>= mips_regnum (gdbarch
)->fp0
)
106 && (regno
< mips_regnum (gdbarch
)->fp0
+ 32))
107 regaddr
= MIPS64_FPR_BASE
+ (regno
- gdbarch_fp0_regnum (gdbarch
));
108 else if (regno
== mips_regnum (gdbarch
)->pc
)
110 else if (regno
== mips_regnum (gdbarch
)->cause
)
111 regaddr
= store
? (CORE_ADDR
) -1 : MIPS64_CAUSE
;
112 else if (regno
== mips_regnum (gdbarch
)->badvaddr
)
113 regaddr
= store
? (CORE_ADDR
) -1 : MIPS64_BADVADDR
;
114 else if (regno
== mips_regnum (gdbarch
)->lo
)
115 regaddr
= MIPS64_MMLO
;
116 else if (regno
== mips_regnum (gdbarch
)->hi
)
117 regaddr
= MIPS64_MMHI
;
118 else if (regno
== mips_regnum (gdbarch
)->fp_control_status
)
119 regaddr
= MIPS64_FPC_CSR
;
120 else if (regno
== mips_regnum (gdbarch
)->fp_implementation_revision
)
121 regaddr
= store
? (CORE_ADDR
) -1 : MIPS64_FPC_EIR
;
122 else if (mips_linux_restart_reg_p (gdbarch
) && regno
== MIPS_RESTART_REGNUM
)
125 regaddr
= (CORE_ADDR
) -1;
130 /* Fetch the thread-local storage pointer for libthread_db. */
133 ps_get_thread_area (const struct ps_prochandle
*ph
,
134 lwpid_t lwpid
, int idx
, void **base
)
136 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
, NULL
, base
) != 0)
139 /* IDX is the bias from the thread pointer to the beginning of the
140 thread descriptor. It has to be subtracted due to implementation
141 quirks in libthread_db. */
142 *base
= (void *) ((char *)*base
- idx
);
147 /* Wrapper functions. These are only used by libthread_db. */
150 supply_gregset (struct regcache
*regcache
, const gdb_gregset_t
*gregsetp
)
152 if (mips_isa_regsize (get_regcache_arch (regcache
)) == 4)
153 mips_supply_gregset (regcache
, (const mips_elf_gregset_t
*) gregsetp
);
155 mips64_supply_gregset (regcache
, (const mips64_elf_gregset_t
*) gregsetp
);
159 fill_gregset (const struct regcache
*regcache
,
160 gdb_gregset_t
*gregsetp
, int regno
)
162 if (mips_isa_regsize (get_regcache_arch (regcache
)) == 4)
163 mips_fill_gregset (regcache
, (mips_elf_gregset_t
*) gregsetp
, regno
);
165 mips64_fill_gregset (regcache
, (mips64_elf_gregset_t
*) gregsetp
, regno
);
169 supply_fpregset (struct regcache
*regcache
, const gdb_fpregset_t
*fpregsetp
)
171 if (mips_isa_regsize (get_regcache_arch (regcache
)) == 4)
172 mips_supply_fpregset (regcache
, (const mips_elf_fpregset_t
*) fpregsetp
);
174 mips64_supply_fpregset (regcache
, (const mips64_elf_fpregset_t
*) fpregsetp
);
178 fill_fpregset (const struct regcache
*regcache
,
179 gdb_fpregset_t
*fpregsetp
, int regno
)
181 if (mips_isa_regsize (get_regcache_arch (regcache
)) == 4)
182 mips_fill_fpregset (regcache
, (mips_elf_fpregset_t
*) fpregsetp
, regno
);
184 mips64_fill_fpregset (regcache
, (mips64_elf_fpregset_t
*) fpregsetp
, regno
);
188 /* Fetch REGNO (or all registers if REGNO == -1) from the target
189 using PTRACE_GETREGS et al. */
192 mips64_linux_regsets_fetch_registers (struct regcache
*regcache
, int regno
)
194 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
198 if (regno
>= mips_regnum (gdbarch
)->fp0
199 && regno
<= mips_regnum (gdbarch
)->fp0
+ 32)
201 else if (regno
== mips_regnum (gdbarch
)->fp_control_status
)
203 else if (regno
== mips_regnum (gdbarch
)->fp_implementation_revision
)
208 tid
= ptid_get_lwp (inferior_ptid
);
210 tid
= ptid_get_pid (inferior_ptid
);
212 if (regno
== -1 || !is_fp
)
214 mips64_elf_gregset_t regs
;
216 if (ptrace (PTRACE_GETREGS
, tid
, 0L, (PTRACE_TYPE_ARG3
) ®s
) == -1)
220 have_ptrace_regsets
= 0;
223 perror_with_name (_("Couldn't get registers"));
226 mips64_supply_gregset (regcache
,
227 (const mips64_elf_gregset_t
*) ®s
);
230 if (regno
== -1 || is_fp
)
232 mips64_elf_fpregset_t fp_regs
;
234 if (ptrace (PTRACE_GETFPREGS
, tid
, 0L,
235 (PTRACE_TYPE_ARG3
) &fp_regs
) == -1)
239 have_ptrace_regsets
= 0;
242 perror_with_name (_("Couldn't get FP registers"));
245 mips64_supply_fpregset (regcache
,
246 (const mips64_elf_fpregset_t
*) &fp_regs
);
250 /* Store REGNO (or all registers if REGNO == -1) to the target
251 using PTRACE_SETREGS et al. */
254 mips64_linux_regsets_store_registers (const struct regcache
*regcache
, int regno
)
256 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
260 if (regno
>= mips_regnum (gdbarch
)->fp0
261 && regno
<= mips_regnum (gdbarch
)->fp0
+ 32)
263 else if (regno
== mips_regnum (gdbarch
)->fp_control_status
)
265 else if (regno
== mips_regnum (gdbarch
)->fp_implementation_revision
)
270 tid
= ptid_get_lwp (inferior_ptid
);
272 tid
= ptid_get_pid (inferior_ptid
);
274 if (regno
== -1 || !is_fp
)
276 mips64_elf_gregset_t regs
;
278 if (ptrace (PTRACE_GETREGS
, tid
, 0L, (PTRACE_TYPE_ARG3
) ®s
) == -1)
279 perror_with_name (_("Couldn't get registers"));
281 mips64_fill_gregset (regcache
, ®s
, regno
);
283 if (ptrace (PTRACE_SETREGS
, tid
, 0L, (PTRACE_TYPE_ARG3
) ®s
) == -1)
284 perror_with_name (_("Couldn't set registers"));
287 if (regno
== -1 || is_fp
)
289 mips64_elf_fpregset_t fp_regs
;
291 if (ptrace (PTRACE_GETFPREGS
, tid
, 0L,
292 (PTRACE_TYPE_ARG3
) &fp_regs
) == -1)
293 perror_with_name (_("Couldn't get FP registers"));
295 mips64_fill_fpregset (regcache
, &fp_regs
, regno
);
297 if (ptrace (PTRACE_SETFPREGS
, tid
, 0L,
298 (PTRACE_TYPE_ARG3
) &fp_regs
) == -1)
299 perror_with_name (_("Couldn't set FP registers"));
303 /* Fetch REGNO (or all registers if REGNO == -1) from the target
304 using any working method. */
307 mips64_linux_fetch_registers (struct target_ops
*ops
,
308 struct regcache
*regcache
, int regnum
)
310 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
311 if (have_ptrace_regsets
)
312 mips64_linux_regsets_fetch_registers (regcache
, regnum
);
314 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
315 back to PTRACE_PEEKUSER. */
316 if (!have_ptrace_regsets
)
317 super_fetch_registers (ops
, regcache
, regnum
);
320 /* Store REGNO (or all registers if REGNO == -1) to the target
321 using any working method. */
324 mips64_linux_store_registers (struct target_ops
*ops
,
325 struct regcache
*regcache
, int regnum
)
327 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
328 if (have_ptrace_regsets
)
329 mips64_linux_regsets_store_registers (regcache
, regnum
);
331 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
332 back to PTRACE_PEEKUSER. */
333 if (!have_ptrace_regsets
)
334 super_store_registers (ops
, regcache
, regnum
);
337 /* Return the address in the core dump or inferior of register
341 mips_linux_register_u_offset (struct gdbarch
*gdbarch
, int regno
, int store_p
)
343 if (mips_abi_regsize (gdbarch
) == 8)
344 return mips64_linux_register_addr (gdbarch
, regno
, store_p
);
346 return mips_linux_register_addr (gdbarch
, regno
, store_p
);
349 static const struct target_desc
*
350 mips_linux_read_description (struct target_ops
*ops
)
352 /* Report that target registers are a size we know for sure
353 that we can get from ptrace. */
354 if (_MIPS_SIM
== _ABIO32
)
355 return tdesc_mips_linux
;
357 return tdesc_mips64_linux
;
360 void _initialize_mips_linux_nat (void);
363 _initialize_mips_linux_nat (void)
365 struct target_ops
*t
= linux_trad_target (mips_linux_register_u_offset
);
367 super_fetch_registers
= t
->to_fetch_registers
;
368 super_store_registers
= t
->to_store_registers
;
370 t
->to_fetch_registers
= mips64_linux_fetch_registers
;
371 t
->to_store_registers
= mips64_linux_store_registers
;
373 t
->to_read_description
= mips_linux_read_description
;
375 linux_nat_add_target (t
);
377 /* Initialize the standard target descriptions. */
378 initialize_tdesc_mips_linux ();
379 initialize_tdesc_mips64_linux ();