opt.c 13 KB

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