1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2004 Paul Mackerras <paulus@au.ibm.com>, IBM
7 #include <linux/kernel.h>
8 #include <linux/kprobes.h>
9 #include <linux/ptrace.h>
10 #include <linux/prefetch.h>
11 #include <asm/sstep.h>
12 #include <asm/processor.h>
13 #include <linux/uaccess.h>
14 #include <asm/cpu_has_feature.h>
15 #include <asm/cputable.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 #define XER_SO 0x80000000U
28 #define XER_OV 0x40000000U
29 #define XER_CA 0x20000000U
30 #define XER_OV32 0x00080000U
31 #define XER_CA32 0x00040000U
35 * Functions in ldstfp.S
37 extern void get_fpr(int rn
, double *p
);
38 extern void put_fpr(int rn
, const double *p
);
39 extern void get_vr(int rn
, __vector128
*p
);
40 extern void put_vr(int rn
, __vector128
*p
);
41 extern void load_vsrn(int vsr
, const void *p
);
42 extern void store_vsrn(int vsr
, void *p
);
43 extern void conv_sp_to_dp(const float *sp
, double *dp
);
44 extern void conv_dp_to_sp(const double *dp
, float *sp
);
51 extern int do_lq(unsigned long ea
, unsigned long *regs
);
52 extern int do_stq(unsigned long ea
, unsigned long val0
, unsigned long val1
);
53 extern int do_lqarx(unsigned long ea
, unsigned long *regs
);
54 extern int do_stqcx(unsigned long ea
, unsigned long val0
, unsigned long val1
,
58 #ifdef __LITTLE_ENDIAN__
67 * Emulate the truncation of 64 bit values in 32-bit mode.
69 static nokprobe_inline
unsigned long truncate_if_32bit(unsigned long msr
,
73 if ((msr
& MSR_64BIT
) == 0)
80 * Determine whether a conditional branch instruction would branch.
82 static nokprobe_inline
int branch_taken(unsigned int instr
,
83 const struct pt_regs
*regs
,
84 struct instruction_op
*op
)
86 unsigned int bo
= (instr
>> 21) & 0x1f;
90 /* decrement counter */
92 if (((bo
>> 1) & 1) ^ (regs
->ctr
== 1))
95 if ((bo
& 0x10) == 0) {
96 /* check bit from CR */
97 bi
= (instr
>> 16) & 0x1f;
98 if (((regs
->ccr
>> (31 - bi
)) & 1) != ((bo
>> 3) & 1))
104 static nokprobe_inline
long address_ok(struct pt_regs
*regs
,
105 unsigned long ea
, int nb
)
107 if (!user_mode(regs
))
109 if (__access_ok(ea
, nb
, USER_DS
))
111 if (__access_ok(ea
, 1, USER_DS
))
112 /* Access overlaps the end of the user region */
113 regs
->dar
= USER_DS
.seg
;
120 * Calculate effective address for a D-form instruction
122 static nokprobe_inline
unsigned long dform_ea(unsigned int instr
,
123 const struct pt_regs
*regs
)
128 ra
= (instr
>> 16) & 0x1f;
129 ea
= (signed short) instr
; /* sign-extend */
138 * Calculate effective address for a DS-form instruction
140 static nokprobe_inline
unsigned long dsform_ea(unsigned int instr
,
141 const struct pt_regs
*regs
)
146 ra
= (instr
>> 16) & 0x1f;
147 ea
= (signed short) (instr
& ~3); /* sign-extend */
155 * Calculate effective address for a DQ-form instruction
157 static nokprobe_inline
unsigned long dqform_ea(unsigned int instr
,
158 const struct pt_regs
*regs
)
163 ra
= (instr
>> 16) & 0x1f;
164 ea
= (signed short) (instr
& ~0xf); /* sign-extend */
170 #endif /* __powerpc64 */
173 * Calculate effective address for an X-form instruction
175 static nokprobe_inline
unsigned long xform_ea(unsigned int instr
,
176 const struct pt_regs
*regs
)
181 ra
= (instr
>> 16) & 0x1f;
182 rb
= (instr
>> 11) & 0x1f;
191 * Return the largest power of 2, not greater than sizeof(unsigned long),
192 * such that x is a multiple of it.
194 static nokprobe_inline
unsigned long max_align(unsigned long x
)
196 x
|= sizeof(unsigned long);
197 return x
& -x
; /* isolates rightmost bit */
200 static nokprobe_inline
unsigned long byterev_2(unsigned long x
)
202 return ((x
>> 8) & 0xff) | ((x
& 0xff) << 8);
205 static nokprobe_inline
unsigned long byterev_4(unsigned long x
)
207 return ((x
>> 24) & 0xff) | ((x
>> 8) & 0xff00) |
208 ((x
& 0xff00) << 8) | ((x
& 0xff) << 24);
212 static nokprobe_inline
unsigned long byterev_8(unsigned long x
)
214 return (byterev_4(x
) << 32) | byterev_4(x
>> 32);
218 static nokprobe_inline
void do_byte_reverse(void *ptr
, int nb
)
222 *(u16
*)ptr
= byterev_2(*(u16
*)ptr
);
225 *(u32
*)ptr
= byterev_4(*(u32
*)ptr
);
229 *(unsigned long *)ptr
= byterev_8(*(unsigned long *)ptr
);
232 unsigned long *up
= (unsigned long *)ptr
;
234 tmp
= byterev_8(up
[0]);
235 up
[0] = byterev_8(up
[1]);
245 static nokprobe_inline
int read_mem_aligned(unsigned long *dest
,
246 unsigned long ea
, int nb
,
247 struct pt_regs
*regs
)
254 err
= __get_user(x
, (unsigned char __user
*) ea
);
257 err
= __get_user(x
, (unsigned short __user
*) ea
);
260 err
= __get_user(x
, (unsigned int __user
*) ea
);
264 err
= __get_user(x
, (unsigned long __user
*) ea
);
276 * Copy from userspace to a buffer, using the largest possible
277 * aligned accesses, up to sizeof(long).
279 static nokprobe_inline
int copy_mem_in(u8
*dest
, unsigned long ea
, int nb
,
280 struct pt_regs
*regs
)
285 for (; nb
> 0; nb
-= c
) {
291 err
= __get_user(*dest
, (unsigned char __user
*) ea
);
294 err
= __get_user(*(u16
*)dest
,
295 (unsigned short __user
*) ea
);
298 err
= __get_user(*(u32
*)dest
,
299 (unsigned int __user
*) ea
);
303 err
= __get_user(*(unsigned long *)dest
,
304 (unsigned long __user
*) ea
);
318 static nokprobe_inline
int read_mem_unaligned(unsigned long *dest
,
319 unsigned long ea
, int nb
,
320 struct pt_regs
*regs
)
324 u8 b
[sizeof(unsigned long)];
330 i
= IS_BE
? sizeof(unsigned long) - nb
: 0;
331 err
= copy_mem_in(&u
.b
[i
], ea
, nb
, regs
);
338 * Read memory at address ea for nb bytes, return 0 for success
339 * or -EFAULT if an error occurred. N.B. nb must be 1, 2, 4 or 8.
340 * If nb < sizeof(long), the result is right-justified on BE systems.
342 static int read_mem(unsigned long *dest
, unsigned long ea
, int nb
,
343 struct pt_regs
*regs
)
345 if (!address_ok(regs
, ea
, nb
))
347 if ((ea
& (nb
- 1)) == 0)
348 return read_mem_aligned(dest
, ea
, nb
, regs
);
349 return read_mem_unaligned(dest
, ea
, nb
, regs
);
351 NOKPROBE_SYMBOL(read_mem
);
353 static nokprobe_inline
int write_mem_aligned(unsigned long val
,
354 unsigned long ea
, int nb
,
355 struct pt_regs
*regs
)
361 err
= __put_user(val
, (unsigned char __user
*) ea
);
364 err
= __put_user(val
, (unsigned short __user
*) ea
);
367 err
= __put_user(val
, (unsigned int __user
*) ea
);
371 err
= __put_user(val
, (unsigned long __user
*) ea
);
381 * Copy from a buffer to userspace, using the largest possible
382 * aligned accesses, up to sizeof(long).
384 static nokprobe_inline
int copy_mem_out(u8
*dest
, unsigned long ea
, int nb
,
385 struct pt_regs
*regs
)
390 for (; nb
> 0; nb
-= c
) {
396 err
= __put_user(*dest
, (unsigned char __user
*) ea
);
399 err
= __put_user(*(u16
*)dest
,
400 (unsigned short __user
*) ea
);
403 err
= __put_user(*(u32
*)dest
,
404 (unsigned int __user
*) ea
);
408 err
= __put_user(*(unsigned long *)dest
,
409 (unsigned long __user
*) ea
);
423 static nokprobe_inline
int write_mem_unaligned(unsigned long val
,
424 unsigned long ea
, int nb
,
425 struct pt_regs
*regs
)
429 u8 b
[sizeof(unsigned long)];
434 i
= IS_BE
? sizeof(unsigned long) - nb
: 0;
435 return copy_mem_out(&u
.b
[i
], ea
, nb
, regs
);
439 * Write memory at address ea for nb bytes, return 0 for success
440 * or -EFAULT if an error occurred. N.B. nb must be 1, 2, 4 or 8.
442 static int write_mem(unsigned long val
, unsigned long ea
, int nb
,
443 struct pt_regs
*regs
)
445 if (!address_ok(regs
, ea
, nb
))
447 if ((ea
& (nb
- 1)) == 0)
448 return write_mem_aligned(val
, ea
, nb
, regs
);
449 return write_mem_unaligned(val
, ea
, nb
, regs
);
451 NOKPROBE_SYMBOL(write_mem
);
453 #ifdef CONFIG_PPC_FPU
455 * These access either the real FP register or the image in the
456 * thread_struct, depending on regs->msr & MSR_FP.
458 static int do_fp_load(struct instruction_op
*op
, unsigned long ea
,
459 struct pt_regs
*regs
, bool cross_endian
)
468 u8 b
[2 * sizeof(double)];
471 nb
= GETSIZE(op
->type
);
472 if (!address_ok(regs
, ea
, nb
))
475 err
= copy_mem_in(u
.b
, ea
, nb
, regs
);
478 if (unlikely(cross_endian
)) {
479 do_byte_reverse(u
.b
, min(nb
, 8));
481 do_byte_reverse(&u
.b
[8], 8);
485 if (op
->type
& FPCONV
)
486 conv_sp_to_dp(&u
.f
, &u
.d
[0]);
487 else if (op
->type
& SIGNEXT
)
492 if (regs
->msr
& MSR_FP
)
493 put_fpr(rn
, &u
.d
[0]);
495 current
->thread
.TS_FPR(rn
) = u
.l
[0];
499 if (regs
->msr
& MSR_FP
)
500 put_fpr(rn
, &u
.d
[1]);
502 current
->thread
.TS_FPR(rn
) = u
.l
[1];
507 NOKPROBE_SYMBOL(do_fp_load
);
509 static int do_fp_store(struct instruction_op
*op
, unsigned long ea
,
510 struct pt_regs
*regs
, bool cross_endian
)
518 u8 b
[2 * sizeof(double)];
521 nb
= GETSIZE(op
->type
);
522 if (!address_ok(regs
, ea
, nb
))
526 if (regs
->msr
& MSR_FP
)
527 get_fpr(rn
, &u
.d
[0]);
529 u
.l
[0] = current
->thread
.TS_FPR(rn
);
531 if (op
->type
& FPCONV
)
532 conv_dp_to_sp(&u
.d
[0], &u
.f
);
538 if (regs
->msr
& MSR_FP
)
539 get_fpr(rn
, &u
.d
[1]);
541 u
.l
[1] = current
->thread
.TS_FPR(rn
);
544 if (unlikely(cross_endian
)) {
545 do_byte_reverse(u
.b
, min(nb
, 8));
547 do_byte_reverse(&u
.b
[8], 8);
549 return copy_mem_out(u
.b
, ea
, nb
, regs
);
551 NOKPROBE_SYMBOL(do_fp_store
);
554 #ifdef CONFIG_ALTIVEC
555 /* For Altivec/VMX, no need to worry about alignment */
556 static nokprobe_inline
int do_vec_load(int rn
, unsigned long ea
,
557 int size
, struct pt_regs
*regs
,
563 u8 b
[sizeof(__vector128
)];
566 if (!address_ok(regs
, ea
& ~0xfUL
, 16))
568 /* align to multiple of size */
570 err
= copy_mem_in(&u
.b
[ea
& 0xf], ea
, size
, regs
);
573 if (unlikely(cross_endian
))
574 do_byte_reverse(&u
.b
[ea
& 0xf], size
);
576 if (regs
->msr
& MSR_VEC
)
579 current
->thread
.vr_state
.vr
[rn
] = u
.v
;
584 static nokprobe_inline
int do_vec_store(int rn
, unsigned long ea
,
585 int size
, struct pt_regs
*regs
,
590 u8 b
[sizeof(__vector128
)];
593 if (!address_ok(regs
, ea
& ~0xfUL
, 16))
595 /* align to multiple of size */
599 if (regs
->msr
& MSR_VEC
)
602 u
.v
= current
->thread
.vr_state
.vr
[rn
];
604 if (unlikely(cross_endian
))
605 do_byte_reverse(&u
.b
[ea
& 0xf], size
);
606 return copy_mem_out(&u
.b
[ea
& 0xf], ea
, size
, regs
);
608 #endif /* CONFIG_ALTIVEC */
611 static nokprobe_inline
int emulate_lq(struct pt_regs
*regs
, unsigned long ea
,
612 int reg
, bool cross_endian
)
616 if (!address_ok(regs
, ea
, 16))
618 /* if aligned, should be atomic */
619 if ((ea
& 0xf) == 0) {
620 err
= do_lq(ea
, ®s
->gpr
[reg
]);
622 err
= read_mem(®s
->gpr
[reg
+ IS_LE
], ea
, 8, regs
);
624 err
= read_mem(®s
->gpr
[reg
+ IS_BE
], ea
+ 8, 8, regs
);
626 if (!err
&& unlikely(cross_endian
))
627 do_byte_reverse(®s
->gpr
[reg
], 16);
631 static nokprobe_inline
int emulate_stq(struct pt_regs
*regs
, unsigned long ea
,
632 int reg
, bool cross_endian
)
635 unsigned long vals
[2];
637 if (!address_ok(regs
, ea
, 16))
639 vals
[0] = regs
->gpr
[reg
];
640 vals
[1] = regs
->gpr
[reg
+ 1];
641 if (unlikely(cross_endian
))
642 do_byte_reverse(vals
, 16);
644 /* if aligned, should be atomic */
646 return do_stq(ea
, vals
[0], vals
[1]);
648 err
= write_mem(vals
[IS_LE
], ea
, 8, regs
);
650 err
= write_mem(vals
[IS_BE
], ea
+ 8, 8, regs
);
653 #endif /* __powerpc64 */
656 void emulate_vsx_load(struct instruction_op
*op
, union vsx_reg
*reg
,
657 const void *mem
, bool rev
)
661 const unsigned int *wp
;
662 const unsigned short *hp
;
663 const unsigned char *bp
;
665 size
= GETSIZE(op
->type
);
666 reg
->d
[0] = reg
->d
[1] = 0;
668 switch (op
->element_size
) {
670 /* whole vector; lxv[x] or lxvl[l] */
673 memcpy(reg
, mem
, size
);
674 if (IS_LE
&& (op
->vsx_flags
& VSX_LDLEFT
))
677 do_byte_reverse(reg
, 16);
680 /* scalar loads, lxvd2x, lxvdsx */
681 read_size
= (size
>= 8) ? 8 : size
;
682 i
= IS_LE
? 8 : 8 - read_size
;
683 memcpy(®
->b
[i
], mem
, read_size
);
685 do_byte_reverse(®
->b
[i
], 8);
687 if (op
->type
& SIGNEXT
) {
688 /* size == 4 is the only case here */
689 reg
->d
[IS_LE
] = (signed int) reg
->d
[IS_LE
];
690 } else if (op
->vsx_flags
& VSX_FPCONV
) {
692 conv_sp_to_dp(®
->fp
[1 + IS_LE
],
698 unsigned long v
= *(unsigned long *)(mem
+ 8);
699 reg
->d
[IS_BE
] = !rev
? v
: byterev_8(v
);
700 } else if (op
->vsx_flags
& VSX_SPLAT
)
701 reg
->d
[IS_BE
] = reg
->d
[IS_LE
];
707 for (j
= 0; j
< size
/ 4; ++j
) {
708 i
= IS_LE
? 3 - j
: j
;
709 reg
->w
[i
] = !rev
? *wp
++ : byterev_4(*wp
++);
711 if (op
->vsx_flags
& VSX_SPLAT
) {
712 u32 val
= reg
->w
[IS_LE
? 3 : 0];
714 i
= IS_LE
? 3 - j
: j
;
722 for (j
= 0; j
< size
/ 2; ++j
) {
723 i
= IS_LE
? 7 - j
: j
;
724 reg
->h
[i
] = !rev
? *hp
++ : byterev_2(*hp
++);
730 for (j
= 0; j
< size
; ++j
) {
731 i
= IS_LE
? 15 - j
: j
;
737 EXPORT_SYMBOL_GPL(emulate_vsx_load
);
738 NOKPROBE_SYMBOL(emulate_vsx_load
);
740 void emulate_vsx_store(struct instruction_op
*op
, const union vsx_reg
*reg
,
743 int size
, write_size
;
750 size
= GETSIZE(op
->type
);
752 switch (op
->element_size
) {
754 /* stxv, stxvx, stxvl, stxvll */
757 if (IS_LE
&& (op
->vsx_flags
& VSX_LDLEFT
))
760 /* reverse 16 bytes */
761 buf
.d
[0] = byterev_8(reg
->d
[1]);
762 buf
.d
[1] = byterev_8(reg
->d
[0]);
765 memcpy(mem
, reg
, size
);
768 /* scalar stores, stxvd2x */
769 write_size
= (size
>= 8) ? 8 : size
;
770 i
= IS_LE
? 8 : 8 - write_size
;
771 if (size
< 8 && op
->vsx_flags
& VSX_FPCONV
) {
772 buf
.d
[0] = buf
.d
[1] = 0;
774 conv_dp_to_sp(®
->dp
[IS_LE
], &buf
.fp
[1 + IS_LE
]);
778 memcpy(mem
, ®
->b
[i
], write_size
);
780 memcpy(mem
+ 8, ®
->d
[IS_BE
], 8);
782 do_byte_reverse(mem
, write_size
);
784 do_byte_reverse(mem
+ 8, 8);
790 for (j
= 0; j
< size
/ 4; ++j
) {
791 i
= IS_LE
? 3 - j
: j
;
792 *wp
++ = !rev
? reg
->w
[i
] : byterev_4(reg
->w
[i
]);
798 for (j
= 0; j
< size
/ 2; ++j
) {
799 i
= IS_LE
? 7 - j
: j
;
800 *hp
++ = !rev
? reg
->h
[i
] : byterev_2(reg
->h
[i
]);
806 for (j
= 0; j
< size
; ++j
) {
807 i
= IS_LE
? 15 - j
: j
;
813 EXPORT_SYMBOL_GPL(emulate_vsx_store
);
814 NOKPROBE_SYMBOL(emulate_vsx_store
);
816 static nokprobe_inline
int do_vsx_load(struct instruction_op
*op
,
817 unsigned long ea
, struct pt_regs
*regs
,
823 int size
= GETSIZE(op
->type
);
825 if (!address_ok(regs
, ea
, size
) || copy_mem_in(mem
, ea
, size
, regs
))
828 emulate_vsx_load(op
, &buf
, mem
, cross_endian
);
831 /* FP regs + extensions */
832 if (regs
->msr
& MSR_FP
) {
833 load_vsrn(reg
, &buf
);
835 current
->thread
.fp_state
.fpr
[reg
][0] = buf
.d
[0];
836 current
->thread
.fp_state
.fpr
[reg
][1] = buf
.d
[1];
839 if (regs
->msr
& MSR_VEC
)
840 load_vsrn(reg
, &buf
);
842 current
->thread
.vr_state
.vr
[reg
- 32] = buf
.v
;
848 static nokprobe_inline
int do_vsx_store(struct instruction_op
*op
,
849 unsigned long ea
, struct pt_regs
*regs
,
855 int size
= GETSIZE(op
->type
);
857 if (!address_ok(regs
, ea
, size
))
862 /* FP regs + extensions */
863 if (regs
->msr
& MSR_FP
) {
864 store_vsrn(reg
, &buf
);
866 buf
.d
[0] = current
->thread
.fp_state
.fpr
[reg
][0];
867 buf
.d
[1] = current
->thread
.fp_state
.fpr
[reg
][1];
870 if (regs
->msr
& MSR_VEC
)
871 store_vsrn(reg
, &buf
);
873 buf
.v
= current
->thread
.vr_state
.vr
[reg
- 32];
876 emulate_vsx_store(op
, &buf
, mem
, cross_endian
);
877 return copy_mem_out(mem
, ea
, size
, regs
);
879 #endif /* CONFIG_VSX */
881 int emulate_dcbz(unsigned long ea
, struct pt_regs
*regs
)
884 unsigned long i
, size
;
887 size
= ppc64_caches
.l1d
.block_size
;
888 if (!(regs
->msr
& MSR_64BIT
))
891 size
= L1_CACHE_BYTES
;
894 if (!address_ok(regs
, ea
, size
))
896 for (i
= 0; i
< size
; i
+= sizeof(long)) {
897 err
= __put_user(0, (unsigned long __user
*) (ea
+ i
));
905 NOKPROBE_SYMBOL(emulate_dcbz
);
907 #define __put_user_asmx(x, addr, err, op, cr) \
908 __asm__ __volatile__( \
909 "1: " op " %2,0,%3\n" \
912 ".section .fixup,\"ax\"\n" \
917 : "=r" (err), "=r" (cr) \
918 : "r" (x), "r" (addr), "i" (-EFAULT), "0" (err))
920 #define __get_user_asmx(x, addr, err, op) \
921 __asm__ __volatile__( \
922 "1: "op" %1,0,%2\n" \
924 ".section .fixup,\"ax\"\n" \
929 : "=r" (err), "=r" (x) \
930 : "r" (addr), "i" (-EFAULT), "0" (err))
932 #define __cacheop_user_asmx(addr, err, op) \
933 __asm__ __volatile__( \
936 ".section .fixup,\"ax\"\n" \
942 : "r" (addr), "i" (-EFAULT), "0" (err))
944 static nokprobe_inline
void set_cr0(const struct pt_regs
*regs
,
945 struct instruction_op
*op
)
950 op
->ccval
= (regs
->ccr
& 0x0fffffff) | ((regs
->xer
>> 3) & 0x10000000);
952 if (!(regs
->msr
& MSR_64BIT
))
956 op
->ccval
|= 0x80000000;
958 op
->ccval
|= 0x40000000;
960 op
->ccval
|= 0x20000000;
963 static nokprobe_inline
void set_ca32(struct instruction_op
*op
, bool val
)
965 if (cpu_has_feature(CPU_FTR_ARCH_300
)) {
967 op
->xerval
|= XER_CA32
;
969 op
->xerval
&= ~XER_CA32
;
973 static nokprobe_inline
void add_with_carry(const struct pt_regs
*regs
,
974 struct instruction_op
*op
, int rd
,
975 unsigned long val1
, unsigned long val2
,
976 unsigned long carry_in
)
978 unsigned long val
= val1
+ val2
;
982 op
->type
= COMPUTE
+ SETREG
+ SETXER
;
986 if (!(regs
->msr
& MSR_64BIT
)) {
987 val
= (unsigned int) val
;
988 val1
= (unsigned int) val1
;
991 op
->xerval
= regs
->xer
;
992 if (val
< val1
|| (carry_in
&& val
== val1
))
993 op
->xerval
|= XER_CA
;
995 op
->xerval
&= ~XER_CA
;
997 set_ca32(op
, (unsigned int)val
< (unsigned int)val1
||
998 (carry_in
&& (unsigned int)val
== (unsigned int)val1
));
1001 static nokprobe_inline
void do_cmp_signed(const struct pt_regs
*regs
,
1002 struct instruction_op
*op
,
1003 long v1
, long v2
, int crfld
)
1005 unsigned int crval
, shift
;
1007 op
->type
= COMPUTE
+ SETCC
;
1008 crval
= (regs
->xer
>> 31) & 1; /* get SO bit */
1015 shift
= (7 - crfld
) * 4;
1016 op
->ccval
= (regs
->ccr
& ~(0xf << shift
)) | (crval
<< shift
);
1019 static nokprobe_inline
void do_cmp_unsigned(const struct pt_regs
*regs
,
1020 struct instruction_op
*op
,
1022 unsigned long v2
, int crfld
)
1024 unsigned int crval
, shift
;
1026 op
->type
= COMPUTE
+ SETCC
;
1027 crval
= (regs
->xer
>> 31) & 1; /* get SO bit */
1034 shift
= (7 - crfld
) * 4;
1035 op
->ccval
= (regs
->ccr
& ~(0xf << shift
)) | (crval
<< shift
);
1038 static nokprobe_inline
void do_cmpb(const struct pt_regs
*regs
,
1039 struct instruction_op
*op
,
1040 unsigned long v1
, unsigned long v2
)
1042 unsigned long long out_val
, mask
;
1046 for (i
= 0; i
< 8; i
++) {
1047 mask
= 0xffUL
<< (i
* 8);
1048 if ((v1
& mask
) == (v2
& mask
))
1055 * The size parameter is used to adjust the equivalent popcnt instruction.
1056 * popcntb = 8, popcntw = 32, popcntd = 64
1058 static nokprobe_inline
void do_popcnt(const struct pt_regs
*regs
,
1059 struct instruction_op
*op
,
1060 unsigned long v1
, int size
)
1062 unsigned long long out
= v1
;
1064 out
-= (out
>> 1) & 0x5555555555555555ULL
;
1065 out
= (0x3333333333333333ULL
& out
) +
1066 (0x3333333333333333ULL
& (out
>> 2));
1067 out
= (out
+ (out
>> 4)) & 0x0f0f0f0f0f0f0f0fULL
;
1069 if (size
== 8) { /* popcntb */
1075 if (size
== 32) { /* popcntw */
1076 op
->val
= out
& 0x0000003f0000003fULL
;
1080 out
= (out
+ (out
>> 32)) & 0x7f;
1081 op
->val
= out
; /* popcntd */
1085 static nokprobe_inline
void do_bpermd(const struct pt_regs
*regs
,
1086 struct instruction_op
*op
,
1087 unsigned long v1
, unsigned long v2
)
1089 unsigned char perm
, idx
;
1093 for (i
= 0; i
< 8; i
++) {
1094 idx
= (v1
>> (i
* 8)) & 0xff;
1096 if (v2
& PPC_BIT(idx
))
1101 #endif /* CONFIG_PPC64 */
1103 * The size parameter adjusts the equivalent prty instruction.
1104 * prtyw = 32, prtyd = 64
1106 static nokprobe_inline
void do_prty(const struct pt_regs
*regs
,
1107 struct instruction_op
*op
,
1108 unsigned long v
, int size
)
1110 unsigned long long res
= v
^ (v
>> 8);
1113 if (size
== 32) { /* prtyw */
1114 op
->val
= res
& 0x0000000100000001ULL
;
1119 op
->val
= res
& 1; /*prtyd */
1122 static nokprobe_inline
int trap_compare(long v1
, long v2
)
1132 if ((unsigned long)v1
< (unsigned long)v2
)
1134 else if ((unsigned long)v1
> (unsigned long)v2
)
1140 * Elements of 32-bit rotate and mask instructions.
1142 #define MASK32(mb, me) ((0xffffffffUL >> (mb)) + \
1143 ((signed long)-0x80000000L >> (me)) + ((me) >= (mb)))
1144 #ifdef __powerpc64__
1145 #define MASK64_L(mb) (~0UL >> (mb))
1146 #define MASK64_R(me) ((signed long)-0x8000000000000000L >> (me))
1147 #define MASK64(mb, me) (MASK64_L(mb) + MASK64_R(me) + ((me) >= (mb)))
1148 #define DATA32(x) (((x) & 0xffffffffUL) | (((x) & 0xffffffffUL) << 32))
1150 #define DATA32(x) (x)
1152 #define ROTATE(x, n) ((n) ? (((x) << (n)) | ((x) >> (8 * sizeof(long) - (n)))) : (x))
1155 * Decode an instruction, and return information about it in *op
1156 * without changing *regs.
1157 * Integer arithmetic and logical instructions, branches, and barrier
1158 * instructions can be emulated just using the information in *op.
1160 * Return value is 1 if the instruction can be emulated just by
1161 * updating *regs with the information in *op, -1 if we need the
1162 * GPRs but *regs doesn't contain the full register set, or 0
1165 int analyse_instr(struct instruction_op
*op
, const struct pt_regs
*regs
,
1168 unsigned int opcode
, ra
, rb
, rc
, rd
, spr
, u
;
1169 unsigned long int imm
;
1170 unsigned long int val
, val2
;
1171 unsigned int mb
, me
, sh
;
1176 opcode
= instr
>> 26;
1180 imm
= (signed short)(instr
& 0xfffc);
1181 if ((instr
& 2) == 0)
1183 op
->val
= truncate_if_32bit(regs
->msr
, imm
);
1186 if (branch_taken(instr
, regs
, op
))
1187 op
->type
|= BRTAKEN
;
1191 if ((instr
& 0xfe2) == 2)
1198 op
->type
= BRANCH
| BRTAKEN
;
1199 imm
= instr
& 0x03fffffc;
1200 if (imm
& 0x02000000)
1202 if ((instr
& 2) == 0)
1204 op
->val
= truncate_if_32bit(regs
->msr
, imm
);
1209 switch ((instr
>> 1) & 0x3ff) {
1211 op
->type
= COMPUTE
+ SETCC
;
1212 rd
= 7 - ((instr
>> 23) & 0x7);
1213 ra
= 7 - ((instr
>> 18) & 0x7);
1216 val
= (regs
->ccr
>> ra
) & 0xf;
1217 op
->ccval
= (regs
->ccr
& ~(0xfUL
<< rd
)) | (val
<< rd
);
1221 case 528: /* bcctr */
1223 imm
= (instr
& 0x400)? regs
->ctr
: regs
->link
;
1224 op
->val
= truncate_if_32bit(regs
->msr
, imm
);
1227 if (branch_taken(instr
, regs
, op
))
1228 op
->type
|= BRTAKEN
;
1231 case 18: /* rfid, scary */
1232 if (regs
->msr
& MSR_PR
)
1237 case 150: /* isync */
1238 op
->type
= BARRIER
| BARRIER_ISYNC
;
1241 case 33: /* crnor */
1242 case 129: /* crandc */
1243 case 193: /* crxor */
1244 case 225: /* crnand */
1245 case 257: /* crand */
1246 case 289: /* creqv */
1247 case 417: /* crorc */
1248 case 449: /* cror */
1249 op
->type
= COMPUTE
+ SETCC
;
1250 ra
= (instr
>> 16) & 0x1f;
1251 rb
= (instr
>> 11) & 0x1f;
1252 rd
= (instr
>> 21) & 0x1f;
1253 ra
= (regs
->ccr
>> (31 - ra
)) & 1;
1254 rb
= (regs
->ccr
>> (31 - rb
)) & 1;
1255 val
= (instr
>> (6 + ra
* 2 + rb
)) & 1;
1256 op
->ccval
= (regs
->ccr
& ~(1UL << (31 - rd
))) |
1262 switch ((instr
>> 1) & 0x3ff) {
1263 case 598: /* sync */
1264 op
->type
= BARRIER
+ BARRIER_SYNC
;
1265 #ifdef __powerpc64__
1266 switch ((instr
>> 21) & 3) {
1267 case 1: /* lwsync */
1268 op
->type
= BARRIER
+ BARRIER_LWSYNC
;
1270 case 2: /* ptesync */
1271 op
->type
= BARRIER
+ BARRIER_PTESYNC
;
1277 case 854: /* eieio */
1278 op
->type
= BARRIER
+ BARRIER_EIEIO
;
1284 /* Following cases refer to regs->gpr[], so we need all regs */
1285 if (!FULL_REGS(regs
))
1288 rd
= (instr
>> 21) & 0x1f;
1289 ra
= (instr
>> 16) & 0x1f;
1290 rb
= (instr
>> 11) & 0x1f;
1291 rc
= (instr
>> 6) & 0x1f;
1294 #ifdef __powerpc64__
1296 if (rd
& trap_compare(regs
->gpr
[ra
], (short) instr
))
1301 if (rd
& trap_compare((int)regs
->gpr
[ra
], (short) instr
))
1305 #ifdef __powerpc64__
1307 if (!cpu_has_feature(CPU_FTR_ARCH_300
))
1310 switch (instr
& 0x3f) {
1311 case 48: /* maddhd */
1312 asm volatile(PPC_MADDHD(%0, %1, %2, %3) :
1313 "=r" (op
->val
) : "r" (regs
->gpr
[ra
]),
1314 "r" (regs
->gpr
[rb
]), "r" (regs
->gpr
[rc
]));
1317 case 49: /* maddhdu */
1318 asm volatile(PPC_MADDHDU(%0, %1, %2, %3) :
1319 "=r" (op
->val
) : "r" (regs
->gpr
[ra
]),
1320 "r" (regs
->gpr
[rb
]), "r" (regs
->gpr
[rc
]));
1323 case 51: /* maddld */
1324 asm volatile(PPC_MADDLD(%0, %1, %2, %3) :
1325 "=r" (op
->val
) : "r" (regs
->gpr
[ra
]),
1326 "r" (regs
->gpr
[rb
]), "r" (regs
->gpr
[rc
]));
1331 * There are other instructions from ISA 3.0 with the same
1332 * primary opcode which do not have emulation support yet.
1338 op
->val
= regs
->gpr
[ra
] * (short) instr
;
1341 case 8: /* subfic */
1342 imm
= (short) instr
;
1343 add_with_carry(regs
, op
, rd
, ~regs
->gpr
[ra
], imm
, 1);
1346 case 10: /* cmpli */
1347 imm
= (unsigned short) instr
;
1348 val
= regs
->gpr
[ra
];
1349 #ifdef __powerpc64__
1351 val
= (unsigned int) val
;
1353 do_cmp_unsigned(regs
, op
, val
, imm
, rd
>> 2);
1357 imm
= (short) instr
;
1358 val
= regs
->gpr
[ra
];
1359 #ifdef __powerpc64__
1363 do_cmp_signed(regs
, op
, val
, imm
, rd
>> 2);
1366 case 12: /* addic */
1367 imm
= (short) instr
;
1368 add_with_carry(regs
, op
, rd
, regs
->gpr
[ra
], imm
, 0);
1371 case 13: /* addic. */
1372 imm
= (short) instr
;
1373 add_with_carry(regs
, op
, rd
, regs
->gpr
[ra
], imm
, 0);
1378 imm
= (short) instr
;
1380 imm
+= regs
->gpr
[ra
];
1384 case 15: /* addis */
1385 imm
= ((short) instr
) << 16;
1387 imm
+= regs
->gpr
[ra
];
1392 if (((instr
>> 1) & 0x1f) == 2) {
1394 imm
= (short) (instr
& 0xffc1); /* d0 + d2 fields */
1395 imm
|= (instr
>> 15) & 0x3e; /* d1 field */
1396 op
->val
= regs
->nip
+ (imm
<< 16) + 4;
1402 case 20: /* rlwimi */
1403 mb
= (instr
>> 6) & 0x1f;
1404 me
= (instr
>> 1) & 0x1f;
1405 val
= DATA32(regs
->gpr
[rd
]);
1406 imm
= MASK32(mb
, me
);
1407 op
->val
= (regs
->gpr
[ra
] & ~imm
) | (ROTATE(val
, rb
) & imm
);
1410 case 21: /* rlwinm */
1411 mb
= (instr
>> 6) & 0x1f;
1412 me
= (instr
>> 1) & 0x1f;
1413 val
= DATA32(regs
->gpr
[rd
]);
1414 op
->val
= ROTATE(val
, rb
) & MASK32(mb
, me
);
1417 case 23: /* rlwnm */
1418 mb
= (instr
>> 6) & 0x1f;
1419 me
= (instr
>> 1) & 0x1f;
1420 rb
= regs
->gpr
[rb
] & 0x1f;
1421 val
= DATA32(regs
->gpr
[rd
]);
1422 op
->val
= ROTATE(val
, rb
) & MASK32(mb
, me
);
1426 op
->val
= regs
->gpr
[rd
] | (unsigned short) instr
;
1427 goto logical_done_nocc
;
1430 imm
= (unsigned short) instr
;
1431 op
->val
= regs
->gpr
[rd
] | (imm
<< 16);
1432 goto logical_done_nocc
;
1435 op
->val
= regs
->gpr
[rd
] ^ (unsigned short) instr
;
1436 goto logical_done_nocc
;
1438 case 27: /* xoris */
1439 imm
= (unsigned short) instr
;
1440 op
->val
= regs
->gpr
[rd
] ^ (imm
<< 16);
1441 goto logical_done_nocc
;
1443 case 28: /* andi. */
1444 op
->val
= regs
->gpr
[rd
] & (unsigned short) instr
;
1446 goto logical_done_nocc
;
1448 case 29: /* andis. */
1449 imm
= (unsigned short) instr
;
1450 op
->val
= regs
->gpr
[rd
] & (imm
<< 16);
1452 goto logical_done_nocc
;
1454 #ifdef __powerpc64__
1456 mb
= ((instr
>> 6) & 0x1f) | (instr
& 0x20);
1457 val
= regs
->gpr
[rd
];
1458 if ((instr
& 0x10) == 0) {
1459 sh
= rb
| ((instr
& 2) << 4);
1460 val
= ROTATE(val
, sh
);
1461 switch ((instr
>> 2) & 3) {
1462 case 0: /* rldicl */
1463 val
&= MASK64_L(mb
);
1465 case 1: /* rldicr */
1466 val
&= MASK64_R(mb
);
1469 val
&= MASK64(mb
, 63 - sh
);
1471 case 3: /* rldimi */
1472 imm
= MASK64(mb
, 63 - sh
);
1473 val
= (regs
->gpr
[ra
] & ~imm
) |
1479 sh
= regs
->gpr
[rb
] & 0x3f;
1480 val
= ROTATE(val
, sh
);
1481 switch ((instr
>> 1) & 7) {
1483 op
->val
= val
& MASK64_L(mb
);
1486 op
->val
= val
& MASK64_R(mb
);
1491 op
->type
= UNKNOWN
; /* illegal instruction */
1495 /* isel occupies 32 minor opcodes */
1496 if (((instr
>> 1) & 0x1f) == 15) {
1497 mb
= (instr
>> 6) & 0x1f; /* bc field */
1498 val
= (regs
->ccr
>> (31 - mb
)) & 1;
1499 val2
= (ra
) ? regs
->gpr
[ra
] : 0;
1501 op
->val
= (val
) ? val2
: regs
->gpr
[rb
];
1505 switch ((instr
>> 1) & 0x3ff) {
1508 (rd
& trap_compare((int)regs
->gpr
[ra
],
1509 (int)regs
->gpr
[rb
])))
1512 #ifdef __powerpc64__
1514 if (rd
& trap_compare(regs
->gpr
[ra
], regs
->gpr
[rb
]))
1518 case 83: /* mfmsr */
1519 if (regs
->msr
& MSR_PR
)
1524 case 146: /* mtmsr */
1525 if (regs
->msr
& MSR_PR
)
1529 op
->val
= 0xffffffff & ~(MSR_ME
| MSR_LE
);
1532 case 178: /* mtmsrd */
1533 if (regs
->msr
& MSR_PR
)
1537 /* only MSR_EE and MSR_RI get changed if bit 15 set */
1538 /* mtmsrd doesn't change MSR_HV, MSR_ME or MSR_LE */
1539 imm
= (instr
& 0x10000)? 0x8002: 0xefffffffffffeffeUL
;
1546 if ((instr
>> 20) & 1) {
1548 for (sh
= 0; sh
< 8; ++sh
) {
1549 if (instr
& (0x80000 >> sh
))
1554 op
->val
= regs
->ccr
& imm
;
1557 case 144: /* mtcrf */
1558 op
->type
= COMPUTE
+ SETCC
;
1560 val
= regs
->gpr
[rd
];
1561 op
->ccval
= regs
->ccr
;
1562 for (sh
= 0; sh
< 8; ++sh
) {
1563 if (instr
& (0x80000 >> sh
))
1564 op
->ccval
= (op
->ccval
& ~imm
) |
1570 case 339: /* mfspr */
1571 spr
= ((instr
>> 16) & 0x1f) | ((instr
>> 6) & 0x3e0);
1575 if (spr
== SPRN_XER
|| spr
== SPRN_LR
||
1580 case 467: /* mtspr */
1581 spr
= ((instr
>> 16) & 0x1f) | ((instr
>> 6) & 0x3e0);
1583 op
->val
= regs
->gpr
[rd
];
1585 if (spr
== SPRN_XER
|| spr
== SPRN_LR
||
1591 * Compare instructions
1594 val
= regs
->gpr
[ra
];
1595 val2
= regs
->gpr
[rb
];
1596 #ifdef __powerpc64__
1597 if ((rd
& 1) == 0) {
1598 /* word (32-bit) compare */
1603 do_cmp_signed(regs
, op
, val
, val2
, rd
>> 2);
1607 val
= regs
->gpr
[ra
];
1608 val2
= regs
->gpr
[rb
];
1609 #ifdef __powerpc64__
1610 if ((rd
& 1) == 0) {
1611 /* word (32-bit) compare */
1612 val
= (unsigned int) val
;
1613 val2
= (unsigned int) val2
;
1616 do_cmp_unsigned(regs
, op
, val
, val2
, rd
>> 2);
1619 case 508: /* cmpb */
1620 do_cmpb(regs
, op
, regs
->gpr
[rd
], regs
->gpr
[rb
]);
1621 goto logical_done_nocc
;
1624 * Arithmetic instructions
1627 add_with_carry(regs
, op
, rd
, ~regs
->gpr
[ra
],
1630 #ifdef __powerpc64__
1631 case 9: /* mulhdu */
1632 asm("mulhdu %0,%1,%2" : "=r" (op
->val
) :
1633 "r" (regs
->gpr
[ra
]), "r" (regs
->gpr
[rb
]));
1637 add_with_carry(regs
, op
, rd
, regs
->gpr
[ra
],
1641 case 11: /* mulhwu */
1642 asm("mulhwu %0,%1,%2" : "=r" (op
->val
) :
1643 "r" (regs
->gpr
[ra
]), "r" (regs
->gpr
[rb
]));
1647 op
->val
= regs
->gpr
[rb
] - regs
->gpr
[ra
];
1649 #ifdef __powerpc64__
1650 case 73: /* mulhd */
1651 asm("mulhd %0,%1,%2" : "=r" (op
->val
) :
1652 "r" (regs
->gpr
[ra
]), "r" (regs
->gpr
[rb
]));
1655 case 75: /* mulhw */
1656 asm("mulhw %0,%1,%2" : "=r" (op
->val
) :
1657 "r" (regs
->gpr
[ra
]), "r" (regs
->gpr
[rb
]));
1661 op
->val
= -regs
->gpr
[ra
];
1664 case 136: /* subfe */
1665 add_with_carry(regs
, op
, rd
, ~regs
->gpr
[ra
],
1666 regs
->gpr
[rb
], regs
->xer
& XER_CA
);
1669 case 138: /* adde */
1670 add_with_carry(regs
, op
, rd
, regs
->gpr
[ra
],
1671 regs
->gpr
[rb
], regs
->xer
& XER_CA
);
1674 case 200: /* subfze */
1675 add_with_carry(regs
, op
, rd
, ~regs
->gpr
[ra
], 0L,
1676 regs
->xer
& XER_CA
);
1679 case 202: /* addze */
1680 add_with_carry(regs
, op
, rd
, regs
->gpr
[ra
], 0L,
1681 regs
->xer
& XER_CA
);
1684 case 232: /* subfme */
1685 add_with_carry(regs
, op
, rd
, ~regs
->gpr
[ra
], -1L,
1686 regs
->xer
& XER_CA
);
1688 #ifdef __powerpc64__
1689 case 233: /* mulld */
1690 op
->val
= regs
->gpr
[ra
] * regs
->gpr
[rb
];
1693 case 234: /* addme */
1694 add_with_carry(regs
, op
, rd
, regs
->gpr
[ra
], -1L,
1695 regs
->xer
& XER_CA
);
1698 case 235: /* mullw */
1699 op
->val
= (long)(int) regs
->gpr
[ra
] *
1700 (int) regs
->gpr
[rb
];
1703 #ifdef __powerpc64__
1704 case 265: /* modud */
1705 if (!cpu_has_feature(CPU_FTR_ARCH_300
))
1707 op
->val
= regs
->gpr
[ra
] % regs
->gpr
[rb
];
1711 op
->val
= regs
->gpr
[ra
] + regs
->gpr
[rb
];
1714 case 267: /* moduw */
1715 if (!cpu_has_feature(CPU_FTR_ARCH_300
))
1717 op
->val
= (unsigned int) regs
->gpr
[ra
] %
1718 (unsigned int) regs
->gpr
[rb
];
1720 #ifdef __powerpc64__
1721 case 457: /* divdu */
1722 op
->val
= regs
->gpr
[ra
] / regs
->gpr
[rb
];
1725 case 459: /* divwu */
1726 op
->val
= (unsigned int) regs
->gpr
[ra
] /
1727 (unsigned int) regs
->gpr
[rb
];
1729 #ifdef __powerpc64__
1730 case 489: /* divd */
1731 op
->val
= (long int) regs
->gpr
[ra
] /
1732 (long int) regs
->gpr
[rb
];
1735 case 491: /* divw */
1736 op
->val
= (int) regs
->gpr
[ra
] /
1737 (int) regs
->gpr
[rb
];
1740 case 755: /* darn */
1741 if (!cpu_has_feature(CPU_FTR_ARCH_300
))
1745 /* 32-bit conditioned */
1746 asm volatile(PPC_DARN(%0, 0) : "=r" (op
->val
));
1750 /* 64-bit conditioned */
1751 asm volatile(PPC_DARN(%0, 1) : "=r" (op
->val
));
1756 asm volatile(PPC_DARN(%0, 2) : "=r" (op
->val
));
1761 #ifdef __powerpc64__
1762 case 777: /* modsd */
1763 if (!cpu_has_feature(CPU_FTR_ARCH_300
))
1765 op
->val
= (long int) regs
->gpr
[ra
] %
1766 (long int) regs
->gpr
[rb
];
1769 case 779: /* modsw */
1770 if (!cpu_has_feature(CPU_FTR_ARCH_300
))
1772 op
->val
= (int) regs
->gpr
[ra
] %
1773 (int) regs
->gpr
[rb
];
1778 * Logical instructions
1780 case 26: /* cntlzw */
1781 val
= (unsigned int) regs
->gpr
[rd
];
1782 op
->val
= ( val
? __builtin_clz(val
) : 32 );
1784 #ifdef __powerpc64__
1785 case 58: /* cntlzd */
1786 val
= regs
->gpr
[rd
];
1787 op
->val
= ( val
? __builtin_clzl(val
) : 64 );
1791 op
->val
= regs
->gpr
[rd
] & regs
->gpr
[rb
];
1795 op
->val
= regs
->gpr
[rd
] & ~regs
->gpr
[rb
];
1798 case 122: /* popcntb */
1799 do_popcnt(regs
, op
, regs
->gpr
[rd
], 8);
1800 goto logical_done_nocc
;
1803 op
->val
= ~(regs
->gpr
[rd
] | regs
->gpr
[rb
]);
1806 case 154: /* prtyw */
1807 do_prty(regs
, op
, regs
->gpr
[rd
], 32);
1808 goto logical_done_nocc
;
1810 case 186: /* prtyd */
1811 do_prty(regs
, op
, regs
->gpr
[rd
], 64);
1812 goto logical_done_nocc
;
1814 case 252: /* bpermd */
1815 do_bpermd(regs
, op
, regs
->gpr
[rd
], regs
->gpr
[rb
]);
1816 goto logical_done_nocc
;
1819 op
->val
= ~(regs
->gpr
[rd
] ^ regs
->gpr
[rb
]);
1823 op
->val
= regs
->gpr
[rd
] ^ regs
->gpr
[rb
];
1826 case 378: /* popcntw */
1827 do_popcnt(regs
, op
, regs
->gpr
[rd
], 32);
1828 goto logical_done_nocc
;
1831 op
->val
= regs
->gpr
[rd
] | ~regs
->gpr
[rb
];
1835 op
->val
= regs
->gpr
[rd
] | regs
->gpr
[rb
];
1838 case 476: /* nand */
1839 op
->val
= ~(regs
->gpr
[rd
] & regs
->gpr
[rb
]);
1842 case 506: /* popcntd */
1843 do_popcnt(regs
, op
, regs
->gpr
[rd
], 64);
1844 goto logical_done_nocc
;
1846 case 538: /* cnttzw */
1847 if (!cpu_has_feature(CPU_FTR_ARCH_300
))
1849 val
= (unsigned int) regs
->gpr
[rd
];
1850 op
->val
= (val
? __builtin_ctz(val
) : 32);
1852 #ifdef __powerpc64__
1853 case 570: /* cnttzd */
1854 if (!cpu_has_feature(CPU_FTR_ARCH_300
))
1856 val
= regs
->gpr
[rd
];
1857 op
->val
= (val
? __builtin_ctzl(val
) : 64);
1860 case 922: /* extsh */
1861 op
->val
= (signed short) regs
->gpr
[rd
];
1864 case 954: /* extsb */
1865 op
->val
= (signed char) regs
->gpr
[rd
];
1867 #ifdef __powerpc64__
1868 case 986: /* extsw */
1869 op
->val
= (signed int) regs
->gpr
[rd
];
1874 * Shift instructions
1877 sh
= regs
->gpr
[rb
] & 0x3f;
1879 op
->val
= (regs
->gpr
[rd
] << sh
) & 0xffffffffUL
;
1885 sh
= regs
->gpr
[rb
] & 0x3f;
1887 op
->val
= (regs
->gpr
[rd
] & 0xffffffffUL
) >> sh
;
1892 case 792: /* sraw */
1893 op
->type
= COMPUTE
+ SETREG
+ SETXER
;
1894 sh
= regs
->gpr
[rb
] & 0x3f;
1895 ival
= (signed int) regs
->gpr
[rd
];
1896 op
->val
= ival
>> (sh
< 32 ? sh
: 31);
1897 op
->xerval
= regs
->xer
;
1898 if (ival
< 0 && (sh
>= 32 || (ival
& ((1ul << sh
) - 1)) != 0))
1899 op
->xerval
|= XER_CA
;
1901 op
->xerval
&= ~XER_CA
;
1902 set_ca32(op
, op
->xerval
& XER_CA
);
1905 case 824: /* srawi */
1906 op
->type
= COMPUTE
+ SETREG
+ SETXER
;
1908 ival
= (signed int) regs
->gpr
[rd
];
1909 op
->val
= ival
>> sh
;
1910 op
->xerval
= regs
->xer
;
1911 if (ival
< 0 && (ival
& ((1ul << sh
) - 1)) != 0)
1912 op
->xerval
|= XER_CA
;
1914 op
->xerval
&= ~XER_CA
;
1915 set_ca32(op
, op
->xerval
& XER_CA
);
1918 #ifdef __powerpc64__
1920 sh
= regs
->gpr
[rb
] & 0x7f;
1922 op
->val
= regs
->gpr
[rd
] << sh
;
1928 sh
= regs
->gpr
[rb
] & 0x7f;
1930 op
->val
= regs
->gpr
[rd
] >> sh
;
1935 case 794: /* srad */
1936 op
->type
= COMPUTE
+ SETREG
+ SETXER
;
1937 sh
= regs
->gpr
[rb
] & 0x7f;
1938 ival
= (signed long int) regs
->gpr
[rd
];
1939 op
->val
= ival
>> (sh
< 64 ? sh
: 63);
1940 op
->xerval
= regs
->xer
;
1941 if (ival
< 0 && (sh
>= 64 || (ival
& ((1ul << sh
) - 1)) != 0))
1942 op
->xerval
|= XER_CA
;
1944 op
->xerval
&= ~XER_CA
;
1945 set_ca32(op
, op
->xerval
& XER_CA
);
1948 case 826: /* sradi with sh_5 = 0 */
1949 case 827: /* sradi with sh_5 = 1 */
1950 op
->type
= COMPUTE
+ SETREG
+ SETXER
;
1951 sh
= rb
| ((instr
& 2) << 4);
1952 ival
= (signed long int) regs
->gpr
[rd
];
1953 op
->val
= ival
>> sh
;
1954 op
->xerval
= regs
->xer
;
1955 if (ival
< 0 && (ival
& ((1ul << sh
) - 1)) != 0)
1956 op
->xerval
|= XER_CA
;
1958 op
->xerval
&= ~XER_CA
;
1959 set_ca32(op
, op
->xerval
& XER_CA
);
1962 case 890: /* extswsli with sh_5 = 0 */
1963 case 891: /* extswsli with sh_5 = 1 */
1964 if (!cpu_has_feature(CPU_FTR_ARCH_300
))
1966 op
->type
= COMPUTE
+ SETREG
;
1967 sh
= rb
| ((instr
& 2) << 4);
1968 val
= (signed int) regs
->gpr
[rd
];
1970 op
->val
= ROTATE(val
, sh
) & MASK64(0, 63 - sh
);
1975 #endif /* __powerpc64__ */
1978 * Cache instructions
1980 case 54: /* dcbst */
1981 op
->type
= MKOP(CACHEOP
, DCBST
, 0);
1982 op
->ea
= xform_ea(instr
, regs
);
1986 op
->type
= MKOP(CACHEOP
, DCBF
, 0);
1987 op
->ea
= xform_ea(instr
, regs
);
1990 case 246: /* dcbtst */
1991 op
->type
= MKOP(CACHEOP
, DCBTST
, 0);
1992 op
->ea
= xform_ea(instr
, regs
);
1996 case 278: /* dcbt */
1997 op
->type
= MKOP(CACHEOP
, DCBTST
, 0);
1998 op
->ea
= xform_ea(instr
, regs
);
2002 case 982: /* icbi */
2003 op
->type
= MKOP(CACHEOP
, ICBI
, 0);
2004 op
->ea
= xform_ea(instr
, regs
);
2007 case 1014: /* dcbz */
2008 op
->type
= MKOP(CACHEOP
, DCBZ
, 0);
2009 op
->ea
= xform_ea(instr
, regs
);
2019 op
->update_reg
= ra
;
2021 op
->val
= regs
->gpr
[rd
];
2022 u
= (instr
>> 20) & UPDATE
;
2028 op
->ea
= xform_ea(instr
, regs
);
2029 switch ((instr
>> 1) & 0x3ff) {
2030 case 20: /* lwarx */
2031 op
->type
= MKOP(LARX
, 0, 4);
2034 case 150: /* stwcx. */
2035 op
->type
= MKOP(STCX
, 0, 4);
2038 #ifdef __powerpc64__
2039 case 84: /* ldarx */
2040 op
->type
= MKOP(LARX
, 0, 8);
2043 case 214: /* stdcx. */
2044 op
->type
= MKOP(STCX
, 0, 8);
2047 case 52: /* lbarx */
2048 op
->type
= MKOP(LARX
, 0, 1);
2051 case 694: /* stbcx. */
2052 op
->type
= MKOP(STCX
, 0, 1);
2055 case 116: /* lharx */
2056 op
->type
= MKOP(LARX
, 0, 2);
2059 case 726: /* sthcx. */
2060 op
->type
= MKOP(STCX
, 0, 2);
2063 case 276: /* lqarx */
2064 if (!((rd
& 1) || rd
== ra
|| rd
== rb
))
2065 op
->type
= MKOP(LARX
, 0, 16);
2068 case 182: /* stqcx. */
2070 op
->type
= MKOP(STCX
, 0, 16);
2075 case 55: /* lwzux */
2076 op
->type
= MKOP(LOAD
, u
, 4);
2080 case 119: /* lbzux */
2081 op
->type
= MKOP(LOAD
, u
, 1);
2084 #ifdef CONFIG_ALTIVEC
2086 * Note: for the load/store vector element instructions,
2087 * bits of the EA say which field of the VMX register to use.
2090 op
->type
= MKOP(LOAD_VMX
, 0, 1);
2091 op
->element_size
= 1;
2094 case 39: /* lvehx */
2095 op
->type
= MKOP(LOAD_VMX
, 0, 2);
2096 op
->element_size
= 2;
2099 case 71: /* lvewx */
2100 op
->type
= MKOP(LOAD_VMX
, 0, 4);
2101 op
->element_size
= 4;
2105 case 359: /* lvxl */
2106 op
->type
= MKOP(LOAD_VMX
, 0, 16);
2107 op
->element_size
= 16;
2110 case 135: /* stvebx */
2111 op
->type
= MKOP(STORE_VMX
, 0, 1);
2112 op
->element_size
= 1;
2115 case 167: /* stvehx */
2116 op
->type
= MKOP(STORE_VMX
, 0, 2);
2117 op
->element_size
= 2;
2120 case 199: /* stvewx */
2121 op
->type
= MKOP(STORE_VMX
, 0, 4);
2122 op
->element_size
= 4;
2125 case 231: /* stvx */
2126 case 487: /* stvxl */
2127 op
->type
= MKOP(STORE_VMX
, 0, 16);
2129 #endif /* CONFIG_ALTIVEC */
2131 #ifdef __powerpc64__
2134 op
->type
= MKOP(LOAD
, u
, 8);
2137 case 149: /* stdx */
2138 case 181: /* stdux */
2139 op
->type
= MKOP(STORE
, u
, 8);
2143 case 151: /* stwx */
2144 case 183: /* stwux */
2145 op
->type
= MKOP(STORE
, u
, 4);
2148 case 215: /* stbx */
2149 case 247: /* stbux */
2150 op
->type
= MKOP(STORE
, u
, 1);
2153 case 279: /* lhzx */
2154 case 311: /* lhzux */
2155 op
->type
= MKOP(LOAD
, u
, 2);
2158 #ifdef __powerpc64__
2159 case 341: /* lwax */
2160 case 373: /* lwaux */
2161 op
->type
= MKOP(LOAD
, SIGNEXT
| u
, 4);
2165 case 343: /* lhax */
2166 case 375: /* lhaux */
2167 op
->type
= MKOP(LOAD
, SIGNEXT
| u
, 2);
2170 case 407: /* sthx */
2171 case 439: /* sthux */
2172 op
->type
= MKOP(STORE
, u
, 2);
2175 #ifdef __powerpc64__
2176 case 532: /* ldbrx */
2177 op
->type
= MKOP(LOAD
, BYTEREV
, 8);
2181 case 533: /* lswx */
2182 op
->type
= MKOP(LOAD_MULTI
, 0, regs
->xer
& 0x7f);
2185 case 534: /* lwbrx */
2186 op
->type
= MKOP(LOAD
, BYTEREV
, 4);
2189 case 597: /* lswi */
2191 rb
= 32; /* # bytes to load */
2192 op
->type
= MKOP(LOAD_MULTI
, 0, rb
);
2193 op
->ea
= ra
? regs
->gpr
[ra
] : 0;
2196 #ifdef CONFIG_PPC_FPU
2197 case 535: /* lfsx */
2198 case 567: /* lfsux */
2199 op
->type
= MKOP(LOAD_FP
, u
| FPCONV
, 4);
2202 case 599: /* lfdx */
2203 case 631: /* lfdux */
2204 op
->type
= MKOP(LOAD_FP
, u
, 8);
2207 case 663: /* stfsx */
2208 case 695: /* stfsux */
2209 op
->type
= MKOP(STORE_FP
, u
| FPCONV
, 4);
2212 case 727: /* stfdx */
2213 case 759: /* stfdux */
2214 op
->type
= MKOP(STORE_FP
, u
, 8);
2217 #ifdef __powerpc64__
2218 case 791: /* lfdpx */
2219 op
->type
= MKOP(LOAD_FP
, 0, 16);
2222 case 855: /* lfiwax */
2223 op
->type
= MKOP(LOAD_FP
, SIGNEXT
, 4);
2226 case 887: /* lfiwzx */
2227 op
->type
= MKOP(LOAD_FP
, 0, 4);
2230 case 919: /* stfdpx */
2231 op
->type
= MKOP(STORE_FP
, 0, 16);
2234 case 983: /* stfiwx */
2235 op
->type
= MKOP(STORE_FP
, 0, 4);
2237 #endif /* __powerpc64 */
2238 #endif /* CONFIG_PPC_FPU */
2240 #ifdef __powerpc64__
2241 case 660: /* stdbrx */
2242 op
->type
= MKOP(STORE
, BYTEREV
, 8);
2243 op
->val
= byterev_8(regs
->gpr
[rd
]);
2247 case 661: /* stswx */
2248 op
->type
= MKOP(STORE_MULTI
, 0, regs
->xer
& 0x7f);
2251 case 662: /* stwbrx */
2252 op
->type
= MKOP(STORE
, BYTEREV
, 4);
2253 op
->val
= byterev_4(regs
->gpr
[rd
]);
2256 case 725: /* stswi */
2258 rb
= 32; /* # bytes to store */
2259 op
->type
= MKOP(STORE_MULTI
, 0, rb
);
2260 op
->ea
= ra
? regs
->gpr
[ra
] : 0;
2263 case 790: /* lhbrx */
2264 op
->type
= MKOP(LOAD
, BYTEREV
, 2);
2267 case 918: /* sthbrx */
2268 op
->type
= MKOP(STORE
, BYTEREV
, 2);
2269 op
->val
= byterev_2(regs
->gpr
[rd
]);
2273 case 12: /* lxsiwzx */
2274 op
->reg
= rd
| ((instr
& 1) << 5);
2275 op
->type
= MKOP(LOAD_VSX
, 0, 4);
2276 op
->element_size
= 8;
2279 case 76: /* lxsiwax */
2280 op
->reg
= rd
| ((instr
& 1) << 5);
2281 op
->type
= MKOP(LOAD_VSX
, SIGNEXT
, 4);
2282 op
->element_size
= 8;
2285 case 140: /* stxsiwx */
2286 op
->reg
= rd
| ((instr
& 1) << 5);
2287 op
->type
= MKOP(STORE_VSX
, 0, 4);
2288 op
->element_size
= 8;
2291 case 268: /* lxvx */
2292 op
->reg
= rd
| ((instr
& 1) << 5);
2293 op
->type
= MKOP(LOAD_VSX
, 0, 16);
2294 op
->element_size
= 16;
2295 op
->vsx_flags
= VSX_CHECK_VEC
;
2298 case 269: /* lxvl */
2299 case 301: { /* lxvll */
2301 op
->reg
= rd
| ((instr
& 1) << 5);
2302 op
->ea
= ra
? regs
->gpr
[ra
] : 0;
2303 nb
= regs
->gpr
[rb
] & 0xff;
2306 op
->type
= MKOP(LOAD_VSX
, 0, nb
);
2307 op
->element_size
= 16;
2308 op
->vsx_flags
= ((instr
& 0x20) ? VSX_LDLEFT
: 0) |
2312 case 332: /* lxvdsx */
2313 op
->reg
= rd
| ((instr
& 1) << 5);
2314 op
->type
= MKOP(LOAD_VSX
, 0, 8);
2315 op
->element_size
= 8;
2316 op
->vsx_flags
= VSX_SPLAT
;
2319 case 364: /* lxvwsx */
2320 op
->reg
= rd
| ((instr
& 1) << 5);
2321 op
->type
= MKOP(LOAD_VSX
, 0, 4);
2322 op
->element_size
= 4;
2323 op
->vsx_flags
= VSX_SPLAT
| VSX_CHECK_VEC
;
2326 case 396: /* stxvx */
2327 op
->reg
= rd
| ((instr
& 1) << 5);
2328 op
->type
= MKOP(STORE_VSX
, 0, 16);
2329 op
->element_size
= 16;
2330 op
->vsx_flags
= VSX_CHECK_VEC
;
2333 case 397: /* stxvl */
2334 case 429: { /* stxvll */
2336 op
->reg
= rd
| ((instr
& 1) << 5);
2337 op
->ea
= ra
? regs
->gpr
[ra
] : 0;
2338 nb
= regs
->gpr
[rb
] & 0xff;
2341 op
->type
= MKOP(STORE_VSX
, 0, nb
);
2342 op
->element_size
= 16;
2343 op
->vsx_flags
= ((instr
& 0x20) ? VSX_LDLEFT
: 0) |
2347 case 524: /* lxsspx */
2348 op
->reg
= rd
| ((instr
& 1) << 5);
2349 op
->type
= MKOP(LOAD_VSX
, 0, 4);
2350 op
->element_size
= 8;
2351 op
->vsx_flags
= VSX_FPCONV
;
2354 case 588: /* lxsdx */
2355 op
->reg
= rd
| ((instr
& 1) << 5);
2356 op
->type
= MKOP(LOAD_VSX
, 0, 8);
2357 op
->element_size
= 8;
2360 case 652: /* stxsspx */
2361 op
->reg
= rd
| ((instr
& 1) << 5);
2362 op
->type
= MKOP(STORE_VSX
, 0, 4);
2363 op
->element_size
= 8;
2364 op
->vsx_flags
= VSX_FPCONV
;
2367 case 716: /* stxsdx */
2368 op
->reg
= rd
| ((instr
& 1) << 5);
2369 op
->type
= MKOP(STORE_VSX
, 0, 8);
2370 op
->element_size
= 8;
2373 case 780: /* lxvw4x */
2374 op
->reg
= rd
| ((instr
& 1) << 5);
2375 op
->type
= MKOP(LOAD_VSX
, 0, 16);
2376 op
->element_size
= 4;
2379 case 781: /* lxsibzx */
2380 op
->reg
= rd
| ((instr
& 1) << 5);
2381 op
->type
= MKOP(LOAD_VSX
, 0, 1);
2382 op
->element_size
= 8;
2383 op
->vsx_flags
= VSX_CHECK_VEC
;
2386 case 812: /* lxvh8x */
2387 op
->reg
= rd
| ((instr
& 1) << 5);
2388 op
->type
= MKOP(LOAD_VSX
, 0, 16);
2389 op
->element_size
= 2;
2390 op
->vsx_flags
= VSX_CHECK_VEC
;
2393 case 813: /* lxsihzx */
2394 op
->reg
= rd
| ((instr
& 1) << 5);
2395 op
->type
= MKOP(LOAD_VSX
, 0, 2);
2396 op
->element_size
= 8;
2397 op
->vsx_flags
= VSX_CHECK_VEC
;
2400 case 844: /* lxvd2x */
2401 op
->reg
= rd
| ((instr
& 1) << 5);
2402 op
->type
= MKOP(LOAD_VSX
, 0, 16);
2403 op
->element_size
= 8;
2406 case 876: /* lxvb16x */
2407 op
->reg
= rd
| ((instr
& 1) << 5);
2408 op
->type
= MKOP(LOAD_VSX
, 0, 16);
2409 op
->element_size
= 1;
2410 op
->vsx_flags
= VSX_CHECK_VEC
;
2413 case 908: /* stxvw4x */
2414 op
->reg
= rd
| ((instr
& 1) << 5);
2415 op
->type
= MKOP(STORE_VSX
, 0, 16);
2416 op
->element_size
= 4;
2419 case 909: /* stxsibx */
2420 op
->reg
= rd
| ((instr
& 1) << 5);
2421 op
->type
= MKOP(STORE_VSX
, 0, 1);
2422 op
->element_size
= 8;
2423 op
->vsx_flags
= VSX_CHECK_VEC
;
2426 case 940: /* stxvh8x */
2427 op
->reg
= rd
| ((instr
& 1) << 5);
2428 op
->type
= MKOP(STORE_VSX
, 0, 16);
2429 op
->element_size
= 2;
2430 op
->vsx_flags
= VSX_CHECK_VEC
;
2433 case 941: /* stxsihx */
2434 op
->reg
= rd
| ((instr
& 1) << 5);
2435 op
->type
= MKOP(STORE_VSX
, 0, 2);
2436 op
->element_size
= 8;
2437 op
->vsx_flags
= VSX_CHECK_VEC
;
2440 case 972: /* stxvd2x */
2441 op
->reg
= rd
| ((instr
& 1) << 5);
2442 op
->type
= MKOP(STORE_VSX
, 0, 16);
2443 op
->element_size
= 8;
2446 case 1004: /* stxvb16x */
2447 op
->reg
= rd
| ((instr
& 1) << 5);
2448 op
->type
= MKOP(STORE_VSX
, 0, 16);
2449 op
->element_size
= 1;
2450 op
->vsx_flags
= VSX_CHECK_VEC
;
2453 #endif /* CONFIG_VSX */
2459 op
->type
= MKOP(LOAD
, u
, 4);
2460 op
->ea
= dform_ea(instr
, regs
);
2465 op
->type
= MKOP(LOAD
, u
, 1);
2466 op
->ea
= dform_ea(instr
, regs
);
2471 op
->type
= MKOP(STORE
, u
, 4);
2472 op
->ea
= dform_ea(instr
, regs
);
2477 op
->type
= MKOP(STORE
, u
, 1);
2478 op
->ea
= dform_ea(instr
, regs
);
2483 op
->type
= MKOP(LOAD
, u
, 2);
2484 op
->ea
= dform_ea(instr
, regs
);
2489 op
->type
= MKOP(LOAD
, SIGNEXT
| u
, 2);
2490 op
->ea
= dform_ea(instr
, regs
);
2495 op
->type
= MKOP(STORE
, u
, 2);
2496 op
->ea
= dform_ea(instr
, regs
);
2501 break; /* invalid form, ra in range to load */
2502 op
->type
= MKOP(LOAD_MULTI
, 0, 4 * (32 - rd
));
2503 op
->ea
= dform_ea(instr
, regs
);
2507 op
->type
= MKOP(STORE_MULTI
, 0, 4 * (32 - rd
));
2508 op
->ea
= dform_ea(instr
, regs
);
2511 #ifdef CONFIG_PPC_FPU
2514 op
->type
= MKOP(LOAD_FP
, u
| FPCONV
, 4);
2515 op
->ea
= dform_ea(instr
, regs
);
2520 op
->type
= MKOP(LOAD_FP
, u
, 8);
2521 op
->ea
= dform_ea(instr
, regs
);
2525 case 53: /* stfsu */
2526 op
->type
= MKOP(STORE_FP
, u
| FPCONV
, 4);
2527 op
->ea
= dform_ea(instr
, regs
);
2531 case 55: /* stfdu */
2532 op
->type
= MKOP(STORE_FP
, u
, 8);
2533 op
->ea
= dform_ea(instr
, regs
);
2537 #ifdef __powerpc64__
2539 if (!((rd
& 1) || (rd
== ra
)))
2540 op
->type
= MKOP(LOAD
, 0, 16);
2541 op
->ea
= dqform_ea(instr
, regs
);
2546 case 57: /* lfdp, lxsd, lxssp */
2547 op
->ea
= dsform_ea(instr
, regs
);
2548 switch (instr
& 3) {
2551 break; /* reg must be even */
2552 op
->type
= MKOP(LOAD_FP
, 0, 16);
2556 op
->type
= MKOP(LOAD_VSX
, 0, 8);
2557 op
->element_size
= 8;
2558 op
->vsx_flags
= VSX_CHECK_VEC
;
2562 op
->type
= MKOP(LOAD_VSX
, 0, 4);
2563 op
->element_size
= 8;
2564 op
->vsx_flags
= VSX_FPCONV
| VSX_CHECK_VEC
;
2568 #endif /* CONFIG_VSX */
2570 #ifdef __powerpc64__
2571 case 58: /* ld[u], lwa */
2572 op
->ea
= dsform_ea(instr
, regs
);
2573 switch (instr
& 3) {
2575 op
->type
= MKOP(LOAD
, 0, 8);
2578 op
->type
= MKOP(LOAD
, UPDATE
, 8);
2581 op
->type
= MKOP(LOAD
, SIGNEXT
, 4);
2588 case 61: /* stfdp, lxv, stxsd, stxssp, stxv */
2589 switch (instr
& 7) {
2590 case 0: /* stfdp with LSB of DS field = 0 */
2591 case 4: /* stfdp with LSB of DS field = 1 */
2592 op
->ea
= dsform_ea(instr
, regs
);
2593 op
->type
= MKOP(STORE_FP
, 0, 16);
2597 op
->ea
= dqform_ea(instr
, regs
);
2600 op
->type
= MKOP(LOAD_VSX
, 0, 16);
2601 op
->element_size
= 16;
2602 op
->vsx_flags
= VSX_CHECK_VEC
;
2605 case 2: /* stxsd with LSB of DS field = 0 */
2606 case 6: /* stxsd with LSB of DS field = 1 */
2607 op
->ea
= dsform_ea(instr
, regs
);
2609 op
->type
= MKOP(STORE_VSX
, 0, 8);
2610 op
->element_size
= 8;
2611 op
->vsx_flags
= VSX_CHECK_VEC
;
2614 case 3: /* stxssp with LSB of DS field = 0 */
2615 case 7: /* stxssp with LSB of DS field = 1 */
2616 op
->ea
= dsform_ea(instr
, regs
);
2618 op
->type
= MKOP(STORE_VSX
, 0, 4);
2619 op
->element_size
= 8;
2620 op
->vsx_flags
= VSX_FPCONV
| VSX_CHECK_VEC
;
2624 op
->ea
= dqform_ea(instr
, regs
);
2627 op
->type
= MKOP(STORE_VSX
, 0, 16);
2628 op
->element_size
= 16;
2629 op
->vsx_flags
= VSX_CHECK_VEC
;
2633 #endif /* CONFIG_VSX */
2635 #ifdef __powerpc64__
2636 case 62: /* std[u] */
2637 op
->ea
= dsform_ea(instr
, regs
);
2638 switch (instr
& 3) {
2640 op
->type
= MKOP(STORE
, 0, 8);
2643 op
->type
= MKOP(STORE
, UPDATE
, 8);
2647 op
->type
= MKOP(STORE
, 0, 16);
2651 #endif /* __powerpc64__ */
2656 if ((GETTYPE(op
->type
) == LOAD_VSX
||
2657 GETTYPE(op
->type
) == STORE_VSX
) &&
2658 !cpu_has_feature(CPU_FTR_VSX
)) {
2661 #endif /* CONFIG_VSX */
2682 op
->type
= INTERRUPT
| 0x700;
2683 op
->val
= SRR1_PROGPRIV
;
2687 op
->type
= INTERRUPT
| 0x700;
2688 op
->val
= SRR1_PROGTRAP
;
2691 EXPORT_SYMBOL_GPL(analyse_instr
);
2692 NOKPROBE_SYMBOL(analyse_instr
);
2695 * For PPC32 we always use stwu with r1 to change the stack pointer.
2696 * So this emulated store may corrupt the exception frame, now we
2697 * have to provide the exception frame trampoline, which is pushed
2698 * below the kprobed function stack. So we only update gpr[1] but
2699 * don't emulate the real store operation. We will do real store
2700 * operation safely in exception return code by checking this flag.
2702 static nokprobe_inline
int handle_stack_update(unsigned long ea
, struct pt_regs
*regs
)
2706 * Check if we will touch kernel stack overflow
2708 if (ea
- STACK_INT_FRAME_SIZE
<= current
->thread
.ksp_limit
) {
2709 printk(KERN_CRIT
"Can't kprobe this since kernel stack would overflow.\n");
2712 #endif /* CONFIG_PPC32 */
2714 * Check if we already set since that means we'll
2715 * lose the previous value.
2717 WARN_ON(test_thread_flag(TIF_EMULATE_STACK_STORE
));
2718 set_thread_flag(TIF_EMULATE_STACK_STORE
);
2722 static nokprobe_inline
void do_signext(unsigned long *valp
, int size
)
2726 *valp
= (signed short) *valp
;
2729 *valp
= (signed int) *valp
;
2734 static nokprobe_inline
void do_byterev(unsigned long *valp
, int size
)
2738 *valp
= byterev_2(*valp
);
2741 *valp
= byterev_4(*valp
);
2743 #ifdef __powerpc64__
2745 *valp
= byterev_8(*valp
);
2752 * Emulate an instruction that can be executed just by updating
2755 void emulate_update_regs(struct pt_regs
*regs
, struct instruction_op
*op
)
2757 unsigned long next_pc
;
2759 next_pc
= truncate_if_32bit(regs
->msr
, regs
->nip
+ 4);
2760 switch (GETTYPE(op
->type
)) {
2762 if (op
->type
& SETREG
)
2763 regs
->gpr
[op
->reg
] = op
->val
;
2764 if (op
->type
& SETCC
)
2765 regs
->ccr
= op
->ccval
;
2766 if (op
->type
& SETXER
)
2767 regs
->xer
= op
->xerval
;
2771 if (op
->type
& SETLK
)
2772 regs
->link
= next_pc
;
2773 if (op
->type
& BRTAKEN
)
2775 if (op
->type
& DECCTR
)
2780 switch (op
->type
& BARRIER_MASK
) {
2790 case BARRIER_LWSYNC
:
2791 asm volatile("lwsync" : : : "memory");
2793 case BARRIER_PTESYNC
:
2794 asm volatile("ptesync" : : : "memory");
2802 regs
->gpr
[op
->reg
] = regs
->xer
& 0xffffffffUL
;
2805 regs
->gpr
[op
->reg
] = regs
->link
;
2808 regs
->gpr
[op
->reg
] = regs
->ctr
;
2818 regs
->xer
= op
->val
& 0xffffffffUL
;
2821 regs
->link
= op
->val
;
2824 regs
->ctr
= op
->val
;
2834 regs
->nip
= next_pc
;
2836 NOKPROBE_SYMBOL(emulate_update_regs
);
2839 * Emulate a previously-analysed load or store instruction.
2840 * Return values are:
2841 * 0 = instruction emulated successfully
2842 * -EFAULT = address out of range or access faulted (regs->dar
2843 * contains the faulting address)
2844 * -EACCES = misaligned access, instruction requires alignment
2845 * -EINVAL = unknown operation in *op
2847 int emulate_loadstore(struct pt_regs
*regs
, struct instruction_op
*op
)
2849 int err
, size
, type
;
2857 size
= GETSIZE(op
->type
);
2858 type
= GETTYPE(op
->type
);
2859 cross_endian
= (regs
->msr
& MSR_LE
) != (MSR_KERNEL
& MSR_LE
);
2860 ea
= truncate_if_32bit(regs
->msr
, op
->ea
);
2864 if (ea
& (size
- 1))
2865 return -EACCES
; /* can't handle misaligned */
2866 if (!address_ok(regs
, ea
, size
))
2871 #ifdef __powerpc64__
2873 __get_user_asmx(val
, ea
, err
, "lbarx");
2876 __get_user_asmx(val
, ea
, err
, "lharx");
2880 __get_user_asmx(val
, ea
, err
, "lwarx");
2882 #ifdef __powerpc64__
2884 __get_user_asmx(val
, ea
, err
, "ldarx");
2887 err
= do_lqarx(ea
, ®s
->gpr
[op
->reg
]);
2898 regs
->gpr
[op
->reg
] = val
;
2902 if (ea
& (size
- 1))
2903 return -EACCES
; /* can't handle misaligned */
2904 if (!address_ok(regs
, ea
, size
))
2908 #ifdef __powerpc64__
2910 __put_user_asmx(op
->val
, ea
, err
, "stbcx.", cr
);
2913 __put_user_asmx(op
->val
, ea
, err
, "stbcx.", cr
);
2917 __put_user_asmx(op
->val
, ea
, err
, "stwcx.", cr
);
2919 #ifdef __powerpc64__
2921 __put_user_asmx(op
->val
, ea
, err
, "stdcx.", cr
);
2924 err
= do_stqcx(ea
, regs
->gpr
[op
->reg
],
2925 regs
->gpr
[op
->reg
+ 1], &cr
);
2932 regs
->ccr
= (regs
->ccr
& 0x0fffffff) |
2934 ((regs
->xer
>> 3) & 0x10000000);
2940 #ifdef __powerpc64__
2942 err
= emulate_lq(regs
, ea
, op
->reg
, cross_endian
);
2946 err
= read_mem(®s
->gpr
[op
->reg
], ea
, size
, regs
);
2948 if (op
->type
& SIGNEXT
)
2949 do_signext(®s
->gpr
[op
->reg
], size
);
2950 if ((op
->type
& BYTEREV
) == (cross_endian
? 0 : BYTEREV
))
2951 do_byterev(®s
->gpr
[op
->reg
], size
);
2955 #ifdef CONFIG_PPC_FPU
2958 * If the instruction is in userspace, we can emulate it even
2959 * if the VMX state is not live, because we have the state
2960 * stored in the thread_struct. If the instruction is in
2961 * the kernel, we must not touch the state in the thread_struct.
2963 if (!(regs
->msr
& MSR_PR
) && !(regs
->msr
& MSR_FP
))
2965 err
= do_fp_load(op
, ea
, regs
, cross_endian
);
2968 #ifdef CONFIG_ALTIVEC
2970 if (!(regs
->msr
& MSR_PR
) && !(regs
->msr
& MSR_VEC
))
2972 err
= do_vec_load(op
->reg
, ea
, size
, regs
, cross_endian
);
2977 unsigned long msrbit
= MSR_VSX
;
2980 * Some VSX instructions check the MSR_VEC bit rather than MSR_VSX
2981 * when the target of the instruction is a vector register.
2983 if (op
->reg
>= 32 && (op
->vsx_flags
& VSX_CHECK_VEC
))
2985 if (!(regs
->msr
& MSR_PR
) && !(regs
->msr
& msrbit
))
2987 err
= do_vsx_load(op
, ea
, regs
, cross_endian
);
2992 if (!address_ok(regs
, ea
, size
))
2995 for (i
= 0; i
< size
; i
+= 4) {
2996 unsigned int v32
= 0;
3001 err
= copy_mem_in((u8
*) &v32
, ea
, nb
, regs
);
3004 if (unlikely(cross_endian
))
3005 v32
= byterev_4(v32
);
3006 regs
->gpr
[rd
] = v32
;
3008 /* reg number wraps from 31 to 0 for lsw[ix] */
3009 rd
= (rd
+ 1) & 0x1f;
3014 #ifdef __powerpc64__
3016 err
= emulate_stq(regs
, ea
, op
->reg
, cross_endian
);
3020 if ((op
->type
& UPDATE
) && size
== sizeof(long) &&
3021 op
->reg
== 1 && op
->update_reg
== 1 &&
3022 !(regs
->msr
& MSR_PR
) &&
3023 ea
>= regs
->gpr
[1] - STACK_INT_FRAME_SIZE
) {
3024 err
= handle_stack_update(ea
, regs
);
3027 if (unlikely(cross_endian
))
3028 do_byterev(&op
->val
, size
);
3029 err
= write_mem(op
->val
, ea
, size
, regs
);
3032 #ifdef CONFIG_PPC_FPU
3034 if (!(regs
->msr
& MSR_PR
) && !(regs
->msr
& MSR_FP
))
3036 err
= do_fp_store(op
, ea
, regs
, cross_endian
);
3039 #ifdef CONFIG_ALTIVEC
3041 if (!(regs
->msr
& MSR_PR
) && !(regs
->msr
& MSR_VEC
))
3043 err
= do_vec_store(op
->reg
, ea
, size
, regs
, cross_endian
);
3048 unsigned long msrbit
= MSR_VSX
;
3051 * Some VSX instructions check the MSR_VEC bit rather than MSR_VSX
3052 * when the target of the instruction is a vector register.
3054 if (op
->reg
>= 32 && (op
->vsx_flags
& VSX_CHECK_VEC
))
3056 if (!(regs
->msr
& MSR_PR
) && !(regs
->msr
& msrbit
))
3058 err
= do_vsx_store(op
, ea
, regs
, cross_endian
);
3063 if (!address_ok(regs
, ea
, size
))
3066 for (i
= 0; i
< size
; i
+= 4) {
3067 unsigned int v32
= regs
->gpr
[rd
];
3072 if (unlikely(cross_endian
))
3073 v32
= byterev_4(v32
);
3074 err
= copy_mem_out((u8
*) &v32
, ea
, nb
, regs
);
3078 /* reg number wraps from 31 to 0 for stsw[ix] */
3079 rd
= (rd
+ 1) & 0x1f;
3090 if (op
->type
& UPDATE
)
3091 regs
->gpr
[op
->update_reg
] = op
->ea
;
3095 NOKPROBE_SYMBOL(emulate_loadstore
);
3098 * Emulate instructions that cause a transfer of control,
3099 * loads and stores, and a few other instructions.
3100 * Returns 1 if the step was emulated, 0 if not,
3101 * or -1 if the instruction is one that should not be stepped,
3102 * such as an rfid, or a mtmsrd that would clear MSR_RI.
3104 int emulate_step(struct pt_regs
*regs
, unsigned int instr
)
3106 struct instruction_op op
;
3111 r
= analyse_instr(&op
, regs
, instr
);
3115 emulate_update_regs(regs
, &op
);
3120 type
= GETTYPE(op
.type
);
3122 if (OP_IS_LOAD_STORE(type
)) {
3123 err
= emulate_loadstore(regs
, &op
);
3131 ea
= truncate_if_32bit(regs
->msr
, op
.ea
);
3132 if (!address_ok(regs
, ea
, 8))
3134 switch (op
.type
& CACHEOP_MASK
) {
3136 __cacheop_user_asmx(ea
, err
, "dcbst");
3139 __cacheop_user_asmx(ea
, err
, "dcbf");
3143 prefetchw((void *) ea
);
3147 prefetch((void *) ea
);
3150 __cacheop_user_asmx(ea
, err
, "icbi");
3153 err
= emulate_dcbz(ea
, regs
);
3163 regs
->gpr
[op
.reg
] = regs
->msr
& MSR_MASK
;
3167 val
= regs
->gpr
[op
.reg
];
3168 if ((val
& MSR_RI
) == 0)
3169 /* can't step mtmsr[d] that would clear MSR_RI */
3171 /* here op.val is the mask of bits to change */
3172 regs
->msr
= (regs
->msr
& ~op
.val
) | (val
& op
.val
);
3176 case SYSCALL
: /* sc */
3178 * N.B. this uses knowledge about how the syscall
3179 * entry code works. If that is changed, this will
3180 * need to be changed also.
3182 if (regs
->gpr
[0] == 0x1ebe &&
3183 cpu_has_feature(CPU_FTR_REAL_LE
)) {
3184 regs
->msr
^= MSR_LE
;
3187 regs
->gpr
[9] = regs
->gpr
[13];
3188 regs
->gpr
[10] = MSR_KERNEL
;
3189 regs
->gpr
[11] = regs
->nip
+ 4;
3190 regs
->gpr
[12] = regs
->msr
& MSR_MASK
;
3191 regs
->gpr
[13] = (unsigned long) get_paca();
3192 regs
->nip
= (unsigned long) &system_call_common
;
3193 regs
->msr
= MSR_KERNEL
;
3203 regs
->nip
= truncate_if_32bit(regs
->msr
, regs
->nip
+ 4);
3206 NOKPROBE_SYMBOL(emulate_step
);