Remove path name from test case
[binutils-gdb.git] / gdb / sparc64-tdep.c
1 /* Target-dependent code for UltraSPARC.
2
3 Copyright (C) 2003-2023 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 "defs.h"
21 #include "arch-utils.h"
22 #include "dwarf2/frame.h"
23 #include "frame.h"
24 #include "frame-base.h"
25 #include "frame-unwind.h"
26 #include "gdbcore.h"
27 #include "gdbtypes.h"
28 #include "inferior.h"
29 #include "symtab.h"
30 #include "objfiles.h"
31 #include "osabi.h"
32 #include "regcache.h"
33 #include "target-descriptions.h"
34 #include "target.h"
35 #include "value.h"
36 #include "sparc64-tdep.h"
37 #include <forward_list>
38
39 /* This file implements the SPARC 64-bit ABI as defined by the
40 section "Low-Level System Information" of the SPARC Compliance
41 Definition (SCD) 2.4.1, which is the 64-bit System V psABI for
42 SPARC. */
43
44 /* Please use the sparc32_-prefix for 32-bit specific code, the
45 sparc64_-prefix for 64-bit specific code and the sparc_-prefix for
46 code can handle both. */
47 \f
48 /* The M7 processor supports an Application Data Integrity (ADI) feature
49 that detects invalid data accesses. When software allocates memory and
50 enables ADI on the allocated memory, it chooses a 4-bit version number,
51 sets the version in the upper 4 bits of the 64-bit pointer to that data,
52 and stores the 4-bit version in every cacheline of the object. Hardware
53 saves the latter in spare bits in the cache and memory hierarchy. On each
54 load and store, the processor compares the upper 4 VA (virtual address) bits
55 to the cacheline's version. If there is a mismatch, the processor generates
56 a version mismatch trap which can be either precise or disrupting.
57 The trap is an error condition which the kernel delivers to the process
58 as a SIGSEGV signal.
59
60 The upper 4 bits of the VA represent a version and are not part of the
61 true address. The processor clears these bits and sign extends bit 59
62 to generate the true address.
63
64 Note that 32-bit applications cannot use ADI. */
65
66
67 #include <algorithm>
68 #include "cli/cli-utils.h"
69 #include "gdbcmd.h"
70 #include "auxv.h"
71
72 #define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/9999/adi/lstatus")
73
74 /* ELF Auxiliary vectors */
75 #ifndef AT_ADI_BLKSZ
76 #define AT_ADI_BLKSZ 34
77 #endif
78 #ifndef AT_ADI_NBITS
79 #define AT_ADI_NBITS 35
80 #endif
81 #ifndef AT_ADI_UEONADI
82 #define AT_ADI_UEONADI 36
83 #endif
84
85 /* ADI command list. */
86 static struct cmd_list_element *sparc64adilist = NULL;
87
88 /* ADI stat settings. */
89 struct adi_stat_t
90 {
91 /* The ADI block size. */
92 unsigned long blksize;
93
94 /* Number of bits used for an ADI version tag which can be
95 used together with the shift value for an ADI version tag
96 to encode or extract the ADI version value in a pointer. */
97 unsigned long nbits;
98
99 /* The maximum ADI version tag value supported. */
100 int max_version;
101
102 /* ADI version tag file. */
103 int tag_fd = 0;
104
105 /* ADI availability check has been done. */
106 bool checked_avail = false;
107
108 /* ADI is available. */
109 bool is_avail = false;
110
111 };
112
113 /* Per-process ADI stat info. */
114
115 struct sparc64_adi_info
116 {
117 sparc64_adi_info (pid_t pid_)
118 : pid (pid_)
119 {}
120
121 /* The process identifier. */
122 pid_t pid;
123
124 /* The ADI stat. */
125 adi_stat_t stat = {};
126
127 };
128
129 static std::forward_list<sparc64_adi_info> adi_proc_list;
130
131
132 /* Get ADI info for process PID, creating one if it doesn't exist. */
133
134 static sparc64_adi_info *
135 get_adi_info_proc (pid_t pid)
136 {
137 auto found = std::find_if (adi_proc_list.begin (), adi_proc_list.end (),
138 [&pid] (const sparc64_adi_info &info)
139 {
140 return info.pid == pid;
141 });
142
143 if (found == adi_proc_list.end ())
144 {
145 adi_proc_list.emplace_front (pid);
146 return &adi_proc_list.front ();
147 }
148 else
149 {
150 return &(*found);
151 }
152 }
153
154 static adi_stat_t
155 get_adi_info (pid_t pid)
156 {
157 sparc64_adi_info *proc;
158
159 proc = get_adi_info_proc (pid);
160 return proc->stat;
161 }
162
163 /* Is called when GDB is no longer debugging process PID. It
164 deletes data structure that keeps track of the ADI stat. */
165
166 void
167 sparc64_forget_process (pid_t pid)
168 {
169 fileio_error target_errno;
170
171 for (auto pit = adi_proc_list.before_begin (),
172 it = std::next (pit);
173 it != adi_proc_list.end ();
174 )
175 {
176 if ((*it).pid == pid)
177 {
178 if ((*it).stat.tag_fd > 0)
179 target_fileio_close ((*it).stat.tag_fd, &target_errno);
180 adi_proc_list.erase_after (pit);
181 break;
182 }
183 else
184 pit = it++;
185 }
186
187 }
188
189 /* Read attributes of a maps entry in /proc/[pid]/adi/maps. */
190
191 static void
192 read_maps_entry (const char *line,
193 ULONGEST *addr, ULONGEST *endaddr)
194 {
195 const char *p = line;
196
197 *addr = strtoulst (p, &p, 16);
198 if (*p == '-')
199 p++;
200
201 *endaddr = strtoulst (p, &p, 16);
202 }
203
204 /* Check if ADI is available. */
205
206 static bool
207 adi_available (void)
208 {
209 pid_t pid = inferior_ptid.pid ();
210 sparc64_adi_info *proc = get_adi_info_proc (pid);
211 CORE_ADDR value;
212
213 if (proc->stat.checked_avail)
214 return proc->stat.is_avail;
215
216 proc->stat.checked_avail = true;
217 if (target_auxv_search (AT_ADI_BLKSZ, &value) <= 0)
218 return false;
219 proc->stat.blksize = value;
220 target_auxv_search (AT_ADI_NBITS, &value);
221 proc->stat.nbits = value;
222 proc->stat.max_version = (1 << proc->stat.nbits) - 2;
223 proc->stat.is_avail = true;
224
225 return proc->stat.is_avail;
226 }
227
228 /* Normalize a versioned address - a VA with ADI bits (63-60) set. */
229
230 static CORE_ADDR
231 adi_normalize_address (CORE_ADDR addr)
232 {
233 adi_stat_t ast = get_adi_info (inferior_ptid.pid ());
234
235 if (ast.nbits)
236 {
237 /* Clear upper bits. */
238 addr &= ((uint64_t) -1) >> ast.nbits;
239
240 /* Sign extend. */
241 CORE_ADDR signbit = (uint64_t) 1 << (64 - ast.nbits - 1);
242 return (addr ^ signbit) - signbit;
243 }
244 return addr;
245 }
246
247 /* Align a normalized address - a VA with bit 59 sign extended into
248 ADI bits. */
249
250 static CORE_ADDR
251 adi_align_address (CORE_ADDR naddr)
252 {
253 adi_stat_t ast = get_adi_info (inferior_ptid.pid ());
254
255 return (naddr - (naddr % ast.blksize)) / ast.blksize;
256 }
257
258 /* Convert a byte count to count at a ratio of 1:adi_blksz. */
259
260 static int
261 adi_convert_byte_count (CORE_ADDR naddr, int nbytes, CORE_ADDR locl)
262 {
263 adi_stat_t ast = get_adi_info (inferior_ptid.pid ());
264
265 return ((naddr + nbytes + ast.blksize - 1) / ast.blksize) - locl;
266 }
267
268 /* The /proc/[pid]/adi/tags file, which allows gdb to get/set ADI
269 version in a target process, maps linearly to the address space
270 of the target process at a ratio of 1:adi_blksz.
271
272 A read (or write) at offset K in the file returns (or modifies)
273 the ADI version tag stored in the cacheline containing address
274 K * adi_blksz, encoded as 1 version tag per byte. The allowed
275 version tag values are between 0 and adi_stat.max_version. */
276
277 static int
278 adi_tag_fd (void)
279 {
280 pid_t pid = inferior_ptid.pid ();
281 sparc64_adi_info *proc = get_adi_info_proc (pid);
282
283 if (proc->stat.tag_fd != 0)
284 return proc->stat.tag_fd;
285
286 char cl_name[MAX_PROC_NAME_SIZE];
287 snprintf (cl_name, sizeof(cl_name), "/proc/%ld/adi/tags", (long) pid);
288 fileio_error target_errno;
289 proc->stat.tag_fd = target_fileio_open (NULL, cl_name, O_RDWR|O_EXCL,
290 false, 0, &target_errno);
291 return proc->stat.tag_fd;
292 }
293
294 /* Check if an address set is ADI enabled, using /proc/[pid]/adi/maps
295 which was exported by the kernel and contains the currently ADI
296 mapped memory regions and their access permissions. */
297
298 static bool
299 adi_is_addr_mapped (CORE_ADDR vaddr, size_t cnt)
300 {
301 char filename[MAX_PROC_NAME_SIZE];
302 size_t i = 0;
303
304 pid_t pid = inferior_ptid.pid ();
305 snprintf (filename, sizeof filename, "/proc/%ld/adi/maps", (long) pid);
306 gdb::unique_xmalloc_ptr<char> data
307 = target_fileio_read_stralloc (NULL, filename);
308 if (data)
309 {
310 adi_stat_t adi_stat = get_adi_info (pid);
311 char *saveptr;
312 for (char *line = strtok_r (data.get (), "\n", &saveptr);
313 line;
314 line = strtok_r (NULL, "\n", &saveptr))
315 {
316 ULONGEST addr, endaddr;
317
318 read_maps_entry (line, &addr, &endaddr);
319
320 while (((vaddr + i) * adi_stat.blksize) >= addr
321 && ((vaddr + i) * adi_stat.blksize) < endaddr)
322 {
323 if (++i == cnt)
324 return true;
325 }
326 }
327 }
328 else
329 warning (_("unable to open /proc file '%s'"), filename);
330
331 return false;
332 }
333
334 /* Read ADI version tag value for memory locations starting at "VADDR"
335 for "SIZE" number of bytes. */
336
337 static int
338 adi_read_versions (CORE_ADDR vaddr, size_t size, gdb_byte *tags)
339 {
340 int fd = adi_tag_fd ();
341 if (fd == -1)
342 return -1;
343
344 if (!adi_is_addr_mapped (vaddr, size))
345 {
346 adi_stat_t ast = get_adi_info (inferior_ptid.pid ());
347 error(_("Address at %s is not in ADI maps"),
348 paddress (current_inferior ()->arch (), vaddr * ast.blksize));
349 }
350
351 fileio_error target_errno;
352 return target_fileio_pread (fd, tags, size, vaddr, &target_errno);
353 }
354
355 /* Write ADI version tag for memory locations starting at "VADDR" for
356 "SIZE" number of bytes to "TAGS". */
357
358 static int
359 adi_write_versions (CORE_ADDR vaddr, size_t size, unsigned char *tags)
360 {
361 int fd = adi_tag_fd ();
362 if (fd == -1)
363 return -1;
364
365 if (!adi_is_addr_mapped (vaddr, size))
366 {
367 adi_stat_t ast = get_adi_info (inferior_ptid.pid ());
368 error(_("Address at %s is not in ADI maps"),
369 paddress (current_inferior ()->arch (), vaddr * ast.blksize));
370 }
371
372 fileio_error target_errno;
373 return target_fileio_pwrite (fd, tags, size, vaddr, &target_errno);
374 }
375
376 /* Print ADI version tag value in "TAGS" for memory locations starting
377 at "VADDR" with number of "CNT". */
378
379 static void
380 adi_print_versions (CORE_ADDR vaddr, size_t cnt, gdb_byte *tags)
381 {
382 int v_idx = 0;
383 const int maxelts = 8; /* # of elements per line */
384
385 adi_stat_t adi_stat = get_adi_info (inferior_ptid.pid ());
386
387 while (cnt > 0)
388 {
389 QUIT;
390 gdb_printf ("%s:\t",
391 paddress (current_inferior ()->arch (),
392 vaddr * adi_stat.blksize));
393 for (int i = maxelts; i > 0 && cnt > 0; i--, cnt--)
394 {
395 if (tags[v_idx] == 0xff) /* no version tag */
396 gdb_printf ("-");
397 else
398 gdb_printf ("%1X", tags[v_idx]);
399 if (cnt > 1)
400 gdb_printf (" ");
401 ++v_idx;
402 }
403 gdb_printf ("\n");
404 vaddr += maxelts;
405 }
406 }
407
408 static void
409 do_examine (CORE_ADDR start, int bcnt)
410 {
411 CORE_ADDR vaddr = adi_normalize_address (start);
412
413 CORE_ADDR vstart = adi_align_address (vaddr);
414 int cnt = adi_convert_byte_count (vaddr, bcnt, vstart);
415 gdb::byte_vector buf (cnt);
416 int read_cnt = adi_read_versions (vstart, cnt, buf.data ());
417 if (read_cnt == -1)
418 error (_("No ADI information"));
419 else if (read_cnt < cnt)
420 error(_("No ADI information at %s"),
421 paddress (current_inferior ()->arch (), vaddr));
422
423 adi_print_versions (vstart, cnt, buf.data ());
424 }
425
426 static void
427 do_assign (CORE_ADDR start, size_t bcnt, int version)
428 {
429 CORE_ADDR vaddr = adi_normalize_address (start);
430
431 CORE_ADDR vstart = adi_align_address (vaddr);
432 int cnt = adi_convert_byte_count (vaddr, bcnt, vstart);
433 std::vector<unsigned char> buf (cnt, version);
434 int set_cnt = adi_write_versions (vstart, cnt, buf.data ());
435
436 if (set_cnt == -1)
437 error (_("No ADI information"));
438 else if (set_cnt < cnt)
439 error(_("No ADI information at %s"),
440 paddress (current_inferior ()->arch (), vaddr));
441 }
442
443 /* ADI examine version tag command.
444
445 Command syntax:
446
447 adi (examine|x)[/COUNT] [ADDR] */
448
449 static void
450 adi_examine_command (const char *args, int from_tty)
451 {
452 /* make sure program is active and adi is available */
453 if (!target_has_execution ())
454 error (_("ADI command requires a live process/thread"));
455
456 if (!adi_available ())
457 error (_("No ADI information"));
458
459 int cnt = 1;
460 const char *p = args;
461 if (p && *p == '/')
462 {
463 p++;
464 cnt = get_number (&p);
465 }
466
467 CORE_ADDR next_address = 0;
468 if (p != 0 && *p != 0)
469 next_address = parse_and_eval_address (p);
470 if (!cnt || !next_address)
471 error (_("Usage: adi examine|x[/COUNT] [ADDR]"));
472
473 do_examine (next_address, cnt);
474 }
475
476 /* ADI assign version tag command.
477
478 Command syntax:
479
480 adi (assign|a)[/COUNT] ADDR = VERSION */
481
482 static void
483 adi_assign_command (const char *args, int from_tty)
484 {
485 static const char *adi_usage
486 = N_("Usage: adi assign|a[/COUNT] ADDR = VERSION");
487
488 /* make sure program is active and adi is available */
489 if (!target_has_execution ())
490 error (_("ADI command requires a live process/thread"));
491
492 if (!adi_available ())
493 error (_("No ADI information"));
494
495 const char *exp = args;
496 if (exp == 0)
497 error_no_arg (_(adi_usage));
498
499 char *q = (char *) strchr (exp, '=');
500 if (q)
501 *q++ = 0;
502 else
503 error ("%s", _(adi_usage));
504
505 size_t cnt = 1;
506 const char *p = args;
507 if (exp && *exp == '/')
508 {
509 p = exp + 1;
510 cnt = get_number (&p);
511 }
512
513 CORE_ADDR next_address = 0;
514 if (p != 0 && *p != 0)
515 next_address = parse_and_eval_address (p);
516 else
517 error ("%s", _(adi_usage));
518
519 int version = 0;
520 if (q != NULL) /* parse version tag */
521 {
522 adi_stat_t ast = get_adi_info (inferior_ptid.pid ());
523 version = parse_and_eval_long (q);
524 if (version < 0 || version > ast.max_version)
525 error (_("Invalid ADI version tag %d"), version);
526 }
527
528 do_assign (next_address, cnt, version);
529 }
530
531 void _initialize_sparc64_adi_tdep ();
532 void
533 _initialize_sparc64_adi_tdep ()
534 {
535 add_basic_prefix_cmd ("adi", class_support,
536 _("ADI version related commands."),
537 &sparc64adilist, 0, &cmdlist);
538 cmd_list_element *adi_examine_cmd
539 = add_cmd ("examine", class_support, adi_examine_command,
540 _("Examine ADI versions."), &sparc64adilist);
541 add_alias_cmd ("x", adi_examine_cmd, no_class, 1, &sparc64adilist);
542 add_cmd ("assign", class_support, adi_assign_command,
543 _("Assign ADI versions."), &sparc64adilist);
544
545 }
546 \f
547
548 /* The functions on this page are intended to be used to classify
549 function arguments. */
550
551 /* Check whether TYPE is "Integral or Pointer". */
552
553 static int
554 sparc64_integral_or_pointer_p (const struct type *type)
555 {
556 switch (type->code ())
557 {
558 case TYPE_CODE_INT:
559 case TYPE_CODE_BOOL:
560 case TYPE_CODE_CHAR:
561 case TYPE_CODE_ENUM:
562 case TYPE_CODE_RANGE:
563 {
564 int len = type->length ();
565 gdb_assert (len == 1 || len == 2 || len == 4 || len == 8);
566 }
567 return 1;
568 case TYPE_CODE_PTR:
569 case TYPE_CODE_REF:
570 case TYPE_CODE_RVALUE_REF:
571 {
572 int len = type->length ();
573 gdb_assert (len == 8);
574 }
575 return 1;
576 default:
577 break;
578 }
579
580 return 0;
581 }
582
583 /* Check whether TYPE is "Floating". */
584
585 static int
586 sparc64_floating_p (const struct type *type)
587 {
588 switch (type->code ())
589 {
590 case TYPE_CODE_FLT:
591 {
592 int len = type->length ();
593 gdb_assert (len == 4 || len == 8 || len == 16);
594 }
595 return 1;
596 default:
597 break;
598 }
599
600 return 0;
601 }
602
603 /* Check whether TYPE is "Complex Floating". */
604
605 static int
606 sparc64_complex_floating_p (const struct type *type)
607 {
608 switch (type->code ())
609 {
610 case TYPE_CODE_COMPLEX:
611 {
612 int len = type->length ();
613 gdb_assert (len == 8 || len == 16 || len == 32);
614 }
615 return 1;
616 default:
617 break;
618 }
619
620 return 0;
621 }
622
623 /* Check whether TYPE is "Structure or Union".
624
625 In terms of Ada subprogram calls, arrays are treated the same as
626 struct and union types. So this function also returns non-zero
627 for array types. */
628
629 static int
630 sparc64_structure_or_union_p (const struct type *type)
631 {
632 switch (type->code ())
633 {
634 case TYPE_CODE_STRUCT:
635 case TYPE_CODE_UNION:
636 case TYPE_CODE_ARRAY:
637 return 1;
638 default:
639 break;
640 }
641
642 return 0;
643 }
644 \f
645
646 /* Construct types for ISA-specific registers. */
647
648 static struct type *
649 sparc64_pstate_type (struct gdbarch *gdbarch)
650 {
651 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (gdbarch);
652
653 if (!tdep->sparc64_pstate_type)
654 {
655 struct type *type;
656
657 type = arch_flags_type (gdbarch, "builtin_type_sparc64_pstate", 64);
658 append_flags_type_flag (type, 0, "AG");
659 append_flags_type_flag (type, 1, "IE");
660 append_flags_type_flag (type, 2, "PRIV");
661 append_flags_type_flag (type, 3, "AM");
662 append_flags_type_flag (type, 4, "PEF");
663 append_flags_type_flag (type, 5, "RED");
664 append_flags_type_flag (type, 8, "TLE");
665 append_flags_type_flag (type, 9, "CLE");
666 append_flags_type_flag (type, 10, "PID0");
667 append_flags_type_flag (type, 11, "PID1");
668
669 tdep->sparc64_pstate_type = type;
670 }
671
672 return tdep->sparc64_pstate_type;
673 }
674
675 static struct type *
676 sparc64_ccr_type (struct gdbarch *gdbarch)
677 {
678 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (gdbarch);
679
680 if (tdep->sparc64_ccr_type == NULL)
681 {
682 struct type *type;
683
684 type = arch_flags_type (gdbarch, "builtin_type_sparc64_ccr", 64);
685 append_flags_type_flag (type, 0, "icc.c");
686 append_flags_type_flag (type, 1, "icc.v");
687 append_flags_type_flag (type, 2, "icc.z");
688 append_flags_type_flag (type, 3, "icc.n");
689 append_flags_type_flag (type, 4, "xcc.c");
690 append_flags_type_flag (type, 5, "xcc.v");
691 append_flags_type_flag (type, 6, "xcc.z");
692 append_flags_type_flag (type, 7, "xcc.n");
693
694 tdep->sparc64_ccr_type = type;
695 }
696
697 return tdep->sparc64_ccr_type;
698 }
699
700 static struct type *
701 sparc64_fsr_type (struct gdbarch *gdbarch)
702 {
703 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (gdbarch);
704
705 if (!tdep->sparc64_fsr_type)
706 {
707 struct type *type;
708
709 type = arch_flags_type (gdbarch, "builtin_type_sparc64_fsr", 64);
710 append_flags_type_flag (type, 0, "NXC");
711 append_flags_type_flag (type, 1, "DZC");
712 append_flags_type_flag (type, 2, "UFC");
713 append_flags_type_flag (type, 3, "OFC");
714 append_flags_type_flag (type, 4, "NVC");
715 append_flags_type_flag (type, 5, "NXA");
716 append_flags_type_flag (type, 6, "DZA");
717 append_flags_type_flag (type, 7, "UFA");
718 append_flags_type_flag (type, 8, "OFA");
719 append_flags_type_flag (type, 9, "NVA");
720 append_flags_type_flag (type, 22, "NS");
721 append_flags_type_flag (type, 23, "NXM");
722 append_flags_type_flag (type, 24, "DZM");
723 append_flags_type_flag (type, 25, "UFM");
724 append_flags_type_flag (type, 26, "OFM");
725 append_flags_type_flag (type, 27, "NVM");
726
727 tdep->sparc64_fsr_type = type;
728 }
729
730 return tdep->sparc64_fsr_type;
731 }
732
733 static struct type *
734 sparc64_fprs_type (struct gdbarch *gdbarch)
735 {
736 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (gdbarch);
737
738 if (!tdep->sparc64_fprs_type)
739 {
740 struct type *type;
741
742 type = arch_flags_type (gdbarch, "builtin_type_sparc64_fprs", 64);
743 append_flags_type_flag (type, 0, "DL");
744 append_flags_type_flag (type, 1, "DU");
745 append_flags_type_flag (type, 2, "FEF");
746
747 tdep->sparc64_fprs_type = type;
748 }
749
750 return tdep->sparc64_fprs_type;
751 }
752
753
754 /* Register information. */
755 #define SPARC64_FPU_REGISTERS \
756 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", \
757 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15", \
758 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23", \
759 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31", \
760 "f32", "f34", "f36", "f38", "f40", "f42", "f44", "f46", \
761 "f48", "f50", "f52", "f54", "f56", "f58", "f60", "f62"
762 #define SPARC64_CP0_REGISTERS \
763 "pc", "npc", \
764 /* FIXME: Give "state" a name until we start using register groups. */ \
765 "state", \
766 "fsr", \
767 "fprs", \
768 "y"
769
770 static const char * const sparc64_fpu_register_names[] = {
771 SPARC64_FPU_REGISTERS
772 };
773 static const char * const sparc64_cp0_register_names[] = {
774 SPARC64_CP0_REGISTERS
775 };
776
777 static const char * const sparc64_register_names[] =
778 {
779 SPARC_CORE_REGISTERS,
780 SPARC64_FPU_REGISTERS,
781 SPARC64_CP0_REGISTERS
782 };
783
784 /* Total number of registers. */
785 #define SPARC64_NUM_REGS ARRAY_SIZE (sparc64_register_names)
786
787 /* We provide the aliases %d0..%d62 and %q0..%q60 for the floating
788 registers as "psuedo" registers. */
789
790 static const char * const sparc64_pseudo_register_names[] =
791 {
792 "cwp", "pstate", "asi", "ccr",
793
794 "d0", "d2", "d4", "d6", "d8", "d10", "d12", "d14",
795 "d16", "d18", "d20", "d22", "d24", "d26", "d28", "d30",
796 "d32", "d34", "d36", "d38", "d40", "d42", "d44", "d46",
797 "d48", "d50", "d52", "d54", "d56", "d58", "d60", "d62",
798
799 "q0", "q4", "q8", "q12", "q16", "q20", "q24", "q28",
800 "q32", "q36", "q40", "q44", "q48", "q52", "q56", "q60",
801 };
802
803 /* Total number of pseudo registers. */
804 #define SPARC64_NUM_PSEUDO_REGS ARRAY_SIZE (sparc64_pseudo_register_names)
805
806 /* Return the name of pseudo register REGNUM. */
807
808 static const char *
809 sparc64_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
810 {
811 regnum -= gdbarch_num_regs (gdbarch);
812
813 gdb_assert (regnum < SPARC64_NUM_PSEUDO_REGS);
814 return sparc64_pseudo_register_names[regnum];
815 }
816
817 /* Return the name of register REGNUM. */
818
819 static const char *
820 sparc64_register_name (struct gdbarch *gdbarch, int regnum)
821 {
822 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
823 return tdesc_register_name (gdbarch, regnum);
824
825 if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
826 return sparc64_register_names[regnum];
827
828 return sparc64_pseudo_register_name (gdbarch, regnum);
829 }
830
831 /* Return the GDB type object for the "standard" data type of data in
832 pseudo register REGNUM. */
833
834 static struct type *
835 sparc64_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
836 {
837 regnum -= gdbarch_num_regs (gdbarch);
838
839 if (regnum == SPARC64_CWP_REGNUM)
840 return builtin_type (gdbarch)->builtin_int64;
841 if (regnum == SPARC64_PSTATE_REGNUM)
842 return sparc64_pstate_type (gdbarch);
843 if (regnum == SPARC64_ASI_REGNUM)
844 return builtin_type (gdbarch)->builtin_int64;
845 if (regnum == SPARC64_CCR_REGNUM)
846 return sparc64_ccr_type (gdbarch);
847 if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D62_REGNUM)
848 return builtin_type (gdbarch)->builtin_double;
849 if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q60_REGNUM)
850 return builtin_type (gdbarch)->builtin_long_double;
851
852 internal_error (_("sparc64_pseudo_register_type: bad register number %d"),
853 regnum);
854 }
855
856 /* Return the GDB type object for the "standard" data type of data in
857 register REGNUM. */
858
859 static struct type *
860 sparc64_register_type (struct gdbarch *gdbarch, int regnum)
861 {
862 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
863 return tdesc_register_type (gdbarch, regnum);
864
865 /* Raw registers. */
866 if (regnum == SPARC_SP_REGNUM || regnum == SPARC_FP_REGNUM)
867 return builtin_type (gdbarch)->builtin_data_ptr;
868 if (regnum >= SPARC_G0_REGNUM && regnum <= SPARC_I7_REGNUM)
869 return builtin_type (gdbarch)->builtin_int64;
870 if (regnum >= SPARC_F0_REGNUM && regnum <= SPARC_F31_REGNUM)
871 return builtin_type (gdbarch)->builtin_float;
872 if (regnum >= SPARC64_F32_REGNUM && regnum <= SPARC64_F62_REGNUM)
873 return builtin_type (gdbarch)->builtin_double;
874 if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM)
875 return builtin_type (gdbarch)->builtin_func_ptr;
876 /* This raw register contains the contents of %cwp, %pstate, %asi
877 and %ccr as laid out in a %tstate register. */
878 if (regnum == SPARC64_STATE_REGNUM)
879 return builtin_type (gdbarch)->builtin_int64;
880 if (regnum == SPARC64_FSR_REGNUM)
881 return sparc64_fsr_type (gdbarch);
882 if (regnum == SPARC64_FPRS_REGNUM)
883 return sparc64_fprs_type (gdbarch);
884 /* "Although Y is a 64-bit register, its high-order 32 bits are
885 reserved and always read as 0." */
886 if (regnum == SPARC64_Y_REGNUM)
887 return builtin_type (gdbarch)->builtin_int64;
888
889 /* Pseudo registers. */
890 if (regnum >= gdbarch_num_regs (gdbarch))
891 return sparc64_pseudo_register_type (gdbarch, regnum);
892
893 internal_error (_("invalid regnum"));
894 }
895
896 static enum register_status
897 sparc64_pseudo_register_read (struct gdbarch *gdbarch,
898 readable_regcache *regcache,
899 int regnum, gdb_byte *buf)
900 {
901 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
902 enum register_status status;
903
904 regnum -= gdbarch_num_regs (gdbarch);
905
906 if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D30_REGNUM)
907 {
908 regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC64_D0_REGNUM);
909 status = regcache->raw_read (regnum, buf);
910 if (status == REG_VALID)
911 status = regcache->raw_read (regnum + 1, buf + 4);
912 return status;
913 }
914 else if (regnum >= SPARC64_D32_REGNUM && regnum <= SPARC64_D62_REGNUM)
915 {
916 regnum = SPARC64_F32_REGNUM + (regnum - SPARC64_D32_REGNUM);
917 return regcache->raw_read (regnum, buf);
918 }
919 else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q28_REGNUM)
920 {
921 regnum = SPARC_F0_REGNUM + 4 * (regnum - SPARC64_Q0_REGNUM);
922
923 status = regcache->raw_read (regnum, buf);
924 if (status == REG_VALID)
925 status = regcache->raw_read (regnum + 1, buf + 4);
926 if (status == REG_VALID)
927 status = regcache->raw_read (regnum + 2, buf + 8);
928 if (status == REG_VALID)
929 status = regcache->raw_read (regnum + 3, buf + 12);
930
931 return status;
932 }
933 else if (regnum >= SPARC64_Q32_REGNUM && regnum <= SPARC64_Q60_REGNUM)
934 {
935 regnum = SPARC64_F32_REGNUM + 2 * (regnum - SPARC64_Q32_REGNUM);
936
937 status = regcache->raw_read (regnum, buf);
938 if (status == REG_VALID)
939 status = regcache->raw_read (regnum + 1, buf + 8);
940
941 return status;
942 }
943 else if (regnum == SPARC64_CWP_REGNUM
944 || regnum == SPARC64_PSTATE_REGNUM
945 || regnum == SPARC64_ASI_REGNUM
946 || regnum == SPARC64_CCR_REGNUM)
947 {
948 ULONGEST state;
949
950 status = regcache->raw_read (SPARC64_STATE_REGNUM, &state);
951 if (status != REG_VALID)
952 return status;
953
954 switch (regnum)
955 {
956 case SPARC64_CWP_REGNUM:
957 state = (state >> 0) & ((1 << 5) - 1);
958 break;
959 case SPARC64_PSTATE_REGNUM:
960 state = (state >> 8) & ((1 << 12) - 1);
961 break;
962 case SPARC64_ASI_REGNUM:
963 state = (state >> 24) & ((1 << 8) - 1);
964 break;
965 case SPARC64_CCR_REGNUM:
966 state = (state >> 32) & ((1 << 8) - 1);
967 break;
968 }
969 store_unsigned_integer (buf, 8, byte_order, state);
970 }
971
972 return REG_VALID;
973 }
974
975 static void
976 sparc64_pseudo_register_write (struct gdbarch *gdbarch,
977 struct regcache *regcache,
978 int regnum, const gdb_byte *buf)
979 {
980 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
981
982 regnum -= gdbarch_num_regs (gdbarch);
983
984 if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D30_REGNUM)
985 {
986 regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC64_D0_REGNUM);
987 regcache->raw_write (regnum, buf);
988 regcache->raw_write (regnum + 1, buf + 4);
989 }
990 else if (regnum >= SPARC64_D32_REGNUM && regnum <= SPARC64_D62_REGNUM)
991 {
992 regnum = SPARC64_F32_REGNUM + (regnum - SPARC64_D32_REGNUM);
993 regcache->raw_write (regnum, buf);
994 }
995 else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q28_REGNUM)
996 {
997 regnum = SPARC_F0_REGNUM + 4 * (regnum - SPARC64_Q0_REGNUM);
998 regcache->raw_write (regnum, buf);
999 regcache->raw_write (regnum + 1, buf + 4);
1000 regcache->raw_write (regnum + 2, buf + 8);
1001 regcache->raw_write (regnum + 3, buf + 12);
1002 }
1003 else if (regnum >= SPARC64_Q32_REGNUM && regnum <= SPARC64_Q60_REGNUM)
1004 {
1005 regnum = SPARC64_F32_REGNUM + 2 * (regnum - SPARC64_Q32_REGNUM);
1006 regcache->raw_write (regnum, buf);
1007 regcache->raw_write (regnum + 1, buf + 8);
1008 }
1009 else if (regnum == SPARC64_CWP_REGNUM
1010 || regnum == SPARC64_PSTATE_REGNUM
1011 || regnum == SPARC64_ASI_REGNUM
1012 || regnum == SPARC64_CCR_REGNUM)
1013 {
1014 ULONGEST state, bits;
1015
1016 regcache_raw_read_unsigned (regcache, SPARC64_STATE_REGNUM, &state);
1017 bits = extract_unsigned_integer (buf, 8, byte_order);
1018 switch (regnum)
1019 {
1020 case SPARC64_CWP_REGNUM:
1021 state |= ((bits & ((1 << 5) - 1)) << 0);
1022 break;
1023 case SPARC64_PSTATE_REGNUM:
1024 state |= ((bits & ((1 << 12) - 1)) << 8);
1025 break;
1026 case SPARC64_ASI_REGNUM:
1027 state |= ((bits & ((1 << 8) - 1)) << 24);
1028 break;
1029 case SPARC64_CCR_REGNUM:
1030 state |= ((bits & ((1 << 8) - 1)) << 32);
1031 break;
1032 }
1033 regcache_raw_write_unsigned (regcache, SPARC64_STATE_REGNUM, state);
1034 }
1035 }
1036 \f
1037
1038 /* Return PC of first real instruction of the function starting at
1039 START_PC. */
1040
1041 static CORE_ADDR
1042 sparc64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
1043 {
1044 struct symtab_and_line sal;
1045 CORE_ADDR func_start, func_end;
1046 struct sparc_frame_cache cache;
1047
1048 /* This is the preferred method, find the end of the prologue by
1049 using the debugging information. */
1050 if (find_pc_partial_function (start_pc, NULL, &func_start, &func_end))
1051 {
1052 sal = find_pc_line (func_start, 0);
1053
1054 if (sal.end < func_end
1055 && start_pc <= sal.end)
1056 return sal.end;
1057 }
1058
1059 return sparc_analyze_prologue (gdbarch, start_pc, 0xffffffffffffffffULL,
1060 &cache);
1061 }
1062
1063 /* Normal frames. */
1064
1065 static struct sparc_frame_cache *
1066 sparc64_frame_cache (frame_info_ptr this_frame, void **this_cache)
1067 {
1068 return sparc_frame_cache (this_frame, this_cache);
1069 }
1070
1071 static void
1072 sparc64_frame_this_id (frame_info_ptr this_frame, void **this_cache,
1073 struct frame_id *this_id)
1074 {
1075 struct sparc_frame_cache *cache =
1076 sparc64_frame_cache (this_frame, this_cache);
1077
1078 /* This marks the outermost frame. */
1079 if (cache->base == 0)
1080 return;
1081
1082 (*this_id) = frame_id_build (cache->base, cache->pc);
1083 }
1084
1085 static struct value *
1086 sparc64_frame_prev_register (frame_info_ptr this_frame, void **this_cache,
1087 int regnum)
1088 {
1089 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1090 struct sparc_frame_cache *cache =
1091 sparc64_frame_cache (this_frame, this_cache);
1092
1093 if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM)
1094 {
1095 CORE_ADDR pc = (regnum == SPARC64_NPC_REGNUM) ? 4 : 0;
1096
1097 regnum =
1098 (cache->copied_regs_mask & 0x80) ? SPARC_I7_REGNUM : SPARC_O7_REGNUM;
1099 pc += get_frame_register_unsigned (this_frame, regnum) + 8;
1100 return frame_unwind_got_constant (this_frame, regnum, pc);
1101 }
1102
1103 /* Handle StackGhost. */
1104 {
1105 ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
1106
1107 if (wcookie != 0 && !cache->frameless_p && regnum == SPARC_I7_REGNUM)
1108 {
1109 CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 8;
1110 ULONGEST i7;
1111
1112 /* Read the value in from memory. */
1113 i7 = get_frame_memory_unsigned (this_frame, addr, 8);
1114 return frame_unwind_got_constant (this_frame, regnum, i7 ^ wcookie);
1115 }
1116 }
1117
1118 /* The previous frame's `local' and `in' registers may have been saved
1119 in the register save area. */
1120 if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM
1121 && (cache->saved_regs_mask & (1 << (regnum - SPARC_L0_REGNUM))))
1122 {
1123 CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 8;
1124
1125 return frame_unwind_got_memory (this_frame, regnum, addr);
1126 }
1127
1128 /* The previous frame's `out' registers may be accessible as the current
1129 frame's `in' registers. */
1130 if (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O7_REGNUM
1131 && (cache->copied_regs_mask & (1 << (regnum - SPARC_O0_REGNUM))))
1132 regnum += (SPARC_I0_REGNUM - SPARC_O0_REGNUM);
1133
1134 return frame_unwind_got_register (this_frame, regnum, regnum);
1135 }
1136
1137 static const struct frame_unwind sparc64_frame_unwind =
1138 {
1139 "sparc64 prologue",
1140 NORMAL_FRAME,
1141 default_frame_unwind_stop_reason,
1142 sparc64_frame_this_id,
1143 sparc64_frame_prev_register,
1144 NULL,
1145 default_frame_sniffer
1146 };
1147 \f
1148
1149 static CORE_ADDR
1150 sparc64_frame_base_address (frame_info_ptr this_frame, void **this_cache)
1151 {
1152 struct sparc_frame_cache *cache =
1153 sparc64_frame_cache (this_frame, this_cache);
1154
1155 return cache->base;
1156 }
1157
1158 static const struct frame_base sparc64_frame_base =
1159 {
1160 &sparc64_frame_unwind,
1161 sparc64_frame_base_address,
1162 sparc64_frame_base_address,
1163 sparc64_frame_base_address
1164 };
1165 \f
1166 /* Check whether TYPE must be 16-byte aligned. */
1167
1168 static int
1169 sparc64_16_byte_align_p (struct type *type)
1170 {
1171 if (type->code () == TYPE_CODE_ARRAY)
1172 {
1173 struct type *t = check_typedef (type->target_type ());
1174
1175 if (sparc64_floating_p (t))
1176 return 1;
1177 }
1178 if (sparc64_floating_p (type) && type->length () == 16)
1179 return 1;
1180
1181 if (sparc64_structure_or_union_p (type))
1182 {
1183 int i;
1184
1185 for (i = 0; i < type->num_fields (); i++)
1186 {
1187 struct type *subtype = check_typedef (type->field (i).type ());
1188
1189 if (sparc64_16_byte_align_p (subtype))
1190 return 1;
1191 }
1192 }
1193
1194 return 0;
1195 }
1196
1197 /* Store floating fields of element ELEMENT of an "parameter array"
1198 that has type TYPE and is stored at BITPOS in VALBUF in the
1199 appropriate registers of REGCACHE. This function can be called
1200 recursively and therefore handles floating types in addition to
1201 structures. */
1202
1203 static void
1204 sparc64_store_floating_fields (struct regcache *regcache, struct type *type,
1205 const gdb_byte *valbuf, int element, int bitpos)
1206 {
1207 struct gdbarch *gdbarch = regcache->arch ();
1208 int len = type->length ();
1209
1210 gdb_assert (element < 16);
1211
1212 if (type->code () == TYPE_CODE_ARRAY)
1213 {
1214 gdb_byte buf[8];
1215 int regnum = SPARC_F0_REGNUM + element * 2 + bitpos / 32;
1216
1217 valbuf += bitpos / 8;
1218 if (len < 8)
1219 {
1220 memset (buf, 0, 8 - len);
1221 memcpy (buf + 8 - len, valbuf, len);
1222 valbuf = buf;
1223 len = 8;
1224 }
1225 for (int n = 0; n < (len + 3) / 4; n++)
1226 regcache->cooked_write (regnum + n, valbuf + n * 4);
1227 }
1228 else if (sparc64_floating_p (type)
1229 || (sparc64_complex_floating_p (type) && len <= 16))
1230 {
1231 int regnum;
1232
1233 if (len == 16)
1234 {
1235 gdb_assert (bitpos == 0);
1236 gdb_assert ((element % 2) == 0);
1237
1238 regnum = gdbarch_num_regs (gdbarch) + SPARC64_Q0_REGNUM + element / 2;
1239 regcache->cooked_write (regnum, valbuf);
1240 }
1241 else if (len == 8)
1242 {
1243 gdb_assert (bitpos == 0 || bitpos == 64);
1244
1245 regnum = gdbarch_num_regs (gdbarch) + SPARC64_D0_REGNUM
1246 + element + bitpos / 64;
1247 regcache->cooked_write (regnum, valbuf + (bitpos / 8));
1248 }
1249 else
1250 {
1251 gdb_assert (len == 4);
1252 gdb_assert (bitpos % 32 == 0 && bitpos >= 0 && bitpos < 128);
1253
1254 regnum = SPARC_F0_REGNUM + element * 2 + bitpos / 32;
1255 regcache->cooked_write (regnum, valbuf + (bitpos / 8));
1256 }
1257 }
1258 else if (sparc64_structure_or_union_p (type))
1259 {
1260 int i;
1261
1262 for (i = 0; i < type->num_fields (); i++)
1263 {
1264 struct type *subtype = check_typedef (type->field (i).type ());
1265 int subpos = bitpos + type->field (i).loc_bitpos ();
1266
1267 sparc64_store_floating_fields (regcache, subtype, valbuf,
1268 element, subpos);
1269 }
1270
1271 /* GCC has an interesting bug. If TYPE is a structure that has
1272 a single `float' member, GCC doesn't treat it as a structure
1273 at all, but rather as an ordinary `float' argument. This
1274 argument will be stored in %f1, as required by the psABI.
1275 However, as a member of a structure the psABI requires it to
1276 be stored in %f0. This bug is present in GCC 3.3.2, but
1277 probably in older releases to. To appease GCC, if a
1278 structure has only a single `float' member, we store its
1279 value in %f1 too (we already have stored in %f0). */
1280 if (type->num_fields () == 1)
1281 {
1282 struct type *subtype = check_typedef (type->field (0).type ());
1283
1284 if (sparc64_floating_p (subtype) && subtype->length () == 4)
1285 regcache->cooked_write (SPARC_F1_REGNUM, valbuf);
1286 }
1287 }
1288 }
1289
1290 /* Fetch floating fields from a variable of type TYPE from the
1291 appropriate registers for BITPOS in REGCACHE and store it at BITPOS
1292 in VALBUF. This function can be called recursively and therefore
1293 handles floating types in addition to structures. */
1294
1295 static void
1296 sparc64_extract_floating_fields (struct regcache *regcache, struct type *type,
1297 gdb_byte *valbuf, int bitpos)
1298 {
1299 struct gdbarch *gdbarch = regcache->arch ();
1300
1301 if (type->code () == TYPE_CODE_ARRAY)
1302 {
1303 int len = type->length ();
1304 int regnum = SPARC_F0_REGNUM + bitpos / 32;
1305
1306 valbuf += bitpos / 8;
1307 if (len < 4)
1308 {
1309 gdb_byte buf[4];
1310 regcache->cooked_read (regnum, buf);
1311 memcpy (valbuf, buf + 4 - len, len);
1312 }
1313 else
1314 for (int i = 0; i < (len + 3) / 4; i++)
1315 regcache->cooked_read (regnum + i, valbuf + i * 4);
1316 }
1317 else if (sparc64_floating_p (type))
1318 {
1319 int len = type->length ();
1320 int regnum;
1321
1322 if (len == 16)
1323 {
1324 gdb_assert (bitpos == 0 || bitpos == 128);
1325
1326 regnum = gdbarch_num_regs (gdbarch) + SPARC64_Q0_REGNUM
1327 + bitpos / 128;
1328 regcache->cooked_read (regnum, valbuf + (bitpos / 8));
1329 }
1330 else if (len == 8)
1331 {
1332 gdb_assert (bitpos % 64 == 0 && bitpos >= 0 && bitpos < 256);
1333
1334 regnum = gdbarch_num_regs (gdbarch) + SPARC64_D0_REGNUM + bitpos / 64;
1335 regcache->cooked_read (regnum, valbuf + (bitpos / 8));
1336 }
1337 else
1338 {
1339 gdb_assert (len == 4);
1340 gdb_assert (bitpos % 32 == 0 && bitpos >= 0 && bitpos < 256);
1341
1342 regnum = SPARC_F0_REGNUM + bitpos / 32;
1343 regcache->cooked_read (regnum, valbuf + (bitpos / 8));
1344 }
1345 }
1346 else if (sparc64_structure_or_union_p (type))
1347 {
1348 int i;
1349
1350 for (i = 0; i < type->num_fields (); i++)
1351 {
1352 struct type *subtype = check_typedef (type->field (i).type ());
1353 int subpos = bitpos + type->field (i).loc_bitpos ();
1354
1355 sparc64_extract_floating_fields (regcache, subtype, valbuf, subpos);
1356 }
1357 }
1358 }
1359
1360 /* Store the NARGS arguments ARGS and STRUCT_ADDR (if STRUCT_RETURN is
1361 non-zero) in REGCACHE and on the stack (starting from address SP). */
1362
1363 static CORE_ADDR
1364 sparc64_store_arguments (struct regcache *regcache, int nargs,
1365 struct value **args, CORE_ADDR sp,
1366 function_call_return_method return_method,
1367 CORE_ADDR struct_addr)
1368 {
1369 struct gdbarch *gdbarch = regcache->arch ();
1370 /* Number of extended words in the "parameter array". */
1371 int num_elements = 0;
1372 int element = 0;
1373 int i;
1374
1375 /* Take BIAS into account. */
1376 sp += BIAS;
1377
1378 /* First we calculate the number of extended words in the "parameter
1379 array". While doing so we also convert some of the arguments. */
1380
1381 if (return_method == return_method_struct)
1382 num_elements++;
1383
1384 for (i = 0; i < nargs; i++)
1385 {
1386 struct type *type = args[i]->type ();
1387 int len = type->length ();
1388
1389 if (sparc64_structure_or_union_p (type)
1390 || (sparc64_complex_floating_p (type) && len == 32))
1391 {
1392 /* Structure or Union arguments. */
1393 if (len <= 16)
1394 {
1395 if (num_elements % 2 && sparc64_16_byte_align_p (type))
1396 num_elements++;
1397 num_elements += ((len + 7) / 8);
1398 }
1399 else
1400 {
1401 /* The psABI says that "Structures or unions larger than
1402 sixteen bytes are copied by the caller and passed
1403 indirectly; the caller will pass the address of a
1404 correctly aligned structure value. This sixty-four
1405 bit address will occupy one word in the parameter
1406 array, and may be promoted to an %o register like any
1407 other pointer value." Allocate memory for these
1408 values on the stack. */
1409 sp -= len;
1410
1411 /* Use 16-byte alignment for these values. That's
1412 always correct, and wasting a few bytes shouldn't be
1413 a problem. */
1414 sp &= ~0xf;
1415
1416 write_memory (sp, args[i]->contents ().data (), len);
1417 args[i] = value_from_pointer (lookup_pointer_type (type), sp);
1418 num_elements++;
1419 }
1420 }
1421 else if (sparc64_floating_p (type) || sparc64_complex_floating_p (type))
1422 {
1423 /* Floating arguments. */
1424 if (len == 16)
1425 {
1426 /* The psABI says that "Each quad-precision parameter
1427 value will be assigned to two extended words in the
1428 parameter array. */
1429 num_elements += 2;
1430
1431 /* The psABI says that "Long doubles must be
1432 quad-aligned, and thus a hole might be introduced
1433 into the parameter array to force alignment." Skip
1434 an element if necessary. */
1435 if ((num_elements % 2) && sparc64_16_byte_align_p (type))
1436 num_elements++;
1437 }
1438 else
1439 num_elements++;
1440 }
1441 else
1442 {
1443 /* Integral and pointer arguments. */
1444 gdb_assert (sparc64_integral_or_pointer_p (type));
1445
1446 /* The psABI says that "Each argument value of integral type
1447 smaller than an extended word will be widened by the
1448 caller to an extended word according to the signed-ness
1449 of the argument type." */
1450 if (len < 8)
1451 args[i] = value_cast (builtin_type (gdbarch)->builtin_int64,
1452 args[i]);
1453 num_elements++;
1454 }
1455 }
1456
1457 /* Allocate the "parameter array". */
1458 sp -= num_elements * 8;
1459
1460 /* The psABI says that "Every stack frame must be 16-byte aligned." */
1461 sp &= ~0xf;
1462
1463 /* Now we store the arguments in to the "parameter array". Some
1464 Integer or Pointer arguments and Structure or Union arguments
1465 will be passed in %o registers. Some Floating arguments and
1466 floating members of structures are passed in floating-point
1467 registers. However, for functions with variable arguments,
1468 floating arguments are stored in an %0 register, and for
1469 functions without a prototype floating arguments are stored in
1470 both a floating-point and an %o registers, or a floating-point
1471 register and memory. To simplify the logic here we always pass
1472 arguments in memory, an %o register, and a floating-point
1473 register if appropriate. This should be no problem since the
1474 contents of any unused memory or registers in the "parameter
1475 array" are undefined. */
1476
1477 if (return_method == return_method_struct)
1478 {
1479 regcache_cooked_write_unsigned (regcache, SPARC_O0_REGNUM, struct_addr);
1480 element++;
1481 }
1482
1483 for (i = 0; i < nargs; i++)
1484 {
1485 const gdb_byte *valbuf = args[i]->contents ().data ();
1486 struct type *type = args[i]->type ();
1487 int len = type->length ();
1488 int regnum = -1;
1489 gdb_byte buf[16];
1490
1491 if (sparc64_structure_or_union_p (type)
1492 || (sparc64_complex_floating_p (type) && len == 32))
1493 {
1494 /* Structure, Union or long double Complex arguments. */
1495 gdb_assert (len <= 16);
1496 memset (buf, 0, sizeof (buf));
1497 memcpy (buf, valbuf, len);
1498 valbuf = buf;
1499
1500 if (element % 2 && sparc64_16_byte_align_p (type))
1501 element++;
1502
1503 if (element < 6)
1504 {
1505 regnum = SPARC_O0_REGNUM + element;
1506 if (len > 8 && element < 5)
1507 regcache->cooked_write (regnum + 1, valbuf + 8);
1508 }
1509
1510 if (element < 16)
1511 sparc64_store_floating_fields (regcache, type, valbuf, element, 0);
1512 }
1513 else if (sparc64_complex_floating_p (type))
1514 {
1515 /* Float Complex or double Complex arguments. */
1516 if (element < 16)
1517 {
1518 regnum = gdbarch_num_regs (gdbarch) + SPARC64_D0_REGNUM + element;
1519
1520 if (len == 16)
1521 {
1522 if (regnum < gdbarch_num_regs (gdbarch) + SPARC64_D30_REGNUM)
1523 regcache->cooked_write (regnum + 1, valbuf + 8);
1524 if (regnum < gdbarch_num_regs (gdbarch) + SPARC64_D10_REGNUM)
1525 regcache->cooked_write (SPARC_O0_REGNUM + element + 1,
1526 valbuf + 8);
1527 }
1528 }
1529 }
1530 else if (sparc64_floating_p (type))
1531 {
1532 /* Floating arguments. */
1533 if (len == 16)
1534 {
1535 if (element % 2)
1536 element++;
1537 if (element < 16)
1538 regnum = gdbarch_num_regs (gdbarch) + SPARC64_Q0_REGNUM
1539 + element / 2;
1540 }
1541 else if (len == 8)
1542 {
1543 if (element < 16)
1544 regnum = gdbarch_num_regs (gdbarch) + SPARC64_D0_REGNUM
1545 + element;
1546 }
1547 else if (len == 4)
1548 {
1549 /* The psABI says "Each single-precision parameter value
1550 will be assigned to one extended word in the
1551 parameter array, and right-justified within that
1552 word; the left half (even float register) is
1553 undefined." Even though the psABI says that "the
1554 left half is undefined", set it to zero here. */
1555 memset (buf, 0, 4);
1556 memcpy (buf + 4, valbuf, 4);
1557 valbuf = buf;
1558 len = 8;
1559 if (element < 16)
1560 regnum = gdbarch_num_regs (gdbarch) + SPARC64_D0_REGNUM
1561 + element;
1562 }
1563 }
1564 else
1565 {
1566 /* Integral and pointer arguments. */
1567 gdb_assert (len == 8);
1568 if (element < 6)
1569 regnum = SPARC_O0_REGNUM + element;
1570 }
1571
1572 if (regnum != -1)
1573 {
1574 regcache->cooked_write (regnum, valbuf);
1575
1576 /* If we're storing the value in a floating-point register,
1577 also store it in the corresponding %0 register(s). */
1578 if (regnum >= gdbarch_num_regs (gdbarch))
1579 {
1580 regnum -= gdbarch_num_regs (gdbarch);
1581
1582 if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D10_REGNUM)
1583 {
1584 gdb_assert (element < 6);
1585 regnum = SPARC_O0_REGNUM + element;
1586 regcache->cooked_write (regnum, valbuf);
1587 }
1588 else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q8_REGNUM)
1589 {
1590 gdb_assert (element < 5);
1591 regnum = SPARC_O0_REGNUM + element;
1592 regcache->cooked_write (regnum, valbuf);
1593 regcache->cooked_write (regnum + 1, valbuf + 8);
1594 }
1595 }
1596 }
1597
1598 /* Always store the argument in memory. */
1599 write_memory (sp + element * 8, valbuf, len);
1600 element += ((len + 7) / 8);
1601 }
1602
1603 gdb_assert (element == num_elements);
1604
1605 /* Take BIAS into account. */
1606 sp -= BIAS;
1607 return sp;
1608 }
1609
1610 static CORE_ADDR
1611 sparc64_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
1612 {
1613 /* The ABI requires 16-byte alignment. */
1614 return address & ~0xf;
1615 }
1616
1617 static CORE_ADDR
1618 sparc64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
1619 struct regcache *regcache, CORE_ADDR bp_addr,
1620 int nargs, struct value **args, CORE_ADDR sp,
1621 function_call_return_method return_method,
1622 CORE_ADDR struct_addr)
1623 {
1624 /* Set return address. */
1625 regcache_cooked_write_unsigned (regcache, SPARC_O7_REGNUM, bp_addr - 8);
1626
1627 /* Set up function arguments. */
1628 sp = sparc64_store_arguments (regcache, nargs, args, sp, return_method,
1629 struct_addr);
1630
1631 /* Allocate the register save area. */
1632 sp -= 16 * 8;
1633
1634 /* Stack should be 16-byte aligned at this point. */
1635 gdb_assert ((sp + BIAS) % 16 == 0);
1636
1637 /* Finally, update the stack pointer. */
1638 regcache_cooked_write_unsigned (regcache, SPARC_SP_REGNUM, sp);
1639
1640 return sp + BIAS;
1641 }
1642 \f
1643
1644 /* Extract from an array REGBUF containing the (raw) register state, a
1645 function return value of TYPE, and copy that into VALBUF. */
1646
1647 static void
1648 sparc64_extract_return_value (struct type *type, struct regcache *regcache,
1649 gdb_byte *valbuf)
1650 {
1651 int len = type->length ();
1652 gdb_byte buf[32];
1653 int i;
1654
1655 if (sparc64_structure_or_union_p (type))
1656 {
1657 /* Structure or Union return values. */
1658 gdb_assert (len <= 32);
1659
1660 for (i = 0; i < ((len + 7) / 8); i++)
1661 regcache->cooked_read (SPARC_O0_REGNUM + i, buf + i * 8);
1662 if (type->code () != TYPE_CODE_UNION)
1663 sparc64_extract_floating_fields (regcache, type, buf, 0);
1664 memcpy (valbuf, buf, len);
1665 }
1666 else if (sparc64_floating_p (type) || sparc64_complex_floating_p (type))
1667 {
1668 /* Floating return values. */
1669 for (i = 0; i < len / 4; i++)
1670 regcache->cooked_read (SPARC_F0_REGNUM + i, buf + i * 4);
1671 memcpy (valbuf, buf, len);
1672 }
1673 else if (type->code () == TYPE_CODE_ARRAY)
1674 {
1675 /* Small arrays are returned the same way as small structures. */
1676 gdb_assert (len <= 32);
1677
1678 for (i = 0; i < ((len + 7) / 8); i++)
1679 regcache->cooked_read (SPARC_O0_REGNUM + i, buf + i * 8);
1680 memcpy (valbuf, buf, len);
1681 }
1682 else
1683 {
1684 /* Integral and pointer return values. */
1685 gdb_assert (sparc64_integral_or_pointer_p (type));
1686
1687 /* Just stripping off any unused bytes should preserve the
1688 signed-ness just fine. */
1689 regcache->cooked_read (SPARC_O0_REGNUM, buf);
1690 memcpy (valbuf, buf + 8 - len, len);
1691 }
1692 }
1693
1694 /* Write into the appropriate registers a function return value stored
1695 in VALBUF of type TYPE. */
1696
1697 static void
1698 sparc64_store_return_value (struct type *type, struct regcache *regcache,
1699 const gdb_byte *valbuf)
1700 {
1701 int len = type->length ();
1702 gdb_byte buf[16];
1703 int i;
1704
1705 if (sparc64_structure_or_union_p (type))
1706 {
1707 /* Structure or Union return values. */
1708 gdb_assert (len <= 32);
1709
1710 /* Simplify matters by storing the complete value (including
1711 floating members) into %o0 and %o1. Floating members are
1712 also store in the appropriate floating-point registers. */
1713 memset (buf, 0, sizeof (buf));
1714 memcpy (buf, valbuf, len);
1715 for (i = 0; i < ((len + 7) / 8); i++)
1716 regcache->cooked_write (SPARC_O0_REGNUM + i, buf + i * 8);
1717 if (type->code () != TYPE_CODE_UNION)
1718 sparc64_store_floating_fields (regcache, type, buf, 0, 0);
1719 }
1720 else if (sparc64_floating_p (type) || sparc64_complex_floating_p (type))
1721 {
1722 /* Floating return values. */
1723 memcpy (buf, valbuf, len);
1724 for (i = 0; i < len / 4; i++)
1725 regcache->cooked_write (SPARC_F0_REGNUM + i, buf + i * 4);
1726 }
1727 else if (type->code () == TYPE_CODE_ARRAY)
1728 {
1729 /* Small arrays are returned the same way as small structures. */
1730 gdb_assert (len <= 32);
1731
1732 memset (buf, 0, sizeof (buf));
1733 memcpy (buf, valbuf, len);
1734 for (i = 0; i < ((len + 7) / 8); i++)
1735 regcache->cooked_write (SPARC_O0_REGNUM + i, buf + i * 8);
1736 }
1737 else
1738 {
1739 /* Integral and pointer return values. */
1740 gdb_assert (sparc64_integral_or_pointer_p (type));
1741
1742 /* ??? Do we need to do any sign-extension here? */
1743 memset (buf, 0, 8);
1744 memcpy (buf + 8 - len, valbuf, len);
1745 regcache->cooked_write (SPARC_O0_REGNUM, buf);
1746 }
1747 }
1748
1749 static enum return_value_convention
1750 sparc64_return_value (struct gdbarch *gdbarch, struct value *function,
1751 struct type *type, struct regcache *regcache,
1752 gdb_byte *readbuf, const gdb_byte *writebuf)
1753 {
1754 if (type->length () > 32)
1755 return RETURN_VALUE_STRUCT_CONVENTION;
1756
1757 if (readbuf)
1758 sparc64_extract_return_value (type, regcache, readbuf);
1759 if (writebuf)
1760 sparc64_store_return_value (type, regcache, writebuf);
1761
1762 return RETURN_VALUE_REGISTER_CONVENTION;
1763 }
1764 \f
1765
1766 static void
1767 sparc64_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
1768 struct dwarf2_frame_state_reg *reg,
1769 frame_info_ptr this_frame)
1770 {
1771 switch (regnum)
1772 {
1773 case SPARC_G0_REGNUM:
1774 /* Since %g0 is always zero, there is no point in saving it, and
1775 people will be inclined omit it from the CFI. Make sure we
1776 don't warn about that. */
1777 reg->how = DWARF2_FRAME_REG_SAME_VALUE;
1778 break;
1779 case SPARC_SP_REGNUM:
1780 reg->how = DWARF2_FRAME_REG_CFA;
1781 break;
1782 case SPARC64_PC_REGNUM:
1783 reg->how = DWARF2_FRAME_REG_RA_OFFSET;
1784 reg->loc.offset = 8;
1785 break;
1786 case SPARC64_NPC_REGNUM:
1787 reg->how = DWARF2_FRAME_REG_RA_OFFSET;
1788 reg->loc.offset = 12;
1789 break;
1790 }
1791 }
1792
1793 /* sparc64_addr_bits_remove - remove useless address bits */
1794
1795 static CORE_ADDR
1796 sparc64_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR addr)
1797 {
1798 return adi_normalize_address (addr);
1799 }
1800
1801 void
1802 sparc64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1803 {
1804 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (gdbarch);
1805
1806 tdep->pc_regnum = SPARC64_PC_REGNUM;
1807 tdep->npc_regnum = SPARC64_NPC_REGNUM;
1808 tdep->fpu_register_names = sparc64_fpu_register_names;
1809 tdep->fpu_registers_num = ARRAY_SIZE (sparc64_fpu_register_names);
1810 tdep->cp0_register_names = sparc64_cp0_register_names;
1811 tdep->cp0_registers_num = ARRAY_SIZE (sparc64_cp0_register_names);
1812
1813 /* This is what all the fuss is about. */
1814 set_gdbarch_long_bit (gdbarch, 64);
1815 set_gdbarch_long_long_bit (gdbarch, 64);
1816 set_gdbarch_ptr_bit (gdbarch, 64);
1817
1818 set_gdbarch_wchar_bit (gdbarch, 16);
1819 set_gdbarch_wchar_signed (gdbarch, 0);
1820
1821 set_gdbarch_num_regs (gdbarch, SPARC64_NUM_REGS);
1822 set_gdbarch_register_name (gdbarch, sparc64_register_name);
1823 set_gdbarch_register_type (gdbarch, sparc64_register_type);
1824 set_gdbarch_num_pseudo_regs (gdbarch, SPARC64_NUM_PSEUDO_REGS);
1825 set_tdesc_pseudo_register_name (gdbarch, sparc64_pseudo_register_name);
1826 set_tdesc_pseudo_register_type (gdbarch, sparc64_pseudo_register_type);
1827 set_gdbarch_pseudo_register_read (gdbarch, sparc64_pseudo_register_read);
1828 set_gdbarch_pseudo_register_write (gdbarch, sparc64_pseudo_register_write);
1829
1830 /* Register numbers of various important registers. */
1831 set_gdbarch_pc_regnum (gdbarch, SPARC64_PC_REGNUM); /* %pc */
1832
1833 /* Call dummy code. */
1834 set_gdbarch_frame_align (gdbarch, sparc64_frame_align);
1835 set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
1836 set_gdbarch_push_dummy_code (gdbarch, NULL);
1837 set_gdbarch_push_dummy_call (gdbarch, sparc64_push_dummy_call);
1838
1839 set_gdbarch_return_value (gdbarch, sparc64_return_value);
1840 set_gdbarch_return_value_as_value (gdbarch, default_gdbarch_return_value);
1841 set_gdbarch_stabs_argument_has_addr
1842 (gdbarch, default_stabs_argument_has_addr);
1843
1844 set_gdbarch_skip_prologue (gdbarch, sparc64_skip_prologue);
1845 set_gdbarch_stack_frame_destroyed_p (gdbarch, sparc_stack_frame_destroyed_p);
1846
1847 /* Hook in the DWARF CFI frame unwinder. */
1848 dwarf2_frame_set_init_reg (gdbarch, sparc64_dwarf2_frame_init_reg);
1849 /* FIXME: kettenis/20050423: Don't enable the unwinder until the
1850 StackGhost issues have been resolved. */
1851
1852 frame_unwind_append_unwinder (gdbarch, &sparc64_frame_unwind);
1853 frame_base_set_default (gdbarch, &sparc64_frame_base);
1854
1855 set_gdbarch_addr_bits_remove (gdbarch, sparc64_addr_bits_remove);
1856 }
1857 \f
1858
1859 /* Helper functions for dealing with register sets. */
1860
1861 #define TSTATE_CWP 0x000000000000001fULL
1862 #define TSTATE_ICC 0x0000000f00000000ULL
1863 #define TSTATE_XCC 0x000000f000000000ULL
1864
1865 #define PSR_S 0x00000080
1866 #ifndef PSR_ICC
1867 #define PSR_ICC 0x00f00000
1868 #endif
1869 #define PSR_VERS 0x0f000000
1870 #ifndef PSR_IMPL
1871 #define PSR_IMPL 0xf0000000
1872 #endif
1873 #define PSR_V8PLUS 0xff000000
1874 #define PSR_XCC 0x000f0000
1875
1876 void
1877 sparc64_supply_gregset (const struct sparc_gregmap *gregmap,
1878 struct regcache *regcache,
1879 int regnum, const void *gregs)
1880 {
1881 struct gdbarch *gdbarch = regcache->arch ();
1882 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1883 int sparc32 = (gdbarch_ptr_bit (gdbarch) == 32);
1884 const gdb_byte *regs = (const gdb_byte *) gregs;
1885 gdb_byte zero[8] = { 0 };
1886 int i;
1887
1888 if (sparc32)
1889 {
1890 if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
1891 {
1892 int offset = gregmap->r_tstate_offset;
1893 ULONGEST tstate, psr;
1894 gdb_byte buf[4];
1895
1896 tstate = extract_unsigned_integer (regs + offset, 8, byte_order);
1897 psr = ((tstate & TSTATE_CWP) | PSR_S | ((tstate & TSTATE_ICC) >> 12)
1898 | ((tstate & TSTATE_XCC) >> 20) | PSR_V8PLUS);
1899 store_unsigned_integer (buf, 4, byte_order, psr);
1900 regcache->raw_supply (SPARC32_PSR_REGNUM, buf);
1901 }
1902
1903 if (regnum == SPARC32_PC_REGNUM || regnum == -1)
1904 regcache->raw_supply (SPARC32_PC_REGNUM,
1905 regs + gregmap->r_pc_offset + 4);
1906
1907 if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
1908 regcache->raw_supply (SPARC32_NPC_REGNUM,
1909 regs + gregmap->r_npc_offset + 4);
1910
1911 if (regnum == SPARC32_Y_REGNUM || regnum == -1)
1912 {
1913 int offset = gregmap->r_y_offset + 8 - gregmap->r_y_size;
1914 regcache->raw_supply (SPARC32_Y_REGNUM, regs + offset);
1915 }
1916 }
1917 else
1918 {
1919 if (regnum == SPARC64_STATE_REGNUM || regnum == -1)
1920 regcache->raw_supply (SPARC64_STATE_REGNUM,
1921 regs + gregmap->r_tstate_offset);
1922
1923 if (regnum == SPARC64_PC_REGNUM || regnum == -1)
1924 regcache->raw_supply (SPARC64_PC_REGNUM,
1925 regs + gregmap->r_pc_offset);
1926
1927 if (regnum == SPARC64_NPC_REGNUM || regnum == -1)
1928 regcache->raw_supply (SPARC64_NPC_REGNUM,
1929 regs + gregmap->r_npc_offset);
1930
1931 if (regnum == SPARC64_Y_REGNUM || regnum == -1)
1932 {
1933 gdb_byte buf[8];
1934
1935 memset (buf, 0, 8);
1936 memcpy (buf + 8 - gregmap->r_y_size,
1937 regs + gregmap->r_y_offset, gregmap->r_y_size);
1938 regcache->raw_supply (SPARC64_Y_REGNUM, buf);
1939 }
1940
1941 if ((regnum == SPARC64_FPRS_REGNUM || regnum == -1)
1942 && gregmap->r_fprs_offset != -1)
1943 regcache->raw_supply (SPARC64_FPRS_REGNUM,
1944 regs + gregmap->r_fprs_offset);
1945 }
1946
1947 if (regnum == SPARC_G0_REGNUM || regnum == -1)
1948 regcache->raw_supply (SPARC_G0_REGNUM, &zero);
1949
1950 if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
1951 {
1952 int offset = gregmap->r_g1_offset;
1953
1954 if (sparc32)
1955 offset += 4;
1956
1957 for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
1958 {
1959 if (regnum == i || regnum == -1)
1960 regcache->raw_supply (i, regs + offset);
1961 offset += 8;
1962 }
1963 }
1964
1965 if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
1966 {
1967 /* Not all of the register set variants include Locals and
1968 Inputs. For those that don't, we read them off the stack. */
1969 if (gregmap->r_l0_offset == -1)
1970 {
1971 ULONGEST sp;
1972
1973 regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
1974 sparc_supply_rwindow (regcache, sp, regnum);
1975 }
1976 else
1977 {
1978 int offset = gregmap->r_l0_offset;
1979
1980 if (sparc32)
1981 offset += 4;
1982
1983 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1984 {
1985 if (regnum == i || regnum == -1)
1986 regcache->raw_supply (i, regs + offset);
1987 offset += 8;
1988 }
1989 }
1990 }
1991 }
1992
1993 void
1994 sparc64_collect_gregset (const struct sparc_gregmap *gregmap,
1995 const struct regcache *regcache,
1996 int regnum, void *gregs)
1997 {
1998 struct gdbarch *gdbarch = regcache->arch ();
1999 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2000 int sparc32 = (gdbarch_ptr_bit (gdbarch) == 32);
2001 gdb_byte *regs = (gdb_byte *) gregs;
2002 int i;
2003
2004 if (sparc32)
2005 {
2006 if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
2007 {
2008 int offset = gregmap->r_tstate_offset;
2009 ULONGEST tstate, psr;
2010 gdb_byte buf[8];
2011
2012 tstate = extract_unsigned_integer (regs + offset, 8, byte_order);
2013 regcache->raw_collect (SPARC32_PSR_REGNUM, buf);
2014 psr = extract_unsigned_integer (buf, 4, byte_order);
2015 tstate |= (psr & PSR_ICC) << 12;
2016 if ((psr & (PSR_VERS | PSR_IMPL)) == PSR_V8PLUS)
2017 tstate |= (psr & PSR_XCC) << 20;
2018 store_unsigned_integer (buf, 8, byte_order, tstate);
2019 memcpy (regs + offset, buf, 8);
2020 }
2021
2022 if (regnum == SPARC32_PC_REGNUM || regnum == -1)
2023 regcache->raw_collect (SPARC32_PC_REGNUM,
2024 regs + gregmap->r_pc_offset + 4);
2025
2026 if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
2027 regcache->raw_collect (SPARC32_NPC_REGNUM,
2028 regs + gregmap->r_npc_offset + 4);
2029
2030 if (regnum == SPARC32_Y_REGNUM || regnum == -1)
2031 {
2032 int offset = gregmap->r_y_offset + 8 - gregmap->r_y_size;
2033 regcache->raw_collect (SPARC32_Y_REGNUM, regs + offset);
2034 }
2035 }
2036 else
2037 {
2038 if (regnum == SPARC64_STATE_REGNUM || regnum == -1)
2039 regcache->raw_collect (SPARC64_STATE_REGNUM,
2040 regs + gregmap->r_tstate_offset);
2041
2042 if (regnum == SPARC64_PC_REGNUM || regnum == -1)
2043 regcache->raw_collect (SPARC64_PC_REGNUM,
2044 regs + gregmap->r_pc_offset);
2045
2046 if (regnum == SPARC64_NPC_REGNUM || regnum == -1)
2047 regcache->raw_collect (SPARC64_NPC_REGNUM,
2048 regs + gregmap->r_npc_offset);
2049
2050 if (regnum == SPARC64_Y_REGNUM || regnum == -1)
2051 {
2052 gdb_byte buf[8];
2053
2054 regcache->raw_collect (SPARC64_Y_REGNUM, buf);
2055 memcpy (regs + gregmap->r_y_offset,
2056 buf + 8 - gregmap->r_y_size, gregmap->r_y_size);
2057 }
2058
2059 if ((regnum == SPARC64_FPRS_REGNUM || regnum == -1)
2060 && gregmap->r_fprs_offset != -1)
2061 regcache->raw_collect (SPARC64_FPRS_REGNUM,
2062 regs + gregmap->r_fprs_offset);
2063
2064 }
2065
2066 if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
2067 {
2068 int offset = gregmap->r_g1_offset;
2069
2070 if (sparc32)
2071 offset += 4;
2072
2073 /* %g0 is always zero. */
2074 for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
2075 {
2076 if (regnum == i || regnum == -1)
2077 regcache->raw_collect (i, regs + offset);
2078 offset += 8;
2079 }
2080 }
2081
2082 if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
2083 {
2084 /* Not all of the register set variants include Locals and
2085 Inputs. For those that don't, we read them off the stack. */
2086 if (gregmap->r_l0_offset != -1)
2087 {
2088 int offset = gregmap->r_l0_offset;
2089
2090 if (sparc32)
2091 offset += 4;
2092
2093 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
2094 {
2095 if (regnum == i || regnum == -1)
2096 regcache->raw_collect (i, regs + offset);
2097 offset += 8;
2098 }
2099 }
2100 }
2101 }
2102
2103 void
2104 sparc64_supply_fpregset (const struct sparc_fpregmap *fpregmap,
2105 struct regcache *regcache,
2106 int regnum, const void *fpregs)
2107 {
2108 int sparc32 = (gdbarch_ptr_bit (regcache->arch ()) == 32);
2109 const gdb_byte *regs = (const gdb_byte *) fpregs;
2110 int i;
2111
2112 for (i = 0; i < 32; i++)
2113 {
2114 if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
2115 regcache->raw_supply (SPARC_F0_REGNUM + i,
2116 regs + fpregmap->r_f0_offset + (i * 4));
2117 }
2118
2119 if (sparc32)
2120 {
2121 if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
2122 regcache->raw_supply (SPARC32_FSR_REGNUM,
2123 regs + fpregmap->r_fsr_offset);
2124 }
2125 else
2126 {
2127 for (i = 0; i < 16; i++)
2128 {
2129 if (regnum == (SPARC64_F32_REGNUM + i) || regnum == -1)
2130 regcache->raw_supply
2131 (SPARC64_F32_REGNUM + i,
2132 regs + fpregmap->r_f0_offset + (32 * 4) + (i * 8));
2133 }
2134
2135 if (regnum == SPARC64_FSR_REGNUM || regnum == -1)
2136 regcache->raw_supply (SPARC64_FSR_REGNUM,
2137 regs + fpregmap->r_fsr_offset);
2138 }
2139 }
2140
2141 void
2142 sparc64_collect_fpregset (const struct sparc_fpregmap *fpregmap,
2143 const struct regcache *regcache,
2144 int regnum, void *fpregs)
2145 {
2146 int sparc32 = (gdbarch_ptr_bit (regcache->arch ()) == 32);
2147 gdb_byte *regs = (gdb_byte *) fpregs;
2148 int i;
2149
2150 for (i = 0; i < 32; i++)
2151 {
2152 if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
2153 regcache->raw_collect (SPARC_F0_REGNUM + i,
2154 regs + fpregmap->r_f0_offset + (i * 4));
2155 }
2156
2157 if (sparc32)
2158 {
2159 if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
2160 regcache->raw_collect (SPARC32_FSR_REGNUM,
2161 regs + fpregmap->r_fsr_offset);
2162 }
2163 else
2164 {
2165 for (i = 0; i < 16; i++)
2166 {
2167 if (regnum == (SPARC64_F32_REGNUM + i) || regnum == -1)
2168 regcache->raw_collect (SPARC64_F32_REGNUM + i,
2169 (regs + fpregmap->r_f0_offset
2170 + (32 * 4) + (i * 8)));
2171 }
2172
2173 if (regnum == SPARC64_FSR_REGNUM || regnum == -1)
2174 regcache->raw_collect (SPARC64_FSR_REGNUM,
2175 regs + fpregmap->r_fsr_offset);
2176 }
2177 }
2178
2179 const struct sparc_fpregmap sparc64_bsd_fpregmap =
2180 {
2181 0 * 8, /* %f0 */
2182 32 * 8, /* %fsr */
2183 };