Home | History | Annotate | Line # | Download | only in gdb
sparc64-tdep.c revision 1.1.1.6
      1 /* Target-dependent code for UltraSPARC.
      2 
      3    Copyright (C) 2003-2019 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 
     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 typedef struct sparc64_adi_info
    117 {
    118   sparc64_adi_info (pid_t pid_)
    119     : pid (pid_)
    120   {}
    121 
    122   /* The process identifier.  */
    123   pid_t pid;
    124 
    125   /* The ADI stat.  */
    126   adi_stat_t stat = {};
    127 
    128 } sparc64_adi_info;
    129 
    130 static std::forward_list<sparc64_adi_info> adi_proc_list;
    131 
    132 
    133 /* Get ADI info for process PID, creating one if it doesn't exist.  */
    134 
    135 static sparc64_adi_info *
    136 get_adi_info_proc (pid_t pid)
    137 {
    138   auto found = std::find_if (adi_proc_list.begin (), adi_proc_list.end (),
    139                              [&pid] (const sparc64_adi_info &info)
    140                              {
    141                                return info.pid == pid;
    142                              });
    143 
    144   if (found == adi_proc_list.end ())
    145     {
    146       adi_proc_list.emplace_front (pid);
    147       return &adi_proc_list.front ();
    148     }
    149   else
    150     {
    151       return &(*found);
    152     }
    153 }
    154 
    155 static adi_stat_t
    156 get_adi_info (pid_t pid)
    157 {
    158   sparc64_adi_info *proc;
    159 
    160   proc = get_adi_info_proc (pid);
    161   return proc->stat;
    162 }
    163 
    164 /* Is called when GDB is no longer debugging process PID.  It
    165    deletes data structure that keeps track of the ADI stat.  */
    166 
    167 void
    168 sparc64_forget_process (pid_t pid)
    169 {
    170   int target_errno;
    171 
    172   for (auto pit = adi_proc_list.before_begin (),
    173 	 it = std::next (pit);
    174        it != adi_proc_list.end ();
    175        )
    176     {
    177       if ((*it).pid == pid)
    178 	{
    179           if ((*it).stat.tag_fd > 0)
    180             target_fileio_close ((*it).stat.tag_fd, &target_errno);
    181 	  adi_proc_list.erase_after (pit);
    182           break;
    183 	}
    184       else
    185 	pit = it++;
    186     }
    187 
    188 }
    189 
    190 static void
    191 info_adi_command (const char *args, int from_tty)
    192 {
    193   printf_unfiltered ("\"adi\" must be followed by \"examine\" "
    194                      "or \"assign\".\n");
    195   help_list (sparc64adilist, "adi ", all_commands, gdb_stdout);
    196 }
    197 
    198 /* Read attributes of a maps entry in /proc/[pid]/adi/maps.  */
    199 
    200 static void
    201 read_maps_entry (const char *line,
    202               ULONGEST *addr, ULONGEST *endaddr)
    203 {
    204   const char *p = line;
    205 
    206   *addr = strtoulst (p, &p, 16);
    207   if (*p == '-')
    208     p++;
    209 
    210   *endaddr = strtoulst (p, &p, 16);
    211 }
    212 
    213 /* Check if ADI is available.  */
    214 
    215 static bool
    216 adi_available (void)
    217 {
    218   pid_t pid = inferior_ptid.pid ();
    219   sparc64_adi_info *proc = get_adi_info_proc (pid);
    220   CORE_ADDR value;
    221 
    222   if (proc->stat.checked_avail)
    223     return proc->stat.is_avail;
    224 
    225   proc->stat.checked_avail = true;
    226   if (target_auxv_search (current_top_target (), AT_ADI_BLKSZ, &value) <= 0)
    227     return false;
    228   proc->stat.blksize = value;
    229   target_auxv_search (current_top_target (), AT_ADI_NBITS, &value);
    230   proc->stat.nbits = value;
    231   proc->stat.max_version = (1 << proc->stat.nbits) - 2;
    232   proc->stat.is_avail = true;
    233 
    234   return proc->stat.is_avail;
    235 }
    236 
    237 /* Normalize a versioned address - a VA with ADI bits (63-60) set.  */
    238 
    239 static CORE_ADDR
    240 adi_normalize_address (CORE_ADDR addr)
    241 {
    242   adi_stat_t ast = get_adi_info (inferior_ptid.pid ());
    243 
    244   if (ast.nbits)
    245     {
    246       /* Clear upper bits.  */
    247       addr &= ((uint64_t) -1) >> ast.nbits;
    248 
    249       /* Sign extend.  */
    250       CORE_ADDR signbit = (uint64_t) 1 << (64 - ast.nbits - 1);
    251       return (addr ^ signbit) - signbit;
    252     }
    253   return addr;
    254 }
    255 
    256 /* Align a normalized address - a VA with bit 59 sign extended into
    257    ADI bits.  */
    258 
    259 static CORE_ADDR
    260 adi_align_address (CORE_ADDR naddr)
    261 {
    262   adi_stat_t ast = get_adi_info (inferior_ptid.pid ());
    263 
    264   return (naddr - (naddr % ast.blksize)) / ast.blksize;
    265 }
    266 
    267 /* Convert a byte count to count at a ratio of 1:adi_blksz.  */
    268 
    269 static int
    270 adi_convert_byte_count (CORE_ADDR naddr, int nbytes, CORE_ADDR locl)
    271 {
    272   adi_stat_t ast = get_adi_info (inferior_ptid.pid ());
    273 
    274   return ((naddr + nbytes + ast.blksize - 1) / ast.blksize) - locl;
    275 }
    276 
    277 /* The /proc/[pid]/adi/tags file, which allows gdb to get/set ADI
    278    version in a target process, maps linearly to the address space
    279    of the target process at a ratio of 1:adi_blksz.
    280 
    281    A read (or write) at offset K in the file returns (or modifies)
    282    the ADI version tag stored in the cacheline containing address
    283    K * adi_blksz, encoded as 1 version tag per byte.  The allowed
    284    version tag values are between 0 and adi_stat.max_version.  */
    285 
    286 static int
    287 adi_tag_fd (void)
    288 {
    289   pid_t pid = inferior_ptid.pid ();
    290   sparc64_adi_info *proc = get_adi_info_proc (pid);
    291 
    292   if (proc->stat.tag_fd != 0)
    293     return proc->stat.tag_fd;
    294 
    295   char cl_name[MAX_PROC_NAME_SIZE];
    296   snprintf (cl_name, sizeof(cl_name), "/proc/%ld/adi/tags", (long) pid);
    297   int target_errno;
    298   proc->stat.tag_fd = target_fileio_open (NULL, cl_name, O_RDWR|O_EXCL,
    299                                           0, &target_errno);
    300   return proc->stat.tag_fd;
    301 }
    302 
    303 /* Check if an address set is ADI enabled, using /proc/[pid]/adi/maps
    304    which was exported by the kernel and contains the currently ADI
    305    mapped memory regions and their access permissions.  */
    306 
    307 static bool
    308 adi_is_addr_mapped (CORE_ADDR vaddr, size_t cnt)
    309 {
    310   char filename[MAX_PROC_NAME_SIZE];
    311   size_t i = 0;
    312 
    313   pid_t pid = inferior_ptid.pid ();
    314   snprintf (filename, sizeof filename, "/proc/%ld/adi/maps", (long) pid);
    315   gdb::unique_xmalloc_ptr<char> data
    316     = target_fileio_read_stralloc (NULL, filename);
    317   if (data)
    318     {
    319       adi_stat_t adi_stat = get_adi_info (pid);
    320       char *line;
    321       for (line = strtok (data.get (), "\n"); line; line = strtok (NULL, "\n"))
    322         {
    323           ULONGEST addr, endaddr;
    324 
    325           read_maps_entry (line, &addr, &endaddr);
    326 
    327           while (((vaddr + i) * adi_stat.blksize) >= addr
    328                  && ((vaddr + i) * adi_stat.blksize) < endaddr)
    329             {
    330               if (++i == cnt)
    331 		return true;
    332             }
    333         }
    334       }
    335     else
    336       warning (_("unable to open /proc file '%s'"), filename);
    337 
    338   return false;
    339 }
    340 
    341 /* Read ADI version tag value for memory locations starting at "VADDR"
    342    for "SIZE" number of bytes.  */
    343 
    344 static int
    345 adi_read_versions (CORE_ADDR vaddr, size_t size, gdb_byte *tags)
    346 {
    347   int fd = adi_tag_fd ();
    348   if (fd == -1)
    349     return -1;
    350 
    351   if (!adi_is_addr_mapped (vaddr, size))
    352     {
    353       adi_stat_t ast = get_adi_info (inferior_ptid.pid ());
    354       error(_("Address at %s is not in ADI maps"),
    355             paddress (target_gdbarch (), vaddr * ast.blksize));
    356     }
    357 
    358   int target_errno;
    359   return target_fileio_pread (fd, tags, size, vaddr, &target_errno);
    360 }
    361 
    362 /* Write ADI version tag for memory locations starting at "VADDR" for
    363  "SIZE" number of bytes to "TAGS".  */
    364 
    365 static int
    366 adi_write_versions (CORE_ADDR vaddr, size_t size, unsigned char *tags)
    367 {
    368   int fd = adi_tag_fd ();
    369   if (fd == -1)
    370     return -1;
    371 
    372   if (!adi_is_addr_mapped (vaddr, size))
    373     {
    374       adi_stat_t ast = get_adi_info (inferior_ptid.pid ());
    375       error(_("Address at %s is not in ADI maps"),
    376             paddress (target_gdbarch (), vaddr * ast.blksize));
    377     }
    378 
    379   int target_errno;
    380   return target_fileio_pwrite (fd, tags, size, vaddr, &target_errno);
    381 }
    382 
    383 /* Print ADI version tag value in "TAGS" for memory locations starting
    384    at "VADDR" with number of "CNT".  */
    385 
    386 static void
    387 adi_print_versions (CORE_ADDR vaddr, size_t cnt, gdb_byte *tags)
    388 {
    389   int v_idx = 0;
    390   const int maxelts = 8;  /* # of elements per line */
    391 
    392   adi_stat_t adi_stat = get_adi_info (inferior_ptid.pid ());
    393 
    394   while (cnt > 0)
    395     {
    396       QUIT;
    397       printf_filtered ("%s:\t",
    398 	               paddress (target_gdbarch (), vaddr * adi_stat.blksize));
    399       for (int i = maxelts; i > 0 && cnt > 0; i--, cnt--)
    400         {
    401           if (tags[v_idx] == 0xff)    /* no version tag */
    402             printf_filtered ("-");
    403           else
    404             printf_filtered ("%1X", tags[v_idx]);
    405 	  if (cnt > 1)
    406             printf_filtered (" ");
    407           ++v_idx;
    408         }
    409       printf_filtered ("\n");
    410       gdb_flush (gdb_stdout);
    411       vaddr += maxelts;
    412     }
    413 }
    414 
    415 static void
    416 do_examine (CORE_ADDR start, int bcnt)
    417 {
    418   CORE_ADDR vaddr = adi_normalize_address (start);
    419 
    420   CORE_ADDR vstart = adi_align_address (vaddr);
    421   int cnt = adi_convert_byte_count (vaddr, bcnt, vstart);
    422   gdb::def_vector<gdb_byte> buf (cnt);
    423   int read_cnt = adi_read_versions (vstart, cnt, buf.data ());
    424   if (read_cnt == -1)
    425     error (_("No ADI information"));
    426   else if (read_cnt < cnt)
    427     error(_("No ADI information at %s"), paddress (target_gdbarch (), vaddr));
    428 
    429   adi_print_versions (vstart, cnt, buf.data ());
    430 }
    431 
    432 static void
    433 do_assign (CORE_ADDR start, size_t bcnt, int version)
    434 {
    435   CORE_ADDR vaddr = adi_normalize_address (start);
    436 
    437   CORE_ADDR vstart = adi_align_address (vaddr);
    438   int cnt = adi_convert_byte_count (vaddr, bcnt, vstart);
    439   std::vector<unsigned char> buf (cnt, version);
    440   int set_cnt = adi_write_versions (vstart, cnt, buf.data ());
    441 
    442   if (set_cnt == -1)
    443     error (_("No ADI information"));
    444   else if (set_cnt < cnt)
    445     error(_("No ADI information at %s"), paddress (target_gdbarch (), vaddr));
    446 
    447 }
    448 
    449 /* ADI examine version tag command.
    450 
    451    Command syntax:
    452 
    453      adi (examine|x)[/COUNT] [ADDR] */
    454 
    455 static void
    456 adi_examine_command (const char *args, int from_tty)
    457 {
    458   /* make sure program is active and adi is available */
    459   if (!target_has_execution)
    460     error (_("ADI command requires a live process/thread"));
    461 
    462   if (!adi_available ())
    463     error (_("No ADI information"));
    464 
    465   int cnt = 1;
    466   const char *p = args;
    467   if (p && *p == '/')
    468     {
    469       p++;
    470       cnt = get_number (&p);
    471     }
    472 
    473   CORE_ADDR next_address = 0;
    474   if (p != 0 && *p != 0)
    475     next_address = parse_and_eval_address (p);
    476   if (!cnt || !next_address)
    477     error (_("Usage: adi examine|x[/COUNT] [ADDR]"));
    478 
    479   do_examine (next_address, cnt);
    480 }
    481 
    482 /* ADI assign version tag command.
    483 
    484    Command syntax:
    485 
    486      adi (assign|a)[/COUNT] ADDR = VERSION  */
    487 
    488 static void
    489 adi_assign_command (const char *args, int from_tty)
    490 {
    491   static const char *adi_usage
    492     = N_("Usage: adi assign|a[/COUNT] ADDR = VERSION");
    493 
    494   /* make sure program is active and adi is available */
    495   if (!target_has_execution)
    496     error (_("ADI command requires a live process/thread"));
    497 
    498   if (!adi_available ())
    499     error (_("No ADI information"));
    500 
    501   const char *exp = args;
    502   if (exp == 0)
    503     error_no_arg (_(adi_usage));
    504 
    505   char *q = (char *) strchr (exp, '=');
    506   if (q)
    507     *q++ = 0;
    508   else
    509     error ("%s", _(adi_usage));
    510 
    511   size_t cnt = 1;
    512   const char *p = args;
    513   if (exp && *exp == '/')
    514     {
    515       p = exp + 1;
    516       cnt = get_number (&p);
    517     }
    518 
    519   CORE_ADDR next_address = 0;
    520   if (p != 0 && *p != 0)
    521     next_address = parse_and_eval_address (p);
    522   else
    523     error ("%s", _(adi_usage));
    524 
    525   int version = 0;
    526   if (q != NULL)           /* parse version tag */
    527     {
    528       adi_stat_t ast = get_adi_info (inferior_ptid.pid ());
    529       version = parse_and_eval_long (q);
    530       if (version < 0 || version > ast.max_version)
    531         error (_("Invalid ADI version tag %d"), version);
    532     }
    533 
    534   do_assign (next_address, cnt, version);
    535 }
    536 
    537 void
    538 _initialize_sparc64_adi_tdep (void)
    539 {
    540 
    541   add_prefix_cmd ("adi", class_support, info_adi_command,
    542                   _("ADI version related commands."),
    543                   &sparc64adilist, "adi ", 0, &cmdlist);
    544   add_cmd ("examine", class_support, adi_examine_command,
    545            _("Examine ADI versions."), &sparc64adilist);
    546   add_alias_cmd ("x", "examine", no_class, 1, &sparc64adilist);
    547   add_cmd ("assign", class_support, adi_assign_command,
    548            _("Assign ADI versions."), &sparc64adilist);
    549 
    550 }
    551 
    552 
    554 /* The functions on this page are intended to be used to classify
    555    function arguments.  */
    556 
    557 /* Check whether TYPE is "Integral or Pointer".  */
    558 
    559 static int
    560 sparc64_integral_or_pointer_p (const struct type *type)
    561 {
    562   switch (TYPE_CODE (type))
    563     {
    564     case TYPE_CODE_INT:
    565     case TYPE_CODE_BOOL:
    566     case TYPE_CODE_CHAR:
    567     case TYPE_CODE_ENUM:
    568     case TYPE_CODE_RANGE:
    569       {
    570 	int len = TYPE_LENGTH (type);
    571 	gdb_assert (len == 1 || len == 2 || len == 4 || len == 8);
    572       }
    573       return 1;
    574     case TYPE_CODE_PTR:
    575     case TYPE_CODE_REF:
    576     case TYPE_CODE_RVALUE_REF:
    577       {
    578 	int len = TYPE_LENGTH (type);
    579 	gdb_assert (len == 8);
    580       }
    581       return 1;
    582     default:
    583       break;
    584     }
    585 
    586   return 0;
    587 }
    588 
    589 /* Check whether TYPE is "Floating".  */
    590 
    591 static int
    592 sparc64_floating_p (const struct type *type)
    593 {
    594   switch (TYPE_CODE (type))
    595     {
    596     case TYPE_CODE_FLT:
    597       {
    598 	int len = TYPE_LENGTH (type);
    599 	gdb_assert (len == 4 || len == 8 || len == 16);
    600       }
    601       return 1;
    602     default:
    603       break;
    604     }
    605 
    606   return 0;
    607 }
    608 
    609 /* Check whether TYPE is "Complex Floating".  */
    610 
    611 static int
    612 sparc64_complex_floating_p (const struct type *type)
    613 {
    614   switch (TYPE_CODE (type))
    615     {
    616     case TYPE_CODE_COMPLEX:
    617       {
    618 	int len = TYPE_LENGTH (type);
    619 	gdb_assert (len == 8 || len == 16 || len == 32);
    620       }
    621       return 1;
    622     default:
    623       break;
    624     }
    625 
    626   return 0;
    627 }
    628 
    629 /* Check whether TYPE is "Structure or Union".
    630 
    631    In terms of Ada subprogram calls, arrays are treated the same as
    632    struct and union types.  So this function also returns non-zero
    633    for array types.  */
    634 
    635 static int
    636 sparc64_structure_or_union_p (const struct type *type)
    637 {
    638   switch (TYPE_CODE (type))
    639     {
    640     case TYPE_CODE_STRUCT:
    641     case TYPE_CODE_UNION:
    642     case TYPE_CODE_ARRAY:
    643       return 1;
    644     default:
    645       break;
    646     }
    647 
    648   return 0;
    649 }
    650 
    651 
    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 			      readable_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 (regnum, buf);
    918       if (status == REG_VALID)
    919 	status = regcache->raw_read (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 (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 (regnum, buf);
    932       if (status == REG_VALID)
    933 	status = regcache->raw_read (regnum + 1, buf + 4);
    934       if (status == REG_VALID)
    935 	status = regcache->raw_read (regnum + 2, buf + 8);
    936       if (status == REG_VALID)
    937 	status = regcache->raw_read (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 (regnum, buf);
    946       if (status == REG_VALID)
    947 	status = regcache->raw_read (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 (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 (regnum, buf);
    996       regcache->raw_write (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 (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 (regnum, buf);
   1007       regcache->raw_write (regnum + 1, buf + 4);
   1008       regcache->raw_write (regnum + 2, buf + 8);
   1009       regcache->raw_write (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 (regnum, buf);
   1015       regcache->raw_write (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 
   1045 
   1047 /* Return PC of first real instruction of the function starting at
   1048    START_PC.  */
   1049 
   1050 static CORE_ADDR
   1051 sparc64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
   1052 {
   1053   struct symtab_and_line sal;
   1054   CORE_ADDR func_start, func_end;
   1055   struct sparc_frame_cache cache;
   1056 
   1057   /* This is the preferred method, find the end of the prologue by
   1058      using the debugging information.  */
   1059   if (find_pc_partial_function (start_pc, NULL, &func_start, &func_end))
   1060     {
   1061       sal = find_pc_line (func_start, 0);
   1062 
   1063       if (sal.end < func_end
   1064 	  && start_pc <= sal.end)
   1065 	return sal.end;
   1066     }
   1067 
   1068   return sparc_analyze_prologue (gdbarch, start_pc, 0xffffffffffffffffULL,
   1069 				 &cache);
   1070 }
   1071 
   1072 /* Normal frames.  */
   1073 
   1074 static struct sparc_frame_cache *
   1075 sparc64_frame_cache (struct frame_info *this_frame, void **this_cache)
   1076 {
   1077   return sparc_frame_cache (this_frame, this_cache);
   1078 }
   1079 
   1080 static void
   1081 sparc64_frame_this_id (struct frame_info *this_frame, void **this_cache,
   1082 		       struct frame_id *this_id)
   1083 {
   1084   struct sparc_frame_cache *cache =
   1085     sparc64_frame_cache (this_frame, this_cache);
   1086 
   1087   /* This marks the outermost frame.  */
   1088   if (cache->base == 0)
   1089     return;
   1090 
   1091   (*this_id) = frame_id_build (cache->base, cache->pc);
   1092 }
   1093 
   1094 static struct value *
   1095 sparc64_frame_prev_register (struct frame_info *this_frame, void **this_cache,
   1096 			     int regnum)
   1097 {
   1098   struct gdbarch *gdbarch = get_frame_arch (this_frame);
   1099   struct sparc_frame_cache *cache =
   1100     sparc64_frame_cache (this_frame, this_cache);
   1101 
   1102   if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM)
   1103     {
   1104       CORE_ADDR pc = (regnum == SPARC64_NPC_REGNUM) ? 4 : 0;
   1105 
   1106       regnum =
   1107 	(cache->copied_regs_mask & 0x80) ? SPARC_I7_REGNUM : SPARC_O7_REGNUM;
   1108       pc += get_frame_register_unsigned (this_frame, regnum) + 8;
   1109       return frame_unwind_got_constant (this_frame, regnum, pc);
   1110     }
   1111 
   1112   /* Handle StackGhost.  */
   1113   {
   1114     ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
   1115 
   1116     if (wcookie != 0 && !cache->frameless_p && regnum == SPARC_I7_REGNUM)
   1117       {
   1118         CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 8;
   1119         ULONGEST i7;
   1120 
   1121         /* Read the value in from memory.  */
   1122         i7 = get_frame_memory_unsigned (this_frame, addr, 8);
   1123         return frame_unwind_got_constant (this_frame, regnum, i7 ^ wcookie);
   1124       }
   1125   }
   1126 
   1127   /* The previous frame's `local' and `in' registers may have been saved
   1128      in the register save area.  */
   1129   if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM
   1130       && (cache->saved_regs_mask & (1 << (regnum - SPARC_L0_REGNUM))))
   1131     {
   1132       CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 8;
   1133 
   1134       return frame_unwind_got_memory (this_frame, regnum, addr);
   1135     }
   1136 
   1137   /* The previous frame's `out' registers may be accessible as the current
   1138      frame's `in' registers.  */
   1139   if (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O7_REGNUM
   1140       && (cache->copied_regs_mask & (1 << (regnum - SPARC_O0_REGNUM))))
   1141     regnum += (SPARC_I0_REGNUM - SPARC_O0_REGNUM);
   1142 
   1143   return frame_unwind_got_register (this_frame, regnum, regnum);
   1144 }
   1145 
   1146 static const struct frame_unwind sparc64_frame_unwind =
   1147 {
   1148   NORMAL_FRAME,
   1149   default_frame_unwind_stop_reason,
   1150   sparc64_frame_this_id,
   1151   sparc64_frame_prev_register,
   1152   NULL,
   1153   default_frame_sniffer
   1154 };
   1155 
   1156 
   1158 static CORE_ADDR
   1159 sparc64_frame_base_address (struct frame_info *this_frame, void **this_cache)
   1160 {
   1161   struct sparc_frame_cache *cache =
   1162     sparc64_frame_cache (this_frame, this_cache);
   1163 
   1164   return cache->base;
   1165 }
   1166 
   1167 static const struct frame_base sparc64_frame_base =
   1168 {
   1169   &sparc64_frame_unwind,
   1170   sparc64_frame_base_address,
   1171   sparc64_frame_base_address,
   1172   sparc64_frame_base_address
   1173 };
   1174 
   1175 /* Check whether TYPE must be 16-byte aligned.  */
   1177 
   1178 static int
   1179 sparc64_16_byte_align_p (struct type *type)
   1180 {
   1181   if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
   1182     {
   1183       struct type *t = check_typedef (TYPE_TARGET_TYPE (type));
   1184 
   1185       if (sparc64_floating_p (t))
   1186         return 1;
   1187     }
   1188   if (sparc64_floating_p (type) && TYPE_LENGTH (type) == 16)
   1189     return 1;
   1190 
   1191   if (sparc64_structure_or_union_p (type))
   1192     {
   1193       int i;
   1194 
   1195       for (i = 0; i < TYPE_NFIELDS (type); i++)
   1196 	{
   1197 	  struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
   1198 
   1199 	  if (sparc64_16_byte_align_p (subtype))
   1200 	    return 1;
   1201 	}
   1202     }
   1203 
   1204   return 0;
   1205 }
   1206 
   1207 /* Store floating fields of element ELEMENT of an "parameter array"
   1208    that has type TYPE and is stored at BITPOS in VALBUF in the
   1209    apropriate registers of REGCACHE.  This function can be called
   1210    recursively and therefore handles floating types in addition to
   1211    structures.  */
   1212 
   1213 static void
   1214 sparc64_store_floating_fields (struct regcache *regcache, struct type *type,
   1215 			       const gdb_byte *valbuf, int element, int bitpos)
   1216 {
   1217   struct gdbarch *gdbarch = regcache->arch ();
   1218   int len = TYPE_LENGTH (type);
   1219 
   1220   gdb_assert (element < 16);
   1221 
   1222   if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
   1223     {
   1224       gdb_byte buf[8];
   1225       int regnum = SPARC_F0_REGNUM + element * 2 + bitpos / 32;
   1226 
   1227       valbuf += bitpos / 8;
   1228       if (len < 8)
   1229         {
   1230           memset (buf, 0, 8 - len);
   1231           memcpy (buf + 8 - len, valbuf, len);
   1232           valbuf = buf;
   1233           len = 8;
   1234         }
   1235       for (int n = 0; n < (len + 3) / 4; n++)
   1236         regcache->cooked_write (regnum + n, valbuf + n * 4);
   1237     }
   1238   else if (sparc64_floating_p (type)
   1239       || (sparc64_complex_floating_p (type) && len <= 16))
   1240     {
   1241       int regnum;
   1242 
   1243       if (len == 16)
   1244 	{
   1245 	  gdb_assert (bitpos == 0);
   1246 	  gdb_assert ((element % 2) == 0);
   1247 
   1248 	  regnum = gdbarch_num_regs (gdbarch) + SPARC64_Q0_REGNUM + element / 2;
   1249 	  regcache->cooked_write (regnum, valbuf);
   1250 	}
   1251       else if (len == 8)
   1252 	{
   1253 	  gdb_assert (bitpos == 0 || bitpos == 64);
   1254 
   1255 	  regnum = gdbarch_num_regs (gdbarch) + SPARC64_D0_REGNUM
   1256                    + element + bitpos / 64;
   1257 	  regcache->cooked_write (regnum, valbuf + (bitpos / 8));
   1258 	}
   1259       else
   1260 	{
   1261 	  gdb_assert (len == 4);
   1262 	  gdb_assert (bitpos % 32 == 0 && bitpos >= 0 && bitpos < 128);
   1263 
   1264 	  regnum = SPARC_F0_REGNUM + element * 2 + bitpos / 32;
   1265 	  regcache->cooked_write (regnum, valbuf + (bitpos / 8));
   1266 	}
   1267     }
   1268   else if (sparc64_structure_or_union_p (type))
   1269     {
   1270       int i;
   1271 
   1272       for (i = 0; i < TYPE_NFIELDS (type); i++)
   1273 	{
   1274 	  struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
   1275 	  int subpos = bitpos + TYPE_FIELD_BITPOS (type, i);
   1276 
   1277 	  sparc64_store_floating_fields (regcache, subtype, valbuf,
   1278 					 element, subpos);
   1279 	}
   1280 
   1281       /* GCC has an interesting bug.  If TYPE is a structure that has
   1282          a single `float' member, GCC doesn't treat it as a structure
   1283          at all, but rather as an ordinary `float' argument.  This
   1284          argument will be stored in %f1, as required by the psABI.
   1285          However, as a member of a structure the psABI requires it to
   1286          be stored in %f0.  This bug is present in GCC 3.3.2, but
   1287          probably in older releases to.  To appease GCC, if a
   1288          structure has only a single `float' member, we store its
   1289          value in %f1 too (we already have stored in %f0).  */
   1290       if (TYPE_NFIELDS (type) == 1)
   1291 	{
   1292 	  struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, 0));
   1293 
   1294 	  if (sparc64_floating_p (subtype) && TYPE_LENGTH (subtype) == 4)
   1295 	    regcache->cooked_write (SPARC_F1_REGNUM, valbuf);
   1296 	}
   1297     }
   1298 }
   1299 
   1300 /* Fetch floating fields from a variable of type TYPE from the
   1301    appropriate registers for BITPOS in REGCACHE and store it at BITPOS
   1302    in VALBUF.  This function can be called recursively and therefore
   1303    handles floating types in addition to structures.  */
   1304 
   1305 static void
   1306 sparc64_extract_floating_fields (struct regcache *regcache, struct type *type,
   1307 				 gdb_byte *valbuf, int bitpos)
   1308 {
   1309   struct gdbarch *gdbarch = regcache->arch ();
   1310 
   1311   if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
   1312     {
   1313       int len = TYPE_LENGTH (type);
   1314       int regnum =  SPARC_F0_REGNUM + bitpos / 32;
   1315 
   1316       valbuf += bitpos / 8;
   1317       if (len < 4)
   1318         {
   1319           gdb_byte buf[4];
   1320           regcache->cooked_read (regnum, buf);
   1321           memcpy (valbuf, buf + 4 - len, len);
   1322         }
   1323       else
   1324         for (int i = 0; i < (len + 3) / 4; i++)
   1325           regcache->cooked_read (regnum + i, valbuf + i * 4);
   1326     }
   1327   else if (sparc64_floating_p (type))
   1328     {
   1329       int len = TYPE_LENGTH (type);
   1330       int regnum;
   1331 
   1332       if (len == 16)
   1333 	{
   1334 	  gdb_assert (bitpos == 0 || bitpos == 128);
   1335 
   1336 	  regnum = gdbarch_num_regs (gdbarch) + SPARC64_Q0_REGNUM
   1337                    + bitpos / 128;
   1338 	  regcache->cooked_read (regnum, valbuf + (bitpos / 8));
   1339 	}
   1340       else if (len == 8)
   1341 	{
   1342 	  gdb_assert (bitpos % 64 == 0 && bitpos >= 0 && bitpos < 256);
   1343 
   1344 	  regnum = gdbarch_num_regs (gdbarch) + SPARC64_D0_REGNUM + bitpos / 64;
   1345 	  regcache->cooked_read (regnum, valbuf + (bitpos / 8));
   1346 	}
   1347       else
   1348 	{
   1349 	  gdb_assert (len == 4);
   1350 	  gdb_assert (bitpos % 32 == 0 && bitpos >= 0 && bitpos < 256);
   1351 
   1352 	  regnum = SPARC_F0_REGNUM + bitpos / 32;
   1353 	  regcache->cooked_read (regnum, valbuf + (bitpos / 8));
   1354 	}
   1355     }
   1356   else if (sparc64_structure_or_union_p (type))
   1357     {
   1358       int i;
   1359 
   1360       for (i = 0; i < TYPE_NFIELDS (type); i++)
   1361 	{
   1362 	  struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
   1363 	  int subpos = bitpos + TYPE_FIELD_BITPOS (type, i);
   1364 
   1365 	  sparc64_extract_floating_fields (regcache, subtype, valbuf, subpos);
   1366 	}
   1367     }
   1368 }
   1369 
   1370 /* Store the NARGS arguments ARGS and STRUCT_ADDR (if STRUCT_RETURN is
   1371    non-zero) in REGCACHE and on the stack (starting from address SP).  */
   1372 
   1373 static CORE_ADDR
   1374 sparc64_store_arguments (struct regcache *regcache, int nargs,
   1375 			 struct value **args, CORE_ADDR sp,
   1376 			 function_call_return_method return_method,
   1377 			 CORE_ADDR struct_addr)
   1378 {
   1379   struct gdbarch *gdbarch = regcache->arch ();
   1380   /* Number of extended words in the "parameter array".  */
   1381   int num_elements = 0;
   1382   int element = 0;
   1383   int i;
   1384 
   1385   /* Take BIAS into account.  */
   1386   sp += BIAS;
   1387 
   1388   /* First we calculate the number of extended words in the "parameter
   1389      array".  While doing so we also convert some of the arguments.  */
   1390 
   1391   if (return_method == return_method_struct)
   1392     num_elements++;
   1393 
   1394   for (i = 0; i < nargs; i++)
   1395     {
   1396       struct type *type = value_type (args[i]);
   1397       int len = TYPE_LENGTH (type);
   1398 
   1399       if (sparc64_structure_or_union_p (type)
   1400 	  || (sparc64_complex_floating_p (type) && len == 32))
   1401 	{
   1402 	  /* Structure or Union arguments.  */
   1403 	  if (len <= 16)
   1404 	    {
   1405 	      if (num_elements % 2 && sparc64_16_byte_align_p (type))
   1406 		num_elements++;
   1407 	      num_elements += ((len + 7) / 8);
   1408 	    }
   1409 	  else
   1410 	    {
   1411 	      /* The psABI says that "Structures or unions larger than
   1412 		 sixteen bytes are copied by the caller and passed
   1413 		 indirectly; the caller will pass the address of a
   1414 		 correctly aligned structure value.  This sixty-four
   1415 		 bit address will occupy one word in the parameter
   1416 		 array, and may be promoted to an %o register like any
   1417 		 other pointer value."  Allocate memory for these
   1418 		 values on the stack.  */
   1419 	      sp -= len;
   1420 
   1421 	      /* Use 16-byte alignment for these values.  That's
   1422                  always correct, and wasting a few bytes shouldn't be
   1423                  a problem.  */
   1424 	      sp &= ~0xf;
   1425 
   1426 	      write_memory (sp, value_contents (args[i]), len);
   1427 	      args[i] = value_from_pointer (lookup_pointer_type (type), sp);
   1428 	      num_elements++;
   1429 	    }
   1430 	}
   1431       else if (sparc64_floating_p (type) || sparc64_complex_floating_p (type))
   1432 	{
   1433 	  /* Floating arguments.  */
   1434 	  if (len == 16)
   1435 	    {
   1436 	      /* The psABI says that "Each quad-precision parameter
   1437                  value will be assigned to two extended words in the
   1438                  parameter array.  */
   1439 	      num_elements += 2;
   1440 
   1441 	      /* The psABI says that "Long doubles must be
   1442                  quad-aligned, and thus a hole might be introduced
   1443                  into the parameter array to force alignment."  Skip
   1444                  an element if necessary.  */
   1445 	      if ((num_elements % 2) && sparc64_16_byte_align_p (type))
   1446 		num_elements++;
   1447 	    }
   1448 	  else
   1449 	    num_elements++;
   1450 	}
   1451       else
   1452 	{
   1453 	  /* Integral and pointer arguments.  */
   1454 	  gdb_assert (sparc64_integral_or_pointer_p (type));
   1455 
   1456 	  /* The psABI says that "Each argument value of integral type
   1457 	     smaller than an extended word will be widened by the
   1458 	     caller to an extended word according to the signed-ness
   1459 	     of the argument type."  */
   1460 	  if (len < 8)
   1461 	    args[i] = value_cast (builtin_type (gdbarch)->builtin_int64,
   1462 				  args[i]);
   1463 	  num_elements++;
   1464 	}
   1465     }
   1466 
   1467   /* Allocate the "parameter array".  */
   1468   sp -= num_elements * 8;
   1469 
   1470   /* The psABI says that "Every stack frame must be 16-byte aligned."  */
   1471   sp &= ~0xf;
   1472 
   1473   /* Now we store the arguments in to the "paramater array".  Some
   1474      Integer or Pointer arguments and Structure or Union arguments
   1475      will be passed in %o registers.  Some Floating arguments and
   1476      floating members of structures are passed in floating-point
   1477      registers.  However, for functions with variable arguments,
   1478      floating arguments are stored in an %0 register, and for
   1479      functions without a prototype floating arguments are stored in
   1480      both a floating-point and an %o registers, or a floating-point
   1481      register and memory.  To simplify the logic here we always pass
   1482      arguments in memory, an %o register, and a floating-point
   1483      register if appropriate.  This should be no problem since the
   1484      contents of any unused memory or registers in the "parameter
   1485      array" are undefined.  */
   1486 
   1487   if (return_method == return_method_struct)
   1488     {
   1489       regcache_cooked_write_unsigned (regcache, SPARC_O0_REGNUM, struct_addr);
   1490       element++;
   1491     }
   1492 
   1493   for (i = 0; i < nargs; i++)
   1494     {
   1495       const gdb_byte *valbuf = value_contents (args[i]);
   1496       struct type *type = value_type (args[i]);
   1497       int len = TYPE_LENGTH (type);
   1498       int regnum = -1;
   1499       gdb_byte buf[16];
   1500 
   1501       if (sparc64_structure_or_union_p (type)
   1502 	  || (sparc64_complex_floating_p (type) && len == 32))
   1503 	{
   1504 	  /* Structure, Union or long double Complex arguments.  */
   1505 	  gdb_assert (len <= 16);
   1506 	  memset (buf, 0, sizeof (buf));
   1507 	  memcpy (buf, valbuf, len);
   1508 	  valbuf = buf;
   1509 
   1510 	  if (element % 2 && sparc64_16_byte_align_p (type))
   1511 	    element++;
   1512 
   1513 	  if (element < 6)
   1514 	    {
   1515 	      regnum = SPARC_O0_REGNUM + element;
   1516 	      if (len > 8 && element < 5)
   1517 		regcache->cooked_write (regnum + 1, valbuf + 8);
   1518 	    }
   1519 
   1520 	  if (element < 16)
   1521 	    sparc64_store_floating_fields (regcache, type, valbuf, element, 0);
   1522 	}
   1523       else if (sparc64_complex_floating_p (type))
   1524 	{
   1525 	  /* Float Complex or double Complex arguments.  */
   1526 	  if (element < 16)
   1527 	    {
   1528 	      regnum = gdbarch_num_regs (gdbarch) + SPARC64_D0_REGNUM + element;
   1529 
   1530 	      if (len == 16)
   1531 		{
   1532 		  if (regnum < gdbarch_num_regs (gdbarch) + SPARC64_D30_REGNUM)
   1533 		    regcache->cooked_write (regnum + 1, valbuf + 8);
   1534 		  if (regnum < gdbarch_num_regs (gdbarch) + SPARC64_D10_REGNUM)
   1535 		    regcache->cooked_write (SPARC_O0_REGNUM + element + 1,
   1536 					    valbuf + 8);
   1537 		}
   1538 	    }
   1539 	}
   1540       else if (sparc64_floating_p (type))
   1541 	{
   1542 	  /* Floating arguments.  */
   1543 	  if (len == 16)
   1544 	    {
   1545 	      if (element % 2)
   1546 		element++;
   1547 	      if (element < 16)
   1548 		regnum = gdbarch_num_regs (gdbarch) + SPARC64_Q0_REGNUM
   1549                          + element / 2;
   1550 	    }
   1551 	  else if (len == 8)
   1552 	    {
   1553 	      if (element < 16)
   1554 		regnum = gdbarch_num_regs (gdbarch) + SPARC64_D0_REGNUM
   1555                          + element;
   1556 	    }
   1557 	  else if (len == 4)
   1558 	    {
   1559 	      /* The psABI says "Each single-precision parameter value
   1560                  will be assigned to one extended word in the
   1561                  parameter array, and right-justified within that
   1562                  word; the left half (even float register) is
   1563                  undefined."  Even though the psABI says that "the
   1564                  left half is undefined", set it to zero here.  */
   1565 	      memset (buf, 0, 4);
   1566 	      memcpy (buf + 4, valbuf, 4);
   1567 	      valbuf = buf;
   1568 	      len = 8;
   1569 	      if (element < 16)
   1570 		regnum = gdbarch_num_regs (gdbarch) + SPARC64_D0_REGNUM
   1571                          + element;
   1572 	    }
   1573 	}
   1574       else
   1575 	{
   1576 	  /* Integral and pointer arguments.  */
   1577 	  gdb_assert (len == 8);
   1578 	  if (element < 6)
   1579 	    regnum = SPARC_O0_REGNUM + element;
   1580 	}
   1581 
   1582       if (regnum != -1)
   1583 	{
   1584 	  regcache->cooked_write (regnum, valbuf);
   1585 
   1586 	  /* If we're storing the value in a floating-point register,
   1587              also store it in the corresponding %0 register(s).  */
   1588 	  if (regnum >= gdbarch_num_regs (gdbarch))
   1589             {
   1590               regnum -= gdbarch_num_regs (gdbarch);
   1591 
   1592               if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D10_REGNUM)
   1593 	        {
   1594 	          gdb_assert (element < 6);
   1595 	          regnum = SPARC_O0_REGNUM + element;
   1596 	          regcache->cooked_write (regnum, valbuf);
   1597                 }
   1598               else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q8_REGNUM)
   1599                 {
   1600                   gdb_assert (element < 5);
   1601                   regnum = SPARC_O0_REGNUM + element;
   1602                   regcache->cooked_write (regnum, valbuf);
   1603                   regcache->cooked_write (regnum + 1, valbuf + 8);
   1604 	        }
   1605             }
   1606 	}
   1607 
   1608       /* Always store the argument in memory.  */
   1609       write_memory (sp + element * 8, valbuf, len);
   1610       element += ((len + 7) / 8);
   1611     }
   1612 
   1613   gdb_assert (element == num_elements);
   1614 
   1615   /* Take BIAS into account.  */
   1616   sp -= BIAS;
   1617   return sp;
   1618 }
   1619 
   1620 static CORE_ADDR
   1621 sparc64_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
   1622 {
   1623   /* The ABI requires 16-byte alignment.  */
   1624   return address & ~0xf;
   1625 }
   1626 
   1627 static CORE_ADDR
   1628 sparc64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
   1629 			 struct regcache *regcache, CORE_ADDR bp_addr,
   1630 			 int nargs, struct value **args, CORE_ADDR sp,
   1631 			 function_call_return_method return_method,
   1632 			 CORE_ADDR struct_addr)
   1633 {
   1634   /* Set return address.  */
   1635   regcache_cooked_write_unsigned (regcache, SPARC_O7_REGNUM, bp_addr - 8);
   1636 
   1637   /* Set up function arguments.  */
   1638   sp = sparc64_store_arguments (regcache, nargs, args, sp, return_method,
   1639 				struct_addr);
   1640 
   1641   /* Allocate the register save area.  */
   1642   sp -= 16 * 8;
   1643 
   1644   /* Stack should be 16-byte aligned at this point.  */
   1645   gdb_assert ((sp + BIAS) % 16 == 0);
   1646 
   1647   /* Finally, update the stack pointer.  */
   1648   regcache_cooked_write_unsigned (regcache, SPARC_SP_REGNUM, sp);
   1649 
   1650   return sp + BIAS;
   1651 }
   1652 
   1653 
   1655 /* Extract from an array REGBUF containing the (raw) register state, a
   1656    function return value of TYPE, and copy that into VALBUF.  */
   1657 
   1658 static void
   1659 sparc64_extract_return_value (struct type *type, struct regcache *regcache,
   1660 			      gdb_byte *valbuf)
   1661 {
   1662   int len = TYPE_LENGTH (type);
   1663   gdb_byte buf[32];
   1664   int i;
   1665 
   1666   if (sparc64_structure_or_union_p (type))
   1667     {
   1668       /* Structure or Union return values.  */
   1669       gdb_assert (len <= 32);
   1670 
   1671       for (i = 0; i < ((len + 7) / 8); i++)
   1672 	regcache->cooked_read (SPARC_O0_REGNUM + i, buf + i * 8);
   1673       if (TYPE_CODE (type) != TYPE_CODE_UNION)
   1674 	sparc64_extract_floating_fields (regcache, type, buf, 0);
   1675       memcpy (valbuf, buf, len);
   1676     }
   1677   else if (sparc64_floating_p (type) || sparc64_complex_floating_p (type))
   1678     {
   1679       /* Floating return values.  */
   1680       for (i = 0; i < len / 4; i++)
   1681 	regcache->cooked_read (SPARC_F0_REGNUM + i, buf + i * 4);
   1682       memcpy (valbuf, buf, len);
   1683     }
   1684   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
   1685     {
   1686       /* Small arrays are returned the same way as small structures.  */
   1687       gdb_assert (len <= 32);
   1688 
   1689       for (i = 0; i < ((len + 7) / 8); i++)
   1690 	regcache->cooked_read (SPARC_O0_REGNUM + i, buf + i * 8);
   1691       memcpy (valbuf, buf, len);
   1692     }
   1693   else
   1694     {
   1695       /* Integral and pointer return values.  */
   1696       gdb_assert (sparc64_integral_or_pointer_p (type));
   1697 
   1698       /* Just stripping off any unused bytes should preserve the
   1699          signed-ness just fine.  */
   1700       regcache->cooked_read (SPARC_O0_REGNUM, buf);
   1701       memcpy (valbuf, buf + 8 - len, len);
   1702     }
   1703 }
   1704 
   1705 /* Write into the appropriate registers a function return value stored
   1706    in VALBUF of type TYPE.  */
   1707 
   1708 static void
   1709 sparc64_store_return_value (struct type *type, struct regcache *regcache,
   1710 			    const gdb_byte *valbuf)
   1711 {
   1712   int len = TYPE_LENGTH (type);
   1713   gdb_byte buf[16];
   1714   int i;
   1715 
   1716   if (sparc64_structure_or_union_p (type))
   1717     {
   1718       /* Structure or Union return values.  */
   1719       gdb_assert (len <= 32);
   1720 
   1721       /* Simplify matters by storing the complete value (including
   1722          floating members) into %o0 and %o1.  Floating members are
   1723          also store in the appropriate floating-point registers.  */
   1724       memset (buf, 0, sizeof (buf));
   1725       memcpy (buf, valbuf, len);
   1726       for (i = 0; i < ((len + 7) / 8); i++)
   1727 	regcache->cooked_write (SPARC_O0_REGNUM + i, buf + i * 8);
   1728       if (TYPE_CODE (type) != TYPE_CODE_UNION)
   1729 	sparc64_store_floating_fields (regcache, type, buf, 0, 0);
   1730     }
   1731   else if (sparc64_floating_p (type) || sparc64_complex_floating_p (type))
   1732     {
   1733       /* Floating return values.  */
   1734       memcpy (buf, valbuf, len);
   1735       for (i = 0; i < len / 4; i++)
   1736 	regcache->cooked_write (SPARC_F0_REGNUM + i, buf + i * 4);
   1737     }
   1738   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
   1739     {
   1740       /* Small arrays are returned the same way as small structures.  */
   1741       gdb_assert (len <= 32);
   1742 
   1743       memset (buf, 0, sizeof (buf));
   1744       memcpy (buf, valbuf, len);
   1745       for (i = 0; i < ((len + 7) / 8); i++)
   1746 	regcache->cooked_write (SPARC_O0_REGNUM + i, buf + i * 8);
   1747     }
   1748   else
   1749     {
   1750       /* Integral and pointer return values.  */
   1751       gdb_assert (sparc64_integral_or_pointer_p (type));
   1752 
   1753       /* ??? Do we need to do any sign-extension here?  */
   1754       memset (buf, 0, 8);
   1755       memcpy (buf + 8 - len, valbuf, len);
   1756       regcache->cooked_write (SPARC_O0_REGNUM, buf);
   1757     }
   1758 }
   1759 
   1760 static enum return_value_convention
   1761 sparc64_return_value (struct gdbarch *gdbarch, struct value *function,
   1762 		      struct type *type, struct regcache *regcache,
   1763 		      gdb_byte *readbuf, const gdb_byte *writebuf)
   1764 {
   1765   if (TYPE_LENGTH (type) > 32)
   1766     return RETURN_VALUE_STRUCT_CONVENTION;
   1767 
   1768   if (readbuf)
   1769     sparc64_extract_return_value (type, regcache, readbuf);
   1770   if (writebuf)
   1771     sparc64_store_return_value (type, regcache, writebuf);
   1772 
   1773   return RETURN_VALUE_REGISTER_CONVENTION;
   1774 }
   1775 
   1776 
   1778 static void
   1779 sparc64_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
   1780 			       struct dwarf2_frame_state_reg *reg,
   1781 			       struct frame_info *this_frame)
   1782 {
   1783   switch (regnum)
   1784     {
   1785     case SPARC_G0_REGNUM:
   1786       /* Since %g0 is always zero, there is no point in saving it, and
   1787 	 people will be inclined omit it from the CFI.  Make sure we
   1788 	 don't warn about that.  */
   1789       reg->how = DWARF2_FRAME_REG_SAME_VALUE;
   1790       break;
   1791     case SPARC_SP_REGNUM:
   1792       reg->how = DWARF2_FRAME_REG_CFA;
   1793       break;
   1794     case SPARC64_PC_REGNUM:
   1795       reg->how = DWARF2_FRAME_REG_RA_OFFSET;
   1796       reg->loc.offset = 8;
   1797       break;
   1798     case SPARC64_NPC_REGNUM:
   1799       reg->how = DWARF2_FRAME_REG_RA_OFFSET;
   1800       reg->loc.offset = 12;
   1801       break;
   1802     }
   1803 }
   1804 
   1805 /* sparc64_addr_bits_remove - remove useless address bits  */
   1806 
   1807 static CORE_ADDR
   1808 sparc64_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR addr)
   1809 {
   1810   return adi_normalize_address (addr);
   1811 }
   1812 
   1813 void
   1814 sparc64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   1815 {
   1816   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
   1817 
   1818   tdep->pc_regnum = SPARC64_PC_REGNUM;
   1819   tdep->npc_regnum = SPARC64_NPC_REGNUM;
   1820   tdep->fpu_register_names = sparc64_fpu_register_names;
   1821   tdep->fpu_registers_num = ARRAY_SIZE (sparc64_fpu_register_names);
   1822   tdep->cp0_register_names = sparc64_cp0_register_names;
   1823   tdep->cp0_registers_num = ARRAY_SIZE (sparc64_cp0_register_names);
   1824 
   1825   /* This is what all the fuss is about.  */
   1826   set_gdbarch_long_bit (gdbarch, 64);
   1827   set_gdbarch_long_long_bit (gdbarch, 64);
   1828   set_gdbarch_ptr_bit (gdbarch, 64);
   1829 
   1830   set_gdbarch_wchar_bit (gdbarch, 16);
   1831   set_gdbarch_wchar_signed (gdbarch, 0);
   1832 
   1833   set_gdbarch_num_regs (gdbarch, SPARC64_NUM_REGS);
   1834   set_gdbarch_register_name (gdbarch, sparc64_register_name);
   1835   set_gdbarch_register_type (gdbarch, sparc64_register_type);
   1836   set_gdbarch_num_pseudo_regs (gdbarch, SPARC64_NUM_PSEUDO_REGS);
   1837   set_tdesc_pseudo_register_name (gdbarch, sparc64_pseudo_register_name);
   1838   set_tdesc_pseudo_register_type (gdbarch, sparc64_pseudo_register_type);
   1839   set_gdbarch_pseudo_register_read (gdbarch, sparc64_pseudo_register_read);
   1840   set_gdbarch_pseudo_register_write (gdbarch, sparc64_pseudo_register_write);
   1841 
   1842   /* Register numbers of various important registers.  */
   1843   set_gdbarch_pc_regnum (gdbarch, SPARC64_PC_REGNUM); /* %pc */
   1844 
   1845   /* Call dummy code.  */
   1846   set_gdbarch_frame_align (gdbarch, sparc64_frame_align);
   1847   set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
   1848   set_gdbarch_push_dummy_code (gdbarch, NULL);
   1849   set_gdbarch_push_dummy_call (gdbarch, sparc64_push_dummy_call);
   1850 
   1851   set_gdbarch_return_value (gdbarch, sparc64_return_value);
   1852   set_gdbarch_stabs_argument_has_addr
   1853     (gdbarch, default_stabs_argument_has_addr);
   1854 
   1855   set_gdbarch_skip_prologue (gdbarch, sparc64_skip_prologue);
   1856   set_gdbarch_stack_frame_destroyed_p (gdbarch, sparc_stack_frame_destroyed_p);
   1857 
   1858   /* Hook in the DWARF CFI frame unwinder.  */
   1859   dwarf2_frame_set_init_reg (gdbarch, sparc64_dwarf2_frame_init_reg);
   1860   /* FIXME: kettenis/20050423: Don't enable the unwinder until the
   1861      StackGhost issues have been resolved.  */
   1862 
   1863   frame_unwind_append_unwinder (gdbarch, &sparc64_frame_unwind);
   1864   frame_base_set_default (gdbarch, &sparc64_frame_base);
   1865 
   1866   set_gdbarch_addr_bits_remove (gdbarch, sparc64_addr_bits_remove);
   1867 }
   1868 
   1869 
   1871 /* Helper functions for dealing with register sets.  */
   1872 
   1873 #define TSTATE_CWP	0x000000000000001fULL
   1874 #define TSTATE_ICC	0x0000000f00000000ULL
   1875 #define TSTATE_XCC	0x000000f000000000ULL
   1876 
   1877 #define PSR_S		0x00000080
   1878 #ifndef PSR_ICC
   1879 #define PSR_ICC		0x00f00000
   1880 #endif
   1881 #define PSR_VERS	0x0f000000
   1882 #ifndef PSR_IMPL
   1883 #define PSR_IMPL	0xf0000000
   1884 #endif
   1885 #define PSR_V8PLUS	0xff000000
   1886 #define PSR_XCC		0x000f0000
   1887 
   1888 void
   1889 sparc64_supply_gregset (const struct sparc_gregmap *gregmap,
   1890 			struct regcache *regcache,
   1891 			int regnum, const void *gregs)
   1892 {
   1893   struct gdbarch *gdbarch = regcache->arch ();
   1894   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   1895   int sparc32 = (gdbarch_ptr_bit (gdbarch) == 32);
   1896   const gdb_byte *regs = (const gdb_byte *) gregs;
   1897   gdb_byte zero[8] = { 0 };
   1898   int i;
   1899 
   1900   if (sparc32)
   1901     {
   1902       if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
   1903 	{
   1904 	  int offset = gregmap->r_tstate_offset;
   1905 	  ULONGEST tstate, psr;
   1906 	  gdb_byte buf[4];
   1907 
   1908 	  tstate = extract_unsigned_integer (regs + offset, 8, byte_order);
   1909 	  psr = ((tstate & TSTATE_CWP) | PSR_S | ((tstate & TSTATE_ICC) >> 12)
   1910 		 | ((tstate & TSTATE_XCC) >> 20) | PSR_V8PLUS);
   1911 	  store_unsigned_integer (buf, 4, byte_order, psr);
   1912 	  regcache->raw_supply (SPARC32_PSR_REGNUM, buf);
   1913 	}
   1914 
   1915       if (regnum == SPARC32_PC_REGNUM || regnum == -1)
   1916 	regcache->raw_supply (SPARC32_PC_REGNUM,
   1917 			      regs + gregmap->r_pc_offset + 4);
   1918 
   1919       if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
   1920 	regcache->raw_supply (SPARC32_NPC_REGNUM,
   1921 			      regs + gregmap->r_npc_offset + 4);
   1922 
   1923       if (regnum == SPARC32_Y_REGNUM || regnum == -1)
   1924 	{
   1925 	  int offset = gregmap->r_y_offset + 8 - gregmap->r_y_size;
   1926 	  regcache->raw_supply (SPARC32_Y_REGNUM, regs + offset);
   1927 	}
   1928     }
   1929   else
   1930     {
   1931       if (regnum == SPARC64_STATE_REGNUM || regnum == -1)
   1932 	regcache->raw_supply (SPARC64_STATE_REGNUM,
   1933 			      regs + gregmap->r_tstate_offset);
   1934 
   1935       if (regnum == SPARC64_PC_REGNUM || regnum == -1)
   1936 	regcache->raw_supply (SPARC64_PC_REGNUM,
   1937 			      regs + gregmap->r_pc_offset);
   1938 
   1939       if (regnum == SPARC64_NPC_REGNUM || regnum == -1)
   1940 	regcache->raw_supply (SPARC64_NPC_REGNUM,
   1941 			      regs + gregmap->r_npc_offset);
   1942 
   1943       if (regnum == SPARC64_Y_REGNUM || regnum == -1)
   1944 	{
   1945 	  gdb_byte buf[8];
   1946 
   1947 	  memset (buf, 0, 8);
   1948 	  memcpy (buf + 8 - gregmap->r_y_size,
   1949 		  regs + gregmap->r_y_offset, gregmap->r_y_size);
   1950 	  regcache->raw_supply (SPARC64_Y_REGNUM, buf);
   1951 	}
   1952 
   1953       if ((regnum == SPARC64_FPRS_REGNUM || regnum == -1)
   1954 	  && gregmap->r_fprs_offset != -1)
   1955 	regcache->raw_supply (SPARC64_FPRS_REGNUM,
   1956 			      regs + gregmap->r_fprs_offset);
   1957     }
   1958 
   1959   if (regnum == SPARC_G0_REGNUM || regnum == -1)
   1960     regcache->raw_supply (SPARC_G0_REGNUM, &zero);
   1961 
   1962   if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
   1963     {
   1964       int offset = gregmap->r_g1_offset;
   1965 
   1966       if (sparc32)
   1967 	offset += 4;
   1968 
   1969       for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
   1970 	{
   1971 	  if (regnum == i || regnum == -1)
   1972 	    regcache->raw_supply (i, regs + offset);
   1973 	  offset += 8;
   1974 	}
   1975     }
   1976 
   1977   if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
   1978     {
   1979       /* Not all of the register set variants include Locals and
   1980          Inputs.  For those that don't, we read them off the stack.  */
   1981       if (gregmap->r_l0_offset == -1)
   1982 	{
   1983 	  ULONGEST sp;
   1984 
   1985 	  regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
   1986 	  sparc_supply_rwindow (regcache, sp, regnum);
   1987 	}
   1988       else
   1989 	{
   1990 	  int offset = gregmap->r_l0_offset;
   1991 
   1992 	  if (sparc32)
   1993 	    offset += 4;
   1994 
   1995 	  for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
   1996 	    {
   1997 	      if (regnum == i || regnum == -1)
   1998 		regcache->raw_supply (i, regs + offset);
   1999 	      offset += 8;
   2000 	    }
   2001 	}
   2002     }
   2003 }
   2004 
   2005 void
   2006 sparc64_collect_gregset (const struct sparc_gregmap *gregmap,
   2007 			 const struct regcache *regcache,
   2008 			 int regnum, void *gregs)
   2009 {
   2010   struct gdbarch *gdbarch = regcache->arch ();
   2011   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   2012   int sparc32 = (gdbarch_ptr_bit (gdbarch) == 32);
   2013   gdb_byte *regs = (gdb_byte *) gregs;
   2014   int i;
   2015 
   2016   if (sparc32)
   2017     {
   2018       if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
   2019 	{
   2020 	  int offset = gregmap->r_tstate_offset;
   2021 	  ULONGEST tstate, psr;
   2022 	  gdb_byte buf[8];
   2023 
   2024 	  tstate = extract_unsigned_integer (regs + offset, 8, byte_order);
   2025 	  regcache->raw_collect (SPARC32_PSR_REGNUM, buf);
   2026 	  psr = extract_unsigned_integer (buf, 4, byte_order);
   2027 	  tstate |= (psr & PSR_ICC) << 12;
   2028 	  if ((psr & (PSR_VERS | PSR_IMPL)) == PSR_V8PLUS)
   2029 	    tstate |= (psr & PSR_XCC) << 20;
   2030 	  store_unsigned_integer (buf, 8, byte_order, tstate);
   2031 	  memcpy (regs + offset, buf, 8);
   2032 	}
   2033 
   2034       if (regnum == SPARC32_PC_REGNUM || regnum == -1)
   2035 	regcache->raw_collect (SPARC32_PC_REGNUM,
   2036 			       regs + gregmap->r_pc_offset + 4);
   2037 
   2038       if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
   2039 	regcache->raw_collect (SPARC32_NPC_REGNUM,
   2040 			       regs + gregmap->r_npc_offset + 4);
   2041 
   2042       if (regnum == SPARC32_Y_REGNUM || regnum == -1)
   2043 	{
   2044 	  int offset = gregmap->r_y_offset + 8 - gregmap->r_y_size;
   2045 	  regcache->raw_collect (SPARC32_Y_REGNUM, regs + offset);
   2046 	}
   2047     }
   2048   else
   2049     {
   2050       if (regnum == SPARC64_STATE_REGNUM || regnum == -1)
   2051 	regcache->raw_collect (SPARC64_STATE_REGNUM,
   2052 			       regs + gregmap->r_tstate_offset);
   2053 
   2054       if (regnum == SPARC64_PC_REGNUM || regnum == -1)
   2055 	regcache->raw_collect (SPARC64_PC_REGNUM,
   2056 			       regs + gregmap->r_pc_offset);
   2057 
   2058       if (regnum == SPARC64_NPC_REGNUM || regnum == -1)
   2059 	regcache->raw_collect (SPARC64_NPC_REGNUM,
   2060 			       regs + gregmap->r_npc_offset);
   2061 
   2062       if (regnum == SPARC64_Y_REGNUM || regnum == -1)
   2063 	{
   2064 	  gdb_byte buf[8];
   2065 
   2066 	  regcache->raw_collect (SPARC64_Y_REGNUM, buf);
   2067 	  memcpy (regs + gregmap->r_y_offset,
   2068 		  buf + 8 - gregmap->r_y_size, gregmap->r_y_size);
   2069 	}
   2070 
   2071       if ((regnum == SPARC64_FPRS_REGNUM || regnum == -1)
   2072 	  && gregmap->r_fprs_offset != -1)
   2073 	regcache->raw_collect (SPARC64_FPRS_REGNUM,
   2074 			       regs + gregmap->r_fprs_offset);
   2075 
   2076     }
   2077 
   2078   if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
   2079     {
   2080       int offset = gregmap->r_g1_offset;
   2081 
   2082       if (sparc32)
   2083 	offset += 4;
   2084 
   2085       /* %g0 is always zero.  */
   2086       for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
   2087 	{
   2088 	  if (regnum == i || regnum == -1)
   2089 	    regcache->raw_collect (i, regs + offset);
   2090 	  offset += 8;
   2091 	}
   2092     }
   2093 
   2094   if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
   2095     {
   2096       /* Not all of the register set variants include Locals and
   2097          Inputs.  For those that don't, we read them off the stack.  */
   2098       if (gregmap->r_l0_offset != -1)
   2099 	{
   2100 	  int offset = gregmap->r_l0_offset;
   2101 
   2102 	  if (sparc32)
   2103 	    offset += 4;
   2104 
   2105 	  for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
   2106 	    {
   2107 	      if (regnum == i || regnum == -1)
   2108 		regcache->raw_collect (i, regs + offset);
   2109 	      offset += 8;
   2110 	    }
   2111 	}
   2112     }
   2113 }
   2114 
   2115 void
   2116 sparc64_supply_fpregset (const struct sparc_fpregmap *fpregmap,
   2117 			 struct regcache *regcache,
   2118 			 int regnum, const void *fpregs)
   2119 {
   2120   int sparc32 = (gdbarch_ptr_bit (regcache->arch ()) == 32);
   2121   const gdb_byte *regs = (const gdb_byte *) fpregs;
   2122   int i;
   2123 
   2124   for (i = 0; i < 32; i++)
   2125     {
   2126       if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
   2127 	regcache->raw_supply (SPARC_F0_REGNUM + i,
   2128 			      regs + fpregmap->r_f0_offset + (i * 4));
   2129     }
   2130 
   2131   if (sparc32)
   2132     {
   2133       if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
   2134 	regcache->raw_supply (SPARC32_FSR_REGNUM,
   2135 			     regs + fpregmap->r_fsr_offset);
   2136     }
   2137   else
   2138     {
   2139       for (i = 0; i < 16; i++)
   2140 	{
   2141 	  if (regnum == (SPARC64_F32_REGNUM + i) || regnum == -1)
   2142 	    regcache->raw_supply
   2143 	      (SPARC64_F32_REGNUM + i,
   2144 	       regs + fpregmap->r_f0_offset + (32 * 4) + (i * 8));
   2145 	}
   2146 
   2147       if (regnum == SPARC64_FSR_REGNUM || regnum == -1)
   2148 	regcache->raw_supply (SPARC64_FSR_REGNUM,
   2149 			      regs + fpregmap->r_fsr_offset);
   2150     }
   2151 }
   2152 
   2153 void
   2154 sparc64_collect_fpregset (const struct sparc_fpregmap *fpregmap,
   2155 			  const struct regcache *regcache,
   2156 			  int regnum, void *fpregs)
   2157 {
   2158   int sparc32 = (gdbarch_ptr_bit (regcache->arch ()) == 32);
   2159   gdb_byte *regs = (gdb_byte *) fpregs;
   2160   int i;
   2161 
   2162   for (i = 0; i < 32; i++)
   2163     {
   2164       if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
   2165 	regcache->raw_collect (SPARC_F0_REGNUM + i,
   2166 			       regs + fpregmap->r_f0_offset + (i * 4));
   2167     }
   2168 
   2169   if (sparc32)
   2170     {
   2171       if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
   2172 	regcache->raw_collect (SPARC32_FSR_REGNUM,
   2173 			       regs + fpregmap->r_fsr_offset);
   2174     }
   2175   else
   2176     {
   2177       for (i = 0; i < 16; i++)
   2178 	{
   2179 	  if (regnum == (SPARC64_F32_REGNUM + i) || regnum == -1)
   2180 	    regcache->raw_collect (SPARC64_F32_REGNUM + i,
   2181 				   (regs + fpregmap->r_f0_offset
   2182 				    + (32 * 4) + (i * 8)));
   2183 	}
   2184 
   2185       if (regnum == SPARC64_FSR_REGNUM || regnum == -1)
   2186 	regcache->raw_collect (SPARC64_FSR_REGNUM,
   2187 			       regs + fpregmap->r_fsr_offset);
   2188     }
   2189 }
   2190 
   2191 const struct sparc_fpregmap sparc64_bsd_fpregmap =
   2192 {
   2193   0 * 8,			/* %f0 */
   2194   32 * 8,			/* %fsr */
   2195 };
   2196