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