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