sstep.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010
  1. /*
  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>
  12. #include <linux/kprobes.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/prefetch.h>
  15. #include <asm/sstep.h>
  16. #include <asm/processor.h>
  17. #include <asm/uaccess.h>
  18. #include <asm/cputable.h>
  19. extern char system_call_common[];
  20. #ifdef CONFIG_PPC64
  21. /* Bits in SRR1 that are copied from MSR */
  22. #define MSR_MASK 0xffffffff87c0ffffUL
  23. #else
  24. #define MSR_MASK 0x87c0ffff
  25. #endif
  26. /* Bits in XER */
  27. #define XER_SO 0x80000000U
  28. #define XER_OV 0x40000000U
  29. #define XER_CA 0x20000000U
  30. #ifdef CONFIG_PPC_FPU
  31. /*
  32. * Functions in ldstfp.S
  33. */
  34. extern int do_lfs(int rn, unsigned long ea);
  35. extern int do_lfd(int rn, unsigned long ea);
  36. extern int do_stfs(int rn, unsigned long ea);
  37. extern int do_stfd(int rn, unsigned long ea);
  38. extern int do_lvx(int rn, unsigned long ea);
  39. extern int do_stvx(int rn, unsigned long ea);
  40. extern int do_lxvd2x(int rn, unsigned long ea);
  41. extern int do_stxvd2x(int rn, unsigned long ea);
  42. #endif
  43. /*
  44. * Emulate the truncation of 64 bit values in 32-bit mode.
  45. */
  46. static unsigned long truncate_if_32bit(unsigned long msr, unsigned long val)
  47. {
  48. #ifdef __powerpc64__
  49. if ((msr & MSR_64BIT) == 0)
  50. val &= 0xffffffffUL;
  51. #endif
  52. return val;
  53. }
  54. /*
  55. * Determine whether a conditional branch instruction would branch.
  56. */
  57. static int __kprobes branch_taken(unsigned int instr, struct pt_regs *regs)
  58. {
  59. unsigned int bo = (instr >> 21) & 0x1f;
  60. unsigned int bi;
  61. if ((bo & 4) == 0) {
  62. /* decrement counter */
  63. --regs->ctr;
  64. if (((bo >> 1) & 1) ^ (regs->ctr == 0))
  65. return 0;
  66. }
  67. if ((bo & 0x10) == 0) {
  68. /* check bit from CR */
  69. bi = (instr >> 16) & 0x1f;
  70. if (((regs->ccr >> (31 - bi)) & 1) != ((bo >> 3) & 1))
  71. return 0;
  72. }
  73. return 1;
  74. }
  75. static long __kprobes address_ok(struct pt_regs *regs, unsigned long ea, int nb)
  76. {
  77. if (!user_mode(regs))
  78. return 1;
  79. return __access_ok(ea, nb, USER_DS);
  80. }
  81. /*
  82. * Calculate effective address for a D-form instruction
  83. */
  84. static unsigned long __kprobes dform_ea(unsigned int instr, struct pt_regs *regs)
  85. {
  86. int ra;
  87. unsigned long ea;
  88. ra = (instr >> 16) & 0x1f;
  89. ea = (signed short) instr; /* sign-extend */
  90. if (ra)
  91. ea += regs->gpr[ra];
  92. return truncate_if_32bit(regs->msr, ea);
  93. }
  94. #ifdef __powerpc64__
  95. /*
  96. * Calculate effective address for a DS-form instruction
  97. */
  98. static unsigned long __kprobes dsform_ea(unsigned int instr, struct pt_regs *regs)
  99. {
  100. int ra;
  101. unsigned long ea;
  102. ra = (instr >> 16) & 0x1f;
  103. ea = (signed short) (instr & ~3); /* sign-extend */
  104. if (ra)
  105. ea += regs->gpr[ra];
  106. return truncate_if_32bit(regs->msr, ea);
  107. }
  108. #endif /* __powerpc64 */
  109. /*
  110. * Calculate effective address for an X-form instruction
  111. */
  112. static unsigned long __kprobes xform_ea(unsigned int instr,
  113. struct pt_regs *regs)
  114. {
  115. int ra, rb;
  116. unsigned long ea;
  117. ra = (instr >> 16) & 0x1f;
  118. rb = (instr >> 11) & 0x1f;
  119. ea = regs->gpr[rb];
  120. if (ra)
  121. ea += regs->gpr[ra];
  122. return truncate_if_32bit(regs->msr, ea);
  123. }
  124. /*
  125. * Return the largest power of 2, not greater than sizeof(unsigned long),
  126. * such that x is a multiple of it.
  127. */
  128. static inline unsigned long max_align(unsigned long x)
  129. {
  130. x |= sizeof(unsigned long);
  131. return x & -x; /* isolates rightmost bit */
  132. }
  133. static inline unsigned long byterev_2(unsigned long x)
  134. {
  135. return ((x >> 8) & 0xff) | ((x & 0xff) << 8);
  136. }
  137. static inline unsigned long byterev_4(unsigned long x)
  138. {
  139. return ((x >> 24) & 0xff) | ((x >> 8) & 0xff00) |
  140. ((x & 0xff00) << 8) | ((x & 0xff) << 24);
  141. }
  142. #ifdef __powerpc64__
  143. static inline unsigned long byterev_8(unsigned long x)
  144. {
  145. return (byterev_4(x) << 32) | byterev_4(x >> 32);
  146. }
  147. #endif
  148. static int __kprobes read_mem_aligned(unsigned long *dest, unsigned long ea,
  149. int nb)
  150. {
  151. int err = 0;
  152. unsigned long x = 0;
  153. switch (nb) {
  154. case 1:
  155. err = __get_user(x, (unsigned char __user *) ea);
  156. break;
  157. case 2:
  158. err = __get_user(x, (unsigned short __user *) ea);
  159. break;
  160. case 4:
  161. err = __get_user(x, (unsigned int __user *) ea);
  162. break;
  163. #ifdef __powerpc64__
  164. case 8:
  165. err = __get_user(x, (unsigned long __user *) ea);
  166. break;
  167. #endif
  168. }
  169. if (!err)
  170. *dest = x;
  171. return err;
  172. }
  173. static int __kprobes read_mem_unaligned(unsigned long *dest, unsigned long ea,
  174. int nb, struct pt_regs *regs)
  175. {
  176. int err;
  177. unsigned long x, b, c;
  178. #ifdef __LITTLE_ENDIAN__
  179. int len = nb; /* save a copy of the length for byte reversal */
  180. #endif
  181. /* unaligned, do this in pieces */
  182. x = 0;
  183. for (; nb > 0; nb -= c) {
  184. #ifdef __LITTLE_ENDIAN__
  185. c = 1;
  186. #endif
  187. #ifdef __BIG_ENDIAN__
  188. c = max_align(ea);
  189. #endif
  190. if (c > nb)
  191. c = max_align(nb);
  192. err = read_mem_aligned(&b, ea, c);
  193. if (err)
  194. return err;
  195. x = (x << (8 * c)) + b;
  196. ea += c;
  197. }
  198. #ifdef __LITTLE_ENDIAN__
  199. switch (len) {
  200. case 2:
  201. *dest = byterev_2(x);
  202. break;
  203. case 4:
  204. *dest = byterev_4(x);
  205. break;
  206. #ifdef __powerpc64__
  207. case 8:
  208. *dest = byterev_8(x);
  209. break;
  210. #endif
  211. }
  212. #endif
  213. #ifdef __BIG_ENDIAN__
  214. *dest = x;
  215. #endif
  216. return 0;
  217. }
  218. /*
  219. * Read memory at address ea for nb bytes, return 0 for success
  220. * or -EFAULT if an error occurred.
  221. */
  222. static int __kprobes read_mem(unsigned long *dest, unsigned long ea, int nb,
  223. struct pt_regs *regs)
  224. {
  225. if (!address_ok(regs, ea, nb))
  226. return -EFAULT;
  227. if ((ea & (nb - 1)) == 0)
  228. return read_mem_aligned(dest, ea, nb);
  229. return read_mem_unaligned(dest, ea, nb, regs);
  230. }
  231. static int __kprobes write_mem_aligned(unsigned long val, unsigned long ea,
  232. int nb)
  233. {
  234. int err = 0;
  235. switch (nb) {
  236. case 1:
  237. err = __put_user(val, (unsigned char __user *) ea);
  238. break;
  239. case 2:
  240. err = __put_user(val, (unsigned short __user *) ea);
  241. break;
  242. case 4:
  243. err = __put_user(val, (unsigned int __user *) ea);
  244. break;
  245. #ifdef __powerpc64__
  246. case 8:
  247. err = __put_user(val, (unsigned long __user *) ea);
  248. break;
  249. #endif
  250. }
  251. return err;
  252. }
  253. static int __kprobes write_mem_unaligned(unsigned long val, unsigned long ea,
  254. int nb, struct pt_regs *regs)
  255. {
  256. int err;
  257. unsigned long c;
  258. #ifdef __LITTLE_ENDIAN__
  259. switch (nb) {
  260. case 2:
  261. val = byterev_2(val);
  262. break;
  263. case 4:
  264. val = byterev_4(val);
  265. break;
  266. #ifdef __powerpc64__
  267. case 8:
  268. val = byterev_8(val);
  269. break;
  270. #endif
  271. }
  272. #endif
  273. /* unaligned or little-endian, do this in pieces */
  274. for (; nb > 0; nb -= c) {
  275. #ifdef __LITTLE_ENDIAN__
  276. c = 1;
  277. #endif
  278. #ifdef __BIG_ENDIAN__
  279. c = max_align(ea);
  280. #endif
  281. if (c > nb)
  282. c = max_align(nb);
  283. err = write_mem_aligned(val >> (nb - c) * 8, ea, c);
  284. if (err)
  285. return err;
  286. ea += c;
  287. }
  288. return 0;
  289. }
  290. /*
  291. * Write memory at address ea for nb bytes, return 0 for success
  292. * or -EFAULT if an error occurred.
  293. */
  294. static int __kprobes write_mem(unsigned long val, unsigned long ea, int nb,
  295. struct pt_regs *regs)
  296. {
  297. if (!address_ok(regs, ea, nb))
  298. return -EFAULT;
  299. if ((ea & (nb - 1)) == 0)
  300. return write_mem_aligned(val, ea, nb);
  301. return write_mem_unaligned(val, ea, nb, regs);
  302. }
  303. #ifdef CONFIG_PPC_FPU
  304. /*
  305. * Check the address and alignment, and call func to do the actual
  306. * load or store.
  307. */
  308. static int __kprobes do_fp_load(int rn, int (*func)(int, unsigned long),
  309. unsigned long ea, int nb,
  310. struct pt_regs *regs)
  311. {
  312. int err;
  313. union {
  314. double dbl;
  315. unsigned long ul[2];
  316. struct {
  317. #ifdef __BIG_ENDIAN__
  318. unsigned _pad_;
  319. unsigned word;
  320. #endif
  321. #ifdef __LITTLE_ENDIAN__
  322. unsigned word;
  323. unsigned _pad_;
  324. #endif
  325. } single;
  326. } data;
  327. unsigned long ptr;
  328. if (!address_ok(regs, ea, nb))
  329. return -EFAULT;
  330. if ((ea & 3) == 0)
  331. return (*func)(rn, ea);
  332. ptr = (unsigned long) &data.ul;
  333. if (sizeof(unsigned long) == 8 || nb == 4) {
  334. err = read_mem_unaligned(&data.ul[0], ea, nb, regs);
  335. if (nb == 4)
  336. ptr = (unsigned long)&(data.single.word);
  337. } else {
  338. /* reading a double on 32-bit */
  339. err = read_mem_unaligned(&data.ul[0], ea, 4, regs);
  340. if (!err)
  341. err = read_mem_unaligned(&data.ul[1], ea + 4, 4, regs);
  342. }
  343. if (err)
  344. return err;
  345. return (*func)(rn, ptr);
  346. }
  347. static int __kprobes do_fp_store(int rn, int (*func)(int, unsigned long),
  348. unsigned long ea, int nb,
  349. struct pt_regs *regs)
  350. {
  351. int err;
  352. union {
  353. double dbl;
  354. unsigned long ul[2];
  355. struct {
  356. #ifdef __BIG_ENDIAN__
  357. unsigned _pad_;
  358. unsigned word;
  359. #endif
  360. #ifdef __LITTLE_ENDIAN__
  361. unsigned word;
  362. unsigned _pad_;
  363. #endif
  364. } single;
  365. } data;
  366. unsigned long ptr;
  367. if (!address_ok(regs, ea, nb))
  368. return -EFAULT;
  369. if ((ea & 3) == 0)
  370. return (*func)(rn, ea);
  371. ptr = (unsigned long) &data.ul[0];
  372. if (sizeof(unsigned long) == 8 || nb == 4) {
  373. if (nb == 4)
  374. ptr = (unsigned long)&(data.single.word);
  375. err = (*func)(rn, ptr);
  376. if (err)
  377. return err;
  378. err = write_mem_unaligned(data.ul[0], ea, nb, regs);
  379. } else {
  380. /* writing a double on 32-bit */
  381. err = (*func)(rn, ptr);
  382. if (err)
  383. return err;
  384. err = write_mem_unaligned(data.ul[0], ea, 4, regs);
  385. if (!err)
  386. err = write_mem_unaligned(data.ul[1], ea + 4, 4, regs);
  387. }
  388. return err;
  389. }
  390. #endif
  391. #ifdef CONFIG_ALTIVEC
  392. /* For Altivec/VMX, no need to worry about alignment */
  393. static int __kprobes do_vec_load(int rn, int (*func)(int, unsigned long),
  394. unsigned long ea, struct pt_regs *regs)
  395. {
  396. if (!address_ok(regs, ea & ~0xfUL, 16))
  397. return -EFAULT;
  398. return (*func)(rn, ea);
  399. }
  400. static int __kprobes do_vec_store(int rn, int (*func)(int, unsigned long),
  401. unsigned long ea, struct pt_regs *regs)
  402. {
  403. if (!address_ok(regs, ea & ~0xfUL, 16))
  404. return -EFAULT;
  405. return (*func)(rn, ea);
  406. }
  407. #endif /* CONFIG_ALTIVEC */
  408. #ifdef CONFIG_VSX
  409. static int __kprobes do_vsx_load(int rn, int (*func)(int, unsigned long),
  410. unsigned long ea, struct pt_regs *regs)
  411. {
  412. int err;
  413. unsigned long val[2];
  414. if (!address_ok(regs, ea, 16))
  415. return -EFAULT;
  416. if ((ea & 3) == 0)
  417. return (*func)(rn, ea);
  418. err = read_mem_unaligned(&val[0], ea, 8, regs);
  419. if (!err)
  420. err = read_mem_unaligned(&val[1], ea + 8, 8, regs);
  421. if (!err)
  422. err = (*func)(rn, (unsigned long) &val[0]);
  423. return err;
  424. }
  425. static int __kprobes do_vsx_store(int rn, int (*func)(int, unsigned long),
  426. unsigned long ea, struct pt_regs *regs)
  427. {
  428. int err;
  429. unsigned long val[2];
  430. if (!address_ok(regs, ea, 16))
  431. return -EFAULT;
  432. if ((ea & 3) == 0)
  433. return (*func)(rn, ea);
  434. err = (*func)(rn, (unsigned long) &val[0]);
  435. if (err)
  436. return err;
  437. err = write_mem_unaligned(val[0], ea, 8, regs);
  438. if (!err)
  439. err = write_mem_unaligned(val[1], ea + 8, 8, regs);
  440. return err;
  441. }
  442. #endif /* CONFIG_VSX */
  443. #define __put_user_asmx(x, addr, err, op, cr) \
  444. __asm__ __volatile__( \
  445. "1: " op " %2,0,%3\n" \
  446. " mfcr %1\n" \
  447. "2:\n" \
  448. ".section .fixup,\"ax\"\n" \
  449. "3: li %0,%4\n" \
  450. " b 2b\n" \
  451. ".previous\n" \
  452. ".section __ex_table,\"a\"\n" \
  453. PPC_LONG_ALIGN "\n" \
  454. PPC_LONG "1b,3b\n" \
  455. ".previous" \
  456. : "=r" (err), "=r" (cr) \
  457. : "r" (x), "r" (addr), "i" (-EFAULT), "0" (err))
  458. #define __get_user_asmx(x, addr, err, op) \
  459. __asm__ __volatile__( \
  460. "1: "op" %1,0,%2\n" \
  461. "2:\n" \
  462. ".section .fixup,\"ax\"\n" \
  463. "3: li %0,%3\n" \
  464. " b 2b\n" \
  465. ".previous\n" \
  466. ".section __ex_table,\"a\"\n" \
  467. PPC_LONG_ALIGN "\n" \
  468. PPC_LONG "1b,3b\n" \
  469. ".previous" \
  470. : "=r" (err), "=r" (x) \
  471. : "r" (addr), "i" (-EFAULT), "0" (err))
  472. #define __cacheop_user_asmx(addr, err, op) \
  473. __asm__ __volatile__( \
  474. "1: "op" 0,%1\n" \
  475. "2:\n" \
  476. ".section .fixup,\"ax\"\n" \
  477. "3: li %0,%3\n" \
  478. " b 2b\n" \
  479. ".previous\n" \
  480. ".section __ex_table,\"a\"\n" \
  481. PPC_LONG_ALIGN "\n" \
  482. PPC_LONG "1b,3b\n" \
  483. ".previous" \
  484. : "=r" (err) \
  485. : "r" (addr), "i" (-EFAULT), "0" (err))
  486. static void __kprobes set_cr0(struct pt_regs *regs, int rd)
  487. {
  488. long val = regs->gpr[rd];
  489. regs->ccr = (regs->ccr & 0x0fffffff) | ((regs->xer >> 3) & 0x10000000);
  490. #ifdef __powerpc64__
  491. if (!(regs->msr & MSR_64BIT))
  492. val = (int) val;
  493. #endif
  494. if (val < 0)
  495. regs->ccr |= 0x80000000;
  496. else if (val > 0)
  497. regs->ccr |= 0x40000000;
  498. else
  499. regs->ccr |= 0x20000000;
  500. }
  501. static void __kprobes add_with_carry(struct pt_regs *regs, int rd,
  502. unsigned long val1, unsigned long val2,
  503. unsigned long carry_in)
  504. {
  505. unsigned long val = val1 + val2;
  506. if (carry_in)
  507. ++val;
  508. regs->gpr[rd] = val;
  509. #ifdef __powerpc64__
  510. if (!(regs->msr & MSR_64BIT)) {
  511. val = (unsigned int) val;
  512. val1 = (unsigned int) val1;
  513. }
  514. #endif
  515. if (val < val1 || (carry_in && val == val1))
  516. regs->xer |= XER_CA;
  517. else
  518. regs->xer &= ~XER_CA;
  519. }
  520. static void __kprobes do_cmp_signed(struct pt_regs *regs, long v1, long v2,
  521. int crfld)
  522. {
  523. unsigned int crval, shift;
  524. crval = (regs->xer >> 31) & 1; /* get SO bit */
  525. if (v1 < v2)
  526. crval |= 8;
  527. else if (v1 > v2)
  528. crval |= 4;
  529. else
  530. crval |= 2;
  531. shift = (7 - crfld) * 4;
  532. regs->ccr = (regs->ccr & ~(0xf << shift)) | (crval << shift);
  533. }
  534. static void __kprobes do_cmp_unsigned(struct pt_regs *regs, unsigned long v1,
  535. unsigned long v2, int crfld)
  536. {
  537. unsigned int crval, shift;
  538. crval = (regs->xer >> 31) & 1; /* get SO bit */
  539. if (v1 < v2)
  540. crval |= 8;
  541. else if (v1 > v2)
  542. crval |= 4;
  543. else
  544. crval |= 2;
  545. shift = (7 - crfld) * 4;
  546. regs->ccr = (regs->ccr & ~(0xf << shift)) | (crval << shift);
  547. }
  548. static int __kprobes trap_compare(long v1, long v2)
  549. {
  550. int ret = 0;
  551. if (v1 < v2)
  552. ret |= 0x10;
  553. else if (v1 > v2)
  554. ret |= 0x08;
  555. else
  556. ret |= 0x04;
  557. if ((unsigned long)v1 < (unsigned long)v2)
  558. ret |= 0x02;
  559. else if ((unsigned long)v1 > (unsigned long)v2)
  560. ret |= 0x01;
  561. return ret;
  562. }
  563. /*
  564. * Elements of 32-bit rotate and mask instructions.
  565. */
  566. #define MASK32(mb, me) ((0xffffffffUL >> (mb)) + \
  567. ((signed long)-0x80000000L >> (me)) + ((me) >= (mb)))
  568. #ifdef __powerpc64__
  569. #define MASK64_L(mb) (~0UL >> (mb))
  570. #define MASK64_R(me) ((signed long)-0x8000000000000000L >> (me))
  571. #define MASK64(mb, me) (MASK64_L(mb) + MASK64_R(me) + ((me) >= (mb)))
  572. #define DATA32(x) (((x) & 0xffffffffUL) | (((x) & 0xffffffffUL) << 32))
  573. #else
  574. #define DATA32(x) (x)
  575. #endif
  576. #define ROTATE(x, n) ((n) ? (((x) << (n)) | ((x) >> (8 * sizeof(long) - (n)))) : (x))
  577. /*
  578. * Decode an instruction, and execute it if that can be done just by
  579. * modifying *regs (i.e. integer arithmetic and logical instructions,
  580. * branches, and barrier instructions).
  581. * Returns 1 if the instruction has been executed, or 0 if not.
  582. * Sets *op to indicate what the instruction does.
  583. */
  584. int __kprobes analyse_instr(struct instruction_op *op, struct pt_regs *regs,
  585. unsigned int instr)
  586. {
  587. unsigned int opcode, ra, rb, rd, spr, u;
  588. unsigned long int imm;
  589. unsigned long int val, val2;
  590. unsigned int mb, me, sh;
  591. long ival;
  592. op->type = COMPUTE;
  593. opcode = instr >> 26;
  594. switch (opcode) {
  595. case 16: /* bc */
  596. op->type = BRANCH;
  597. imm = (signed short)(instr & 0xfffc);
  598. if ((instr & 2) == 0)
  599. imm += regs->nip;
  600. regs->nip += 4;
  601. regs->nip = truncate_if_32bit(regs->msr, regs->nip);
  602. if (instr & 1)
  603. regs->link = regs->nip;
  604. if (branch_taken(instr, regs))
  605. regs->nip = truncate_if_32bit(regs->msr, imm);
  606. return 1;
  607. #ifdef CONFIG_PPC64
  608. case 17: /* sc */
  609. if ((instr & 0xfe2) == 2)
  610. op->type = SYSCALL;
  611. else
  612. op->type = UNKNOWN;
  613. return 0;
  614. #endif
  615. case 18: /* b */
  616. op->type = BRANCH;
  617. imm = instr & 0x03fffffc;
  618. if (imm & 0x02000000)
  619. imm -= 0x04000000;
  620. if ((instr & 2) == 0)
  621. imm += regs->nip;
  622. if (instr & 1)
  623. regs->link = truncate_if_32bit(regs->msr, regs->nip + 4);
  624. imm = truncate_if_32bit(regs->msr, imm);
  625. regs->nip = imm;
  626. return 1;
  627. case 19:
  628. switch ((instr >> 1) & 0x3ff) {
  629. case 0: /* mcrf */
  630. rd = 7 - ((instr >> 23) & 0x7);
  631. ra = 7 - ((instr >> 18) & 0x7);
  632. rd *= 4;
  633. ra *= 4;
  634. val = (regs->ccr >> ra) & 0xf;
  635. regs->ccr = (regs->ccr & ~(0xfUL << rd)) | (val << rd);
  636. goto instr_done;
  637. case 16: /* bclr */
  638. case 528: /* bcctr */
  639. op->type = BRANCH;
  640. imm = (instr & 0x400)? regs->ctr: regs->link;
  641. regs->nip = truncate_if_32bit(regs->msr, regs->nip + 4);
  642. imm = truncate_if_32bit(regs->msr, imm);
  643. if (instr & 1)
  644. regs->link = regs->nip;
  645. if (branch_taken(instr, regs))
  646. regs->nip = imm;
  647. return 1;
  648. case 18: /* rfid, scary */
  649. if (regs->msr & MSR_PR)
  650. goto priv;
  651. op->type = RFI;
  652. return 0;
  653. case 150: /* isync */
  654. op->type = BARRIER;
  655. isync();
  656. goto instr_done;
  657. case 33: /* crnor */
  658. case 129: /* crandc */
  659. case 193: /* crxor */
  660. case 225: /* crnand */
  661. case 257: /* crand */
  662. case 289: /* creqv */
  663. case 417: /* crorc */
  664. case 449: /* cror */
  665. ra = (instr >> 16) & 0x1f;
  666. rb = (instr >> 11) & 0x1f;
  667. rd = (instr >> 21) & 0x1f;
  668. ra = (regs->ccr >> (31 - ra)) & 1;
  669. rb = (regs->ccr >> (31 - rb)) & 1;
  670. val = (instr >> (6 + ra * 2 + rb)) & 1;
  671. regs->ccr = (regs->ccr & ~(1UL << (31 - rd))) |
  672. (val << (31 - rd));
  673. goto instr_done;
  674. }
  675. break;
  676. case 31:
  677. switch ((instr >> 1) & 0x3ff) {
  678. case 598: /* sync */
  679. op->type = BARRIER;
  680. #ifdef __powerpc64__
  681. switch ((instr >> 21) & 3) {
  682. case 1: /* lwsync */
  683. asm volatile("lwsync" : : : "memory");
  684. goto instr_done;
  685. case 2: /* ptesync */
  686. asm volatile("ptesync" : : : "memory");
  687. goto instr_done;
  688. }
  689. #endif
  690. mb();
  691. goto instr_done;
  692. case 854: /* eieio */
  693. op->type = BARRIER;
  694. eieio();
  695. goto instr_done;
  696. }
  697. break;
  698. }
  699. /* Following cases refer to regs->gpr[], so we need all regs */
  700. if (!FULL_REGS(regs))
  701. return 0;
  702. rd = (instr >> 21) & 0x1f;
  703. ra = (instr >> 16) & 0x1f;
  704. rb = (instr >> 11) & 0x1f;
  705. switch (opcode) {
  706. #ifdef __powerpc64__
  707. case 2: /* tdi */
  708. if (rd & trap_compare(regs->gpr[ra], (short) instr))
  709. goto trap;
  710. goto instr_done;
  711. #endif
  712. case 3: /* twi */
  713. if (rd & trap_compare((int)regs->gpr[ra], (short) instr))
  714. goto trap;
  715. goto instr_done;
  716. case 7: /* mulli */
  717. regs->gpr[rd] = regs->gpr[ra] * (short) instr;
  718. goto instr_done;
  719. case 8: /* subfic */
  720. imm = (short) instr;
  721. add_with_carry(regs, rd, ~regs->gpr[ra], imm, 1);
  722. goto instr_done;
  723. case 10: /* cmpli */
  724. imm = (unsigned short) instr;
  725. val = regs->gpr[ra];
  726. #ifdef __powerpc64__
  727. if ((rd & 1) == 0)
  728. val = (unsigned int) val;
  729. #endif
  730. do_cmp_unsigned(regs, val, imm, rd >> 2);
  731. goto instr_done;
  732. case 11: /* cmpi */
  733. imm = (short) instr;
  734. val = regs->gpr[ra];
  735. #ifdef __powerpc64__
  736. if ((rd & 1) == 0)
  737. val = (int) val;
  738. #endif
  739. do_cmp_signed(regs, val, imm, rd >> 2);
  740. goto instr_done;
  741. case 12: /* addic */
  742. imm = (short) instr;
  743. add_with_carry(regs, rd, regs->gpr[ra], imm, 0);
  744. goto instr_done;
  745. case 13: /* addic. */
  746. imm = (short) instr;
  747. add_with_carry(regs, rd, regs->gpr[ra], imm, 0);
  748. set_cr0(regs, rd);
  749. goto instr_done;
  750. case 14: /* addi */
  751. imm = (short) instr;
  752. if (ra)
  753. imm += regs->gpr[ra];
  754. regs->gpr[rd] = imm;
  755. goto instr_done;
  756. case 15: /* addis */
  757. imm = ((short) instr) << 16;
  758. if (ra)
  759. imm += regs->gpr[ra];
  760. regs->gpr[rd] = imm;
  761. goto instr_done;
  762. case 20: /* rlwimi */
  763. mb = (instr >> 6) & 0x1f;
  764. me = (instr >> 1) & 0x1f;
  765. val = DATA32(regs->gpr[rd]);
  766. imm = MASK32(mb, me);
  767. regs->gpr[ra] = (regs->gpr[ra] & ~imm) | (ROTATE(val, rb) & imm);
  768. goto logical_done;
  769. case 21: /* rlwinm */
  770. mb = (instr >> 6) & 0x1f;
  771. me = (instr >> 1) & 0x1f;
  772. val = DATA32(regs->gpr[rd]);
  773. regs->gpr[ra] = ROTATE(val, rb) & MASK32(mb, me);
  774. goto logical_done;
  775. case 23: /* rlwnm */
  776. mb = (instr >> 6) & 0x1f;
  777. me = (instr >> 1) & 0x1f;
  778. rb = regs->gpr[rb] & 0x1f;
  779. val = DATA32(regs->gpr[rd]);
  780. regs->gpr[ra] = ROTATE(val, rb) & MASK32(mb, me);
  781. goto logical_done;
  782. case 24: /* ori */
  783. imm = (unsigned short) instr;
  784. regs->gpr[ra] = regs->gpr[rd] | imm;
  785. goto instr_done;
  786. case 25: /* oris */
  787. imm = (unsigned short) instr;
  788. regs->gpr[ra] = regs->gpr[rd] | (imm << 16);
  789. goto instr_done;
  790. case 26: /* xori */
  791. imm = (unsigned short) instr;
  792. regs->gpr[ra] = regs->gpr[rd] ^ imm;
  793. goto instr_done;
  794. case 27: /* xoris */
  795. imm = (unsigned short) instr;
  796. regs->gpr[ra] = regs->gpr[rd] ^ (imm << 16);
  797. goto instr_done;
  798. case 28: /* andi. */
  799. imm = (unsigned short) instr;
  800. regs->gpr[ra] = regs->gpr[rd] & imm;
  801. set_cr0(regs, ra);
  802. goto instr_done;
  803. case 29: /* andis. */
  804. imm = (unsigned short) instr;
  805. regs->gpr[ra] = regs->gpr[rd] & (imm << 16);
  806. set_cr0(regs, ra);
  807. goto instr_done;
  808. #ifdef __powerpc64__
  809. case 30: /* rld* */
  810. mb = ((instr >> 6) & 0x1f) | (instr & 0x20);
  811. val = regs->gpr[rd];
  812. if ((instr & 0x10) == 0) {
  813. sh = rb | ((instr & 2) << 4);
  814. val = ROTATE(val, sh);
  815. switch ((instr >> 2) & 3) {
  816. case 0: /* rldicl */
  817. regs->gpr[ra] = val & MASK64_L(mb);
  818. goto logical_done;
  819. case 1: /* rldicr */
  820. regs->gpr[ra] = val & MASK64_R(mb);
  821. goto logical_done;
  822. case 2: /* rldic */
  823. regs->gpr[ra] = val & MASK64(mb, 63 - sh);
  824. goto logical_done;
  825. case 3: /* rldimi */
  826. imm = MASK64(mb, 63 - sh);
  827. regs->gpr[ra] = (regs->gpr[ra] & ~imm) |
  828. (val & imm);
  829. goto logical_done;
  830. }
  831. } else {
  832. sh = regs->gpr[rb] & 0x3f;
  833. val = ROTATE(val, sh);
  834. switch ((instr >> 1) & 7) {
  835. case 0: /* rldcl */
  836. regs->gpr[ra] = val & MASK64_L(mb);
  837. goto logical_done;
  838. case 1: /* rldcr */
  839. regs->gpr[ra] = val & MASK64_R(mb);
  840. goto logical_done;
  841. }
  842. }
  843. #endif
  844. case 31:
  845. switch ((instr >> 1) & 0x3ff) {
  846. case 4: /* tw */
  847. if (rd == 0x1f ||
  848. (rd & trap_compare((int)regs->gpr[ra],
  849. (int)regs->gpr[rb])))
  850. goto trap;
  851. goto instr_done;
  852. #ifdef __powerpc64__
  853. case 68: /* td */
  854. if (rd & trap_compare(regs->gpr[ra], regs->gpr[rb]))
  855. goto trap;
  856. goto instr_done;
  857. #endif
  858. case 83: /* mfmsr */
  859. if (regs->msr & MSR_PR)
  860. goto priv;
  861. op->type = MFMSR;
  862. op->reg = rd;
  863. return 0;
  864. case 146: /* mtmsr */
  865. if (regs->msr & MSR_PR)
  866. goto priv;
  867. op->type = MTMSR;
  868. op->reg = rd;
  869. op->val = 0xffffffff & ~(MSR_ME | MSR_LE);
  870. return 0;
  871. #ifdef CONFIG_PPC64
  872. case 178: /* mtmsrd */
  873. if (regs->msr & MSR_PR)
  874. goto priv;
  875. op->type = MTMSR;
  876. op->reg = rd;
  877. /* only MSR_EE and MSR_RI get changed if bit 15 set */
  878. /* mtmsrd doesn't change MSR_HV, MSR_ME or MSR_LE */
  879. imm = (instr & 0x10000)? 0x8002: 0xefffffffffffeffeUL;
  880. op->val = imm;
  881. return 0;
  882. #endif
  883. case 19: /* mfcr */
  884. if ((instr >> 20) & 1) {
  885. imm = 0xf0000000UL;
  886. for (sh = 0; sh < 8; ++sh) {
  887. if (instr & (0x80000 >> sh)) {
  888. regs->gpr[rd] = regs->ccr & imm;
  889. break;
  890. }
  891. imm >>= 4;
  892. }
  893. goto instr_done;
  894. }
  895. regs->gpr[rd] = regs->ccr;
  896. regs->gpr[rd] &= 0xffffffffUL;
  897. goto instr_done;
  898. case 144: /* mtcrf */
  899. imm = 0xf0000000UL;
  900. val = regs->gpr[rd];
  901. for (sh = 0; sh < 8; ++sh) {
  902. if (instr & (0x80000 >> sh))
  903. regs->ccr = (regs->ccr & ~imm) |
  904. (val & imm);
  905. imm >>= 4;
  906. }
  907. goto instr_done;
  908. case 339: /* mfspr */
  909. spr = ((instr >> 16) & 0x1f) | ((instr >> 6) & 0x3e0);
  910. switch (spr) {
  911. case SPRN_XER: /* mfxer */
  912. regs->gpr[rd] = regs->xer;
  913. regs->gpr[rd] &= 0xffffffffUL;
  914. goto instr_done;
  915. case SPRN_LR: /* mflr */
  916. regs->gpr[rd] = regs->link;
  917. goto instr_done;
  918. case SPRN_CTR: /* mfctr */
  919. regs->gpr[rd] = regs->ctr;
  920. goto instr_done;
  921. default:
  922. op->type = MFSPR;
  923. op->reg = rd;
  924. op->spr = spr;
  925. return 0;
  926. }
  927. break;
  928. case 467: /* mtspr */
  929. spr = ((instr >> 16) & 0x1f) | ((instr >> 6) & 0x3e0);
  930. switch (spr) {
  931. case SPRN_XER: /* mtxer */
  932. regs->xer = (regs->gpr[rd] & 0xffffffffUL);
  933. goto instr_done;
  934. case SPRN_LR: /* mtlr */
  935. regs->link = regs->gpr[rd];
  936. goto instr_done;
  937. case SPRN_CTR: /* mtctr */
  938. regs->ctr = regs->gpr[rd];
  939. goto instr_done;
  940. default:
  941. op->type = MTSPR;
  942. op->val = regs->gpr[rd];
  943. op->spr = spr;
  944. return 0;
  945. }
  946. break;
  947. /*
  948. * Compare instructions
  949. */
  950. case 0: /* cmp */
  951. val = regs->gpr[ra];
  952. val2 = regs->gpr[rb];
  953. #ifdef __powerpc64__
  954. if ((rd & 1) == 0) {
  955. /* word (32-bit) compare */
  956. val = (int) val;
  957. val2 = (int) val2;
  958. }
  959. #endif
  960. do_cmp_signed(regs, val, val2, rd >> 2);
  961. goto instr_done;
  962. case 32: /* cmpl */
  963. val = regs->gpr[ra];
  964. val2 = regs->gpr[rb];
  965. #ifdef __powerpc64__
  966. if ((rd & 1) == 0) {
  967. /* word (32-bit) compare */
  968. val = (unsigned int) val;
  969. val2 = (unsigned int) val2;
  970. }
  971. #endif
  972. do_cmp_unsigned(regs, val, val2, rd >> 2);
  973. goto instr_done;
  974. /*
  975. * Arithmetic instructions
  976. */
  977. case 8: /* subfc */
  978. add_with_carry(regs, rd, ~regs->gpr[ra],
  979. regs->gpr[rb], 1);
  980. goto arith_done;
  981. #ifdef __powerpc64__
  982. case 9: /* mulhdu */
  983. asm("mulhdu %0,%1,%2" : "=r" (regs->gpr[rd]) :
  984. "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
  985. goto arith_done;
  986. #endif
  987. case 10: /* addc */
  988. add_with_carry(regs, rd, regs->gpr[ra],
  989. regs->gpr[rb], 0);
  990. goto arith_done;
  991. case 11: /* mulhwu */
  992. asm("mulhwu %0,%1,%2" : "=r" (regs->gpr[rd]) :
  993. "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
  994. goto arith_done;
  995. case 40: /* subf */
  996. regs->gpr[rd] = regs->gpr[rb] - regs->gpr[ra];
  997. goto arith_done;
  998. #ifdef __powerpc64__
  999. case 73: /* mulhd */
  1000. asm("mulhd %0,%1,%2" : "=r" (regs->gpr[rd]) :
  1001. "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
  1002. goto arith_done;
  1003. #endif
  1004. case 75: /* mulhw */
  1005. asm("mulhw %0,%1,%2" : "=r" (regs->gpr[rd]) :
  1006. "r" (regs->gpr[ra]), "r" (regs->gpr[rb]));
  1007. goto arith_done;
  1008. case 104: /* neg */
  1009. regs->gpr[rd] = -regs->gpr[ra];
  1010. goto arith_done;
  1011. case 136: /* subfe */
  1012. add_with_carry(regs, rd, ~regs->gpr[ra], regs->gpr[rb],
  1013. regs->xer & XER_CA);
  1014. goto arith_done;
  1015. case 138: /* adde */
  1016. add_with_carry(regs, rd, regs->gpr[ra], regs->gpr[rb],
  1017. regs->xer & XER_CA);
  1018. goto arith_done;
  1019. case 200: /* subfze */
  1020. add_with_carry(regs, rd, ~regs->gpr[ra], 0L,
  1021. regs->xer & XER_CA);
  1022. goto arith_done;
  1023. case 202: /* addze */
  1024. add_with_carry(regs, rd, regs->gpr[ra], 0L,
  1025. regs->xer & XER_CA);
  1026. goto arith_done;
  1027. case 232: /* subfme */
  1028. add_with_carry(regs, rd, ~regs->gpr[ra], -1L,
  1029. regs->xer & XER_CA);
  1030. goto arith_done;
  1031. #ifdef __powerpc64__
  1032. case 233: /* mulld */
  1033. regs->gpr[rd] = regs->gpr[ra] * regs->gpr[rb];
  1034. goto arith_done;
  1035. #endif
  1036. case 234: /* addme */
  1037. add_with_carry(regs, rd, regs->gpr[ra], -1L,
  1038. regs->xer & XER_CA);
  1039. goto arith_done;
  1040. case 235: /* mullw */
  1041. regs->gpr[rd] = (unsigned int) regs->gpr[ra] *
  1042. (unsigned int) regs->gpr[rb];
  1043. goto arith_done;
  1044. case 266: /* add */
  1045. regs->gpr[rd] = regs->gpr[ra] + regs->gpr[rb];
  1046. goto arith_done;
  1047. #ifdef __powerpc64__
  1048. case 457: /* divdu */
  1049. regs->gpr[rd] = regs->gpr[ra] / regs->gpr[rb];
  1050. goto arith_done;
  1051. #endif
  1052. case 459: /* divwu */
  1053. regs->gpr[rd] = (unsigned int) regs->gpr[ra] /
  1054. (unsigned int) regs->gpr[rb];
  1055. goto arith_done;
  1056. #ifdef __powerpc64__
  1057. case 489: /* divd */
  1058. regs->gpr[rd] = (long int) regs->gpr[ra] /
  1059. (long int) regs->gpr[rb];
  1060. goto arith_done;
  1061. #endif
  1062. case 491: /* divw */
  1063. regs->gpr[rd] = (int) regs->gpr[ra] /
  1064. (int) regs->gpr[rb];
  1065. goto arith_done;
  1066. /*
  1067. * Logical instructions
  1068. */
  1069. case 26: /* cntlzw */
  1070. asm("cntlzw %0,%1" : "=r" (regs->gpr[ra]) :
  1071. "r" (regs->gpr[rd]));
  1072. goto logical_done;
  1073. #ifdef __powerpc64__
  1074. case 58: /* cntlzd */
  1075. asm("cntlzd %0,%1" : "=r" (regs->gpr[ra]) :
  1076. "r" (regs->gpr[rd]));
  1077. goto logical_done;
  1078. #endif
  1079. case 28: /* and */
  1080. regs->gpr[ra] = regs->gpr[rd] & regs->gpr[rb];
  1081. goto logical_done;
  1082. case 60: /* andc */
  1083. regs->gpr[ra] = regs->gpr[rd] & ~regs->gpr[rb];
  1084. goto logical_done;
  1085. case 124: /* nor */
  1086. regs->gpr[ra] = ~(regs->gpr[rd] | regs->gpr[rb]);
  1087. goto logical_done;
  1088. case 284: /* xor */
  1089. regs->gpr[ra] = ~(regs->gpr[rd] ^ regs->gpr[rb]);
  1090. goto logical_done;
  1091. case 316: /* xor */
  1092. regs->gpr[ra] = regs->gpr[rd] ^ regs->gpr[rb];
  1093. goto logical_done;
  1094. case 412: /* orc */
  1095. regs->gpr[ra] = regs->gpr[rd] | ~regs->gpr[rb];
  1096. goto logical_done;
  1097. case 444: /* or */
  1098. regs->gpr[ra] = regs->gpr[rd] | regs->gpr[rb];
  1099. goto logical_done;
  1100. case 476: /* nand */
  1101. regs->gpr[ra] = ~(regs->gpr[rd] & regs->gpr[rb]);
  1102. goto logical_done;
  1103. case 922: /* extsh */
  1104. regs->gpr[ra] = (signed short) regs->gpr[rd];
  1105. goto logical_done;
  1106. case 954: /* extsb */
  1107. regs->gpr[ra] = (signed char) regs->gpr[rd];
  1108. goto logical_done;
  1109. #ifdef __powerpc64__
  1110. case 986: /* extsw */
  1111. regs->gpr[ra] = (signed int) regs->gpr[rd];
  1112. goto logical_done;
  1113. #endif
  1114. /*
  1115. * Shift instructions
  1116. */
  1117. case 24: /* slw */
  1118. sh = regs->gpr[rb] & 0x3f;
  1119. if (sh < 32)
  1120. regs->gpr[ra] = (regs->gpr[rd] << sh) & 0xffffffffUL;
  1121. else
  1122. regs->gpr[ra] = 0;
  1123. goto logical_done;
  1124. case 536: /* srw */
  1125. sh = regs->gpr[rb] & 0x3f;
  1126. if (sh < 32)
  1127. regs->gpr[ra] = (regs->gpr[rd] & 0xffffffffUL) >> sh;
  1128. else
  1129. regs->gpr[ra] = 0;
  1130. goto logical_done;
  1131. case 792: /* sraw */
  1132. sh = regs->gpr[rb] & 0x3f;
  1133. ival = (signed int) regs->gpr[rd];
  1134. regs->gpr[ra] = ival >> (sh < 32 ? sh : 31);
  1135. if (ival < 0 && (sh >= 32 || (ival & ((1ul << sh) - 1)) != 0))
  1136. regs->xer |= XER_CA;
  1137. else
  1138. regs->xer &= ~XER_CA;
  1139. goto logical_done;
  1140. case 824: /* srawi */
  1141. sh = rb;
  1142. ival = (signed int) regs->gpr[rd];
  1143. regs->gpr[ra] = ival >> sh;
  1144. if (ival < 0 && (ival & ((1ul << sh) - 1)) != 0)
  1145. regs->xer |= XER_CA;
  1146. else
  1147. regs->xer &= ~XER_CA;
  1148. goto logical_done;
  1149. #ifdef __powerpc64__
  1150. case 27: /* sld */
  1151. sh = regs->gpr[rb] & 0x7f;
  1152. if (sh < 64)
  1153. regs->gpr[ra] = regs->gpr[rd] << sh;
  1154. else
  1155. regs->gpr[ra] = 0;
  1156. goto logical_done;
  1157. case 539: /* srd */
  1158. sh = regs->gpr[rb] & 0x7f;
  1159. if (sh < 64)
  1160. regs->gpr[ra] = regs->gpr[rd] >> sh;
  1161. else
  1162. regs->gpr[ra] = 0;
  1163. goto logical_done;
  1164. case 794: /* srad */
  1165. sh = regs->gpr[rb] & 0x7f;
  1166. ival = (signed long int) regs->gpr[rd];
  1167. regs->gpr[ra] = ival >> (sh < 64 ? sh : 63);
  1168. if (ival < 0 && (sh >= 64 || (ival & ((1ul << sh) - 1)) != 0))
  1169. regs->xer |= XER_CA;
  1170. else
  1171. regs->xer &= ~XER_CA;
  1172. goto logical_done;
  1173. case 826: /* sradi with sh_5 = 0 */
  1174. case 827: /* sradi with sh_5 = 1 */
  1175. sh = rb | ((instr & 2) << 4);
  1176. ival = (signed long int) regs->gpr[rd];
  1177. regs->gpr[ra] = ival >> sh;
  1178. if (ival < 0 && (ival & ((1ul << sh) - 1)) != 0)
  1179. regs->xer |= XER_CA;
  1180. else
  1181. regs->xer &= ~XER_CA;
  1182. goto logical_done;
  1183. #endif /* __powerpc64__ */
  1184. /*
  1185. * Cache instructions
  1186. */
  1187. case 54: /* dcbst */
  1188. op->type = MKOP(CACHEOP, DCBST, 0);
  1189. op->ea = xform_ea(instr, regs);
  1190. return 0;
  1191. case 86: /* dcbf */
  1192. op->type = MKOP(CACHEOP, DCBF, 0);
  1193. op->ea = xform_ea(instr, regs);
  1194. return 0;
  1195. case 246: /* dcbtst */
  1196. op->type = MKOP(CACHEOP, DCBTST, 0);
  1197. op->ea = xform_ea(instr, regs);
  1198. op->reg = rd;
  1199. return 0;
  1200. case 278: /* dcbt */
  1201. op->type = MKOP(CACHEOP, DCBTST, 0);
  1202. op->ea = xform_ea(instr, regs);
  1203. op->reg = rd;
  1204. return 0;
  1205. case 982: /* icbi */
  1206. op->type = MKOP(CACHEOP, ICBI, 0);
  1207. op->ea = xform_ea(instr, regs);
  1208. return 0;
  1209. }
  1210. break;
  1211. }
  1212. /*
  1213. * Loads and stores.
  1214. */
  1215. op->type = UNKNOWN;
  1216. op->update_reg = ra;
  1217. op->reg = rd;
  1218. op->val = regs->gpr[rd];
  1219. u = (instr >> 20) & UPDATE;
  1220. switch (opcode) {
  1221. case 31:
  1222. u = instr & UPDATE;
  1223. op->ea = xform_ea(instr, regs);
  1224. switch ((instr >> 1) & 0x3ff) {
  1225. case 20: /* lwarx */
  1226. op->type = MKOP(LARX, 0, 4);
  1227. break;
  1228. case 150: /* stwcx. */
  1229. op->type = MKOP(STCX, 0, 4);
  1230. break;
  1231. #ifdef __powerpc64__
  1232. case 84: /* ldarx */
  1233. op->type = MKOP(LARX, 0, 8);
  1234. break;
  1235. case 214: /* stdcx. */
  1236. op->type = MKOP(STCX, 0, 8);
  1237. break;
  1238. case 21: /* ldx */
  1239. case 53: /* ldux */
  1240. op->type = MKOP(LOAD, u, 8);
  1241. break;
  1242. #endif
  1243. case 23: /* lwzx */
  1244. case 55: /* lwzux */
  1245. op->type = MKOP(LOAD, u, 4);
  1246. break;
  1247. case 87: /* lbzx */
  1248. case 119: /* lbzux */
  1249. op->type = MKOP(LOAD, u, 1);
  1250. break;
  1251. #ifdef CONFIG_ALTIVEC
  1252. case 103: /* lvx */
  1253. case 359: /* lvxl */
  1254. if (!(regs->msr & MSR_VEC))
  1255. goto vecunavail;
  1256. op->type = MKOP(LOAD_VMX, 0, 16);
  1257. break;
  1258. case 231: /* stvx */
  1259. case 487: /* stvxl */
  1260. if (!(regs->msr & MSR_VEC))
  1261. goto vecunavail;
  1262. op->type = MKOP(STORE_VMX, 0, 16);
  1263. break;
  1264. #endif /* CONFIG_ALTIVEC */
  1265. #ifdef __powerpc64__
  1266. case 149: /* stdx */
  1267. case 181: /* stdux */
  1268. op->type = MKOP(STORE, u, 8);
  1269. break;
  1270. #endif
  1271. case 151: /* stwx */
  1272. case 183: /* stwux */
  1273. op->type = MKOP(STORE, u, 4);
  1274. break;
  1275. case 215: /* stbx */
  1276. case 247: /* stbux */
  1277. op->type = MKOP(STORE, u, 1);
  1278. break;
  1279. case 279: /* lhzx */
  1280. case 311: /* lhzux */
  1281. op->type = MKOP(LOAD, u, 2);
  1282. break;
  1283. #ifdef __powerpc64__
  1284. case 341: /* lwax */
  1285. case 373: /* lwaux */
  1286. op->type = MKOP(LOAD, SIGNEXT | u, 4);
  1287. break;
  1288. #endif
  1289. case 343: /* lhax */
  1290. case 375: /* lhaux */
  1291. op->type = MKOP(LOAD, SIGNEXT | u, 2);
  1292. break;
  1293. case 407: /* sthx */
  1294. case 439: /* sthux */
  1295. op->type = MKOP(STORE, u, 2);
  1296. break;
  1297. #ifdef __powerpc64__
  1298. case 532: /* ldbrx */
  1299. op->type = MKOP(LOAD, BYTEREV, 8);
  1300. break;
  1301. #endif
  1302. case 533: /* lswx */
  1303. op->type = MKOP(LOAD_MULTI, 0, regs->xer & 0x7f);
  1304. break;
  1305. case 534: /* lwbrx */
  1306. op->type = MKOP(LOAD, BYTEREV, 4);
  1307. break;
  1308. case 597: /* lswi */
  1309. if (rb == 0)
  1310. rb = 32; /* # bytes to load */
  1311. op->type = MKOP(LOAD_MULTI, 0, rb);
  1312. op->ea = 0;
  1313. if (ra)
  1314. op->ea = truncate_if_32bit(regs->msr,
  1315. regs->gpr[ra]);
  1316. break;
  1317. #ifdef CONFIG_PPC_FPU
  1318. case 535: /* lfsx */
  1319. case 567: /* lfsux */
  1320. if (!(regs->msr & MSR_FP))
  1321. goto fpunavail;
  1322. op->type = MKOP(LOAD_FP, u, 4);
  1323. break;
  1324. case 599: /* lfdx */
  1325. case 631: /* lfdux */
  1326. if (!(regs->msr & MSR_FP))
  1327. goto fpunavail;
  1328. op->type = MKOP(LOAD_FP, u, 8);
  1329. break;
  1330. case 663: /* stfsx */
  1331. case 695: /* stfsux */
  1332. if (!(regs->msr & MSR_FP))
  1333. goto fpunavail;
  1334. op->type = MKOP(STORE_FP, u, 4);
  1335. break;
  1336. case 727: /* stfdx */
  1337. case 759: /* stfdux */
  1338. if (!(regs->msr & MSR_FP))
  1339. goto fpunavail;
  1340. op->type = MKOP(STORE_FP, u, 8);
  1341. break;
  1342. #endif
  1343. #ifdef __powerpc64__
  1344. case 660: /* stdbrx */
  1345. op->type = MKOP(STORE, BYTEREV, 8);
  1346. op->val = byterev_8(regs->gpr[rd]);
  1347. break;
  1348. #endif
  1349. case 661: /* stswx */
  1350. op->type = MKOP(STORE_MULTI, 0, regs->xer & 0x7f);
  1351. break;
  1352. case 662: /* stwbrx */
  1353. op->type = MKOP(STORE, BYTEREV, 4);
  1354. op->val = byterev_4(regs->gpr[rd]);
  1355. break;
  1356. case 725:
  1357. if (rb == 0)
  1358. rb = 32; /* # bytes to store */
  1359. op->type = MKOP(STORE_MULTI, 0, rb);
  1360. op->ea = 0;
  1361. if (ra)
  1362. op->ea = truncate_if_32bit(regs->msr,
  1363. regs->gpr[ra]);
  1364. break;
  1365. case 790: /* lhbrx */
  1366. op->type = MKOP(LOAD, BYTEREV, 2);
  1367. break;
  1368. case 918: /* sthbrx */
  1369. op->type = MKOP(STORE, BYTEREV, 2);
  1370. op->val = byterev_2(regs->gpr[rd]);
  1371. break;
  1372. #ifdef CONFIG_VSX
  1373. case 844: /* lxvd2x */
  1374. case 876: /* lxvd2ux */
  1375. if (!(regs->msr & MSR_VSX))
  1376. goto vsxunavail;
  1377. op->reg = rd | ((instr & 1) << 5);
  1378. op->type = MKOP(LOAD_VSX, u, 16);
  1379. break;
  1380. case 972: /* stxvd2x */
  1381. case 1004: /* stxvd2ux */
  1382. if (!(regs->msr & MSR_VSX))
  1383. goto vsxunavail;
  1384. op->reg = rd | ((instr & 1) << 5);
  1385. op->type = MKOP(STORE_VSX, u, 16);
  1386. break;
  1387. #endif /* CONFIG_VSX */
  1388. }
  1389. break;
  1390. case 32: /* lwz */
  1391. case 33: /* lwzu */
  1392. op->type = MKOP(LOAD, u, 4);
  1393. op->ea = dform_ea(instr, regs);
  1394. break;
  1395. case 34: /* lbz */
  1396. case 35: /* lbzu */
  1397. op->type = MKOP(LOAD, u, 1);
  1398. op->ea = dform_ea(instr, regs);
  1399. break;
  1400. case 36: /* stw */
  1401. case 37: /* stwu */
  1402. op->type = MKOP(STORE, u, 4);
  1403. op->ea = dform_ea(instr, regs);
  1404. break;
  1405. case 38: /* stb */
  1406. case 39: /* stbu */
  1407. op->type = MKOP(STORE, u, 1);
  1408. op->ea = dform_ea(instr, regs);
  1409. break;
  1410. case 40: /* lhz */
  1411. case 41: /* lhzu */
  1412. op->type = MKOP(LOAD, u, 2);
  1413. op->ea = dform_ea(instr, regs);
  1414. break;
  1415. case 42: /* lha */
  1416. case 43: /* lhau */
  1417. op->type = MKOP(LOAD, SIGNEXT | u, 2);
  1418. op->ea = dform_ea(instr, regs);
  1419. break;
  1420. case 44: /* sth */
  1421. case 45: /* sthu */
  1422. op->type = MKOP(STORE, u, 2);
  1423. op->ea = dform_ea(instr, regs);
  1424. break;
  1425. case 46: /* lmw */
  1426. if (ra >= rd)
  1427. break; /* invalid form, ra in range to load */
  1428. op->type = MKOP(LOAD_MULTI, 0, 4 * (32 - rd));
  1429. op->ea = dform_ea(instr, regs);
  1430. break;
  1431. case 47: /* stmw */
  1432. op->type = MKOP(STORE_MULTI, 0, 4 * (32 - rd));
  1433. op->ea = dform_ea(instr, regs);
  1434. break;
  1435. #ifdef CONFIG_PPC_FPU
  1436. case 48: /* lfs */
  1437. case 49: /* lfsu */
  1438. if (!(regs->msr & MSR_FP))
  1439. goto fpunavail;
  1440. op->type = MKOP(LOAD_FP, u, 4);
  1441. op->ea = dform_ea(instr, regs);
  1442. break;
  1443. case 50: /* lfd */
  1444. case 51: /* lfdu */
  1445. if (!(regs->msr & MSR_FP))
  1446. goto fpunavail;
  1447. op->type = MKOP(LOAD_FP, u, 8);
  1448. op->ea = dform_ea(instr, regs);
  1449. break;
  1450. case 52: /* stfs */
  1451. case 53: /* stfsu */
  1452. if (!(regs->msr & MSR_FP))
  1453. goto fpunavail;
  1454. op->type = MKOP(STORE_FP, u, 4);
  1455. op->ea = dform_ea(instr, regs);
  1456. break;
  1457. case 54: /* stfd */
  1458. case 55: /* stfdu */
  1459. if (!(regs->msr & MSR_FP))
  1460. goto fpunavail;
  1461. op->type = MKOP(STORE_FP, u, 8);
  1462. op->ea = dform_ea(instr, regs);
  1463. break;
  1464. #endif
  1465. #ifdef __powerpc64__
  1466. case 58: /* ld[u], lwa */
  1467. op->ea = dsform_ea(instr, regs);
  1468. switch (instr & 3) {
  1469. case 0: /* ld */
  1470. op->type = MKOP(LOAD, 0, 8);
  1471. break;
  1472. case 1: /* ldu */
  1473. op->type = MKOP(LOAD, UPDATE, 8);
  1474. break;
  1475. case 2: /* lwa */
  1476. op->type = MKOP(LOAD, SIGNEXT, 4);
  1477. break;
  1478. }
  1479. break;
  1480. case 62: /* std[u] */
  1481. op->ea = dsform_ea(instr, regs);
  1482. switch (instr & 3) {
  1483. case 0: /* std */
  1484. op->type = MKOP(STORE, 0, 8);
  1485. break;
  1486. case 1: /* stdu */
  1487. op->type = MKOP(STORE, UPDATE, 8);
  1488. break;
  1489. }
  1490. break;
  1491. #endif /* __powerpc64__ */
  1492. }
  1493. return 0;
  1494. logical_done:
  1495. if (instr & 1)
  1496. set_cr0(regs, ra);
  1497. goto instr_done;
  1498. arith_done:
  1499. if (instr & 1)
  1500. set_cr0(regs, rd);
  1501. instr_done:
  1502. regs->nip = truncate_if_32bit(regs->msr, regs->nip + 4);
  1503. return 1;
  1504. priv:
  1505. op->type = INTERRUPT | 0x700;
  1506. op->val = SRR1_PROGPRIV;
  1507. return 0;
  1508. trap:
  1509. op->type = INTERRUPT | 0x700;
  1510. op->val = SRR1_PROGTRAP;
  1511. return 0;
  1512. #ifdef CONFIG_PPC_FPU
  1513. fpunavail:
  1514. op->type = INTERRUPT | 0x800;
  1515. return 0;
  1516. #endif
  1517. #ifdef CONFIG_ALTIVEC
  1518. vecunavail:
  1519. op->type = INTERRUPT | 0xf20;
  1520. return 0;
  1521. #endif
  1522. #ifdef CONFIG_VSX
  1523. vsxunavail:
  1524. op->type = INTERRUPT | 0xf40;
  1525. return 0;
  1526. #endif
  1527. }
  1528. EXPORT_SYMBOL_GPL(analyse_instr);
  1529. /*
  1530. * For PPC32 we always use stwu with r1 to change the stack pointer.
  1531. * So this emulated store may corrupt the exception frame, now we
  1532. * have to provide the exception frame trampoline, which is pushed
  1533. * below the kprobed function stack. So we only update gpr[1] but
  1534. * don't emulate the real store operation. We will do real store
  1535. * operation safely in exception return code by checking this flag.
  1536. */
  1537. static __kprobes int handle_stack_update(unsigned long ea, struct pt_regs *regs)
  1538. {
  1539. #ifdef CONFIG_PPC32
  1540. /*
  1541. * Check if we will touch kernel stack overflow
  1542. */
  1543. if (ea - STACK_INT_FRAME_SIZE <= current->thread.ksp_limit) {
  1544. printk(KERN_CRIT "Can't kprobe this since kernel stack would overflow.\n");
  1545. return -EINVAL;
  1546. }
  1547. #endif /* CONFIG_PPC32 */
  1548. /*
  1549. * Check if we already set since that means we'll
  1550. * lose the previous value.
  1551. */
  1552. WARN_ON(test_thread_flag(TIF_EMULATE_STACK_STORE));
  1553. set_thread_flag(TIF_EMULATE_STACK_STORE);
  1554. return 0;
  1555. }
  1556. static __kprobes void do_signext(unsigned long *valp, int size)
  1557. {
  1558. switch (size) {
  1559. case 2:
  1560. *valp = (signed short) *valp;
  1561. break;
  1562. case 4:
  1563. *valp = (signed int) *valp;
  1564. break;
  1565. }
  1566. }
  1567. static __kprobes void do_byterev(unsigned long *valp, int size)
  1568. {
  1569. switch (size) {
  1570. case 2:
  1571. *valp = byterev_2(*valp);
  1572. break;
  1573. case 4:
  1574. *valp = byterev_4(*valp);
  1575. break;
  1576. #ifdef __powerpc64__
  1577. case 8:
  1578. *valp = byterev_8(*valp);
  1579. break;
  1580. #endif
  1581. }
  1582. }
  1583. /*
  1584. * Emulate instructions that cause a transfer of control,
  1585. * loads and stores, and a few other instructions.
  1586. * Returns 1 if the step was emulated, 0 if not,
  1587. * or -1 if the instruction is one that should not be stepped,
  1588. * such as an rfid, or a mtmsrd that would clear MSR_RI.
  1589. */
  1590. int __kprobes emulate_step(struct pt_regs *regs, unsigned int instr)
  1591. {
  1592. struct instruction_op op;
  1593. int r, err, size;
  1594. unsigned long val;
  1595. unsigned int cr;
  1596. int i, rd, nb;
  1597. r = analyse_instr(&op, regs, instr);
  1598. if (r != 0)
  1599. return r;
  1600. err = 0;
  1601. size = GETSIZE(op.type);
  1602. switch (op.type & INSTR_TYPE_MASK) {
  1603. case CACHEOP:
  1604. if (!address_ok(regs, op.ea, 8))
  1605. return 0;
  1606. switch (op.type & CACHEOP_MASK) {
  1607. case DCBST:
  1608. __cacheop_user_asmx(op.ea, err, "dcbst");
  1609. break;
  1610. case DCBF:
  1611. __cacheop_user_asmx(op.ea, err, "dcbf");
  1612. break;
  1613. case DCBTST:
  1614. if (op.reg == 0)
  1615. prefetchw((void *) op.ea);
  1616. break;
  1617. case DCBT:
  1618. if (op.reg == 0)
  1619. prefetch((void *) op.ea);
  1620. break;
  1621. case ICBI:
  1622. __cacheop_user_asmx(op.ea, err, "icbi");
  1623. break;
  1624. }
  1625. if (err)
  1626. return 0;
  1627. goto instr_done;
  1628. case LARX:
  1629. if (op.ea & (size - 1))
  1630. break; /* can't handle misaligned */
  1631. err = -EFAULT;
  1632. if (!address_ok(regs, op.ea, size))
  1633. goto ldst_done;
  1634. err = 0;
  1635. switch (size) {
  1636. case 4:
  1637. __get_user_asmx(val, op.ea, err, "lwarx");
  1638. break;
  1639. case 8:
  1640. __get_user_asmx(val, op.ea, err, "ldarx");
  1641. break;
  1642. default:
  1643. return 0;
  1644. }
  1645. if (!err)
  1646. regs->gpr[op.reg] = val;
  1647. goto ldst_done;
  1648. case STCX:
  1649. if (op.ea & (size - 1))
  1650. break; /* can't handle misaligned */
  1651. err = -EFAULT;
  1652. if (!address_ok(regs, op.ea, size))
  1653. goto ldst_done;
  1654. err = 0;
  1655. switch (size) {
  1656. case 4:
  1657. __put_user_asmx(op.val, op.ea, err, "stwcx.", cr);
  1658. break;
  1659. case 8:
  1660. __put_user_asmx(op.val, op.ea, err, "stdcx.", cr);
  1661. break;
  1662. default:
  1663. return 0;
  1664. }
  1665. if (!err)
  1666. regs->ccr = (regs->ccr & 0x0fffffff) |
  1667. (cr & 0xe0000000) |
  1668. ((regs->xer >> 3) & 0x10000000);
  1669. goto ldst_done;
  1670. case LOAD:
  1671. err = read_mem(&regs->gpr[op.reg], op.ea, size, regs);
  1672. if (!err) {
  1673. if (op.type & SIGNEXT)
  1674. do_signext(&regs->gpr[op.reg], size);
  1675. if (op.type & BYTEREV)
  1676. do_byterev(&regs->gpr[op.reg], size);
  1677. }
  1678. goto ldst_done;
  1679. #ifdef CONFIG_PPC_FPU
  1680. case LOAD_FP:
  1681. if (size == 4)
  1682. err = do_fp_load(op.reg, do_lfs, op.ea, size, regs);
  1683. else
  1684. err = do_fp_load(op.reg, do_lfd, op.ea, size, regs);
  1685. goto ldst_done;
  1686. #endif
  1687. #ifdef CONFIG_ALTIVEC
  1688. case LOAD_VMX:
  1689. err = do_vec_load(op.reg, do_lvx, op.ea & ~0xfUL, regs);
  1690. goto ldst_done;
  1691. #endif
  1692. #ifdef CONFIG_VSX
  1693. case LOAD_VSX:
  1694. err = do_vsx_load(op.reg, do_lxvd2x, op.ea, regs);
  1695. goto ldst_done;
  1696. #endif
  1697. case LOAD_MULTI:
  1698. if (regs->msr & MSR_LE)
  1699. return 0;
  1700. rd = op.reg;
  1701. for (i = 0; i < size; i += 4) {
  1702. nb = size - i;
  1703. if (nb > 4)
  1704. nb = 4;
  1705. err = read_mem(&regs->gpr[rd], op.ea, nb, regs);
  1706. if (err)
  1707. return 0;
  1708. if (nb < 4) /* left-justify last bytes */
  1709. regs->gpr[rd] <<= 32 - 8 * nb;
  1710. op.ea += 4;
  1711. ++rd;
  1712. }
  1713. goto instr_done;
  1714. case STORE:
  1715. if ((op.type & UPDATE) && size == sizeof(long) &&
  1716. op.reg == 1 && op.update_reg == 1 &&
  1717. !(regs->msr & MSR_PR) &&
  1718. op.ea >= regs->gpr[1] - STACK_INT_FRAME_SIZE) {
  1719. err = handle_stack_update(op.ea, regs);
  1720. goto ldst_done;
  1721. }
  1722. err = write_mem(op.val, op.ea, size, regs);
  1723. goto ldst_done;
  1724. #ifdef CONFIG_PPC_FPU
  1725. case STORE_FP:
  1726. if (size == 4)
  1727. err = do_fp_store(op.reg, do_stfs, op.ea, size, regs);
  1728. else
  1729. err = do_fp_store(op.reg, do_stfd, op.ea, size, regs);
  1730. goto ldst_done;
  1731. #endif
  1732. #ifdef CONFIG_ALTIVEC
  1733. case STORE_VMX:
  1734. err = do_vec_store(op.reg, do_stvx, op.ea & ~0xfUL, regs);
  1735. goto ldst_done;
  1736. #endif
  1737. #ifdef CONFIG_VSX
  1738. case STORE_VSX:
  1739. err = do_vsx_store(op.reg, do_stxvd2x, op.ea, regs);
  1740. goto ldst_done;
  1741. #endif
  1742. case STORE_MULTI:
  1743. if (regs->msr & MSR_LE)
  1744. return 0;
  1745. rd = op.reg;
  1746. for (i = 0; i < size; i += 4) {
  1747. val = regs->gpr[rd];
  1748. nb = size - i;
  1749. if (nb > 4)
  1750. nb = 4;
  1751. else
  1752. val >>= 32 - 8 * nb;
  1753. err = write_mem(val, op.ea, nb, regs);
  1754. if (err)
  1755. return 0;
  1756. op.ea += 4;
  1757. ++rd;
  1758. }
  1759. goto instr_done;
  1760. case MFMSR:
  1761. regs->gpr[op.reg] = regs->msr & MSR_MASK;
  1762. goto instr_done;
  1763. case MTMSR:
  1764. val = regs->gpr[op.reg];
  1765. if ((val & MSR_RI) == 0)
  1766. /* can't step mtmsr[d] that would clear MSR_RI */
  1767. return -1;
  1768. /* here op.val is the mask of bits to change */
  1769. regs->msr = (regs->msr & ~op.val) | (val & op.val);
  1770. goto instr_done;
  1771. #ifdef CONFIG_PPC64
  1772. case SYSCALL: /* sc */
  1773. /*
  1774. * N.B. this uses knowledge about how the syscall
  1775. * entry code works. If that is changed, this will
  1776. * need to be changed also.
  1777. */
  1778. if (regs->gpr[0] == 0x1ebe &&
  1779. cpu_has_feature(CPU_FTR_REAL_LE)) {
  1780. regs->msr ^= MSR_LE;
  1781. goto instr_done;
  1782. }
  1783. regs->gpr[9] = regs->gpr[13];
  1784. regs->gpr[10] = MSR_KERNEL;
  1785. regs->gpr[11] = regs->nip + 4;
  1786. regs->gpr[12] = regs->msr & MSR_MASK;
  1787. regs->gpr[13] = (unsigned long) get_paca();
  1788. regs->nip = (unsigned long) &system_call_common;
  1789. regs->msr = MSR_KERNEL;
  1790. return 1;
  1791. case RFI:
  1792. return -1;
  1793. #endif
  1794. }
  1795. return 0;
  1796. ldst_done:
  1797. if (err)
  1798. return 0;
  1799. if (op.type & UPDATE)
  1800. regs->gpr[op.update_reg] = op.ea;
  1801. instr_done:
  1802. regs->nip = truncate_if_32bit(regs->msr, regs->nip + 4);
  1803. return 1;
  1804. }