* target.h (struct regcache): Add forward declaration.
[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 "regcache.h"
28 #include "linux-nat.h"
29 #include "mips-linux-tdep.h"
30
31 #include "gdb_proc_service.h"
32 #include "gregset.h"
33
34 #include <sys/ptrace.h>
35
36 #ifndef PTRACE_GET_THREAD_AREA
37 #define PTRACE_GET_THREAD_AREA 25
38 #endif
39
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;
43
44 /* Saved function pointers to fetch and store a single register using
45 PTRACE_PEEKUSER and PTRACE_POKEUSER. */
46
47 void (*super_fetch_registers) (struct regcache *, int);
48 void (*super_store_registers) (struct regcache *, int);
49
50 /* Pseudo registers can not be read. ptrace does not provide a way to
51 read (or set) MIPS_PS_REGNUM, and there's no point in reading or
52 setting MIPS_ZERO_REGNUM. We also can not set BADVADDR, CAUSE, or
53 FCRIR via ptrace(). */
54
55 int
56 mips_linux_cannot_fetch_register (int regno)
57 {
58 if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
59 return 0;
60 else if (regno >= mips_regnum (current_gdbarch)->fp0
61 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
62 return 0;
63 else if (regno == mips_regnum (current_gdbarch)->lo
64 || regno == mips_regnum (current_gdbarch)->hi
65 || regno == mips_regnum (current_gdbarch)->badvaddr
66 || regno == mips_regnum (current_gdbarch)->cause
67 || regno == mips_regnum (current_gdbarch)->pc
68 || regno == mips_regnum (current_gdbarch)->fp_control_status
69 || regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
70 return 0;
71 else
72 return 1;
73 }
74
75 int
76 mips_linux_cannot_store_register (int regno)
77 {
78 if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
79 return 0;
80 else if (regno >= FP0_REGNUM && regno <= FP0_REGNUM + 32)
81 return 0;
82 else if (regno == mips_regnum (current_gdbarch)->lo
83 || regno == mips_regnum (current_gdbarch)->hi
84 || regno == mips_regnum (current_gdbarch)->pc
85 || regno == mips_regnum (current_gdbarch)->fp_control_status)
86 return 0;
87 else
88 return 1;
89 }
90
91 /* Map gdb internal register number to ptrace ``address''.
92 These ``addresses'' are normally defined in <asm/ptrace.h>. */
93
94 static CORE_ADDR
95 mips_linux_register_addr (int regno)
96 {
97 int regaddr;
98
99 if (regno < 0 || regno >= NUM_REGS)
100 error (_("Bogon register number %d."), regno);
101
102 if (regno < 32)
103 regaddr = regno;
104 else if ((regno >= mips_regnum (current_gdbarch)->fp0)
105 && (regno < mips_regnum (current_gdbarch)->fp0 + 32))
106 regaddr = FPR_BASE + (regno - mips_regnum (current_gdbarch)->fp0);
107 else if (regno == mips_regnum (current_gdbarch)->pc)
108 regaddr = PC;
109 else if (regno == mips_regnum (current_gdbarch)->cause)
110 regaddr = CAUSE;
111 else if (regno == mips_regnum (current_gdbarch)->badvaddr)
112 regaddr = BADVADDR;
113 else if (regno == mips_regnum (current_gdbarch)->lo)
114 regaddr = MMLO;
115 else if (regno == mips_regnum (current_gdbarch)->hi)
116 regaddr = MMHI;
117 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
118 regaddr = FPC_CSR;
119 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
120 regaddr = FPC_EIR;
121 else
122 error (_("Unknowable register number %d."), regno);
123
124 return regaddr;
125 }
126
127 static CORE_ADDR
128 mips64_linux_register_addr (int regno)
129 {
130 int regaddr;
131
132 if (regno < 0 || regno >= NUM_REGS)
133 error (_("Bogon register number %d."), regno);
134
135 if (regno < 32)
136 regaddr = regno;
137 else if ((regno >= mips_regnum (current_gdbarch)->fp0)
138 && (regno < mips_regnum (current_gdbarch)->fp0 + 32))
139 regaddr = MIPS64_FPR_BASE + (regno - FP0_REGNUM);
140 else if (regno == mips_regnum (current_gdbarch)->pc)
141 regaddr = MIPS64_PC;
142 else if (regno == mips_regnum (current_gdbarch)->cause)
143 regaddr = MIPS64_CAUSE;
144 else if (regno == mips_regnum (current_gdbarch)->badvaddr)
145 regaddr = MIPS64_BADVADDR;
146 else if (regno == mips_regnum (current_gdbarch)->lo)
147 regaddr = MIPS64_MMLO;
148 else if (regno == mips_regnum (current_gdbarch)->hi)
149 regaddr = MIPS64_MMHI;
150 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
151 regaddr = MIPS64_FPC_CSR;
152 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
153 regaddr = MIPS64_FPC_EIR;
154 else
155 error (_("Unknowable register number %d."), regno);
156
157 return regaddr;
158 }
159
160 /* Fetch the thread-local storage pointer for libthread_db. */
161
162 ps_err_e
163 ps_get_thread_area (const struct ps_prochandle *ph,
164 lwpid_t lwpid, int idx, void **base)
165 {
166 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
167 return PS_ERR;
168
169 /* IDX is the bias from the thread pointer to the beginning of the
170 thread descriptor. It has to be subtracted due to implementation
171 quirks in libthread_db. */
172 *base = (void *) ((char *)*base - idx);
173
174 return PS_OK;
175 }
176
177 /* Wrapper functions. These are only used by libthread_db. */
178
179 void
180 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
181 {
182 if (mips_isa_regsize (current_gdbarch) == 4)
183 mips_supply_gregset (regcache, (const mips_elf_gregset_t *) gregsetp);
184 else
185 mips64_supply_gregset (regcache, (const mips64_elf_gregset_t *) gregsetp);
186 }
187
188 void
189 fill_gregset (const struct regcache *regcache,
190 gdb_gregset_t *gregsetp, int regno)
191 {
192 if (mips_isa_regsize (current_gdbarch) == 4)
193 mips_fill_gregset (regcache, (mips_elf_gregset_t *) gregsetp, regno);
194 else
195 mips64_fill_gregset (regcache, (mips64_elf_gregset_t *) gregsetp, regno);
196 }
197
198 void
199 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
200 {
201 if (mips_isa_regsize (current_gdbarch) == 4)
202 mips_supply_fpregset (regcache, (const mips_elf_fpregset_t *) fpregsetp);
203 else
204 mips64_supply_fpregset (regcache, (const mips64_elf_fpregset_t *) fpregsetp);
205 }
206
207 void
208 fill_fpregset (const struct regcache *regcache,
209 gdb_fpregset_t *fpregsetp, int regno)
210 {
211 if (mips_isa_regsize (current_gdbarch) == 4)
212 mips_fill_fpregset (regcache, (mips_elf_fpregset_t *) fpregsetp, regno);
213 else
214 mips64_fill_fpregset (regcache, (mips64_elf_fpregset_t *) fpregsetp, regno);
215 }
216
217
218 /* Fetch REGNO (or all registers if REGNO == -1) from the target
219 using PTRACE_GETREGS et al. */
220
221 static void
222 mips64_linux_regsets_fetch_registers (struct regcache *regcache, int regno)
223 {
224 int is_fp;
225 int tid;
226
227 if (regno >= mips_regnum (current_gdbarch)->fp0
228 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
229 is_fp = 1;
230 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
231 is_fp = 1;
232 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
233 is_fp = 1;
234 else
235 is_fp = 0;
236
237 tid = ptid_get_lwp (inferior_ptid);
238 if (tid == 0)
239 tid = ptid_get_pid (inferior_ptid);
240
241 if (regno == -1 || !is_fp)
242 {
243 mips64_elf_gregset_t regs;
244
245 if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
246 {
247 if (errno == EIO)
248 {
249 have_ptrace_regsets = 0;
250 return;
251 }
252 perror_with_name (_("Couldn't get registers"));
253 }
254
255 mips64_supply_gregset (regcache,
256 (const mips64_elf_gregset_t *) &regs);
257 }
258
259 if (regno == -1 || is_fp)
260 {
261 mips64_elf_fpregset_t fp_regs;
262
263 if (ptrace (PTRACE_GETFPREGS, tid, 0L,
264 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
265 {
266 if (errno == EIO)
267 {
268 have_ptrace_regsets = 0;
269 return;
270 }
271 perror_with_name (_("Couldn't get FP registers"));
272 }
273
274 mips64_supply_fpregset (regcache,
275 (const mips64_elf_fpregset_t *) &fp_regs);
276 }
277 }
278
279 /* Store REGNO (or all registers if REGNO == -1) to the target
280 using PTRACE_SETREGS et al. */
281
282 static void
283 mips64_linux_regsets_store_registers (const struct regcache *regcache, int regno)
284 {
285 int is_fp;
286 int tid;
287
288 if (regno >= mips_regnum (current_gdbarch)->fp0
289 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
290 is_fp = 1;
291 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
292 is_fp = 1;
293 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
294 is_fp = 1;
295 else
296 is_fp = 0;
297
298 tid = ptid_get_lwp (inferior_ptid);
299 if (tid == 0)
300 tid = ptid_get_pid (inferior_ptid);
301
302 if (regno == -1 || !is_fp)
303 {
304 mips64_elf_gregset_t regs;
305
306 if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
307 perror_with_name (_("Couldn't get registers"));
308
309 mips64_fill_gregset (regcache, &regs, regno);
310
311 if (ptrace (PTRACE_SETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
312 perror_with_name (_("Couldn't set registers"));
313 }
314
315 if (regno == -1 || is_fp)
316 {
317 mips64_elf_fpregset_t fp_regs;
318
319 if (ptrace (PTRACE_GETFPREGS, tid, 0L,
320 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
321 perror_with_name (_("Couldn't get FP registers"));
322
323 mips64_fill_fpregset (regcache, &fp_regs, regno);
324
325 if (ptrace (PTRACE_SETFPREGS, tid, 0L,
326 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
327 perror_with_name (_("Couldn't set FP registers"));
328 }
329 }
330
331 /* Fetch REGNO (or all registers if REGNO == -1) from the target
332 using any working method. */
333
334 static void
335 mips64_linux_fetch_registers (struct regcache *regcache, int regnum)
336 {
337 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
338 if (have_ptrace_regsets)
339 mips64_linux_regsets_fetch_registers (regcache, regnum);
340
341 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
342 back to PTRACE_PEEKUSER. */
343 if (!have_ptrace_regsets)
344 super_fetch_registers (regcache, regnum);
345 }
346
347 /* Store REGNO (or all registers if REGNO == -1) to the target
348 using any working method. */
349
350 static void
351 mips64_linux_store_registers (struct regcache *regcache, int regnum)
352 {
353 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
354 if (have_ptrace_regsets)
355 mips64_linux_regsets_store_registers (regcache, regnum);
356
357 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
358 back to PTRACE_PEEKUSER. */
359 if (!have_ptrace_regsets)
360 super_store_registers (regcache, regnum);
361 }
362
363 /* Return the address in the core dump or inferior of register
364 REGNO. */
365
366 static CORE_ADDR
367 mips_linux_register_u_offset (int regno)
368 {
369 if (mips_abi_regsize (current_gdbarch) == 8)
370 return mips64_linux_register_addr (regno);
371 else
372 return mips_linux_register_addr (regno);
373 }
374
375 void _initialize_mips_linux_nat (void);
376
377 void
378 _initialize_mips_linux_nat (void)
379 {
380 struct target_ops *t = linux_trad_target (mips_linux_register_u_offset);
381
382 super_fetch_registers = t->to_fetch_registers;
383 super_store_registers = t->to_store_registers;
384
385 t->to_fetch_registers = mips64_linux_fetch_registers;
386 t->to_store_registers = mips64_linux_store_registers;
387
388 linux_nat_add_target (t);
389 }