sstep.c 47.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * Single-step support.
 *
 * Copyright (C) 2004 Paul Mackerras <paulus@au.ibm.com>, IBM
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */
#include <linux/kernel.h>
12
#include <linux/kprobes.h>
13
#include <linux/ptrace.h>
14
#include <linux/prefetch.h>
15 16
#include <asm/sstep.h>
#include <asm/processor.h>
17
#include <linux/uaccess.h>
18
#include <asm/cpu_has_feature.h>
19
#include <asm/cputable.h>
20 21 22

extern char system_call_common[];

23
#ifdef CONFIG_PPC64
24
/* Bits in SRR1 that are copied from MSR */
25
#define MSR_MASK	0xffffffff87c0ffffUL
26 27 28
#else
#define MSR_MASK	0x87c0ffff
#endif
29

30 31 32 33 34
/* Bits in XER */
#define XER_SO		0x80000000U
#define XER_OV		0x40000000U
#define XER_CA		0x20000000U

S
Sean MacLennan 已提交
35
#ifdef CONFIG_PPC_FPU
36 37 38 39 40 41 42 43 44 45 46
/*
 * Functions in ldstfp.S
 */
extern int do_lfs(int rn, unsigned long ea);
extern int do_lfd(int rn, unsigned long ea);
extern int do_stfs(int rn, unsigned long ea);
extern int do_stfd(int rn, unsigned long ea);
extern int do_lvx(int rn, unsigned long ea);
extern int do_stvx(int rn, unsigned long ea);
extern int do_lxvd2x(int rn, unsigned long ea);
extern int do_stxvd2x(int rn, unsigned long ea);
S
Sean MacLennan 已提交
47
#endif
48

49 50 51
/*
 * Emulate the truncation of 64 bit values in 32-bit mode.
 */
52 53
static nokprobe_inline unsigned long truncate_if_32bit(unsigned long msr,
							unsigned long val)
54 55 56 57 58 59 60 61
{
#ifdef __powerpc64__
	if ((msr & MSR_64BIT) == 0)
		val &= 0xffffffffUL;
#endif
	return val;
}

62 63 64
/*
 * Determine whether a conditional branch instruction would branch.
 */
65 66 67
static nokprobe_inline int branch_taken(unsigned int instr,
					const struct pt_regs *regs,
					struct instruction_op *op)
68 69 70 71 72 73
{
	unsigned int bo = (instr >> 21) & 0x1f;
	unsigned int bi;

	if ((bo & 4) == 0) {
		/* decrement counter */
74 75
		op->type |= DECCTR;
		if (((bo >> 1) & 1) ^ (regs->ctr == 1))
76 77 78 79 80 81 82 83 84 85 86
			return 0;
	}
	if ((bo & 0x10) == 0) {
		/* check bit from CR */
		bi = (instr >> 16) & 0x1f;
		if (((regs->ccr >> (31 - bi)) & 1) != ((bo >> 3) & 1))
			return 0;
	}
	return 1;
}

87
static nokprobe_inline long address_ok(struct pt_regs *regs, unsigned long ea, int nb)
88 89 90 91 92 93 94 95 96
{
	if (!user_mode(regs))
		return 1;
	return __access_ok(ea, nb, USER_DS);
}

/*
 * Calculate effective address for a D-form instruction
 */
97 98
static nokprobe_inline unsigned long dform_ea(unsigned int instr,
					      const struct pt_regs *regs)
99 100 101 102 103 104
{
	int ra;
	unsigned long ea;

	ra = (instr >> 16) & 0x1f;
	ea = (signed short) instr;		/* sign-extend */
105
	if (ra)
106
		ea += regs->gpr[ra];
107 108

	return truncate_if_32bit(regs->msr, ea);
109 110 111 112 113 114
}

#ifdef __powerpc64__
/*
 * Calculate effective address for a DS-form instruction
 */
115 116
static nokprobe_inline unsigned long dsform_ea(unsigned int instr,
					       const struct pt_regs *regs)
117 118 119 120 121 122
{
	int ra;
	unsigned long ea;

	ra = (instr >> 16) & 0x1f;
	ea = (signed short) (instr & ~3);	/* sign-extend */
123
	if (ra)
124
		ea += regs->gpr[ra];
125 126

	return truncate_if_32bit(regs->msr, ea);
127 128 129 130 131 132
}
#endif /* __powerpc64 */

/*
 * Calculate effective address for an X-form instruction
 */
133
static nokprobe_inline unsigned long xform_ea(unsigned int instr,
134
					      const struct pt_regs *regs)
135 136 137 138 139 140 141
{
	int ra, rb;
	unsigned long ea;

	ra = (instr >> 16) & 0x1f;
	rb = (instr >> 11) & 0x1f;
	ea = regs->gpr[rb];
142
	if (ra)
143
		ea += regs->gpr[ra];
144 145

	return truncate_if_32bit(regs->msr, ea);
146 147 148 149 150 151
}

/*
 * Return the largest power of 2, not greater than sizeof(unsigned long),
 * such that x is a multiple of it.
 */
152
static nokprobe_inline unsigned long max_align(unsigned long x)
153 154 155 156 157 158
{
	x |= sizeof(unsigned long);
	return x & -x;		/* isolates rightmost bit */
}


159
static nokprobe_inline unsigned long byterev_2(unsigned long x)
160 161 162 163
{
	return ((x >> 8) & 0xff) | ((x & 0xff) << 8);
}

164
static nokprobe_inline unsigned long byterev_4(unsigned long x)
165 166 167 168 169 170
{
	return ((x >> 24) & 0xff) | ((x >> 8) & 0xff00) |
		((x & 0xff00) << 8) | ((x & 0xff) << 24);
}

#ifdef __powerpc64__
171
static nokprobe_inline unsigned long byterev_8(unsigned long x)
172 173 174 175 176
{
	return (byterev_4(x) << 32) | byterev_4(x >> 32);
}
#endif

177 178
static nokprobe_inline int read_mem_aligned(unsigned long *dest,
					unsigned long ea, int nb)
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
{
	int err = 0;
	unsigned long x = 0;

	switch (nb) {
	case 1:
		err = __get_user(x, (unsigned char __user *) ea);
		break;
	case 2:
		err = __get_user(x, (unsigned short __user *) ea);
		break;
	case 4:
		err = __get_user(x, (unsigned int __user *) ea);
		break;
#ifdef __powerpc64__
	case 8:
		err = __get_user(x, (unsigned long __user *) ea);
		break;
#endif
	}
	if (!err)
		*dest = x;
	return err;
}

204 205
static nokprobe_inline int read_mem_unaligned(unsigned long *dest,
				unsigned long ea, int nb, struct pt_regs *regs)
206 207 208
{
	int err;
	unsigned long x, b, c;
209 210 211
#ifdef __LITTLE_ENDIAN__
	int len = nb; /* save a copy of the length for byte reversal */
#endif
212 213 214 215

	/* unaligned, do this in pieces */
	x = 0;
	for (; nb > 0; nb -= c) {
216 217 218 219
#ifdef __LITTLE_ENDIAN__
		c = 1;
#endif
#ifdef __BIG_ENDIAN__
220
		c = max_align(ea);
221
#endif
222 223 224 225 226 227 228 229
		if (c > nb)
			c = max_align(nb);
		err = read_mem_aligned(&b, ea, c);
		if (err)
			return err;
		x = (x << (8 * c)) + b;
		ea += c;
	}
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
#ifdef __LITTLE_ENDIAN__
	switch (len) {
	case 2:
		*dest = byterev_2(x);
		break;
	case 4:
		*dest = byterev_4(x);
		break;
#ifdef __powerpc64__
	case 8:
		*dest = byterev_8(x);
		break;
#endif
	}
#endif
#ifdef __BIG_ENDIAN__
246
	*dest = x;
247
#endif
248 249 250 251 252 253 254
	return 0;
}

/*
 * Read memory at address ea for nb bytes, return 0 for success
 * or -EFAULT if an error occurred.
 */
255
static int read_mem(unsigned long *dest, unsigned long ea, int nb,
256 257 258 259 260 261 262 263
			      struct pt_regs *regs)
{
	if (!address_ok(regs, ea, nb))
		return -EFAULT;
	if ((ea & (nb - 1)) == 0)
		return read_mem_aligned(dest, ea, nb);
	return read_mem_unaligned(dest, ea, nb, regs);
}
264
NOKPROBE_SYMBOL(read_mem);
265

266 267
static nokprobe_inline int write_mem_aligned(unsigned long val,
					unsigned long ea, int nb)
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
{
	int err = 0;

	switch (nb) {
	case 1:
		err = __put_user(val, (unsigned char __user *) ea);
		break;
	case 2:
		err = __put_user(val, (unsigned short __user *) ea);
		break;
	case 4:
		err = __put_user(val, (unsigned int __user *) ea);
		break;
#ifdef __powerpc64__
	case 8:
		err = __put_user(val, (unsigned long __user *) ea);
		break;
#endif
	}
	return err;
}

290 291
static nokprobe_inline int write_mem_unaligned(unsigned long val,
				unsigned long ea, int nb, struct pt_regs *regs)
292 293 294 295
{
	int err;
	unsigned long c;

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
#ifdef __LITTLE_ENDIAN__
	switch (nb) {
	case 2:
		val = byterev_2(val);
		break;
	case 4:
		val = byterev_4(val);
		break;
#ifdef __powerpc64__
	case 8:
		val = byterev_8(val);
		break;
#endif
	}
#endif
311 312
	/* unaligned or little-endian, do this in pieces */
	for (; nb > 0; nb -= c) {
313 314 315 316
#ifdef __LITTLE_ENDIAN__
		c = 1;
#endif
#ifdef __BIG_ENDIAN__
317
		c = max_align(ea);
318
#endif
319 320 321 322 323
		if (c > nb)
			c = max_align(nb);
		err = write_mem_aligned(val >> (nb - c) * 8, ea, c);
		if (err)
			return err;
324
		ea += c;
325 326 327 328 329 330 331 332
	}
	return 0;
}

/*
 * Write memory at address ea for nb bytes, return 0 for success
 * or -EFAULT if an error occurred.
 */
333
static int write_mem(unsigned long val, unsigned long ea, int nb,
334 335 336 337 338 339 340 341
			       struct pt_regs *regs)
{
	if (!address_ok(regs, ea, nb))
		return -EFAULT;
	if ((ea & (nb - 1)) == 0)
		return write_mem_aligned(val, ea, nb);
	return write_mem_unaligned(val, ea, nb, regs);
}
342
NOKPROBE_SYMBOL(write_mem);
343

S
Sean MacLennan 已提交
344
#ifdef CONFIG_PPC_FPU
345
/*
346 347 348
 * Check the address and alignment, and call func to do the actual
 * load or store.
 */
349
static int do_fp_load(int rn, int (*func)(int, unsigned long),
350 351 352 353
				unsigned long ea, int nb,
				struct pt_regs *regs)
{
	int err;
354 355 356 357 358 359 360 361 362 363 364 365 366 367
	union {
		double dbl;
		unsigned long ul[2];
		struct {
#ifdef __BIG_ENDIAN__
			unsigned _pad_;
			unsigned word;
#endif
#ifdef __LITTLE_ENDIAN__
			unsigned word;
			unsigned _pad_;
#endif
		} single;
	} data;
368 369 370 371 372 373
	unsigned long ptr;

	if (!address_ok(regs, ea, nb))
		return -EFAULT;
	if ((ea & 3) == 0)
		return (*func)(rn, ea);
374
	ptr = (unsigned long) &data.ul;
375
	if (sizeof(unsigned long) == 8 || nb == 4) {
376 377 378
		err = read_mem_unaligned(&data.ul[0], ea, nb, regs);
		if (nb == 4)
			ptr = (unsigned long)&(data.single.word);
379 380
	} else {
		/* reading a double on 32-bit */
381
		err = read_mem_unaligned(&data.ul[0], ea, 4, regs);
382
		if (!err)
383
			err = read_mem_unaligned(&data.ul[1], ea + 4, 4, regs);
384 385 386 387 388
	}
	if (err)
		return err;
	return (*func)(rn, ptr);
}
389
NOKPROBE_SYMBOL(do_fp_load);
390

391
static int do_fp_store(int rn, int (*func)(int, unsigned long),
392 393 394 395
				 unsigned long ea, int nb,
				 struct pt_regs *regs)
{
	int err;
396 397 398 399 400 401 402 403 404 405 406 407 408 409
	union {
		double dbl;
		unsigned long ul[2];
		struct {
#ifdef __BIG_ENDIAN__
			unsigned _pad_;
			unsigned word;
#endif
#ifdef __LITTLE_ENDIAN__
			unsigned word;
			unsigned _pad_;
#endif
		} single;
	} data;
410 411 412 413 414 415
	unsigned long ptr;

	if (!address_ok(regs, ea, nb))
		return -EFAULT;
	if ((ea & 3) == 0)
		return (*func)(rn, ea);
416
	ptr = (unsigned long) &data.ul[0];
417
	if (sizeof(unsigned long) == 8 || nb == 4) {
418 419
		if (nb == 4)
			ptr = (unsigned long)&(data.single.word);
420 421 422
		err = (*func)(rn, ptr);
		if (err)
			return err;
423
		err = write_mem_unaligned(data.ul[0], ea, nb, regs);
424 425 426 427 428
	} else {
		/* writing a double on 32-bit */
		err = (*func)(rn, ptr);
		if (err)
			return err;
429
		err = write_mem_unaligned(data.ul[0], ea, 4, regs);
430
		if (!err)
431
			err = write_mem_unaligned(data.ul[1], ea + 4, 4, regs);
432 433 434
	}
	return err;
}
435
NOKPROBE_SYMBOL(do_fp_store);
S
Sean MacLennan 已提交
436
#endif
437 438 439

#ifdef CONFIG_ALTIVEC
/* For Altivec/VMX, no need to worry about alignment */
440
static nokprobe_inline int do_vec_load(int rn, int (*func)(int, unsigned long),
441 442 443 444 445 446 447
				 unsigned long ea, struct pt_regs *regs)
{
	if (!address_ok(regs, ea & ~0xfUL, 16))
		return -EFAULT;
	return (*func)(rn, ea);
}

448
static nokprobe_inline int do_vec_store(int rn, int (*func)(int, unsigned long),
449 450 451 452 453 454 455 456 457
				  unsigned long ea, struct pt_regs *regs)
{
	if (!address_ok(regs, ea & ~0xfUL, 16))
		return -EFAULT;
	return (*func)(rn, ea);
}
#endif /* CONFIG_ALTIVEC */

#ifdef CONFIG_VSX
458
static nokprobe_inline int do_vsx_load(int rn, int (*func)(int, unsigned long),
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
				 unsigned long ea, struct pt_regs *regs)
{
	int err;
	unsigned long val[2];

	if (!address_ok(regs, ea, 16))
		return -EFAULT;
	if ((ea & 3) == 0)
		return (*func)(rn, ea);
	err = read_mem_unaligned(&val[0], ea, 8, regs);
	if (!err)
		err = read_mem_unaligned(&val[1], ea + 8, 8, regs);
	if (!err)
		err = (*func)(rn, (unsigned long) &val[0]);
	return err;
}

476
static nokprobe_inline int do_vsx_store(int rn, int (*func)(int, unsigned long),
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
				 unsigned long ea, struct pt_regs *regs)
{
	int err;
	unsigned long val[2];

	if (!address_ok(regs, ea, 16))
		return -EFAULT;
	if ((ea & 3) == 0)
		return (*func)(rn, ea);
	err = (*func)(rn, (unsigned long) &val[0]);
	if (err)
		return err;
	err = write_mem_unaligned(val[0], ea, 8, regs);
	if (!err)
		err = write_mem_unaligned(val[1], ea + 8, 8, regs);
	return err;
}
#endif /* CONFIG_VSX */

#define __put_user_asmx(x, addr, err, op, cr)		\
	__asm__ __volatile__(				\
		"1:	" op " %2,0,%3\n"		\
		"	mfcr	%1\n"			\
		"2:\n"					\
		".section .fixup,\"ax\"\n"		\
		"3:	li	%0,%4\n"		\
		"	b	2b\n"			\
		".previous\n"				\
505
		EX_TABLE(1b, 3b)			\
506 507 508 509 510 511 512 513 514 515 516
		: "=r" (err), "=r" (cr)			\
		: "r" (x), "r" (addr), "i" (-EFAULT), "0" (err))

#define __get_user_asmx(x, addr, err, op)		\
	__asm__ __volatile__(				\
		"1:	"op" %1,0,%2\n"			\
		"2:\n"					\
		".section .fixup,\"ax\"\n"		\
		"3:	li	%0,%3\n"		\
		"	b	2b\n"			\
		".previous\n"				\
517
		EX_TABLE(1b, 3b)			\
518 519 520 521 522 523 524 525 526 527 528
		: "=r" (err), "=r" (x)			\
		: "r" (addr), "i" (-EFAULT), "0" (err))

#define __cacheop_user_asmx(addr, err, op)		\
	__asm__ __volatile__(				\
		"1:	"op" 0,%1\n"			\
		"2:\n"					\
		".section .fixup,\"ax\"\n"		\
		"3:	li	%0,%3\n"		\
		"	b	2b\n"			\
		".previous\n"				\
529
		EX_TABLE(1b, 3b)			\
530 531 532
		: "=r" (err)				\
		: "r" (addr), "i" (-EFAULT), "0" (err))

533 534
static nokprobe_inline void set_cr0(const struct pt_regs *regs,
				    struct instruction_op *op, int rd)
535 536 537
{
	long val = regs->gpr[rd];

538 539
	op->type |= SETCC;
	op->ccval = (regs->ccr & 0x0fffffff) | ((regs->xer >> 3) & 0x10000000);
540
#ifdef __powerpc64__
541
	if (!(regs->msr & MSR_64BIT))
542 543 544
		val = (int) val;
#endif
	if (val < 0)
545
		op->ccval |= 0x80000000;
546
	else if (val > 0)
547
		op->ccval |= 0x40000000;
548
	else
549
		op->ccval |= 0x20000000;
550 551
}

552 553
static nokprobe_inline void add_with_carry(const struct pt_regs *regs,
				     struct instruction_op *op, int rd,
554 555 556 557 558 559 560
				     unsigned long val1, unsigned long val2,
				     unsigned long carry_in)
{
	unsigned long val = val1 + val2;

	if (carry_in)
		++val;
561 562 563
	op->type = COMPUTE + SETREG + SETXER;
	op->reg = rd;
	op->val = val;
564
#ifdef __powerpc64__
565
	if (!(regs->msr & MSR_64BIT)) {
566 567 568 569
		val = (unsigned int) val;
		val1 = (unsigned int) val1;
	}
#endif
570
	op->xerval = regs->xer;
571
	if (val < val1 || (carry_in && val == val1))
572
		op->xerval |= XER_CA;
573
	else
574
		op->xerval &= ~XER_CA;
575 576
}

577 578 579
static nokprobe_inline void do_cmp_signed(const struct pt_regs *regs,
					  struct instruction_op *op,
					  long v1, long v2, int crfld)
580 581 582
{
	unsigned int crval, shift;

583
	op->type = COMPUTE + SETCC;
584 585 586 587 588 589 590 591
	crval = (regs->xer >> 31) & 1;		/* get SO bit */
	if (v1 < v2)
		crval |= 8;
	else if (v1 > v2)
		crval |= 4;
	else
		crval |= 2;
	shift = (7 - crfld) * 4;
592
	op->ccval = (regs->ccr & ~(0xf << shift)) | (crval << shift);
593 594
}

595 596 597 598
static nokprobe_inline void do_cmp_unsigned(const struct pt_regs *regs,
					    struct instruction_op *op,
					    unsigned long v1,
					    unsigned long v2, int crfld)
599 600 601
{
	unsigned int crval, shift;

602
	op->type = COMPUTE + SETCC;
603 604 605 606 607 608 609 610
	crval = (regs->xer >> 31) & 1;		/* get SO bit */
	if (v1 < v2)
		crval |= 8;
	else if (v1 > v2)
		crval |= 4;
	else
		crval |= 2;
	shift = (7 - crfld) * 4;
611
	op->ccval = (regs->ccr & ~(0xf << shift)) | (crval << shift);
612 613
}

614 615 616
static nokprobe_inline void do_cmpb(const struct pt_regs *regs,
				    struct instruction_op *op,
				    unsigned long v1, unsigned long v2)
617 618 619 620 621 622 623 624 625 626
{
	unsigned long long out_val, mask;
	int i;

	out_val = 0;
	for (i = 0; i < 8; i++) {
		mask = 0xffUL << (i * 8);
		if ((v1 & mask) == (v2 & mask))
			out_val |= mask;
	}
627
	op->val = out_val;
628 629
}

630 631 632 633
/*
 * The size parameter is used to adjust the equivalent popcnt instruction.
 * popcntb = 8, popcntw = 32, popcntd = 64
 */
634 635 636
static nokprobe_inline void do_popcnt(const struct pt_regs *regs,
				      struct instruction_op *op,
				      unsigned long v1, int size)
637 638 639 640 641 642 643 644
{
	unsigned long long out = v1;

	out -= (out >> 1) & 0x5555555555555555;
	out = (0x3333333333333333 & out) + (0x3333333333333333 & (out >> 2));
	out = (out + (out >> 4)) & 0x0f0f0f0f0f0f0f0f;

	if (size == 8) {	/* popcntb */
645
		op->val = out;
646 647 648 649 650
		return;
	}
	out += out >> 8;
	out += out >> 16;
	if (size == 32) {	/* popcntw */
651
		op->val = out & 0x0000003f0000003f;
652 653 654 655
		return;
	}

	out = (out + (out >> 32)) & 0x7f;
656
	op->val = out;	/* popcntd */
657 658
}

659
#ifdef CONFIG_PPC64
660 661 662
static nokprobe_inline void do_bpermd(const struct pt_regs *regs,
				      struct instruction_op *op,
				      unsigned long v1, unsigned long v2)
663 664 665 666 667 668 669 670 671 672 673
{
	unsigned char perm, idx;
	unsigned int i;

	perm = 0;
	for (i = 0; i < 8; i++) {
		idx = (v1 >> (i * 8)) & 0xff;
		if (idx < 64)
			if (v2 & PPC_BIT(idx))
				perm |= 1 << i;
	}
674
	op->val = perm;
675 676
}
#endif /* CONFIG_PPC64 */
677 678 679 680
/*
 * The size parameter adjusts the equivalent prty instruction.
 * prtyw = 32, prtyd = 64
 */
681 682 683
static nokprobe_inline void do_prty(const struct pt_regs *regs,
				    struct instruction_op *op,
				    unsigned long v, int size)
684 685 686 687 688
{
	unsigned long long res = v ^ (v >> 8);

	res ^= res >> 16;
	if (size == 32) {		/* prtyw */
689
		op->val = res & 0x0000000100000001;
690 691 692 693
		return;
	}

	res ^= res >> 32;
694
	op->val = res & 1;	/*prtyd */
695
}
696

697
static nokprobe_inline int trap_compare(long v1, long v2)
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
{
	int ret = 0;

	if (v1 < v2)
		ret |= 0x10;
	else if (v1 > v2)
		ret |= 0x08;
	else
		ret |= 0x04;
	if ((unsigned long)v1 < (unsigned long)v2)
		ret |= 0x02;
	else if ((unsigned long)v1 > (unsigned long)v2)
		ret |= 0x01;
	return ret;
}

714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
/*
 * Elements of 32-bit rotate and mask instructions.
 */
#define MASK32(mb, me)	((0xffffffffUL >> (mb)) + \
			 ((signed long)-0x80000000L >> (me)) + ((me) >= (mb)))
#ifdef __powerpc64__
#define MASK64_L(mb)	(~0UL >> (mb))
#define MASK64_R(me)	((signed long)-0x8000000000000000L >> (me))
#define MASK64(mb, me)	(MASK64_L(mb) + MASK64_R(me) + ((me) >= (mb)))
#define DATA32(x)	(((x) & 0xffffffffUL) | (((x) & 0xffffffffUL) << 32))
#else
#define DATA32(x)	(x)
#endif
#define ROTATE(x, n)	((n) ? (((x) << (n)) | ((x) >> (8 * sizeof(long) - (n)))) : (x))

/*
730 731 732 733 734 735 736 737 738
 * Decode an instruction, and return information about it in *op
 * without changing *regs.
 * Integer arithmetic and logical instructions, branches, and barrier
 * instructions can be emulated just using the information in *op.
 *
 * Return value is 1 if the instruction can be emulated just by
 * updating *regs with the information in *op, -1 if we need the
 * GPRs but *regs doesn't contain the full register set, or 0
 * otherwise.
739
 */
740 741
int analyse_instr(struct instruction_op *op, const struct pt_regs *regs,
		  unsigned int instr)
742
{
743
	unsigned int opcode, ra, rb, rd, spr, u;
744
	unsigned long int imm;
745
	unsigned long int val, val2;
746
	unsigned int mb, me, sh;
747
	long ival;
748

749 750
	op->type = COMPUTE;

751 752 753
	opcode = instr >> 26;
	switch (opcode) {
	case 16:	/* bc */
754
		op->type = BRANCH;
755 756 757
		imm = (signed short)(instr & 0xfffc);
		if ((instr & 2) == 0)
			imm += regs->nip;
758
		op->val = truncate_if_32bit(regs->msr, imm);
759
		if (instr & 1)
760 761 762
			op->type |= SETLK;
		if (branch_taken(instr, regs, op))
			op->type |= BRTAKEN;
763
		return 1;
764
#ifdef CONFIG_PPC64
765
	case 17:	/* sc */
766 767 768 769 770
		if ((instr & 0xfe2) == 2)
			op->type = SYSCALL;
		else
			op->type = UNKNOWN;
		return 0;
771
#endif
772
	case 18:	/* b */
773
		op->type = BRANCH | BRTAKEN;
774 775 776 777 778
		imm = instr & 0x03fffffc;
		if (imm & 0x02000000)
			imm -= 0x04000000;
		if ((instr & 2) == 0)
			imm += regs->nip;
779
		op->val = truncate_if_32bit(regs->msr, imm);
780
		if (instr & 1)
781
			op->type |= SETLK;
782 783
		return 1;
	case 19:
784
		switch ((instr >> 1) & 0x3ff) {
785
		case 0:		/* mcrf */
786
			op->type = COMPUTE + SETCC;
787 788 789 790
			rd = 7 - ((instr >> 23) & 0x7);
			ra = 7 - ((instr >> 18) & 0x7);
			rd *= 4;
			ra *= 4;
791
			val = (regs->ccr >> ra) & 0xf;
792 793
			op->ccval = (regs->ccr & ~(0xfUL << rd)) | (val << rd);
			return 1;
794

795 796
		case 16:	/* bclr */
		case 528:	/* bcctr */
797
			op->type = BRANCH;
798
			imm = (instr & 0x400)? regs->ctr: regs->link;
799
			op->val = truncate_if_32bit(regs->msr, imm);
800
			if (instr & 1)
801 802 803
				op->type |= SETLK;
			if (branch_taken(instr, regs, op))
				op->type |= BRTAKEN;
804
			return 1;
805 806

		case 18:	/* rfid, scary */
807 808 809 810
			if (regs->msr & MSR_PR)
				goto priv;
			op->type = RFI;
			return 0;
811 812

		case 150:	/* isync */
813 814
			op->type = BARRIER | BARRIER_ISYNC;
			return 1;
815 816 817 818 819 820 821 822 823

		case 33:	/* crnor */
		case 129:	/* crandc */
		case 193:	/* crxor */
		case 225:	/* crnand */
		case 257:	/* crand */
		case 289:	/* creqv */
		case 417:	/* crorc */
		case 449:	/* cror */
824
			op->type = COMPUTE + SETCC;
825 826 827 828 829 830
			ra = (instr >> 16) & 0x1f;
			rb = (instr >> 11) & 0x1f;
			rd = (instr >> 21) & 0x1f;
			ra = (regs->ccr >> (31 - ra)) & 1;
			rb = (regs->ccr >> (31 - rb)) & 1;
			val = (instr >> (6 + ra * 2 + rb)) & 1;
831
			op->ccval = (regs->ccr & ~(1UL << (31 - rd))) |
832
				(val << (31 - rd));
833 834 835 836
			return 1;
		default:
			op->type = UNKNOWN;
			return 0;
837 838 839 840 841
		}
		break;
	case 31:
		switch ((instr >> 1) & 0x3ff) {
		case 598:	/* sync */
842
			op->type = BARRIER + BARRIER_SYNC;
843 844 845
#ifdef __powerpc64__
			switch ((instr >> 21) & 3) {
			case 1:		/* lwsync */
846 847
				op->type = BARRIER + BARRIER_LWSYNC;
				break;
848
			case 2:		/* ptesync */
849 850
				op->type = BARRIER + BARRIER_PTESYNC;
				break;
851 852
			}
#endif
853
			return 1;
854 855

		case 854:	/* eieio */
856 857
			op->type = BARRIER + BARRIER_EIEIO;
			return 1;
858 859 860 861 862 863
		}
		break;
	}

	/* Following cases refer to regs->gpr[], so we need all regs */
	if (!FULL_REGS(regs))
864
		return -1;
865 866 867 868 869 870

	rd = (instr >> 21) & 0x1f;
	ra = (instr >> 16) & 0x1f;
	rb = (instr >> 11) & 0x1f;

	switch (opcode) {
871 872 873 874
#ifdef __powerpc64__
	case 2:		/* tdi */
		if (rd & trap_compare(regs->gpr[ra], (short) instr))
			goto trap;
875
		return 1;
876 877 878 879
#endif
	case 3:		/* twi */
		if (rd & trap_compare((int)regs->gpr[ra], (short) instr))
			goto trap;
880
		return 1;
881

882
	case 7:		/* mulli */
883 884
		op->val = regs->gpr[ra] * (short) instr;
		goto compute_done;
885 886 887

	case 8:		/* subfic */
		imm = (short) instr;
888 889
		add_with_carry(regs, op, rd, ~regs->gpr[ra], imm, 1);
		return 1;
890 891 892 893 894 895 896 897

	case 10:	/* cmpli */
		imm = (unsigned short) instr;
		val = regs->gpr[ra];
#ifdef __powerpc64__
		if ((rd & 1) == 0)
			val = (unsigned int) val;
#endif
898 899
		do_cmp_unsigned(regs, op, val, imm, rd >> 2);
		return 1;
900 901 902 903 904 905 906 907

	case 11:	/* cmpi */
		imm = (short) instr;
		val = regs->gpr[ra];
#ifdef __powerpc64__
		if ((rd & 1) == 0)
			val = (int) val;
#endif
908 909
		do_cmp_signed(regs, op, val, imm, rd >> 2);
		return 1;
910 911 912

	case 12:	/* addic */
		imm = (short) instr;
913 914
		add_with_carry(regs, op, rd, regs->gpr[ra], imm, 0);
		return 1;
915 916 917

	case 13:	/* addic. */
		imm = (short) instr;
918 919 920
		add_with_carry(regs, op, rd, regs->gpr[ra], imm, 0);
		set_cr0(regs, op, rd);
		return 1;
921 922 923 924 925

	case 14:	/* addi */
		imm = (short) instr;
		if (ra)
			imm += regs->gpr[ra];
926 927
		op->val = imm;
		goto compute_done;
928 929 930 931 932

	case 15:	/* addis */
		imm = ((short) instr) << 16;
		if (ra)
			imm += regs->gpr[ra];
933 934
		op->val = imm;
		goto compute_done;
935 936 937 938 939 940

	case 20:	/* rlwimi */
		mb = (instr >> 6) & 0x1f;
		me = (instr >> 1) & 0x1f;
		val = DATA32(regs->gpr[rd]);
		imm = MASK32(mb, me);
941
		op->val = (regs->gpr[ra] & ~imm) | (ROTATE(val, rb) & imm);
942 943 944 945 946 947
		goto logical_done;

	case 21:	/* rlwinm */
		mb = (instr >> 6) & 0x1f;
		me = (instr >> 1) & 0x1f;
		val = DATA32(regs->gpr[rd]);
948
		op->val = ROTATE(val, rb) & MASK32(mb, me);
949 950 951 952 953 954 955
		goto logical_done;

	case 23:	/* rlwnm */
		mb = (instr >> 6) & 0x1f;
		me = (instr >> 1) & 0x1f;
		rb = regs->gpr[rb] & 0x1f;
		val = DATA32(regs->gpr[rd]);
956
		op->val = ROTATE(val, rb) & MASK32(mb, me);
957 958 959
		goto logical_done;

	case 24:	/* ori */
960 961
		op->val = regs->gpr[rd] | (unsigned short) instr;
		goto logical_done_nocc;
962 963 964

	case 25:	/* oris */
		imm = (unsigned short) instr;
965 966
		op->val = regs->gpr[rd] | (imm << 16);
		goto logical_done_nocc;
967 968

	case 26:	/* xori */
969 970
		op->val = regs->gpr[rd] ^ (unsigned short) instr;
		goto logical_done_nocc;
971 972 973

	case 27:	/* xoris */
		imm = (unsigned short) instr;
974 975
		op->val = regs->gpr[rd] ^ (imm << 16);
		goto logical_done_nocc;
976 977

	case 28:	/* andi. */
978 979 980
		op->val = regs->gpr[rd] & (unsigned short) instr;
		set_cr0(regs, op, ra);
		goto logical_done_nocc;
981 982 983

	case 29:	/* andis. */
		imm = (unsigned short) instr;
984 985 986
		op->val = regs->gpr[rd] & (imm << 16);
		set_cr0(regs, op, ra);
		goto logical_done_nocc;
987 988 989 990 991 992 993 994 995 996

#ifdef __powerpc64__
	case 30:	/* rld* */
		mb = ((instr >> 6) & 0x1f) | (instr & 0x20);
		val = regs->gpr[rd];
		if ((instr & 0x10) == 0) {
			sh = rb | ((instr & 2) << 4);
			val = ROTATE(val, sh);
			switch ((instr >> 2) & 3) {
			case 0:		/* rldicl */
997 998
				val &= MASK64_L(mb);
				break;
999
			case 1:		/* rldicr */
1000 1001
				val &= MASK64_R(mb);
				break;
1002
			case 2:		/* rldic */
1003 1004
				val &= MASK64(mb, 63 - sh);
				break;
1005 1006
			case 3:		/* rldimi */
				imm = MASK64(mb, 63 - sh);
1007
				val = (regs->gpr[ra] & ~imm) |
1008 1009
					(val & imm);
			}
1010 1011
			op->val = val;
			goto logical_done;
1012 1013 1014 1015 1016
		} else {
			sh = regs->gpr[rb] & 0x3f;
			val = ROTATE(val, sh);
			switch ((instr >> 1) & 7) {
			case 0:		/* rldcl */
1017
				op->val = val & MASK64_L(mb);
1018 1019
				goto logical_done;
			case 1:		/* rldcr */
1020
				op->val = val & MASK64_R(mb);
1021 1022
				goto logical_done;
			}
1023
		}
1024
#endif
1025 1026
		op->type = UNKNOWN;	/* illegal instruction */
		return 0;
1027

1028
	case 31:
1029
		switch ((instr >> 1) & 0x3ff) {
1030 1031 1032 1033 1034
		case 4:		/* tw */
			if (rd == 0x1f ||
			    (rd & trap_compare((int)regs->gpr[ra],
					       (int)regs->gpr[rb])))
				goto trap;
1035
			return 1;
1036 1037 1038 1039
#ifdef __powerpc64__
		case 68:	/* td */
			if (rd & trap_compare(regs->gpr[ra], regs->gpr[rb]))
				goto trap;
1040
			return 1;
1041
#endif
1042 1043
		case 83:	/* mfmsr */
			if (regs->msr & MSR_PR)
1044 1045 1046 1047
				goto priv;
			op->type = MFMSR;
			op->reg = rd;
			return 0;
1048 1049
		case 146:	/* mtmsr */
			if (regs->msr & MSR_PR)
1050 1051 1052 1053 1054
				goto priv;
			op->type = MTMSR;
			op->reg = rd;
			op->val = 0xffffffff & ~(MSR_ME | MSR_LE);
			return 0;
1055
#ifdef CONFIG_PPC64
1056 1057
		case 178:	/* mtmsrd */
			if (regs->msr & MSR_PR)
1058 1059 1060 1061 1062 1063 1064 1065
				goto priv;
			op->type = MTMSR;
			op->reg = rd;
			/* only MSR_EE and MSR_RI get changed if bit 15 set */
			/* mtmsrd doesn't change MSR_HV, MSR_ME or MSR_LE */
			imm = (instr & 0x10000)? 0x8002: 0xefffffffffffeffeUL;
			op->val = imm;
			return 0;
1066
#endif
1067

1068
		case 19:	/* mfcr */
1069
			imm = 0xffffffffUL;
1070 1071 1072
			if ((instr >> 20) & 1) {
				imm = 0xf0000000UL;
				for (sh = 0; sh < 8; ++sh) {
1073
					if (instr & (0x80000 >> sh))
1074 1075 1076 1077
						break;
					imm >>= 4;
				}
			}
1078 1079
			op->val = regs->ccr & imm;
			goto compute_done;
1080 1081

		case 144:	/* mtcrf */
1082
			op->type = COMPUTE + SETCC;
1083 1084
			imm = 0xf0000000UL;
			val = regs->gpr[rd];
1085
			op->val = regs->ccr;
1086 1087
			for (sh = 0; sh < 8; ++sh) {
				if (instr & (0x80000 >> sh))
1088
					op->val = (op->val & ~imm) |
1089 1090 1091
						(val & imm);
				imm >>= 4;
			}
1092
			return 1;
1093 1094

		case 339:	/* mfspr */
1095
			spr = ((instr >> 16) & 0x1f) | ((instr >> 6) & 0x3e0);
1096 1097 1098 1099 1100 1101 1102
			op->type = MFSPR;
			op->reg = rd;
			op->spr = spr;
			if (spr == SPRN_XER || spr == SPRN_LR ||
			    spr == SPRN_CTR)
				return 1;
			return 0;
1103 1104

		case 467:	/* mtspr */
1105
			spr = ((instr >> 16) & 0x1f) | ((instr >> 6) & 0x3e0);
1106 1107 1108 1109 1110 1111 1112
			op->type = MTSPR;
			op->val = regs->gpr[rd];
			op->spr = spr;
			if (spr == SPRN_XER || spr == SPRN_LR ||
			    spr == SPRN_CTR)
				return 1;
			return 0;
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126

/*
 * Compare instructions
 */
		case 0:	/* cmp */
			val = regs->gpr[ra];
			val2 = regs->gpr[rb];
#ifdef __powerpc64__
			if ((rd & 1) == 0) {
				/* word (32-bit) compare */
				val = (int) val;
				val2 = (int) val2;
			}
#endif
1127 1128
			do_cmp_signed(regs, op, val, val2, rd >> 2);
			return 1;
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139

		case 32:	/* cmpl */
			val = regs->gpr[ra];
			val2 = regs->gpr[rb];
#ifdef __powerpc64__
			if ((rd & 1) == 0) {
				/* word (32-bit) compare */
				val = (unsigned int) val;
				val2 = (unsigned int) val2;
			}
#endif
1140 1141
			do_cmp_unsigned(regs, op, val, val2, rd >> 2);
			return 1;
1142

1143
		case 508: /* cmpb */
1144 1145
			do_cmpb(regs, op, regs->gpr[rd], regs->gpr[rb]);
			goto logical_done_nocc;
1146

1147 1148 1149 1150
/*
 * Arithmetic instructions
 */
		case 8:	/* subfc */
1151
			add_with_carry(regs, op, rd, ~regs->gpr[ra],
1152 1153 1154 1155
				       regs->gpr[rb], 1);
			goto arith_done;
#ifdef __powerpc64__
		case 9:	/* mulhdu */
1156
			asm("mulhdu %0,%1,%2" : "=r" (op->val) :
1157 1158 1159 1160
			    "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
			goto arith_done;
#endif
		case 10:	/* addc */
1161
			add_with_carry(regs, op, rd, regs->gpr[ra],
1162 1163 1164 1165
				       regs->gpr[rb], 0);
			goto arith_done;

		case 11:	/* mulhwu */
1166
			asm("mulhwu %0,%1,%2" : "=r" (op->val) :
1167 1168 1169 1170
			    "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
			goto arith_done;

		case 40:	/* subf */
1171
			op->val = regs->gpr[rb] - regs->gpr[ra];
1172 1173 1174
			goto arith_done;
#ifdef __powerpc64__
		case 73:	/* mulhd */
1175
			asm("mulhd %0,%1,%2" : "=r" (op->val) :
1176 1177 1178 1179
			    "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
			goto arith_done;
#endif
		case 75:	/* mulhw */
1180
			asm("mulhw %0,%1,%2" : "=r" (op->val) :
1181 1182 1183 1184
			    "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
			goto arith_done;

		case 104:	/* neg */
1185
			op->val = -regs->gpr[ra];
1186 1187 1188
			goto arith_done;

		case 136:	/* subfe */
1189 1190
			add_with_carry(regs, op, rd, ~regs->gpr[ra],
				       regs->gpr[rb], regs->xer & XER_CA);
1191 1192 1193
			goto arith_done;

		case 138:	/* adde */
1194 1195
			add_with_carry(regs, op, rd, regs->gpr[ra],
				       regs->gpr[rb], regs->xer & XER_CA);
1196 1197 1198
			goto arith_done;

		case 200:	/* subfze */
1199
			add_with_carry(regs, op, rd, ~regs->gpr[ra], 0L,
1200 1201 1202 1203
				       regs->xer & XER_CA);
			goto arith_done;

		case 202:	/* addze */
1204
			add_with_carry(regs, op, rd, regs->gpr[ra], 0L,
1205 1206 1207 1208
				       regs->xer & XER_CA);
			goto arith_done;

		case 232:	/* subfme */
1209
			add_with_carry(regs, op, rd, ~regs->gpr[ra], -1L,
1210 1211 1212 1213
				       regs->xer & XER_CA);
			goto arith_done;
#ifdef __powerpc64__
		case 233:	/* mulld */
1214
			op->val = regs->gpr[ra] * regs->gpr[rb];
1215 1216 1217
			goto arith_done;
#endif
		case 234:	/* addme */
1218
			add_with_carry(regs, op, rd, regs->gpr[ra], -1L,
1219 1220 1221 1222
				       regs->xer & XER_CA);
			goto arith_done;

		case 235:	/* mullw */
1223
			op->val = (unsigned int) regs->gpr[ra] *
1224 1225 1226 1227
				(unsigned int) regs->gpr[rb];
			goto arith_done;

		case 266:	/* add */
1228
			op->val = regs->gpr[ra] + regs->gpr[rb];
1229 1230 1231
			goto arith_done;
#ifdef __powerpc64__
		case 457:	/* divdu */
1232
			op->val = regs->gpr[ra] / regs->gpr[rb];
1233 1234 1235
			goto arith_done;
#endif
		case 459:	/* divwu */
1236
			op->val = (unsigned int) regs->gpr[ra] /
1237 1238 1239 1240
				(unsigned int) regs->gpr[rb];
			goto arith_done;
#ifdef __powerpc64__
		case 489:	/* divd */
1241
			op->val = (long int) regs->gpr[ra] /
1242 1243 1244 1245
				(long int) regs->gpr[rb];
			goto arith_done;
#endif
		case 491:	/* divw */
1246
			op->val = (int) regs->gpr[ra] /
1247 1248 1249 1250 1251 1252 1253
				(int) regs->gpr[rb];
			goto arith_done;


/*
 * Logical instructions
 */
1254 1255 1256 1257 1258
		case 15:	/* isel */
			mb = (instr >> 6) & 0x1f; /* bc */
			val = (regs->ccr >> (31 - mb)) & 1;
			val2 = (ra) ? regs->gpr[ra] : 0;

1259 1260
			op->val = (val) ? val2 : regs->gpr[rb];
			goto compute_done;
1261

1262
		case 26:	/* cntlzw */
1263
			op->val = __builtin_clz((unsigned int) regs->gpr[rd]);
1264 1265 1266
			goto logical_done;
#ifdef __powerpc64__
		case 58:	/* cntlzd */
1267
			op->val = __builtin_clzl(regs->gpr[rd]);
1268 1269 1270
			goto logical_done;
#endif
		case 28:	/* and */
1271
			op->val = regs->gpr[rd] & regs->gpr[rb];
1272 1273 1274
			goto logical_done;

		case 60:	/* andc */
1275
			op->val = regs->gpr[rd] & ~regs->gpr[rb];
1276 1277
			goto logical_done;

1278
		case 122:	/* popcntb */
1279
			do_popcnt(regs, op, regs->gpr[rd], 8);
1280 1281
			goto logical_done;

1282
		case 124:	/* nor */
1283
			op->val = ~(regs->gpr[rd] | regs->gpr[rb]);
1284
			goto logical_done;
1285 1286

		case 154:	/* prtyw */
1287
			do_prty(regs, op, regs->gpr[rd], 32);
1288 1289 1290
			goto logical_done;

		case 186:	/* prtyd */
1291
			do_prty(regs, op, regs->gpr[rd], 64);
1292
			goto logical_done;
1293 1294
#ifdef CONFIG_PPC64
		case 252:	/* bpermd */
1295
			do_bpermd(regs, op, regs->gpr[rd], regs->gpr[rb]);
1296 1297
			goto logical_done;
#endif
1298
		case 284:	/* xor */
1299
			op->val = ~(regs->gpr[rd] ^ regs->gpr[rb]);
1300 1301 1302
			goto logical_done;

		case 316:	/* xor */
1303
			op->val = regs->gpr[rd] ^ regs->gpr[rb];
1304 1305
			goto logical_done;

1306
		case 378:	/* popcntw */
1307
			do_popcnt(regs, op, regs->gpr[rd], 32);
1308 1309
			goto logical_done;

1310
		case 412:	/* orc */
1311
			op->val = regs->gpr[rd] | ~regs->gpr[rb];
1312 1313 1314
			goto logical_done;

		case 444:	/* or */
1315
			op->val = regs->gpr[rd] | regs->gpr[rb];
1316 1317 1318
			goto logical_done;

		case 476:	/* nand */
1319
			op->val = ~(regs->gpr[rd] & regs->gpr[rb]);
1320
			goto logical_done;
1321 1322
#ifdef CONFIG_PPC64
		case 506:	/* popcntd */
1323
			do_popcnt(regs, op, regs->gpr[rd], 64);
1324 1325
			goto logical_done;
#endif
1326
		case 922:	/* extsh */
1327
			op->val = (signed short) regs->gpr[rd];
1328 1329 1330
			goto logical_done;

		case 954:	/* extsb */
1331
			op->val = (signed char) regs->gpr[rd];
1332 1333 1334
			goto logical_done;
#ifdef __powerpc64__
		case 986:	/* extsw */
1335
			op->val = (signed int) regs->gpr[rd];
1336 1337 1338 1339 1340 1341 1342 1343 1344
			goto logical_done;
#endif

/*
 * Shift instructions
 */
		case 24:	/* slw */
			sh = regs->gpr[rb] & 0x3f;
			if (sh < 32)
1345
				op->val = (regs->gpr[rd] << sh) & 0xffffffffUL;
1346
			else
1347
				op->val = 0;
1348 1349 1350 1351 1352
			goto logical_done;

		case 536:	/* srw */
			sh = regs->gpr[rb] & 0x3f;
			if (sh < 32)
1353
				op->val = (regs->gpr[rd] & 0xffffffffUL) >> sh;
1354
			else
1355
				op->val = 0;
1356 1357 1358
			goto logical_done;

		case 792:	/* sraw */
1359
			op->type = COMPUTE + SETREG + SETXER;
1360 1361
			sh = regs->gpr[rb] & 0x3f;
			ival = (signed int) regs->gpr[rd];
1362 1363
			op->val = ival >> (sh < 32 ? sh : 31);
			op->xerval = regs->xer;
1364
			if (ival < 0 && (sh >= 32 || (ival & ((1ul << sh) - 1)) != 0))
1365
				op->xerval |= XER_CA;
1366
			else
1367
				op->xerval &= ~XER_CA;
1368 1369 1370
			goto logical_done;

		case 824:	/* srawi */
1371
			op->type = COMPUTE + SETREG + SETXER;
1372 1373
			sh = rb;
			ival = (signed int) regs->gpr[rd];
1374 1375
			op->val = ival >> sh;
			op->xerval = regs->xer;
1376
			if (ival < 0 && (ival & ((1ul << sh) - 1)) != 0)
1377
				op->xerval |= XER_CA;
1378
			else
1379
				op->xerval &= ~XER_CA;
1380 1381 1382 1383
			goto logical_done;

#ifdef __powerpc64__
		case 27:	/* sld */
1384
			sh = regs->gpr[rb] & 0x7f;
1385
			if (sh < 64)
1386
				op->val = regs->gpr[rd] << sh;
1387
			else
1388
				op->val = 0;
1389 1390 1391 1392 1393
			goto logical_done;

		case 539:	/* srd */
			sh = regs->gpr[rb] & 0x7f;
			if (sh < 64)
1394
				op->val = regs->gpr[rd] >> sh;
1395
			else
1396
				op->val = 0;
1397 1398 1399
			goto logical_done;

		case 794:	/* srad */
1400
			op->type = COMPUTE + SETREG + SETXER;
1401 1402
			sh = regs->gpr[rb] & 0x7f;
			ival = (signed long int) regs->gpr[rd];
1403 1404
			op->val = ival >> (sh < 64 ? sh : 63);
			op->xerval = regs->xer;
1405
			if (ival < 0 && (sh >= 64 || (ival & ((1ul << sh) - 1)) != 0))
1406
				op->xerval |= XER_CA;
1407
			else
1408
				op->xerval &= ~XER_CA;
1409 1410 1411 1412
			goto logical_done;

		case 826:	/* sradi with sh_5 = 0 */
		case 827:	/* sradi with sh_5 = 1 */
1413
			op->type = COMPUTE + SETREG + SETXER;
1414 1415
			sh = rb | ((instr & 2) << 4);
			ival = (signed long int) regs->gpr[rd];
1416 1417
			op->val = ival >> sh;
			op->xerval = regs->xer;
1418
			if (ival < 0 && (ival & ((1ul << sh) - 1)) != 0)
1419
				op->xerval |= XER_CA;
1420
			else
1421
				op->xerval &= ~XER_CA;
1422 1423 1424 1425 1426 1427 1428
			goto logical_done;
#endif /* __powerpc64__ */

/*
 * Cache instructions
 */
		case 54:	/* dcbst */
1429 1430 1431
			op->type = MKOP(CACHEOP, DCBST, 0);
			op->ea = xform_ea(instr, regs);
			return 0;
1432 1433

		case 86:	/* dcbf */
1434 1435 1436
			op->type = MKOP(CACHEOP, DCBF, 0);
			op->ea = xform_ea(instr, regs);
			return 0;
1437 1438

		case 246:	/* dcbtst */
1439 1440 1441 1442
			op->type = MKOP(CACHEOP, DCBTST, 0);
			op->ea = xform_ea(instr, regs);
			op->reg = rd;
			return 0;
1443 1444

		case 278:	/* dcbt */
1445 1446 1447 1448
			op->type = MKOP(CACHEOP, DCBTST, 0);
			op->ea = xform_ea(instr, regs);
			op->reg = rd;
			return 0;
1449 1450 1451 1452 1453

		case 982:	/* icbi */
			op->type = MKOP(CACHEOP, ICBI, 0);
			op->ea = xform_ea(instr, regs);
			return 0;
1454
		}
1455
		break;
1456
	}
1457 1458

	/*
1459
	 * Loads and stores.
1460
	 */
1461 1462 1463 1464 1465
	op->type = UNKNOWN;
	op->update_reg = ra;
	op->reg = rd;
	op->val = regs->gpr[rd];
	u = (instr >> 20) & UPDATE;
1466 1467 1468

	switch (opcode) {
	case 31:
1469 1470
		u = instr & UPDATE;
		op->ea = xform_ea(instr, regs);
1471 1472
		switch ((instr >> 1) & 0x3ff) {
		case 20:	/* lwarx */
1473 1474
			op->type = MKOP(LARX, 0, 4);
			break;
1475 1476

		case 150:	/* stwcx. */
1477 1478
			op->type = MKOP(STCX, 0, 4);
			break;
1479 1480 1481

#ifdef __powerpc64__
		case 84:	/* ldarx */
1482 1483
			op->type = MKOP(LARX, 0, 8);
			break;
1484 1485

		case 214:	/* stdcx. */
1486 1487
			op->type = MKOP(STCX, 0, 8);
			break;
1488 1489 1490

		case 21:	/* ldx */
		case 53:	/* ldux */
1491 1492
			op->type = MKOP(LOAD, u, 8);
			break;
1493 1494 1495 1496
#endif

		case 23:	/* lwzx */
		case 55:	/* lwzux */
1497 1498
			op->type = MKOP(LOAD, u, 4);
			break;
1499 1500 1501

		case 87:	/* lbzx */
		case 119:	/* lbzux */
1502 1503
			op->type = MKOP(LOAD, u, 1);
			break;
1504 1505 1506 1507

#ifdef CONFIG_ALTIVEC
		case 103:	/* lvx */
		case 359:	/* lvxl */
1508 1509
			op->type = MKOP(LOAD_VMX, 0, 16);
			break;
1510 1511 1512

		case 231:	/* stvx */
		case 487:	/* stvxl */
1513 1514
			op->type = MKOP(STORE_VMX, 0, 16);
			break;
1515 1516 1517 1518 1519
#endif /* CONFIG_ALTIVEC */

#ifdef __powerpc64__
		case 149:	/* stdx */
		case 181:	/* stdux */
1520 1521
			op->type = MKOP(STORE, u, 8);
			break;
1522 1523 1524 1525
#endif

		case 151:	/* stwx */
		case 183:	/* stwux */
1526 1527
			op->type = MKOP(STORE, u, 4);
			break;
1528 1529 1530

		case 215:	/* stbx */
		case 247:	/* stbux */
1531 1532
			op->type = MKOP(STORE, u, 1);
			break;
1533 1534 1535

		case 279:	/* lhzx */
		case 311:	/* lhzux */
1536 1537
			op->type = MKOP(LOAD, u, 2);
			break;
1538 1539 1540 1541

#ifdef __powerpc64__
		case 341:	/* lwax */
		case 373:	/* lwaux */
1542 1543
			op->type = MKOP(LOAD, SIGNEXT | u, 4);
			break;
1544 1545 1546 1547
#endif

		case 343:	/* lhax */
		case 375:	/* lhaux */
1548 1549
			op->type = MKOP(LOAD, SIGNEXT | u, 2);
			break;
1550 1551 1552

		case 407:	/* sthx */
		case 439:	/* sthux */
1553 1554
			op->type = MKOP(STORE, u, 2);
			break;
1555 1556 1557

#ifdef __powerpc64__
		case 532:	/* ldbrx */
1558 1559
			op->type = MKOP(LOAD, BYTEREV, 8);
			break;
1560 1561

#endif
1562 1563 1564
		case 533:	/* lswx */
			op->type = MKOP(LOAD_MULTI, 0, regs->xer & 0x7f);
			break;
1565 1566

		case 534:	/* lwbrx */
1567 1568
			op->type = MKOP(LOAD, BYTEREV, 4);
			break;
1569

1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
		case 597:	/* lswi */
			if (rb == 0)
				rb = 32;	/* # bytes to load */
			op->type = MKOP(LOAD_MULTI, 0, rb);
			op->ea = 0;
			if (ra)
				op->ea = truncate_if_32bit(regs->msr,
							   regs->gpr[ra]);
			break;

P
Paul Bolle 已提交
1580
#ifdef CONFIG_PPC_FPU
1581 1582
		case 535:	/* lfsx */
		case 567:	/* lfsux */
1583 1584
			op->type = MKOP(LOAD_FP, u, 4);
			break;
1585 1586 1587

		case 599:	/* lfdx */
		case 631:	/* lfdux */
1588 1589
			op->type = MKOP(LOAD_FP, u, 8);
			break;
1590 1591 1592

		case 663:	/* stfsx */
		case 695:	/* stfsux */
1593 1594
			op->type = MKOP(STORE_FP, u, 4);
			break;
1595 1596 1597

		case 727:	/* stfdx */
		case 759:	/* stfdux */
1598 1599
			op->type = MKOP(STORE_FP, u, 8);
			break;
S
Sean MacLennan 已提交
1600
#endif
1601 1602 1603

#ifdef __powerpc64__
		case 660:	/* stdbrx */
1604 1605 1606
			op->type = MKOP(STORE, BYTEREV, 8);
			op->val = byterev_8(regs->gpr[rd]);
			break;
1607 1608

#endif
1609 1610 1611 1612
		case 661:	/* stswx */
			op->type = MKOP(STORE_MULTI, 0, regs->xer & 0x7f);
			break;

1613
		case 662:	/* stwbrx */
1614 1615 1616
			op->type = MKOP(STORE, BYTEREV, 4);
			op->val = byterev_4(regs->gpr[rd]);
			break;
1617

1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
		case 725:
			if (rb == 0)
				rb = 32;	/* # bytes to store */
			op->type = MKOP(STORE_MULTI, 0, rb);
			op->ea = 0;
			if (ra)
				op->ea = truncate_if_32bit(regs->msr,
							   regs->gpr[ra]);
			break;

1628
		case 790:	/* lhbrx */
1629 1630
			op->type = MKOP(LOAD, BYTEREV, 2);
			break;
1631 1632

		case 918:	/* sthbrx */
1633 1634 1635
			op->type = MKOP(STORE, BYTEREV, 2);
			op->val = byterev_2(regs->gpr[rd]);
			break;
1636 1637 1638 1639

#ifdef CONFIG_VSX
		case 844:	/* lxvd2x */
		case 876:	/* lxvd2ux */
1640 1641 1642
			op->reg = rd | ((instr & 1) << 5);
			op->type = MKOP(LOAD_VSX, u, 16);
			break;
1643 1644 1645

		case 972:	/* stxvd2x */
		case 1004:	/* stxvd2ux */
1646 1647 1648
			op->reg = rd | ((instr & 1) << 5);
			op->type = MKOP(STORE_VSX, u, 16);
			break;
1649 1650 1651 1652 1653 1654 1655

#endif /* CONFIG_VSX */
		}
		break;

	case 32:	/* lwz */
	case 33:	/* lwzu */
1656 1657 1658
		op->type = MKOP(LOAD, u, 4);
		op->ea = dform_ea(instr, regs);
		break;
1659 1660 1661

	case 34:	/* lbz */
	case 35:	/* lbzu */
1662 1663 1664
		op->type = MKOP(LOAD, u, 1);
		op->ea = dform_ea(instr, regs);
		break;
1665 1666

	case 36:	/* stw */
1667
	case 37:	/* stwu */
1668 1669 1670
		op->type = MKOP(STORE, u, 4);
		op->ea = dform_ea(instr, regs);
		break;
1671

1672 1673
	case 38:	/* stb */
	case 39:	/* stbu */
1674 1675 1676
		op->type = MKOP(STORE, u, 1);
		op->ea = dform_ea(instr, regs);
		break;
1677 1678 1679

	case 40:	/* lhz */
	case 41:	/* lhzu */
1680 1681 1682
		op->type = MKOP(LOAD, u, 2);
		op->ea = dform_ea(instr, regs);
		break;
1683 1684 1685

	case 42:	/* lha */
	case 43:	/* lhau */
1686 1687 1688
		op->type = MKOP(LOAD, SIGNEXT | u, 2);
		op->ea = dform_ea(instr, regs);
		break;
1689 1690 1691

	case 44:	/* sth */
	case 45:	/* sthu */
1692 1693 1694
		op->type = MKOP(STORE, u, 2);
		op->ea = dform_ea(instr, regs);
		break;
1695 1696 1697 1698

	case 46:	/* lmw */
		if (ra >= rd)
			break;		/* invalid form, ra in range to load */
1699
		op->type = MKOP(LOAD_MULTI, 0, 4 * (32 - rd));
1700 1701
		op->ea = dform_ea(instr, regs);
		break;
1702 1703

	case 47:	/* stmw */
1704
		op->type = MKOP(STORE_MULTI, 0, 4 * (32 - rd));
1705 1706
		op->ea = dform_ea(instr, regs);
		break;
1707

S
Sean MacLennan 已提交
1708
#ifdef CONFIG_PPC_FPU
1709 1710
	case 48:	/* lfs */
	case 49:	/* lfsu */
1711 1712 1713
		op->type = MKOP(LOAD_FP, u, 4);
		op->ea = dform_ea(instr, regs);
		break;
1714 1715 1716

	case 50:	/* lfd */
	case 51:	/* lfdu */
1717 1718 1719
		op->type = MKOP(LOAD_FP, u, 8);
		op->ea = dform_ea(instr, regs);
		break;
1720 1721 1722

	case 52:	/* stfs */
	case 53:	/* stfsu */
1723 1724 1725
		op->type = MKOP(STORE_FP, u, 4);
		op->ea = dform_ea(instr, regs);
		break;
1726 1727 1728

	case 54:	/* stfd */
	case 55:	/* stfdu */
1729 1730 1731
		op->type = MKOP(STORE_FP, u, 8);
		op->ea = dform_ea(instr, regs);
		break;
S
Sean MacLennan 已提交
1732
#endif
1733 1734 1735

#ifdef __powerpc64__
	case 58:	/* ld[u], lwa */
1736
		op->ea = dsform_ea(instr, regs);
1737 1738
		switch (instr & 3) {
		case 0:		/* ld */
1739 1740
			op->type = MKOP(LOAD, 0, 8);
			break;
1741
		case 1:		/* ldu */
1742 1743
			op->type = MKOP(LOAD, UPDATE, 8);
			break;
1744
		case 2:		/* lwa */
1745 1746
			op->type = MKOP(LOAD, SIGNEXT, 4);
			break;
1747 1748 1749 1750
		}
		break;

	case 62:	/* std[u] */
1751
		op->ea = dsform_ea(instr, regs);
1752 1753
		switch (instr & 3) {
		case 0:		/* std */
1754 1755
			op->type = MKOP(STORE, 0, 8);
			break;
1756
		case 1:		/* stdu */
1757 1758
			op->type = MKOP(STORE, UPDATE, 8);
			break;
1759 1760 1761 1762 1763
		}
		break;
#endif /* __powerpc64__ */

	}
1764
	return 0;
1765 1766 1767

 logical_done:
	if (instr & 1)
1768 1769 1770 1771 1772
		set_cr0(regs, op, ra);
 logical_done_nocc:
	op->reg = ra;
	op->type |= SETREG;
	return 1;
1773 1774 1775

 arith_done:
	if (instr & 1)
1776 1777 1778 1779
		set_cr0(regs, op, rd);
 compute_done:
	op->reg = rd;
	op->type |= SETREG;
1780 1781 1782 1783 1784 1785 1786
	return 1;

 priv:
	op->type = INTERRUPT | 0x700;
	op->val = SRR1_PROGPRIV;
	return 0;

1787 1788 1789 1790
 trap:
	op->type = INTERRUPT | 0x700;
	op->val = SRR1_PROGTRAP;
	return 0;
1791 1792
}
EXPORT_SYMBOL_GPL(analyse_instr);
1793
NOKPROBE_SYMBOL(analyse_instr);
1794 1795 1796 1797 1798 1799 1800 1801 1802

/*
 * For PPC32 we always use stwu with r1 to change the stack pointer.
 * So this emulated store may corrupt the exception frame, now we
 * have to provide the exception frame trampoline, which is pushed
 * below the kprobed function stack. So we only update gpr[1] but
 * don't emulate the real store operation. We will do real store
 * operation safely in exception return code by checking this flag.
 */
1803
static nokprobe_inline int handle_stack_update(unsigned long ea, struct pt_regs *regs)
1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822
{
#ifdef CONFIG_PPC32
	/*
	 * Check if we will touch kernel stack overflow
	 */
	if (ea - STACK_INT_FRAME_SIZE <= current->thread.ksp_limit) {
		printk(KERN_CRIT "Can't kprobe this since kernel stack would overflow.\n");
		return -EINVAL;
	}
#endif /* CONFIG_PPC32 */
	/*
	 * Check if we already set since that means we'll
	 * lose the previous value.
	 */
	WARN_ON(test_thread_flag(TIF_EMULATE_STACK_STORE));
	set_thread_flag(TIF_EMULATE_STACK_STORE);
	return 0;
}

1823
static nokprobe_inline void do_signext(unsigned long *valp, int size)
1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
{
	switch (size) {
	case 2:
		*valp = (signed short) *valp;
		break;
	case 4:
		*valp = (signed int) *valp;
		break;
	}
}

1835
static nokprobe_inline void do_byterev(unsigned long *valp, int size)
1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851
{
	switch (size) {
	case 2:
		*valp = byterev_2(*valp);
		break;
	case 4:
		*valp = byterev_4(*valp);
		break;
#ifdef __powerpc64__
	case 8:
		*valp = byterev_8(*valp);
		break;
#endif
	}
}

1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
/*
 * Emulate an instruction that can be executed just by updating
 * fields in *regs.
 */
void emulate_update_regs(struct pt_regs *regs, struct instruction_op *op)
{
	unsigned long next_pc;

	next_pc = truncate_if_32bit(regs->msr, regs->nip + 4);
	switch (op->type & INSTR_TYPE_MASK) {
	case COMPUTE:
		if (op->type & SETREG)
			regs->gpr[op->reg] = op->val;
		if (op->type & SETCC)
			regs->ccr = op->ccval;
		if (op->type & SETXER)
			regs->xer = op->xerval;
		break;

	case BRANCH:
		if (op->type & SETLK)
			regs->link = next_pc;
		if (op->type & BRTAKEN)
			next_pc = op->val;
		if (op->type & DECCTR)
			--regs->ctr;
		break;

	case BARRIER:
		switch (op->type & BARRIER_MASK) {
		case BARRIER_SYNC:
			mb();
			break;
		case BARRIER_ISYNC:
			isync();
			break;
		case BARRIER_EIEIO:
			eieio();
			break;
		case BARRIER_LWSYNC:
			asm volatile("lwsync" : : : "memory");
			break;
		case BARRIER_PTESYNC:
			asm volatile("ptesync" : : : "memory");
			break;
		}
		break;

	case MFSPR:
		switch (op->spr) {
		case SPRN_XER:
			regs->gpr[op->reg] = regs->xer & 0xffffffffUL;
			break;
		case SPRN_LR:
			regs->gpr[op->reg] = regs->link;
			break;
		case SPRN_CTR:
			regs->gpr[op->reg] = regs->ctr;
			break;
		default:
			WARN_ON_ONCE(1);
		}
		break;

	case MTSPR:
		switch (op->spr) {
		case SPRN_XER:
			regs->xer = op->val & 0xffffffffUL;
			break;
		case SPRN_LR:
			regs->link = op->val;
			break;
		case SPRN_CTR:
			regs->ctr = op->val;
			break;
		default:
			WARN_ON_ONCE(1);
		}
		break;

	default:
		WARN_ON_ONCE(1);
	}
	regs->nip = next_pc;
}

1938 1939 1940 1941 1942 1943 1944
/*
 * Emulate instructions that cause a transfer of control,
 * loads and stores, and a few other instructions.
 * Returns 1 if the step was emulated, 0 if not,
 * or -1 if the instruction is one that should not be stepped,
 * such as an rfid, or a mtmsrd that would clear MSR_RI.
 */
1945
int emulate_step(struct pt_regs *regs, unsigned int instr)
1946 1947 1948 1949 1950
{
	struct instruction_op op;
	int r, err, size;
	unsigned long val;
	unsigned int cr;
1951
	int i, rd, nb;
1952 1953

	r = analyse_instr(&op, regs, instr);
1954
	if (r < 0)
1955
		return r;
1956 1957 1958 1959
	if (r > 0) {
		emulate_update_regs(regs, &op);
		return 1;
	}
1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981

	err = 0;
	size = GETSIZE(op.type);
	switch (op.type & INSTR_TYPE_MASK) {
	case CACHEOP:
		if (!address_ok(regs, op.ea, 8))
			return 0;
		switch (op.type & CACHEOP_MASK) {
		case DCBST:
			__cacheop_user_asmx(op.ea, err, "dcbst");
			break;
		case DCBF:
			__cacheop_user_asmx(op.ea, err, "dcbf");
			break;
		case DCBTST:
			if (op.reg == 0)
				prefetchw((void *) op.ea);
			break;
		case DCBT:
			if (op.reg == 0)
				prefetch((void *) op.ea);
			break;
1982 1983 1984
		case ICBI:
			__cacheop_user_asmx(op.ea, err, "icbi");
			break;
1985 1986 1987 1988 1989 1990 1991 1992 1993
		}
		if (err)
			return 0;
		goto instr_done;

	case LARX:
		if (op.ea & (size - 1))
			break;		/* can't handle misaligned */
		if (!address_ok(regs, op.ea, size))
1994
			return 0;
1995 1996 1997 1998 1999
		err = 0;
		switch (size) {
		case 4:
			__get_user_asmx(val, op.ea, err, "lwarx");
			break;
2000
#ifdef __powerpc64__
2001 2002 2003
		case 8:
			__get_user_asmx(val, op.ea, err, "ldarx");
			break;
2004
#endif
2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
		default:
			return 0;
		}
		if (!err)
			regs->gpr[op.reg] = val;
		goto ldst_done;

	case STCX:
		if (op.ea & (size - 1))
			break;		/* can't handle misaligned */
		if (!address_ok(regs, op.ea, size))
2016
			return 0;
2017 2018 2019 2020 2021
		err = 0;
		switch (size) {
		case 4:
			__put_user_asmx(op.val, op.ea, err, "stwcx.", cr);
			break;
2022
#ifdef __powerpc64__
2023 2024 2025
		case 8:
			__put_user_asmx(op.val, op.ea, err, "stdcx.", cr);
			break;
2026
#endif
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045
		default:
			return 0;
		}
		if (!err)
			regs->ccr = (regs->ccr & 0x0fffffff) |
				(cr & 0xe0000000) |
				((regs->xer >> 3) & 0x10000000);
		goto ldst_done;

	case LOAD:
		err = read_mem(&regs->gpr[op.reg], op.ea, size, regs);
		if (!err) {
			if (op.type & SIGNEXT)
				do_signext(&regs->gpr[op.reg], size);
			if (op.type & BYTEREV)
				do_byterev(&regs->gpr[op.reg], size);
		}
		goto ldst_done;

2046
#ifdef CONFIG_PPC_FPU
2047
	case LOAD_FP:
2048 2049
		if (!(regs->msr & MSR_FP))
			return 0;
2050 2051 2052 2053 2054
		if (size == 4)
			err = do_fp_load(op.reg, do_lfs, op.ea, size, regs);
		else
			err = do_fp_load(op.reg, do_lfd, op.ea, size, regs);
		goto ldst_done;
2055
#endif
2056 2057
#ifdef CONFIG_ALTIVEC
	case LOAD_VMX:
2058 2059
		if (!(regs->msr & MSR_VEC))
			return 0;
2060 2061 2062 2063 2064
		err = do_vec_load(op.reg, do_lvx, op.ea & ~0xfUL, regs);
		goto ldst_done;
#endif
#ifdef CONFIG_VSX
	case LOAD_VSX:
2065 2066
		if (!(regs->msr & MSR_VSX))
			return 0;
2067 2068 2069 2070 2071 2072 2073
		err = do_vsx_load(op.reg, do_lxvd2x, op.ea, regs);
		goto ldst_done;
#endif
	case LOAD_MULTI:
		if (regs->msr & MSR_LE)
			return 0;
		rd = op.reg;
2074 2075 2076 2077 2078
		for (i = 0; i < size; i += 4) {
			nb = size - i;
			if (nb > 4)
				nb = 4;
			err = read_mem(&regs->gpr[rd], op.ea, nb, regs);
2079 2080
			if (err)
				return 0;
2081 2082
			if (nb < 4)	/* left-justify last bytes */
				regs->gpr[rd] <<= 32 - 8 * nb;
2083
			op.ea += 4;
2084 2085
			++rd;
		}
2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098
		goto instr_done;

	case STORE:
		if ((op.type & UPDATE) && size == sizeof(long) &&
		    op.reg == 1 && op.update_reg == 1 &&
		    !(regs->msr & MSR_PR) &&
		    op.ea >= regs->gpr[1] - STACK_INT_FRAME_SIZE) {
			err = handle_stack_update(op.ea, regs);
			goto ldst_done;
		}
		err = write_mem(op.val, op.ea, size, regs);
		goto ldst_done;

2099
#ifdef CONFIG_PPC_FPU
2100
	case STORE_FP:
2101 2102
		if (!(regs->msr & MSR_FP))
			return 0;
2103 2104 2105 2106 2107
		if (size == 4)
			err = do_fp_store(op.reg, do_stfs, op.ea, size, regs);
		else
			err = do_fp_store(op.reg, do_stfd, op.ea, size, regs);
		goto ldst_done;
2108
#endif
2109 2110
#ifdef CONFIG_ALTIVEC
	case STORE_VMX:
2111 2112
		if (!(regs->msr & MSR_VEC))
			return 0;
2113 2114 2115 2116 2117
		err = do_vec_store(op.reg, do_stvx, op.ea & ~0xfUL, regs);
		goto ldst_done;
#endif
#ifdef CONFIG_VSX
	case STORE_VSX:
2118 2119
		if (!(regs->msr & MSR_VSX))
			return 0;
2120 2121 2122 2123 2124 2125 2126
		err = do_vsx_store(op.reg, do_stxvd2x, op.ea, regs);
		goto ldst_done;
#endif
	case STORE_MULTI:
		if (regs->msr & MSR_LE)
			return 0;
		rd = op.reg;
2127 2128 2129 2130 2131 2132 2133 2134
		for (i = 0; i < size; i += 4) {
			val = regs->gpr[rd];
			nb = size - i;
			if (nb > 4)
				nb = 4;
			else
				val >>= 32 - 8 * nb;
			err = write_mem(val, op.ea, nb, regs);
2135 2136 2137
			if (err)
				return 0;
			op.ea += 4;
2138 2139
			++rd;
		}
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190
		goto instr_done;

	case MFMSR:
		regs->gpr[op.reg] = regs->msr & MSR_MASK;
		goto instr_done;

	case MTMSR:
		val = regs->gpr[op.reg];
		if ((val & MSR_RI) == 0)
			/* can't step mtmsr[d] that would clear MSR_RI */
			return -1;
		/* here op.val is the mask of bits to change */
		regs->msr = (regs->msr & ~op.val) | (val & op.val);
		goto instr_done;

#ifdef CONFIG_PPC64
	case SYSCALL:	/* sc */
		/*
		 * N.B. this uses knowledge about how the syscall
		 * entry code works.  If that is changed, this will
		 * need to be changed also.
		 */
		if (regs->gpr[0] == 0x1ebe &&
		    cpu_has_feature(CPU_FTR_REAL_LE)) {
			regs->msr ^= MSR_LE;
			goto instr_done;
		}
		regs->gpr[9] = regs->gpr[13];
		regs->gpr[10] = MSR_KERNEL;
		regs->gpr[11] = regs->nip + 4;
		regs->gpr[12] = regs->msr & MSR_MASK;
		regs->gpr[13] = (unsigned long) get_paca();
		regs->nip = (unsigned long) &system_call_common;
		regs->msr = MSR_KERNEL;
		return 1;

	case RFI:
		return -1;
#endif
	}
	return 0;

 ldst_done:
	if (err)
		return 0;
	if (op.type & UPDATE)
		regs->gpr[op.update_reg] = op.ea;

 instr_done:
	regs->nip = truncate_if_32bit(regs->msr, regs->nip + 4);
	return 1;
2191
}
2192
NOKPROBE_SYMBOL(emulate_step);