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