1 /* Target-dependent code for GDB, the GNU debugger.
3 Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 Contributed by D.J. Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
6 for IBM Deutschland Entwicklung GmbH, IBM Corporation.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #include "arch-utils.h"
35 #include "../bfd/bfd.h"
36 #include "floatformat.h"
38 #include "reggroups.h"
41 #include "gdb_assert.h"
44 #include "s390-tdep.h"
47 /* The tdep structure. */
52 enum { ABI_LINUX_S390, ABI_LINUX_ZSERIES } abi;
54 /* Core file register sets. */
55 const struct regset *gregset;
58 const struct regset *fpregset;
63 /* Register information. */
65 struct s390_register_info
71 static struct s390_register_info s390_register_info[S390_NUM_TOTAL_REGS] =
73 /* Program Status Word. */
74 { "pswm", &builtin_type_long },
75 { "pswa", &builtin_type_long },
77 /* General Purpose Registers. */
78 { "r0", &builtin_type_long },
79 { "r1", &builtin_type_long },
80 { "r2", &builtin_type_long },
81 { "r3", &builtin_type_long },
82 { "r4", &builtin_type_long },
83 { "r5", &builtin_type_long },
84 { "r6", &builtin_type_long },
85 { "r7", &builtin_type_long },
86 { "r8", &builtin_type_long },
87 { "r9", &builtin_type_long },
88 { "r10", &builtin_type_long },
89 { "r11", &builtin_type_long },
90 { "r12", &builtin_type_long },
91 { "r13", &builtin_type_long },
92 { "r14", &builtin_type_long },
93 { "r15", &builtin_type_long },
95 /* Access Registers. */
96 { "acr0", &builtin_type_int },
97 { "acr1", &builtin_type_int },
98 { "acr2", &builtin_type_int },
99 { "acr3", &builtin_type_int },
100 { "acr4", &builtin_type_int },
101 { "acr5", &builtin_type_int },
102 { "acr6", &builtin_type_int },
103 { "acr7", &builtin_type_int },
104 { "acr8", &builtin_type_int },
105 { "acr9", &builtin_type_int },
106 { "acr10", &builtin_type_int },
107 { "acr11", &builtin_type_int },
108 { "acr12", &builtin_type_int },
109 { "acr13", &builtin_type_int },
110 { "acr14", &builtin_type_int },
111 { "acr15", &builtin_type_int },
113 /* Floating Point Control Word. */
114 { "fpc", &builtin_type_int },
116 /* Floating Point Registers. */
117 { "f0", &builtin_type_double },
118 { "f1", &builtin_type_double },
119 { "f2", &builtin_type_double },
120 { "f3", &builtin_type_double },
121 { "f4", &builtin_type_double },
122 { "f5", &builtin_type_double },
123 { "f6", &builtin_type_double },
124 { "f7", &builtin_type_double },
125 { "f8", &builtin_type_double },
126 { "f9", &builtin_type_double },
127 { "f10", &builtin_type_double },
128 { "f11", &builtin_type_double },
129 { "f12", &builtin_type_double },
130 { "f13", &builtin_type_double },
131 { "f14", &builtin_type_double },
132 { "f15", &builtin_type_double },
134 /* Pseudo registers. */
135 { "pc", &builtin_type_void_func_ptr },
136 { "cc", &builtin_type_int },
139 /* Return the name of register REGNUM. */
141 s390_register_name (int regnum)
143 gdb_assert (regnum >= 0 && regnum < S390_NUM_TOTAL_REGS);
144 return s390_register_info[regnum].name;
147 /* Return the GDB type object for the "standard" data type of data in
150 s390_register_type (struct gdbarch *gdbarch, int regnum)
152 gdb_assert (regnum >= 0 && regnum < S390_NUM_TOTAL_REGS);
153 return *s390_register_info[regnum].type;
156 /* DWARF Register Mapping. */
158 static int s390_dwarf_regmap[] =
160 /* General Purpose Registers. */
161 S390_R0_REGNUM, S390_R1_REGNUM, S390_R2_REGNUM, S390_R3_REGNUM,
162 S390_R4_REGNUM, S390_R5_REGNUM, S390_R6_REGNUM, S390_R7_REGNUM,
163 S390_R8_REGNUM, S390_R9_REGNUM, S390_R10_REGNUM, S390_R11_REGNUM,
164 S390_R12_REGNUM, S390_R13_REGNUM, S390_R14_REGNUM, S390_R15_REGNUM,
166 /* Floating Point Registers. */
167 S390_F0_REGNUM, S390_F2_REGNUM, S390_F4_REGNUM, S390_F6_REGNUM,
168 S390_F1_REGNUM, S390_F3_REGNUM, S390_F5_REGNUM, S390_F7_REGNUM,
169 S390_F8_REGNUM, S390_F10_REGNUM, S390_F12_REGNUM, S390_F14_REGNUM,
170 S390_F9_REGNUM, S390_F11_REGNUM, S390_F13_REGNUM, S390_F15_REGNUM,
172 /* Control Registers (not mapped). */
173 -1, -1, -1, -1, -1, -1, -1, -1,
174 -1, -1, -1, -1, -1, -1, -1, -1,
176 /* Access Registers. */
177 S390_A0_REGNUM, S390_A1_REGNUM, S390_A2_REGNUM, S390_A3_REGNUM,
178 S390_A4_REGNUM, S390_A5_REGNUM, S390_A6_REGNUM, S390_A7_REGNUM,
179 S390_A8_REGNUM, S390_A9_REGNUM, S390_A10_REGNUM, S390_A11_REGNUM,
180 S390_A12_REGNUM, S390_A13_REGNUM, S390_A14_REGNUM, S390_A15_REGNUM,
182 /* Program Status Word. */
187 /* Convert DWARF register number REG to the appropriate register
188 number used by GDB. */
190 s390_dwarf_reg_to_regnum (int reg)
194 if (reg >= 0 || reg < ARRAY_SIZE (s390_dwarf_regmap))
195 regnum = s390_dwarf_regmap[reg];
198 warning ("Unmapped DWARF Register #%d encountered\n", reg);
203 /* Pseudo registers - PC and condition code. */
206 s390_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
207 int regnum, void *buf)
214 regcache_raw_read_unsigned (regcache, S390_PSWA_REGNUM, &val);
215 store_unsigned_integer (buf, 4, val & 0x7fffffff);
219 regcache_raw_read_unsigned (regcache, S390_PSWM_REGNUM, &val);
220 store_unsigned_integer (buf, 4, (val >> 12) & 3);
224 internal_error (__FILE__, __LINE__, "invalid regnum");
229 s390_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
230 int regnum, const void *buf)
237 val = extract_unsigned_integer (buf, 4);
238 regcache_raw_read_unsigned (regcache, S390_PSWA_REGNUM, &psw);
239 psw = (psw & 0x80000000) | (val & 0x7fffffff);
240 regcache_raw_write_unsigned (regcache, S390_PSWA_REGNUM, psw);
244 val = extract_unsigned_integer (buf, 4);
245 regcache_raw_read_unsigned (regcache, S390_PSWM_REGNUM, &psw);
246 psw = (psw & ~((ULONGEST)3 << 12)) | ((val & 3) << 12);
247 regcache_raw_write_unsigned (regcache, S390_PSWM_REGNUM, psw);
251 internal_error (__FILE__, __LINE__, "invalid regnum");
256 s390x_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
257 int regnum, void *buf)
264 regcache_raw_read (regcache, S390_PSWA_REGNUM, buf);
268 regcache_raw_read_unsigned (regcache, S390_PSWM_REGNUM, &val);
269 store_unsigned_integer (buf, 4, (val >> 44) & 3);
273 internal_error (__FILE__, __LINE__, "invalid regnum");
278 s390x_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
279 int regnum, const void *buf)
286 regcache_raw_write (regcache, S390_PSWA_REGNUM, buf);
290 val = extract_unsigned_integer (buf, 4);
291 regcache_raw_read_unsigned (regcache, S390_PSWM_REGNUM, &psw);
292 psw = (psw & ~((ULONGEST)3 << 44)) | ((val & 3) << 44);
293 regcache_raw_write_unsigned (regcache, S390_PSWM_REGNUM, psw);
297 internal_error (__FILE__, __LINE__, "invalid regnum");
301 /* 'float' values are stored in the upper half of floating-point
302 registers, even though we are otherwise a big-endian platform. */
305 s390_convert_register_p (int regno, struct type *type)
307 return (regno >= S390_F0_REGNUM && regno <= S390_F15_REGNUM)
308 && TYPE_LENGTH (type) < 8;
312 s390_register_to_value (struct frame_info *frame, int regnum,
313 struct type *valtype, void *out)
316 int len = TYPE_LENGTH (valtype);
317 gdb_assert (len < 8);
319 get_frame_register (frame, regnum, in);
320 memcpy (out, in, len);
324 s390_value_to_register (struct frame_info *frame, int regnum,
325 struct type *valtype, const void *in)
328 int len = TYPE_LENGTH (valtype);
329 gdb_assert (len < 8);
332 memcpy (out, in, len);
333 put_frame_register (frame, regnum, out);
336 /* Register groups. */
339 s390_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
340 struct reggroup *group)
342 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
344 /* Registers displayed via 'info regs'. */
345 if (group == general_reggroup)
346 return (regnum >= S390_R0_REGNUM && regnum <= S390_R15_REGNUM)
347 || regnum == S390_PC_REGNUM
348 || regnum == S390_CC_REGNUM;
350 /* Registers displayed via 'info float'. */
351 if (group == float_reggroup)
352 return (regnum >= S390_F0_REGNUM && regnum <= S390_F15_REGNUM)
353 || regnum == S390_FPC_REGNUM;
355 /* Registers that need to be saved/restored in order to
356 push or pop frames. */
357 if (group == save_reggroup || group == restore_reggroup)
358 return regnum != S390_PSWM_REGNUM && regnum != S390_PSWA_REGNUM;
360 return default_register_reggroup_p (gdbarch, regnum, group);
364 /* Core file register sets. */
366 int s390_regmap_gregset[S390_NUM_REGS] =
368 /* Program Status Word. */
370 /* General Purpose Registers. */
371 0x08, 0x0c, 0x10, 0x14,
372 0x18, 0x1c, 0x20, 0x24,
373 0x28, 0x2c, 0x30, 0x34,
374 0x38, 0x3c, 0x40, 0x44,
375 /* Access Registers. */
376 0x48, 0x4c, 0x50, 0x54,
377 0x58, 0x5c, 0x60, 0x64,
378 0x68, 0x6c, 0x70, 0x74,
379 0x78, 0x7c, 0x80, 0x84,
380 /* Floating Point Control Word. */
382 /* Floating Point Registers. */
383 -1, -1, -1, -1, -1, -1, -1, -1,
384 -1, -1, -1, -1, -1, -1, -1, -1,
387 int s390x_regmap_gregset[S390_NUM_REGS] =
390 /* General Purpose Registers. */
391 0x10, 0x18, 0x20, 0x28,
392 0x30, 0x38, 0x40, 0x48,
393 0x50, 0x58, 0x60, 0x68,
394 0x70, 0x78, 0x80, 0x88,
395 /* Access Registers. */
396 0x90, 0x94, 0x98, 0x9c,
397 0xa0, 0xa4, 0xa8, 0xac,
398 0xb0, 0xb4, 0xb8, 0xbc,
399 0xc0, 0xc4, 0xc8, 0xcc,
400 /* Floating Point Control Word. */
402 /* Floating Point Registers. */
403 -1, -1, -1, -1, -1, -1, -1, -1,
404 -1, -1, -1, -1, -1, -1, -1, -1,
407 int s390_regmap_fpregset[S390_NUM_REGS] =
409 /* Program Status Word. */
411 /* General Purpose Registers. */
412 -1, -1, -1, -1, -1, -1, -1, -1,
413 -1, -1, -1, -1, -1, -1, -1, -1,
414 /* Access Registers. */
415 -1, -1, -1, -1, -1, -1, -1, -1,
416 -1, -1, -1, -1, -1, -1, -1, -1,
417 /* Floating Point Control Word. */
419 /* Floating Point Registers. */
420 0x08, 0x10, 0x18, 0x20,
421 0x28, 0x30, 0x38, 0x40,
422 0x48, 0x50, 0x58, 0x60,
423 0x68, 0x70, 0x78, 0x80,
426 /* Supply register REGNUM from the register set REGSET to register cache
427 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
429 s390_supply_regset (const struct regset *regset, struct regcache *regcache,
430 int regnum, const void *regs, size_t len)
432 const int *offset = regset->descr;
435 for (i = 0; i < S390_NUM_REGS; i++)
437 if ((regnum == i || regnum == -1) && offset[i] != -1)
438 regcache_raw_supply (regcache, i, (const char *)regs + offset[i]);
442 static const struct regset s390_gregset = {
447 static const struct regset s390x_gregset = {
448 s390x_regmap_gregset,
452 static const struct regset s390_fpregset = {
453 s390_regmap_fpregset,
457 /* Return the appropriate register set for the core section identified
458 by SECT_NAME and SECT_SIZE. */
459 const struct regset *
460 s390_regset_from_core_section (struct gdbarch *gdbarch,
461 const char *sect_name, size_t sect_size)
463 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
465 if (strcmp (sect_name, ".reg") == 0 && sect_size == tdep->sizeof_gregset)
466 return tdep->gregset;
468 if (strcmp (sect_name, ".reg2") == 0 && sect_size == tdep->sizeof_fpregset)
469 return tdep->fpregset;
475 #define GDB_TARGET_IS_ESAME (TARGET_ARCHITECTURE->mach == bfd_mach_s390_64)
476 #define S390_GPR_SIZE (GDB_TARGET_IS_ESAME ? 8 : 4)
477 #define S390_FPR_SIZE (8)
478 #define S390_MAX_INSTR_SIZE (6)
479 #define S390_SYSCALL_OPCODE (0x0a)
480 #define S390_SYSCALL_SIZE (2)
481 #define S390_SIGCONTEXT_SREGS_OFFSET (8)
482 #define S390X_SIGCONTEXT_SREGS_OFFSET (8)
483 #define S390_SIGREGS_FP0_OFFSET (144)
484 #define S390X_SIGREGS_FP0_OFFSET (216)
485 #define S390_UC_MCONTEXT_OFFSET (256)
486 #define S390X_UC_MCONTEXT_OFFSET (344)
487 #define S390_SIGNAL_FRAMESIZE (GDB_TARGET_IS_ESAME ? 160:96)
488 #define s390_NR_sigreturn 119
489 #define s390_NR_rt_sigreturn 173
493 struct frame_extra_info
497 CORE_ADDR function_start;
498 CORE_ADDR skip_prologue_function_start;
499 CORE_ADDR saved_pc_valid;
501 CORE_ADDR sig_fixed_saved_pc_valid;
502 CORE_ADDR sig_fixed_saved_pc;
503 CORE_ADDR frame_pointer_saved_pc; /* frame pointer needed for alloca */
504 CORE_ADDR stack_bought_valid;
505 CORE_ADDR stack_bought; /* amount we decrement the stack pointer by */
506 CORE_ADDR sigcontext;
510 static CORE_ADDR s390_frame_saved_pc_nofix (struct frame_info *fi);
513 s390_readinstruction (bfd_byte instr[], CORE_ADDR at)
517 static int s390_instrlen[] = {
523 if (target_read_memory (at, &instr[0], 2))
525 instrlen = s390_instrlen[instr[0] >> 6];
528 if (target_read_memory (at + 2, &instr[2], instrlen - 2))
535 s390_memset_extra_info (struct frame_extra_info *fextra_info)
537 memset (fextra_info, 0, sizeof (struct frame_extra_info));
541 /* Prologue analysis. */
543 /* When we analyze a prologue, we're really doing 'abstract
544 interpretation' or 'pseudo-evaluation': running the function's code
545 in simulation, but using conservative approximations of the values
546 it would have when it actually runs. For example, if our function
547 starts with the instruction:
549 ahi r1, 42 # add halfword immediate 42 to r1
551 we don't know exactly what value will be in r1 after executing this
552 instruction, but we do know it'll be 42 greater than its original
555 If we then see an instruction like:
557 ahi r1, 22 # add halfword immediate 22 to r1
559 we still don't know what r1's value is, but again, we can say it is
560 now 64 greater than its original value.
562 If the next instruction were:
564 lr r2, r1 # set r2 to r1's value
566 then we can say that r2's value is now the original value of r1
569 Of course, this can only go so far before it gets unreasonable. If
570 we wanted to be able to say anything about the value of r1 after
573 xr r1, r3 # exclusive-or r1 and r3, place result in r1
575 then things would get pretty complex. But remember, we're just
576 doing a conservative approximation; if exclusive-or instructions
577 aren't relevant to prologues, we can just say r1's value is now
578 'unknown'. We can ignore things that are too complex, if that loss
579 of information is acceptable for our application.
581 Once you've reached an instruction that you don't know how to
582 simulate, you stop. Now you examine the state of the registers and
583 stack slots you've kept track of. For example:
585 - To see how large your stack frame is, just check the value of sp;
586 if it's the original value of sp minus a constant, then that
587 constant is the stack frame's size. If the sp's value has been
588 marked as 'unknown', then that means the prologue has done
589 something too complex for us to track, and we don't know the
592 - To see whether we've saved the SP in the current frame's back
593 chain slot, we just check whether the current value of the back
594 chain stack slot is the original value of the sp.
596 Sure, this takes some work. But prologue analyzers aren't
597 quick-and-simple pattern patching to recognize a few fixed prologue
598 forms any more; they're big, hairy functions. Along with inferior
599 function calls, prologue analysis accounts for a substantial
600 portion of the time needed to stabilize a GDB port. So I think
601 it's worthwhile to look for an approach that will be easier to
602 understand and maintain. In the approach used here:
604 - It's easier to see that the analyzer is correct: you just see
605 whether the analyzer properly (albiet conservatively) simulates
606 the effect of each instruction.
608 - It's easier to extend the analyzer: you can add support for new
609 instructions, and know that you haven't broken anything that
610 wasn't already broken before.
612 - It's orthogonal: to gather new information, you don't need to
613 complicate the code for each instruction. As long as your domain
614 of conservative values is already detailed enough to tell you
615 what you need, then all the existing instruction simulations are
616 already gathering the right data for you.
618 A 'struct prologue_value' is a conservative approximation of the
619 real value the register or stack slot will have. */
621 struct prologue_value {
623 /* What sort of value is this? This determines the interpretation
624 of subsequent fields. */
627 /* We don't know anything about the value. This is also used for
628 values we could have kept track of, when doing so would have
629 been too complex and we don't want to bother. The bottom of
633 /* A known constant. K is its value. */
636 /* The value that register REG originally had *UPON ENTRY TO THE
637 FUNCTION*, plus K. If K is zero, this means, obviously, just
638 the value REG had upon entry to the function. REG is a GDB
639 register number. Before we start interpreting, we initialize
640 every register R to { pv_register, R, 0 }. */
645 /* The meanings of the following fields depend on 'kind'; see the
646 comments for the specific 'kind' values. */
652 /* Set V to be unknown. */
654 pv_set_to_unknown (struct prologue_value *v)
656 v->kind = pv_unknown;
660 /* Set V to the constant K. */
662 pv_set_to_constant (struct prologue_value *v, CORE_ADDR k)
664 v->kind = pv_constant;
669 /* Set V to the original value of register REG, plus K. */
671 pv_set_to_register (struct prologue_value *v, int reg, CORE_ADDR k)
673 v->kind = pv_register;
679 /* If one of *A and *B is a constant, and the other isn't, swap the
680 pointers as necessary to ensure that *B points to the constant.
681 This can reduce the number of cases we need to analyze in the
684 pv_constant_last (struct prologue_value **a,
685 struct prologue_value **b)
687 if ((*a)->kind == pv_constant
688 && (*b)->kind != pv_constant)
690 struct prologue_value *temp = *a;
697 /* Set SUM to the sum of A and B. SUM, A, and B may point to the same
698 'struct prologue_value' object. */
700 pv_add (struct prologue_value *sum,
701 struct prologue_value *a,
702 struct prologue_value *b)
704 pv_constant_last (&a, &b);
706 /* We can handle adding constants to registers, and other constants. */
707 if (b->kind == pv_constant
708 && (a->kind == pv_register
709 || a->kind == pv_constant))
712 sum->reg = a->reg; /* not meaningful if a is pv_constant, but
714 sum->k = a->k + b->k;
717 /* Anything else we don't know how to add. We don't have a
718 representation for, say, the sum of two registers, or a multiple
719 of a register's value (adding a register to itself). */
721 sum->kind = pv_unknown;
725 /* Add the constant K to V. */
727 pv_add_constant (struct prologue_value *v, CORE_ADDR k)
729 struct prologue_value pv_k;
731 /* Rather than thinking of all the cases we can and can't handle,
732 we'll just let pv_add take care of that for us. */
733 pv_set_to_constant (&pv_k, k);
734 pv_add (v, v, &pv_k);
738 /* Subtract B from A, and put the result in DIFF.
740 This isn't quite the same as negating B and adding it to A, since
741 we don't have a representation for the negation of anything but a
742 constant. For example, we can't negate { pv_register, R1, 10 },
743 but we do know that { pv_register, R1, 10 } minus { pv_register,
744 R1, 5 } is { pv_constant, <ignored>, 5 }.
746 This means, for example, that we can subtract two stack addresses;
747 they're both relative to the original SP. Since the frame pointer
748 is set based on the SP, its value will be the original SP plus some
749 constant (probably zero), so we can use its value just fine. */
751 pv_subtract (struct prologue_value *diff,
752 struct prologue_value *a,
753 struct prologue_value *b)
755 pv_constant_last (&a, &b);
757 /* We can subtract a constant from another constant, or from a
759 if (b->kind == pv_constant
760 && (a->kind == pv_register
761 || a->kind == pv_constant))
763 diff->kind = a->kind;
764 diff->reg = a->reg; /* not always meaningful, but harmless */
765 diff->k = a->k - b->k;
768 /* We can subtract a register from itself, yielding a constant. */
769 else if (a->kind == pv_register
770 && b->kind == pv_register
773 diff->kind = pv_constant;
774 diff->k = a->k - b->k;
777 /* We don't know how to subtract anything else. */
779 diff->kind = pv_unknown;
783 /* Set AND to the logical and of A and B. */
785 pv_logical_and (struct prologue_value *and,
786 struct prologue_value *a,
787 struct prologue_value *b)
789 pv_constant_last (&a, &b);
791 /* We can 'and' two constants. */
792 if (a->kind == pv_constant
793 && b->kind == pv_constant)
795 and->kind = pv_constant;
796 and->k = a->k & b->k;
799 /* We can 'and' anything with the constant zero. */
800 else if (b->kind == pv_constant
803 and->kind = pv_constant;
807 /* We can 'and' anything with ~0. */
808 else if (b->kind == pv_constant
809 && b->k == ~ (CORE_ADDR) 0)
812 /* We can 'and' a register with itself. */
813 else if (a->kind == pv_register
814 && b->kind == pv_register
819 /* Otherwise, we don't know. */
821 pv_set_to_unknown (and);
825 /* Return non-zero iff A and B are identical expressions.
827 This is not the same as asking if the two values are equal; the
828 result of such a comparison would have to be a pv_boolean, and
829 asking whether two 'unknown' values were equal would give you
830 pv_maybe. Same for comparing, say, { pv_register, R1, 0 } and {
831 pv_register, R2, 0}. Instead, this is asking whether the two
832 representations are the same. */
834 pv_is_identical (struct prologue_value *a,
835 struct prologue_value *b)
837 if (a->kind != b->kind)
845 return (a->k == b->k);
847 return (a->reg == b->reg && a->k == b->k);
854 /* Return non-zero if A is the original value of register number R
855 plus K, zero otherwise. */
857 pv_is_register (struct prologue_value *a, int r, CORE_ADDR k)
859 return (a->kind == pv_register
865 /* A prologue-value-esque boolean type, including "maybe", when we
866 can't figure out whether something is true or not. */
874 /* Decide whether a reference to SIZE bytes at ADDR refers exactly to
875 an element of an array. The array starts at ARRAY_ADDR, and has
876 ARRAY_LEN values of ELT_SIZE bytes each. If ADDR definitely does
877 refer to an array element, set *I to the index of the referenced
878 element in the array, and return pv_definite_yes. If it definitely
879 doesn't, return pv_definite_no. If we can't tell, return pv_maybe.
881 If the reference does touch the array, but doesn't fall exactly on
882 an element boundary, or doesn't refer to the whole element, return
884 static enum pv_boolean
885 pv_is_array_ref (struct prologue_value *addr,
887 struct prologue_value *array_addr,
892 struct prologue_value offset;
894 /* Note that, since ->k is a CORE_ADDR, and CORE_ADDR is unsigned,
895 if addr is *before* the start of the array, then this isn't going
897 pv_subtract (&offset, addr, array_addr);
899 if (offset.kind == pv_constant)
901 /* This is a rather odd test. We want to know if the SIZE bytes
902 at ADDR don't overlap the array at all, so you'd expect it to
903 be an || expression: "if we're completely before || we're
904 completely after". But with unsigned arithmetic, things are
905 different: since it's a number circle, not a number line, the
906 right values for offset.k are actually one contiguous range. */
907 if (offset.k <= -size
908 && offset.k >= array_len * elt_size)
909 return pv_definite_no;
910 else if (offset.k % elt_size != 0
915 *i = offset.k / elt_size;
916 return pv_definite_yes;
925 /* Decoding S/390 instructions. */
927 /* Named opcode values for the S/390 instructions we recognize. Some
928 instructions have their opcode split across two fields; those are the
929 op1_* and op2_* enums. */
932 op1_aghi = 0xa7, op2_aghi = 0xb,
933 op1_ahi = 0xa7, op2_ahi = 0xa,
936 op1_bras = 0xa7, op2_bras = 0x5,
939 op1_larl = 0xc0, op2_larl = 0x0,
941 op1_lghi = 0xa7, op2_lghi = 0x9,
942 op1_lhi = 0xa7, op2_lhi = 0x8,
949 op1_stg = 0xe3, op2_stg = 0x24,
951 op1_stmg = 0xeb, op2_stmg = 0x24,
953 op1_lmg = 0xeb, op2_lmg = 0x04,
958 /* The functions below are for recognizing and decoding S/390
959 instructions of various formats. Each of them checks whether INSN
960 is an instruction of the given format, with the specified opcodes.
961 If it is, it sets the remaining arguments to the values of the
962 instruction's fields, and returns a non-zero value; otherwise, it
965 These functions' arguments appear in the order they appear in the
966 instruction, not in the machine-language form. So, opcodes always
967 come first, even though they're sometimes scattered around the
968 instructions. And displacements appear before base and extension
969 registers, as they do in the assembly syntax, not at the end, as
970 they do in the machine language. */
972 is_ri (bfd_byte *insn, int op1, int op2, unsigned int *r1, int *i2)
974 if (insn[0] == op1 && (insn[1] & 0xf) == op2)
976 *r1 = (insn[1] >> 4) & 0xf;
977 /* i2 is a 16-bit signed quantity. */
978 *i2 = (((insn[2] << 8) | insn[3]) ^ 0x8000) - 0x8000;
987 is_ril (bfd_byte *insn, int op1, int op2,
988 unsigned int *r1, int *i2)
990 if (insn[0] == op1 && (insn[1] & 0xf) == op2)
992 *r1 = (insn[1] >> 4) & 0xf;
993 /* i2 is a signed quantity. If the host 'int' is 32 bits long,
994 no sign extension is necessary, but we don't want to assume
996 *i2 = (((insn[2] << 24)
999 | (insn[5])) ^ 0x80000000) - 0x80000000;
1008 is_rr (bfd_byte *insn, int op, unsigned int *r1, unsigned int *r2)
1012 *r1 = (insn[1] >> 4) & 0xf;
1013 *r2 = insn[1] & 0xf;
1022 is_rre (bfd_byte *insn, int op, unsigned int *r1, unsigned int *r2)
1024 if (((insn[0] << 8) | insn[1]) == op)
1026 /* Yes, insn[3]. insn[2] is unused in RRE format. */
1027 *r1 = (insn[3] >> 4) & 0xf;
1028 *r2 = insn[3] & 0xf;
1037 is_rs (bfd_byte *insn, int op,
1038 unsigned int *r1, unsigned int *r3, unsigned int *d2, unsigned int *b2)
1042 *r1 = (insn[1] >> 4) & 0xf;
1043 *r3 = insn[1] & 0xf;
1044 *b2 = (insn[2] >> 4) & 0xf;
1045 *d2 = ((insn[2] & 0xf) << 8) | insn[3];
1054 is_rse (bfd_byte *insn, int op1, int op2,
1055 unsigned int *r1, unsigned int *r3, unsigned int *d2, unsigned int *b2)
1058 /* Yes, insn[5]. insn[4] is unused. */
1061 *r1 = (insn[1] >> 4) & 0xf;
1062 *r3 = insn[1] & 0xf;
1063 *b2 = (insn[2] >> 4) & 0xf;
1064 *d2 = ((insn[2] & 0xf) << 8) | insn[3];
1073 is_rx (bfd_byte *insn, int op,
1074 unsigned int *r1, unsigned int *d2, unsigned int *x2, unsigned int *b2)
1078 *r1 = (insn[1] >> 4) & 0xf;
1079 *x2 = insn[1] & 0xf;
1080 *b2 = (insn[2] >> 4) & 0xf;
1081 *d2 = ((insn[2] & 0xf) << 8) | insn[3];
1090 is_rxe (bfd_byte *insn, int op1, int op2,
1091 unsigned int *r1, unsigned int *d2, unsigned int *x2, unsigned int *b2)
1094 /* Yes, insn[5]. insn[4] is unused. */
1097 *r1 = (insn[1] >> 4) & 0xf;
1098 *x2 = insn[1] & 0xf;
1099 *b2 = (insn[2] >> 4) & 0xf;
1100 *d2 = ((insn[2] & 0xf) << 8) | insn[3];
1108 /* Set ADDR to the effective address for an X-style instruction, like:
1112 Here, X2 and B2 are registers, and D2 is an unsigned 12-bit
1113 constant; the effective address is the sum of all three. If either
1114 X2 or B2 are zero, then it doesn't contribute to the sum --- this
1115 means that r0 can't be used as either X2 or B2.
1117 GPR is an array of general register values, indexed by GPR number,
1118 not GDB register number. */
1120 compute_x_addr (struct prologue_value *addr,
1121 struct prologue_value *gpr,
1122 unsigned int d2, unsigned int x2, unsigned int b2)
1124 /* We can't just add stuff directly in addr; it might alias some of
1125 the registers we need to read. */
1126 struct prologue_value result;
1128 pv_set_to_constant (&result, d2);
1130 pv_add (&result, &result, &gpr[x2]);
1132 pv_add (&result, &result, &gpr[b2]);
1138 /* The number of GPR and FPR spill slots in an S/390 stack frame. We
1139 track general-purpose registers r2 -- r15, and floating-point
1140 registers f0, f2, f4, and f6. */
1141 #define S390_NUM_SPILL_SLOTS (14 + 4)
1142 #define S390_NUM_GPRS 16
1143 #define S390_NUM_FPRS 16
1146 /* If the SIZE bytes at ADDR are a stack slot we're actually tracking,
1147 return pv_definite_yes and set *STACK to point to the slot. If
1148 we're sure that they are not any of our stack slots, then return
1149 pv_definite_no. Otherwise, return pv_maybe.
1150 - GPR is an array indexed by GPR number giving the current values
1151 of the general-purpose registers.
1152 - SPILL is an array tracking the spill area of the caller's frame;
1153 SPILL[i] is the i'th spill slot. The spill slots are designated
1154 for r2 -- r15, and then f0, f2, f4, and f6.
1155 - BACK_CHAIN is the value of the back chain slot; it's only valid
1156 when the current frame actually has some space for a back chain
1157 slot --- that is, when the current value of the stack pointer
1158 (according to GPR) is at least S390_STACK_FRAME_OVERHEAD bytes
1159 less than its original value. */
1160 static enum pv_boolean
1161 s390_on_stack (struct prologue_value *addr,
1163 struct prologue_value *gpr,
1164 struct prologue_value *spill,
1165 struct prologue_value *back_chain,
1166 struct prologue_value **stack)
1168 struct prologue_value gpr_spill_addr;
1169 struct prologue_value fpr_spill_addr;
1170 struct prologue_value back_chain_addr;
1174 /* Construct the addresses of the spill arrays and the back chain. */
1175 pv_set_to_register (&gpr_spill_addr, S390_SP_REGNUM, 2 * S390_GPR_SIZE);
1176 pv_set_to_register (&fpr_spill_addr, S390_SP_REGNUM, 16 * S390_GPR_SIZE);
1177 back_chain_addr = gpr[S390_SP_REGNUM - S390_R0_REGNUM];
1179 /* We have to check for GPR and FPR references using two separate
1180 calls to pv_is_array_ref, since the GPR and FPR spill slots are
1181 different sizes. (SPILL is an array, but the thing it tracks
1182 isn't really an array.) */
1184 /* Was it a reference to the GPR spill array? */
1185 b = pv_is_array_ref (addr, size, &gpr_spill_addr, 14, S390_GPR_SIZE, &i);
1186 if (b == pv_definite_yes)
1189 return pv_definite_yes;
1194 /* Was it a reference to the FPR spill array? */
1195 b = pv_is_array_ref (addr, size, &fpr_spill_addr, 4, S390_FPR_SIZE, &i);
1196 if (b == pv_definite_yes)
1198 *stack = &spill[14 + i];
1199 return pv_definite_yes;
1204 /* Was it a reference to the back chain?
1205 This isn't quite right. We ought to check whether we have
1206 actually allocated any new frame at all. */
1207 b = pv_is_array_ref (addr, size, &back_chain_addr, 1, S390_GPR_SIZE, &i);
1208 if (b == pv_definite_yes)
1210 *stack = back_chain;
1211 return pv_definite_yes;
1216 /* All the above queries returned definite 'no's. */
1217 return pv_definite_no;
1221 /* Do a SIZE-byte store of VALUE to ADDR. GPR, SPILL, and BACK_CHAIN,
1222 and the return value are as described for s390_on_stack, above.
1223 Note that, when this returns pv_maybe, we have to assume that all
1224 of our memory now contains unknown values. */
1225 static enum pv_boolean
1226 s390_store (struct prologue_value *addr,
1228 struct prologue_value *value,
1229 struct prologue_value *gpr,
1230 struct prologue_value *spill,
1231 struct prologue_value *back_chain)
1233 struct prologue_value *stack;
1234 enum pv_boolean on_stack
1235 = s390_on_stack (addr, size, gpr, spill, back_chain, &stack);
1237 if (on_stack == pv_definite_yes)
1244 /* The current frame looks like a signal delivery frame: the first
1245 instruction is an 'svc' opcode. If the next frame is a signal
1246 handler's frame, set FI's saved register map to point into the
1247 signal context structure. */
1249 s390_get_signal_frame_info (struct frame_info *fi)
1251 struct frame_info *next_frame = get_next_frame (fi);
1254 && get_frame_extra_info (next_frame)
1255 && get_frame_extra_info (next_frame)->sigcontext)
1257 /* We're definitely backtracing from a signal handler. */
1258 CORE_ADDR *saved_regs = deprecated_get_frame_saved_regs (fi);
1259 CORE_ADDR save_reg_addr = (get_frame_extra_info (next_frame)->sigcontext
1260 + DEPRECATED_REGISTER_BYTE (S390_R0_REGNUM));
1263 for (reg = 0; reg < S390_NUM_GPRS; reg++)
1265 saved_regs[S390_R0_REGNUM + reg] = save_reg_addr;
1266 save_reg_addr += S390_GPR_SIZE;
1269 save_reg_addr = (get_frame_extra_info (next_frame)->sigcontext
1270 + (GDB_TARGET_IS_ESAME ? S390X_SIGREGS_FP0_OFFSET :
1271 S390_SIGREGS_FP0_OFFSET));
1272 for (reg = 0; reg < S390_NUM_FPRS; reg++)
1274 saved_regs[S390_F0_REGNUM + reg] = save_reg_addr;
1275 save_reg_addr += S390_FPR_SIZE;
1282 s390_get_frame_info (CORE_ADDR start_pc,
1283 struct frame_extra_info *fextra_info,
1284 struct frame_info *fi,
1285 int init_extra_info)
1287 /* Our return value:
1288 zero if we were able to read all the instructions we wanted, or
1289 -1 if we got an error trying to read memory. */
1292 /* The current PC for our abstract interpretation. */
1295 /* The address of the next instruction after that. */
1298 /* The general-purpose registers. */
1299 struct prologue_value gpr[S390_NUM_GPRS];
1301 /* The floating-point registers. */
1302 struct prologue_value fpr[S390_NUM_FPRS];
1304 /* The register spill stack slots in the caller's frame ---
1305 general-purpose registers r2 through r15, and floating-point
1306 registers. spill[i] is where gpr i+2 gets spilled;
1307 spill[(14, 15, 16, 17)] is where (f0, f2, f4, f6) get spilled. */
1308 struct prologue_value spill[S390_NUM_SPILL_SLOTS];
1310 /* The value of the back chain slot. This is only valid if the stack
1311 pointer is known to be less than its original value --- that is,
1312 if we have indeed allocated space on the stack. */
1313 struct prologue_value back_chain;
1315 /* The address of the instruction after the last one that changed
1316 the SP, FP, or back chain. */
1317 CORE_ADDR after_last_frame_setup_insn = start_pc;
1319 /* Set up everything's initial value. */
1323 for (i = 0; i < S390_NUM_GPRS; i++)
1324 pv_set_to_register (&gpr[i], S390_R0_REGNUM + i, 0);
1326 for (i = 0; i < S390_NUM_FPRS; i++)
1327 pv_set_to_register (&fpr[i], S390_F0_REGNUM + i, 0);
1329 for (i = 0; i < S390_NUM_SPILL_SLOTS; i++)
1330 pv_set_to_unknown (&spill[i]);
1332 pv_set_to_unknown (&back_chain);
1335 /* Start interpreting instructions, until we hit something we don't
1336 know how to interpret. (Ideally, we should stop at the frame's
1337 real current PC, but at the moment, our callers don't give us
1339 for (pc = start_pc; ; pc = next_pc)
1341 bfd_byte insn[S390_MAX_INSTR_SIZE];
1342 int insn_len = s390_readinstruction (insn, pc);
1344 /* Fields for various kinds of instructions. */
1345 unsigned int b2, r1, r2, d2, x2, r3;
1348 /* The values of SP, FP, and back chain before this instruction,
1349 for detecting instructions that change them. */
1350 struct prologue_value pre_insn_sp, pre_insn_fp, pre_insn_back_chain;
1352 /* If we got an error trying to read the instruction, report it. */
1359 next_pc = pc + insn_len;
1361 pre_insn_sp = gpr[S390_SP_REGNUM - S390_R0_REGNUM];
1362 pre_insn_fp = gpr[S390_FRAME_REGNUM - S390_R0_REGNUM];
1363 pre_insn_back_chain = back_chain;
1365 /* A special case, first --- only recognized as the very first
1366 instruction of the function, for signal delivery frames:
1367 SVC i --- system call */
1369 && is_rr (insn, op_svc, &r1, &r2))
1372 s390_get_signal_frame_info (fi);
1376 /* AHI r1, i2 --- add halfword immediate */
1377 else if (is_ri (insn, op1_ahi, op2_ahi, &r1, &i2))
1378 pv_add_constant (&gpr[r1], i2);
1381 /* AGHI r1, i2 --- add halfword immediate (64-bit version) */
1382 else if (GDB_TARGET_IS_ESAME
1383 && is_ri (insn, op1_aghi, op2_aghi, &r1, &i2))
1384 pv_add_constant (&gpr[r1], i2);
1386 /* AR r1, r2 -- add register */
1387 else if (is_rr (insn, op_ar, &r1, &r2))
1388 pv_add (&gpr[r1], &gpr[r1], &gpr[r2]);
1390 /* BASR r1, 0 --- branch and save
1391 Since r2 is zero, this saves the PC in r1, but doesn't branch. */
1392 else if (is_rr (insn, op_basr, &r1, &r2)
1394 pv_set_to_constant (&gpr[r1], next_pc);
1396 /* BRAS r1, i2 --- branch relative and save */
1397 else if (is_ri (insn, op1_bras, op2_bras, &r1, &i2))
1399 pv_set_to_constant (&gpr[r1], next_pc);
1400 next_pc = pc + i2 * 2;
1402 /* We'd better not interpret any backward branches. We'll
1408 /* L r1, d2(x2, b2) --- load */
1409 else if (is_rx (insn, op_l, &r1, &d2, &x2, &b2))
1411 struct prologue_value addr;
1412 struct prologue_value *stack;
1414 compute_x_addr (&addr, gpr, d2, x2, b2);
1416 /* If it's a load from an in-line constant pool, then we can
1417 simulate that, under the assumption that the code isn't
1418 going to change between the time the processor actually
1419 executed it creating the current frame, and the time when
1420 we're analyzing the code to unwind past that frame. */
1421 if (addr.kind == pv_constant
1422 && start_pc <= addr.k
1423 && addr.k < next_pc)
1424 pv_set_to_constant (&gpr[r1],
1425 read_memory_integer (addr.k, 4));
1427 /* If it's definitely a reference to something on the stack,
1429 else if (s390_on_stack (&addr, 4, gpr, spill, &back_chain, &stack)
1433 /* Otherwise, we don't know the value. */
1435 pv_set_to_unknown (&gpr[r1]);
1438 /* LA r1, d2(x2, b2) --- load address */
1439 else if (is_rx (insn, op_la, &r1, &d2, &x2, &b2))
1440 compute_x_addr (&gpr[r1], gpr, d2, x2, b2);
1442 /* LARL r1, i2 --- load address relative long */
1443 else if (GDB_TARGET_IS_ESAME
1444 && is_ril (insn, op1_larl, op2_larl, &r1, &i2))
1445 pv_set_to_constant (&gpr[r1], pc + i2 * 2);
1447 /* LGR r1, r2 --- load from register */
1448 else if (GDB_TARGET_IS_ESAME
1449 && is_rre (insn, op_lgr, &r1, &r2))
1452 /* LHI r1, i2 --- load halfword immediate */
1453 else if (is_ri (insn, op1_lhi, op2_lhi, &r1, &i2))
1454 pv_set_to_constant (&gpr[r1], i2);
1456 /* LGHI r1, i2 --- load halfword immediate --- 64-bit version */
1457 else if (is_ri (insn, op1_lghi, op2_lghi, &r1, &i2))
1458 pv_set_to_constant (&gpr[r1], i2);
1460 /* LR r1, r2 --- load from register */
1461 else if (is_rr (insn, op_lr, &r1, &r2))
1464 /* NGR r1, r2 --- logical and --- 64-bit version */
1465 else if (GDB_TARGET_IS_ESAME
1466 && is_rre (insn, op_ngr, &r1, &r2))
1467 pv_logical_and (&gpr[r1], &gpr[r1], &gpr[r2]);
1469 /* NR r1, r2 --- logical and */
1470 else if (is_rr (insn, op_nr, &r1, &r2))
1471 pv_logical_and (&gpr[r1], &gpr[r1], &gpr[r2]);
1473 /* NGR r1, r2 --- logical and --- 64-bit version */
1474 else if (GDB_TARGET_IS_ESAME
1475 && is_rre (insn, op_ngr, &r1, &r2))
1476 pv_logical_and (&gpr[r1], &gpr[r1], &gpr[r2]);
1478 /* NR r1, r2 --- logical and */
1479 else if (is_rr (insn, op_nr, &r1, &r2))
1480 pv_logical_and (&gpr[r1], &gpr[r1], &gpr[r2]);
1482 /* S r1, d2(x2, b2) --- subtract from memory */
1483 else if (is_rx (insn, op_s, &r1, &d2, &x2, &b2))
1485 struct prologue_value addr;
1486 struct prologue_value value;
1487 struct prologue_value *stack;
1489 compute_x_addr (&addr, gpr, d2, x2, b2);
1491 /* If it's a load from an in-line constant pool, then we can
1492 simulate that, under the assumption that the code isn't
1493 going to change between the time the processor actually
1494 executed it and the time when we're analyzing it. */
1495 if (addr.kind == pv_constant
1496 && start_pc <= addr.k
1498 pv_set_to_constant (&value, read_memory_integer (addr.k, 4));
1500 /* If it's definitely a reference to something on the stack,
1501 we could do that. */
1502 else if (s390_on_stack (&addr, 4, gpr, spill, &back_chain, &stack)
1506 /* Otherwise, we don't know the value. */
1508 pv_set_to_unknown (&value);
1510 pv_subtract (&gpr[r1], &gpr[r1], &value);
1513 /* ST r1, d2(x2, b2) --- store */
1514 else if (is_rx (insn, op_st, &r1, &d2, &x2, &b2))
1516 struct prologue_value addr;
1518 compute_x_addr (&addr, gpr, d2, x2, b2);
1520 /* The below really should be '4', not 'S390_GPR_SIZE'; this
1521 instruction always stores 32 bits, regardless of the full
1523 if (s390_store (&addr, 4, &gpr[r1], gpr, spill, &back_chain)
1525 /* If we can't be sure that it's *not* a store to
1526 something we're tracing, then we would have to mark all
1527 our memory as unknown --- after all, it *could* be a
1528 store to any of them --- so we might as well just stop
1533 /* STD r1, d2(x2,b2) --- store floating-point register */
1534 else if (is_rx (insn, op_std, &r1, &d2, &x2, &b2))
1536 struct prologue_value addr;
1538 compute_x_addr (&addr, gpr, d2, x2, b2);
1540 if (s390_store (&addr, 8, &fpr[r1], gpr, spill, &back_chain)
1542 /* If we can't be sure that it's *not* a store to
1543 something we're tracing, then we would have to mark all
1544 our memory as unknown --- after all, it *could* be a
1545 store to any of them --- so we might as well just stop
1550 /* STG r1, d2(x2, b2) --- 64-bit store */
1551 else if (GDB_TARGET_IS_ESAME
1552 && is_rxe (insn, op1_stg, op2_stg, &r1, &d2, &x2, &b2))
1554 struct prologue_value addr;
1556 compute_x_addr (&addr, gpr, d2, x2, b2);
1558 /* The below really should be '8', not 'S390_GPR_SIZE'; this
1559 instruction always stores 64 bits, regardless of the full
1561 if (s390_store (&addr, 8, &gpr[r1], gpr, spill, &back_chain)
1563 /* If we can't be sure that it's *not* a store to
1564 something we're tracing, then we would have to mark all
1565 our memory as unknown --- after all, it *could* be a
1566 store to any of them --- so we might as well just stop
1571 /* STM r1, r3, d2(b2) --- store multiple */
1572 else if (is_rs (insn, op_stm, &r1, &r3, &d2, &b2))
1576 struct prologue_value addr;
1578 for (regnum = r1, offset = 0;
1580 regnum++, offset += 4)
1582 compute_x_addr (&addr, gpr, d2 + offset, 0, b2);
1584 if (s390_store (&addr, 4, &gpr[regnum], gpr, spill, &back_chain)
1586 /* If we can't be sure that it's *not* a store to
1587 something we're tracing, then we would have to mark all
1588 our memory as unknown --- after all, it *could* be a
1589 store to any of them --- so we might as well just stop
1594 /* If we left the loop early, we should stop interpreting
1600 /* STMG r1, r3, d2(b2) --- store multiple, 64-bit */
1601 else if (GDB_TARGET_IS_ESAME
1602 && is_rse (insn, op1_stmg, op2_stmg, &r1, &r3, &d2, &b2))
1606 struct prologue_value addr;
1608 for (regnum = r1, offset = 0;
1610 regnum++, offset += 8)
1612 compute_x_addr (&addr, gpr, d2 + offset, 0, b2);
1614 if (s390_store (&addr, 8, &gpr[regnum], gpr, spill, &back_chain)
1616 /* If we can't be sure that it's *not* a store to
1617 something we're tracing, then we would have to mark all
1618 our memory as unknown --- after all, it *could* be a
1619 store to any of them --- so we might as well just stop
1624 /* If we left the loop early, we should stop interpreting
1631 /* An instruction we don't know how to simulate. The only
1632 safe thing to do would be to set every value we're tracking
1633 to 'unknown'. Instead, we'll be optimistic: we just stop
1634 interpreting, and assume that the machine state we've got
1635 now is good enough for unwinding the stack. */
1638 /* Record the address after the last instruction that changed
1639 the FP, SP, or backlink. Ignore instructions that changed
1640 them back to their original values --- those are probably
1641 restore instructions. (The back chain is never restored,
1644 struct prologue_value *sp = &gpr[S390_SP_REGNUM - S390_R0_REGNUM];
1645 struct prologue_value *fp = &gpr[S390_FRAME_REGNUM - S390_R0_REGNUM];
1647 if ((! pv_is_identical (&pre_insn_sp, sp)
1648 && ! pv_is_register (sp, S390_SP_REGNUM, 0))
1649 || (! pv_is_identical (&pre_insn_fp, fp)
1650 && ! pv_is_register (fp, S390_FRAME_REGNUM, 0))
1651 || ! pv_is_identical (&pre_insn_back_chain, &back_chain))
1652 after_last_frame_setup_insn = next_pc;
1656 /* Okay, now gpr[], fpr[], spill[], and back_chain reflect the state
1657 of the machine as of the first instruction we couldn't interpret
1658 (hopefully the first non-prologue instruction). */
1660 /* The size of the frame, or (CORE_ADDR) -1 if we couldn't figure
1662 CORE_ADDR frame_size = -1;
1664 /* The value the SP had upon entry to the function, or
1665 (CORE_ADDR) -1 if we can't figure that out. */
1666 CORE_ADDR original_sp = -1;
1668 /* Are we using S390_FRAME_REGNUM as a frame pointer register? */
1669 int using_frame_pointer = 0;
1671 /* If S390_FRAME_REGNUM is some constant offset from the SP, then
1672 that strongly suggests that we're going to use that as our
1673 frame pointer register, not the SP. */
1675 struct prologue_value *fp = &gpr[S390_FRAME_REGNUM - S390_R0_REGNUM];
1677 if (fp->kind == pv_register
1678 && fp->reg == S390_SP_REGNUM)
1679 using_frame_pointer = 1;
1682 /* If we were given a frame_info structure, we may be able to use
1683 the frame's base address to figure out the actual value of the
1685 if (fi && get_frame_base (fi))
1687 int frame_base_regno;
1688 struct prologue_value *frame_base;
1690 /* The meaning of the frame base depends on whether the
1691 function uses a frame pointer register other than the SP or
1692 not (see s390_read_fp):
1693 - If the function does use a frame pointer register other
1694 than the SP, then the frame base is that register's
1696 - If the function doesn't use a frame pointer, then the
1697 frame base is the SP itself.
1698 We're duplicating some of the logic of s390_fp_regnum here,
1699 but we don't want to call that, because it would just do
1700 exactly the same analysis we've already done above. */
1701 if (using_frame_pointer)
1702 frame_base_regno = S390_FRAME_REGNUM;
1704 frame_base_regno = S390_SP_REGNUM;
1706 frame_base = &gpr[frame_base_regno - S390_R0_REGNUM];
1708 /* We know the frame base address; if the value of whatever
1709 register it came from is a constant offset from the
1710 original SP, then we can reconstruct the original SP just
1711 by subtracting off that constant. */
1712 if (frame_base->kind == pv_register
1713 && frame_base->reg == S390_SP_REGNUM)
1714 original_sp = get_frame_base (fi) - frame_base->k;
1717 /* If the analysis said that the current SP value is the original
1718 value less some constant, then that constant is the frame size. */
1720 struct prologue_value *sp = &gpr[S390_SP_REGNUM - S390_R0_REGNUM];
1722 if (sp->kind == pv_register
1723 && sp->reg == S390_SP_REGNUM)
1724 frame_size = -sp->k;
1727 /* If we knew other registers' current values, we could check if
1728 the analysis said any of those were related to the original SP
1729 value, too. But for now, we'll just punt. */
1731 /* If the caller passed in an 'extra info' structure, fill in the
1735 if (init_extra_info || ! fextra_info->initialised)
1737 s390_memset_extra_info (fextra_info);
1738 fextra_info->function_start = start_pc;
1739 fextra_info->initialised = 1;
1742 if (frame_size != -1)
1744 fextra_info->stack_bought_valid = 1;
1745 fextra_info->stack_bought = frame_size;
1748 /* Assume everything was okay, and indicate otherwise when we
1749 find something amiss. */
1750 fextra_info->good_prologue = 1;
1752 if (using_frame_pointer)
1753 /* Actually, nobody cares about the exact PC, so any
1754 non-zero value will do here. */
1755 fextra_info->frame_pointer_saved_pc = 1;
1757 /* If we weren't able to find the size of the frame, or find
1758 the original sp based on actual current register values,
1759 then we're not going to be able to unwind this frame.
1761 (If we're just doing prologue analysis to set a breakpoint,
1762 then frame_size might be known, but original_sp unknown; if
1763 we're analyzing a real frame which uses alloca, then
1764 original_sp might be known (from the frame pointer
1765 register), but the frame size might be unknown.) */
1766 if (original_sp == -1 && frame_size == -1)
1767 fextra_info->good_prologue = 0;
1769 if (fextra_info->good_prologue)
1770 fextra_info->skip_prologue_function_start
1771 = after_last_frame_setup_insn;
1773 /* If the prologue was too complex for us to make sense of,
1774 then perhaps it's better to just not skip anything at
1776 fextra_info->skip_prologue_function_start = start_pc;
1779 /* Indicate where registers were saved on the stack, if:
1780 - the caller seems to want to know,
1781 - the caller provided an actual SP, and
1782 - the analysis gave us enough information to actually figure it
1785 && deprecated_get_frame_saved_regs (fi)
1786 && original_sp != -1)
1789 CORE_ADDR slot_addr;
1790 CORE_ADDR *saved_regs = deprecated_get_frame_saved_regs (fi);
1792 /* Scan the spill array; if a spill slot says it holds the
1793 original value of some register, then record that slot's
1794 address as the place that register was saved.
1796 Just for kicks, note that, even if registers aren't saved
1797 in their officially-sanctioned slots, this will still work
1798 --- we know what really got put where. */
1800 /* First, the slots for r2 -- r15. */
1801 for (slot_num = 0, slot_addr = original_sp + 2 * S390_GPR_SIZE;
1803 slot_num++, slot_addr += S390_GPR_SIZE)
1805 struct prologue_value *slot = &spill[slot_num];
1807 if (slot->kind == pv_register
1809 saved_regs[slot->reg] = slot_addr;
1812 /* Then, the slots for f0, f2, f4, and f6. They're a
1814 for (slot_num = 14, slot_addr = original_sp + 16 * S390_GPR_SIZE;
1815 slot_num < S390_NUM_SPILL_SLOTS;
1816 slot_num++, slot_addr += S390_FPR_SIZE)
1818 struct prologue_value *slot = &spill[slot_num];
1820 if (slot->kind == pv_register
1822 saved_regs[slot->reg] = slot_addr;
1825 /* The stack pointer's element of saved_regs[] is special. */
1826 saved_regs[S390_SP_REGNUM] = original_sp;
1833 /* Return true if we are in the functin's epilogue, i.e. after the
1834 instruction that destroyed the function's stack frame. */
1836 s390_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
1838 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
1840 /* In frameless functions, there's not frame to destroy and thus
1841 we don't care about the epilogue.
1843 In functions with frame, the epilogue sequence is a pair of
1844 a LM-type instruction that restores (amongst others) the
1845 return register %r14 and the stack pointer %r15, followed
1846 by a branch 'br %r14' --or equivalent-- that effects the
1849 In that situation, this function needs to return 'true' in
1850 exactly one case: when pc points to that branch instruction.
1852 Thus we try to disassemble the one instructions immediately
1853 preceeding pc and check whether it is an LM-type instruction
1854 modifying the stack pointer.
1856 Note that disassembling backwards is not reliable, so there
1857 is a slight chance of false positives here ... */
1860 unsigned int r1, r3, b2;
1864 && !read_memory_nobpt (pc - 4, insn, 4)
1865 && is_rs (insn, op_lm, &r1, &r3, &d2, &b2)
1866 && r3 == S390_SP_REGNUM - S390_R0_REGNUM)
1870 && !read_memory_nobpt (pc - 6, insn, 6)
1871 && is_rse (insn, op1_lmg, op2_lmg, &r1, &r3, &d2, &b2)
1872 && r3 == S390_SP_REGNUM - S390_R0_REGNUM)
1879 s390_check_function_end (CORE_ADDR pc)
1881 bfd_byte instr[S390_MAX_INSTR_SIZE];
1882 int regidx, instrlen;
1884 instrlen = s390_readinstruction (instr, pc);
1888 if (instrlen != 2 || instr[0] != 07 || (instr[1] >> 4) != 0xf)
1890 regidx = instr[1] & 0xf;
1891 /* Check for LMG or LG */
1893 s390_readinstruction (instr, pc - (GDB_TARGET_IS_ESAME ? 6 : 4));
1896 if (GDB_TARGET_IS_ESAME)
1899 if (instrlen != 6 || instr[0] != 0xeb || instr[5] != 0x4)
1902 else if (instrlen != 4 || instr[0] != 0x98)
1906 if ((instr[2] >> 4) != 0xf)
1910 instrlen = s390_readinstruction (instr, pc - (GDB_TARGET_IS_ESAME ? 12 : 8));
1913 if (GDB_TARGET_IS_ESAME)
1916 if (instrlen != 6 || instr[0] != 0xe3 || instr[5] != 0x4)
1922 if (instrlen != 4 || instr[0] != 0x58)
1925 if (instr[2] >> 4 != 0xf)
1927 if (instr[1] >> 4 != regidx)
1933 s390_sniff_pc_function_start (CORE_ADDR pc, struct frame_info *fi)
1935 CORE_ADDR function_start, test_function_start;
1936 int loop_cnt, err, function_end;
1937 struct frame_extra_info fextra_info;
1938 function_start = get_pc_function_start (pc);
1940 if (function_start == 0)
1942 test_function_start = pc;
1943 if (test_function_start & 1)
1944 return 0; /* This has to be bogus */
1950 s390_get_frame_info (test_function_start, &fextra_info, fi, 1);
1952 test_function_start -= 2;
1953 function_end = s390_check_function_end (test_function_start);
1955 while (!(function_end == 1 || err || loop_cnt >= 4096 ||
1956 (fextra_info.good_prologue)));
1957 if (fextra_info.good_prologue)
1958 function_start = fextra_info.function_start;
1959 else if (function_end == 1)
1960 function_start = test_function_start;
1962 return function_start;
1967 s390_frameless_function_invocation (struct frame_info *fi)
1969 struct frame_extra_info fextra_info, *fextra_info_ptr;
1972 if (get_next_frame (fi) == NULL) /* no may be frameless */
1974 if (get_frame_extra_info (fi))
1975 fextra_info_ptr = get_frame_extra_info (fi);
1978 fextra_info_ptr = &fextra_info;
1979 s390_get_frame_info (s390_sniff_pc_function_start (get_frame_pc (fi), fi),
1980 fextra_info_ptr, fi, 1);
1982 frameless = (fextra_info_ptr->stack_bought_valid
1983 && fextra_info_ptr->stack_bought == 0);
1991 s390_is_sigreturn (CORE_ADDR pc, struct frame_info *sighandler_fi,
1992 CORE_ADDR *sregs, CORE_ADDR *sigcaller_pc)
1994 bfd_byte instr[S390_MAX_INSTR_SIZE];
1999 CORE_ADDR temp_sregs;
2001 scontext = temp_sregs = 0;
2003 instrlen = s390_readinstruction (instr, pc);
2006 if (((instrlen == S390_SYSCALL_SIZE) &&
2007 (instr[0] == S390_SYSCALL_OPCODE)) &&
2008 ((instr[1] == s390_NR_sigreturn) || (instr[1] == s390_NR_rt_sigreturn)))
2012 if (s390_frameless_function_invocation (sighandler_fi))
2013 orig_sp = get_frame_base (sighandler_fi);
2015 orig_sp = ADDR_BITS_REMOVE ((CORE_ADDR)
2016 read_memory_integer (get_frame_base (sighandler_fi),
2018 if (orig_sp && sigcaller_pc)
2020 scontext = orig_sp + S390_SIGNAL_FRAMESIZE;
2021 if (pc == scontext && instr[1] == s390_NR_rt_sigreturn)
2023 /* We got a new style rt_signal */
2024 /* get address of read ucontext->uc_mcontext */
2025 temp_sregs = orig_sp + (GDB_TARGET_IS_ESAME ?
2026 S390X_UC_MCONTEXT_OFFSET :
2027 S390_UC_MCONTEXT_OFFSET);
2031 /* read sigcontext->sregs */
2032 temp_sregs = ADDR_BITS_REMOVE ((CORE_ADDR)
2033 read_memory_integer (scontext
2035 (GDB_TARGET_IS_ESAME
2037 S390X_SIGCONTEXT_SREGS_OFFSET
2039 S390_SIGCONTEXT_SREGS_OFFSET),
2043 /* read sigregs->psw.addr */
2045 ADDR_BITS_REMOVE ((CORE_ADDR)
2046 read_memory_integer (temp_sregs +
2047 DEPRECATED_REGISTER_BYTE (S390_PSWA_REGNUM),
2054 *sregs = temp_sregs;
2059 We need to do something better here but this will keep us out of trouble
2061 For some reason the blockframe.c calls us with fi->next->fromleaf
2062 so this seems of little use to us. */
2064 s390_init_frame_pc_first (int next_fromleaf, struct frame_info *fi)
2066 CORE_ADDR sigcaller_pc;
2070 pc = ADDR_BITS_REMOVE (read_register (S390_RETADDR_REGNUM));
2071 /* fix signal handlers */
2073 else if (get_next_frame (fi) && get_frame_pc (get_next_frame (fi)))
2074 pc = s390_frame_saved_pc_nofix (get_next_frame (fi));
2075 if (pc && get_next_frame (fi) && get_frame_base (get_next_frame (fi))
2076 && s390_is_sigreturn (pc, get_next_frame (fi), NULL, &sigcaller_pc))
2084 s390_init_extra_frame_info (int fromleaf, struct frame_info *fi)
2086 frame_extra_info_zalloc (fi, sizeof (struct frame_extra_info));
2087 if (get_frame_pc (fi))
2088 s390_get_frame_info (s390_sniff_pc_function_start (get_frame_pc (fi), fi),
2089 get_frame_extra_info (fi), fi, 1);
2091 s390_memset_extra_info (get_frame_extra_info (fi));
2094 /* If saved registers of frame FI are not known yet, read and cache them.
2095 &FEXTRA_INFOP contains struct frame_extra_info; TDATAP can be NULL,
2096 in which case the framedata are read. */
2099 s390_frame_init_saved_regs (struct frame_info *fi)
2104 if (deprecated_get_frame_saved_regs (fi) == NULL)
2106 /* zalloc memsets the saved regs */
2107 frame_saved_regs_zalloc (fi);
2108 if (get_frame_pc (fi))
2110 quick = (get_frame_extra_info (fi)
2111 && get_frame_extra_info (fi)->initialised
2112 && get_frame_extra_info (fi)->good_prologue);
2113 s390_get_frame_info (quick
2114 ? get_frame_extra_info (fi)->function_start
2115 : s390_sniff_pc_function_start (get_frame_pc (fi), fi),
2116 get_frame_extra_info (fi), fi, !quick);
2124 s390_frame_saved_pc_nofix (struct frame_info *fi)
2126 if (get_frame_extra_info (fi) && get_frame_extra_info (fi)->saved_pc_valid)
2127 return get_frame_extra_info (fi)->saved_pc;
2129 if (deprecated_generic_find_dummy_frame (get_frame_pc (fi),
2130 get_frame_base (fi)))
2131 return deprecated_read_register_dummy (get_frame_pc (fi),
2132 get_frame_base (fi), S390_PC_REGNUM);
2134 s390_frame_init_saved_regs (fi);
2135 if (get_frame_extra_info (fi))
2137 get_frame_extra_info (fi)->saved_pc_valid = 1;
2138 if (get_frame_extra_info (fi)->good_prologue
2139 && deprecated_get_frame_saved_regs (fi)[S390_RETADDR_REGNUM])
2140 get_frame_extra_info (fi)->saved_pc
2141 = ADDR_BITS_REMOVE (read_memory_integer
2142 (deprecated_get_frame_saved_regs (fi)[S390_RETADDR_REGNUM],
2145 get_frame_extra_info (fi)->saved_pc
2146 = ADDR_BITS_REMOVE (read_register (S390_RETADDR_REGNUM));
2147 return get_frame_extra_info (fi)->saved_pc;
2153 s390_frame_saved_pc (struct frame_info *fi)
2155 CORE_ADDR saved_pc = 0, sig_pc;
2157 if (get_frame_extra_info (fi)
2158 && get_frame_extra_info (fi)->sig_fixed_saved_pc_valid)
2159 return get_frame_extra_info (fi)->sig_fixed_saved_pc;
2160 saved_pc = s390_frame_saved_pc_nofix (fi);
2162 if (get_frame_extra_info (fi))
2164 get_frame_extra_info (fi)->sig_fixed_saved_pc_valid = 1;
2167 if (s390_is_sigreturn (saved_pc, fi, NULL, &sig_pc))
2170 get_frame_extra_info (fi)->sig_fixed_saved_pc = saved_pc;
2178 /* We want backtraces out of signal handlers so we don't set
2179 (get_frame_type (thisframe) == SIGTRAMP_FRAME) to 1 */
2182 s390_frame_chain (struct frame_info *thisframe)
2184 CORE_ADDR prev_fp = 0;
2186 if (deprecated_generic_find_dummy_frame (get_frame_pc (thisframe),
2187 get_frame_base (thisframe)))
2188 return deprecated_read_register_dummy (get_frame_pc (thisframe),
2189 get_frame_base (thisframe),
2194 CORE_ADDR sregs = 0;
2195 struct frame_extra_info prev_fextra_info;
2197 memset (&prev_fextra_info, 0, sizeof (prev_fextra_info));
2198 if (get_frame_pc (thisframe))
2200 CORE_ADDR saved_pc, sig_pc;
2202 saved_pc = s390_frame_saved_pc_nofix (thisframe);
2206 s390_is_sigreturn (saved_pc, thisframe, &sregs, &sig_pc)))
2208 s390_get_frame_info (s390_sniff_pc_function_start
2209 (saved_pc, NULL), &prev_fextra_info, NULL,
2215 /* read sigregs,regs.gprs[11 or 15] */
2216 prev_fp = read_memory_integer (sregs +
2217 DEPRECATED_REGISTER_BYTE (S390_R0_REGNUM +
2219 frame_pointer_saved_pc
2222 get_frame_extra_info (thisframe)->sigcontext = sregs;
2226 if (deprecated_get_frame_saved_regs (thisframe))
2230 if (prev_fextra_info.frame_pointer_saved_pc
2231 && deprecated_get_frame_saved_regs (thisframe)[S390_FRAME_REGNUM])
2232 regno = S390_FRAME_REGNUM;
2234 regno = S390_SP_REGNUM;
2236 if (deprecated_get_frame_saved_regs (thisframe)[regno])
2238 /* The SP's entry of `saved_regs' is special. */
2239 if (regno == S390_SP_REGNUM)
2240 prev_fp = deprecated_get_frame_saved_regs (thisframe)[regno];
2243 read_memory_integer (deprecated_get_frame_saved_regs (thisframe)[regno],
2249 return ADDR_BITS_REMOVE (prev_fp);
2253 Whether struct frame_extra_info is actually needed I'll have to figure
2254 out as our frames are similar to rs6000 there is a possibility
2255 i386 dosen't need it. */
2257 /* Not the most efficent code in the world */
2259 s390_fp_regnum (void)
2261 int regno = S390_SP_REGNUM;
2262 struct frame_extra_info fextra_info;
2264 CORE_ADDR pc = ADDR_BITS_REMOVE (read_register (S390_PC_REGNUM));
2266 s390_get_frame_info (s390_sniff_pc_function_start (pc, NULL), &fextra_info,
2268 if (fextra_info.frame_pointer_saved_pc)
2269 regno = S390_FRAME_REGNUM;
2276 return read_register (s390_fp_regnum ());
2281 s390_pop_frame_regular (struct frame_info *frame)
2285 write_register (S390_PC_REGNUM, DEPRECATED_FRAME_SAVED_PC (frame));
2287 /* Restore any saved registers. */
2288 if (deprecated_get_frame_saved_regs (frame))
2290 for (regnum = 0; regnum < NUM_REGS; regnum++)
2291 if (deprecated_get_frame_saved_regs (frame)[regnum] != 0)
2295 value = read_memory_unsigned_integer (deprecated_get_frame_saved_regs (frame)[regnum],
2296 DEPRECATED_REGISTER_RAW_SIZE (regnum));
2297 write_register (regnum, value);
2300 /* Actually cut back the stack. Remember that the SP's element of
2301 saved_regs is the old SP itself, not the address at which it is
2303 write_register (S390_SP_REGNUM, deprecated_get_frame_saved_regs (frame)[S390_SP_REGNUM]);
2306 /* Throw away any cached frame information. */
2307 flush_cached_frames ();
2311 /* Destroy the innermost (Top-Of-Stack) stack frame, restoring the
2312 machine state that was in effect before the frame was created.
2313 Used in the contexts of the "return" command, and of
2314 target function calls from the debugger. */
2316 s390_pop_frame (void)
2318 /* This function checks for and handles generic dummy frames, and
2319 calls back to our function for ordinary frames. */
2320 generic_pop_current_frame (s390_pop_frame_regular);
2324 /* Dummy function calls. */
2326 /* Return non-zero if TYPE is an integer-like type, zero otherwise.
2327 "Integer-like" types are those that should be passed the way
2328 integers are: integers, enums, ranges, characters, and booleans. */
2330 is_integer_like (struct type *type)
2332 enum type_code code = TYPE_CODE (type);
2334 return (code == TYPE_CODE_INT
2335 || code == TYPE_CODE_ENUM
2336 || code == TYPE_CODE_RANGE
2337 || code == TYPE_CODE_CHAR
2338 || code == TYPE_CODE_BOOL);
2341 /* Return non-zero if TYPE is a pointer-like type, zero otherwise.
2342 "Pointer-like" types are those that should be passed the way
2343 pointers are: pointers and references. */
2345 is_pointer_like (struct type *type)
2347 enum type_code code = TYPE_CODE (type);
2349 return (code == TYPE_CODE_PTR
2350 || code == TYPE_CODE_REF);
2354 /* Return non-zero if TYPE is a `float singleton' or `double
2355 singleton', zero otherwise.
2357 A `T singleton' is a struct type with one member, whose type is
2358 either T or a `T singleton'. So, the following are all float
2362 struct { struct { float x; } x; };
2363 struct { struct { struct { float x; } x; } x; };
2367 All such structures are passed as if they were floats or doubles,
2368 as the (revised) ABI says. */
2370 is_float_singleton (struct type *type)
2372 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
2374 struct type *singleton_type = TYPE_FIELD_TYPE (type, 0);
2375 CHECK_TYPEDEF (singleton_type);
2377 return (TYPE_CODE (singleton_type) == TYPE_CODE_FLT
2378 || is_float_singleton (singleton_type));
2385 /* Return non-zero if TYPE is a struct-like type, zero otherwise.
2386 "Struct-like" types are those that should be passed as structs are:
2389 As an odd quirk, not mentioned in the ABI, GCC passes float and
2390 double singletons as if they were a plain float, double, etc. (The
2391 corresponding union types are handled normally.) So we exclude
2392 those types here. *shrug* */
2394 is_struct_like (struct type *type)
2396 enum type_code code = TYPE_CODE (type);
2398 return (code == TYPE_CODE_UNION
2399 || (code == TYPE_CODE_STRUCT && ! is_float_singleton (type)));
2403 /* Return non-zero if TYPE is a float-like type, zero otherwise.
2404 "Float-like" types are those that should be passed as
2405 floating-point values are.
2407 You'd think this would just be floats, doubles, long doubles, etc.
2408 But as an odd quirk, not mentioned in the ABI, GCC passes float and
2409 double singletons as if they were a plain float, double, etc. (The
2410 corresponding union types are handled normally.) So we include
2411 those types here. *shrug* */
2413 is_float_like (struct type *type)
2415 return (TYPE_CODE (type) == TYPE_CODE_FLT
2416 || is_float_singleton (type));
2421 is_power_of_two (unsigned int n)
2423 return ((n & (n - 1)) == 0);
2426 /* Return non-zero if TYPE should be passed as a pointer to a copy,
2429 s390_function_arg_pass_by_reference (struct type *type)
2431 unsigned length = TYPE_LENGTH (type);
2435 /* FIXME: All complex and vector types are also returned by reference. */
2436 return is_struct_like (type) && !is_power_of_two (length);
2439 /* Return non-zero if TYPE should be passed in a float register
2442 s390_function_arg_float (struct type *type)
2444 unsigned length = TYPE_LENGTH (type);
2448 return is_float_like (type);
2451 /* Return non-zero if TYPE should be passed in an integer register
2452 (or a pair of integer registers) if possible. */
2454 s390_function_arg_integer (struct type *type)
2456 unsigned length = TYPE_LENGTH (type);
2460 return is_integer_like (type)
2461 || is_pointer_like (type)
2462 || (is_struct_like (type) && is_power_of_two (length));
2465 /* Return ARG, a `SIMPLE_ARG', sign-extended or zero-extended to a full
2466 word as required for the ABI. */
2468 extend_simple_arg (struct value *arg)
2470 struct type *type = VALUE_TYPE (arg);
2472 /* Even structs get passed in the least significant bits of the
2473 register / memory word. It's not really right to extract them as
2474 an integer, but it does take care of the extension. */
2475 if (TYPE_UNSIGNED (type))
2476 return extract_unsigned_integer (VALUE_CONTENTS (arg),
2477 TYPE_LENGTH (type));
2479 return extract_signed_integer (VALUE_CONTENTS (arg),
2480 TYPE_LENGTH (type));
2484 /* Return the alignment required by TYPE. */
2486 alignment_of (struct type *type)
2490 if (is_integer_like (type)
2491 || is_pointer_like (type)
2492 || TYPE_CODE (type) == TYPE_CODE_FLT)
2493 alignment = TYPE_LENGTH (type);
2494 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
2495 || TYPE_CODE (type) == TYPE_CODE_UNION)
2500 for (i = 0; i < TYPE_NFIELDS (type); i++)
2502 int field_alignment = alignment_of (TYPE_FIELD_TYPE (type, i));
2504 if (field_alignment > alignment)
2505 alignment = field_alignment;
2511 /* Check that everything we ever return is a power of two. Lots of
2512 code doesn't want to deal with aligning things to arbitrary
2514 gdb_assert ((alignment & (alignment - 1)) == 0);
2520 /* Put the actual parameter values pointed to by ARGS[0..NARGS-1] in
2521 place to be passed to a function, as specified by the "GNU/Linux
2522 for S/390 ELF Application Binary Interface Supplement".
2524 SP is the current stack pointer. We must put arguments, links,
2525 padding, etc. whereever they belong, and return the new stack
2528 If STRUCT_RETURN is non-zero, then the function we're calling is
2529 going to return a structure by value; STRUCT_ADDR is the address of
2530 a block we've allocated for it on the stack.
2532 Our caller has taken care of any type promotions needed to satisfy
2533 prototypes or the old K&R argument-passing rules. */
2535 s390_push_dummy_call (struct gdbarch *gdbarch, CORE_ADDR func_addr,
2536 struct regcache *regcache, CORE_ADDR bp_addr,
2537 int nargs, struct value **args, CORE_ADDR sp,
2538 int struct_return, CORE_ADDR struct_addr)
2540 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2541 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
2545 /* If the i'th argument is passed as a reference to a copy, then
2546 copy_addr[i] is the address of the copy we made. */
2547 CORE_ADDR *copy_addr = alloca (nargs * sizeof (CORE_ADDR));
2549 /* Build the reference-to-copy area. */
2550 for (i = 0; i < nargs; i++)
2552 struct value *arg = args[i];
2553 struct type *type = VALUE_TYPE (arg);
2554 unsigned length = TYPE_LENGTH (type);
2556 if (s390_function_arg_pass_by_reference (type))
2559 sp = align_down (sp, alignment_of (type));
2560 write_memory (sp, VALUE_CONTENTS (arg), length);
2565 /* Reserve space for the parameter area. As a conservative
2566 simplification, we assume that everything will be passed on the
2567 stack. Since every argument larger than 8 bytes will be
2568 passed by reference, we use this simple upper bound. */
2571 /* After all that, make sure it's still aligned on an eight-byte
2573 sp = align_down (sp, 8);
2575 /* Finally, place the actual parameters, working from SP towards
2576 higher addresses. The code above is supposed to reserve enough
2581 CORE_ADDR starg = sp;
2583 /* A struct is returned using general register 2. */
2586 regcache_cooked_write_unsigned (regcache, S390_R0_REGNUM + gr,
2591 for (i = 0; i < nargs; i++)
2593 struct value *arg = args[i];
2594 struct type *type = VALUE_TYPE (arg);
2595 unsigned length = TYPE_LENGTH (type);
2597 if (s390_function_arg_pass_by_reference (type))
2601 regcache_cooked_write_unsigned (regcache, S390_R0_REGNUM + gr,
2607 write_memory_unsigned_integer (starg, word_size, copy_addr[i]);
2611 else if (s390_function_arg_float (type))
2613 /* The GNU/Linux for S/390 ABI uses FPRs 0 and 2 to pass arguments,
2614 the GNU/Linux for zSeries ABI uses 0, 2, 4, and 6. */
2615 if (fr <= (tdep->abi == ABI_LINUX_S390 ? 2 : 6))
2617 /* When we store a single-precision value in an FP register,
2618 it occupies the leftmost bits. */
2619 regcache_cooked_write_part (regcache, S390_F0_REGNUM + fr,
2620 0, length, VALUE_CONTENTS (arg));
2625 /* When we store a single-precision value in a stack slot,
2626 it occupies the rightmost bits. */
2627 starg = align_up (starg + length, word_size);
2628 write_memory (starg - length, VALUE_CONTENTS (arg), length);
2631 else if (s390_function_arg_integer (type) && length <= word_size)
2635 /* Integer arguments are always extended to word size. */
2636 regcache_cooked_write_signed (regcache, S390_R0_REGNUM + gr,
2637 extend_simple_arg (arg));
2642 /* Integer arguments are always extended to word size. */
2643 write_memory_signed_integer (starg, word_size,
2644 extend_simple_arg (arg));
2648 else if (s390_function_arg_integer (type) && length == 2*word_size)
2652 regcache_cooked_write (regcache, S390_R0_REGNUM + gr,
2653 VALUE_CONTENTS (arg));
2654 regcache_cooked_write (regcache, S390_R0_REGNUM + gr + 1,
2655 VALUE_CONTENTS (arg) + word_size);
2660 /* If we skipped r6 because we couldn't fit a DOUBLE_ARG
2661 in it, then don't go back and use it again later. */
2664 write_memory (starg, VALUE_CONTENTS (arg), length);
2669 internal_error (__FILE__, __LINE__, "unknown argument type");
2673 /* Allocate the standard frame areas: the register save area, the
2674 word reserved for the compiler (which seems kind of meaningless),
2675 and the back chain pointer. */
2676 sp -= 16*word_size + 32;
2678 /* Write the back chain pointer into the first word of the stack
2679 frame. This is needed to unwind across a dummy frame. */
2680 regcache_cooked_read_unsigned (regcache, S390_SP_REGNUM, &orig_sp);
2681 write_memory_unsigned_integer (sp, word_size, orig_sp);
2683 /* Store return address. */
2684 regcache_cooked_write_unsigned (regcache, S390_RETADDR_REGNUM, bp_addr);
2686 /* Store updated stack pointer. */
2687 regcache_cooked_write_unsigned (regcache, S390_SP_REGNUM, sp);
2689 /* Return stack pointer. */
2693 /* Assuming NEXT_FRAME->prev is a dummy, return the frame ID of that
2694 dummy frame. The frame ID's base needs to match the TOS value
2695 returned by push_dummy_call, and the PC match the dummy frame's
2697 static struct frame_id
2698 s390_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
2701 frame_unwind_unsigned_register (next_frame, S390_SP_REGNUM, &sp);
2702 return frame_id_build (sp, frame_pc_unwind (next_frame));
2706 s390_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
2708 /* Both the 32- and 64-bit ABI's say that the stack pointer should
2709 always be aligned on an eight-byte boundary. */
2714 /* Function return value access. */
2716 static enum return_value_convention
2717 s390_return_value_convention (struct gdbarch *gdbarch, struct type *type)
2719 int length = TYPE_LENGTH (type);
2721 return RETURN_VALUE_STRUCT_CONVENTION;
2723 switch (TYPE_CODE (type))
2725 case TYPE_CODE_STRUCT:
2726 case TYPE_CODE_UNION:
2727 case TYPE_CODE_ARRAY:
2728 return RETURN_VALUE_STRUCT_CONVENTION;
2731 return RETURN_VALUE_REGISTER_CONVENTION;
2735 static enum return_value_convention
2736 s390_return_value (struct gdbarch *gdbarch, struct type *type,
2737 struct regcache *regcache, void *out, const void *in)
2739 int word_size = gdbarch_ptr_bit (gdbarch) / 8;
2740 int length = TYPE_LENGTH (type);
2741 enum return_value_convention rvc =
2742 s390_return_value_convention (gdbarch, type);
2747 case RETURN_VALUE_REGISTER_CONVENTION:
2748 if (TYPE_CODE (type) == TYPE_CODE_FLT)
2750 /* When we store a single-precision value in an FP register,
2751 it occupies the leftmost bits. */
2752 regcache_cooked_write_part (regcache, S390_F0_REGNUM,
2755 else if (length <= word_size)
2757 /* Integer arguments are always extended to word size. */
2758 if (TYPE_UNSIGNED (type))
2759 regcache_cooked_write_unsigned (regcache, S390_R2_REGNUM,
2760 extract_unsigned_integer (in, length));
2762 regcache_cooked_write_signed (regcache, S390_R2_REGNUM,
2763 extract_signed_integer (in, length));
2765 else if (length == 2*word_size)
2767 regcache_cooked_write (regcache, S390_R2_REGNUM, in);
2768 regcache_cooked_write (regcache, S390_R3_REGNUM,
2769 (const char *)in + word_size);
2772 internal_error (__FILE__, __LINE__, "invalid return type");
2775 case RETURN_VALUE_STRUCT_CONVENTION:
2776 error ("Cannot set function return value.");
2784 case RETURN_VALUE_REGISTER_CONVENTION:
2785 if (TYPE_CODE (type) == TYPE_CODE_FLT)
2787 /* When we store a single-precision value in an FP register,
2788 it occupies the leftmost bits. */
2789 regcache_cooked_read_part (regcache, S390_F0_REGNUM,
2792 else if (length <= word_size)
2794 /* Integer arguments occupy the rightmost bits. */
2795 regcache_cooked_read_part (regcache, S390_R2_REGNUM,
2796 word_size - length, length, out);
2798 else if (length == 2*word_size)
2800 regcache_cooked_read (regcache, S390_R2_REGNUM, out);
2801 regcache_cooked_read (regcache, S390_R3_REGNUM,
2802 (char *)out + word_size);
2805 internal_error (__FILE__, __LINE__, "invalid return type");
2808 case RETURN_VALUE_STRUCT_CONVENTION:
2809 error ("Function return value unknown.");
2818 static const unsigned char *
2819 s390_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
2821 static unsigned char breakpoint[] = { 0x0, 0x1 };
2823 *lenptr = sizeof (breakpoint);
2827 /* Advance PC across any function entry prologue instructions to reach some
2830 s390_skip_prologue (CORE_ADDR pc)
2832 struct frame_extra_info fextra_info;
2834 s390_get_frame_info (pc, &fextra_info, NULL, 1);
2835 return fextra_info.skip_prologue_function_start;
2838 /* Immediately after a function call, return the saved pc.
2839 Can't go through the frames for this because on some machines
2840 the new frame is not set up until the new function executes
2841 some instructions. */
2843 s390_saved_pc_after_call (struct frame_info *frame)
2845 return ADDR_BITS_REMOVE (read_register (S390_RETADDR_REGNUM));
2849 s390_addr_bits_remove (CORE_ADDR addr)
2851 return (addr) & 0x7fffffff;
2855 s390_address_class_type_flags (int byte_size, int dwarf2_addr_class)
2858 return TYPE_FLAG_ADDRESS_CLASS_1;
2864 s390_address_class_type_flags_to_name (struct gdbarch *gdbarch, int type_flags)
2866 if (type_flags & TYPE_FLAG_ADDRESS_CLASS_1)
2873 s390_address_class_name_to_type_flags (struct gdbarch *gdbarch, const char *name,
2874 int *type_flags_ptr)
2876 if (strcmp (name, "mode32") == 0)
2878 *type_flags_ptr = TYPE_FLAG_ADDRESS_CLASS_1;
2885 static struct gdbarch *
2886 s390_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2888 struct gdbarch *gdbarch;
2889 struct gdbarch_tdep *tdep;
2891 /* First see if there is already a gdbarch that can satisfy the request. */
2892 arches = gdbarch_list_lookup_by_info (arches, &info);
2894 return arches->gdbarch;
2896 /* None found: is the request for a s390 architecture? */
2897 if (info.bfd_arch_info->arch != bfd_arch_s390)
2898 return NULL; /* No; then it's not for us. */
2900 /* Yes: create a new gdbarch for the specified machine type. */
2901 tdep = XCALLOC (1, struct gdbarch_tdep);
2902 gdbarch = gdbarch_alloc (&info, tdep);
2904 /* NOTE: cagney/2002-12-06: This can be deleted when this arch is
2905 ready to unwind the PC first (see frame.c:get_prev_frame()). */
2906 set_gdbarch_deprecated_init_frame_pc (gdbarch, deprecated_init_frame_pc_default);
2908 set_gdbarch_believe_pcc_promotion (gdbarch, 0);
2909 set_gdbarch_char_signed (gdbarch, 0);
2911 set_gdbarch_deprecated_frame_chain (gdbarch, s390_frame_chain);
2912 set_gdbarch_deprecated_frame_init_saved_regs (gdbarch, s390_frame_init_saved_regs);
2913 /* Amount PC must be decremented by after a breakpoint. This is
2914 often the number of bytes returned by BREAKPOINT_FROM_PC but not
2916 set_gdbarch_decr_pc_after_break (gdbarch, 2);
2917 set_gdbarch_deprecated_pop_frame (gdbarch, s390_pop_frame);
2918 /* Stack grows downward. */
2919 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2920 set_gdbarch_breakpoint_from_pc (gdbarch, s390_breakpoint_from_pc);
2921 set_gdbarch_skip_prologue (gdbarch, s390_skip_prologue);
2922 set_gdbarch_deprecated_init_extra_frame_info (gdbarch, s390_init_extra_frame_info);
2923 set_gdbarch_deprecated_init_frame_pc_first (gdbarch, s390_init_frame_pc_first);
2924 set_gdbarch_deprecated_target_read_fp (gdbarch, s390_read_fp);
2925 set_gdbarch_in_function_epilogue_p (gdbarch, s390_in_function_epilogue_p);
2926 /* This function that tells us whether the function invocation represented
2927 by FI does not have a frame on the stack associated with it. If it
2928 does not, FRAMELESS is set to 1, else 0. */
2929 set_gdbarch_deprecated_frameless_function_invocation (gdbarch, s390_frameless_function_invocation);
2930 /* Return saved PC from a frame */
2931 set_gdbarch_deprecated_frame_saved_pc (gdbarch, s390_frame_saved_pc);
2932 /* DEPRECATED_FRAME_CHAIN takes a frame's nominal address and
2933 produces the frame's chain-pointer. */
2934 set_gdbarch_deprecated_frame_chain (gdbarch, s390_frame_chain);
2935 set_gdbarch_deprecated_saved_pc_after_call (gdbarch, s390_saved_pc_after_call);
2936 set_gdbarch_pc_regnum (gdbarch, S390_PC_REGNUM);
2937 set_gdbarch_sp_regnum (gdbarch, S390_SP_REGNUM);
2938 set_gdbarch_deprecated_fp_regnum (gdbarch, S390_SP_REGNUM);
2939 set_gdbarch_fp0_regnum (gdbarch, S390_F0_REGNUM);
2940 set_gdbarch_num_regs (gdbarch, S390_NUM_REGS);
2941 set_gdbarch_num_pseudo_regs (gdbarch, S390_NUM_PSEUDO_REGS);
2942 set_gdbarch_register_name (gdbarch, s390_register_name);
2943 set_gdbarch_register_type (gdbarch, s390_register_type);
2944 set_gdbarch_stab_reg_to_regnum (gdbarch, s390_dwarf_reg_to_regnum);
2945 set_gdbarch_dwarf_reg_to_regnum (gdbarch, s390_dwarf_reg_to_regnum);
2946 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, s390_dwarf_reg_to_regnum);
2947 set_gdbarch_convert_register_p (gdbarch, s390_convert_register_p);
2948 set_gdbarch_register_to_value (gdbarch, s390_register_to_value);
2949 set_gdbarch_value_to_register (gdbarch, s390_value_to_register);
2950 set_gdbarch_register_reggroup_p (gdbarch, s390_register_reggroup_p);
2951 set_gdbarch_regset_from_core_section (gdbarch,
2952 s390_regset_from_core_section);
2954 /* Inferior function calls. */
2955 set_gdbarch_push_dummy_call (gdbarch, s390_push_dummy_call);
2956 set_gdbarch_unwind_dummy_id (gdbarch, s390_unwind_dummy_id);
2957 set_gdbarch_frame_align (gdbarch, s390_frame_align);
2958 set_gdbarch_return_value (gdbarch, s390_return_value);
2960 switch (info.bfd_arch_info->mach)
2962 case bfd_mach_s390_31:
2963 tdep->abi = ABI_LINUX_S390;
2965 tdep->gregset = &s390_gregset;
2966 tdep->sizeof_gregset = s390_sizeof_gregset;
2967 tdep->fpregset = &s390_fpregset;
2968 tdep->sizeof_fpregset = s390_sizeof_fpregset;
2970 set_gdbarch_addr_bits_remove (gdbarch, s390_addr_bits_remove);
2971 set_gdbarch_pseudo_register_read (gdbarch, s390_pseudo_register_read);
2972 set_gdbarch_pseudo_register_write (gdbarch, s390_pseudo_register_write);
2974 case bfd_mach_s390_64:
2975 tdep->abi = ABI_LINUX_ZSERIES;
2977 tdep->gregset = &s390x_gregset;
2978 tdep->sizeof_gregset = s390x_sizeof_gregset;
2979 tdep->fpregset = &s390_fpregset;
2980 tdep->sizeof_fpregset = s390_sizeof_fpregset;
2982 set_gdbarch_long_bit (gdbarch, 64);
2983 set_gdbarch_long_long_bit (gdbarch, 64);
2984 set_gdbarch_ptr_bit (gdbarch, 64);
2985 set_gdbarch_pseudo_register_read (gdbarch, s390x_pseudo_register_read);
2986 set_gdbarch_pseudo_register_write (gdbarch, s390x_pseudo_register_write);
2987 set_gdbarch_address_class_type_flags (gdbarch,
2988 s390_address_class_type_flags);
2989 set_gdbarch_address_class_type_flags_to_name (gdbarch,
2990 s390_address_class_type_flags_to_name);
2991 set_gdbarch_address_class_name_to_type_flags (gdbarch,
2992 s390_address_class_name_to_type_flags);
2996 set_gdbarch_print_insn (gdbarch, print_insn_s390);
3003 extern initialize_file_ftype _initialize_s390_tdep; /* -Wmissing-prototypes */
3006 _initialize_s390_tdep (void)
3009 /* Hook us into the gdbarch mechanism. */
3010 register_gdbarch_init (bfd_arch_s390, s390_gdbarch_init);