opt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 "common.h"
  40. unsigned long __recover_optprobed_insn(kprobe_opcode_t *buf, unsigned long addr)
  41. {
  42. struct optimized_kprobe *op;
  43. struct kprobe *kp;
  44. long offs;
  45. int i;
  46. for (i = 0; i < RELATIVEJUMP_SIZE; i++) {
  47. kp = get_kprobe((void *)addr - i);
  48. /* This function only handles jump-optimized kprobe */
  49. if (kp && kprobe_optimized(kp)) {
  50. op = container_of(kp, struct optimized_kprobe, kp);
  51. /* If op->list is not empty, op is under optimizing */
  52. if (list_empty(&op->list))
  53. goto found;
  54. }
  55. }
  56. return addr;
  57. found:
  58. /*
  59. * If the kprobe can be optimized, original bytes which can be
  60. * overwritten by jump destination address. In this case, original
  61. * bytes must be recovered from op->optinsn.copied_insn buffer.
  62. */
  63. memcpy(buf, (void *)addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
  64. if (addr == (unsigned long)kp->addr) {
  65. buf[0] = kp->opcode;
  66. memcpy(buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE);
  67. } else {
  68. offs = addr - (unsigned long)kp->addr - 1;
  69. memcpy(buf, op->optinsn.copied_insn + offs, RELATIVE_ADDR_SIZE - offs);
  70. }
  71. return (unsigned long)buf;
  72. }
  73. /* Insert a move instruction which sets a pointer to eax/rdi (1st arg). */
  74. static void synthesize_set_arg1(kprobe_opcode_t *addr, unsigned long val)
  75. {
  76. #ifdef CONFIG_X86_64
  77. *addr++ = 0x48;
  78. *addr++ = 0xbf;
  79. #else
  80. *addr++ = 0xb8;
  81. #endif
  82. *(unsigned long *)addr = val;
  83. }
  84. asm (
  85. ".global optprobe_template_entry\n"
  86. "optprobe_template_entry:\n"
  87. #ifdef CONFIG_X86_64
  88. /* We don't bother saving the ss register */
  89. " pushq %rsp\n"
  90. " pushfq\n"
  91. SAVE_REGS_STRING
  92. " movq %rsp, %rsi\n"
  93. ".global optprobe_template_val\n"
  94. "optprobe_template_val:\n"
  95. ASM_NOP5
  96. ASM_NOP5
  97. ".global optprobe_template_call\n"
  98. "optprobe_template_call:\n"
  99. ASM_NOP5
  100. /* Move flags to rsp */
  101. " movq 144(%rsp), %rdx\n"
  102. " movq %rdx, 152(%rsp)\n"
  103. RESTORE_REGS_STRING
  104. /* Skip flags entry */
  105. " addq $8, %rsp\n"
  106. " popfq\n"
  107. #else /* CONFIG_X86_32 */
  108. " pushf\n"
  109. SAVE_REGS_STRING
  110. " movl %esp, %edx\n"
  111. ".global optprobe_template_val\n"
  112. "optprobe_template_val:\n"
  113. ASM_NOP5
  114. ".global optprobe_template_call\n"
  115. "optprobe_template_call:\n"
  116. ASM_NOP5
  117. RESTORE_REGS_STRING
  118. " addl $4, %esp\n" /* skip cs */
  119. " popf\n"
  120. #endif
  121. ".global optprobe_template_end\n"
  122. "optprobe_template_end:\n");
  123. #define TMPL_MOVE_IDX \
  124. ((long)&optprobe_template_val - (long)&optprobe_template_entry)
  125. #define TMPL_CALL_IDX \
  126. ((long)&optprobe_template_call - (long)&optprobe_template_entry)
  127. #define TMPL_END_IDX \
  128. ((long)&optprobe_template_end - (long)&optprobe_template_entry)
  129. #define INT3_SIZE sizeof(kprobe_opcode_t)
  130. /* Optimized kprobe call back function: called from optinsn */
  131. static void
  132. optimized_callback(struct optimized_kprobe *op, struct pt_regs *regs)
  133. {
  134. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  135. unsigned long flags;
  136. /* This is possible if op is under delayed unoptimizing */
  137. if (kprobe_disabled(&op->kp))
  138. return;
  139. local_irq_save(flags);
  140. if (kprobe_running()) {
  141. kprobes_inc_nmissed_count(&op->kp);
  142. } else {
  143. /* Save skipped registers */
  144. #ifdef CONFIG_X86_64
  145. regs->cs = __KERNEL_CS;
  146. #else
  147. regs->cs = __KERNEL_CS | get_kernel_rpl();
  148. regs->gs = 0;
  149. #endif
  150. regs->ip = (unsigned long)op->kp.addr + INT3_SIZE;
  151. regs->orig_ax = ~0UL;
  152. __this_cpu_write(current_kprobe, &op->kp);
  153. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  154. opt_pre_handler(&op->kp, regs);
  155. __this_cpu_write(current_kprobe, NULL);
  156. }
  157. local_irq_restore(flags);
  158. }
  159. NOKPROBE_SYMBOL(optimized_callback);
  160. static int 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 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 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. unsigned long recovered_insn;
  230. if (search_exception_tables(addr))
  231. /*
  232. * Since some fixup code will jumps into this function,
  233. * we can't optimize kprobe in this function.
  234. */
  235. return 0;
  236. recovered_insn = recover_probed_instruction(buf, addr);
  237. if (!recovered_insn)
  238. return 0;
  239. kernel_insn_init(&insn, (void *)recovered_insn, MAX_INSN_SIZE);
  240. insn_get_length(&insn);
  241. /* Another subsystem puts a breakpoint */
  242. if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
  243. return 0;
  244. /* Recover address */
  245. insn.kaddr = (void *)addr;
  246. insn.next_byte = (void *)(addr + insn.length);
  247. /* Check any instructions don't jump into target */
  248. if (insn_is_indirect_jump(&insn) ||
  249. insn_jump_into_range(&insn, paddr + INT3_SIZE,
  250. RELATIVE_ADDR_SIZE))
  251. return 0;
  252. addr += insn.length;
  253. }
  254. return 1;
  255. }
  256. /* Check optimized_kprobe can actually be optimized. */
  257. int arch_check_optimized_kprobe(struct optimized_kprobe *op)
  258. {
  259. int i;
  260. struct kprobe *p;
  261. for (i = 1; i < op->optinsn.size; i++) {
  262. p = get_kprobe(op->kp.addr + i);
  263. if (p && !kprobe_disabled(p))
  264. return -EEXIST;
  265. }
  266. return 0;
  267. }
  268. /* Check the addr is within the optimized instructions. */
  269. int arch_within_optimized_kprobe(struct optimized_kprobe *op,
  270. unsigned long addr)
  271. {
  272. return ((unsigned long)op->kp.addr <= addr &&
  273. (unsigned long)op->kp.addr + op->optinsn.size > addr);
  274. }
  275. /* Free optimized instruction slot */
  276. static
  277. void __arch_remove_optimized_kprobe(struct optimized_kprobe *op, int dirty)
  278. {
  279. if (op->optinsn.insn) {
  280. free_optinsn_slot(op->optinsn.insn, dirty);
  281. op->optinsn.insn = NULL;
  282. op->optinsn.size = 0;
  283. }
  284. }
  285. void arch_remove_optimized_kprobe(struct optimized_kprobe *op)
  286. {
  287. __arch_remove_optimized_kprobe(op, 1);
  288. }
  289. /*
  290. * Copy replacing target instructions
  291. * Target instructions MUST be relocatable (checked inside)
  292. * This is called when new aggr(opt)probe is allocated or reused.
  293. */
  294. int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
  295. struct kprobe *__unused)
  296. {
  297. u8 *buf;
  298. int ret;
  299. long rel;
  300. if (!can_optimize((unsigned long)op->kp.addr))
  301. return -EILSEQ;
  302. op->optinsn.insn = get_optinsn_slot();
  303. if (!op->optinsn.insn)
  304. return -ENOMEM;
  305. /*
  306. * Verify if the address gap is in 2GB range, because this uses
  307. * a relative jump.
  308. */
  309. rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE;
  310. if (abs(rel) > 0x7fffffff) {
  311. __arch_remove_optimized_kprobe(op, 0);
  312. return -ERANGE;
  313. }
  314. buf = (u8 *)op->optinsn.insn;
  315. /* Copy instructions into the out-of-line buffer */
  316. ret = copy_optimized_instructions(buf + TMPL_END_IDX, op->kp.addr);
  317. if (ret < 0) {
  318. __arch_remove_optimized_kprobe(op, 0);
  319. return ret;
  320. }
  321. op->optinsn.size = ret;
  322. /* Copy arch-dep-instance from template */
  323. memcpy(buf, &optprobe_template_entry, TMPL_END_IDX);
  324. /* Set probe information */
  325. synthesize_set_arg1(buf + TMPL_MOVE_IDX, (unsigned long)op);
  326. /* Set probe function call */
  327. synthesize_relcall(buf + TMPL_CALL_IDX, optimized_callback);
  328. /* Set returning jmp instruction at the tail of out-of-line buffer */
  329. synthesize_reljump(buf + TMPL_END_IDX + op->optinsn.size,
  330. (u8 *)op->kp.addr + op->optinsn.size);
  331. flush_icache_range((unsigned long) buf,
  332. (unsigned long) buf + TMPL_END_IDX +
  333. op->optinsn.size + RELATIVEJUMP_SIZE);
  334. return 0;
  335. }
  336. /*
  337. * Replace breakpoints (int3) with relative jumps.
  338. * Caller must call with locking kprobe_mutex and text_mutex.
  339. */
  340. void arch_optimize_kprobes(struct list_head *oplist)
  341. {
  342. struct optimized_kprobe *op, *tmp;
  343. u8 insn_buf[RELATIVEJUMP_SIZE];
  344. list_for_each_entry_safe(op, tmp, oplist, list) {
  345. s32 rel = (s32)((long)op->optinsn.insn -
  346. ((long)op->kp.addr + RELATIVEJUMP_SIZE));
  347. WARN_ON(kprobe_disabled(&op->kp));
  348. /* Backup instructions which will be replaced by jump address */
  349. memcpy(op->optinsn.copied_insn, op->kp.addr + INT3_SIZE,
  350. RELATIVE_ADDR_SIZE);
  351. insn_buf[0] = RELATIVEJUMP_OPCODE;
  352. *(s32 *)(&insn_buf[1]) = rel;
  353. text_poke_bp(op->kp.addr, insn_buf, RELATIVEJUMP_SIZE,
  354. op->optinsn.insn);
  355. list_del_init(&op->list);
  356. }
  357. }
  358. /* Replace a relative jump with a breakpoint (int3). */
  359. void arch_unoptimize_kprobe(struct optimized_kprobe *op)
  360. {
  361. u8 insn_buf[RELATIVEJUMP_SIZE];
  362. /* Set int3 to first byte for kprobes */
  363. insn_buf[0] = BREAKPOINT_INSTRUCTION;
  364. memcpy(insn_buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE);
  365. text_poke_bp(op->kp.addr, insn_buf, RELATIVEJUMP_SIZE,
  366. op->optinsn.insn);
  367. }
  368. /*
  369. * Recover original instructions and breakpoints from relative jumps.
  370. * Caller must call with locking kprobe_mutex.
  371. */
  372. extern void arch_unoptimize_kprobes(struct list_head *oplist,
  373. struct list_head *done_list)
  374. {
  375. struct optimized_kprobe *op, *tmp;
  376. list_for_each_entry_safe(op, tmp, oplist, list) {
  377. arch_unoptimize_kprobe(op);
  378. list_move(&op->list, done_list);
  379. }
  380. }
  381. int setup_detour_execution(struct kprobe *p, struct pt_regs *regs, int reenter)
  382. {
  383. struct optimized_kprobe *op;
  384. if (p->flags & KPROBE_FLAG_OPTIMIZED) {
  385. /* This kprobe is really able to run optimized path. */
  386. op = container_of(p, struct optimized_kprobe, kp);
  387. /* Detour through copied instructions */
  388. regs->ip = (unsigned long)op->optinsn.insn + TMPL_END_IDX;
  389. if (!reenter)
  390. reset_current_kprobe();
  391. preempt_enable_no_resched();
  392. return 1;
  393. }
  394. return 0;
  395. }
  396. NOKPROBE_SYMBOL(setup_detour_execution);