opt-arm.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Kernel Probes Jump Optimization (Optprobes)
  4. *
  5. * Copyright (C) IBM Corporation, 2002, 2004
  6. * Copyright (C) Hitachi Ltd., 2012
  7. * Copyright (C) Huawei Inc., 2014
  8. */
  9. #include <linux/kprobes.h>
  10. #include <linux/jump_label.h>
  11. #include <asm/kprobes.h>
  12. #include <asm/cacheflush.h>
  13. /* for arm_gen_branch */
  14. #include <asm/insn.h>
  15. /* for patch_text */
  16. #include <asm/patch.h>
  17. #include "core.h"
  18. /*
  19. * See register_usage_flags. If the probed instruction doesn't use PC,
  20. * we can copy it into template and have it executed directly without
  21. * simulation or emulation.
  22. */
  23. #define ARM_REG_PC 15
  24. #define can_kprobe_direct_exec(m) (!test_bit(ARM_REG_PC, &(m)))
  25. /*
  26. * NOTE: the first sub and add instruction will be modified according
  27. * to the stack cost of the instruction.
  28. */
  29. asm (
  30. ".global optprobe_template_entry\n"
  31. "optprobe_template_entry:\n"
  32. ".global optprobe_template_sub_sp\n"
  33. "optprobe_template_sub_sp:"
  34. " sub sp, sp, #0xff\n"
  35. " stmia sp, {r0 - r14} \n"
  36. ".global optprobe_template_add_sp\n"
  37. "optprobe_template_add_sp:"
  38. " add r3, sp, #0xff\n"
  39. " str r3, [sp, #52]\n"
  40. " mrs r4, cpsr\n"
  41. " str r4, [sp, #64]\n"
  42. " mov r1, sp\n"
  43. " ldr r0, 1f\n"
  44. " ldr r2, 2f\n"
  45. /*
  46. * AEABI requires an 8-bytes alignment stack. If
  47. * SP % 8 != 0 (SP % 4 == 0 should be ensured),
  48. * alloc more bytes here.
  49. */
  50. " and r4, sp, #4\n"
  51. " sub sp, sp, r4\n"
  52. #if __LINUX_ARM_ARCH__ >= 5
  53. " blx r2\n"
  54. #else
  55. " mov lr, pc\n"
  56. " mov pc, r2\n"
  57. #endif
  58. " add sp, sp, r4\n"
  59. " ldr r1, [sp, #64]\n"
  60. " tst r1, #"__stringify(PSR_T_BIT)"\n"
  61. " ldrne r2, [sp, #60]\n"
  62. " orrne r2, #1\n"
  63. " strne r2, [sp, #60] @ set bit0 of PC for thumb\n"
  64. " msr cpsr_cxsf, r1\n"
  65. ".global optprobe_template_restore_begin\n"
  66. "optprobe_template_restore_begin:\n"
  67. " ldmia sp, {r0 - r15}\n"
  68. ".global optprobe_template_restore_orig_insn\n"
  69. "optprobe_template_restore_orig_insn:\n"
  70. " nop\n"
  71. ".global optprobe_template_restore_end\n"
  72. "optprobe_template_restore_end:\n"
  73. " nop\n"
  74. ".global optprobe_template_val\n"
  75. "optprobe_template_val:\n"
  76. "1: .long 0\n"
  77. ".global optprobe_template_call\n"
  78. "optprobe_template_call:\n"
  79. "2: .long 0\n"
  80. ".global optprobe_template_end\n"
  81. "optprobe_template_end:\n");
  82. #define TMPL_VAL_IDX \
  83. ((unsigned long *)optprobe_template_val - (unsigned long *)optprobe_template_entry)
  84. #define TMPL_CALL_IDX \
  85. ((unsigned long *)optprobe_template_call - (unsigned long *)optprobe_template_entry)
  86. #define TMPL_END_IDX \
  87. ((unsigned long *)optprobe_template_end - (unsigned long *)optprobe_template_entry)
  88. #define TMPL_ADD_SP \
  89. ((unsigned long *)optprobe_template_add_sp - (unsigned long *)optprobe_template_entry)
  90. #define TMPL_SUB_SP \
  91. ((unsigned long *)optprobe_template_sub_sp - (unsigned long *)optprobe_template_entry)
  92. #define TMPL_RESTORE_BEGIN \
  93. ((unsigned long *)optprobe_template_restore_begin - (unsigned long *)optprobe_template_entry)
  94. #define TMPL_RESTORE_ORIGN_INSN \
  95. ((unsigned long *)optprobe_template_restore_orig_insn - (unsigned long *)optprobe_template_entry)
  96. #define TMPL_RESTORE_END \
  97. ((unsigned long *)optprobe_template_restore_end - (unsigned long *)optprobe_template_entry)
  98. /*
  99. * ARM can always optimize an instruction when using ARM ISA, except
  100. * instructions like 'str r0, [sp, r1]' which store to stack and unable
  101. * to determine stack space consumption statically.
  102. */
  103. int arch_prepared_optinsn(struct arch_optimized_insn *optinsn)
  104. {
  105. return optinsn->insn != NULL;
  106. }
  107. /*
  108. * In ARM ISA, kprobe opt always replace one instruction (4 bytes
  109. * aligned and 4 bytes long). It is impossible to encounter another
  110. * kprobe in the address range. So always return 0.
  111. */
  112. int arch_check_optimized_kprobe(struct optimized_kprobe *op)
  113. {
  114. return 0;
  115. }
  116. /* Caller must ensure addr & 3 == 0 */
  117. static int can_optimize(struct kprobe *kp)
  118. {
  119. if (kp->ainsn.stack_space < 0)
  120. return 0;
  121. /*
  122. * 255 is the biggest imm can be used in 'sub r0, r0, #<imm>'.
  123. * Number larger than 255 needs special encoding.
  124. */
  125. if (kp->ainsn.stack_space > 255 - sizeof(struct pt_regs))
  126. return 0;
  127. return 1;
  128. }
  129. /* Free optimized instruction slot */
  130. static void
  131. __arch_remove_optimized_kprobe(struct optimized_kprobe *op, int dirty)
  132. {
  133. if (op->optinsn.insn) {
  134. free_optinsn_slot(op->optinsn.insn, dirty);
  135. op->optinsn.insn = NULL;
  136. }
  137. }
  138. static void
  139. optimized_callback(struct optimized_kprobe *op, struct pt_regs *regs)
  140. {
  141. unsigned long flags;
  142. struct kprobe *p = &op->kp;
  143. struct kprobe_ctlblk *kcb;
  144. /* Save skipped registers */
  145. regs->ARM_pc = (unsigned long)op->kp.addr;
  146. regs->ARM_ORIG_r0 = ~0UL;
  147. local_irq_save(flags);
  148. kcb = get_kprobe_ctlblk();
  149. if (kprobe_running()) {
  150. kprobes_inc_nmissed_count(&op->kp);
  151. } else {
  152. __this_cpu_write(current_kprobe, &op->kp);
  153. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  154. opt_pre_handler(&op->kp, regs);
  155. __this_cpu_write(current_kprobe, NULL);
  156. }
  157. /*
  158. * We singlestep the replaced instruction only when it can't be
  159. * executed directly during restore.
  160. */
  161. if (!p->ainsn.kprobe_direct_exec)
  162. op->kp.ainsn.insn_singlestep(p->opcode, &p->ainsn, regs);
  163. local_irq_restore(flags);
  164. }
  165. NOKPROBE_SYMBOL(optimized_callback)
  166. int arch_prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *orig)
  167. {
  168. kprobe_opcode_t *code;
  169. unsigned long rel_chk;
  170. unsigned long val;
  171. unsigned long stack_protect = sizeof(struct pt_regs);
  172. if (!can_optimize(orig))
  173. return -EILSEQ;
  174. code = get_optinsn_slot();
  175. if (!code)
  176. return -ENOMEM;
  177. /*
  178. * Verify if the address gap is in 32MiB range, because this uses
  179. * a relative jump.
  180. *
  181. * kprobe opt use a 'b' instruction to branch to optinsn.insn.
  182. * According to ARM manual, branch instruction is:
  183. *
  184. * 31 28 27 24 23 0
  185. * +------+---+---+---+---+----------------+
  186. * | cond | 1 | 0 | 1 | 0 | imm24 |
  187. * +------+---+---+---+---+----------------+
  188. *
  189. * imm24 is a signed 24 bits integer. The real branch offset is computed
  190. * by: imm32 = SignExtend(imm24:'00', 32);
  191. *
  192. * So the maximum forward branch should be:
  193. * (0x007fffff << 2) = 0x01fffffc = 0x1fffffc
  194. * The maximum backword branch should be:
  195. * (0xff800000 << 2) = 0xfe000000 = -0x2000000
  196. *
  197. * We can simply check (rel & 0xfe000003):
  198. * if rel is positive, (rel & 0xfe000000) shoule be 0
  199. * if rel is negitive, (rel & 0xfe000000) should be 0xfe000000
  200. * the last '3' is used for alignment checking.
  201. */
  202. rel_chk = (unsigned long)((long)code -
  203. (long)orig->addr + 8) & 0xfe000003;
  204. if ((rel_chk != 0) && (rel_chk != 0xfe000000)) {
  205. /*
  206. * Different from x86, we free code buf directly instead of
  207. * calling __arch_remove_optimized_kprobe() because
  208. * we have not fill any field in op.
  209. */
  210. free_optinsn_slot(code, 0);
  211. return -ERANGE;
  212. }
  213. /* Copy arch-dep-instance from template. */
  214. memcpy(code, (unsigned long *)optprobe_template_entry,
  215. TMPL_END_IDX * sizeof(kprobe_opcode_t));
  216. /* Adjust buffer according to instruction. */
  217. BUG_ON(orig->ainsn.stack_space < 0);
  218. stack_protect += orig->ainsn.stack_space;
  219. /* Should have been filtered by can_optimize(). */
  220. BUG_ON(stack_protect > 255);
  221. /* Create a 'sub sp, sp, #<stack_protect>' */
  222. code[TMPL_SUB_SP] = __opcode_to_mem_arm(0xe24dd000 | stack_protect);
  223. /* Create a 'add r3, sp, #<stack_protect>' */
  224. code[TMPL_ADD_SP] = __opcode_to_mem_arm(0xe28d3000 | stack_protect);
  225. /* Set probe information */
  226. val = (unsigned long)op;
  227. code[TMPL_VAL_IDX] = val;
  228. /* Set probe function call */
  229. val = (unsigned long)optimized_callback;
  230. code[TMPL_CALL_IDX] = val;
  231. /* If possible, copy insn and have it executed during restore */
  232. orig->ainsn.kprobe_direct_exec = false;
  233. if (can_kprobe_direct_exec(orig->ainsn.register_usage_flags)) {
  234. kprobe_opcode_t final_branch = arm_gen_branch(
  235. (unsigned long)(&code[TMPL_RESTORE_END]),
  236. (unsigned long)(op->kp.addr) + 4);
  237. if (final_branch != 0) {
  238. /*
  239. * Replace original 'ldmia sp, {r0 - r15}' with
  240. * 'ldmia {r0 - r14}', restore all registers except pc.
  241. */
  242. code[TMPL_RESTORE_BEGIN] = __opcode_to_mem_arm(0xe89d7fff);
  243. /* The original probed instruction */
  244. code[TMPL_RESTORE_ORIGN_INSN] = __opcode_to_mem_arm(orig->opcode);
  245. /* Jump back to next instruction */
  246. code[TMPL_RESTORE_END] = __opcode_to_mem_arm(final_branch);
  247. orig->ainsn.kprobe_direct_exec = true;
  248. }
  249. }
  250. flush_icache_range((unsigned long)code,
  251. (unsigned long)(&code[TMPL_END_IDX]));
  252. /* Set op->optinsn.insn means prepared. */
  253. op->optinsn.insn = code;
  254. return 0;
  255. }
  256. void __kprobes arch_optimize_kprobes(struct list_head *oplist)
  257. {
  258. struct optimized_kprobe *op, *tmp;
  259. list_for_each_entry_safe(op, tmp, oplist, list) {
  260. unsigned long insn;
  261. WARN_ON(kprobe_disabled(&op->kp));
  262. /*
  263. * Backup instructions which will be replaced
  264. * by jump address
  265. */
  266. memcpy(op->optinsn.copied_insn, op->kp.addr,
  267. RELATIVEJUMP_SIZE);
  268. insn = arm_gen_branch((unsigned long)op->kp.addr,
  269. (unsigned long)op->optinsn.insn);
  270. BUG_ON(insn == 0);
  271. /*
  272. * Make it a conditional branch if replaced insn
  273. * is consitional
  274. */
  275. insn = (__mem_to_opcode_arm(
  276. op->optinsn.copied_insn[0]) & 0xf0000000) |
  277. (insn & 0x0fffffff);
  278. /*
  279. * Similar to __arch_disarm_kprobe, operations which
  280. * removing breakpoints must be wrapped by stop_machine
  281. * to avoid racing.
  282. */
  283. kprobes_remove_breakpoint(op->kp.addr, insn);
  284. list_del_init(&op->list);
  285. }
  286. }
  287. void arch_unoptimize_kprobe(struct optimized_kprobe *op)
  288. {
  289. arch_arm_kprobe(&op->kp);
  290. }
  291. /*
  292. * Recover original instructions and breakpoints from relative jumps.
  293. * Caller must call with locking kprobe_mutex.
  294. */
  295. void arch_unoptimize_kprobes(struct list_head *oplist,
  296. struct list_head *done_list)
  297. {
  298. struct optimized_kprobe *op, *tmp;
  299. list_for_each_entry_safe(op, tmp, oplist, list) {
  300. arch_unoptimize_kprobe(op);
  301. list_move(&op->list, done_list);
  302. }
  303. }
  304. int arch_within_optimized_kprobe(struct optimized_kprobe *op,
  305. kprobe_opcode_t *addr)
  306. {
  307. return (op->kp.addr <= addr &&
  308. op->kp.addr + (RELATIVEJUMP_SIZE / sizeof(kprobe_opcode_t)) > addr);
  309. }
  310. void arch_remove_optimized_kprobe(struct optimized_kprobe *op)
  311. {
  312. __arch_remove_optimized_kprobe(op, 1);
  313. }