2007-05-31 Markus Deuling <deuling@de.ibm.com>
[external/binutils.git] / gdb / irix5-nat.c
1 /* Native support for the SGI Iris running IRIX version 5, for GDB.
2
3    Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998,
4    1999, 2000, 2001, 2002, 2004, 2006, 2007 Free Software Foundation, Inc.
5
6    Contributed by Alessandro Forin(af@cs.cmu.edu) at CMU
7    and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin.
8    Implemented for Irix 4.x by Garrett A. Wollman.
9    Modified for Irix 5.x by Ian Lance Taylor.
10
11    This file is part of GDB.
12
13    This program is free software; you can redistribute it and/or modify
14    it under the terms of the GNU General Public License as published by
15    the Free Software Foundation; either version 2 of the License, or
16    (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21    GNU General Public License for more details.
22
23    You should have received a copy of the GNU General Public License
24    along with this program; if not, write to the Free Software
25    Foundation, Inc., 51 Franklin Street, Fifth Floor,
26    Boston, MA 02110-1301, USA.  */
27
28 #include "defs.h"
29 #include "inferior.h"
30 #include "gdbcore.h"
31 #include "target.h"
32 #include "regcache.h"
33
34 #include "gdb_string.h"
35 #include <sys/time.h>
36 #include <sys/procfs.h>
37 #include <setjmp.h>             /* For JB_XXX.  */
38
39 /* Prototypes for supply_gregset etc. */
40 #include "gregset.h"
41 #include "mips-tdep.h"
42
43 static void fetch_core_registers (struct regcache *, char *,
44                                   unsigned int, int, CORE_ADDR);
45
46 /* Size of elements in jmpbuf */
47
48 #define JB_ELEMENT_SIZE 4
49
50 /*
51  * See the comment in m68k-tdep.c regarding the utility of these functions.
52  *
53  * These definitions are from the MIPS SVR4 ABI, so they may work for
54  * any MIPS SVR4 target.
55  */
56
57 void
58 supply_gregset (struct regcache *regcache, const gregset_t *gregsetp)
59 {
60   int regi;
61   const greg_t *regp = &(*gregsetp)[0];
62   int gregoff = sizeof (greg_t) - mips_isa_regsize (current_gdbarch);
63   static char zerobuf[32] = {0};
64
65   for (regi = 0; regi <= CTX_RA; regi++)
66     regcache_raw_supply (regcache, regi,
67                          (const char *) (regp + regi) + gregoff);
68
69   regcache_raw_supply (regcache, mips_regnum (current_gdbarch)->pc,
70                        (const char *) (regp + CTX_EPC) + gregoff);
71   regcache_raw_supply (regcache, mips_regnum (current_gdbarch)->hi,
72                        (const char *) (regp + CTX_MDHI) + gregoff);
73   regcache_raw_supply (regcache, mips_regnum (current_gdbarch)->lo,
74                        (const char *) (regp + CTX_MDLO) + gregoff);
75   regcache_raw_supply (regcache, mips_regnum (current_gdbarch)->cause,
76                        (const char *) (regp + CTX_CAUSE) + gregoff);
77
78   /* Fill inaccessible registers with zero.  */
79   regcache_raw_supply (regcache, mips_regnum (current_gdbarch)->badvaddr, zerobuf);
80 }
81
82 void
83 fill_gregset (const struct regcache *regcache, gregset_t *gregsetp, int regno)
84 {
85   int regi, size;
86   greg_t *regp = &(*gregsetp)[0];
87   gdb_byte buf[MAX_REGISTER_SIZE];
88
89   /* Under Irix6, if GDB is built with N32 ABI and is debugging an O32
90      executable, we have to sign extend the registers to 64 bits before
91      filling in the gregset structure.  */
92
93   for (regi = 0; regi <= CTX_RA; regi++)
94     if ((regno == -1) || (regno == regi))
95       {
96         size = register_size (current_gdbarch, regi);
97         regcache_raw_collect (regcache, regi, buf);
98         *(regp + regi) = extract_signed_integer (buf, size);
99       }
100
101   if ((regno == -1) || (regno == PC_REGNUM))
102     {
103       regi = mips_regnum (current_gdbarch)->pc;
104       size = register_size (current_gdbarch, regi);
105       regcache_raw_collect (regcache, regi, buf);
106       *(regp + CTX_EPC) = extract_signed_integer (buf, size);
107     }
108
109   if ((regno == -1) || (regno == mips_regnum (current_gdbarch)->cause))
110     {
111       regi = mips_regnum (current_gdbarch)->cause;
112       size = register_size (current_gdbarch, regi);
113       regcache_raw_collect (regcache, regi, buf);
114       *(regp + CTX_CAUSE) = extract_signed_integer (buf, size);
115     }
116
117   if ((regno == -1) || (regno == mips_regnum (current_gdbarch)->hi))
118     {
119       regi = mips_regnum (current_gdbarch)->hi;
120       size = register_size (current_gdbarch, regi);
121       regcache_raw_collect (regcache, regi, buf);
122       *(regp + CTX_MDHI) = extract_signed_integer (buf, size);
123     }
124
125   if ((regno == -1) || (regno == mips_regnum (current_gdbarch)->lo))
126     {
127       regi = mips_regnum (current_gdbarch)->lo;
128       size = register_size (current_gdbarch, regi);
129       regcache_raw_collect (regcache, regi, buf);
130       *(regp + CTX_MDLO) = extract_signed_integer (buf, size);
131     }
132 }
133
134 /*
135  * Now we do the same thing for floating-point registers.
136  * We don't bother to condition on FP0_REGNUM since any
137  * reasonable MIPS configuration has an R3010 in it.
138  *
139  * Again, see the comments in m68k-tdep.c.
140  */
141
142 void
143 supply_fpregset (struct regcache *regcache, const fpregset_t *fpregsetp)
144 {
145   int regi;
146   static char zerobuf[32] = {0};
147   char fsrbuf[8];
148
149   /* FIXME, this is wrong for the N32 ABI which has 64 bit FP regs. */
150
151   for (regi = 0; regi < 32; regi++)
152     regcache_raw_supply (regcache, FP0_REGNUM + regi,
153                          (const char *) &fpregsetp->fp_r.fp_regs[regi]);
154
155   /* We can't supply the FSR register directly to the regcache,
156      because there is a size issue: On one hand, fpregsetp->fp_csr
157      is 32bits long, while the regcache expects a 64bits long value.
158      So we use a buffer of the correct size and copy into it the register
159      value at the proper location.  */
160   memset (fsrbuf, 0, 4);
161   memcpy (fsrbuf + 4, &fpregsetp->fp_csr, 4);
162
163   regcache_raw_supply (regcache,
164                        mips_regnum (current_gdbarch)->fp_control_status,
165                        fsrbuf);
166
167   /* FIXME: how can we supply FCRIR?  SGI doesn't tell us. */
168   regcache_raw_supply (regcache,
169                        mips_regnum (current_gdbarch)->fp_implementation_revision,
170                        zerobuf);
171 }
172
173 void
174 fill_fpregset (const struct regcache *regcache, fpregset_t *fpregsetp, int regno)
175 {
176   int regi;
177   char *from, *to;
178
179   /* FIXME, this is wrong for the N32 ABI which has 64 bit FP regs. */
180
181   for (regi = FP0_REGNUM; regi < FP0_REGNUM + 32; regi++)
182     {
183       if ((regno == -1) || (regno == regi))
184         {
185           to = (char *) &(fpregsetp->fp_r.fp_regs[regi - FP0_REGNUM]);
186           regcache_raw_collect (regcache, regi, to);
187         }
188     }
189
190   if (regno == -1
191       || regno == mips_regnum (current_gdbarch)->fp_control_status)
192     {
193       char fsrbuf[8];
194
195       /* We can't fill the FSR register directly from the regcache,
196          because there is a size issue: On one hand, fpregsetp->fp_csr
197          is 32bits long, while the regcache expects a 64bits long buffer.
198          So we use a buffer of the correct size and copy the register
199          value from that buffer.  */
200       regcache_raw_collect (regcache,
201                             mips_regnum (current_gdbarch)->fp_control_status,
202                             fsrbuf);
203
204       memcpy (&fpregsetp->fp_csr, fsrbuf + 4, 4);
205     }
206 }
207
208
209 /* Figure out where the longjmp will land.
210    We expect the first arg to be a pointer to the jmp_buf structure from which
211    we extract the pc (JB_PC) that we will land at.  The pc is copied into PC.
212    This routine returns true on success. */
213
214 int
215 get_longjmp_target (CORE_ADDR *pc)
216 {
217   char *buf;
218   CORE_ADDR jb_addr;
219
220   buf = alloca (TARGET_PTR_BIT / TARGET_CHAR_BIT);
221   jb_addr = read_register (MIPS_A0_REGNUM);
222
223   if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
224                           TARGET_PTR_BIT / TARGET_CHAR_BIT))
225     return 0;
226
227   *pc = extract_unsigned_integer (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
228
229   return 1;
230 }
231
232 /* Provide registers to GDB from a core file.
233
234    CORE_REG_SECT points to an array of bytes, which were obtained from
235    a core file which BFD thinks might contain register contents. 
236    CORE_REG_SIZE is its size.
237
238    Normally, WHICH says which register set corelow suspects this is:
239      0 --- the general-purpose register set
240      2 --- the floating-point register set
241    However, for Irix 5, WHICH isn't used.
242
243    REG_ADDR is also unused.  */
244
245 static void
246 fetch_core_registers (struct regcache *regcache,
247                       char *core_reg_sect, unsigned core_reg_size,
248                       int which, CORE_ADDR reg_addr)
249 {
250   char *srcp = core_reg_sect;
251   int regsize = mips_isa_regsize (current_gdbarch);
252   int regno;
253
254   /* If regsize is 8, this is a N32 or N64 core file.
255      If regsize is 4, this is an O32 core file.  */
256   if (core_reg_size != regsize * gdbarch_num_regs (current_gdbarch))
257     {
258       warning (_("wrong size gregset struct in core file"));
259       return;
260     }
261
262   for (regno = 0; regno < gdbarch_num_regs (current_gdbarch); regno++)
263     {
264       regcache_raw_supply (regcache, regno, srcp);
265       srcp += regsize;
266     }
267 }
268
269 /* Register that we are able to handle irix5 core file formats.
270    This really is bfd_target_unknown_flavour */
271
272 static struct core_fns irix5_core_fns =
273 {
274   bfd_target_unknown_flavour,           /* core_flavour */
275   default_check_format,                 /* check_format */
276   default_core_sniffer,                 /* core_sniffer */
277   fetch_core_registers,                 /* core_read_registers */
278   NULL                                  /* next */
279 };
280
281 void
282 _initialize_core_irix5 (void)
283 {
284   deprecated_add_core_fns (&irix5_core_fns);
285 }