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