1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
16 #include <asm/disassemble.h>
18 extern char system_call_common[];
19 extern char system_call_vectored_emulate[];
22 /* Bits in SRR1 that are copied from MSR */
23 #define MSR_MASK 0xffffffff87c0ffffUL
25 #define MSR_MASK 0x87c0ffff
29 #define XER_SO 0x80000000U
30 #define XER_OV 0x40000000U
31 #define XER_CA 0x20000000U
32 #define XER_OV32 0x00080000U
33 #define XER_CA32 0x00040000U
37 * Functions in ldstfp.S
39 extern void get_fpr(int rn, double *p);
40 extern void put_fpr(int rn, const double *p);
41 extern void get_vr(int rn, __vector128 *p);
42 extern void put_vr(int rn, __vector128 *p);
43 extern void load_vsrn(int vsr, const void *p);
44 extern void store_vsrn(int vsr, void *p);
45 extern void conv_sp_to_dp(const float *sp, double *dp);
46 extern void conv_dp_to_sp(const double *dp, float *sp);
53 extern int do_lq(unsigned long ea, unsigned long *regs);
54 extern int do_stq(unsigned long ea, unsigned long val0, unsigned long val1);
55 extern int do_lqarx(unsigned long ea, unsigned long *regs);
56 extern int do_stqcx(unsigned long ea, unsigned long val0, unsigned long val1,
60 #ifdef __LITTLE_ENDIAN__
69 * Emulate the truncation of 64 bit values in 32-bit mode.
71 static nokprobe_inline unsigned long truncate_if_32bit(unsigned long msr,
75 if ((msr & MSR_64BIT) == 0)
82 * Determine whether a conditional branch instruction would branch.
84 static nokprobe_inline int branch_taken(unsigned int instr,
85 const struct pt_regs *regs,
86 struct instruction_op *op)
88 unsigned int bo = (instr >> 21) & 0x1f;
92 /* decrement counter */
94 if (((bo >> 1) & 1) ^ (regs->ctr == 1))
97 if ((bo & 0x10) == 0) {
98 /* check bit from CR */
99 bi = (instr >> 16) & 0x1f;
100 if (((regs->ccr >> (31 - bi)) & 1) != ((bo >> 3) & 1))
106 static nokprobe_inline long address_ok(struct pt_regs *regs,
107 unsigned long ea, int nb)
109 if (!user_mode(regs))
111 if (__access_ok(ea, nb))
113 if (__access_ok(ea, 1))
114 /* Access overlaps the end of the user region */
115 regs->dar = TASK_SIZE_MAX - 1;
122 * Calculate effective address for a D-form instruction
124 static nokprobe_inline unsigned long dform_ea(unsigned int instr,
125 const struct pt_regs *regs)
130 ra = (instr >> 16) & 0x1f;
131 ea = (signed short) instr; /* sign-extend */
140 * Calculate effective address for a DS-form instruction
142 static nokprobe_inline unsigned long dsform_ea(unsigned int instr,
143 const struct pt_regs *regs)
148 ra = (instr >> 16) & 0x1f;
149 ea = (signed short) (instr & ~3); /* sign-extend */
157 * Calculate effective address for a DQ-form instruction
159 static nokprobe_inline unsigned long dqform_ea(unsigned int instr,
160 const struct pt_regs *regs)
165 ra = (instr >> 16) & 0x1f;
166 ea = (signed short) (instr & ~0xf); /* sign-extend */
172 #endif /* __powerpc64 */
175 * Calculate effective address for an X-form instruction
177 static nokprobe_inline unsigned long xform_ea(unsigned int instr,
178 const struct pt_regs *regs)
183 ra = (instr >> 16) & 0x1f;
184 rb = (instr >> 11) & 0x1f;
193 * Calculate effective address for a MLS:D-form / 8LS:D-form
194 * prefixed instruction
196 static nokprobe_inline unsigned long mlsd_8lsd_ea(unsigned int instr,
198 const struct pt_regs *regs)
202 unsigned long ea, d0, d1, d;
204 prefix_r = GET_PREFIX_R(instr);
205 ra = GET_PREFIX_RA(suffix);
207 d0 = instr & 0x3ffff;
208 d1 = suffix & 0xffff;
212 * sign extend a 34 bit number
214 dd = (unsigned int)(d >> 2);
216 ea = (ea << 2) | (d & 0x3);
220 else if (!prefix_r && !ra)
221 ; /* Leave ea as is */
226 * (prefix_r && ra) is an invalid form. Should already be
227 * checked for by caller!
234 * Return the largest power of 2, not greater than sizeof(unsigned long),
235 * such that x is a multiple of it.
237 static nokprobe_inline unsigned long max_align(unsigned long x)
239 x |= sizeof(unsigned long);
240 return x & -x; /* isolates rightmost bit */
243 static nokprobe_inline unsigned long byterev_2(unsigned long x)
245 return ((x >> 8) & 0xff) | ((x & 0xff) << 8);
248 static nokprobe_inline unsigned long byterev_4(unsigned long x)
250 return ((x >> 24) & 0xff) | ((x >> 8) & 0xff00) |
251 ((x & 0xff00) << 8) | ((x & 0xff) << 24);
255 static nokprobe_inline unsigned long byterev_8(unsigned long x)
257 return (byterev_4(x) << 32) | byterev_4(x >> 32);
261 static nokprobe_inline void do_byte_reverse(void *ptr, int nb)
265 *(u16 *)ptr = byterev_2(*(u16 *)ptr);
268 *(u32 *)ptr = byterev_4(*(u32 *)ptr);
272 *(unsigned long *)ptr = byterev_8(*(unsigned long *)ptr);
275 unsigned long *up = (unsigned long *)ptr;
277 tmp = byterev_8(up[0]);
278 up[0] = byterev_8(up[1]);
288 static nokprobe_inline int read_mem_aligned(unsigned long *dest,
289 unsigned long ea, int nb,
290 struct pt_regs *regs)
297 err = __get_user(x, (unsigned char __user *) ea);
300 err = __get_user(x, (unsigned short __user *) ea);
303 err = __get_user(x, (unsigned int __user *) ea);
307 err = __get_user(x, (unsigned long __user *) ea);
319 * Copy from userspace to a buffer, using the largest possible
320 * aligned accesses, up to sizeof(long).
322 static nokprobe_inline int copy_mem_in(u8 *dest, unsigned long ea, int nb,
323 struct pt_regs *regs)
328 for (; nb > 0; nb -= c) {
334 err = __get_user(*dest, (unsigned char __user *) ea);
337 err = __get_user(*(u16 *)dest,
338 (unsigned short __user *) ea);
341 err = __get_user(*(u32 *)dest,
342 (unsigned int __user *) ea);
346 err = __get_user(*(unsigned long *)dest,
347 (unsigned long __user *) ea);
361 static nokprobe_inline int read_mem_unaligned(unsigned long *dest,
362 unsigned long ea, int nb,
363 struct pt_regs *regs)
367 u8 b[sizeof(unsigned long)];
373 i = IS_BE ? sizeof(unsigned long) - nb : 0;
374 err = copy_mem_in(&u.b[i], ea, nb, regs);
381 * Read memory at address ea for nb bytes, return 0 for success
382 * or -EFAULT if an error occurred. N.B. nb must be 1, 2, 4 or 8.
383 * If nb < sizeof(long), the result is right-justified on BE systems.
385 static int read_mem(unsigned long *dest, unsigned long ea, int nb,
386 struct pt_regs *regs)
388 if (!address_ok(regs, ea, nb))
390 if ((ea & (nb - 1)) == 0)
391 return read_mem_aligned(dest, ea, nb, regs);
392 return read_mem_unaligned(dest, ea, nb, regs);
394 NOKPROBE_SYMBOL(read_mem);
396 static nokprobe_inline int write_mem_aligned(unsigned long val,
397 unsigned long ea, int nb,
398 struct pt_regs *regs)
404 err = __put_user(val, (unsigned char __user *) ea);
407 err = __put_user(val, (unsigned short __user *) ea);
410 err = __put_user(val, (unsigned int __user *) ea);
414 err = __put_user(val, (unsigned long __user *) ea);
424 * Copy from a buffer to userspace, using the largest possible
425 * aligned accesses, up to sizeof(long).
427 static nokprobe_inline int copy_mem_out(u8 *dest, unsigned long ea, int nb,
428 struct pt_regs *regs)
433 for (; nb > 0; nb -= c) {
439 err = __put_user(*dest, (unsigned char __user *) ea);
442 err = __put_user(*(u16 *)dest,
443 (unsigned short __user *) ea);
446 err = __put_user(*(u32 *)dest,
447 (unsigned int __user *) ea);
451 err = __put_user(*(unsigned long *)dest,
452 (unsigned long __user *) ea);
466 static nokprobe_inline int write_mem_unaligned(unsigned long val,
467 unsigned long ea, int nb,
468 struct pt_regs *regs)
472 u8 b[sizeof(unsigned long)];
477 i = IS_BE ? sizeof(unsigned long) - nb : 0;
478 return copy_mem_out(&u.b[i], ea, nb, regs);
482 * Write memory at address ea for nb bytes, return 0 for success
483 * or -EFAULT if an error occurred. N.B. nb must be 1, 2, 4 or 8.
485 static int write_mem(unsigned long val, unsigned long ea, int nb,
486 struct pt_regs *regs)
488 if (!address_ok(regs, ea, nb))
490 if ((ea & (nb - 1)) == 0)
491 return write_mem_aligned(val, ea, nb, regs);
492 return write_mem_unaligned(val, ea, nb, regs);
494 NOKPROBE_SYMBOL(write_mem);
496 #ifdef CONFIG_PPC_FPU
498 * These access either the real FP register or the image in the
499 * thread_struct, depending on regs->msr & MSR_FP.
501 static int do_fp_load(struct instruction_op *op, unsigned long ea,
502 struct pt_regs *regs, bool cross_endian)
511 u8 b[2 * sizeof(double)];
514 nb = GETSIZE(op->type);
515 if (!address_ok(regs, ea, nb))
518 err = copy_mem_in(u.b, ea, nb, regs);
521 if (unlikely(cross_endian)) {
522 do_byte_reverse(u.b, min(nb, 8));
524 do_byte_reverse(&u.b[8], 8);
528 if (op->type & FPCONV)
529 conv_sp_to_dp(&u.f, &u.d[0]);
530 else if (op->type & SIGNEXT)
535 if (regs->msr & MSR_FP)
536 put_fpr(rn, &u.d[0]);
538 current->thread.TS_FPR(rn) = u.l[0];
542 if (regs->msr & MSR_FP)
543 put_fpr(rn, &u.d[1]);
545 current->thread.TS_FPR(rn) = u.l[1];
550 NOKPROBE_SYMBOL(do_fp_load);
552 static int do_fp_store(struct instruction_op *op, unsigned long ea,
553 struct pt_regs *regs, bool cross_endian)
561 u8 b[2 * sizeof(double)];
564 nb = GETSIZE(op->type);
565 if (!address_ok(regs, ea, nb))
569 if (regs->msr & MSR_FP)
570 get_fpr(rn, &u.d[0]);
572 u.l[0] = current->thread.TS_FPR(rn);
574 if (op->type & FPCONV)
575 conv_dp_to_sp(&u.d[0], &u.f);
581 if (regs->msr & MSR_FP)
582 get_fpr(rn, &u.d[1]);
584 u.l[1] = current->thread.TS_FPR(rn);
587 if (unlikely(cross_endian)) {
588 do_byte_reverse(u.b, min(nb, 8));
590 do_byte_reverse(&u.b[8], 8);
592 return copy_mem_out(u.b, ea, nb, regs);
594 NOKPROBE_SYMBOL(do_fp_store);
597 #ifdef CONFIG_ALTIVEC
598 /* For Altivec/VMX, no need to worry about alignment */
599 static nokprobe_inline int do_vec_load(int rn, unsigned long ea,
600 int size, struct pt_regs *regs,
606 u8 b[sizeof(__vector128)];
609 if (!address_ok(regs, ea & ~0xfUL, 16))
611 /* align to multiple of size */
613 err = copy_mem_in(&u.b[ea & 0xf], ea, size, regs);
616 if (unlikely(cross_endian))
617 do_byte_reverse(&u.b[ea & 0xf], size);
619 if (regs->msr & MSR_VEC)
622 current->thread.vr_state.vr[rn] = u.v;
627 static nokprobe_inline int do_vec_store(int rn, unsigned long ea,
628 int size, struct pt_regs *regs,
633 u8 b[sizeof(__vector128)];
636 if (!address_ok(regs, ea & ~0xfUL, 16))
638 /* align to multiple of size */
642 if (regs->msr & MSR_VEC)
645 u.v = current->thread.vr_state.vr[rn];
647 if (unlikely(cross_endian))
648 do_byte_reverse(&u.b[ea & 0xf], size);
649 return copy_mem_out(&u.b[ea & 0xf], ea, size, regs);
651 #endif /* CONFIG_ALTIVEC */
654 static nokprobe_inline int emulate_lq(struct pt_regs *regs, unsigned long ea,
655 int reg, bool cross_endian)
659 if (!address_ok(regs, ea, 16))
661 /* if aligned, should be atomic */
662 if ((ea & 0xf) == 0) {
663 err = do_lq(ea, ®s->gpr[reg]);
665 err = read_mem(®s->gpr[reg + IS_LE], ea, 8, regs);
667 err = read_mem(®s->gpr[reg + IS_BE], ea + 8, 8, regs);
669 if (!err && unlikely(cross_endian))
670 do_byte_reverse(®s->gpr[reg], 16);
674 static nokprobe_inline int emulate_stq(struct pt_regs *regs, unsigned long ea,
675 int reg, bool cross_endian)
678 unsigned long vals[2];
680 if (!address_ok(regs, ea, 16))
682 vals[0] = regs->gpr[reg];
683 vals[1] = regs->gpr[reg + 1];
684 if (unlikely(cross_endian))
685 do_byte_reverse(vals, 16);
687 /* if aligned, should be atomic */
689 return do_stq(ea, vals[0], vals[1]);
691 err = write_mem(vals[IS_LE], ea, 8, regs);
693 err = write_mem(vals[IS_BE], ea + 8, 8, regs);
696 #endif /* __powerpc64 */
699 void emulate_vsx_load(struct instruction_op *op, union vsx_reg *reg,
700 const void *mem, bool rev)
704 const unsigned int *wp;
705 const unsigned short *hp;
706 const unsigned char *bp;
708 size = GETSIZE(op->type);
709 reg->d[0] = reg->d[1] = 0;
711 switch (op->element_size) {
713 /* whole vector; lxv[x] or lxvl[l] */
716 memcpy(reg, mem, size);
717 if (IS_LE && (op->vsx_flags & VSX_LDLEFT))
720 do_byte_reverse(reg, 16);
723 /* scalar loads, lxvd2x, lxvdsx */
724 read_size = (size >= 8) ? 8 : size;
725 i = IS_LE ? 8 : 8 - read_size;
726 memcpy(®->b[i], mem, read_size);
728 do_byte_reverse(®->b[i], 8);
730 if (op->type & SIGNEXT) {
731 /* size == 4 is the only case here */
732 reg->d[IS_LE] = (signed int) reg->d[IS_LE];
733 } else if (op->vsx_flags & VSX_FPCONV) {
735 conv_sp_to_dp(®->fp[1 + IS_LE],
741 unsigned long v = *(unsigned long *)(mem + 8);
742 reg->d[IS_BE] = !rev ? v : byterev_8(v);
743 } else if (op->vsx_flags & VSX_SPLAT)
744 reg->d[IS_BE] = reg->d[IS_LE];
750 for (j = 0; j < size / 4; ++j) {
751 i = IS_LE ? 3 - j : j;
752 reg->w[i] = !rev ? *wp++ : byterev_4(*wp++);
754 if (op->vsx_flags & VSX_SPLAT) {
755 u32 val = reg->w[IS_LE ? 3 : 0];
757 i = IS_LE ? 3 - j : j;
765 for (j = 0; j < size / 2; ++j) {
766 i = IS_LE ? 7 - j : j;
767 reg->h[i] = !rev ? *hp++ : byterev_2(*hp++);
773 for (j = 0; j < size; ++j) {
774 i = IS_LE ? 15 - j : j;
780 EXPORT_SYMBOL_GPL(emulate_vsx_load);
781 NOKPROBE_SYMBOL(emulate_vsx_load);
783 void emulate_vsx_store(struct instruction_op *op, const union vsx_reg *reg,
786 int size, write_size;
793 size = GETSIZE(op->type);
795 switch (op->element_size) {
797 /* stxv, stxvx, stxvl, stxvll */
800 if (IS_LE && (op->vsx_flags & VSX_LDLEFT))
803 /* reverse 16 bytes */
804 buf.d[0] = byterev_8(reg->d[1]);
805 buf.d[1] = byterev_8(reg->d[0]);
808 memcpy(mem, reg, size);
811 /* scalar stores, stxvd2x */
812 write_size = (size >= 8) ? 8 : size;
813 i = IS_LE ? 8 : 8 - write_size;
814 if (size < 8 && op->vsx_flags & VSX_FPCONV) {
815 buf.d[0] = buf.d[1] = 0;
817 conv_dp_to_sp(®->dp[IS_LE], &buf.fp[1 + IS_LE]);
821 memcpy(mem, ®->b[i], write_size);
823 memcpy(mem + 8, ®->d[IS_BE], 8);
825 do_byte_reverse(mem, write_size);
827 do_byte_reverse(mem + 8, 8);
833 for (j = 0; j < size / 4; ++j) {
834 i = IS_LE ? 3 - j : j;
835 *wp++ = !rev ? reg->w[i] : byterev_4(reg->w[i]);
841 for (j = 0; j < size / 2; ++j) {
842 i = IS_LE ? 7 - j : j;
843 *hp++ = !rev ? reg->h[i] : byterev_2(reg->h[i]);
849 for (j = 0; j < size; ++j) {
850 i = IS_LE ? 15 - j : j;
856 EXPORT_SYMBOL_GPL(emulate_vsx_store);
857 NOKPROBE_SYMBOL(emulate_vsx_store);
859 static nokprobe_inline int do_vsx_load(struct instruction_op *op,
860 unsigned long ea, struct pt_regs *regs,
866 int size = GETSIZE(op->type);
868 if (!address_ok(regs, ea, size) || copy_mem_in(mem, ea, size, regs))
871 emulate_vsx_load(op, &buf, mem, cross_endian);
874 /* FP regs + extensions */
875 if (regs->msr & MSR_FP) {
876 load_vsrn(reg, &buf);
878 current->thread.fp_state.fpr[reg][0] = buf.d[0];
879 current->thread.fp_state.fpr[reg][1] = buf.d[1];
882 if (regs->msr & MSR_VEC)
883 load_vsrn(reg, &buf);
885 current->thread.vr_state.vr[reg - 32] = buf.v;
891 static nokprobe_inline int do_vsx_store(struct instruction_op *op,
892 unsigned long ea, struct pt_regs *regs,
898 int size = GETSIZE(op->type);
900 if (!address_ok(regs, ea, size))
905 /* FP regs + extensions */
906 if (regs->msr & MSR_FP) {
907 store_vsrn(reg, &buf);
909 buf.d[0] = current->thread.fp_state.fpr[reg][0];
910 buf.d[1] = current->thread.fp_state.fpr[reg][1];
913 if (regs->msr & MSR_VEC)
914 store_vsrn(reg, &buf);
916 buf.v = current->thread.vr_state.vr[reg - 32];
919 emulate_vsx_store(op, &buf, mem, cross_endian);
920 return copy_mem_out(mem, ea, size, regs);
922 #endif /* CONFIG_VSX */
924 int emulate_dcbz(unsigned long ea, struct pt_regs *regs)
927 unsigned long i, size;
930 size = ppc64_caches.l1d.block_size;
931 if (!(regs->msr & MSR_64BIT))
934 size = L1_CACHE_BYTES;
937 if (!address_ok(regs, ea, size))
939 for (i = 0; i < size; i += sizeof(long)) {
940 err = __put_user(0, (unsigned long __user *) (ea + i));
948 NOKPROBE_SYMBOL(emulate_dcbz);
950 #define __put_user_asmx(x, addr, err, op, cr) \
951 __asm__ __volatile__( \
952 "1: " op " %2,0,%3\n" \
955 ".section .fixup,\"ax\"\n" \
960 : "=r" (err), "=r" (cr) \
961 : "r" (x), "r" (addr), "i" (-EFAULT), "0" (err))
963 #define __get_user_asmx(x, addr, err, op) \
964 __asm__ __volatile__( \
965 "1: "op" %1,0,%2\n" \
967 ".section .fixup,\"ax\"\n" \
972 : "=r" (err), "=r" (x) \
973 : "r" (addr), "i" (-EFAULT), "0" (err))
975 #define __cacheop_user_asmx(addr, err, op) \
976 __asm__ __volatile__( \
979 ".section .fixup,\"ax\"\n" \
985 : "r" (addr), "i" (-EFAULT), "0" (err))
987 static nokprobe_inline void set_cr0(const struct pt_regs *regs,
988 struct instruction_op *op)
993 op->ccval = (regs->ccr & 0x0fffffff) | ((regs->xer >> 3) & 0x10000000);
995 if (!(regs->msr & MSR_64BIT))
999 op->ccval |= 0x80000000;
1001 op->ccval |= 0x40000000;
1003 op->ccval |= 0x20000000;
1006 static nokprobe_inline void set_ca32(struct instruction_op *op, bool val)
1008 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
1010 op->xerval |= XER_CA32;
1012 op->xerval &= ~XER_CA32;
1016 static nokprobe_inline void add_with_carry(const struct pt_regs *regs,
1017 struct instruction_op *op, int rd,
1018 unsigned long val1, unsigned long val2,
1019 unsigned long carry_in)
1021 unsigned long val = val1 + val2;
1025 op->type = COMPUTE + SETREG + SETXER;
1028 #ifdef __powerpc64__
1029 if (!(regs->msr & MSR_64BIT)) {
1030 val = (unsigned int) val;
1031 val1 = (unsigned int) val1;
1034 op->xerval = regs->xer;
1035 if (val < val1 || (carry_in && val == val1))
1036 op->xerval |= XER_CA;
1038 op->xerval &= ~XER_CA;
1040 set_ca32(op, (unsigned int)val < (unsigned int)val1 ||
1041 (carry_in && (unsigned int)val == (unsigned int)val1));
1044 static nokprobe_inline void do_cmp_signed(const struct pt_regs *regs,
1045 struct instruction_op *op,
1046 long v1, long v2, int crfld)
1048 unsigned int crval, shift;
1050 op->type = COMPUTE + SETCC;
1051 crval = (regs->xer >> 31) & 1; /* get SO bit */
1058 shift = (7 - crfld) * 4;
1059 op->ccval = (regs->ccr & ~(0xf << shift)) | (crval << shift);
1062 static nokprobe_inline void do_cmp_unsigned(const struct pt_regs *regs,
1063 struct instruction_op *op,
1065 unsigned long v2, int crfld)
1067 unsigned int crval, shift;
1069 op->type = COMPUTE + SETCC;
1070 crval = (regs->xer >> 31) & 1; /* get SO bit */
1077 shift = (7 - crfld) * 4;
1078 op->ccval = (regs->ccr & ~(0xf << shift)) | (crval << shift);
1081 static nokprobe_inline void do_cmpb(const struct pt_regs *regs,
1082 struct instruction_op *op,
1083 unsigned long v1, unsigned long v2)
1085 unsigned long long out_val, mask;
1089 for (i = 0; i < 8; i++) {
1090 mask = 0xffUL << (i * 8);
1091 if ((v1 & mask) == (v2 & mask))
1098 * The size parameter is used to adjust the equivalent popcnt instruction.
1099 * popcntb = 8, popcntw = 32, popcntd = 64
1101 static nokprobe_inline void do_popcnt(const struct pt_regs *regs,
1102 struct instruction_op *op,
1103 unsigned long v1, int size)
1105 unsigned long long out = v1;
1107 out -= (out >> 1) & 0x5555555555555555ULL;
1108 out = (0x3333333333333333ULL & out) +
1109 (0x3333333333333333ULL & (out >> 2));
1110 out = (out + (out >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
1112 if (size == 8) { /* popcntb */
1118 if (size == 32) { /* popcntw */
1119 op->val = out & 0x0000003f0000003fULL;
1123 out = (out + (out >> 32)) & 0x7f;
1124 op->val = out; /* popcntd */
1128 static nokprobe_inline void do_bpermd(const struct pt_regs *regs,
1129 struct instruction_op *op,
1130 unsigned long v1, unsigned long v2)
1132 unsigned char perm, idx;
1136 for (i = 0; i < 8; i++) {
1137 idx = (v1 >> (i * 8)) & 0xff;
1139 if (v2 & PPC_BIT(idx))
1144 #endif /* CONFIG_PPC64 */
1146 * The size parameter adjusts the equivalent prty instruction.
1147 * prtyw = 32, prtyd = 64
1149 static nokprobe_inline void do_prty(const struct pt_regs *regs,
1150 struct instruction_op *op,
1151 unsigned long v, int size)
1153 unsigned long long res = v ^ (v >> 8);
1156 if (size == 32) { /* prtyw */
1157 op->val = res & 0x0000000100000001ULL;
1162 op->val = res & 1; /*prtyd */
1165 static nokprobe_inline int trap_compare(long v1, long v2)
1175 if ((unsigned long)v1 < (unsigned long)v2)
1177 else if ((unsigned long)v1 > (unsigned long)v2)
1183 * Elements of 32-bit rotate and mask instructions.
1185 #define MASK32(mb, me) ((0xffffffffUL >> (mb)) + \
1186 ((signed long)-0x80000000L >> (me)) + ((me) >= (mb)))
1187 #ifdef __powerpc64__
1188 #define MASK64_L(mb) (~0UL >> (mb))
1189 #define MASK64_R(me) ((signed long)-0x8000000000000000L >> (me))
1190 #define MASK64(mb, me) (MASK64_L(mb) + MASK64_R(me) + ((me) >= (mb)))
1191 #define DATA32(x) (((x) & 0xffffffffUL) | (((x) & 0xffffffffUL) << 32))
1193 #define DATA32(x) (x)
1195 #define ROTATE(x, n) ((n) ? (((x) << (n)) | ((x) >> (8 * sizeof(long) - (n)))) : (x))
1198 * Decode an instruction, and return information about it in *op
1199 * without changing *regs.
1200 * Integer arithmetic and logical instructions, branches, and barrier
1201 * instructions can be emulated just using the information in *op.
1203 * Return value is 1 if the instruction can be emulated just by
1204 * updating *regs with the information in *op, -1 if we need the
1205 * GPRs but *regs doesn't contain the full register set, or 0
1208 int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
1209 struct ppc_inst instr)
1212 unsigned int suffixopcode, prefixtype, prefix_r;
1214 unsigned int opcode, ra, rb, rc, rd, spr, u;
1215 unsigned long int imm;
1216 unsigned long int val, val2;
1217 unsigned int mb, me, sh;
1218 unsigned int word, suffix;
1221 word = ppc_inst_val(instr);
1222 suffix = ppc_inst_suffix(instr);
1226 opcode = ppc_inst_primary_opcode(instr);
1230 imm = (signed short)(word & 0xfffc);
1231 if ((word & 2) == 0)
1233 op->val = truncate_if_32bit(regs->msr, imm);
1236 if (branch_taken(word, regs, op))
1237 op->type |= BRTAKEN;
1241 if ((word & 0xfe2) == 2)
1243 else if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) &&
1244 (word & 0xfe3) == 1)
1245 op->type = SYSCALL_VECTORED_0;
1251 op->type = BRANCH | BRTAKEN;
1252 imm = word & 0x03fffffc;
1253 if (imm & 0x02000000)
1255 if ((word & 2) == 0)
1257 op->val = truncate_if_32bit(regs->msr, imm);
1262 switch ((word >> 1) & 0x3ff) {
1264 op->type = COMPUTE + SETCC;
1265 rd = 7 - ((word >> 23) & 0x7);
1266 ra = 7 - ((word >> 18) & 0x7);
1269 val = (regs->ccr >> ra) & 0xf;
1270 op->ccval = (regs->ccr & ~(0xfUL << rd)) | (val << rd);
1274 case 528: /* bcctr */
1276 imm = (word & 0x400)? regs->ctr: regs->link;
1277 op->val = truncate_if_32bit(regs->msr, imm);
1280 if (branch_taken(word, regs, op))
1281 op->type |= BRTAKEN;
1284 case 18: /* rfid, scary */
1285 if (regs->msr & MSR_PR)
1290 case 150: /* isync */
1291 op->type = BARRIER | BARRIER_ISYNC;
1294 case 33: /* crnor */
1295 case 129: /* crandc */
1296 case 193: /* crxor */
1297 case 225: /* crnand */
1298 case 257: /* crand */
1299 case 289: /* creqv */
1300 case 417: /* crorc */
1301 case 449: /* cror */
1302 op->type = COMPUTE + SETCC;
1303 ra = (word >> 16) & 0x1f;
1304 rb = (word >> 11) & 0x1f;
1305 rd = (word >> 21) & 0x1f;
1306 ra = (regs->ccr >> (31 - ra)) & 1;
1307 rb = (regs->ccr >> (31 - rb)) & 1;
1308 val = (word >> (6 + ra * 2 + rb)) & 1;
1309 op->ccval = (regs->ccr & ~(1UL << (31 - rd))) |
1315 switch ((word >> 1) & 0x3ff) {
1316 case 598: /* sync */
1317 op->type = BARRIER + BARRIER_SYNC;
1318 #ifdef __powerpc64__
1319 switch ((word >> 21) & 3) {
1320 case 1: /* lwsync */
1321 op->type = BARRIER + BARRIER_LWSYNC;
1323 case 2: /* ptesync */
1324 op->type = BARRIER + BARRIER_PTESYNC;
1330 case 854: /* eieio */
1331 op->type = BARRIER + BARRIER_EIEIO;
1337 /* Following cases refer to regs->gpr[], so we need all regs */
1338 if (!FULL_REGS(regs))
1341 rd = (word >> 21) & 0x1f;
1342 ra = (word >> 16) & 0x1f;
1343 rb = (word >> 11) & 0x1f;
1344 rc = (word >> 6) & 0x1f;
1347 #ifdef __powerpc64__
1349 prefix_r = GET_PREFIX_R(word);
1350 ra = GET_PREFIX_RA(suffix);
1351 rd = (suffix >> 21) & 0x1f;
1353 op->val = regs->gpr[rd];
1354 suffixopcode = get_op(suffix);
1355 prefixtype = (word >> 24) & 0x3;
1356 switch (prefixtype) {
1360 switch (suffixopcode) {
1361 case 14: /* paddi */
1362 op->type = COMPUTE | PREFIXED;
1363 op->val = mlsd_8lsd_ea(word, suffix, regs);
1369 if (rd & trap_compare(regs->gpr[ra], (short) word))
1374 if (rd & trap_compare((int)regs->gpr[ra], (short) word))
1378 #ifdef __powerpc64__
1380 if (!cpu_has_feature(CPU_FTR_ARCH_300))
1383 switch (word & 0x3f) {
1384 case 48: /* maddhd */
1385 asm volatile(PPC_MADDHD(%0, %1, %2, %3) :
1386 "=r" (op->val) : "r" (regs->gpr[ra]),
1387 "r" (regs->gpr[rb]), "r" (regs->gpr[rc]));
1390 case 49: /* maddhdu */
1391 asm volatile(PPC_MADDHDU(%0, %1, %2, %3) :
1392 "=r" (op->val) : "r" (regs->gpr[ra]),
1393 "r" (regs->gpr[rb]), "r" (regs->gpr[rc]));
1396 case 51: /* maddld */
1397 asm volatile(PPC_MADDLD(%0, %1, %2, %3) :
1398 "=r" (op->val) : "r" (regs->gpr[ra]),
1399 "r" (regs->gpr[rb]), "r" (regs->gpr[rc]));
1404 * There are other instructions from ISA 3.0 with the same
1405 * primary opcode which do not have emulation support yet.
1411 op->val = regs->gpr[ra] * (short) word;
1414 case 8: /* subfic */
1416 add_with_carry(regs, op, rd, ~regs->gpr[ra], imm, 1);
1419 case 10: /* cmpli */
1420 imm = (unsigned short) word;
1421 val = regs->gpr[ra];
1422 #ifdef __powerpc64__
1424 val = (unsigned int) val;
1426 do_cmp_unsigned(regs, op, val, imm, rd >> 2);
1431 val = regs->gpr[ra];
1432 #ifdef __powerpc64__
1436 do_cmp_signed(regs, op, val, imm, rd >> 2);
1439 case 12: /* addic */
1441 add_with_carry(regs, op, rd, regs->gpr[ra], imm, 0);
1444 case 13: /* addic. */
1446 add_with_carry(regs, op, rd, regs->gpr[ra], imm, 0);
1453 imm += regs->gpr[ra];
1457 case 15: /* addis */
1458 imm = ((short) word) << 16;
1460 imm += regs->gpr[ra];
1465 if (((word >> 1) & 0x1f) == 2) {
1467 imm = (short) (word & 0xffc1); /* d0 + d2 fields */
1468 imm |= (word >> 15) & 0x3e; /* d1 field */
1469 op->val = regs->nip + (imm << 16) + 4;
1475 case 20: /* rlwimi */
1476 mb = (word >> 6) & 0x1f;
1477 me = (word >> 1) & 0x1f;
1478 val = DATA32(regs->gpr[rd]);
1479 imm = MASK32(mb, me);
1480 op->val = (regs->gpr[ra] & ~imm) | (ROTATE(val, rb) & imm);
1483 case 21: /* rlwinm */
1484 mb = (word >> 6) & 0x1f;
1485 me = (word >> 1) & 0x1f;
1486 val = DATA32(regs->gpr[rd]);
1487 op->val = ROTATE(val, rb) & MASK32(mb, me);
1490 case 23: /* rlwnm */
1491 mb = (word >> 6) & 0x1f;
1492 me = (word >> 1) & 0x1f;
1493 rb = regs->gpr[rb] & 0x1f;
1494 val = DATA32(regs->gpr[rd]);
1495 op->val = ROTATE(val, rb) & MASK32(mb, me);
1499 op->val = regs->gpr[rd] | (unsigned short) word;
1500 goto logical_done_nocc;
1503 imm = (unsigned short) word;
1504 op->val = regs->gpr[rd] | (imm << 16);
1505 goto logical_done_nocc;
1508 op->val = regs->gpr[rd] ^ (unsigned short) word;
1509 goto logical_done_nocc;
1511 case 27: /* xoris */
1512 imm = (unsigned short) word;
1513 op->val = regs->gpr[rd] ^ (imm << 16);
1514 goto logical_done_nocc;
1516 case 28: /* andi. */
1517 op->val = regs->gpr[rd] & (unsigned short) word;
1519 goto logical_done_nocc;
1521 case 29: /* andis. */
1522 imm = (unsigned short) word;
1523 op->val = regs->gpr[rd] & (imm << 16);
1525 goto logical_done_nocc;
1527 #ifdef __powerpc64__
1529 mb = ((word >> 6) & 0x1f) | (word & 0x20);
1530 val = regs->gpr[rd];
1531 if ((word & 0x10) == 0) {
1532 sh = rb | ((word & 2) << 4);
1533 val = ROTATE(val, sh);
1534 switch ((word >> 2) & 3) {
1535 case 0: /* rldicl */
1536 val &= MASK64_L(mb);
1538 case 1: /* rldicr */
1539 val &= MASK64_R(mb);
1542 val &= MASK64(mb, 63 - sh);
1544 case 3: /* rldimi */
1545 imm = MASK64(mb, 63 - sh);
1546 val = (regs->gpr[ra] & ~imm) |
1552 sh = regs->gpr[rb] & 0x3f;
1553 val = ROTATE(val, sh);
1554 switch ((word >> 1) & 7) {
1556 op->val = val & MASK64_L(mb);
1559 op->val = val & MASK64_R(mb);
1564 op->type = UNKNOWN; /* illegal instruction */
1568 /* isel occupies 32 minor opcodes */
1569 if (((word >> 1) & 0x1f) == 15) {
1570 mb = (word >> 6) & 0x1f; /* bc field */
1571 val = (regs->ccr >> (31 - mb)) & 1;
1572 val2 = (ra) ? regs->gpr[ra] : 0;
1574 op->val = (val) ? val2 : regs->gpr[rb];
1578 switch ((word >> 1) & 0x3ff) {
1581 (rd & trap_compare((int)regs->gpr[ra],
1582 (int)regs->gpr[rb])))
1585 #ifdef __powerpc64__
1587 if (rd & trap_compare(regs->gpr[ra], regs->gpr[rb]))
1591 case 83: /* mfmsr */
1592 if (regs->msr & MSR_PR)
1597 case 146: /* mtmsr */
1598 if (regs->msr & MSR_PR)
1602 op->val = 0xffffffff & ~(MSR_ME | MSR_LE);
1605 case 178: /* mtmsrd */
1606 if (regs->msr & MSR_PR)
1610 /* only MSR_EE and MSR_RI get changed if bit 15 set */
1611 /* mtmsrd doesn't change MSR_HV, MSR_ME or MSR_LE */
1612 imm = (word & 0x10000)? 0x8002: 0xefffffffffffeffeUL;
1619 if ((word >> 20) & 1) {
1621 for (sh = 0; sh < 8; ++sh) {
1622 if (word & (0x80000 >> sh))
1627 op->val = regs->ccr & imm;
1630 case 144: /* mtcrf */
1631 op->type = COMPUTE + SETCC;
1633 val = regs->gpr[rd];
1634 op->ccval = regs->ccr;
1635 for (sh = 0; sh < 8; ++sh) {
1636 if (word & (0x80000 >> sh))
1637 op->ccval = (op->ccval & ~imm) |
1643 case 339: /* mfspr */
1644 spr = ((word >> 16) & 0x1f) | ((word >> 6) & 0x3e0);
1648 if (spr == SPRN_XER || spr == SPRN_LR ||
1653 case 467: /* mtspr */
1654 spr = ((word >> 16) & 0x1f) | ((word >> 6) & 0x3e0);
1656 op->val = regs->gpr[rd];
1658 if (spr == SPRN_XER || spr == SPRN_LR ||
1664 * Compare instructions
1667 val = regs->gpr[ra];
1668 val2 = regs->gpr[rb];
1669 #ifdef __powerpc64__
1670 if ((rd & 1) == 0) {
1671 /* word (32-bit) compare */
1676 do_cmp_signed(regs, op, val, val2, rd >> 2);
1680 val = regs->gpr[ra];
1681 val2 = regs->gpr[rb];
1682 #ifdef __powerpc64__
1683 if ((rd & 1) == 0) {
1684 /* word (32-bit) compare */
1685 val = (unsigned int) val;
1686 val2 = (unsigned int) val2;
1689 do_cmp_unsigned(regs, op, val, val2, rd >> 2);
1692 case 508: /* cmpb */
1693 do_cmpb(regs, op, regs->gpr[rd], regs->gpr[rb]);
1694 goto logical_done_nocc;
1697 * Arithmetic instructions
1700 add_with_carry(regs, op, rd, ~regs->gpr[ra],
1703 #ifdef __powerpc64__
1704 case 9: /* mulhdu */
1705 asm("mulhdu %0,%1,%2" : "=r" (op->val) :
1706 "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
1710 add_with_carry(regs, op, rd, regs->gpr[ra],
1714 case 11: /* mulhwu */
1715 asm("mulhwu %0,%1,%2" : "=r" (op->val) :
1716 "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
1720 op->val = regs->gpr[rb] - regs->gpr[ra];
1722 #ifdef __powerpc64__
1723 case 73: /* mulhd */
1724 asm("mulhd %0,%1,%2" : "=r" (op->val) :
1725 "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
1728 case 75: /* mulhw */
1729 asm("mulhw %0,%1,%2" : "=r" (op->val) :
1730 "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
1734 op->val = -regs->gpr[ra];
1737 case 136: /* subfe */
1738 add_with_carry(regs, op, rd, ~regs->gpr[ra],
1739 regs->gpr[rb], regs->xer & XER_CA);
1742 case 138: /* adde */
1743 add_with_carry(regs, op, rd, regs->gpr[ra],
1744 regs->gpr[rb], regs->xer & XER_CA);
1747 case 200: /* subfze */
1748 add_with_carry(regs, op, rd, ~regs->gpr[ra], 0L,
1749 regs->xer & XER_CA);
1752 case 202: /* addze */
1753 add_with_carry(regs, op, rd, regs->gpr[ra], 0L,
1754 regs->xer & XER_CA);
1757 case 232: /* subfme */
1758 add_with_carry(regs, op, rd, ~regs->gpr[ra], -1L,
1759 regs->xer & XER_CA);
1761 #ifdef __powerpc64__
1762 case 233: /* mulld */
1763 op->val = regs->gpr[ra] * regs->gpr[rb];
1766 case 234: /* addme */
1767 add_with_carry(regs, op, rd, regs->gpr[ra], -1L,
1768 regs->xer & XER_CA);
1771 case 235: /* mullw */
1772 op->val = (long)(int) regs->gpr[ra] *
1773 (int) regs->gpr[rb];
1776 #ifdef __powerpc64__
1777 case 265: /* modud */
1778 if (!cpu_has_feature(CPU_FTR_ARCH_300))
1780 op->val = regs->gpr[ra] % regs->gpr[rb];
1784 op->val = regs->gpr[ra] + regs->gpr[rb];
1787 case 267: /* moduw */
1788 if (!cpu_has_feature(CPU_FTR_ARCH_300))
1790 op->val = (unsigned int) regs->gpr[ra] %
1791 (unsigned int) regs->gpr[rb];
1793 #ifdef __powerpc64__
1794 case 457: /* divdu */
1795 op->val = regs->gpr[ra] / regs->gpr[rb];
1798 case 459: /* divwu */
1799 op->val = (unsigned int) regs->gpr[ra] /
1800 (unsigned int) regs->gpr[rb];
1802 #ifdef __powerpc64__
1803 case 489: /* divd */
1804 op->val = (long int) regs->gpr[ra] /
1805 (long int) regs->gpr[rb];
1808 case 491: /* divw */
1809 op->val = (int) regs->gpr[ra] /
1810 (int) regs->gpr[rb];
1812 #ifdef __powerpc64__
1813 case 425: /* divde[.] */
1814 asm volatile(PPC_DIVDE(%0, %1, %2) :
1815 "=r" (op->val) : "r" (regs->gpr[ra]),
1816 "r" (regs->gpr[rb]));
1818 case 393: /* divdeu[.] */
1819 asm volatile(PPC_DIVDEU(%0, %1, %2) :
1820 "=r" (op->val) : "r" (regs->gpr[ra]),
1821 "r" (regs->gpr[rb]));
1824 case 755: /* darn */
1825 if (!cpu_has_feature(CPU_FTR_ARCH_300))
1829 /* 32-bit conditioned */
1830 asm volatile(PPC_DARN(%0, 0) : "=r" (op->val));
1834 /* 64-bit conditioned */
1835 asm volatile(PPC_DARN(%0, 1) : "=r" (op->val));
1840 asm volatile(PPC_DARN(%0, 2) : "=r" (op->val));
1845 #ifdef __powerpc64__
1846 case 777: /* modsd */
1847 if (!cpu_has_feature(CPU_FTR_ARCH_300))
1849 op->val = (long int) regs->gpr[ra] %
1850 (long int) regs->gpr[rb];
1853 case 779: /* modsw */
1854 if (!cpu_has_feature(CPU_FTR_ARCH_300))
1856 op->val = (int) regs->gpr[ra] %
1857 (int) regs->gpr[rb];
1862 * Logical instructions
1864 case 26: /* cntlzw */
1865 val = (unsigned int) regs->gpr[rd];
1866 op->val = ( val ? __builtin_clz(val) : 32 );
1868 #ifdef __powerpc64__
1869 case 58: /* cntlzd */
1870 val = regs->gpr[rd];
1871 op->val = ( val ? __builtin_clzl(val) : 64 );
1875 op->val = regs->gpr[rd] & regs->gpr[rb];
1879 op->val = regs->gpr[rd] & ~regs->gpr[rb];
1882 case 122: /* popcntb */
1883 do_popcnt(regs, op, regs->gpr[rd], 8);
1884 goto logical_done_nocc;
1887 op->val = ~(regs->gpr[rd] | regs->gpr[rb]);
1890 case 154: /* prtyw */
1891 do_prty(regs, op, regs->gpr[rd], 32);
1892 goto logical_done_nocc;
1894 case 186: /* prtyd */
1895 do_prty(regs, op, regs->gpr[rd], 64);
1896 goto logical_done_nocc;
1898 case 252: /* bpermd */
1899 do_bpermd(regs, op, regs->gpr[rd], regs->gpr[rb]);
1900 goto logical_done_nocc;
1903 op->val = ~(regs->gpr[rd] ^ regs->gpr[rb]);
1907 op->val = regs->gpr[rd] ^ regs->gpr[rb];
1910 case 378: /* popcntw */
1911 do_popcnt(regs, op, regs->gpr[rd], 32);
1912 goto logical_done_nocc;
1915 op->val = regs->gpr[rd] | ~regs->gpr[rb];
1919 op->val = regs->gpr[rd] | regs->gpr[rb];
1922 case 476: /* nand */
1923 op->val = ~(regs->gpr[rd] & regs->gpr[rb]);
1926 case 506: /* popcntd */
1927 do_popcnt(regs, op, regs->gpr[rd], 64);
1928 goto logical_done_nocc;
1930 case 538: /* cnttzw */
1931 if (!cpu_has_feature(CPU_FTR_ARCH_300))
1933 val = (unsigned int) regs->gpr[rd];
1934 op->val = (val ? __builtin_ctz(val) : 32);
1936 #ifdef __powerpc64__
1937 case 570: /* cnttzd */
1938 if (!cpu_has_feature(CPU_FTR_ARCH_300))
1940 val = regs->gpr[rd];
1941 op->val = (val ? __builtin_ctzl(val) : 64);
1944 case 922: /* extsh */
1945 op->val = (signed short) regs->gpr[rd];
1948 case 954: /* extsb */
1949 op->val = (signed char) regs->gpr[rd];
1951 #ifdef __powerpc64__
1952 case 986: /* extsw */
1953 op->val = (signed int) regs->gpr[rd];
1958 * Shift instructions
1961 sh = regs->gpr[rb] & 0x3f;
1963 op->val = (regs->gpr[rd] << sh) & 0xffffffffUL;
1969 sh = regs->gpr[rb] & 0x3f;
1971 op->val = (regs->gpr[rd] & 0xffffffffUL) >> sh;
1976 case 792: /* sraw */
1977 op->type = COMPUTE + SETREG + SETXER;
1978 sh = regs->gpr[rb] & 0x3f;
1979 ival = (signed int) regs->gpr[rd];
1980 op->val = ival >> (sh < 32 ? sh : 31);
1981 op->xerval = regs->xer;
1982 if (ival < 0 && (sh >= 32 || (ival & ((1ul << sh) - 1)) != 0))
1983 op->xerval |= XER_CA;
1985 op->xerval &= ~XER_CA;
1986 set_ca32(op, op->xerval & XER_CA);
1989 case 824: /* srawi */
1990 op->type = COMPUTE + SETREG + SETXER;
1992 ival = (signed int) regs->gpr[rd];
1993 op->val = ival >> sh;
1994 op->xerval = regs->xer;
1995 if (ival < 0 && (ival & ((1ul << sh) - 1)) != 0)
1996 op->xerval |= XER_CA;
1998 op->xerval &= ~XER_CA;
1999 set_ca32(op, op->xerval & XER_CA);
2002 #ifdef __powerpc64__
2004 sh = regs->gpr[rb] & 0x7f;
2006 op->val = regs->gpr[rd] << sh;
2012 sh = regs->gpr[rb] & 0x7f;
2014 op->val = regs->gpr[rd] >> sh;
2019 case 794: /* srad */
2020 op->type = COMPUTE + SETREG + SETXER;
2021 sh = regs->gpr[rb] & 0x7f;
2022 ival = (signed long int) regs->gpr[rd];
2023 op->val = ival >> (sh < 64 ? sh : 63);
2024 op->xerval = regs->xer;
2025 if (ival < 0 && (sh >= 64 || (ival & ((1ul << sh) - 1)) != 0))
2026 op->xerval |= XER_CA;
2028 op->xerval &= ~XER_CA;
2029 set_ca32(op, op->xerval & XER_CA);
2032 case 826: /* sradi with sh_5 = 0 */
2033 case 827: /* sradi with sh_5 = 1 */
2034 op->type = COMPUTE + SETREG + SETXER;
2035 sh = rb | ((word & 2) << 4);
2036 ival = (signed long int) regs->gpr[rd];
2037 op->val = ival >> sh;
2038 op->xerval = regs->xer;
2039 if (ival < 0 && (ival & ((1ul << sh) - 1)) != 0)
2040 op->xerval |= XER_CA;
2042 op->xerval &= ~XER_CA;
2043 set_ca32(op, op->xerval & XER_CA);
2046 case 890: /* extswsli with sh_5 = 0 */
2047 case 891: /* extswsli with sh_5 = 1 */
2048 if (!cpu_has_feature(CPU_FTR_ARCH_300))
2050 op->type = COMPUTE + SETREG;
2051 sh = rb | ((word & 2) << 4);
2052 val = (signed int) regs->gpr[rd];
2054 op->val = ROTATE(val, sh) & MASK64(0, 63 - sh);
2059 #endif /* __powerpc64__ */
2062 * Cache instructions
2064 case 54: /* dcbst */
2065 op->type = MKOP(CACHEOP, DCBST, 0);
2066 op->ea = xform_ea(word, regs);
2070 op->type = MKOP(CACHEOP, DCBF, 0);
2071 op->ea = xform_ea(word, regs);
2074 case 246: /* dcbtst */
2075 op->type = MKOP(CACHEOP, DCBTST, 0);
2076 op->ea = xform_ea(word, regs);
2080 case 278: /* dcbt */
2081 op->type = MKOP(CACHEOP, DCBTST, 0);
2082 op->ea = xform_ea(word, regs);
2086 case 982: /* icbi */
2087 op->type = MKOP(CACHEOP, ICBI, 0);
2088 op->ea = xform_ea(word, regs);
2091 case 1014: /* dcbz */
2092 op->type = MKOP(CACHEOP, DCBZ, 0);
2093 op->ea = xform_ea(word, regs);
2103 op->update_reg = ra;
2105 op->val = regs->gpr[rd];
2106 u = (word >> 20) & UPDATE;
2112 op->ea = xform_ea(word, regs);
2113 switch ((word >> 1) & 0x3ff) {
2114 case 20: /* lwarx */
2115 op->type = MKOP(LARX, 0, 4);
2118 case 150: /* stwcx. */
2119 op->type = MKOP(STCX, 0, 4);
2122 #ifdef __powerpc64__
2123 case 84: /* ldarx */
2124 op->type = MKOP(LARX, 0, 8);
2127 case 214: /* stdcx. */
2128 op->type = MKOP(STCX, 0, 8);
2131 case 52: /* lbarx */
2132 op->type = MKOP(LARX, 0, 1);
2135 case 694: /* stbcx. */
2136 op->type = MKOP(STCX, 0, 1);
2139 case 116: /* lharx */
2140 op->type = MKOP(LARX, 0, 2);
2143 case 726: /* sthcx. */
2144 op->type = MKOP(STCX, 0, 2);
2147 case 276: /* lqarx */
2148 if (!((rd & 1) || rd == ra || rd == rb))
2149 op->type = MKOP(LARX, 0, 16);
2152 case 182: /* stqcx. */
2154 op->type = MKOP(STCX, 0, 16);
2159 case 55: /* lwzux */
2160 op->type = MKOP(LOAD, u, 4);
2164 case 119: /* lbzux */
2165 op->type = MKOP(LOAD, u, 1);
2168 #ifdef CONFIG_ALTIVEC
2170 * Note: for the load/store vector element instructions,
2171 * bits of the EA say which field of the VMX register to use.
2174 op->type = MKOP(LOAD_VMX, 0, 1);
2175 op->element_size = 1;
2178 case 39: /* lvehx */
2179 op->type = MKOP(LOAD_VMX, 0, 2);
2180 op->element_size = 2;
2183 case 71: /* lvewx */
2184 op->type = MKOP(LOAD_VMX, 0, 4);
2185 op->element_size = 4;
2189 case 359: /* lvxl */
2190 op->type = MKOP(LOAD_VMX, 0, 16);
2191 op->element_size = 16;
2194 case 135: /* stvebx */
2195 op->type = MKOP(STORE_VMX, 0, 1);
2196 op->element_size = 1;
2199 case 167: /* stvehx */
2200 op->type = MKOP(STORE_VMX, 0, 2);
2201 op->element_size = 2;
2204 case 199: /* stvewx */
2205 op->type = MKOP(STORE_VMX, 0, 4);
2206 op->element_size = 4;
2209 case 231: /* stvx */
2210 case 487: /* stvxl */
2211 op->type = MKOP(STORE_VMX, 0, 16);
2213 #endif /* CONFIG_ALTIVEC */
2215 #ifdef __powerpc64__
2218 op->type = MKOP(LOAD, u, 8);
2221 case 149: /* stdx */
2222 case 181: /* stdux */
2223 op->type = MKOP(STORE, u, 8);
2227 case 151: /* stwx */
2228 case 183: /* stwux */
2229 op->type = MKOP(STORE, u, 4);
2232 case 215: /* stbx */
2233 case 247: /* stbux */
2234 op->type = MKOP(STORE, u, 1);
2237 case 279: /* lhzx */
2238 case 311: /* lhzux */
2239 op->type = MKOP(LOAD, u, 2);
2242 #ifdef __powerpc64__
2243 case 341: /* lwax */
2244 case 373: /* lwaux */
2245 op->type = MKOP(LOAD, SIGNEXT | u, 4);
2249 case 343: /* lhax */
2250 case 375: /* lhaux */
2251 op->type = MKOP(LOAD, SIGNEXT | u, 2);
2254 case 407: /* sthx */
2255 case 439: /* sthux */
2256 op->type = MKOP(STORE, u, 2);
2259 #ifdef __powerpc64__
2260 case 532: /* ldbrx */
2261 op->type = MKOP(LOAD, BYTEREV, 8);
2265 case 533: /* lswx */
2266 op->type = MKOP(LOAD_MULTI, 0, regs->xer & 0x7f);
2269 case 534: /* lwbrx */
2270 op->type = MKOP(LOAD, BYTEREV, 4);
2273 case 597: /* lswi */
2275 rb = 32; /* # bytes to load */
2276 op->type = MKOP(LOAD_MULTI, 0, rb);
2277 op->ea = ra ? regs->gpr[ra] : 0;
2280 #ifdef CONFIG_PPC_FPU
2281 case 535: /* lfsx */
2282 case 567: /* lfsux */
2283 op->type = MKOP(LOAD_FP, u | FPCONV, 4);
2286 case 599: /* lfdx */
2287 case 631: /* lfdux */
2288 op->type = MKOP(LOAD_FP, u, 8);
2291 case 663: /* stfsx */
2292 case 695: /* stfsux */
2293 op->type = MKOP(STORE_FP, u | FPCONV, 4);
2296 case 727: /* stfdx */
2297 case 759: /* stfdux */
2298 op->type = MKOP(STORE_FP, u, 8);
2301 #ifdef __powerpc64__
2302 case 791: /* lfdpx */
2303 op->type = MKOP(LOAD_FP, 0, 16);
2306 case 855: /* lfiwax */
2307 op->type = MKOP(LOAD_FP, SIGNEXT, 4);
2310 case 887: /* lfiwzx */
2311 op->type = MKOP(LOAD_FP, 0, 4);
2314 case 919: /* stfdpx */
2315 op->type = MKOP(STORE_FP, 0, 16);
2318 case 983: /* stfiwx */
2319 op->type = MKOP(STORE_FP, 0, 4);
2321 #endif /* __powerpc64 */
2322 #endif /* CONFIG_PPC_FPU */
2324 #ifdef __powerpc64__
2325 case 660: /* stdbrx */
2326 op->type = MKOP(STORE, BYTEREV, 8);
2327 op->val = byterev_8(regs->gpr[rd]);
2331 case 661: /* stswx */
2332 op->type = MKOP(STORE_MULTI, 0, regs->xer & 0x7f);
2335 case 662: /* stwbrx */
2336 op->type = MKOP(STORE, BYTEREV, 4);
2337 op->val = byterev_4(regs->gpr[rd]);
2340 case 725: /* stswi */
2342 rb = 32; /* # bytes to store */
2343 op->type = MKOP(STORE_MULTI, 0, rb);
2344 op->ea = ra ? regs->gpr[ra] : 0;
2347 case 790: /* lhbrx */
2348 op->type = MKOP(LOAD, BYTEREV, 2);
2351 case 918: /* sthbrx */
2352 op->type = MKOP(STORE, BYTEREV, 2);
2353 op->val = byterev_2(regs->gpr[rd]);
2357 case 12: /* lxsiwzx */
2358 op->reg = rd | ((word & 1) << 5);
2359 op->type = MKOP(LOAD_VSX, 0, 4);
2360 op->element_size = 8;
2363 case 76: /* lxsiwax */
2364 op->reg = rd | ((word & 1) << 5);
2365 op->type = MKOP(LOAD_VSX, SIGNEXT, 4);
2366 op->element_size = 8;
2369 case 140: /* stxsiwx */
2370 op->reg = rd | ((word & 1) << 5);
2371 op->type = MKOP(STORE_VSX, 0, 4);
2372 op->element_size = 8;
2375 case 268: /* lxvx */
2376 op->reg = rd | ((word & 1) << 5);
2377 op->type = MKOP(LOAD_VSX, 0, 16);
2378 op->element_size = 16;
2379 op->vsx_flags = VSX_CHECK_VEC;
2382 case 269: /* lxvl */
2383 case 301: { /* lxvll */
2385 op->reg = rd | ((word & 1) << 5);
2386 op->ea = ra ? regs->gpr[ra] : 0;
2387 nb = regs->gpr[rb] & 0xff;
2390 op->type = MKOP(LOAD_VSX, 0, nb);
2391 op->element_size = 16;
2392 op->vsx_flags = ((word & 0x20) ? VSX_LDLEFT : 0) |
2396 case 332: /* lxvdsx */
2397 op->reg = rd | ((word & 1) << 5);
2398 op->type = MKOP(LOAD_VSX, 0, 8);
2399 op->element_size = 8;
2400 op->vsx_flags = VSX_SPLAT;
2403 case 364: /* lxvwsx */
2404 op->reg = rd | ((word & 1) << 5);
2405 op->type = MKOP(LOAD_VSX, 0, 4);
2406 op->element_size = 4;
2407 op->vsx_flags = VSX_SPLAT | VSX_CHECK_VEC;
2410 case 396: /* stxvx */
2411 op->reg = rd | ((word & 1) << 5);
2412 op->type = MKOP(STORE_VSX, 0, 16);
2413 op->element_size = 16;
2414 op->vsx_flags = VSX_CHECK_VEC;
2417 case 397: /* stxvl */
2418 case 429: { /* stxvll */
2420 op->reg = rd | ((word & 1) << 5);
2421 op->ea = ra ? regs->gpr[ra] : 0;
2422 nb = regs->gpr[rb] & 0xff;
2425 op->type = MKOP(STORE_VSX, 0, nb);
2426 op->element_size = 16;
2427 op->vsx_flags = ((word & 0x20) ? VSX_LDLEFT : 0) |
2431 case 524: /* lxsspx */
2432 op->reg = rd | ((word & 1) << 5);
2433 op->type = MKOP(LOAD_VSX, 0, 4);
2434 op->element_size = 8;
2435 op->vsx_flags = VSX_FPCONV;
2438 case 588: /* lxsdx */
2439 op->reg = rd | ((word & 1) << 5);
2440 op->type = MKOP(LOAD_VSX, 0, 8);
2441 op->element_size = 8;
2444 case 652: /* stxsspx */
2445 op->reg = rd | ((word & 1) << 5);
2446 op->type = MKOP(STORE_VSX, 0, 4);
2447 op->element_size = 8;
2448 op->vsx_flags = VSX_FPCONV;
2451 case 716: /* stxsdx */
2452 op->reg = rd | ((word & 1) << 5);
2453 op->type = MKOP(STORE_VSX, 0, 8);
2454 op->element_size = 8;
2457 case 780: /* lxvw4x */
2458 op->reg = rd | ((word & 1) << 5);
2459 op->type = MKOP(LOAD_VSX, 0, 16);
2460 op->element_size = 4;
2463 case 781: /* lxsibzx */
2464 op->reg = rd | ((word & 1) << 5);
2465 op->type = MKOP(LOAD_VSX, 0, 1);
2466 op->element_size = 8;
2467 op->vsx_flags = VSX_CHECK_VEC;
2470 case 812: /* lxvh8x */
2471 op->reg = rd | ((word & 1) << 5);
2472 op->type = MKOP(LOAD_VSX, 0, 16);
2473 op->element_size = 2;
2474 op->vsx_flags = VSX_CHECK_VEC;
2477 case 813: /* lxsihzx */
2478 op->reg = rd | ((word & 1) << 5);
2479 op->type = MKOP(LOAD_VSX, 0, 2);
2480 op->element_size = 8;
2481 op->vsx_flags = VSX_CHECK_VEC;
2484 case 844: /* lxvd2x */
2485 op->reg = rd | ((word & 1) << 5);
2486 op->type = MKOP(LOAD_VSX, 0, 16);
2487 op->element_size = 8;
2490 case 876: /* lxvb16x */
2491 op->reg = rd | ((word & 1) << 5);
2492 op->type = MKOP(LOAD_VSX, 0, 16);
2493 op->element_size = 1;
2494 op->vsx_flags = VSX_CHECK_VEC;
2497 case 908: /* stxvw4x */
2498 op->reg = rd | ((word & 1) << 5);
2499 op->type = MKOP(STORE_VSX, 0, 16);
2500 op->element_size = 4;
2503 case 909: /* stxsibx */
2504 op->reg = rd | ((word & 1) << 5);
2505 op->type = MKOP(STORE_VSX, 0, 1);
2506 op->element_size = 8;
2507 op->vsx_flags = VSX_CHECK_VEC;
2510 case 940: /* stxvh8x */
2511 op->reg = rd | ((word & 1) << 5);
2512 op->type = MKOP(STORE_VSX, 0, 16);
2513 op->element_size = 2;
2514 op->vsx_flags = VSX_CHECK_VEC;
2517 case 941: /* stxsihx */
2518 op->reg = rd | ((word & 1) << 5);
2519 op->type = MKOP(STORE_VSX, 0, 2);
2520 op->element_size = 8;
2521 op->vsx_flags = VSX_CHECK_VEC;
2524 case 972: /* stxvd2x */
2525 op->reg = rd | ((word & 1) << 5);
2526 op->type = MKOP(STORE_VSX, 0, 16);
2527 op->element_size = 8;
2530 case 1004: /* stxvb16x */
2531 op->reg = rd | ((word & 1) << 5);
2532 op->type = MKOP(STORE_VSX, 0, 16);
2533 op->element_size = 1;
2534 op->vsx_flags = VSX_CHECK_VEC;
2537 #endif /* CONFIG_VSX */
2543 op->type = MKOP(LOAD, u, 4);
2544 op->ea = dform_ea(word, regs);
2549 op->type = MKOP(LOAD, u, 1);
2550 op->ea = dform_ea(word, regs);
2555 op->type = MKOP(STORE, u, 4);
2556 op->ea = dform_ea(word, regs);
2561 op->type = MKOP(STORE, u, 1);
2562 op->ea = dform_ea(word, regs);
2567 op->type = MKOP(LOAD, u, 2);
2568 op->ea = dform_ea(word, regs);
2573 op->type = MKOP(LOAD, SIGNEXT | u, 2);
2574 op->ea = dform_ea(word, regs);
2579 op->type = MKOP(STORE, u, 2);
2580 op->ea = dform_ea(word, regs);
2585 break; /* invalid form, ra in range to load */
2586 op->type = MKOP(LOAD_MULTI, 0, 4 * (32 - rd));
2587 op->ea = dform_ea(word, regs);
2591 op->type = MKOP(STORE_MULTI, 0, 4 * (32 - rd));
2592 op->ea = dform_ea(word, regs);
2595 #ifdef CONFIG_PPC_FPU
2598 op->type = MKOP(LOAD_FP, u | FPCONV, 4);
2599 op->ea = dform_ea(word, regs);
2604 op->type = MKOP(LOAD_FP, u, 8);
2605 op->ea = dform_ea(word, regs);
2609 case 53: /* stfsu */
2610 op->type = MKOP(STORE_FP, u | FPCONV, 4);
2611 op->ea = dform_ea(word, regs);
2615 case 55: /* stfdu */
2616 op->type = MKOP(STORE_FP, u, 8);
2617 op->ea = dform_ea(word, regs);
2621 #ifdef __powerpc64__
2623 if (!((rd & 1) || (rd == ra)))
2624 op->type = MKOP(LOAD, 0, 16);
2625 op->ea = dqform_ea(word, regs);
2630 case 57: /* lfdp, lxsd, lxssp */
2631 op->ea = dsform_ea(word, regs);
2635 break; /* reg must be even */
2636 op->type = MKOP(LOAD_FP, 0, 16);
2640 op->type = MKOP(LOAD_VSX, 0, 8);
2641 op->element_size = 8;
2642 op->vsx_flags = VSX_CHECK_VEC;
2646 op->type = MKOP(LOAD_VSX, 0, 4);
2647 op->element_size = 8;
2648 op->vsx_flags = VSX_FPCONV | VSX_CHECK_VEC;
2652 #endif /* CONFIG_VSX */
2654 #ifdef __powerpc64__
2655 case 58: /* ld[u], lwa */
2656 op->ea = dsform_ea(word, regs);
2659 op->type = MKOP(LOAD, 0, 8);
2662 op->type = MKOP(LOAD, UPDATE, 8);
2665 op->type = MKOP(LOAD, SIGNEXT, 4);
2672 case 61: /* stfdp, lxv, stxsd, stxssp, stxv */
2674 case 0: /* stfdp with LSB of DS field = 0 */
2675 case 4: /* stfdp with LSB of DS field = 1 */
2676 op->ea = dsform_ea(word, regs);
2677 op->type = MKOP(STORE_FP, 0, 16);
2681 op->ea = dqform_ea(word, regs);
2684 op->type = MKOP(LOAD_VSX, 0, 16);
2685 op->element_size = 16;
2686 op->vsx_flags = VSX_CHECK_VEC;
2689 case 2: /* stxsd with LSB of DS field = 0 */
2690 case 6: /* stxsd with LSB of DS field = 1 */
2691 op->ea = dsform_ea(word, regs);
2693 op->type = MKOP(STORE_VSX, 0, 8);
2694 op->element_size = 8;
2695 op->vsx_flags = VSX_CHECK_VEC;
2698 case 3: /* stxssp with LSB of DS field = 0 */
2699 case 7: /* stxssp with LSB of DS field = 1 */
2700 op->ea = dsform_ea(word, regs);
2702 op->type = MKOP(STORE_VSX, 0, 4);
2703 op->element_size = 8;
2704 op->vsx_flags = VSX_FPCONV | VSX_CHECK_VEC;
2708 op->ea = dqform_ea(word, regs);
2711 op->type = MKOP(STORE_VSX, 0, 16);
2712 op->element_size = 16;
2713 op->vsx_flags = VSX_CHECK_VEC;
2717 #endif /* CONFIG_VSX */
2719 #ifdef __powerpc64__
2720 case 62: /* std[u] */
2721 op->ea = dsform_ea(word, regs);
2724 op->type = MKOP(STORE, 0, 8);
2727 op->type = MKOP(STORE, UPDATE, 8);
2731 op->type = MKOP(STORE, 0, 16);
2735 case 1: /* Prefixed instructions */
2736 prefix_r = GET_PREFIX_R(word);
2737 ra = GET_PREFIX_RA(suffix);
2738 op->update_reg = ra;
2739 rd = (suffix >> 21) & 0x1f;
2741 op->val = regs->gpr[rd];
2743 suffixopcode = get_op(suffix);
2744 prefixtype = (word >> 24) & 0x3;
2745 switch (prefixtype) {
2746 case 0: /* Type 00 Eight-Byte Load/Store */
2749 op->ea = mlsd_8lsd_ea(word, suffix, regs);
2750 switch (suffixopcode) {
2752 op->type = MKOP(LOAD, PREFIXED | SIGNEXT, 4);
2754 case 42: /* plxsd */
2756 op->type = MKOP(LOAD_VSX, PREFIXED, 8);
2757 op->element_size = 8;
2758 op->vsx_flags = VSX_CHECK_VEC;
2760 case 43: /* plxssp */
2762 op->type = MKOP(LOAD_VSX, PREFIXED, 4);
2763 op->element_size = 8;
2764 op->vsx_flags = VSX_FPCONV | VSX_CHECK_VEC;
2766 case 46: /* pstxsd */
2768 op->type = MKOP(STORE_VSX, PREFIXED, 8);
2769 op->element_size = 8;
2770 op->vsx_flags = VSX_CHECK_VEC;
2772 case 47: /* pstxssp */
2774 op->type = MKOP(STORE_VSX, PREFIXED, 4);
2775 op->element_size = 8;
2776 op->vsx_flags = VSX_FPCONV | VSX_CHECK_VEC;
2778 case 51: /* plxv1 */
2781 case 50: /* plxv0 */
2782 op->type = MKOP(LOAD_VSX, PREFIXED, 16);
2783 op->element_size = 16;
2784 op->vsx_flags = VSX_CHECK_VEC;
2786 case 55: /* pstxv1 */
2789 case 54: /* pstxv0 */
2790 op->type = MKOP(STORE_VSX, PREFIXED, 16);
2791 op->element_size = 16;
2792 op->vsx_flags = VSX_CHECK_VEC;
2795 op->type = MKOP(LOAD, PREFIXED, 16);
2798 op->type = MKOP(LOAD, PREFIXED, 8);
2801 op->type = MKOP(STORE, PREFIXED, 16);
2804 op->type = MKOP(STORE, PREFIXED, 8);
2808 case 1: /* Type 01 Eight-Byte Register-to-Register */
2810 case 2: /* Type 10 Modified Load/Store */
2813 op->ea = mlsd_8lsd_ea(word, suffix, regs);
2814 switch (suffixopcode) {
2816 op->type = MKOP(LOAD, PREFIXED, 4);
2819 op->type = MKOP(LOAD, PREFIXED, 1);
2822 op->type = MKOP(STORE, PREFIXED, 4);
2825 op->type = MKOP(STORE, PREFIXED, 1);
2828 op->type = MKOP(LOAD, PREFIXED, 2);
2831 op->type = MKOP(LOAD, PREFIXED | SIGNEXT, 2);
2834 op->type = MKOP(STORE, PREFIXED, 2);
2837 op->type = MKOP(LOAD_FP, PREFIXED | FPCONV, 4);
2840 op->type = MKOP(LOAD_FP, PREFIXED, 8);
2842 case 52: /* pstfs */
2843 op->type = MKOP(STORE_FP, PREFIXED | FPCONV, 4);
2845 case 54: /* pstfd */
2846 op->type = MKOP(STORE_FP, PREFIXED, 8);
2850 case 3: /* Type 11 Modified Register-to-Register */
2853 #endif /* __powerpc64__ */
2858 if ((GETTYPE(op->type) == LOAD_VSX ||
2859 GETTYPE(op->type) == STORE_VSX) &&
2860 !cpu_has_feature(CPU_FTR_VSX)) {
2863 #endif /* CONFIG_VSX */
2884 op->type = INTERRUPT | 0x700;
2885 op->val = SRR1_PROGPRIV;
2889 op->type = INTERRUPT | 0x700;
2890 op->val = SRR1_PROGTRAP;
2893 EXPORT_SYMBOL_GPL(analyse_instr);
2894 NOKPROBE_SYMBOL(analyse_instr);
2897 * For PPC32 we always use stwu with r1 to change the stack pointer.
2898 * So this emulated store may corrupt the exception frame, now we
2899 * have to provide the exception frame trampoline, which is pushed
2900 * below the kprobed function stack. So we only update gpr[1] but
2901 * don't emulate the real store operation. We will do real store
2902 * operation safely in exception return code by checking this flag.
2904 static nokprobe_inline int handle_stack_update(unsigned long ea, struct pt_regs *regs)
2908 * Check if we will touch kernel stack overflow
2910 if (ea - STACK_INT_FRAME_SIZE <= current->thread.ksp_limit) {
2911 printk(KERN_CRIT "Can't kprobe this since kernel stack would overflow.\n");
2914 #endif /* CONFIG_PPC32 */
2916 * Check if we already set since that means we'll
2917 * lose the previous value.
2919 WARN_ON(test_thread_flag(TIF_EMULATE_STACK_STORE));
2920 set_thread_flag(TIF_EMULATE_STACK_STORE);
2924 static nokprobe_inline void do_signext(unsigned long *valp, int size)
2928 *valp = (signed short) *valp;
2931 *valp = (signed int) *valp;
2936 static nokprobe_inline void do_byterev(unsigned long *valp, int size)
2940 *valp = byterev_2(*valp);
2943 *valp = byterev_4(*valp);
2945 #ifdef __powerpc64__
2947 *valp = byterev_8(*valp);
2954 * Emulate an instruction that can be executed just by updating
2957 void emulate_update_regs(struct pt_regs *regs, struct instruction_op *op)
2959 unsigned long next_pc;
2961 next_pc = truncate_if_32bit(regs->msr, regs->nip + GETLENGTH(op->type));
2962 switch (GETTYPE(op->type)) {
2964 if (op->type & SETREG)
2965 regs->gpr[op->reg] = op->val;
2966 if (op->type & SETCC)
2967 regs->ccr = op->ccval;
2968 if (op->type & SETXER)
2969 regs->xer = op->xerval;
2973 if (op->type & SETLK)
2974 regs->link = next_pc;
2975 if (op->type & BRTAKEN)
2977 if (op->type & DECCTR)
2982 switch (op->type & BARRIER_MASK) {
2992 case BARRIER_LWSYNC:
2993 asm volatile("lwsync" : : : "memory");
2995 case BARRIER_PTESYNC:
2996 asm volatile("ptesync" : : : "memory");
3004 regs->gpr[op->reg] = regs->xer & 0xffffffffUL;
3007 regs->gpr[op->reg] = regs->link;
3010 regs->gpr[op->reg] = regs->ctr;
3020 regs->xer = op->val & 0xffffffffUL;
3023 regs->link = op->val;
3026 regs->ctr = op->val;
3036 regs->nip = next_pc;
3038 NOKPROBE_SYMBOL(emulate_update_regs);
3041 * Emulate a previously-analysed load or store instruction.
3042 * Return values are:
3043 * 0 = instruction emulated successfully
3044 * -EFAULT = address out of range or access faulted (regs->dar
3045 * contains the faulting address)
3046 * -EACCES = misaligned access, instruction requires alignment
3047 * -EINVAL = unknown operation in *op
3049 int emulate_loadstore(struct pt_regs *regs, struct instruction_op *op)
3051 int err, size, type;
3059 size = GETSIZE(op->type);
3060 type = GETTYPE(op->type);
3061 cross_endian = (regs->msr & MSR_LE) != (MSR_KERNEL & MSR_LE);
3062 ea = truncate_if_32bit(regs->msr, op->ea);
3066 if (ea & (size - 1))
3067 return -EACCES; /* can't handle misaligned */
3068 if (!address_ok(regs, ea, size))
3073 #ifdef __powerpc64__
3075 __get_user_asmx(val, ea, err, "lbarx");
3078 __get_user_asmx(val, ea, err, "lharx");
3082 __get_user_asmx(val, ea, err, "lwarx");
3084 #ifdef __powerpc64__
3086 __get_user_asmx(val, ea, err, "ldarx");
3089 err = do_lqarx(ea, ®s->gpr[op->reg]);
3100 regs->gpr[op->reg] = val;
3104 if (ea & (size - 1))
3105 return -EACCES; /* can't handle misaligned */
3106 if (!address_ok(regs, ea, size))
3110 #ifdef __powerpc64__
3112 __put_user_asmx(op->val, ea, err, "stbcx.", cr);
3115 __put_user_asmx(op->val, ea, err, "stbcx.", cr);
3119 __put_user_asmx(op->val, ea, err, "stwcx.", cr);
3121 #ifdef __powerpc64__
3123 __put_user_asmx(op->val, ea, err, "stdcx.", cr);
3126 err = do_stqcx(ea, regs->gpr[op->reg],
3127 regs->gpr[op->reg + 1], &cr);
3134 regs->ccr = (regs->ccr & 0x0fffffff) |
3136 ((regs->xer >> 3) & 0x10000000);
3142 #ifdef __powerpc64__
3144 err = emulate_lq(regs, ea, op->reg, cross_endian);
3148 err = read_mem(®s->gpr[op->reg], ea, size, regs);
3150 if (op->type & SIGNEXT)
3151 do_signext(®s->gpr[op->reg], size);
3152 if ((op->type & BYTEREV) == (cross_endian ? 0 : BYTEREV))
3153 do_byterev(®s->gpr[op->reg], size);
3157 #ifdef CONFIG_PPC_FPU
3160 * If the instruction is in userspace, we can emulate it even
3161 * if the VMX state is not live, because we have the state
3162 * stored in the thread_struct. If the instruction is in
3163 * the kernel, we must not touch the state in the thread_struct.
3165 if (!(regs->msr & MSR_PR) && !(regs->msr & MSR_FP))
3167 err = do_fp_load(op, ea, regs, cross_endian);
3170 #ifdef CONFIG_ALTIVEC
3172 if (!(regs->msr & MSR_PR) && !(regs->msr & MSR_VEC))
3174 err = do_vec_load(op->reg, ea, size, regs, cross_endian);
3179 unsigned long msrbit = MSR_VSX;
3182 * Some VSX instructions check the MSR_VEC bit rather than MSR_VSX
3183 * when the target of the instruction is a vector register.
3185 if (op->reg >= 32 && (op->vsx_flags & VSX_CHECK_VEC))
3187 if (!(regs->msr & MSR_PR) && !(regs->msr & msrbit))
3189 err = do_vsx_load(op, ea, regs, cross_endian);
3194 if (!address_ok(regs, ea, size))
3197 for (i = 0; i < size; i += 4) {
3198 unsigned int v32 = 0;
3203 err = copy_mem_in((u8 *) &v32, ea, nb, regs);
3206 if (unlikely(cross_endian))
3207 v32 = byterev_4(v32);
3208 regs->gpr[rd] = v32;
3210 /* reg number wraps from 31 to 0 for lsw[ix] */
3211 rd = (rd + 1) & 0x1f;
3216 #ifdef __powerpc64__
3218 err = emulate_stq(regs, ea, op->reg, cross_endian);
3222 if ((op->type & UPDATE) && size == sizeof(long) &&
3223 op->reg == 1 && op->update_reg == 1 &&
3224 !(regs->msr & MSR_PR) &&
3225 ea >= regs->gpr[1] - STACK_INT_FRAME_SIZE) {
3226 err = handle_stack_update(ea, regs);
3229 if (unlikely(cross_endian))
3230 do_byterev(&op->val, size);
3231 err = write_mem(op->val, ea, size, regs);
3234 #ifdef CONFIG_PPC_FPU
3236 if (!(regs->msr & MSR_PR) && !(regs->msr & MSR_FP))
3238 err = do_fp_store(op, ea, regs, cross_endian);
3241 #ifdef CONFIG_ALTIVEC
3243 if (!(regs->msr & MSR_PR) && !(regs->msr & MSR_VEC))
3245 err = do_vec_store(op->reg, ea, size, regs, cross_endian);
3250 unsigned long msrbit = MSR_VSX;
3253 * Some VSX instructions check the MSR_VEC bit rather than MSR_VSX
3254 * when the target of the instruction is a vector register.
3256 if (op->reg >= 32 && (op->vsx_flags & VSX_CHECK_VEC))
3258 if (!(regs->msr & MSR_PR) && !(regs->msr & msrbit))
3260 err = do_vsx_store(op, ea, regs, cross_endian);
3265 if (!address_ok(regs, ea, size))
3268 for (i = 0; i < size; i += 4) {
3269 unsigned int v32 = regs->gpr[rd];
3274 if (unlikely(cross_endian))
3275 v32 = byterev_4(v32);
3276 err = copy_mem_out((u8 *) &v32, ea, nb, regs);
3280 /* reg number wraps from 31 to 0 for stsw[ix] */
3281 rd = (rd + 1) & 0x1f;
3292 if (op->type & UPDATE)
3293 regs->gpr[op->update_reg] = op->ea;
3297 NOKPROBE_SYMBOL(emulate_loadstore);
3300 * Emulate instructions that cause a transfer of control,
3301 * loads and stores, and a few other instructions.
3302 * Returns 1 if the step was emulated, 0 if not,
3303 * or -1 if the instruction is one that should not be stepped,
3304 * such as an rfid, or a mtmsrd that would clear MSR_RI.
3306 int emulate_step(struct pt_regs *regs, struct ppc_inst instr)
3308 struct instruction_op op;
3313 r = analyse_instr(&op, regs, instr);
3317 emulate_update_regs(regs, &op);
3322 type = GETTYPE(op.type);
3324 if (OP_IS_LOAD_STORE(type)) {
3325 err = emulate_loadstore(regs, &op);
3333 ea = truncate_if_32bit(regs->msr, op.ea);
3334 if (!address_ok(regs, ea, 8))
3336 switch (op.type & CACHEOP_MASK) {
3338 __cacheop_user_asmx(ea, err, "dcbst");
3341 __cacheop_user_asmx(ea, err, "dcbf");
3345 prefetchw((void *) ea);
3349 prefetch((void *) ea);
3352 __cacheop_user_asmx(ea, err, "icbi");
3355 err = emulate_dcbz(ea, regs);
3365 regs->gpr[op.reg] = regs->msr & MSR_MASK;
3369 val = regs->gpr[op.reg];
3370 if ((val & MSR_RI) == 0)
3371 /* can't step mtmsr[d] that would clear MSR_RI */
3373 /* here op.val is the mask of bits to change */
3374 regs->msr = (regs->msr & ~op.val) | (val & op.val);
3378 case SYSCALL: /* sc */
3380 * N.B. this uses knowledge about how the syscall
3381 * entry code works. If that is changed, this will
3382 * need to be changed also.
3384 if (IS_ENABLED(CONFIG_PPC_FAST_ENDIAN_SWITCH) &&
3385 cpu_has_feature(CPU_FTR_REAL_LE) &&
3386 regs->gpr[0] == 0x1ebe) {
3387 regs->msr ^= MSR_LE;
3390 regs->gpr[9] = regs->gpr[13];
3391 regs->gpr[10] = MSR_KERNEL;
3392 regs->gpr[11] = regs->nip + 4;
3393 regs->gpr[12] = regs->msr & MSR_MASK;
3394 regs->gpr[13] = (unsigned long) get_paca();
3395 regs->nip = (unsigned long) &system_call_common;
3396 regs->msr = MSR_KERNEL;
3399 #ifdef CONFIG_PPC_BOOK3S_64
3400 case SYSCALL_VECTORED_0: /* scv 0 */
3401 regs->gpr[9] = regs->gpr[13];
3402 regs->gpr[10] = MSR_KERNEL;
3403 regs->gpr[11] = regs->nip + 4;
3404 regs->gpr[12] = regs->msr & MSR_MASK;
3405 regs->gpr[13] = (unsigned long) get_paca();
3406 regs->nip = (unsigned long) &system_call_vectored_emulate;
3407 regs->msr = MSR_KERNEL;
3418 regs->nip = truncate_if_32bit(regs->msr, regs->nip + GETLENGTH(op.type));
3421 NOKPROBE_SYMBOL(emulate_step);