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