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