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