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