PageRenderTime 64ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/arch/powerpc/kernel/align.c

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