align.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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. struct aligninfo {
  29. unsigned char len;
  30. unsigned char flags;
  31. };
  32. #define INVALID { 0, 0 }
  33. /* Bits in the flags field */
  34. #define LD 0 /* load */
  35. #define ST 1 /* store */
  36. #define SE 2 /* sign-extend value, or FP ld/st as word */
  37. #define F 4 /* to/from fp regs */
  38. #define U 8 /* update index register */
  39. #define M 0x10 /* multiple load/store */
  40. #define SW 0x20 /* byte swap */
  41. #define S 0x40 /* single-precision fp or... */
  42. #define SX 0x40 /* ... byte count in XER */
  43. #define HARD 0x80 /* string, stwcx. */
  44. #define E4 0x40 /* SPE endianness is word */
  45. #define E8 0x80 /* SPE endianness is double word */
  46. #define SPLT 0x80 /* VSX SPLAT load */
  47. /* DSISR bits reported for a DCBZ instruction: */
  48. #define DCBZ 0x5f /* 8xx/82xx dcbz faults when cache not enabled */
  49. /*
  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. { 16, LD }, /* 00 0 1100: lq */
  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. { 16, ST }, /* 10 0 1111: stq */
  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. { 4, LD+F }, /* 11 1 1101: lfiwzx */
  182. INVALID, /* 11 1 1110 */
  183. INVALID, /* 11 1 1111 */
  184. };
  185. /*
  186. * The dcbz (data cache block zero) instruction
  187. * gives an alignment fault if used on non-cacheable
  188. * memory. We handle the fault mainly for the
  189. * case when we are running with the cache disabled
  190. * for debugging.
  191. */
  192. static int emulate_dcbz(struct pt_regs *regs, unsigned char __user *addr)
  193. {
  194. long __user *p;
  195. int i, size;
  196. #ifdef __powerpc64__
  197. size = ppc64_caches.dline_size;
  198. #else
  199. size = L1_CACHE_BYTES;
  200. #endif
  201. p = (long __user *) (regs->dar & -size);
  202. if (user_mode(regs) && !access_ok(VERIFY_WRITE, p, size))
  203. return -EFAULT;
  204. for (i = 0; i < size / sizeof(long); ++i)
  205. if (__put_user_inatomic(0, p+i))
  206. return -EFAULT;
  207. return 1;
  208. }
  209. /*
  210. * Emulate load & store multiple instructions
  211. * On 64-bit machines, these instructions only affect/use the
  212. * bottom 4 bytes of each register, and the loads clear the
  213. * top 4 bytes of the affected register.
  214. */
  215. #ifdef __BIG_ENDIAN__
  216. #ifdef CONFIG_PPC64
  217. #define REG_BYTE(rp, i) *((u8 *)((rp) + ((i) >> 2)) + ((i) & 3) + 4)
  218. #else
  219. #define REG_BYTE(rp, i) *((u8 *)(rp) + (i))
  220. #endif
  221. #endif
  222. #ifdef __LITTLE_ENDIAN__
  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. /* A size of 0 indicates an instruction we don't support, with
  844. * the exception of DCBZ which is handled as a special case here
  845. */
  846. if (instr == DCBZ) {
  847. PPC_WARN_ALIGNMENT(dcbz, regs);
  848. return emulate_dcbz(regs, addr);
  849. }
  850. if (unlikely(nb == 0))
  851. return 0;
  852. /* Load/Store Multiple instructions are handled in their own
  853. * function
  854. */
  855. if (flags & M) {
  856. PPC_WARN_ALIGNMENT(multiple, regs);
  857. return emulate_multiple(regs, addr, reg, nb,
  858. flags, instr, swiz);
  859. }
  860. /* Verify the address of the operand */
  861. if (unlikely(user_mode(regs) &&
  862. !access_ok((flags & ST ? VERIFY_WRITE : VERIFY_READ),
  863. addr, nb)))
  864. return -EFAULT;
  865. /* Force the fprs into the save area so we can reference them */
  866. if (flags & F) {
  867. /* userland only */
  868. if (unlikely(!user_mode(regs)))
  869. return 0;
  870. flush_fp_to_thread(current);
  871. }
  872. if (nb == 16) {
  873. if (flags & F) {
  874. /* Special case for 16-byte FP loads and stores */
  875. PPC_WARN_ALIGNMENT(fp_pair, regs);
  876. return emulate_fp_pair(regs, addr, reg, flags);
  877. } else {
  878. #ifdef CONFIG_PPC64
  879. /* Special case for 16-byte loads and stores */
  880. PPC_WARN_ALIGNMENT(lq_stq, regs);
  881. return emulate_lq_stq(regs, addr, reg, flags);
  882. #else
  883. return 0;
  884. #endif
  885. }
  886. }
  887. PPC_WARN_ALIGNMENT(unaligned, regs);
  888. /* If we are loading, get the data from user space, else
  889. * get it from register values
  890. */
  891. if (!(flags & ST)) {
  892. unsigned int start = 0;
  893. switch (nb) {
  894. case 4:
  895. start = offsetof(union data, x32.low32);
  896. break;
  897. case 2:
  898. start = offsetof(union data, x16.low16);
  899. break;
  900. }
  901. data.ll = 0;
  902. p = (unsigned long)addr;
  903. for (i = 0; i < nb; i++)
  904. if (__get_user_or_set_dar(regs, data.v[start + i],
  905. SWIZ_PTR(p++)))
  906. return -EFAULT;
  907. } else if (flags & F) {
  908. data.ll = current->thread.TS_FPR(reg);
  909. if (flags & S) {
  910. /* Single-precision FP store requires conversion... */
  911. #ifdef CONFIG_PPC_FPU
  912. preempt_disable();
  913. enable_kernel_fp();
  914. cvt_df(&data.dd, (float *)&data.x32.low32);
  915. preempt_enable();
  916. #else
  917. return 0;
  918. #endif
  919. }
  920. } else
  921. data.ll = regs->gpr[reg];
  922. if (flags & SW) {
  923. switch (nb) {
  924. case 8:
  925. data.ll = swab64(data.ll);
  926. break;
  927. case 4:
  928. data.x32.low32 = swab32(data.x32.low32);
  929. break;
  930. case 2:
  931. data.x16.low16 = swab16(data.x16.low16);
  932. break;
  933. }
  934. }
  935. /* Perform other misc operations like sign extension
  936. * or floating point single precision conversion
  937. */
  938. switch (flags & ~(U|SW)) {
  939. case LD+SE: /* sign extending integer loads */
  940. case LD+F+SE: /* sign extend for lfiwax */
  941. if ( nb == 2 )
  942. data.ll = data.x16.low16;
  943. else /* nb must be 4 */
  944. data.ll = data.x32.low32;
  945. break;
  946. /* Single-precision FP load requires conversion... */
  947. case LD+F+S:
  948. #ifdef CONFIG_PPC_FPU
  949. preempt_disable();
  950. enable_kernel_fp();
  951. cvt_fd((float *)&data.x32.low32, &data.dd);
  952. preempt_enable();
  953. #else
  954. return 0;
  955. #endif
  956. break;
  957. }
  958. /* Store result to memory or update registers */
  959. if (flags & ST) {
  960. unsigned int start = 0;
  961. switch (nb) {
  962. case 4:
  963. start = offsetof(union data, x32.low32);
  964. break;
  965. case 2:
  966. start = offsetof(union data, x16.low16);
  967. break;
  968. }
  969. p = (unsigned long)addr;
  970. for (i = 0; i < nb; i++)
  971. if (__put_user_or_set_dar(regs, data.v[start + i],
  972. SWIZ_PTR(p++)))
  973. return -EFAULT;
  974. } else if (flags & F)
  975. current->thread.TS_FPR(reg) = data.ll;
  976. else
  977. regs->gpr[reg] = data.ll;
  978. /* Update RA as needed */
  979. if (flags & U)
  980. regs->gpr[areg] = regs->dar;
  981. return 1;
  982. }