- *r1 = (insn[1] >> 4) & 0xf;
- *r3 = insn[1] & 0xf;
- *b2 = (insn[2] >> 4) & 0xf;
- /* The 'long displacement' is a 20-bit signed integer. */
- *d2 = ((((insn[2] & 0xf) << 8) | insn[3] | (insn[4] << 12))
- ^ 0x80000) - 0x80000;
- return 1;
- }
- else
- return 0;
-}
-
-
-static int
-is_rsi (bfd_byte *insn, int op,
- unsigned int *r1, unsigned int *r3, int *i2)
-{
- if (insn[0] == op)
- {
- *r1 = (insn[1] >> 4) & 0xf;
- *r3 = insn[1] & 0xf;
- /* i2 is a 16-bit signed quantity. */
- *i2 = (((insn[2] << 8) | insn[3]) ^ 0x8000) - 0x8000;
- return 1;
- }
- else
- return 0;
-}
-
-
-static int
-is_rie (bfd_byte *insn, int op1, int op2,
- unsigned int *r1, unsigned int *r3, int *i2)
-{
- if (insn[0] == op1
- && insn[5] == op2)
- {
- *r1 = (insn[1] >> 4) & 0xf;
- *r3 = insn[1] & 0xf;
- /* i2 is a 16-bit signed quantity. */
- *i2 = (((insn[2] << 8) | insn[3]) ^ 0x8000) - 0x8000;
- return 1;
- }
- else
- return 0;
-}
-
-
-static int
-is_rx (bfd_byte *insn, int op,
- unsigned int *r1, int *d2, unsigned int *x2, unsigned int *b2)
-{
- if (insn[0] == op)
- {
- *r1 = (insn[1] >> 4) & 0xf;
- *x2 = insn[1] & 0xf;
- *b2 = (insn[2] >> 4) & 0xf;
- *d2 = ((insn[2] & 0xf) << 8) | insn[3];
- return 1;
- }
- else
- return 0;
-}
-
-
-static int
-is_rxy (bfd_byte *insn, int op1, int op2,
- unsigned int *r1, int *d2, unsigned int *x2, unsigned int *b2)
-{
- if (insn[0] == op1
- && insn[5] == op2)
- {
- *r1 = (insn[1] >> 4) & 0xf;
- *x2 = insn[1] & 0xf;
- *b2 = (insn[2] >> 4) & 0xf;
- /* The 'long displacement' is a 20-bit signed integer. */
- *d2 = ((((insn[2] & 0xf) << 8) | insn[3] | (insn[4] << 12))
- ^ 0x80000) - 0x80000;
- return 1;
- }
- else
- return 0;
-}
-
-
-/* Prologue analysis. */
-
-#define S390_NUM_GPRS 16
-#define S390_NUM_FPRS 16
-
-struct s390_prologue_data {
-
- /* The stack. */
- struct pv_area *stack;
-
- /* The size and byte-order of a GPR or FPR. */
- int gpr_size;
- int fpr_size;
- enum bfd_endian byte_order;
-
- /* The general-purpose registers. */
- pv_t gpr[S390_NUM_GPRS];
-
- /* The floating-point registers. */
- pv_t fpr[S390_NUM_FPRS];
-
- /* The offset relative to the CFA where the incoming GPR N was saved
- by the function prologue. 0 if not saved or unknown. */
- int gpr_slot[S390_NUM_GPRS];
-
- /* Likewise for FPRs. */
- int fpr_slot[S390_NUM_FPRS];
-
- /* Nonzero if the backchain was saved. This is assumed to be the
- case when the incoming SP is saved at the current SP location. */
- int back_chain_saved_p;
-};
-
-/* Return the effective address for an X-style instruction, like:
-
- L R1, D2(X2, B2)
-
- Here, X2 and B2 are registers, and D2 is a signed 20-bit
- constant; the effective address is the sum of all three. If either
- X2 or B2 are zero, then it doesn't contribute to the sum --- this
- means that r0 can't be used as either X2 or B2. */
-static pv_t
-s390_addr (struct s390_prologue_data *data,
- int d2, unsigned int x2, unsigned int b2)
-{
- pv_t result;
-
- result = pv_constant (d2);
- if (x2)
- result = pv_add (result, data->gpr[x2]);
- if (b2)
- result = pv_add (result, data->gpr[b2]);
-
- return result;
-}
-
-/* Do a SIZE-byte store of VALUE to D2(X2,B2). */
-static void
-s390_store (struct s390_prologue_data *data,
- int d2, unsigned int x2, unsigned int b2, CORE_ADDR size,
- pv_t value)
-{
- pv_t addr = s390_addr (data, d2, x2, b2);
- pv_t offset;
-
- /* Check whether we are storing the backchain. */
- offset = pv_subtract (data->gpr[S390_SP_REGNUM - S390_R0_REGNUM], addr);
-
- if (pv_is_constant (offset) && offset.k == 0)
- if (size == data->gpr_size
- && pv_is_register_k (value, S390_SP_REGNUM, 0))
- {
- data->back_chain_saved_p = 1;
- return;
- }
-
-
- /* Check whether we are storing a register into the stack. */
- if (!pv_area_store_would_trash (data->stack, addr))
- pv_area_store (data->stack, addr, size, value);
-
-
- /* Note: If this is some store we cannot identify, you might think we
- should forget our cached values, as any of those might have been hit.
-
- However, we make the assumption that the register save areas are only
- ever stored to once in any given function, and we do recognize these
- stores. Thus every store we cannot recognize does not hit our data. */
-}
-
-/* Do a SIZE-byte load from D2(X2,B2). */
-static pv_t
-s390_load (struct s390_prologue_data *data,
- int d2, unsigned int x2, unsigned int b2, CORE_ADDR size)
-
-{
- pv_t addr = s390_addr (data, d2, x2, b2);
-
- /* If it's a load from an in-line constant pool, then we can
- simulate that, under the assumption that the code isn't
- going to change between the time the processor actually
- executed it creating the current frame, and the time when
- we're analyzing the code to unwind past that frame. */
- if (pv_is_constant (addr))
- {
- struct target_section *secp;
- secp = target_section_by_addr (¤t_target, addr.k);
- if (secp != NULL
- && (bfd_get_section_flags (secp->the_bfd_section->owner,
- secp->the_bfd_section)
- & SEC_READONLY))
- return pv_constant (read_memory_integer (addr.k, size,
- data->byte_order));
- }
-
- /* Check whether we are accessing one of our save slots. */
- return pv_area_fetch (data->stack, addr, size);
-}
-
-/* Function for finding saved registers in a 'struct pv_area'; we pass
- this to pv_area_scan.
-
- If VALUE is a saved register, ADDR says it was saved at a constant
- offset from the frame base, and SIZE indicates that the whole
- register was saved, record its offset in the reg_offset table in
- PROLOGUE_UNTYPED. */
-static void
-s390_check_for_saved (void *data_untyped, pv_t addr,
- CORE_ADDR size, pv_t value)
-{
- struct s390_prologue_data *data = data_untyped;
- int i, offset;
-
- if (!pv_is_register (addr, S390_SP_REGNUM))
- return;
-
- offset = 16 * data->gpr_size + 32 - addr.k;
-
- /* If we are storing the original value of a register, we want to
- record the CFA offset. If the same register is stored multiple
- times, the stack slot with the highest address counts. */
-
- for (i = 0; i < S390_NUM_GPRS; i++)
- if (size == data->gpr_size
- && pv_is_register_k (value, S390_R0_REGNUM + i, 0))
- if (data->gpr_slot[i] == 0
- || data->gpr_slot[i] > offset)
- {
- data->gpr_slot[i] = offset;
- return;
- }
-
- for (i = 0; i < S390_NUM_FPRS; i++)
- if (size == data->fpr_size
- && pv_is_register_k (value, S390_F0_REGNUM + i, 0))
- if (data->fpr_slot[i] == 0
- || data->fpr_slot[i] > offset)
- {
- data->fpr_slot[i] = offset;
- return;
- }
-}
-
-/* Analyze the prologue of the function starting at START_PC,
- continuing at most until CURRENT_PC. Initialize DATA to
- hold all information we find out about the state of the registers
- and stack slots. Return the address of the instruction after
- the last one that changed the SP, FP, or back chain; or zero
- on error. */
-static CORE_ADDR
-s390_analyze_prologue (struct gdbarch *gdbarch,
- CORE_ADDR start_pc,
- CORE_ADDR current_pc,
- struct s390_prologue_data *data)
-{
- int word_size = gdbarch_ptr_bit (gdbarch) / 8;
-
- /* Our return value:
- The address of the instruction after the last one that changed
- the SP, FP, or back chain; zero if we got an error trying to
- read memory. */
- CORE_ADDR result = start_pc;
-
- /* The current PC for our abstract interpretation. */
- CORE_ADDR pc;
-
- /* The address of the next instruction after that. */
- CORE_ADDR next_pc;
-
- /* Set up everything's initial value. */
- {
- int i;
-
- data->stack = make_pv_area (S390_SP_REGNUM, gdbarch_addr_bit (gdbarch));
-
- /* For the purpose of prologue tracking, we consider the GPR size to
- be equal to the ABI word size, even if it is actually larger
- (i.e. when running a 32-bit binary under a 64-bit kernel). */
- data->gpr_size = word_size;
- data->fpr_size = 8;
- data->byte_order = gdbarch_byte_order (gdbarch);
-
- for (i = 0; i < S390_NUM_GPRS; i++)
- data->gpr[i] = pv_register (S390_R0_REGNUM + i, 0);
-
- for (i = 0; i < S390_NUM_FPRS; i++)
- data->fpr[i] = pv_register (S390_F0_REGNUM + i, 0);
-
- for (i = 0; i < S390_NUM_GPRS; i++)
- data->gpr_slot[i] = 0;
-
- for (i = 0; i < S390_NUM_FPRS; i++)
- data->fpr_slot[i] = 0;
-
- data->back_chain_saved_p = 0;
- }
-
- /* Start interpreting instructions, until we hit the frame's
- current PC or the first branch instruction. */
- for (pc = start_pc; pc > 0 && pc < current_pc; pc = next_pc)
- {
- bfd_byte insn[S390_MAX_INSTR_SIZE];
- int insn_len = s390_readinstruction (insn, pc);
-
- bfd_byte dummy[S390_MAX_INSTR_SIZE] = { 0 };
- bfd_byte *insn32 = word_size == 4 ? insn : dummy;
- bfd_byte *insn64 = word_size == 8 ? insn : dummy;
-
- /* Fields for various kinds of instructions. */
- unsigned int b2, r1, r2, x2, r3;
- int i2, d2;
-
- /* The values of SP and FP before this instruction,
- for detecting instructions that change them. */
- pv_t pre_insn_sp, pre_insn_fp;
- /* Likewise for the flag whether the back chain was saved. */
- int pre_insn_back_chain_saved_p;
-
- /* If we got an error trying to read the instruction, report it. */
- if (insn_len < 0)
- {
- result = 0;
- break;
- }
-
- next_pc = pc + insn_len;
-
- pre_insn_sp = data->gpr[S390_SP_REGNUM - S390_R0_REGNUM];
- pre_insn_fp = data->gpr[S390_FRAME_REGNUM - S390_R0_REGNUM];
- pre_insn_back_chain_saved_p = data->back_chain_saved_p;
-
-
- /* LHI r1, i2 --- load halfword immediate. */
- /* LGHI r1, i2 --- load halfword immediate (64-bit version). */
- /* LGFI r1, i2 --- load fullword immediate. */
- if (is_ri (insn32, op1_lhi, op2_lhi, &r1, &i2)
- || is_ri (insn64, op1_lghi, op2_lghi, &r1, &i2)
- || is_ril (insn, op1_lgfi, op2_lgfi, &r1, &i2))
- data->gpr[r1] = pv_constant (i2);
-
- /* LR r1, r2 --- load from register. */
- /* LGR r1, r2 --- load from register (64-bit version). */
- else if (is_rr (insn32, op_lr, &r1, &r2)
- || is_rre (insn64, op_lgr, &r1, &r2))
- data->gpr[r1] = data->gpr[r2];
-
- /* L r1, d2(x2, b2) --- load. */
- /* LY r1, d2(x2, b2) --- load (long-displacement version). */
- /* LG r1, d2(x2, b2) --- load (64-bit version). */
- else if (is_rx (insn32, op_l, &r1, &d2, &x2, &b2)
- || is_rxy (insn32, op1_ly, op2_ly, &r1, &d2, &x2, &b2)
- || is_rxy (insn64, op1_lg, op2_lg, &r1, &d2, &x2, &b2))
- data->gpr[r1] = s390_load (data, d2, x2, b2, data->gpr_size);
-
- /* ST r1, d2(x2, b2) --- store. */
- /* STY r1, d2(x2, b2) --- store (long-displacement version). */
- /* STG r1, d2(x2, b2) --- store (64-bit version). */
- else if (is_rx (insn32, op_st, &r1, &d2, &x2, &b2)
- || is_rxy (insn32, op1_sty, op2_sty, &r1, &d2, &x2, &b2)
- || is_rxy (insn64, op1_stg, op2_stg, &r1, &d2, &x2, &b2))
- s390_store (data, d2, x2, b2, data->gpr_size, data->gpr[r1]);
-
- /* STD r1, d2(x2,b2) --- store floating-point register. */
- else if (is_rx (insn, op_std, &r1, &d2, &x2, &b2))
- s390_store (data, d2, x2, b2, data->fpr_size, data->fpr[r1]);
-
- /* STM r1, r3, d2(b2) --- store multiple. */
- /* STMY r1, r3, d2(b2) --- store multiple (long-displacement
- version). */
- /* STMG r1, r3, d2(b2) --- store multiple (64-bit version). */
- else if (is_rs (insn32, op_stm, &r1, &r3, &d2, &b2)
- || is_rsy (insn32, op1_stmy, op2_stmy, &r1, &r3, &d2, &b2)
- || is_rsy (insn64, op1_stmg, op2_stmg, &r1, &r3, &d2, &b2))
- {
- for (; r1 <= r3; r1++, d2 += data->gpr_size)
- s390_store (data, d2, 0, b2, data->gpr_size, data->gpr[r1]);
- }
-
- /* AHI r1, i2 --- add halfword immediate. */
- /* AGHI r1, i2 --- add halfword immediate (64-bit version). */
- /* AFI r1, i2 --- add fullword immediate. */
- /* AGFI r1, i2 --- add fullword immediate (64-bit version). */
- else if (is_ri (insn32, op1_ahi, op2_ahi, &r1, &i2)
- || is_ri (insn64, op1_aghi, op2_aghi, &r1, &i2)
- || is_ril (insn32, op1_afi, op2_afi, &r1, &i2)
- || is_ril (insn64, op1_agfi, op2_agfi, &r1, &i2))
- data->gpr[r1] = pv_add_constant (data->gpr[r1], i2);
-
- /* ALFI r1, i2 --- add logical immediate. */
- /* ALGFI r1, i2 --- add logical immediate (64-bit version). */
- else if (is_ril (insn32, op1_alfi, op2_alfi, &r1, &i2)
- || is_ril (insn64, op1_algfi, op2_algfi, &r1, &i2))
- data->gpr[r1] = pv_add_constant (data->gpr[r1],
- (CORE_ADDR)i2 & 0xffffffff);
-
- /* AR r1, r2 -- add register. */
- /* AGR r1, r2 -- add register (64-bit version). */
- else if (is_rr (insn32, op_ar, &r1, &r2)
- || is_rre (insn64, op_agr, &r1, &r2))
- data->gpr[r1] = pv_add (data->gpr[r1], data->gpr[r2]);
-
- /* A r1, d2(x2, b2) -- add. */
- /* AY r1, d2(x2, b2) -- add (long-displacement version). */
- /* AG r1, d2(x2, b2) -- add (64-bit version). */
- else if (is_rx (insn32, op_a, &r1, &d2, &x2, &b2)
- || is_rxy (insn32, op1_ay, op2_ay, &r1, &d2, &x2, &b2)
- || is_rxy (insn64, op1_ag, op2_ag, &r1, &d2, &x2, &b2))
- data->gpr[r1] = pv_add (data->gpr[r1],
- s390_load (data, d2, x2, b2, data->gpr_size));
-
- /* SLFI r1, i2 --- subtract logical immediate. */
- /* SLGFI r1, i2 --- subtract logical immediate (64-bit version). */
- else if (is_ril (insn32, op1_slfi, op2_slfi, &r1, &i2)
- || is_ril (insn64, op1_slgfi, op2_slgfi, &r1, &i2))
- data->gpr[r1] = pv_add_constant (data->gpr[r1],
- -((CORE_ADDR)i2 & 0xffffffff));
-
- /* SR r1, r2 -- subtract register. */
- /* SGR r1, r2 -- subtract register (64-bit version). */
- else if (is_rr (insn32, op_sr, &r1, &r2)
- || is_rre (insn64, op_sgr, &r1, &r2))
- data->gpr[r1] = pv_subtract (data->gpr[r1], data->gpr[r2]);
-
- /* S r1, d2(x2, b2) -- subtract. */
- /* SY r1, d2(x2, b2) -- subtract (long-displacement version). */
- /* SG r1, d2(x2, b2) -- subtract (64-bit version). */
- else if (is_rx (insn32, op_s, &r1, &d2, &x2, &b2)
- || is_rxy (insn32, op1_sy, op2_sy, &r1, &d2, &x2, &b2)
- || is_rxy (insn64, op1_sg, op2_sg, &r1, &d2, &x2, &b2))
- data->gpr[r1] = pv_subtract (data->gpr[r1],
- s390_load (data, d2, x2, b2, data->gpr_size));
-
- /* LA r1, d2(x2, b2) --- load address. */
- /* LAY r1, d2(x2, b2) --- load address (long-displacement version). */
- else if (is_rx (insn, op_la, &r1, &d2, &x2, &b2)
- || is_rxy (insn, op1_lay, op2_lay, &r1, &d2, &x2, &b2))
- data->gpr[r1] = s390_addr (data, d2, x2, b2);
-
- /* LARL r1, i2 --- load address relative long. */
- else if (is_ril (insn, op1_larl, op2_larl, &r1, &i2))
- data->gpr[r1] = pv_constant (pc + i2 * 2);
-
- /* BASR r1, 0 --- branch and save.
- Since r2 is zero, this saves the PC in r1, but doesn't branch. */
- else if (is_rr (insn, op_basr, &r1, &r2)
- && r2 == 0)
- data->gpr[r1] = pv_constant (next_pc);
-
- /* BRAS r1, i2 --- branch relative and save. */
- else if (is_ri (insn, op1_bras, op2_bras, &r1, &i2))
- {
- data->gpr[r1] = pv_constant (next_pc);
- next_pc = pc + i2 * 2;
-
- /* We'd better not interpret any backward branches. We'll
- never terminate. */
- if (next_pc <= pc)
- break;
- }
-
- /* Terminate search when hitting any other branch instruction. */
- else if (is_rr (insn, op_basr, &r1, &r2)
- || is_rx (insn, op_bas, &r1, &d2, &x2, &b2)
- || is_rr (insn, op_bcr, &r1, &r2)
- || is_rx (insn, op_bc, &r1, &d2, &x2, &b2)
- || is_ri (insn, op1_brc, op2_brc, &r1, &i2)
- || is_ril (insn, op1_brcl, op2_brcl, &r1, &i2)
- || is_ril (insn, op1_brasl, op2_brasl, &r2, &i2))
- break;
-
- else
- {
- /* An instruction we don't know how to simulate. The only
- safe thing to do would be to set every value we're tracking
- to 'unknown'. Instead, we'll be optimistic: we assume that
- we *can* interpret every instruction that the compiler uses
- to manipulate any of the data we're interested in here --
- then we can just ignore anything else. */
- }
-
- /* Record the address after the last instruction that changed
- the FP, SP, or backlink. Ignore instructions that changed
- them back to their original values --- those are probably
- restore instructions. (The back chain is never restored,
- just popped.) */
- {
- pv_t sp = data->gpr[S390_SP_REGNUM - S390_R0_REGNUM];
- pv_t fp = data->gpr[S390_FRAME_REGNUM - S390_R0_REGNUM];
-
- if ((! pv_is_identical (pre_insn_sp, sp)
- && ! pv_is_register_k (sp, S390_SP_REGNUM, 0)
- && sp.kind != pvk_unknown)
- || (! pv_is_identical (pre_insn_fp, fp)
- && ! pv_is_register_k (fp, S390_FRAME_REGNUM, 0)
- && fp.kind != pvk_unknown)
- || pre_insn_back_chain_saved_p != data->back_chain_saved_p)
- result = next_pc;
- }
- }
-
- /* Record where all the registers were saved. */
- pv_area_scan (data->stack, s390_check_for_saved, data);
-
- free_pv_area (data->stack);
- data->stack = NULL;
-
- return result;
-}
-
-/* Advance PC across any function entry prologue instructions to reach
- some "real" code. */
-static CORE_ADDR
-s390_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
-{
- struct s390_prologue_data data;
- CORE_ADDR skip_pc;
- skip_pc = s390_analyze_prologue (gdbarch, pc, (CORE_ADDR)-1, &data);
- return skip_pc ? skip_pc : pc;
-}
-
-/* Return true if we are in the functin's epilogue, i.e. after the
- instruction that destroyed the function's stack frame. */
-static int
-s390_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
-{
- int word_size = gdbarch_ptr_bit (gdbarch) / 8;
-
- /* In frameless functions, there's not frame to destroy and thus
- we don't care about the epilogue.
-
- In functions with frame, the epilogue sequence is a pair of
- a LM-type instruction that restores (amongst others) the
- return register %r14 and the stack pointer %r15, followed
- by a branch 'br %r14' --or equivalent-- that effects the
- actual return.
-
- In that situation, this function needs to return 'true' in
- exactly one case: when pc points to that branch instruction.
-
- Thus we try to disassemble the one instructions immediately
- preceding pc and check whether it is an LM-type instruction
- modifying the stack pointer.
-
- Note that disassembling backwards is not reliable, so there
- is a slight chance of false positives here ... */
-
- bfd_byte insn[6];
- unsigned int r1, r3, b2;
- int d2;
-
- if (word_size == 4
- && !target_read_memory (pc - 4, insn, 4)
- && is_rs (insn, op_lm, &r1, &r3, &d2, &b2)
- && r3 == S390_SP_REGNUM - S390_R0_REGNUM)
- return 1;
-
- if (word_size == 4
- && !target_read_memory (pc - 6, insn, 6)
- && is_rsy (insn, op1_lmy, op2_lmy, &r1, &r3, &d2, &b2)
- && r3 == S390_SP_REGNUM - S390_R0_REGNUM)
- return 1;
-
- if (word_size == 8
- && !target_read_memory (pc - 6, insn, 6)
- && is_rsy (insn, op1_lmg, op2_lmg, &r1, &r3, &d2, &b2)
- && r3 == S390_SP_REGNUM - S390_R0_REGNUM)
- return 1;
-
- return 0;
-}
-
-/* Displaced stepping. */
-
-/* Fix up the state of registers and memory after having single-stepped
- a displaced instruction. */
-static void
-s390_displaced_step_fixup (struct gdbarch *gdbarch,
- struct displaced_step_closure *closure,
- CORE_ADDR from, CORE_ADDR to,
- struct regcache *regs)
-{
- /* Since we use simple_displaced_step_copy_insn, our closure is a
- copy of the instruction. */
- gdb_byte *insn = (gdb_byte *) closure;
- static int s390_instrlen[] = { 2, 4, 4, 6 };
- int insnlen = s390_instrlen[insn[0] >> 6];
-
- /* Fields for various kinds of instructions. */
- unsigned int b2, r1, r2, x2, r3;
- int i2, d2;
-
- /* Get current PC and addressing mode bit. */
- CORE_ADDR pc = regcache_read_pc (regs);
- ULONGEST amode = 0;
-
- if (register_size (gdbarch, S390_PSWA_REGNUM) == 4)
- {
- regcache_cooked_read_unsigned (regs, S390_PSWA_REGNUM, &amode);
- amode &= 0x80000000;
- }
-
- if (debug_displaced)
- fprintf_unfiltered (gdb_stdlog,
- "displaced: (s390) fixup (%s, %s) pc %s len %d amode 0x%x\n",
- paddress (gdbarch, from), paddress (gdbarch, to),
- paddress (gdbarch, pc), insnlen, (int) amode);
-
- /* Handle absolute branch and save instructions. */
- if (is_rr (insn, op_basr, &r1, &r2)
- || is_rx (insn, op_bas, &r1, &d2, &x2, &b2))
- {
- /* Recompute saved return address in R1. */
- regcache_cooked_write_unsigned (regs, S390_R0_REGNUM + r1,
- amode | (from + insnlen));
- }
-
- /* Handle absolute branch instructions. */
- else if (is_rr (insn, op_bcr, &r1, &r2)
- || is_rx (insn, op_bc, &r1, &d2, &x2, &b2)
- || is_rr (insn, op_bctr, &r1, &r2)
- || is_rre (insn, op_bctgr, &r1, &r2)
- || is_rx (insn, op_bct, &r1, &d2, &x2, &b2)
- || is_rxy (insn, op1_bctg, op2_brctg, &r1, &d2, &x2, &b2)
- || is_rs (insn, op_bxh, &r1, &r3, &d2, &b2)
- || is_rsy (insn, op1_bxhg, op2_bxhg, &r1, &r3, &d2, &b2)
- || is_rs (insn, op_bxle, &r1, &r3, &d2, &b2)
- || is_rsy (insn, op1_bxleg, op2_bxleg, &r1, &r3, &d2, &b2))
- {
- /* Update PC iff branch was *not* taken. */
- if (pc == to + insnlen)
- regcache_write_pc (regs, from + insnlen);
- }
-
- /* Handle PC-relative branch and save instructions. */
- else if (is_ri (insn, op1_bras, op2_bras, &r1, &i2)
- || is_ril (insn, op1_brasl, op2_brasl, &r1, &i2))
- {
- /* Update PC. */
- regcache_write_pc (regs, pc - to + from);
- /* Recompute saved return address in R1. */
- regcache_cooked_write_unsigned (regs, S390_R0_REGNUM + r1,
- amode | (from + insnlen));
- }
-
- /* Handle PC-relative branch instructions. */
- else if (is_ri (insn, op1_brc, op2_brc, &r1, &i2)
- || is_ril (insn, op1_brcl, op2_brcl, &r1, &i2)
- || is_ri (insn, op1_brct, op2_brct, &r1, &i2)
- || is_ri (insn, op1_brctg, op2_brctg, &r1, &i2)
- || is_rsi (insn, op_brxh, &r1, &r3, &i2)
- || is_rie (insn, op1_brxhg, op2_brxhg, &r1, &r3, &i2)
- || is_rsi (insn, op_brxle, &r1, &r3, &i2)
- || is_rie (insn, op1_brxlg, op2_brxlg, &r1, &r3, &i2))
- {
- /* Update PC. */
- regcache_write_pc (regs, pc - to + from);
- }
-
- /* Handle LOAD ADDRESS RELATIVE LONG. */
- else if (is_ril (insn, op1_larl, op2_larl, &r1, &i2))
- {
- /* Update PC. */
- regcache_write_pc (regs, from + insnlen);
- /* Recompute output address in R1. */
- regcache_cooked_write_unsigned (regs, S390_R0_REGNUM + r1,
- amode | (from + i2 * 2));
- }
-
- /* If we executed a breakpoint instruction, point PC right back at it. */
- else if (insn[0] == 0x0 && insn[1] == 0x1)
- regcache_write_pc (regs, from);
-
- /* For any other insn, PC points right after the original instruction. */
- else
- regcache_write_pc (regs, from + insnlen);
-
- if (debug_displaced)
- fprintf_unfiltered (gdb_stdlog,
- "displaced: (s390) pc is now %s\n",
- paddress (gdbarch, regcache_read_pc (regs)));
-}
-
-
-/* Helper routine to unwind pseudo registers. */
-
-static struct value *
-s390_unwind_pseudo_register (struct frame_info *this_frame, int regnum)
-{
- struct gdbarch *gdbarch = get_frame_arch (this_frame);
- struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
- struct type *type = register_type (gdbarch, regnum);
-
- /* Unwind PC via PSW address. */
- if (regnum == tdep->pc_regnum)
- {
- struct value *val;
-
- val = frame_unwind_register_value (this_frame, S390_PSWA_REGNUM);
- if (!value_optimized_out (val))
- {
- LONGEST pswa = value_as_long (val);
-
- if (TYPE_LENGTH (type) == 4)
- return value_from_pointer (type, pswa & 0x7fffffff);
- else
- return value_from_pointer (type, pswa);
- }
- }
-
- /* Unwind CC via PSW mask. */
- if (regnum == tdep->cc_regnum)
- {
- struct value *val;
-
- val = frame_unwind_register_value (this_frame, S390_PSWM_REGNUM);
- if (!value_optimized_out (val))
- {
- LONGEST pswm = value_as_long (val);
-
- if (TYPE_LENGTH (type) == 4)
- return value_from_longest (type, (pswm >> 12) & 3);
- else
- return value_from_longest (type, (pswm >> 44) & 3);
- }
- }
-
- /* Unwind full GPRs to show at least the lower halves (as the
- upper halves are undefined). */
- if (regnum_is_gpr_full (tdep, regnum))
- {
- int reg = regnum - tdep->gpr_full_regnum;
- struct value *val;
-
- val = frame_unwind_register_value (this_frame, S390_R0_REGNUM + reg);
- if (!value_optimized_out (val))
- return value_cast (type, val);
- }
-
- return allocate_optimized_out_value (type);
-}
-
-static struct value *
-s390_trad_frame_prev_register (struct frame_info *this_frame,
- struct trad_frame_saved_reg saved_regs[],
- int regnum)
-{
- if (regnum < S390_NUM_REGS)
- return trad_frame_get_prev_register (this_frame, saved_regs, regnum);
- else
- return s390_unwind_pseudo_register (this_frame, regnum);
-}
-
-
-/* Normal stack frames. */
-
-struct s390_unwind_cache {
-
- CORE_ADDR func;
- CORE_ADDR frame_base;
- CORE_ADDR local_base;
-
- struct trad_frame_saved_reg *saved_regs;
-};
-
-static int
-s390_prologue_frame_unwind_cache (struct frame_info *this_frame,
- struct s390_unwind_cache *info)
-{
- struct gdbarch *gdbarch = get_frame_arch (this_frame);
- int word_size = gdbarch_ptr_bit (gdbarch) / 8;
- struct s390_prologue_data data;
- pv_t *fp = &data.gpr[S390_FRAME_REGNUM - S390_R0_REGNUM];
- pv_t *sp = &data.gpr[S390_SP_REGNUM - S390_R0_REGNUM];
- int i;
- CORE_ADDR cfa;
- CORE_ADDR func;
- CORE_ADDR result;
- ULONGEST reg;
- CORE_ADDR prev_sp;
- int frame_pointer;
- int size;
- struct frame_info *next_frame;
-
- /* Try to find the function start address. If we can't find it, we don't
- bother searching for it -- with modern compilers this would be mostly
- pointless anyway. Trust that we'll either have valid DWARF-2 CFI data
- or else a valid backchain ... */
- func = get_frame_func (this_frame);
- if (!func)
- return 0;
-
- /* Try to analyze the prologue. */
- result = s390_analyze_prologue (gdbarch, func,
- get_frame_pc (this_frame), &data);
- if (!result)
- return 0;
-
- /* If this was successful, we should have found the instruction that
- sets the stack pointer register to the previous value of the stack
- pointer minus the frame size. */
- if (!pv_is_register (*sp, S390_SP_REGNUM))
- return 0;
-
- /* A frame size of zero at this point can mean either a real
- frameless function, or else a failure to find the prologue.
- Perform some sanity checks to verify we really have a
- frameless function. */
- if (sp->k == 0)
- {
- /* If the next frame is a NORMAL_FRAME, this frame *cannot* have frame
- size zero. This is only possible if the next frame is a sentinel
- frame, a dummy frame, or a signal trampoline frame. */
- /* FIXME: cagney/2004-05-01: This sanity check shouldn't be
- needed, instead the code should simpliy rely on its
- analysis. */
- next_frame = get_next_frame (this_frame);
- while (next_frame && get_frame_type (next_frame) == INLINE_FRAME)
- next_frame = get_next_frame (next_frame);
- if (next_frame
- && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
- return 0;
-
- /* If we really have a frameless function, %r14 must be valid
- -- in particular, it must point to a different function. */
- reg = get_frame_register_unsigned (this_frame, S390_RETADDR_REGNUM);
- reg = gdbarch_addr_bits_remove (gdbarch, reg) - 1;
- if (get_pc_function_start (reg) == func)
- {
- /* However, there is one case where it *is* valid for %r14
- to point to the same function -- if this is a recursive
- call, and we have stopped in the prologue *before* the
- stack frame was allocated.
-
- Recognize this case by looking ahead a bit ... */
-
- struct s390_prologue_data data2;
- pv_t *sp = &data2.gpr[S390_SP_REGNUM - S390_R0_REGNUM];
-
- if (!(s390_analyze_prologue (gdbarch, func, (CORE_ADDR)-1, &data2)
- && pv_is_register (*sp, S390_SP_REGNUM)
- && sp->k != 0))
- return 0;
- }
- }
-
-
- /* OK, we've found valid prologue data. */
- size = -sp->k;
-
- /* If the frame pointer originally also holds the same value
- as the stack pointer, we're probably using it. If it holds
- some other value -- even a constant offset -- it is most
- likely used as temp register. */
- if (pv_is_identical (*sp, *fp))
- frame_pointer = S390_FRAME_REGNUM;
- else
- frame_pointer = S390_SP_REGNUM;
-
- /* If we've detected a function with stack frame, we'll still have to
- treat it as frameless if we're currently within the function epilog
- code at a point where the frame pointer has already been restored.
- This can only happen in an innermost frame. */
- /* FIXME: cagney/2004-05-01: This sanity check shouldn't be needed,
- instead the code should simpliy rely on its analysis. */
- next_frame = get_next_frame (this_frame);
- while (next_frame && get_frame_type (next_frame) == INLINE_FRAME)
- next_frame = get_next_frame (next_frame);
- if (size > 0
- && (next_frame == NULL
- || get_frame_type (get_next_frame (this_frame)) != NORMAL_FRAME))
- {
- /* See the comment in s390_in_function_epilogue_p on why this is
- not completely reliable ... */
- if (s390_in_function_epilogue_p (gdbarch, get_frame_pc (this_frame)))
- {
- memset (&data, 0, sizeof (data));
- size = 0;
- frame_pointer = S390_SP_REGNUM;
- }
- }
-
- /* Once we know the frame register and the frame size, we can unwind
- the current value of the frame register from the next frame, and
- add back the frame size to arrive that the previous frame's
- stack pointer value. */
- prev_sp = get_frame_register_unsigned (this_frame, frame_pointer) + size;
- cfa = prev_sp + 16*word_size + 32;
-
- /* Set up ABI call-saved/call-clobbered registers. */
- for (i = 0; i < S390_NUM_REGS; i++)
- if (!s390_register_call_saved (gdbarch, i))
- trad_frame_set_unknown (info->saved_regs, i);
-
- /* CC is always call-clobbered. */
- trad_frame_set_unknown (info->saved_regs, S390_PSWM_REGNUM);
-
- /* Record the addresses of all register spill slots the prologue parser
- has recognized. Consider only registers defined as call-saved by the
- ABI; for call-clobbered registers the parser may have recognized
- spurious stores. */
-
- for (i = 0; i < 16; i++)
- if (s390_register_call_saved (gdbarch, S390_R0_REGNUM + i)
- && data.gpr_slot[i] != 0)
- info->saved_regs[S390_R0_REGNUM + i].addr = cfa - data.gpr_slot[i];
-
- for (i = 0; i < 16; i++)
- if (s390_register_call_saved (gdbarch, S390_F0_REGNUM + i)
- && data.fpr_slot[i] != 0)
- info->saved_regs[S390_F0_REGNUM + i].addr = cfa - data.fpr_slot[i];
-
- /* Function return will set PC to %r14. */
- info->saved_regs[S390_PSWA_REGNUM] = info->saved_regs[S390_RETADDR_REGNUM];
-
- /* In frameless functions, we unwind simply by moving the return
- address to the PC. However, if we actually stored to the
- save area, use that -- we might only think the function frameless
- because we're in the middle of the prologue ... */
- if (size == 0
- && !trad_frame_addr_p (info->saved_regs, S390_PSWA_REGNUM))
- {
- info->saved_regs[S390_PSWA_REGNUM].realreg = S390_RETADDR_REGNUM;
- }
-
- /* Another sanity check: unless this is a frameless function,
- we should have found spill slots for SP and PC.
- If not, we cannot unwind further -- this happens e.g. in
- libc's thread_start routine. */
- if (size > 0)
- {
- if (!trad_frame_addr_p (info->saved_regs, S390_SP_REGNUM)
- || !trad_frame_addr_p (info->saved_regs, S390_PSWA_REGNUM))
- prev_sp = -1;
- }
-
- /* We use the current value of the frame register as local_base,
- and the top of the register save area as frame_base. */
- if (prev_sp != -1)
- {
- info->frame_base = prev_sp + 16*word_size + 32;
- info->local_base = prev_sp - size;
- }
-
- info->func = func;
- return 1;
-}
-
-static void
-s390_backchain_frame_unwind_cache (struct frame_info *this_frame,
- struct s390_unwind_cache *info)
-{
- struct gdbarch *gdbarch = get_frame_arch (this_frame);
- int word_size = gdbarch_ptr_bit (gdbarch) / 8;
- enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
- CORE_ADDR backchain;
- ULONGEST reg;
- LONGEST sp;
- int i;
-
- /* Set up ABI call-saved/call-clobbered registers. */
- for (i = 0; i < S390_NUM_REGS; i++)
- if (!s390_register_call_saved (gdbarch, i))
- trad_frame_set_unknown (info->saved_regs, i);
-
- /* CC is always call-clobbered. */
- trad_frame_set_unknown (info->saved_regs, S390_PSWM_REGNUM);
-
- /* Get the backchain. */
- reg = get_frame_register_unsigned (this_frame, S390_SP_REGNUM);
- backchain = read_memory_unsigned_integer (reg, word_size, byte_order);
-
- /* A zero backchain terminates the frame chain. As additional
- sanity check, let's verify that the spill slot for SP in the
- save area pointed to by the backchain in fact links back to
- the save area. */
- if (backchain != 0
- && safe_read_memory_integer (backchain + 15*word_size,
- word_size, byte_order, &sp)
- && (CORE_ADDR)sp == backchain)
- {
- /* We don't know which registers were saved, but it will have
- to be at least %r14 and %r15. This will allow us to continue
- unwinding, but other prev-frame registers may be incorrect ... */
- info->saved_regs[S390_SP_REGNUM].addr = backchain + 15*word_size;
- info->saved_regs[S390_RETADDR_REGNUM].addr = backchain + 14*word_size;
-
- /* Function return will set PC to %r14. */
- info->saved_regs[S390_PSWA_REGNUM]
- = info->saved_regs[S390_RETADDR_REGNUM];
-
- /* We use the current value of the frame register as local_base,
- and the top of the register save area as frame_base. */
- info->frame_base = backchain + 16*word_size + 32;
- info->local_base = reg;
- }
-
- info->func = get_frame_pc (this_frame);
-}
-
-static struct s390_unwind_cache *
-s390_frame_unwind_cache (struct frame_info *this_frame,
- void **this_prologue_cache)
-{
- volatile struct gdb_exception ex;
- struct s390_unwind_cache *info;
-
- if (*this_prologue_cache)
- return *this_prologue_cache;
-
- info = FRAME_OBSTACK_ZALLOC (struct s390_unwind_cache);
- *this_prologue_cache = info;
- info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
- info->func = -1;
- info->frame_base = -1;
- info->local_base = -1;
-
- TRY_CATCH (ex, RETURN_MASK_ERROR)
- {
- /* Try to use prologue analysis to fill the unwind cache.
- If this fails, fall back to reading the stack backchain. */
- if (!s390_prologue_frame_unwind_cache (this_frame, info))
- s390_backchain_frame_unwind_cache (this_frame, info);
- }
- if (ex.reason < 0 && ex.error != NOT_AVAILABLE_ERROR)
- throw_exception (ex);
-
- return info;
-}
-
-static void
-s390_frame_this_id (struct frame_info *this_frame,
- void **this_prologue_cache,
- struct frame_id *this_id)
-{
- struct s390_unwind_cache *info
- = s390_frame_unwind_cache (this_frame, this_prologue_cache);
-
- if (info->frame_base == -1)
- return;
-
- *this_id = frame_id_build (info->frame_base, info->func);
-}
-
-static struct value *
-s390_frame_prev_register (struct frame_info *this_frame,
- void **this_prologue_cache, int regnum)
-{
- struct gdbarch *gdbarch = get_frame_arch (this_frame);
- struct s390_unwind_cache *info
- = s390_frame_unwind_cache (this_frame, this_prologue_cache);
-
- return s390_trad_frame_prev_register (this_frame, info->saved_regs, regnum);
-}
-
-static const struct frame_unwind s390_frame_unwind = {
- NORMAL_FRAME,
- default_frame_unwind_stop_reason,
- s390_frame_this_id,
- s390_frame_prev_register,
- NULL,
- default_frame_sniffer
-};
-
-
-/* Code stubs and their stack frames. For things like PLTs and NULL
- function calls (where there is no true frame and the return address
- is in the RETADDR register). */
-
-struct s390_stub_unwind_cache
-{
- CORE_ADDR frame_base;
- struct trad_frame_saved_reg *saved_regs;
-};
-
-static struct s390_stub_unwind_cache *
-s390_stub_frame_unwind_cache (struct frame_info *this_frame,
- void **this_prologue_cache)
-{
- struct gdbarch *gdbarch = get_frame_arch (this_frame);
- int word_size = gdbarch_ptr_bit (gdbarch) / 8;
- struct s390_stub_unwind_cache *info;
- ULONGEST reg;
-
- if (*this_prologue_cache)
- return *this_prologue_cache;
-
- info = FRAME_OBSTACK_ZALLOC (struct s390_stub_unwind_cache);
- *this_prologue_cache = info;
- info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
-
- /* The return address is in register %r14. */
- info->saved_regs[S390_PSWA_REGNUM].realreg = S390_RETADDR_REGNUM;
-
- /* Retrieve stack pointer and determine our frame base. */
- reg = get_frame_register_unsigned (this_frame, S390_SP_REGNUM);
- info->frame_base = reg + 16*word_size + 32;
-
- return info;
-}
-
-static void
-s390_stub_frame_this_id (struct frame_info *this_frame,
- void **this_prologue_cache,
- struct frame_id *this_id)
-{
- struct s390_stub_unwind_cache *info
- = s390_stub_frame_unwind_cache (this_frame, this_prologue_cache);
- *this_id = frame_id_build (info->frame_base, get_frame_pc (this_frame));
-}
-
-static struct value *
-s390_stub_frame_prev_register (struct frame_info *this_frame,
- void **this_prologue_cache, int regnum)
-{
- struct s390_stub_unwind_cache *info
- = s390_stub_frame_unwind_cache (this_frame, this_prologue_cache);
- return s390_trad_frame_prev_register (this_frame, info->saved_regs, regnum);
-}