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