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