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