]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/sparc-nat.c
2004-01-12 Andrew Cagney <cagney@redhat.com>
[thirdparty/binutils-gdb.git] / gdb / sparc-nat.c
1 /* Native-dependent code for SPARC.
2
3 Copyright 2003, 2004 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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. */
21
22 #include "defs.h"
23 #include "inferior.h"
24 #include "regcache.h"
25
26 #include <signal.h>
27 #include "gdb_string.h"
28 #include <sys/ptrace.h>
29 #include "gdb_wait.h"
30 #ifdef HAVE_MACHINE_REG_H
31 #include <machine/reg.h>
32 #endif
33
34 #include "sparc-tdep.h"
35 #include "sparc-nat.h"
36
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 GNU/Linux and the various SPARC BSD's.
40
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
51 the puzzle. */
52
53 #ifdef HAVE_MACHINE_REG_H
54 #ifdef HAVE_STRUCT_REG
55 typedef struct reg gregset_t;
56 typedef struct fpreg fpregset_t;
57 #else
58 typedef struct regs gregset_t;
59 typedef struct fp_status fpregset_t;
60 #endif
61 #endif
62
63 /* Second, we need to remap the BSD ptrace(2) requests to their SunOS
64 equivalents. GNU/Linux already follows SunOS here. */
65
66 #ifndef PTRACE_GETREGS
67 #define PTRACE_GETREGS PT_GETREGS
68 #endif
69
70 #ifndef PTRACE_SETREGS
71 #define PTRACE_SETREGS PT_SETREGS
72 #endif
73
74 #ifndef PTRACE_GETFPREGS
75 #define PTRACE_GETFPREGS PT_GETFPREGS
76 #endif
77
78 #ifndef PTRACE_SETFPREGS
79 #define PTRACE_SETFPREGS PT_SETFPREGS
80 #endif
81
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);
92
93 /* Determine whether `gregset_t' contains register REGNUM. */
94
95 int
96 sparc32_gregset_supplies_p (int regnum)
97 {
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))
103 return 1;
104
105 /* Control registers. */
106 if (regnum == SPARC32_PC_REGNUM
107 || regnum == SPARC32_NPC_REGNUM
108 || regnum == SPARC32_PSR_REGNUM
109 || regnum == SPARC32_Y_REGNUM)
110 return 1;
111
112 return 0;
113 }
114
115 /* Determine whether `fpregset_t' contains register REGNUM. */
116
117 int
118 sparc32_fpregset_supplies_p (int regnum)
119 {
120 /* Floating-point registers. */
121 if (regnum >= SPARC_F0_REGNUM && regnum <= SPARC_F31_REGNUM)
122 return 1;
123
124 /* Control registers. */
125 if (regnum == SPARC32_FSR_REGNUM)
126 return 1;
127
128 return 0;
129 }
130
131 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
132 for all registers (including the floating-point registers). */
133
134 void
135 fetch_inferior_registers (int regnum)
136 {
137 struct regcache *regcache = current_regcache;
138 int pid;
139
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).
149
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);
154 if (pid == 0)
155 pid = PIDGET (inferior_ptid);
156
157 if (regnum == SPARC_G0_REGNUM)
158 {
159 regcache_raw_supply (regcache, SPARC_G0_REGNUM, NULL);
160 return;
161 }
162
163 if (regnum == -1 || sparc_gregset_supplies_p (regnum))
164 {
165 gregset_t regs;
166
167 if (ptrace (PTRACE_GETREGS, pid, (PTRACE_ARG3_TYPE) &regs, 0) == -1)
168 perror_with_name ("Couldn't get registers");
169
170 sparc_supply_gregset (sparc_gregset, regcache, -1, &regs);
171 if (regnum != -1)
172 return;
173 }
174
175 if (regnum == -1 || sparc_fpregset_supplies_p (regnum))
176 {
177 fpregset_t fpregs;
178
179 if (ptrace (PTRACE_GETFPREGS, pid, (PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
180 perror_with_name ("Couldn't get floating point status");
181
182 sparc_supply_fpregset (regcache, -1, &fpregs);
183 }
184 }
185
186 void
187 store_inferior_registers (int regnum)
188 {
189 struct regcache *regcache = current_regcache;
190 int pid;
191
192 /* NOTE: cagney/2002-12-02: See comment in fetch_inferior_registers
193 about threaded assumptions. */
194 pid = TIDGET (inferior_ptid);
195 if (pid == 0)
196 pid = PIDGET (inferior_ptid);
197
198 if (regnum == -1 || sparc_gregset_supplies_p (regnum))
199 {
200 gregset_t regs;
201
202 if (ptrace (PTRACE_GETREGS, pid, (PTRACE_ARG3_TYPE) &regs, 0) == -1)
203 perror_with_name ("Couldn't get registers");
204
205 sparc_collect_gregset (sparc_gregset, regcache, regnum, &regs);
206
207 if (ptrace (PTRACE_SETREGS, pid, (PTRACE_ARG3_TYPE) &regs, 0) == -1)
208 perror_with_name ("Couldn't write registers");
209
210 /* Deal with the stack regs. */
211 if (regnum == -1 || regnum == SPARC_SP_REGNUM
212 || (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM))
213 {
214 ULONGEST sp;
215
216 regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
217 sparc_collect_rwindow (regcache, sp, regnum);
218 }
219
220 if (regnum != -1)
221 return;
222 }
223
224 if (regnum == -1 || sparc_fpregset_supplies_p (regnum))
225 {
226 fpregset_t fpregs, saved_fpregs;
227
228 if (ptrace (PTRACE_GETFPREGS, pid, (PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
229 perror_with_name ("Couldn't get floating-point registers");
230
231 memcpy (&saved_fpregs, &fpregs, sizeof (fpregs));
232 sparc_collect_fpregset (regcache, regnum, &fpregs);
233
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)
239 {
240 if (ptrace (PTRACE_SETFPREGS, pid,
241 (PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
242 perror_with_name ("Couldn't write floating-point registers");
243 }
244
245 if (regnum != -1)
246 return;
247 }
248 }
249 \f
250
251 /* Provide a prototype to silence -Wmissing-prototypes. */
252 void _initialize_sparc_nat (void);
253
254 void
255 _initialize_sparc_nat (void)
256 {
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;
272 }