align.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /* align.c - handle alignment exceptions for the Power PC.
  2. *
  3. * Copyright (c) 1996 Paul Mackerras <paulus@cs.anu.edu.au>
  4. * Copyright (c) 1998-1999 TiVo, Inc.
  5. * PowerPC 403GCX modifications.
  6. * Copyright (c) 1999 Grant Erickson <grant@lcse.umn.edu>
  7. * PowerPC 403GCX/405GP modifications.
  8. * Copyright (c) 2001-2002 PPC64 team, IBM Corp
  9. * 64-bit and Power4 support
  10. * Copyright (c) 2005 Benjamin Herrenschmidt, IBM Corp
  11. * <benh@kernel.crashing.org>
  12. * Merge ppc32 and ppc64 implementations
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/mm.h>
  21. #include <asm/processor.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/system.h>
  24. #include <asm/cache.h>
  25. #include <asm/cputable.h>
  26. struct aligninfo {
  27. unsigned char len;
  28. unsigned char flags;
  29. };
  30. #define IS_XFORM(inst) (((inst) >> 26) == 31)
  31. #define IS_DSFORM(inst) (((inst) >> 26) >= 56)
  32. #define INVALID { 0, 0 }
  33. #define LD 1 /* load */
  34. #define ST 2 /* store */
  35. #define SE 4 /* sign-extend value */
  36. #define F 8 /* to/from fp regs */
  37. #define U 0x10 /* update index register */
  38. #define M 0x20 /* multiple load/store */
  39. #define SW 0x40 /* byte swap int or ... */
  40. #define S 0x40 /* ... single-precision fp */
  41. #define SX 0x40 /* byte count in XER */
  42. #define HARD 0x80 /* string, stwcx. */
  43. #define DCBZ 0x5f /* 8xx/82xx dcbz faults when cache not enabled */
  44. #define SWAP(a, b) (t = (a), (a) = (b), (b) = t)
  45. /*
  46. * The PowerPC stores certain bits of the instruction that caused the
  47. * alignment exception in the DSISR register. This array maps those
  48. * bits to information about the operand length and what the
  49. * instruction would do.
  50. */
  51. static struct aligninfo aligninfo[128] = {
  52. { 4, LD }, /* 00 0 0000: lwz / lwarx */
  53. INVALID, /* 00 0 0001 */
  54. { 4, ST }, /* 00 0 0010: stw */
  55. INVALID, /* 00 0 0011 */
  56. { 2, LD }, /* 00 0 0100: lhz */
  57. { 2, LD+SE }, /* 00 0 0101: lha */
  58. { 2, ST }, /* 00 0 0110: sth */
  59. { 4, LD+M }, /* 00 0 0111: lmw */
  60. { 4, LD+F+S }, /* 00 0 1000: lfs */
  61. { 8, LD+F }, /* 00 0 1001: lfd */
  62. { 4, ST+F+S }, /* 00 0 1010: stfs */
  63. { 8, ST+F }, /* 00 0 1011: stfd */
  64. INVALID, /* 00 0 1100 */
  65. { 8, LD }, /* 00 0 1101: ld/ldu/lwa */
  66. INVALID, /* 00 0 1110 */
  67. { 8, ST }, /* 00 0 1111: std/stdu */
  68. { 4, LD+U }, /* 00 1 0000: lwzu */
  69. INVALID, /* 00 1 0001 */
  70. { 4, ST+U }, /* 00 1 0010: stwu */
  71. INVALID, /* 00 1 0011 */
  72. { 2, LD+U }, /* 00 1 0100: lhzu */
  73. { 2, LD+SE+U }, /* 00 1 0101: lhau */
  74. { 2, ST+U }, /* 00 1 0110: sthu */
  75. { 4, ST+M }, /* 00 1 0111: stmw */
  76. { 4, LD+F+S+U }, /* 00 1 1000: lfsu */
  77. { 8, LD+F+U }, /* 00 1 1001: lfdu */
  78. { 4, ST+F+S+U }, /* 00 1 1010: stfsu */
  79. { 8, ST+F+U }, /* 00 1 1011: stfdu */
  80. INVALID, /* 00 1 1100 */
  81. INVALID, /* 00 1 1101 */
  82. INVALID, /* 00 1 1110 */
  83. INVALID, /* 00 1 1111 */
  84. { 8, LD }, /* 01 0 0000: ldx */
  85. INVALID, /* 01 0 0001 */
  86. { 8, ST }, /* 01 0 0010: stdx */
  87. INVALID, /* 01 0 0011 */
  88. INVALID, /* 01 0 0100 */
  89. { 4, LD+SE }, /* 01 0 0101: lwax */
  90. INVALID, /* 01 0 0110 */
  91. INVALID, /* 01 0 0111 */
  92. { 4, LD+M+HARD+SX }, /* 01 0 1000: lswx */
  93. { 4, LD+M+HARD }, /* 01 0 1001: lswi */
  94. { 4, ST+M+HARD+SX }, /* 01 0 1010: stswx */
  95. { 4, ST+M+HARD }, /* 01 0 1011: stswi */
  96. INVALID, /* 01 0 1100 */
  97. { 8, LD+U }, /* 01 0 1101: ldu */
  98. INVALID, /* 01 0 1110 */
  99. { 8, ST+U }, /* 01 0 1111: stdu */
  100. { 8, LD+U }, /* 01 1 0000: ldux */
  101. INVALID, /* 01 1 0001 */
  102. { 8, ST+U }, /* 01 1 0010: stdux */
  103. INVALID, /* 01 1 0011 */
  104. INVALID, /* 01 1 0100 */
  105. { 4, LD+SE+U }, /* 01 1 0101: lwaux */
  106. INVALID, /* 01 1 0110 */
  107. INVALID, /* 01 1 0111 */
  108. INVALID, /* 01 1 1000 */
  109. INVALID, /* 01 1 1001 */
  110. INVALID, /* 01 1 1010 */
  111. INVALID, /* 01 1 1011 */
  112. INVALID, /* 01 1 1100 */
  113. INVALID, /* 01 1 1101 */
  114. INVALID, /* 01 1 1110 */
  115. INVALID, /* 01 1 1111 */
  116. INVALID, /* 10 0 0000 */
  117. INVALID, /* 10 0 0001 */
  118. INVALID, /* 10 0 0010: stwcx. */
  119. INVALID, /* 10 0 0011 */
  120. INVALID, /* 10 0 0100 */
  121. INVALID, /* 10 0 0101 */
  122. INVALID, /* 10 0 0110 */
  123. INVALID, /* 10 0 0111 */
  124. { 4, LD+SW }, /* 10 0 1000: lwbrx */
  125. INVALID, /* 10 0 1001 */
  126. { 4, ST+SW }, /* 10 0 1010: stwbrx */
  127. INVALID, /* 10 0 1011 */
  128. { 2, LD+SW }, /* 10 0 1100: lhbrx */
  129. { 4, LD+SE }, /* 10 0 1101 lwa */
  130. { 2, ST+SW }, /* 10 0 1110: sthbrx */
  131. INVALID, /* 10 0 1111 */
  132. INVALID, /* 10 1 0000 */
  133. INVALID, /* 10 1 0001 */
  134. INVALID, /* 10 1 0010 */
  135. INVALID, /* 10 1 0011 */
  136. INVALID, /* 10 1 0100 */
  137. INVALID, /* 10 1 0101 */
  138. INVALID, /* 10 1 0110 */
  139. INVALID, /* 10 1 0111 */
  140. INVALID, /* 10 1 1000 */
  141. INVALID, /* 10 1 1001 */
  142. INVALID, /* 10 1 1010 */
  143. INVALID, /* 10 1 1011 */
  144. INVALID, /* 10 1 1100 */
  145. INVALID, /* 10 1 1101 */
  146. INVALID, /* 10 1 1110 */
  147. { 0, ST+HARD }, /* 10 1 1111: dcbz */
  148. { 4, LD }, /* 11 0 0000: lwzx */
  149. INVALID, /* 11 0 0001 */
  150. { 4, ST }, /* 11 0 0010: stwx */
  151. INVALID, /* 11 0 0011 */
  152. { 2, LD }, /* 11 0 0100: lhzx */
  153. { 2, LD+SE }, /* 11 0 0101: lhax */
  154. { 2, ST }, /* 11 0 0110: sthx */
  155. INVALID, /* 11 0 0111 */
  156. { 4, LD+F+S }, /* 11 0 1000: lfsx */
  157. { 8, LD+F }, /* 11 0 1001: lfdx */
  158. { 4, ST+F+S }, /* 11 0 1010: stfsx */
  159. { 8, ST+F }, /* 11 0 1011: stfdx */
  160. INVALID, /* 11 0 1100 */
  161. { 8, LD+M }, /* 11 0 1101: lmd */
  162. INVALID, /* 11 0 1110 */
  163. { 8, ST+M }, /* 11 0 1111: stmd */
  164. { 4, LD+U }, /* 11 1 0000: lwzux */
  165. INVALID, /* 11 1 0001 */
  166. { 4, ST+U }, /* 11 1 0010: stwux */
  167. INVALID, /* 11 1 0011 */
  168. { 2, LD+U }, /* 11 1 0100: lhzux */
  169. { 2, LD+SE+U }, /* 11 1 0101: lhaux */
  170. { 2, ST+U }, /* 11 1 0110: sthux */
  171. INVALID, /* 11 1 0111 */
  172. { 4, LD+F+S+U }, /* 11 1 1000: lfsux */
  173. { 8, LD+F+U }, /* 11 1 1001: lfdux */
  174. { 4, ST+F+S+U }, /* 11 1 1010: stfsux */
  175. { 8, ST+F+U }, /* 11 1 1011: stfdux */
  176. INVALID, /* 11 1 1100 */
  177. INVALID, /* 11 1 1101 */
  178. INVALID, /* 11 1 1110 */
  179. INVALID, /* 11 1 1111 */
  180. };
  181. /*
  182. * Create a DSISR value from the instruction
  183. */
  184. static inline unsigned make_dsisr(unsigned instr)
  185. {
  186. unsigned dsisr;
  187. /* bits 6:15 --> 22:31 */
  188. dsisr = (instr & 0x03ff0000) >> 16;
  189. if (IS_XFORM(instr)) {
  190. /* bits 29:30 --> 15:16 */
  191. dsisr |= (instr & 0x00000006) << 14;
  192. /* bit 25 --> 17 */
  193. dsisr |= (instr & 0x00000040) << 8;
  194. /* bits 21:24 --> 18:21 */
  195. dsisr |= (instr & 0x00000780) << 3;
  196. } else {
  197. /* bit 5 --> 17 */
  198. dsisr |= (instr & 0x04000000) >> 12;
  199. /* bits 1: 4 --> 18:21 */
  200. dsisr |= (instr & 0x78000000) >> 17;
  201. /* bits 30:31 --> 12:13 */
  202. if (IS_DSFORM(instr))
  203. dsisr |= (instr & 0x00000003) << 18;
  204. }
  205. return dsisr;
  206. }
  207. /*
  208. * The dcbz (data cache block zero) instruction
  209. * gives an alignment fault if used on non-cacheable
  210. * memory. We handle the fault mainly for the
  211. * case when we are running with the cache disabled
  212. * for debugging.
  213. */
  214. static int emulate_dcbz(struct pt_regs *regs, unsigned char __user *addr)
  215. {
  216. long __user *p;
  217. int i, size;
  218. #ifdef __powerpc64__
  219. size = ppc64_caches.dline_size;
  220. #else
  221. size = L1_CACHE_BYTES;
  222. #endif
  223. p = (long __user *) (regs->dar & -size);
  224. if (user_mode(regs) && !access_ok(VERIFY_WRITE, p, size))
  225. return -EFAULT;
  226. for (i = 0; i < size / sizeof(long); ++i)
  227. if (__put_user(0, p+i))
  228. return -EFAULT;
  229. return 1;
  230. }
  231. /*
  232. * Emulate load & store multiple instructions
  233. * On 64-bit machines, these instructions only affect/use the
  234. * bottom 4 bytes of each register, and the loads clear the
  235. * top 4 bytes of the affected register.
  236. */
  237. #ifdef CONFIG_PPC64
  238. #define REG_BYTE(rp, i) *((u8 *)((rp) + ((i) >> 2)) + ((i) & 3) + 4)
  239. #else
  240. #define REG_BYTE(rp, i) *((u8 *)(rp) + (i))
  241. #endif
  242. static int emulate_multiple(struct pt_regs *regs, unsigned char __user *addr,
  243. unsigned int reg, unsigned int nb,
  244. unsigned int flags, unsigned int instr)
  245. {
  246. unsigned long *rptr;
  247. unsigned int nb0, i;
  248. /*
  249. * We do not try to emulate 8 bytes multiple as they aren't really
  250. * available in our operating environments and we don't try to
  251. * emulate multiples operations in kernel land as they should never
  252. * be used/generated there at least not on unaligned boundaries
  253. */
  254. if (unlikely((nb > 4) || !user_mode(regs)))
  255. return 0;
  256. /* lmw, stmw, lswi/x, stswi/x */
  257. nb0 = 0;
  258. if (flags & HARD) {
  259. if (flags & SX) {
  260. nb = regs->xer & 127;
  261. if (nb == 0)
  262. return 1;
  263. } else {
  264. if (__get_user(instr,
  265. (unsigned int __user *)regs->nip))
  266. return -EFAULT;
  267. nb = (instr >> 11) & 0x1f;
  268. if (nb == 0)
  269. nb = 32;
  270. }
  271. if (nb + reg * 4 > 128) {
  272. nb0 = nb + reg * 4 - 128;
  273. nb = 128 - reg * 4;
  274. }
  275. } else {
  276. /* lwm, stmw */
  277. nb = (32 - reg) * 4;
  278. }
  279. if (!access_ok((flags & ST ? VERIFY_WRITE: VERIFY_READ), addr, nb+nb0))
  280. return -EFAULT; /* bad address */
  281. rptr = &regs->gpr[reg];
  282. if (flags & LD) {
  283. /*
  284. * This zeroes the top 4 bytes of the affected registers
  285. * in 64-bit mode, and also zeroes out any remaining
  286. * bytes of the last register for lsw*.
  287. */
  288. memset(rptr, 0, ((nb + 3) / 4) * sizeof(unsigned long));
  289. if (nb0 > 0)
  290. memset(&regs->gpr[0], 0,
  291. ((nb0 + 3) / 4) * sizeof(unsigned long));
  292. for (i = 0; i < nb; ++i)
  293. if (__get_user(REG_BYTE(rptr, i), addr + i))
  294. return -EFAULT;
  295. if (nb0 > 0) {
  296. rptr = &regs->gpr[0];
  297. addr += nb;
  298. for (i = 0; i < nb0; ++i)
  299. if (__get_user(REG_BYTE(rptr, i), addr + i))
  300. return -EFAULT;
  301. }
  302. } else {
  303. for (i = 0; i < nb; ++i)
  304. if (__put_user(REG_BYTE(rptr, i), addr + i))
  305. return -EFAULT;
  306. if (nb0 > 0) {
  307. rptr = &regs->gpr[0];
  308. addr += nb;
  309. for (i = 0; i < nb0; ++i)
  310. if (__put_user(REG_BYTE(rptr, i), addr + i))
  311. return -EFAULT;
  312. }
  313. }
  314. return 1;
  315. }
  316. /*
  317. * Called on alignment exception. Attempts to fixup
  318. *
  319. * Return 1 on success
  320. * Return 0 if unable to handle the interrupt
  321. * Return -EFAULT if data address is bad
  322. */
  323. int fix_alignment(struct pt_regs *regs)
  324. {
  325. unsigned int instr, nb, flags;
  326. unsigned int reg, areg;
  327. unsigned int dsisr;
  328. unsigned char __user *addr;
  329. unsigned char __user *p;
  330. int ret, t;
  331. union {
  332. u64 ll;
  333. double dd;
  334. unsigned char v[8];
  335. struct {
  336. unsigned hi32;
  337. int low32;
  338. } x32;
  339. struct {
  340. unsigned char hi48[6];
  341. short low16;
  342. } x16;
  343. } data;
  344. /*
  345. * We require a complete register set, if not, then our assembly
  346. * is broken
  347. */
  348. CHECK_FULL_REGS(regs);
  349. dsisr = regs->dsisr;
  350. /* Some processors don't provide us with a DSISR we can use here,
  351. * let's make one up from the instruction
  352. */
  353. if (cpu_has_feature(CPU_FTR_NODSISRALIGN)) {
  354. unsigned int real_instr;
  355. if (unlikely(__get_user(real_instr,
  356. (unsigned int __user *)regs->nip)))
  357. return -EFAULT;
  358. dsisr = make_dsisr(real_instr);
  359. }
  360. /* extract the operation and registers from the dsisr */
  361. reg = (dsisr >> 5) & 0x1f; /* source/dest register */
  362. areg = dsisr & 0x1f; /* register to update */
  363. instr = (dsisr >> 10) & 0x7f;
  364. instr |= (dsisr >> 13) & 0x60;
  365. /* Lookup the operation in our table */
  366. nb = aligninfo[instr].len;
  367. flags = aligninfo[instr].flags;
  368. /* DAR has the operand effective address */
  369. addr = (unsigned char __user *)regs->dar;
  370. /* A size of 0 indicates an instruction we don't support, with
  371. * the exception of DCBZ which is handled as a special case here
  372. */
  373. if (instr == DCBZ)
  374. return emulate_dcbz(regs, addr);
  375. if (unlikely(nb == 0))
  376. return 0;
  377. /* Load/Store Multiple instructions are handled in their own
  378. * function
  379. */
  380. if (flags & M)
  381. return emulate_multiple(regs, addr, reg, nb, flags, instr);
  382. /* Verify the address of the operand */
  383. if (unlikely(user_mode(regs) &&
  384. !access_ok((flags & ST ? VERIFY_WRITE : VERIFY_READ),
  385. addr, nb)))
  386. return -EFAULT;
  387. /* Force the fprs into the save area so we can reference them */
  388. if (flags & F) {
  389. /* userland only */
  390. if (unlikely(!user_mode(regs)))
  391. return 0;
  392. flush_fp_to_thread(current);
  393. }
  394. /* If we are loading, get the data from user space, else
  395. * get it from register values
  396. */
  397. if (flags & LD) {
  398. data.ll = 0;
  399. ret = 0;
  400. p = addr;
  401. switch (nb) {
  402. case 8:
  403. ret |= __get_user(data.v[0], p++);
  404. ret |= __get_user(data.v[1], p++);
  405. ret |= __get_user(data.v[2], p++);
  406. ret |= __get_user(data.v[3], p++);
  407. case 4:
  408. ret |= __get_user(data.v[4], p++);
  409. ret |= __get_user(data.v[5], p++);
  410. case 2:
  411. ret |= __get_user(data.v[6], p++);
  412. ret |= __get_user(data.v[7], p++);
  413. if (unlikely(ret))
  414. return -EFAULT;
  415. }
  416. } else if (flags & F)
  417. data.dd = current->thread.fpr[reg];
  418. else
  419. data.ll = regs->gpr[reg];
  420. /* Perform other misc operations like sign extension, byteswap,
  421. * or floating point single precision conversion
  422. */
  423. switch (flags & ~U) {
  424. case LD+SE: /* sign extend */
  425. if ( nb == 2 )
  426. data.ll = data.x16.low16;
  427. else /* nb must be 4 */
  428. data.ll = data.x32.low32;
  429. break;
  430. case LD+S: /* byte-swap */
  431. case ST+S:
  432. if (nb == 2) {
  433. SWAP(data.v[6], data.v[7]);
  434. } else {
  435. SWAP(data.v[4], data.v[7]);
  436. SWAP(data.v[5], data.v[6]);
  437. }
  438. break;
  439. /* Single-precision FP load and store require conversions... */
  440. case LD+F+S:
  441. #ifdef CONFIG_PPC_FPU
  442. preempt_disable();
  443. enable_kernel_fp();
  444. cvt_fd((float *)&data.v[4], &data.dd, &current->thread);
  445. preempt_enable();
  446. #else
  447. return 0;
  448. #endif
  449. break;
  450. case ST+F+S:
  451. #ifdef CONFIG_PPC_FPU
  452. preempt_disable();
  453. enable_kernel_fp();
  454. cvt_df(&data.dd, (float *)&data.v[4], &current->thread);
  455. preempt_enable();
  456. #else
  457. return 0;
  458. #endif
  459. break;
  460. }
  461. /* Store result to memory or update registers */
  462. if (flags & ST) {
  463. ret = 0;
  464. p = addr;
  465. switch (nb) {
  466. case 8:
  467. ret |= __put_user(data.v[0], p++);
  468. ret |= __put_user(data.v[1], p++);
  469. ret |= __put_user(data.v[2], p++);
  470. ret |= __put_user(data.v[3], p++);
  471. case 4:
  472. ret |= __put_user(data.v[4], p++);
  473. ret |= __put_user(data.v[5], p++);
  474. case 2:
  475. ret |= __put_user(data.v[6], p++);
  476. ret |= __put_user(data.v[7], p++);
  477. }
  478. if (unlikely(ret))
  479. return -EFAULT;
  480. } else if (flags & F)
  481. current->thread.fpr[reg] = data.dd;
  482. else
  483. regs->gpr[reg] = data.ll;
  484. /* Update RA as needed */
  485. if (flags & U)
  486. regs->gpr[areg] = regs->dar;
  487. return 1;
  488. }