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 <linux/prefetch.h>
15 #include <asm/sstep.h>
16 #include <asm/processor.h>
17 #include <linux/uaccess.h>
18 #include <asm/cpu_has_feature.h>
19 #include <asm/cputable.h>
21 extern char system_call_common
[];
24 /* Bits in SRR1 that are copied from MSR */
25 #define MSR_MASK 0xffffffff87c0ffffUL
27 #define MSR_MASK 0x87c0ffff
31 #define XER_SO 0x80000000U
32 #define XER_OV 0x40000000U
33 #define XER_CA 0x20000000U
34 #define XER_OV32 0x00080000U
35 #define XER_CA32 0x00040000U
39 * Functions in ldstfp.S
41 extern void get_fpr(int rn
, double *p
);
42 extern void put_fpr(int rn
, const double *p
);
43 extern void get_vr(int rn
, __vector128
*p
);
44 extern void put_vr(int rn
, __vector128
*p
);
45 extern void load_vsrn(int vsr
, const void *p
);
46 extern void store_vsrn(int vsr
, void *p
);
47 extern void conv_sp_to_dp(const float *sp
, double *dp
);
48 extern void conv_dp_to_sp(const double *dp
, float *sp
);
55 extern int do_lq(unsigned long ea
, unsigned long *regs
);
56 extern int do_stq(unsigned long ea
, unsigned long val0
, unsigned long val1
);
57 extern int do_lqarx(unsigned long ea
, unsigned long *regs
);
58 extern int do_stqcx(unsigned long ea
, unsigned long val0
, unsigned long val1
,
62 #ifdef __LITTLE_ENDIAN__
71 * Emulate the truncation of 64 bit values in 32-bit mode.
73 static nokprobe_inline
unsigned long truncate_if_32bit(unsigned long msr
,
77 if ((msr
& MSR_64BIT
) == 0)
84 * Determine whether a conditional branch instruction would branch.
86 static nokprobe_inline
int branch_taken(unsigned int instr
,
87 const struct pt_regs
*regs
,
88 struct instruction_op
*op
)
90 unsigned int bo
= (instr
>> 21) & 0x1f;
94 /* decrement counter */
96 if (((bo
>> 1) & 1) ^ (regs
->ctr
== 1))
99 if ((bo
& 0x10) == 0) {
100 /* check bit from CR */
101 bi
= (instr
>> 16) & 0x1f;
102 if (((regs
->ccr
>> (31 - bi
)) & 1) != ((bo
>> 3) & 1))
108 static nokprobe_inline
long address_ok(struct pt_regs
*regs
,
109 unsigned long ea
, int nb
)
111 if (!user_mode(regs
))
113 if (__access_ok(ea
, nb
, USER_DS
))
115 if (__access_ok(ea
, 1, USER_DS
))
116 /* Access overlaps the end of the user region */
117 regs
->dar
= USER_DS
.seg
;
124 * Calculate effective address for a D-form instruction
126 static nokprobe_inline
unsigned long dform_ea(unsigned int instr
,
127 const struct pt_regs
*regs
)
132 ra
= (instr
>> 16) & 0x1f;
133 ea
= (signed short) instr
; /* sign-extend */
142 * Calculate effective address for a DS-form instruction
144 static nokprobe_inline
unsigned long dsform_ea(unsigned int instr
,
145 const struct pt_regs
*regs
)
150 ra
= (instr
>> 16) & 0x1f;
151 ea
= (signed short) (instr
& ~3); /* sign-extend */
159 * Calculate effective address for a DQ-form instruction
161 static nokprobe_inline
unsigned long dqform_ea(unsigned int instr
,
162 const struct pt_regs
*regs
)
167 ra
= (instr
>> 16) & 0x1f;
168 ea
= (signed short) (instr
& ~0xf); /* sign-extend */
174 #endif /* __powerpc64 */
177 * Calculate effective address for an X-form instruction
179 static nokprobe_inline
unsigned long xform_ea(unsigned int instr
,
180 const struct pt_regs
*regs
)
185 ra
= (instr
>> 16) & 0x1f;
186 rb
= (instr
>> 11) & 0x1f;
195 * Return the largest power of 2, not greater than sizeof(unsigned long),
196 * such that x is a multiple of it.
198 static nokprobe_inline
unsigned long max_align(unsigned long x
)
200 x
|= sizeof(unsigned long);
201 return x
& -x
; /* isolates rightmost bit */
204 static nokprobe_inline
unsigned long byterev_2(unsigned long x
)
206 return ((x
>> 8) & 0xff) | ((x
& 0xff) << 8);
209 static nokprobe_inline
unsigned long byterev_4(unsigned long x
)
211 return ((x
>> 24) & 0xff) | ((x
>> 8) & 0xff00) |
212 ((x
& 0xff00) << 8) | ((x
& 0xff) << 24);
216 static nokprobe_inline
unsigned long byterev_8(unsigned long x
)
218 return (byterev_4(x
) << 32) | byterev_4(x
>> 32);
222 static nokprobe_inline
void do_byte_reverse(void *ptr
, int nb
)
226 *(u16
*)ptr
= byterev_2(*(u16
*)ptr
);
229 *(u32
*)ptr
= byterev_4(*(u32
*)ptr
);
233 *(unsigned long *)ptr
= byterev_8(*(unsigned long *)ptr
);
236 unsigned long *up
= (unsigned long *)ptr
;
238 tmp
= byterev_8(up
[0]);
239 up
[0] = byterev_8(up
[1]);
249 static nokprobe_inline
int read_mem_aligned(unsigned long *dest
,
250 unsigned long ea
, int nb
,
251 struct pt_regs
*regs
)
258 err
= __get_user(x
, (unsigned char __user
*) ea
);
261 err
= __get_user(x
, (unsigned short __user
*) ea
);
264 err
= __get_user(x
, (unsigned int __user
*) ea
);
268 err
= __get_user(x
, (unsigned long __user
*) ea
);
280 * Copy from userspace to a buffer, using the largest possible
281 * aligned accesses, up to sizeof(long).
283 static int nokprobe_inline
copy_mem_in(u8
*dest
, unsigned long ea
, int nb
,
284 struct pt_regs
*regs
)
289 for (; nb
> 0; nb
-= c
) {
295 err
= __get_user(*dest
, (unsigned char __user
*) ea
);
298 err
= __get_user(*(u16
*)dest
,
299 (unsigned short __user
*) ea
);
302 err
= __get_user(*(u32
*)dest
,
303 (unsigned int __user
*) ea
);
307 err
= __get_user(*(unsigned long *)dest
,
308 (unsigned long __user
*) ea
);
322 static nokprobe_inline
int read_mem_unaligned(unsigned long *dest
,
323 unsigned long ea
, int nb
,
324 struct pt_regs
*regs
)
328 u8 b
[sizeof(unsigned long)];
334 i
= IS_BE
? sizeof(unsigned long) - nb
: 0;
335 err
= copy_mem_in(&u
.b
[i
], ea
, nb
, regs
);
342 * Read memory at address ea for nb bytes, return 0 for success
343 * or -EFAULT if an error occurred. N.B. nb must be 1, 2, 4 or 8.
344 * If nb < sizeof(long), the result is right-justified on BE systems.
346 static int read_mem(unsigned long *dest
, unsigned long ea
, int nb
,
347 struct pt_regs
*regs
)
349 if (!address_ok(regs
, ea
, nb
))
351 if ((ea
& (nb
- 1)) == 0)
352 return read_mem_aligned(dest
, ea
, nb
, regs
);
353 return read_mem_unaligned(dest
, ea
, nb
, regs
);
355 NOKPROBE_SYMBOL(read_mem
);
357 static nokprobe_inline
int write_mem_aligned(unsigned long val
,
358 unsigned long ea
, int nb
,
359 struct pt_regs
*regs
)
365 err
= __put_user(val
, (unsigned char __user
*) ea
);
368 err
= __put_user(val
, (unsigned short __user
*) ea
);
371 err
= __put_user(val
, (unsigned int __user
*) ea
);
375 err
= __put_user(val
, (unsigned long __user
*) ea
);
385 * Copy from a buffer to userspace, using the largest possible
386 * aligned accesses, up to sizeof(long).
388 static int nokprobe_inline
copy_mem_out(u8
*dest
, unsigned long ea
, int nb
,
389 struct pt_regs
*regs
)
394 for (; nb
> 0; nb
-= c
) {
400 err
= __put_user(*dest
, (unsigned char __user
*) ea
);
403 err
= __put_user(*(u16
*)dest
,
404 (unsigned short __user
*) ea
);
407 err
= __put_user(*(u32
*)dest
,
408 (unsigned int __user
*) ea
);
412 err
= __put_user(*(unsigned long *)dest
,
413 (unsigned long __user
*) ea
);
427 static nokprobe_inline
int write_mem_unaligned(unsigned long val
,
428 unsigned long ea
, int nb
,
429 struct pt_regs
*regs
)
433 u8 b
[sizeof(unsigned long)];
438 i
= IS_BE
? sizeof(unsigned long) - nb
: 0;
439 return copy_mem_out(&u
.b
[i
], ea
, nb
, regs
);
443 * Write memory at address ea for nb bytes, return 0 for success
444 * or -EFAULT if an error occurred. N.B. nb must be 1, 2, 4 or 8.
446 static int write_mem(unsigned long val
, unsigned long ea
, int nb
,
447 struct pt_regs
*regs
)
449 if (!address_ok(regs
, ea
, nb
))
451 if ((ea
& (nb
- 1)) == 0)
452 return write_mem_aligned(val
, ea
, nb
, regs
);
453 return write_mem_unaligned(val
, ea
, nb
, regs
);
455 NOKPROBE_SYMBOL(write_mem
);
457 #ifdef CONFIG_PPC_FPU
459 * These access either the real FP register or the image in the
460 * thread_struct, depending on regs->msr & MSR_FP.
462 static int do_fp_load(struct instruction_op
*op
, unsigned long ea
,
463 struct pt_regs
*regs
, bool cross_endian
)
472 u8 b
[2 * sizeof(double)];
475 nb
= GETSIZE(op
->type
);
476 if (!address_ok(regs
, ea
, nb
))
479 err
= copy_mem_in(u
.b
, ea
, nb
, regs
);
482 if (unlikely(cross_endian
)) {
483 do_byte_reverse(u
.b
, min(nb
, 8));
485 do_byte_reverse(&u
.b
[8], 8);
489 if (op
->type
& FPCONV
)
490 conv_sp_to_dp(&u
.f
, &u
.d
[0]);
491 else if (op
->type
& SIGNEXT
)
496 if (regs
->msr
& MSR_FP
)
497 put_fpr(rn
, &u
.d
[0]);
499 current
->thread
.TS_FPR(rn
) = u
.l
[0];
503 if (regs
->msr
& MSR_FP
)
504 put_fpr(rn
, &u
.d
[1]);
506 current
->thread
.TS_FPR(rn
) = u
.l
[1];
511 NOKPROBE_SYMBOL(do_fp_load
);
513 static int do_fp_store(struct instruction_op
*op
, unsigned long ea
,
514 struct pt_regs
*regs
, bool cross_endian
)
522 u8 b
[2 * sizeof(double)];
525 nb
= GETSIZE(op
->type
);
526 if (!address_ok(regs
, ea
, nb
))
530 if (regs
->msr
& MSR_FP
)
531 get_fpr(rn
, &u
.d
[0]);
533 u
.l
[0] = current
->thread
.TS_FPR(rn
);
535 if (op
->type
& FPCONV
)
536 conv_dp_to_sp(&u
.d
[0], &u
.f
);
542 if (regs
->msr
& MSR_FP
)
543 get_fpr(rn
, &u
.d
[1]);
545 u
.l
[1] = current
->thread
.TS_FPR(rn
);
548 if (unlikely(cross_endian
)) {
549 do_byte_reverse(u
.b
, min(nb
, 8));
551 do_byte_reverse(&u
.b
[8], 8);
553 return copy_mem_out(u
.b
, ea
, nb
, regs
);
555 NOKPROBE_SYMBOL(do_fp_store
);
558 #ifdef CONFIG_ALTIVEC
559 /* For Altivec/VMX, no need to worry about alignment */
560 static nokprobe_inline
int do_vec_load(int rn
, unsigned long ea
,
561 int size
, struct pt_regs
*regs
,
567 u8 b
[sizeof(__vector128
)];
570 if (!address_ok(regs
, ea
& ~0xfUL
, 16))
572 /* align to multiple of size */
574 err
= copy_mem_in(&u
.b
[ea
& 0xf], ea
, size
, regs
);
577 if (unlikely(cross_endian
))
578 do_byte_reverse(&u
.b
[ea
& 0xf], size
);
580 if (regs
->msr
& MSR_VEC
)
583 current
->thread
.vr_state
.vr
[rn
] = u
.v
;
588 static nokprobe_inline
int do_vec_store(int rn
, unsigned long ea
,
589 int size
, struct pt_regs
*regs
,
594 u8 b
[sizeof(__vector128
)];
597 if (!address_ok(regs
, ea
& ~0xfUL
, 16))
599 /* align to multiple of size */
603 if (regs
->msr
& MSR_VEC
)
606 u
.v
= current
->thread
.vr_state
.vr
[rn
];
608 if (unlikely(cross_endian
))
609 do_byte_reverse(&u
.b
[ea
& 0xf], size
);
610 return copy_mem_out(&u
.b
[ea
& 0xf], ea
, size
, regs
);
612 #endif /* CONFIG_ALTIVEC */
615 static nokprobe_inline
int emulate_lq(struct pt_regs
*regs
, unsigned long ea
,
616 int reg
, bool cross_endian
)
620 if (!address_ok(regs
, ea
, 16))
622 /* if aligned, should be atomic */
623 if ((ea
& 0xf) == 0) {
624 err
= do_lq(ea
, ®s
->gpr
[reg
]);
626 err
= read_mem(®s
->gpr
[reg
+ IS_LE
], ea
, 8, regs
);
628 err
= read_mem(®s
->gpr
[reg
+ IS_BE
], ea
+ 8, 8, regs
);
630 if (!err
&& unlikely(cross_endian
))
631 do_byte_reverse(®s
->gpr
[reg
], 16);
635 static nokprobe_inline
int emulate_stq(struct pt_regs
*regs
, unsigned long ea
,
636 int reg
, bool cross_endian
)
639 unsigned long vals
[2];
641 if (!address_ok(regs
, ea
, 16))
643 vals
[0] = regs
->gpr
[reg
];
644 vals
[1] = regs
->gpr
[reg
+ 1];
645 if (unlikely(cross_endian
))
646 do_byte_reverse(vals
, 16);
648 /* if aligned, should be atomic */
650 return do_stq(ea
, vals
[0], vals
[1]);
652 err
= write_mem(vals
[IS_LE
], ea
, 8, regs
);
654 err
= write_mem(vals
[IS_BE
], ea
+ 8, 8, regs
);
657 #endif /* __powerpc64 */
660 void emulate_vsx_load(struct instruction_op
*op
, union vsx_reg
*reg
,
661 const void *mem
, bool rev
)
665 const unsigned int *wp
;
666 const unsigned short *hp
;
667 const unsigned char *bp
;
669 size
= GETSIZE(op
->type
);
670 reg
->d
[0] = reg
->d
[1] = 0;
672 switch (op
->element_size
) {
674 /* whole vector; lxv[x] or lxvl[l] */
677 memcpy(reg
, mem
, size
);
678 if (IS_LE
&& (op
->vsx_flags
& VSX_LDLEFT
))
681 do_byte_reverse(reg
, 16);
684 /* scalar loads, lxvd2x, lxvdsx */
685 read_size
= (size
>= 8) ? 8 : size
;
686 i
= IS_LE
? 8 : 8 - read_size
;
687 memcpy(®
->b
[i
], mem
, read_size
);
689 do_byte_reverse(®
->b
[i
], 8);
691 if (op
->type
& SIGNEXT
) {
692 /* size == 4 is the only case here */
693 reg
->d
[IS_LE
] = (signed int) reg
->d
[IS_LE
];
694 } else if (op
->vsx_flags
& VSX_FPCONV
) {
696 conv_sp_to_dp(®
->fp
[1 + IS_LE
],
702 unsigned long v
= *(unsigned long *)(mem
+ 8);
703 reg
->d
[IS_BE
] = !rev
? v
: byterev_8(v
);
704 } else if (op
->vsx_flags
& VSX_SPLAT
)
705 reg
->d
[IS_BE
] = reg
->d
[IS_LE
];
711 for (j
= 0; j
< size
/ 4; ++j
) {
712 i
= IS_LE
? 3 - j
: j
;
713 reg
->w
[i
] = !rev
? *wp
++ : byterev_4(*wp
++);
715 if (op
->vsx_flags
& VSX_SPLAT
) {
716 u32 val
= reg
->w
[IS_LE
? 3 : 0];
718 i
= IS_LE
? 3 - j
: j
;
726 for (j
= 0; j
< size
/ 2; ++j
) {
727 i
= IS_LE
? 7 - j
: j
;
728 reg
->h
[i
] = !rev
? *hp
++ : byterev_2(*hp
++);
734 for (j
= 0; j
< size
; ++j
) {
735 i
= IS_LE
? 15 - j
: j
;
741 EXPORT_SYMBOL_GPL(emulate_vsx_load
);
742 NOKPROBE_SYMBOL(emulate_vsx_load
);
744 void emulate_vsx_store(struct instruction_op
*op
, const union vsx_reg
*reg
,
747 int size
, write_size
;
754 size
= GETSIZE(op
->type
);
756 switch (op
->element_size
) {
758 /* stxv, stxvx, stxvl, stxvll */
761 if (IS_LE
&& (op
->vsx_flags
& VSX_LDLEFT
))
764 /* reverse 16 bytes */
765 buf
.d
[0] = byterev_8(reg
->d
[1]);
766 buf
.d
[1] = byterev_8(reg
->d
[0]);
769 memcpy(mem
, reg
, size
);
772 /* scalar stores, stxvd2x */
773 write_size
= (size
>= 8) ? 8 : size
;
774 i
= IS_LE
? 8 : 8 - write_size
;
775 if (size
< 8 && op
->vsx_flags
& VSX_FPCONV
) {
776 buf
.d
[0] = buf
.d
[1] = 0;
778 conv_dp_to_sp(®
->dp
[IS_LE
], &buf
.fp
[1 + IS_LE
]);
782 memcpy(mem
, ®
->b
[i
], write_size
);
784 memcpy(mem
+ 8, ®
->d
[IS_BE
], 8);
786 do_byte_reverse(mem
, write_size
);
788 do_byte_reverse(mem
+ 8, 8);
794 for (j
= 0; j
< size
/ 4; ++j
) {
795 i
= IS_LE
? 3 - j
: j
;
796 *wp
++ = !rev
? reg
->w
[i
] : byterev_4(reg
->w
[i
]);
802 for (j
= 0; j
< size
/ 2; ++j
) {
803 i
= IS_LE
? 7 - j
: j
;
804 *hp
++ = !rev
? reg
->h
[i
] : byterev_2(reg
->h
[i
]);
810 for (j
= 0; j
< size
; ++j
) {
811 i
= IS_LE
? 15 - j
: j
;
817 EXPORT_SYMBOL_GPL(emulate_vsx_store
);
818 NOKPROBE_SYMBOL(emulate_vsx_store
);
820 static nokprobe_inline
int do_vsx_load(struct instruction_op
*op
,
821 unsigned long ea
, struct pt_regs
*regs
,
827 int size
= GETSIZE(op
->type
);
829 if (!address_ok(regs
, ea
, size
) || copy_mem_in(mem
, ea
, size
, regs
))
832 emulate_vsx_load(op
, &buf
, mem
, cross_endian
);
835 /* FP regs + extensions */
836 if (regs
->msr
& MSR_FP
) {
837 load_vsrn(reg
, &buf
);
839 current
->thread
.fp_state
.fpr
[reg
][0] = buf
.d
[0];
840 current
->thread
.fp_state
.fpr
[reg
][1] = buf
.d
[1];
843 if (regs
->msr
& MSR_VEC
)
844 load_vsrn(reg
, &buf
);
846 current
->thread
.vr_state
.vr
[reg
- 32] = buf
.v
;
852 static nokprobe_inline
int do_vsx_store(struct instruction_op
*op
,
853 unsigned long ea
, struct pt_regs
*regs
,
859 int size
= GETSIZE(op
->type
);
861 if (!address_ok(regs
, ea
, size
))
866 /* FP regs + extensions */
867 if (regs
->msr
& MSR_FP
) {
868 store_vsrn(reg
, &buf
);
870 buf
.d
[0] = current
->thread
.fp_state
.fpr
[reg
][0];
871 buf
.d
[1] = current
->thread
.fp_state
.fpr
[reg
][1];
874 if (regs
->msr
& MSR_VEC
)
875 store_vsrn(reg
, &buf
);
877 buf
.v
= current
->thread
.vr_state
.vr
[reg
- 32];
880 emulate_vsx_store(op
, &buf
, mem
, cross_endian
);
881 return copy_mem_out(mem
, ea
, size
, regs
);
883 #endif /* CONFIG_VSX */
885 int emulate_dcbz(unsigned long ea
, struct pt_regs
*regs
)
888 unsigned long i
, size
;
891 size
= ppc64_caches
.l1d
.block_size
;
892 if (!(regs
->msr
& MSR_64BIT
))
895 size
= L1_CACHE_BYTES
;
898 if (!address_ok(regs
, ea
, size
))
900 for (i
= 0; i
< size
; i
+= sizeof(long)) {
901 err
= __put_user(0, (unsigned long __user
*) (ea
+ i
));
909 NOKPROBE_SYMBOL(emulate_dcbz
);
911 #define __put_user_asmx(x, addr, err, op, cr) \
912 __asm__ __volatile__( \
913 "1: " op " %2,0,%3\n" \
916 ".section .fixup,\"ax\"\n" \
921 : "=r" (err), "=r" (cr) \
922 : "r" (x), "r" (addr), "i" (-EFAULT), "0" (err))
924 #define __get_user_asmx(x, addr, err, op) \
925 __asm__ __volatile__( \
926 "1: "op" %1,0,%2\n" \
928 ".section .fixup,\"ax\"\n" \
933 : "=r" (err), "=r" (x) \
934 : "r" (addr), "i" (-EFAULT), "0" (err))
936 #define __cacheop_user_asmx(addr, err, op) \
937 __asm__ __volatile__( \
940 ".section .fixup,\"ax\"\n" \
946 : "r" (addr), "i" (-EFAULT), "0" (err))
948 static nokprobe_inline
void set_cr0(const struct pt_regs
*regs
,
949 struct instruction_op
*op
)
954 op
->ccval
= (regs
->ccr
& 0x0fffffff) | ((regs
->xer
>> 3) & 0x10000000);
956 if (!(regs
->msr
& MSR_64BIT
))
960 op
->ccval
|= 0x80000000;
962 op
->ccval
|= 0x40000000;
964 op
->ccval
|= 0x20000000;
967 static nokprobe_inline
void set_ca32(struct instruction_op
*op
, bool val
)
969 if (cpu_has_feature(CPU_FTR_ARCH_300
)) {
971 op
->xerval
|= XER_CA32
;
973 op
->xerval
&= ~XER_CA32
;
977 static nokprobe_inline
void add_with_carry(const struct pt_regs
*regs
,
978 struct instruction_op
*op
, int rd
,
979 unsigned long val1
, unsigned long val2
,
980 unsigned long carry_in
)
982 unsigned long val
= val1
+ val2
;
986 op
->type
= COMPUTE
+ SETREG
+ SETXER
;
990 if (!(regs
->msr
& MSR_64BIT
)) {
991 val
= (unsigned int) val
;
992 val1
= (unsigned int) val1
;
995 op
->xerval
= regs
->xer
;
996 if (val
< val1
|| (carry_in
&& val
== val1
))
997 op
->xerval
|= XER_CA
;
999 op
->xerval
&= ~XER_CA
;
1001 set_ca32(op
, (unsigned int)val
< (unsigned int)val1
||
1002 (carry_in
&& (unsigned int)val
== (unsigned int)val1
));
1005 static nokprobe_inline
void do_cmp_signed(const struct pt_regs
*regs
,
1006 struct instruction_op
*op
,
1007 long v1
, long v2
, int crfld
)
1009 unsigned int crval
, shift
;
1011 op
->type
= COMPUTE
+ SETCC
;
1012 crval
= (regs
->xer
>> 31) & 1; /* get SO bit */
1019 shift
= (7 - crfld
) * 4;
1020 op
->ccval
= (regs
->ccr
& ~(0xf << shift
)) | (crval
<< shift
);
1023 static nokprobe_inline
void do_cmp_unsigned(const struct pt_regs
*regs
,
1024 struct instruction_op
*op
,
1026 unsigned long v2
, int crfld
)
1028 unsigned int crval
, shift
;
1030 op
->type
= COMPUTE
+ SETCC
;
1031 crval
= (regs
->xer
>> 31) & 1; /* get SO bit */
1038 shift
= (7 - crfld
) * 4;
1039 op
->ccval
= (regs
->ccr
& ~(0xf << shift
)) | (crval
<< shift
);
1042 static nokprobe_inline
void do_cmpb(const struct pt_regs
*regs
,
1043 struct instruction_op
*op
,
1044 unsigned long v1
, unsigned long v2
)
1046 unsigned long long out_val
, mask
;
1050 for (i
= 0; i
< 8; i
++) {
1051 mask
= 0xffUL
<< (i
* 8);
1052 if ((v1
& mask
) == (v2
& mask
))
1059 * The size parameter is used to adjust the equivalent popcnt instruction.
1060 * popcntb = 8, popcntw = 32, popcntd = 64
1062 static nokprobe_inline
void do_popcnt(const struct pt_regs
*regs
,
1063 struct instruction_op
*op
,
1064 unsigned long v1
, int size
)
1066 unsigned long long out
= v1
;
1068 out
-= (out
>> 1) & 0x5555555555555555;
1069 out
= (0x3333333333333333 & out
) + (0x3333333333333333 & (out
>> 2));
1070 out
= (out
+ (out
>> 4)) & 0x0f0f0f0f0f0f0f0f;
1072 if (size
== 8) { /* popcntb */
1078 if (size
== 32) { /* popcntw */
1079 op
->val
= out
& 0x0000003f0000003f;
1083 out
= (out
+ (out
>> 32)) & 0x7f;
1084 op
->val
= out
; /* popcntd */
1088 static nokprobe_inline
void do_bpermd(const struct pt_regs
*regs
,
1089 struct instruction_op
*op
,
1090 unsigned long v1
, unsigned long v2
)
1092 unsigned char perm
, idx
;
1096 for (i
= 0; i
< 8; i
++) {
1097 idx
= (v1
>> (i
* 8)) & 0xff;
1099 if (v2
& PPC_BIT(idx
))
1104 #endif /* CONFIG_PPC64 */
1106 * The size parameter adjusts the equivalent prty instruction.
1107 * prtyw = 32, prtyd = 64
1109 static nokprobe_inline
void do_prty(const struct pt_regs
*regs
,
1110 struct instruction_op
*op
,
1111 unsigned long v
, int size
)
1113 unsigned long long res
= v
^ (v
>> 8);
1116 if (size
== 32) { /* prtyw */
1117 op
->val
= res
& 0x0000000100000001;
1122 op
->val
= res
& 1; /*prtyd */
1125 static nokprobe_inline
int trap_compare(long v1
, long v2
)
1135 if ((unsigned long)v1
< (unsigned long)v2
)
1137 else if ((unsigned long)v1
> (unsigned long)v2
)
1143 * Elements of 32-bit rotate and mask instructions.
1145 #define MASK32(mb, me) ((0xffffffffUL >> (mb)) + \
1146 ((signed long)-0x80000000L >> (me)) + ((me) >= (mb)))
1147 #ifdef __powerpc64__
1148 #define MASK64_L(mb) (~0UL >> (mb))
1149 #define MASK64_R(me) ((signed long)-0x8000000000000000L >> (me))
1150 #define MASK64(mb, me) (MASK64_L(mb) + MASK64_R(me) + ((me) >= (mb)))
1151 #define DATA32(x) (((x) & 0xffffffffUL) | (((x) & 0xffffffffUL) << 32))
1153 #define DATA32(x) (x)
1155 #define ROTATE(x, n) ((n) ? (((x) << (n)) | ((x) >> (8 * sizeof(long) - (n)))) : (x))
1158 * Decode an instruction, and return information about it in *op
1159 * without changing *regs.
1160 * Integer arithmetic and logical instructions, branches, and barrier
1161 * instructions can be emulated just using the information in *op.
1163 * Return value is 1 if the instruction can be emulated just by
1164 * updating *regs with the information in *op, -1 if we need the
1165 * GPRs but *regs doesn't contain the full register set, or 0
1168 int analyse_instr(struct instruction_op
*op
, const struct pt_regs
*regs
,
1171 unsigned int opcode
, ra
, rb
, rd
, spr
, u
;
1172 unsigned long int imm
;
1173 unsigned long int val
, val2
;
1174 unsigned int mb
, me
, sh
;
1179 opcode
= instr
>> 26;
1183 imm
= (signed short)(instr
& 0xfffc);
1184 if ((instr
& 2) == 0)
1186 op
->val
= truncate_if_32bit(regs
->msr
, imm
);
1189 if (branch_taken(instr
, regs
, op
))
1190 op
->type
|= BRTAKEN
;
1194 if ((instr
& 0xfe2) == 2)
1201 op
->type
= BRANCH
| BRTAKEN
;
1202 imm
= instr
& 0x03fffffc;
1203 if (imm
& 0x02000000)
1205 if ((instr
& 2) == 0)
1207 op
->val
= truncate_if_32bit(regs
->msr
, imm
);
1212 switch ((instr
>> 1) & 0x3ff) {
1214 op
->type
= COMPUTE
+ SETCC
;
1215 rd
= 7 - ((instr
>> 23) & 0x7);
1216 ra
= 7 - ((instr
>> 18) & 0x7);
1219 val
= (regs
->ccr
>> ra
) & 0xf;
1220 op
->ccval
= (regs
->ccr
& ~(0xfUL
<< rd
)) | (val
<< rd
);
1224 case 528: /* bcctr */
1226 imm
= (instr
& 0x400)? regs
->ctr
: regs
->link
;
1227 op
->val
= truncate_if_32bit(regs
->msr
, imm
);
1230 if (branch_taken(instr
, regs
, op
))
1231 op
->type
|= BRTAKEN
;
1234 case 18: /* rfid, scary */
1235 if (regs
->msr
& MSR_PR
)
1240 case 150: /* isync */
1241 op
->type
= BARRIER
| BARRIER_ISYNC
;
1244 case 33: /* crnor */
1245 case 129: /* crandc */
1246 case 193: /* crxor */
1247 case 225: /* crnand */
1248 case 257: /* crand */
1249 case 289: /* creqv */
1250 case 417: /* crorc */
1251 case 449: /* cror */
1252 op
->type
= COMPUTE
+ SETCC
;
1253 ra
= (instr
>> 16) & 0x1f;
1254 rb
= (instr
>> 11) & 0x1f;
1255 rd
= (instr
>> 21) & 0x1f;
1256 ra
= (regs
->ccr
>> (31 - ra
)) & 1;
1257 rb
= (regs
->ccr
>> (31 - rb
)) & 1;
1258 val
= (instr
>> (6 + ra
* 2 + rb
)) & 1;
1259 op
->ccval
= (regs
->ccr
& ~(1UL << (31 - rd
))) |
1265 switch ((instr
>> 1) & 0x3ff) {
1266 case 598: /* sync */
1267 op
->type
= BARRIER
+ BARRIER_SYNC
;
1268 #ifdef __powerpc64__
1269 switch ((instr
>> 21) & 3) {
1270 case 1: /* lwsync */
1271 op
->type
= BARRIER
+ BARRIER_LWSYNC
;
1273 case 2: /* ptesync */
1274 op
->type
= BARRIER
+ BARRIER_PTESYNC
;
1280 case 854: /* eieio */
1281 op
->type
= BARRIER
+ BARRIER_EIEIO
;
1287 /* Following cases refer to regs->gpr[], so we need all regs */
1288 if (!FULL_REGS(regs
))
1291 rd
= (instr
>> 21) & 0x1f;
1292 ra
= (instr
>> 16) & 0x1f;
1293 rb
= (instr
>> 11) & 0x1f;
1296 #ifdef __powerpc64__
1298 if (rd
& trap_compare(regs
->gpr
[ra
], (short) instr
))
1303 if (rd
& trap_compare((int)regs
->gpr
[ra
], (short) instr
))
1308 op
->val
= regs
->gpr
[ra
] * (short) instr
;
1311 case 8: /* subfic */
1312 imm
= (short) instr
;
1313 add_with_carry(regs
, op
, rd
, ~regs
->gpr
[ra
], imm
, 1);
1316 case 10: /* cmpli */
1317 imm
= (unsigned short) instr
;
1318 val
= regs
->gpr
[ra
];
1319 #ifdef __powerpc64__
1321 val
= (unsigned int) val
;
1323 do_cmp_unsigned(regs
, op
, val
, imm
, rd
>> 2);
1327 imm
= (short) instr
;
1328 val
= regs
->gpr
[ra
];
1329 #ifdef __powerpc64__
1333 do_cmp_signed(regs
, op
, val
, imm
, rd
>> 2);
1336 case 12: /* addic */
1337 imm
= (short) instr
;
1338 add_with_carry(regs
, op
, rd
, regs
->gpr
[ra
], imm
, 0);
1341 case 13: /* addic. */
1342 imm
= (short) instr
;
1343 add_with_carry(regs
, op
, rd
, regs
->gpr
[ra
], imm
, 0);
1348 imm
= (short) instr
;
1350 imm
+= regs
->gpr
[ra
];
1354 case 15: /* addis */
1355 imm
= ((short) instr
) << 16;
1357 imm
+= regs
->gpr
[ra
];
1362 if (((instr
>> 1) & 0x1f) == 2) {
1364 imm
= (short) (instr
& 0xffc1); /* d0 + d2 fields */
1365 imm
|= (instr
>> 15) & 0x3e; /* d1 field */
1366 op
->val
= regs
->nip
+ (imm
<< 16) + 4;
1372 case 20: /* rlwimi */
1373 mb
= (instr
>> 6) & 0x1f;
1374 me
= (instr
>> 1) & 0x1f;
1375 val
= DATA32(regs
->gpr
[rd
]);
1376 imm
= MASK32(mb
, me
);
1377 op
->val
= (regs
->gpr
[ra
] & ~imm
) | (ROTATE(val
, rb
) & imm
);
1380 case 21: /* rlwinm */
1381 mb
= (instr
>> 6) & 0x1f;
1382 me
= (instr
>> 1) & 0x1f;
1383 val
= DATA32(regs
->gpr
[rd
]);
1384 op
->val
= ROTATE(val
, rb
) & MASK32(mb
, me
);
1387 case 23: /* rlwnm */
1388 mb
= (instr
>> 6) & 0x1f;
1389 me
= (instr
>> 1) & 0x1f;
1390 rb
= regs
->gpr
[rb
] & 0x1f;
1391 val
= DATA32(regs
->gpr
[rd
]);
1392 op
->val
= ROTATE(val
, rb
) & MASK32(mb
, me
);
1396 op
->val
= regs
->gpr
[rd
] | (unsigned short) instr
;
1397 goto logical_done_nocc
;
1400 imm
= (unsigned short) instr
;
1401 op
->val
= regs
->gpr
[rd
] | (imm
<< 16);
1402 goto logical_done_nocc
;
1405 op
->val
= regs
->gpr
[rd
] ^ (unsigned short) instr
;
1406 goto logical_done_nocc
;
1408 case 27: /* xoris */
1409 imm
= (unsigned short) instr
;
1410 op
->val
= regs
->gpr
[rd
] ^ (imm
<< 16);
1411 goto logical_done_nocc
;
1413 case 28: /* andi. */
1414 op
->val
= regs
->gpr
[rd
] & (unsigned short) instr
;
1416 goto logical_done_nocc
;
1418 case 29: /* andis. */
1419 imm
= (unsigned short) instr
;
1420 op
->val
= regs
->gpr
[rd
] & (imm
<< 16);
1422 goto logical_done_nocc
;
1424 #ifdef __powerpc64__
1426 mb
= ((instr
>> 6) & 0x1f) | (instr
& 0x20);
1427 val
= regs
->gpr
[rd
];
1428 if ((instr
& 0x10) == 0) {
1429 sh
= rb
| ((instr
& 2) << 4);
1430 val
= ROTATE(val
, sh
);
1431 switch ((instr
>> 2) & 3) {
1432 case 0: /* rldicl */
1433 val
&= MASK64_L(mb
);
1435 case 1: /* rldicr */
1436 val
&= MASK64_R(mb
);
1439 val
&= MASK64(mb
, 63 - sh
);
1441 case 3: /* rldimi */
1442 imm
= MASK64(mb
, 63 - sh
);
1443 val
= (regs
->gpr
[ra
] & ~imm
) |
1449 sh
= regs
->gpr
[rb
] & 0x3f;
1450 val
= ROTATE(val
, sh
);
1451 switch ((instr
>> 1) & 7) {
1453 op
->val
= val
& MASK64_L(mb
);
1456 op
->val
= val
& MASK64_R(mb
);
1461 op
->type
= UNKNOWN
; /* illegal instruction */
1465 /* isel occupies 32 minor opcodes */
1466 if (((instr
>> 1) & 0x1f) == 15) {
1467 mb
= (instr
>> 6) & 0x1f; /* bc field */
1468 val
= (regs
->ccr
>> (31 - mb
)) & 1;
1469 val2
= (ra
) ? regs
->gpr
[ra
] : 0;
1471 op
->val
= (val
) ? val2
: regs
->gpr
[rb
];
1475 switch ((instr
>> 1) & 0x3ff) {
1478 (rd
& trap_compare((int)regs
->gpr
[ra
],
1479 (int)regs
->gpr
[rb
])))
1482 #ifdef __powerpc64__
1484 if (rd
& trap_compare(regs
->gpr
[ra
], regs
->gpr
[rb
]))
1488 case 83: /* mfmsr */
1489 if (regs
->msr
& MSR_PR
)
1494 case 146: /* mtmsr */
1495 if (regs
->msr
& MSR_PR
)
1499 op
->val
= 0xffffffff & ~(MSR_ME
| MSR_LE
);
1502 case 178: /* mtmsrd */
1503 if (regs
->msr
& MSR_PR
)
1507 /* only MSR_EE and MSR_RI get changed if bit 15 set */
1508 /* mtmsrd doesn't change MSR_HV, MSR_ME or MSR_LE */
1509 imm
= (instr
& 0x10000)? 0x8002: 0xefffffffffffeffeUL
;
1516 if ((instr
>> 20) & 1) {
1518 for (sh
= 0; sh
< 8; ++sh
) {
1519 if (instr
& (0x80000 >> sh
))
1524 op
->val
= regs
->ccr
& imm
;
1527 case 144: /* mtcrf */
1528 op
->type
= COMPUTE
+ SETCC
;
1530 val
= regs
->gpr
[rd
];
1531 op
->ccval
= regs
->ccr
;
1532 for (sh
= 0; sh
< 8; ++sh
) {
1533 if (instr
& (0x80000 >> sh
))
1534 op
->ccval
= (op
->ccval
& ~imm
) |
1540 case 339: /* mfspr */
1541 spr
= ((instr
>> 16) & 0x1f) | ((instr
>> 6) & 0x3e0);
1545 if (spr
== SPRN_XER
|| spr
== SPRN_LR
||
1550 case 467: /* mtspr */
1551 spr
= ((instr
>> 16) & 0x1f) | ((instr
>> 6) & 0x3e0);
1553 op
->val
= regs
->gpr
[rd
];
1555 if (spr
== SPRN_XER
|| spr
== SPRN_LR
||
1561 * Compare instructions
1564 val
= regs
->gpr
[ra
];
1565 val2
= regs
->gpr
[rb
];
1566 #ifdef __powerpc64__
1567 if ((rd
& 1) == 0) {
1568 /* word (32-bit) compare */
1573 do_cmp_signed(regs
, op
, val
, val2
, rd
>> 2);
1577 val
= regs
->gpr
[ra
];
1578 val2
= regs
->gpr
[rb
];
1579 #ifdef __powerpc64__
1580 if ((rd
& 1) == 0) {
1581 /* word (32-bit) compare */
1582 val
= (unsigned int) val
;
1583 val2
= (unsigned int) val2
;
1586 do_cmp_unsigned(regs
, op
, val
, val2
, rd
>> 2);
1589 case 508: /* cmpb */
1590 do_cmpb(regs
, op
, regs
->gpr
[rd
], regs
->gpr
[rb
]);
1591 goto logical_done_nocc
;
1594 * Arithmetic instructions
1597 add_with_carry(regs
, op
, rd
, ~regs
->gpr
[ra
],
1600 #ifdef __powerpc64__
1601 case 9: /* mulhdu */
1602 asm("mulhdu %0,%1,%2" : "=r" (op
->val
) :
1603 "r" (regs
->gpr
[ra
]), "r" (regs
->gpr
[rb
]));
1607 add_with_carry(regs
, op
, rd
, regs
->gpr
[ra
],
1611 case 11: /* mulhwu */
1612 asm("mulhwu %0,%1,%2" : "=r" (op
->val
) :
1613 "r" (regs
->gpr
[ra
]), "r" (regs
->gpr
[rb
]));
1617 op
->val
= regs
->gpr
[rb
] - regs
->gpr
[ra
];
1619 #ifdef __powerpc64__
1620 case 73: /* mulhd */
1621 asm("mulhd %0,%1,%2" : "=r" (op
->val
) :
1622 "r" (regs
->gpr
[ra
]), "r" (regs
->gpr
[rb
]));
1625 case 75: /* mulhw */
1626 asm("mulhw %0,%1,%2" : "=r" (op
->val
) :
1627 "r" (regs
->gpr
[ra
]), "r" (regs
->gpr
[rb
]));
1631 op
->val
= -regs
->gpr
[ra
];
1634 case 136: /* subfe */
1635 add_with_carry(regs
, op
, rd
, ~regs
->gpr
[ra
],
1636 regs
->gpr
[rb
], regs
->xer
& XER_CA
);
1639 case 138: /* adde */
1640 add_with_carry(regs
, op
, rd
, regs
->gpr
[ra
],
1641 regs
->gpr
[rb
], regs
->xer
& XER_CA
);
1644 case 200: /* subfze */
1645 add_with_carry(regs
, op
, rd
, ~regs
->gpr
[ra
], 0L,
1646 regs
->xer
& XER_CA
);
1649 case 202: /* addze */
1650 add_with_carry(regs
, op
, rd
, regs
->gpr
[ra
], 0L,
1651 regs
->xer
& XER_CA
);
1654 case 232: /* subfme */
1655 add_with_carry(regs
, op
, rd
, ~regs
->gpr
[ra
], -1L,
1656 regs
->xer
& XER_CA
);
1658 #ifdef __powerpc64__
1659 case 233: /* mulld */
1660 op
->val
= regs
->gpr
[ra
] * regs
->gpr
[rb
];
1663 case 234: /* addme */
1664 add_with_carry(regs
, op
, rd
, regs
->gpr
[ra
], -1L,
1665 regs
->xer
& XER_CA
);
1668 case 235: /* mullw */
1669 op
->val
= (long)(int) regs
->gpr
[ra
] *
1670 (int) regs
->gpr
[rb
];
1675 op
->val
= regs
->gpr
[ra
] + regs
->gpr
[rb
];
1677 #ifdef __powerpc64__
1678 case 457: /* divdu */
1679 op
->val
= regs
->gpr
[ra
] / regs
->gpr
[rb
];
1682 case 459: /* divwu */
1683 op
->val
= (unsigned int) regs
->gpr
[ra
] /
1684 (unsigned int) regs
->gpr
[rb
];
1686 #ifdef __powerpc64__
1687 case 489: /* divd */
1688 op
->val
= (long int) regs
->gpr
[ra
] /
1689 (long int) regs
->gpr
[rb
];
1692 case 491: /* divw */
1693 op
->val
= (int) regs
->gpr
[ra
] /
1694 (int) regs
->gpr
[rb
];
1699 * Logical instructions
1701 case 26: /* cntlzw */
1702 val
= (unsigned int) regs
->gpr
[rd
];
1703 op
->val
= ( val
? __builtin_clz(val
) : 32 );
1705 #ifdef __powerpc64__
1706 case 58: /* cntlzd */
1707 val
= regs
->gpr
[rd
];
1708 op
->val
= ( val
? __builtin_clzl(val
) : 64 );
1712 op
->val
= regs
->gpr
[rd
] & regs
->gpr
[rb
];
1716 op
->val
= regs
->gpr
[rd
] & ~regs
->gpr
[rb
];
1719 case 122: /* popcntb */
1720 do_popcnt(regs
, op
, regs
->gpr
[rd
], 8);
1721 goto logical_done_nocc
;
1724 op
->val
= ~(regs
->gpr
[rd
] | regs
->gpr
[rb
]);
1727 case 154: /* prtyw */
1728 do_prty(regs
, op
, regs
->gpr
[rd
], 32);
1729 goto logical_done_nocc
;
1731 case 186: /* prtyd */
1732 do_prty(regs
, op
, regs
->gpr
[rd
], 64);
1733 goto logical_done_nocc
;
1735 case 252: /* bpermd */
1736 do_bpermd(regs
, op
, regs
->gpr
[rd
], regs
->gpr
[rb
]);
1737 goto logical_done_nocc
;
1740 op
->val
= ~(regs
->gpr
[rd
] ^ regs
->gpr
[rb
]);
1744 op
->val
= regs
->gpr
[rd
] ^ regs
->gpr
[rb
];
1747 case 378: /* popcntw */
1748 do_popcnt(regs
, op
, regs
->gpr
[rd
], 32);
1749 goto logical_done_nocc
;
1752 op
->val
= regs
->gpr
[rd
] | ~regs
->gpr
[rb
];
1756 op
->val
= regs
->gpr
[rd
] | regs
->gpr
[rb
];
1759 case 476: /* nand */
1760 op
->val
= ~(regs
->gpr
[rd
] & regs
->gpr
[rb
]);
1763 case 506: /* popcntd */
1764 do_popcnt(regs
, op
, regs
->gpr
[rd
], 64);
1765 goto logical_done_nocc
;
1767 case 922: /* extsh */
1768 op
->val
= (signed short) regs
->gpr
[rd
];
1771 case 954: /* extsb */
1772 op
->val
= (signed char) regs
->gpr
[rd
];
1774 #ifdef __powerpc64__
1775 case 986: /* extsw */
1776 op
->val
= (signed int) regs
->gpr
[rd
];
1781 * Shift instructions
1784 sh
= regs
->gpr
[rb
] & 0x3f;
1786 op
->val
= (regs
->gpr
[rd
] << sh
) & 0xffffffffUL
;
1792 sh
= regs
->gpr
[rb
] & 0x3f;
1794 op
->val
= (regs
->gpr
[rd
] & 0xffffffffUL
) >> sh
;
1799 case 792: /* sraw */
1800 op
->type
= COMPUTE
+ SETREG
+ SETXER
;
1801 sh
= regs
->gpr
[rb
] & 0x3f;
1802 ival
= (signed int) regs
->gpr
[rd
];
1803 op
->val
= ival
>> (sh
< 32 ? sh
: 31);
1804 op
->xerval
= regs
->xer
;
1805 if (ival
< 0 && (sh
>= 32 || (ival
& ((1ul << sh
) - 1)) != 0))
1806 op
->xerval
|= XER_CA
;
1808 op
->xerval
&= ~XER_CA
;
1809 set_ca32(op
, op
->xerval
& XER_CA
);
1812 case 824: /* srawi */
1813 op
->type
= COMPUTE
+ SETREG
+ SETXER
;
1815 ival
= (signed int) regs
->gpr
[rd
];
1816 op
->val
= ival
>> sh
;
1817 op
->xerval
= regs
->xer
;
1818 if (ival
< 0 && (ival
& ((1ul << sh
) - 1)) != 0)
1819 op
->xerval
|= XER_CA
;
1821 op
->xerval
&= ~XER_CA
;
1822 set_ca32(op
, op
->xerval
& XER_CA
);
1825 #ifdef __powerpc64__
1827 sh
= regs
->gpr
[rb
] & 0x7f;
1829 op
->val
= regs
->gpr
[rd
] << sh
;
1835 sh
= regs
->gpr
[rb
] & 0x7f;
1837 op
->val
= regs
->gpr
[rd
] >> sh
;
1842 case 794: /* srad */
1843 op
->type
= COMPUTE
+ SETREG
+ SETXER
;
1844 sh
= regs
->gpr
[rb
] & 0x7f;
1845 ival
= (signed long int) regs
->gpr
[rd
];
1846 op
->val
= ival
>> (sh
< 64 ? sh
: 63);
1847 op
->xerval
= regs
->xer
;
1848 if (ival
< 0 && (sh
>= 64 || (ival
& ((1ul << sh
) - 1)) != 0))
1849 op
->xerval
|= XER_CA
;
1851 op
->xerval
&= ~XER_CA
;
1852 set_ca32(op
, op
->xerval
& XER_CA
);
1855 case 826: /* sradi with sh_5 = 0 */
1856 case 827: /* sradi with sh_5 = 1 */
1857 op
->type
= COMPUTE
+ SETREG
+ SETXER
;
1858 sh
= rb
| ((instr
& 2) << 4);
1859 ival
= (signed long int) regs
->gpr
[rd
];
1860 op
->val
= ival
>> sh
;
1861 op
->xerval
= regs
->xer
;
1862 if (ival
< 0 && (ival
& ((1ul << sh
) - 1)) != 0)
1863 op
->xerval
|= XER_CA
;
1865 op
->xerval
&= ~XER_CA
;
1866 set_ca32(op
, op
->xerval
& XER_CA
);
1868 #endif /* __powerpc64__ */
1871 * Cache instructions
1873 case 54: /* dcbst */
1874 op
->type
= MKOP(CACHEOP
, DCBST
, 0);
1875 op
->ea
= xform_ea(instr
, regs
);
1879 op
->type
= MKOP(CACHEOP
, DCBF
, 0);
1880 op
->ea
= xform_ea(instr
, regs
);
1883 case 246: /* dcbtst */
1884 op
->type
= MKOP(CACHEOP
, DCBTST
, 0);
1885 op
->ea
= xform_ea(instr
, regs
);
1889 case 278: /* dcbt */
1890 op
->type
= MKOP(CACHEOP
, DCBTST
, 0);
1891 op
->ea
= xform_ea(instr
, regs
);
1895 case 982: /* icbi */
1896 op
->type
= MKOP(CACHEOP
, ICBI
, 0);
1897 op
->ea
= xform_ea(instr
, regs
);
1900 case 1014: /* dcbz */
1901 op
->type
= MKOP(CACHEOP
, DCBZ
, 0);
1902 op
->ea
= xform_ea(instr
, regs
);
1912 op
->update_reg
= ra
;
1914 op
->val
= regs
->gpr
[rd
];
1915 u
= (instr
>> 20) & UPDATE
;
1921 op
->ea
= xform_ea(instr
, regs
);
1922 switch ((instr
>> 1) & 0x3ff) {
1923 case 20: /* lwarx */
1924 op
->type
= MKOP(LARX
, 0, 4);
1927 case 150: /* stwcx. */
1928 op
->type
= MKOP(STCX
, 0, 4);
1931 #ifdef __powerpc64__
1932 case 84: /* ldarx */
1933 op
->type
= MKOP(LARX
, 0, 8);
1936 case 214: /* stdcx. */
1937 op
->type
= MKOP(STCX
, 0, 8);
1940 case 52: /* lbarx */
1941 op
->type
= MKOP(LARX
, 0, 1);
1944 case 694: /* stbcx. */
1945 op
->type
= MKOP(STCX
, 0, 1);
1948 case 116: /* lharx */
1949 op
->type
= MKOP(LARX
, 0, 2);
1952 case 726: /* sthcx. */
1953 op
->type
= MKOP(STCX
, 0, 2);
1956 case 276: /* lqarx */
1957 if (!((rd
& 1) || rd
== ra
|| rd
== rb
))
1958 op
->type
= MKOP(LARX
, 0, 16);
1961 case 182: /* stqcx. */
1963 op
->type
= MKOP(STCX
, 0, 16);
1968 case 55: /* lwzux */
1969 op
->type
= MKOP(LOAD
, u
, 4);
1973 case 119: /* lbzux */
1974 op
->type
= MKOP(LOAD
, u
, 1);
1977 #ifdef CONFIG_ALTIVEC
1979 * Note: for the load/store vector element instructions,
1980 * bits of the EA say which field of the VMX register to use.
1983 op
->type
= MKOP(LOAD_VMX
, 0, 1);
1984 op
->element_size
= 1;
1987 case 39: /* lvehx */
1988 op
->type
= MKOP(LOAD_VMX
, 0, 2);
1989 op
->element_size
= 2;
1992 case 71: /* lvewx */
1993 op
->type
= MKOP(LOAD_VMX
, 0, 4);
1994 op
->element_size
= 4;
1998 case 359: /* lvxl */
1999 op
->type
= MKOP(LOAD_VMX
, 0, 16);
2000 op
->element_size
= 16;
2003 case 135: /* stvebx */
2004 op
->type
= MKOP(STORE_VMX
, 0, 1);
2005 op
->element_size
= 1;
2008 case 167: /* stvehx */
2009 op
->type
= MKOP(STORE_VMX
, 0, 2);
2010 op
->element_size
= 2;
2013 case 199: /* stvewx */
2014 op
->type
= MKOP(STORE_VMX
, 0, 4);
2015 op
->element_size
= 4;
2018 case 231: /* stvx */
2019 case 487: /* stvxl */
2020 op
->type
= MKOP(STORE_VMX
, 0, 16);
2022 #endif /* CONFIG_ALTIVEC */
2024 #ifdef __powerpc64__
2027 op
->type
= MKOP(LOAD
, u
, 8);
2030 case 149: /* stdx */
2031 case 181: /* stdux */
2032 op
->type
= MKOP(STORE
, u
, 8);
2036 case 151: /* stwx */
2037 case 183: /* stwux */
2038 op
->type
= MKOP(STORE
, u
, 4);
2041 case 215: /* stbx */
2042 case 247: /* stbux */
2043 op
->type
= MKOP(STORE
, u
, 1);
2046 case 279: /* lhzx */
2047 case 311: /* lhzux */
2048 op
->type
= MKOP(LOAD
, u
, 2);
2051 #ifdef __powerpc64__
2052 case 341: /* lwax */
2053 case 373: /* lwaux */
2054 op
->type
= MKOP(LOAD
, SIGNEXT
| u
, 4);
2058 case 343: /* lhax */
2059 case 375: /* lhaux */
2060 op
->type
= MKOP(LOAD
, SIGNEXT
| u
, 2);
2063 case 407: /* sthx */
2064 case 439: /* sthux */
2065 op
->type
= MKOP(STORE
, u
, 2);
2068 #ifdef __powerpc64__
2069 case 532: /* ldbrx */
2070 op
->type
= MKOP(LOAD
, BYTEREV
, 8);
2074 case 533: /* lswx */
2075 op
->type
= MKOP(LOAD_MULTI
, 0, regs
->xer
& 0x7f);
2078 case 534: /* lwbrx */
2079 op
->type
= MKOP(LOAD
, BYTEREV
, 4);
2082 case 597: /* lswi */
2084 rb
= 32; /* # bytes to load */
2085 op
->type
= MKOP(LOAD_MULTI
, 0, rb
);
2086 op
->ea
= ra
? regs
->gpr
[ra
] : 0;
2089 #ifdef CONFIG_PPC_FPU
2090 case 535: /* lfsx */
2091 case 567: /* lfsux */
2092 op
->type
= MKOP(LOAD_FP
, u
| FPCONV
, 4);
2095 case 599: /* lfdx */
2096 case 631: /* lfdux */
2097 op
->type
= MKOP(LOAD_FP
, u
, 8);
2100 case 663: /* stfsx */
2101 case 695: /* stfsux */
2102 op
->type
= MKOP(STORE_FP
, u
| FPCONV
, 4);
2105 case 727: /* stfdx */
2106 case 759: /* stfdux */
2107 op
->type
= MKOP(STORE_FP
, u
, 8);
2110 #ifdef __powerpc64__
2111 case 791: /* lfdpx */
2112 op
->type
= MKOP(LOAD_FP
, 0, 16);
2115 case 855: /* lfiwax */
2116 op
->type
= MKOP(LOAD_FP
, SIGNEXT
, 4);
2119 case 887: /* lfiwzx */
2120 op
->type
= MKOP(LOAD_FP
, 0, 4);
2123 case 919: /* stfdpx */
2124 op
->type
= MKOP(STORE_FP
, 0, 16);
2127 case 983: /* stfiwx */
2128 op
->type
= MKOP(STORE_FP
, 0, 4);
2130 #endif /* __powerpc64 */
2131 #endif /* CONFIG_PPC_FPU */
2133 #ifdef __powerpc64__
2134 case 660: /* stdbrx */
2135 op
->type
= MKOP(STORE
, BYTEREV
, 8);
2136 op
->val
= byterev_8(regs
->gpr
[rd
]);
2140 case 661: /* stswx */
2141 op
->type
= MKOP(STORE_MULTI
, 0, regs
->xer
& 0x7f);
2144 case 662: /* stwbrx */
2145 op
->type
= MKOP(STORE
, BYTEREV
, 4);
2146 op
->val
= byterev_4(regs
->gpr
[rd
]);
2149 case 725: /* stswi */
2151 rb
= 32; /* # bytes to store */
2152 op
->type
= MKOP(STORE_MULTI
, 0, rb
);
2153 op
->ea
= ra
? regs
->gpr
[ra
] : 0;
2156 case 790: /* lhbrx */
2157 op
->type
= MKOP(LOAD
, BYTEREV
, 2);
2160 case 918: /* sthbrx */
2161 op
->type
= MKOP(STORE
, BYTEREV
, 2);
2162 op
->val
= byterev_2(regs
->gpr
[rd
]);
2166 case 12: /* lxsiwzx */
2167 op
->reg
= rd
| ((instr
& 1) << 5);
2168 op
->type
= MKOP(LOAD_VSX
, 0, 4);
2169 op
->element_size
= 8;
2172 case 76: /* lxsiwax */
2173 op
->reg
= rd
| ((instr
& 1) << 5);
2174 op
->type
= MKOP(LOAD_VSX
, SIGNEXT
, 4);
2175 op
->element_size
= 8;
2178 case 140: /* stxsiwx */
2179 op
->reg
= rd
| ((instr
& 1) << 5);
2180 op
->type
= MKOP(STORE_VSX
, 0, 4);
2181 op
->element_size
= 8;
2184 case 268: /* lxvx */
2185 op
->reg
= rd
| ((instr
& 1) << 5);
2186 op
->type
= MKOP(LOAD_VSX
, 0, 16);
2187 op
->element_size
= 16;
2188 op
->vsx_flags
= VSX_CHECK_VEC
;
2191 case 269: /* lxvl */
2192 case 301: { /* lxvll */
2194 op
->reg
= rd
| ((instr
& 1) << 5);
2195 op
->ea
= ra
? regs
->gpr
[ra
] : 0;
2196 nb
= regs
->gpr
[rb
] & 0xff;
2199 op
->type
= MKOP(LOAD_VSX
, 0, nb
);
2200 op
->element_size
= 16;
2201 op
->vsx_flags
= ((instr
& 0x20) ? VSX_LDLEFT
: 0) |
2205 case 332: /* lxvdsx */
2206 op
->reg
= rd
| ((instr
& 1) << 5);
2207 op
->type
= MKOP(LOAD_VSX
, 0, 8);
2208 op
->element_size
= 8;
2209 op
->vsx_flags
= VSX_SPLAT
;
2212 case 364: /* lxvwsx */
2213 op
->reg
= rd
| ((instr
& 1) << 5);
2214 op
->type
= MKOP(LOAD_VSX
, 0, 4);
2215 op
->element_size
= 4;
2216 op
->vsx_flags
= VSX_SPLAT
| VSX_CHECK_VEC
;
2219 case 396: /* stxvx */
2220 op
->reg
= rd
| ((instr
& 1) << 5);
2221 op
->type
= MKOP(STORE_VSX
, 0, 16);
2222 op
->element_size
= 16;
2223 op
->vsx_flags
= VSX_CHECK_VEC
;
2226 case 397: /* stxvl */
2227 case 429: { /* stxvll */
2229 op
->reg
= rd
| ((instr
& 1) << 5);
2230 op
->ea
= ra
? regs
->gpr
[ra
] : 0;
2231 nb
= regs
->gpr
[rb
] & 0xff;
2234 op
->type
= MKOP(STORE_VSX
, 0, nb
);
2235 op
->element_size
= 16;
2236 op
->vsx_flags
= ((instr
& 0x20) ? VSX_LDLEFT
: 0) |
2240 case 524: /* lxsspx */
2241 op
->reg
= rd
| ((instr
& 1) << 5);
2242 op
->type
= MKOP(LOAD_VSX
, 0, 4);
2243 op
->element_size
= 8;
2244 op
->vsx_flags
= VSX_FPCONV
;
2247 case 588: /* lxsdx */
2248 op
->reg
= rd
| ((instr
& 1) << 5);
2249 op
->type
= MKOP(LOAD_VSX
, 0, 8);
2250 op
->element_size
= 8;
2253 case 652: /* stxsspx */
2254 op
->reg
= rd
| ((instr
& 1) << 5);
2255 op
->type
= MKOP(STORE_VSX
, 0, 4);
2256 op
->element_size
= 8;
2257 op
->vsx_flags
= VSX_FPCONV
;
2260 case 716: /* stxsdx */
2261 op
->reg
= rd
| ((instr
& 1) << 5);
2262 op
->type
= MKOP(STORE_VSX
, 0, 8);
2263 op
->element_size
= 8;
2266 case 780: /* lxvw4x */
2267 op
->reg
= rd
| ((instr
& 1) << 5);
2268 op
->type
= MKOP(LOAD_VSX
, 0, 16);
2269 op
->element_size
= 4;
2272 case 781: /* lxsibzx */
2273 op
->reg
= rd
| ((instr
& 1) << 5);
2274 op
->type
= MKOP(LOAD_VSX
, 0, 1);
2275 op
->element_size
= 8;
2276 op
->vsx_flags
= VSX_CHECK_VEC
;
2279 case 812: /* lxvh8x */
2280 op
->reg
= rd
| ((instr
& 1) << 5);
2281 op
->type
= MKOP(LOAD_VSX
, 0, 16);
2282 op
->element_size
= 2;
2283 op
->vsx_flags
= VSX_CHECK_VEC
;
2286 case 813: /* lxsihzx */
2287 op
->reg
= rd
| ((instr
& 1) << 5);
2288 op
->type
= MKOP(LOAD_VSX
, 0, 2);
2289 op
->element_size
= 8;
2290 op
->vsx_flags
= VSX_CHECK_VEC
;
2293 case 844: /* lxvd2x */
2294 op
->reg
= rd
| ((instr
& 1) << 5);
2295 op
->type
= MKOP(LOAD_VSX
, 0, 16);
2296 op
->element_size
= 8;
2299 case 876: /* lxvb16x */
2300 op
->reg
= rd
| ((instr
& 1) << 5);
2301 op
->type
= MKOP(LOAD_VSX
, 0, 16);
2302 op
->element_size
= 1;
2303 op
->vsx_flags
= VSX_CHECK_VEC
;
2306 case 908: /* stxvw4x */
2307 op
->reg
= rd
| ((instr
& 1) << 5);
2308 op
->type
= MKOP(STORE_VSX
, 0, 16);
2309 op
->element_size
= 4;
2312 case 909: /* stxsibx */
2313 op
->reg
= rd
| ((instr
& 1) << 5);
2314 op
->type
= MKOP(STORE_VSX
, 0, 1);
2315 op
->element_size
= 8;
2316 op
->vsx_flags
= VSX_CHECK_VEC
;
2319 case 940: /* stxvh8x */
2320 op
->reg
= rd
| ((instr
& 1) << 5);
2321 op
->type
= MKOP(STORE_VSX
, 0, 16);
2322 op
->element_size
= 2;
2323 op
->vsx_flags
= VSX_CHECK_VEC
;
2326 case 941: /* stxsihx */
2327 op
->reg
= rd
| ((instr
& 1) << 5);
2328 op
->type
= MKOP(STORE_VSX
, 0, 2);
2329 op
->element_size
= 8;
2330 op
->vsx_flags
= VSX_CHECK_VEC
;
2333 case 972: /* stxvd2x */
2334 op
->reg
= rd
| ((instr
& 1) << 5);
2335 op
->type
= MKOP(STORE_VSX
, 0, 16);
2336 op
->element_size
= 8;
2339 case 1004: /* stxvb16x */
2340 op
->reg
= rd
| ((instr
& 1) << 5);
2341 op
->type
= MKOP(STORE_VSX
, 0, 16);
2342 op
->element_size
= 1;
2343 op
->vsx_flags
= VSX_CHECK_VEC
;
2346 #endif /* CONFIG_VSX */
2352 op
->type
= MKOP(LOAD
, u
, 4);
2353 op
->ea
= dform_ea(instr
, regs
);
2358 op
->type
= MKOP(LOAD
, u
, 1);
2359 op
->ea
= dform_ea(instr
, regs
);
2364 op
->type
= MKOP(STORE
, u
, 4);
2365 op
->ea
= dform_ea(instr
, regs
);
2370 op
->type
= MKOP(STORE
, u
, 1);
2371 op
->ea
= dform_ea(instr
, regs
);
2376 op
->type
= MKOP(LOAD
, u
, 2);
2377 op
->ea
= dform_ea(instr
, regs
);
2382 op
->type
= MKOP(LOAD
, SIGNEXT
| u
, 2);
2383 op
->ea
= dform_ea(instr
, regs
);
2388 op
->type
= MKOP(STORE
, u
, 2);
2389 op
->ea
= dform_ea(instr
, regs
);
2394 break; /* invalid form, ra in range to load */
2395 op
->type
= MKOP(LOAD_MULTI
, 0, 4 * (32 - rd
));
2396 op
->ea
= dform_ea(instr
, regs
);
2400 op
->type
= MKOP(STORE_MULTI
, 0, 4 * (32 - rd
));
2401 op
->ea
= dform_ea(instr
, regs
);
2404 #ifdef CONFIG_PPC_FPU
2407 op
->type
= MKOP(LOAD_FP
, u
| FPCONV
, 4);
2408 op
->ea
= dform_ea(instr
, regs
);
2413 op
->type
= MKOP(LOAD_FP
, u
, 8);
2414 op
->ea
= dform_ea(instr
, regs
);
2418 case 53: /* stfsu */
2419 op
->type
= MKOP(STORE_FP
, u
| FPCONV
, 4);
2420 op
->ea
= dform_ea(instr
, regs
);
2424 case 55: /* stfdu */
2425 op
->type
= MKOP(STORE_FP
, u
, 8);
2426 op
->ea
= dform_ea(instr
, regs
);
2430 #ifdef __powerpc64__
2432 if (!((rd
& 1) || (rd
== ra
)))
2433 op
->type
= MKOP(LOAD
, 0, 16);
2434 op
->ea
= dqform_ea(instr
, regs
);
2439 case 57: /* lfdp, lxsd, lxssp */
2440 op
->ea
= dsform_ea(instr
, regs
);
2441 switch (instr
& 3) {
2444 break; /* reg must be even */
2445 op
->type
= MKOP(LOAD_FP
, 0, 16);
2449 op
->type
= MKOP(LOAD_VSX
, 0, 8);
2450 op
->element_size
= 8;
2451 op
->vsx_flags
= VSX_CHECK_VEC
;
2455 op
->type
= MKOP(LOAD_VSX
, 0, 4);
2456 op
->element_size
= 8;
2457 op
->vsx_flags
= VSX_FPCONV
| VSX_CHECK_VEC
;
2461 #endif /* CONFIG_VSX */
2463 #ifdef __powerpc64__
2464 case 58: /* ld[u], lwa */
2465 op
->ea
= dsform_ea(instr
, regs
);
2466 switch (instr
& 3) {
2468 op
->type
= MKOP(LOAD
, 0, 8);
2471 op
->type
= MKOP(LOAD
, UPDATE
, 8);
2474 op
->type
= MKOP(LOAD
, SIGNEXT
, 4);
2481 case 61: /* stfdp, lxv, stxsd, stxssp, stxv */
2482 switch (instr
& 7) {
2483 case 0: /* stfdp with LSB of DS field = 0 */
2484 case 4: /* stfdp with LSB of DS field = 1 */
2485 op
->ea
= dsform_ea(instr
, regs
);
2486 op
->type
= MKOP(STORE_FP
, 0, 16);
2490 op
->ea
= dqform_ea(instr
, regs
);
2493 op
->type
= MKOP(LOAD_VSX
, 0, 16);
2494 op
->element_size
= 16;
2495 op
->vsx_flags
= VSX_CHECK_VEC
;
2498 case 2: /* stxsd with LSB of DS field = 0 */
2499 case 6: /* stxsd with LSB of DS field = 1 */
2500 op
->ea
= dsform_ea(instr
, regs
);
2502 op
->type
= MKOP(STORE_VSX
, 0, 8);
2503 op
->element_size
= 8;
2504 op
->vsx_flags
= VSX_CHECK_VEC
;
2507 case 3: /* stxssp with LSB of DS field = 0 */
2508 case 7: /* stxssp with LSB of DS field = 1 */
2509 op
->ea
= dsform_ea(instr
, regs
);
2511 op
->type
= MKOP(STORE_VSX
, 0, 4);
2512 op
->element_size
= 8;
2513 op
->vsx_flags
= VSX_FPCONV
| VSX_CHECK_VEC
;
2517 op
->ea
= dqform_ea(instr
, regs
);
2520 op
->type
= MKOP(STORE_VSX
, 0, 16);
2521 op
->element_size
= 16;
2522 op
->vsx_flags
= VSX_CHECK_VEC
;
2526 #endif /* CONFIG_VSX */
2528 #ifdef __powerpc64__
2529 case 62: /* std[u] */
2530 op
->ea
= dsform_ea(instr
, regs
);
2531 switch (instr
& 3) {
2533 op
->type
= MKOP(STORE
, 0, 8);
2536 op
->type
= MKOP(STORE
, UPDATE
, 8);
2540 op
->type
= MKOP(STORE
, 0, 16);
2544 #endif /* __powerpc64__ */
2566 op
->type
= INTERRUPT
| 0x700;
2567 op
->val
= SRR1_PROGPRIV
;
2571 op
->type
= INTERRUPT
| 0x700;
2572 op
->val
= SRR1_PROGTRAP
;
2575 EXPORT_SYMBOL_GPL(analyse_instr
);
2576 NOKPROBE_SYMBOL(analyse_instr
);
2579 * For PPC32 we always use stwu with r1 to change the stack pointer.
2580 * So this emulated store may corrupt the exception frame, now we
2581 * have to provide the exception frame trampoline, which is pushed
2582 * below the kprobed function stack. So we only update gpr[1] but
2583 * don't emulate the real store operation. We will do real store
2584 * operation safely in exception return code by checking this flag.
2586 static nokprobe_inline
int handle_stack_update(unsigned long ea
, struct pt_regs
*regs
)
2590 * Check if we will touch kernel stack overflow
2592 if (ea
- STACK_INT_FRAME_SIZE
<= current
->thread
.ksp_limit
) {
2593 printk(KERN_CRIT
"Can't kprobe this since kernel stack would overflow.\n");
2596 #endif /* CONFIG_PPC32 */
2598 * Check if we already set since that means we'll
2599 * lose the previous value.
2601 WARN_ON(test_thread_flag(TIF_EMULATE_STACK_STORE
));
2602 set_thread_flag(TIF_EMULATE_STACK_STORE
);
2606 static nokprobe_inline
void do_signext(unsigned long *valp
, int size
)
2610 *valp
= (signed short) *valp
;
2613 *valp
= (signed int) *valp
;
2618 static nokprobe_inline
void do_byterev(unsigned long *valp
, int size
)
2622 *valp
= byterev_2(*valp
);
2625 *valp
= byterev_4(*valp
);
2627 #ifdef __powerpc64__
2629 *valp
= byterev_8(*valp
);
2636 * Emulate an instruction that can be executed just by updating
2639 void emulate_update_regs(struct pt_regs
*regs
, struct instruction_op
*op
)
2641 unsigned long next_pc
;
2643 next_pc
= truncate_if_32bit(regs
->msr
, regs
->nip
+ 4);
2644 switch (op
->type
& INSTR_TYPE_MASK
) {
2646 if (op
->type
& SETREG
)
2647 regs
->gpr
[op
->reg
] = op
->val
;
2648 if (op
->type
& SETCC
)
2649 regs
->ccr
= op
->ccval
;
2650 if (op
->type
& SETXER
)
2651 regs
->xer
= op
->xerval
;
2655 if (op
->type
& SETLK
)
2656 regs
->link
= next_pc
;
2657 if (op
->type
& BRTAKEN
)
2659 if (op
->type
& DECCTR
)
2664 switch (op
->type
& BARRIER_MASK
) {
2674 case BARRIER_LWSYNC
:
2675 asm volatile("lwsync" : : : "memory");
2677 case BARRIER_PTESYNC
:
2678 asm volatile("ptesync" : : : "memory");
2686 regs
->gpr
[op
->reg
] = regs
->xer
& 0xffffffffUL
;
2689 regs
->gpr
[op
->reg
] = regs
->link
;
2692 regs
->gpr
[op
->reg
] = regs
->ctr
;
2702 regs
->xer
= op
->val
& 0xffffffffUL
;
2705 regs
->link
= op
->val
;
2708 regs
->ctr
= op
->val
;
2718 regs
->nip
= next_pc
;
2720 NOKPROBE_SYMBOL(emulate_update_regs
);
2723 * Emulate a previously-analysed load or store instruction.
2724 * Return values are:
2725 * 0 = instruction emulated successfully
2726 * -EFAULT = address out of range or access faulted (regs->dar
2727 * contains the faulting address)
2728 * -EACCES = misaligned access, instruction requires alignment
2729 * -EINVAL = unknown operation in *op
2731 int emulate_loadstore(struct pt_regs
*regs
, struct instruction_op
*op
)
2733 int err
, size
, type
;
2741 size
= GETSIZE(op
->type
);
2742 type
= op
->type
& INSTR_TYPE_MASK
;
2743 cross_endian
= (regs
->msr
& MSR_LE
) != (MSR_KERNEL
& MSR_LE
);
2744 ea
= truncate_if_32bit(regs
->msr
, op
->ea
);
2748 if (ea
& (size
- 1))
2749 return -EACCES
; /* can't handle misaligned */
2750 if (!address_ok(regs
, ea
, size
))
2755 #ifdef __powerpc64__
2757 __get_user_asmx(val
, ea
, err
, "lbarx");
2760 __get_user_asmx(val
, ea
, err
, "lharx");
2764 __get_user_asmx(val
, ea
, err
, "lwarx");
2766 #ifdef __powerpc64__
2768 __get_user_asmx(val
, ea
, err
, "ldarx");
2771 err
= do_lqarx(ea
, ®s
->gpr
[op
->reg
]);
2782 regs
->gpr
[op
->reg
] = val
;
2786 if (ea
& (size
- 1))
2787 return -EACCES
; /* can't handle misaligned */
2788 if (!address_ok(regs
, ea
, size
))
2792 #ifdef __powerpc64__
2794 __put_user_asmx(op
->val
, ea
, err
, "stbcx.", cr
);
2797 __put_user_asmx(op
->val
, ea
, err
, "stbcx.", cr
);
2801 __put_user_asmx(op
->val
, ea
, err
, "stwcx.", cr
);
2803 #ifdef __powerpc64__
2805 __put_user_asmx(op
->val
, ea
, err
, "stdcx.", cr
);
2808 err
= do_stqcx(ea
, regs
->gpr
[op
->reg
],
2809 regs
->gpr
[op
->reg
+ 1], &cr
);
2816 regs
->ccr
= (regs
->ccr
& 0x0fffffff) |
2818 ((regs
->xer
>> 3) & 0x10000000);
2824 #ifdef __powerpc64__
2826 err
= emulate_lq(regs
, ea
, op
->reg
, cross_endian
);
2830 err
= read_mem(®s
->gpr
[op
->reg
], ea
, size
, regs
);
2832 if (op
->type
& SIGNEXT
)
2833 do_signext(®s
->gpr
[op
->reg
], size
);
2834 if ((op
->type
& BYTEREV
) == (cross_endian
? 0 : BYTEREV
))
2835 do_byterev(®s
->gpr
[op
->reg
], size
);
2839 #ifdef CONFIG_PPC_FPU
2842 * If the instruction is in userspace, we can emulate it even
2843 * if the VMX state is not live, because we have the state
2844 * stored in the thread_struct. If the instruction is in
2845 * the kernel, we must not touch the state in the thread_struct.
2847 if (!(regs
->msr
& MSR_PR
) && !(regs
->msr
& MSR_FP
))
2849 err
= do_fp_load(op
, ea
, regs
, cross_endian
);
2852 #ifdef CONFIG_ALTIVEC
2854 if (!(regs
->msr
& MSR_PR
) && !(regs
->msr
& MSR_VEC
))
2856 err
= do_vec_load(op
->reg
, ea
, size
, regs
, cross_endian
);
2861 unsigned long msrbit
= MSR_VSX
;
2864 * Some VSX instructions check the MSR_VEC bit rather than MSR_VSX
2865 * when the target of the instruction is a vector register.
2867 if (op
->reg
>= 32 && (op
->vsx_flags
& VSX_CHECK_VEC
))
2869 if (!(regs
->msr
& MSR_PR
) && !(regs
->msr
& msrbit
))
2871 err
= do_vsx_load(op
, ea
, regs
, cross_endian
);
2876 if (!address_ok(regs
, ea
, size
))
2879 for (i
= 0; i
< size
; i
+= 4) {
2880 unsigned int v32
= 0;
2885 err
= copy_mem_in((u8
*) &v32
, ea
, nb
, regs
);
2888 if (unlikely(cross_endian
))
2889 v32
= byterev_4(v32
);
2890 regs
->gpr
[rd
] = v32
;
2892 /* reg number wraps from 31 to 0 for lsw[ix] */
2893 rd
= (rd
+ 1) & 0x1f;
2898 #ifdef __powerpc64__
2900 err
= emulate_stq(regs
, ea
, op
->reg
, cross_endian
);
2904 if ((op
->type
& UPDATE
) && size
== sizeof(long) &&
2905 op
->reg
== 1 && op
->update_reg
== 1 &&
2906 !(regs
->msr
& MSR_PR
) &&
2907 ea
>= regs
->gpr
[1] - STACK_INT_FRAME_SIZE
) {
2908 err
= handle_stack_update(ea
, regs
);
2911 if (unlikely(cross_endian
))
2912 do_byterev(&op
->val
, size
);
2913 err
= write_mem(op
->val
, ea
, size
, regs
);
2916 #ifdef CONFIG_PPC_FPU
2918 if (!(regs
->msr
& MSR_PR
) && !(regs
->msr
& MSR_FP
))
2920 err
= do_fp_store(op
, ea
, regs
, cross_endian
);
2923 #ifdef CONFIG_ALTIVEC
2925 if (!(regs
->msr
& MSR_PR
) && !(regs
->msr
& MSR_VEC
))
2927 err
= do_vec_store(op
->reg
, ea
, size
, regs
, cross_endian
);
2932 unsigned long msrbit
= MSR_VSX
;
2935 * Some VSX instructions check the MSR_VEC bit rather than MSR_VSX
2936 * when the target of the instruction is a vector register.
2938 if (op
->reg
>= 32 && (op
->vsx_flags
& VSX_CHECK_VEC
))
2940 if (!(regs
->msr
& MSR_PR
) && !(regs
->msr
& msrbit
))
2942 err
= do_vsx_store(op
, ea
, regs
, cross_endian
);
2947 if (!address_ok(regs
, ea
, size
))
2950 for (i
= 0; i
< size
; i
+= 4) {
2951 unsigned int v32
= regs
->gpr
[rd
];
2956 if (unlikely(cross_endian
))
2957 v32
= byterev_4(v32
);
2958 err
= copy_mem_out((u8
*) &v32
, ea
, nb
, regs
);
2962 /* reg number wraps from 31 to 0 for stsw[ix] */
2963 rd
= (rd
+ 1) & 0x1f;
2974 if (op
->type
& UPDATE
)
2975 regs
->gpr
[op
->update_reg
] = op
->ea
;
2979 NOKPROBE_SYMBOL(emulate_loadstore
);
2982 * Emulate instructions that cause a transfer of control,
2983 * loads and stores, and a few other instructions.
2984 * Returns 1 if the step was emulated, 0 if not,
2985 * or -1 if the instruction is one that should not be stepped,
2986 * such as an rfid, or a mtmsrd that would clear MSR_RI.
2988 int emulate_step(struct pt_regs
*regs
, unsigned int instr
)
2990 struct instruction_op op
;
2995 r
= analyse_instr(&op
, regs
, instr
);
2999 emulate_update_regs(regs
, &op
);
3004 type
= op
.type
& INSTR_TYPE_MASK
;
3006 if (OP_IS_LOAD_STORE(type
)) {
3007 err
= emulate_loadstore(regs
, &op
);
3015 ea
= truncate_if_32bit(regs
->msr
, op
.ea
);
3016 if (!address_ok(regs
, ea
, 8))
3018 switch (op
.type
& CACHEOP_MASK
) {
3020 __cacheop_user_asmx(ea
, err
, "dcbst");
3023 __cacheop_user_asmx(ea
, err
, "dcbf");
3027 prefetchw((void *) ea
);
3031 prefetch((void *) ea
);
3034 __cacheop_user_asmx(ea
, err
, "icbi");
3037 err
= emulate_dcbz(ea
, regs
);
3047 regs
->gpr
[op
.reg
] = regs
->msr
& MSR_MASK
;
3051 val
= regs
->gpr
[op
.reg
];
3052 if ((val
& MSR_RI
) == 0)
3053 /* can't step mtmsr[d] that would clear MSR_RI */
3055 /* here op.val is the mask of bits to change */
3056 regs
->msr
= (regs
->msr
& ~op
.val
) | (val
& op
.val
);
3060 case SYSCALL
: /* sc */
3062 * N.B. this uses knowledge about how the syscall
3063 * entry code works. If that is changed, this will
3064 * need to be changed also.
3066 if (regs
->gpr
[0] == 0x1ebe &&
3067 cpu_has_feature(CPU_FTR_REAL_LE
)) {
3068 regs
->msr
^= MSR_LE
;
3071 regs
->gpr
[9] = regs
->gpr
[13];
3072 regs
->gpr
[10] = MSR_KERNEL
;
3073 regs
->gpr
[11] = regs
->nip
+ 4;
3074 regs
->gpr
[12] = regs
->msr
& MSR_MASK
;
3075 regs
->gpr
[13] = (unsigned long) get_paca();
3076 regs
->nip
= (unsigned long) &system_call_common
;
3077 regs
->msr
= MSR_KERNEL
;
3087 regs
->nip
= truncate_if_32bit(regs
->msr
, regs
->nip
+ 4);
3090 NOKPROBE_SYMBOL(emulate_step
);