opt-arm.c 11 KB

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