gas/testsuite/
[binutils-gdb.git] / gdb / mips-linux-nat.c
1 /* Native-dependent code for GNU/Linux on MIPS processors.
2
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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.
12
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.
17
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. */
22
23 #include "defs.h"
24 #include "inferior.h"
25 #include "mips-tdep.h"
26 #include "target.h"
27 #include "linux-nat.h"
28 #include "mips-linux-tdep.h"
29
30 #include "gdb_proc_service.h"
31
32 #include <sys/ptrace.h>
33
34 #ifndef PTRACE_GET_THREAD_AREA
35 #define PTRACE_GET_THREAD_AREA 25
36 #endif
37
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;
41
42 /* Saved function pointers to fetch and store a single register using
43 PTRACE_PEEKUSER and PTRACE_POKEUSER. */
44
45 void (*super_fetch_registers) (int);
46 void (*super_store_registers) (int);
47
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(). */
52
53 int
54 mips_linux_cannot_fetch_register (int regno)
55 {
56 if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
57 return 0;
58 else if (regno >= mips_regnum (current_gdbarch)->fp0
59 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
60 return 0;
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)
68 return 0;
69 else
70 return 1;
71 }
72
73 int
74 mips_linux_cannot_store_register (int regno)
75 {
76 if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
77 return 0;
78 else if (regno >= FP0_REGNUM && regno <= FP0_REGNUM + 32)
79 return 0;
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)
84 return 0;
85 else
86 return 1;
87 }
88
89 /* Map gdb internal register number to ptrace ``address''.
90 These ``addresses'' are normally defined in <asm/ptrace.h>. */
91
92 static CORE_ADDR
93 mips_linux_register_addr (int regno)
94 {
95 int regaddr;
96
97 if (regno < 0 || regno >= NUM_REGS)
98 error (_("Bogon register number %d."), regno);
99
100 if (regno < 32)
101 regaddr = regno;
102 else if ((regno >= mips_regnum (current_gdbarch)->fp0)
103 && (regno < mips_regnum (current_gdbarch)->fp0 + 32))
104 regaddr = FPR_BASE + (regno - mips_regnum (current_gdbarch)->fp0);
105 else if (regno == mips_regnum (current_gdbarch)->pc)
106 regaddr = PC;
107 else if (regno == mips_regnum (current_gdbarch)->cause)
108 regaddr = CAUSE;
109 else if (regno == mips_regnum (current_gdbarch)->badvaddr)
110 regaddr = BADVADDR;
111 else if (regno == mips_regnum (current_gdbarch)->lo)
112 regaddr = MMLO;
113 else if (regno == mips_regnum (current_gdbarch)->hi)
114 regaddr = MMHI;
115 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
116 regaddr = FPC_CSR;
117 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
118 regaddr = FPC_EIR;
119 else
120 error (_("Unknowable register number %d."), regno);
121
122 return regaddr;
123 }
124
125 static CORE_ADDR
126 mips64_linux_register_addr (int regno)
127 {
128 int regaddr;
129
130 if (regno < 0 || regno >= NUM_REGS)
131 error (_("Bogon register number %d."), regno);
132
133 if (regno < 32)
134 regaddr = regno;
135 else if ((regno >= mips_regnum (current_gdbarch)->fp0)
136 && (regno < mips_regnum (current_gdbarch)->fp0 + 32))
137 regaddr = MIPS64_FPR_BASE + (regno - FP0_REGNUM);
138 else if (regno == mips_regnum (current_gdbarch)->pc)
139 regaddr = MIPS64_PC;
140 else if (regno == mips_regnum (current_gdbarch)->cause)
141 regaddr = MIPS64_CAUSE;
142 else if (regno == mips_regnum (current_gdbarch)->badvaddr)
143 regaddr = MIPS64_BADVADDR;
144 else if (regno == mips_regnum (current_gdbarch)->lo)
145 regaddr = MIPS64_MMLO;
146 else if (regno == mips_regnum (current_gdbarch)->hi)
147 regaddr = MIPS64_MMHI;
148 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
149 regaddr = MIPS64_FPC_CSR;
150 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
151 regaddr = MIPS64_FPC_EIR;
152 else
153 error (_("Unknowable register number %d."), regno);
154
155 return regaddr;
156 }
157
158 /* Fetch the thread-local storage pointer for libthread_db. */
159
160 ps_err_e
161 ps_get_thread_area (const struct ps_prochandle *ph,
162 lwpid_t lwpid, int idx, void **base)
163 {
164 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
165 return PS_ERR;
166
167 /* IDX is the bias from the thread pointer to the beginning of the
168 thread descriptor. It has to be subtracted due to implementation
169 quirks in libthread_db. */
170 *base = (void *) ((char *)*base - idx);
171
172 return PS_OK;
173 }
174
175 /* Fetch REGNO (or all registers if REGNO == -1) from the target
176 using PTRACE_GETREGS et al. */
177
178 static void
179 mips64_linux_regsets_fetch_registers (int regno)
180 {
181 int is_fp;
182 int tid;
183
184 if (regno >= mips_regnum (current_gdbarch)->fp0
185 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
186 is_fp = 1;
187 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
188 is_fp = 1;
189 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
190 is_fp = 1;
191 else
192 is_fp = 0;
193
194 tid = ptid_get_lwp (inferior_ptid);
195 if (tid == 0)
196 tid = ptid_get_pid (inferior_ptid);
197
198 if (regno == -1 || !is_fp)
199 {
200 mips64_elf_gregset_t regs;
201
202 if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
203 {
204 if (errno == EIO)
205 {
206 have_ptrace_regsets = 0;
207 return;
208 }
209 perror_with_name (_("Couldn't get registers"));
210 }
211
212 mips64_supply_gregset (&regs);
213 }
214
215 if (regno == -1 || is_fp)
216 {
217 mips64_elf_fpregset_t fp_regs;
218
219 if (ptrace (PTRACE_GETFPREGS, tid, 0L,
220 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
221 {
222 if (errno == EIO)
223 {
224 have_ptrace_regsets = 0;
225 return;
226 }
227 perror_with_name (_("Couldn't get FP registers"));
228 }
229
230 mips64_supply_fpregset (&fp_regs);
231 }
232 }
233
234 /* Store REGNO (or all registers if REGNO == -1) to the target
235 using PTRACE_SETREGS et al. */
236
237 static void
238 mips64_linux_regsets_store_registers (int regno)
239 {
240 int is_fp;
241 int tid;
242
243 if (regno >= mips_regnum (current_gdbarch)->fp0
244 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
245 is_fp = 1;
246 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
247 is_fp = 1;
248 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
249 is_fp = 1;
250 else
251 is_fp = 0;
252
253 tid = ptid_get_lwp (inferior_ptid);
254 if (tid == 0)
255 tid = ptid_get_pid (inferior_ptid);
256
257 if (regno == -1 || !is_fp)
258 {
259 mips64_elf_gregset_t regs;
260
261 if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
262 perror_with_name (_("Couldn't get registers"));
263
264 mips64_fill_gregset (&regs, regno);
265
266 if (ptrace (PTRACE_SETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
267 perror_with_name (_("Couldn't set registers"));
268 }
269
270 if (regno == -1 || is_fp)
271 {
272 mips64_elf_fpregset_t fp_regs;
273
274 if (ptrace (PTRACE_GETFPREGS, tid, 0L,
275 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
276 perror_with_name (_("Couldn't get FP registers"));
277
278 mips64_fill_fpregset (&fp_regs, regno);
279
280 if (ptrace (PTRACE_SETFPREGS, tid, 0L,
281 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
282 perror_with_name (_("Couldn't set FP registers"));
283 }
284 }
285
286 /* Fetch REGNO (or all registers if REGNO == -1) from the target
287 using any working method. */
288
289 static void
290 mips64_linux_fetch_registers (int regnum)
291 {
292 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
293 if (have_ptrace_regsets)
294 mips64_linux_regsets_fetch_registers (regnum);
295
296 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
297 back to PTRACE_PEEKUSER. */
298 if (!have_ptrace_regsets)
299 super_fetch_registers (regnum);
300 }
301
302 /* Store REGNO (or all registers if REGNO == -1) to the target
303 using any working method. */
304
305 static void
306 mips64_linux_store_registers (int regnum)
307 {
308 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
309 if (have_ptrace_regsets)
310 mips64_linux_regsets_store_registers (regnum);
311
312 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
313 back to PTRACE_PEEKUSER. */
314 if (!have_ptrace_regsets)
315 super_store_registers (regnum);
316 }
317
318 /* Return the address in the core dump or inferior of register
319 REGNO. */
320
321 static CORE_ADDR
322 mips_linux_register_u_offset (int regno)
323 {
324 if (mips_abi_regsize (current_gdbarch) == 8)
325 return mips64_linux_register_addr (regno);
326 else
327 return mips_linux_register_addr (regno);
328 }
329
330 void _initialize_mips_linux_nat (void);
331
332 void
333 _initialize_mips_linux_nat (void)
334 {
335 struct target_ops *t = linux_trad_target (mips_linux_register_u_offset);
336
337 super_fetch_registers = t->to_fetch_registers;
338 super_store_registers = t->to_store_registers;
339
340 t->to_fetch_registers = mips64_linux_fetch_registers;
341 t->to_store_registers = mips64_linux_store_registers;
342
343 linux_nat_add_target (t);
344 }