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"
32 #include <sys/ptrace.h>
34 #ifndef PTRACE_GET_THREAD_AREA
35 #define PTRACE_GET_THREAD_AREA 25
38 /* Assume that we have PTRACE_GETREGS et al. support. If we do not,
39 we'll clear this and use PTRACE_PEEKUSER instead. */
40 static int have_ptrace_regsets
= 1;
42 /* Saved function pointers to fetch and store a single register using
43 PTRACE_PEEKUSER and PTRACE_POKEUSER. */
45 void (*super_fetch_registers
) (int);
46 void (*super_store_registers
) (int);
48 /* Pseudo registers can not be read. ptrace does not provide a way to
49 read (or set) MIPS_PS_REGNUM, and there's no point in reading or
50 setting MIPS_ZERO_REGNUM. We also can not set BADVADDR, CAUSE, or
51 FCRIR via ptrace(). */
54 mips_linux_cannot_fetch_register (int regno
)
56 if (regno
> MIPS_ZERO_REGNUM
&& regno
< MIPS_ZERO_REGNUM
+ 32)
58 else if (regno
>= mips_regnum (current_gdbarch
)->fp0
59 && regno
<= mips_regnum (current_gdbarch
)->fp0
+ 32)
61 else if (regno
== mips_regnum (current_gdbarch
)->lo
62 || regno
== mips_regnum (current_gdbarch
)->hi
63 || regno
== mips_regnum (current_gdbarch
)->badvaddr
64 || regno
== mips_regnum (current_gdbarch
)->cause
65 || regno
== mips_regnum (current_gdbarch
)->pc
66 || regno
== mips_regnum (current_gdbarch
)->fp_control_status
67 || regno
== mips_regnum (current_gdbarch
)->fp_implementation_revision
)
74 mips_linux_cannot_store_register (int regno
)
76 if (regno
> MIPS_ZERO_REGNUM
&& regno
< MIPS_ZERO_REGNUM
+ 32)
78 else if (regno
>= FP0_REGNUM
&& regno
<= FP0_REGNUM
+ 32)
80 else if (regno
== mips_regnum (current_gdbarch
)->lo
81 || regno
== mips_regnum (current_gdbarch
)->hi
82 || regno
== mips_regnum (current_gdbarch
)->pc
83 || regno
== mips_regnum (current_gdbarch
)->fp_control_status
)
89 /* Fetch the thread-local storage pointer for libthread_db. */
92 ps_get_thread_area (const struct ps_prochandle
*ph
,
93 lwpid_t lwpid
, int idx
, void **base
)
95 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
, NULL
, base
) != 0)
98 /* IDX is the bias from the thread pointer to the beginning of the
99 thread descriptor. It has to be subtracted due to implementation
100 quirks in libthread_db. */
101 *base
= (void *) ((char *)*base
- idx
);
106 /* Fetch REGNO (or all registers if REGNO == -1) from the target
107 using PTRACE_GETREGS et al. */
110 mips64_linux_regsets_fetch_registers (int regno
)
115 if (regno
>= mips_regnum (current_gdbarch
)->fp0
116 && regno
<= mips_regnum (current_gdbarch
)->fp0
+ 32)
118 else if (regno
== mips_regnum (current_gdbarch
)->fp_control_status
)
120 else if (regno
== mips_regnum (current_gdbarch
)->fp_implementation_revision
)
125 tid
= ptid_get_lwp (inferior_ptid
);
127 tid
= ptid_get_pid (inferior_ptid
);
129 if (regno
== -1 || !is_fp
)
131 mips64_elf_gregset_t regs
;
133 if (ptrace (PTRACE_GETREGS
, tid
, 0L, (PTRACE_TYPE_ARG3
) ®s
) == -1)
137 have_ptrace_regsets
= 0;
140 perror_with_name (_("Couldn't get registers"));
143 mips64_supply_gregset (®s
);
146 if (regno
== -1 || is_fp
)
148 mips64_elf_fpregset_t fp_regs
;
150 if (ptrace (PTRACE_GETFPREGS
, tid
, 0L,
151 (PTRACE_TYPE_ARG3
) &fp_regs
) == -1)
155 have_ptrace_regsets
= 0;
158 perror_with_name (_("Couldn't get FP registers"));
161 mips64_supply_fpregset (&fp_regs
);
165 /* Store REGNO (or all registers if REGNO == -1) to the target
166 using PTRACE_SETREGS et al. */
169 mips64_linux_regsets_store_registers (int regno
)
174 if (regno
>= mips_regnum (current_gdbarch
)->fp0
175 && regno
<= mips_regnum (current_gdbarch
)->fp0
+ 32)
177 else if (regno
== mips_regnum (current_gdbarch
)->fp_control_status
)
179 else if (regno
== mips_regnum (current_gdbarch
)->fp_implementation_revision
)
184 tid
= ptid_get_lwp (inferior_ptid
);
186 tid
= ptid_get_pid (inferior_ptid
);
188 if (regno
== -1 || !is_fp
)
190 mips64_elf_gregset_t regs
;
192 if (ptrace (PTRACE_GETREGS
, tid
, 0L, (PTRACE_TYPE_ARG3
) ®s
) == -1)
193 perror_with_name (_("Couldn't get registers"));
195 mips64_fill_gregset (®s
, regno
);
197 if (ptrace (PTRACE_SETREGS
, tid
, 0L, (PTRACE_TYPE_ARG3
) ®s
) == -1)
198 perror_with_name (_("Couldn't set registers"));
201 if (regno
== -1 || is_fp
)
203 mips64_elf_fpregset_t fp_regs
;
205 if (ptrace (PTRACE_GETFPREGS
, tid
, 0L,
206 (PTRACE_TYPE_ARG3
) &fp_regs
) == -1)
207 perror_with_name (_("Couldn't get FP registers"));
209 mips64_fill_fpregset (&fp_regs
, regno
);
211 if (ptrace (PTRACE_SETFPREGS
, tid
, 0L,
212 (PTRACE_TYPE_ARG3
) &fp_regs
) == -1)
213 perror_with_name (_("Couldn't set FP registers"));
217 /* Fetch REGNO (or all registers if REGNO == -1) from the target
218 using any working method. */
221 mips64_linux_fetch_registers (int regnum
)
223 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
224 if (have_ptrace_regsets
)
225 mips64_linux_regsets_fetch_registers (regnum
);
227 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
228 back to PTRACE_PEEKUSER. */
229 if (!have_ptrace_regsets
)
230 super_fetch_registers (regnum
);
233 /* Store REGNO (or all registers if REGNO == -1) to the target
234 using any working method. */
237 mips64_linux_store_registers (int regnum
)
239 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
240 if (have_ptrace_regsets
)
241 mips64_linux_regsets_store_registers (regnum
);
243 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
244 back to PTRACE_PEEKUSER. */
245 if (!have_ptrace_regsets
)
246 super_store_registers (regnum
);
249 void _initialize_mips_linux_nat (void);
252 _initialize_mips_linux_nat (void)
254 struct target_ops
*t
= linux_target ();
256 super_fetch_registers
= t
->to_fetch_registers
;
257 super_store_registers
= t
->to_store_registers
;
259 t
->to_fetch_registers
= mips64_linux_fetch_registers
;
260 t
->to_store_registers
= mips64_linux_store_registers
;
262 linux_nat_add_target (t
);