traps_misaligned.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020 Western Digital Corporation or its affiliates.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/init.h>
  7. #include <linux/mm.h>
  8. #include <linux/module.h>
  9. #include <linux/irq.h>
  10. #include <linux/stringify.h>
  11. #include <asm/processor.h>
  12. #include <asm/ptrace.h>
  13. #include <asm/csr.h>
  14. #define INSN_MATCH_LB 0x3
  15. #define INSN_MASK_LB 0x707f
  16. #define INSN_MATCH_LH 0x1003
  17. #define INSN_MASK_LH 0x707f
  18. #define INSN_MATCH_LW 0x2003
  19. #define INSN_MASK_LW 0x707f
  20. #define INSN_MATCH_LD 0x3003
  21. #define INSN_MASK_LD 0x707f
  22. #define INSN_MATCH_LBU 0x4003
  23. #define INSN_MASK_LBU 0x707f
  24. #define INSN_MATCH_LHU 0x5003
  25. #define INSN_MASK_LHU 0x707f
  26. #define INSN_MATCH_LWU 0x6003
  27. #define INSN_MASK_LWU 0x707f
  28. #define INSN_MATCH_SB 0x23
  29. #define INSN_MASK_SB 0x707f
  30. #define INSN_MATCH_SH 0x1023
  31. #define INSN_MASK_SH 0x707f
  32. #define INSN_MATCH_SW 0x2023
  33. #define INSN_MASK_SW 0x707f
  34. #define INSN_MATCH_SD 0x3023
  35. #define INSN_MASK_SD 0x707f
  36. #define INSN_MATCH_FLW 0x2007
  37. #define INSN_MASK_FLW 0x707f
  38. #define INSN_MATCH_FLD 0x3007
  39. #define INSN_MASK_FLD 0x707f
  40. #define INSN_MATCH_FLQ 0x4007
  41. #define INSN_MASK_FLQ 0x707f
  42. #define INSN_MATCH_FSW 0x2027
  43. #define INSN_MASK_FSW 0x707f
  44. #define INSN_MATCH_FSD 0x3027
  45. #define INSN_MASK_FSD 0x707f
  46. #define INSN_MATCH_FSQ 0x4027
  47. #define INSN_MASK_FSQ 0x707f
  48. #define INSN_MATCH_C_LD 0x6000
  49. #define INSN_MASK_C_LD 0xe003
  50. #define INSN_MATCH_C_SD 0xe000
  51. #define INSN_MASK_C_SD 0xe003
  52. #define INSN_MATCH_C_LW 0x4000
  53. #define INSN_MASK_C_LW 0xe003
  54. #define INSN_MATCH_C_SW 0xc000
  55. #define INSN_MASK_C_SW 0xe003
  56. #define INSN_MATCH_C_LDSP 0x6002
  57. #define INSN_MASK_C_LDSP 0xe003
  58. #define INSN_MATCH_C_SDSP 0xe002
  59. #define INSN_MASK_C_SDSP 0xe003
  60. #define INSN_MATCH_C_LWSP 0x4002
  61. #define INSN_MASK_C_LWSP 0xe003
  62. #define INSN_MATCH_C_SWSP 0xc002
  63. #define INSN_MASK_C_SWSP 0xe003
  64. #define INSN_MATCH_C_FLD 0x2000
  65. #define INSN_MASK_C_FLD 0xe003
  66. #define INSN_MATCH_C_FLW 0x6000
  67. #define INSN_MASK_C_FLW 0xe003
  68. #define INSN_MATCH_C_FSD 0xa000
  69. #define INSN_MASK_C_FSD 0xe003
  70. #define INSN_MATCH_C_FSW 0xe000
  71. #define INSN_MASK_C_FSW 0xe003
  72. #define INSN_MATCH_C_FLDSP 0x2002
  73. #define INSN_MASK_C_FLDSP 0xe003
  74. #define INSN_MATCH_C_FSDSP 0xa002
  75. #define INSN_MASK_C_FSDSP 0xe003
  76. #define INSN_MATCH_C_FLWSP 0x6002
  77. #define INSN_MASK_C_FLWSP 0xe003
  78. #define INSN_MATCH_C_FSWSP 0xe002
  79. #define INSN_MASK_C_FSWSP 0xe003
  80. #define INSN_LEN(insn) ((((insn) & 0x3) < 0x3) ? 2 : 4)
  81. #if defined(CONFIG_64BIT)
  82. #define LOG_REGBYTES 3
  83. #define XLEN 64
  84. #else
  85. #define LOG_REGBYTES 2
  86. #define XLEN 32
  87. #endif
  88. #define REGBYTES (1 << LOG_REGBYTES)
  89. #define XLEN_MINUS_16 ((XLEN) - 16)
  90. #define SH_RD 7
  91. #define SH_RS1 15
  92. #define SH_RS2 20
  93. #define SH_RS2C 2
  94. #define RV_X(x, s, n) (((x) >> (s)) & ((1 << (n)) - 1))
  95. #define RVC_LW_IMM(x) ((RV_X(x, 6, 1) << 2) | \
  96. (RV_X(x, 10, 3) << 3) | \
  97. (RV_X(x, 5, 1) << 6))
  98. #define RVC_LD_IMM(x) ((RV_X(x, 10, 3) << 3) | \
  99. (RV_X(x, 5, 2) << 6))
  100. #define RVC_LWSP_IMM(x) ((RV_X(x, 4, 3) << 2) | \
  101. (RV_X(x, 12, 1) << 5) | \
  102. (RV_X(x, 2, 2) << 6))
  103. #define RVC_LDSP_IMM(x) ((RV_X(x, 5, 2) << 3) | \
  104. (RV_X(x, 12, 1) << 5) | \
  105. (RV_X(x, 2, 3) << 6))
  106. #define RVC_SWSP_IMM(x) ((RV_X(x, 9, 4) << 2) | \
  107. (RV_X(x, 7, 2) << 6))
  108. #define RVC_SDSP_IMM(x) ((RV_X(x, 10, 3) << 3) | \
  109. (RV_X(x, 7, 3) << 6))
  110. #define RVC_RS1S(insn) (8 + RV_X(insn, SH_RD, 3))
  111. #define RVC_RS2S(insn) (8 + RV_X(insn, SH_RS2C, 3))
  112. #define RVC_RS2(insn) RV_X(insn, SH_RS2C, 5)
  113. #define SHIFT_RIGHT(x, y) \
  114. ((y) < 0 ? ((x) << -(y)) : ((x) >> (y)))
  115. #define REG_MASK \
  116. ((1 << (5 + LOG_REGBYTES)) - (1 << LOG_REGBYTES))
  117. #define REG_OFFSET(insn, pos) \
  118. (SHIFT_RIGHT((insn), (pos) - LOG_REGBYTES) & REG_MASK)
  119. #define REG_PTR(insn, pos, regs) \
  120. (ulong *)((ulong)(regs) + REG_OFFSET(insn, pos))
  121. #define GET_RM(insn) (((insn) >> 12) & 7)
  122. #define GET_RS1(insn, regs) (*REG_PTR(insn, SH_RS1, regs))
  123. #define GET_RS2(insn, regs) (*REG_PTR(insn, SH_RS2, regs))
  124. #define GET_RS1S(insn, regs) (*REG_PTR(RVC_RS1S(insn), 0, regs))
  125. #define GET_RS2S(insn, regs) (*REG_PTR(RVC_RS2S(insn), 0, regs))
  126. #define GET_RS2C(insn, regs) (*REG_PTR(insn, SH_RS2C, regs))
  127. #define GET_SP(regs) (*REG_PTR(2, 0, regs))
  128. #define SET_RD(insn, regs, val) (*REG_PTR(insn, SH_RD, regs) = (val))
  129. #define IMM_I(insn) ((s32)(insn) >> 20)
  130. #define IMM_S(insn) (((s32)(insn) >> 25 << 5) | \
  131. (s32)(((insn) >> 7) & 0x1f))
  132. #define MASK_FUNCT3 0x7000
  133. #define GET_PRECISION(insn) (((insn) >> 25) & 3)
  134. #define GET_RM(insn) (((insn) >> 12) & 7)
  135. #define PRECISION_S 0
  136. #define PRECISION_D 1
  137. #define DECLARE_UNPRIVILEGED_LOAD_FUNCTION(type, insn) \
  138. static inline type load_##type(const type *addr) \
  139. { \
  140. type val; \
  141. asm (#insn " %0, %1" \
  142. : "=&r" (val) : "m" (*addr)); \
  143. return val; \
  144. }
  145. #define DECLARE_UNPRIVILEGED_STORE_FUNCTION(type, insn) \
  146. static inline void store_##type(type *addr, type val) \
  147. { \
  148. asm volatile (#insn " %0, %1\n" \
  149. : : "r" (val), "m" (*addr)); \
  150. }
  151. DECLARE_UNPRIVILEGED_LOAD_FUNCTION(u8, lbu)
  152. DECLARE_UNPRIVILEGED_LOAD_FUNCTION(u16, lhu)
  153. DECLARE_UNPRIVILEGED_LOAD_FUNCTION(s8, lb)
  154. DECLARE_UNPRIVILEGED_LOAD_FUNCTION(s16, lh)
  155. DECLARE_UNPRIVILEGED_LOAD_FUNCTION(s32, lw)
  156. DECLARE_UNPRIVILEGED_STORE_FUNCTION(u8, sb)
  157. DECLARE_UNPRIVILEGED_STORE_FUNCTION(u16, sh)
  158. DECLARE_UNPRIVILEGED_STORE_FUNCTION(u32, sw)
  159. #if defined(CONFIG_64BIT)
  160. DECLARE_UNPRIVILEGED_LOAD_FUNCTION(u32, lwu)
  161. DECLARE_UNPRIVILEGED_LOAD_FUNCTION(u64, ld)
  162. DECLARE_UNPRIVILEGED_STORE_FUNCTION(u64, sd)
  163. DECLARE_UNPRIVILEGED_LOAD_FUNCTION(ulong, ld)
  164. #else
  165. DECLARE_UNPRIVILEGED_LOAD_FUNCTION(u32, lw)
  166. DECLARE_UNPRIVILEGED_LOAD_FUNCTION(ulong, lw)
  167. static inline u64 load_u64(const u64 *addr)
  168. {
  169. return load_u32((u32 *)addr)
  170. + ((u64)load_u32((u32 *)addr + 1) << 32);
  171. }
  172. static inline void store_u64(u64 *addr, u64 val)
  173. {
  174. store_u32((u32 *)addr, val);
  175. store_u32((u32 *)addr + 1, val >> 32);
  176. }
  177. #endif
  178. static inline ulong get_insn(ulong mepc)
  179. {
  180. register ulong __mepc asm ("a2") = mepc;
  181. ulong val, rvc_mask = 3, tmp;
  182. asm ("and %[tmp], %[addr], 2\n"
  183. "bnez %[tmp], 1f\n"
  184. #if defined(CONFIG_64BIT)
  185. __stringify(LWU) " %[insn], (%[addr])\n"
  186. #else
  187. __stringify(LW) " %[insn], (%[addr])\n"
  188. #endif
  189. "and %[tmp], %[insn], %[rvc_mask]\n"
  190. "beq %[tmp], %[rvc_mask], 2f\n"
  191. "sll %[insn], %[insn], %[xlen_minus_16]\n"
  192. "srl %[insn], %[insn], %[xlen_minus_16]\n"
  193. "j 2f\n"
  194. "1:\n"
  195. "lhu %[insn], (%[addr])\n"
  196. "and %[tmp], %[insn], %[rvc_mask]\n"
  197. "bne %[tmp], %[rvc_mask], 2f\n"
  198. "lhu %[tmp], 2(%[addr])\n"
  199. "sll %[tmp], %[tmp], 16\n"
  200. "add %[insn], %[insn], %[tmp]\n"
  201. "2:"
  202. : [insn] "=&r" (val), [tmp] "=&r" (tmp)
  203. : [addr] "r" (__mepc), [rvc_mask] "r" (rvc_mask),
  204. [xlen_minus_16] "i" (XLEN_MINUS_16));
  205. return val;
  206. }
  207. union reg_data {
  208. u8 data_bytes[8];
  209. ulong data_ulong;
  210. u64 data_u64;
  211. };
  212. int handle_misaligned_load(struct pt_regs *regs)
  213. {
  214. union reg_data val;
  215. unsigned long epc = regs->epc;
  216. unsigned long insn = get_insn(epc);
  217. unsigned long addr = csr_read(mtval);
  218. int i, fp = 0, shift = 0, len = 0;
  219. regs->epc = 0;
  220. if ((insn & INSN_MASK_LW) == INSN_MATCH_LW) {
  221. len = 4;
  222. shift = 8 * (sizeof(unsigned long) - len);
  223. #if defined(CONFIG_64BIT)
  224. } else if ((insn & INSN_MASK_LD) == INSN_MATCH_LD) {
  225. len = 8;
  226. shift = 8 * (sizeof(unsigned long) - len);
  227. } else if ((insn & INSN_MASK_LWU) == INSN_MATCH_LWU) {
  228. len = 4;
  229. #endif
  230. } else if ((insn & INSN_MASK_FLD) == INSN_MATCH_FLD) {
  231. fp = 1;
  232. len = 8;
  233. } else if ((insn & INSN_MASK_FLW) == INSN_MATCH_FLW) {
  234. fp = 1;
  235. len = 4;
  236. } else if ((insn & INSN_MASK_LH) == INSN_MATCH_LH) {
  237. len = 2;
  238. shift = 8 * (sizeof(unsigned long) - len);
  239. } else if ((insn & INSN_MASK_LHU) == INSN_MATCH_LHU) {
  240. len = 2;
  241. #if defined(CONFIG_64BIT)
  242. } else if ((insn & INSN_MASK_C_LD) == INSN_MATCH_C_LD) {
  243. len = 8;
  244. shift = 8 * (sizeof(unsigned long) - len);
  245. insn = RVC_RS2S(insn) << SH_RD;
  246. } else if ((insn & INSN_MASK_C_LDSP) == INSN_MATCH_C_LDSP &&
  247. ((insn >> SH_RD) & 0x1f)) {
  248. len = 8;
  249. shift = 8 * (sizeof(unsigned long) - len);
  250. #endif
  251. } else if ((insn & INSN_MASK_C_LW) == INSN_MATCH_C_LW) {
  252. len = 4;
  253. shift = 8 * (sizeof(unsigned long) - len);
  254. insn = RVC_RS2S(insn) << SH_RD;
  255. } else if ((insn & INSN_MASK_C_LWSP) == INSN_MATCH_C_LWSP &&
  256. ((insn >> SH_RD) & 0x1f)) {
  257. len = 4;
  258. shift = 8 * (sizeof(unsigned long) - len);
  259. } else if ((insn & INSN_MASK_C_FLD) == INSN_MATCH_C_FLD) {
  260. fp = 1;
  261. len = 8;
  262. insn = RVC_RS2S(insn) << SH_RD;
  263. } else if ((insn & INSN_MASK_C_FLDSP) == INSN_MATCH_C_FLDSP) {
  264. fp = 1;
  265. len = 8;
  266. #if defined(CONFIG_32BIT)
  267. } else if ((insn & INSN_MASK_C_FLW) == INSN_MATCH_C_FLW) {
  268. fp = 1;
  269. len = 4;
  270. insn = RVC_RS2S(insn) << SH_RD;
  271. } else if ((insn & INSN_MASK_C_FLWSP) == INSN_MATCH_C_FLWSP) {
  272. fp = 1;
  273. len = 4;
  274. #endif
  275. } else {
  276. regs->epc = epc;
  277. return -1;
  278. }
  279. val.data_u64 = 0;
  280. for (i = 0; i < len; i++)
  281. val.data_bytes[i] = load_u8((void *)(addr + i));
  282. if (fp)
  283. return -1;
  284. SET_RD(insn, regs, val.data_ulong << shift >> shift);
  285. regs->epc = epc + INSN_LEN(insn);
  286. return 0;
  287. }
  288. int handle_misaligned_store(struct pt_regs *regs)
  289. {
  290. union reg_data val;
  291. unsigned long epc = regs->epc;
  292. unsigned long insn = get_insn(epc);
  293. unsigned long addr = csr_read(mtval);
  294. int i, len = 0;
  295. regs->epc = 0;
  296. val.data_ulong = GET_RS2(insn, regs);
  297. if ((insn & INSN_MASK_SW) == INSN_MATCH_SW) {
  298. len = 4;
  299. #if defined(CONFIG_64BIT)
  300. } else if ((insn & INSN_MASK_SD) == INSN_MATCH_SD) {
  301. len = 8;
  302. #endif
  303. } else if ((insn & INSN_MASK_SH) == INSN_MATCH_SH) {
  304. len = 2;
  305. #if defined(CONFIG_64BIT)
  306. } else if ((insn & INSN_MASK_C_SD) == INSN_MATCH_C_SD) {
  307. len = 8;
  308. val.data_ulong = GET_RS2S(insn, regs);
  309. } else if ((insn & INSN_MASK_C_SDSP) == INSN_MATCH_C_SDSP) {
  310. len = 8;
  311. val.data_ulong = GET_RS2C(insn, regs);
  312. #endif
  313. } else if ((insn & INSN_MASK_C_SW) == INSN_MATCH_C_SW) {
  314. len = 4;
  315. val.data_ulong = GET_RS2S(insn, regs);
  316. } else if ((insn & INSN_MASK_C_SWSP) == INSN_MATCH_C_SWSP) {
  317. len = 4;
  318. val.data_ulong = GET_RS2C(insn, regs);
  319. } else {
  320. regs->epc = epc;
  321. return -1;
  322. }
  323. for (i = 0; i < len; i++)
  324. store_u8((void *)(addr + i), val.data_bytes[i]);
  325. regs->epc = epc + INSN_LEN(insn);
  326. return 0;
  327. }