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
[];
19 /* Bits in SRR1 that are copied from MSR */
20 #define MSR_MASK 0xffffffff87c0ffffUL
22 #define MSR_MASK 0x87c0ffff
26 * Determine whether a conditional branch instruction would branch.
28 static int branch_taken(unsigned int instr
, struct pt_regs
*regs
)
30 unsigned int bo
= (instr
>> 21) & 0x1f;
34 /* decrement counter */
36 if (((bo
>> 1) & 1) ^ (regs
->ctr
== 0))
39 if ((bo
& 0x10) == 0) {
40 /* check bit from CR */
41 bi
= (instr
>> 16) & 0x1f;
42 if (((regs
->ccr
>> (31 - bi
)) & 1) != ((bo
>> 3) & 1))
49 * Emulate instructions that cause a transfer of control.
50 * Returns 1 if the step was emulated, 0 if not,
51 * or -1 if the instruction is one that should not be stepped,
52 * such as an rfid, or a mtmsrd that would clear MSR_RI.
54 int emulate_step(struct pt_regs
*regs
, unsigned int instr
)
56 unsigned int opcode
, rd
;
57 unsigned long int imm
;
62 imm
= (signed short)(instr
& 0xfffc);
66 if ((regs
->msr
& MSR_SF
) == 0)
67 regs
->nip
&= 0xffffffffUL
;
69 regs
->link
= regs
->nip
;
70 if (branch_taken(instr
, regs
))
76 * N.B. this uses knowledge about how the syscall
77 * entry code works. If that is changed, this will
78 * need to be changed also.
80 regs
->gpr
[9] = regs
->gpr
[13];
81 regs
->gpr
[11] = regs
->nip
+ 4;
82 regs
->gpr
[12] = regs
->msr
& MSR_MASK
;
83 regs
->gpr
[13] = (unsigned long) get_paca();
84 regs
->nip
= (unsigned long) &system_call_common
;
85 regs
->msr
= MSR_KERNEL
;
89 imm
= instr
& 0x03fffffc;
95 regs
->link
= regs
->nip
+ 4;
96 if ((regs
->msr
& MSR_SF
) == 0)
97 regs
->link
&= 0xffffffffUL
;
99 if ((regs
->msr
& MSR_SF
) == 0)
104 switch (instr
& 0x7fe) {
105 case 0x20: /* bclr */
106 case 0x420: /* bcctr */
107 imm
= (instr
& 0x400)? regs
->ctr
: regs
->link
;
109 if ((regs
->msr
& MSR_SF
) == 0) {
110 regs
->nip
&= 0xffffffffUL
;
114 regs
->link
= regs
->nip
;
115 if (branch_taken(instr
, regs
))
118 case 0x24: /* rfid, scary */
122 rd
= (instr
>> 21) & 0x1f;
123 switch (instr
& 0x7fe) {
124 case 0xa6: /* mfmsr */
125 regs
->gpr
[rd
] = regs
->msr
& MSR_MASK
;
127 if ((regs
->msr
& MSR_SF
) == 0)
128 regs
->nip
&= 0xffffffffUL
;
130 case 0x124: /* mtmsr */
132 if ((imm
& MSR_RI
) == 0)
133 /* can't step mtmsr that would clear MSR_RI */
139 case 0x164: /* mtmsrd */
140 /* only MSR_EE and MSR_RI get changed if bit 15 set */
141 /* mtmsrd doesn't change MSR_HV and MSR_ME */
142 imm
= (instr
& 0x10000)? 0x8002: 0xefffffffffffefffUL
;
143 imm
= (regs
->msr
& MSR_MASK
& ~imm
)
144 | (regs
->gpr
[rd
] & imm
);
145 if ((imm
& MSR_RI
) == 0)
146 /* can't step mtmsrd that would clear MSR_RI */
150 if ((imm
& MSR_SF
) == 0)
151 regs
->nip
&= 0xffffffffUL
;