align.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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. /*
  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. #ifdef __BIG_ENDIAN__
  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. #endif
  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 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. data.ll = swab64(data.ll);
  543. break;
  544. case E4:
  545. data.w[0] = swab32(data.w[0]);
  546. data.w[1] = swab32(data.w[1]);
  547. break;
  548. /* Its half word endian */
  549. default:
  550. data.h[0] = swab16(data.h[0]);
  551. data.h[1] = swab16(data.h[1]);
  552. data.h[2] = swab16(data.h[2]);
  553. data.h[3] = swab16(data.h[3]);
  554. break;
  555. }
  556. }
  557. if (flags & SE) {
  558. data.w[0] = (s16)data.h[1];
  559. data.w[1] = (s16)data.h[3];
  560. }
  561. /* Store result to memory or update registers */
  562. if (flags & ST) {
  563. ret = 0;
  564. p = addr;
  565. switch (nb) {
  566. case 8:
  567. ret |= __put_user_inatomic(data.v[0], p++);
  568. ret |= __put_user_inatomic(data.v[1], p++);
  569. ret |= __put_user_inatomic(data.v[2], p++);
  570. ret |= __put_user_inatomic(data.v[3], p++);
  571. case 4:
  572. ret |= __put_user_inatomic(data.v[4], p++);
  573. ret |= __put_user_inatomic(data.v[5], p++);
  574. case 2:
  575. ret |= __put_user_inatomic(data.v[6], p++);
  576. ret |= __put_user_inatomic(data.v[7], p++);
  577. }
  578. if (unlikely(ret))
  579. return -EFAULT;
  580. } else {
  581. *evr = data.w[0];
  582. regs->gpr[reg] = data.w[1];
  583. }
  584. return 1;
  585. }
  586. #endif /* CONFIG_SPE */
  587. #ifdef CONFIG_VSX
  588. /*
  589. * Emulate VSX instructions...
  590. */
  591. static int emulate_vsx(unsigned char __user *addr, unsigned int reg,
  592. unsigned int areg, struct pt_regs *regs,
  593. unsigned int flags, unsigned int length,
  594. unsigned int elsize)
  595. {
  596. char *ptr;
  597. unsigned long *lptr;
  598. int ret = 0;
  599. int sw = 0;
  600. int i, j;
  601. /* userland only */
  602. if (unlikely(!user_mode(regs)))
  603. return 0;
  604. flush_vsx_to_thread(current);
  605. if (reg < 32)
  606. ptr = (char *) &current->thread.fp_state.fpr[reg][0];
  607. else
  608. ptr = (char *) &current->thread.vr_state.vr[reg - 32];
  609. lptr = (unsigned long *) ptr;
  610. #ifdef __LITTLE_ENDIAN__
  611. if (flags & SW) {
  612. elsize = length;
  613. sw = length-1;
  614. } else {
  615. /*
  616. * The elements are BE ordered, even in LE mode, so process
  617. * them in reverse order.
  618. */
  619. addr += length - elsize;
  620. /* 8 byte memory accesses go in the top 8 bytes of the VR */
  621. if (length == 8)
  622. ptr += 8;
  623. }
  624. #else
  625. if (flags & SW)
  626. sw = elsize-1;
  627. #endif
  628. for (j = 0; j < length; j += elsize) {
  629. for (i = 0; i < elsize; ++i) {
  630. if (flags & ST)
  631. ret |= __put_user(ptr[i^sw], addr + i);
  632. else
  633. ret |= __get_user(ptr[i^sw], addr + i);
  634. }
  635. ptr += elsize;
  636. #ifdef __LITTLE_ENDIAN__
  637. addr -= elsize;
  638. #else
  639. addr += elsize;
  640. #endif
  641. }
  642. #ifdef __BIG_ENDIAN__
  643. #define VSX_HI 0
  644. #define VSX_LO 1
  645. #else
  646. #define VSX_HI 1
  647. #define VSX_LO 0
  648. #endif
  649. if (!ret) {
  650. if (flags & U)
  651. regs->gpr[areg] = regs->dar;
  652. /* Splat load copies the same data to top and bottom 8 bytes */
  653. if (flags & SPLT)
  654. lptr[VSX_LO] = lptr[VSX_HI];
  655. /* For 8 byte loads, zero the low 8 bytes */
  656. else if (!(flags & ST) && (8 == length))
  657. lptr[VSX_LO] = 0;
  658. } else
  659. return -EFAULT;
  660. return 1;
  661. }
  662. #endif
  663. /*
  664. * Called on alignment exception. Attempts to fixup
  665. *
  666. * Return 1 on success
  667. * Return 0 if unable to handle the interrupt
  668. * Return -EFAULT if data address is bad
  669. */
  670. int fix_alignment(struct pt_regs *regs)
  671. {
  672. unsigned int instr, nb, flags, instruction = 0;
  673. unsigned int reg, areg;
  674. unsigned int dsisr;
  675. unsigned char __user *addr;
  676. unsigned long p, swiz;
  677. int ret, i;
  678. union data {
  679. u64 ll;
  680. double dd;
  681. unsigned char v[8];
  682. struct {
  683. #ifdef __LITTLE_ENDIAN__
  684. int low32;
  685. unsigned hi32;
  686. #else
  687. unsigned hi32;
  688. int low32;
  689. #endif
  690. } x32;
  691. struct {
  692. #ifdef __LITTLE_ENDIAN__
  693. short low16;
  694. unsigned char hi48[6];
  695. #else
  696. unsigned char hi48[6];
  697. short low16;
  698. #endif
  699. } x16;
  700. } data;
  701. /*
  702. * We require a complete register set, if not, then our assembly
  703. * is broken
  704. */
  705. CHECK_FULL_REGS(regs);
  706. dsisr = regs->dsisr;
  707. /* Some processors don't provide us with a DSISR we can use here,
  708. * let's make one up from the instruction
  709. */
  710. if (cpu_has_feature(CPU_FTR_NODSISRALIGN)) {
  711. unsigned long pc = regs->nip;
  712. if (cpu_has_feature(CPU_FTR_PPC_LE) && (regs->msr & MSR_LE))
  713. pc ^= 4;
  714. if (unlikely(__get_user_inatomic(instr,
  715. (unsigned int __user *)pc)))
  716. return -EFAULT;
  717. if (cpu_has_feature(CPU_FTR_REAL_LE) && (regs->msr & MSR_LE))
  718. instr = cpu_to_le32(instr);
  719. dsisr = make_dsisr(instr);
  720. instruction = instr;
  721. }
  722. /* extract the operation and registers from the dsisr */
  723. reg = (dsisr >> 5) & 0x1f; /* source/dest register */
  724. areg = dsisr & 0x1f; /* register to update */
  725. #ifdef CONFIG_SPE
  726. if ((instr >> 26) == 0x4) {
  727. PPC_WARN_ALIGNMENT(spe, regs);
  728. return emulate_spe(regs, reg, instr);
  729. }
  730. #endif
  731. instr = (dsisr >> 10) & 0x7f;
  732. instr |= (dsisr >> 13) & 0x60;
  733. /* Lookup the operation in our table */
  734. nb = aligninfo[instr].len;
  735. flags = aligninfo[instr].flags;
  736. /* ldbrx/stdbrx overlap lfs/stfs in the DSISR unfortunately */
  737. if (IS_XFORM(instruction) && ((instruction >> 1) & 0x3ff) == 532) {
  738. nb = 8;
  739. flags = LD+SW;
  740. } else if (IS_XFORM(instruction) &&
  741. ((instruction >> 1) & 0x3ff) == 660) {
  742. nb = 8;
  743. flags = ST+SW;
  744. }
  745. /* Byteswap little endian loads and stores */
  746. swiz = 0;
  747. if ((regs->msr & MSR_LE) != (MSR_KERNEL & MSR_LE)) {
  748. flags ^= SW;
  749. #ifdef __BIG_ENDIAN__
  750. /*
  751. * So-called "PowerPC little endian" mode works by
  752. * swizzling addresses rather than by actually doing
  753. * any byte-swapping. To emulate this, we XOR each
  754. * byte address with 7. We also byte-swap, because
  755. * the processor's address swizzling depends on the
  756. * operand size (it xors the address with 7 for bytes,
  757. * 6 for halfwords, 4 for words, 0 for doublewords) but
  758. * we will xor with 7 and load/store each byte separately.
  759. */
  760. if (cpu_has_feature(CPU_FTR_PPC_LE))
  761. swiz = 7;
  762. #endif
  763. }
  764. /* DAR has the operand effective address */
  765. addr = (unsigned char __user *)regs->dar;
  766. #ifdef CONFIG_VSX
  767. if ((instruction & 0xfc00003e) == 0x7c000018) {
  768. unsigned int elsize;
  769. /* Additional register addressing bit (64 VSX vs 32 FPR/GPR) */
  770. reg |= (instruction & 0x1) << 5;
  771. /* Simple inline decoder instead of a table */
  772. /* VSX has only 8 and 16 byte memory accesses */
  773. nb = 8;
  774. if (instruction & 0x200)
  775. nb = 16;
  776. /* Vector stores in little-endian mode swap individual
  777. elements, so process them separately */
  778. elsize = 4;
  779. if (instruction & 0x80)
  780. elsize = 8;
  781. flags = 0;
  782. if ((regs->msr & MSR_LE) != (MSR_KERNEL & MSR_LE))
  783. flags |= SW;
  784. if (instruction & 0x100)
  785. flags |= ST;
  786. if (instruction & 0x040)
  787. flags |= U;
  788. /* splat load needs a special decoder */
  789. if ((instruction & 0x400) == 0){
  790. flags |= SPLT;
  791. nb = 8;
  792. }
  793. PPC_WARN_ALIGNMENT(vsx, regs);
  794. return emulate_vsx(addr, reg, areg, regs, flags, nb, elsize);
  795. }
  796. #endif
  797. /* A size of 0 indicates an instruction we don't support, with
  798. * the exception of DCBZ which is handled as a special case here
  799. */
  800. if (instr == DCBZ) {
  801. PPC_WARN_ALIGNMENT(dcbz, regs);
  802. return emulate_dcbz(regs, addr);
  803. }
  804. if (unlikely(nb == 0))
  805. return 0;
  806. /* Load/Store Multiple instructions are handled in their own
  807. * function
  808. */
  809. if (flags & M) {
  810. #ifdef __BIG_ENDIAN__
  811. PPC_WARN_ALIGNMENT(multiple, regs);
  812. return emulate_multiple(regs, addr, reg, nb,
  813. flags, instr, swiz);
  814. #else
  815. return -EFAULT;
  816. #endif
  817. }
  818. /* Verify the address of the operand */
  819. if (unlikely(user_mode(regs) &&
  820. !access_ok((flags & ST ? VERIFY_WRITE : VERIFY_READ),
  821. addr, nb)))
  822. return -EFAULT;
  823. /* Force the fprs into the save area so we can reference them */
  824. if (flags & F) {
  825. /* userland only */
  826. if (unlikely(!user_mode(regs)))
  827. return 0;
  828. flush_fp_to_thread(current);
  829. }
  830. /* Special case for 16-byte FP loads and stores */
  831. if (nb == 16) {
  832. #ifdef __BIG_ENDIAN__
  833. PPC_WARN_ALIGNMENT(fp_pair, regs);
  834. return emulate_fp_pair(addr, reg, flags);
  835. #else
  836. return -EFAULT;
  837. #endif
  838. }
  839. PPC_WARN_ALIGNMENT(unaligned, regs);
  840. /* If we are loading, get the data from user space, else
  841. * get it from register values
  842. */
  843. if (!(flags & ST)) {
  844. unsigned int start = 0;
  845. switch (nb) {
  846. case 4:
  847. start = offsetof(union data, x32.low32);
  848. break;
  849. case 2:
  850. start = offsetof(union data, x16.low16);
  851. break;
  852. }
  853. data.ll = 0;
  854. ret = 0;
  855. p = (unsigned long)addr;
  856. for (i = 0; i < nb; i++)
  857. ret |= __get_user_inatomic(data.v[start + i],
  858. SWIZ_PTR(p++));
  859. if (unlikely(ret))
  860. return -EFAULT;
  861. } else if (flags & F) {
  862. data.ll = current->thread.TS_FPR(reg);
  863. if (flags & S) {
  864. /* Single-precision FP store requires conversion... */
  865. #ifdef CONFIG_PPC_FPU
  866. preempt_disable();
  867. enable_kernel_fp();
  868. cvt_df(&data.dd, (float *)&data.x32.low32);
  869. preempt_enable();
  870. #else
  871. return 0;
  872. #endif
  873. }
  874. } else
  875. data.ll = regs->gpr[reg];
  876. if (flags & SW) {
  877. switch (nb) {
  878. case 8:
  879. data.ll = swab64(data.ll);
  880. break;
  881. case 4:
  882. data.x32.low32 = swab32(data.x32.low32);
  883. break;
  884. case 2:
  885. data.x16.low16 = swab16(data.x16.low16);
  886. break;
  887. }
  888. }
  889. /* Perform other misc operations like sign extension
  890. * or floating point single precision conversion
  891. */
  892. switch (flags & ~(U|SW)) {
  893. case LD+SE: /* sign extending integer loads */
  894. case LD+F+SE: /* sign extend for lfiwax */
  895. if ( nb == 2 )
  896. data.ll = data.x16.low16;
  897. else /* nb must be 4 */
  898. data.ll = data.x32.low32;
  899. break;
  900. /* Single-precision FP load requires conversion... */
  901. case LD+F+S:
  902. #ifdef CONFIG_PPC_FPU
  903. preempt_disable();
  904. enable_kernel_fp();
  905. cvt_fd((float *)&data.x32.low32, &data.dd);
  906. preempt_enable();
  907. #else
  908. return 0;
  909. #endif
  910. break;
  911. }
  912. /* Store result to memory or update registers */
  913. if (flags & ST) {
  914. unsigned int start = 0;
  915. switch (nb) {
  916. case 4:
  917. start = offsetof(union data, x32.low32);
  918. break;
  919. case 2:
  920. start = offsetof(union data, x16.low16);
  921. break;
  922. }
  923. ret = 0;
  924. p = (unsigned long)addr;
  925. for (i = 0; i < nb; i++)
  926. ret |= __put_user_inatomic(data.v[start + i],
  927. SWIZ_PTR(p++));
  928. if (unlikely(ret))
  929. return -EFAULT;
  930. } else if (flags & F)
  931. current->thread.TS_FPR(reg) = data.ll;
  932. else
  933. regs->gpr[reg] = data.ll;
  934. /* Update RA as needed */
  935. if (flags & U)
  936. regs->gpr[areg] = regs->dar;
  937. return 1;
  938. }