align.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. /* align.c - handle alignment exceptions for the Power PC.
  2. *
  3. * Copyright (c) 1996 Paul Mackerras <paulus@cs.anu.edu.au>
  4. * Copyright (c) 1998-1999 TiVo, Inc.
  5. * PowerPC 403GCX modifications.
  6. * Copyright (c) 1999 Grant Erickson <grant@lcse.umn.edu>
  7. * PowerPC 403GCX/405GP modifications.
  8. * Copyright (c) 2001-2002 PPC64 team, IBM Corp
  9. * 64-bit and Power4 support
  10. * Copyright (c) 2005 Benjamin Herrenschmidt, IBM Corp
  11. * <benh@kernel.crashing.org>
  12. * Merge ppc32 and ppc64 implementations
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/mm.h>
  21. #include <asm/processor.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/cache.h>
  24. #include <asm/cputable.h>
  25. #include <asm/emulated_ops.h>
  26. #include <asm/switch_to.h>
  27. struct aligninfo {
  28. unsigned char len;
  29. unsigned char flags;
  30. };
  31. #define IS_XFORM(inst) (((inst) >> 26) == 31)
  32. #define IS_DSFORM(inst) (((inst) >> 26) >= 56)
  33. #define INVALID { 0, 0 }
  34. /* Bits in the flags field */
  35. #define LD 0 /* load */
  36. #define ST 1 /* store */
  37. #define SE 2 /* sign-extend value, or FP ld/st as word */
  38. #define F 4 /* to/from fp regs */
  39. #define U 8 /* update index register */
  40. #define M 0x10 /* multiple load/store */
  41. #define SW 0x20 /* byte swap */
  42. #define S 0x40 /* single-precision fp or... */
  43. #define SX 0x40 /* ... byte count in XER */
  44. #define HARD 0x80 /* string, stwcx. */
  45. #define E4 0x40 /* SPE endianness is word */
  46. #define E8 0x80 /* SPE endianness is double word */
  47. #define SPLT 0x80 /* VSX SPLAT load */
  48. /* DSISR bits reported for a DCBZ instruction: */
  49. #define DCBZ 0x5f /* 8xx/82xx dcbz faults when cache not enabled */
  50. #define SWAP(a, b) (t = (a), (a) = (b), (b) = t)
  51. /*
  52. * The PowerPC stores certain bits of the instruction that caused the
  53. * alignment exception in the DSISR register. This array maps those
  54. * bits to information about the operand length and what the
  55. * instruction would do.
  56. */
  57. static struct aligninfo aligninfo[128] = {
  58. { 4, LD }, /* 00 0 0000: lwz / lwarx */
  59. INVALID, /* 00 0 0001 */
  60. { 4, ST }, /* 00 0 0010: stw */
  61. INVALID, /* 00 0 0011 */
  62. { 2, LD }, /* 00 0 0100: lhz */
  63. { 2, LD+SE }, /* 00 0 0101: lha */
  64. { 2, ST }, /* 00 0 0110: sth */
  65. { 4, LD+M }, /* 00 0 0111: lmw */
  66. { 4, LD+F+S }, /* 00 0 1000: lfs */
  67. { 8, LD+F }, /* 00 0 1001: lfd */
  68. { 4, ST+F+S }, /* 00 0 1010: stfs */
  69. { 8, ST+F }, /* 00 0 1011: stfd */
  70. INVALID, /* 00 0 1100 */
  71. { 8, LD }, /* 00 0 1101: ld/ldu/lwa */
  72. INVALID, /* 00 0 1110 */
  73. { 8, ST }, /* 00 0 1111: std/stdu */
  74. { 4, LD+U }, /* 00 1 0000: lwzu */
  75. INVALID, /* 00 1 0001 */
  76. { 4, ST+U }, /* 00 1 0010: stwu */
  77. INVALID, /* 00 1 0011 */
  78. { 2, LD+U }, /* 00 1 0100: lhzu */
  79. { 2, LD+SE+U }, /* 00 1 0101: lhau */
  80. { 2, ST+U }, /* 00 1 0110: sthu */
  81. { 4, ST+M }, /* 00 1 0111: stmw */
  82. { 4, LD+F+S+U }, /* 00 1 1000: lfsu */
  83. { 8, LD+F+U }, /* 00 1 1001: lfdu */
  84. { 4, ST+F+S+U }, /* 00 1 1010: stfsu */
  85. { 8, ST+F+U }, /* 00 1 1011: stfdu */
  86. { 16, LD+F }, /* 00 1 1100: lfdp */
  87. INVALID, /* 00 1 1101 */
  88. { 16, ST+F }, /* 00 1 1110: stfdp */
  89. INVALID, /* 00 1 1111 */
  90. { 8, LD }, /* 01 0 0000: ldx */
  91. INVALID, /* 01 0 0001 */
  92. { 8, ST }, /* 01 0 0010: stdx */
  93. INVALID, /* 01 0 0011 */
  94. INVALID, /* 01 0 0100 */
  95. { 4, LD+SE }, /* 01 0 0101: lwax */
  96. INVALID, /* 01 0 0110 */
  97. INVALID, /* 01 0 0111 */
  98. { 4, LD+M+HARD+SX }, /* 01 0 1000: lswx */
  99. { 4, LD+M+HARD }, /* 01 0 1001: lswi */
  100. { 4, ST+M+HARD+SX }, /* 01 0 1010: stswx */
  101. { 4, ST+M+HARD }, /* 01 0 1011: stswi */
  102. INVALID, /* 01 0 1100 */
  103. { 8, LD+U }, /* 01 0 1101: ldu */
  104. INVALID, /* 01 0 1110 */
  105. { 8, ST+U }, /* 01 0 1111: stdu */
  106. { 8, LD+U }, /* 01 1 0000: ldux */
  107. INVALID, /* 01 1 0001 */
  108. { 8, ST+U }, /* 01 1 0010: stdux */
  109. INVALID, /* 01 1 0011 */
  110. INVALID, /* 01 1 0100 */
  111. { 4, LD+SE+U }, /* 01 1 0101: lwaux */
  112. INVALID, /* 01 1 0110 */
  113. INVALID, /* 01 1 0111 */
  114. INVALID, /* 01 1 1000 */
  115. INVALID, /* 01 1 1001 */
  116. INVALID, /* 01 1 1010 */
  117. INVALID, /* 01 1 1011 */
  118. INVALID, /* 01 1 1100 */
  119. INVALID, /* 01 1 1101 */
  120. INVALID, /* 01 1 1110 */
  121. INVALID, /* 01 1 1111 */
  122. INVALID, /* 10 0 0000 */
  123. INVALID, /* 10 0 0001 */
  124. INVALID, /* 10 0 0010: stwcx. */
  125. INVALID, /* 10 0 0011 */
  126. INVALID, /* 10 0 0100 */
  127. INVALID, /* 10 0 0101 */
  128. INVALID, /* 10 0 0110 */
  129. INVALID, /* 10 0 0111 */
  130. { 4, LD+SW }, /* 10 0 1000: lwbrx */
  131. INVALID, /* 10 0 1001 */
  132. { 4, ST+SW }, /* 10 0 1010: stwbrx */
  133. INVALID, /* 10 0 1011 */
  134. { 2, LD+SW }, /* 10 0 1100: lhbrx */
  135. { 4, LD+SE }, /* 10 0 1101 lwa */
  136. { 2, ST+SW }, /* 10 0 1110: sthbrx */
  137. INVALID, /* 10 0 1111 */
  138. INVALID, /* 10 1 0000 */
  139. INVALID, /* 10 1 0001 */
  140. INVALID, /* 10 1 0010 */
  141. INVALID, /* 10 1 0011 */
  142. INVALID, /* 10 1 0100 */
  143. INVALID, /* 10 1 0101 */
  144. INVALID, /* 10 1 0110 */
  145. INVALID, /* 10 1 0111 */
  146. INVALID, /* 10 1 1000 */
  147. INVALID, /* 10 1 1001 */
  148. INVALID, /* 10 1 1010 */
  149. INVALID, /* 10 1 1011 */
  150. INVALID, /* 10 1 1100 */
  151. INVALID, /* 10 1 1101 */
  152. INVALID, /* 10 1 1110 */
  153. { 0, ST+HARD }, /* 10 1 1111: dcbz */
  154. { 4, LD }, /* 11 0 0000: lwzx */
  155. INVALID, /* 11 0 0001 */
  156. { 4, ST }, /* 11 0 0010: stwx */
  157. INVALID, /* 11 0 0011 */
  158. { 2, LD }, /* 11 0 0100: lhzx */
  159. { 2, LD+SE }, /* 11 0 0101: lhax */
  160. { 2, ST }, /* 11 0 0110: sthx */
  161. INVALID, /* 11 0 0111 */
  162. { 4, LD+F+S }, /* 11 0 1000: lfsx */
  163. { 8, LD+F }, /* 11 0 1001: lfdx */
  164. { 4, ST+F+S }, /* 11 0 1010: stfsx */
  165. { 8, ST+F }, /* 11 0 1011: stfdx */
  166. { 16, LD+F }, /* 11 0 1100: lfdpx */
  167. { 4, LD+F+SE }, /* 11 0 1101: lfiwax */
  168. { 16, ST+F }, /* 11 0 1110: stfdpx */
  169. { 4, ST+F }, /* 11 0 1111: stfiwx */
  170. { 4, LD+U }, /* 11 1 0000: lwzux */
  171. INVALID, /* 11 1 0001 */
  172. { 4, ST+U }, /* 11 1 0010: stwux */
  173. INVALID, /* 11 1 0011 */
  174. { 2, LD+U }, /* 11 1 0100: lhzux */
  175. { 2, LD+SE+U }, /* 11 1 0101: lhaux */
  176. { 2, ST+U }, /* 11 1 0110: sthux */
  177. INVALID, /* 11 1 0111 */
  178. { 4, LD+F+S+U }, /* 11 1 1000: lfsux */
  179. { 8, LD+F+U }, /* 11 1 1001: lfdux */
  180. { 4, ST+F+S+U }, /* 11 1 1010: stfsux */
  181. { 8, ST+F+U }, /* 11 1 1011: stfdux */
  182. INVALID, /* 11 1 1100 */
  183. { 4, LD+F }, /* 11 1 1101: lfiwzx */
  184. INVALID, /* 11 1 1110 */
  185. INVALID, /* 11 1 1111 */
  186. };
  187. /*
  188. * Create a DSISR value from the instruction
  189. */
  190. static inline unsigned make_dsisr(unsigned instr)
  191. {
  192. unsigned dsisr;
  193. /* bits 6:15 --> 22:31 */
  194. dsisr = (instr & 0x03ff0000) >> 16;
  195. if (IS_XFORM(instr)) {
  196. /* bits 29:30 --> 15:16 */
  197. dsisr |= (instr & 0x00000006) << 14;
  198. /* bit 25 --> 17 */
  199. dsisr |= (instr & 0x00000040) << 8;
  200. /* bits 21:24 --> 18:21 */
  201. dsisr |= (instr & 0x00000780) << 3;
  202. } else {
  203. /* bit 5 --> 17 */
  204. dsisr |= (instr & 0x04000000) >> 12;
  205. /* bits 1: 4 --> 18:21 */
  206. dsisr |= (instr & 0x78000000) >> 17;
  207. /* bits 30:31 --> 12:13 */
  208. if (IS_DSFORM(instr))
  209. dsisr |= (instr & 0x00000003) << 18;
  210. }
  211. return dsisr;
  212. }
  213. /*
  214. * The dcbz (data cache block zero) instruction
  215. * gives an alignment fault if used on non-cacheable
  216. * memory. We handle the fault mainly for the
  217. * case when we are running with the cache disabled
  218. * for debugging.
  219. */
  220. static int emulate_dcbz(struct pt_regs *regs, unsigned char __user *addr)
  221. {
  222. long __user *p;
  223. int i, size;
  224. #ifdef __powerpc64__
  225. size = ppc64_caches.dline_size;
  226. #else
  227. size = L1_CACHE_BYTES;
  228. #endif
  229. p = (long __user *) (regs->dar & -size);
  230. if (user_mode(regs) && !access_ok(VERIFY_WRITE, p, size))
  231. return -EFAULT;
  232. for (i = 0; i < size / sizeof(long); ++i)
  233. if (__put_user_inatomic(0, p+i))
  234. return -EFAULT;
  235. return 1;
  236. }
  237. /*
  238. * Emulate load & store multiple instructions
  239. * On 64-bit machines, these instructions only affect/use the
  240. * bottom 4 bytes of each register, and the loads clear the
  241. * top 4 bytes of the affected register.
  242. */
  243. #ifdef CONFIG_PPC64
  244. #define REG_BYTE(rp, i) *((u8 *)((rp) + ((i) >> 2)) + ((i) & 3) + 4)
  245. #else
  246. #define REG_BYTE(rp, i) *((u8 *)(rp) + (i))
  247. #endif
  248. #define SWIZ_PTR(p) ((unsigned char __user *)((p) ^ swiz))
  249. static int emulate_multiple(struct pt_regs *regs, unsigned char __user *addr,
  250. unsigned int reg, unsigned int nb,
  251. unsigned int flags, unsigned int instr,
  252. unsigned long swiz)
  253. {
  254. unsigned long *rptr;
  255. unsigned int nb0, i, bswiz;
  256. unsigned long p;
  257. /*
  258. * We do not try to emulate 8 bytes multiple as they aren't really
  259. * available in our operating environments and we don't try to
  260. * emulate multiples operations in kernel land as they should never
  261. * be used/generated there at least not on unaligned boundaries
  262. */
  263. if (unlikely((nb > 4) || !user_mode(regs)))
  264. return 0;
  265. /* lmw, stmw, lswi/x, stswi/x */
  266. nb0 = 0;
  267. if (flags & HARD) {
  268. if (flags & SX) {
  269. nb = regs->xer & 127;
  270. if (nb == 0)
  271. return 1;
  272. } else {
  273. unsigned long pc = regs->nip ^ (swiz & 4);
  274. if (__get_user_inatomic(instr,
  275. (unsigned int __user *)pc))
  276. return -EFAULT;
  277. if (swiz == 0 && (flags & SW))
  278. instr = cpu_to_le32(instr);
  279. nb = (instr >> 11) & 0x1f;
  280. if (nb == 0)
  281. nb = 32;
  282. }
  283. if (nb + reg * 4 > 128) {
  284. nb0 = nb + reg * 4 - 128;
  285. nb = 128 - reg * 4;
  286. }
  287. } else {
  288. /* lwm, stmw */
  289. nb = (32 - reg) * 4;
  290. }
  291. if (!access_ok((flags & ST ? VERIFY_WRITE: VERIFY_READ), addr, nb+nb0))
  292. return -EFAULT; /* bad address */
  293. rptr = &regs->gpr[reg];
  294. p = (unsigned long) addr;
  295. bswiz = (flags & SW)? 3: 0;
  296. if (!(flags & ST)) {
  297. /*
  298. * This zeroes the top 4 bytes of the affected registers
  299. * in 64-bit mode, and also zeroes out any remaining
  300. * bytes of the last register for lsw*.
  301. */
  302. memset(rptr, 0, ((nb + 3) / 4) * sizeof(unsigned long));
  303. if (nb0 > 0)
  304. memset(&regs->gpr[0], 0,
  305. ((nb0 + 3) / 4) * sizeof(unsigned long));
  306. for (i = 0; i < nb; ++i, ++p)
  307. if (__get_user_inatomic(REG_BYTE(rptr, i ^ bswiz),
  308. SWIZ_PTR(p)))
  309. return -EFAULT;
  310. if (nb0 > 0) {
  311. rptr = &regs->gpr[0];
  312. addr += nb;
  313. for (i = 0; i < nb0; ++i, ++p)
  314. if (__get_user_inatomic(REG_BYTE(rptr,
  315. i ^ bswiz),
  316. SWIZ_PTR(p)))
  317. return -EFAULT;
  318. }
  319. } else {
  320. for (i = 0; i < nb; ++i, ++p)
  321. if (__put_user_inatomic(REG_BYTE(rptr, i ^ bswiz),
  322. SWIZ_PTR(p)))
  323. return -EFAULT;
  324. if (nb0 > 0) {
  325. rptr = &regs->gpr[0];
  326. addr += nb;
  327. for (i = 0; i < nb0; ++i, ++p)
  328. if (__put_user_inatomic(REG_BYTE(rptr,
  329. i ^ bswiz),
  330. SWIZ_PTR(p)))
  331. return -EFAULT;
  332. }
  333. }
  334. return 1;
  335. }
  336. /*
  337. * Emulate floating-point pair loads and stores.
  338. * Only POWER6 has these instructions, and it does true little-endian,
  339. * so we don't need the address swizzling.
  340. */
  341. static int emulate_fp_pair(unsigned char __user *addr, unsigned int reg,
  342. unsigned int flags)
  343. {
  344. char *ptr0 = (char *) &current->thread.TS_FPR(reg);
  345. char *ptr1 = (char *) &current->thread.TS_FPR(reg+1);
  346. int i, ret, sw = 0;
  347. if (!(flags & F))
  348. return 0;
  349. if (reg & 1)
  350. return 0; /* invalid form: FRS/FRT must be even */
  351. if (flags & SW)
  352. sw = 7;
  353. ret = 0;
  354. for (i = 0; i < 8; ++i) {
  355. if (!(flags & ST)) {
  356. ret |= __get_user(ptr0[i^sw], addr + i);
  357. ret |= __get_user(ptr1[i^sw], addr + i + 8);
  358. } else {
  359. ret |= __put_user(ptr0[i^sw], addr + i);
  360. ret |= __put_user(ptr1[i^sw], addr + i + 8);
  361. }
  362. }
  363. if (ret)
  364. return -EFAULT;
  365. return 1; /* exception handled and fixed up */
  366. }
  367. #ifdef CONFIG_SPE
  368. static struct aligninfo spe_aligninfo[32] = {
  369. { 8, LD+E8 }, /* 0 00 00: evldd[x] */
  370. { 8, LD+E4 }, /* 0 00 01: evldw[x] */
  371. { 8, LD }, /* 0 00 10: evldh[x] */
  372. INVALID, /* 0 00 11 */
  373. { 2, LD }, /* 0 01 00: evlhhesplat[x] */
  374. INVALID, /* 0 01 01 */
  375. { 2, LD }, /* 0 01 10: evlhhousplat[x] */
  376. { 2, LD+SE }, /* 0 01 11: evlhhossplat[x] */
  377. { 4, LD }, /* 0 10 00: evlwhe[x] */
  378. INVALID, /* 0 10 01 */
  379. { 4, LD }, /* 0 10 10: evlwhou[x] */
  380. { 4, LD+SE }, /* 0 10 11: evlwhos[x] */
  381. { 4, LD+E4 }, /* 0 11 00: evlwwsplat[x] */
  382. INVALID, /* 0 11 01 */
  383. { 4, LD }, /* 0 11 10: evlwhsplat[x] */
  384. INVALID, /* 0 11 11 */
  385. { 8, ST+E8 }, /* 1 00 00: evstdd[x] */
  386. { 8, ST+E4 }, /* 1 00 01: evstdw[x] */
  387. { 8, ST }, /* 1 00 10: evstdh[x] */
  388. INVALID, /* 1 00 11 */
  389. INVALID, /* 1 01 00 */
  390. INVALID, /* 1 01 01 */
  391. INVALID, /* 1 01 10 */
  392. INVALID, /* 1 01 11 */
  393. { 4, ST }, /* 1 10 00: evstwhe[x] */
  394. INVALID, /* 1 10 01 */
  395. { 4, ST }, /* 1 10 10: evstwho[x] */
  396. INVALID, /* 1 10 11 */
  397. { 4, ST+E4 }, /* 1 11 00: evstwwe[x] */
  398. INVALID, /* 1 11 01 */
  399. { 4, ST+E4 }, /* 1 11 10: evstwwo[x] */
  400. INVALID, /* 1 11 11 */
  401. };
  402. #define EVLDD 0x00
  403. #define EVLDW 0x01
  404. #define EVLDH 0x02
  405. #define EVLHHESPLAT 0x04
  406. #define EVLHHOUSPLAT 0x06
  407. #define EVLHHOSSPLAT 0x07
  408. #define EVLWHE 0x08
  409. #define EVLWHOU 0x0A
  410. #define EVLWHOS 0x0B
  411. #define EVLWWSPLAT 0x0C
  412. #define EVLWHSPLAT 0x0E
  413. #define EVSTDD 0x10
  414. #define EVSTDW 0x11
  415. #define EVSTDH 0x12
  416. #define EVSTWHE 0x18
  417. #define EVSTWHO 0x1A
  418. #define EVSTWWE 0x1C
  419. #define EVSTWWO 0x1E
  420. /*
  421. * Emulate SPE loads and stores.
  422. * Only Book-E has these instructions, and it does true little-endian,
  423. * so we don't need the address swizzling.
  424. */
  425. static int emulate_spe(struct pt_regs *regs, unsigned int reg,
  426. unsigned int instr)
  427. {
  428. int t, ret;
  429. union {
  430. u64 ll;
  431. u32 w[2];
  432. u16 h[4];
  433. u8 v[8];
  434. } data, temp;
  435. unsigned char __user *p, *addr;
  436. unsigned long *evr = &current->thread.evr[reg];
  437. unsigned int nb, flags;
  438. instr = (instr >> 1) & 0x1f;
  439. /* DAR has the operand effective address */
  440. addr = (unsigned char __user *)regs->dar;
  441. nb = spe_aligninfo[instr].len;
  442. flags = spe_aligninfo[instr].flags;
  443. /* Verify the address of the operand */
  444. if (unlikely(user_mode(regs) &&
  445. !access_ok((flags & ST ? VERIFY_WRITE : VERIFY_READ),
  446. addr, nb)))
  447. return -EFAULT;
  448. /* userland only */
  449. if (unlikely(!user_mode(regs)))
  450. return 0;
  451. flush_spe_to_thread(current);
  452. /* If we are loading, get the data from user space, else
  453. * get it from register values
  454. */
  455. if (flags & ST) {
  456. data.ll = 0;
  457. switch (instr) {
  458. case EVSTDD:
  459. case EVSTDW:
  460. case EVSTDH:
  461. data.w[0] = *evr;
  462. data.w[1] = regs->gpr[reg];
  463. break;
  464. case EVSTWHE:
  465. data.h[2] = *evr >> 16;
  466. data.h[3] = regs->gpr[reg] >> 16;
  467. break;
  468. case EVSTWHO:
  469. data.h[2] = *evr & 0xffff;
  470. data.h[3] = regs->gpr[reg] & 0xffff;
  471. break;
  472. case EVSTWWE:
  473. data.w[1] = *evr;
  474. break;
  475. case EVSTWWO:
  476. data.w[1] = regs->gpr[reg];
  477. break;
  478. default:
  479. return -EINVAL;
  480. }
  481. } else {
  482. temp.ll = data.ll = 0;
  483. ret = 0;
  484. p = addr;
  485. switch (nb) {
  486. case 8:
  487. ret |= __get_user_inatomic(temp.v[0], p++);
  488. ret |= __get_user_inatomic(temp.v[1], p++);
  489. ret |= __get_user_inatomic(temp.v[2], p++);
  490. ret |= __get_user_inatomic(temp.v[3], p++);
  491. case 4:
  492. ret |= __get_user_inatomic(temp.v[4], p++);
  493. ret |= __get_user_inatomic(temp.v[5], p++);
  494. case 2:
  495. ret |= __get_user_inatomic(temp.v[6], p++);
  496. ret |= __get_user_inatomic(temp.v[7], p++);
  497. if (unlikely(ret))
  498. return -EFAULT;
  499. }
  500. switch (instr) {
  501. case EVLDD:
  502. case EVLDW:
  503. case EVLDH:
  504. data.ll = temp.ll;
  505. break;
  506. case EVLHHESPLAT:
  507. data.h[0] = temp.h[3];
  508. data.h[2] = temp.h[3];
  509. break;
  510. case EVLHHOUSPLAT:
  511. case EVLHHOSSPLAT:
  512. data.h[1] = temp.h[3];
  513. data.h[3] = temp.h[3];
  514. break;
  515. case EVLWHE:
  516. data.h[0] = temp.h[2];
  517. data.h[2] = temp.h[3];
  518. break;
  519. case EVLWHOU:
  520. case EVLWHOS:
  521. data.h[1] = temp.h[2];
  522. data.h[3] = temp.h[3];
  523. break;
  524. case EVLWWSPLAT:
  525. data.w[0] = temp.w[1];
  526. data.w[1] = temp.w[1];
  527. break;
  528. case EVLWHSPLAT:
  529. data.h[0] = temp.h[2];
  530. data.h[1] = temp.h[2];
  531. data.h[2] = temp.h[3];
  532. data.h[3] = temp.h[3];
  533. break;
  534. default:
  535. return -EINVAL;
  536. }
  537. }
  538. if (flags & SW) {
  539. switch (flags & 0xf0) {
  540. case E8:
  541. SWAP(data.v[0], data.v[7]);
  542. SWAP(data.v[1], data.v[6]);
  543. SWAP(data.v[2], data.v[5]);
  544. SWAP(data.v[3], data.v[4]);
  545. break;
  546. case E4:
  547. SWAP(data.v[0], data.v[3]);
  548. SWAP(data.v[1], data.v[2]);
  549. SWAP(data.v[4], data.v[7]);
  550. SWAP(data.v[5], data.v[6]);
  551. break;
  552. /* Its half word endian */
  553. default:
  554. SWAP(data.v[0], data.v[1]);
  555. SWAP(data.v[2], data.v[3]);
  556. SWAP(data.v[4], data.v[5]);
  557. SWAP(data.v[6], data.v[7]);
  558. break;
  559. }
  560. }
  561. if (flags & SE) {
  562. data.w[0] = (s16)data.h[1];
  563. data.w[1] = (s16)data.h[3];
  564. }
  565. /* Store result to memory or update registers */
  566. if (flags & ST) {
  567. ret = 0;
  568. p = addr;
  569. switch (nb) {
  570. case 8:
  571. ret |= __put_user_inatomic(data.v[0], p++);
  572. ret |= __put_user_inatomic(data.v[1], p++);
  573. ret |= __put_user_inatomic(data.v[2], p++);
  574. ret |= __put_user_inatomic(data.v[3], p++);
  575. case 4:
  576. ret |= __put_user_inatomic(data.v[4], p++);
  577. ret |= __put_user_inatomic(data.v[5], p++);
  578. case 2:
  579. ret |= __put_user_inatomic(data.v[6], p++);
  580. ret |= __put_user_inatomic(data.v[7], p++);
  581. }
  582. if (unlikely(ret))
  583. return -EFAULT;
  584. } else {
  585. *evr = data.w[0];
  586. regs->gpr[reg] = data.w[1];
  587. }
  588. return 1;
  589. }
  590. #endif /* CONFIG_SPE */
  591. #ifdef CONFIG_VSX
  592. /*
  593. * Emulate VSX instructions...
  594. */
  595. static int emulate_vsx(unsigned char __user *addr, unsigned int reg,
  596. unsigned int areg, struct pt_regs *regs,
  597. unsigned int flags, unsigned int length,
  598. unsigned int elsize)
  599. {
  600. char *ptr;
  601. unsigned long *lptr;
  602. int ret = 0;
  603. int sw = 0;
  604. int i, j;
  605. flush_vsx_to_thread(current);
  606. if (reg < 32)
  607. ptr = (char *) &current->thread.TS_FPR(reg);
  608. else
  609. ptr = (char *) &current->thread.vr[reg - 32];
  610. lptr = (unsigned long *) ptr;
  611. if (flags & SW)
  612. sw = elsize-1;
  613. for (j = 0; j < length; j += elsize) {
  614. for (i = 0; i < elsize; ++i) {
  615. if (flags & ST)
  616. ret |= __put_user(ptr[i^sw], addr + i);
  617. else
  618. ret |= __get_user(ptr[i^sw], addr + i);
  619. }
  620. ptr += elsize;
  621. addr += elsize;
  622. }
  623. if (!ret) {
  624. if (flags & U)
  625. regs->gpr[areg] = regs->dar;
  626. /* Splat load copies the same data to top and bottom 8 bytes */
  627. if (flags & SPLT)
  628. lptr[1] = lptr[0];
  629. /* For 8 byte loads, zero the top 8 bytes */
  630. else if (!(flags & ST) && (8 == length))
  631. lptr[1] = 0;
  632. } else
  633. return -EFAULT;
  634. return 1;
  635. }
  636. #endif
  637. /*
  638. * Called on alignment exception. Attempts to fixup
  639. *
  640. * Return 1 on success
  641. * Return 0 if unable to handle the interrupt
  642. * Return -EFAULT if data address is bad
  643. */
  644. int fix_alignment(struct pt_regs *regs)
  645. {
  646. unsigned int instr, nb, flags, instruction = 0;
  647. unsigned int reg, areg;
  648. unsigned int dsisr;
  649. unsigned char __user *addr;
  650. unsigned long p, swiz;
  651. int ret, t;
  652. union {
  653. u64 ll;
  654. double dd;
  655. unsigned char v[8];
  656. struct {
  657. unsigned hi32;
  658. int low32;
  659. } x32;
  660. struct {
  661. unsigned char hi48[6];
  662. short low16;
  663. } x16;
  664. } data;
  665. /*
  666. * We require a complete register set, if not, then our assembly
  667. * is broken
  668. */
  669. CHECK_FULL_REGS(regs);
  670. dsisr = regs->dsisr;
  671. /* Some processors don't provide us with a DSISR we can use here,
  672. * let's make one up from the instruction
  673. */
  674. if (cpu_has_feature(CPU_FTR_NODSISRALIGN)) {
  675. unsigned long pc = regs->nip;
  676. if (cpu_has_feature(CPU_FTR_PPC_LE) && (regs->msr & MSR_LE))
  677. pc ^= 4;
  678. if (unlikely(__get_user_inatomic(instr,
  679. (unsigned int __user *)pc)))
  680. return -EFAULT;
  681. if (cpu_has_feature(CPU_FTR_REAL_LE) && (regs->msr & MSR_LE))
  682. instr = cpu_to_le32(instr);
  683. dsisr = make_dsisr(instr);
  684. instruction = instr;
  685. }
  686. /* extract the operation and registers from the dsisr */
  687. reg = (dsisr >> 5) & 0x1f; /* source/dest register */
  688. areg = dsisr & 0x1f; /* register to update */
  689. #ifdef CONFIG_SPE
  690. if ((instr >> 26) == 0x4) {
  691. PPC_WARN_ALIGNMENT(spe, regs);
  692. return emulate_spe(regs, reg, instr);
  693. }
  694. #endif
  695. instr = (dsisr >> 10) & 0x7f;
  696. instr |= (dsisr >> 13) & 0x60;
  697. /* Lookup the operation in our table */
  698. nb = aligninfo[instr].len;
  699. flags = aligninfo[instr].flags;
  700. /* ldbrx/stdbrx overlap lfs/stfs in the DSISR unfortunately */
  701. if (IS_XFORM(instruction) && ((instruction >> 1) & 0x3ff) == 532) {
  702. nb = 8;
  703. flags = LD+SW;
  704. } else if (IS_XFORM(instruction) &&
  705. ((instruction >> 1) & 0x3ff) == 660) {
  706. nb = 8;
  707. flags = ST+SW;
  708. }
  709. /* Byteswap little endian loads and stores */
  710. swiz = 0;
  711. if (regs->msr & MSR_LE) {
  712. flags ^= SW;
  713. /*
  714. * So-called "PowerPC little endian" mode works by
  715. * swizzling addresses rather than by actually doing
  716. * any byte-swapping. To emulate this, we XOR each
  717. * byte address with 7. We also byte-swap, because
  718. * the processor's address swizzling depends on the
  719. * operand size (it xors the address with 7 for bytes,
  720. * 6 for halfwords, 4 for words, 0 for doublewords) but
  721. * we will xor with 7 and load/store each byte separately.
  722. */
  723. if (cpu_has_feature(CPU_FTR_PPC_LE))
  724. swiz = 7;
  725. }
  726. /* DAR has the operand effective address */
  727. addr = (unsigned char __user *)regs->dar;
  728. #ifdef CONFIG_VSX
  729. if ((instruction & 0xfc00003e) == 0x7c000018) {
  730. unsigned int elsize;
  731. /* Additional register addressing bit (64 VSX vs 32 FPR/GPR) */
  732. reg |= (instruction & 0x1) << 5;
  733. /* Simple inline decoder instead of a table */
  734. /* VSX has only 8 and 16 byte memory accesses */
  735. nb = 8;
  736. if (instruction & 0x200)
  737. nb = 16;
  738. /* Vector stores in little-endian mode swap individual
  739. elements, so process them separately */
  740. elsize = 4;
  741. if (instruction & 0x80)
  742. elsize = 8;
  743. flags = 0;
  744. if (regs->msr & MSR_LE)
  745. flags |= SW;
  746. if (instruction & 0x100)
  747. flags |= ST;
  748. if (instruction & 0x040)
  749. flags |= U;
  750. /* splat load needs a special decoder */
  751. if ((instruction & 0x400) == 0){
  752. flags |= SPLT;
  753. nb = 8;
  754. }
  755. PPC_WARN_ALIGNMENT(vsx, regs);
  756. return emulate_vsx(addr, reg, areg, regs, flags, nb, elsize);
  757. }
  758. #endif
  759. /* A size of 0 indicates an instruction we don't support, with
  760. * the exception of DCBZ which is handled as a special case here
  761. */
  762. if (instr == DCBZ) {
  763. PPC_WARN_ALIGNMENT(dcbz, regs);
  764. return emulate_dcbz(regs, addr);
  765. }
  766. if (unlikely(nb == 0))
  767. return 0;
  768. /* Load/Store Multiple instructions are handled in their own
  769. * function
  770. */
  771. if (flags & M) {
  772. PPC_WARN_ALIGNMENT(multiple, regs);
  773. return emulate_multiple(regs, addr, reg, nb,
  774. flags, instr, swiz);
  775. }
  776. /* Verify the address of the operand */
  777. if (unlikely(user_mode(regs) &&
  778. !access_ok((flags & ST ? VERIFY_WRITE : VERIFY_READ),
  779. addr, nb)))
  780. return -EFAULT;
  781. /* Force the fprs into the save area so we can reference them */
  782. if (flags & F) {
  783. /* userland only */
  784. if (unlikely(!user_mode(regs)))
  785. return 0;
  786. flush_fp_to_thread(current);
  787. }
  788. /* Special case for 16-byte FP loads and stores */
  789. if (nb == 16) {
  790. PPC_WARN_ALIGNMENT(fp_pair, regs);
  791. return emulate_fp_pair(addr, reg, flags);
  792. }
  793. PPC_WARN_ALIGNMENT(unaligned, regs);
  794. /* If we are loading, get the data from user space, else
  795. * get it from register values
  796. */
  797. if (!(flags & ST)) {
  798. data.ll = 0;
  799. ret = 0;
  800. p = (unsigned long) addr;
  801. switch (nb) {
  802. case 8:
  803. ret |= __get_user_inatomic(data.v[0], SWIZ_PTR(p++));
  804. ret |= __get_user_inatomic(data.v[1], SWIZ_PTR(p++));
  805. ret |= __get_user_inatomic(data.v[2], SWIZ_PTR(p++));
  806. ret |= __get_user_inatomic(data.v[3], SWIZ_PTR(p++));
  807. case 4:
  808. ret |= __get_user_inatomic(data.v[4], SWIZ_PTR(p++));
  809. ret |= __get_user_inatomic(data.v[5], SWIZ_PTR(p++));
  810. case 2:
  811. ret |= __get_user_inatomic(data.v[6], SWIZ_PTR(p++));
  812. ret |= __get_user_inatomic(data.v[7], SWIZ_PTR(p++));
  813. if (unlikely(ret))
  814. return -EFAULT;
  815. }
  816. } else if (flags & F) {
  817. data.dd = current->thread.TS_FPR(reg);
  818. if (flags & S) {
  819. /* Single-precision FP store requires conversion... */
  820. #ifdef CONFIG_PPC_FPU
  821. preempt_disable();
  822. enable_kernel_fp();
  823. cvt_df(&data.dd, (float *)&data.v[4]);
  824. preempt_enable();
  825. #else
  826. return 0;
  827. #endif
  828. }
  829. } else
  830. data.ll = regs->gpr[reg];
  831. if (flags & SW) {
  832. switch (nb) {
  833. case 8:
  834. SWAP(data.v[0], data.v[7]);
  835. SWAP(data.v[1], data.v[6]);
  836. SWAP(data.v[2], data.v[5]);
  837. SWAP(data.v[3], data.v[4]);
  838. break;
  839. case 4:
  840. SWAP(data.v[4], data.v[7]);
  841. SWAP(data.v[5], data.v[6]);
  842. break;
  843. case 2:
  844. SWAP(data.v[6], data.v[7]);
  845. break;
  846. }
  847. }
  848. /* Perform other misc operations like sign extension
  849. * or floating point single precision conversion
  850. */
  851. switch (flags & ~(U|SW)) {
  852. case LD+SE: /* sign extending integer loads */
  853. case LD+F+SE: /* sign extend for lfiwax */
  854. if ( nb == 2 )
  855. data.ll = data.x16.low16;
  856. else /* nb must be 4 */
  857. data.ll = data.x32.low32;
  858. break;
  859. /* Single-precision FP load requires conversion... */
  860. case LD+F+S:
  861. #ifdef CONFIG_PPC_FPU
  862. preempt_disable();
  863. enable_kernel_fp();
  864. cvt_fd((float *)&data.v[4], &data.dd);
  865. preempt_enable();
  866. #else
  867. return 0;
  868. #endif
  869. break;
  870. }
  871. /* Store result to memory or update registers */
  872. if (flags & ST) {
  873. ret = 0;
  874. p = (unsigned long) addr;
  875. switch (nb) {
  876. case 8:
  877. ret |= __put_user_inatomic(data.v[0], SWIZ_PTR(p++));
  878. ret |= __put_user_inatomic(data.v[1], SWIZ_PTR(p++));
  879. ret |= __put_user_inatomic(data.v[2], SWIZ_PTR(p++));
  880. ret |= __put_user_inatomic(data.v[3], SWIZ_PTR(p++));
  881. case 4:
  882. ret |= __put_user_inatomic(data.v[4], SWIZ_PTR(p++));
  883. ret |= __put_user_inatomic(data.v[5], SWIZ_PTR(p++));
  884. case 2:
  885. ret |= __put_user_inatomic(data.v[6], SWIZ_PTR(p++));
  886. ret |= __put_user_inatomic(data.v[7], SWIZ_PTR(p++));
  887. }
  888. if (unlikely(ret))
  889. return -EFAULT;
  890. } else if (flags & F)
  891. current->thread.TS_FPR(reg) = data.dd;
  892. else
  893. regs->gpr[reg] = data.ll;
  894. /* Update RA as needed */
  895. if (flags & U)
  896. regs->gpr[areg] = regs->dar;
  897. return 1;
  898. }