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