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>
16 extern char system_call_common
[];
18 /* Bits in SRR1 that are copied from MSR */
19 #define MSR_MASK 0xffffffff87c0ffff
22 * Determine whether a conditional branch instruction would branch.
24 static int branch_taken(unsigned int instr
, struct pt_regs
*regs
)
26 unsigned int bo
= (instr
>> 21) & 0x1f;
30 /* decrement counter */
32 if (((bo
>> 1) & 1) ^ (regs
->ctr
== 0))
35 if ((bo
& 0x10) == 0) {
36 /* check bit from CR */
37 bi
= (instr
>> 16) & 0x1f;
38 if (((regs
->ccr
>> (31 - bi
)) & 1) != ((bo
>> 3) & 1))
45 * Emulate instructions that cause a transfer of control.
46 * Returns 1 if the step was emulated, 0 if not,
47 * or -1 if the instruction is one that should not be stepped,
48 * such as an rfid, or a mtmsrd that would clear MSR_RI.
50 int emulate_step(struct pt_regs
*regs
, unsigned int instr
)
52 unsigned int opcode
, rd
;
53 unsigned long int imm
;
58 imm
= (signed short)(instr
& 0xfffc);
62 if ((regs
->msr
& MSR_SF
) == 0)
63 regs
->nip
&= 0xffffffffUL
;
65 regs
->link
= regs
->nip
;
66 if (branch_taken(instr
, regs
))
71 * N.B. this uses knowledge about how the syscall
72 * entry code works. If that is changed, this will
73 * need to be changed also.
75 regs
->gpr
[9] = regs
->gpr
[13];
76 regs
->gpr
[11] = regs
->nip
+ 4;
77 regs
->gpr
[12] = regs
->msr
& MSR_MASK
;
78 regs
->gpr
[13] = (unsigned long) get_paca();
79 regs
->nip
= (unsigned long) &system_call_common
;
80 regs
->msr
= MSR_KERNEL
;
83 imm
= instr
& 0x03fffffc;
89 regs
->link
= regs
->nip
+ 4;
90 if ((regs
->msr
& MSR_SF
) == 0)
91 regs
->link
&= 0xffffffffUL
;
93 if ((regs
->msr
& MSR_SF
) == 0)
98 switch (instr
& 0x7fe) {
100 case 0x420: /* bcctr */
101 imm
= (instr
& 0x400)? regs
->ctr
: regs
->link
;
103 if ((regs
->msr
& MSR_SF
) == 0) {
104 regs
->nip
&= 0xffffffffUL
;
108 regs
->link
= regs
->nip
;
109 if (branch_taken(instr
, regs
))
112 case 0x24: /* rfid, scary */
116 rd
= (instr
>> 21) & 0x1f;
117 switch (instr
& 0x7fe) {
118 case 0xa6: /* mfmsr */
119 regs
->gpr
[rd
] = regs
->msr
& MSR_MASK
;
121 if ((regs
->msr
& MSR_SF
) == 0)
122 regs
->nip
&= 0xffffffffUL
;
124 case 0x164: /* mtmsrd */
125 /* only MSR_EE and MSR_RI get changed if bit 15 set */
126 /* mtmsrd doesn't change MSR_HV and MSR_ME */
127 imm
= (instr
& 0x10000)? 0x8002: 0xefffffffffffefffUL
;
128 imm
= (regs
->msr
& MSR_MASK
& ~imm
)
129 | (regs
->gpr
[rd
] & imm
);
130 if ((imm
& MSR_RI
) == 0)
131 /* can't step mtmsrd that would clear MSR_RI */
135 if ((imm
& MSR_SF
) == 0)
136 regs
->nip
&= 0xffffffffUL
;