-static int branch_taken(unsigned int instr, struct pt_regs *regs)
-{
- unsigned int bo = (instr >> 21) & 0x1f;
- unsigned int bi;
-
- if ((bo & 4) == 0) {
- /* decrement counter */
- --regs->ctr;
- if (((bo >> 1) & 1) ^ (regs->ctr == 0))
- return 0;
- }
- if ((bo & 0x10) == 0) {
- /* check bit from CR */
- bi = (instr >> 16) & 0x1f;
- if (((regs->ccr >> (31 - bi)) & 1) != ((bo >> 3) & 1))
- return 0;
- }
- return 1;
-}
-
-/*
- * Emulate instructions that cause a transfer of control.
- * Returns 1 if the step was emulated, 0 if not,
- * or -1 if the instruction is one that should not be stepped,
- * such as an rfid, or a mtmsrd that would clear MSR_RI.
- */
-static int emulate_step(struct pt_regs *regs, unsigned int instr)
-{
- unsigned int opcode, rd;
- unsigned long int imm;
-
- opcode = instr >> 26;
- switch (opcode) {
- case 16: /* bc */
- imm = (signed short)(instr & 0xfffc);
- if ((instr & 2) == 0)
- imm += regs->nip;
- regs->nip += 4; /* XXX check 32-bit mode */
- if (instr & 1)
- regs->link = regs->nip;
- if (branch_taken(instr, regs))
- regs->nip = imm;
- return 1;
- case 17: /* sc */
- regs->gpr[9] = regs->gpr[13];
- regs->gpr[11] = regs->nip + 4;
- regs->gpr[12] = regs->msr & MSR_MASK;
- regs->gpr[13] = (unsigned long) get_paca();
- regs->nip = (unsigned long) &SystemCall_common;
- regs->msr = MSR_KERNEL;
- return 1;
- case 18: /* b */
- imm = instr & 0x03fffffc;
- if (imm & 0x02000000)
- imm -= 0x04000000;
- if ((instr & 2) == 0)
- imm += regs->nip;
- if (instr & 1)
- regs->link = regs->nip + 4;
- regs->nip = imm;
- return 1;
- case 19:
- switch (instr & 0x7fe) {
- case 0x20: /* bclr */
- case 0x420: /* bcctr */
- imm = (instr & 0x400)? regs->ctr: regs->link;
- regs->nip += 4; /* XXX check 32-bit mode */
- if (instr & 1)
- regs->link = regs->nip;
- if (branch_taken(instr, regs))
- regs->nip = imm;
- return 1;
- case 0x24: /* rfid, scary */
- printf("Can't single-step an rfid instruction\n");
- return -1;
- }
- case 31:
- rd = (instr >> 21) & 0x1f;
- switch (instr & 0x7fe) {
- case 0xa6: /* mfmsr */
- regs->gpr[rd] = regs->msr & MSR_MASK;
- regs->nip += 4;
- return 1;
- case 0x164: /* mtmsrd */
- /* only MSR_EE and MSR_RI get changed if bit 15 set */
- /* mtmsrd doesn't change MSR_HV and MSR_ME */
- imm = (instr & 0x10000)? 0x8002: 0xefffffffffffefffUL;
- imm = (regs->msr & MSR_MASK & ~imm)
- | (regs->gpr[rd] & imm);
- if ((imm & MSR_RI) == 0) {
- printf("Can't step an instruction that would "
- "clear MSR.RI\n");
- return -1;
- }
- regs->msr = imm;
- regs->nip += 4;
- return 1;
- }
- }
- return 0;
-}
-