opt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. */
  21. #include <linux/kprobes.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/string.h>
  24. #include <linux/slab.h>
  25. #include <linux/hardirq.h>
  26. #include <linux/preempt.h>
  27. #include <linux/extable.h>
  28. #include <linux/kdebug.h>
  29. #include <linux/kallsyms.h>
  30. #include <linux/ftrace.h>
  31. #include <linux/frame.h>
  32. #include <asm/text-patching.h>
  33. #include <asm/cacheflush.h>
  34. #include <asm/desc.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/alternative.h>
  38. #include <asm/insn.h>
  39. #include <asm/debugreg.h>
  40. #include <asm/nospec-branch.h>
  41. #include "common.h"
  42. unsigned long __recover_optprobed_insn(kprobe_opcode_t *buf, unsigned long addr)
  43. {
  44. struct optimized_kprobe *op;
  45. struct kprobe *kp;
  46. long offs;
  47. int i;
  48. for (i = 0; i < RELATIVEJUMP_SIZE; i++) {
  49. kp = get_kprobe((void *)addr - i);
  50. /* This function only handles jump-optimized kprobe */
  51. if (kp && kprobe_optimized(kp)) {
  52. op = container_of(kp, struct optimized_kprobe, kp);
  53. /* If op->list is not empty, op is under optimizing */
  54. if (list_empty(&op->list))
  55. goto found;
  56. }
  57. }
  58. return addr;
  59. found:
  60. /*
  61. * If the kprobe can be optimized, original bytes which can be
  62. * overwritten by jump destination address. In this case, original
  63. * bytes must be recovered from op->optinsn.copied_insn buffer.
  64. */
  65. memcpy(buf, (void *)addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
  66. if (addr == (unsigned long)kp->addr) {
  67. buf[0] = kp->opcode;
  68. memcpy(buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE);
  69. } else {
  70. offs = addr - (unsigned long)kp->addr - 1;
  71. memcpy(buf, op->optinsn.copied_insn + offs, RELATIVE_ADDR_SIZE - offs);
  72. }
  73. return (unsigned long)buf;
  74. }
  75. /* Insert a move instruction which sets a pointer to eax/rdi (1st arg). */
  76. static void synthesize_set_arg1(kprobe_opcode_t *addr, unsigned long val)
  77. {
  78. #ifdef CONFIG_X86_64
  79. *addr++ = 0x48;
  80. *addr++ = 0xbf;
  81. #else
  82. *addr++ = 0xb8;
  83. #endif
  84. *(unsigned long *)addr = val;
  85. }
  86. asm (
  87. "optprobe_template_func:\n"
  88. ".global optprobe_template_entry\n"
  89. "optprobe_template_entry:\n"
  90. #ifdef CONFIG_X86_64
  91. /* We don't bother saving the ss register */
  92. " pushq %rsp\n"
  93. " pushfq\n"
  94. SAVE_REGS_STRING
  95. " movq %rsp, %rsi\n"
  96. ".global optprobe_template_val\n"
  97. "optprobe_template_val:\n"
  98. ASM_NOP5
  99. ASM_NOP5
  100. ".global optprobe_template_call\n"
  101. "optprobe_template_call:\n"
  102. ASM_NOP5
  103. /* Move flags to rsp */
  104. " movq 144(%rsp), %rdx\n"
  105. " movq %rdx, 152(%rsp)\n"
  106. RESTORE_REGS_STRING
  107. /* Skip flags entry */
  108. " addq $8, %rsp\n"
  109. " popfq\n"
  110. #else /* CONFIG_X86_32 */
  111. " pushf\n"
  112. SAVE_REGS_STRING
  113. " movl %esp, %edx\n"
  114. ".global optprobe_template_val\n"
  115. "optprobe_template_val:\n"
  116. ASM_NOP5
  117. ".global optprobe_template_call\n"
  118. "optprobe_template_call:\n"
  119. ASM_NOP5
  120. RESTORE_REGS_STRING
  121. " addl $4, %esp\n" /* skip cs */
  122. " popf\n"
  123. #endif
  124. ".global optprobe_template_end\n"
  125. "optprobe_template_end:\n"
  126. ".type optprobe_template_func, @function\n"
  127. ".size optprobe_template_func, .-optprobe_template_func\n");
  128. void optprobe_template_func(void);
  129. STACK_FRAME_NON_STANDARD(optprobe_template_func);
  130. #define TMPL_MOVE_IDX \
  131. ((long)&optprobe_template_val - (long)&optprobe_template_entry)
  132. #define TMPL_CALL_IDX \
  133. ((long)&optprobe_template_call - (long)&optprobe_template_entry)
  134. #define TMPL_END_IDX \
  135. ((long)&optprobe_template_end - (long)&optprobe_template_entry)
  136. #define INT3_SIZE sizeof(kprobe_opcode_t)
  137. /* Optimized kprobe call back function: called from optinsn */
  138. static void
  139. optimized_callback(struct optimized_kprobe *op, struct pt_regs *regs)
  140. {
  141. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  142. unsigned long flags;
  143. /* This is possible if op is under delayed unoptimizing */
  144. if (kprobe_disabled(&op->kp))
  145. return;
  146. local_irq_save(flags);
  147. if (kprobe_running()) {
  148. kprobes_inc_nmissed_count(&op->kp);
  149. } else {
  150. /* Save skipped registers */
  151. #ifdef CONFIG_X86_64
  152. regs->cs = __KERNEL_CS;
  153. #else
  154. regs->cs = __KERNEL_CS | get_kernel_rpl();
  155. regs->gs = 0;
  156. #endif
  157. regs->ip = (unsigned long)op->kp.addr + INT3_SIZE;
  158. regs->orig_ax = ~0UL;
  159. __this_cpu_write(current_kprobe, &op->kp);
  160. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  161. opt_pre_handler(&op->kp, regs);
  162. __this_cpu_write(current_kprobe, NULL);
  163. }
  164. local_irq_restore(flags);
  165. }
  166. NOKPROBE_SYMBOL(optimized_callback);
  167. static int copy_optimized_instructions(u8 *dest, u8 *src)
  168. {
  169. int len = 0, ret;
  170. while (len < RELATIVEJUMP_SIZE) {
  171. ret = __copy_instruction(dest + len, src + len);
  172. if (!ret || !can_boost(dest + len, src + len))
  173. return -EINVAL;
  174. len += ret;
  175. }
  176. /* Check whether the address range is reserved */
  177. if (ftrace_text_reserved(src, src + len - 1) ||
  178. alternatives_text_reserved(src, src + len - 1) ||
  179. jump_label_text_reserved(src, src + len - 1))
  180. return -EBUSY;
  181. return len;
  182. }
  183. /* Check whether insn is indirect jump */
  184. static int __insn_is_indirect_jump(struct insn *insn)
  185. {
  186. return ((insn->opcode.bytes[0] == 0xff &&
  187. (X86_MODRM_REG(insn->modrm.value) & 6) == 4) || /* Jump */
  188. insn->opcode.bytes[0] == 0xea); /* Segment based jump */
  189. }
  190. /* Check whether insn jumps into specified address range */
  191. static int insn_jump_into_range(struct insn *insn, unsigned long start, int len)
  192. {
  193. unsigned long target = 0;
  194. switch (insn->opcode.bytes[0]) {
  195. case 0xe0: /* loopne */
  196. case 0xe1: /* loope */
  197. case 0xe2: /* loop */
  198. case 0xe3: /* jcxz */
  199. case 0xe9: /* near relative jump */
  200. case 0xeb: /* short relative jump */
  201. break;
  202. case 0x0f:
  203. if ((insn->opcode.bytes[1] & 0xf0) == 0x80) /* jcc near */
  204. break;
  205. return 0;
  206. default:
  207. if ((insn->opcode.bytes[0] & 0xf0) == 0x70) /* jcc short */
  208. break;
  209. return 0;
  210. }
  211. target = (unsigned long)insn->next_byte + insn->immediate.value;
  212. return (start <= target && target <= start + len);
  213. }
  214. static int insn_is_indirect_jump(struct insn *insn)
  215. {
  216. int ret = __insn_is_indirect_jump(insn);
  217. #ifdef CONFIG_RETPOLINE
  218. /*
  219. * Jump to x86_indirect_thunk_* is treated as an indirect jump.
  220. * Note that even with CONFIG_RETPOLINE=y, the kernel compiled with
  221. * older gcc may use indirect jump. So we add this check instead of
  222. * replace indirect-jump check.
  223. */
  224. if (!ret)
  225. ret = insn_jump_into_range(insn,
  226. (unsigned long)__indirect_thunk_start,
  227. (unsigned long)__indirect_thunk_end -
  228. (unsigned long)__indirect_thunk_start);
  229. #endif
  230. return ret;
  231. }
  232. /* Decode whole function to ensure any instructions don't jump into target */
  233. static int can_optimize(unsigned long paddr)
  234. {
  235. unsigned long addr, size = 0, offset = 0;
  236. struct insn insn;
  237. kprobe_opcode_t buf[MAX_INSN_SIZE];
  238. /* Lookup symbol including addr */
  239. if (!kallsyms_lookup_size_offset(paddr, &size, &offset))
  240. return 0;
  241. /*
  242. * Do not optimize in the entry code due to the unstable
  243. * stack handling.
  244. */
  245. if ((paddr >= (unsigned long)__entry_text_start) &&
  246. (paddr < (unsigned long)__entry_text_end))
  247. return 0;
  248. /* Check there is enough space for a relative jump. */
  249. if (size - offset < RELATIVEJUMP_SIZE)
  250. return 0;
  251. /* Decode instructions */
  252. addr = paddr - offset;
  253. while (addr < paddr - offset + size) { /* Decode until function end */
  254. unsigned long recovered_insn;
  255. if (search_exception_tables(addr))
  256. /*
  257. * Since some fixup code will jumps into this function,
  258. * we can't optimize kprobe in this function.
  259. */
  260. return 0;
  261. recovered_insn = recover_probed_instruction(buf, addr);
  262. if (!recovered_insn)
  263. return 0;
  264. kernel_insn_init(&insn, (void *)recovered_insn, MAX_INSN_SIZE);
  265. insn_get_length(&insn);
  266. /* Another subsystem puts a breakpoint */
  267. if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
  268. return 0;
  269. /* Recover address */
  270. insn.kaddr = (void *)addr;
  271. insn.next_byte = (void *)(addr + insn.length);
  272. /* Check any instructions don't jump into target */
  273. if (insn_is_indirect_jump(&insn) ||
  274. insn_jump_into_range(&insn, paddr + INT3_SIZE,
  275. RELATIVE_ADDR_SIZE))
  276. return 0;
  277. addr += insn.length;
  278. }
  279. return 1;
  280. }
  281. /* Check optimized_kprobe can actually be optimized. */
  282. int arch_check_optimized_kprobe(struct optimized_kprobe *op)
  283. {
  284. int i;
  285. struct kprobe *p;
  286. for (i = 1; i < op->optinsn.size; i++) {
  287. p = get_kprobe(op->kp.addr + i);
  288. if (p && !kprobe_disabled(p))
  289. return -EEXIST;
  290. }
  291. return 0;
  292. }
  293. /* Check the addr is within the optimized instructions. */
  294. int arch_within_optimized_kprobe(struct optimized_kprobe *op,
  295. unsigned long addr)
  296. {
  297. return ((unsigned long)op->kp.addr <= addr &&
  298. (unsigned long)op->kp.addr + op->optinsn.size > addr);
  299. }
  300. /* Free optimized instruction slot */
  301. static
  302. void __arch_remove_optimized_kprobe(struct optimized_kprobe *op, int dirty)
  303. {
  304. if (op->optinsn.insn) {
  305. free_optinsn_slot(op->optinsn.insn, dirty);
  306. op->optinsn.insn = NULL;
  307. op->optinsn.size = 0;
  308. }
  309. }
  310. void arch_remove_optimized_kprobe(struct optimized_kprobe *op)
  311. {
  312. __arch_remove_optimized_kprobe(op, 1);
  313. }
  314. /*
  315. * Copy replacing target instructions
  316. * Target instructions MUST be relocatable (checked inside)
  317. * This is called when new aggr(opt)probe is allocated or reused.
  318. */
  319. int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
  320. struct kprobe *__unused)
  321. {
  322. u8 *buf;
  323. int ret;
  324. long rel;
  325. if (!can_optimize((unsigned long)op->kp.addr))
  326. return -EILSEQ;
  327. op->optinsn.insn = get_optinsn_slot();
  328. if (!op->optinsn.insn)
  329. return -ENOMEM;
  330. /*
  331. * Verify if the address gap is in 2GB range, because this uses
  332. * a relative jump.
  333. */
  334. rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE;
  335. if (abs(rel) > 0x7fffffff) {
  336. __arch_remove_optimized_kprobe(op, 0);
  337. return -ERANGE;
  338. }
  339. buf = (u8 *)op->optinsn.insn;
  340. set_memory_rw((unsigned long)buf & PAGE_MASK, 1);
  341. /* Copy instructions into the out-of-line buffer */
  342. ret = copy_optimized_instructions(buf + TMPL_END_IDX, op->kp.addr);
  343. if (ret < 0) {
  344. __arch_remove_optimized_kprobe(op, 0);
  345. return ret;
  346. }
  347. op->optinsn.size = ret;
  348. /* Copy arch-dep-instance from template */
  349. memcpy(buf, &optprobe_template_entry, TMPL_END_IDX);
  350. /* Set probe information */
  351. synthesize_set_arg1(buf + TMPL_MOVE_IDX, (unsigned long)op);
  352. /* Set probe function call */
  353. synthesize_relcall(buf + TMPL_CALL_IDX, optimized_callback);
  354. /* Set returning jmp instruction at the tail of out-of-line buffer */
  355. synthesize_reljump(buf + TMPL_END_IDX + op->optinsn.size,
  356. (u8 *)op->kp.addr + op->optinsn.size);
  357. set_memory_ro((unsigned long)buf & PAGE_MASK, 1);
  358. flush_icache_range((unsigned long) buf,
  359. (unsigned long) buf + TMPL_END_IDX +
  360. op->optinsn.size + RELATIVEJUMP_SIZE);
  361. return 0;
  362. }
  363. /*
  364. * Replace breakpoints (int3) with relative jumps.
  365. * Caller must call with locking kprobe_mutex and text_mutex.
  366. */
  367. void arch_optimize_kprobes(struct list_head *oplist)
  368. {
  369. struct optimized_kprobe *op, *tmp;
  370. u8 insn_buf[RELATIVEJUMP_SIZE];
  371. list_for_each_entry_safe(op, tmp, oplist, list) {
  372. s32 rel = (s32)((long)op->optinsn.insn -
  373. ((long)op->kp.addr + RELATIVEJUMP_SIZE));
  374. WARN_ON(kprobe_disabled(&op->kp));
  375. /* Backup instructions which will be replaced by jump address */
  376. memcpy(op->optinsn.copied_insn, op->kp.addr + INT3_SIZE,
  377. RELATIVE_ADDR_SIZE);
  378. insn_buf[0] = RELATIVEJUMP_OPCODE;
  379. *(s32 *)(&insn_buf[1]) = rel;
  380. text_poke_bp(op->kp.addr, insn_buf, RELATIVEJUMP_SIZE,
  381. op->optinsn.insn);
  382. list_del_init(&op->list);
  383. }
  384. }
  385. /* Replace a relative jump with a breakpoint (int3). */
  386. void arch_unoptimize_kprobe(struct optimized_kprobe *op)
  387. {
  388. u8 insn_buf[RELATIVEJUMP_SIZE];
  389. /* Set int3 to first byte for kprobes */
  390. insn_buf[0] = BREAKPOINT_INSTRUCTION;
  391. memcpy(insn_buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE);
  392. text_poke_bp(op->kp.addr, insn_buf, RELATIVEJUMP_SIZE,
  393. op->optinsn.insn);
  394. }
  395. /*
  396. * Recover original instructions and breakpoints from relative jumps.
  397. * Caller must call with locking kprobe_mutex.
  398. */
  399. extern void arch_unoptimize_kprobes(struct list_head *oplist,
  400. struct list_head *done_list)
  401. {
  402. struct optimized_kprobe *op, *tmp;
  403. list_for_each_entry_safe(op, tmp, oplist, list) {
  404. arch_unoptimize_kprobe(op);
  405. list_move(&op->list, done_list);
  406. }
  407. }
  408. int setup_detour_execution(struct kprobe *p, struct pt_regs *regs, int reenter)
  409. {
  410. struct optimized_kprobe *op;
  411. if (p->flags & KPROBE_FLAG_OPTIMIZED) {
  412. /* This kprobe is really able to run optimized path. */
  413. op = container_of(p, struct optimized_kprobe, kp);
  414. /* Detour through copied instructions */
  415. regs->ip = (unsigned long)op->optinsn.insn + TMPL_END_IDX;
  416. if (!reenter)
  417. reset_current_kprobe();
  418. preempt_enable_no_resched();
  419. return 1;
  420. }
  421. return 0;
  422. }
  423. NOKPROBE_SYMBOL(setup_detour_execution);