GNU Linux-libre 6.1.90-gnu
[releases.git] / arch / powerpc / lib / sstep.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Single-step support.
4  *
5  * Copyright (C) 2004 Paul Mackerras <paulus@au.ibm.com>, IBM
6  */
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>
17
18 #ifdef CONFIG_PPC64
19 /* Bits in SRR1 that are copied from MSR */
20 #define MSR_MASK        0xffffffff87c0ffffUL
21 #else
22 #define MSR_MASK        0x87c0ffff
23 #endif
24
25 /* Bits in XER */
26 #define XER_SO          0x80000000U
27 #define XER_OV          0x40000000U
28 #define XER_CA          0x20000000U
29 #define XER_OV32        0x00080000U
30 #define XER_CA32        0x00040000U
31
32 #ifdef CONFIG_VSX
33 #define VSX_REGISTER_XTP(rd)   ((((rd) & 1) << 5) | ((rd) & 0xfe))
34 #endif
35
36 #ifdef CONFIG_PPC_FPU
37 /*
38  * Functions in ldstfp.S
39  */
40 extern void get_fpr(int rn, double *p);
41 extern void put_fpr(int rn, const double *p);
42 extern void get_vr(int rn, __vector128 *p);
43 extern void put_vr(int rn, __vector128 *p);
44 extern void load_vsrn(int vsr, const void *p);
45 extern void store_vsrn(int vsr, void *p);
46 extern void conv_sp_to_dp(const float *sp, double *dp);
47 extern void conv_dp_to_sp(const double *dp, float *sp);
48 #endif
49
50 #ifdef __powerpc64__
51 /*
52  * Functions in quad.S
53  */
54 extern int do_lq(unsigned long ea, unsigned long *regs);
55 extern int do_stq(unsigned long ea, unsigned long val0, unsigned long val1);
56 extern int do_lqarx(unsigned long ea, unsigned long *regs);
57 extern int do_stqcx(unsigned long ea, unsigned long val0, unsigned long val1,
58                     unsigned int *crp);
59 #endif
60
61 #ifdef __LITTLE_ENDIAN__
62 #define IS_LE   1
63 #define IS_BE   0
64 #else
65 #define IS_LE   0
66 #define IS_BE   1
67 #endif
68
69 /*
70  * Emulate the truncation of 64 bit values in 32-bit mode.
71  */
72 static nokprobe_inline unsigned long truncate_if_32bit(unsigned long msr,
73                                                         unsigned long val)
74 {
75         if ((msr & MSR_64BIT) == 0)
76                 val &= 0xffffffffUL;
77         return val;
78 }
79
80 /*
81  * Determine whether a conditional branch instruction would branch.
82  */
83 static nokprobe_inline int branch_taken(unsigned int instr,
84                                         const struct pt_regs *regs,
85                                         struct instruction_op *op)
86 {
87         unsigned int bo = (instr >> 21) & 0x1f;
88         unsigned int bi;
89
90         if ((bo & 4) == 0) {
91                 /* decrement counter */
92                 op->type |= DECCTR;
93                 if (((bo >> 1) & 1) ^ (regs->ctr == 1))
94                         return 0;
95         }
96         if ((bo & 0x10) == 0) {
97                 /* check bit from CR */
98                 bi = (instr >> 16) & 0x1f;
99                 if (((regs->ccr >> (31 - bi)) & 1) != ((bo >> 3) & 1))
100                         return 0;
101         }
102         return 1;
103 }
104
105 static nokprobe_inline long address_ok(struct pt_regs *regs,
106                                        unsigned long ea, int nb)
107 {
108         if (!user_mode(regs))
109                 return 1;
110         if (access_ok((void __user *)ea, nb))
111                 return 1;
112         if (access_ok((void __user *)ea, 1))
113                 /* Access overlaps the end of the user region */
114                 regs->dar = TASK_SIZE_MAX - 1;
115         else
116                 regs->dar = ea;
117         return 0;
118 }
119
120 /*
121  * Calculate effective address for a D-form instruction
122  */
123 static nokprobe_inline unsigned long dform_ea(unsigned int instr,
124                                               const struct pt_regs *regs)
125 {
126         int ra;
127         unsigned long ea;
128
129         ra = (instr >> 16) & 0x1f;
130         ea = (signed short) instr;              /* sign-extend */
131         if (ra)
132                 ea += regs->gpr[ra];
133
134         return ea;
135 }
136
137 #ifdef __powerpc64__
138 /*
139  * Calculate effective address for a DS-form instruction
140  */
141 static nokprobe_inline unsigned long dsform_ea(unsigned int instr,
142                                                const struct pt_regs *regs)
143 {
144         int ra;
145         unsigned long ea;
146
147         ra = (instr >> 16) & 0x1f;
148         ea = (signed short) (instr & ~3);       /* sign-extend */
149         if (ra)
150                 ea += regs->gpr[ra];
151
152         return ea;
153 }
154
155 /*
156  * Calculate effective address for a DQ-form instruction
157  */
158 static nokprobe_inline unsigned long dqform_ea(unsigned int instr,
159                                                const struct pt_regs *regs)
160 {
161         int ra;
162         unsigned long ea;
163
164         ra = (instr >> 16) & 0x1f;
165         ea = (signed short) (instr & ~0xf);     /* sign-extend */
166         if (ra)
167                 ea += regs->gpr[ra];
168
169         return ea;
170 }
171 #endif /* __powerpc64 */
172
173 /*
174  * Calculate effective address for an X-form instruction
175  */
176 static nokprobe_inline unsigned long xform_ea(unsigned int instr,
177                                               const struct pt_regs *regs)
178 {
179         int ra, rb;
180         unsigned long ea;
181
182         ra = (instr >> 16) & 0x1f;
183         rb = (instr >> 11) & 0x1f;
184         ea = regs->gpr[rb];
185         if (ra)
186                 ea += regs->gpr[ra];
187
188         return ea;
189 }
190
191 /*
192  * Calculate effective address for a MLS:D-form / 8LS:D-form
193  * prefixed instruction
194  */
195 static nokprobe_inline unsigned long mlsd_8lsd_ea(unsigned int instr,
196                                                   unsigned int suffix,
197                                                   const struct pt_regs *regs)
198 {
199         int ra, prefix_r;
200         unsigned int  dd;
201         unsigned long ea, d0, d1, d;
202
203         prefix_r = GET_PREFIX_R(instr);
204         ra = GET_PREFIX_RA(suffix);
205
206         d0 = instr & 0x3ffff;
207         d1 = suffix & 0xffff;
208         d = (d0 << 16) | d1;
209
210         /*
211          * sign extend a 34 bit number
212          */
213         dd = (unsigned int)(d >> 2);
214         ea = (signed int)dd;
215         ea = (ea << 2) | (d & 0x3);
216
217         if (!prefix_r && ra)
218                 ea += regs->gpr[ra];
219         else if (!prefix_r && !ra)
220                 ; /* Leave ea as is */
221         else if (prefix_r)
222                 ea += regs->nip;
223
224         /*
225          * (prefix_r && ra) is an invalid form. Should already be
226          * checked for by caller!
227          */
228
229         return ea;
230 }
231
232 /*
233  * Return the largest power of 2, not greater than sizeof(unsigned long),
234  * such that x is a multiple of it.
235  */
236 static nokprobe_inline unsigned long max_align(unsigned long x)
237 {
238         x |= sizeof(unsigned long);
239         return x & -x;          /* isolates rightmost bit */
240 }
241
242 static nokprobe_inline unsigned long byterev_2(unsigned long x)
243 {
244         return ((x >> 8) & 0xff) | ((x & 0xff) << 8);
245 }
246
247 static nokprobe_inline unsigned long byterev_4(unsigned long x)
248 {
249         return ((x >> 24) & 0xff) | ((x >> 8) & 0xff00) |
250                 ((x & 0xff00) << 8) | ((x & 0xff) << 24);
251 }
252
253 #ifdef __powerpc64__
254 static nokprobe_inline unsigned long byterev_8(unsigned long x)
255 {
256         return (byterev_4(x) << 32) | byterev_4(x >> 32);
257 }
258 #endif
259
260 static nokprobe_inline void do_byte_reverse(void *ptr, int nb)
261 {
262         switch (nb) {
263         case 2:
264                 *(u16 *)ptr = byterev_2(*(u16 *)ptr);
265                 break;
266         case 4:
267                 *(u32 *)ptr = byterev_4(*(u32 *)ptr);
268                 break;
269 #ifdef __powerpc64__
270         case 8:
271                 *(unsigned long *)ptr = byterev_8(*(unsigned long *)ptr);
272                 break;
273         case 16: {
274                 unsigned long *up = (unsigned long *)ptr;
275                 unsigned long tmp;
276                 tmp = byterev_8(up[0]);
277                 up[0] = byterev_8(up[1]);
278                 up[1] = tmp;
279                 break;
280         }
281         case 32: {
282                 unsigned long *up = (unsigned long *)ptr;
283                 unsigned long tmp;
284
285                 tmp = byterev_8(up[0]);
286                 up[0] = byterev_8(up[3]);
287                 up[3] = tmp;
288                 tmp = byterev_8(up[2]);
289                 up[2] = byterev_8(up[1]);
290                 up[1] = tmp;
291                 break;
292         }
293
294 #endif
295         default:
296                 WARN_ON_ONCE(1);
297         }
298 }
299
300 static __always_inline int
301 __read_mem_aligned(unsigned long *dest, unsigned long ea, int nb, struct pt_regs *regs)
302 {
303         unsigned long x = 0;
304
305         switch (nb) {
306         case 1:
307                 unsafe_get_user(x, (unsigned char __user *)ea, Efault);
308                 break;
309         case 2:
310                 unsafe_get_user(x, (unsigned short __user *)ea, Efault);
311                 break;
312         case 4:
313                 unsafe_get_user(x, (unsigned int __user *)ea, Efault);
314                 break;
315 #ifdef __powerpc64__
316         case 8:
317                 unsafe_get_user(x, (unsigned long __user *)ea, Efault);
318                 break;
319 #endif
320         }
321         *dest = x;
322         return 0;
323
324 Efault:
325         regs->dar = ea;
326         return -EFAULT;
327 }
328
329 static nokprobe_inline int
330 read_mem_aligned(unsigned long *dest, unsigned long ea, int nb, struct pt_regs *regs)
331 {
332         int err;
333
334         if (is_kernel_addr(ea))
335                 return __read_mem_aligned(dest, ea, nb, regs);
336
337         if (user_read_access_begin((void __user *)ea, nb)) {
338                 err = __read_mem_aligned(dest, ea, nb, regs);
339                 user_read_access_end();
340         } else {
341                 err = -EFAULT;
342                 regs->dar = ea;
343         }
344
345         return err;
346 }
347
348 /*
349  * Copy from userspace to a buffer, using the largest possible
350  * aligned accesses, up to sizeof(long).
351  */
352 static __always_inline int __copy_mem_in(u8 *dest, unsigned long ea, int nb, struct pt_regs *regs)
353 {
354         int c;
355
356         for (; nb > 0; nb -= c) {
357                 c = max_align(ea);
358                 if (c > nb)
359                         c = max_align(nb);
360                 switch (c) {
361                 case 1:
362                         unsafe_get_user(*dest, (u8 __user *)ea, Efault);
363                         break;
364                 case 2:
365                         unsafe_get_user(*(u16 *)dest, (u16 __user *)ea, Efault);
366                         break;
367                 case 4:
368                         unsafe_get_user(*(u32 *)dest, (u32 __user *)ea, Efault);
369                         break;
370 #ifdef __powerpc64__
371                 case 8:
372                         unsafe_get_user(*(u64 *)dest, (u64 __user *)ea, Efault);
373                         break;
374 #endif
375                 }
376                 dest += c;
377                 ea += c;
378         }
379         return 0;
380
381 Efault:
382         regs->dar = ea;
383         return -EFAULT;
384 }
385
386 static nokprobe_inline int copy_mem_in(u8 *dest, unsigned long ea, int nb, struct pt_regs *regs)
387 {
388         int err;
389
390         if (is_kernel_addr(ea))
391                 return __copy_mem_in(dest, ea, nb, regs);
392
393         if (user_read_access_begin((void __user *)ea, nb)) {
394                 err = __copy_mem_in(dest, ea, nb, regs);
395                 user_read_access_end();
396         } else {
397                 err = -EFAULT;
398                 regs->dar = ea;
399         }
400
401         return err;
402 }
403
404 static nokprobe_inline int read_mem_unaligned(unsigned long *dest,
405                                               unsigned long ea, int nb,
406                                               struct pt_regs *regs)
407 {
408         union {
409                 unsigned long ul;
410                 u8 b[sizeof(unsigned long)];
411         } u;
412         int i;
413         int err;
414
415         u.ul = 0;
416         i = IS_BE ? sizeof(unsigned long) - nb : 0;
417         err = copy_mem_in(&u.b[i], ea, nb, regs);
418         if (!err)
419                 *dest = u.ul;
420         return err;
421 }
422
423 /*
424  * Read memory at address ea for nb bytes, return 0 for success
425  * or -EFAULT if an error occurred.  N.B. nb must be 1, 2, 4 or 8.
426  * If nb < sizeof(long), the result is right-justified on BE systems.
427  */
428 static int read_mem(unsigned long *dest, unsigned long ea, int nb,
429                               struct pt_regs *regs)
430 {
431         if (!address_ok(regs, ea, nb))
432                 return -EFAULT;
433         if ((ea & (nb - 1)) == 0)
434                 return read_mem_aligned(dest, ea, nb, regs);
435         return read_mem_unaligned(dest, ea, nb, regs);
436 }
437 NOKPROBE_SYMBOL(read_mem);
438
439 static __always_inline int
440 __write_mem_aligned(unsigned long val, unsigned long ea, int nb, struct pt_regs *regs)
441 {
442         switch (nb) {
443         case 1:
444                 unsafe_put_user(val, (unsigned char __user *)ea, Efault);
445                 break;
446         case 2:
447                 unsafe_put_user(val, (unsigned short __user *)ea, Efault);
448                 break;
449         case 4:
450                 unsafe_put_user(val, (unsigned int __user *)ea, Efault);
451                 break;
452 #ifdef __powerpc64__
453         case 8:
454                 unsafe_put_user(val, (unsigned long __user *)ea, Efault);
455                 break;
456 #endif
457         }
458         return 0;
459
460 Efault:
461         regs->dar = ea;
462         return -EFAULT;
463 }
464
465 static nokprobe_inline int
466 write_mem_aligned(unsigned long val, unsigned long ea, int nb, struct pt_regs *regs)
467 {
468         int err;
469
470         if (is_kernel_addr(ea))
471                 return __write_mem_aligned(val, ea, nb, regs);
472
473         if (user_write_access_begin((void __user *)ea, nb)) {
474                 err = __write_mem_aligned(val, ea, nb, regs);
475                 user_write_access_end();
476         } else {
477                 err = -EFAULT;
478                 regs->dar = ea;
479         }
480
481         return err;
482 }
483
484 /*
485  * Copy from a buffer to userspace, using the largest possible
486  * aligned accesses, up to sizeof(long).
487  */
488 static nokprobe_inline int __copy_mem_out(u8 *dest, unsigned long ea, int nb, struct pt_regs *regs)
489 {
490         int c;
491
492         for (; nb > 0; nb -= c) {
493                 c = max_align(ea);
494                 if (c > nb)
495                         c = max_align(nb);
496                 switch (c) {
497                 case 1:
498                         unsafe_put_user(*dest, (u8 __user *)ea, Efault);
499                         break;
500                 case 2:
501                         unsafe_put_user(*(u16 *)dest, (u16 __user *)ea, Efault);
502                         break;
503                 case 4:
504                         unsafe_put_user(*(u32 *)dest, (u32 __user *)ea, Efault);
505                         break;
506 #ifdef __powerpc64__
507                 case 8:
508                         unsafe_put_user(*(u64 *)dest, (u64 __user *)ea, Efault);
509                         break;
510 #endif
511                 }
512                 dest += c;
513                 ea += c;
514         }
515         return 0;
516
517 Efault:
518         regs->dar = ea;
519         return -EFAULT;
520 }
521
522 static nokprobe_inline int copy_mem_out(u8 *dest, unsigned long ea, int nb, struct pt_regs *regs)
523 {
524         int err;
525
526         if (is_kernel_addr(ea))
527                 return __copy_mem_out(dest, ea, nb, regs);
528
529         if (user_write_access_begin((void __user *)ea, nb)) {
530                 err = __copy_mem_out(dest, ea, nb, regs);
531                 user_write_access_end();
532         } else {
533                 err = -EFAULT;
534                 regs->dar = ea;
535         }
536
537         return err;
538 }
539
540 static nokprobe_inline int write_mem_unaligned(unsigned long val,
541                                                unsigned long ea, int nb,
542                                                struct pt_regs *regs)
543 {
544         union {
545                 unsigned long ul;
546                 u8 b[sizeof(unsigned long)];
547         } u;
548         int i;
549
550         u.ul = val;
551         i = IS_BE ? sizeof(unsigned long) - nb : 0;
552         return copy_mem_out(&u.b[i], ea, nb, regs);
553 }
554
555 /*
556  * Write memory at address ea for nb bytes, return 0 for success
557  * or -EFAULT if an error occurred.  N.B. nb must be 1, 2, 4 or 8.
558  */
559 static int write_mem(unsigned long val, unsigned long ea, int nb,
560                                struct pt_regs *regs)
561 {
562         if (!address_ok(regs, ea, nb))
563                 return -EFAULT;
564         if ((ea & (nb - 1)) == 0)
565                 return write_mem_aligned(val, ea, nb, regs);
566         return write_mem_unaligned(val, ea, nb, regs);
567 }
568 NOKPROBE_SYMBOL(write_mem);
569
570 #ifdef CONFIG_PPC_FPU
571 /*
572  * These access either the real FP register or the image in the
573  * thread_struct, depending on regs->msr & MSR_FP.
574  */
575 static int do_fp_load(struct instruction_op *op, unsigned long ea,
576                       struct pt_regs *regs, bool cross_endian)
577 {
578         int err, rn, nb;
579         union {
580                 int i;
581                 unsigned int u;
582                 float f;
583                 double d[2];
584                 unsigned long l[2];
585                 u8 b[2 * sizeof(double)];
586         } u;
587
588         nb = GETSIZE(op->type);
589         if (nb > sizeof(u))
590                 return -EINVAL;
591         if (!address_ok(regs, ea, nb))
592                 return -EFAULT;
593         rn = op->reg;
594         err = copy_mem_in(u.b, ea, nb, regs);
595         if (err)
596                 return err;
597         if (unlikely(cross_endian)) {
598                 do_byte_reverse(u.b, min(nb, 8));
599                 if (nb == 16)
600                         do_byte_reverse(&u.b[8], 8);
601         }
602         preempt_disable();
603         if (nb == 4) {
604                 if (op->type & FPCONV)
605                         conv_sp_to_dp(&u.f, &u.d[0]);
606                 else if (op->type & SIGNEXT)
607                         u.l[0] = u.i;
608                 else
609                         u.l[0] = u.u;
610         }
611         if (regs->msr & MSR_FP)
612                 put_fpr(rn, &u.d[0]);
613         else
614                 current->thread.TS_FPR(rn) = u.l[0];
615         if (nb == 16) {
616                 /* lfdp */
617                 rn |= 1;
618                 if (regs->msr & MSR_FP)
619                         put_fpr(rn, &u.d[1]);
620                 else
621                         current->thread.TS_FPR(rn) = u.l[1];
622         }
623         preempt_enable();
624         return 0;
625 }
626 NOKPROBE_SYMBOL(do_fp_load);
627
628 static int do_fp_store(struct instruction_op *op, unsigned long ea,
629                        struct pt_regs *regs, bool cross_endian)
630 {
631         int rn, nb;
632         union {
633                 unsigned int u;
634                 float f;
635                 double d[2];
636                 unsigned long l[2];
637                 u8 b[2 * sizeof(double)];
638         } u;
639
640         nb = GETSIZE(op->type);
641         if (nb > sizeof(u))
642                 return -EINVAL;
643         if (!address_ok(regs, ea, nb))
644                 return -EFAULT;
645         rn = op->reg;
646         preempt_disable();
647         if (regs->msr & MSR_FP)
648                 get_fpr(rn, &u.d[0]);
649         else
650                 u.l[0] = current->thread.TS_FPR(rn);
651         if (nb == 4) {
652                 if (op->type & FPCONV)
653                         conv_dp_to_sp(&u.d[0], &u.f);
654                 else
655                         u.u = u.l[0];
656         }
657         if (nb == 16) {
658                 rn |= 1;
659                 if (regs->msr & MSR_FP)
660                         get_fpr(rn, &u.d[1]);
661                 else
662                         u.l[1] = current->thread.TS_FPR(rn);
663         }
664         preempt_enable();
665         if (unlikely(cross_endian)) {
666                 do_byte_reverse(u.b, min(nb, 8));
667                 if (nb == 16)
668                         do_byte_reverse(&u.b[8], 8);
669         }
670         return copy_mem_out(u.b, ea, nb, regs);
671 }
672 NOKPROBE_SYMBOL(do_fp_store);
673 #endif
674
675 #ifdef CONFIG_ALTIVEC
676 /* For Altivec/VMX, no need to worry about alignment */
677 static nokprobe_inline int do_vec_load(int rn, unsigned long ea,
678                                        int size, struct pt_regs *regs,
679                                        bool cross_endian)
680 {
681         int err;
682         union {
683                 __vector128 v;
684                 u8 b[sizeof(__vector128)];
685         } u = {};
686
687         if (size > sizeof(u))
688                 return -EINVAL;
689
690         if (!address_ok(regs, ea & ~0xfUL, 16))
691                 return -EFAULT;
692         /* align to multiple of size */
693         ea &= ~(size - 1);
694         err = copy_mem_in(&u.b[ea & 0xf], ea, size, regs);
695         if (err)
696                 return err;
697         if (unlikely(cross_endian))
698                 do_byte_reverse(&u.b[ea & 0xf], size);
699         preempt_disable();
700         if (regs->msr & MSR_VEC)
701                 put_vr(rn, &u.v);
702         else
703                 current->thread.vr_state.vr[rn] = u.v;
704         preempt_enable();
705         return 0;
706 }
707
708 static nokprobe_inline int do_vec_store(int rn, unsigned long ea,
709                                         int size, struct pt_regs *regs,
710                                         bool cross_endian)
711 {
712         union {
713                 __vector128 v;
714                 u8 b[sizeof(__vector128)];
715         } u;
716
717         if (size > sizeof(u))
718                 return -EINVAL;
719
720         if (!address_ok(regs, ea & ~0xfUL, 16))
721                 return -EFAULT;
722         /* align to multiple of size */
723         ea &= ~(size - 1);
724
725         preempt_disable();
726         if (regs->msr & MSR_VEC)
727                 get_vr(rn, &u.v);
728         else
729                 u.v = current->thread.vr_state.vr[rn];
730         preempt_enable();
731         if (unlikely(cross_endian))
732                 do_byte_reverse(&u.b[ea & 0xf], size);
733         return copy_mem_out(&u.b[ea & 0xf], ea, size, regs);
734 }
735 #endif /* CONFIG_ALTIVEC */
736
737 #ifdef __powerpc64__
738 static nokprobe_inline int emulate_lq(struct pt_regs *regs, unsigned long ea,
739                                       int reg, bool cross_endian)
740 {
741         int err;
742
743         if (!address_ok(regs, ea, 16))
744                 return -EFAULT;
745         /* if aligned, should be atomic */
746         if ((ea & 0xf) == 0) {
747                 err = do_lq(ea, &regs->gpr[reg]);
748         } else {
749                 err = read_mem(&regs->gpr[reg + IS_LE], ea, 8, regs);
750                 if (!err)
751                         err = read_mem(&regs->gpr[reg + IS_BE], ea + 8, 8, regs);
752         }
753         if (!err && unlikely(cross_endian))
754                 do_byte_reverse(&regs->gpr[reg], 16);
755         return err;
756 }
757
758 static nokprobe_inline int emulate_stq(struct pt_regs *regs, unsigned long ea,
759                                        int reg, bool cross_endian)
760 {
761         int err;
762         unsigned long vals[2];
763
764         if (!address_ok(regs, ea, 16))
765                 return -EFAULT;
766         vals[0] = regs->gpr[reg];
767         vals[1] = regs->gpr[reg + 1];
768         if (unlikely(cross_endian))
769                 do_byte_reverse(vals, 16);
770
771         /* if aligned, should be atomic */
772         if ((ea & 0xf) == 0)
773                 return do_stq(ea, vals[0], vals[1]);
774
775         err = write_mem(vals[IS_LE], ea, 8, regs);
776         if (!err)
777                 err = write_mem(vals[IS_BE], ea + 8, 8, regs);
778         return err;
779 }
780 #endif /* __powerpc64 */
781
782 #ifdef CONFIG_VSX
783 void emulate_vsx_load(struct instruction_op *op, union vsx_reg *reg,
784                       const void *mem, bool rev)
785 {
786         int size, read_size;
787         int i, j;
788         const unsigned int *wp;
789         const unsigned short *hp;
790         const unsigned char *bp;
791
792         size = GETSIZE(op->type);
793         reg->d[0] = reg->d[1] = 0;
794
795         switch (op->element_size) {
796         case 32:
797                 /* [p]lxvp[x] */
798         case 16:
799                 /* whole vector; lxv[x] or lxvl[l] */
800                 if (size == 0)
801                         break;
802                 memcpy(reg, mem, size);
803                 if (IS_LE && (op->vsx_flags & VSX_LDLEFT))
804                         rev = !rev;
805                 if (rev)
806                         do_byte_reverse(reg, size);
807                 break;
808         case 8:
809                 /* scalar loads, lxvd2x, lxvdsx */
810                 read_size = (size >= 8) ? 8 : size;
811                 i = IS_LE ? 8 : 8 - read_size;
812                 memcpy(&reg->b[i], mem, read_size);
813                 if (rev)
814                         do_byte_reverse(&reg->b[i], 8);
815                 if (size < 8) {
816                         if (op->type & SIGNEXT) {
817                                 /* size == 4 is the only case here */
818                                 reg->d[IS_LE] = (signed int) reg->d[IS_LE];
819                         } else if (op->vsx_flags & VSX_FPCONV) {
820                                 preempt_disable();
821                                 conv_sp_to_dp(&reg->fp[1 + IS_LE],
822                                               &reg->dp[IS_LE]);
823                                 preempt_enable();
824                         }
825                 } else {
826                         if (size == 16) {
827                                 unsigned long v = *(unsigned long *)(mem + 8);
828                                 reg->d[IS_BE] = !rev ? v : byterev_8(v);
829                         } else if (op->vsx_flags & VSX_SPLAT)
830                                 reg->d[IS_BE] = reg->d[IS_LE];
831                 }
832                 break;
833         case 4:
834                 /* lxvw4x, lxvwsx */
835                 wp = mem;
836                 for (j = 0; j < size / 4; ++j) {
837                         i = IS_LE ? 3 - j : j;
838                         reg->w[i] = !rev ? *wp++ : byterev_4(*wp++);
839                 }
840                 if (op->vsx_flags & VSX_SPLAT) {
841                         u32 val = reg->w[IS_LE ? 3 : 0];
842                         for (; j < 4; ++j) {
843                                 i = IS_LE ? 3 - j : j;
844                                 reg->w[i] = val;
845                         }
846                 }
847                 break;
848         case 2:
849                 /* lxvh8x */
850                 hp = mem;
851                 for (j = 0; j < size / 2; ++j) {
852                         i = IS_LE ? 7 - j : j;
853                         reg->h[i] = !rev ? *hp++ : byterev_2(*hp++);
854                 }
855                 break;
856         case 1:
857                 /* lxvb16x */
858                 bp = mem;
859                 for (j = 0; j < size; ++j) {
860                         i = IS_LE ? 15 - j : j;
861                         reg->b[i] = *bp++;
862                 }
863                 break;
864         }
865 }
866 EXPORT_SYMBOL_GPL(emulate_vsx_load);
867 NOKPROBE_SYMBOL(emulate_vsx_load);
868
869 void emulate_vsx_store(struct instruction_op *op, const union vsx_reg *reg,
870                        void *mem, bool rev)
871 {
872         int size, write_size;
873         int i, j;
874         union vsx_reg buf;
875         unsigned int *wp;
876         unsigned short *hp;
877         unsigned char *bp;
878
879         size = GETSIZE(op->type);
880
881         switch (op->element_size) {
882         case 32:
883                 /* [p]stxvp[x] */
884                 if (size == 0)
885                         break;
886                 if (rev) {
887                         /* reverse 32 bytes */
888                         union vsx_reg buf32[2];
889                         buf32[0].d[0] = byterev_8(reg[1].d[1]);
890                         buf32[0].d[1] = byterev_8(reg[1].d[0]);
891                         buf32[1].d[0] = byterev_8(reg[0].d[1]);
892                         buf32[1].d[1] = byterev_8(reg[0].d[0]);
893                         memcpy(mem, buf32, size);
894                 } else {
895                         memcpy(mem, reg, size);
896                 }
897                 break;
898         case 16:
899                 /* stxv, stxvx, stxvl, stxvll */
900                 if (size == 0)
901                         break;
902                 if (IS_LE && (op->vsx_flags & VSX_LDLEFT))
903                         rev = !rev;
904                 if (rev) {
905                         /* reverse 16 bytes */
906                         buf.d[0] = byterev_8(reg->d[1]);
907                         buf.d[1] = byterev_8(reg->d[0]);
908                         reg = &buf;
909                 }
910                 memcpy(mem, reg, size);
911                 break;
912         case 8:
913                 /* scalar stores, stxvd2x */
914                 write_size = (size >= 8) ? 8 : size;
915                 i = IS_LE ? 8 : 8 - write_size;
916                 if (size < 8 && op->vsx_flags & VSX_FPCONV) {
917                         buf.d[0] = buf.d[1] = 0;
918                         preempt_disable();
919                         conv_dp_to_sp(&reg->dp[IS_LE], &buf.fp[1 + IS_LE]);
920                         preempt_enable();
921                         reg = &buf;
922                 }
923                 memcpy(mem, &reg->b[i], write_size);
924                 if (size == 16)
925                         memcpy(mem + 8, &reg->d[IS_BE], 8);
926                 if (unlikely(rev)) {
927                         do_byte_reverse(mem, write_size);
928                         if (size == 16)
929                                 do_byte_reverse(mem + 8, 8);
930                 }
931                 break;
932         case 4:
933                 /* stxvw4x */
934                 wp = mem;
935                 for (j = 0; j < size / 4; ++j) {
936                         i = IS_LE ? 3 - j : j;
937                         *wp++ = !rev ? reg->w[i] : byterev_4(reg->w[i]);
938                 }
939                 break;
940         case 2:
941                 /* stxvh8x */
942                 hp = mem;
943                 for (j = 0; j < size / 2; ++j) {
944                         i = IS_LE ? 7 - j : j;
945                         *hp++ = !rev ? reg->h[i] : byterev_2(reg->h[i]);
946                 }
947                 break;
948         case 1:
949                 /* stvxb16x */
950                 bp = mem;
951                 for (j = 0; j < size; ++j) {
952                         i = IS_LE ? 15 - j : j;
953                         *bp++ = reg->b[i];
954                 }
955                 break;
956         }
957 }
958 EXPORT_SYMBOL_GPL(emulate_vsx_store);
959 NOKPROBE_SYMBOL(emulate_vsx_store);
960
961 static nokprobe_inline int do_vsx_load(struct instruction_op *op,
962                                        unsigned long ea, struct pt_regs *regs,
963                                        bool cross_endian)
964 {
965         int reg = op->reg;
966         int i, j, nr_vsx_regs;
967         u8 mem[32];
968         union vsx_reg buf[2];
969         int size = GETSIZE(op->type);
970
971         if (!address_ok(regs, ea, size) || copy_mem_in(mem, ea, size, regs))
972                 return -EFAULT;
973
974         nr_vsx_regs = max(1ul, size / sizeof(__vector128));
975         emulate_vsx_load(op, buf, mem, cross_endian);
976         preempt_disable();
977         if (reg < 32) {
978                 /* FP regs + extensions */
979                 if (regs->msr & MSR_FP) {
980                         for (i = 0; i < nr_vsx_regs; i++) {
981                                 j = IS_LE ? nr_vsx_regs - i - 1 : i;
982                                 load_vsrn(reg + i, &buf[j].v);
983                         }
984                 } else {
985                         for (i = 0; i < nr_vsx_regs; i++) {
986                                 j = IS_LE ? nr_vsx_regs - i - 1 : i;
987                                 current->thread.fp_state.fpr[reg + i][0] = buf[j].d[0];
988                                 current->thread.fp_state.fpr[reg + i][1] = buf[j].d[1];
989                         }
990                 }
991         } else {
992                 if (regs->msr & MSR_VEC) {
993                         for (i = 0; i < nr_vsx_regs; i++) {
994                                 j = IS_LE ? nr_vsx_regs - i - 1 : i;
995                                 load_vsrn(reg + i, &buf[j].v);
996                         }
997                 } else {
998                         for (i = 0; i < nr_vsx_regs; i++) {
999                                 j = IS_LE ? nr_vsx_regs - i - 1 : i;
1000                                 current->thread.vr_state.vr[reg - 32 + i] = buf[j].v;
1001                         }
1002                 }
1003         }
1004         preempt_enable();
1005         return 0;
1006 }
1007
1008 static nokprobe_inline int do_vsx_store(struct instruction_op *op,
1009                                         unsigned long ea, struct pt_regs *regs,
1010                                         bool cross_endian)
1011 {
1012         int reg = op->reg;
1013         int i, j, nr_vsx_regs;
1014         u8 mem[32];
1015         union vsx_reg buf[2];
1016         int size = GETSIZE(op->type);
1017
1018         if (!address_ok(regs, ea, size))
1019                 return -EFAULT;
1020
1021         nr_vsx_regs = max(1ul, size / sizeof(__vector128));
1022         preempt_disable();
1023         if (reg < 32) {
1024                 /* FP regs + extensions */
1025                 if (regs->msr & MSR_FP) {
1026                         for (i = 0; i < nr_vsx_regs; i++) {
1027                                 j = IS_LE ? nr_vsx_regs - i - 1 : i;
1028                                 store_vsrn(reg + i, &buf[j].v);
1029                         }
1030                 } else {
1031                         for (i = 0; i < nr_vsx_regs; i++) {
1032                                 j = IS_LE ? nr_vsx_regs - i - 1 : i;
1033                                 buf[j].d[0] = current->thread.fp_state.fpr[reg + i][0];
1034                                 buf[j].d[1] = current->thread.fp_state.fpr[reg + i][1];
1035                         }
1036                 }
1037         } else {
1038                 if (regs->msr & MSR_VEC) {
1039                         for (i = 0; i < nr_vsx_regs; i++) {
1040                                 j = IS_LE ? nr_vsx_regs - i - 1 : i;
1041                                 store_vsrn(reg + i, &buf[j].v);
1042                         }
1043                 } else {
1044                         for (i = 0; i < nr_vsx_regs; i++) {
1045                                 j = IS_LE ? nr_vsx_regs - i - 1 : i;
1046                                 buf[j].v = current->thread.vr_state.vr[reg - 32 + i];
1047                         }
1048                 }
1049         }
1050         preempt_enable();
1051         emulate_vsx_store(op, buf, mem, cross_endian);
1052         return  copy_mem_out(mem, ea, size, regs);
1053 }
1054 #endif /* CONFIG_VSX */
1055
1056 static int __emulate_dcbz(unsigned long ea)
1057 {
1058         unsigned long i;
1059         unsigned long size = l1_dcache_bytes();
1060
1061         for (i = 0; i < size; i += sizeof(long))
1062                 unsafe_put_user(0, (unsigned long __user *)(ea + i), Efault);
1063
1064         return 0;
1065
1066 Efault:
1067         return -EFAULT;
1068 }
1069
1070 int emulate_dcbz(unsigned long ea, struct pt_regs *regs)
1071 {
1072         int err;
1073         unsigned long size = l1_dcache_bytes();
1074
1075         ea = truncate_if_32bit(regs->msr, ea);
1076         ea &= ~(size - 1);
1077         if (!address_ok(regs, ea, size))
1078                 return -EFAULT;
1079
1080         if (is_kernel_addr(ea)) {
1081                 err = __emulate_dcbz(ea);
1082         } else if (user_write_access_begin((void __user *)ea, size)) {
1083                 err = __emulate_dcbz(ea);
1084                 user_write_access_end();
1085         } else {
1086                 err = -EFAULT;
1087         }
1088
1089         if (err)
1090                 regs->dar = ea;
1091
1092
1093         return err;
1094 }
1095 NOKPROBE_SYMBOL(emulate_dcbz);
1096
1097 #define __put_user_asmx(x, addr, err, op, cr)           \
1098         __asm__ __volatile__(                           \
1099                 ".machine push\n"                       \
1100                 ".machine power8\n"                     \
1101                 "1:     " op " %2,0,%3\n"               \
1102                 ".machine pop\n"                        \
1103                 "       mfcr    %1\n"                   \
1104                 "2:\n"                                  \
1105                 ".section .fixup,\"ax\"\n"              \
1106                 "3:     li      %0,%4\n"                \
1107                 "       b       2b\n"                   \
1108                 ".previous\n"                           \
1109                 EX_TABLE(1b, 3b)                        \
1110                 : "=r" (err), "=r" (cr)                 \
1111                 : "r" (x), "r" (addr), "i" (-EFAULT), "0" (err))
1112
1113 #define __get_user_asmx(x, addr, err, op)               \
1114         __asm__ __volatile__(                           \
1115                 ".machine push\n"                       \
1116                 ".machine power8\n"                     \
1117                 "1:     "op" %1,0,%2\n"                 \
1118                 ".machine pop\n"                        \
1119                 "2:\n"                                  \
1120                 ".section .fixup,\"ax\"\n"              \
1121                 "3:     li      %0,%3\n"                \
1122                 "       b       2b\n"                   \
1123                 ".previous\n"                           \
1124                 EX_TABLE(1b, 3b)                        \
1125                 : "=r" (err), "=r" (x)                  \
1126                 : "r" (addr), "i" (-EFAULT), "0" (err))
1127
1128 #define __cacheop_user_asmx(addr, err, op)              \
1129         __asm__ __volatile__(                           \
1130                 "1:     "op" 0,%1\n"                    \
1131                 "2:\n"                                  \
1132                 ".section .fixup,\"ax\"\n"              \
1133                 "3:     li      %0,%3\n"                \
1134                 "       b       2b\n"                   \
1135                 ".previous\n"                           \
1136                 EX_TABLE(1b, 3b)                        \
1137                 : "=r" (err)                            \
1138                 : "r" (addr), "i" (-EFAULT), "0" (err))
1139
1140 static nokprobe_inline void set_cr0(const struct pt_regs *regs,
1141                                     struct instruction_op *op)
1142 {
1143         long val = op->val;
1144
1145         op->type |= SETCC;
1146         op->ccval = (regs->ccr & 0x0fffffff) | ((regs->xer >> 3) & 0x10000000);
1147         if (!(regs->msr & MSR_64BIT))
1148                 val = (int) val;
1149         if (val < 0)
1150                 op->ccval |= 0x80000000;
1151         else if (val > 0)
1152                 op->ccval |= 0x40000000;
1153         else
1154                 op->ccval |= 0x20000000;
1155 }
1156
1157 static nokprobe_inline void set_ca32(struct instruction_op *op, bool val)
1158 {
1159         if (cpu_has_feature(CPU_FTR_ARCH_300)) {
1160                 if (val)
1161                         op->xerval |= XER_CA32;
1162                 else
1163                         op->xerval &= ~XER_CA32;
1164         }
1165 }
1166
1167 static nokprobe_inline void add_with_carry(const struct pt_regs *regs,
1168                                      struct instruction_op *op, int rd,
1169                                      unsigned long val1, unsigned long val2,
1170                                      unsigned long carry_in)
1171 {
1172         unsigned long val = val1 + val2;
1173
1174         if (carry_in)
1175                 ++val;
1176         op->type = COMPUTE | SETREG | SETXER;
1177         op->reg = rd;
1178         op->val = val;
1179         val = truncate_if_32bit(regs->msr, val);
1180         val1 = truncate_if_32bit(regs->msr, val1);
1181         op->xerval = regs->xer;
1182         if (val < val1 || (carry_in && val == val1))
1183                 op->xerval |= XER_CA;
1184         else
1185                 op->xerval &= ~XER_CA;
1186
1187         set_ca32(op, (unsigned int)val < (unsigned int)val1 ||
1188                         (carry_in && (unsigned int)val == (unsigned int)val1));
1189 }
1190
1191 static nokprobe_inline void do_cmp_signed(const struct pt_regs *regs,
1192                                           struct instruction_op *op,
1193                                           long v1, long v2, int crfld)
1194 {
1195         unsigned int crval, shift;
1196
1197         op->type = COMPUTE | SETCC;
1198         crval = (regs->xer >> 31) & 1;          /* get SO bit */
1199         if (v1 < v2)
1200                 crval |= 8;
1201         else if (v1 > v2)
1202                 crval |= 4;
1203         else
1204                 crval |= 2;
1205         shift = (7 - crfld) * 4;
1206         op->ccval = (regs->ccr & ~(0xf << shift)) | (crval << shift);
1207 }
1208
1209 static nokprobe_inline void do_cmp_unsigned(const struct pt_regs *regs,
1210                                             struct instruction_op *op,
1211                                             unsigned long v1,
1212                                             unsigned long v2, int crfld)
1213 {
1214         unsigned int crval, shift;
1215
1216         op->type = COMPUTE | SETCC;
1217         crval = (regs->xer >> 31) & 1;          /* get SO bit */
1218         if (v1 < v2)
1219                 crval |= 8;
1220         else if (v1 > v2)
1221                 crval |= 4;
1222         else
1223                 crval |= 2;
1224         shift = (7 - crfld) * 4;
1225         op->ccval = (regs->ccr & ~(0xf << shift)) | (crval << shift);
1226 }
1227
1228 static nokprobe_inline void do_cmpb(const struct pt_regs *regs,
1229                                     struct instruction_op *op,
1230                                     unsigned long v1, unsigned long v2)
1231 {
1232         unsigned long long out_val, mask;
1233         int i;
1234
1235         out_val = 0;
1236         for (i = 0; i < 8; i++) {
1237                 mask = 0xffUL << (i * 8);
1238                 if ((v1 & mask) == (v2 & mask))
1239                         out_val |= mask;
1240         }
1241         op->val = out_val;
1242 }
1243
1244 /*
1245  * The size parameter is used to adjust the equivalent popcnt instruction.
1246  * popcntb = 8, popcntw = 32, popcntd = 64
1247  */
1248 static nokprobe_inline void do_popcnt(const struct pt_regs *regs,
1249                                       struct instruction_op *op,
1250                                       unsigned long v1, int size)
1251 {
1252         unsigned long long out = v1;
1253
1254         out -= (out >> 1) & 0x5555555555555555ULL;
1255         out = (0x3333333333333333ULL & out) +
1256               (0x3333333333333333ULL & (out >> 2));
1257         out = (out + (out >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
1258
1259         if (size == 8) {        /* popcntb */
1260                 op->val = out;
1261                 return;
1262         }
1263         out += out >> 8;
1264         out += out >> 16;
1265         if (size == 32) {       /* popcntw */
1266                 op->val = out & 0x0000003f0000003fULL;
1267                 return;
1268         }
1269
1270         out = (out + (out >> 32)) & 0x7f;
1271         op->val = out;  /* popcntd */
1272 }
1273
1274 #ifdef CONFIG_PPC64
1275 static nokprobe_inline void do_bpermd(const struct pt_regs *regs,
1276                                       struct instruction_op *op,
1277                                       unsigned long v1, unsigned long v2)
1278 {
1279         unsigned char perm, idx;
1280         unsigned int i;
1281
1282         perm = 0;
1283         for (i = 0; i < 8; i++) {
1284                 idx = (v1 >> (i * 8)) & 0xff;
1285                 if (idx < 64)
1286                         if (v2 & PPC_BIT(idx))
1287                                 perm |= 1 << i;
1288         }
1289         op->val = perm;
1290 }
1291 #endif /* CONFIG_PPC64 */
1292 /*
1293  * The size parameter adjusts the equivalent prty instruction.
1294  * prtyw = 32, prtyd = 64
1295  */
1296 static nokprobe_inline void do_prty(const struct pt_regs *regs,
1297                                     struct instruction_op *op,
1298                                     unsigned long v, int size)
1299 {
1300         unsigned long long res = v ^ (v >> 8);
1301
1302         res ^= res >> 16;
1303         if (size == 32) {               /* prtyw */
1304                 op->val = res & 0x0000000100000001ULL;
1305                 return;
1306         }
1307
1308         res ^= res >> 32;
1309         op->val = res & 1;      /*prtyd */
1310 }
1311
1312 static nokprobe_inline int trap_compare(long v1, long v2)
1313 {
1314         int ret = 0;
1315
1316         if (v1 < v2)
1317                 ret |= 0x10;
1318         else if (v1 > v2)
1319                 ret |= 0x08;
1320         else
1321                 ret |= 0x04;
1322         if ((unsigned long)v1 < (unsigned long)v2)
1323                 ret |= 0x02;
1324         else if ((unsigned long)v1 > (unsigned long)v2)
1325                 ret |= 0x01;
1326         return ret;
1327 }
1328
1329 /*
1330  * Elements of 32-bit rotate and mask instructions.
1331  */
1332 #define MASK32(mb, me)  ((0xffffffffUL >> (mb)) + \
1333                          ((signed long)-0x80000000L >> (me)) + ((me) >= (mb)))
1334 #ifdef __powerpc64__
1335 #define MASK64_L(mb)    (~0UL >> (mb))
1336 #define MASK64_R(me)    ((signed long)-0x8000000000000000L >> (me))
1337 #define MASK64(mb, me)  (MASK64_L(mb) + MASK64_R(me) + ((me) >= (mb)))
1338 #define DATA32(x)       (((x) & 0xffffffffUL) | (((x) & 0xffffffffUL) << 32))
1339 #else
1340 #define DATA32(x)       (x)
1341 #endif
1342 #define ROTATE(x, n)    ((n) ? (((x) << (n)) | ((x) >> (8 * sizeof(long) - (n)))) : (x))
1343
1344 /*
1345  * Decode an instruction, and return information about it in *op
1346  * without changing *regs.
1347  * Integer arithmetic and logical instructions, branches, and barrier
1348  * instructions can be emulated just using the information in *op.
1349  *
1350  * Return value is 1 if the instruction can be emulated just by
1351  * updating *regs with the information in *op, -1 if we need the
1352  * GPRs but *regs doesn't contain the full register set, or 0
1353  * otherwise.
1354  */
1355 int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
1356                   ppc_inst_t instr)
1357 {
1358 #ifdef CONFIG_PPC64
1359         unsigned int suffixopcode, prefixtype, prefix_r;
1360 #endif
1361         unsigned int opcode, ra, rb, rc, rd, spr, u;
1362         unsigned long int imm;
1363         unsigned long int val, val2;
1364         unsigned int mb, me, sh;
1365         unsigned int word, suffix;
1366         long ival;
1367
1368         word = ppc_inst_val(instr);
1369         suffix = ppc_inst_suffix(instr);
1370
1371         op->type = COMPUTE;
1372
1373         opcode = ppc_inst_primary_opcode(instr);
1374         switch (opcode) {
1375         case 16:        /* bc */
1376                 op->type = BRANCH;
1377                 imm = (signed short)(word & 0xfffc);
1378                 if ((word & 2) == 0)
1379                         imm += regs->nip;
1380                 op->val = truncate_if_32bit(regs->msr, imm);
1381                 if (word & 1)
1382                         op->type |= SETLK;
1383                 if (branch_taken(word, regs, op))
1384                         op->type |= BRTAKEN;
1385                 return 1;
1386         case 17:        /* sc */
1387                 if ((word & 0xfe2) == 2)
1388                         op->type = SYSCALL;
1389                 else if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) &&
1390                                 (word & 0xfe3) == 1) {  /* scv */
1391                         op->type = SYSCALL_VECTORED_0;
1392                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
1393                                 goto unknown_opcode;
1394                 } else
1395                         op->type = UNKNOWN;
1396                 return 0;
1397         case 18:        /* b */
1398                 op->type = BRANCH | BRTAKEN;
1399                 imm = word & 0x03fffffc;
1400                 if (imm & 0x02000000)
1401                         imm -= 0x04000000;
1402                 if ((word & 2) == 0)
1403                         imm += regs->nip;
1404                 op->val = truncate_if_32bit(regs->msr, imm);
1405                 if (word & 1)
1406                         op->type |= SETLK;
1407                 return 1;
1408         case 19:
1409                 switch ((word >> 1) & 0x3ff) {
1410                 case 0:         /* mcrf */
1411                         op->type = COMPUTE + SETCC;
1412                         rd = 7 - ((word >> 23) & 0x7);
1413                         ra = 7 - ((word >> 18) & 0x7);
1414                         rd *= 4;
1415                         ra *= 4;
1416                         val = (regs->ccr >> ra) & 0xf;
1417                         op->ccval = (regs->ccr & ~(0xfUL << rd)) | (val << rd);
1418                         return 1;
1419
1420                 case 16:        /* bclr */
1421                 case 528:       /* bcctr */
1422                         op->type = BRANCH;
1423                         imm = (word & 0x400)? regs->ctr: regs->link;
1424                         op->val = truncate_if_32bit(regs->msr, imm);
1425                         if (word & 1)
1426                                 op->type |= SETLK;
1427                         if (branch_taken(word, regs, op))
1428                                 op->type |= BRTAKEN;
1429                         return 1;
1430
1431                 case 18:        /* rfid, scary */
1432                         if (regs->msr & MSR_PR)
1433                                 goto priv;
1434                         op->type = RFI;
1435                         return 0;
1436
1437                 case 150:       /* isync */
1438                         op->type = BARRIER | BARRIER_ISYNC;
1439                         return 1;
1440
1441                 case 33:        /* crnor */
1442                 case 129:       /* crandc */
1443                 case 193:       /* crxor */
1444                 case 225:       /* crnand */
1445                 case 257:       /* crand */
1446                 case 289:       /* creqv */
1447                 case 417:       /* crorc */
1448                 case 449:       /* cror */
1449                         op->type = COMPUTE + SETCC;
1450                         ra = (word >> 16) & 0x1f;
1451                         rb = (word >> 11) & 0x1f;
1452                         rd = (word >> 21) & 0x1f;
1453                         ra = (regs->ccr >> (31 - ra)) & 1;
1454                         rb = (regs->ccr >> (31 - rb)) & 1;
1455                         val = (word >> (6 + ra * 2 + rb)) & 1;
1456                         op->ccval = (regs->ccr & ~(1UL << (31 - rd))) |
1457                                 (val << (31 - rd));
1458                         return 1;
1459                 }
1460                 break;
1461         case 31:
1462                 switch ((word >> 1) & 0x3ff) {
1463                 case 598:       /* sync */
1464                         op->type = BARRIER + BARRIER_SYNC;
1465 #ifdef __powerpc64__
1466                         switch ((word >> 21) & 3) {
1467                         case 1:         /* lwsync */
1468                                 op->type = BARRIER + BARRIER_LWSYNC;
1469                                 break;
1470                         case 2:         /* ptesync */
1471                                 op->type = BARRIER + BARRIER_PTESYNC;
1472                                 break;
1473                         }
1474 #endif
1475                         return 1;
1476
1477                 case 854:       /* eieio */
1478                         op->type = BARRIER + BARRIER_EIEIO;
1479                         return 1;
1480                 }
1481                 break;
1482         }
1483
1484         rd = (word >> 21) & 0x1f;
1485         ra = (word >> 16) & 0x1f;
1486         rb = (word >> 11) & 0x1f;
1487         rc = (word >> 6) & 0x1f;
1488
1489         switch (opcode) {
1490 #ifdef __powerpc64__
1491         case 1:
1492                 if (!cpu_has_feature(CPU_FTR_ARCH_31))
1493                         goto unknown_opcode;
1494
1495                 prefix_r = GET_PREFIX_R(word);
1496                 ra = GET_PREFIX_RA(suffix);
1497                 rd = (suffix >> 21) & 0x1f;
1498                 op->reg = rd;
1499                 op->val = regs->gpr[rd];
1500                 suffixopcode = get_op(suffix);
1501                 prefixtype = (word >> 24) & 0x3;
1502                 switch (prefixtype) {
1503                 case 2:
1504                         if (prefix_r && ra)
1505                                 return 0;
1506                         switch (suffixopcode) {
1507                         case 14:        /* paddi */
1508                                 op->type = COMPUTE | PREFIXED;
1509                                 op->val = mlsd_8lsd_ea(word, suffix, regs);
1510                                 goto compute_done;
1511                         }
1512                 }
1513                 break;
1514         case 2:         /* tdi */
1515                 if (rd & trap_compare(regs->gpr[ra], (short) word))
1516                         goto trap;
1517                 return 1;
1518 #endif
1519         case 3:         /* twi */
1520                 if (rd & trap_compare((int)regs->gpr[ra], (short) word))
1521                         goto trap;
1522                 return 1;
1523
1524 #ifdef __powerpc64__
1525         case 4:
1526                 /*
1527                  * There are very many instructions with this primary opcode
1528                  * introduced in the ISA as early as v2.03. However, the ones
1529                  * we currently emulate were all introduced with ISA 3.0
1530                  */
1531                 if (!cpu_has_feature(CPU_FTR_ARCH_300))
1532                         goto unknown_opcode;
1533
1534                 switch (word & 0x3f) {
1535                 case 48:        /* maddhd */
1536                         asm volatile(PPC_MADDHD(%0, %1, %2, %3) :
1537                                      "=r" (op->val) : "r" (regs->gpr[ra]),
1538                                      "r" (regs->gpr[rb]), "r" (regs->gpr[rc]));
1539                         goto compute_done;
1540
1541                 case 49:        /* maddhdu */
1542                         asm volatile(PPC_MADDHDU(%0, %1, %2, %3) :
1543                                      "=r" (op->val) : "r" (regs->gpr[ra]),
1544                                      "r" (regs->gpr[rb]), "r" (regs->gpr[rc]));
1545                         goto compute_done;
1546
1547                 case 51:        /* maddld */
1548                         asm volatile(PPC_MADDLD(%0, %1, %2, %3) :
1549                                      "=r" (op->val) : "r" (regs->gpr[ra]),
1550                                      "r" (regs->gpr[rb]), "r" (regs->gpr[rc]));
1551                         goto compute_done;
1552                 }
1553
1554                 /*
1555                  * There are other instructions from ISA 3.0 with the same
1556                  * primary opcode which do not have emulation support yet.
1557                  */
1558                 goto unknown_opcode;
1559 #endif
1560
1561         case 7:         /* mulli */
1562                 op->val = regs->gpr[ra] * (short) word;
1563                 goto compute_done;
1564
1565         case 8:         /* subfic */
1566                 imm = (short) word;
1567                 add_with_carry(regs, op, rd, ~regs->gpr[ra], imm, 1);
1568                 return 1;
1569
1570         case 10:        /* cmpli */
1571                 imm = (unsigned short) word;
1572                 val = regs->gpr[ra];
1573 #ifdef __powerpc64__
1574                 if ((rd & 1) == 0)
1575                         val = (unsigned int) val;
1576 #endif
1577                 do_cmp_unsigned(regs, op, val, imm, rd >> 2);
1578                 return 1;
1579
1580         case 11:        /* cmpi */
1581                 imm = (short) word;
1582                 val = regs->gpr[ra];
1583 #ifdef __powerpc64__
1584                 if ((rd & 1) == 0)
1585                         val = (int) val;
1586 #endif
1587                 do_cmp_signed(regs, op, val, imm, rd >> 2);
1588                 return 1;
1589
1590         case 12:        /* addic */
1591                 imm = (short) word;
1592                 add_with_carry(regs, op, rd, regs->gpr[ra], imm, 0);
1593                 return 1;
1594
1595         case 13:        /* addic. */
1596                 imm = (short) word;
1597                 add_with_carry(regs, op, rd, regs->gpr[ra], imm, 0);
1598                 set_cr0(regs, op);
1599                 return 1;
1600
1601         case 14:        /* addi */
1602                 imm = (short) word;
1603                 if (ra)
1604                         imm += regs->gpr[ra];
1605                 op->val = imm;
1606                 goto compute_done;
1607
1608         case 15:        /* addis */
1609                 imm = ((short) word) << 16;
1610                 if (ra)
1611                         imm += regs->gpr[ra];
1612                 op->val = imm;
1613                 goto compute_done;
1614
1615         case 19:
1616                 if (((word >> 1) & 0x1f) == 2) {
1617                         /* addpcis */
1618                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
1619                                 goto unknown_opcode;
1620                         imm = (short) (word & 0xffc1);  /* d0 + d2 fields */
1621                         imm |= (word >> 15) & 0x3e;     /* d1 field */
1622                         op->val = regs->nip + (imm << 16) + 4;
1623                         goto compute_done;
1624                 }
1625                 op->type = UNKNOWN;
1626                 return 0;
1627
1628         case 20:        /* rlwimi */
1629                 mb = (word >> 6) & 0x1f;
1630                 me = (word >> 1) & 0x1f;
1631                 val = DATA32(regs->gpr[rd]);
1632                 imm = MASK32(mb, me);
1633                 op->val = (regs->gpr[ra] & ~imm) | (ROTATE(val, rb) & imm);
1634                 goto logical_done;
1635
1636         case 21:        /* rlwinm */
1637                 mb = (word >> 6) & 0x1f;
1638                 me = (word >> 1) & 0x1f;
1639                 val = DATA32(regs->gpr[rd]);
1640                 op->val = ROTATE(val, rb) & MASK32(mb, me);
1641                 goto logical_done;
1642
1643         case 23:        /* rlwnm */
1644                 mb = (word >> 6) & 0x1f;
1645                 me = (word >> 1) & 0x1f;
1646                 rb = regs->gpr[rb] & 0x1f;
1647                 val = DATA32(regs->gpr[rd]);
1648                 op->val = ROTATE(val, rb) & MASK32(mb, me);
1649                 goto logical_done;
1650
1651         case 24:        /* ori */
1652                 op->val = regs->gpr[rd] | (unsigned short) word;
1653                 goto logical_done_nocc;
1654
1655         case 25:        /* oris */
1656                 imm = (unsigned short) word;
1657                 op->val = regs->gpr[rd] | (imm << 16);
1658                 goto logical_done_nocc;
1659
1660         case 26:        /* xori */
1661                 op->val = regs->gpr[rd] ^ (unsigned short) word;
1662                 goto logical_done_nocc;
1663
1664         case 27:        /* xoris */
1665                 imm = (unsigned short) word;
1666                 op->val = regs->gpr[rd] ^ (imm << 16);
1667                 goto logical_done_nocc;
1668
1669         case 28:        /* andi. */
1670                 op->val = regs->gpr[rd] & (unsigned short) word;
1671                 set_cr0(regs, op);
1672                 goto logical_done_nocc;
1673
1674         case 29:        /* andis. */
1675                 imm = (unsigned short) word;
1676                 op->val = regs->gpr[rd] & (imm << 16);
1677                 set_cr0(regs, op);
1678                 goto logical_done_nocc;
1679
1680 #ifdef __powerpc64__
1681         case 30:        /* rld* */
1682                 mb = ((word >> 6) & 0x1f) | (word & 0x20);
1683                 val = regs->gpr[rd];
1684                 if ((word & 0x10) == 0) {
1685                         sh = rb | ((word & 2) << 4);
1686                         val = ROTATE(val, sh);
1687                         switch ((word >> 2) & 3) {
1688                         case 0:         /* rldicl */
1689                                 val &= MASK64_L(mb);
1690                                 break;
1691                         case 1:         /* rldicr */
1692                                 val &= MASK64_R(mb);
1693                                 break;
1694                         case 2:         /* rldic */
1695                                 val &= MASK64(mb, 63 - sh);
1696                                 break;
1697                         case 3:         /* rldimi */
1698                                 imm = MASK64(mb, 63 - sh);
1699                                 val = (regs->gpr[ra] & ~imm) |
1700                                         (val & imm);
1701                         }
1702                         op->val = val;
1703                         goto logical_done;
1704                 } else {
1705                         sh = regs->gpr[rb] & 0x3f;
1706                         val = ROTATE(val, sh);
1707                         switch ((word >> 1) & 7) {
1708                         case 0:         /* rldcl */
1709                                 op->val = val & MASK64_L(mb);
1710                                 goto logical_done;
1711                         case 1:         /* rldcr */
1712                                 op->val = val & MASK64_R(mb);
1713                                 goto logical_done;
1714                         }
1715                 }
1716 #endif
1717                 op->type = UNKNOWN;     /* illegal instruction */
1718                 return 0;
1719
1720         case 31:
1721                 /* isel occupies 32 minor opcodes */
1722                 if (((word >> 1) & 0x1f) == 15) {
1723                         mb = (word >> 6) & 0x1f; /* bc field */
1724                         val = (regs->ccr >> (31 - mb)) & 1;
1725                         val2 = (ra) ? regs->gpr[ra] : 0;
1726
1727                         op->val = (val) ? val2 : regs->gpr[rb];
1728                         goto compute_done;
1729                 }
1730
1731                 switch ((word >> 1) & 0x3ff) {
1732                 case 4:         /* tw */
1733                         if (rd == 0x1f ||
1734                             (rd & trap_compare((int)regs->gpr[ra],
1735                                                (int)regs->gpr[rb])))
1736                                 goto trap;
1737                         return 1;
1738 #ifdef __powerpc64__
1739                 case 68:        /* td */
1740                         if (rd & trap_compare(regs->gpr[ra], regs->gpr[rb]))
1741                                 goto trap;
1742                         return 1;
1743 #endif
1744                 case 83:        /* mfmsr */
1745                         if (regs->msr & MSR_PR)
1746                                 goto priv;
1747                         op->type = MFMSR;
1748                         op->reg = rd;
1749                         return 0;
1750                 case 146:       /* mtmsr */
1751                         if (regs->msr & MSR_PR)
1752                                 goto priv;
1753                         op->type = MTMSR;
1754                         op->reg = rd;
1755                         op->val = 0xffffffff & ~(MSR_ME | MSR_LE);
1756                         return 0;
1757 #ifdef CONFIG_PPC64
1758                 case 178:       /* mtmsrd */
1759                         if (regs->msr & MSR_PR)
1760                                 goto priv;
1761                         op->type = MTMSR;
1762                         op->reg = rd;
1763                         /* only MSR_EE and MSR_RI get changed if bit 15 set */
1764                         /* mtmsrd doesn't change MSR_HV, MSR_ME or MSR_LE */
1765                         imm = (word & 0x10000)? 0x8002: 0xefffffffffffeffeUL;
1766                         op->val = imm;
1767                         return 0;
1768 #endif
1769
1770                 case 19:        /* mfcr */
1771                         imm = 0xffffffffUL;
1772                         if ((word >> 20) & 1) {
1773                                 imm = 0xf0000000UL;
1774                                 for (sh = 0; sh < 8; ++sh) {
1775                                         if (word & (0x80000 >> sh))
1776                                                 break;
1777                                         imm >>= 4;
1778                                 }
1779                         }
1780                         op->val = regs->ccr & imm;
1781                         goto compute_done;
1782
1783                 case 128:       /* setb */
1784                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
1785                                 goto unknown_opcode;
1786                         /*
1787                          * 'ra' encodes the CR field number (bfa) in the top 3 bits.
1788                          * Since each CR field is 4 bits,
1789                          * we can simply mask off the bottom two bits (bfa * 4)
1790                          * to yield the first bit in the CR field.
1791                          */
1792                         ra = ra & ~0x3;
1793                         /* 'val' stores bits of the CR field (bfa) */
1794                         val = regs->ccr >> (CR0_SHIFT - ra);
1795                         /* checks if the LT bit of CR field (bfa) is set */
1796                         if (val & 8)
1797                                 op->val = -1;
1798                         /* checks if the GT bit of CR field (bfa) is set */
1799                         else if (val & 4)
1800                                 op->val = 1;
1801                         else
1802                                 op->val = 0;
1803                         goto compute_done;
1804
1805                 case 144:       /* mtcrf */
1806                         op->type = COMPUTE + SETCC;
1807                         imm = 0xf0000000UL;
1808                         val = regs->gpr[rd];
1809                         op->ccval = regs->ccr;
1810                         for (sh = 0; sh < 8; ++sh) {
1811                                 if (word & (0x80000 >> sh))
1812                                         op->ccval = (op->ccval & ~imm) |
1813                                                 (val & imm);
1814                                 imm >>= 4;
1815                         }
1816                         return 1;
1817
1818                 case 339:       /* mfspr */
1819                         spr = ((word >> 16) & 0x1f) | ((word >> 6) & 0x3e0);
1820                         op->type = MFSPR;
1821                         op->reg = rd;
1822                         op->spr = spr;
1823                         if (spr == SPRN_XER || spr == SPRN_LR ||
1824                             spr == SPRN_CTR)
1825                                 return 1;
1826                         return 0;
1827
1828                 case 467:       /* mtspr */
1829                         spr = ((word >> 16) & 0x1f) | ((word >> 6) & 0x3e0);
1830                         op->type = MTSPR;
1831                         op->val = regs->gpr[rd];
1832                         op->spr = spr;
1833                         if (spr == SPRN_XER || spr == SPRN_LR ||
1834                             spr == SPRN_CTR)
1835                                 return 1;
1836                         return 0;
1837
1838 /*
1839  * Compare instructions
1840  */
1841                 case 0: /* cmp */
1842                         val = regs->gpr[ra];
1843                         val2 = regs->gpr[rb];
1844 #ifdef __powerpc64__
1845                         if ((rd & 1) == 0) {
1846                                 /* word (32-bit) compare */
1847                                 val = (int) val;
1848                                 val2 = (int) val2;
1849                         }
1850 #endif
1851                         do_cmp_signed(regs, op, val, val2, rd >> 2);
1852                         return 1;
1853
1854                 case 32:        /* cmpl */
1855                         val = regs->gpr[ra];
1856                         val2 = regs->gpr[rb];
1857 #ifdef __powerpc64__
1858                         if ((rd & 1) == 0) {
1859                                 /* word (32-bit) compare */
1860                                 val = (unsigned int) val;
1861                                 val2 = (unsigned int) val2;
1862                         }
1863 #endif
1864                         do_cmp_unsigned(regs, op, val, val2, rd >> 2);
1865                         return 1;
1866
1867                 case 508: /* cmpb */
1868                         do_cmpb(regs, op, regs->gpr[rd], regs->gpr[rb]);
1869                         goto logical_done_nocc;
1870
1871 /*
1872  * Arithmetic instructions
1873  */
1874                 case 8: /* subfc */
1875                         add_with_carry(regs, op, rd, ~regs->gpr[ra],
1876                                        regs->gpr[rb], 1);
1877                         goto arith_done;
1878 #ifdef __powerpc64__
1879                 case 9: /* mulhdu */
1880                         asm("mulhdu %0,%1,%2" : "=r" (op->val) :
1881                             "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
1882                         goto arith_done;
1883 #endif
1884                 case 10:        /* addc */
1885                         add_with_carry(regs, op, rd, regs->gpr[ra],
1886                                        regs->gpr[rb], 0);
1887                         goto arith_done;
1888
1889                 case 11:        /* mulhwu */
1890                         asm("mulhwu %0,%1,%2" : "=r" (op->val) :
1891                             "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
1892                         goto arith_done;
1893
1894                 case 40:        /* subf */
1895                         op->val = regs->gpr[rb] - regs->gpr[ra];
1896                         goto arith_done;
1897 #ifdef __powerpc64__
1898                 case 73:        /* mulhd */
1899                         asm("mulhd %0,%1,%2" : "=r" (op->val) :
1900                             "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
1901                         goto arith_done;
1902 #endif
1903                 case 75:        /* mulhw */
1904                         asm("mulhw %0,%1,%2" : "=r" (op->val) :
1905                             "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
1906                         goto arith_done;
1907
1908                 case 104:       /* neg */
1909                         op->val = -regs->gpr[ra];
1910                         goto arith_done;
1911
1912                 case 136:       /* subfe */
1913                         add_with_carry(regs, op, rd, ~regs->gpr[ra],
1914                                        regs->gpr[rb], regs->xer & XER_CA);
1915                         goto arith_done;
1916
1917                 case 138:       /* adde */
1918                         add_with_carry(regs, op, rd, regs->gpr[ra],
1919                                        regs->gpr[rb], regs->xer & XER_CA);
1920                         goto arith_done;
1921
1922                 case 200:       /* subfze */
1923                         add_with_carry(regs, op, rd, ~regs->gpr[ra], 0L,
1924                                        regs->xer & XER_CA);
1925                         goto arith_done;
1926
1927                 case 202:       /* addze */
1928                         add_with_carry(regs, op, rd, regs->gpr[ra], 0L,
1929                                        regs->xer & XER_CA);
1930                         goto arith_done;
1931
1932                 case 232:       /* subfme */
1933                         add_with_carry(regs, op, rd, ~regs->gpr[ra], -1L,
1934                                        regs->xer & XER_CA);
1935                         goto arith_done;
1936 #ifdef __powerpc64__
1937                 case 233:       /* mulld */
1938                         op->val = regs->gpr[ra] * regs->gpr[rb];
1939                         goto arith_done;
1940 #endif
1941                 case 234:       /* addme */
1942                         add_with_carry(regs, op, rd, regs->gpr[ra], -1L,
1943                                        regs->xer & XER_CA);
1944                         goto arith_done;
1945
1946                 case 235:       /* mullw */
1947                         op->val = (long)(int) regs->gpr[ra] *
1948                                 (int) regs->gpr[rb];
1949
1950                         goto arith_done;
1951 #ifdef __powerpc64__
1952                 case 265:       /* modud */
1953                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
1954                                 goto unknown_opcode;
1955                         op->val = regs->gpr[ra] % regs->gpr[rb];
1956                         goto compute_done;
1957 #endif
1958                 case 266:       /* add */
1959                         op->val = regs->gpr[ra] + regs->gpr[rb];
1960                         goto arith_done;
1961
1962                 case 267:       /* moduw */
1963                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
1964                                 goto unknown_opcode;
1965                         op->val = (unsigned int) regs->gpr[ra] %
1966                                 (unsigned int) regs->gpr[rb];
1967                         goto compute_done;
1968 #ifdef __powerpc64__
1969                 case 457:       /* divdu */
1970                         op->val = regs->gpr[ra] / regs->gpr[rb];
1971                         goto arith_done;
1972 #endif
1973                 case 459:       /* divwu */
1974                         op->val = (unsigned int) regs->gpr[ra] /
1975                                 (unsigned int) regs->gpr[rb];
1976                         goto arith_done;
1977 #ifdef __powerpc64__
1978                 case 489:       /* divd */
1979                         op->val = (long int) regs->gpr[ra] /
1980                                 (long int) regs->gpr[rb];
1981                         goto arith_done;
1982 #endif
1983                 case 491:       /* divw */
1984                         op->val = (int) regs->gpr[ra] /
1985                                 (int) regs->gpr[rb];
1986                         goto arith_done;
1987 #ifdef __powerpc64__
1988                 case 425:       /* divde[.] */
1989                         asm volatile(PPC_DIVDE(%0, %1, %2) :
1990                                 "=r" (op->val) : "r" (regs->gpr[ra]),
1991                                 "r" (regs->gpr[rb]));
1992                         goto arith_done;
1993                 case 393:       /* divdeu[.] */
1994                         asm volatile(PPC_DIVDEU(%0, %1, %2) :
1995                                 "=r" (op->val) : "r" (regs->gpr[ra]),
1996                                 "r" (regs->gpr[rb]));
1997                         goto arith_done;
1998 #endif
1999                 case 755:       /* darn */
2000                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2001                                 goto unknown_opcode;
2002                         switch (ra & 0x3) {
2003                         case 0:
2004                                 /* 32-bit conditioned */
2005                                 asm volatile(PPC_DARN(%0, 0) : "=r" (op->val));
2006                                 goto compute_done;
2007
2008                         case 1:
2009                                 /* 64-bit conditioned */
2010                                 asm volatile(PPC_DARN(%0, 1) : "=r" (op->val));
2011                                 goto compute_done;
2012
2013                         case 2:
2014                                 /* 64-bit raw */
2015                                 asm volatile(PPC_DARN(%0, 2) : "=r" (op->val));
2016                                 goto compute_done;
2017                         }
2018
2019                         goto unknown_opcode;
2020 #ifdef __powerpc64__
2021                 case 777:       /* modsd */
2022                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2023                                 goto unknown_opcode;
2024                         op->val = (long int) regs->gpr[ra] %
2025                                 (long int) regs->gpr[rb];
2026                         goto compute_done;
2027 #endif
2028                 case 779:       /* modsw */
2029                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2030                                 goto unknown_opcode;
2031                         op->val = (int) regs->gpr[ra] %
2032                                 (int) regs->gpr[rb];
2033                         goto compute_done;
2034
2035
2036 /*
2037  * Logical instructions
2038  */
2039                 case 26:        /* cntlzw */
2040                         val = (unsigned int) regs->gpr[rd];
2041                         op->val = ( val ? __builtin_clz(val) : 32 );
2042                         goto logical_done;
2043 #ifdef __powerpc64__
2044                 case 58:        /* cntlzd */
2045                         val = regs->gpr[rd];
2046                         op->val = ( val ? __builtin_clzl(val) : 64 );
2047                         goto logical_done;
2048 #endif
2049                 case 28:        /* and */
2050                         op->val = regs->gpr[rd] & regs->gpr[rb];
2051                         goto logical_done;
2052
2053                 case 60:        /* andc */
2054                         op->val = regs->gpr[rd] & ~regs->gpr[rb];
2055                         goto logical_done;
2056
2057                 case 122:       /* popcntb */
2058                         do_popcnt(regs, op, regs->gpr[rd], 8);
2059                         goto logical_done_nocc;
2060
2061                 case 124:       /* nor */
2062                         op->val = ~(regs->gpr[rd] | regs->gpr[rb]);
2063                         goto logical_done;
2064
2065                 case 154:       /* prtyw */
2066                         do_prty(regs, op, regs->gpr[rd], 32);
2067                         goto logical_done_nocc;
2068
2069                 case 186:       /* prtyd */
2070                         do_prty(regs, op, regs->gpr[rd], 64);
2071                         goto logical_done_nocc;
2072 #ifdef CONFIG_PPC64
2073                 case 252:       /* bpermd */
2074                         do_bpermd(regs, op, regs->gpr[rd], regs->gpr[rb]);
2075                         goto logical_done_nocc;
2076 #endif
2077                 case 284:       /* xor */
2078                         op->val = ~(regs->gpr[rd] ^ regs->gpr[rb]);
2079                         goto logical_done;
2080
2081                 case 316:       /* xor */
2082                         op->val = regs->gpr[rd] ^ regs->gpr[rb];
2083                         goto logical_done;
2084
2085                 case 378:       /* popcntw */
2086                         do_popcnt(regs, op, regs->gpr[rd], 32);
2087                         goto logical_done_nocc;
2088
2089                 case 412:       /* orc */
2090                         op->val = regs->gpr[rd] | ~regs->gpr[rb];
2091                         goto logical_done;
2092
2093                 case 444:       /* or */
2094                         op->val = regs->gpr[rd] | regs->gpr[rb];
2095                         goto logical_done;
2096
2097                 case 476:       /* nand */
2098                         op->val = ~(regs->gpr[rd] & regs->gpr[rb]);
2099                         goto logical_done;
2100 #ifdef CONFIG_PPC64
2101                 case 506:       /* popcntd */
2102                         do_popcnt(regs, op, regs->gpr[rd], 64);
2103                         goto logical_done_nocc;
2104 #endif
2105                 case 538:       /* cnttzw */
2106                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2107                                 goto unknown_opcode;
2108                         val = (unsigned int) regs->gpr[rd];
2109                         op->val = (val ? __builtin_ctz(val) : 32);
2110                         goto logical_done;
2111 #ifdef __powerpc64__
2112                 case 570:       /* cnttzd */
2113                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2114                                 goto unknown_opcode;
2115                         val = regs->gpr[rd];
2116                         op->val = (val ? __builtin_ctzl(val) : 64);
2117                         goto logical_done;
2118 #endif
2119                 case 922:       /* extsh */
2120                         op->val = (signed short) regs->gpr[rd];
2121                         goto logical_done;
2122
2123                 case 954:       /* extsb */
2124                         op->val = (signed char) regs->gpr[rd];
2125                         goto logical_done;
2126 #ifdef __powerpc64__
2127                 case 986:       /* extsw */
2128                         op->val = (signed int) regs->gpr[rd];
2129                         goto logical_done;
2130 #endif
2131
2132 /*
2133  * Shift instructions
2134  */
2135                 case 24:        /* slw */
2136                         sh = regs->gpr[rb] & 0x3f;
2137                         if (sh < 32)
2138                                 op->val = (regs->gpr[rd] << sh) & 0xffffffffUL;
2139                         else
2140                                 op->val = 0;
2141                         goto logical_done;
2142
2143                 case 536:       /* srw */
2144                         sh = regs->gpr[rb] & 0x3f;
2145                         if (sh < 32)
2146                                 op->val = (regs->gpr[rd] & 0xffffffffUL) >> sh;
2147                         else
2148                                 op->val = 0;
2149                         goto logical_done;
2150
2151                 case 792:       /* sraw */
2152                         op->type = COMPUTE + SETREG + SETXER;
2153                         sh = regs->gpr[rb] & 0x3f;
2154                         ival = (signed int) regs->gpr[rd];
2155                         op->val = ival >> (sh < 32 ? sh : 31);
2156                         op->xerval = regs->xer;
2157                         if (ival < 0 && (sh >= 32 || (ival & ((1ul << sh) - 1)) != 0))
2158                                 op->xerval |= XER_CA;
2159                         else
2160                                 op->xerval &= ~XER_CA;
2161                         set_ca32(op, op->xerval & XER_CA);
2162                         goto logical_done;
2163
2164                 case 824:       /* srawi */
2165                         op->type = COMPUTE + SETREG + SETXER;
2166                         sh = rb;
2167                         ival = (signed int) regs->gpr[rd];
2168                         op->val = ival >> sh;
2169                         op->xerval = regs->xer;
2170                         if (ival < 0 && (ival & ((1ul << sh) - 1)) != 0)
2171                                 op->xerval |= XER_CA;
2172                         else
2173                                 op->xerval &= ~XER_CA;
2174                         set_ca32(op, op->xerval & XER_CA);
2175                         goto logical_done;
2176
2177 #ifdef __powerpc64__
2178                 case 27:        /* sld */
2179                         sh = regs->gpr[rb] & 0x7f;
2180                         if (sh < 64)
2181                                 op->val = regs->gpr[rd] << sh;
2182                         else
2183                                 op->val = 0;
2184                         goto logical_done;
2185
2186                 case 539:       /* srd */
2187                         sh = regs->gpr[rb] & 0x7f;
2188                         if (sh < 64)
2189                                 op->val = regs->gpr[rd] >> sh;
2190                         else
2191                                 op->val = 0;
2192                         goto logical_done;
2193
2194                 case 794:       /* srad */
2195                         op->type = COMPUTE + SETREG + SETXER;
2196                         sh = regs->gpr[rb] & 0x7f;
2197                         ival = (signed long int) regs->gpr[rd];
2198                         op->val = ival >> (sh < 64 ? sh : 63);
2199                         op->xerval = regs->xer;
2200                         if (ival < 0 && (sh >= 64 || (ival & ((1ul << sh) - 1)) != 0))
2201                                 op->xerval |= XER_CA;
2202                         else
2203                                 op->xerval &= ~XER_CA;
2204                         set_ca32(op, op->xerval & XER_CA);
2205                         goto logical_done;
2206
2207                 case 826:       /* sradi with sh_5 = 0 */
2208                 case 827:       /* sradi with sh_5 = 1 */
2209                         op->type = COMPUTE + SETREG + SETXER;
2210                         sh = rb | ((word & 2) << 4);
2211                         ival = (signed long int) regs->gpr[rd];
2212                         op->val = ival >> sh;
2213                         op->xerval = regs->xer;
2214                         if (ival < 0 && (ival & ((1ul << sh) - 1)) != 0)
2215                                 op->xerval |= XER_CA;
2216                         else
2217                                 op->xerval &= ~XER_CA;
2218                         set_ca32(op, op->xerval & XER_CA);
2219                         goto logical_done;
2220
2221                 case 890:       /* extswsli with sh_5 = 0 */
2222                 case 891:       /* extswsli with sh_5 = 1 */
2223                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2224                                 goto unknown_opcode;
2225                         op->type = COMPUTE + SETREG;
2226                         sh = rb | ((word & 2) << 4);
2227                         val = (signed int) regs->gpr[rd];
2228                         if (sh)
2229                                 op->val = ROTATE(val, sh) & MASK64(0, 63 - sh);
2230                         else
2231                                 op->val = val;
2232                         goto logical_done;
2233
2234 #endif /* __powerpc64__ */
2235
2236 /*
2237  * Cache instructions
2238  */
2239                 case 54:        /* dcbst */
2240                         op->type = MKOP(CACHEOP, DCBST, 0);
2241                         op->ea = xform_ea(word, regs);
2242                         return 0;
2243
2244                 case 86:        /* dcbf */
2245                         op->type = MKOP(CACHEOP, DCBF, 0);
2246                         op->ea = xform_ea(word, regs);
2247                         return 0;
2248
2249                 case 246:       /* dcbtst */
2250                         op->type = MKOP(CACHEOP, DCBTST, 0);
2251                         op->ea = xform_ea(word, regs);
2252                         op->reg = rd;
2253                         return 0;
2254
2255                 case 278:       /* dcbt */
2256                         op->type = MKOP(CACHEOP, DCBTST, 0);
2257                         op->ea = xform_ea(word, regs);
2258                         op->reg = rd;
2259                         return 0;
2260
2261                 case 982:       /* icbi */
2262                         op->type = MKOP(CACHEOP, ICBI, 0);
2263                         op->ea = xform_ea(word, regs);
2264                         return 0;
2265
2266                 case 1014:      /* dcbz */
2267                         op->type = MKOP(CACHEOP, DCBZ, 0);
2268                         op->ea = xform_ea(word, regs);
2269                         return 0;
2270                 }
2271                 break;
2272         }
2273
2274 /*
2275  * Loads and stores.
2276  */
2277         op->type = UNKNOWN;
2278         op->update_reg = ra;
2279         op->reg = rd;
2280         op->val = regs->gpr[rd];
2281         u = (word >> 20) & UPDATE;
2282         op->vsx_flags = 0;
2283
2284         switch (opcode) {
2285         case 31:
2286                 u = word & UPDATE;
2287                 op->ea = xform_ea(word, regs);
2288                 switch ((word >> 1) & 0x3ff) {
2289                 case 20:        /* lwarx */
2290                         op->type = MKOP(LARX, 0, 4);
2291                         break;
2292
2293                 case 150:       /* stwcx. */
2294                         op->type = MKOP(STCX, 0, 4);
2295                         break;
2296
2297 #ifdef __powerpc64__
2298                 case 84:        /* ldarx */
2299                         op->type = MKOP(LARX, 0, 8);
2300                         break;
2301
2302                 case 214:       /* stdcx. */
2303                         op->type = MKOP(STCX, 0, 8);
2304                         break;
2305
2306                 case 52:        /* lbarx */
2307                         op->type = MKOP(LARX, 0, 1);
2308                         break;
2309
2310                 case 694:       /* stbcx. */
2311                         op->type = MKOP(STCX, 0, 1);
2312                         break;
2313
2314                 case 116:       /* lharx */
2315                         op->type = MKOP(LARX, 0, 2);
2316                         break;
2317
2318                 case 726:       /* sthcx. */
2319                         op->type = MKOP(STCX, 0, 2);
2320                         break;
2321
2322                 case 276:       /* lqarx */
2323                         if (!((rd & 1) || rd == ra || rd == rb))
2324                                 op->type = MKOP(LARX, 0, 16);
2325                         break;
2326
2327                 case 182:       /* stqcx. */
2328                         if (!(rd & 1))
2329                                 op->type = MKOP(STCX, 0, 16);
2330                         break;
2331 #endif
2332
2333                 case 23:        /* lwzx */
2334                 case 55:        /* lwzux */
2335                         op->type = MKOP(LOAD, u, 4);
2336                         break;
2337
2338                 case 87:        /* lbzx */
2339                 case 119:       /* lbzux */
2340                         op->type = MKOP(LOAD, u, 1);
2341                         break;
2342
2343 #ifdef CONFIG_ALTIVEC
2344                 /*
2345                  * Note: for the load/store vector element instructions,
2346                  * bits of the EA say which field of the VMX register to use.
2347                  */
2348                 case 7:         /* lvebx */
2349                         op->type = MKOP(LOAD_VMX, 0, 1);
2350                         op->element_size = 1;
2351                         break;
2352
2353                 case 39:        /* lvehx */
2354                         op->type = MKOP(LOAD_VMX, 0, 2);
2355                         op->element_size = 2;
2356                         break;
2357
2358                 case 71:        /* lvewx */
2359                         op->type = MKOP(LOAD_VMX, 0, 4);
2360                         op->element_size = 4;
2361                         break;
2362
2363                 case 103:       /* lvx */
2364                 case 359:       /* lvxl */
2365                         op->type = MKOP(LOAD_VMX, 0, 16);
2366                         op->element_size = 16;
2367                         break;
2368
2369                 case 135:       /* stvebx */
2370                         op->type = MKOP(STORE_VMX, 0, 1);
2371                         op->element_size = 1;
2372                         break;
2373
2374                 case 167:       /* stvehx */
2375                         op->type = MKOP(STORE_VMX, 0, 2);
2376                         op->element_size = 2;
2377                         break;
2378
2379                 case 199:       /* stvewx */
2380                         op->type = MKOP(STORE_VMX, 0, 4);
2381                         op->element_size = 4;
2382                         break;
2383
2384                 case 231:       /* stvx */
2385                 case 487:       /* stvxl */
2386                         op->type = MKOP(STORE_VMX, 0, 16);
2387                         break;
2388 #endif /* CONFIG_ALTIVEC */
2389
2390 #ifdef __powerpc64__
2391                 case 21:        /* ldx */
2392                 case 53:        /* ldux */
2393                         op->type = MKOP(LOAD, u, 8);
2394                         break;
2395
2396                 case 149:       /* stdx */
2397                 case 181:       /* stdux */
2398                         op->type = MKOP(STORE, u, 8);
2399                         break;
2400 #endif
2401
2402                 case 151:       /* stwx */
2403                 case 183:       /* stwux */
2404                         op->type = MKOP(STORE, u, 4);
2405                         break;
2406
2407                 case 215:       /* stbx */
2408                 case 247:       /* stbux */
2409                         op->type = MKOP(STORE, u, 1);
2410                         break;
2411
2412                 case 279:       /* lhzx */
2413                 case 311:       /* lhzux */
2414                         op->type = MKOP(LOAD, u, 2);
2415                         break;
2416
2417 #ifdef __powerpc64__
2418                 case 341:       /* lwax */
2419                 case 373:       /* lwaux */
2420                         op->type = MKOP(LOAD, SIGNEXT | u, 4);
2421                         break;
2422 #endif
2423
2424                 case 343:       /* lhax */
2425                 case 375:       /* lhaux */
2426                         op->type = MKOP(LOAD, SIGNEXT | u, 2);
2427                         break;
2428
2429                 case 407:       /* sthx */
2430                 case 439:       /* sthux */
2431                         op->type = MKOP(STORE, u, 2);
2432                         break;
2433
2434 #ifdef __powerpc64__
2435                 case 532:       /* ldbrx */
2436                         op->type = MKOP(LOAD, BYTEREV, 8);
2437                         break;
2438
2439 #endif
2440                 case 533:       /* lswx */
2441                         op->type = MKOP(LOAD_MULTI, 0, regs->xer & 0x7f);
2442                         break;
2443
2444                 case 534:       /* lwbrx */
2445                         op->type = MKOP(LOAD, BYTEREV, 4);
2446                         break;
2447
2448                 case 597:       /* lswi */
2449                         if (rb == 0)
2450                                 rb = 32;        /* # bytes to load */
2451                         op->type = MKOP(LOAD_MULTI, 0, rb);
2452                         op->ea = ra ? regs->gpr[ra] : 0;
2453                         break;
2454
2455 #ifdef CONFIG_PPC_FPU
2456                 case 535:       /* lfsx */
2457                 case 567:       /* lfsux */
2458                         op->type = MKOP(LOAD_FP, u | FPCONV, 4);
2459                         break;
2460
2461                 case 599:       /* lfdx */
2462                 case 631:       /* lfdux */
2463                         op->type = MKOP(LOAD_FP, u, 8);
2464                         break;
2465
2466                 case 663:       /* stfsx */
2467                 case 695:       /* stfsux */
2468                         op->type = MKOP(STORE_FP, u | FPCONV, 4);
2469                         break;
2470
2471                 case 727:       /* stfdx */
2472                 case 759:       /* stfdux */
2473                         op->type = MKOP(STORE_FP, u, 8);
2474                         break;
2475
2476 #ifdef __powerpc64__
2477                 case 791:       /* lfdpx */
2478                         op->type = MKOP(LOAD_FP, 0, 16);
2479                         break;
2480
2481                 case 855:       /* lfiwax */
2482                         op->type = MKOP(LOAD_FP, SIGNEXT, 4);
2483                         break;
2484
2485                 case 887:       /* lfiwzx */
2486                         op->type = MKOP(LOAD_FP, 0, 4);
2487                         break;
2488
2489                 case 919:       /* stfdpx */
2490                         op->type = MKOP(STORE_FP, 0, 16);
2491                         break;
2492
2493                 case 983:       /* stfiwx */
2494                         op->type = MKOP(STORE_FP, 0, 4);
2495                         break;
2496 #endif /* __powerpc64 */
2497 #endif /* CONFIG_PPC_FPU */
2498
2499 #ifdef __powerpc64__
2500                 case 660:       /* stdbrx */
2501                         op->type = MKOP(STORE, BYTEREV, 8);
2502                         op->val = byterev_8(regs->gpr[rd]);
2503                         break;
2504
2505 #endif
2506                 case 661:       /* stswx */
2507                         op->type = MKOP(STORE_MULTI, 0, regs->xer & 0x7f);
2508                         break;
2509
2510                 case 662:       /* stwbrx */
2511                         op->type = MKOP(STORE, BYTEREV, 4);
2512                         op->val = byterev_4(regs->gpr[rd]);
2513                         break;
2514
2515                 case 725:       /* stswi */
2516                         if (rb == 0)
2517                                 rb = 32;        /* # bytes to store */
2518                         op->type = MKOP(STORE_MULTI, 0, rb);
2519                         op->ea = ra ? regs->gpr[ra] : 0;
2520                         break;
2521
2522                 case 790:       /* lhbrx */
2523                         op->type = MKOP(LOAD, BYTEREV, 2);
2524                         break;
2525
2526                 case 918:       /* sthbrx */
2527                         op->type = MKOP(STORE, BYTEREV, 2);
2528                         op->val = byterev_2(regs->gpr[rd]);
2529                         break;
2530
2531 #ifdef CONFIG_VSX
2532                 case 12:        /* lxsiwzx */
2533                         op->reg = rd | ((word & 1) << 5);
2534                         op->type = MKOP(LOAD_VSX, 0, 4);
2535                         op->element_size = 8;
2536                         break;
2537
2538                 case 76:        /* lxsiwax */
2539                         op->reg = rd | ((word & 1) << 5);
2540                         op->type = MKOP(LOAD_VSX, SIGNEXT, 4);
2541                         op->element_size = 8;
2542                         break;
2543
2544                 case 140:       /* stxsiwx */
2545                         op->reg = rd | ((word & 1) << 5);
2546                         op->type = MKOP(STORE_VSX, 0, 4);
2547                         op->element_size = 8;
2548                         break;
2549
2550                 case 268:       /* lxvx */
2551                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2552                                 goto unknown_opcode;
2553                         op->reg = rd | ((word & 1) << 5);
2554                         op->type = MKOP(LOAD_VSX, 0, 16);
2555                         op->element_size = 16;
2556                         op->vsx_flags = VSX_CHECK_VEC;
2557                         break;
2558
2559                 case 269:       /* lxvl */
2560                 case 301: {     /* lxvll */
2561                         int nb;
2562                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2563                                 goto unknown_opcode;
2564                         op->reg = rd | ((word & 1) << 5);
2565                         op->ea = ra ? regs->gpr[ra] : 0;
2566                         nb = regs->gpr[rb] & 0xff;
2567                         if (nb > 16)
2568                                 nb = 16;
2569                         op->type = MKOP(LOAD_VSX, 0, nb);
2570                         op->element_size = 16;
2571                         op->vsx_flags = ((word & 0x20) ? VSX_LDLEFT : 0) |
2572                                 VSX_CHECK_VEC;
2573                         break;
2574                 }
2575                 case 332:       /* lxvdsx */
2576                         op->reg = rd | ((word & 1) << 5);
2577                         op->type = MKOP(LOAD_VSX, 0, 8);
2578                         op->element_size = 8;
2579                         op->vsx_flags = VSX_SPLAT;
2580                         break;
2581
2582                 case 333:       /* lxvpx */
2583                         if (!cpu_has_feature(CPU_FTR_ARCH_31))
2584                                 goto unknown_opcode;
2585                         op->reg = VSX_REGISTER_XTP(rd);
2586                         op->type = MKOP(LOAD_VSX, 0, 32);
2587                         op->element_size = 32;
2588                         break;
2589
2590                 case 364:       /* lxvwsx */
2591                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2592                                 goto unknown_opcode;
2593                         op->reg = rd | ((word & 1) << 5);
2594                         op->type = MKOP(LOAD_VSX, 0, 4);
2595                         op->element_size = 4;
2596                         op->vsx_flags = VSX_SPLAT | VSX_CHECK_VEC;
2597                         break;
2598
2599                 case 396:       /* stxvx */
2600                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2601                                 goto unknown_opcode;
2602                         op->reg = rd | ((word & 1) << 5);
2603                         op->type = MKOP(STORE_VSX, 0, 16);
2604                         op->element_size = 16;
2605                         op->vsx_flags = VSX_CHECK_VEC;
2606                         break;
2607
2608                 case 397:       /* stxvl */
2609                 case 429: {     /* stxvll */
2610                         int nb;
2611                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2612                                 goto unknown_opcode;
2613                         op->reg = rd | ((word & 1) << 5);
2614                         op->ea = ra ? regs->gpr[ra] : 0;
2615                         nb = regs->gpr[rb] & 0xff;
2616                         if (nb > 16)
2617                                 nb = 16;
2618                         op->type = MKOP(STORE_VSX, 0, nb);
2619                         op->element_size = 16;
2620                         op->vsx_flags = ((word & 0x20) ? VSX_LDLEFT : 0) |
2621                                 VSX_CHECK_VEC;
2622                         break;
2623                 }
2624                 case 461:       /* stxvpx */
2625                         if (!cpu_has_feature(CPU_FTR_ARCH_31))
2626                                 goto unknown_opcode;
2627                         op->reg = VSX_REGISTER_XTP(rd);
2628                         op->type = MKOP(STORE_VSX, 0, 32);
2629                         op->element_size = 32;
2630                         break;
2631                 case 524:       /* lxsspx */
2632                         op->reg = rd | ((word & 1) << 5);
2633                         op->type = MKOP(LOAD_VSX, 0, 4);
2634                         op->element_size = 8;
2635                         op->vsx_flags = VSX_FPCONV;
2636                         break;
2637
2638                 case 588:       /* lxsdx */
2639                         op->reg = rd | ((word & 1) << 5);
2640                         op->type = MKOP(LOAD_VSX, 0, 8);
2641                         op->element_size = 8;
2642                         break;
2643
2644                 case 652:       /* stxsspx */
2645                         op->reg = rd | ((word & 1) << 5);
2646                         op->type = MKOP(STORE_VSX, 0, 4);
2647                         op->element_size = 8;
2648                         op->vsx_flags = VSX_FPCONV;
2649                         break;
2650
2651                 case 716:       /* stxsdx */
2652                         op->reg = rd | ((word & 1) << 5);
2653                         op->type = MKOP(STORE_VSX, 0, 8);
2654                         op->element_size = 8;
2655                         break;
2656
2657                 case 780:       /* lxvw4x */
2658                         op->reg = rd | ((word & 1) << 5);
2659                         op->type = MKOP(LOAD_VSX, 0, 16);
2660                         op->element_size = 4;
2661                         break;
2662
2663                 case 781:       /* lxsibzx */
2664                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2665                                 goto unknown_opcode;
2666                         op->reg = rd | ((word & 1) << 5);
2667                         op->type = MKOP(LOAD_VSX, 0, 1);
2668                         op->element_size = 8;
2669                         op->vsx_flags = VSX_CHECK_VEC;
2670                         break;
2671
2672                 case 812:       /* lxvh8x */
2673                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2674                                 goto unknown_opcode;
2675                         op->reg = rd | ((word & 1) << 5);
2676                         op->type = MKOP(LOAD_VSX, 0, 16);
2677                         op->element_size = 2;
2678                         op->vsx_flags = VSX_CHECK_VEC;
2679                         break;
2680
2681                 case 813:       /* lxsihzx */
2682                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2683                                 goto unknown_opcode;
2684                         op->reg = rd | ((word & 1) << 5);
2685                         op->type = MKOP(LOAD_VSX, 0, 2);
2686                         op->element_size = 8;
2687                         op->vsx_flags = VSX_CHECK_VEC;
2688                         break;
2689
2690                 case 844:       /* lxvd2x */
2691                         op->reg = rd | ((word & 1) << 5);
2692                         op->type = MKOP(LOAD_VSX, 0, 16);
2693                         op->element_size = 8;
2694                         break;
2695
2696                 case 876:       /* lxvb16x */
2697                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2698                                 goto unknown_opcode;
2699                         op->reg = rd | ((word & 1) << 5);
2700                         op->type = MKOP(LOAD_VSX, 0, 16);
2701                         op->element_size = 1;
2702                         op->vsx_flags = VSX_CHECK_VEC;
2703                         break;
2704
2705                 case 908:       /* stxvw4x */
2706                         op->reg = rd | ((word & 1) << 5);
2707                         op->type = MKOP(STORE_VSX, 0, 16);
2708                         op->element_size = 4;
2709                         break;
2710
2711                 case 909:       /* stxsibx */
2712                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2713                                 goto unknown_opcode;
2714                         op->reg = rd | ((word & 1) << 5);
2715                         op->type = MKOP(STORE_VSX, 0, 1);
2716                         op->element_size = 8;
2717                         op->vsx_flags = VSX_CHECK_VEC;
2718                         break;
2719
2720                 case 940:       /* stxvh8x */
2721                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2722                                 goto unknown_opcode;
2723                         op->reg = rd | ((word & 1) << 5);
2724                         op->type = MKOP(STORE_VSX, 0, 16);
2725                         op->element_size = 2;
2726                         op->vsx_flags = VSX_CHECK_VEC;
2727                         break;
2728
2729                 case 941:       /* stxsihx */
2730                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2731                                 goto unknown_opcode;
2732                         op->reg = rd | ((word & 1) << 5);
2733                         op->type = MKOP(STORE_VSX, 0, 2);
2734                         op->element_size = 8;
2735                         op->vsx_flags = VSX_CHECK_VEC;
2736                         break;
2737
2738                 case 972:       /* stxvd2x */
2739                         op->reg = rd | ((word & 1) << 5);
2740                         op->type = MKOP(STORE_VSX, 0, 16);
2741                         op->element_size = 8;
2742                         break;
2743
2744                 case 1004:      /* stxvb16x */
2745                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2746                                 goto unknown_opcode;
2747                         op->reg = rd | ((word & 1) << 5);
2748                         op->type = MKOP(STORE_VSX, 0, 16);
2749                         op->element_size = 1;
2750                         op->vsx_flags = VSX_CHECK_VEC;
2751                         break;
2752
2753 #endif /* CONFIG_VSX */
2754                 }
2755                 break;
2756
2757         case 32:        /* lwz */
2758         case 33:        /* lwzu */
2759                 op->type = MKOP(LOAD, u, 4);
2760                 op->ea = dform_ea(word, regs);
2761                 break;
2762
2763         case 34:        /* lbz */
2764         case 35:        /* lbzu */
2765                 op->type = MKOP(LOAD, u, 1);
2766                 op->ea = dform_ea(word, regs);
2767                 break;
2768
2769         case 36:        /* stw */
2770         case 37:        /* stwu */
2771                 op->type = MKOP(STORE, u, 4);
2772                 op->ea = dform_ea(word, regs);
2773                 break;
2774
2775         case 38:        /* stb */
2776         case 39:        /* stbu */
2777                 op->type = MKOP(STORE, u, 1);
2778                 op->ea = dform_ea(word, regs);
2779                 break;
2780
2781         case 40:        /* lhz */
2782         case 41:        /* lhzu */
2783                 op->type = MKOP(LOAD, u, 2);
2784                 op->ea = dform_ea(word, regs);
2785                 break;
2786
2787         case 42:        /* lha */
2788         case 43:        /* lhau */
2789                 op->type = MKOP(LOAD, SIGNEXT | u, 2);
2790                 op->ea = dform_ea(word, regs);
2791                 break;
2792
2793         case 44:        /* sth */
2794         case 45:        /* sthu */
2795                 op->type = MKOP(STORE, u, 2);
2796                 op->ea = dform_ea(word, regs);
2797                 break;
2798
2799         case 46:        /* lmw */
2800                 if (ra >= rd)
2801                         break;          /* invalid form, ra in range to load */
2802                 op->type = MKOP(LOAD_MULTI, 0, 4 * (32 - rd));
2803                 op->ea = dform_ea(word, regs);
2804                 break;
2805
2806         case 47:        /* stmw */
2807                 op->type = MKOP(STORE_MULTI, 0, 4 * (32 - rd));
2808                 op->ea = dform_ea(word, regs);
2809                 break;
2810
2811 #ifdef CONFIG_PPC_FPU
2812         case 48:        /* lfs */
2813         case 49:        /* lfsu */
2814                 op->type = MKOP(LOAD_FP, u | FPCONV, 4);
2815                 op->ea = dform_ea(word, regs);
2816                 break;
2817
2818         case 50:        /* lfd */
2819         case 51:        /* lfdu */
2820                 op->type = MKOP(LOAD_FP, u, 8);
2821                 op->ea = dform_ea(word, regs);
2822                 break;
2823
2824         case 52:        /* stfs */
2825         case 53:        /* stfsu */
2826                 op->type = MKOP(STORE_FP, u | FPCONV, 4);
2827                 op->ea = dform_ea(word, regs);
2828                 break;
2829
2830         case 54:        /* stfd */
2831         case 55:        /* stfdu */
2832                 op->type = MKOP(STORE_FP, u, 8);
2833                 op->ea = dform_ea(word, regs);
2834                 break;
2835 #endif
2836
2837 #ifdef __powerpc64__
2838         case 56:        /* lq */
2839                 if (!((rd & 1) || (rd == ra)))
2840                         op->type = MKOP(LOAD, 0, 16);
2841                 op->ea = dqform_ea(word, regs);
2842                 break;
2843 #endif
2844
2845 #ifdef CONFIG_VSX
2846         case 57:        /* lfdp, lxsd, lxssp */
2847                 op->ea = dsform_ea(word, regs);
2848                 switch (word & 3) {
2849                 case 0:         /* lfdp */
2850                         if (rd & 1)
2851                                 break;          /* reg must be even */
2852                         op->type = MKOP(LOAD_FP, 0, 16);
2853                         break;
2854                 case 2:         /* lxsd */
2855                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2856                                 goto unknown_opcode;
2857                         op->reg = rd + 32;
2858                         op->type = MKOP(LOAD_VSX, 0, 8);
2859                         op->element_size = 8;
2860                         op->vsx_flags = VSX_CHECK_VEC;
2861                         break;
2862                 case 3:         /* lxssp */
2863                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2864                                 goto unknown_opcode;
2865                         op->reg = rd + 32;
2866                         op->type = MKOP(LOAD_VSX, 0, 4);
2867                         op->element_size = 8;
2868                         op->vsx_flags = VSX_FPCONV | VSX_CHECK_VEC;
2869                         break;
2870                 }
2871                 break;
2872 #endif /* CONFIG_VSX */
2873
2874 #ifdef __powerpc64__
2875         case 58:        /* ld[u], lwa */
2876                 op->ea = dsform_ea(word, regs);
2877                 switch (word & 3) {
2878                 case 0:         /* ld */
2879                         op->type = MKOP(LOAD, 0, 8);
2880                         break;
2881                 case 1:         /* ldu */
2882                         op->type = MKOP(LOAD, UPDATE, 8);
2883                         break;
2884                 case 2:         /* lwa */
2885                         op->type = MKOP(LOAD, SIGNEXT, 4);
2886                         break;
2887                 }
2888                 break;
2889 #endif
2890
2891 #ifdef CONFIG_VSX
2892         case 6:
2893                 if (!cpu_has_feature(CPU_FTR_ARCH_31))
2894                         goto unknown_opcode;
2895                 op->ea = dqform_ea(word, regs);
2896                 op->reg = VSX_REGISTER_XTP(rd);
2897                 op->element_size = 32;
2898                 switch (word & 0xf) {
2899                 case 0:         /* lxvp */
2900                         op->type = MKOP(LOAD_VSX, 0, 32);
2901                         break;
2902                 case 1:         /* stxvp */
2903                         op->type = MKOP(STORE_VSX, 0, 32);
2904                         break;
2905                 }
2906                 break;
2907
2908         case 61:        /* stfdp, lxv, stxsd, stxssp, stxv */
2909                 switch (word & 7) {
2910                 case 0:         /* stfdp with LSB of DS field = 0 */
2911                 case 4:         /* stfdp with LSB of DS field = 1 */
2912                         op->ea = dsform_ea(word, regs);
2913                         op->type = MKOP(STORE_FP, 0, 16);
2914                         break;
2915
2916                 case 1:         /* lxv */
2917                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2918                                 goto unknown_opcode;
2919                         op->ea = dqform_ea(word, regs);
2920                         if (word & 8)
2921                                 op->reg = rd + 32;
2922                         op->type = MKOP(LOAD_VSX, 0, 16);
2923                         op->element_size = 16;
2924                         op->vsx_flags = VSX_CHECK_VEC;
2925                         break;
2926
2927                 case 2:         /* stxsd with LSB of DS field = 0 */
2928                 case 6:         /* stxsd with LSB of DS field = 1 */
2929                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2930                                 goto unknown_opcode;
2931                         op->ea = dsform_ea(word, regs);
2932                         op->reg = rd + 32;
2933                         op->type = MKOP(STORE_VSX, 0, 8);
2934                         op->element_size = 8;
2935                         op->vsx_flags = VSX_CHECK_VEC;
2936                         break;
2937
2938                 case 3:         /* stxssp with LSB of DS field = 0 */
2939                 case 7:         /* stxssp with LSB of DS field = 1 */
2940                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2941                                 goto unknown_opcode;
2942                         op->ea = dsform_ea(word, regs);
2943                         op->reg = rd + 32;
2944                         op->type = MKOP(STORE_VSX, 0, 4);
2945                         op->element_size = 8;
2946                         op->vsx_flags = VSX_FPCONV | VSX_CHECK_VEC;
2947                         break;
2948
2949                 case 5:         /* stxv */
2950                         if (!cpu_has_feature(CPU_FTR_ARCH_300))
2951                                 goto unknown_opcode;
2952                         op->ea = dqform_ea(word, regs);
2953                         if (word & 8)
2954                                 op->reg = rd + 32;
2955                         op->type = MKOP(STORE_VSX, 0, 16);
2956                         op->element_size = 16;
2957                         op->vsx_flags = VSX_CHECK_VEC;
2958                         break;
2959                 }
2960                 break;
2961 #endif /* CONFIG_VSX */
2962
2963 #ifdef __powerpc64__
2964         case 62:        /* std[u] */
2965                 op->ea = dsform_ea(word, regs);
2966                 switch (word & 3) {
2967                 case 0:         /* std */
2968                         op->type = MKOP(STORE, 0, 8);
2969                         break;
2970                 case 1:         /* stdu */
2971                         op->type = MKOP(STORE, UPDATE, 8);
2972                         break;
2973                 case 2:         /* stq */
2974                         if (!(rd & 1))
2975                                 op->type = MKOP(STORE, 0, 16);
2976                         break;
2977                 }
2978                 break;
2979         case 1: /* Prefixed instructions */
2980                 if (!cpu_has_feature(CPU_FTR_ARCH_31))
2981                         goto unknown_opcode;
2982
2983                 prefix_r = GET_PREFIX_R(word);
2984                 ra = GET_PREFIX_RA(suffix);
2985                 op->update_reg = ra;
2986                 rd = (suffix >> 21) & 0x1f;
2987                 op->reg = rd;
2988                 op->val = regs->gpr[rd];
2989
2990                 suffixopcode = get_op(suffix);
2991                 prefixtype = (word >> 24) & 0x3;
2992                 switch (prefixtype) {
2993                 case 0: /* Type 00  Eight-Byte Load/Store */
2994                         if (prefix_r && ra)
2995                                 break;
2996                         op->ea = mlsd_8lsd_ea(word, suffix, regs);
2997                         switch (suffixopcode) {
2998                         case 41:        /* plwa */
2999                                 op->type = MKOP(LOAD, PREFIXED | SIGNEXT, 4);
3000                                 break;
3001 #ifdef CONFIG_VSX
3002                         case 42:        /* plxsd */
3003                                 op->reg = rd + 32;
3004                                 op->type = MKOP(LOAD_VSX, PREFIXED, 8);
3005                                 op->element_size = 8;
3006                                 op->vsx_flags = VSX_CHECK_VEC;
3007                                 break;
3008                         case 43:        /* plxssp */
3009                                 op->reg = rd + 32;
3010                                 op->type = MKOP(LOAD_VSX, PREFIXED, 4);
3011                                 op->element_size = 8;
3012                                 op->vsx_flags = VSX_FPCONV | VSX_CHECK_VEC;
3013                                 break;
3014                         case 46:        /* pstxsd */
3015                                 op->reg = rd + 32;
3016                                 op->type = MKOP(STORE_VSX, PREFIXED, 8);
3017                                 op->element_size = 8;
3018                                 op->vsx_flags = VSX_CHECK_VEC;
3019                                 break;
3020                         case 47:        /* pstxssp */
3021                                 op->reg = rd + 32;
3022                                 op->type = MKOP(STORE_VSX, PREFIXED, 4);
3023                                 op->element_size = 8;
3024                                 op->vsx_flags = VSX_FPCONV | VSX_CHECK_VEC;
3025                                 break;
3026                         case 51:        /* plxv1 */
3027                                 op->reg += 32;
3028                                 fallthrough;
3029                         case 50:        /* plxv0 */
3030                                 op->type = MKOP(LOAD_VSX, PREFIXED, 16);
3031                                 op->element_size = 16;
3032                                 op->vsx_flags = VSX_CHECK_VEC;
3033                                 break;
3034                         case 55:        /* pstxv1 */
3035                                 op->reg = rd + 32;
3036                                 fallthrough;
3037                         case 54:        /* pstxv0 */
3038                                 op->type = MKOP(STORE_VSX, PREFIXED, 16);
3039                                 op->element_size = 16;
3040                                 op->vsx_flags = VSX_CHECK_VEC;
3041                                 break;
3042 #endif /* CONFIG_VSX */
3043                         case 56:        /* plq */
3044                                 op->type = MKOP(LOAD, PREFIXED, 16);
3045                                 break;
3046                         case 57:        /* pld */
3047                                 op->type = MKOP(LOAD, PREFIXED, 8);
3048                                 break;
3049 #ifdef CONFIG_VSX
3050                         case 58:        /* plxvp */
3051                                 op->reg = VSX_REGISTER_XTP(rd);
3052                                 op->type = MKOP(LOAD_VSX, PREFIXED, 32);
3053                                 op->element_size = 32;
3054                                 break;
3055 #endif /* CONFIG_VSX */
3056                         case 60:        /* pstq */
3057                                 op->type = MKOP(STORE, PREFIXED, 16);
3058                                 break;
3059                         case 61:        /* pstd */
3060                                 op->type = MKOP(STORE, PREFIXED, 8);
3061                                 break;
3062 #ifdef CONFIG_VSX
3063                         case 62:        /* pstxvp */
3064                                 op->reg = VSX_REGISTER_XTP(rd);
3065                                 op->type = MKOP(STORE_VSX, PREFIXED, 32);
3066                                 op->element_size = 32;
3067                                 break;
3068 #endif /* CONFIG_VSX */
3069                         }
3070                         break;
3071                 case 1: /* Type 01 Eight-Byte Register-to-Register */
3072                         break;
3073                 case 2: /* Type 10 Modified Load/Store */
3074                         if (prefix_r && ra)
3075                                 break;
3076                         op->ea = mlsd_8lsd_ea(word, suffix, regs);
3077                         switch (suffixopcode) {
3078                         case 32:        /* plwz */
3079                                 op->type = MKOP(LOAD, PREFIXED, 4);
3080                                 break;
3081                         case 34:        /* plbz */
3082                                 op->type = MKOP(LOAD, PREFIXED, 1);
3083                                 break;
3084                         case 36:        /* pstw */
3085                                 op->type = MKOP(STORE, PREFIXED, 4);
3086                                 break;
3087                         case 38:        /* pstb */
3088                                 op->type = MKOP(STORE, PREFIXED, 1);
3089                                 break;
3090                         case 40:        /* plhz */
3091                                 op->type = MKOP(LOAD, PREFIXED, 2);
3092                                 break;
3093                         case 42:        /* plha */
3094                                 op->type = MKOP(LOAD, PREFIXED | SIGNEXT, 2);
3095                                 break;
3096                         case 44:        /* psth */
3097                                 op->type = MKOP(STORE, PREFIXED, 2);
3098                                 break;
3099                         case 48:        /* plfs */
3100                                 op->type = MKOP(LOAD_FP, PREFIXED | FPCONV, 4);
3101                                 break;
3102                         case 50:        /* plfd */
3103                                 op->type = MKOP(LOAD_FP, PREFIXED, 8);
3104                                 break;
3105                         case 52:        /* pstfs */
3106                                 op->type = MKOP(STORE_FP, PREFIXED | FPCONV, 4);
3107                                 break;
3108                         case 54:        /* pstfd */
3109                                 op->type = MKOP(STORE_FP, PREFIXED, 8);
3110                                 break;
3111                         }
3112                         break;
3113                 case 3: /* Type 11 Modified Register-to-Register */
3114                         break;
3115                 }
3116 #endif /* __powerpc64__ */
3117
3118         }
3119
3120         if (OP_IS_LOAD_STORE(op->type) && (op->type & UPDATE)) {
3121                 switch (GETTYPE(op->type)) {
3122                 case LOAD:
3123                         if (ra == rd)
3124                                 goto unknown_opcode;
3125                         fallthrough;
3126                 case STORE:
3127                 case LOAD_FP:
3128                 case STORE_FP:
3129                         if (ra == 0)
3130                                 goto unknown_opcode;
3131                 }
3132         }
3133
3134 #ifdef CONFIG_VSX
3135         if ((GETTYPE(op->type) == LOAD_VSX ||
3136              GETTYPE(op->type) == STORE_VSX) &&
3137             !cpu_has_feature(CPU_FTR_VSX)) {
3138                 return -1;
3139         }
3140 #endif /* CONFIG_VSX */
3141
3142         return 0;
3143
3144  unknown_opcode:
3145         op->type = UNKNOWN;
3146         return 0;
3147
3148  logical_done:
3149         if (word & 1)
3150                 set_cr0(regs, op);
3151  logical_done_nocc:
3152         op->reg = ra;
3153         op->type |= SETREG;
3154         return 1;
3155
3156  arith_done:
3157         if (word & 1)
3158                 set_cr0(regs, op);
3159  compute_done:
3160         op->reg = rd;
3161         op->type |= SETREG;
3162         return 1;
3163
3164  priv:
3165         op->type = INTERRUPT | 0x700;
3166         op->val = SRR1_PROGPRIV;
3167         return 0;
3168
3169  trap:
3170         op->type = INTERRUPT | 0x700;
3171         op->val = SRR1_PROGTRAP;
3172         return 0;
3173 }
3174 EXPORT_SYMBOL_GPL(analyse_instr);
3175 NOKPROBE_SYMBOL(analyse_instr);
3176
3177 /*
3178  * For PPC32 we always use stwu with r1 to change the stack pointer.
3179  * So this emulated store may corrupt the exception frame, now we
3180  * have to provide the exception frame trampoline, which is pushed
3181  * below the kprobed function stack. So we only update gpr[1] but
3182  * don't emulate the real store operation. We will do real store
3183  * operation safely in exception return code by checking this flag.
3184  */
3185 static nokprobe_inline int handle_stack_update(unsigned long ea, struct pt_regs *regs)
3186 {
3187         /*
3188          * Check if we already set since that means we'll
3189          * lose the previous value.
3190          */
3191         WARN_ON(test_thread_flag(TIF_EMULATE_STACK_STORE));
3192         set_thread_flag(TIF_EMULATE_STACK_STORE);
3193         return 0;
3194 }
3195
3196 static nokprobe_inline void do_signext(unsigned long *valp, int size)
3197 {
3198         switch (size) {
3199         case 2:
3200                 *valp = (signed short) *valp;
3201                 break;
3202         case 4:
3203                 *valp = (signed int) *valp;
3204                 break;
3205         }
3206 }
3207
3208 static nokprobe_inline void do_byterev(unsigned long *valp, int size)
3209 {
3210         switch (size) {
3211         case 2:
3212                 *valp = byterev_2(*valp);
3213                 break;
3214         case 4:
3215                 *valp = byterev_4(*valp);
3216                 break;
3217 #ifdef __powerpc64__
3218         case 8:
3219                 *valp = byterev_8(*valp);
3220                 break;
3221 #endif
3222         }
3223 }
3224
3225 /*
3226  * Emulate an instruction that can be executed just by updating
3227  * fields in *regs.
3228  */
3229 void emulate_update_regs(struct pt_regs *regs, struct instruction_op *op)
3230 {
3231         unsigned long next_pc;
3232
3233         next_pc = truncate_if_32bit(regs->msr, regs->nip + GETLENGTH(op->type));
3234         switch (GETTYPE(op->type)) {
3235         case COMPUTE:
3236                 if (op->type & SETREG)
3237                         regs->gpr[op->reg] = op->val;
3238                 if (op->type & SETCC)
3239                         regs->ccr = op->ccval;
3240                 if (op->type & SETXER)
3241                         regs->xer = op->xerval;
3242                 break;
3243
3244         case BRANCH:
3245                 if (op->type & SETLK)
3246                         regs->link = next_pc;
3247                 if (op->type & BRTAKEN)
3248                         next_pc = op->val;
3249                 if (op->type & DECCTR)
3250                         --regs->ctr;
3251                 break;
3252
3253         case BARRIER:
3254                 switch (op->type & BARRIER_MASK) {
3255                 case BARRIER_SYNC:
3256                         mb();
3257                         break;
3258                 case BARRIER_ISYNC:
3259                         isync();
3260                         break;
3261                 case BARRIER_EIEIO:
3262                         eieio();
3263                         break;
3264 #ifdef CONFIG_PPC64
3265                 case BARRIER_LWSYNC:
3266                         asm volatile("lwsync" : : : "memory");
3267                         break;
3268                 case BARRIER_PTESYNC:
3269                         asm volatile("ptesync" : : : "memory");
3270                         break;
3271 #endif
3272                 }
3273                 break;
3274
3275         case MFSPR:
3276                 switch (op->spr) {
3277                 case SPRN_XER:
3278                         regs->gpr[op->reg] = regs->xer & 0xffffffffUL;
3279                         break;
3280                 case SPRN_LR:
3281                         regs->gpr[op->reg] = regs->link;
3282                         break;
3283                 case SPRN_CTR:
3284                         regs->gpr[op->reg] = regs->ctr;
3285                         break;
3286                 default:
3287                         WARN_ON_ONCE(1);
3288                 }
3289                 break;
3290
3291         case MTSPR:
3292                 switch (op->spr) {
3293                 case SPRN_XER:
3294                         regs->xer = op->val & 0xffffffffUL;
3295                         break;
3296                 case SPRN_LR:
3297                         regs->link = op->val;
3298                         break;
3299                 case SPRN_CTR:
3300                         regs->ctr = op->val;
3301                         break;
3302                 default:
3303                         WARN_ON_ONCE(1);
3304                 }
3305                 break;
3306
3307         default:
3308                 WARN_ON_ONCE(1);
3309         }
3310         regs_set_return_ip(regs, next_pc);
3311 }
3312 NOKPROBE_SYMBOL(emulate_update_regs);
3313
3314 /*
3315  * Emulate a previously-analysed load or store instruction.
3316  * Return values are:
3317  * 0 = instruction emulated successfully
3318  * -EFAULT = address out of range or access faulted (regs->dar
3319  *           contains the faulting address)
3320  * -EACCES = misaligned access, instruction requires alignment
3321  * -EINVAL = unknown operation in *op
3322  */
3323 int emulate_loadstore(struct pt_regs *regs, struct instruction_op *op)
3324 {
3325         int err, size, type;
3326         int i, rd, nb;
3327         unsigned int cr;
3328         unsigned long val;
3329         unsigned long ea;
3330         bool cross_endian;
3331
3332         err = 0;
3333         size = GETSIZE(op->type);
3334         type = GETTYPE(op->type);
3335         cross_endian = (regs->msr & MSR_LE) != (MSR_KERNEL & MSR_LE);
3336         ea = truncate_if_32bit(regs->msr, op->ea);
3337
3338         switch (type) {
3339         case LARX:
3340                 if (ea & (size - 1))
3341                         return -EACCES;         /* can't handle misaligned */
3342                 if (!address_ok(regs, ea, size))
3343                         return -EFAULT;
3344                 err = 0;
3345                 val = 0;
3346                 switch (size) {
3347 #ifdef __powerpc64__
3348                 case 1:
3349                         __get_user_asmx(val, ea, err, "lbarx");
3350                         break;
3351                 case 2:
3352                         __get_user_asmx(val, ea, err, "lharx");
3353                         break;
3354 #endif
3355                 case 4:
3356                         __get_user_asmx(val, ea, err, "lwarx");
3357                         break;
3358 #ifdef __powerpc64__
3359                 case 8:
3360                         __get_user_asmx(val, ea, err, "ldarx");
3361                         break;
3362                 case 16:
3363                         err = do_lqarx(ea, &regs->gpr[op->reg]);
3364                         break;
3365 #endif
3366                 default:
3367                         return -EINVAL;
3368                 }
3369                 if (err) {
3370                         regs->dar = ea;
3371                         break;
3372                 }
3373                 if (size < 16)
3374                         regs->gpr[op->reg] = val;
3375                 break;
3376
3377         case STCX:
3378                 if (ea & (size - 1))
3379                         return -EACCES;         /* can't handle misaligned */
3380                 if (!address_ok(regs, ea, size))
3381                         return -EFAULT;
3382                 err = 0;
3383                 switch (size) {
3384 #ifdef __powerpc64__
3385                 case 1:
3386                         __put_user_asmx(op->val, ea, err, "stbcx.", cr);
3387                         break;
3388                 case 2:
3389                         __put_user_asmx(op->val, ea, err, "sthcx.", cr);
3390                         break;
3391 #endif
3392                 case 4:
3393                         __put_user_asmx(op->val, ea, err, "stwcx.", cr);
3394                         break;
3395 #ifdef __powerpc64__
3396                 case 8:
3397                         __put_user_asmx(op->val, ea, err, "stdcx.", cr);
3398                         break;
3399                 case 16:
3400                         err = do_stqcx(ea, regs->gpr[op->reg],
3401                                        regs->gpr[op->reg + 1], &cr);
3402                         break;
3403 #endif
3404                 default:
3405                         return -EINVAL;
3406                 }
3407                 if (!err)
3408                         regs->ccr = (regs->ccr & 0x0fffffff) |
3409                                 (cr & 0xe0000000) |
3410                                 ((regs->xer >> 3) & 0x10000000);
3411                 else
3412                         regs->dar = ea;
3413                 break;
3414
3415         case LOAD:
3416 #ifdef __powerpc64__
3417                 if (size == 16) {
3418                         err = emulate_lq(regs, ea, op->reg, cross_endian);
3419                         break;
3420                 }
3421 #endif
3422                 err = read_mem(&regs->gpr[op->reg], ea, size, regs);
3423                 if (!err) {
3424                         if (op->type & SIGNEXT)
3425                                 do_signext(&regs->gpr[op->reg], size);
3426                         if ((op->type & BYTEREV) == (cross_endian ? 0 : BYTEREV))
3427                                 do_byterev(&regs->gpr[op->reg], size);
3428                 }
3429                 break;
3430
3431 #ifdef CONFIG_PPC_FPU
3432         case LOAD_FP:
3433                 /*
3434                  * If the instruction is in userspace, we can emulate it even
3435                  * if the VMX state is not live, because we have the state
3436                  * stored in the thread_struct.  If the instruction is in
3437                  * the kernel, we must not touch the state in the thread_struct.
3438                  */
3439                 if (!(regs->msr & MSR_PR) && !(regs->msr & MSR_FP))
3440                         return 0;
3441                 err = do_fp_load(op, ea, regs, cross_endian);
3442                 break;
3443 #endif
3444 #ifdef CONFIG_ALTIVEC
3445         case LOAD_VMX:
3446                 if (!(regs->msr & MSR_PR) && !(regs->msr & MSR_VEC))
3447                         return 0;
3448                 err = do_vec_load(op->reg, ea, size, regs, cross_endian);
3449                 break;
3450 #endif
3451 #ifdef CONFIG_VSX
3452         case LOAD_VSX: {
3453                 unsigned long msrbit = MSR_VSX;
3454
3455                 /*
3456                  * Some VSX instructions check the MSR_VEC bit rather than MSR_VSX
3457                  * when the target of the instruction is a vector register.
3458                  */
3459                 if (op->reg >= 32 && (op->vsx_flags & VSX_CHECK_VEC))
3460                         msrbit = MSR_VEC;
3461                 if (!(regs->msr & MSR_PR) && !(regs->msr & msrbit))
3462                         return 0;
3463                 err = do_vsx_load(op, ea, regs, cross_endian);
3464                 break;
3465         }
3466 #endif
3467         case LOAD_MULTI:
3468                 if (!address_ok(regs, ea, size))
3469                         return -EFAULT;
3470                 rd = op->reg;
3471                 for (i = 0; i < size; i += 4) {
3472                         unsigned int v32 = 0;
3473
3474                         nb = size - i;
3475                         if (nb > 4)
3476                                 nb = 4;
3477                         err = copy_mem_in((u8 *) &v32, ea, nb, regs);
3478                         if (err)
3479                                 break;
3480                         if (unlikely(cross_endian))
3481                                 v32 = byterev_4(v32);
3482                         regs->gpr[rd] = v32;
3483                         ea += 4;
3484                         /* reg number wraps from 31 to 0 for lsw[ix] */
3485                         rd = (rd + 1) & 0x1f;
3486                 }
3487                 break;
3488
3489         case STORE:
3490 #ifdef __powerpc64__
3491                 if (size == 16) {
3492                         err = emulate_stq(regs, ea, op->reg, cross_endian);
3493                         break;
3494                 }
3495 #endif
3496                 if ((op->type & UPDATE) && size == sizeof(long) &&
3497                     op->reg == 1 && op->update_reg == 1 &&
3498                     !(regs->msr & MSR_PR) &&
3499                     ea >= regs->gpr[1] - STACK_INT_FRAME_SIZE) {
3500                         err = handle_stack_update(ea, regs);
3501                         break;
3502                 }
3503                 if (unlikely(cross_endian))
3504                         do_byterev(&op->val, size);
3505                 err = write_mem(op->val, ea, size, regs);
3506                 break;
3507
3508 #ifdef CONFIG_PPC_FPU
3509         case STORE_FP:
3510                 if (!(regs->msr & MSR_PR) && !(regs->msr & MSR_FP))
3511                         return 0;
3512                 err = do_fp_store(op, ea, regs, cross_endian);
3513                 break;
3514 #endif
3515 #ifdef CONFIG_ALTIVEC
3516         case STORE_VMX:
3517                 if (!(regs->msr & MSR_PR) && !(regs->msr & MSR_VEC))
3518                         return 0;
3519                 err = do_vec_store(op->reg, ea, size, regs, cross_endian);
3520                 break;
3521 #endif
3522 #ifdef CONFIG_VSX
3523         case STORE_VSX: {
3524                 unsigned long msrbit = MSR_VSX;
3525
3526                 /*
3527                  * Some VSX instructions check the MSR_VEC bit rather than MSR_VSX
3528                  * when the target of the instruction is a vector register.
3529                  */
3530                 if (op->reg >= 32 && (op->vsx_flags & VSX_CHECK_VEC))
3531                         msrbit = MSR_VEC;
3532                 if (!(regs->msr & MSR_PR) && !(regs->msr & msrbit))
3533                         return 0;
3534                 err = do_vsx_store(op, ea, regs, cross_endian);
3535                 break;
3536         }
3537 #endif
3538         case STORE_MULTI:
3539                 if (!address_ok(regs, ea, size))
3540                         return -EFAULT;
3541                 rd = op->reg;
3542                 for (i = 0; i < size; i += 4) {
3543                         unsigned int v32 = regs->gpr[rd];
3544
3545                         nb = size - i;
3546                         if (nb > 4)
3547                                 nb = 4;
3548                         if (unlikely(cross_endian))
3549                                 v32 = byterev_4(v32);
3550                         err = copy_mem_out((u8 *) &v32, ea, nb, regs);
3551                         if (err)
3552                                 break;
3553                         ea += 4;
3554                         /* reg number wraps from 31 to 0 for stsw[ix] */
3555                         rd = (rd + 1) & 0x1f;
3556                 }
3557                 break;
3558
3559         default:
3560                 return -EINVAL;
3561         }
3562
3563         if (err)
3564                 return err;
3565
3566         if (op->type & UPDATE)
3567                 regs->gpr[op->update_reg] = op->ea;
3568
3569         return 0;
3570 }
3571 NOKPROBE_SYMBOL(emulate_loadstore);
3572
3573 /*
3574  * Emulate instructions that cause a transfer of control,
3575  * loads and stores, and a few other instructions.
3576  * Returns 1 if the step was emulated, 0 if not,
3577  * or -1 if the instruction is one that should not be stepped,
3578  * such as an rfid, or a mtmsrd that would clear MSR_RI.
3579  */
3580 int emulate_step(struct pt_regs *regs, ppc_inst_t instr)
3581 {
3582         struct instruction_op op;
3583         int r, err, type;
3584         unsigned long val;
3585         unsigned long ea;
3586
3587         r = analyse_instr(&op, regs, instr);
3588         if (r < 0)
3589                 return r;
3590         if (r > 0) {
3591                 emulate_update_regs(regs, &op);
3592                 return 1;
3593         }
3594
3595         err = 0;
3596         type = GETTYPE(op.type);
3597
3598         if (OP_IS_LOAD_STORE(type)) {
3599                 err = emulate_loadstore(regs, &op);
3600                 if (err)
3601                         return 0;
3602                 goto instr_done;
3603         }
3604
3605         switch (type) {
3606         case CACHEOP:
3607                 ea = truncate_if_32bit(regs->msr, op.ea);
3608                 if (!address_ok(regs, ea, 8))
3609                         return 0;
3610                 switch (op.type & CACHEOP_MASK) {
3611                 case DCBST:
3612                         __cacheop_user_asmx(ea, err, "dcbst");
3613                         break;
3614                 case DCBF:
3615                         __cacheop_user_asmx(ea, err, "dcbf");
3616                         break;
3617                 case DCBTST:
3618                         if (op.reg == 0)
3619                                 prefetchw((void *) ea);
3620                         break;
3621                 case DCBT:
3622                         if (op.reg == 0)
3623                                 prefetch((void *) ea);
3624                         break;
3625                 case ICBI:
3626                         __cacheop_user_asmx(ea, err, "icbi");
3627                         break;
3628                 case DCBZ:
3629                         err = emulate_dcbz(ea, regs);
3630                         break;
3631                 }
3632                 if (err) {
3633                         regs->dar = ea;
3634                         return 0;
3635                 }
3636                 goto instr_done;
3637
3638         case MFMSR:
3639                 regs->gpr[op.reg] = regs->msr & MSR_MASK;
3640                 goto instr_done;
3641
3642         case MTMSR:
3643                 val = regs->gpr[op.reg];
3644                 if ((val & MSR_RI) == 0)
3645                         /* can't step mtmsr[d] that would clear MSR_RI */
3646                         return -1;
3647                 /* here op.val is the mask of bits to change */
3648                 regs_set_return_msr(regs, (regs->msr & ~op.val) | (val & op.val));
3649                 goto instr_done;
3650
3651         case SYSCALL:   /* sc */
3652                 /*
3653                  * Per ISA v3.1, section 7.5.15 'Trace Interrupt', we can't
3654                  * single step a system call instruction:
3655                  *
3656                  *   Successful completion for an instruction means that the
3657                  *   instruction caused no other interrupt. Thus a Trace
3658                  *   interrupt never occurs for a System Call or System Call
3659                  *   Vectored instruction, or for a Trap instruction that
3660                  *   traps.
3661                  */
3662                 return -1;
3663         case SYSCALL_VECTORED_0:        /* scv 0 */
3664                 return -1;
3665         case RFI:
3666                 return -1;
3667         }
3668         return 0;
3669
3670  instr_done:
3671         regs_set_return_ip(regs,
3672                 truncate_if_32bit(regs->msr, regs->nip + GETLENGTH(op.type)));
3673         return 1;
3674 }
3675 NOKPROBE_SYMBOL(emulate_step);