1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2004 Paul Mackerras <paulus@au.ibm.com>, IBM
7 #include <linux/kernel.h>
8 #include <linux/kprobes.h>
9 #include <linux/ptrace.h>
10 #include <linux/prefetch.h>
11 #include <asm/sstep.h>
12 #include <asm/processor.h>
13 #include <linux/uaccess.h>
14 #include <asm/cpu_has_feature.h>
15 #include <asm/cputable.h>
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
36 #define VSX_REGISTER_XTP(rd) ((((rd) & 1) << 5) | ((rd) & 0xfe))
41 * Functions in ldstfp.S
43 extern void get_fpr(int rn, double *p);
44 extern void put_fpr(int rn, const double *p);
45 extern void get_vr(int rn, __vector128 *p);
46 extern void put_vr(int rn, __vector128 *p);
47 extern void load_vsrn(int vsr, const void *p);
48 extern void store_vsrn(int vsr, void *p);
49 extern void conv_sp_to_dp(const float *sp, double *dp);
50 extern void conv_dp_to_sp(const double *dp, float *sp);
57 extern int do_lq(unsigned long ea, unsigned long *regs);
58 extern int do_stq(unsigned long ea, unsigned long val0, unsigned long val1);
59 extern int do_lqarx(unsigned long ea, unsigned long *regs);
60 extern int do_stqcx(unsigned long ea, unsigned long val0, unsigned long val1,
64 #ifdef __LITTLE_ENDIAN__
73 * Emulate the truncation of 64 bit values in 32-bit mode.
75 static nokprobe_inline unsigned long truncate_if_32bit(unsigned long msr,
79 if ((msr & MSR_64BIT) == 0)
86 * Determine whether a conditional branch instruction would branch.
88 static nokprobe_inline int branch_taken(unsigned int instr,
89 const struct pt_regs *regs,
90 struct instruction_op *op)
92 unsigned int bo = (instr >> 21) & 0x1f;
96 /* decrement counter */
98 if (((bo >> 1) & 1) ^ (regs->ctr == 1))
101 if ((bo & 0x10) == 0) {
102 /* check bit from CR */
103 bi = (instr >> 16) & 0x1f;
104 if (((regs->ccr >> (31 - bi)) & 1) != ((bo >> 3) & 1))
110 static nokprobe_inline long address_ok(struct pt_regs *regs,
111 unsigned long ea, int nb)
113 if (!user_mode(regs))
115 if (__access_ok(ea, nb))
117 if (__access_ok(ea, 1))
118 /* Access overlaps the end of the user region */
119 regs->dar = TASK_SIZE_MAX - 1;
126 * Calculate effective address for a D-form instruction
128 static nokprobe_inline unsigned long dform_ea(unsigned int instr,
129 const struct pt_regs *regs)
134 ra = (instr >> 16) & 0x1f;
135 ea = (signed short) instr; /* sign-extend */
144 * Calculate effective address for a DS-form instruction
146 static nokprobe_inline unsigned long dsform_ea(unsigned int instr,
147 const struct pt_regs *regs)
152 ra = (instr >> 16) & 0x1f;
153 ea = (signed short) (instr & ~3); /* sign-extend */
161 * Calculate effective address for a DQ-form instruction
163 static nokprobe_inline unsigned long dqform_ea(unsigned int instr,
164 const struct pt_regs *regs)
169 ra = (instr >> 16) & 0x1f;
170 ea = (signed short) (instr & ~0xf); /* sign-extend */
176 #endif /* __powerpc64 */
179 * Calculate effective address for an X-form instruction
181 static nokprobe_inline unsigned long xform_ea(unsigned int instr,
182 const struct pt_regs *regs)
187 ra = (instr >> 16) & 0x1f;
188 rb = (instr >> 11) & 0x1f;
197 * Calculate effective address for a MLS:D-form / 8LS:D-form
198 * prefixed instruction
200 static nokprobe_inline unsigned long mlsd_8lsd_ea(unsigned int instr,
202 const struct pt_regs *regs)
206 unsigned long ea, d0, d1, d;
208 prefix_r = GET_PREFIX_R(instr);
209 ra = GET_PREFIX_RA(suffix);
211 d0 = instr & 0x3ffff;
212 d1 = suffix & 0xffff;
216 * sign extend a 34 bit number
218 dd = (unsigned int)(d >> 2);
220 ea = (ea << 2) | (d & 0x3);
224 else if (!prefix_r && !ra)
225 ; /* Leave ea as is */
230 * (prefix_r && ra) is an invalid form. Should already be
231 * checked for by caller!
238 * Return the largest power of 2, not greater than sizeof(unsigned long),
239 * such that x is a multiple of it.
241 static nokprobe_inline unsigned long max_align(unsigned long x)
243 x |= sizeof(unsigned long);
244 return x & -x; /* isolates rightmost bit */
247 static nokprobe_inline unsigned long byterev_2(unsigned long x)
249 return ((x >> 8) & 0xff) | ((x & 0xff) << 8);
252 static nokprobe_inline unsigned long byterev_4(unsigned long x)
254 return ((x >> 24) & 0xff) | ((x >> 8) & 0xff00) |
255 ((x & 0xff00) << 8) | ((x & 0xff) << 24);
259 static nokprobe_inline unsigned long byterev_8(unsigned long x)
261 return (byterev_4(x) << 32) | byterev_4(x >> 32);
265 static nokprobe_inline void do_byte_reverse(void *ptr, int nb)
269 *(u16 *)ptr = byterev_2(*(u16 *)ptr);
272 *(u32 *)ptr = byterev_4(*(u32 *)ptr);
276 *(unsigned long *)ptr = byterev_8(*(unsigned long *)ptr);
279 unsigned long *up = (unsigned long *)ptr;
281 tmp = byterev_8(up[0]);
282 up[0] = byterev_8(up[1]);
287 unsigned long *up = (unsigned long *)ptr;
290 tmp = byterev_8(up[0]);
291 up[0] = byterev_8(up[3]);
293 tmp = byterev_8(up[2]);
294 up[2] = byterev_8(up[1]);
305 static nokprobe_inline int read_mem_aligned(unsigned long *dest,
306 unsigned long ea, int nb,
307 struct pt_regs *regs)
314 err = __get_user(x, (unsigned char __user *) ea);
317 err = __get_user(x, (unsigned short __user *) ea);
320 err = __get_user(x, (unsigned int __user *) ea);
324 err = __get_user(x, (unsigned long __user *) ea);
336 * Copy from userspace to a buffer, using the largest possible
337 * aligned accesses, up to sizeof(long).
339 static nokprobe_inline int copy_mem_in(u8 *dest, unsigned long ea, int nb,
340 struct pt_regs *regs)
345 for (; nb > 0; nb -= c) {
351 err = __get_user(*dest, (unsigned char __user *) ea);
354 err = __get_user(*(u16 *)dest,
355 (unsigned short __user *) ea);
358 err = __get_user(*(u32 *)dest,
359 (unsigned int __user *) ea);
363 err = __get_user(*(unsigned long *)dest,
364 (unsigned long __user *) ea);
378 static nokprobe_inline int read_mem_unaligned(unsigned long *dest,
379 unsigned long ea, int nb,
380 struct pt_regs *regs)
384 u8 b[sizeof(unsigned long)];
390 i = IS_BE ? sizeof(unsigned long) - nb : 0;
391 err = copy_mem_in(&u.b[i], ea, nb, regs);
398 * Read memory at address ea for nb bytes, return 0 for success
399 * or -EFAULT if an error occurred. N.B. nb must be 1, 2, 4 or 8.
400 * If nb < sizeof(long), the result is right-justified on BE systems.
402 static int read_mem(unsigned long *dest, unsigned long ea, int nb,
403 struct pt_regs *regs)
405 if (!address_ok(regs, ea, nb))
407 if ((ea & (nb - 1)) == 0)
408 return read_mem_aligned(dest, ea, nb, regs);
409 return read_mem_unaligned(dest, ea, nb, regs);
411 NOKPROBE_SYMBOL(read_mem);
413 static nokprobe_inline int write_mem_aligned(unsigned long val,
414 unsigned long ea, int nb,
415 struct pt_regs *regs)
421 err = __put_user(val, (unsigned char __user *) ea);
424 err = __put_user(val, (unsigned short __user *) ea);
427 err = __put_user(val, (unsigned int __user *) ea);
431 err = __put_user(val, (unsigned long __user *) ea);
441 * Copy from a buffer to userspace, using the largest possible
442 * aligned accesses, up to sizeof(long).
444 static nokprobe_inline int copy_mem_out(u8 *dest, unsigned long ea, int nb,
445 struct pt_regs *regs)
450 for (; nb > 0; nb -= c) {
456 err = __put_user(*dest, (unsigned char __user *) ea);
459 err = __put_user(*(u16 *)dest,
460 (unsigned short __user *) ea);
463 err = __put_user(*(u32 *)dest,
464 (unsigned int __user *) ea);
468 err = __put_user(*(unsigned long *)dest,
469 (unsigned long __user *) ea);
483 static nokprobe_inline int write_mem_unaligned(unsigned long val,
484 unsigned long ea, int nb,
485 struct pt_regs *regs)
489 u8 b[sizeof(unsigned long)];
494 i = IS_BE ? sizeof(unsigned long) - nb : 0;
495 return copy_mem_out(&u.b[i], ea, nb, regs);
499 * Write memory at address ea for nb bytes, return 0 for success
500 * or -EFAULT if an error occurred. N.B. nb must be 1, 2, 4 or 8.
502 static int write_mem(unsigned long val, unsigned long ea, int nb,
503 struct pt_regs *regs)
505 if (!address_ok(regs, ea, nb))
507 if ((ea & (nb - 1)) == 0)
508 return write_mem_aligned(val, ea, nb, regs);
509 return write_mem_unaligned(val, ea, nb, regs);
511 NOKPROBE_SYMBOL(write_mem);
513 #ifdef CONFIG_PPC_FPU
515 * These access either the real FP register or the image in the
516 * thread_struct, depending on regs->msr & MSR_FP.
518 static int do_fp_load(struct instruction_op *op, unsigned long ea,
519 struct pt_regs *regs, bool cross_endian)
528 u8 b[2 * sizeof(double)];
531 nb = GETSIZE(op->type);
532 if (!address_ok(regs, ea, nb))
535 err = copy_mem_in(u.b, ea, nb, regs);
538 if (unlikely(cross_endian)) {
539 do_byte_reverse(u.b, min(nb, 8));
541 do_byte_reverse(&u.b[8], 8);
545 if (op->type & FPCONV)
546 conv_sp_to_dp(&u.f, &u.d[0]);
547 else if (op->type & SIGNEXT)
552 if (regs->msr & MSR_FP)
553 put_fpr(rn, &u.d[0]);
555 current->thread.TS_FPR(rn) = u.l[0];
559 if (regs->msr & MSR_FP)
560 put_fpr(rn, &u.d[1]);
562 current->thread.TS_FPR(rn) = u.l[1];
567 NOKPROBE_SYMBOL(do_fp_load);
569 static int do_fp_store(struct instruction_op *op, unsigned long ea,
570 struct pt_regs *regs, bool cross_endian)
578 u8 b[2 * sizeof(double)];
581 nb = GETSIZE(op->type);
582 if (!address_ok(regs, ea, nb))
586 if (regs->msr & MSR_FP)
587 get_fpr(rn, &u.d[0]);
589 u.l[0] = current->thread.TS_FPR(rn);
591 if (op->type & FPCONV)
592 conv_dp_to_sp(&u.d[0], &u.f);
598 if (regs->msr & MSR_FP)
599 get_fpr(rn, &u.d[1]);
601 u.l[1] = current->thread.TS_FPR(rn);
604 if (unlikely(cross_endian)) {
605 do_byte_reverse(u.b, min(nb, 8));
607 do_byte_reverse(&u.b[8], 8);
609 return copy_mem_out(u.b, ea, nb, regs);
611 NOKPROBE_SYMBOL(do_fp_store);
614 #ifdef CONFIG_ALTIVEC
615 /* For Altivec/VMX, no need to worry about alignment */
616 static nokprobe_inline int do_vec_load(int rn, unsigned long ea,
617 int size, struct pt_regs *regs,
623 u8 b[sizeof(__vector128)];
626 if (!address_ok(regs, ea & ~0xfUL, 16))
628 /* align to multiple of size */
630 err = copy_mem_in(&u.b[ea & 0xf], ea, size, regs);
633 if (unlikely(cross_endian))
634 do_byte_reverse(&u.b[ea & 0xf], size);
636 if (regs->msr & MSR_VEC)
639 current->thread.vr_state.vr[rn] = u.v;
644 static nokprobe_inline int do_vec_store(int rn, unsigned long ea,
645 int size, struct pt_regs *regs,
650 u8 b[sizeof(__vector128)];
653 if (!address_ok(regs, ea & ~0xfUL, 16))
655 /* align to multiple of size */
659 if (regs->msr & MSR_VEC)
662 u.v = current->thread.vr_state.vr[rn];
664 if (unlikely(cross_endian))
665 do_byte_reverse(&u.b[ea & 0xf], size);
666 return copy_mem_out(&u.b[ea & 0xf], ea, size, regs);
668 #endif /* CONFIG_ALTIVEC */
671 static nokprobe_inline int emulate_lq(struct pt_regs *regs, unsigned long ea,
672 int reg, bool cross_endian)
676 if (!address_ok(regs, ea, 16))
678 /* if aligned, should be atomic */
679 if ((ea & 0xf) == 0) {
680 err = do_lq(ea, ®s->gpr[reg]);
682 err = read_mem(®s->gpr[reg + IS_LE], ea, 8, regs);
684 err = read_mem(®s->gpr[reg + IS_BE], ea + 8, 8, regs);
686 if (!err && unlikely(cross_endian))
687 do_byte_reverse(®s->gpr[reg], 16);
691 static nokprobe_inline int emulate_stq(struct pt_regs *regs, unsigned long ea,
692 int reg, bool cross_endian)
695 unsigned long vals[2];
697 if (!address_ok(regs, ea, 16))
699 vals[0] = regs->gpr[reg];
700 vals[1] = regs->gpr[reg + 1];
701 if (unlikely(cross_endian))
702 do_byte_reverse(vals, 16);
704 /* if aligned, should be atomic */
706 return do_stq(ea, vals[0], vals[1]);
708 err = write_mem(vals[IS_LE], ea, 8, regs);
710 err = write_mem(vals[IS_BE], ea + 8, 8, regs);
713 #endif /* __powerpc64 */
716 void emulate_vsx_load(struct instruction_op *op, union vsx_reg *reg,
717 const void *mem, bool rev)
721 const unsigned int *wp;
722 const unsigned short *hp;
723 const unsigned char *bp;
725 size = GETSIZE(op->type);
726 reg->d[0] = reg->d[1] = 0;
728 switch (op->element_size) {
732 /* whole vector; lxv[x] or lxvl[l] */
735 memcpy(reg, mem, size);
736 if (IS_LE && (op->vsx_flags & VSX_LDLEFT))
739 do_byte_reverse(reg, size);
742 /* scalar loads, lxvd2x, lxvdsx */
743 read_size = (size >= 8) ? 8 : size;
744 i = IS_LE ? 8 : 8 - read_size;
745 memcpy(®->b[i], mem, read_size);
747 do_byte_reverse(®->b[i], 8);
749 if (op->type & SIGNEXT) {
750 /* size == 4 is the only case here */
751 reg->d[IS_LE] = (signed int) reg->d[IS_LE];
752 } else if (op->vsx_flags & VSX_FPCONV) {
754 conv_sp_to_dp(®->fp[1 + IS_LE],
760 unsigned long v = *(unsigned long *)(mem + 8);
761 reg->d[IS_BE] = !rev ? v : byterev_8(v);
762 } else if (op->vsx_flags & VSX_SPLAT)
763 reg->d[IS_BE] = reg->d[IS_LE];
769 for (j = 0; j < size / 4; ++j) {
770 i = IS_LE ? 3 - j : j;
771 reg->w[i] = !rev ? *wp++ : byterev_4(*wp++);
773 if (op->vsx_flags & VSX_SPLAT) {
774 u32 val = reg->w[IS_LE ? 3 : 0];
776 i = IS_LE ? 3 - j : j;
784 for (j = 0; j < size / 2; ++j) {
785 i = IS_LE ? 7 - j : j;
786 reg->h[i] = !rev ? *hp++ : byterev_2(*hp++);
792 for (j = 0; j < size; ++j) {
793 i = IS_LE ? 15 - j : j;
799 EXPORT_SYMBOL_GPL(emulate_vsx_load);
800 NOKPROBE_SYMBOL(emulate_vsx_load);
802 void emulate_vsx_store(struct instruction_op *op, const union vsx_reg *reg,
805 int size, write_size;
812 size = GETSIZE(op->type);
814 switch (op->element_size) {
820 /* reverse 32 bytes */
821 buf.d[0] = byterev_8(reg->d[3]);
822 buf.d[1] = byterev_8(reg->d[2]);
823 buf.d[2] = byterev_8(reg->d[1]);
824 buf.d[3] = byterev_8(reg->d[0]);
827 memcpy(mem, reg, size);
830 /* stxv, stxvx, stxvl, stxvll */
833 if (IS_LE && (op->vsx_flags & VSX_LDLEFT))
836 /* reverse 16 bytes */
837 buf.d[0] = byterev_8(reg->d[1]);
838 buf.d[1] = byterev_8(reg->d[0]);
841 memcpy(mem, reg, size);
844 /* scalar stores, stxvd2x */
845 write_size = (size >= 8) ? 8 : size;
846 i = IS_LE ? 8 : 8 - write_size;
847 if (size < 8 && op->vsx_flags & VSX_FPCONV) {
848 buf.d[0] = buf.d[1] = 0;
850 conv_dp_to_sp(®->dp[IS_LE], &buf.fp[1 + IS_LE]);
854 memcpy(mem, ®->b[i], write_size);
856 memcpy(mem + 8, ®->d[IS_BE], 8);
858 do_byte_reverse(mem, write_size);
860 do_byte_reverse(mem + 8, 8);
866 for (j = 0; j < size / 4; ++j) {
867 i = IS_LE ? 3 - j : j;
868 *wp++ = !rev ? reg->w[i] : byterev_4(reg->w[i]);
874 for (j = 0; j < size / 2; ++j) {
875 i = IS_LE ? 7 - j : j;
876 *hp++ = !rev ? reg->h[i] : byterev_2(reg->h[i]);
882 for (j = 0; j < size; ++j) {
883 i = IS_LE ? 15 - j : j;
889 EXPORT_SYMBOL_GPL(emulate_vsx_store);
890 NOKPROBE_SYMBOL(emulate_vsx_store);
892 static nokprobe_inline int do_vsx_load(struct instruction_op *op,
893 unsigned long ea, struct pt_regs *regs,
897 int i, j, nr_vsx_regs;
899 union vsx_reg buf[2];
900 int size = GETSIZE(op->type);
902 if (!address_ok(regs, ea, size) || copy_mem_in(mem, ea, size, regs))
905 nr_vsx_regs = size / sizeof(__vector128);
906 emulate_vsx_load(op, buf, mem, cross_endian);
909 /* FP regs + extensions */
910 if (regs->msr & MSR_FP) {
911 for (i = 0; i < nr_vsx_regs; i++) {
912 j = IS_LE ? nr_vsx_regs - i - 1 : i;
913 load_vsrn(reg + i, &buf[j].v);
916 for (i = 0; i < nr_vsx_regs; i++) {
917 j = IS_LE ? nr_vsx_regs - i - 1 : i;
918 current->thread.fp_state.fpr[reg + i][0] = buf[j].d[0];
919 current->thread.fp_state.fpr[reg + i][1] = buf[j].d[1];
923 if (regs->msr & MSR_VEC) {
924 for (i = 0; i < nr_vsx_regs; i++) {
925 j = IS_LE ? nr_vsx_regs - i - 1 : i;
926 load_vsrn(reg + i, &buf[j].v);
929 for (i = 0; i < nr_vsx_regs; i++) {
930 j = IS_LE ? nr_vsx_regs - i - 1 : i;
931 current->thread.vr_state.vr[reg - 32 + i] = buf[j].v;
939 static nokprobe_inline int do_vsx_store(struct instruction_op *op,
940 unsigned long ea, struct pt_regs *regs,
944 int i, j, nr_vsx_regs;
946 union vsx_reg buf[2];
947 int size = GETSIZE(op->type);
949 if (!address_ok(regs, ea, size))
952 nr_vsx_regs = size / sizeof(__vector128);
955 /* FP regs + extensions */
956 if (regs->msr & MSR_FP) {
957 for (i = 0; i < nr_vsx_regs; i++) {
958 j = IS_LE ? nr_vsx_regs - i - 1 : i;
959 store_vsrn(reg + i, &buf[j].v);
962 for (i = 0; i < nr_vsx_regs; i++) {
963 j = IS_LE ? nr_vsx_regs - i - 1 : i;
964 buf[j].d[0] = current->thread.fp_state.fpr[reg + i][0];
965 buf[j].d[1] = current->thread.fp_state.fpr[reg + i][1];
969 if (regs->msr & MSR_VEC) {
970 for (i = 0; i < nr_vsx_regs; i++) {
971 j = IS_LE ? nr_vsx_regs - i - 1 : i;
972 store_vsrn(reg + i, &buf[j].v);
975 for (i = 0; i < nr_vsx_regs; i++) {
976 j = IS_LE ? nr_vsx_regs - i - 1 : i;
977 buf[j].v = current->thread.vr_state.vr[reg - 32 + i];
982 emulate_vsx_store(op, buf, mem, cross_endian);
983 return copy_mem_out(mem, ea, size, regs);
985 #endif /* CONFIG_VSX */
987 int emulate_dcbz(unsigned long ea, struct pt_regs *regs)
990 unsigned long i, size;
993 size = ppc64_caches.l1d.block_size;
994 if (!(regs->msr & MSR_64BIT))
997 size = L1_CACHE_BYTES;
1000 if (!address_ok(regs, ea, size))
1002 for (i = 0; i < size; i += sizeof(long)) {
1003 err = __put_user(0, (unsigned long __user *) (ea + i));
1011 NOKPROBE_SYMBOL(emulate_dcbz);
1013 #define __put_user_asmx(x, addr, err, op, cr) \
1014 __asm__ __volatile__( \
1015 "1: " op " %2,0,%3\n" \
1018 ".section .fixup,\"ax\"\n" \
1023 : "=r" (err), "=r" (cr) \
1024 : "r" (x), "r" (addr), "i" (-EFAULT), "0" (err))
1026 #define __get_user_asmx(x, addr, err, op) \
1027 __asm__ __volatile__( \
1028 "1: "op" %1,0,%2\n" \
1030 ".section .fixup,\"ax\"\n" \
1035 : "=r" (err), "=r" (x) \
1036 : "r" (addr), "i" (-EFAULT), "0" (err))
1038 #define __cacheop_user_asmx(addr, err, op) \
1039 __asm__ __volatile__( \
1042 ".section .fixup,\"ax\"\n" \
1048 : "r" (addr), "i" (-EFAULT), "0" (err))
1050 static nokprobe_inline void set_cr0(const struct pt_regs *regs,
1051 struct instruction_op *op)
1056 op->ccval = (regs->ccr & 0x0fffffff) | ((regs->xer >> 3) & 0x10000000);
1057 #ifdef __powerpc64__
1058 if (!(regs->msr & MSR_64BIT))
1062 op->ccval |= 0x80000000;
1064 op->ccval |= 0x40000000;
1066 op->ccval |= 0x20000000;
1069 static nokprobe_inline void set_ca32(struct instruction_op *op, bool val)
1071 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
1073 op->xerval |= XER_CA32;
1075 op->xerval &= ~XER_CA32;
1079 static nokprobe_inline void add_with_carry(const struct pt_regs *regs,
1080 struct instruction_op *op, int rd,
1081 unsigned long val1, unsigned long val2,
1082 unsigned long carry_in)
1084 unsigned long val = val1 + val2;
1088 op->type = COMPUTE + SETREG + SETXER;
1091 #ifdef __powerpc64__
1092 if (!(regs->msr & MSR_64BIT)) {
1093 val = (unsigned int) val;
1094 val1 = (unsigned int) val1;
1097 op->xerval = regs->xer;
1098 if (val < val1 || (carry_in && val == val1))
1099 op->xerval |= XER_CA;
1101 op->xerval &= ~XER_CA;
1103 set_ca32(op, (unsigned int)val < (unsigned int)val1 ||
1104 (carry_in && (unsigned int)val == (unsigned int)val1));
1107 static nokprobe_inline void do_cmp_signed(const struct pt_regs *regs,
1108 struct instruction_op *op,
1109 long v1, long v2, int crfld)
1111 unsigned int crval, shift;
1113 op->type = COMPUTE + SETCC;
1114 crval = (regs->xer >> 31) & 1; /* get SO bit */
1121 shift = (7 - crfld) * 4;
1122 op->ccval = (regs->ccr & ~(0xf << shift)) | (crval << shift);
1125 static nokprobe_inline void do_cmp_unsigned(const struct pt_regs *regs,
1126 struct instruction_op *op,
1128 unsigned long v2, int crfld)
1130 unsigned int crval, shift;
1132 op->type = COMPUTE + SETCC;
1133 crval = (regs->xer >> 31) & 1; /* get SO bit */
1140 shift = (7 - crfld) * 4;
1141 op->ccval = (regs->ccr & ~(0xf << shift)) | (crval << shift);
1144 static nokprobe_inline void do_cmpb(const struct pt_regs *regs,
1145 struct instruction_op *op,
1146 unsigned long v1, unsigned long v2)
1148 unsigned long long out_val, mask;
1152 for (i = 0; i < 8; i++) {
1153 mask = 0xffUL << (i * 8);
1154 if ((v1 & mask) == (v2 & mask))
1161 * The size parameter is used to adjust the equivalent popcnt instruction.
1162 * popcntb = 8, popcntw = 32, popcntd = 64
1164 static nokprobe_inline void do_popcnt(const struct pt_regs *regs,
1165 struct instruction_op *op,
1166 unsigned long v1, int size)
1168 unsigned long long out = v1;
1170 out -= (out >> 1) & 0x5555555555555555ULL;
1171 out = (0x3333333333333333ULL & out) +
1172 (0x3333333333333333ULL & (out >> 2));
1173 out = (out + (out >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
1175 if (size == 8) { /* popcntb */
1181 if (size == 32) { /* popcntw */
1182 op->val = out & 0x0000003f0000003fULL;
1186 out = (out + (out >> 32)) & 0x7f;
1187 op->val = out; /* popcntd */
1191 static nokprobe_inline void do_bpermd(const struct pt_regs *regs,
1192 struct instruction_op *op,
1193 unsigned long v1, unsigned long v2)
1195 unsigned char perm, idx;
1199 for (i = 0; i < 8; i++) {
1200 idx = (v1 >> (i * 8)) & 0xff;
1202 if (v2 & PPC_BIT(idx))
1207 #endif /* CONFIG_PPC64 */
1209 * The size parameter adjusts the equivalent prty instruction.
1210 * prtyw = 32, prtyd = 64
1212 static nokprobe_inline void do_prty(const struct pt_regs *regs,
1213 struct instruction_op *op,
1214 unsigned long v, int size)
1216 unsigned long long res = v ^ (v >> 8);
1219 if (size == 32) { /* prtyw */
1220 op->val = res & 0x0000000100000001ULL;
1225 op->val = res & 1; /*prtyd */
1228 static nokprobe_inline int trap_compare(long v1, long v2)
1238 if ((unsigned long)v1 < (unsigned long)v2)
1240 else if ((unsigned long)v1 > (unsigned long)v2)
1246 * Elements of 32-bit rotate and mask instructions.
1248 #define MASK32(mb, me) ((0xffffffffUL >> (mb)) + \
1249 ((signed long)-0x80000000L >> (me)) + ((me) >= (mb)))
1250 #ifdef __powerpc64__
1251 #define MASK64_L(mb) (~0UL >> (mb))
1252 #define MASK64_R(me) ((signed long)-0x8000000000000000L >> (me))
1253 #define MASK64(mb, me) (MASK64_L(mb) + MASK64_R(me) + ((me) >= (mb)))
1254 #define DATA32(x) (((x) & 0xffffffffUL) | (((x) & 0xffffffffUL) << 32))
1256 #define DATA32(x) (x)
1258 #define ROTATE(x, n) ((n) ? (((x) << (n)) | ((x) >> (8 * sizeof(long) - (n)))) : (x))
1261 * Decode an instruction, and return information about it in *op
1262 * without changing *regs.
1263 * Integer arithmetic and logical instructions, branches, and barrier
1264 * instructions can be emulated just using the information in *op.
1266 * Return value is 1 if the instruction can be emulated just by
1267 * updating *regs with the information in *op, -1 if we need the
1268 * GPRs but *regs doesn't contain the full register set, or 0
1271 int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
1272 struct ppc_inst instr)
1275 unsigned int suffixopcode, prefixtype, prefix_r;
1277 unsigned int opcode, ra, rb, rc, rd, spr, u;
1278 unsigned long int imm;
1279 unsigned long int val, val2;
1280 unsigned int mb, me, sh;
1281 unsigned int word, suffix;
1284 word = ppc_inst_val(instr);
1285 suffix = ppc_inst_suffix(instr);
1289 opcode = ppc_inst_primary_opcode(instr);
1293 imm = (signed short)(word & 0xfffc);
1294 if ((word & 2) == 0)
1296 op->val = truncate_if_32bit(regs->msr, imm);
1299 if (branch_taken(word, regs, op))
1300 op->type |= BRTAKEN;
1304 if ((word & 0xfe2) == 2)
1306 else if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) &&
1307 (word & 0xfe3) == 1)
1308 op->type = SYSCALL_VECTORED_0;
1314 op->type = BRANCH | BRTAKEN;
1315 imm = word & 0x03fffffc;
1316 if (imm & 0x02000000)
1318 if ((word & 2) == 0)
1320 op->val = truncate_if_32bit(regs->msr, imm);
1325 switch ((word >> 1) & 0x3ff) {
1327 op->type = COMPUTE + SETCC;
1328 rd = 7 - ((word >> 23) & 0x7);
1329 ra = 7 - ((word >> 18) & 0x7);
1332 val = (regs->ccr >> ra) & 0xf;
1333 op->ccval = (regs->ccr & ~(0xfUL << rd)) | (val << rd);
1337 case 528: /* bcctr */
1339 imm = (word & 0x400)? regs->ctr: regs->link;
1340 op->val = truncate_if_32bit(regs->msr, imm);
1343 if (branch_taken(word, regs, op))
1344 op->type |= BRTAKEN;
1347 case 18: /* rfid, scary */
1348 if (regs->msr & MSR_PR)
1353 case 150: /* isync */
1354 op->type = BARRIER | BARRIER_ISYNC;
1357 case 33: /* crnor */
1358 case 129: /* crandc */
1359 case 193: /* crxor */
1360 case 225: /* crnand */
1361 case 257: /* crand */
1362 case 289: /* creqv */
1363 case 417: /* crorc */
1364 case 449: /* cror */
1365 op->type = COMPUTE + SETCC;
1366 ra = (word >> 16) & 0x1f;
1367 rb = (word >> 11) & 0x1f;
1368 rd = (word >> 21) & 0x1f;
1369 ra = (regs->ccr >> (31 - ra)) & 1;
1370 rb = (regs->ccr >> (31 - rb)) & 1;
1371 val = (word >> (6 + ra * 2 + rb)) & 1;
1372 op->ccval = (regs->ccr & ~(1UL << (31 - rd))) |
1378 switch ((word >> 1) & 0x3ff) {
1379 case 598: /* sync */
1380 op->type = BARRIER + BARRIER_SYNC;
1381 #ifdef __powerpc64__
1382 switch ((word >> 21) & 3) {
1383 case 1: /* lwsync */
1384 op->type = BARRIER + BARRIER_LWSYNC;
1386 case 2: /* ptesync */
1387 op->type = BARRIER + BARRIER_PTESYNC;
1393 case 854: /* eieio */
1394 op->type = BARRIER + BARRIER_EIEIO;
1400 /* Following cases refer to regs->gpr[], so we need all regs */
1401 if (!FULL_REGS(regs))
1404 rd = (word >> 21) & 0x1f;
1405 ra = (word >> 16) & 0x1f;
1406 rb = (word >> 11) & 0x1f;
1407 rc = (word >> 6) & 0x1f;
1410 #ifdef __powerpc64__
1412 if (!cpu_has_feature(CPU_FTR_ARCH_31))
1415 prefix_r = GET_PREFIX_R(word);
1416 ra = GET_PREFIX_RA(suffix);
1417 rd = (suffix >> 21) & 0x1f;
1419 op->val = regs->gpr[rd];
1420 suffixopcode = get_op(suffix);
1421 prefixtype = (word >> 24) & 0x3;
1422 switch (prefixtype) {
1426 switch (suffixopcode) {
1427 case 14: /* paddi */
1428 op->type = COMPUTE | PREFIXED;
1429 op->val = mlsd_8lsd_ea(word, suffix, regs);
1435 if (rd & trap_compare(regs->gpr[ra], (short) word))
1440 if (rd & trap_compare((int)regs->gpr[ra], (short) word))
1444 #ifdef __powerpc64__
1446 if (!cpu_has_feature(CPU_FTR_ARCH_300))
1449 switch (word & 0x3f) {
1450 case 48: /* maddhd */
1451 asm volatile(PPC_MADDHD(%0, %1, %2, %3) :
1452 "=r" (op->val) : "r" (regs->gpr[ra]),
1453 "r" (regs->gpr[rb]), "r" (regs->gpr[rc]));
1456 case 49: /* maddhdu */
1457 asm volatile(PPC_MADDHDU(%0, %1, %2, %3) :
1458 "=r" (op->val) : "r" (regs->gpr[ra]),
1459 "r" (regs->gpr[rb]), "r" (regs->gpr[rc]));
1462 case 51: /* maddld */
1463 asm volatile(PPC_MADDLD(%0, %1, %2, %3) :
1464 "=r" (op->val) : "r" (regs->gpr[ra]),
1465 "r" (regs->gpr[rb]), "r" (regs->gpr[rc]));
1470 * There are other instructions from ISA 3.0 with the same
1471 * primary opcode which do not have emulation support yet.
1477 op->val = regs->gpr[ra] * (short) word;
1480 case 8: /* subfic */
1482 add_with_carry(regs, op, rd, ~regs->gpr[ra], imm, 1);
1485 case 10: /* cmpli */
1486 imm = (unsigned short) word;
1487 val = regs->gpr[ra];
1488 #ifdef __powerpc64__
1490 val = (unsigned int) val;
1492 do_cmp_unsigned(regs, op, val, imm, rd >> 2);
1497 val = regs->gpr[ra];
1498 #ifdef __powerpc64__
1502 do_cmp_signed(regs, op, val, imm, rd >> 2);
1505 case 12: /* addic */
1507 add_with_carry(regs, op, rd, regs->gpr[ra], imm, 0);
1510 case 13: /* addic. */
1512 add_with_carry(regs, op, rd, regs->gpr[ra], imm, 0);
1519 imm += regs->gpr[ra];
1523 case 15: /* addis */
1524 imm = ((short) word) << 16;
1526 imm += regs->gpr[ra];
1531 if (((word >> 1) & 0x1f) == 2) {
1533 imm = (short) (word & 0xffc1); /* d0 + d2 fields */
1534 imm |= (word >> 15) & 0x3e; /* d1 field */
1535 op->val = regs->nip + (imm << 16) + 4;
1541 case 20: /* rlwimi */
1542 mb = (word >> 6) & 0x1f;
1543 me = (word >> 1) & 0x1f;
1544 val = DATA32(regs->gpr[rd]);
1545 imm = MASK32(mb, me);
1546 op->val = (regs->gpr[ra] & ~imm) | (ROTATE(val, rb) & imm);
1549 case 21: /* rlwinm */
1550 mb = (word >> 6) & 0x1f;
1551 me = (word >> 1) & 0x1f;
1552 val = DATA32(regs->gpr[rd]);
1553 op->val = ROTATE(val, rb) & MASK32(mb, me);
1556 case 23: /* rlwnm */
1557 mb = (word >> 6) & 0x1f;
1558 me = (word >> 1) & 0x1f;
1559 rb = regs->gpr[rb] & 0x1f;
1560 val = DATA32(regs->gpr[rd]);
1561 op->val = ROTATE(val, rb) & MASK32(mb, me);
1565 op->val = regs->gpr[rd] | (unsigned short) word;
1566 goto logical_done_nocc;
1569 imm = (unsigned short) word;
1570 op->val = regs->gpr[rd] | (imm << 16);
1571 goto logical_done_nocc;
1574 op->val = regs->gpr[rd] ^ (unsigned short) word;
1575 goto logical_done_nocc;
1577 case 27: /* xoris */
1578 imm = (unsigned short) word;
1579 op->val = regs->gpr[rd] ^ (imm << 16);
1580 goto logical_done_nocc;
1582 case 28: /* andi. */
1583 op->val = regs->gpr[rd] & (unsigned short) word;
1585 goto logical_done_nocc;
1587 case 29: /* andis. */
1588 imm = (unsigned short) word;
1589 op->val = regs->gpr[rd] & (imm << 16);
1591 goto logical_done_nocc;
1593 #ifdef __powerpc64__
1595 mb = ((word >> 6) & 0x1f) | (word & 0x20);
1596 val = regs->gpr[rd];
1597 if ((word & 0x10) == 0) {
1598 sh = rb | ((word & 2) << 4);
1599 val = ROTATE(val, sh);
1600 switch ((word >> 2) & 3) {
1601 case 0: /* rldicl */
1602 val &= MASK64_L(mb);
1604 case 1: /* rldicr */
1605 val &= MASK64_R(mb);
1608 val &= MASK64(mb, 63 - sh);
1610 case 3: /* rldimi */
1611 imm = MASK64(mb, 63 - sh);
1612 val = (regs->gpr[ra] & ~imm) |
1618 sh = regs->gpr[rb] & 0x3f;
1619 val = ROTATE(val, sh);
1620 switch ((word >> 1) & 7) {
1622 op->val = val & MASK64_L(mb);
1625 op->val = val & MASK64_R(mb);
1630 op->type = UNKNOWN; /* illegal instruction */
1634 /* isel occupies 32 minor opcodes */
1635 if (((word >> 1) & 0x1f) == 15) {
1636 mb = (word >> 6) & 0x1f; /* bc field */
1637 val = (regs->ccr >> (31 - mb)) & 1;
1638 val2 = (ra) ? regs->gpr[ra] : 0;
1640 op->val = (val) ? val2 : regs->gpr[rb];
1644 switch ((word >> 1) & 0x3ff) {
1647 (rd & trap_compare((int)regs->gpr[ra],
1648 (int)regs->gpr[rb])))
1651 #ifdef __powerpc64__
1653 if (rd & trap_compare(regs->gpr[ra], regs->gpr[rb]))
1657 case 83: /* mfmsr */
1658 if (regs->msr & MSR_PR)
1663 case 146: /* mtmsr */
1664 if (regs->msr & MSR_PR)
1668 op->val = 0xffffffff & ~(MSR_ME | MSR_LE);
1671 case 178: /* mtmsrd */
1672 if (regs->msr & MSR_PR)
1676 /* only MSR_EE and MSR_RI get changed if bit 15 set */
1677 /* mtmsrd doesn't change MSR_HV, MSR_ME or MSR_LE */
1678 imm = (word & 0x10000)? 0x8002: 0xefffffffffffeffeUL;
1685 if ((word >> 20) & 1) {
1687 for (sh = 0; sh < 8; ++sh) {
1688 if (word & (0x80000 >> sh))
1693 op->val = regs->ccr & imm;
1696 case 144: /* mtcrf */
1697 op->type = COMPUTE + SETCC;
1699 val = regs->gpr[rd];
1700 op->ccval = regs->ccr;
1701 for (sh = 0; sh < 8; ++sh) {
1702 if (word & (0x80000 >> sh))
1703 op->ccval = (op->ccval & ~imm) |
1709 case 339: /* mfspr */
1710 spr = ((word >> 16) & 0x1f) | ((word >> 6) & 0x3e0);
1714 if (spr == SPRN_XER || spr == SPRN_LR ||
1719 case 467: /* mtspr */
1720 spr = ((word >> 16) & 0x1f) | ((word >> 6) & 0x3e0);
1722 op->val = regs->gpr[rd];
1724 if (spr == SPRN_XER || spr == SPRN_LR ||
1730 * Compare instructions
1733 val = regs->gpr[ra];
1734 val2 = regs->gpr[rb];
1735 #ifdef __powerpc64__
1736 if ((rd & 1) == 0) {
1737 /* word (32-bit) compare */
1742 do_cmp_signed(regs, op, val, val2, rd >> 2);
1746 val = regs->gpr[ra];
1747 val2 = regs->gpr[rb];
1748 #ifdef __powerpc64__
1749 if ((rd & 1) == 0) {
1750 /* word (32-bit) compare */
1751 val = (unsigned int) val;
1752 val2 = (unsigned int) val2;
1755 do_cmp_unsigned(regs, op, val, val2, rd >> 2);
1758 case 508: /* cmpb */
1759 do_cmpb(regs, op, regs->gpr[rd], regs->gpr[rb]);
1760 goto logical_done_nocc;
1763 * Arithmetic instructions
1766 add_with_carry(regs, op, rd, ~regs->gpr[ra],
1769 #ifdef __powerpc64__
1770 case 9: /* mulhdu */
1771 asm("mulhdu %0,%1,%2" : "=r" (op->val) :
1772 "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
1776 add_with_carry(regs, op, rd, regs->gpr[ra],
1780 case 11: /* mulhwu */
1781 asm("mulhwu %0,%1,%2" : "=r" (op->val) :
1782 "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
1786 op->val = regs->gpr[rb] - regs->gpr[ra];
1788 #ifdef __powerpc64__
1789 case 73: /* mulhd */
1790 asm("mulhd %0,%1,%2" : "=r" (op->val) :
1791 "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
1794 case 75: /* mulhw */
1795 asm("mulhw %0,%1,%2" : "=r" (op->val) :
1796 "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
1800 op->val = -regs->gpr[ra];
1803 case 136: /* subfe */
1804 add_with_carry(regs, op, rd, ~regs->gpr[ra],
1805 regs->gpr[rb], regs->xer & XER_CA);
1808 case 138: /* adde */
1809 add_with_carry(regs, op, rd, regs->gpr[ra],
1810 regs->gpr[rb], regs->xer & XER_CA);
1813 case 200: /* subfze */
1814 add_with_carry(regs, op, rd, ~regs->gpr[ra], 0L,
1815 regs->xer & XER_CA);
1818 case 202: /* addze */
1819 add_with_carry(regs, op, rd, regs->gpr[ra], 0L,
1820 regs->xer & XER_CA);
1823 case 232: /* subfme */
1824 add_with_carry(regs, op, rd, ~regs->gpr[ra], -1L,
1825 regs->xer & XER_CA);
1827 #ifdef __powerpc64__
1828 case 233: /* mulld */
1829 op->val = regs->gpr[ra] * regs->gpr[rb];
1832 case 234: /* addme */
1833 add_with_carry(regs, op, rd, regs->gpr[ra], -1L,
1834 regs->xer & XER_CA);
1837 case 235: /* mullw */
1838 op->val = (long)(int) regs->gpr[ra] *
1839 (int) regs->gpr[rb];
1842 #ifdef __powerpc64__
1843 case 265: /* modud */
1844 if (!cpu_has_feature(CPU_FTR_ARCH_300))
1846 op->val = regs->gpr[ra] % regs->gpr[rb];
1850 op->val = regs->gpr[ra] + regs->gpr[rb];
1853 case 267: /* moduw */
1854 if (!cpu_has_feature(CPU_FTR_ARCH_300))
1856 op->val = (unsigned int) regs->gpr[ra] %
1857 (unsigned int) regs->gpr[rb];
1859 #ifdef __powerpc64__
1860 case 457: /* divdu */
1861 op->val = regs->gpr[ra] / regs->gpr[rb];
1864 case 459: /* divwu */
1865 op->val = (unsigned int) regs->gpr[ra] /
1866 (unsigned int) regs->gpr[rb];
1868 #ifdef __powerpc64__
1869 case 489: /* divd */
1870 op->val = (long int) regs->gpr[ra] /
1871 (long int) regs->gpr[rb];
1874 case 491: /* divw */
1875 op->val = (int) regs->gpr[ra] /
1876 (int) regs->gpr[rb];
1878 #ifdef __powerpc64__
1879 case 425: /* divde[.] */
1880 asm volatile(PPC_DIVDE(%0, %1, %2) :
1881 "=r" (op->val) : "r" (regs->gpr[ra]),
1882 "r" (regs->gpr[rb]));
1884 case 393: /* divdeu[.] */
1885 asm volatile(PPC_DIVDEU(%0, %1, %2) :
1886 "=r" (op->val) : "r" (regs->gpr[ra]),
1887 "r" (regs->gpr[rb]));
1890 case 755: /* darn */
1891 if (!cpu_has_feature(CPU_FTR_ARCH_300))
1895 /* 32-bit conditioned */
1896 asm volatile(PPC_DARN(%0, 0) : "=r" (op->val));
1900 /* 64-bit conditioned */
1901 asm volatile(PPC_DARN(%0, 1) : "=r" (op->val));
1906 asm volatile(PPC_DARN(%0, 2) : "=r" (op->val));
1911 #ifdef __powerpc64__
1912 case 777: /* modsd */
1913 if (!cpu_has_feature(CPU_FTR_ARCH_300))
1915 op->val = (long int) regs->gpr[ra] %
1916 (long int) regs->gpr[rb];
1919 case 779: /* modsw */
1920 if (!cpu_has_feature(CPU_FTR_ARCH_300))
1922 op->val = (int) regs->gpr[ra] %
1923 (int) regs->gpr[rb];
1928 * Logical instructions
1930 case 26: /* cntlzw */
1931 val = (unsigned int) regs->gpr[rd];
1932 op->val = ( val ? __builtin_clz(val) : 32 );
1934 #ifdef __powerpc64__
1935 case 58: /* cntlzd */
1936 val = regs->gpr[rd];
1937 op->val = ( val ? __builtin_clzl(val) : 64 );
1941 op->val = regs->gpr[rd] & regs->gpr[rb];
1945 op->val = regs->gpr[rd] & ~regs->gpr[rb];
1948 case 122: /* popcntb */
1949 do_popcnt(regs, op, regs->gpr[rd], 8);
1950 goto logical_done_nocc;
1953 op->val = ~(regs->gpr[rd] | regs->gpr[rb]);
1956 case 154: /* prtyw */
1957 do_prty(regs, op, regs->gpr[rd], 32);
1958 goto logical_done_nocc;
1960 case 186: /* prtyd */
1961 do_prty(regs, op, regs->gpr[rd], 64);
1962 goto logical_done_nocc;
1964 case 252: /* bpermd */
1965 do_bpermd(regs, op, regs->gpr[rd], regs->gpr[rb]);
1966 goto logical_done_nocc;
1969 op->val = ~(regs->gpr[rd] ^ regs->gpr[rb]);
1973 op->val = regs->gpr[rd] ^ regs->gpr[rb];
1976 case 378: /* popcntw */
1977 do_popcnt(regs, op, regs->gpr[rd], 32);
1978 goto logical_done_nocc;
1981 op->val = regs->gpr[rd] | ~regs->gpr[rb];
1985 op->val = regs->gpr[rd] | regs->gpr[rb];
1988 case 476: /* nand */
1989 op->val = ~(regs->gpr[rd] & regs->gpr[rb]);
1992 case 506: /* popcntd */
1993 do_popcnt(regs, op, regs->gpr[rd], 64);
1994 goto logical_done_nocc;
1996 case 538: /* cnttzw */
1997 if (!cpu_has_feature(CPU_FTR_ARCH_300))
1999 val = (unsigned int) regs->gpr[rd];
2000 op->val = (val ? __builtin_ctz(val) : 32);
2002 #ifdef __powerpc64__
2003 case 570: /* cnttzd */
2004 if (!cpu_has_feature(CPU_FTR_ARCH_300))
2006 val = regs->gpr[rd];
2007 op->val = (val ? __builtin_ctzl(val) : 64);
2010 case 922: /* extsh */
2011 op->val = (signed short) regs->gpr[rd];
2014 case 954: /* extsb */
2015 op->val = (signed char) regs->gpr[rd];
2017 #ifdef __powerpc64__
2018 case 986: /* extsw */
2019 op->val = (signed int) regs->gpr[rd];
2024 * Shift instructions
2027 sh = regs->gpr[rb] & 0x3f;
2029 op->val = (regs->gpr[rd] << sh) & 0xffffffffUL;
2035 sh = regs->gpr[rb] & 0x3f;
2037 op->val = (regs->gpr[rd] & 0xffffffffUL) >> sh;
2042 case 792: /* sraw */
2043 op->type = COMPUTE + SETREG + SETXER;
2044 sh = regs->gpr[rb] & 0x3f;
2045 ival = (signed int) regs->gpr[rd];
2046 op->val = ival >> (sh < 32 ? sh : 31);
2047 op->xerval = regs->xer;
2048 if (ival < 0 && (sh >= 32 || (ival & ((1ul << sh) - 1)) != 0))
2049 op->xerval |= XER_CA;
2051 op->xerval &= ~XER_CA;
2052 set_ca32(op, op->xerval & XER_CA);
2055 case 824: /* srawi */
2056 op->type = COMPUTE + SETREG + SETXER;
2058 ival = (signed int) regs->gpr[rd];
2059 op->val = ival >> sh;
2060 op->xerval = regs->xer;
2061 if (ival < 0 && (ival & ((1ul << sh) - 1)) != 0)
2062 op->xerval |= XER_CA;
2064 op->xerval &= ~XER_CA;
2065 set_ca32(op, op->xerval & XER_CA);
2068 #ifdef __powerpc64__
2070 sh = regs->gpr[rb] & 0x7f;
2072 op->val = regs->gpr[rd] << sh;
2078 sh = regs->gpr[rb] & 0x7f;
2080 op->val = regs->gpr[rd] >> sh;
2085 case 794: /* srad */
2086 op->type = COMPUTE + SETREG + SETXER;
2087 sh = regs->gpr[rb] & 0x7f;
2088 ival = (signed long int) regs->gpr[rd];
2089 op->val = ival >> (sh < 64 ? sh : 63);
2090 op->xerval = regs->xer;
2091 if (ival < 0 && (sh >= 64 || (ival & ((1ul << sh) - 1)) != 0))
2092 op->xerval |= XER_CA;
2094 op->xerval &= ~XER_CA;
2095 set_ca32(op, op->xerval & XER_CA);
2098 case 826: /* sradi with sh_5 = 0 */
2099 case 827: /* sradi with sh_5 = 1 */
2100 op->type = COMPUTE + SETREG + SETXER;
2101 sh = rb | ((word & 2) << 4);
2102 ival = (signed long int) regs->gpr[rd];
2103 op->val = ival >> sh;
2104 op->xerval = regs->xer;
2105 if (ival < 0 && (ival & ((1ul << sh) - 1)) != 0)
2106 op->xerval |= XER_CA;
2108 op->xerval &= ~XER_CA;
2109 set_ca32(op, op->xerval & XER_CA);
2112 case 890: /* extswsli with sh_5 = 0 */
2113 case 891: /* extswsli with sh_5 = 1 */
2114 if (!cpu_has_feature(CPU_FTR_ARCH_300))
2116 op->type = COMPUTE + SETREG;
2117 sh = rb | ((word & 2) << 4);
2118 val = (signed int) regs->gpr[rd];
2120 op->val = ROTATE(val, sh) & MASK64(0, 63 - sh);
2125 #endif /* __powerpc64__ */
2128 * Cache instructions
2130 case 54: /* dcbst */
2131 op->type = MKOP(CACHEOP, DCBST, 0);
2132 op->ea = xform_ea(word, regs);
2136 op->type = MKOP(CACHEOP, DCBF, 0);
2137 op->ea = xform_ea(word, regs);
2140 case 246: /* dcbtst */
2141 op->type = MKOP(CACHEOP, DCBTST, 0);
2142 op->ea = xform_ea(word, regs);
2146 case 278: /* dcbt */
2147 op->type = MKOP(CACHEOP, DCBTST, 0);
2148 op->ea = xform_ea(word, regs);
2152 case 982: /* icbi */
2153 op->type = MKOP(CACHEOP, ICBI, 0);
2154 op->ea = xform_ea(word, regs);
2157 case 1014: /* dcbz */
2158 op->type = MKOP(CACHEOP, DCBZ, 0);
2159 op->ea = xform_ea(word, regs);
2169 op->update_reg = ra;
2171 op->val = regs->gpr[rd];
2172 u = (word >> 20) & UPDATE;
2178 op->ea = xform_ea(word, regs);
2179 switch ((word >> 1) & 0x3ff) {
2180 case 20: /* lwarx */
2181 op->type = MKOP(LARX, 0, 4);
2184 case 150: /* stwcx. */
2185 op->type = MKOP(STCX, 0, 4);
2188 #ifdef __powerpc64__
2189 case 84: /* ldarx */
2190 op->type = MKOP(LARX, 0, 8);
2193 case 214: /* stdcx. */
2194 op->type = MKOP(STCX, 0, 8);
2197 case 52: /* lbarx */
2198 op->type = MKOP(LARX, 0, 1);
2201 case 694: /* stbcx. */
2202 op->type = MKOP(STCX, 0, 1);
2205 case 116: /* lharx */
2206 op->type = MKOP(LARX, 0, 2);
2209 case 726: /* sthcx. */
2210 op->type = MKOP(STCX, 0, 2);
2213 case 276: /* lqarx */
2214 if (!((rd & 1) || rd == ra || rd == rb))
2215 op->type = MKOP(LARX, 0, 16);
2218 case 182: /* stqcx. */
2220 op->type = MKOP(STCX, 0, 16);
2225 case 55: /* lwzux */
2226 op->type = MKOP(LOAD, u, 4);
2230 case 119: /* lbzux */
2231 op->type = MKOP(LOAD, u, 1);
2234 #ifdef CONFIG_ALTIVEC
2236 * Note: for the load/store vector element instructions,
2237 * bits of the EA say which field of the VMX register to use.
2240 op->type = MKOP(LOAD_VMX, 0, 1);
2241 op->element_size = 1;
2244 case 39: /* lvehx */
2245 op->type = MKOP(LOAD_VMX, 0, 2);
2246 op->element_size = 2;
2249 case 71: /* lvewx */
2250 op->type = MKOP(LOAD_VMX, 0, 4);
2251 op->element_size = 4;
2255 case 359: /* lvxl */
2256 op->type = MKOP(LOAD_VMX, 0, 16);
2257 op->element_size = 16;
2260 case 135: /* stvebx */
2261 op->type = MKOP(STORE_VMX, 0, 1);
2262 op->element_size = 1;
2265 case 167: /* stvehx */
2266 op->type = MKOP(STORE_VMX, 0, 2);
2267 op->element_size = 2;
2270 case 199: /* stvewx */
2271 op->type = MKOP(STORE_VMX, 0, 4);
2272 op->element_size = 4;
2275 case 231: /* stvx */
2276 case 487: /* stvxl */
2277 op->type = MKOP(STORE_VMX, 0, 16);
2279 #endif /* CONFIG_ALTIVEC */
2281 #ifdef __powerpc64__
2284 op->type = MKOP(LOAD, u, 8);
2287 case 149: /* stdx */
2288 case 181: /* stdux */
2289 op->type = MKOP(STORE, u, 8);
2293 case 151: /* stwx */
2294 case 183: /* stwux */
2295 op->type = MKOP(STORE, u, 4);
2298 case 215: /* stbx */
2299 case 247: /* stbux */
2300 op->type = MKOP(STORE, u, 1);
2303 case 279: /* lhzx */
2304 case 311: /* lhzux */
2305 op->type = MKOP(LOAD, u, 2);
2308 #ifdef __powerpc64__
2309 case 341: /* lwax */
2310 case 373: /* lwaux */
2311 op->type = MKOP(LOAD, SIGNEXT | u, 4);
2315 case 343: /* lhax */
2316 case 375: /* lhaux */
2317 op->type = MKOP(LOAD, SIGNEXT | u, 2);
2320 case 407: /* sthx */
2321 case 439: /* sthux */
2322 op->type = MKOP(STORE, u, 2);
2325 #ifdef __powerpc64__
2326 case 532: /* ldbrx */
2327 op->type = MKOP(LOAD, BYTEREV, 8);
2331 case 533: /* lswx */
2332 op->type = MKOP(LOAD_MULTI, 0, regs->xer & 0x7f);
2335 case 534: /* lwbrx */
2336 op->type = MKOP(LOAD, BYTEREV, 4);
2339 case 597: /* lswi */
2341 rb = 32; /* # bytes to load */
2342 op->type = MKOP(LOAD_MULTI, 0, rb);
2343 op->ea = ra ? regs->gpr[ra] : 0;
2346 #ifdef CONFIG_PPC_FPU
2347 case 535: /* lfsx */
2348 case 567: /* lfsux */
2349 op->type = MKOP(LOAD_FP, u | FPCONV, 4);
2352 case 599: /* lfdx */
2353 case 631: /* lfdux */
2354 op->type = MKOP(LOAD_FP, u, 8);
2357 case 663: /* stfsx */
2358 case 695: /* stfsux */
2359 op->type = MKOP(STORE_FP, u | FPCONV, 4);
2362 case 727: /* stfdx */
2363 case 759: /* stfdux */
2364 op->type = MKOP(STORE_FP, u, 8);
2367 #ifdef __powerpc64__
2368 case 791: /* lfdpx */
2369 op->type = MKOP(LOAD_FP, 0, 16);
2372 case 855: /* lfiwax */
2373 op->type = MKOP(LOAD_FP, SIGNEXT, 4);
2376 case 887: /* lfiwzx */
2377 op->type = MKOP(LOAD_FP, 0, 4);
2380 case 919: /* stfdpx */
2381 op->type = MKOP(STORE_FP, 0, 16);
2384 case 983: /* stfiwx */
2385 op->type = MKOP(STORE_FP, 0, 4);
2387 #endif /* __powerpc64 */
2388 #endif /* CONFIG_PPC_FPU */
2390 #ifdef __powerpc64__
2391 case 660: /* stdbrx */
2392 op->type = MKOP(STORE, BYTEREV, 8);
2393 op->val = byterev_8(regs->gpr[rd]);
2397 case 661: /* stswx */
2398 op->type = MKOP(STORE_MULTI, 0, regs->xer & 0x7f);
2401 case 662: /* stwbrx */
2402 op->type = MKOP(STORE, BYTEREV, 4);
2403 op->val = byterev_4(regs->gpr[rd]);
2406 case 725: /* stswi */
2408 rb = 32; /* # bytes to store */
2409 op->type = MKOP(STORE_MULTI, 0, rb);
2410 op->ea = ra ? regs->gpr[ra] : 0;
2413 case 790: /* lhbrx */
2414 op->type = MKOP(LOAD, BYTEREV, 2);
2417 case 918: /* sthbrx */
2418 op->type = MKOP(STORE, BYTEREV, 2);
2419 op->val = byterev_2(regs->gpr[rd]);
2423 case 12: /* lxsiwzx */
2424 op->reg = rd | ((word & 1) << 5);
2425 op->type = MKOP(LOAD_VSX, 0, 4);
2426 op->element_size = 8;
2429 case 76: /* lxsiwax */
2430 op->reg = rd | ((word & 1) << 5);
2431 op->type = MKOP(LOAD_VSX, SIGNEXT, 4);
2432 op->element_size = 8;
2435 case 140: /* stxsiwx */
2436 op->reg = rd | ((word & 1) << 5);
2437 op->type = MKOP(STORE_VSX, 0, 4);
2438 op->element_size = 8;
2441 case 268: /* lxvx */
2442 op->reg = rd | ((word & 1) << 5);
2443 op->type = MKOP(LOAD_VSX, 0, 16);
2444 op->element_size = 16;
2445 op->vsx_flags = VSX_CHECK_VEC;
2448 case 269: /* lxvl */
2449 case 301: { /* lxvll */
2451 op->reg = rd | ((word & 1) << 5);
2452 op->ea = ra ? regs->gpr[ra] : 0;
2453 nb = regs->gpr[rb] & 0xff;
2456 op->type = MKOP(LOAD_VSX, 0, nb);
2457 op->element_size = 16;
2458 op->vsx_flags = ((word & 0x20) ? VSX_LDLEFT : 0) |
2462 case 332: /* lxvdsx */
2463 op->reg = rd | ((word & 1) << 5);
2464 op->type = MKOP(LOAD_VSX, 0, 8);
2465 op->element_size = 8;
2466 op->vsx_flags = VSX_SPLAT;
2469 case 333: /* lxvpx */
2470 if (!cpu_has_feature(CPU_FTR_ARCH_31))
2472 op->reg = VSX_REGISTER_XTP(rd);
2473 op->type = MKOP(LOAD_VSX, 0, 32);
2474 op->element_size = 32;
2477 case 364: /* lxvwsx */
2478 op->reg = rd | ((word & 1) << 5);
2479 op->type = MKOP(LOAD_VSX, 0, 4);
2480 op->element_size = 4;
2481 op->vsx_flags = VSX_SPLAT | VSX_CHECK_VEC;
2484 case 396: /* stxvx */
2485 op->reg = rd | ((word & 1) << 5);
2486 op->type = MKOP(STORE_VSX, 0, 16);
2487 op->element_size = 16;
2488 op->vsx_flags = VSX_CHECK_VEC;
2491 case 397: /* stxvl */
2492 case 429: { /* stxvll */
2494 op->reg = rd | ((word & 1) << 5);
2495 op->ea = ra ? regs->gpr[ra] : 0;
2496 nb = regs->gpr[rb] & 0xff;
2499 op->type = MKOP(STORE_VSX, 0, nb);
2500 op->element_size = 16;
2501 op->vsx_flags = ((word & 0x20) ? VSX_LDLEFT : 0) |
2505 case 461: /* stxvpx */
2506 if (!cpu_has_feature(CPU_FTR_ARCH_31))
2508 op->reg = VSX_REGISTER_XTP(rd);
2509 op->type = MKOP(STORE_VSX, 0, 32);
2510 op->element_size = 32;
2512 case 524: /* lxsspx */
2513 op->reg = rd | ((word & 1) << 5);
2514 op->type = MKOP(LOAD_VSX, 0, 4);
2515 op->element_size = 8;
2516 op->vsx_flags = VSX_FPCONV;
2519 case 588: /* lxsdx */
2520 op->reg = rd | ((word & 1) << 5);
2521 op->type = MKOP(LOAD_VSX, 0, 8);
2522 op->element_size = 8;
2525 case 652: /* stxsspx */
2526 op->reg = rd | ((word & 1) << 5);
2527 op->type = MKOP(STORE_VSX, 0, 4);
2528 op->element_size = 8;
2529 op->vsx_flags = VSX_FPCONV;
2532 case 716: /* stxsdx */
2533 op->reg = rd | ((word & 1) << 5);
2534 op->type = MKOP(STORE_VSX, 0, 8);
2535 op->element_size = 8;
2538 case 780: /* lxvw4x */
2539 op->reg = rd | ((word & 1) << 5);
2540 op->type = MKOP(LOAD_VSX, 0, 16);
2541 op->element_size = 4;
2544 case 781: /* lxsibzx */
2545 op->reg = rd | ((word & 1) << 5);
2546 op->type = MKOP(LOAD_VSX, 0, 1);
2547 op->element_size = 8;
2548 op->vsx_flags = VSX_CHECK_VEC;
2551 case 812: /* lxvh8x */
2552 op->reg = rd | ((word & 1) << 5);
2553 op->type = MKOP(LOAD_VSX, 0, 16);
2554 op->element_size = 2;
2555 op->vsx_flags = VSX_CHECK_VEC;
2558 case 813: /* lxsihzx */
2559 op->reg = rd | ((word & 1) << 5);
2560 op->type = MKOP(LOAD_VSX, 0, 2);
2561 op->element_size = 8;
2562 op->vsx_flags = VSX_CHECK_VEC;
2565 case 844: /* lxvd2x */
2566 op->reg = rd | ((word & 1) << 5);
2567 op->type = MKOP(LOAD_VSX, 0, 16);
2568 op->element_size = 8;
2571 case 876: /* lxvb16x */
2572 op->reg = rd | ((word & 1) << 5);
2573 op->type = MKOP(LOAD_VSX, 0, 16);
2574 op->element_size = 1;
2575 op->vsx_flags = VSX_CHECK_VEC;
2578 case 908: /* stxvw4x */
2579 op->reg = rd | ((word & 1) << 5);
2580 op->type = MKOP(STORE_VSX, 0, 16);
2581 op->element_size = 4;
2584 case 909: /* stxsibx */
2585 op->reg = rd | ((word & 1) << 5);
2586 op->type = MKOP(STORE_VSX, 0, 1);
2587 op->element_size = 8;
2588 op->vsx_flags = VSX_CHECK_VEC;
2591 case 940: /* stxvh8x */
2592 op->reg = rd | ((word & 1) << 5);
2593 op->type = MKOP(STORE_VSX, 0, 16);
2594 op->element_size = 2;
2595 op->vsx_flags = VSX_CHECK_VEC;
2598 case 941: /* stxsihx */
2599 op->reg = rd | ((word & 1) << 5);
2600 op->type = MKOP(STORE_VSX, 0, 2);
2601 op->element_size = 8;
2602 op->vsx_flags = VSX_CHECK_VEC;
2605 case 972: /* stxvd2x */
2606 op->reg = rd | ((word & 1) << 5);
2607 op->type = MKOP(STORE_VSX, 0, 16);
2608 op->element_size = 8;
2611 case 1004: /* stxvb16x */
2612 op->reg = rd | ((word & 1) << 5);
2613 op->type = MKOP(STORE_VSX, 0, 16);
2614 op->element_size = 1;
2615 op->vsx_flags = VSX_CHECK_VEC;
2618 #endif /* CONFIG_VSX */
2624 op->type = MKOP(LOAD, u, 4);
2625 op->ea = dform_ea(word, regs);
2630 op->type = MKOP(LOAD, u, 1);
2631 op->ea = dform_ea(word, regs);
2636 op->type = MKOP(STORE, u, 4);
2637 op->ea = dform_ea(word, regs);
2642 op->type = MKOP(STORE, u, 1);
2643 op->ea = dform_ea(word, regs);
2648 op->type = MKOP(LOAD, u, 2);
2649 op->ea = dform_ea(word, regs);
2654 op->type = MKOP(LOAD, SIGNEXT | u, 2);
2655 op->ea = dform_ea(word, regs);
2660 op->type = MKOP(STORE, u, 2);
2661 op->ea = dform_ea(word, regs);
2666 break; /* invalid form, ra in range to load */
2667 op->type = MKOP(LOAD_MULTI, 0, 4 * (32 - rd));
2668 op->ea = dform_ea(word, regs);
2672 op->type = MKOP(STORE_MULTI, 0, 4 * (32 - rd));
2673 op->ea = dform_ea(word, regs);
2676 #ifdef CONFIG_PPC_FPU
2679 op->type = MKOP(LOAD_FP, u | FPCONV, 4);
2680 op->ea = dform_ea(word, regs);
2685 op->type = MKOP(LOAD_FP, u, 8);
2686 op->ea = dform_ea(word, regs);
2690 case 53: /* stfsu */
2691 op->type = MKOP(STORE_FP, u | FPCONV, 4);
2692 op->ea = dform_ea(word, regs);
2696 case 55: /* stfdu */
2697 op->type = MKOP(STORE_FP, u, 8);
2698 op->ea = dform_ea(word, regs);
2702 #ifdef __powerpc64__
2704 if (!((rd & 1) || (rd == ra)))
2705 op->type = MKOP(LOAD, 0, 16);
2706 op->ea = dqform_ea(word, regs);
2711 case 57: /* lfdp, lxsd, lxssp */
2712 op->ea = dsform_ea(word, regs);
2716 break; /* reg must be even */
2717 op->type = MKOP(LOAD_FP, 0, 16);
2721 op->type = MKOP(LOAD_VSX, 0, 8);
2722 op->element_size = 8;
2723 op->vsx_flags = VSX_CHECK_VEC;
2727 op->type = MKOP(LOAD_VSX, 0, 4);
2728 op->element_size = 8;
2729 op->vsx_flags = VSX_FPCONV | VSX_CHECK_VEC;
2733 #endif /* CONFIG_VSX */
2735 #ifdef __powerpc64__
2736 case 58: /* ld[u], lwa */
2737 op->ea = dsform_ea(word, regs);
2740 op->type = MKOP(LOAD, 0, 8);
2743 op->type = MKOP(LOAD, UPDATE, 8);
2746 op->type = MKOP(LOAD, SIGNEXT, 4);
2754 if (!cpu_has_feature(CPU_FTR_ARCH_31))
2756 op->ea = dqform_ea(word, regs);
2757 op->reg = VSX_REGISTER_XTP(rd);
2758 op->element_size = 32;
2759 switch (word & 0xf) {
2761 op->type = MKOP(LOAD_VSX, 0, 32);
2764 op->type = MKOP(STORE_VSX, 0, 32);
2769 case 61: /* stfdp, lxv, stxsd, stxssp, stxv */
2771 case 0: /* stfdp with LSB of DS field = 0 */
2772 case 4: /* stfdp with LSB of DS field = 1 */
2773 op->ea = dsform_ea(word, regs);
2774 op->type = MKOP(STORE_FP, 0, 16);
2778 op->ea = dqform_ea(word, regs);
2781 op->type = MKOP(LOAD_VSX, 0, 16);
2782 op->element_size = 16;
2783 op->vsx_flags = VSX_CHECK_VEC;
2786 case 2: /* stxsd with LSB of DS field = 0 */
2787 case 6: /* stxsd with LSB of DS field = 1 */
2788 op->ea = dsform_ea(word, regs);
2790 op->type = MKOP(STORE_VSX, 0, 8);
2791 op->element_size = 8;
2792 op->vsx_flags = VSX_CHECK_VEC;
2795 case 3: /* stxssp with LSB of DS field = 0 */
2796 case 7: /* stxssp with LSB of DS field = 1 */
2797 op->ea = dsform_ea(word, regs);
2799 op->type = MKOP(STORE_VSX, 0, 4);
2800 op->element_size = 8;
2801 op->vsx_flags = VSX_FPCONV | VSX_CHECK_VEC;
2805 op->ea = dqform_ea(word, regs);
2808 op->type = MKOP(STORE_VSX, 0, 16);
2809 op->element_size = 16;
2810 op->vsx_flags = VSX_CHECK_VEC;
2814 #endif /* CONFIG_VSX */
2816 #ifdef __powerpc64__
2817 case 62: /* std[u] */
2818 op->ea = dsform_ea(word, regs);
2821 op->type = MKOP(STORE, 0, 8);
2824 op->type = MKOP(STORE, UPDATE, 8);
2828 op->type = MKOP(STORE, 0, 16);
2832 case 1: /* Prefixed instructions */
2833 if (!cpu_has_feature(CPU_FTR_ARCH_31))
2836 prefix_r = GET_PREFIX_R(word);
2837 ra = GET_PREFIX_RA(suffix);
2838 op->update_reg = ra;
2839 rd = (suffix >> 21) & 0x1f;
2841 op->val = regs->gpr[rd];
2843 suffixopcode = get_op(suffix);
2844 prefixtype = (word >> 24) & 0x3;
2845 switch (prefixtype) {
2846 case 0: /* Type 00 Eight-Byte Load/Store */
2849 op->ea = mlsd_8lsd_ea(word, suffix, regs);
2850 switch (suffixopcode) {
2852 op->type = MKOP(LOAD, PREFIXED | SIGNEXT, 4);
2855 case 42: /* plxsd */
2857 op->type = MKOP(LOAD_VSX, PREFIXED, 8);
2858 op->element_size = 8;
2859 op->vsx_flags = VSX_CHECK_VEC;
2861 case 43: /* plxssp */
2863 op->type = MKOP(LOAD_VSX, PREFIXED, 4);
2864 op->element_size = 8;
2865 op->vsx_flags = VSX_FPCONV | VSX_CHECK_VEC;
2867 case 46: /* pstxsd */
2869 op->type = MKOP(STORE_VSX, PREFIXED, 8);
2870 op->element_size = 8;
2871 op->vsx_flags = VSX_CHECK_VEC;
2873 case 47: /* pstxssp */
2875 op->type = MKOP(STORE_VSX, PREFIXED, 4);
2876 op->element_size = 8;
2877 op->vsx_flags = VSX_FPCONV | VSX_CHECK_VEC;
2879 case 51: /* plxv1 */
2882 case 50: /* plxv0 */
2883 op->type = MKOP(LOAD_VSX, PREFIXED, 16);
2884 op->element_size = 16;
2885 op->vsx_flags = VSX_CHECK_VEC;
2887 case 55: /* pstxv1 */
2890 case 54: /* pstxv0 */
2891 op->type = MKOP(STORE_VSX, PREFIXED, 16);
2892 op->element_size = 16;
2893 op->vsx_flags = VSX_CHECK_VEC;
2895 #endif /* CONFIG_VSX */
2897 op->type = MKOP(LOAD, PREFIXED, 16);
2900 op->type = MKOP(LOAD, PREFIXED, 8);
2903 case 58: /* plxvp */
2904 op->reg = VSX_REGISTER_XTP(rd);
2905 op->type = MKOP(LOAD_VSX, PREFIXED, 32);
2906 op->element_size = 32;
2908 #endif /* CONFIG_VSX */
2910 op->type = MKOP(STORE, PREFIXED, 16);
2913 op->type = MKOP(STORE, PREFIXED, 8);
2916 case 62: /* pstxvp */
2917 op->reg = VSX_REGISTER_XTP(rd);
2918 op->type = MKOP(STORE_VSX, PREFIXED, 32);
2919 op->element_size = 32;
2921 #endif /* CONFIG_VSX */
2924 case 1: /* Type 01 Eight-Byte Register-to-Register */
2926 case 2: /* Type 10 Modified Load/Store */
2929 op->ea = mlsd_8lsd_ea(word, suffix, regs);
2930 switch (suffixopcode) {
2932 op->type = MKOP(LOAD, PREFIXED, 4);
2935 op->type = MKOP(LOAD, PREFIXED, 1);
2938 op->type = MKOP(STORE, PREFIXED, 4);
2941 op->type = MKOP(STORE, PREFIXED, 1);
2944 op->type = MKOP(LOAD, PREFIXED, 2);
2947 op->type = MKOP(LOAD, PREFIXED | SIGNEXT, 2);
2950 op->type = MKOP(STORE, PREFIXED, 2);
2953 op->type = MKOP(LOAD_FP, PREFIXED | FPCONV, 4);
2956 op->type = MKOP(LOAD_FP, PREFIXED, 8);
2958 case 52: /* pstfs */
2959 op->type = MKOP(STORE_FP, PREFIXED | FPCONV, 4);
2961 case 54: /* pstfd */
2962 op->type = MKOP(STORE_FP, PREFIXED, 8);
2966 case 3: /* Type 11 Modified Register-to-Register */
2969 #endif /* __powerpc64__ */
2974 if ((GETTYPE(op->type) == LOAD_VSX ||
2975 GETTYPE(op->type) == STORE_VSX) &&
2976 !cpu_has_feature(CPU_FTR_VSX)) {
2979 #endif /* CONFIG_VSX */
3000 op->type = INTERRUPT | 0x700;
3001 op->val = SRR1_PROGPRIV;
3005 op->type = INTERRUPT | 0x700;
3006 op->val = SRR1_PROGTRAP;
3009 EXPORT_SYMBOL_GPL(analyse_instr);
3010 NOKPROBE_SYMBOL(analyse_instr);
3013 * For PPC32 we always use stwu with r1 to change the stack pointer.
3014 * So this emulated store may corrupt the exception frame, now we
3015 * have to provide the exception frame trampoline, which is pushed
3016 * below the kprobed function stack. So we only update gpr[1] but
3017 * don't emulate the real store operation. We will do real store
3018 * operation safely in exception return code by checking this flag.
3020 static nokprobe_inline int handle_stack_update(unsigned long ea, struct pt_regs *regs)
3024 * Check if we will touch kernel stack overflow
3026 if (ea - STACK_INT_FRAME_SIZE <= current->thread.ksp_limit) {
3027 printk(KERN_CRIT "Can't kprobe this since kernel stack would overflow.\n");
3030 #endif /* CONFIG_PPC32 */
3032 * Check if we already set since that means we'll
3033 * lose the previous value.
3035 WARN_ON(test_thread_flag(TIF_EMULATE_STACK_STORE));
3036 set_thread_flag(TIF_EMULATE_STACK_STORE);
3040 static nokprobe_inline void do_signext(unsigned long *valp, int size)
3044 *valp = (signed short) *valp;
3047 *valp = (signed int) *valp;
3052 static nokprobe_inline void do_byterev(unsigned long *valp, int size)
3056 *valp = byterev_2(*valp);
3059 *valp = byterev_4(*valp);
3061 #ifdef __powerpc64__
3063 *valp = byterev_8(*valp);
3070 * Emulate an instruction that can be executed just by updating
3073 void emulate_update_regs(struct pt_regs *regs, struct instruction_op *op)
3075 unsigned long next_pc;
3077 next_pc = truncate_if_32bit(regs->msr, regs->nip + GETLENGTH(op->type));
3078 switch (GETTYPE(op->type)) {
3080 if (op->type & SETREG)
3081 regs->gpr[op->reg] = op->val;
3082 if (op->type & SETCC)
3083 regs->ccr = op->ccval;
3084 if (op->type & SETXER)
3085 regs->xer = op->xerval;
3089 if (op->type & SETLK)
3090 regs->link = next_pc;
3091 if (op->type & BRTAKEN)
3093 if (op->type & DECCTR)
3098 switch (op->type & BARRIER_MASK) {
3108 case BARRIER_LWSYNC:
3109 asm volatile("lwsync" : : : "memory");
3111 case BARRIER_PTESYNC:
3112 asm volatile("ptesync" : : : "memory");
3120 regs->gpr[op->reg] = regs->xer & 0xffffffffUL;
3123 regs->gpr[op->reg] = regs->link;
3126 regs->gpr[op->reg] = regs->ctr;
3136 regs->xer = op->val & 0xffffffffUL;
3139 regs->link = op->val;
3142 regs->ctr = op->val;
3152 regs->nip = next_pc;
3154 NOKPROBE_SYMBOL(emulate_update_regs);
3157 * Emulate a previously-analysed load or store instruction.
3158 * Return values are:
3159 * 0 = instruction emulated successfully
3160 * -EFAULT = address out of range or access faulted (regs->dar
3161 * contains the faulting address)
3162 * -EACCES = misaligned access, instruction requires alignment
3163 * -EINVAL = unknown operation in *op
3165 int emulate_loadstore(struct pt_regs *regs, struct instruction_op *op)
3167 int err, size, type;
3175 size = GETSIZE(op->type);
3176 type = GETTYPE(op->type);
3177 cross_endian = (regs->msr & MSR_LE) != (MSR_KERNEL & MSR_LE);
3178 ea = truncate_if_32bit(regs->msr, op->ea);
3182 if (ea & (size - 1))
3183 return -EACCES; /* can't handle misaligned */
3184 if (!address_ok(regs, ea, size))
3189 #ifdef __powerpc64__
3191 __get_user_asmx(val, ea, err, "lbarx");
3194 __get_user_asmx(val, ea, err, "lharx");
3198 __get_user_asmx(val, ea, err, "lwarx");
3200 #ifdef __powerpc64__
3202 __get_user_asmx(val, ea, err, "ldarx");
3205 err = do_lqarx(ea, ®s->gpr[op->reg]);
3216 regs->gpr[op->reg] = val;
3220 if (ea & (size - 1))
3221 return -EACCES; /* can't handle misaligned */
3222 if (!address_ok(regs, ea, size))
3226 #ifdef __powerpc64__
3228 __put_user_asmx(op->val, ea, err, "stbcx.", cr);
3231 __put_user_asmx(op->val, ea, err, "stbcx.", cr);
3235 __put_user_asmx(op->val, ea, err, "stwcx.", cr);
3237 #ifdef __powerpc64__
3239 __put_user_asmx(op->val, ea, err, "stdcx.", cr);
3242 err = do_stqcx(ea, regs->gpr[op->reg],
3243 regs->gpr[op->reg + 1], &cr);
3250 regs->ccr = (regs->ccr & 0x0fffffff) |
3252 ((regs->xer >> 3) & 0x10000000);
3258 #ifdef __powerpc64__
3260 err = emulate_lq(regs, ea, op->reg, cross_endian);
3264 err = read_mem(®s->gpr[op->reg], ea, size, regs);
3266 if (op->type & SIGNEXT)
3267 do_signext(®s->gpr[op->reg], size);
3268 if ((op->type & BYTEREV) == (cross_endian ? 0 : BYTEREV))
3269 do_byterev(®s->gpr[op->reg], size);
3273 #ifdef CONFIG_PPC_FPU
3276 * If the instruction is in userspace, we can emulate it even
3277 * if the VMX state is not live, because we have the state
3278 * stored in the thread_struct. If the instruction is in
3279 * the kernel, we must not touch the state in the thread_struct.
3281 if (!(regs->msr & MSR_PR) && !(regs->msr & MSR_FP))
3283 err = do_fp_load(op, ea, regs, cross_endian);
3286 #ifdef CONFIG_ALTIVEC
3288 if (!(regs->msr & MSR_PR) && !(regs->msr & MSR_VEC))
3290 err = do_vec_load(op->reg, ea, size, regs, cross_endian);
3295 unsigned long msrbit = MSR_VSX;
3298 * Some VSX instructions check the MSR_VEC bit rather than MSR_VSX
3299 * when the target of the instruction is a vector register.
3301 if (op->reg >= 32 && (op->vsx_flags & VSX_CHECK_VEC))
3303 if (!(regs->msr & MSR_PR) && !(regs->msr & msrbit))
3305 err = do_vsx_load(op, ea, regs, cross_endian);
3310 if (!address_ok(regs, ea, size))
3313 for (i = 0; i < size; i += 4) {
3314 unsigned int v32 = 0;
3319 err = copy_mem_in((u8 *) &v32, ea, nb, regs);
3322 if (unlikely(cross_endian))
3323 v32 = byterev_4(v32);
3324 regs->gpr[rd] = v32;
3326 /* reg number wraps from 31 to 0 for lsw[ix] */
3327 rd = (rd + 1) & 0x1f;
3332 #ifdef __powerpc64__
3334 err = emulate_stq(regs, ea, op->reg, cross_endian);
3338 if ((op->type & UPDATE) && size == sizeof(long) &&
3339 op->reg == 1 && op->update_reg == 1 &&
3340 !(regs->msr & MSR_PR) &&
3341 ea >= regs->gpr[1] - STACK_INT_FRAME_SIZE) {
3342 err = handle_stack_update(ea, regs);
3345 if (unlikely(cross_endian))
3346 do_byterev(&op->val, size);
3347 err = write_mem(op->val, ea, size, regs);
3350 #ifdef CONFIG_PPC_FPU
3352 if (!(regs->msr & MSR_PR) && !(regs->msr & MSR_FP))
3354 err = do_fp_store(op, ea, regs, cross_endian);
3357 #ifdef CONFIG_ALTIVEC
3359 if (!(regs->msr & MSR_PR) && !(regs->msr & MSR_VEC))
3361 err = do_vec_store(op->reg, ea, size, regs, cross_endian);
3366 unsigned long msrbit = MSR_VSX;
3369 * Some VSX instructions check the MSR_VEC bit rather than MSR_VSX
3370 * when the target of the instruction is a vector register.
3372 if (op->reg >= 32 && (op->vsx_flags & VSX_CHECK_VEC))
3374 if (!(regs->msr & MSR_PR) && !(regs->msr & msrbit))
3376 err = do_vsx_store(op, ea, regs, cross_endian);
3381 if (!address_ok(regs, ea, size))
3384 for (i = 0; i < size; i += 4) {
3385 unsigned int v32 = regs->gpr[rd];
3390 if (unlikely(cross_endian))
3391 v32 = byterev_4(v32);
3392 err = copy_mem_out((u8 *) &v32, ea, nb, regs);
3396 /* reg number wraps from 31 to 0 for stsw[ix] */
3397 rd = (rd + 1) & 0x1f;
3408 if (op->type & UPDATE)
3409 regs->gpr[op->update_reg] = op->ea;
3413 NOKPROBE_SYMBOL(emulate_loadstore);
3416 * Emulate instructions that cause a transfer of control,
3417 * loads and stores, and a few other instructions.
3418 * Returns 1 if the step was emulated, 0 if not,
3419 * or -1 if the instruction is one that should not be stepped,
3420 * such as an rfid, or a mtmsrd that would clear MSR_RI.
3422 int emulate_step(struct pt_regs *regs, struct ppc_inst instr)
3424 struct instruction_op op;
3429 r = analyse_instr(&op, regs, instr);
3433 emulate_update_regs(regs, &op);
3438 type = GETTYPE(op.type);
3440 if (OP_IS_LOAD_STORE(type)) {
3441 err = emulate_loadstore(regs, &op);
3449 ea = truncate_if_32bit(regs->msr, op.ea);
3450 if (!address_ok(regs, ea, 8))
3452 switch (op.type & CACHEOP_MASK) {
3454 __cacheop_user_asmx(ea, err, "dcbst");
3457 __cacheop_user_asmx(ea, err, "dcbf");
3461 prefetchw((void *) ea);
3465 prefetch((void *) ea);
3468 __cacheop_user_asmx(ea, err, "icbi");
3471 err = emulate_dcbz(ea, regs);
3481 regs->gpr[op.reg] = regs->msr & MSR_MASK;
3485 val = regs->gpr[op.reg];
3486 if ((val & MSR_RI) == 0)
3487 /* can't step mtmsr[d] that would clear MSR_RI */
3489 /* here op.val is the mask of bits to change */
3490 regs->msr = (regs->msr & ~op.val) | (val & op.val);
3494 case SYSCALL: /* sc */
3496 * N.B. this uses knowledge about how the syscall
3497 * entry code works. If that is changed, this will
3498 * need to be changed also.
3500 if (IS_ENABLED(CONFIG_PPC_FAST_ENDIAN_SWITCH) &&
3501 cpu_has_feature(CPU_FTR_REAL_LE) &&
3502 regs->gpr[0] == 0x1ebe) {
3503 regs->msr ^= MSR_LE;
3506 regs->gpr[9] = regs->gpr[13];
3507 regs->gpr[10] = MSR_KERNEL;
3508 regs->gpr[11] = regs->nip + 4;
3509 regs->gpr[12] = regs->msr & MSR_MASK;
3510 regs->gpr[13] = (unsigned long) get_paca();
3511 regs->nip = (unsigned long) &system_call_common;
3512 regs->msr = MSR_KERNEL;
3515 #ifdef CONFIG_PPC_BOOK3S_64
3516 case SYSCALL_VECTORED_0: /* scv 0 */
3517 regs->gpr[9] = regs->gpr[13];
3518 regs->gpr[10] = MSR_KERNEL;
3519 regs->gpr[11] = regs->nip + 4;
3520 regs->gpr[12] = regs->msr & MSR_MASK;
3521 regs->gpr[13] = (unsigned long) get_paca();
3522 regs->nip = (unsigned long) &system_call_vectored_emulate;
3523 regs->msr = MSR_KERNEL;
3534 regs->nip = truncate_if_32bit(regs->msr, regs->nip + GETLENGTH(op.type));
3537 NOKPROBE_SYMBOL(emulate_step);