Commit | Line | Data |
---|---|---|
386c036b | 1 | /* Native-dependent code for SPARC. |
2555fe1a | 2 | |
1a111ce3 | 3 | Copyright 2003, 2004 Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
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. | |
c906108c | 11 | |
c5aa993b JM |
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. | |
c906108c | 16 | |
c5aa993b JM |
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. */ | |
c906108c SS |
21 | |
22 | #include "defs.h" | |
23 | #include "inferior.h" | |
4e052eda | 24 | #include "regcache.h" |
c906108c SS |
25 | |
26 | #include <signal.h> | |
386c036b | 27 | #include "gdb_string.h" |
c906108c | 28 | #include <sys/ptrace.h> |
2555fe1a | 29 | #include "gdb_wait.h" |
386c036b | 30 | #ifdef HAVE_MACHINE_REG_H |
c906108c SS |
31 | #include <machine/reg.h> |
32 | #endif | |
c906108c | 33 | |
386c036b MK |
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, | |
1a111ce3 | 39 | GNU/Linux and the various SPARC BSD's. |
386c036b MK |
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. */ | |
c906108c | 65 | |
386c036b MK |
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 | |
c906108c | 73 | |
386c036b MK |
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). */ | |
c906108c SS |
133 | |
134 | void | |
386c036b | 135 | fetch_inferior_registers (int regnum) |
c906108c | 136 | { |
386c036b MK |
137 | struct regcache *regcache = current_regcache; |
138 | int pid; | |
02ae7771 AC |
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. */ | |
386c036b MK |
153 | pid = TIDGET (inferior_ptid); |
154 | if (pid == 0) | |
155 | pid = PIDGET (inferior_ptid); | |
02ae7771 | 156 | |
386c036b | 157 | if (regnum == SPARC_G0_REGNUM) |
c906108c | 158 | { |
386c036b MK |
159 | regcache_raw_supply (regcache, SPARC_G0_REGNUM, NULL); |
160 | return; | |
c906108c SS |
161 | } |
162 | ||
386c036b | 163 | if (regnum == -1 || sparc_gregset_supplies_p (regnum)) |
c906108c | 164 | { |
386c036b | 165 | gregset_t regs; |
c906108c | 166 | |
386c036b MK |
167 | if (ptrace (PTRACE_GETREGS, pid, (PTRACE_ARG3_TYPE) ®s, 0) == -1) |
168 | perror_with_name ("Couldn't get registers"); | |
169 | ||
170 | sparc_supply_gregset (sparc_gregset, regcache, -1, ®s); | |
171 | if (regnum != -1) | |
172 | return; | |
c906108c | 173 | } |
386c036b MK |
174 | |
175 | if (regnum == -1 || sparc_fpregset_supplies_p (regnum)) | |
c906108c | 176 | { |
386c036b MK |
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); | |
c906108c SS |
183 | } |
184 | } | |
185 | ||
c906108c | 186 | void |
386c036b | 187 | store_inferior_registers (int regnum) |
c906108c | 188 | { |
386c036b MK |
189 | struct regcache *regcache = current_regcache; |
190 | int pid; | |
02ae7771 AC |
191 | |
192 | /* NOTE: cagney/2002-12-02: See comment in fetch_inferior_registers | |
193 | about threaded assumptions. */ | |
386c036b MK |
194 | pid = TIDGET (inferior_ptid); |
195 | if (pid == 0) | |
196 | pid = PIDGET (inferior_ptid); | |
c906108c | 197 | |
386c036b | 198 | if (regnum == -1 || sparc_gregset_supplies_p (regnum)) |
e1925118 | 199 | { |
386c036b | 200 | gregset_t regs; |
c906108c | 201 | |
386c036b MK |
202 | if (ptrace (PTRACE_GETREGS, pid, (PTRACE_ARG3_TYPE) ®s, 0) == -1) |
203 | perror_with_name ("Couldn't get registers"); | |
c906108c | 204 | |
386c036b | 205 | sparc_collect_gregset (sparc_gregset, regcache, regnum, ®s); |
c906108c | 206 | |
386c036b MK |
207 | if (ptrace (PTRACE_SETREGS, pid, (PTRACE_ARG3_TYPE) ®s, 0) == -1) |
208 | perror_with_name ("Couldn't write registers"); | |
c5aa993b | 209 | |
386c036b MK |
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; | |
c906108c | 215 | |
386c036b MK |
216 | regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp); |
217 | sparc_collect_rwindow (regcache, sp, regnum); | |
218 | } | |
c906108c | 219 | |
386c036b MK |
220 | if (regnum != -1) |
221 | return; | |
c906108c | 222 | } |
c906108c | 223 | |
386c036b | 224 | if (regnum == -1 || sparc_fpregset_supplies_p (regnum)) |
c5aa993b | 225 | { |
386c036b | 226 | fpregset_t fpregs, saved_fpregs; |
c906108c | 227 | |
386c036b MK |
228 | if (ptrace (PTRACE_GETFPREGS, pid, (PTRACE_ARG3_TYPE) &fpregs, 0) == -1) |
229 | perror_with_name ("Couldn't get floating-point registers"); | |
c906108c | 230 | |
386c036b MK |
231 | memcpy (&saved_fpregs, &fpregs, sizeof (fpregs)); |
232 | sparc_collect_fpregset (regcache, regnum, &fpregs); | |
c906108c | 233 | |
386c036b MK |
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) | |
c5aa993b | 239 | { |
386c036b MK |
240 | if (ptrace (PTRACE_SETFPREGS, pid, |
241 | (PTRACE_ARG3_TYPE) &fpregs, 0) == -1) | |
242 | perror_with_name ("Couldn't write floating-point registers"); | |
c5aa993b | 243 | } |
c906108c | 244 | |
386c036b MK |
245 | if (regnum != -1) |
246 | return; | |
247 | } | |
c906108c | 248 | } |
c906108c | 249 | \f |
c5aa993b | 250 | |
386c036b MK |
251 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
252 | void _initialize_sparc_nat (void); | |
c906108c SS |
253 | |
254 | void | |
386c036b | 255 | _initialize_sparc_nat (void) |
c906108c | 256 | { |
386c036b MK |
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; | |
c906108c | 272 | } |