blob: 6ca3b902f7b9b21aefeda661d9586d892e7a5e16 [file] [log] [blame]
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001/*
2 * Single-step support.
3 *
4 * Copyright (C) 2004 Paul Mackerras <paulus@au.ibm.com>, IBM
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11#include <linux/kernel.h>
Gui,Jian0d69a052006-11-01 10:50:15 +080012#include <linux/kprobes.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100013#include <linux/ptrace.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070014#include <linux/prefetch.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100015#include <asm/sstep.h>
16#include <asm/processor.h>
Paul Mackerras0016a4c2010-06-15 14:48:58 +100017#include <asm/uaccess.h>
18#include <asm/cputable.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100019
20extern char system_call_common[];
21
Paul Mackerrasc0325242005-10-28 22:48:08 +100022#ifdef CONFIG_PPC64
Paul Mackerras14cf11a2005-09-26 16:04:21 +100023/* Bits in SRR1 that are copied from MSR */
Stephen Rothwellaf308372006-03-23 17:38:10 +110024#define MSR_MASK 0xffffffff87c0ffffUL
Paul Mackerrasc0325242005-10-28 22:48:08 +100025#else
26#define MSR_MASK 0x87c0ffff
27#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +100028
Paul Mackerras0016a4c2010-06-15 14:48:58 +100029/* Bits in XER */
30#define XER_SO 0x80000000U
31#define XER_OV 0x40000000U
32#define XER_CA 0x20000000U
33
Sean MacLennancd64d162010-09-01 07:21:21 +000034#ifdef CONFIG_PPC_FPU
Paul Mackerras0016a4c2010-06-15 14:48:58 +100035/*
36 * Functions in ldstfp.S
37 */
38extern int do_lfs(int rn, unsigned long ea);
39extern int do_lfd(int rn, unsigned long ea);
40extern int do_stfs(int rn, unsigned long ea);
41extern int do_stfd(int rn, unsigned long ea);
42extern int do_lvx(int rn, unsigned long ea);
43extern int do_stvx(int rn, unsigned long ea);
44extern int do_lxvd2x(int rn, unsigned long ea);
45extern int do_stxvd2x(int rn, unsigned long ea);
Sean MacLennancd64d162010-09-01 07:21:21 +000046#endif
Paul Mackerras0016a4c2010-06-15 14:48:58 +100047
Paul Mackerras14cf11a2005-09-26 16:04:21 +100048/*
Michael Ellermanb91e1362011-04-07 21:56:04 +000049 * Emulate the truncation of 64 bit values in 32-bit mode.
50 */
51static unsigned long truncate_if_32bit(unsigned long msr, unsigned long val)
52{
53#ifdef __powerpc64__
54 if ((msr & MSR_64BIT) == 0)
55 val &= 0xffffffffUL;
56#endif
57 return val;
58}
59
60/*
Paul Mackerras14cf11a2005-09-26 16:04:21 +100061 * Determine whether a conditional branch instruction would branch.
62 */
Gui,Jian0d69a052006-11-01 10:50:15 +080063static int __kprobes branch_taken(unsigned int instr, struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100064{
65 unsigned int bo = (instr >> 21) & 0x1f;
66 unsigned int bi;
67
68 if ((bo & 4) == 0) {
69 /* decrement counter */
70 --regs->ctr;
71 if (((bo >> 1) & 1) ^ (regs->ctr == 0))
72 return 0;
73 }
74 if ((bo & 0x10) == 0) {
75 /* check bit from CR */
76 bi = (instr >> 16) & 0x1f;
77 if (((regs->ccr >> (31 - bi)) & 1) != ((bo >> 3) & 1))
78 return 0;
79 }
80 return 1;
81}
82
Paul Mackerras0016a4c2010-06-15 14:48:58 +100083
84static long __kprobes address_ok(struct pt_regs *regs, unsigned long ea, int nb)
85{
86 if (!user_mode(regs))
87 return 1;
88 return __access_ok(ea, nb, USER_DS);
89}
90
Paul Mackerras14cf11a2005-09-26 16:04:21 +100091/*
Paul Mackerras0016a4c2010-06-15 14:48:58 +100092 * Calculate effective address for a D-form instruction
93 */
94static unsigned long __kprobes dform_ea(unsigned int instr, struct pt_regs *regs)
95{
96 int ra;
97 unsigned long ea;
98
99 ra = (instr >> 16) & 0x1f;
100 ea = (signed short) instr; /* sign-extend */
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000101 if (ra)
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000102 ea += regs->gpr[ra];
Michael Ellermanb91e1362011-04-07 21:56:04 +0000103
104 return truncate_if_32bit(regs->msr, ea);
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000105}
106
107#ifdef __powerpc64__
108/*
109 * Calculate effective address for a DS-form instruction
110 */
111static unsigned long __kprobes dsform_ea(unsigned int instr, struct pt_regs *regs)
112{
113 int ra;
114 unsigned long ea;
115
116 ra = (instr >> 16) & 0x1f;
117 ea = (signed short) (instr & ~3); /* sign-extend */
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000118 if (ra)
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000119 ea += regs->gpr[ra];
Michael Ellermanb91e1362011-04-07 21:56:04 +0000120
121 return truncate_if_32bit(regs->msr, ea);
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000122}
123#endif /* __powerpc64 */
124
125/*
126 * Calculate effective address for an X-form instruction
127 */
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000128static unsigned long __kprobes xform_ea(unsigned int instr,
129 struct pt_regs *regs)
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000130{
131 int ra, rb;
132 unsigned long ea;
133
134 ra = (instr >> 16) & 0x1f;
135 rb = (instr >> 11) & 0x1f;
136 ea = regs->gpr[rb];
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000137 if (ra)
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000138 ea += regs->gpr[ra];
Michael Ellermanb91e1362011-04-07 21:56:04 +0000139
140 return truncate_if_32bit(regs->msr, ea);
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000141}
142
143/*
144 * Return the largest power of 2, not greater than sizeof(unsigned long),
145 * such that x is a multiple of it.
146 */
147static inline unsigned long max_align(unsigned long x)
148{
149 x |= sizeof(unsigned long);
150 return x & -x; /* isolates rightmost bit */
151}
152
153
154static inline unsigned long byterev_2(unsigned long x)
155{
156 return ((x >> 8) & 0xff) | ((x & 0xff) << 8);
157}
158
159static inline unsigned long byterev_4(unsigned long x)
160{
161 return ((x >> 24) & 0xff) | ((x >> 8) & 0xff00) |
162 ((x & 0xff00) << 8) | ((x & 0xff) << 24);
163}
164
165#ifdef __powerpc64__
166static inline unsigned long byterev_8(unsigned long x)
167{
168 return (byterev_4(x) << 32) | byterev_4(x >> 32);
169}
170#endif
171
172static int __kprobes read_mem_aligned(unsigned long *dest, unsigned long ea,
173 int nb)
174{
175 int err = 0;
176 unsigned long x = 0;
177
178 switch (nb) {
179 case 1:
180 err = __get_user(x, (unsigned char __user *) ea);
181 break;
182 case 2:
183 err = __get_user(x, (unsigned short __user *) ea);
184 break;
185 case 4:
186 err = __get_user(x, (unsigned int __user *) ea);
187 break;
188#ifdef __powerpc64__
189 case 8:
190 err = __get_user(x, (unsigned long __user *) ea);
191 break;
192#endif
193 }
194 if (!err)
195 *dest = x;
196 return err;
197}
198
199static int __kprobes read_mem_unaligned(unsigned long *dest, unsigned long ea,
200 int nb, struct pt_regs *regs)
201{
202 int err;
203 unsigned long x, b, c;
Tom Musta6506b472013-10-18 14:42:08 -0500204#ifdef __LITTLE_ENDIAN__
205 int len = nb; /* save a copy of the length for byte reversal */
206#endif
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000207
208 /* unaligned, do this in pieces */
209 x = 0;
210 for (; nb > 0; nb -= c) {
Tom Musta6506b472013-10-18 14:42:08 -0500211#ifdef __LITTLE_ENDIAN__
212 c = 1;
213#endif
214#ifdef __BIG_ENDIAN__
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000215 c = max_align(ea);
Tom Musta6506b472013-10-18 14:42:08 -0500216#endif
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000217 if (c > nb)
218 c = max_align(nb);
219 err = read_mem_aligned(&b, ea, c);
220 if (err)
221 return err;
222 x = (x << (8 * c)) + b;
223 ea += c;
224 }
Tom Musta6506b472013-10-18 14:42:08 -0500225#ifdef __LITTLE_ENDIAN__
226 switch (len) {
227 case 2:
228 *dest = byterev_2(x);
229 break;
230 case 4:
231 *dest = byterev_4(x);
232 break;
233#ifdef __powerpc64__
234 case 8:
235 *dest = byterev_8(x);
236 break;
237#endif
238 }
239#endif
240#ifdef __BIG_ENDIAN__
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000241 *dest = x;
Tom Musta6506b472013-10-18 14:42:08 -0500242#endif
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000243 return 0;
244}
245
246/*
247 * Read memory at address ea for nb bytes, return 0 for success
248 * or -EFAULT if an error occurred.
249 */
250static int __kprobes read_mem(unsigned long *dest, unsigned long ea, int nb,
251 struct pt_regs *regs)
252{
253 if (!address_ok(regs, ea, nb))
254 return -EFAULT;
255 if ((ea & (nb - 1)) == 0)
256 return read_mem_aligned(dest, ea, nb);
257 return read_mem_unaligned(dest, ea, nb, regs);
258}
259
260static int __kprobes write_mem_aligned(unsigned long val, unsigned long ea,
261 int nb)
262{
263 int err = 0;
264
265 switch (nb) {
266 case 1:
267 err = __put_user(val, (unsigned char __user *) ea);
268 break;
269 case 2:
270 err = __put_user(val, (unsigned short __user *) ea);
271 break;
272 case 4:
273 err = __put_user(val, (unsigned int __user *) ea);
274 break;
275#ifdef __powerpc64__
276 case 8:
277 err = __put_user(val, (unsigned long __user *) ea);
278 break;
279#endif
280 }
281 return err;
282}
283
284static int __kprobes write_mem_unaligned(unsigned long val, unsigned long ea,
285 int nb, struct pt_regs *regs)
286{
287 int err;
288 unsigned long c;
289
Tom Musta6506b472013-10-18 14:42:08 -0500290#ifdef __LITTLE_ENDIAN__
291 switch (nb) {
292 case 2:
293 val = byterev_2(val);
294 break;
295 case 4:
296 val = byterev_4(val);
297 break;
298#ifdef __powerpc64__
299 case 8:
300 val = byterev_8(val);
301 break;
302#endif
303 }
304#endif
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000305 /* unaligned or little-endian, do this in pieces */
306 for (; nb > 0; nb -= c) {
Tom Musta6506b472013-10-18 14:42:08 -0500307#ifdef __LITTLE_ENDIAN__
308 c = 1;
309#endif
310#ifdef __BIG_ENDIAN__
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000311 c = max_align(ea);
Tom Musta6506b472013-10-18 14:42:08 -0500312#endif
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000313 if (c > nb)
314 c = max_align(nb);
315 err = write_mem_aligned(val >> (nb - c) * 8, ea, c);
316 if (err)
317 return err;
Tom Musta17e8de72013-08-22 09:25:28 -0500318 ea += c;
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000319 }
320 return 0;
321}
322
323/*
324 * Write memory at address ea for nb bytes, return 0 for success
325 * or -EFAULT if an error occurred.
326 */
327static int __kprobes write_mem(unsigned long val, unsigned long ea, int nb,
328 struct pt_regs *regs)
329{
330 if (!address_ok(regs, ea, nb))
331 return -EFAULT;
332 if ((ea & (nb - 1)) == 0)
333 return write_mem_aligned(val, ea, nb);
334 return write_mem_unaligned(val, ea, nb, regs);
335}
336
Sean MacLennancd64d162010-09-01 07:21:21 +0000337#ifdef CONFIG_PPC_FPU
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000338/*
339 * Check the address and alignment, and call func to do the actual
340 * load or store.
341 */
342static int __kprobes do_fp_load(int rn, int (*func)(int, unsigned long),
343 unsigned long ea, int nb,
344 struct pt_regs *regs)
345{
346 int err;
Tom Mustadbc2fbd2013-10-18 14:44:17 -0500347 union {
348 double dbl;
349 unsigned long ul[2];
350 struct {
351#ifdef __BIG_ENDIAN__
352 unsigned _pad_;
353 unsigned word;
354#endif
355#ifdef __LITTLE_ENDIAN__
356 unsigned word;
357 unsigned _pad_;
358#endif
359 } single;
360 } data;
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000361 unsigned long ptr;
362
363 if (!address_ok(regs, ea, nb))
364 return -EFAULT;
365 if ((ea & 3) == 0)
366 return (*func)(rn, ea);
Tom Mustadbc2fbd2013-10-18 14:44:17 -0500367 ptr = (unsigned long) &data.ul;
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000368 if (sizeof(unsigned long) == 8 || nb == 4) {
Tom Mustadbc2fbd2013-10-18 14:44:17 -0500369 err = read_mem_unaligned(&data.ul[0], ea, nb, regs);
370 if (nb == 4)
371 ptr = (unsigned long)&(data.single.word);
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000372 } else {
373 /* reading a double on 32-bit */
Tom Mustadbc2fbd2013-10-18 14:44:17 -0500374 err = read_mem_unaligned(&data.ul[0], ea, 4, regs);
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000375 if (!err)
Tom Mustadbc2fbd2013-10-18 14:44:17 -0500376 err = read_mem_unaligned(&data.ul[1], ea + 4, 4, regs);
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000377 }
378 if (err)
379 return err;
380 return (*func)(rn, ptr);
381}
382
383static int __kprobes do_fp_store(int rn, int (*func)(int, unsigned long),
384 unsigned long ea, int nb,
385 struct pt_regs *regs)
386{
387 int err;
Tom Mustadbc2fbd2013-10-18 14:44:17 -0500388 union {
389 double dbl;
390 unsigned long ul[2];
391 struct {
392#ifdef __BIG_ENDIAN__
393 unsigned _pad_;
394 unsigned word;
395#endif
396#ifdef __LITTLE_ENDIAN__
397 unsigned word;
398 unsigned _pad_;
399#endif
400 } single;
401 } data;
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000402 unsigned long ptr;
403
404 if (!address_ok(regs, ea, nb))
405 return -EFAULT;
406 if ((ea & 3) == 0)
407 return (*func)(rn, ea);
Tom Mustadbc2fbd2013-10-18 14:44:17 -0500408 ptr = (unsigned long) &data.ul[0];
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000409 if (sizeof(unsigned long) == 8 || nb == 4) {
Tom Mustadbc2fbd2013-10-18 14:44:17 -0500410 if (nb == 4)
411 ptr = (unsigned long)&(data.single.word);
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000412 err = (*func)(rn, ptr);
413 if (err)
414 return err;
Tom Mustadbc2fbd2013-10-18 14:44:17 -0500415 err = write_mem_unaligned(data.ul[0], ea, nb, regs);
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000416 } else {
417 /* writing a double on 32-bit */
418 err = (*func)(rn, ptr);
419 if (err)
420 return err;
Tom Mustadbc2fbd2013-10-18 14:44:17 -0500421 err = write_mem_unaligned(data.ul[0], ea, 4, regs);
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000422 if (!err)
Tom Mustadbc2fbd2013-10-18 14:44:17 -0500423 err = write_mem_unaligned(data.ul[1], ea + 4, 4, regs);
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000424 }
425 return err;
426}
Sean MacLennancd64d162010-09-01 07:21:21 +0000427#endif
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000428
429#ifdef CONFIG_ALTIVEC
430/* For Altivec/VMX, no need to worry about alignment */
431static int __kprobes do_vec_load(int rn, int (*func)(int, unsigned long),
432 unsigned long ea, struct pt_regs *regs)
433{
434 if (!address_ok(regs, ea & ~0xfUL, 16))
435 return -EFAULT;
436 return (*func)(rn, ea);
437}
438
439static int __kprobes do_vec_store(int rn, int (*func)(int, unsigned long),
440 unsigned long ea, struct pt_regs *regs)
441{
442 if (!address_ok(regs, ea & ~0xfUL, 16))
443 return -EFAULT;
444 return (*func)(rn, ea);
445}
446#endif /* CONFIG_ALTIVEC */
447
448#ifdef CONFIG_VSX
449static int __kprobes do_vsx_load(int rn, int (*func)(int, unsigned long),
450 unsigned long ea, struct pt_regs *regs)
451{
452 int err;
453 unsigned long val[2];
454
455 if (!address_ok(regs, ea, 16))
456 return -EFAULT;
457 if ((ea & 3) == 0)
458 return (*func)(rn, ea);
459 err = read_mem_unaligned(&val[0], ea, 8, regs);
460 if (!err)
461 err = read_mem_unaligned(&val[1], ea + 8, 8, regs);
462 if (!err)
463 err = (*func)(rn, (unsigned long) &val[0]);
464 return err;
465}
466
467static int __kprobes do_vsx_store(int rn, int (*func)(int, unsigned long),
468 unsigned long ea, struct pt_regs *regs)
469{
470 int err;
471 unsigned long val[2];
472
473 if (!address_ok(regs, ea, 16))
474 return -EFAULT;
475 if ((ea & 3) == 0)
476 return (*func)(rn, ea);
477 err = (*func)(rn, (unsigned long) &val[0]);
478 if (err)
479 return err;
480 err = write_mem_unaligned(val[0], ea, 8, regs);
481 if (!err)
482 err = write_mem_unaligned(val[1], ea + 8, 8, regs);
483 return err;
484}
485#endif /* CONFIG_VSX */
486
487#define __put_user_asmx(x, addr, err, op, cr) \
488 __asm__ __volatile__( \
489 "1: " op " %2,0,%3\n" \
490 " mfcr %1\n" \
491 "2:\n" \
492 ".section .fixup,\"ax\"\n" \
493 "3: li %0,%4\n" \
494 " b 2b\n" \
495 ".previous\n" \
496 ".section __ex_table,\"a\"\n" \
497 PPC_LONG_ALIGN "\n" \
498 PPC_LONG "1b,3b\n" \
499 ".previous" \
500 : "=r" (err), "=r" (cr) \
501 : "r" (x), "r" (addr), "i" (-EFAULT), "0" (err))
502
503#define __get_user_asmx(x, addr, err, op) \
504 __asm__ __volatile__( \
505 "1: "op" %1,0,%2\n" \
506 "2:\n" \
507 ".section .fixup,\"ax\"\n" \
508 "3: li %0,%3\n" \
509 " b 2b\n" \
510 ".previous\n" \
511 ".section __ex_table,\"a\"\n" \
512 PPC_LONG_ALIGN "\n" \
513 PPC_LONG "1b,3b\n" \
514 ".previous" \
515 : "=r" (err), "=r" (x) \
516 : "r" (addr), "i" (-EFAULT), "0" (err))
517
518#define __cacheop_user_asmx(addr, err, op) \
519 __asm__ __volatile__( \
520 "1: "op" 0,%1\n" \
521 "2:\n" \
522 ".section .fixup,\"ax\"\n" \
523 "3: li %0,%3\n" \
524 " b 2b\n" \
525 ".previous\n" \
526 ".section __ex_table,\"a\"\n" \
527 PPC_LONG_ALIGN "\n" \
528 PPC_LONG "1b,3b\n" \
529 ".previous" \
530 : "=r" (err) \
531 : "r" (addr), "i" (-EFAULT), "0" (err))
532
533static void __kprobes set_cr0(struct pt_regs *regs, int rd)
534{
535 long val = regs->gpr[rd];
536
537 regs->ccr = (regs->ccr & 0x0fffffff) | ((regs->xer >> 3) & 0x10000000);
538#ifdef __powerpc64__
Michael Ellermanb91e1362011-04-07 21:56:04 +0000539 if (!(regs->msr & MSR_64BIT))
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000540 val = (int) val;
541#endif
542 if (val < 0)
543 regs->ccr |= 0x80000000;
544 else if (val > 0)
545 regs->ccr |= 0x40000000;
546 else
547 regs->ccr |= 0x20000000;
548}
549
550static void __kprobes add_with_carry(struct pt_regs *regs, int rd,
551 unsigned long val1, unsigned long val2,
552 unsigned long carry_in)
553{
554 unsigned long val = val1 + val2;
555
556 if (carry_in)
557 ++val;
558 regs->gpr[rd] = val;
559#ifdef __powerpc64__
Michael Ellermanb91e1362011-04-07 21:56:04 +0000560 if (!(regs->msr & MSR_64BIT)) {
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000561 val = (unsigned int) val;
562 val1 = (unsigned int) val1;
563 }
564#endif
565 if (val < val1 || (carry_in && val == val1))
566 regs->xer |= XER_CA;
567 else
568 regs->xer &= ~XER_CA;
569}
570
571static void __kprobes do_cmp_signed(struct pt_regs *regs, long v1, long v2,
572 int crfld)
573{
574 unsigned int crval, shift;
575
576 crval = (regs->xer >> 31) & 1; /* get SO bit */
577 if (v1 < v2)
578 crval |= 8;
579 else if (v1 > v2)
580 crval |= 4;
581 else
582 crval |= 2;
583 shift = (7 - crfld) * 4;
584 regs->ccr = (regs->ccr & ~(0xf << shift)) | (crval << shift);
585}
586
587static void __kprobes do_cmp_unsigned(struct pt_regs *regs, unsigned long v1,
588 unsigned long v2, int crfld)
589{
590 unsigned int crval, shift;
591
592 crval = (regs->xer >> 31) & 1; /* get SO bit */
593 if (v1 < v2)
594 crval |= 8;
595 else if (v1 > v2)
596 crval |= 4;
597 else
598 crval |= 2;
599 shift = (7 - crfld) * 4;
600 regs->ccr = (regs->ccr & ~(0xf << shift)) | (crval << shift);
601}
602
Paul Mackerrascf87c3f2014-09-02 14:35:08 +1000603static int __kprobes trap_compare(long v1, long v2)
604{
605 int ret = 0;
606
607 if (v1 < v2)
608 ret |= 0x10;
609 else if (v1 > v2)
610 ret |= 0x08;
611 else
612 ret |= 0x04;
613 if ((unsigned long)v1 < (unsigned long)v2)
614 ret |= 0x02;
615 else if ((unsigned long)v1 > (unsigned long)v2)
616 ret |= 0x01;
617 return ret;
618}
619
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000620/*
621 * Elements of 32-bit rotate and mask instructions.
622 */
623#define MASK32(mb, me) ((0xffffffffUL >> (mb)) + \
624 ((signed long)-0x80000000L >> (me)) + ((me) >= (mb)))
625#ifdef __powerpc64__
626#define MASK64_L(mb) (~0UL >> (mb))
627#define MASK64_R(me) ((signed long)-0x8000000000000000L >> (me))
628#define MASK64(mb, me) (MASK64_L(mb) + MASK64_R(me) + ((me) >= (mb)))
629#define DATA32(x) (((x) & 0xffffffffUL) | (((x) & 0xffffffffUL) << 32))
630#else
631#define DATA32(x) (x)
632#endif
633#define ROTATE(x, n) ((n) ? (((x) << (n)) | ((x) >> (8 * sizeof(long) - (n)))) : (x))
634
635/*
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000636 * Decode an instruction, and execute it if that can be done just by
637 * modifying *regs (i.e. integer arithmetic and logical instructions,
638 * branches, and barrier instructions).
639 * Returns 1 if the instruction has been executed, or 0 if not.
640 * Sets *op to indicate what the instruction does.
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000641 */
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000642int __kprobes analyse_instr(struct instruction_op *op, struct pt_regs *regs,
643 unsigned int instr)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000644{
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000645 unsigned int opcode, ra, rb, rd, spr, u;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000646 unsigned long int imm;
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000647 unsigned long int val, val2;
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000648 unsigned int mb, me, sh;
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000649 long ival;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000650
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000651 op->type = COMPUTE;
652
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000653 opcode = instr >> 26;
654 switch (opcode) {
655 case 16: /* bc */
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000656 op->type = BRANCH;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000657 imm = (signed short)(instr & 0xfffc);
658 if ((instr & 2) == 0)
659 imm += regs->nip;
660 regs->nip += 4;
Michael Ellermanb91e1362011-04-07 21:56:04 +0000661 regs->nip = truncate_if_32bit(regs->msr, regs->nip);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000662 if (instr & 1)
663 regs->link = regs->nip;
664 if (branch_taken(instr, regs))
Michael Neuling70a54a42013-05-06 21:32:40 +1000665 regs->nip = truncate_if_32bit(regs->msr, imm);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000666 return 1;
Paul Mackerrasc0325242005-10-28 22:48:08 +1000667#ifdef CONFIG_PPC64
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000668 case 17: /* sc */
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000669 if ((instr & 0xfe2) == 2)
670 op->type = SYSCALL;
671 else
672 op->type = UNKNOWN;
673 return 0;
Paul Mackerrasc0325242005-10-28 22:48:08 +1000674#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000675 case 18: /* b */
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000676 op->type = BRANCH;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000677 imm = instr & 0x03fffffc;
678 if (imm & 0x02000000)
679 imm -= 0x04000000;
680 if ((instr & 2) == 0)
681 imm += regs->nip;
Michael Ellermanb91e1362011-04-07 21:56:04 +0000682 if (instr & 1)
683 regs->link = truncate_if_32bit(regs->msr, regs->nip + 4);
684 imm = truncate_if_32bit(regs->msr, imm);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000685 regs->nip = imm;
686 return 1;
687 case 19:
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000688 switch ((instr >> 1) & 0x3ff) {
Paul Mackerrascf87c3f2014-09-02 14:35:08 +1000689 case 0: /* mcrf */
690 rd = (instr >> 21) & 0x1c;
691 ra = (instr >> 16) & 0x1c;
692 val = (regs->ccr >> ra) & 0xf;
693 regs->ccr = (regs->ccr & ~(0xfUL << rd)) | (val << rd);
694 goto instr_done;
695
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000696 case 16: /* bclr */
697 case 528: /* bcctr */
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000698 op->type = BRANCH;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000699 imm = (instr & 0x400)? regs->ctr: regs->link;
Michael Ellermanb91e1362011-04-07 21:56:04 +0000700 regs->nip = truncate_if_32bit(regs->msr, regs->nip + 4);
701 imm = truncate_if_32bit(regs->msr, imm);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000702 if (instr & 1)
703 regs->link = regs->nip;
704 if (branch_taken(instr, regs))
705 regs->nip = imm;
706 return 1;
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000707
708 case 18: /* rfid, scary */
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000709 if (regs->msr & MSR_PR)
710 goto priv;
711 op->type = RFI;
712 return 0;
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000713
714 case 150: /* isync */
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000715 op->type = BARRIER;
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000716 isync();
717 goto instr_done;
718
719 case 33: /* crnor */
720 case 129: /* crandc */
721 case 193: /* crxor */
722 case 225: /* crnand */
723 case 257: /* crand */
724 case 289: /* creqv */
725 case 417: /* crorc */
726 case 449: /* cror */
727 ra = (instr >> 16) & 0x1f;
728 rb = (instr >> 11) & 0x1f;
729 rd = (instr >> 21) & 0x1f;
730 ra = (regs->ccr >> (31 - ra)) & 1;
731 rb = (regs->ccr >> (31 - rb)) & 1;
732 val = (instr >> (6 + ra * 2 + rb)) & 1;
733 regs->ccr = (regs->ccr & ~(1UL << (31 - rd))) |
734 (val << (31 - rd));
735 goto instr_done;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000736 }
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000737 break;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000738 case 31:
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000739 switch ((instr >> 1) & 0x3ff) {
740 case 598: /* sync */
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000741 op->type = BARRIER;
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000742#ifdef __powerpc64__
743 switch ((instr >> 21) & 3) {
744 case 1: /* lwsync */
745 asm volatile("lwsync" : : : "memory");
746 goto instr_done;
747 case 2: /* ptesync */
748 asm volatile("ptesync" : : : "memory");
749 goto instr_done;
750 }
751#endif
752 mb();
753 goto instr_done;
754
755 case 854: /* eieio */
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000756 op->type = BARRIER;
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000757 eieio();
758 goto instr_done;
759 }
760 break;
761 }
762
763 /* Following cases refer to regs->gpr[], so we need all regs */
764 if (!FULL_REGS(regs))
765 return 0;
766
767 rd = (instr >> 21) & 0x1f;
768 ra = (instr >> 16) & 0x1f;
769 rb = (instr >> 11) & 0x1f;
770
771 switch (opcode) {
Paul Mackerrascf87c3f2014-09-02 14:35:08 +1000772#ifdef __powerpc64__
773 case 2: /* tdi */
774 if (rd & trap_compare(regs->gpr[ra], (short) instr))
775 goto trap;
776 goto instr_done;
777#endif
778 case 3: /* twi */
779 if (rd & trap_compare((int)regs->gpr[ra], (short) instr))
780 goto trap;
781 goto instr_done;
782
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000783 case 7: /* mulli */
784 regs->gpr[rd] = regs->gpr[ra] * (short) instr;
785 goto instr_done;
786
787 case 8: /* subfic */
788 imm = (short) instr;
789 add_with_carry(regs, rd, ~regs->gpr[ra], imm, 1);
790 goto instr_done;
791
792 case 10: /* cmpli */
793 imm = (unsigned short) instr;
794 val = regs->gpr[ra];
795#ifdef __powerpc64__
796 if ((rd & 1) == 0)
797 val = (unsigned int) val;
798#endif
799 do_cmp_unsigned(regs, val, imm, rd >> 2);
800 goto instr_done;
801
802 case 11: /* cmpi */
803 imm = (short) instr;
804 val = regs->gpr[ra];
805#ifdef __powerpc64__
806 if ((rd & 1) == 0)
807 val = (int) val;
808#endif
809 do_cmp_signed(regs, val, imm, rd >> 2);
810 goto instr_done;
811
812 case 12: /* addic */
813 imm = (short) instr;
814 add_with_carry(regs, rd, regs->gpr[ra], imm, 0);
815 goto instr_done;
816
817 case 13: /* addic. */
818 imm = (short) instr;
819 add_with_carry(regs, rd, regs->gpr[ra], imm, 0);
820 set_cr0(regs, rd);
821 goto instr_done;
822
823 case 14: /* addi */
824 imm = (short) instr;
825 if (ra)
826 imm += regs->gpr[ra];
827 regs->gpr[rd] = imm;
828 goto instr_done;
829
830 case 15: /* addis */
831 imm = ((short) instr) << 16;
832 if (ra)
833 imm += regs->gpr[ra];
834 regs->gpr[rd] = imm;
835 goto instr_done;
836
837 case 20: /* rlwimi */
838 mb = (instr >> 6) & 0x1f;
839 me = (instr >> 1) & 0x1f;
840 val = DATA32(regs->gpr[rd]);
841 imm = MASK32(mb, me);
842 regs->gpr[ra] = (regs->gpr[ra] & ~imm) | (ROTATE(val, rb) & imm);
843 goto logical_done;
844
845 case 21: /* rlwinm */
846 mb = (instr >> 6) & 0x1f;
847 me = (instr >> 1) & 0x1f;
848 val = DATA32(regs->gpr[rd]);
849 regs->gpr[ra] = ROTATE(val, rb) & MASK32(mb, me);
850 goto logical_done;
851
852 case 23: /* rlwnm */
853 mb = (instr >> 6) & 0x1f;
854 me = (instr >> 1) & 0x1f;
855 rb = regs->gpr[rb] & 0x1f;
856 val = DATA32(regs->gpr[rd]);
857 regs->gpr[ra] = ROTATE(val, rb) & MASK32(mb, me);
858 goto logical_done;
859
860 case 24: /* ori */
861 imm = (unsigned short) instr;
862 regs->gpr[ra] = regs->gpr[rd] | imm;
863 goto instr_done;
864
865 case 25: /* oris */
866 imm = (unsigned short) instr;
867 regs->gpr[ra] = regs->gpr[rd] | (imm << 16);
868 goto instr_done;
869
870 case 26: /* xori */
871 imm = (unsigned short) instr;
872 regs->gpr[ra] = regs->gpr[rd] ^ imm;
873 goto instr_done;
874
875 case 27: /* xoris */
876 imm = (unsigned short) instr;
877 regs->gpr[ra] = regs->gpr[rd] ^ (imm << 16);
878 goto instr_done;
879
880 case 28: /* andi. */
881 imm = (unsigned short) instr;
882 regs->gpr[ra] = regs->gpr[rd] & imm;
883 set_cr0(regs, ra);
884 goto instr_done;
885
886 case 29: /* andis. */
887 imm = (unsigned short) instr;
888 regs->gpr[ra] = regs->gpr[rd] & (imm << 16);
889 set_cr0(regs, ra);
890 goto instr_done;
891
892#ifdef __powerpc64__
893 case 30: /* rld* */
894 mb = ((instr >> 6) & 0x1f) | (instr & 0x20);
895 val = regs->gpr[rd];
896 if ((instr & 0x10) == 0) {
897 sh = rb | ((instr & 2) << 4);
898 val = ROTATE(val, sh);
899 switch ((instr >> 2) & 3) {
900 case 0: /* rldicl */
901 regs->gpr[ra] = val & MASK64_L(mb);
902 goto logical_done;
903 case 1: /* rldicr */
904 regs->gpr[ra] = val & MASK64_R(mb);
905 goto logical_done;
906 case 2: /* rldic */
907 regs->gpr[ra] = val & MASK64(mb, 63 - sh);
908 goto logical_done;
909 case 3: /* rldimi */
910 imm = MASK64(mb, 63 - sh);
911 regs->gpr[ra] = (regs->gpr[ra] & ~imm) |
912 (val & imm);
913 goto logical_done;
914 }
915 } else {
916 sh = regs->gpr[rb] & 0x3f;
917 val = ROTATE(val, sh);
918 switch ((instr >> 1) & 7) {
919 case 0: /* rldcl */
920 regs->gpr[ra] = val & MASK64_L(mb);
921 goto logical_done;
922 case 1: /* rldcr */
923 regs->gpr[ra] = val & MASK64_R(mb);
924 goto logical_done;
925 }
926 }
927#endif
Oliver O'Halloran66707832016-02-16 17:31:53 +1100928 break; /* illegal instruction */
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000929
930 case 31:
931 switch ((instr >> 1) & 0x3ff) {
Paul Mackerrascf87c3f2014-09-02 14:35:08 +1000932 case 4: /* tw */
933 if (rd == 0x1f ||
934 (rd & trap_compare((int)regs->gpr[ra],
935 (int)regs->gpr[rb])))
936 goto trap;
937 goto instr_done;
938#ifdef __powerpc64__
939 case 68: /* td */
940 if (rd & trap_compare(regs->gpr[ra], regs->gpr[rb]))
941 goto trap;
942 goto instr_done;
943#endif
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000944 case 83: /* mfmsr */
945 if (regs->msr & MSR_PR)
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000946 goto priv;
947 op->type = MFMSR;
948 op->reg = rd;
949 return 0;
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000950 case 146: /* mtmsr */
951 if (regs->msr & MSR_PR)
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000952 goto priv;
953 op->type = MTMSR;
954 op->reg = rd;
955 op->val = 0xffffffff & ~(MSR_ME | MSR_LE);
956 return 0;
Paul Mackerrasc0325242005-10-28 22:48:08 +1000957#ifdef CONFIG_PPC64
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000958 case 178: /* mtmsrd */
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000959 if (regs->msr & MSR_PR)
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000960 goto priv;
961 op->type = MTMSR;
962 op->reg = rd;
963 /* only MSR_EE and MSR_RI get changed if bit 15 set */
964 /* mtmsrd doesn't change MSR_HV, MSR_ME or MSR_LE */
965 imm = (instr & 0x10000)? 0x8002: 0xefffffffffffeffeUL;
966 op->val = imm;
967 return 0;
Paul Mackerrasc0325242005-10-28 22:48:08 +1000968#endif
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000969
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000970 case 19: /* mfcr */
Ananth N Mavinakayanahalli68881992007-04-18 15:56:38 +1000971 regs->gpr[rd] = regs->ccr;
972 regs->gpr[rd] &= 0xffffffffUL;
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000973 goto instr_done;
974
975 case 144: /* mtcrf */
976 imm = 0xf0000000UL;
977 val = regs->gpr[rd];
978 for (sh = 0; sh < 8; ++sh) {
979 if (instr & (0x80000 >> sh))
980 regs->ccr = (regs->ccr & ~imm) |
981 (val & imm);
982 imm >>= 4;
983 }
984 goto instr_done;
985
986 case 339: /* mfspr */
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000987 spr = ((instr >> 16) & 0x1f) | ((instr >> 6) & 0x3e0);
Ananth N Mavinakayanahalli68881992007-04-18 15:56:38 +1000988 switch (spr) {
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000989 case SPRN_XER: /* mfxer */
Ananth N Mavinakayanahalli68881992007-04-18 15:56:38 +1000990 regs->gpr[rd] = regs->xer;
991 regs->gpr[rd] &= 0xffffffffUL;
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000992 goto instr_done;
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000993 case SPRN_LR: /* mflr */
Ananth N Mavinakayanahalli68881992007-04-18 15:56:38 +1000994 regs->gpr[rd] = regs->link;
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000995 goto instr_done;
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000996 case SPRN_CTR: /* mfctr */
Ananth N Mavinakayanahalli68881992007-04-18 15:56:38 +1000997 regs->gpr[rd] = regs->ctr;
Paul Mackerras0016a4c2010-06-15 14:48:58 +1000998 goto instr_done;
Paul Mackerrasbe96f632014-09-02 14:35:07 +1000999 default:
1000 op->type = MFSPR;
1001 op->reg = rd;
1002 op->spr = spr;
1003 return 0;
Ananth N Mavinakayanahalli68881992007-04-18 15:56:38 +10001004 }
1005 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001006
1007 case 467: /* mtspr */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001008 spr = ((instr >> 16) & 0x1f) | ((instr >> 6) & 0x3e0);
Ananth N Mavinakayanahalli68881992007-04-18 15:56:38 +10001009 switch (spr) {
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001010 case SPRN_XER: /* mtxer */
Ananth N Mavinakayanahalli68881992007-04-18 15:56:38 +10001011 regs->xer = (regs->gpr[rd] & 0xffffffffUL);
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001012 goto instr_done;
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001013 case SPRN_LR: /* mtlr */
Ananth N Mavinakayanahalli68881992007-04-18 15:56:38 +10001014 regs->link = regs->gpr[rd];
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001015 goto instr_done;
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001016 case SPRN_CTR: /* mtctr */
Ananth N Mavinakayanahalli68881992007-04-18 15:56:38 +10001017 regs->ctr = regs->gpr[rd];
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001018 goto instr_done;
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001019 default:
1020 op->type = MTSPR;
1021 op->val = regs->gpr[rd];
1022 op->spr = spr;
1023 return 0;
Ananth N Mavinakayanahalli68881992007-04-18 15:56:38 +10001024 }
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001025 break;
1026
1027/*
1028 * Compare instructions
1029 */
1030 case 0: /* cmp */
1031 val = regs->gpr[ra];
1032 val2 = regs->gpr[rb];
1033#ifdef __powerpc64__
1034 if ((rd & 1) == 0) {
1035 /* word (32-bit) compare */
1036 val = (int) val;
1037 val2 = (int) val2;
1038 }
1039#endif
1040 do_cmp_signed(regs, val, val2, rd >> 2);
1041 goto instr_done;
1042
1043 case 32: /* cmpl */
1044 val = regs->gpr[ra];
1045 val2 = regs->gpr[rb];
1046#ifdef __powerpc64__
1047 if ((rd & 1) == 0) {
1048 /* word (32-bit) compare */
1049 val = (unsigned int) val;
1050 val2 = (unsigned int) val2;
1051 }
1052#endif
1053 do_cmp_unsigned(regs, val, val2, rd >> 2);
1054 goto instr_done;
1055
1056/*
1057 * Arithmetic instructions
1058 */
1059 case 8: /* subfc */
1060 add_with_carry(regs, rd, ~regs->gpr[ra],
1061 regs->gpr[rb], 1);
1062 goto arith_done;
1063#ifdef __powerpc64__
1064 case 9: /* mulhdu */
1065 asm("mulhdu %0,%1,%2" : "=r" (regs->gpr[rd]) :
1066 "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
1067 goto arith_done;
1068#endif
1069 case 10: /* addc */
1070 add_with_carry(regs, rd, regs->gpr[ra],
1071 regs->gpr[rb], 0);
1072 goto arith_done;
1073
1074 case 11: /* mulhwu */
1075 asm("mulhwu %0,%1,%2" : "=r" (regs->gpr[rd]) :
1076 "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
1077 goto arith_done;
1078
1079 case 40: /* subf */
1080 regs->gpr[rd] = regs->gpr[rb] - regs->gpr[ra];
1081 goto arith_done;
1082#ifdef __powerpc64__
1083 case 73: /* mulhd */
1084 asm("mulhd %0,%1,%2" : "=r" (regs->gpr[rd]) :
1085 "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
1086 goto arith_done;
1087#endif
1088 case 75: /* mulhw */
1089 asm("mulhw %0,%1,%2" : "=r" (regs->gpr[rd]) :
1090 "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
1091 goto arith_done;
1092
1093 case 104: /* neg */
1094 regs->gpr[rd] = -regs->gpr[ra];
1095 goto arith_done;
1096
1097 case 136: /* subfe */
1098 add_with_carry(regs, rd, ~regs->gpr[ra], regs->gpr[rb],
1099 regs->xer & XER_CA);
1100 goto arith_done;
1101
1102 case 138: /* adde */
1103 add_with_carry(regs, rd, regs->gpr[ra], regs->gpr[rb],
1104 regs->xer & XER_CA);
1105 goto arith_done;
1106
1107 case 200: /* subfze */
1108 add_with_carry(regs, rd, ~regs->gpr[ra], 0L,
1109 regs->xer & XER_CA);
1110 goto arith_done;
1111
1112 case 202: /* addze */
1113 add_with_carry(regs, rd, regs->gpr[ra], 0L,
1114 regs->xer & XER_CA);
1115 goto arith_done;
1116
1117 case 232: /* subfme */
1118 add_with_carry(regs, rd, ~regs->gpr[ra], -1L,
1119 regs->xer & XER_CA);
1120 goto arith_done;
1121#ifdef __powerpc64__
1122 case 233: /* mulld */
1123 regs->gpr[rd] = regs->gpr[ra] * regs->gpr[rb];
1124 goto arith_done;
1125#endif
1126 case 234: /* addme */
1127 add_with_carry(regs, rd, regs->gpr[ra], -1L,
1128 regs->xer & XER_CA);
1129 goto arith_done;
1130
1131 case 235: /* mullw */
1132 regs->gpr[rd] = (unsigned int) regs->gpr[ra] *
1133 (unsigned int) regs->gpr[rb];
1134 goto arith_done;
1135
1136 case 266: /* add */
1137 regs->gpr[rd] = regs->gpr[ra] + regs->gpr[rb];
1138 goto arith_done;
1139#ifdef __powerpc64__
1140 case 457: /* divdu */
1141 regs->gpr[rd] = regs->gpr[ra] / regs->gpr[rb];
1142 goto arith_done;
1143#endif
1144 case 459: /* divwu */
1145 regs->gpr[rd] = (unsigned int) regs->gpr[ra] /
1146 (unsigned int) regs->gpr[rb];
1147 goto arith_done;
1148#ifdef __powerpc64__
1149 case 489: /* divd */
1150 regs->gpr[rd] = (long int) regs->gpr[ra] /
1151 (long int) regs->gpr[rb];
1152 goto arith_done;
1153#endif
1154 case 491: /* divw */
1155 regs->gpr[rd] = (int) regs->gpr[ra] /
1156 (int) regs->gpr[rb];
1157 goto arith_done;
1158
1159
1160/*
1161 * Logical instructions
1162 */
1163 case 26: /* cntlzw */
1164 asm("cntlzw %0,%1" : "=r" (regs->gpr[ra]) :
1165 "r" (regs->gpr[rd]));
1166 goto logical_done;
1167#ifdef __powerpc64__
1168 case 58: /* cntlzd */
1169 asm("cntlzd %0,%1" : "=r" (regs->gpr[ra]) :
1170 "r" (regs->gpr[rd]));
1171 goto logical_done;
1172#endif
1173 case 28: /* and */
1174 regs->gpr[ra] = regs->gpr[rd] & regs->gpr[rb];
1175 goto logical_done;
1176
1177 case 60: /* andc */
1178 regs->gpr[ra] = regs->gpr[rd] & ~regs->gpr[rb];
1179 goto logical_done;
1180
1181 case 124: /* nor */
1182 regs->gpr[ra] = ~(regs->gpr[rd] | regs->gpr[rb]);
1183 goto logical_done;
1184
1185 case 284: /* xor */
1186 regs->gpr[ra] = ~(regs->gpr[rd] ^ regs->gpr[rb]);
1187 goto logical_done;
1188
1189 case 316: /* xor */
1190 regs->gpr[ra] = regs->gpr[rd] ^ regs->gpr[rb];
1191 goto logical_done;
1192
1193 case 412: /* orc */
1194 regs->gpr[ra] = regs->gpr[rd] | ~regs->gpr[rb];
1195 goto logical_done;
1196
1197 case 444: /* or */
1198 regs->gpr[ra] = regs->gpr[rd] | regs->gpr[rb];
1199 goto logical_done;
1200
1201 case 476: /* nand */
1202 regs->gpr[ra] = ~(regs->gpr[rd] & regs->gpr[rb]);
1203 goto logical_done;
1204
1205 case 922: /* extsh */
1206 regs->gpr[ra] = (signed short) regs->gpr[rd];
1207 goto logical_done;
1208
1209 case 954: /* extsb */
1210 regs->gpr[ra] = (signed char) regs->gpr[rd];
1211 goto logical_done;
1212#ifdef __powerpc64__
1213 case 986: /* extsw */
1214 regs->gpr[ra] = (signed int) regs->gpr[rd];
1215 goto logical_done;
1216#endif
1217
1218/*
1219 * Shift instructions
1220 */
1221 case 24: /* slw */
1222 sh = regs->gpr[rb] & 0x3f;
1223 if (sh < 32)
1224 regs->gpr[ra] = (regs->gpr[rd] << sh) & 0xffffffffUL;
1225 else
1226 regs->gpr[ra] = 0;
1227 goto logical_done;
1228
1229 case 536: /* srw */
1230 sh = regs->gpr[rb] & 0x3f;
1231 if (sh < 32)
1232 regs->gpr[ra] = (regs->gpr[rd] & 0xffffffffUL) >> sh;
1233 else
1234 regs->gpr[ra] = 0;
1235 goto logical_done;
1236
1237 case 792: /* sraw */
1238 sh = regs->gpr[rb] & 0x3f;
1239 ival = (signed int) regs->gpr[rd];
1240 regs->gpr[ra] = ival >> (sh < 32 ? sh : 31);
Paul Mackerrase698b962014-07-19 17:47:57 +10001241 if (ival < 0 && (sh >= 32 || (ival & ((1ul << sh) - 1)) != 0))
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001242 regs->xer |= XER_CA;
1243 else
1244 regs->xer &= ~XER_CA;
1245 goto logical_done;
1246
1247 case 824: /* srawi */
1248 sh = rb;
1249 ival = (signed int) regs->gpr[rd];
1250 regs->gpr[ra] = ival >> sh;
Paul Mackerrase698b962014-07-19 17:47:57 +10001251 if (ival < 0 && (ival & ((1ul << sh) - 1)) != 0)
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001252 regs->xer |= XER_CA;
1253 else
1254 regs->xer &= ~XER_CA;
1255 goto logical_done;
1256
1257#ifdef __powerpc64__
1258 case 27: /* sld */
Paul Mackerrase698b962014-07-19 17:47:57 +10001259 sh = regs->gpr[rb] & 0x7f;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001260 if (sh < 64)
1261 regs->gpr[ra] = regs->gpr[rd] << sh;
1262 else
1263 regs->gpr[ra] = 0;
1264 goto logical_done;
1265
1266 case 539: /* srd */
1267 sh = regs->gpr[rb] & 0x7f;
1268 if (sh < 64)
1269 regs->gpr[ra] = regs->gpr[rd] >> sh;
1270 else
1271 regs->gpr[ra] = 0;
1272 goto logical_done;
1273
1274 case 794: /* srad */
1275 sh = regs->gpr[rb] & 0x7f;
1276 ival = (signed long int) regs->gpr[rd];
1277 regs->gpr[ra] = ival >> (sh < 64 ? sh : 63);
Paul Mackerrase698b962014-07-19 17:47:57 +10001278 if (ival < 0 && (sh >= 64 || (ival & ((1ul << sh) - 1)) != 0))
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001279 regs->xer |= XER_CA;
1280 else
1281 regs->xer &= ~XER_CA;
1282 goto logical_done;
1283
1284 case 826: /* sradi with sh_5 = 0 */
1285 case 827: /* sradi with sh_5 = 1 */
1286 sh = rb | ((instr & 2) << 4);
1287 ival = (signed long int) regs->gpr[rd];
1288 regs->gpr[ra] = ival >> sh;
Paul Mackerrase698b962014-07-19 17:47:57 +10001289 if (ival < 0 && (ival & ((1ul << sh) - 1)) != 0)
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001290 regs->xer |= XER_CA;
1291 else
1292 regs->xer &= ~XER_CA;
1293 goto logical_done;
1294#endif /* __powerpc64__ */
1295
1296/*
1297 * Cache instructions
1298 */
1299 case 54: /* dcbst */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001300 op->type = MKOP(CACHEOP, DCBST, 0);
1301 op->ea = xform_ea(instr, regs);
1302 return 0;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001303
1304 case 86: /* dcbf */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001305 op->type = MKOP(CACHEOP, DCBF, 0);
1306 op->ea = xform_ea(instr, regs);
1307 return 0;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001308
1309 case 246: /* dcbtst */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001310 op->type = MKOP(CACHEOP, DCBTST, 0);
1311 op->ea = xform_ea(instr, regs);
1312 op->reg = rd;
1313 return 0;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001314
1315 case 278: /* dcbt */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001316 op->type = MKOP(CACHEOP, DCBTST, 0);
1317 op->ea = xform_ea(instr, regs);
1318 op->reg = rd;
1319 return 0;
Paul Mackerrascf87c3f2014-09-02 14:35:08 +10001320
1321 case 982: /* icbi */
1322 op->type = MKOP(CACHEOP, ICBI, 0);
1323 op->ea = xform_ea(instr, regs);
1324 return 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001325 }
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001326 break;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001327 }
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001328
1329 /*
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001330 * Loads and stores.
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001331 */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001332 op->type = UNKNOWN;
1333 op->update_reg = ra;
1334 op->reg = rd;
1335 op->val = regs->gpr[rd];
1336 u = (instr >> 20) & UPDATE;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001337
1338 switch (opcode) {
1339 case 31:
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001340 u = instr & UPDATE;
1341 op->ea = xform_ea(instr, regs);
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001342 switch ((instr >> 1) & 0x3ff) {
1343 case 20: /* lwarx */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001344 op->type = MKOP(LARX, 0, 4);
1345 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001346
1347 case 150: /* stwcx. */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001348 op->type = MKOP(STCX, 0, 4);
1349 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001350
1351#ifdef __powerpc64__
1352 case 84: /* ldarx */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001353 op->type = MKOP(LARX, 0, 8);
1354 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001355
1356 case 214: /* stdcx. */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001357 op->type = MKOP(STCX, 0, 8);
1358 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001359
1360 case 21: /* ldx */
1361 case 53: /* ldux */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001362 op->type = MKOP(LOAD, u, 8);
1363 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001364#endif
1365
1366 case 23: /* lwzx */
1367 case 55: /* lwzux */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001368 op->type = MKOP(LOAD, u, 4);
1369 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001370
1371 case 87: /* lbzx */
1372 case 119: /* lbzux */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001373 op->type = MKOP(LOAD, u, 1);
1374 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001375
1376#ifdef CONFIG_ALTIVEC
1377 case 103: /* lvx */
1378 case 359: /* lvxl */
1379 if (!(regs->msr & MSR_VEC))
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001380 goto vecunavail;
1381 op->type = MKOP(LOAD_VMX, 0, 16);
1382 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001383
1384 case 231: /* stvx */
1385 case 487: /* stvxl */
1386 if (!(regs->msr & MSR_VEC))
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001387 goto vecunavail;
1388 op->type = MKOP(STORE_VMX, 0, 16);
1389 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001390#endif /* CONFIG_ALTIVEC */
1391
1392#ifdef __powerpc64__
1393 case 149: /* stdx */
1394 case 181: /* stdux */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001395 op->type = MKOP(STORE, u, 8);
1396 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001397#endif
1398
1399 case 151: /* stwx */
1400 case 183: /* stwux */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001401 op->type = MKOP(STORE, u, 4);
1402 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001403
1404 case 215: /* stbx */
1405 case 247: /* stbux */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001406 op->type = MKOP(STORE, u, 1);
1407 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001408
1409 case 279: /* lhzx */
1410 case 311: /* lhzux */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001411 op->type = MKOP(LOAD, u, 2);
1412 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001413
1414#ifdef __powerpc64__
1415 case 341: /* lwax */
1416 case 373: /* lwaux */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001417 op->type = MKOP(LOAD, SIGNEXT | u, 4);
1418 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001419#endif
1420
1421 case 343: /* lhax */
1422 case 375: /* lhaux */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001423 op->type = MKOP(LOAD, SIGNEXT | u, 2);
1424 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001425
1426 case 407: /* sthx */
1427 case 439: /* sthux */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001428 op->type = MKOP(STORE, u, 2);
1429 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001430
1431#ifdef __powerpc64__
1432 case 532: /* ldbrx */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001433 op->type = MKOP(LOAD, BYTEREV, 8);
1434 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001435
1436#endif
Paul Mackerrasc9f6f4e2014-09-02 14:35:09 +10001437 case 533: /* lswx */
1438 op->type = MKOP(LOAD_MULTI, 0, regs->xer & 0x7f);
1439 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001440
1441 case 534: /* lwbrx */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001442 op->type = MKOP(LOAD, BYTEREV, 4);
1443 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001444
Paul Mackerrasc9f6f4e2014-09-02 14:35:09 +10001445 case 597: /* lswi */
1446 if (rb == 0)
1447 rb = 32; /* # bytes to load */
1448 op->type = MKOP(LOAD_MULTI, 0, rb);
1449 op->ea = 0;
1450 if (ra)
1451 op->ea = truncate_if_32bit(regs->msr,
1452 regs->gpr[ra]);
1453 break;
1454
Paul Bolleb69a1da2014-05-20 21:59:42 +02001455#ifdef CONFIG_PPC_FPU
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001456 case 535: /* lfsx */
1457 case 567: /* lfsux */
1458 if (!(regs->msr & MSR_FP))
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001459 goto fpunavail;
1460 op->type = MKOP(LOAD_FP, u, 4);
1461 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001462
1463 case 599: /* lfdx */
1464 case 631: /* lfdux */
1465 if (!(regs->msr & MSR_FP))
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001466 goto fpunavail;
1467 op->type = MKOP(LOAD_FP, u, 8);
1468 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001469
1470 case 663: /* stfsx */
1471 case 695: /* stfsux */
1472 if (!(regs->msr & MSR_FP))
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001473 goto fpunavail;
1474 op->type = MKOP(STORE_FP, u, 4);
1475 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001476
1477 case 727: /* stfdx */
1478 case 759: /* stfdux */
1479 if (!(regs->msr & MSR_FP))
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001480 goto fpunavail;
1481 op->type = MKOP(STORE_FP, u, 8);
1482 break;
Sean MacLennancd64d162010-09-01 07:21:21 +00001483#endif
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001484
1485#ifdef __powerpc64__
1486 case 660: /* stdbrx */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001487 op->type = MKOP(STORE, BYTEREV, 8);
1488 op->val = byterev_8(regs->gpr[rd]);
1489 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001490
1491#endif
Paul Mackerrasc9f6f4e2014-09-02 14:35:09 +10001492 case 661: /* stswx */
1493 op->type = MKOP(STORE_MULTI, 0, regs->xer & 0x7f);
1494 break;
1495
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001496 case 662: /* stwbrx */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001497 op->type = MKOP(STORE, BYTEREV, 4);
1498 op->val = byterev_4(regs->gpr[rd]);
1499 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001500
Paul Mackerrasc9f6f4e2014-09-02 14:35:09 +10001501 case 725:
1502 if (rb == 0)
1503 rb = 32; /* # bytes to store */
1504 op->type = MKOP(STORE_MULTI, 0, rb);
1505 op->ea = 0;
1506 if (ra)
1507 op->ea = truncate_if_32bit(regs->msr,
1508 regs->gpr[ra]);
1509 break;
1510
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001511 case 790: /* lhbrx */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001512 op->type = MKOP(LOAD, BYTEREV, 2);
1513 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001514
1515 case 918: /* sthbrx */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001516 op->type = MKOP(STORE, BYTEREV, 2);
1517 op->val = byterev_2(regs->gpr[rd]);
1518 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001519
1520#ifdef CONFIG_VSX
1521 case 844: /* lxvd2x */
1522 case 876: /* lxvd2ux */
1523 if (!(regs->msr & MSR_VSX))
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001524 goto vsxunavail;
1525 op->reg = rd | ((instr & 1) << 5);
1526 op->type = MKOP(LOAD_VSX, u, 16);
1527 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001528
1529 case 972: /* stxvd2x */
1530 case 1004: /* stxvd2ux */
1531 if (!(regs->msr & MSR_VSX))
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001532 goto vsxunavail;
1533 op->reg = rd | ((instr & 1) << 5);
1534 op->type = MKOP(STORE_VSX, u, 16);
1535 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001536
1537#endif /* CONFIG_VSX */
1538 }
1539 break;
1540
1541 case 32: /* lwz */
1542 case 33: /* lwzu */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001543 op->type = MKOP(LOAD, u, 4);
1544 op->ea = dform_ea(instr, regs);
1545 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001546
1547 case 34: /* lbz */
1548 case 35: /* lbzu */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001549 op->type = MKOP(LOAD, u, 1);
1550 op->ea = dform_ea(instr, regs);
1551 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001552
1553 case 36: /* stw */
Tiejun Chen8e9f6932012-09-16 23:54:31 +00001554 case 37: /* stwu */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001555 op->type = MKOP(STORE, u, 4);
1556 op->ea = dform_ea(instr, regs);
1557 break;
Tiejun Chen8e9f6932012-09-16 23:54:31 +00001558
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001559 case 38: /* stb */
1560 case 39: /* stbu */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001561 op->type = MKOP(STORE, u, 1);
1562 op->ea = dform_ea(instr, regs);
1563 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001564
1565 case 40: /* lhz */
1566 case 41: /* lhzu */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001567 op->type = MKOP(LOAD, u, 2);
1568 op->ea = dform_ea(instr, regs);
1569 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001570
1571 case 42: /* lha */
1572 case 43: /* lhau */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001573 op->type = MKOP(LOAD, SIGNEXT | u, 2);
1574 op->ea = dform_ea(instr, regs);
1575 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001576
1577 case 44: /* sth */
1578 case 45: /* sthu */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001579 op->type = MKOP(STORE, u, 2);
1580 op->ea = dform_ea(instr, regs);
1581 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001582
1583 case 46: /* lmw */
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001584 if (ra >= rd)
1585 break; /* invalid form, ra in range to load */
Paul Mackerrasc9f6f4e2014-09-02 14:35:09 +10001586 op->type = MKOP(LOAD_MULTI, 0, 4 * (32 - rd));
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001587 op->ea = dform_ea(instr, regs);
1588 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001589
1590 case 47: /* stmw */
Paul Mackerrasc9f6f4e2014-09-02 14:35:09 +10001591 op->type = MKOP(STORE_MULTI, 0, 4 * (32 - rd));
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001592 op->ea = dform_ea(instr, regs);
1593 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001594
Sean MacLennancd64d162010-09-01 07:21:21 +00001595#ifdef CONFIG_PPC_FPU
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001596 case 48: /* lfs */
1597 case 49: /* lfsu */
1598 if (!(regs->msr & MSR_FP))
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001599 goto fpunavail;
1600 op->type = MKOP(LOAD_FP, u, 4);
1601 op->ea = dform_ea(instr, regs);
1602 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001603
1604 case 50: /* lfd */
1605 case 51: /* lfdu */
1606 if (!(regs->msr & MSR_FP))
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001607 goto fpunavail;
1608 op->type = MKOP(LOAD_FP, u, 8);
1609 op->ea = dform_ea(instr, regs);
1610 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001611
1612 case 52: /* stfs */
1613 case 53: /* stfsu */
1614 if (!(regs->msr & MSR_FP))
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001615 goto fpunavail;
1616 op->type = MKOP(STORE_FP, u, 4);
1617 op->ea = dform_ea(instr, regs);
1618 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001619
1620 case 54: /* stfd */
1621 case 55: /* stfdu */
1622 if (!(regs->msr & MSR_FP))
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001623 goto fpunavail;
1624 op->type = MKOP(STORE_FP, u, 8);
1625 op->ea = dform_ea(instr, regs);
1626 break;
Sean MacLennancd64d162010-09-01 07:21:21 +00001627#endif
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001628
1629#ifdef __powerpc64__
1630 case 58: /* ld[u], lwa */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001631 op->ea = dsform_ea(instr, regs);
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001632 switch (instr & 3) {
1633 case 0: /* ld */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001634 op->type = MKOP(LOAD, 0, 8);
1635 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001636 case 1: /* ldu */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001637 op->type = MKOP(LOAD, UPDATE, 8);
1638 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001639 case 2: /* lwa */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001640 op->type = MKOP(LOAD, SIGNEXT, 4);
1641 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001642 }
1643 break;
1644
1645 case 62: /* std[u] */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001646 op->ea = dsform_ea(instr, regs);
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001647 switch (instr & 3) {
1648 case 0: /* std */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001649 op->type = MKOP(STORE, 0, 8);
1650 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001651 case 1: /* stdu */
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001652 op->type = MKOP(STORE, UPDATE, 8);
1653 break;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001654 }
1655 break;
1656#endif /* __powerpc64__ */
1657
1658 }
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001659 return 0;
Paul Mackerras0016a4c2010-06-15 14:48:58 +10001660
1661 logical_done:
1662 if (instr & 1)
1663 set_cr0(regs, ra);
1664 goto instr_done;
1665
1666 arith_done:
1667 if (instr & 1)
1668 set_cr0(regs, rd);
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001669
1670 instr_done:
1671 regs->nip = truncate_if_32bit(regs->msr, regs->nip + 4);
1672 return 1;
1673
1674 priv:
1675 op->type = INTERRUPT | 0x700;
1676 op->val = SRR1_PROGPRIV;
1677 return 0;
1678
Paul Mackerrascf87c3f2014-09-02 14:35:08 +10001679 trap:
1680 op->type = INTERRUPT | 0x700;
1681 op->val = SRR1_PROGTRAP;
1682 return 0;
1683
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001684#ifdef CONFIG_PPC_FPU
1685 fpunavail:
1686 op->type = INTERRUPT | 0x800;
1687 return 0;
1688#endif
1689
1690#ifdef CONFIG_ALTIVEC
1691 vecunavail:
1692 op->type = INTERRUPT | 0xf20;
1693 return 0;
1694#endif
1695
1696#ifdef CONFIG_VSX
1697 vsxunavail:
1698 op->type = INTERRUPT | 0xf40;
1699 return 0;
1700#endif
1701}
1702EXPORT_SYMBOL_GPL(analyse_instr);
1703
1704/*
1705 * For PPC32 we always use stwu with r1 to change the stack pointer.
1706 * So this emulated store may corrupt the exception frame, now we
1707 * have to provide the exception frame trampoline, which is pushed
1708 * below the kprobed function stack. So we only update gpr[1] but
1709 * don't emulate the real store operation. We will do real store
1710 * operation safely in exception return code by checking this flag.
1711 */
1712static __kprobes int handle_stack_update(unsigned long ea, struct pt_regs *regs)
1713{
1714#ifdef CONFIG_PPC32
1715 /*
1716 * Check if we will touch kernel stack overflow
1717 */
1718 if (ea - STACK_INT_FRAME_SIZE <= current->thread.ksp_limit) {
1719 printk(KERN_CRIT "Can't kprobe this since kernel stack would overflow.\n");
1720 return -EINVAL;
1721 }
1722#endif /* CONFIG_PPC32 */
1723 /*
1724 * Check if we already set since that means we'll
1725 * lose the previous value.
1726 */
1727 WARN_ON(test_thread_flag(TIF_EMULATE_STACK_STORE));
1728 set_thread_flag(TIF_EMULATE_STACK_STORE);
1729 return 0;
1730}
1731
1732static __kprobes void do_signext(unsigned long *valp, int size)
1733{
1734 switch (size) {
1735 case 2:
1736 *valp = (signed short) *valp;
1737 break;
1738 case 4:
1739 *valp = (signed int) *valp;
1740 break;
1741 }
1742}
1743
1744static __kprobes void do_byterev(unsigned long *valp, int size)
1745{
1746 switch (size) {
1747 case 2:
1748 *valp = byterev_2(*valp);
1749 break;
1750 case 4:
1751 *valp = byterev_4(*valp);
1752 break;
1753#ifdef __powerpc64__
1754 case 8:
1755 *valp = byterev_8(*valp);
1756 break;
1757#endif
1758 }
1759}
1760
1761/*
1762 * Emulate instructions that cause a transfer of control,
1763 * loads and stores, and a few other instructions.
1764 * Returns 1 if the step was emulated, 0 if not,
1765 * or -1 if the instruction is one that should not be stepped,
1766 * such as an rfid, or a mtmsrd that would clear MSR_RI.
1767 */
1768int __kprobes emulate_step(struct pt_regs *regs, unsigned int instr)
1769{
1770 struct instruction_op op;
1771 int r, err, size;
1772 unsigned long val;
1773 unsigned int cr;
Paul Mackerrasc9f6f4e2014-09-02 14:35:09 +10001774 int i, rd, nb;
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001775
1776 r = analyse_instr(&op, regs, instr);
1777 if (r != 0)
1778 return r;
1779
1780 err = 0;
1781 size = GETSIZE(op.type);
1782 switch (op.type & INSTR_TYPE_MASK) {
1783 case CACHEOP:
1784 if (!address_ok(regs, op.ea, 8))
1785 return 0;
1786 switch (op.type & CACHEOP_MASK) {
1787 case DCBST:
1788 __cacheop_user_asmx(op.ea, err, "dcbst");
1789 break;
1790 case DCBF:
1791 __cacheop_user_asmx(op.ea, err, "dcbf");
1792 break;
1793 case DCBTST:
1794 if (op.reg == 0)
1795 prefetchw((void *) op.ea);
1796 break;
1797 case DCBT:
1798 if (op.reg == 0)
1799 prefetch((void *) op.ea);
1800 break;
Paul Mackerrascf87c3f2014-09-02 14:35:08 +10001801 case ICBI:
1802 __cacheop_user_asmx(op.ea, err, "icbi");
1803 break;
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001804 }
1805 if (err)
1806 return 0;
1807 goto instr_done;
1808
1809 case LARX:
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001810 if (op.ea & (size - 1))
1811 break; /* can't handle misaligned */
1812 err = -EFAULT;
1813 if (!address_ok(regs, op.ea, size))
1814 goto ldst_done;
1815 err = 0;
1816 switch (size) {
1817 case 4:
1818 __get_user_asmx(val, op.ea, err, "lwarx");
1819 break;
Lennart Sorensendd217312016-05-05 16:44:44 -04001820#ifdef __powerpc64__
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001821 case 8:
1822 __get_user_asmx(val, op.ea, err, "ldarx");
1823 break;
Lennart Sorensendd217312016-05-05 16:44:44 -04001824#endif
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001825 default:
1826 return 0;
1827 }
1828 if (!err)
1829 regs->gpr[op.reg] = val;
1830 goto ldst_done;
1831
1832 case STCX:
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001833 if (op.ea & (size - 1))
1834 break; /* can't handle misaligned */
1835 err = -EFAULT;
1836 if (!address_ok(regs, op.ea, size))
1837 goto ldst_done;
1838 err = 0;
1839 switch (size) {
1840 case 4:
1841 __put_user_asmx(op.val, op.ea, err, "stwcx.", cr);
1842 break;
Lennart Sorensendd217312016-05-05 16:44:44 -04001843#ifdef __powerpc64__
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001844 case 8:
1845 __put_user_asmx(op.val, op.ea, err, "stdcx.", cr);
1846 break;
Lennart Sorensendd217312016-05-05 16:44:44 -04001847#endif
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001848 default:
1849 return 0;
1850 }
1851 if (!err)
1852 regs->ccr = (regs->ccr & 0x0fffffff) |
1853 (cr & 0xe0000000) |
1854 ((regs->xer >> 3) & 0x10000000);
1855 goto ldst_done;
1856
1857 case LOAD:
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001858 err = read_mem(&regs->gpr[op.reg], op.ea, size, regs);
1859 if (!err) {
1860 if (op.type & SIGNEXT)
1861 do_signext(&regs->gpr[op.reg], size);
1862 if (op.type & BYTEREV)
1863 do_byterev(&regs->gpr[op.reg], size);
1864 }
1865 goto ldst_done;
1866
Paul Mackerras7048c842014-11-03 15:46:43 +11001867#ifdef CONFIG_PPC_FPU
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001868 case LOAD_FP:
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001869 if (size == 4)
1870 err = do_fp_load(op.reg, do_lfs, op.ea, size, regs);
1871 else
1872 err = do_fp_load(op.reg, do_lfd, op.ea, size, regs);
1873 goto ldst_done;
Paul Mackerras7048c842014-11-03 15:46:43 +11001874#endif
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001875#ifdef CONFIG_ALTIVEC
1876 case LOAD_VMX:
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001877 err = do_vec_load(op.reg, do_lvx, op.ea & ~0xfUL, regs);
1878 goto ldst_done;
1879#endif
1880#ifdef CONFIG_VSX
1881 case LOAD_VSX:
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001882 err = do_vsx_load(op.reg, do_lxvd2x, op.ea, regs);
1883 goto ldst_done;
1884#endif
1885 case LOAD_MULTI:
1886 if (regs->msr & MSR_LE)
1887 return 0;
1888 rd = op.reg;
Paul Mackerrasc9f6f4e2014-09-02 14:35:09 +10001889 for (i = 0; i < size; i += 4) {
1890 nb = size - i;
1891 if (nb > 4)
1892 nb = 4;
1893 err = read_mem(&regs->gpr[rd], op.ea, nb, regs);
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001894 if (err)
1895 return 0;
Paul Mackerrasc9f6f4e2014-09-02 14:35:09 +10001896 if (nb < 4) /* left-justify last bytes */
1897 regs->gpr[rd] <<= 32 - 8 * nb;
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001898 op.ea += 4;
Paul Mackerrasc9f6f4e2014-09-02 14:35:09 +10001899 ++rd;
1900 }
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001901 goto instr_done;
1902
1903 case STORE:
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001904 if ((op.type & UPDATE) && size == sizeof(long) &&
1905 op.reg == 1 && op.update_reg == 1 &&
1906 !(regs->msr & MSR_PR) &&
1907 op.ea >= regs->gpr[1] - STACK_INT_FRAME_SIZE) {
1908 err = handle_stack_update(op.ea, regs);
1909 goto ldst_done;
1910 }
1911 err = write_mem(op.val, op.ea, size, regs);
1912 goto ldst_done;
1913
Paul Mackerras7048c842014-11-03 15:46:43 +11001914#ifdef CONFIG_PPC_FPU
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001915 case STORE_FP:
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001916 if (size == 4)
1917 err = do_fp_store(op.reg, do_stfs, op.ea, size, regs);
1918 else
1919 err = do_fp_store(op.reg, do_stfd, op.ea, size, regs);
1920 goto ldst_done;
Paul Mackerras7048c842014-11-03 15:46:43 +11001921#endif
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001922#ifdef CONFIG_ALTIVEC
1923 case STORE_VMX:
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001924 err = do_vec_store(op.reg, do_stvx, op.ea & ~0xfUL, regs);
1925 goto ldst_done;
1926#endif
1927#ifdef CONFIG_VSX
1928 case STORE_VSX:
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001929 err = do_vsx_store(op.reg, do_stxvd2x, op.ea, regs);
1930 goto ldst_done;
1931#endif
1932 case STORE_MULTI:
1933 if (regs->msr & MSR_LE)
1934 return 0;
1935 rd = op.reg;
Paul Mackerrasc9f6f4e2014-09-02 14:35:09 +10001936 for (i = 0; i < size; i += 4) {
1937 val = regs->gpr[rd];
1938 nb = size - i;
1939 if (nb > 4)
1940 nb = 4;
1941 else
1942 val >>= 32 - 8 * nb;
1943 err = write_mem(val, op.ea, nb, regs);
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001944 if (err)
1945 return 0;
1946 op.ea += 4;
Paul Mackerrasc9f6f4e2014-09-02 14:35:09 +10001947 ++rd;
1948 }
Paul Mackerrasbe96f632014-09-02 14:35:07 +10001949 goto instr_done;
1950
1951 case MFMSR:
1952 regs->gpr[op.reg] = regs->msr & MSR_MASK;
1953 goto instr_done;
1954
1955 case MTMSR:
1956 val = regs->gpr[op.reg];
1957 if ((val & MSR_RI) == 0)
1958 /* can't step mtmsr[d] that would clear MSR_RI */
1959 return -1;
1960 /* here op.val is the mask of bits to change */
1961 regs->msr = (regs->msr & ~op.val) | (val & op.val);
1962 goto instr_done;
1963
1964#ifdef CONFIG_PPC64
1965 case SYSCALL: /* sc */
1966 /*
1967 * N.B. this uses knowledge about how the syscall
1968 * entry code works. If that is changed, this will
1969 * need to be changed also.
1970 */
1971 if (regs->gpr[0] == 0x1ebe &&
1972 cpu_has_feature(CPU_FTR_REAL_LE)) {
1973 regs->msr ^= MSR_LE;
1974 goto instr_done;
1975 }
1976 regs->gpr[9] = regs->gpr[13];
1977 regs->gpr[10] = MSR_KERNEL;
1978 regs->gpr[11] = regs->nip + 4;
1979 regs->gpr[12] = regs->msr & MSR_MASK;
1980 regs->gpr[13] = (unsigned long) get_paca();
1981 regs->nip = (unsigned long) &system_call_common;
1982 regs->msr = MSR_KERNEL;
1983 return 1;
1984
1985 case RFI:
1986 return -1;
1987#endif
1988 }
1989 return 0;
1990
1991 ldst_done:
1992 if (err)
1993 return 0;
1994 if (op.type & UPDATE)
1995 regs->gpr[op.update_reg] = op.ea;
1996
1997 instr_done:
1998 regs->nip = truncate_if_32bit(regs->msr, regs->nip + 4);
1999 return 1;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002000}