align.c 24 KB

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