alignment.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
  3. #include <linux/kernel.h>
  4. #include <linux/uaccess.h>
  5. #include <linux/ptrace.h>
  6. static int align_kern_enable = 1;
  7. static int align_usr_enable = 1;
  8. static int align_kern_count = 0;
  9. static int align_usr_count = 0;
  10. static inline uint32_t get_ptreg(struct pt_regs *regs, uint32_t rx)
  11. {
  12. return rx == 15 ? regs->lr : *((uint32_t *)&(regs->a0) - 2 + rx);
  13. }
  14. static inline void put_ptreg(struct pt_regs *regs, uint32_t rx, uint32_t val)
  15. {
  16. if (rx == 15)
  17. regs->lr = val;
  18. else
  19. *((uint32_t *)&(regs->a0) - 2 + rx) = val;
  20. }
  21. /*
  22. * Get byte-value from addr and set it to *valp.
  23. *
  24. * Success: return 0
  25. * Failure: return 1
  26. */
  27. static int ldb_asm(uint32_t addr, uint32_t *valp)
  28. {
  29. uint32_t val;
  30. int err;
  31. asm volatile (
  32. "movi %0, 0\n"
  33. "1:\n"
  34. "ldb %1, (%2)\n"
  35. "br 3f\n"
  36. "2:\n"
  37. "movi %0, 1\n"
  38. "br 3f\n"
  39. ".section __ex_table,\"a\"\n"
  40. ".align 2\n"
  41. ".long 1b, 2b\n"
  42. ".previous\n"
  43. "3:\n"
  44. : "=&r"(err), "=r"(val)
  45. : "r" (addr)
  46. );
  47. *valp = val;
  48. return err;
  49. }
  50. /*
  51. * Put byte-value to addr.
  52. *
  53. * Success: return 0
  54. * Failure: return 1
  55. */
  56. static int stb_asm(uint32_t addr, uint32_t val)
  57. {
  58. int err;
  59. asm volatile (
  60. "movi %0, 0\n"
  61. "1:\n"
  62. "stb %1, (%2)\n"
  63. "br 3f\n"
  64. "2:\n"
  65. "movi %0, 1\n"
  66. "br 3f\n"
  67. ".section __ex_table,\"a\"\n"
  68. ".align 2\n"
  69. ".long 1b, 2b\n"
  70. ".previous\n"
  71. "3:\n"
  72. : "=&r"(err)
  73. : "r"(val), "r" (addr)
  74. );
  75. return err;
  76. }
  77. /*
  78. * Get half-word from [rx + imm]
  79. *
  80. * Success: return 0
  81. * Failure: return 1
  82. */
  83. static int ldh_c(struct pt_regs *regs, uint32_t rz, uint32_t addr)
  84. {
  85. uint32_t byte0, byte1;
  86. if (ldb_asm(addr, &byte0))
  87. return 1;
  88. addr += 1;
  89. if (ldb_asm(addr, &byte1))
  90. return 1;
  91. byte0 |= byte1 << 8;
  92. put_ptreg(regs, rz, byte0);
  93. return 0;
  94. }
  95. /*
  96. * Store half-word to [rx + imm]
  97. *
  98. * Success: return 0
  99. * Failure: return 1
  100. */
  101. static int sth_c(struct pt_regs *regs, uint32_t rz, uint32_t addr)
  102. {
  103. uint32_t byte0, byte1;
  104. byte0 = byte1 = get_ptreg(regs, rz);
  105. byte0 &= 0xff;
  106. if (stb_asm(addr, byte0))
  107. return 1;
  108. addr += 1;
  109. byte1 = (byte1 >> 8) & 0xff;
  110. if (stb_asm(addr, byte1))
  111. return 1;
  112. return 0;
  113. }
  114. /*
  115. * Get word from [rx + imm]
  116. *
  117. * Success: return 0
  118. * Failure: return 1
  119. */
  120. static int ldw_c(struct pt_regs *regs, uint32_t rz, uint32_t addr)
  121. {
  122. uint32_t byte0, byte1, byte2, byte3;
  123. if (ldb_asm(addr, &byte0))
  124. return 1;
  125. addr += 1;
  126. if (ldb_asm(addr, &byte1))
  127. return 1;
  128. addr += 1;
  129. if (ldb_asm(addr, &byte2))
  130. return 1;
  131. addr += 1;
  132. if (ldb_asm(addr, &byte3))
  133. return 1;
  134. byte0 |= byte1 << 8;
  135. byte0 |= byte2 << 16;
  136. byte0 |= byte3 << 24;
  137. put_ptreg(regs, rz, byte0);
  138. return 0;
  139. }
  140. /*
  141. * Store word to [rx + imm]
  142. *
  143. * Success: return 0
  144. * Failure: return 1
  145. */
  146. static int stw_c(struct pt_regs *regs, uint32_t rz, uint32_t addr)
  147. {
  148. uint32_t byte0, byte1, byte2, byte3;
  149. byte0 = byte1 = byte2 = byte3 = get_ptreg(regs, rz);
  150. byte0 &= 0xff;
  151. if (stb_asm(addr, byte0))
  152. return 1;
  153. addr += 1;
  154. byte1 = (byte1 >> 8) & 0xff;
  155. if (stb_asm(addr, byte1))
  156. return 1;
  157. addr += 1;
  158. byte2 = (byte2 >> 16) & 0xff;
  159. if (stb_asm(addr, byte2))
  160. return 1;
  161. addr += 1;
  162. byte3 = (byte3 >> 24) & 0xff;
  163. if (stb_asm(addr, byte3))
  164. return 1;
  165. return 0;
  166. }
  167. extern int fixup_exception(struct pt_regs *regs);
  168. #define OP_LDH 0xc000
  169. #define OP_STH 0xd000
  170. #define OP_LDW 0x8000
  171. #define OP_STW 0x9000
  172. void csky_alignment(struct pt_regs *regs)
  173. {
  174. int ret;
  175. uint16_t tmp;
  176. uint32_t opcode = 0;
  177. uint32_t rx = 0;
  178. uint32_t rz = 0;
  179. uint32_t imm = 0;
  180. uint32_t addr = 0;
  181. if (!user_mode(regs))
  182. goto kernel_area;
  183. if (!align_usr_enable) {
  184. pr_err("%s user disabled.\n", __func__);
  185. goto bad_area;
  186. }
  187. align_usr_count++;
  188. ret = get_user(tmp, (uint16_t *)instruction_pointer(regs));
  189. if (ret) {
  190. pr_err("%s get_user failed.\n", __func__);
  191. goto bad_area;
  192. }
  193. goto good_area;
  194. kernel_area:
  195. if (!align_kern_enable) {
  196. pr_err("%s kernel disabled.\n", __func__);
  197. goto bad_area;
  198. }
  199. align_kern_count++;
  200. tmp = *(uint16_t *)instruction_pointer(regs);
  201. good_area:
  202. opcode = (uint32_t)tmp;
  203. rx = opcode & 0xf;
  204. imm = (opcode >> 4) & 0xf;
  205. rz = (opcode >> 8) & 0xf;
  206. opcode &= 0xf000;
  207. if (rx == 0 || rx == 1 || rz == 0 || rz == 1)
  208. goto bad_area;
  209. switch (opcode) {
  210. case OP_LDH:
  211. addr = get_ptreg(regs, rx) + (imm << 1);
  212. ret = ldh_c(regs, rz, addr);
  213. break;
  214. case OP_LDW:
  215. addr = get_ptreg(regs, rx) + (imm << 2);
  216. ret = ldw_c(regs, rz, addr);
  217. break;
  218. case OP_STH:
  219. addr = get_ptreg(regs, rx) + (imm << 1);
  220. ret = sth_c(regs, rz, addr);
  221. break;
  222. case OP_STW:
  223. addr = get_ptreg(regs, rx) + (imm << 2);
  224. ret = stw_c(regs, rz, addr);
  225. break;
  226. }
  227. if (ret)
  228. goto bad_area;
  229. regs->pc += 2;
  230. return;
  231. bad_area:
  232. if (!user_mode(regs)) {
  233. if (fixup_exception(regs))
  234. return;
  235. bust_spinlocks(1);
  236. pr_alert("%s opcode: %x, rz: %d, rx: %d, imm: %d, addr: %x.\n",
  237. __func__, opcode, rz, rx, imm, addr);
  238. show_regs(regs);
  239. bust_spinlocks(0);
  240. make_task_dead(SIGKILL);
  241. }
  242. force_sig_fault(SIGBUS, BUS_ADRALN, (void __user *)addr);
  243. }
  244. static struct ctl_table alignment_tbl[5] = {
  245. {
  246. .procname = "kernel_enable",
  247. .data = &align_kern_enable,
  248. .maxlen = sizeof(align_kern_enable),
  249. .mode = 0666,
  250. .proc_handler = &proc_dointvec
  251. },
  252. {
  253. .procname = "user_enable",
  254. .data = &align_usr_enable,
  255. .maxlen = sizeof(align_usr_enable),
  256. .mode = 0666,
  257. .proc_handler = &proc_dointvec
  258. },
  259. {
  260. .procname = "kernel_count",
  261. .data = &align_kern_count,
  262. .maxlen = sizeof(align_kern_count),
  263. .mode = 0666,
  264. .proc_handler = &proc_dointvec
  265. },
  266. {
  267. .procname = "user_count",
  268. .data = &align_usr_count,
  269. .maxlen = sizeof(align_usr_count),
  270. .mode = 0666,
  271. .proc_handler = &proc_dointvec
  272. },
  273. {}
  274. };
  275. static struct ctl_table sysctl_table[2] = {
  276. {
  277. .procname = "csky_alignment",
  278. .mode = 0555,
  279. .child = alignment_tbl},
  280. {}
  281. };
  282. static struct ctl_path sysctl_path[2] = {
  283. {.procname = "csky"},
  284. {}
  285. };
  286. static int __init csky_alignment_init(void)
  287. {
  288. register_sysctl_paths(sysctl_path, sysctl_table);
  289. return 0;
  290. }
  291. arch_initcall(csky_alignment_init);