align.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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. #include <asm/disassemble.h>
  28. #include <asm/cpu_has_feature.h>
  29. struct aligninfo {
  30. unsigned char len;
  31. unsigned char flags;
  32. };
  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. { 16, LD }, /* 00 0 1100: lq */
  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. { 16, ST }, /* 10 0 1111: stq */
  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. * The dcbz (data cache block zero) instruction
  188. * gives an alignment fault if used on non-cacheable
  189. * memory. We handle the fault mainly for the
  190. * case when we are running with the cache disabled
  191. * for debugging.
  192. */
  193. static int emulate_dcbz(struct pt_regs *regs, unsigned char __user *addr)
  194. {
  195. long __user *p;
  196. int i, size;
  197. #ifdef __powerpc64__
  198. size = ppc64_caches.dline_size;
  199. #else
  200. size = L1_CACHE_BYTES;
  201. #endif
  202. p = (long __user *) (regs->dar & -size);
  203. if (user_mode(regs) && !access_ok(VERIFY_WRITE, p, size))
  204. return -EFAULT;
  205. for (i = 0; i < size / sizeof(long); ++i)
  206. if (__put_user_inatomic(0, p+i))
  207. return -EFAULT;
  208. return 1;
  209. }
  210. /*
  211. * Emulate load & store multiple instructions
  212. * On 64-bit machines, these instructions only affect/use the
  213. * bottom 4 bytes of each register, and the loads clear the
  214. * top 4 bytes of the affected register.
  215. */
  216. #ifdef __BIG_ENDIAN__
  217. #ifdef CONFIG_PPC64
  218. #define REG_BYTE(rp, i) *((u8 *)((rp) + ((i) >> 2)) + ((i) & 3) + 4)
  219. #else
  220. #define REG_BYTE(rp, i) *((u8 *)(rp) + (i))
  221. #endif
  222. #else
  223. #define REG_BYTE(rp, i) (*(((u8 *)((rp) + ((i)>>2)) + ((i)&3))))
  224. #endif
  225. #define SWIZ_PTR(p) ((unsigned char __user *)((p) ^ swiz))
  226. #define __get_user_or_set_dar(_regs, _dest, _addr) \
  227. ({ \
  228. int rc = 0; \
  229. typeof(_addr) __addr = (_addr); \
  230. if (__get_user_inatomic(_dest, __addr)) { \
  231. _regs->dar = (unsigned long)__addr; \
  232. rc = -EFAULT; \
  233. } \
  234. rc; \
  235. })
  236. #define __put_user_or_set_dar(_regs, _src, _addr) \
  237. ({ \
  238. int rc = 0; \
  239. typeof(_addr) __addr = (_addr); \
  240. if (__put_user_inatomic(_src, __addr)) { \
  241. _regs->dar = (unsigned long)__addr; \
  242. rc = -EFAULT; \
  243. } \
  244. rc; \
  245. })
  246. static int emulate_multiple(struct pt_regs *regs, unsigned char __user *addr,
  247. unsigned int reg, unsigned int nb,
  248. unsigned int flags, unsigned int instr,
  249. unsigned long swiz)
  250. {
  251. unsigned long *rptr;
  252. unsigned int nb0, i, bswiz;
  253. unsigned long p;
  254. /*
  255. * We do not try to emulate 8 bytes multiple as they aren't really
  256. * available in our operating environments and we don't try to
  257. * emulate multiples operations in kernel land as they should never
  258. * be used/generated there at least not on unaligned boundaries
  259. */
  260. if (unlikely((nb > 4) || !user_mode(regs)))
  261. return 0;
  262. /* lmw, stmw, lswi/x, stswi/x */
  263. nb0 = 0;
  264. if (flags & HARD) {
  265. if (flags & SX) {
  266. nb = regs->xer & 127;
  267. if (nb == 0)
  268. return 1;
  269. } else {
  270. unsigned long pc = regs->nip ^ (swiz & 4);
  271. if (__get_user_or_set_dar(regs, instr,
  272. (unsigned int __user *)pc))
  273. return -EFAULT;
  274. if (swiz == 0 && (flags & SW))
  275. instr = cpu_to_le32(instr);
  276. nb = (instr >> 11) & 0x1f;
  277. if (nb == 0)
  278. nb = 32;
  279. }
  280. if (nb + reg * 4 > 128) {
  281. nb0 = nb + reg * 4 - 128;
  282. nb = 128 - reg * 4;
  283. }
  284. #ifdef __LITTLE_ENDIAN__
  285. /*
  286. * String instructions are endian neutral but the code
  287. * below is not. Force byte swapping on so that the
  288. * effects of swizzling are undone in the load/store
  289. * loops below.
  290. */
  291. flags ^= SW;
  292. #endif
  293. } else {
  294. /* lwm, stmw */
  295. nb = (32 - reg) * 4;
  296. }
  297. if (!access_ok((flags & ST ? VERIFY_WRITE: VERIFY_READ), addr, nb+nb0))
  298. return -EFAULT; /* bad address */
  299. rptr = &regs->gpr[reg];
  300. p = (unsigned long) addr;
  301. bswiz = (flags & SW)? 3: 0;
  302. if (!(flags & ST)) {
  303. /*
  304. * This zeroes the top 4 bytes of the affected registers
  305. * in 64-bit mode, and also zeroes out any remaining
  306. * bytes of the last register for lsw*.
  307. */
  308. memset(rptr, 0, ((nb + 3) / 4) * sizeof(unsigned long));
  309. if (nb0 > 0)
  310. memset(&regs->gpr[0], 0,
  311. ((nb0 + 3) / 4) * sizeof(unsigned long));
  312. for (i = 0; i < nb; ++i, ++p)
  313. if (__get_user_or_set_dar(regs, REG_BYTE(rptr, i ^ bswiz),
  314. SWIZ_PTR(p)))
  315. return -EFAULT;
  316. if (nb0 > 0) {
  317. rptr = &regs->gpr[0];
  318. addr += nb;
  319. for (i = 0; i < nb0; ++i, ++p)
  320. if (__get_user_or_set_dar(regs,
  321. REG_BYTE(rptr, i ^ bswiz),
  322. SWIZ_PTR(p)))
  323. return -EFAULT;
  324. }
  325. } else {
  326. for (i = 0; i < nb; ++i, ++p)
  327. if (__put_user_or_set_dar(regs, REG_BYTE(rptr, i ^ bswiz),
  328. SWIZ_PTR(p)))
  329. return -EFAULT;
  330. if (nb0 > 0) {
  331. rptr = &regs->gpr[0];
  332. addr += nb;
  333. for (i = 0; i < nb0; ++i, ++p)
  334. if (__put_user_or_set_dar(regs,
  335. REG_BYTE(rptr, i ^ bswiz),
  336. SWIZ_PTR(p)))
  337. return -EFAULT;
  338. }
  339. }
  340. return 1;
  341. }
  342. /*
  343. * Emulate floating-point pair loads and stores.
  344. * Only POWER6 has these instructions, and it does true little-endian,
  345. * so we don't need the address swizzling.
  346. */
  347. static int emulate_fp_pair(struct pt_regs *regs, unsigned char __user *addr,
  348. unsigned int reg, unsigned int flags)
  349. {
  350. char *ptr0 = (char *) &current->thread.TS_FPR(reg);
  351. char *ptr1 = (char *) &current->thread.TS_FPR(reg+1);
  352. int i, sw = 0;
  353. if (reg & 1)
  354. return 0; /* invalid form: FRS/FRT must be even */
  355. if (flags & SW)
  356. sw = 7;
  357. for (i = 0; i < 8; ++i) {
  358. if (!(flags & ST)) {
  359. if (__get_user_or_set_dar(regs, ptr0[i^sw], addr + i))
  360. return -EFAULT;
  361. if (__get_user_or_set_dar(regs, ptr1[i^sw], addr + i + 8))
  362. return -EFAULT;
  363. } else {
  364. if (__put_user_or_set_dar(regs, ptr0[i^sw], addr + i))
  365. return -EFAULT;
  366. if (__put_user_or_set_dar(regs, ptr1[i^sw], addr + i + 8))
  367. return -EFAULT;
  368. }
  369. }
  370. return 1; /* exception handled and fixed up */
  371. }
  372. #ifdef CONFIG_PPC64
  373. static int emulate_lq_stq(struct pt_regs *regs, unsigned char __user *addr,
  374. unsigned int reg, unsigned int flags)
  375. {
  376. char *ptr0 = (char *)&regs->gpr[reg];
  377. char *ptr1 = (char *)&regs->gpr[reg+1];
  378. int i, sw = 0;
  379. if (reg & 1)
  380. return 0; /* invalid form: GPR must be even */
  381. if (flags & SW)
  382. sw = 7;
  383. for (i = 0; i < 8; ++i) {
  384. if (!(flags & ST)) {
  385. if (__get_user_or_set_dar(regs, ptr0[i^sw], addr + i))
  386. return -EFAULT;
  387. if (__get_user_or_set_dar(regs, ptr1[i^sw], addr + i + 8))
  388. return -EFAULT;
  389. } else {
  390. if (__put_user_or_set_dar(regs, ptr0[i^sw], addr + i))
  391. return -EFAULT;
  392. if (__put_user_or_set_dar(regs, ptr1[i^sw], addr + i + 8))
  393. return -EFAULT;
  394. }
  395. }
  396. return 1; /* exception handled and fixed up */
  397. }
  398. #endif /* CONFIG_PPC64 */
  399. #ifdef CONFIG_SPE
  400. static struct aligninfo spe_aligninfo[32] = {
  401. { 8, LD+E8 }, /* 0 00 00: evldd[x] */
  402. { 8, LD+E4 }, /* 0 00 01: evldw[x] */
  403. { 8, LD }, /* 0 00 10: evldh[x] */
  404. INVALID, /* 0 00 11 */
  405. { 2, LD }, /* 0 01 00: evlhhesplat[x] */
  406. INVALID, /* 0 01 01 */
  407. { 2, LD }, /* 0 01 10: evlhhousplat[x] */
  408. { 2, LD+SE }, /* 0 01 11: evlhhossplat[x] */
  409. { 4, LD }, /* 0 10 00: evlwhe[x] */
  410. INVALID, /* 0 10 01 */
  411. { 4, LD }, /* 0 10 10: evlwhou[x] */
  412. { 4, LD+SE }, /* 0 10 11: evlwhos[x] */
  413. { 4, LD+E4 }, /* 0 11 00: evlwwsplat[x] */
  414. INVALID, /* 0 11 01 */
  415. { 4, LD }, /* 0 11 10: evlwhsplat[x] */
  416. INVALID, /* 0 11 11 */
  417. { 8, ST+E8 }, /* 1 00 00: evstdd[x] */
  418. { 8, ST+E4 }, /* 1 00 01: evstdw[x] */
  419. { 8, ST }, /* 1 00 10: evstdh[x] */
  420. INVALID, /* 1 00 11 */
  421. INVALID, /* 1 01 00 */
  422. INVALID, /* 1 01 01 */
  423. INVALID, /* 1 01 10 */
  424. INVALID, /* 1 01 11 */
  425. { 4, ST }, /* 1 10 00: evstwhe[x] */
  426. INVALID, /* 1 10 01 */
  427. { 4, ST }, /* 1 10 10: evstwho[x] */
  428. INVALID, /* 1 10 11 */
  429. { 4, ST+E4 }, /* 1 11 00: evstwwe[x] */
  430. INVALID, /* 1 11 01 */
  431. { 4, ST+E4 }, /* 1 11 10: evstwwo[x] */
  432. INVALID, /* 1 11 11 */
  433. };
  434. #define EVLDD 0x00
  435. #define EVLDW 0x01
  436. #define EVLDH 0x02
  437. #define EVLHHESPLAT 0x04
  438. #define EVLHHOUSPLAT 0x06
  439. #define EVLHHOSSPLAT 0x07
  440. #define EVLWHE 0x08
  441. #define EVLWHOU 0x0A
  442. #define EVLWHOS 0x0B
  443. #define EVLWWSPLAT 0x0C
  444. #define EVLWHSPLAT 0x0E
  445. #define EVSTDD 0x10
  446. #define EVSTDW 0x11
  447. #define EVSTDH 0x12
  448. #define EVSTWHE 0x18
  449. #define EVSTWHO 0x1A
  450. #define EVSTWWE 0x1C
  451. #define EVSTWWO 0x1E
  452. /*
  453. * Emulate SPE loads and stores.
  454. * Only Book-E has these instructions, and it does true little-endian,
  455. * so we don't need the address swizzling.
  456. */
  457. static int emulate_spe(struct pt_regs *regs, unsigned int reg,
  458. unsigned int instr)
  459. {
  460. int ret;
  461. union {
  462. u64 ll;
  463. u32 w[2];
  464. u16 h[4];
  465. u8 v[8];
  466. } data, temp;
  467. unsigned char __user *p, *addr;
  468. unsigned long *evr = &current->thread.evr[reg];
  469. unsigned int nb, flags;
  470. instr = (instr >> 1) & 0x1f;
  471. /* DAR has the operand effective address */
  472. addr = (unsigned char __user *)regs->dar;
  473. nb = spe_aligninfo[instr].len;
  474. flags = spe_aligninfo[instr].flags;
  475. /* Verify the address of the operand */
  476. if (unlikely(user_mode(regs) &&
  477. !access_ok((flags & ST ? VERIFY_WRITE : VERIFY_READ),
  478. addr, nb)))
  479. return -EFAULT;
  480. /* userland only */
  481. if (unlikely(!user_mode(regs)))
  482. return 0;
  483. flush_spe_to_thread(current);
  484. /* If we are loading, get the data from user space, else
  485. * get it from register values
  486. */
  487. if (flags & ST) {
  488. data.ll = 0;
  489. switch (instr) {
  490. case EVSTDD:
  491. case EVSTDW:
  492. case EVSTDH:
  493. data.w[0] = *evr;
  494. data.w[1] = regs->gpr[reg];
  495. break;
  496. case EVSTWHE:
  497. data.h[2] = *evr >> 16;
  498. data.h[3] = regs->gpr[reg] >> 16;
  499. break;
  500. case EVSTWHO:
  501. data.h[2] = *evr & 0xffff;
  502. data.h[3] = regs->gpr[reg] & 0xffff;
  503. break;
  504. case EVSTWWE:
  505. data.w[1] = *evr;
  506. break;
  507. case EVSTWWO:
  508. data.w[1] = regs->gpr[reg];
  509. break;
  510. default:
  511. return -EINVAL;
  512. }
  513. } else {
  514. temp.ll = data.ll = 0;
  515. ret = 0;
  516. p = addr;
  517. switch (nb) {
  518. case 8:
  519. ret |= __get_user_inatomic(temp.v[0], p++);
  520. ret |= __get_user_inatomic(temp.v[1], p++);
  521. ret |= __get_user_inatomic(temp.v[2], p++);
  522. ret |= __get_user_inatomic(temp.v[3], p++);
  523. case 4:
  524. ret |= __get_user_inatomic(temp.v[4], p++);
  525. ret |= __get_user_inatomic(temp.v[5], p++);
  526. case 2:
  527. ret |= __get_user_inatomic(temp.v[6], p++);
  528. ret |= __get_user_inatomic(temp.v[7], p++);
  529. if (unlikely(ret))
  530. return -EFAULT;
  531. }
  532. switch (instr) {
  533. case EVLDD:
  534. case EVLDW:
  535. case EVLDH:
  536. data.ll = temp.ll;
  537. break;
  538. case EVLHHESPLAT:
  539. data.h[0] = temp.h[3];
  540. data.h[2] = temp.h[3];
  541. break;
  542. case EVLHHOUSPLAT:
  543. case EVLHHOSSPLAT:
  544. data.h[1] = temp.h[3];
  545. data.h[3] = temp.h[3];
  546. break;
  547. case EVLWHE:
  548. data.h[0] = temp.h[2];
  549. data.h[2] = temp.h[3];
  550. break;
  551. case EVLWHOU:
  552. case EVLWHOS:
  553. data.h[1] = temp.h[2];
  554. data.h[3] = temp.h[3];
  555. break;
  556. case EVLWWSPLAT:
  557. data.w[0] = temp.w[1];
  558. data.w[1] = temp.w[1];
  559. break;
  560. case EVLWHSPLAT:
  561. data.h[0] = temp.h[2];
  562. data.h[1] = temp.h[2];
  563. data.h[2] = temp.h[3];
  564. data.h[3] = temp.h[3];
  565. break;
  566. default:
  567. return -EINVAL;
  568. }
  569. }
  570. if (flags & SW) {
  571. switch (flags & 0xf0) {
  572. case E8:
  573. data.ll = swab64(data.ll);
  574. break;
  575. case E4:
  576. data.w[0] = swab32(data.w[0]);
  577. data.w[1] = swab32(data.w[1]);
  578. break;
  579. /* Its half word endian */
  580. default:
  581. data.h[0] = swab16(data.h[0]);
  582. data.h[1] = swab16(data.h[1]);
  583. data.h[2] = swab16(data.h[2]);
  584. data.h[3] = swab16(data.h[3]);
  585. break;
  586. }
  587. }
  588. if (flags & SE) {
  589. data.w[0] = (s16)data.h[1];
  590. data.w[1] = (s16)data.h[3];
  591. }
  592. /* Store result to memory or update registers */
  593. if (flags & ST) {
  594. ret = 0;
  595. p = addr;
  596. switch (nb) {
  597. case 8:
  598. ret |= __put_user_inatomic(data.v[0], p++);
  599. ret |= __put_user_inatomic(data.v[1], p++);
  600. ret |= __put_user_inatomic(data.v[2], p++);
  601. ret |= __put_user_inatomic(data.v[3], p++);
  602. case 4:
  603. ret |= __put_user_inatomic(data.v[4], p++);
  604. ret |= __put_user_inatomic(data.v[5], p++);
  605. case 2:
  606. ret |= __put_user_inatomic(data.v[6], p++);
  607. ret |= __put_user_inatomic(data.v[7], p++);
  608. }
  609. if (unlikely(ret))
  610. return -EFAULT;
  611. } else {
  612. *evr = data.w[0];
  613. regs->gpr[reg] = data.w[1];
  614. }
  615. return 1;
  616. }
  617. #endif /* CONFIG_SPE */
  618. #ifdef CONFIG_VSX
  619. /*
  620. * Emulate VSX instructions...
  621. */
  622. static int emulate_vsx(unsigned char __user *addr, unsigned int reg,
  623. unsigned int areg, struct pt_regs *regs,
  624. unsigned int flags, unsigned int length,
  625. unsigned int elsize)
  626. {
  627. char *ptr;
  628. unsigned long *lptr;
  629. int ret = 0;
  630. int sw = 0;
  631. int i, j;
  632. /* userland only */
  633. if (unlikely(!user_mode(regs)))
  634. return 0;
  635. flush_vsx_to_thread(current);
  636. if (reg < 32)
  637. ptr = (char *) &current->thread.fp_state.fpr[reg][0];
  638. else
  639. ptr = (char *) &current->thread.vr_state.vr[reg - 32];
  640. lptr = (unsigned long *) ptr;
  641. #ifdef __LITTLE_ENDIAN__
  642. if (flags & SW) {
  643. elsize = length;
  644. sw = length-1;
  645. } else {
  646. /*
  647. * The elements are BE ordered, even in LE mode, so process
  648. * them in reverse order.
  649. */
  650. addr += length - elsize;
  651. /* 8 byte memory accesses go in the top 8 bytes of the VR */
  652. if (length == 8)
  653. ptr += 8;
  654. }
  655. #else
  656. if (flags & SW)
  657. sw = elsize-1;
  658. #endif
  659. for (j = 0; j < length; j += elsize) {
  660. for (i = 0; i < elsize; ++i) {
  661. if (flags & ST)
  662. ret = __put_user_or_set_dar(regs, ptr[i^sw],
  663. addr + i);
  664. else
  665. ret = __get_user_or_set_dar(regs, ptr[i^sw],
  666. addr + i);
  667. if (ret)
  668. return ret;
  669. }
  670. ptr += elsize;
  671. #ifdef __LITTLE_ENDIAN__
  672. addr -= elsize;
  673. #else
  674. addr += elsize;
  675. #endif
  676. }
  677. #ifdef __BIG_ENDIAN__
  678. #define VSX_HI 0
  679. #define VSX_LO 1
  680. #else
  681. #define VSX_HI 1
  682. #define VSX_LO 0
  683. #endif
  684. if (!ret) {
  685. if (flags & U)
  686. regs->gpr[areg] = regs->dar;
  687. /* Splat load copies the same data to top and bottom 8 bytes */
  688. if (flags & SPLT)
  689. lptr[VSX_LO] = lptr[VSX_HI];
  690. /* For 8 byte loads, zero the low 8 bytes */
  691. else if (!(flags & ST) && (8 == length))
  692. lptr[VSX_LO] = 0;
  693. } else
  694. return -EFAULT;
  695. return 1;
  696. }
  697. #endif
  698. /*
  699. * Called on alignment exception. Attempts to fixup
  700. *
  701. * Return 1 on success
  702. * Return 0 if unable to handle the interrupt
  703. * Return -EFAULT if data address is bad
  704. */
  705. int fix_alignment(struct pt_regs *regs)
  706. {
  707. unsigned int instr, nb, flags, instruction = 0;
  708. unsigned int reg, areg;
  709. unsigned int dsisr;
  710. unsigned char __user *addr;
  711. unsigned long p, swiz;
  712. int i;
  713. union data {
  714. u64 ll;
  715. double dd;
  716. unsigned char v[8];
  717. struct {
  718. #ifdef __LITTLE_ENDIAN__
  719. int low32;
  720. unsigned hi32;
  721. #else
  722. unsigned hi32;
  723. int low32;
  724. #endif
  725. } x32;
  726. struct {
  727. #ifdef __LITTLE_ENDIAN__
  728. short low16;
  729. unsigned char hi48[6];
  730. #else
  731. unsigned char hi48[6];
  732. short low16;
  733. #endif
  734. } x16;
  735. } data;
  736. /*
  737. * We require a complete register set, if not, then our assembly
  738. * is broken
  739. */
  740. CHECK_FULL_REGS(regs);
  741. dsisr = regs->dsisr;
  742. /* Some processors don't provide us with a DSISR we can use here,
  743. * let's make one up from the instruction
  744. */
  745. if (cpu_has_feature(CPU_FTR_NODSISRALIGN)) {
  746. unsigned long pc = regs->nip;
  747. if (cpu_has_feature(CPU_FTR_PPC_LE) && (regs->msr & MSR_LE))
  748. pc ^= 4;
  749. if (unlikely(__get_user_inatomic(instr,
  750. (unsigned int __user *)pc)))
  751. return -EFAULT;
  752. if (cpu_has_feature(CPU_FTR_REAL_LE) && (regs->msr & MSR_LE))
  753. instr = cpu_to_le32(instr);
  754. dsisr = make_dsisr(instr);
  755. instruction = instr;
  756. }
  757. /* extract the operation and registers from the dsisr */
  758. reg = (dsisr >> 5) & 0x1f; /* source/dest register */
  759. areg = dsisr & 0x1f; /* register to update */
  760. #ifdef CONFIG_SPE
  761. if ((instr >> 26) == 0x4) {
  762. PPC_WARN_ALIGNMENT(spe, regs);
  763. return emulate_spe(regs, reg, instr);
  764. }
  765. #endif
  766. instr = (dsisr >> 10) & 0x7f;
  767. instr |= (dsisr >> 13) & 0x60;
  768. /* Lookup the operation in our table */
  769. nb = aligninfo[instr].len;
  770. flags = aligninfo[instr].flags;
  771. /*
  772. * Handle some cases which give overlaps in the DSISR values.
  773. */
  774. if (IS_XFORM(instruction)) {
  775. switch (get_xop(instruction)) {
  776. case 532: /* ldbrx */
  777. nb = 8;
  778. flags = LD+SW;
  779. break;
  780. case 660: /* stdbrx */
  781. nb = 8;
  782. flags = ST+SW;
  783. break;
  784. case 20: /* lwarx */
  785. case 84: /* ldarx */
  786. case 116: /* lharx */
  787. case 276: /* lqarx */
  788. return 0; /* not emulated ever */
  789. }
  790. }
  791. /* Byteswap little endian loads and stores */
  792. swiz = 0;
  793. if ((regs->msr & MSR_LE) != (MSR_KERNEL & MSR_LE)) {
  794. flags ^= SW;
  795. #ifdef __BIG_ENDIAN__
  796. /*
  797. * So-called "PowerPC little endian" mode works by
  798. * swizzling addresses rather than by actually doing
  799. * any byte-swapping. To emulate this, we XOR each
  800. * byte address with 7. We also byte-swap, because
  801. * the processor's address swizzling depends on the
  802. * operand size (it xors the address with 7 for bytes,
  803. * 6 for halfwords, 4 for words, 0 for doublewords) but
  804. * we will xor with 7 and load/store each byte separately.
  805. */
  806. if (cpu_has_feature(CPU_FTR_PPC_LE))
  807. swiz = 7;
  808. #endif
  809. }
  810. /* DAR has the operand effective address */
  811. addr = (unsigned char __user *)regs->dar;
  812. #ifdef CONFIG_VSX
  813. if ((instruction & 0xfc00003e) == 0x7c000018) {
  814. unsigned int elsize;
  815. /* Additional register addressing bit (64 VSX vs 32 FPR/GPR) */
  816. reg |= (instruction & 0x1) << 5;
  817. /* Simple inline decoder instead of a table */
  818. /* VSX has only 8 and 16 byte memory accesses */
  819. nb = 8;
  820. if (instruction & 0x200)
  821. nb = 16;
  822. /* Vector stores in little-endian mode swap individual
  823. elements, so process them separately */
  824. elsize = 4;
  825. if (instruction & 0x80)
  826. elsize = 8;
  827. flags = 0;
  828. if ((regs->msr & MSR_LE) != (MSR_KERNEL & MSR_LE))
  829. flags |= SW;
  830. if (instruction & 0x100)
  831. flags |= ST;
  832. if (instruction & 0x040)
  833. flags |= U;
  834. /* splat load needs a special decoder */
  835. if ((instruction & 0x400) == 0){
  836. flags |= SPLT;
  837. nb = 8;
  838. }
  839. PPC_WARN_ALIGNMENT(vsx, regs);
  840. return emulate_vsx(addr, reg, areg, regs, flags, nb, elsize);
  841. }
  842. #endif
  843. /*
  844. * ISA 3.0 (such as P9) copy, copy_first, paste and paste_last alignment
  845. * check.
  846. *
  847. * Send a SIGBUS to the process that caused the fault.
  848. *
  849. * We do not emulate these because paste may contain additional metadata
  850. * when pasting to a co-processor. Furthermore, paste_last is the
  851. * synchronisation point for preceding copy/paste sequences.
  852. */
  853. if ((instruction & 0xfc0006fe) == PPC_INST_COPY)
  854. return -EIO;
  855. /* A size of 0 indicates an instruction we don't support, with
  856. * the exception of DCBZ which is handled as a special case here
  857. */
  858. if (instr == DCBZ) {
  859. PPC_WARN_ALIGNMENT(dcbz, regs);
  860. return emulate_dcbz(regs, addr);
  861. }
  862. if (unlikely(nb == 0))
  863. return 0;
  864. /* Load/Store Multiple instructions are handled in their own
  865. * function
  866. */
  867. if (flags & M) {
  868. PPC_WARN_ALIGNMENT(multiple, regs);
  869. return emulate_multiple(regs, addr, reg, nb,
  870. flags, instr, swiz);
  871. }
  872. /* Verify the address of the operand */
  873. if (unlikely(user_mode(regs) &&
  874. !access_ok((flags & ST ? VERIFY_WRITE : VERIFY_READ),
  875. addr, nb)))
  876. return -EFAULT;
  877. /* Force the fprs into the save area so we can reference them */
  878. if (flags & F) {
  879. /* userland only */
  880. if (unlikely(!user_mode(regs)))
  881. return 0;
  882. flush_fp_to_thread(current);
  883. }
  884. if (nb == 16) {
  885. if (flags & F) {
  886. /* Special case for 16-byte FP loads and stores */
  887. PPC_WARN_ALIGNMENT(fp_pair, regs);
  888. return emulate_fp_pair(regs, addr, reg, flags);
  889. } else {
  890. #ifdef CONFIG_PPC64
  891. /* Special case for 16-byte loads and stores */
  892. PPC_WARN_ALIGNMENT(lq_stq, regs);
  893. return emulate_lq_stq(regs, addr, reg, flags);
  894. #else
  895. return 0;
  896. #endif
  897. }
  898. }
  899. PPC_WARN_ALIGNMENT(unaligned, regs);
  900. /* If we are loading, get the data from user space, else
  901. * get it from register values
  902. */
  903. if (!(flags & ST)) {
  904. unsigned int start = 0;
  905. switch (nb) {
  906. case 4:
  907. start = offsetof(union data, x32.low32);
  908. break;
  909. case 2:
  910. start = offsetof(union data, x16.low16);
  911. break;
  912. }
  913. data.ll = 0;
  914. p = (unsigned long)addr;
  915. for (i = 0; i < nb; i++)
  916. if (__get_user_or_set_dar(regs, data.v[start + i],
  917. SWIZ_PTR(p++)))
  918. return -EFAULT;
  919. } else if (flags & F) {
  920. data.ll = current->thread.TS_FPR(reg);
  921. if (flags & S) {
  922. /* Single-precision FP store requires conversion... */
  923. #ifdef CONFIG_PPC_FPU
  924. preempt_disable();
  925. enable_kernel_fp();
  926. cvt_df(&data.dd, (float *)&data.x32.low32);
  927. disable_kernel_fp();
  928. preempt_enable();
  929. #else
  930. return 0;
  931. #endif
  932. }
  933. } else
  934. data.ll = regs->gpr[reg];
  935. if (flags & SW) {
  936. switch (nb) {
  937. case 8:
  938. data.ll = swab64(data.ll);
  939. break;
  940. case 4:
  941. data.x32.low32 = swab32(data.x32.low32);
  942. break;
  943. case 2:
  944. data.x16.low16 = swab16(data.x16.low16);
  945. break;
  946. }
  947. }
  948. /* Perform other misc operations like sign extension
  949. * or floating point single precision conversion
  950. */
  951. switch (flags & ~(U|SW)) {
  952. case LD+SE: /* sign extending integer loads */
  953. case LD+F+SE: /* sign extend for lfiwax */
  954. if ( nb == 2 )
  955. data.ll = data.x16.low16;
  956. else /* nb must be 4 */
  957. data.ll = data.x32.low32;
  958. break;
  959. /* Single-precision FP load requires conversion... */
  960. case LD+F+S:
  961. #ifdef CONFIG_PPC_FPU
  962. preempt_disable();
  963. enable_kernel_fp();
  964. cvt_fd((float *)&data.x32.low32, &data.dd);
  965. disable_kernel_fp();
  966. preempt_enable();
  967. #else
  968. return 0;
  969. #endif
  970. break;
  971. }
  972. /* Store result to memory or update registers */
  973. if (flags & ST) {
  974. unsigned int start = 0;
  975. switch (nb) {
  976. case 4:
  977. start = offsetof(union data, x32.low32);
  978. break;
  979. case 2:
  980. start = offsetof(union data, x16.low16);
  981. break;
  982. }
  983. p = (unsigned long)addr;
  984. for (i = 0; i < nb; i++)
  985. if (__put_user_or_set_dar(regs, data.v[start + i],
  986. SWIZ_PTR(p++)))
  987. return -EFAULT;
  988. } else if (flags & F)
  989. current->thread.TS_FPR(reg) = data.ll;
  990. else
  991. regs->gpr[reg] = data.ll;
  992. /* Update RA as needed */
  993. if (flags & U)
  994. regs->gpr[areg] = regs->dar;
  995. return 1;
  996. }