4 * Copyright (C) 2004 Paul Mackerras <paulus@au.ibm.com>, IBM
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 #include <linux/kernel.h>
12 #include <linux/ptrace.h>
13 #include <asm/sstep.h>
14 #include <asm/processor.h>
19 extern char system_call_common[];
22 /* Bits in SRR1 that are copied from MSR */
23 #define MSR_MASK 0xffffffff87c0ffffUL
25 #define MSR_MASK 0x87c0ffff
29 * Determine whether a conditional branch instruction would branch.
31 static int branch_taken(unsigned int instr, struct pt_regs *regs)
33 unsigned int bo = (instr >> 21) & 0x1f;
37 /* decrement counter */
39 if (((bo >> 1) & 1) ^ (regs->ctr == 0))
42 if ((bo & 0x10) == 0) {
43 /* check bit from CR */
44 bi = (instr >> 16) & 0x1f;
45 if (((regs->ccr >> (31 - bi)) & 1) != ((bo >> 3) & 1))
52 * Emulate instructions that cause a transfer of control.
53 * Returns 1 if the step was emulated, 0 if not,
54 * or -1 if the instruction is one that should not be stepped,
55 * such as an rfid, or a mtmsrd that would clear MSR_RI.
57 int emulate_step(struct pt_regs *regs, unsigned int instr)
59 unsigned int opcode, rd;
60 unsigned long int imm;
65 imm = (signed short)(instr & 0xfffc);
69 if ((regs->msr & MSR_SF) == 0)
70 regs->nip &= 0xffffffffUL;
72 regs->link = regs->nip;
73 if (branch_taken(instr, regs))
79 * N.B. this uses knowledge about how the syscall
80 * entry code works. If that is changed, this will
81 * need to be changed also.
83 regs->gpr[9] = regs->gpr[13];
84 regs->gpr[11] = regs->nip + 4;
85 regs->gpr[12] = regs->msr & MSR_MASK;
86 regs->gpr[13] = (unsigned long) get_paca();
87 regs->nip = (unsigned long) &system_call_common;
88 regs->msr = MSR_KERNEL;
92 imm = instr & 0x03fffffc;
98 regs->link = regs->nip + 4;
99 if ((regs->msr & MSR_SF) == 0)
100 regs->link &= 0xffffffffUL;
102 if ((regs->msr & MSR_SF) == 0)
107 switch (instr & 0x7fe) {
108 case 0x20: /* bclr */
109 case 0x420: /* bcctr */
110 imm = (instr & 0x400)? regs->ctr: regs->link;
112 if ((regs->msr & MSR_SF) == 0) {
113 regs->nip &= 0xffffffffUL;
117 regs->link = regs->nip;
118 if (branch_taken(instr, regs))
121 case 0x24: /* rfid, scary */
125 rd = (instr >> 21) & 0x1f;
126 switch (instr & 0x7fe) {
127 case 0xa6: /* mfmsr */
128 regs->gpr[rd] = regs->msr & MSR_MASK;
130 if ((regs->msr & MSR_SF) == 0)
131 regs->nip &= 0xffffffffUL;
133 case 0x124: /* mtmsr */
135 if ((imm & MSR_RI) == 0)
136 /* can't step mtmsr that would clear MSR_RI */
142 case 0x164: /* mtmsrd */
143 /* only MSR_EE and MSR_RI get changed if bit 15 set */
144 /* mtmsrd doesn't change MSR_HV and MSR_ME */
145 imm = (instr & 0x10000)? 0x8002: 0xefffffffffffefffUL;
146 imm = (regs->msr & MSR_MASK & ~imm)
147 | (regs->gpr[rd] & imm);
148 if ((imm & MSR_RI) == 0)
149 /* can't step mtmsrd that would clear MSR_RI */
153 if ((imm & MSR_SF) == 0)
154 regs->nip &= 0xffffffffUL;