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