sparc-tdep.c revision 1.7 1 1.1 christos /* Target-dependent code for SPARC.
2 1.1 christos
3 1.7 christos Copyright (C) 2003-2017 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos #include "defs.h"
21 1.1 christos #include "arch-utils.h"
22 1.1 christos #include "dis-asm.h"
23 1.1 christos #include "dwarf2-frame.h"
24 1.1 christos #include "floatformat.h"
25 1.1 christos #include "frame.h"
26 1.1 christos #include "frame-base.h"
27 1.1 christos #include "frame-unwind.h"
28 1.1 christos #include "gdbcore.h"
29 1.1 christos #include "gdbtypes.h"
30 1.1 christos #include "inferior.h"
31 1.1 christos #include "symtab.h"
32 1.1 christos #include "objfiles.h"
33 1.1 christos #include "osabi.h"
34 1.1 christos #include "regcache.h"
35 1.1 christos #include "target.h"
36 1.7 christos #include "target-descriptions.h"
37 1.1 christos #include "value.h"
38 1.1 christos
39 1.1 christos #include "sparc-tdep.h"
40 1.1 christos #include "sparc-ravenscar-thread.h"
41 1.7 christos #include <algorithm>
42 1.1 christos
43 1.1 christos struct regset;
44 1.1 christos
45 1.1 christos /* This file implements the SPARC 32-bit ABI as defined by the section
46 1.1 christos "Low-Level System Information" of the SPARC Compliance Definition
47 1.1 christos (SCD) 2.4.1, which is the 32-bit System V psABI for SPARC. The SCD
48 1.1 christos lists changes with respect to the original 32-bit psABI as defined
49 1.1 christos in the "System V ABI, SPARC Processor Supplement".
50 1.1 christos
51 1.1 christos Note that if we talk about SunOS, we mean SunOS 4.x, which was
52 1.1 christos BSD-based, which is sometimes (retroactively?) referred to as
53 1.1 christos Solaris 1.x. If we talk about Solaris we mean Solaris 2.x and
54 1.1 christos above (Solaris 7, 8 and 9 are nothing but Solaris 2.7, 2.8 and 2.9
55 1.1 christos suffering from severe version number inflation). Solaris 2.x is
56 1.1 christos also known as SunOS 5.x, since that's what uname(1) says. Solaris
57 1.1 christos 2.x is SVR4-based. */
58 1.1 christos
59 1.1 christos /* Please use the sparc32_-prefix for 32-bit specific code, the
60 1.1 christos sparc64_-prefix for 64-bit specific code and the sparc_-prefix for
61 1.1 christos code that can handle both. The 64-bit specific code lives in
62 1.1 christos sparc64-tdep.c; don't add any here. */
63 1.1 christos
64 1.1 christos /* The SPARC Floating-Point Quad-Precision format is similar to
65 1.1 christos big-endian IA-64 Quad-Precision format. */
66 1.1 christos #define floatformats_sparc_quad floatformats_ia64_quad
67 1.1 christos
68 1.1 christos /* The stack pointer is offset from the stack frame by a BIAS of 2047
69 1.1 christos (0x7ff) for 64-bit code. BIAS is likely to be defined on SPARC
70 1.1 christos hosts, so undefine it first. */
71 1.1 christos #undef BIAS
72 1.1 christos #define BIAS 2047
73 1.1 christos
74 1.1 christos /* Macros to extract fields from SPARC instructions. */
75 1.1 christos #define X_OP(i) (((i) >> 30) & 0x3)
76 1.1 christos #define X_RD(i) (((i) >> 25) & 0x1f)
77 1.1 christos #define X_A(i) (((i) >> 29) & 1)
78 1.1 christos #define X_COND(i) (((i) >> 25) & 0xf)
79 1.1 christos #define X_OP2(i) (((i) >> 22) & 0x7)
80 1.1 christos #define X_IMM22(i) ((i) & 0x3fffff)
81 1.1 christos #define X_OP3(i) (((i) >> 19) & 0x3f)
82 1.1 christos #define X_RS1(i) (((i) >> 14) & 0x1f)
83 1.1 christos #define X_RS2(i) ((i) & 0x1f)
84 1.1 christos #define X_I(i) (((i) >> 13) & 1)
85 1.1 christos /* Sign extension macros. */
86 1.1 christos #define X_DISP22(i) ((X_IMM22 (i) ^ 0x200000) - 0x200000)
87 1.1 christos #define X_DISP19(i) ((((i) & 0x7ffff) ^ 0x40000) - 0x40000)
88 1.1 christos #define X_DISP10(i) ((((((i) >> 11) && 0x300) | (((i) >> 5) & 0xff)) ^ 0x200) - 0x200)
89 1.1 christos #define X_SIMM13(i) ((((i) & 0x1fff) ^ 0x1000) - 0x1000)
90 1.3 christos /* Macros to identify some instructions. */
91 1.3 christos /* RETURN (RETT in V8) */
92 1.3 christos #define X_RETTURN(i) ((X_OP (i) == 0x2) && (X_OP3 (i) == 0x39))
93 1.1 christos
94 1.1 christos /* Fetch the instruction at PC. Instructions are always big-endian
95 1.1 christos even if the processor operates in little-endian mode. */
96 1.1 christos
97 1.1 christos unsigned long
98 1.1 christos sparc_fetch_instruction (CORE_ADDR pc)
99 1.1 christos {
100 1.1 christos gdb_byte buf[4];
101 1.1 christos unsigned long insn;
102 1.1 christos int i;
103 1.1 christos
104 1.1 christos /* If we can't read the instruction at PC, return zero. */
105 1.1 christos if (target_read_memory (pc, buf, sizeof (buf)))
106 1.1 christos return 0;
107 1.1 christos
108 1.1 christos insn = 0;
109 1.1 christos for (i = 0; i < sizeof (buf); i++)
110 1.1 christos insn = (insn << 8) | buf[i];
111 1.1 christos return insn;
112 1.1 christos }
113 1.1 christos
114 1.1 christos
116 1.1 christos /* Return non-zero if the instruction corresponding to PC is an "unimp"
117 1.1 christos instruction. */
118 1.1 christos
119 1.1 christos static int
120 1.1 christos sparc_is_unimp_insn (CORE_ADDR pc)
121 1.1 christos {
122 1.1 christos const unsigned long insn = sparc_fetch_instruction (pc);
123 1.1 christos
124 1.1 christos return ((insn & 0xc1c00000) == 0);
125 1.1 christos }
126 1.1 christos
127 1.1 christos /* Return non-zero if the instruction corresponding to PC is an
128 1.1 christos "annulled" branch, i.e. the annul bit is set. */
129 1.1 christos
130 1.1 christos int
131 1.1 christos sparc_is_annulled_branch_insn (CORE_ADDR pc)
132 1.1 christos {
133 1.1 christos /* The branch instructions featuring an annul bit can be identified
134 1.1 christos by the following bit patterns:
135 1.1 christos
136 1.1 christos OP=0
137 1.1 christos OP2=1: Branch on Integer Condition Codes with Prediction (BPcc).
138 1.1 christos OP2=2: Branch on Integer Condition Codes (Bcc).
139 1.1 christos OP2=5: Branch on FP Condition Codes with Prediction (FBfcc).
140 1.1 christos OP2=6: Branch on FP Condition Codes (FBcc).
141 1.1 christos OP2=3 && Bit28=0:
142 1.1 christos Branch on Integer Register with Prediction (BPr).
143 1.1 christos
144 1.1 christos This leaves out ILLTRAP (OP2=0), SETHI/NOP (OP2=4) and the V8
145 1.1 christos coprocessor branch instructions (Op2=7). */
146 1.1 christos
147 1.1 christos const unsigned long insn = sparc_fetch_instruction (pc);
148 1.1 christos const unsigned op2 = X_OP2 (insn);
149 1.1 christos
150 1.1 christos if ((X_OP (insn) == 0)
151 1.1 christos && ((op2 == 1) || (op2 == 2) || (op2 == 5) || (op2 == 6)
152 1.1 christos || ((op2 == 3) && ((insn & 0x10000000) == 0))))
153 1.1 christos return X_A (insn);
154 1.1 christos else
155 1.1 christos return 0;
156 1.1 christos }
157 1.1 christos
158 1.1 christos /* OpenBSD/sparc includes StackGhost, which according to the author's
159 1.1 christos website http://stackghost.cerias.purdue.edu "... transparently and
160 1.1 christos automatically protects applications' stack frames; more
161 1.1 christos specifically, it guards the return pointers. The protection
162 1.1 christos mechanisms require no application source or binary modification and
163 1.1 christos imposes only a negligible performance penalty."
164 1.1 christos
165 1.1 christos The same website provides the following description of how
166 1.1 christos StackGhost works:
167 1.1 christos
168 1.1 christos "StackGhost interfaces with the kernel trap handler that would
169 1.1 christos normally write out registers to the stack and the handler that
170 1.1 christos would read them back in. By XORing a cookie into the
171 1.1 christos return-address saved in the user stack when it is actually written
172 1.1 christos to the stack, and then XOR it out when the return-address is pulled
173 1.1 christos from the stack, StackGhost can cause attacker corrupted return
174 1.1 christos pointers to behave in a manner the attacker cannot predict.
175 1.1 christos StackGhost can also use several unused bits in the return pointer
176 1.1 christos to detect a smashed return pointer and abort the process."
177 1.1 christos
178 1.1 christos For GDB this means that whenever we're reading %i7 from a stack
179 1.1 christos frame's window save area, we'll have to XOR the cookie.
180 1.1 christos
181 1.1 christos More information on StackGuard can be found on in:
182 1.1 christos
183 1.1 christos Mike Frantzen and Mike Shuey. "StackGhost: Hardware Facilitated
184 1.1 christos Stack Protection." 2001. Published in USENIX Security Symposium
185 1.1 christos '01. */
186 1.1 christos
187 1.1 christos /* Fetch StackGhost Per-Process XOR cookie. */
188 1.1 christos
189 1.1 christos ULONGEST
190 1.1 christos sparc_fetch_wcookie (struct gdbarch *gdbarch)
191 1.1 christos {
192 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
193 1.1 christos struct target_ops *ops = ¤t_target;
194 1.1 christos gdb_byte buf[8];
195 1.1 christos int len;
196 1.1 christos
197 1.1 christos len = target_read (ops, TARGET_OBJECT_WCOOKIE, NULL, buf, 0, 8);
198 1.1 christos if (len == -1)
199 1.1 christos return 0;
200 1.1 christos
201 1.1 christos /* We should have either an 32-bit or an 64-bit cookie. */
202 1.1 christos gdb_assert (len == 4 || len == 8);
203 1.1 christos
204 1.1 christos return extract_unsigned_integer (buf, len, byte_order);
205 1.1 christos }
206 1.1 christos
207 1.1 christos
209 1.1 christos /* The functions on this page are intended to be used to classify
210 1.1 christos function arguments. */
211 1.1 christos
212 1.1 christos /* Check whether TYPE is "Integral or Pointer". */
213 1.1 christos
214 1.1 christos static int
215 1.1 christos sparc_integral_or_pointer_p (const struct type *type)
216 1.1 christos {
217 1.1 christos int len = TYPE_LENGTH (type);
218 1.1 christos
219 1.1 christos switch (TYPE_CODE (type))
220 1.1 christos {
221 1.1 christos case TYPE_CODE_INT:
222 1.1 christos case TYPE_CODE_BOOL:
223 1.1 christos case TYPE_CODE_CHAR:
224 1.1 christos case TYPE_CODE_ENUM:
225 1.1 christos case TYPE_CODE_RANGE:
226 1.1 christos /* We have byte, half-word, word and extended-word/doubleword
227 1.1 christos integral types. The doubleword is an extension to the
228 1.1 christos original 32-bit ABI by the SCD 2.4.x. */
229 1.1 christos return (len == 1 || len == 2 || len == 4 || len == 8);
230 1.7 christos case TYPE_CODE_PTR:
231 1.1 christos case TYPE_CODE_REF:
232 1.1 christos case TYPE_CODE_RVALUE_REF:
233 1.1 christos /* Allow either 32-bit or 64-bit pointers. */
234 1.1 christos return (len == 4 || len == 8);
235 1.1 christos default:
236 1.1 christos break;
237 1.1 christos }
238 1.1 christos
239 1.1 christos return 0;
240 1.1 christos }
241 1.1 christos
242 1.1 christos /* Check whether TYPE is "Floating". */
243 1.1 christos
244 1.1 christos static int
245 1.1 christos sparc_floating_p (const struct type *type)
246 1.1 christos {
247 1.1 christos switch (TYPE_CODE (type))
248 1.1 christos {
249 1.1 christos case TYPE_CODE_FLT:
250 1.1 christos {
251 1.1 christos int len = TYPE_LENGTH (type);
252 1.1 christos return (len == 4 || len == 8 || len == 16);
253 1.1 christos }
254 1.1 christos default:
255 1.1 christos break;
256 1.1 christos }
257 1.1 christos
258 1.1 christos return 0;
259 1.1 christos }
260 1.1 christos
261 1.1 christos /* Check whether TYPE is "Complex Floating". */
262 1.1 christos
263 1.1 christos static int
264 1.1 christos sparc_complex_floating_p (const struct type *type)
265 1.1 christos {
266 1.1 christos switch (TYPE_CODE (type))
267 1.1 christos {
268 1.1 christos case TYPE_CODE_COMPLEX:
269 1.1 christos {
270 1.1 christos int len = TYPE_LENGTH (type);
271 1.1 christos return (len == 8 || len == 16 || len == 32);
272 1.1 christos }
273 1.1 christos default:
274 1.1 christos break;
275 1.1 christos }
276 1.1 christos
277 1.1 christos return 0;
278 1.1 christos }
279 1.1 christos
280 1.1 christos /* Check whether TYPE is "Structure or Union".
281 1.1 christos
282 1.1 christos In terms of Ada subprogram calls, arrays are treated the same as
283 1.1 christos struct and union types. So this function also returns non-zero
284 1.1 christos for array types. */
285 1.1 christos
286 1.1 christos static int
287 1.1 christos sparc_structure_or_union_p (const struct type *type)
288 1.1 christos {
289 1.1 christos switch (TYPE_CODE (type))
290 1.1 christos {
291 1.1 christos case TYPE_CODE_STRUCT:
292 1.1 christos case TYPE_CODE_UNION:
293 1.1 christos case TYPE_CODE_ARRAY:
294 1.1 christos return 1;
295 1.1 christos default:
296 1.1 christos break;
297 1.1 christos }
298 1.1 christos
299 1.1 christos return 0;
300 1.1 christos }
301 1.7 christos
302 1.7 christos /* Register information. */
303 1.7 christos #define SPARC32_FPU_REGISTERS \
304 1.7 christos "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", \
305 1.7 christos "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15", \
306 1.7 christos "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23", \
307 1.7 christos "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31"
308 1.7 christos #define SPARC32_CP0_REGISTERS \
309 1.7 christos "y", "psr", "wim", "tbr", "pc", "npc", "fsr", "csr"
310 1.7 christos
311 1.7 christos static const char *sparc_core_register_names[] = { SPARC_CORE_REGISTERS };
312 1.1 christos static const char *sparc32_fpu_register_names[] = { SPARC32_FPU_REGISTERS };
313 1.1 christos static const char *sparc32_cp0_register_names[] = { SPARC32_CP0_REGISTERS };
314 1.1 christos
315 1.7 christos static const char *sparc32_register_names[] =
316 1.7 christos {
317 1.7 christos SPARC_CORE_REGISTERS,
318 1.1 christos SPARC32_FPU_REGISTERS,
319 1.1 christos SPARC32_CP0_REGISTERS
320 1.1 christos };
321 1.1 christos
322 1.1 christos /* Total number of registers. */
323 1.1 christos #define SPARC32_NUM_REGS ARRAY_SIZE (sparc32_register_names)
324 1.1 christos
325 1.1 christos /* We provide the aliases %d0..%d30 for the floating registers as
326 1.1 christos "psuedo" registers. */
327 1.1 christos
328 1.1 christos static const char *sparc32_pseudo_register_names[] =
329 1.1 christos {
330 1.1 christos "d0", "d2", "d4", "d6", "d8", "d10", "d12", "d14",
331 1.1 christos "d16", "d18", "d20", "d22", "d24", "d26", "d28", "d30"
332 1.1 christos };
333 1.1 christos
334 1.1 christos /* Total number of pseudo registers. */
335 1.7 christos #define SPARC32_NUM_PSEUDO_REGS ARRAY_SIZE (sparc32_pseudo_register_names)
336 1.7 christos
337 1.7 christos /* Return the name of pseudo register REGNUM. */
338 1.7 christos
339 1.7 christos static const char *
340 1.7 christos sparc32_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
341 1.7 christos {
342 1.7 christos regnum -= gdbarch_num_regs (gdbarch);
343 1.7 christos
344 1.7 christos if (regnum < SPARC32_NUM_PSEUDO_REGS)
345 1.7 christos return sparc32_pseudo_register_names[regnum];
346 1.7 christos
347 1.7 christos internal_error (__FILE__, __LINE__,
348 1.7 christos _("sparc32_pseudo_register_name: bad register number %d"),
349 1.7 christos regnum);
350 1.1 christos }
351 1.1 christos
352 1.1 christos /* Return the name of register REGNUM. */
353 1.1 christos
354 1.1 christos static const char *
355 1.7 christos sparc32_register_name (struct gdbarch *gdbarch, int regnum)
356 1.7 christos {
357 1.7 christos if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
358 1.7 christos return tdesc_register_name (gdbarch, regnum);
359 1.1 christos
360 1.1 christos if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
361 1.7 christos return sparc32_register_names[regnum];
362 1.1 christos
363 1.1 christos return sparc32_pseudo_register_name (gdbarch, regnum);
364 1.1 christos }
365 1.1 christos
366 1.1 christos /* Construct types for ISA-specific registers. */
368 1.1 christos
369 1.1 christos static struct type *
370 1.1 christos sparc_psr_type (struct gdbarch *gdbarch)
371 1.1 christos {
372 1.1 christos struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
373 1.1 christos
374 1.1 christos if (!tdep->sparc_psr_type)
375 1.1 christos {
376 1.1 christos struct type *type;
377 1.1 christos
378 1.1 christos type = arch_flags_type (gdbarch, "builtin_type_sparc_psr", 4);
379 1.1 christos append_flags_type_flag (type, 5, "ET");
380 1.1 christos append_flags_type_flag (type, 6, "PS");
381 1.1 christos append_flags_type_flag (type, 7, "S");
382 1.1 christos append_flags_type_flag (type, 12, "EF");
383 1.1 christos append_flags_type_flag (type, 13, "EC");
384 1.1 christos
385 1.1 christos tdep->sparc_psr_type = type;
386 1.1 christos }
387 1.1 christos
388 1.1 christos return tdep->sparc_psr_type;
389 1.1 christos }
390 1.1 christos
391 1.1 christos static struct type *
392 1.1 christos sparc_fsr_type (struct gdbarch *gdbarch)
393 1.1 christos {
394 1.1 christos struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
395 1.1 christos
396 1.1 christos if (!tdep->sparc_fsr_type)
397 1.1 christos {
398 1.1 christos struct type *type;
399 1.1 christos
400 1.1 christos type = arch_flags_type (gdbarch, "builtin_type_sparc_fsr", 4);
401 1.1 christos append_flags_type_flag (type, 0, "NXA");
402 1.1 christos append_flags_type_flag (type, 1, "DZA");
403 1.1 christos append_flags_type_flag (type, 2, "UFA");
404 1.1 christos append_flags_type_flag (type, 3, "OFA");
405 1.1 christos append_flags_type_flag (type, 4, "NVA");
406 1.1 christos append_flags_type_flag (type, 5, "NXC");
407 1.1 christos append_flags_type_flag (type, 6, "DZC");
408 1.1 christos append_flags_type_flag (type, 7, "UFC");
409 1.1 christos append_flags_type_flag (type, 8, "OFC");
410 1.1 christos append_flags_type_flag (type, 9, "NVC");
411 1.1 christos append_flags_type_flag (type, 22, "NS");
412 1.1 christos append_flags_type_flag (type, 23, "NXM");
413 1.1 christos append_flags_type_flag (type, 24, "DZM");
414 1.1 christos append_flags_type_flag (type, 25, "UFM");
415 1.1 christos append_flags_type_flag (type, 26, "OFM");
416 1.1 christos append_flags_type_flag (type, 27, "NVM");
417 1.1 christos
418 1.1 christos tdep->sparc_fsr_type = type;
419 1.1 christos }
420 1.1 christos
421 1.1 christos return tdep->sparc_fsr_type;
422 1.7 christos }
423 1.7 christos
424 1.7 christos /* Return the GDB type object for the "standard" data type of data in
425 1.7 christos pseudo register REGNUM. */
426 1.7 christos
427 1.7 christos static struct type *
428 1.7 christos sparc32_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
429 1.7 christos {
430 1.7 christos regnum -= gdbarch_num_regs (gdbarch);
431 1.7 christos
432 1.7 christos if (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM)
433 1.7 christos return builtin_type (gdbarch)->builtin_double;
434 1.7 christos
435 1.7 christos internal_error (__FILE__, __LINE__,
436 1.7 christos _("sparc32_pseudo_register_type: bad register number %d"),
437 1.7 christos regnum);
438 1.1 christos }
439 1.1 christos
440 1.1 christos /* Return the GDB type object for the "standard" data type of data in
441 1.1 christos register REGNUM. */
442 1.1 christos
443 1.7 christos static struct type *
444 1.7 christos sparc32_register_type (struct gdbarch *gdbarch, int regnum)
445 1.7 christos {
446 1.1 christos if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
447 1.1 christos return tdesc_register_type (gdbarch, regnum);
448 1.1 christos
449 1.1 christos if (regnum >= SPARC_F0_REGNUM && regnum <= SPARC_F31_REGNUM)
450 1.1 christos return builtin_type (gdbarch)->builtin_float;
451 1.1 christos
452 1.1 christos if (regnum == SPARC_SP_REGNUM || regnum == SPARC_FP_REGNUM)
453 1.1 christos return builtin_type (gdbarch)->builtin_data_ptr;
454 1.1 christos
455 1.1 christos if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
456 1.1 christos return builtin_type (gdbarch)->builtin_func_ptr;
457 1.1 christos
458 1.1 christos if (regnum == SPARC32_PSR_REGNUM)
459 1.1 christos return sparc_psr_type (gdbarch);
460 1.1 christos
461 1.7 christos if (regnum == SPARC32_FSR_REGNUM)
462 1.7 christos return sparc_fsr_type (gdbarch);
463 1.7 christos
464 1.1 christos if (regnum >= gdbarch_num_regs (gdbarch))
465 1.1 christos return sparc32_pseudo_register_type (gdbarch, regnum);
466 1.1 christos
467 1.1 christos return builtin_type (gdbarch)->builtin_int32;
468 1.1 christos }
469 1.1 christos
470 1.1 christos static enum register_status
471 1.1 christos sparc32_pseudo_register_read (struct gdbarch *gdbarch,
472 1.1 christos struct regcache *regcache,
473 1.1 christos int regnum, gdb_byte *buf)
474 1.7 christos {
475 1.1 christos enum register_status status;
476 1.1 christos
477 1.1 christos regnum -= gdbarch_num_regs (gdbarch);
478 1.1 christos gdb_assert (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM);
479 1.1 christos
480 1.1 christos regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC32_D0_REGNUM);
481 1.1 christos status = regcache_raw_read (regcache, regnum, buf);
482 1.1 christos if (status == REG_VALID)
483 1.1 christos status = regcache_raw_read (regcache, regnum + 1, buf + 4);
484 1.1 christos return status;
485 1.1 christos }
486 1.1 christos
487 1.1 christos static void
488 1.1 christos sparc32_pseudo_register_write (struct gdbarch *gdbarch,
489 1.7 christos struct regcache *regcache,
490 1.1 christos int regnum, const gdb_byte *buf)
491 1.1 christos {
492 1.1 christos regnum -= gdbarch_num_regs (gdbarch);
493 1.1 christos gdb_assert (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM);
494 1.1 christos
495 1.1 christos regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC32_D0_REGNUM);
496 1.1 christos regcache_raw_write (regcache, regnum, buf);
497 1.5 christos regcache_raw_write (regcache, regnum + 1, buf + 4);
498 1.3 christos }
499 1.3 christos
500 1.5 christos /* Implement the stack_frame_destroyed_p gdbarch method. */
502 1.3 christos
503 1.3 christos int
504 1.3 christos sparc_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
505 1.3 christos {
506 1.3 christos /* This function must return true if we are one instruction after an
507 1.3 christos instruction that destroyed the stack frame of the current
508 1.3 christos function. The SPARC instructions used to restore the callers
509 1.3 christos stack frame are RESTORE and RETURN/RETT.
510 1.3 christos
511 1.3 christos Of these RETURN/RETT is a branch instruction and thus we return
512 1.3 christos true if we are in its delay slot.
513 1.3 christos
514 1.3 christos RESTORE is almost always found in the delay slot of a branch
515 1.3 christos instruction that transfers control to the caller, such as JMPL.
516 1.3 christos Thus the next instruction is in the caller frame and we don't
517 1.3 christos need to do anything about it. */
518 1.3 christos
519 1.3 christos unsigned int insn = sparc_fetch_instruction (pc - 4);
520 1.1 christos
521 1.1 christos return X_RETTURN (insn);
522 1.1 christos }
523 1.1 christos
524 1.1 christos
526 1.1 christos static CORE_ADDR
527 1.1 christos sparc32_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
528 1.1 christos {
529 1.1 christos /* The ABI requires double-word alignment. */
530 1.1 christos return address & ~0x7;
531 1.1 christos }
532 1.1 christos
533 1.1 christos static CORE_ADDR
534 1.1 christos sparc32_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
535 1.1 christos CORE_ADDR funcaddr,
536 1.1 christos struct value **args, int nargs,
537 1.1 christos struct type *value_type,
538 1.1 christos CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
539 1.1 christos struct regcache *regcache)
540 1.1 christos {
541 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
542 1.1 christos
543 1.1 christos *bp_addr = sp - 4;
544 1.1 christos *real_pc = funcaddr;
545 1.1 christos
546 1.1 christos if (using_struct_return (gdbarch, NULL, value_type))
547 1.1 christos {
548 1.1 christos gdb_byte buf[4];
549 1.1 christos
550 1.1 christos /* This is an UNIMP instruction. */
551 1.1 christos store_unsigned_integer (buf, 4, byte_order,
552 1.1 christos TYPE_LENGTH (value_type) & 0x1fff);
553 1.1 christos write_memory (sp - 8, buf, 4);
554 1.1 christos return sp - 8;
555 1.1 christos }
556 1.1 christos
557 1.1 christos return sp - 4;
558 1.1 christos }
559 1.1 christos
560 1.1 christos static CORE_ADDR
561 1.1 christos sparc32_store_arguments (struct regcache *regcache, int nargs,
562 1.1 christos struct value **args, CORE_ADDR sp,
563 1.1 christos int struct_return, CORE_ADDR struct_addr)
564 1.1 christos {
565 1.1 christos struct gdbarch *gdbarch = get_regcache_arch (regcache);
566 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
567 1.1 christos /* Number of words in the "parameter array". */
568 1.1 christos int num_elements = 0;
569 1.1 christos int element = 0;
570 1.1 christos int i;
571 1.1 christos
572 1.1 christos for (i = 0; i < nargs; i++)
573 1.1 christos {
574 1.1 christos struct type *type = value_type (args[i]);
575 1.1 christos int len = TYPE_LENGTH (type);
576 1.1 christos
577 1.1 christos if (sparc_structure_or_union_p (type)
578 1.1 christos || (sparc_floating_p (type) && len == 16)
579 1.1 christos || sparc_complex_floating_p (type))
580 1.1 christos {
581 1.1 christos /* Structure, Union and Quad-Precision Arguments. */
582 1.1 christos sp -= len;
583 1.1 christos
584 1.1 christos /* Use doubleword alignment for these values. That's always
585 1.1 christos correct, and wasting a few bytes shouldn't be a problem. */
586 1.1 christos sp &= ~0x7;
587 1.1 christos
588 1.1 christos write_memory (sp, value_contents (args[i]), len);
589 1.1 christos args[i] = value_from_pointer (lookup_pointer_type (type), sp);
590 1.1 christos num_elements++;
591 1.1 christos }
592 1.1 christos else if (sparc_floating_p (type))
593 1.1 christos {
594 1.1 christos /* Floating arguments. */
595 1.1 christos gdb_assert (len == 4 || len == 8);
596 1.1 christos num_elements += (len / 4);
597 1.1 christos }
598 1.1 christos else
599 1.1 christos {
600 1.1 christos /* Integral and pointer arguments. */
601 1.1 christos gdb_assert (sparc_integral_or_pointer_p (type));
602 1.1 christos
603 1.1 christos if (len < 4)
604 1.1 christos args[i] = value_cast (builtin_type (gdbarch)->builtin_int32,
605 1.1 christos args[i]);
606 1.7 christos num_elements += ((len + 3) / 4);
607 1.1 christos }
608 1.1 christos }
609 1.1 christos
610 1.1 christos /* Always allocate at least six words. */
611 1.1 christos sp -= std::max (6, num_elements) * 4;
612 1.1 christos
613 1.1 christos /* The psABI says that "Software convention requires space for the
614 1.1 christos struct/union return value pointer, even if the word is unused." */
615 1.1 christos sp -= 4;
616 1.1 christos
617 1.1 christos /* The psABI says that "Although software convention and the
618 1.1 christos operating system require every stack frame to be doubleword
619 1.1 christos aligned." */
620 1.1 christos sp &= ~0x7;
621 1.1 christos
622 1.1 christos for (i = 0; i < nargs; i++)
623 1.1 christos {
624 1.1 christos const bfd_byte *valbuf = value_contents (args[i]);
625 1.1 christos struct type *type = value_type (args[i]);
626 1.1 christos int len = TYPE_LENGTH (type);
627 1.1 christos
628 1.1 christos gdb_assert (len == 4 || len == 8);
629 1.1 christos
630 1.1 christos if (element < 6)
631 1.1 christos {
632 1.1 christos int regnum = SPARC_O0_REGNUM + element;
633 1.1 christos
634 1.1 christos regcache_cooked_write (regcache, regnum, valbuf);
635 1.1 christos if (len > 4 && element < 5)
636 1.1 christos regcache_cooked_write (regcache, regnum + 1, valbuf + 4);
637 1.1 christos }
638 1.1 christos
639 1.1 christos /* Always store the argument in memory. */
640 1.1 christos write_memory (sp + 4 + element * 4, valbuf, len);
641 1.1 christos element += len / 4;
642 1.1 christos }
643 1.1 christos
644 1.1 christos gdb_assert (element == num_elements);
645 1.1 christos
646 1.1 christos if (struct_return)
647 1.1 christos {
648 1.1 christos gdb_byte buf[4];
649 1.1 christos
650 1.1 christos store_unsigned_integer (buf, 4, byte_order, struct_addr);
651 1.1 christos write_memory (sp, buf, 4);
652 1.1 christos }
653 1.1 christos
654 1.1 christos return sp;
655 1.1 christos }
656 1.1 christos
657 1.1 christos static CORE_ADDR
658 1.1 christos sparc32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
659 1.1 christos struct regcache *regcache, CORE_ADDR bp_addr,
660 1.1 christos int nargs, struct value **args, CORE_ADDR sp,
661 1.1 christos int struct_return, CORE_ADDR struct_addr)
662 1.1 christos {
663 1.1 christos CORE_ADDR call_pc = (struct_return ? (bp_addr - 12) : (bp_addr - 8));
664 1.1 christos
665 1.1 christos /* Set return address. */
666 1.1 christos regcache_cooked_write_unsigned (regcache, SPARC_O7_REGNUM, call_pc);
667 1.1 christos
668 1.1 christos /* Set up function arguments. */
669 1.1 christos sp = sparc32_store_arguments (regcache, nargs, args, sp,
670 1.1 christos struct_return, struct_addr);
671 1.1 christos
672 1.1 christos /* Allocate the 16-word window save area. */
673 1.1 christos sp -= 16 * 4;
674 1.1 christos
675 1.1 christos /* Stack should be doubleword aligned at this point. */
676 1.1 christos gdb_assert (sp % 8 == 0);
677 1.1 christos
678 1.1 christos /* Finally, update the stack pointer. */
679 1.1 christos regcache_cooked_write_unsigned (regcache, SPARC_SP_REGNUM, sp);
680 1.1 christos
681 1.1 christos return sp;
682 1.1 christos }
683 1.1 christos
684 1.1 christos
686 1.1 christos /* Use the program counter to determine the contents and size of a
687 1.7 christos breakpoint instruction. Return a pointer to a string of bytes that
688 1.1 christos encode a breakpoint instruction, store the length of the string in
689 1.1 christos *LEN and optionally adjust *PC to point to the correct memory
690 1.1 christos location for inserting the breakpoint. */
691 1.1 christos constexpr gdb_byte sparc_break_insn[] = { 0x91, 0xd0, 0x20, 0x01 };
692 1.1 christos
693 1.1 christos typedef BP_MANIPULATION (sparc_break_insn) sparc_breakpoint;
694 1.1 christos
695 1.1 christos
697 1.1 christos /* Allocate and initialize a frame cache. */
698 1.1 christos
699 1.1 christos static struct sparc_frame_cache *
700 1.1 christos sparc_alloc_frame_cache (void)
701 1.1 christos {
702 1.1 christos struct sparc_frame_cache *cache;
703 1.1 christos
704 1.1 christos cache = FRAME_OBSTACK_ZALLOC (struct sparc_frame_cache);
705 1.1 christos
706 1.1 christos /* Base address. */
707 1.1 christos cache->base = 0;
708 1.1 christos cache->pc = 0;
709 1.1 christos
710 1.1 christos /* Frameless until proven otherwise. */
711 1.1 christos cache->frameless_p = 1;
712 1.1 christos cache->frame_offset = 0;
713 1.1 christos cache->saved_regs_mask = 0;
714 1.1 christos cache->copied_regs_mask = 0;
715 1.1 christos cache->struct_return_p = 0;
716 1.1 christos
717 1.1 christos return cache;
718 1.1 christos }
719 1.1 christos
720 1.1 christos /* GCC generates several well-known sequences of instructions at the begining
721 1.1 christos of each function prologue when compiling with -fstack-check. If one of
722 1.1 christos such sequences starts at START_PC, then return the address of the
723 1.1 christos instruction immediately past this sequence. Otherwise, return START_PC. */
724 1.1 christos
725 1.1 christos static CORE_ADDR
726 1.1 christos sparc_skip_stack_check (const CORE_ADDR start_pc)
727 1.1 christos {
728 1.1 christos CORE_ADDR pc = start_pc;
729 1.1 christos unsigned long insn;
730 1.1 christos int probing_loop = 0;
731 1.1 christos
732 1.1 christos /* With GCC, all stack checking sequences begin with the same two
733 1.1 christos instructions, plus an optional one in the case of a probing loop:
734 1.1 christos
735 1.1 christos sethi <some immediate>, %g1
736 1.1 christos sub %sp, %g1, %g1
737 1.1 christos
738 1.1 christos or:
739 1.1 christos
740 1.1 christos sethi <some immediate>, %g1
741 1.1 christos sethi <some immediate>, %g4
742 1.1 christos sub %sp, %g1, %g1
743 1.1 christos
744 1.1 christos or:
745 1.1 christos
746 1.1 christos sethi <some immediate>, %g1
747 1.1 christos sub %sp, %g1, %g1
748 1.1 christos sethi <some immediate>, %g4
749 1.1 christos
750 1.1 christos If the optional instruction is found (setting g4), assume that a
751 1.1 christos probing loop will follow. */
752 1.1 christos
753 1.1 christos /* sethi <some immediate>, %g1 */
754 1.1 christos insn = sparc_fetch_instruction (pc);
755 1.1 christos pc = pc + 4;
756 1.1 christos if (!(X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 1))
757 1.1 christos return start_pc;
758 1.1 christos
759 1.1 christos /* optional: sethi <some immediate>, %g4 */
760 1.1 christos insn = sparc_fetch_instruction (pc);
761 1.1 christos pc = pc + 4;
762 1.1 christos if (X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 4)
763 1.1 christos {
764 1.1 christos probing_loop = 1;
765 1.1 christos insn = sparc_fetch_instruction (pc);
766 1.1 christos pc = pc + 4;
767 1.1 christos }
768 1.1 christos
769 1.1 christos /* sub %sp, %g1, %g1 */
770 1.1 christos if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x4 && !X_I(insn)
771 1.1 christos && X_RD (insn) == 1 && X_RS1 (insn) == 14 && X_RS2 (insn) == 1))
772 1.1 christos return start_pc;
773 1.1 christos
774 1.1 christos insn = sparc_fetch_instruction (pc);
775 1.1 christos pc = pc + 4;
776 1.1 christos
777 1.1 christos /* optional: sethi <some immediate>, %g4 */
778 1.1 christos if (X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 4)
779 1.1 christos {
780 1.1 christos probing_loop = 1;
781 1.1 christos insn = sparc_fetch_instruction (pc);
782 1.1 christos pc = pc + 4;
783 1.1 christos }
784 1.1 christos
785 1.1 christos /* First possible sequence:
786 1.1 christos [first two instructions above]
787 1.1 christos clr [%g1 - some immediate] */
788 1.1 christos
789 1.1 christos /* clr [%g1 - some immediate] */
790 1.1 christos if (X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
791 1.1 christos && X_RS1 (insn) == 1 && X_RD (insn) == 0)
792 1.1 christos {
793 1.1 christos /* Valid stack-check sequence, return the new PC. */
794 1.1 christos return pc;
795 1.1 christos }
796 1.1 christos
797 1.1 christos /* Second possible sequence: A small number of probes.
798 1.1 christos [first two instructions above]
799 1.1 christos clr [%g1]
800 1.1 christos add %g1, -<some immediate>, %g1
801 1.1 christos clr [%g1]
802 1.1 christos [repeat the two instructions above any (small) number of times]
803 1.1 christos clr [%g1 - some immediate] */
804 1.1 christos
805 1.1 christos /* clr [%g1] */
806 1.1 christos else if (X_OP (insn) == 3 && X_OP3(insn) == 0x4 && !X_I(insn)
807 1.1 christos && X_RS1 (insn) == 1 && X_RD (insn) == 0)
808 1.1 christos {
809 1.1 christos while (1)
810 1.1 christos {
811 1.1 christos /* add %g1, -<some immediate>, %g1 */
812 1.1 christos insn = sparc_fetch_instruction (pc);
813 1.1 christos pc = pc + 4;
814 1.1 christos if (!(X_OP (insn) == 2 && X_OP3(insn) == 0 && X_I(insn)
815 1.1 christos && X_RS1 (insn) == 1 && X_RD (insn) == 1))
816 1.1 christos break;
817 1.1 christos
818 1.1 christos /* clr [%g1] */
819 1.1 christos insn = sparc_fetch_instruction (pc);
820 1.1 christos pc = pc + 4;
821 1.1 christos if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && !X_I(insn)
822 1.1 christos && X_RD (insn) == 0 && X_RS1 (insn) == 1))
823 1.1 christos return start_pc;
824 1.1 christos }
825 1.1 christos
826 1.1 christos /* clr [%g1 - some immediate] */
827 1.1 christos if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
828 1.1 christos && X_RS1 (insn) == 1 && X_RD (insn) == 0))
829 1.1 christos return start_pc;
830 1.1 christos
831 1.1 christos /* We found a valid stack-check sequence, return the new PC. */
832 1.1 christos return pc;
833 1.1 christos }
834 1.1 christos
835 1.1 christos /* Third sequence: A probing loop.
836 1.1 christos [first three instructions above]
837 1.1 christos sub %g1, %g4, %g4
838 1.1 christos cmp %g1, %g4
839 1.1 christos be <disp>
840 1.1 christos add %g1, -<some immediate>, %g1
841 1.1 christos ba <disp>
842 1.1 christos clr [%g1]
843 1.1 christos
844 1.1 christos And an optional last probe for the remainder:
845 1.1 christos
846 1.1 christos clr [%g4 - some immediate] */
847 1.1 christos
848 1.1 christos if (probing_loop)
849 1.1 christos {
850 1.1 christos /* sub %g1, %g4, %g4 */
851 1.1 christos if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x4 && !X_I(insn)
852 1.1 christos && X_RD (insn) == 4 && X_RS1 (insn) == 1 && X_RS2 (insn) == 4))
853 1.1 christos return start_pc;
854 1.1 christos
855 1.1 christos /* cmp %g1, %g4 */
856 1.1 christos insn = sparc_fetch_instruction (pc);
857 1.1 christos pc = pc + 4;
858 1.1 christos if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x14 && !X_I(insn)
859 1.1 christos && X_RD (insn) == 0 && X_RS1 (insn) == 1 && X_RS2 (insn) == 4))
860 1.1 christos return start_pc;
861 1.1 christos
862 1.1 christos /* be <disp> */
863 1.1 christos insn = sparc_fetch_instruction (pc);
864 1.1 christos pc = pc + 4;
865 1.1 christos if (!(X_OP (insn) == 0 && X_COND (insn) == 0x1))
866 1.1 christos return start_pc;
867 1.1 christos
868 1.1 christos /* add %g1, -<some immediate>, %g1 */
869 1.1 christos insn = sparc_fetch_instruction (pc);
870 1.1 christos pc = pc + 4;
871 1.1 christos if (!(X_OP (insn) == 2 && X_OP3(insn) == 0 && X_I(insn)
872 1.1 christos && X_RS1 (insn) == 1 && X_RD (insn) == 1))
873 1.1 christos return start_pc;
874 1.1 christos
875 1.1 christos /* ba <disp> */
876 1.1 christos insn = sparc_fetch_instruction (pc);
877 1.1 christos pc = pc + 4;
878 1.1 christos if (!(X_OP (insn) == 0 && X_COND (insn) == 0x8))
879 1.1 christos return start_pc;
880 1.1 christos
881 1.1 christos /* clr [%g1] (st %g0, [%g1] or st %g0, [%g1+0]) */
882 1.1 christos insn = sparc_fetch_instruction (pc);
883 1.1 christos pc = pc + 4;
884 1.1 christos if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4
885 1.1 christos && X_RD (insn) == 0 && X_RS1 (insn) == 1
886 1.1 christos && (!X_I(insn) || X_SIMM13 (insn) == 0)))
887 1.1 christos return start_pc;
888 1.1 christos
889 1.1 christos /* We found a valid stack-check sequence, return the new PC. */
890 1.1 christos
891 1.1 christos /* optional: clr [%g4 - some immediate] */
892 1.1 christos insn = sparc_fetch_instruction (pc);
893 1.1 christos pc = pc + 4;
894 1.1 christos if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
895 1.1 christos && X_RS1 (insn) == 4 && X_RD (insn) == 0))
896 1.1 christos return pc - 4;
897 1.1 christos else
898 1.1 christos return pc;
899 1.1 christos }
900 1.1 christos
901 1.1 christos /* No stack check code in our prologue, return the start_pc. */
902 1.1 christos return start_pc;
903 1.1 christos }
904 1.1 christos
905 1.1 christos /* Record the effect of a SAVE instruction on CACHE. */
906 1.1 christos
907 1.1 christos void
908 1.1 christos sparc_record_save_insn (struct sparc_frame_cache *cache)
909 1.1 christos {
910 1.1 christos /* The frame is set up. */
911 1.1 christos cache->frameless_p = 0;
912 1.1 christos
913 1.1 christos /* The frame pointer contains the CFA. */
914 1.1 christos cache->frame_offset = 0;
915 1.1 christos
916 1.1 christos /* The `local' and `in' registers are all saved. */
917 1.1 christos cache->saved_regs_mask = 0xffff;
918 1.1 christos
919 1.1 christos /* The `out' registers are all renamed. */
920 1.1 christos cache->copied_regs_mask = 0xff;
921 1.1 christos }
922 1.1 christos
923 1.1 christos /* Do a full analysis of the prologue at PC and update CACHE accordingly.
924 1.1 christos Bail out early if CURRENT_PC is reached. Return the address where
925 1.1 christos the analysis stopped.
926 1.1 christos
927 1.1 christos We handle both the traditional register window model and the single
928 1.1 christos register window (aka flat) model. */
929 1.1 christos
930 1.1 christos CORE_ADDR
931 1.1 christos sparc_analyze_prologue (struct gdbarch *gdbarch, CORE_ADDR pc,
932 1.1 christos CORE_ADDR current_pc, struct sparc_frame_cache *cache)
933 1.1 christos {
934 1.1 christos struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
935 1.1 christos unsigned long insn;
936 1.1 christos int offset = 0;
937 1.1 christos int dest = -1;
938 1.1 christos
939 1.1 christos pc = sparc_skip_stack_check (pc);
940 1.1 christos
941 1.1 christos if (current_pc <= pc)
942 1.1 christos return current_pc;
943 1.1 christos
944 1.1 christos /* We have to handle to "Procedure Linkage Table" (PLT) special. On
945 1.1 christos SPARC the linker usually defines a symbol (typically
946 1.1 christos _PROCEDURE_LINKAGE_TABLE_) at the start of the .plt section.
947 1.1 christos This symbol makes us end up here with PC pointing at the start of
948 1.1 christos the PLT and CURRENT_PC probably pointing at a PLT entry. If we
949 1.1 christos would do our normal prologue analysis, we would probably conclude
950 1.1 christos that we've got a frame when in reality we don't, since the
951 1.1 christos dynamic linker patches up the first PLT with some code that
952 1.1 christos starts with a SAVE instruction. Patch up PC such that it points
953 1.1 christos at the start of our PLT entry. */
954 1.1 christos if (tdep->plt_entry_size > 0 && in_plt_section (current_pc))
955 1.1 christos pc = current_pc - ((current_pc - pc) % tdep->plt_entry_size);
956 1.1 christos
957 1.1 christos insn = sparc_fetch_instruction (pc);
958 1.1 christos
959 1.1 christos /* Recognize store insns and record their sources. */
960 1.1 christos while (X_OP (insn) == 3
961 1.1 christos && (X_OP3 (insn) == 0x4 /* stw */
962 1.1 christos || X_OP3 (insn) == 0x7 /* std */
963 1.1 christos || X_OP3 (insn) == 0xe) /* stx */
964 1.1 christos && X_RS1 (insn) == SPARC_SP_REGNUM)
965 1.1 christos {
966 1.1 christos int regnum = X_RD (insn);
967 1.1 christos
968 1.1 christos /* Recognize stores into the corresponding stack slots. */
969 1.1 christos if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM
970 1.1 christos && ((X_I (insn)
971 1.1 christos && X_SIMM13 (insn) == (X_OP3 (insn) == 0xe
972 1.1 christos ? (regnum - SPARC_L0_REGNUM) * 8 + BIAS
973 1.1 christos : (regnum - SPARC_L0_REGNUM) * 4))
974 1.1 christos || (!X_I (insn) && regnum == SPARC_L0_REGNUM)))
975 1.1 christos {
976 1.1 christos cache->saved_regs_mask |= (1 << (regnum - SPARC_L0_REGNUM));
977 1.1 christos if (X_OP3 (insn) == 0x7)
978 1.1 christos cache->saved_regs_mask |= (1 << (regnum + 1 - SPARC_L0_REGNUM));
979 1.1 christos }
980 1.1 christos
981 1.1 christos offset += 4;
982 1.1 christos
983 1.1 christos insn = sparc_fetch_instruction (pc + offset);
984 1.1 christos }
985 1.1 christos
986 1.1 christos /* Recognize a SETHI insn and record its destination. */
987 1.1 christos if (X_OP (insn) == 0 && X_OP2 (insn) == 0x04)
988 1.1 christos {
989 1.1 christos dest = X_RD (insn);
990 1.1 christos offset += 4;
991 1.1 christos
992 1.1 christos insn = sparc_fetch_instruction (pc + offset);
993 1.1 christos }
994 1.1 christos
995 1.1 christos /* Allow for an arithmetic operation on DEST or %g1. */
996 1.1 christos if (X_OP (insn) == 2 && X_I (insn)
997 1.1 christos && (X_RD (insn) == 1 || X_RD (insn) == dest))
998 1.1 christos {
999 1.1 christos offset += 4;
1000 1.1 christos
1001 1.1 christos insn = sparc_fetch_instruction (pc + offset);
1002 1.1 christos }
1003 1.1 christos
1004 1.1 christos /* Check for the SAVE instruction that sets up the frame. */
1005 1.1 christos if (X_OP (insn) == 2 && X_OP3 (insn) == 0x3c)
1006 1.1 christos {
1007 1.1 christos sparc_record_save_insn (cache);
1008 1.1 christos offset += 4;
1009 1.1 christos return pc + offset;
1010 1.1 christos }
1011 1.1 christos
1012 1.1 christos /* Check for an arithmetic operation on %sp. */
1013 1.1 christos if (X_OP (insn) == 2
1014 1.1 christos && (X_OP3 (insn) == 0 || X_OP3 (insn) == 0x4)
1015 1.1 christos && X_RS1 (insn) == SPARC_SP_REGNUM
1016 1.1 christos && X_RD (insn) == SPARC_SP_REGNUM)
1017 1.1 christos {
1018 1.1 christos if (X_I (insn))
1019 1.1 christos {
1020 1.1 christos cache->frame_offset = X_SIMM13 (insn);
1021 1.1 christos if (X_OP3 (insn) == 0)
1022 1.1 christos cache->frame_offset = -cache->frame_offset;
1023 1.1 christos }
1024 1.1 christos offset += 4;
1025 1.1 christos
1026 1.1 christos insn = sparc_fetch_instruction (pc + offset);
1027 1.1 christos
1028 1.1 christos /* Check for an arithmetic operation that sets up the frame. */
1029 1.1 christos if (X_OP (insn) == 2
1030 1.1 christos && (X_OP3 (insn) == 0 || X_OP3 (insn) == 0x4)
1031 1.1 christos && X_RS1 (insn) == SPARC_SP_REGNUM
1032 1.1 christos && X_RD (insn) == SPARC_FP_REGNUM)
1033 1.1 christos {
1034 1.1 christos cache->frameless_p = 0;
1035 1.1 christos cache->frame_offset = 0;
1036 1.1 christos /* We could check that the amount subtracted to %sp above is the
1037 1.1 christos same as the one added here, but this seems superfluous. */
1038 1.1 christos cache->copied_regs_mask |= 0x40;
1039 1.1 christos offset += 4;
1040 1.1 christos
1041 1.1 christos insn = sparc_fetch_instruction (pc + offset);
1042 1.1 christos }
1043 1.1 christos
1044 1.1 christos /* Check for a move (or) operation that copies the return register. */
1045 1.1 christos if (X_OP (insn) == 2
1046 1.1 christos && X_OP3 (insn) == 0x2
1047 1.1 christos && !X_I (insn)
1048 1.1 christos && X_RS1 (insn) == SPARC_G0_REGNUM
1049 1.1 christos && X_RS2 (insn) == SPARC_O7_REGNUM
1050 1.1 christos && X_RD (insn) == SPARC_I7_REGNUM)
1051 1.1 christos {
1052 1.1 christos cache->copied_regs_mask |= 0x80;
1053 1.1 christos offset += 4;
1054 1.1 christos }
1055 1.1 christos
1056 1.1 christos return pc + offset;
1057 1.1 christos }
1058 1.1 christos
1059 1.1 christos return pc;
1060 1.1 christos }
1061 1.1 christos
1062 1.1 christos static CORE_ADDR
1063 1.1 christos sparc_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
1064 1.1 christos {
1065 1.1 christos struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1066 1.1 christos return frame_unwind_register_unsigned (this_frame, tdep->pc_regnum);
1067 1.1 christos }
1068 1.1 christos
1069 1.1 christos /* Return PC of first real instruction of the function starting at
1070 1.1 christos START_PC. */
1071 1.1 christos
1072 1.1 christos static CORE_ADDR
1073 1.1 christos sparc32_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
1074 1.1 christos {
1075 1.1 christos struct symtab_and_line sal;
1076 1.1 christos CORE_ADDR func_start, func_end;
1077 1.1 christos struct sparc_frame_cache cache;
1078 1.1 christos
1079 1.1 christos /* This is the preferred method, find the end of the prologue by
1080 1.1 christos using the debugging information. */
1081 1.1 christos if (find_pc_partial_function (start_pc, NULL, &func_start, &func_end))
1082 1.1 christos {
1083 1.1 christos sal = find_pc_line (func_start, 0);
1084 1.1 christos
1085 1.1 christos if (sal.end < func_end
1086 1.1 christos && start_pc <= sal.end)
1087 1.1 christos return sal.end;
1088 1.1 christos }
1089 1.1 christos
1090 1.1 christos start_pc = sparc_analyze_prologue (gdbarch, start_pc, 0xffffffffUL, &cache);
1091 1.1 christos
1092 1.1 christos /* The psABI says that "Although the first 6 words of arguments
1093 1.1 christos reside in registers, the standard stack frame reserves space for
1094 1.1 christos them.". It also suggests that a function may use that space to
1095 1.1 christos "write incoming arguments 0 to 5" into that space, and that's
1096 1.1 christos indeed what GCC seems to be doing. In that case GCC will
1097 1.1 christos generate debug information that points to the stack slots instead
1098 1.1 christos of the registers, so we should consider the instructions that
1099 1.1 christos write out these incoming arguments onto the stack. */
1100 1.1 christos
1101 1.1 christos while (1)
1102 1.1 christos {
1103 1.1 christos unsigned long insn = sparc_fetch_instruction (start_pc);
1104 1.1 christos
1105 1.1 christos /* Recognize instructions that store incoming arguments into the
1106 1.1 christos corresponding stack slots. */
1107 1.1 christos if (X_OP (insn) == 3 && (X_OP3 (insn) & 0x3c) == 0x04
1108 1.1 christos && X_I (insn) && X_RS1 (insn) == SPARC_FP_REGNUM)
1109 1.1 christos {
1110 1.1 christos int regnum = X_RD (insn);
1111 1.1 christos
1112 1.1 christos /* Case of arguments still in %o[0..5]. */
1113 1.1 christos if (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O5_REGNUM
1114 1.1 christos && !(cache.copied_regs_mask & (1 << (regnum - SPARC_O0_REGNUM)))
1115 1.1 christos && X_SIMM13 (insn) == 68 + (regnum - SPARC_O0_REGNUM) * 4)
1116 1.1 christos {
1117 1.1 christos start_pc += 4;
1118 1.1 christos continue;
1119 1.1 christos }
1120 1.1 christos
1121 1.1 christos /* Case of arguments copied into %i[0..5]. */
1122 1.1 christos if (regnum >= SPARC_I0_REGNUM && regnum <= SPARC_I5_REGNUM
1123 1.1 christos && (cache.copied_regs_mask & (1 << (regnum - SPARC_I0_REGNUM)))
1124 1.1 christos && X_SIMM13 (insn) == 68 + (regnum - SPARC_I0_REGNUM) * 4)
1125 1.1 christos {
1126 1.1 christos start_pc += 4;
1127 1.1 christos continue;
1128 1.1 christos }
1129 1.1 christos }
1130 1.1 christos
1131 1.1 christos break;
1132 1.1 christos }
1133 1.1 christos
1134 1.1 christos return start_pc;
1135 1.1 christos }
1136 1.1 christos
1137 1.1 christos /* Normal frames. */
1138 1.6 christos
1139 1.1 christos struct sparc_frame_cache *
1140 1.1 christos sparc_frame_cache (struct frame_info *this_frame, void **this_cache)
1141 1.1 christos {
1142 1.1 christos struct sparc_frame_cache *cache;
1143 1.1 christos
1144 1.1 christos if (*this_cache)
1145 1.1 christos return (struct sparc_frame_cache *) *this_cache;
1146 1.1 christos
1147 1.1 christos cache = sparc_alloc_frame_cache ();
1148 1.1 christos *this_cache = cache;
1149 1.1 christos
1150 1.1 christos cache->pc = get_frame_func (this_frame);
1151 1.1 christos if (cache->pc != 0)
1152 1.1 christos sparc_analyze_prologue (get_frame_arch (this_frame), cache->pc,
1153 1.1 christos get_frame_pc (this_frame), cache);
1154 1.1 christos
1155 1.1 christos if (cache->frameless_p)
1156 1.1 christos {
1157 1.1 christos /* This function is frameless, so %fp (%i6) holds the frame
1158 1.1 christos pointer for our calling frame. Use %sp (%o6) as this frame's
1159 1.1 christos base address. */
1160 1.1 christos cache->base =
1161 1.1 christos get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
1162 1.1 christos }
1163 1.1 christos else
1164 1.1 christos {
1165 1.1 christos /* For normal frames, %fp (%i6) holds the frame pointer, the
1166 1.1 christos base address for the current stack frame. */
1167 1.1 christos cache->base =
1168 1.1 christos get_frame_register_unsigned (this_frame, SPARC_FP_REGNUM);
1169 1.1 christos }
1170 1.1 christos
1171 1.1 christos cache->base += cache->frame_offset;
1172 1.1 christos
1173 1.1 christos if (cache->base & 1)
1174 1.1 christos cache->base += BIAS;
1175 1.1 christos
1176 1.1 christos return cache;
1177 1.1 christos }
1178 1.1 christos
1179 1.1 christos static int
1180 1.1 christos sparc32_struct_return_from_sym (struct symbol *sym)
1181 1.1 christos {
1182 1.1 christos struct type *type = check_typedef (SYMBOL_TYPE (sym));
1183 1.1 christos enum type_code code = TYPE_CODE (type);
1184 1.1 christos
1185 1.1 christos if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
1186 1.1 christos {
1187 1.1 christos type = check_typedef (TYPE_TARGET_TYPE (type));
1188 1.1 christos if (sparc_structure_or_union_p (type)
1189 1.1 christos || (sparc_floating_p (type) && TYPE_LENGTH (type) == 16))
1190 1.1 christos return 1;
1191 1.1 christos }
1192 1.1 christos
1193 1.1 christos return 0;
1194 1.1 christos }
1195 1.1 christos
1196 1.6 christos struct sparc_frame_cache *
1197 1.1 christos sparc32_frame_cache (struct frame_info *this_frame, void **this_cache)
1198 1.1 christos {
1199 1.1 christos struct sparc_frame_cache *cache;
1200 1.1 christos struct symbol *sym;
1201 1.1 christos
1202 1.1 christos if (*this_cache)
1203 1.1 christos return (struct sparc_frame_cache *) *this_cache;
1204 1.1 christos
1205 1.1 christos cache = sparc_frame_cache (this_frame, this_cache);
1206 1.1 christos
1207 1.1 christos sym = find_pc_function (cache->pc);
1208 1.1 christos if (sym)
1209 1.1 christos {
1210 1.1 christos cache->struct_return_p = sparc32_struct_return_from_sym (sym);
1211 1.1 christos }
1212 1.1 christos else
1213 1.1 christos {
1214 1.1 christos /* There is no debugging information for this function to
1215 1.1 christos help us determine whether this function returns a struct
1216 1.1 christos or not. So we rely on another heuristic which is to check
1217 1.1 christos the instruction at the return address and see if this is
1218 1.1 christos an "unimp" instruction. If it is, then it is a struct-return
1219 1.1 christos function. */
1220 1.1 christos CORE_ADDR pc;
1221 1.1 christos int regnum =
1222 1.1 christos (cache->copied_regs_mask & 0x80) ? SPARC_I7_REGNUM : SPARC_O7_REGNUM;
1223 1.1 christos
1224 1.1 christos pc = get_frame_register_unsigned (this_frame, regnum) + 8;
1225 1.1 christos if (sparc_is_unimp_insn (pc))
1226 1.1 christos cache->struct_return_p = 1;
1227 1.1 christos }
1228 1.1 christos
1229 1.1 christos return cache;
1230 1.1 christos }
1231 1.1 christos
1232 1.1 christos static void
1233 1.1 christos sparc32_frame_this_id (struct frame_info *this_frame, void **this_cache,
1234 1.1 christos struct frame_id *this_id)
1235 1.1 christos {
1236 1.1 christos struct sparc_frame_cache *cache =
1237 1.1 christos sparc32_frame_cache (this_frame, this_cache);
1238 1.1 christos
1239 1.1 christos /* This marks the outermost frame. */
1240 1.1 christos if (cache->base == 0)
1241 1.1 christos return;
1242 1.1 christos
1243 1.1 christos (*this_id) = frame_id_build (cache->base, cache->pc);
1244 1.1 christos }
1245 1.1 christos
1246 1.1 christos static struct value *
1247 1.1 christos sparc32_frame_prev_register (struct frame_info *this_frame,
1248 1.1 christos void **this_cache, int regnum)
1249 1.1 christos {
1250 1.1 christos struct gdbarch *gdbarch = get_frame_arch (this_frame);
1251 1.1 christos struct sparc_frame_cache *cache =
1252 1.1 christos sparc32_frame_cache (this_frame, this_cache);
1253 1.1 christos
1254 1.1 christos if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
1255 1.1 christos {
1256 1.1 christos CORE_ADDR pc = (regnum == SPARC32_NPC_REGNUM) ? 4 : 0;
1257 1.1 christos
1258 1.1 christos /* If this functions has a Structure, Union or Quad-Precision
1259 1.1 christos return value, we have to skip the UNIMP instruction that encodes
1260 1.1 christos the size of the structure. */
1261 1.1 christos if (cache->struct_return_p)
1262 1.1 christos pc += 4;
1263 1.1 christos
1264 1.1 christos regnum =
1265 1.1 christos (cache->copied_regs_mask & 0x80) ? SPARC_I7_REGNUM : SPARC_O7_REGNUM;
1266 1.1 christos pc += get_frame_register_unsigned (this_frame, regnum) + 8;
1267 1.1 christos return frame_unwind_got_constant (this_frame, regnum, pc);
1268 1.1 christos }
1269 1.1 christos
1270 1.1 christos /* Handle StackGhost. */
1271 1.1 christos {
1272 1.1 christos ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
1273 1.1 christos
1274 1.1 christos if (wcookie != 0 && !cache->frameless_p && regnum == SPARC_I7_REGNUM)
1275 1.1 christos {
1276 1.1 christos CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 4;
1277 1.1 christos ULONGEST i7;
1278 1.1 christos
1279 1.1 christos /* Read the value in from memory. */
1280 1.1 christos i7 = get_frame_memory_unsigned (this_frame, addr, 4);
1281 1.1 christos return frame_unwind_got_constant (this_frame, regnum, i7 ^ wcookie);
1282 1.1 christos }
1283 1.1 christos }
1284 1.1 christos
1285 1.1 christos /* The previous frame's `local' and `in' registers may have been saved
1286 1.1 christos in the register save area. */
1287 1.1 christos if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM
1288 1.1 christos && (cache->saved_regs_mask & (1 << (regnum - SPARC_L0_REGNUM))))
1289 1.1 christos {
1290 1.1 christos CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 4;
1291 1.1 christos
1292 1.1 christos return frame_unwind_got_memory (this_frame, regnum, addr);
1293 1.1 christos }
1294 1.1 christos
1295 1.1 christos /* The previous frame's `out' registers may be accessible as the current
1296 1.1 christos frame's `in' registers. */
1297 1.1 christos if (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O7_REGNUM
1298 1.1 christos && (cache->copied_regs_mask & (1 << (regnum - SPARC_O0_REGNUM))))
1299 1.1 christos regnum += (SPARC_I0_REGNUM - SPARC_O0_REGNUM);
1300 1.1 christos
1301 1.1 christos return frame_unwind_got_register (this_frame, regnum, regnum);
1302 1.1 christos }
1303 1.1 christos
1304 1.1 christos static const struct frame_unwind sparc32_frame_unwind =
1305 1.1 christos {
1306 1.1 christos NORMAL_FRAME,
1307 1.1 christos default_frame_unwind_stop_reason,
1308 1.1 christos sparc32_frame_this_id,
1309 1.1 christos sparc32_frame_prev_register,
1310 1.1 christos NULL,
1311 1.1 christos default_frame_sniffer
1312 1.1 christos };
1313 1.1 christos
1314 1.1 christos
1316 1.1 christos static CORE_ADDR
1317 1.1 christos sparc32_frame_base_address (struct frame_info *this_frame, void **this_cache)
1318 1.1 christos {
1319 1.1 christos struct sparc_frame_cache *cache =
1320 1.1 christos sparc32_frame_cache (this_frame, this_cache);
1321 1.1 christos
1322 1.1 christos return cache->base;
1323 1.1 christos }
1324 1.1 christos
1325 1.1 christos static const struct frame_base sparc32_frame_base =
1326 1.1 christos {
1327 1.1 christos &sparc32_frame_unwind,
1328 1.1 christos sparc32_frame_base_address,
1329 1.1 christos sparc32_frame_base_address,
1330 1.1 christos sparc32_frame_base_address
1331 1.1 christos };
1332 1.1 christos
1333 1.1 christos static struct frame_id
1334 1.1 christos sparc_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
1335 1.1 christos {
1336 1.1 christos CORE_ADDR sp;
1337 1.1 christos
1338 1.1 christos sp = get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
1339 1.1 christos if (sp & 1)
1340 1.1 christos sp += BIAS;
1341 1.1 christos return frame_id_build (sp, get_frame_pc (this_frame));
1342 1.1 christos }
1343 1.1 christos
1344 1.1 christos
1346 1.1 christos /* Extract a function return value of TYPE from REGCACHE, and copy
1347 1.1 christos that into VALBUF. */
1348 1.1 christos
1349 1.1 christos static void
1350 1.1 christos sparc32_extract_return_value (struct type *type, struct regcache *regcache,
1351 1.1 christos gdb_byte *valbuf)
1352 1.1 christos {
1353 1.1 christos int len = TYPE_LENGTH (type);
1354 1.1 christos gdb_byte buf[32];
1355 1.1 christos
1356 1.1 christos gdb_assert (!sparc_structure_or_union_p (type));
1357 1.1 christos gdb_assert (!(sparc_floating_p (type) && len == 16));
1358 1.1 christos
1359 1.1 christos if (sparc_floating_p (type) || sparc_complex_floating_p (type))
1360 1.1 christos {
1361 1.1 christos /* Floating return values. */
1362 1.1 christos regcache_cooked_read (regcache, SPARC_F0_REGNUM, buf);
1363 1.1 christos if (len > 4)
1364 1.1 christos regcache_cooked_read (regcache, SPARC_F1_REGNUM, buf + 4);
1365 1.1 christos if (len > 8)
1366 1.1 christos {
1367 1.1 christos regcache_cooked_read (regcache, SPARC_F2_REGNUM, buf + 8);
1368 1.1 christos regcache_cooked_read (regcache, SPARC_F3_REGNUM, buf + 12);
1369 1.1 christos }
1370 1.1 christos if (len > 16)
1371 1.1 christos {
1372 1.1 christos regcache_cooked_read (regcache, SPARC_F4_REGNUM, buf + 16);
1373 1.1 christos regcache_cooked_read (regcache, SPARC_F5_REGNUM, buf + 20);
1374 1.1 christos regcache_cooked_read (regcache, SPARC_F6_REGNUM, buf + 24);
1375 1.1 christos regcache_cooked_read (regcache, SPARC_F7_REGNUM, buf + 28);
1376 1.1 christos }
1377 1.1 christos memcpy (valbuf, buf, len);
1378 1.1 christos }
1379 1.1 christos else
1380 1.1 christos {
1381 1.1 christos /* Integral and pointer return values. */
1382 1.1 christos gdb_assert (sparc_integral_or_pointer_p (type));
1383 1.1 christos
1384 1.1 christos regcache_cooked_read (regcache, SPARC_O0_REGNUM, buf);
1385 1.1 christos if (len > 4)
1386 1.1 christos {
1387 1.1 christos regcache_cooked_read (regcache, SPARC_O1_REGNUM, buf + 4);
1388 1.1 christos gdb_assert (len == 8);
1389 1.1 christos memcpy (valbuf, buf, 8);
1390 1.1 christos }
1391 1.1 christos else
1392 1.1 christos {
1393 1.1 christos /* Just stripping off any unused bytes should preserve the
1394 1.1 christos signed-ness just fine. */
1395 1.1 christos memcpy (valbuf, buf + 4 - len, len);
1396 1.1 christos }
1397 1.1 christos }
1398 1.1 christos }
1399 1.1 christos
1400 1.1 christos /* Store the function return value of type TYPE from VALBUF into
1401 1.1 christos REGCACHE. */
1402 1.1 christos
1403 1.1 christos static void
1404 1.1 christos sparc32_store_return_value (struct type *type, struct regcache *regcache,
1405 1.1 christos const gdb_byte *valbuf)
1406 1.1 christos {
1407 1.1 christos int len = TYPE_LENGTH (type);
1408 1.1 christos gdb_byte buf[8];
1409 1.1 christos
1410 1.1 christos gdb_assert (!sparc_structure_or_union_p (type));
1411 1.1 christos gdb_assert (!(sparc_floating_p (type) && len == 16));
1412 1.1 christos gdb_assert (len <= 8);
1413 1.1 christos
1414 1.1 christos if (sparc_floating_p (type) || sparc_complex_floating_p (type))
1415 1.1 christos {
1416 1.1 christos /* Floating return values. */
1417 1.1 christos memcpy (buf, valbuf, len);
1418 1.1 christos regcache_cooked_write (regcache, SPARC_F0_REGNUM, buf);
1419 1.1 christos if (len > 4)
1420 1.1 christos regcache_cooked_write (regcache, SPARC_F1_REGNUM, buf + 4);
1421 1.1 christos if (len > 8)
1422 1.1 christos {
1423 1.1 christos regcache_cooked_write (regcache, SPARC_F2_REGNUM, buf + 8);
1424 1.1 christos regcache_cooked_write (regcache, SPARC_F3_REGNUM, buf + 12);
1425 1.1 christos }
1426 1.1 christos if (len > 16)
1427 1.1 christos {
1428 1.1 christos regcache_cooked_write (regcache, SPARC_F4_REGNUM, buf + 16);
1429 1.1 christos regcache_cooked_write (regcache, SPARC_F5_REGNUM, buf + 20);
1430 1.1 christos regcache_cooked_write (regcache, SPARC_F6_REGNUM, buf + 24);
1431 1.1 christos regcache_cooked_write (regcache, SPARC_F7_REGNUM, buf + 28);
1432 1.1 christos }
1433 1.1 christos }
1434 1.1 christos else
1435 1.1 christos {
1436 1.1 christos /* Integral and pointer return values. */
1437 1.1 christos gdb_assert (sparc_integral_or_pointer_p (type));
1438 1.1 christos
1439 1.1 christos if (len > 4)
1440 1.1 christos {
1441 1.1 christos gdb_assert (len == 8);
1442 1.1 christos memcpy (buf, valbuf, 8);
1443 1.1 christos regcache_cooked_write (regcache, SPARC_O1_REGNUM, buf + 4);
1444 1.1 christos }
1445 1.1 christos else
1446 1.1 christos {
1447 1.1 christos /* ??? Do we need to do any sign-extension here? */
1448 1.1 christos memcpy (buf + 4 - len, valbuf, len);
1449 1.1 christos }
1450 1.1 christos regcache_cooked_write (regcache, SPARC_O0_REGNUM, buf);
1451 1.1 christos }
1452 1.1 christos }
1453 1.1 christos
1454 1.1 christos static enum return_value_convention
1455 1.1 christos sparc32_return_value (struct gdbarch *gdbarch, struct value *function,
1456 1.1 christos struct type *type, struct regcache *regcache,
1457 1.1 christos gdb_byte *readbuf, const gdb_byte *writebuf)
1458 1.1 christos {
1459 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1460 1.1 christos
1461 1.1 christos /* The psABI says that "...every stack frame reserves the word at
1462 1.1 christos %fp+64. If a function returns a structure, union, or
1463 1.1 christos quad-precision value, this word should hold the address of the
1464 1.1 christos object into which the return value should be copied." This
1465 1.1 christos guarantees that we can always find the return value, not just
1466 1.1 christos before the function returns. */
1467 1.1 christos
1468 1.1 christos if (sparc_structure_or_union_p (type)
1469 1.1 christos || (sparc_floating_p (type) && TYPE_LENGTH (type) == 16))
1470 1.1 christos {
1471 1.1 christos ULONGEST sp;
1472 1.1 christos CORE_ADDR addr;
1473 1.1 christos
1474 1.1 christos if (readbuf)
1475 1.1 christos {
1476 1.1 christos regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
1477 1.1 christos addr = read_memory_unsigned_integer (sp + 64, 4, byte_order);
1478 1.1 christos read_memory (addr, readbuf, TYPE_LENGTH (type));
1479 1.1 christos }
1480 1.1 christos if (writebuf)
1481 1.1 christos {
1482 1.1 christos regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
1483 1.1 christos addr = read_memory_unsigned_integer (sp + 64, 4, byte_order);
1484 1.1 christos write_memory (addr, writebuf, TYPE_LENGTH (type));
1485 1.1 christos }
1486 1.1 christos
1487 1.1 christos return RETURN_VALUE_ABI_PRESERVES_ADDRESS;
1488 1.1 christos }
1489 1.1 christos
1490 1.1 christos if (readbuf)
1491 1.1 christos sparc32_extract_return_value (type, regcache, readbuf);
1492 1.1 christos if (writebuf)
1493 1.1 christos sparc32_store_return_value (type, regcache, writebuf);
1494 1.1 christos
1495 1.1 christos return RETURN_VALUE_REGISTER_CONVENTION;
1496 1.1 christos }
1497 1.1 christos
1498 1.1 christos static int
1499 1.1 christos sparc32_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
1500 1.1 christos {
1501 1.1 christos return (sparc_structure_or_union_p (type)
1502 1.1 christos || (sparc_floating_p (type) && TYPE_LENGTH (type) == 16)
1503 1.1 christos || sparc_complex_floating_p (type));
1504 1.1 christos }
1505 1.1 christos
1506 1.1 christos static int
1507 1.1 christos sparc32_dwarf2_struct_return_p (struct frame_info *this_frame)
1508 1.1 christos {
1509 1.1 christos CORE_ADDR pc = get_frame_address_in_block (this_frame);
1510 1.1 christos struct symbol *sym = find_pc_function (pc);
1511 1.1 christos
1512 1.1 christos if (sym)
1513 1.1 christos return sparc32_struct_return_from_sym (sym);
1514 1.1 christos return 0;
1515 1.1 christos }
1516 1.1 christos
1517 1.1 christos static void
1518 1.1 christos sparc32_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
1519 1.1 christos struct dwarf2_frame_state_reg *reg,
1520 1.1 christos struct frame_info *this_frame)
1521 1.1 christos {
1522 1.1 christos int off;
1523 1.1 christos
1524 1.1 christos switch (regnum)
1525 1.1 christos {
1526 1.1 christos case SPARC_G0_REGNUM:
1527 1.1 christos /* Since %g0 is always zero, there is no point in saving it, and
1528 1.1 christos people will be inclined omit it from the CFI. Make sure we
1529 1.1 christos don't warn about that. */
1530 1.1 christos reg->how = DWARF2_FRAME_REG_SAME_VALUE;
1531 1.1 christos break;
1532 1.1 christos case SPARC_SP_REGNUM:
1533 1.1 christos reg->how = DWARF2_FRAME_REG_CFA;
1534 1.1 christos break;
1535 1.1 christos case SPARC32_PC_REGNUM:
1536 1.1 christos case SPARC32_NPC_REGNUM:
1537 1.1 christos reg->how = DWARF2_FRAME_REG_RA_OFFSET;
1538 1.1 christos off = 8;
1539 1.1 christos if (sparc32_dwarf2_struct_return_p (this_frame))
1540 1.1 christos off += 4;
1541 1.1 christos if (regnum == SPARC32_NPC_REGNUM)
1542 1.1 christos off += 4;
1543 1.1 christos reg->loc.offset = off;
1544 1.1 christos break;
1545 1.7 christos }
1546 1.1 christos }
1547 1.1 christos
1548 1.1 christos
1549 1.1 christos /* The SPARC Architecture doesn't have hardware single-step support,
1551 1.1 christos and most operating systems don't implement it either, so we provide
1552 1.1 christos software single-step mechanism. */
1553 1.1 christos
1554 1.1 christos static CORE_ADDR
1555 1.1 christos sparc_analyze_control_transfer (struct regcache *regcache,
1556 1.1 christos CORE_ADDR pc, CORE_ADDR *npc)
1557 1.1 christos {
1558 1.1 christos unsigned long insn = sparc_fetch_instruction (pc);
1559 1.1 christos int conditional_p = X_COND (insn) & 0x7;
1560 1.1 christos int branch_p = 0, fused_p = 0;
1561 1.1 christos long offset = 0; /* Must be signed for sign-extend. */
1562 1.1 christos
1563 1.1 christos if (X_OP (insn) == 0 && X_OP2 (insn) == 3)
1564 1.1 christos {
1565 1.1 christos if ((insn & 0x10000000) == 0)
1566 1.1 christos {
1567 1.1 christos /* Branch on Integer Register with Prediction (BPr). */
1568 1.1 christos branch_p = 1;
1569 1.1 christos conditional_p = 1;
1570 1.1 christos }
1571 1.1 christos else
1572 1.1 christos {
1573 1.1 christos /* Compare and Branch */
1574 1.1 christos branch_p = 1;
1575 1.1 christos fused_p = 1;
1576 1.1 christos offset = 4 * X_DISP10 (insn);
1577 1.1 christos }
1578 1.1 christos }
1579 1.1 christos else if (X_OP (insn) == 0 && X_OP2 (insn) == 6)
1580 1.1 christos {
1581 1.1 christos /* Branch on Floating-Point Condition Codes (FBfcc). */
1582 1.1 christos branch_p = 1;
1583 1.1 christos offset = 4 * X_DISP22 (insn);
1584 1.1 christos }
1585 1.1 christos else if (X_OP (insn) == 0 && X_OP2 (insn) == 5)
1586 1.1 christos {
1587 1.1 christos /* Branch on Floating-Point Condition Codes with Prediction
1588 1.1 christos (FBPfcc). */
1589 1.1 christos branch_p = 1;
1590 1.1 christos offset = 4 * X_DISP19 (insn);
1591 1.1 christos }
1592 1.1 christos else if (X_OP (insn) == 0 && X_OP2 (insn) == 2)
1593 1.1 christos {
1594 1.1 christos /* Branch on Integer Condition Codes (Bicc). */
1595 1.1 christos branch_p = 1;
1596 1.7 christos offset = 4 * X_DISP22 (insn);
1597 1.7 christos }
1598 1.1 christos else if (X_OP (insn) == 0 && X_OP2 (insn) == 1)
1599 1.7 christos {
1600 1.7 christos /* Branch on Integer Condition Codes with Prediction (BPcc). */
1601 1.1 christos branch_p = 1;
1602 1.1 christos offset = 4 * X_DISP19 (insn);
1603 1.1 christos }
1604 1.1 christos else if (X_OP (insn) == 2 && X_OP3 (insn) == 0x3a)
1605 1.1 christos {
1606 1.1 christos struct frame_info *frame = get_current_frame ();
1607 1.1 christos
1608 1.1 christos /* Trap instruction (TRAP). */
1609 1.1 christos return gdbarch_tdep (get_regcache_arch (regcache))->step_trap (frame,
1610 1.1 christos insn);
1611 1.1 christos }
1612 1.1 christos
1613 1.1 christos /* FIXME: Handle DONE and RETRY instructions. */
1614 1.1 christos
1615 1.1 christos if (branch_p)
1616 1.1 christos {
1617 1.1 christos if (fused_p)
1618 1.1 christos {
1619 1.1 christos /* Fused compare-and-branch instructions are non-delayed,
1620 1.1 christos and do not have an annuling capability. So we need to
1621 1.1 christos always set a breakpoint on both the NPC and the branch
1622 1.1 christos target address. */
1623 1.1 christos gdb_assert (offset != 0);
1624 1.1 christos return pc + offset;
1625 1.1 christos }
1626 1.1 christos else if (conditional_p)
1627 1.1 christos {
1628 1.1 christos /* For conditional branches, return nPC + 4 iff the annul
1629 1.1 christos bit is 1. */
1630 1.1 christos return (X_A (insn) ? *npc + 4 : 0);
1631 1.1 christos }
1632 1.1 christos else
1633 1.1 christos {
1634 1.1 christos /* For unconditional branches, return the target if its
1635 1.1 christos specified condition is "always" and return nPC + 4 if the
1636 1.1 christos condition is "never". If the annul bit is 1, set *NPC to
1637 1.1 christos zero. */
1638 1.1 christos if (X_COND (insn) == 0x0)
1639 1.1 christos pc = *npc, offset = 4;
1640 1.1 christos if (X_A (insn))
1641 1.1 christos *npc = 0;
1642 1.1 christos
1643 1.1 christos return pc + offset;
1644 1.1 christos }
1645 1.1 christos }
1646 1.7 christos
1647 1.7 christos return 0;
1648 1.1 christos }
1649 1.7 christos
1650 1.1 christos static CORE_ADDR
1651 1.1 christos sparc_step_trap (struct frame_info *frame, unsigned long insn)
1652 1.1 christos {
1653 1.1 christos return 0;
1654 1.7 christos }
1655 1.1 christos
1656 1.7 christos static VEC (CORE_ADDR) *
1657 1.7 christos sparc_software_single_step (struct regcache *regcache)
1658 1.1 christos {
1659 1.1 christos struct gdbarch *arch = get_regcache_arch (regcache);
1660 1.7 christos struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
1661 1.1 christos CORE_ADDR npc, nnpc;
1662 1.7 christos
1663 1.1 christos CORE_ADDR pc, orig_npc;
1664 1.1 christos VEC (CORE_ADDR) *next_pcs = NULL;
1665 1.7 christos
1666 1.1 christos pc = regcache_raw_get_unsigned (regcache, tdep->pc_regnum);
1667 1.1 christos orig_npc = npc = regcache_raw_get_unsigned (regcache, tdep->npc_regnum);
1668 1.1 christos
1669 1.1 christos /* Analyze the instruction at PC. */
1670 1.1 christos nnpc = sparc_analyze_control_transfer (regcache, pc, &npc);
1671 1.1 christos if (npc != 0)
1672 1.1 christos VEC_safe_push (CORE_ADDR, next_pcs, npc);
1673 1.7 christos
1674 1.1 christos if (nnpc != 0)
1675 1.1 christos VEC_safe_push (CORE_ADDR, next_pcs, nnpc);
1676 1.1 christos
1677 1.1 christos /* Assert that we have set at least one breakpoint, and that
1678 1.1 christos they're not set at the same spot - unless we're going
1679 1.1 christos from here straight to NULL, i.e. a call or jump to 0. */
1680 1.1 christos gdb_assert (npc != 0 || nnpc != 0 || orig_npc == 0);
1681 1.1 christos gdb_assert (nnpc != npc || orig_npc == 0);
1682 1.1 christos
1683 1.1 christos return next_pcs;
1684 1.1 christos }
1685 1.1 christos
1686 1.3 christos static void
1687 1.1 christos sparc_write_pc (struct regcache *regcache, CORE_ADDR pc)
1688 1.3 christos {
1689 1.3 christos struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
1690 1.3 christos
1691 1.3 christos regcache_cooked_write_unsigned (regcache, tdep->pc_regnum, pc);
1692 1.3 christos regcache_cooked_write_unsigned (regcache, tdep->npc_regnum, pc + 4);
1693 1.1 christos }
1694 1.1 christos
1695 1.1 christos
1697 1.3 christos /* Iterate over core file register note sections. */
1698 1.1 christos
1699 1.1 christos static void
1700 1.1 christos sparc_iterate_over_regset_sections (struct gdbarch *gdbarch,
1701 1.7 christos iterate_over_regset_sections_cb *cb,
1702 1.7 christos void *cb_data,
1703 1.7 christos const struct regcache *regcache)
1704 1.7 christos {
1705 1.7 christos struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1706 1.7 christos
1707 1.7 christos cb (".reg", tdep->sizeof_gregset, tdep->gregset, NULL, cb_data);
1708 1.7 christos cb (".reg2", tdep->sizeof_fpregset, tdep->fpregset, NULL, cb_data);
1709 1.7 christos }
1710 1.7 christos
1711 1.7 christos
1713 1.7 christos static int
1714 1.7 christos validate_tdesc_registers (const struct target_desc *tdesc,
1715 1.7 christos struct tdesc_arch_data *tdesc_data,
1716 1.7 christos const char *feature_name,
1717 1.7 christos const char *register_names[],
1718 1.7 christos unsigned int registers_num,
1719 1.7 christos unsigned int reg_start)
1720 1.7 christos {
1721 1.7 christos int valid_p = 1;
1722 1.7 christos const struct tdesc_feature *feature;
1723 1.7 christos
1724 1.1 christos feature = tdesc_find_feature (tdesc, feature_name);
1725 1.1 christos if (feature == NULL)
1726 1.1 christos return 0;
1727 1.1 christos
1728 1.7 christos for (unsigned int i = 0; i < registers_num; i++)
1729 1.1 christos valid_p &= tdesc_numbered_register (feature, tdesc_data,
1730 1.7 christos reg_start + i,
1731 1.1 christos register_names[i]);
1732 1.1 christos
1733 1.1 christos return valid_p;
1734 1.1 christos }
1735 1.1 christos
1736 1.1 christos static struct gdbarch *
1737 1.1 christos sparc32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1738 1.3 christos {
1739 1.1 christos struct gdbarch_tdep *tdep;
1740 1.1 christos const struct target_desc *tdesc = info.target_desc;
1741 1.1 christos struct gdbarch *gdbarch;
1742 1.1 christos int valid_p = 1;
1743 1.1 christos
1744 1.7 christos /* If there is already a candidate, use it. */
1745 1.7 christos arches = gdbarch_list_lookup_by_info (arches, &info);
1746 1.7 christos if (arches != NULL)
1747 1.7 christos return arches->gdbarch;
1748 1.1 christos
1749 1.1 christos /* Allocate space for the new architecture. */
1750 1.1 christos tdep = XCNEW (struct gdbarch_tdep);
1751 1.1 christos gdbarch = gdbarch_alloc (&info, tdep);
1752 1.7 christos
1753 1.7 christos tdep->pc_regnum = SPARC32_PC_REGNUM;
1754 1.7 christos tdep->npc_regnum = SPARC32_NPC_REGNUM;
1755 1.1 christos tdep->step_trap = sparc_step_trap;
1756 1.1 christos tdep->fpu_register_names = sparc32_fpu_register_names;
1757 1.1 christos tdep->fpu_registers_num = ARRAY_SIZE (sparc32_fpu_register_names);
1758 1.1 christos tdep->cp0_register_names = sparc32_cp0_register_names;
1759 1.7 christos tdep->cp0_registers_num = ARRAY_SIZE (sparc32_cp0_register_names);
1760 1.7 christos
1761 1.1 christos set_gdbarch_long_double_bit (gdbarch, 128);
1762 1.1 christos set_gdbarch_long_double_format (gdbarch, floatformats_sparc_quad);
1763 1.1 christos
1764 1.1 christos set_gdbarch_wchar_bit (gdbarch, 16);
1765 1.1 christos set_gdbarch_wchar_signed (gdbarch, 1);
1766 1.1 christos
1767 1.1 christos set_gdbarch_num_regs (gdbarch, SPARC32_NUM_REGS);
1768 1.1 christos set_gdbarch_register_name (gdbarch, sparc32_register_name);
1769 1.1 christos set_gdbarch_register_type (gdbarch, sparc32_register_type);
1770 1.1 christos set_gdbarch_num_pseudo_regs (gdbarch, SPARC32_NUM_PSEUDO_REGS);
1771 1.1 christos set_tdesc_pseudo_register_name (gdbarch, sparc32_pseudo_register_name);
1772 1.1 christos set_tdesc_pseudo_register_type (gdbarch, sparc32_pseudo_register_type);
1773 1.1 christos set_gdbarch_pseudo_register_read (gdbarch, sparc32_pseudo_register_read);
1774 1.1 christos set_gdbarch_pseudo_register_write (gdbarch, sparc32_pseudo_register_write);
1775 1.1 christos
1776 1.1 christos /* Register numbers of various important registers. */
1777 1.1 christos set_gdbarch_sp_regnum (gdbarch, SPARC_SP_REGNUM); /* %sp */
1778 1.1 christos set_gdbarch_pc_regnum (gdbarch, SPARC32_PC_REGNUM); /* %pc */
1779 1.1 christos set_gdbarch_fp0_regnum (gdbarch, SPARC_F0_REGNUM); /* %f0 */
1780 1.1 christos
1781 1.1 christos /* Call dummy code. */
1782 1.1 christos set_gdbarch_frame_align (gdbarch, sparc32_frame_align);
1783 1.1 christos set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
1784 1.7 christos set_gdbarch_push_dummy_code (gdbarch, sparc32_push_dummy_code);
1785 1.7 christos set_gdbarch_push_dummy_call (gdbarch, sparc32_push_dummy_call);
1786 1.7 christos
1787 1.7 christos set_gdbarch_return_value (gdbarch, sparc32_return_value);
1788 1.1 christos set_gdbarch_stabs_argument_has_addr
1789 1.1 christos (gdbarch, sparc32_stabs_argument_has_addr);
1790 1.1 christos
1791 1.1 christos set_gdbarch_skip_prologue (gdbarch, sparc32_skip_prologue);
1792 1.1 christos
1793 1.1 christos /* Stack grows downward. */
1794 1.1 christos set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1795 1.1 christos
1796 1.1 christos set_gdbarch_breakpoint_kind_from_pc (gdbarch,
1797 1.1 christos sparc_breakpoint::kind_from_pc);
1798 1.1 christos set_gdbarch_sw_breakpoint_from_kind (gdbarch,
1799 1.1 christos sparc_breakpoint::bp_from_kind);
1800 1.1 christos
1801 1.1 christos set_gdbarch_frame_args_skip (gdbarch, 8);
1802 1.1 christos
1803 1.1 christos set_gdbarch_print_insn (gdbarch, print_insn_sparc);
1804 1.1 christos
1805 1.1 christos set_gdbarch_software_single_step (gdbarch, sparc_software_single_step);
1806 1.1 christos set_gdbarch_write_pc (gdbarch, sparc_write_pc);
1807 1.1 christos
1808 1.1 christos set_gdbarch_dummy_id (gdbarch, sparc_dummy_id);
1809 1.1 christos
1810 1.1 christos set_gdbarch_unwind_pc (gdbarch, sparc_unwind_pc);
1811 1.1 christos
1812 1.7 christos frame_base_set_default (gdbarch, &sparc32_frame_base);
1813 1.7 christos
1814 1.7 christos /* Hook in the DWARF CFI frame unwinder. */
1815 1.7 christos dwarf2_frame_set_init_reg (gdbarch, sparc32_dwarf2_frame_init_reg);
1816 1.7 christos /* FIXME: kettenis/20050423: Don't enable the unwinder until the
1817 1.7 christos StackGhost issues have been resolved. */
1818 1.7 christos
1819 1.7 christos /* Hook in ABI-specific overrides, if they have been registered. */
1820 1.7 christos gdbarch_init_osabi (info, gdbarch);
1821 1.7 christos
1822 1.7 christos frame_unwind_append_unwinder (gdbarch, &sparc32_frame_unwind);
1823 1.7 christos
1824 1.7 christos if (tdesc_has_registers (tdesc))
1825 1.7 christos {
1826 1.7 christos struct tdesc_arch_data *tdesc_data = tdesc_data_alloc ();
1827 1.7 christos
1828 1.7 christos /* Validate that the descriptor provides the mandatory registers
1829 1.7 christos and allocate their numbers. */
1830 1.7 christos valid_p &= validate_tdesc_registers (tdesc, tdesc_data,
1831 1.7 christos "org.gnu.gdb.sparc.cpu",
1832 1.7 christos sparc_core_register_names,
1833 1.7 christos ARRAY_SIZE (sparc_core_register_names),
1834 1.7 christos SPARC_G0_REGNUM);
1835 1.7 christos valid_p &= validate_tdesc_registers (tdesc, tdesc_data,
1836 1.7 christos "org.gnu.gdb.sparc.fpu",
1837 1.7 christos tdep->fpu_register_names,
1838 1.7 christos tdep->fpu_registers_num,
1839 1.7 christos SPARC_F0_REGNUM);
1840 1.7 christos valid_p &= validate_tdesc_registers (tdesc, tdesc_data,
1841 1.7 christos "org.gnu.gdb.sparc.cp0",
1842 1.7 christos tdep->cp0_register_names,
1843 1.7 christos tdep->cp0_registers_num,
1844 1.7 christos SPARC_F0_REGNUM
1845 1.1 christos + tdep->fpu_registers_num);
1846 1.1 christos if (!valid_p)
1847 1.3 christos {
1848 1.3 christos tdesc_data_cleanup (tdesc_data);
1849 1.1 christos return NULL;
1850 1.1 christos }
1851 1.1 christos
1852 1.1 christos /* Target description may have changed. */
1853 1.1 christos info.tdep_info = tdesc_data;
1854 1.1 christos tdesc_use_registers (gdbarch, tdesc, tdesc_data);
1855 1.1 christos }
1856 1.1 christos
1857 1.1 christos /* If we have register sets, enable the generic core file support. */
1858 1.1 christos if (tdep->gregset)
1859 1.1 christos set_gdbarch_iterate_over_regset_sections
1860 1.1 christos (gdbarch, sparc_iterate_over_regset_sections);
1861 1.1 christos
1862 1.1 christos register_sparc_ravenscar_ops (gdbarch);
1863 1.1 christos
1864 1.1 christos return gdbarch;
1865 1.1 christos }
1866 1.1 christos
1867 1.1 christos /* Helper functions for dealing with register windows. */
1869 1.1 christos
1870 1.1 christos void
1871 1.1 christos sparc_supply_rwindow (struct regcache *regcache, CORE_ADDR sp, int regnum)
1872 1.1 christos {
1873 1.1 christos struct gdbarch *gdbarch = get_regcache_arch (regcache);
1874 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1875 1.1 christos int offset = 0;
1876 1.1 christos gdb_byte buf[8];
1877 1.1 christos int i;
1878 1.1 christos
1879 1.1 christos if (sp & 1)
1880 1.1 christos {
1881 1.1 christos /* Registers are 64-bit. */
1882 1.1 christos sp += BIAS;
1883 1.1 christos
1884 1.1 christos for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1885 1.1 christos {
1886 1.1 christos if (regnum == i || regnum == -1)
1887 1.1 christos {
1888 1.1 christos target_read_memory (sp + ((i - SPARC_L0_REGNUM) * 8), buf, 8);
1889 1.1 christos
1890 1.1 christos /* Handle StackGhost. */
1891 1.1 christos if (i == SPARC_I7_REGNUM)
1892 1.1 christos {
1893 1.1 christos ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
1894 1.1 christos ULONGEST i7;
1895 1.1 christos
1896 1.1 christos i7 = extract_unsigned_integer (buf + offset, 8, byte_order);
1897 1.1 christos store_unsigned_integer (buf + offset, 8, byte_order,
1898 1.1 christos i7 ^ wcookie);
1899 1.1 christos }
1900 1.1 christos
1901 1.1 christos regcache_raw_supply (regcache, i, buf);
1902 1.1 christos }
1903 1.1 christos }
1904 1.1 christos }
1905 1.1 christos else
1906 1.1 christos {
1907 1.1 christos /* Registers are 32-bit. Toss any sign-extension of the stack
1908 1.1 christos pointer. */
1909 1.1 christos sp &= 0xffffffffUL;
1910 1.1 christos
1911 1.1 christos /* Clear out the top half of the temporary buffer, and put the
1912 1.1 christos register value in the bottom half if we're in 64-bit mode. */
1913 1.1 christos if (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 64)
1914 1.1 christos {
1915 1.1 christos memset (buf, 0, 4);
1916 1.1 christos offset = 4;
1917 1.1 christos }
1918 1.1 christos
1919 1.1 christos for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1920 1.1 christos {
1921 1.1 christos if (regnum == i || regnum == -1)
1922 1.1 christos {
1923 1.1 christos target_read_memory (sp + ((i - SPARC_L0_REGNUM) * 4),
1924 1.1 christos buf + offset, 4);
1925 1.1 christos
1926 1.1 christos /* Handle StackGhost. */
1927 1.1 christos if (i == SPARC_I7_REGNUM)
1928 1.1 christos {
1929 1.1 christos ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
1930 1.1 christos ULONGEST i7;
1931 1.1 christos
1932 1.1 christos i7 = extract_unsigned_integer (buf + offset, 4, byte_order);
1933 1.1 christos store_unsigned_integer (buf + offset, 4, byte_order,
1934 1.1 christos i7 ^ wcookie);
1935 1.1 christos }
1936 1.1 christos
1937 1.1 christos regcache_raw_supply (regcache, i, buf);
1938 1.1 christos }
1939 1.1 christos }
1940 1.1 christos }
1941 1.1 christos }
1942 1.1 christos
1943 1.1 christos void
1944 1.1 christos sparc_collect_rwindow (const struct regcache *regcache,
1945 1.1 christos CORE_ADDR sp, int regnum)
1946 1.1 christos {
1947 1.1 christos struct gdbarch *gdbarch = get_regcache_arch (regcache);
1948 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1949 1.1 christos int offset = 0;
1950 1.1 christos gdb_byte buf[8];
1951 1.1 christos int i;
1952 1.1 christos
1953 1.1 christos if (sp & 1)
1954 1.1 christos {
1955 1.1 christos /* Registers are 64-bit. */
1956 1.1 christos sp += BIAS;
1957 1.1 christos
1958 1.1 christos for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1959 1.1 christos {
1960 1.1 christos if (regnum == -1 || regnum == SPARC_SP_REGNUM || regnum == i)
1961 1.1 christos {
1962 1.1 christos regcache_raw_collect (regcache, i, buf);
1963 1.1 christos
1964 1.1 christos /* Handle StackGhost. */
1965 1.1 christos if (i == SPARC_I7_REGNUM)
1966 1.1 christos {
1967 1.1 christos ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
1968 1.1 christos ULONGEST i7;
1969 1.1 christos
1970 1.1 christos i7 = extract_unsigned_integer (buf + offset, 8, byte_order);
1971 1.1 christos store_unsigned_integer (buf, 8, byte_order, i7 ^ wcookie);
1972 1.1 christos }
1973 1.1 christos
1974 1.1 christos target_write_memory (sp + ((i - SPARC_L0_REGNUM) * 8), buf, 8);
1975 1.1 christos }
1976 1.1 christos }
1977 1.1 christos }
1978 1.1 christos else
1979 1.1 christos {
1980 1.1 christos /* Registers are 32-bit. Toss any sign-extension of the stack
1981 1.1 christos pointer. */
1982 1.1 christos sp &= 0xffffffffUL;
1983 1.1 christos
1984 1.1 christos /* Only use the bottom half if we're in 64-bit mode. */
1985 1.1 christos if (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 64)
1986 1.1 christos offset = 4;
1987 1.1 christos
1988 1.1 christos for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1989 1.1 christos {
1990 1.1 christos if (regnum == -1 || regnum == SPARC_SP_REGNUM || regnum == i)
1991 1.1 christos {
1992 1.1 christos regcache_raw_collect (regcache, i, buf);
1993 1.1 christos
1994 1.1 christos /* Handle StackGhost. */
1995 1.1 christos if (i == SPARC_I7_REGNUM)
1996 1.1 christos {
1997 1.1 christos ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
1998 1.1 christos ULONGEST i7;
1999 1.1 christos
2000 1.1 christos i7 = extract_unsigned_integer (buf + offset, 4, byte_order);
2001 1.1 christos store_unsigned_integer (buf + offset, 4, byte_order,
2002 1.3 christos i7 ^ wcookie);
2003 1.1 christos }
2004 1.1 christos
2005 1.1 christos target_write_memory (sp + ((i - SPARC_L0_REGNUM) * 4),
2006 1.6 christos buf + offset, 4);
2007 1.1 christos }
2008 1.1 christos }
2009 1.1 christos }
2010 1.1 christos }
2011 1.1 christos
2012 1.3 christos /* Helper functions for dealing with register sets. */
2013 1.1 christos
2014 1.1 christos void
2015 1.1 christos sparc32_supply_gregset (const struct sparc_gregmap *gregmap,
2016 1.3 christos struct regcache *regcache,
2017 1.1 christos int regnum, const void *gregs)
2018 1.1 christos {
2019 1.1 christos const gdb_byte *regs = (const gdb_byte *) gregs;
2020 1.3 christos gdb_byte zero[4] = { 0 };
2021 1.1 christos int i;
2022 1.1 christos
2023 1.1 christos if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
2024 1.3 christos regcache_raw_supply (regcache, SPARC32_PSR_REGNUM,
2025 1.1 christos regs + gregmap->r_psr_offset);
2026 1.1 christos
2027 1.1 christos if (regnum == SPARC32_PC_REGNUM || regnum == -1)
2028 1.1 christos regcache_raw_supply (regcache, SPARC32_PC_REGNUM,
2029 1.1 christos regs + gregmap->r_pc_offset);
2030 1.1 christos
2031 1.3 christos if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
2032 1.1 christos regcache_raw_supply (regcache, SPARC32_NPC_REGNUM,
2033 1.1 christos regs + gregmap->r_npc_offset);
2034 1.1 christos
2035 1.1 christos if (regnum == SPARC32_Y_REGNUM || regnum == -1)
2036 1.1 christos regcache_raw_supply (regcache, SPARC32_Y_REGNUM,
2037 1.1 christos regs + gregmap->r_y_offset);
2038 1.1 christos
2039 1.1 christos if (regnum == SPARC_G0_REGNUM || regnum == -1)
2040 1.1 christos regcache_raw_supply (regcache, SPARC_G0_REGNUM, &zero);
2041 1.1 christos
2042 1.1 christos if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
2043 1.1 christos {
2044 1.1 christos int offset = gregmap->r_g1_offset;
2045 1.3 christos
2046 1.1 christos for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
2047 1.1 christos {
2048 1.1 christos if (regnum == i || regnum == -1)
2049 1.1 christos regcache_raw_supply (regcache, i, regs + offset);
2050 1.1 christos offset += 4;
2051 1.1 christos }
2052 1.1 christos }
2053 1.1 christos
2054 1.3 christos if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
2055 1.1 christos {
2056 1.1 christos /* Not all of the register set variants include Locals and
2057 1.1 christos Inputs. For those that don't, we read them off the stack. */
2058 1.1 christos if (gregmap->r_l0_offset == -1)
2059 1.1 christos {
2060 1.1 christos ULONGEST sp;
2061 1.1 christos
2062 1.1 christos regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
2063 1.1 christos sparc_supply_rwindow (regcache, sp, regnum);
2064 1.1 christos }
2065 1.1 christos else
2066 1.1 christos {
2067 1.3 christos int offset = gregmap->r_l0_offset;
2068 1.1 christos
2069 1.1 christos for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
2070 1.1 christos {
2071 1.6 christos if (regnum == i || regnum == -1)
2072 1.1 christos regcache_raw_supply (regcache, i, regs + offset);
2073 1.1 christos offset += 4;
2074 1.1 christos }
2075 1.1 christos }
2076 1.3 christos }
2077 1.1 christos }
2078 1.1 christos
2079 1.1 christos void
2080 1.3 christos sparc32_collect_gregset (const struct sparc_gregmap *gregmap,
2081 1.1 christos const struct regcache *regcache,
2082 1.1 christos int regnum, void *gregs)
2083 1.1 christos {
2084 1.3 christos gdb_byte *regs = (gdb_byte *) gregs;
2085 1.1 christos int i;
2086 1.1 christos
2087 1.1 christos if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
2088 1.3 christos regcache_raw_collect (regcache, SPARC32_PSR_REGNUM,
2089 1.1 christos regs + gregmap->r_psr_offset);
2090 1.1 christos
2091 1.1 christos if (regnum == SPARC32_PC_REGNUM || regnum == -1)
2092 1.3 christos regcache_raw_collect (regcache, SPARC32_PC_REGNUM,
2093 1.1 christos regs + gregmap->r_pc_offset);
2094 1.1 christos
2095 1.1 christos if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
2096 1.1 christos regcache_raw_collect (regcache, SPARC32_NPC_REGNUM,
2097 1.1 christos regs + gregmap->r_npc_offset);
2098 1.1 christos
2099 1.1 christos if (regnum == SPARC32_Y_REGNUM || regnum == -1)
2100 1.1 christos regcache_raw_collect (regcache, SPARC32_Y_REGNUM,
2101 1.1 christos regs + gregmap->r_y_offset);
2102 1.1 christos
2103 1.1 christos if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
2104 1.1 christos {
2105 1.1 christos int offset = gregmap->r_g1_offset;
2106 1.1 christos
2107 1.3 christos /* %g0 is always zero. */
2108 1.1 christos for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
2109 1.3 christos {
2110 1.1 christos if (regnum == i || regnum == -1)
2111 1.1 christos regcache_raw_collect (regcache, i, regs + offset);
2112 1.1 christos offset += 4;
2113 1.1 christos }
2114 1.1 christos }
2115 1.1 christos
2116 1.1 christos if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
2117 1.1 christos {
2118 1.1 christos /* Not all of the register set variants include Locals and
2119 1.1 christos Inputs. For those that don't, we read them off the stack. */
2120 1.1 christos if (gregmap->r_l0_offset != -1)
2121 1.1 christos {
2122 1.3 christos int offset = gregmap->r_l0_offset;
2123 1.1 christos
2124 1.1 christos for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
2125 1.1 christos {
2126 1.6 christos if (regnum == i || regnum == -1)
2127 1.1 christos regcache_raw_collect (regcache, i, regs + offset);
2128 1.1 christos offset += 4;
2129 1.1 christos }
2130 1.1 christos }
2131 1.1 christos }
2132 1.1 christos }
2133 1.3 christos
2134 1.1 christos void
2135 1.1 christos sparc32_supply_fpregset (const struct sparc_fpregmap *fpregmap,
2136 1.1 christos struct regcache *regcache,
2137 1.1 christos int regnum, const void *fpregs)
2138 1.3 christos {
2139 1.1 christos const gdb_byte *regs = (const gdb_byte *) fpregs;
2140 1.1 christos int i;
2141 1.1 christos
2142 1.3 christos for (i = 0; i < 32; i++)
2143 1.1 christos {
2144 1.1 christos if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
2145 1.1 christos regcache_raw_supply (regcache, SPARC_F0_REGNUM + i,
2146 1.6 christos regs + fpregmap->r_f0_offset + (i * 4));
2147 1.1 christos }
2148 1.1 christos
2149 1.1 christos if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
2150 1.1 christos regcache_raw_supply (regcache, SPARC32_FSR_REGNUM,
2151 1.1 christos regs + fpregmap->r_fsr_offset);
2152 1.1 christos }
2153 1.3 christos
2154 1.1 christos void
2155 1.1 christos sparc32_collect_fpregset (const struct sparc_fpregmap *fpregmap,
2156 1.1 christos const struct regcache *regcache,
2157 1.1 christos int regnum, void *fpregs)
2158 1.3 christos {
2159 1.1 christos gdb_byte *regs = (gdb_byte *) fpregs;
2160 1.1 christos int i;
2161 1.1 christos
2162 1.1 christos for (i = 0; i < 32; i++)
2163 1.1 christos {
2164 1.1 christos if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
2165 1.3 christos regcache_raw_collect (regcache, SPARC_F0_REGNUM + i,
2166 1.1 christos regs + fpregmap->r_f0_offset + (i * 4));
2167 1.1 christos }
2168 1.1 christos
2169 1.1 christos if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
2170 1.1 christos regcache_raw_collect (regcache, SPARC32_FSR_REGNUM,
2171 1.1 christos regs + fpregmap->r_fsr_offset);
2172 1.1 christos }
2173 1.1 christos
2174 1.1 christos
2176 1.1 christos /* SunOS 4. */
2177 1.3 christos
2178 1.1 christos /* From <machine/reg.h>. */
2179 1.1 christos const struct sparc_gregmap sparc32_sunos4_gregmap =
2180 1.1 christos {
2181 1.1 christos 0 * 4, /* %psr */
2182 1.1 christos 1 * 4, /* %pc */
2183 1.3 christos 2 * 4, /* %npc */
2184 1.1 christos 3 * 4, /* %y */
2185 1.1 christos -1, /* %wim */
2186 1.1 christos -1, /* %tbr */
2187 1.1 christos 4 * 4, /* %g1 */
2188 1.1 christos -1 /* %l0 */
2189 1.1 christos };
2190 1.1 christos
2191 1.1 christos const struct sparc_fpregmap sparc32_sunos4_fpregmap =
2192 1.1 christos {
2193 1.1 christos 0 * 4, /* %f0 */
2194 1.1 christos 33 * 4, /* %fsr */
2195 1.1 christos };
2196 1.1 christos
2197 1.1 christos const struct sparc_fpregmap sparc32_bsd_fpregmap =
2198 {
2199 0 * 4, /* %f0 */
2200 32 * 4, /* %fsr */
2201 };
2202
2203
2205 /* Provide a prototype to silence -Wmissing-prototypes. */
2206 void _initialize_sparc_tdep (void);
2207
2208 void
2209 _initialize_sparc_tdep (void)
2210 {
2211 register_gdbarch_init (bfd_arch_sparc, sparc32_gdbarch_init);
2212 }
2213