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