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