align.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 <linux/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. #include <asm/sstep.h>
  30. struct aligninfo {
  31. unsigned char len;
  32. unsigned char flags;
  33. };
  34. #define INVALID { 0, 0 }
  35. /* Bits in the flags field */
  36. #define LD 0 /* load */
  37. #define ST 1 /* store */
  38. #define SE 2 /* sign-extend value, or FP ld/st as word */
  39. #define SW 0x20 /* byte swap */
  40. #define E4 0x40 /* SPE endianness is word */
  41. #define E8 0x80 /* SPE endianness is double word */
  42. #ifdef CONFIG_SPE
  43. static struct aligninfo spe_aligninfo[32] = {
  44. { 8, LD+E8 }, /* 0 00 00: evldd[x] */
  45. { 8, LD+E4 }, /* 0 00 01: evldw[x] */
  46. { 8, LD }, /* 0 00 10: evldh[x] */
  47. INVALID, /* 0 00 11 */
  48. { 2, LD }, /* 0 01 00: evlhhesplat[x] */
  49. INVALID, /* 0 01 01 */
  50. { 2, LD }, /* 0 01 10: evlhhousplat[x] */
  51. { 2, LD+SE }, /* 0 01 11: evlhhossplat[x] */
  52. { 4, LD }, /* 0 10 00: evlwhe[x] */
  53. INVALID, /* 0 10 01 */
  54. { 4, LD }, /* 0 10 10: evlwhou[x] */
  55. { 4, LD+SE }, /* 0 10 11: evlwhos[x] */
  56. { 4, LD+E4 }, /* 0 11 00: evlwwsplat[x] */
  57. INVALID, /* 0 11 01 */
  58. { 4, LD }, /* 0 11 10: evlwhsplat[x] */
  59. INVALID, /* 0 11 11 */
  60. { 8, ST+E8 }, /* 1 00 00: evstdd[x] */
  61. { 8, ST+E4 }, /* 1 00 01: evstdw[x] */
  62. { 8, ST }, /* 1 00 10: evstdh[x] */
  63. INVALID, /* 1 00 11 */
  64. INVALID, /* 1 01 00 */
  65. INVALID, /* 1 01 01 */
  66. INVALID, /* 1 01 10 */
  67. INVALID, /* 1 01 11 */
  68. { 4, ST }, /* 1 10 00: evstwhe[x] */
  69. INVALID, /* 1 10 01 */
  70. { 4, ST }, /* 1 10 10: evstwho[x] */
  71. INVALID, /* 1 10 11 */
  72. { 4, ST+E4 }, /* 1 11 00: evstwwe[x] */
  73. INVALID, /* 1 11 01 */
  74. { 4, ST+E4 }, /* 1 11 10: evstwwo[x] */
  75. INVALID, /* 1 11 11 */
  76. };
  77. #define EVLDD 0x00
  78. #define EVLDW 0x01
  79. #define EVLDH 0x02
  80. #define EVLHHESPLAT 0x04
  81. #define EVLHHOUSPLAT 0x06
  82. #define EVLHHOSSPLAT 0x07
  83. #define EVLWHE 0x08
  84. #define EVLWHOU 0x0A
  85. #define EVLWHOS 0x0B
  86. #define EVLWWSPLAT 0x0C
  87. #define EVLWHSPLAT 0x0E
  88. #define EVSTDD 0x10
  89. #define EVSTDW 0x11
  90. #define EVSTDH 0x12
  91. #define EVSTWHE 0x18
  92. #define EVSTWHO 0x1A
  93. #define EVSTWWE 0x1C
  94. #define EVSTWWO 0x1E
  95. /*
  96. * Emulate SPE loads and stores.
  97. * Only Book-E has these instructions, and it does true little-endian,
  98. * so we don't need the address swizzling.
  99. */
  100. static int emulate_spe(struct pt_regs *regs, unsigned int reg,
  101. unsigned int instr)
  102. {
  103. int ret;
  104. union {
  105. u64 ll;
  106. u32 w[2];
  107. u16 h[4];
  108. u8 v[8];
  109. } data, temp;
  110. unsigned char __user *p, *addr;
  111. unsigned long *evr = &current->thread.evr[reg];
  112. unsigned int nb, flags;
  113. instr = (instr >> 1) & 0x1f;
  114. /* DAR has the operand effective address */
  115. addr = (unsigned char __user *)regs->dar;
  116. nb = spe_aligninfo[instr].len;
  117. flags = spe_aligninfo[instr].flags;
  118. /* Verify the address of the operand */
  119. if (unlikely(user_mode(regs) &&
  120. !access_ok((flags & ST ? VERIFY_WRITE : VERIFY_READ),
  121. addr, nb)))
  122. return -EFAULT;
  123. /* userland only */
  124. if (unlikely(!user_mode(regs)))
  125. return 0;
  126. flush_spe_to_thread(current);
  127. /* If we are loading, get the data from user space, else
  128. * get it from register values
  129. */
  130. if (flags & ST) {
  131. data.ll = 0;
  132. switch (instr) {
  133. case EVSTDD:
  134. case EVSTDW:
  135. case EVSTDH:
  136. data.w[0] = *evr;
  137. data.w[1] = regs->gpr[reg];
  138. break;
  139. case EVSTWHE:
  140. data.h[2] = *evr >> 16;
  141. data.h[3] = regs->gpr[reg] >> 16;
  142. break;
  143. case EVSTWHO:
  144. data.h[2] = *evr & 0xffff;
  145. data.h[3] = regs->gpr[reg] & 0xffff;
  146. break;
  147. case EVSTWWE:
  148. data.w[1] = *evr;
  149. break;
  150. case EVSTWWO:
  151. data.w[1] = regs->gpr[reg];
  152. break;
  153. default:
  154. return -EINVAL;
  155. }
  156. } else {
  157. temp.ll = data.ll = 0;
  158. ret = 0;
  159. p = addr;
  160. switch (nb) {
  161. case 8:
  162. ret |= __get_user_inatomic(temp.v[0], p++);
  163. ret |= __get_user_inatomic(temp.v[1], p++);
  164. ret |= __get_user_inatomic(temp.v[2], p++);
  165. ret |= __get_user_inatomic(temp.v[3], p++);
  166. case 4:
  167. ret |= __get_user_inatomic(temp.v[4], p++);
  168. ret |= __get_user_inatomic(temp.v[5], p++);
  169. case 2:
  170. ret |= __get_user_inatomic(temp.v[6], p++);
  171. ret |= __get_user_inatomic(temp.v[7], p++);
  172. if (unlikely(ret))
  173. return -EFAULT;
  174. }
  175. switch (instr) {
  176. case EVLDD:
  177. case EVLDW:
  178. case EVLDH:
  179. data.ll = temp.ll;
  180. break;
  181. case EVLHHESPLAT:
  182. data.h[0] = temp.h[3];
  183. data.h[2] = temp.h[3];
  184. break;
  185. case EVLHHOUSPLAT:
  186. case EVLHHOSSPLAT:
  187. data.h[1] = temp.h[3];
  188. data.h[3] = temp.h[3];
  189. break;
  190. case EVLWHE:
  191. data.h[0] = temp.h[2];
  192. data.h[2] = temp.h[3];
  193. break;
  194. case EVLWHOU:
  195. case EVLWHOS:
  196. data.h[1] = temp.h[2];
  197. data.h[3] = temp.h[3];
  198. break;
  199. case EVLWWSPLAT:
  200. data.w[0] = temp.w[1];
  201. data.w[1] = temp.w[1];
  202. break;
  203. case EVLWHSPLAT:
  204. data.h[0] = temp.h[2];
  205. data.h[1] = temp.h[2];
  206. data.h[2] = temp.h[3];
  207. data.h[3] = temp.h[3];
  208. break;
  209. default:
  210. return -EINVAL;
  211. }
  212. }
  213. if (flags & SW) {
  214. switch (flags & 0xf0) {
  215. case E8:
  216. data.ll = swab64(data.ll);
  217. break;
  218. case E4:
  219. data.w[0] = swab32(data.w[0]);
  220. data.w[1] = swab32(data.w[1]);
  221. break;
  222. /* Its half word endian */
  223. default:
  224. data.h[0] = swab16(data.h[0]);
  225. data.h[1] = swab16(data.h[1]);
  226. data.h[2] = swab16(data.h[2]);
  227. data.h[3] = swab16(data.h[3]);
  228. break;
  229. }
  230. }
  231. if (flags & SE) {
  232. data.w[0] = (s16)data.h[1];
  233. data.w[1] = (s16)data.h[3];
  234. }
  235. /* Store result to memory or update registers */
  236. if (flags & ST) {
  237. ret = 0;
  238. p = addr;
  239. switch (nb) {
  240. case 8:
  241. ret |= __put_user_inatomic(data.v[0], p++);
  242. ret |= __put_user_inatomic(data.v[1], p++);
  243. ret |= __put_user_inatomic(data.v[2], p++);
  244. ret |= __put_user_inatomic(data.v[3], p++);
  245. case 4:
  246. ret |= __put_user_inatomic(data.v[4], p++);
  247. ret |= __put_user_inatomic(data.v[5], p++);
  248. case 2:
  249. ret |= __put_user_inatomic(data.v[6], p++);
  250. ret |= __put_user_inatomic(data.v[7], p++);
  251. }
  252. if (unlikely(ret))
  253. return -EFAULT;
  254. } else {
  255. *evr = data.w[0];
  256. regs->gpr[reg] = data.w[1];
  257. }
  258. return 1;
  259. }
  260. #endif /* CONFIG_SPE */
  261. /*
  262. * Called on alignment exception. Attempts to fixup
  263. *
  264. * Return 1 on success
  265. * Return 0 if unable to handle the interrupt
  266. * Return -EFAULT if data address is bad
  267. * Other negative return values indicate that the instruction can't
  268. * be emulated, and the process should be given a SIGBUS.
  269. */
  270. int fix_alignment(struct pt_regs *regs)
  271. {
  272. unsigned int instr;
  273. struct instruction_op op;
  274. int r, type;
  275. /*
  276. * We require a complete register set, if not, then our assembly
  277. * is broken
  278. */
  279. CHECK_FULL_REGS(regs);
  280. if (unlikely(__get_user(instr, (unsigned int __user *)regs->nip)))
  281. return -EFAULT;
  282. if ((regs->msr & MSR_LE) != (MSR_KERNEL & MSR_LE)) {
  283. /* We don't handle PPC little-endian any more... */
  284. if (cpu_has_feature(CPU_FTR_PPC_LE))
  285. return -EIO;
  286. instr = swab32(instr);
  287. }
  288. #ifdef CONFIG_SPE
  289. if ((instr >> 26) == 0x4) {
  290. int reg = (instr >> 21) & 0x1f;
  291. PPC_WARN_ALIGNMENT(spe, regs);
  292. return emulate_spe(regs, reg, instr);
  293. }
  294. #endif
  295. /*
  296. * ISA 3.0 (such as P9) copy, copy_first, paste and paste_last alignment
  297. * check.
  298. *
  299. * Send a SIGBUS to the process that caused the fault.
  300. *
  301. * We do not emulate these because paste may contain additional metadata
  302. * when pasting to a co-processor. Furthermore, paste_last is the
  303. * synchronisation point for preceding copy/paste sequences.
  304. */
  305. if ((instr & 0xfc0006fe) == (PPC_INST_COPY & 0xfc0006fe))
  306. return -EIO;
  307. r = analyse_instr(&op, regs, instr);
  308. if (r < 0)
  309. return -EINVAL;
  310. type = GETTYPE(op.type);
  311. if (!OP_IS_LOAD_STORE(type)) {
  312. if (op.type != CACHEOP + DCBZ)
  313. return -EINVAL;
  314. PPC_WARN_ALIGNMENT(dcbz, regs);
  315. r = emulate_dcbz(op.ea, regs);
  316. } else {
  317. if (type == LARX || type == STCX)
  318. return -EIO;
  319. PPC_WARN_ALIGNMENT(unaligned, regs);
  320. r = emulate_loadstore(regs, &op);
  321. }
  322. if (!r)
  323. return 1;
  324. return r;
  325. }