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