align.c 22 KB

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