sparc64-tdep.c revision 1.3.2.1 1 /* Target-dependent code for UltraSPARC.
2
3 Copyright (C) 2003-2015 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.h"
35 #include "value.h"
36
37 #include "sparc64-tdep.h"
38
39 /* This file implements the SPARC 64-bit ABI as defined by the
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. */
47
48 /* The functions on this page are intended to be used to classify
50 function arguments. */
51
52 /* Check whether TYPE is "Integral or Pointer". */
53
54 static int
55 sparc64_integral_or_pointer_p (const struct type *type)
56 {
57 switch (TYPE_CODE (type))
58 {
59 case TYPE_CODE_INT:
60 case TYPE_CODE_BOOL:
61 case TYPE_CODE_CHAR:
62 case TYPE_CODE_ENUM:
63 case TYPE_CODE_RANGE:
64 {
65 int len = TYPE_LENGTH (type);
66 gdb_assert (len == 1 || len == 2 || len == 4 || len == 8);
67 }
68 return 1;
69 case TYPE_CODE_PTR:
70 case TYPE_CODE_REF:
71 {
72 int len = TYPE_LENGTH (type);
73 gdb_assert (len == 8);
74 }
75 return 1;
76 default:
77 break;
78 }
79
80 return 0;
81 }
82
83 /* Check whether TYPE is "Floating". */
84
85 static int
86 sparc64_floating_p (const struct type *type)
87 {
88 switch (TYPE_CODE (type))
89 {
90 case TYPE_CODE_FLT:
91 {
92 int len = TYPE_LENGTH (type);
93 gdb_assert (len == 4 || len == 8 || len == 16);
94 }
95 return 1;
96 default:
97 break;
98 }
99
100 return 0;
101 }
102
103 /* Check whether TYPE is "Complex Floating". */
104
105 static int
106 sparc64_complex_floating_p (const struct type *type)
107 {
108 switch (TYPE_CODE (type))
109 {
110 case TYPE_CODE_COMPLEX:
111 {
112 int len = TYPE_LENGTH (type);
113 gdb_assert (len == 8 || len == 16 || len == 32);
114 }
115 return 1;
116 default:
117 break;
118 }
119
120 return 0;
121 }
122
123 /* Check whether TYPE is "Structure or Union".
124
125 In terms of Ada subprogram calls, arrays are treated the same as
126 struct and union types. So this function also returns non-zero
127 for array types. */
128
129 static int
130 sparc64_structure_or_union_p (const struct type *type)
131 {
132 switch (TYPE_CODE (type))
133 {
134 case TYPE_CODE_STRUCT:
135 case TYPE_CODE_UNION:
136 case TYPE_CODE_ARRAY:
137 return 1;
138 default:
139 break;
140 }
141
142 return 0;
143 }
144
145
147 /* Construct types for ISA-specific registers. */
148
149 static struct type *
150 sparc64_pstate_type (struct gdbarch *gdbarch)
151 {
152 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
153
154 if (!tdep->sparc64_pstate_type)
155 {
156 struct type *type;
157
158 type = arch_flags_type (gdbarch, "builtin_type_sparc64_pstate", 8);
159 append_flags_type_flag (type, 0, "AG");
160 append_flags_type_flag (type, 1, "IE");
161 append_flags_type_flag (type, 2, "PRIV");
162 append_flags_type_flag (type, 3, "AM");
163 append_flags_type_flag (type, 4, "PEF");
164 append_flags_type_flag (type, 5, "RED");
165 append_flags_type_flag (type, 8, "TLE");
166 append_flags_type_flag (type, 9, "CLE");
167 append_flags_type_flag (type, 10, "PID0");
168 append_flags_type_flag (type, 11, "PID1");
169
170 tdep->sparc64_pstate_type = type;
171 }
172
173 return tdep->sparc64_pstate_type;
174 }
175
176 static struct type *
177 sparc64_fsr_type (struct gdbarch *gdbarch)
178 {
179 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
180
181 if (!tdep->sparc64_fsr_type)
182 {
183 struct type *type;
184
185 type = arch_flags_type (gdbarch, "builtin_type_sparc64_fsr", 8);
186 append_flags_type_flag (type, 0, "NXA");
187 append_flags_type_flag (type, 1, "DZA");
188 append_flags_type_flag (type, 2, "UFA");
189 append_flags_type_flag (type, 3, "OFA");
190 append_flags_type_flag (type, 4, "NVA");
191 append_flags_type_flag (type, 5, "NXC");
192 append_flags_type_flag (type, 6, "DZC");
193 append_flags_type_flag (type, 7, "UFC");
194 append_flags_type_flag (type, 8, "OFC");
195 append_flags_type_flag (type, 9, "NVC");
196 append_flags_type_flag (type, 22, "NS");
197 append_flags_type_flag (type, 23, "NXM");
198 append_flags_type_flag (type, 24, "DZM");
199 append_flags_type_flag (type, 25, "UFM");
200 append_flags_type_flag (type, 26, "OFM");
201 append_flags_type_flag (type, 27, "NVM");
202
203 tdep->sparc64_fsr_type = type;
204 }
205
206 return tdep->sparc64_fsr_type;
207 }
208
209 static struct type *
210 sparc64_fprs_type (struct gdbarch *gdbarch)
211 {
212 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
213
214 if (!tdep->sparc64_fprs_type)
215 {
216 struct type *type;
217
218 type = arch_flags_type (gdbarch, "builtin_type_sparc64_fprs", 8);
219 append_flags_type_flag (type, 0, "DL");
220 append_flags_type_flag (type, 1, "DU");
221 append_flags_type_flag (type, 2, "FEF");
222
223 tdep->sparc64_fprs_type = type;
224 }
225
226 return tdep->sparc64_fprs_type;
227 }
228
229
230 /* Register information. */
231
232 static const char *sparc64_register_names[] =
233 {
234 "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
235 "o0", "o1", "o2", "o3", "o4", "o5", "sp", "o7",
236 "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
237 "i0", "i1", "i2", "i3", "i4", "i5", "fp", "i7",
238
239 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
240 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
241 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
242 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
243 "f32", "f34", "f36", "f38", "f40", "f42", "f44", "f46",
244 "f48", "f50", "f52", "f54", "f56", "f58", "f60", "f62",
245
246 "pc", "npc",
247
248 /* FIXME: Give "state" a name until we start using register groups. */
249 "state",
250 "fsr",
251 "fprs",
252 "y",
253 };
254
255 /* Total number of registers. */
256 #define SPARC64_NUM_REGS ARRAY_SIZE (sparc64_register_names)
257
258 /* We provide the aliases %d0..%d62 and %q0..%q60 for the floating
259 registers as "psuedo" registers. */
260
261 static const char *sparc64_pseudo_register_names[] =
262 {
263 "cwp", "pstate", "asi", "ccr",
264
265 "d0", "d2", "d4", "d6", "d8", "d10", "d12", "d14",
266 "d16", "d18", "d20", "d22", "d24", "d26", "d28", "d30",
267 "d32", "d34", "d36", "d38", "d40", "d42", "d44", "d46",
268 "d48", "d50", "d52", "d54", "d56", "d58", "d60", "d62",
269
270 "q0", "q4", "q8", "q12", "q16", "q20", "q24", "q28",
271 "q32", "q36", "q40", "q44", "q48", "q52", "q56", "q60",
272 };
273
274 /* Total number of pseudo registers. */
275 #define SPARC64_NUM_PSEUDO_REGS ARRAY_SIZE (sparc64_pseudo_register_names)
276
277 /* Return the name of register REGNUM. */
278
279 static const char *
280 sparc64_register_name (struct gdbarch *gdbarch, int regnum)
281 {
282 if (regnum >= 0 && regnum < SPARC64_NUM_REGS)
283 return sparc64_register_names[regnum];
284
285 if (regnum >= SPARC64_NUM_REGS
286 && regnum < SPARC64_NUM_REGS + SPARC64_NUM_PSEUDO_REGS)
287 return sparc64_pseudo_register_names[regnum - SPARC64_NUM_REGS];
288
289 return NULL;
290 }
291
292 /* Return the GDB type object for the "standard" data type of data in
293 register REGNUM. */
294
295 static struct type *
296 sparc64_register_type (struct gdbarch *gdbarch, int regnum)
297 {
298 /* Raw registers. */
299
300 if (regnum == SPARC_SP_REGNUM || regnum == SPARC_FP_REGNUM)
301 return builtin_type (gdbarch)->builtin_data_ptr;
302 if (regnum >= SPARC_G0_REGNUM && regnum <= SPARC_I7_REGNUM)
303 return builtin_type (gdbarch)->builtin_int64;
304 if (regnum >= SPARC_F0_REGNUM && regnum <= SPARC_F31_REGNUM)
305 return builtin_type (gdbarch)->builtin_float;
306 if (regnum >= SPARC64_F32_REGNUM && regnum <= SPARC64_F62_REGNUM)
307 return builtin_type (gdbarch)->builtin_double;
308 if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM)
309 return builtin_type (gdbarch)->builtin_func_ptr;
310 /* This raw register contains the contents of %cwp, %pstate, %asi
311 and %ccr as laid out in a %tstate register. */
312 if (regnum == SPARC64_STATE_REGNUM)
313 return builtin_type (gdbarch)->builtin_int64;
314 if (regnum == SPARC64_FSR_REGNUM)
315 return sparc64_fsr_type (gdbarch);
316 if (regnum == SPARC64_FPRS_REGNUM)
317 return sparc64_fprs_type (gdbarch);
318 /* "Although Y is a 64-bit register, its high-order 32 bits are
319 reserved and always read as 0." */
320 if (regnum == SPARC64_Y_REGNUM)
321 return builtin_type (gdbarch)->builtin_int64;
322
323 /* Pseudo registers. */
324
325 if (regnum == SPARC64_CWP_REGNUM)
326 return builtin_type (gdbarch)->builtin_int64;
327 if (regnum == SPARC64_PSTATE_REGNUM)
328 return sparc64_pstate_type (gdbarch);
329 if (regnum == SPARC64_ASI_REGNUM)
330 return builtin_type (gdbarch)->builtin_int64;
331 if (regnum == SPARC64_CCR_REGNUM)
332 return builtin_type (gdbarch)->builtin_int64;
333 if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D62_REGNUM)
334 return builtin_type (gdbarch)->builtin_double;
335 if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q60_REGNUM)
336 return builtin_type (gdbarch)->builtin_long_double;
337
338 internal_error (__FILE__, __LINE__, _("invalid regnum"));
339 }
340
341 static enum register_status
342 sparc64_pseudo_register_read (struct gdbarch *gdbarch,
343 struct regcache *regcache,
344 int regnum, gdb_byte *buf)
345 {
346 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
347 enum register_status status;
348
349 gdb_assert (regnum >= SPARC64_NUM_REGS);
350
351 if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D30_REGNUM)
352 {
353 regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC64_D0_REGNUM);
354 status = regcache_raw_read (regcache, regnum, buf);
355 if (status == REG_VALID)
356 status = regcache_raw_read (regcache, regnum + 1, buf + 4);
357 return status;
358 }
359 else if (regnum >= SPARC64_D32_REGNUM && regnum <= SPARC64_D62_REGNUM)
360 {
361 regnum = SPARC64_F32_REGNUM + (regnum - SPARC64_D32_REGNUM);
362 return regcache_raw_read (regcache, regnum, buf);
363 }
364 else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q28_REGNUM)
365 {
366 regnum = SPARC_F0_REGNUM + 4 * (regnum - SPARC64_Q0_REGNUM);
367
368 status = regcache_raw_read (regcache, regnum, buf);
369 if (status == REG_VALID)
370 status = regcache_raw_read (regcache, regnum + 1, buf + 4);
371 if (status == REG_VALID)
372 status = regcache_raw_read (regcache, regnum + 2, buf + 8);
373 if (status == REG_VALID)
374 status = regcache_raw_read (regcache, regnum + 3, buf + 12);
375
376 return status;
377 }
378 else if (regnum >= SPARC64_Q32_REGNUM && regnum <= SPARC64_Q60_REGNUM)
379 {
380 regnum = SPARC64_F32_REGNUM + 2 * (regnum - SPARC64_Q32_REGNUM);
381
382 status = regcache_raw_read (regcache, regnum, buf);
383 if (status == REG_VALID)
384 status = regcache_raw_read (regcache, regnum + 1, buf + 8);
385
386 return status;
387 }
388 else if (regnum == SPARC64_CWP_REGNUM
389 || regnum == SPARC64_PSTATE_REGNUM
390 || regnum == SPARC64_ASI_REGNUM
391 || regnum == SPARC64_CCR_REGNUM)
392 {
393 ULONGEST state;
394
395 status = regcache_raw_read_unsigned (regcache, SPARC64_STATE_REGNUM, &state);
396 if (status != REG_VALID)
397 return status;
398
399 switch (regnum)
400 {
401 case SPARC64_CWP_REGNUM:
402 state = (state >> 0) & ((1 << 5) - 1);
403 break;
404 case SPARC64_PSTATE_REGNUM:
405 state = (state >> 8) & ((1 << 12) - 1);
406 break;
407 case SPARC64_ASI_REGNUM:
408 state = (state >> 24) & ((1 << 8) - 1);
409 break;
410 case SPARC64_CCR_REGNUM:
411 state = (state >> 32) & ((1 << 8) - 1);
412 break;
413 }
414 store_unsigned_integer (buf, 8, byte_order, state);
415 }
416
417 return REG_VALID;
418 }
419
420 static void
421 sparc64_pseudo_register_write (struct gdbarch *gdbarch,
422 struct regcache *regcache,
423 int regnum, const gdb_byte *buf)
424 {
425 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
426 gdb_assert (regnum >= SPARC64_NUM_REGS);
427
428 if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D30_REGNUM)
429 {
430 regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC64_D0_REGNUM);
431 regcache_raw_write (regcache, regnum, buf);
432 regcache_raw_write (regcache, regnum + 1, buf + 4);
433 }
434 else if (regnum >= SPARC64_D32_REGNUM && regnum <= SPARC64_D62_REGNUM)
435 {
436 regnum = SPARC64_F32_REGNUM + (regnum - SPARC64_D32_REGNUM);
437 regcache_raw_write (regcache, regnum, buf);
438 }
439 else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q28_REGNUM)
440 {
441 regnum = SPARC_F0_REGNUM + 4 * (regnum - SPARC64_Q0_REGNUM);
442 regcache_raw_write (regcache, regnum, buf);
443 regcache_raw_write (regcache, regnum + 1, buf + 4);
444 regcache_raw_write (regcache, regnum + 2, buf + 8);
445 regcache_raw_write (regcache, regnum + 3, buf + 12);
446 }
447 else if (regnum >= SPARC64_Q32_REGNUM && regnum <= SPARC64_Q60_REGNUM)
448 {
449 regnum = SPARC64_F32_REGNUM + 2 * (regnum - SPARC64_Q32_REGNUM);
450 regcache_raw_write (regcache, regnum, buf);
451 regcache_raw_write (regcache, regnum + 1, buf + 8);
452 }
453 else if (regnum == SPARC64_CWP_REGNUM
454 || regnum == SPARC64_PSTATE_REGNUM
455 || regnum == SPARC64_ASI_REGNUM
456 || regnum == SPARC64_CCR_REGNUM)
457 {
458 ULONGEST state, bits;
459
460 regcache_raw_read_unsigned (regcache, SPARC64_STATE_REGNUM, &state);
461 bits = extract_unsigned_integer (buf, 8, byte_order);
462 switch (regnum)
463 {
464 case SPARC64_CWP_REGNUM:
465 state |= ((bits & ((1 << 5) - 1)) << 0);
466 break;
467 case SPARC64_PSTATE_REGNUM:
468 state |= ((bits & ((1 << 12) - 1)) << 8);
469 break;
470 case SPARC64_ASI_REGNUM:
471 state |= ((bits & ((1 << 8) - 1)) << 24);
472 break;
473 case SPARC64_CCR_REGNUM:
474 state |= ((bits & ((1 << 8) - 1)) << 32);
475 break;
476 }
477 regcache_raw_write_unsigned (regcache, SPARC64_STATE_REGNUM, state);
478 }
479 }
480
481
483 /* Return PC of first real instruction of the function starting at
484 START_PC. */
485
486 static CORE_ADDR
487 sparc64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
488 {
489 struct symtab_and_line sal;
490 CORE_ADDR func_start, func_end;
491 struct sparc_frame_cache cache;
492
493 /* This is the preferred method, find the end of the prologue by
494 using the debugging information. */
495 if (find_pc_partial_function (start_pc, NULL, &func_start, &func_end))
496 {
497 sal = find_pc_line (func_start, 0);
498
499 if (sal.end < func_end
500 && start_pc <= sal.end)
501 return sal.end;
502 }
503
504 return sparc_analyze_prologue (gdbarch, start_pc, 0xffffffffffffffffULL,
505 &cache);
506 }
507
508 /* Normal frames. */
509
510 static struct sparc_frame_cache *
511 sparc64_frame_cache (struct frame_info *this_frame, void **this_cache)
512 {
513 return sparc_frame_cache (this_frame, this_cache);
514 }
515
516 static void
517 sparc64_frame_this_id (struct frame_info *this_frame, void **this_cache,
518 struct frame_id *this_id)
519 {
520 struct sparc_frame_cache *cache =
521 sparc64_frame_cache (this_frame, this_cache);
522
523 /* This marks the outermost frame. */
524 if (cache->base == 0)
525 return;
526
527 (*this_id) = frame_id_build (cache->base, cache->pc);
528 }
529
530 static struct value *
531 sparc64_frame_prev_register (struct frame_info *this_frame, void **this_cache,
532 int regnum)
533 {
534 struct gdbarch *gdbarch = get_frame_arch (this_frame);
535 struct sparc_frame_cache *cache =
536 sparc64_frame_cache (this_frame, this_cache);
537
538 if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM)
539 {
540 CORE_ADDR pc = (regnum == SPARC64_NPC_REGNUM) ? 4 : 0;
541
542 regnum =
543 (cache->copied_regs_mask & 0x80) ? SPARC_I7_REGNUM : SPARC_O7_REGNUM;
544 pc += get_frame_register_unsigned (this_frame, regnum) + 8;
545 return frame_unwind_got_constant (this_frame, regnum, pc);
546 }
547
548 /* Handle StackGhost. */
549 {
550 ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
551
552 if (wcookie != 0 && !cache->frameless_p && regnum == SPARC_I7_REGNUM)
553 {
554 CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 8;
555 ULONGEST i7;
556
557 /* Read the value in from memory. */
558 i7 = get_frame_memory_unsigned (this_frame, addr, 8);
559 return frame_unwind_got_constant (this_frame, regnum, i7 ^ wcookie);
560 }
561 }
562
563 /* The previous frame's `local' and `in' registers may have been saved
564 in the register save area. */
565 if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM
566 && (cache->saved_regs_mask & (1 << (regnum - SPARC_L0_REGNUM))))
567 {
568 CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 8;
569
570 return frame_unwind_got_memory (this_frame, regnum, addr);
571 }
572
573 /* The previous frame's `out' registers may be accessible as the current
574 frame's `in' registers. */
575 if (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O7_REGNUM
576 && (cache->copied_regs_mask & (1 << (regnum - SPARC_O0_REGNUM))))
577 regnum += (SPARC_I0_REGNUM - SPARC_O0_REGNUM);
578
579 return frame_unwind_got_register (this_frame, regnum, regnum);
580 }
581
582 static const struct frame_unwind sparc64_frame_unwind =
583 {
584 NORMAL_FRAME,
585 default_frame_unwind_stop_reason,
586 sparc64_frame_this_id,
587 sparc64_frame_prev_register,
588 NULL,
589 default_frame_sniffer
590 };
591
592
594 static CORE_ADDR
595 sparc64_frame_base_address (struct frame_info *this_frame, void **this_cache)
596 {
597 struct sparc_frame_cache *cache =
598 sparc64_frame_cache (this_frame, this_cache);
599
600 return cache->base;
601 }
602
603 static const struct frame_base sparc64_frame_base =
604 {
605 &sparc64_frame_unwind,
606 sparc64_frame_base_address,
607 sparc64_frame_base_address,
608 sparc64_frame_base_address
609 };
610
611 /* Check whether TYPE must be 16-byte aligned. */
613
614 static int
615 sparc64_16_byte_align_p (struct type *type)
616 {
617 if (sparc64_floating_p (type) && TYPE_LENGTH (type) == 16)
618 return 1;
619
620 if (sparc64_structure_or_union_p (type))
621 {
622 int i;
623
624 for (i = 0; i < TYPE_NFIELDS (type); i++)
625 {
626 struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
627
628 if (sparc64_16_byte_align_p (subtype))
629 return 1;
630 }
631 }
632
633 return 0;
634 }
635
636 /* Store floating fields of element ELEMENT of an "parameter array"
637 that has type TYPE and is stored at BITPOS in VALBUF in the
638 apropriate registers of REGCACHE. This function can be called
639 recursively and therefore handles floating types in addition to
640 structures. */
641
642 static void
643 sparc64_store_floating_fields (struct regcache *regcache, struct type *type,
644 const gdb_byte *valbuf, int element, int bitpos)
645 {
646 int len = TYPE_LENGTH (type);
647
648 gdb_assert (element < 16);
649
650 if (sparc64_floating_p (type)
651 || (sparc64_complex_floating_p (type) && len <= 16))
652 {
653 int regnum;
654
655 if (len == 16)
656 {
657 gdb_assert (bitpos == 0);
658 gdb_assert ((element % 2) == 0);
659
660 regnum = SPARC64_Q0_REGNUM + element / 2;
661 regcache_cooked_write (regcache, regnum, valbuf);
662 }
663 else if (len == 8)
664 {
665 gdb_assert (bitpos == 0 || bitpos == 64);
666
667 regnum = SPARC64_D0_REGNUM + element + bitpos / 64;
668 regcache_cooked_write (regcache, regnum, valbuf + (bitpos / 8));
669 }
670 else
671 {
672 gdb_assert (len == 4);
673 gdb_assert (bitpos % 32 == 0 && bitpos >= 0 && bitpos < 128);
674
675 regnum = SPARC_F0_REGNUM + element * 2 + bitpos / 32;
676 regcache_cooked_write (regcache, regnum, valbuf + (bitpos / 8));
677 }
678 }
679 else if (sparc64_structure_or_union_p (type))
680 {
681 int i;
682
683 for (i = 0; i < TYPE_NFIELDS (type); i++)
684 {
685 struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
686 int subpos = bitpos + TYPE_FIELD_BITPOS (type, i);
687
688 sparc64_store_floating_fields (regcache, subtype, valbuf,
689 element, subpos);
690 }
691
692 /* GCC has an interesting bug. If TYPE is a structure that has
693 a single `float' member, GCC doesn't treat it as a structure
694 at all, but rather as an ordinary `float' argument. This
695 argument will be stored in %f1, as required by the psABI.
696 However, as a member of a structure the psABI requires it to
697 be stored in %f0. This bug is present in GCC 3.3.2, but
698 probably in older releases to. To appease GCC, if a
699 structure has only a single `float' member, we store its
700 value in %f1 too (we already have stored in %f0). */
701 if (TYPE_NFIELDS (type) == 1)
702 {
703 struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, 0));
704
705 if (sparc64_floating_p (subtype) && TYPE_LENGTH (subtype) == 4)
706 regcache_cooked_write (regcache, SPARC_F1_REGNUM, valbuf);
707 }
708 }
709 }
710
711 /* Fetch floating fields from a variable of type TYPE from the
712 appropriate registers for BITPOS in REGCACHE and store it at BITPOS
713 in VALBUF. This function can be called recursively and therefore
714 handles floating types in addition to structures. */
715
716 static void
717 sparc64_extract_floating_fields (struct regcache *regcache, struct type *type,
718 gdb_byte *valbuf, int bitpos)
719 {
720 if (sparc64_floating_p (type))
721 {
722 int len = TYPE_LENGTH (type);
723 int regnum;
724
725 if (len == 16)
726 {
727 gdb_assert (bitpos == 0 || bitpos == 128);
728
729 regnum = SPARC64_Q0_REGNUM + bitpos / 128;
730 regcache_cooked_read (regcache, regnum, valbuf + (bitpos / 8));
731 }
732 else if (len == 8)
733 {
734 gdb_assert (bitpos % 64 == 0 && bitpos >= 0 && bitpos < 256);
735
736 regnum = SPARC64_D0_REGNUM + bitpos / 64;
737 regcache_cooked_read (regcache, regnum, valbuf + (bitpos / 8));
738 }
739 else
740 {
741 gdb_assert (len == 4);
742 gdb_assert (bitpos % 32 == 0 && bitpos >= 0 && bitpos < 256);
743
744 regnum = SPARC_F0_REGNUM + bitpos / 32;
745 regcache_cooked_read (regcache, regnum, valbuf + (bitpos / 8));
746 }
747 }
748 else if (sparc64_structure_or_union_p (type))
749 {
750 int i;
751
752 for (i = 0; i < TYPE_NFIELDS (type); i++)
753 {
754 struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
755 int subpos = bitpos + TYPE_FIELD_BITPOS (type, i);
756
757 sparc64_extract_floating_fields (regcache, subtype, valbuf, subpos);
758 }
759 }
760 }
761
762 /* Store the NARGS arguments ARGS and STRUCT_ADDR (if STRUCT_RETURN is
763 non-zero) in REGCACHE and on the stack (starting from address SP). */
764
765 static CORE_ADDR
766 sparc64_store_arguments (struct regcache *regcache, int nargs,
767 struct value **args, CORE_ADDR sp,
768 int struct_return, CORE_ADDR struct_addr)
769 {
770 struct gdbarch *gdbarch = get_regcache_arch (regcache);
771 /* Number of extended words in the "parameter array". */
772 int num_elements = 0;
773 int element = 0;
774 int i;
775
776 /* Take BIAS into account. */
777 sp += BIAS;
778
779 /* First we calculate the number of extended words in the "parameter
780 array". While doing so we also convert some of the arguments. */
781
782 if (struct_return)
783 num_elements++;
784
785 for (i = 0; i < nargs; i++)
786 {
787 struct type *type = value_type (args[i]);
788 int len = TYPE_LENGTH (type);
789
790 if (sparc64_structure_or_union_p (type)
791 || (sparc64_complex_floating_p (type) && len == 32))
792 {
793 /* Structure or Union arguments. */
794 if (len <= 16)
795 {
796 if (num_elements % 2 && sparc64_16_byte_align_p (type))
797 num_elements++;
798 num_elements += ((len + 7) / 8);
799 }
800 else
801 {
802 /* The psABI says that "Structures or unions larger than
803 sixteen bytes are copied by the caller and passed
804 indirectly; the caller will pass the address of a
805 correctly aligned structure value. This sixty-four
806 bit address will occupy one word in the parameter
807 array, and may be promoted to an %o register like any
808 other pointer value." Allocate memory for these
809 values on the stack. */
810 sp -= len;
811
812 /* Use 16-byte alignment for these values. That's
813 always correct, and wasting a few bytes shouldn't be
814 a problem. */
815 sp &= ~0xf;
816
817 write_memory (sp, value_contents (args[i]), len);
818 args[i] = value_from_pointer (lookup_pointer_type (type), sp);
819 num_elements++;
820 }
821 }
822 else if (sparc64_floating_p (type) || sparc64_complex_floating_p (type))
823 {
824 /* Floating arguments. */
825 if (len == 16)
826 {
827 /* The psABI says that "Each quad-precision parameter
828 value will be assigned to two extended words in the
829 parameter array. */
830 num_elements += 2;
831
832 /* The psABI says that "Long doubles must be
833 quad-aligned, and thus a hole might be introduced
834 into the parameter array to force alignment." Skip
835 an element if necessary. */
836 if ((num_elements % 2) && sparc64_16_byte_align_p (type))
837 num_elements++;
838 }
839 else
840 num_elements++;
841 }
842 else
843 {
844 /* Integral and pointer arguments. */
845 gdb_assert (sparc64_integral_or_pointer_p (type));
846
847 /* The psABI says that "Each argument value of integral type
848 smaller than an extended word will be widened by the
849 caller to an extended word according to the signed-ness
850 of the argument type." */
851 if (len < 8)
852 args[i] = value_cast (builtin_type (gdbarch)->builtin_int64,
853 args[i]);
854 num_elements++;
855 }
856 }
857
858 /* Allocate the "parameter array". */
859 sp -= num_elements * 8;
860
861 /* The psABI says that "Every stack frame must be 16-byte aligned." */
862 sp &= ~0xf;
863
864 /* Now we store the arguments in to the "paramater array". Some
865 Integer or Pointer arguments and Structure or Union arguments
866 will be passed in %o registers. Some Floating arguments and
867 floating members of structures are passed in floating-point
868 registers. However, for functions with variable arguments,
869 floating arguments are stored in an %0 register, and for
870 functions without a prototype floating arguments are stored in
871 both a floating-point and an %o registers, or a floating-point
872 register and memory. To simplify the logic here we always pass
873 arguments in memory, an %o register, and a floating-point
874 register if appropriate. This should be no problem since the
875 contents of any unused memory or registers in the "parameter
876 array" are undefined. */
877
878 if (struct_return)
879 {
880 regcache_cooked_write_unsigned (regcache, SPARC_O0_REGNUM, struct_addr);
881 element++;
882 }
883
884 for (i = 0; i < nargs; i++)
885 {
886 const gdb_byte *valbuf = value_contents (args[i]);
887 struct type *type = value_type (args[i]);
888 int len = TYPE_LENGTH (type);
889 int regnum = -1;
890 gdb_byte buf[16];
891
892 if (sparc64_structure_or_union_p (type)
893 || (sparc64_complex_floating_p (type) && len == 32))
894 {
895 /* Structure, Union or long double Complex arguments. */
896 gdb_assert (len <= 16);
897 memset (buf, 0, sizeof (buf));
898 valbuf = memcpy (buf, valbuf, len);
899
900 if (element % 2 && sparc64_16_byte_align_p (type))
901 element++;
902
903 if (element < 6)
904 {
905 regnum = SPARC_O0_REGNUM + element;
906 if (len > 8 && element < 5)
907 regcache_cooked_write (regcache, regnum + 1, valbuf + 8);
908 }
909
910 if (element < 16)
911 sparc64_store_floating_fields (regcache, type, valbuf, element, 0);
912 }
913 else if (sparc64_complex_floating_p (type))
914 {
915 /* Float Complex or double Complex arguments. */
916 if (element < 16)
917 {
918 regnum = SPARC64_D0_REGNUM + element;
919
920 if (len == 16)
921 {
922 if (regnum < SPARC64_D30_REGNUM)
923 regcache_cooked_write (regcache, regnum + 1, valbuf + 8);
924 if (regnum < SPARC64_D10_REGNUM)
925 regcache_cooked_write (regcache,
926 SPARC_O0_REGNUM + element + 1,
927 valbuf + 8);
928 }
929 }
930 }
931 else if (sparc64_floating_p (type))
932 {
933 /* Floating arguments. */
934 if (len == 16)
935 {
936 if (element % 2)
937 element++;
938 if (element < 16)
939 regnum = SPARC64_Q0_REGNUM + element / 2;
940 }
941 else if (len == 8)
942 {
943 if (element < 16)
944 regnum = SPARC64_D0_REGNUM + element;
945 }
946 else if (len == 4)
947 {
948 /* The psABI says "Each single-precision parameter value
949 will be assigned to one extended word in the
950 parameter array, and right-justified within that
951 word; the left half (even float register) is
952 undefined." Even though the psABI says that "the
953 left half is undefined", set it to zero here. */
954 memset (buf, 0, 4);
955 memcpy (buf + 4, valbuf, 4);
956 valbuf = buf;
957 len = 8;
958 if (element < 16)
959 regnum = SPARC64_D0_REGNUM + element;
960 }
961 }
962 else
963 {
964 /* Integral and pointer arguments. */
965 gdb_assert (len == 8);
966 if (element < 6)
967 regnum = SPARC_O0_REGNUM + element;
968 }
969
970 if (regnum != -1)
971 {
972 regcache_cooked_write (regcache, regnum, valbuf);
973
974 /* If we're storing the value in a floating-point register,
975 also store it in the corresponding %0 register(s). */
976 if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D10_REGNUM)
977 {
978 gdb_assert (element < 6);
979 regnum = SPARC_O0_REGNUM + element;
980 regcache_cooked_write (regcache, regnum, valbuf);
981 }
982 else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q8_REGNUM)
983 {
984 gdb_assert (element < 5);
985 regnum = SPARC_O0_REGNUM + element;
986 regcache_cooked_write (regcache, regnum, valbuf);
987 regcache_cooked_write (regcache, regnum + 1, valbuf + 8);
988 }
989 }
990
991 /* Always store the argument in memory. */
992 write_memory (sp + element * 8, valbuf, len);
993 element += ((len + 7) / 8);
994 }
995
996 gdb_assert (element == num_elements);
997
998 /* Take BIAS into account. */
999 sp -= BIAS;
1000 return sp;
1001 }
1002
1003 static CORE_ADDR
1004 sparc64_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
1005 {
1006 /* The ABI requires 16-byte alignment. */
1007 return address & ~0xf;
1008 }
1009
1010 static CORE_ADDR
1011 sparc64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
1012 struct regcache *regcache, CORE_ADDR bp_addr,
1013 int nargs, struct value **args, CORE_ADDR sp,
1014 int struct_return, CORE_ADDR struct_addr)
1015 {
1016 /* Set return address. */
1017 regcache_cooked_write_unsigned (regcache, SPARC_O7_REGNUM, bp_addr - 8);
1018
1019 /* Set up function arguments. */
1020 sp = sparc64_store_arguments (regcache, nargs, args, sp,
1021 struct_return, struct_addr);
1022
1023 /* Allocate the register save area. */
1024 sp -= 16 * 8;
1025
1026 /* Stack should be 16-byte aligned at this point. */
1027 gdb_assert ((sp + BIAS) % 16 == 0);
1028
1029 /* Finally, update the stack pointer. */
1030 regcache_cooked_write_unsigned (regcache, SPARC_SP_REGNUM, sp);
1031
1032 return sp + BIAS;
1033 }
1034
1035
1037 /* Extract from an array REGBUF containing the (raw) register state, a
1038 function return value of TYPE, and copy that into VALBUF. */
1039
1040 static void
1041 sparc64_extract_return_value (struct type *type, struct regcache *regcache,
1042 gdb_byte *valbuf)
1043 {
1044 int len = TYPE_LENGTH (type);
1045 gdb_byte buf[32];
1046 int i;
1047
1048 if (sparc64_structure_or_union_p (type))
1049 {
1050 /* Structure or Union return values. */
1051 gdb_assert (len <= 32);
1052
1053 for (i = 0; i < ((len + 7) / 8); i++)
1054 regcache_cooked_read (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
1055 if (TYPE_CODE (type) != TYPE_CODE_UNION)
1056 sparc64_extract_floating_fields (regcache, type, buf, 0);
1057 memcpy (valbuf, buf, len);
1058 }
1059 else if (sparc64_floating_p (type) || sparc64_complex_floating_p (type))
1060 {
1061 /* Floating return values. */
1062 for (i = 0; i < len / 4; i++)
1063 regcache_cooked_read (regcache, SPARC_F0_REGNUM + i, buf + i * 4);
1064 memcpy (valbuf, buf, len);
1065 }
1066 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1067 {
1068 /* Small arrays are returned the same way as small structures. */
1069 gdb_assert (len <= 32);
1070
1071 for (i = 0; i < ((len + 7) / 8); i++)
1072 regcache_cooked_read (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
1073 memcpy (valbuf, buf, len);
1074 }
1075 else
1076 {
1077 /* Integral and pointer return values. */
1078 gdb_assert (sparc64_integral_or_pointer_p (type));
1079
1080 /* Just stripping off any unused bytes should preserve the
1081 signed-ness just fine. */
1082 regcache_cooked_read (regcache, SPARC_O0_REGNUM, buf);
1083 memcpy (valbuf, buf + 8 - len, len);
1084 }
1085 }
1086
1087 /* Write into the appropriate registers a function return value stored
1088 in VALBUF of type TYPE. */
1089
1090 static void
1091 sparc64_store_return_value (struct type *type, struct regcache *regcache,
1092 const gdb_byte *valbuf)
1093 {
1094 int len = TYPE_LENGTH (type);
1095 gdb_byte buf[16];
1096 int i;
1097
1098 if (sparc64_structure_or_union_p (type))
1099 {
1100 /* Structure or Union return values. */
1101 gdb_assert (len <= 32);
1102
1103 /* Simplify matters by storing the complete value (including
1104 floating members) into %o0 and %o1. Floating members are
1105 also store in the appropriate floating-point registers. */
1106 memset (buf, 0, sizeof (buf));
1107 memcpy (buf, valbuf, len);
1108 for (i = 0; i < ((len + 7) / 8); i++)
1109 regcache_cooked_write (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
1110 if (TYPE_CODE (type) != TYPE_CODE_UNION)
1111 sparc64_store_floating_fields (regcache, type, buf, 0, 0);
1112 }
1113 else if (sparc64_floating_p (type) || sparc64_complex_floating_p (type))
1114 {
1115 /* Floating return values. */
1116 memcpy (buf, valbuf, len);
1117 for (i = 0; i < len / 4; i++)
1118 regcache_cooked_write (regcache, SPARC_F0_REGNUM + i, buf + i * 4);
1119 }
1120 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1121 {
1122 /* Small arrays are returned the same way as small structures. */
1123 gdb_assert (len <= 32);
1124
1125 memset (buf, 0, sizeof (buf));
1126 memcpy (buf, valbuf, len);
1127 for (i = 0; i < ((len + 7) / 8); i++)
1128 regcache_cooked_write (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
1129 }
1130 else
1131 {
1132 /* Integral and pointer return values. */
1133 gdb_assert (sparc64_integral_or_pointer_p (type));
1134
1135 /* ??? Do we need to do any sign-extension here? */
1136 memset (buf, 0, 8);
1137 memcpy (buf + 8 - len, valbuf, len);
1138 regcache_cooked_write (regcache, SPARC_O0_REGNUM, buf);
1139 }
1140 }
1141
1142 static enum return_value_convention
1143 sparc64_return_value (struct gdbarch *gdbarch, struct value *function,
1144 struct type *type, struct regcache *regcache,
1145 gdb_byte *readbuf, const gdb_byte *writebuf)
1146 {
1147 if (TYPE_LENGTH (type) > 32)
1148 return RETURN_VALUE_STRUCT_CONVENTION;
1149
1150 if (readbuf)
1151 sparc64_extract_return_value (type, regcache, readbuf);
1152 if (writebuf)
1153 sparc64_store_return_value (type, regcache, writebuf);
1154
1155 return RETURN_VALUE_REGISTER_CONVENTION;
1156 }
1157
1158
1160 static void
1161 sparc64_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
1162 struct dwarf2_frame_state_reg *reg,
1163 struct frame_info *this_frame)
1164 {
1165 switch (regnum)
1166 {
1167 case SPARC_G0_REGNUM:
1168 /* Since %g0 is always zero, there is no point in saving it, and
1169 people will be inclined omit it from the CFI. Make sure we
1170 don't warn about that. */
1171 reg->how = DWARF2_FRAME_REG_SAME_VALUE;
1172 break;
1173 case SPARC_SP_REGNUM:
1174 reg->how = DWARF2_FRAME_REG_CFA;
1175 break;
1176 case SPARC64_PC_REGNUM:
1177 reg->how = DWARF2_FRAME_REG_RA_OFFSET;
1178 reg->loc.offset = 8;
1179 break;
1180 case SPARC64_NPC_REGNUM:
1181 reg->how = DWARF2_FRAME_REG_RA_OFFSET;
1182 reg->loc.offset = 12;
1183 break;
1184 }
1185 }
1186
1187 void
1188 sparc64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1189 {
1190 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1191
1192 tdep->pc_regnum = SPARC64_PC_REGNUM;
1193 tdep->npc_regnum = SPARC64_NPC_REGNUM;
1194
1195 /* This is what all the fuss is about. */
1196 set_gdbarch_long_bit (gdbarch, 64);
1197 set_gdbarch_long_long_bit (gdbarch, 64);
1198 set_gdbarch_ptr_bit (gdbarch, 64);
1199
1200 set_gdbarch_num_regs (gdbarch, SPARC64_NUM_REGS);
1201 set_gdbarch_register_name (gdbarch, sparc64_register_name);
1202 set_gdbarch_register_type (gdbarch, sparc64_register_type);
1203 set_gdbarch_num_pseudo_regs (gdbarch, SPARC64_NUM_PSEUDO_REGS);
1204 set_gdbarch_pseudo_register_read (gdbarch, sparc64_pseudo_register_read);
1205 set_gdbarch_pseudo_register_write (gdbarch, sparc64_pseudo_register_write);
1206
1207 /* Register numbers of various important registers. */
1208 set_gdbarch_pc_regnum (gdbarch, SPARC64_PC_REGNUM); /* %pc */
1209
1210 /* Call dummy code. */
1211 set_gdbarch_frame_align (gdbarch, sparc64_frame_align);
1212 set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
1213 set_gdbarch_push_dummy_code (gdbarch, NULL);
1214 set_gdbarch_push_dummy_call (gdbarch, sparc64_push_dummy_call);
1215
1216 set_gdbarch_return_value (gdbarch, sparc64_return_value);
1217 set_gdbarch_stabs_argument_has_addr
1218 (gdbarch, default_stabs_argument_has_addr);
1219
1220 set_gdbarch_skip_prologue (gdbarch, sparc64_skip_prologue);
1221 set_gdbarch_stack_frame_destroyed_p (gdbarch, sparc_stack_frame_destroyed_p);
1222
1223 /* Hook in the DWARF CFI frame unwinder. */
1224 dwarf2_frame_set_init_reg (gdbarch, sparc64_dwarf2_frame_init_reg);
1225 /* FIXME: kettenis/20050423: Don't enable the unwinder until the
1226 StackGhost issues have been resolved. */
1227
1228 frame_unwind_append_unwinder (gdbarch, &sparc64_frame_unwind);
1229 frame_base_set_default (gdbarch, &sparc64_frame_base);
1230 }
1231
1232
1234 /* Helper functions for dealing with register sets. */
1235
1236 #define TSTATE_CWP 0x000000000000001fULL
1237 #define TSTATE_ICC 0x0000000f00000000ULL
1238 #define TSTATE_XCC 0x000000f000000000ULL
1239
1240 #define PSR_S 0x00000080
1241 #define PSR_ICC 0x00f00000
1242 #define PSR_VERS 0x0f000000
1243 #define PSR_IMPL 0xf0000000
1244 #define PSR_V8PLUS 0xff000000
1245 #define PSR_XCC 0x000f0000
1246
1247 void
1248 sparc64_supply_gregset (const struct sparc_gregmap *gregmap,
1249 struct regcache *regcache,
1250 int regnum, const void *gregs)
1251 {
1252 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1253 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1254 int sparc32 = (gdbarch_ptr_bit (gdbarch) == 32);
1255 const gdb_byte *regs = gregs;
1256 gdb_byte zero[8] = { 0 };
1257 int i;
1258
1259 if (sparc32)
1260 {
1261 if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
1262 {
1263 int offset = gregmap->r_tstate_offset;
1264 ULONGEST tstate, psr;
1265 gdb_byte buf[4];
1266
1267 tstate = extract_unsigned_integer (regs + offset, 8, byte_order);
1268 psr = ((tstate & TSTATE_CWP) | PSR_S | ((tstate & TSTATE_ICC) >> 12)
1269 | ((tstate & TSTATE_XCC) >> 20) | PSR_V8PLUS);
1270 store_unsigned_integer (buf, 4, byte_order, psr);
1271 regcache_raw_supply (regcache, SPARC32_PSR_REGNUM, buf);
1272 }
1273
1274 if (regnum == SPARC32_PC_REGNUM || regnum == -1)
1275 regcache_raw_supply (regcache, SPARC32_PC_REGNUM,
1276 regs + gregmap->r_pc_offset + 4);
1277
1278 if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
1279 regcache_raw_supply (regcache, SPARC32_NPC_REGNUM,
1280 regs + gregmap->r_npc_offset + 4);
1281
1282 if (regnum == SPARC32_Y_REGNUM || regnum == -1)
1283 {
1284 int offset = gregmap->r_y_offset + 8 - gregmap->r_y_size;
1285 regcache_raw_supply (regcache, SPARC32_Y_REGNUM, regs + offset);
1286 }
1287 }
1288 else
1289 {
1290 if (regnum == SPARC64_STATE_REGNUM || regnum == -1)
1291 regcache_raw_supply (regcache, SPARC64_STATE_REGNUM,
1292 regs + gregmap->r_tstate_offset);
1293
1294 if (regnum == SPARC64_PC_REGNUM || regnum == -1)
1295 regcache_raw_supply (regcache, SPARC64_PC_REGNUM,
1296 regs + gregmap->r_pc_offset);
1297
1298 if (regnum == SPARC64_NPC_REGNUM || regnum == -1)
1299 regcache_raw_supply (regcache, SPARC64_NPC_REGNUM,
1300 regs + gregmap->r_npc_offset);
1301
1302 if (regnum == SPARC64_Y_REGNUM || regnum == -1)
1303 {
1304 gdb_byte buf[8];
1305
1306 memset (buf, 0, 8);
1307 memcpy (buf + 8 - gregmap->r_y_size,
1308 regs + gregmap->r_y_offset, gregmap->r_y_size);
1309 regcache_raw_supply (regcache, SPARC64_Y_REGNUM, buf);
1310 }
1311
1312 if ((regnum == SPARC64_FPRS_REGNUM || regnum == -1)
1313 && gregmap->r_fprs_offset != -1)
1314 regcache_raw_supply (regcache, SPARC64_FPRS_REGNUM,
1315 regs + gregmap->r_fprs_offset);
1316 }
1317
1318 if (regnum == SPARC_G0_REGNUM || regnum == -1)
1319 regcache_raw_supply (regcache, SPARC_G0_REGNUM, &zero);
1320
1321 if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
1322 {
1323 int offset = gregmap->r_g1_offset;
1324
1325 if (sparc32)
1326 offset += 4;
1327
1328 for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
1329 {
1330 if (regnum == i || regnum == -1)
1331 regcache_raw_supply (regcache, i, regs + offset);
1332 offset += 8;
1333 }
1334 }
1335
1336 if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
1337 {
1338 /* Not all of the register set variants include Locals and
1339 Inputs. For those that don't, we read them off the stack. */
1340 if (gregmap->r_l0_offset == -1)
1341 {
1342 ULONGEST sp;
1343
1344 regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
1345 sparc_supply_rwindow (regcache, sp, regnum);
1346 }
1347 else
1348 {
1349 int offset = gregmap->r_l0_offset;
1350
1351 if (sparc32)
1352 offset += 4;
1353
1354 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1355 {
1356 if (regnum == i || regnum == -1)
1357 regcache_raw_supply (regcache, i, regs + offset);
1358 offset += 8;
1359 }
1360 }
1361 }
1362 }
1363
1364 void
1365 sparc64_collect_gregset (const struct sparc_gregmap *gregmap,
1366 const struct regcache *regcache,
1367 int regnum, void *gregs)
1368 {
1369 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1370 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1371 int sparc32 = (gdbarch_ptr_bit (gdbarch) == 32);
1372 gdb_byte *regs = gregs;
1373 int i;
1374
1375 if (sparc32)
1376 {
1377 if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
1378 {
1379 int offset = gregmap->r_tstate_offset;
1380 ULONGEST tstate, psr;
1381 gdb_byte buf[8];
1382
1383 tstate = extract_unsigned_integer (regs + offset, 8, byte_order);
1384 regcache_raw_collect (regcache, SPARC32_PSR_REGNUM, buf);
1385 psr = extract_unsigned_integer (buf, 4, byte_order);
1386 tstate |= (psr & PSR_ICC) << 12;
1387 if ((psr & (PSR_VERS | PSR_IMPL)) == PSR_V8PLUS)
1388 tstate |= (psr & PSR_XCC) << 20;
1389 store_unsigned_integer (buf, 8, byte_order, tstate);
1390 memcpy (regs + offset, buf, 8);
1391 }
1392
1393 if (regnum == SPARC32_PC_REGNUM || regnum == -1)
1394 regcache_raw_collect (regcache, SPARC32_PC_REGNUM,
1395 regs + gregmap->r_pc_offset + 4);
1396
1397 if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
1398 regcache_raw_collect (regcache, SPARC32_NPC_REGNUM,
1399 regs + gregmap->r_npc_offset + 4);
1400
1401 if (regnum == SPARC32_Y_REGNUM || regnum == -1)
1402 {
1403 int offset = gregmap->r_y_offset + 8 - gregmap->r_y_size;
1404 regcache_raw_collect (regcache, SPARC32_Y_REGNUM, regs + offset);
1405 }
1406 }
1407 else
1408 {
1409 if (regnum == SPARC64_STATE_REGNUM || regnum == -1)
1410 regcache_raw_collect (regcache, SPARC64_STATE_REGNUM,
1411 regs + gregmap->r_tstate_offset);
1412
1413 if (regnum == SPARC64_PC_REGNUM || regnum == -1)
1414 regcache_raw_collect (regcache, SPARC64_PC_REGNUM,
1415 regs + gregmap->r_pc_offset);
1416
1417 if (regnum == SPARC64_NPC_REGNUM || regnum == -1)
1418 regcache_raw_collect (regcache, SPARC64_NPC_REGNUM,
1419 regs + gregmap->r_npc_offset);
1420
1421 if (regnum == SPARC64_Y_REGNUM || regnum == -1)
1422 {
1423 gdb_byte buf[8];
1424
1425 regcache_raw_collect (regcache, SPARC64_Y_REGNUM, buf);
1426 memcpy (regs + gregmap->r_y_offset,
1427 buf + 8 - gregmap->r_y_size, gregmap->r_y_size);
1428 }
1429
1430 if ((regnum == SPARC64_FPRS_REGNUM || regnum == -1)
1431 && gregmap->r_fprs_offset != -1)
1432 regcache_raw_collect (regcache, SPARC64_FPRS_REGNUM,
1433 regs + gregmap->r_fprs_offset);
1434
1435 }
1436
1437 if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
1438 {
1439 int offset = gregmap->r_g1_offset;
1440
1441 if (sparc32)
1442 offset += 4;
1443
1444 /* %g0 is always zero. */
1445 for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
1446 {
1447 if (regnum == i || regnum == -1)
1448 regcache_raw_collect (regcache, i, regs + offset);
1449 offset += 8;
1450 }
1451 }
1452
1453 if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
1454 {
1455 /* Not all of the register set variants include Locals and
1456 Inputs. For those that don't, we read them off the stack. */
1457 if (gregmap->r_l0_offset != -1)
1458 {
1459 int offset = gregmap->r_l0_offset;
1460
1461 if (sparc32)
1462 offset += 4;
1463
1464 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1465 {
1466 if (regnum == i || regnum == -1)
1467 regcache_raw_collect (regcache, i, regs + offset);
1468 offset += 8;
1469 }
1470 }
1471 }
1472 }
1473
1474 void
1475 sparc64_supply_fpregset (const struct sparc_fpregmap *fpregmap,
1476 struct regcache *regcache,
1477 int regnum, const void *fpregs)
1478 {
1479 int sparc32 = (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 32);
1480 const gdb_byte *regs = fpregs;
1481 int i;
1482
1483 for (i = 0; i < 32; i++)
1484 {
1485 if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
1486 regcache_raw_supply (regcache, SPARC_F0_REGNUM + i,
1487 regs + fpregmap->r_f0_offset + (i * 4));
1488 }
1489
1490 if (sparc32)
1491 {
1492 if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
1493 regcache_raw_supply (regcache, SPARC32_FSR_REGNUM,
1494 regs + fpregmap->r_fsr_offset);
1495 }
1496 else
1497 {
1498 for (i = 0; i < 16; i++)
1499 {
1500 if (regnum == (SPARC64_F32_REGNUM + i) || regnum == -1)
1501 regcache_raw_supply (regcache, SPARC64_F32_REGNUM + i,
1502 (regs + fpregmap->r_f0_offset
1503 + (32 * 4) + (i * 8)));
1504 }
1505
1506 if (regnum == SPARC64_FSR_REGNUM || regnum == -1)
1507 regcache_raw_supply (regcache, SPARC64_FSR_REGNUM,
1508 regs + fpregmap->r_fsr_offset);
1509 }
1510 }
1511
1512 void
1513 sparc64_collect_fpregset (const struct sparc_fpregmap *fpregmap,
1514 const struct regcache *regcache,
1515 int regnum, void *fpregs)
1516 {
1517 int sparc32 = (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 32);
1518 gdb_byte *regs = fpregs;
1519 int i;
1520
1521 for (i = 0; i < 32; i++)
1522 {
1523 if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
1524 regcache_raw_collect (regcache, SPARC_F0_REGNUM + i,
1525 regs + fpregmap->r_f0_offset + (i * 4));
1526 }
1527
1528 if (sparc32)
1529 {
1530 if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
1531 regcache_raw_collect (regcache, SPARC32_FSR_REGNUM,
1532 regs + fpregmap->r_fsr_offset);
1533 }
1534 else
1535 {
1536 for (i = 0; i < 16; i++)
1537 {
1538 if (regnum == (SPARC64_F32_REGNUM + i) || regnum == -1)
1539 regcache_raw_collect (regcache, SPARC64_F32_REGNUM + i,
1540 (regs + fpregmap->r_f0_offset
1541 + (32 * 4) + (i * 8)));
1542 }
1543
1544 if (regnum == SPARC64_FSR_REGNUM || regnum == -1)
1545 regcache_raw_collect (regcache, SPARC64_FSR_REGNUM,
1546 regs + fpregmap->r_fsr_offset);
1547 }
1548 }
1549
1550 const struct sparc_fpregmap sparc64_bsd_fpregmap =
1551 {
1552 0 * 8, /* %f0 */
1553 32 * 8, /* %fsr */
1554 };
1555