opt.c 12 KB

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