kprobes-opt.c 14 KB

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