* mips-linux-tdep.c (supply_32bit_reg): Add REGCACHE parameter. Use it
[deliverable/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) (int);
48 void (*super_store_registers) (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 (gdb_gregset_t *gregsetp)
181 {
182 if (mips_isa_regsize (current_gdbarch) == 4)
183 mips_supply_gregset (current_regcache, (void *) gregsetp);
184 else
185 mips64_supply_gregset (current_regcache, (void *) gregsetp);
186 }
187
188 void
189 fill_gregset (gdb_gregset_t *gregsetp, int regno)
190 {
191 if (mips_isa_regsize (current_gdbarch) == 4)
192 mips_fill_gregset (current_regcache, (void *) gregsetp, regno);
193 else
194 mips64_fill_gregset (current_regcache, (void *) gregsetp, regno);
195 }
196
197 void
198 supply_fpregset (gdb_fpregset_t *fpregsetp)
199 {
200 if (mips_isa_regsize (current_gdbarch) == 4)
201 mips_supply_fpregset (current_regcache, (void *) fpregsetp);
202 else
203 mips64_supply_fpregset (current_regcache, (void *) fpregsetp);
204 }
205
206 void
207 fill_fpregset (gdb_fpregset_t *fpregsetp, int regno)
208 {
209 if (mips_isa_regsize (current_gdbarch) == 4)
210 mips_fill_fpregset (current_regcache, (void *) fpregsetp, regno);
211 else
212 mips64_fill_fpregset (current_regcache, (void *) fpregsetp, regno);
213 }
214
215
216 /* Fetch REGNO (or all registers if REGNO == -1) from the target
217 using PTRACE_GETREGS et al. */
218
219 static void
220 mips64_linux_regsets_fetch_registers (int regno)
221 {
222 int is_fp;
223 int tid;
224
225 if (regno >= mips_regnum (current_gdbarch)->fp0
226 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
227 is_fp = 1;
228 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
229 is_fp = 1;
230 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
231 is_fp = 1;
232 else
233 is_fp = 0;
234
235 tid = ptid_get_lwp (inferior_ptid);
236 if (tid == 0)
237 tid = ptid_get_pid (inferior_ptid);
238
239 if (regno == -1 || !is_fp)
240 {
241 mips64_elf_gregset_t regs;
242
243 if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
244 {
245 if (errno == EIO)
246 {
247 have_ptrace_regsets = 0;
248 return;
249 }
250 perror_with_name (_("Couldn't get registers"));
251 }
252
253 mips64_supply_gregset (current_regcache,
254 (const mips64_elf_gregset_t *) &regs);
255 }
256
257 if (regno == -1 || is_fp)
258 {
259 mips64_elf_fpregset_t fp_regs;
260
261 if (ptrace (PTRACE_GETFPREGS, tid, 0L,
262 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
263 {
264 if (errno == EIO)
265 {
266 have_ptrace_regsets = 0;
267 return;
268 }
269 perror_with_name (_("Couldn't get FP registers"));
270 }
271
272 mips64_supply_fpregset (current_regcache,
273 (const mips64_elf_fpregset_t *) &fp_regs);
274 }
275 }
276
277 /* Store REGNO (or all registers if REGNO == -1) to the target
278 using PTRACE_SETREGS et al. */
279
280 static void
281 mips64_linux_regsets_store_registers (int regno)
282 {
283 int is_fp;
284 int tid;
285
286 if (regno >= mips_regnum (current_gdbarch)->fp0
287 && regno <= mips_regnum (current_gdbarch)->fp0 + 32)
288 is_fp = 1;
289 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
290 is_fp = 1;
291 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
292 is_fp = 1;
293 else
294 is_fp = 0;
295
296 tid = ptid_get_lwp (inferior_ptid);
297 if (tid == 0)
298 tid = ptid_get_pid (inferior_ptid);
299
300 if (regno == -1 || !is_fp)
301 {
302 mips64_elf_gregset_t regs;
303
304 if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
305 perror_with_name (_("Couldn't get registers"));
306
307 mips64_fill_gregset (current_regcache, &regs, regno);
308
309 if (ptrace (PTRACE_SETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
310 perror_with_name (_("Couldn't set registers"));
311 }
312
313 if (regno == -1 || is_fp)
314 {
315 mips64_elf_fpregset_t fp_regs;
316
317 if (ptrace (PTRACE_GETFPREGS, tid, 0L,
318 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
319 perror_with_name (_("Couldn't get FP registers"));
320
321 mips64_fill_fpregset (current_regcache, &fp_regs, regno);
322
323 if (ptrace (PTRACE_SETFPREGS, tid, 0L,
324 (PTRACE_TYPE_ARG3) &fp_regs) == -1)
325 perror_with_name (_("Couldn't set FP registers"));
326 }
327 }
328
329 /* Fetch REGNO (or all registers if REGNO == -1) from the target
330 using any working method. */
331
332 static void
333 mips64_linux_fetch_registers (int regnum)
334 {
335 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
336 if (have_ptrace_regsets)
337 mips64_linux_regsets_fetch_registers (regnum);
338
339 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
340 back to PTRACE_PEEKUSER. */
341 if (!have_ptrace_regsets)
342 super_fetch_registers (regnum);
343 }
344
345 /* Store REGNO (or all registers if REGNO == -1) to the target
346 using any working method. */
347
348 static void
349 mips64_linux_store_registers (int regnum)
350 {
351 /* Unless we already know that PTRACE_GETREGS does not work, try it. */
352 if (have_ptrace_regsets)
353 mips64_linux_regsets_store_registers (regnum);
354
355 /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
356 back to PTRACE_PEEKUSER. */
357 if (!have_ptrace_regsets)
358 super_store_registers (regnum);
359 }
360
361 /* Return the address in the core dump or inferior of register
362 REGNO. */
363
364 static CORE_ADDR
365 mips_linux_register_u_offset (int regno)
366 {
367 if (mips_abi_regsize (current_gdbarch) == 8)
368 return mips64_linux_register_addr (regno);
369 else
370 return mips_linux_register_addr (regno);
371 }
372
373 void _initialize_mips_linux_nat (void);
374
375 void
376 _initialize_mips_linux_nat (void)
377 {
378 struct target_ops *t = linux_trad_target (mips_linux_register_u_offset);
379
380 super_fetch_registers = t->to_fetch_registers;
381 super_store_registers = t->to_store_registers;
382
383 t->to_fetch_registers = mips64_linux_fetch_registers;
384 t->to_store_registers = mips64_linux_store_registers;
385
386 linux_nat_add_target (t);
387 }
This page took 0.038113 seconds and 4 git commands to generate.