1 /* Native-dependent code for SPARC.
3 Copyright 2003 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
27 #include "gdb_string.h"
28 #include <sys/ptrace.h>
30 #ifdef HAVE_MACHINE_REG_H
31 #include <machine/reg.h>
34 #include "sparc-tdep.h"
35 #include "sparc-nat.h"
37 /* With some trickery we can use the code in this file for most (if
38 not all) ptrace(2) based SPARC systems, which includes SunOS 4,
39 Linux and the various SPARC BSD's.
41 First, we need a data structure for use with ptrace(2). SunOS has
42 `struct regs' and `struct fp_status' in <machine/reg.h>. BSD's
43 have `struct reg' and `struct fpreg' in <machine/reg.h>. GNU/Linux
44 has the same structures as SunOS 4, but they're in <asm/reg.h>,
45 which is a kernel header. As a general rule we avoid including
46 GNU/Linux kernel headers. Fortunately GNU/Linux has a `gregset_t'
47 and a `fpregset_t' that are equivalent to `struct regs' and `struct
48 fp_status' in <sys/ucontext.h>, which is automatically included by
49 <signal.h>. Settling on using the `gregset_t' and `fpregset_t'
50 typedefs, providing them for the other systems, therefore solves
53 #ifdef HAVE_MACHINE_REG_H
54 #ifdef HAVE_STRUCT_REG
55 typedef struct reg gregset_t
;
56 typedef struct fpreg fpregset_t
;
58 typedef struct regs gregset_t
;
59 typedef struct fp_status fpregset_t
;
63 /* Second, we need to remap the BSD ptrace(2) requests to their SunOS
64 equivalents. GNU/Linux already follows SunOS here. */
66 #ifndef PTRACE_GETREGS
67 #define PTRACE_GETREGS PT_GETREGS
70 #ifndef PTRACE_SETREGS
71 #define PTRACE_SETREGS PT_SETREGS
74 #ifndef PTRACE_GETFPREGS
75 #define PTRACE_GETFPREGS PT_GETFPREGS
78 #ifndef PTRACE_SETFPREGS
79 #define PTRACE_SETFPREGS PT_SETFPREGS
82 /* Register set description. */
83 const struct sparc_gregset
*sparc_gregset
;
84 void (*sparc_supply_gregset
) (const struct sparc_gregset
*,
85 struct regcache
*, int , const void *);
86 void (*sparc_collect_gregset
) (const struct sparc_gregset
*,
87 const struct regcache
*, int, void *);
88 void (*sparc_supply_fpregset
) (struct regcache
*, int , const void *);
89 void (*sparc_collect_fpregset
) (const struct regcache
*, int , void *);
90 int (*sparc_gregset_supplies_p
) (int);
91 int (*sparc_fpregset_supplies_p
) (int);
93 /* Determine whether `gregset_t' contains register REGNUM. */
96 sparc32_gregset_supplies_p (int regnum
)
98 /* Integer registers. */
99 if ((regnum
>= SPARC_G1_REGNUM
&& regnum
<= SPARC_G7_REGNUM
)
100 || (regnum
>= SPARC_O0_REGNUM
&& regnum
<= SPARC_O7_REGNUM
)
101 || (regnum
>= SPARC_L0_REGNUM
&& regnum
<= SPARC_L7_REGNUM
)
102 || (regnum
>= SPARC_I0_REGNUM
&& regnum
<= SPARC_I7_REGNUM
))
105 /* Control registers. */
106 if (regnum
== SPARC32_PC_REGNUM
107 || regnum
== SPARC32_NPC_REGNUM
108 || regnum
== SPARC32_PSR_REGNUM
109 || regnum
== SPARC32_Y_REGNUM
)
115 /* Determine whether `fpregset_t' contains register REGNUM. */
118 sparc32_fpregset_supplies_p (int regnum
)
120 /* Floating-point registers. */
121 if (regnum
>= SPARC_F0_REGNUM
&& regnum
<= SPARC_F31_REGNUM
)
124 /* Control registers. */
125 if (regnum
== SPARC32_FSR_REGNUM
)
131 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
132 for all registers (including the floating-point registers). */
135 fetch_inferior_registers (int regnum
)
137 struct regcache
*regcache
= current_regcache
;
140 /* NOTE: cagney/2002-12-03: This code assumes that the currently
141 selected light weight processes' registers can be written
142 directly into the selected thread's register cache. This works
143 fine when given an 1:1 LWP:thread model (such as found on
144 GNU/Linux) but will, likely, have problems when used on an N:1
145 (userland threads) or N:M (userland multiple LWP) model. In the
146 case of the latter two, the LWP's registers do not necessarily
147 belong to the selected thread (the LWP could be in the middle of
148 executing the thread switch code).
150 These functions should instead be paramaterized with an explicit
151 object (struct regcache, struct thread_info?) into which the LWPs
152 registers can be written. */
153 pid
= TIDGET (inferior_ptid
);
155 pid
= PIDGET (inferior_ptid
);
157 if (regnum
== SPARC_G0_REGNUM
)
159 regcache_raw_supply (regcache
, SPARC_G0_REGNUM
, NULL
);
163 if (regnum
== -1 || sparc_gregset_supplies_p (regnum
))
167 if (ptrace (PTRACE_GETREGS
, pid
, (PTRACE_ARG3_TYPE
) ®s
, 0) == -1)
168 perror_with_name ("Couldn't get registers");
170 sparc_supply_gregset (sparc_gregset
, regcache
, -1, ®s
);
175 if (regnum
== -1 || sparc_fpregset_supplies_p (regnum
))
179 if (ptrace (PTRACE_GETFPREGS
, pid
, (PTRACE_ARG3_TYPE
) &fpregs
, 0) == -1)
180 perror_with_name ("Couldn't get floating point status");
182 sparc_supply_fpregset (regcache
, -1, &fpregs
);
187 store_inferior_registers (int regnum
)
189 struct regcache
*regcache
= current_regcache
;
192 /* NOTE: cagney/2002-12-02: See comment in fetch_inferior_registers
193 about threaded assumptions. */
194 pid
= TIDGET (inferior_ptid
);
196 pid
= PIDGET (inferior_ptid
);
198 if (regnum
== -1 || sparc_gregset_supplies_p (regnum
))
202 if (ptrace (PTRACE_GETREGS
, pid
, (PTRACE_ARG3_TYPE
) ®s
, 0) == -1)
203 perror_with_name ("Couldn't get registers");
205 sparc_collect_gregset (sparc_gregset
, regcache
, regnum
, ®s
);
207 if (ptrace (PTRACE_SETREGS
, pid
, (PTRACE_ARG3_TYPE
) ®s
, 0) == -1)
208 perror_with_name ("Couldn't write registers");
210 /* Deal with the stack regs. */
211 if (regnum
== -1 || regnum
== SPARC_SP_REGNUM
212 || (regnum
>= SPARC_L0_REGNUM
&& regnum
<= SPARC_I7_REGNUM
))
216 regcache_cooked_read_unsigned (regcache
, SPARC_SP_REGNUM
, &sp
);
217 sparc_collect_rwindow (regcache
, sp
, regnum
);
224 if (regnum
== -1 || sparc_fpregset_supplies_p (regnum
))
226 fpregset_t fpregs
, saved_fpregs
;
228 if (ptrace (PTRACE_GETFPREGS
, pid
, (PTRACE_ARG3_TYPE
) &fpregs
, 0) == -1)
229 perror_with_name ("Couldn't get floating-point registers");
231 memcpy (&saved_fpregs
, &fpregs
, sizeof (fpregs
));
232 sparc_collect_fpregset (regcache
, regnum
, &fpregs
);
234 /* Writing the floating-point registers will fail on NetBSD with
235 EINVAL if the inferior process doesn't have an FPU state
236 (i.e. if it didn't use the FPU yet). Therefore we don't try
237 to write the registers if nothing changed. */
238 if (memcmp (&saved_fpregs
, &fpregs
, sizeof (fpregs
)) != 0)
240 if (ptrace (PTRACE_SETFPREGS
, pid
,
241 (PTRACE_ARG3_TYPE
) &fpregs
, 0) == -1)
242 perror_with_name ("Couldn't write floating-point registers");
251 /* Provide a prototype to silence -Wmissing-prototypes. */
252 void _initialize_sparc_nat (void);
255 _initialize_sparc_nat (void)
257 /* Deafult to using SunOS 4 register sets. */
258 if (sparc_gregset
== NULL
)
259 sparc_gregset
= &sparc32_sunos4_gregset
;
260 if (sparc_supply_gregset
== NULL
)
261 sparc_supply_gregset
= sparc32_supply_gregset
;
262 if (sparc_collect_gregset
== NULL
)
263 sparc_collect_gregset
= sparc32_collect_gregset
;
264 if (sparc_supply_fpregset
== NULL
)
265 sparc_supply_fpregset
= sparc32_supply_fpregset
;
266 if (sparc_collect_fpregset
== NULL
)
267 sparc_collect_fpregset
= sparc32_collect_fpregset
;
268 if (sparc_gregset_supplies_p
== NULL
)
269 sparc_gregset_supplies_p
= sparc32_gregset_supplies_p
;
270 if (sparc_fpregset_supplies_p
== NULL
)
271 sparc_fpregset_supplies_p
= sparc32_fpregset_supplies_p
;