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/kprobes.h>
13 #include <linux/ptrace.h>
14 #include <asm/sstep.h>
15 #include <asm/processor.h>
17 extern char system_call_common
[];
20 /* Bits in SRR1 that are copied from MSR */
21 #define MSR_MASK 0xffffffff87c0ffffUL
23 #define MSR_MASK 0x87c0ffff
27 * Determine whether a conditional branch instruction would branch.
29 static int __kprobes
branch_taken(unsigned int instr
, struct pt_regs
*regs
)
31 unsigned int bo
= (instr
>> 21) & 0x1f;
35 /* decrement counter */
37 if (((bo
>> 1) & 1) ^ (regs
->ctr
== 0))
40 if ((bo
& 0x10) == 0) {
41 /* check bit from CR */
42 bi
= (instr
>> 16) & 0x1f;
43 if (((regs
->ccr
>> (31 - bi
)) & 1) != ((bo
>> 3) & 1))
50 * Emulate instructions that cause a transfer of control.
51 * Returns 1 if the step was emulated, 0 if not,
52 * or -1 if the instruction is one that should not be stepped,
53 * such as an rfid, or a mtmsrd that would clear MSR_RI.
55 int __kprobes
emulate_step(struct pt_regs
*regs
, unsigned int instr
)
57 unsigned int opcode
, rs
, rb
, rd
, spr
;
58 unsigned long int imm
;
63 imm
= (signed short)(instr
& 0xfffc);
67 if ((regs
->msr
& MSR_SF
) == 0)
68 regs
->nip
&= 0xffffffffUL
;
70 regs
->link
= regs
->nip
;
71 if (branch_taken(instr
, regs
))
77 * N.B. this uses knowledge about how the syscall
78 * entry code works. If that is changed, this will
79 * need to be changed also.
81 regs
->gpr
[9] = regs
->gpr
[13];
82 regs
->gpr
[11] = regs
->nip
+ 4;
83 regs
->gpr
[12] = regs
->msr
& MSR_MASK
;
84 regs
->gpr
[13] = (unsigned long) get_paca();
85 regs
->nip
= (unsigned long) &system_call_common
;
86 regs
->msr
= MSR_KERNEL
;
90 imm
= instr
& 0x03fffffc;
96 regs
->link
= regs
->nip
+ 4;
97 if ((regs
->msr
& MSR_SF
) == 0)
98 regs
->link
&= 0xffffffffUL
;
100 if ((regs
->msr
& MSR_SF
) == 0)
105 switch (instr
& 0x7fe) {
106 case 0x20: /* bclr */
107 case 0x420: /* bcctr */
108 imm
= (instr
& 0x400)? regs
->ctr
: regs
->link
;
110 if ((regs
->msr
& MSR_SF
) == 0) {
111 regs
->nip
&= 0xffffffffUL
;
115 regs
->link
= regs
->nip
;
116 if (branch_taken(instr
, regs
))
119 case 0x24: /* rfid, scary */
123 rd
= (instr
>> 21) & 0x1f;
124 switch (instr
& 0x7fe) {
125 case 0xa6: /* mfmsr */
126 regs
->gpr
[rd
] = regs
->msr
& MSR_MASK
;
128 if ((regs
->msr
& MSR_SF
) == 0)
129 regs
->nip
&= 0xffffffffUL
;
131 case 0x124: /* mtmsr */
133 if ((imm
& MSR_RI
) == 0)
134 /* can't step mtmsr that would clear MSR_RI */
140 case 0x164: /* mtmsrd */
141 /* only MSR_EE and MSR_RI get changed if bit 15 set */
142 /* mtmsrd doesn't change MSR_HV and MSR_ME */
143 imm
= (instr
& 0x10000)? 0x8002: 0xefffffffffffefffUL
;
144 imm
= (regs
->msr
& MSR_MASK
& ~imm
)
145 | (regs
->gpr
[rd
] & imm
);
146 if ((imm
& MSR_RI
) == 0)
147 /* can't step mtmsrd that would clear MSR_RI */
151 if ((imm
& MSR_SF
) == 0)
152 regs
->nip
&= 0xffffffffUL
;
155 case 0x26: /* mfcr */
156 regs
->gpr
[rd
] = regs
->ccr
;
157 regs
->gpr
[rd
] &= 0xffffffffUL
;
159 case 0x2a6: /* mfspr */
160 spr
= (instr
>> 11) & 0x3ff;
162 case 0x20: /* mfxer */
163 regs
->gpr
[rd
] = regs
->xer
;
164 regs
->gpr
[rd
] &= 0xffffffffUL
;
166 case 0x100: /* mflr */
167 regs
->gpr
[rd
] = regs
->link
;
169 case 0x120: /* mfctr */
170 regs
->gpr
[rd
] = regs
->ctr
;
174 case 0x378: /* orx */
177 rs
= (instr
>> 21) & 0x1f;
178 rb
= (instr
>> 11) & 0x1f;
179 if (rs
== rb
) { /* mr */
180 rd
= (instr
>> 16) & 0x1f;
181 regs
->gpr
[rd
] = regs
->gpr
[rs
];
185 case 0x3a6: /* mtspr */
186 spr
= (instr
>> 11) & 0x3ff;
188 case 0x20: /* mtxer */
189 regs
->xer
= (regs
->gpr
[rd
] & 0xffffffffUL
);
191 case 0x100: /* mtlr */
192 regs
->link
= regs
->gpr
[rd
];
194 case 0x120: /* mtctr */
195 regs
->ctr
= regs
->gpr
[rd
];