opt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 "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 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. asm (
  84. ".global optprobe_template_entry\n"
  85. "optprobe_template_entry:\n"
  86. #ifdef CONFIG_X86_64
  87. /* We don't bother saving the ss register */
  88. " pushq %rsp\n"
  89. " pushfq\n"
  90. SAVE_REGS_STRING
  91. " movq %rsp, %rsi\n"
  92. ".global optprobe_template_val\n"
  93. "optprobe_template_val:\n"
  94. ASM_NOP5
  95. ASM_NOP5
  96. ".global optprobe_template_call\n"
  97. "optprobe_template_call:\n"
  98. ASM_NOP5
  99. /* Move flags to rsp */
  100. " movq 144(%rsp), %rdx\n"
  101. " movq %rdx, 152(%rsp)\n"
  102. RESTORE_REGS_STRING
  103. /* Skip flags entry */
  104. " addq $8, %rsp\n"
  105. " popfq\n"
  106. #else /* CONFIG_X86_32 */
  107. " pushf\n"
  108. SAVE_REGS_STRING
  109. " movl %esp, %edx\n"
  110. ".global optprobe_template_val\n"
  111. "optprobe_template_val:\n"
  112. ASM_NOP5
  113. ".global optprobe_template_call\n"
  114. "optprobe_template_call:\n"
  115. ASM_NOP5
  116. RESTORE_REGS_STRING
  117. " addl $4, %esp\n" /* skip cs */
  118. " popf\n"
  119. #endif
  120. ".global optprobe_template_end\n"
  121. "optprobe_template_end:\n");
  122. #define TMPL_MOVE_IDX \
  123. ((long)&optprobe_template_val - (long)&optprobe_template_entry)
  124. #define TMPL_CALL_IDX \
  125. ((long)&optprobe_template_call - (long)&optprobe_template_entry)
  126. #define TMPL_END_IDX \
  127. ((long)&optprobe_template_end - (long)&optprobe_template_entry)
  128. #define INT3_SIZE sizeof(kprobe_opcode_t)
  129. /* Optimized kprobe call back function: called from optinsn */
  130. static void
  131. optimized_callback(struct optimized_kprobe *op, struct pt_regs *regs)
  132. {
  133. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  134. unsigned long flags;
  135. /* This is possible if op is under delayed unoptimizing */
  136. if (kprobe_disabled(&op->kp))
  137. return;
  138. local_irq_save(flags);
  139. if (kprobe_running()) {
  140. kprobes_inc_nmissed_count(&op->kp);
  141. } else {
  142. /* Save skipped registers */
  143. #ifdef CONFIG_X86_64
  144. regs->cs = __KERNEL_CS;
  145. #else
  146. regs->cs = __KERNEL_CS | get_kernel_rpl();
  147. regs->gs = 0;
  148. #endif
  149. regs->ip = (unsigned long)op->kp.addr + INT3_SIZE;
  150. regs->orig_ax = ~0UL;
  151. __this_cpu_write(current_kprobe, &op->kp);
  152. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  153. opt_pre_handler(&op->kp, regs);
  154. __this_cpu_write(current_kprobe, NULL);
  155. }
  156. local_irq_restore(flags);
  157. }
  158. NOKPROBE_SYMBOL(optimized_callback);
  159. static int copy_optimized_instructions(u8 *dest, u8 *src)
  160. {
  161. int len = 0, ret;
  162. while (len < RELATIVEJUMP_SIZE) {
  163. ret = __copy_instruction(dest + len, src + len);
  164. if (!ret || !can_boost(dest + len))
  165. return -EINVAL;
  166. len += ret;
  167. }
  168. /* Check whether the address range is reserved */
  169. if (ftrace_text_reserved(src, src + len - 1) ||
  170. alternatives_text_reserved(src, src + len - 1) ||
  171. jump_label_text_reserved(src, src + len - 1))
  172. return -EBUSY;
  173. return len;
  174. }
  175. /* Check whether insn is indirect jump */
  176. static int insn_is_indirect_jump(struct insn *insn)
  177. {
  178. return ((insn->opcode.bytes[0] == 0xff &&
  179. (X86_MODRM_REG(insn->modrm.value) & 6) == 4) || /* Jump */
  180. insn->opcode.bytes[0] == 0xea); /* Segment based jump */
  181. }
  182. /* Check whether insn jumps into specified address range */
  183. static int insn_jump_into_range(struct insn *insn, unsigned long start, int len)
  184. {
  185. unsigned long target = 0;
  186. switch (insn->opcode.bytes[0]) {
  187. case 0xe0: /* loopne */
  188. case 0xe1: /* loope */
  189. case 0xe2: /* loop */
  190. case 0xe3: /* jcxz */
  191. case 0xe9: /* near relative jump */
  192. case 0xeb: /* short relative jump */
  193. break;
  194. case 0x0f:
  195. if ((insn->opcode.bytes[1] & 0xf0) == 0x80) /* jcc near */
  196. break;
  197. return 0;
  198. default:
  199. if ((insn->opcode.bytes[0] & 0xf0) == 0x70) /* jcc short */
  200. break;
  201. return 0;
  202. }
  203. target = (unsigned long)insn->next_byte + insn->immediate.value;
  204. return (start <= target && target <= start + len);
  205. }
  206. /* Decode whole function to ensure any instructions don't jump into target */
  207. static int can_optimize(unsigned long paddr)
  208. {
  209. unsigned long addr, size = 0, offset = 0;
  210. struct insn insn;
  211. kprobe_opcode_t buf[MAX_INSN_SIZE];
  212. /* Lookup symbol including addr */
  213. if (!kallsyms_lookup_size_offset(paddr, &size, &offset))
  214. return 0;
  215. /*
  216. * Do not optimize in the entry code due to the unstable
  217. * stack handling.
  218. */
  219. if ((paddr >= (unsigned long)__entry_text_start) &&
  220. (paddr < (unsigned long)__entry_text_end))
  221. return 0;
  222. /* Check there is enough space for a relative jump. */
  223. if (size - offset < RELATIVEJUMP_SIZE)
  224. return 0;
  225. /* Decode instructions */
  226. addr = paddr - offset;
  227. while (addr < paddr - offset + size) { /* Decode until function end */
  228. if (search_exception_tables(addr))
  229. /*
  230. * Since some fixup code will jumps into this function,
  231. * we can't optimize kprobe in this function.
  232. */
  233. return 0;
  234. kernel_insn_init(&insn, (void *)recover_probed_instruction(buf, addr));
  235. insn_get_length(&insn);
  236. /* Another subsystem puts a breakpoint */
  237. if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
  238. return 0;
  239. /* Recover address */
  240. insn.kaddr = (void *)addr;
  241. insn.next_byte = (void *)(addr + insn.length);
  242. /* Check any instructions don't jump into target */
  243. if (insn_is_indirect_jump(&insn) ||
  244. insn_jump_into_range(&insn, paddr + INT3_SIZE,
  245. RELATIVE_ADDR_SIZE))
  246. return 0;
  247. addr += insn.length;
  248. }
  249. return 1;
  250. }
  251. /* Check optimized_kprobe can actually be optimized. */
  252. int arch_check_optimized_kprobe(struct optimized_kprobe *op)
  253. {
  254. int i;
  255. struct kprobe *p;
  256. for (i = 1; i < op->optinsn.size; i++) {
  257. p = get_kprobe(op->kp.addr + i);
  258. if (p && !kprobe_disabled(p))
  259. return -EEXIST;
  260. }
  261. return 0;
  262. }
  263. /* Check the addr is within the optimized instructions. */
  264. int arch_within_optimized_kprobe(struct optimized_kprobe *op,
  265. unsigned long addr)
  266. {
  267. return ((unsigned long)op->kp.addr <= addr &&
  268. (unsigned long)op->kp.addr + op->optinsn.size > addr);
  269. }
  270. /* Free optimized instruction slot */
  271. static
  272. void __arch_remove_optimized_kprobe(struct optimized_kprobe *op, int dirty)
  273. {
  274. if (op->optinsn.insn) {
  275. free_optinsn_slot(op->optinsn.insn, dirty);
  276. op->optinsn.insn = NULL;
  277. op->optinsn.size = 0;
  278. }
  279. }
  280. void arch_remove_optimized_kprobe(struct optimized_kprobe *op)
  281. {
  282. __arch_remove_optimized_kprobe(op, 1);
  283. }
  284. /*
  285. * Copy replacing target instructions
  286. * Target instructions MUST be relocatable (checked inside)
  287. * This is called when new aggr(opt)probe is allocated or reused.
  288. */
  289. int arch_prepare_optimized_kprobe(struct optimized_kprobe *op)
  290. {
  291. u8 *buf;
  292. int ret;
  293. long rel;
  294. if (!can_optimize((unsigned long)op->kp.addr))
  295. return -EILSEQ;
  296. op->optinsn.insn = get_optinsn_slot();
  297. if (!op->optinsn.insn)
  298. return -ENOMEM;
  299. /*
  300. * Verify if the address gap is in 2GB range, because this uses
  301. * a relative jump.
  302. */
  303. rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE;
  304. if (abs(rel) > 0x7fffffff)
  305. return -ERANGE;
  306. buf = (u8 *)op->optinsn.insn;
  307. /* Copy instructions into the out-of-line buffer */
  308. ret = copy_optimized_instructions(buf + TMPL_END_IDX, op->kp.addr);
  309. if (ret < 0) {
  310. __arch_remove_optimized_kprobe(op, 0);
  311. return ret;
  312. }
  313. op->optinsn.size = ret;
  314. /* Copy arch-dep-instance from template */
  315. memcpy(buf, &optprobe_template_entry, TMPL_END_IDX);
  316. /* Set probe information */
  317. synthesize_set_arg1(buf + TMPL_MOVE_IDX, (unsigned long)op);
  318. /* Set probe function call */
  319. synthesize_relcall(buf + TMPL_CALL_IDX, optimized_callback);
  320. /* Set returning jmp instruction at the tail of out-of-line buffer */
  321. synthesize_reljump(buf + TMPL_END_IDX + op->optinsn.size,
  322. (u8 *)op->kp.addr + op->optinsn.size);
  323. flush_icache_range((unsigned long) buf,
  324. (unsigned long) buf + TMPL_END_IDX +
  325. op->optinsn.size + RELATIVEJUMP_SIZE);
  326. return 0;
  327. }
  328. /*
  329. * Replace breakpoints (int3) with relative jumps.
  330. * Caller must call with locking kprobe_mutex and text_mutex.
  331. */
  332. void arch_optimize_kprobes(struct list_head *oplist)
  333. {
  334. struct optimized_kprobe *op, *tmp;
  335. u8 insn_buf[RELATIVEJUMP_SIZE];
  336. list_for_each_entry_safe(op, tmp, oplist, list) {
  337. s32 rel = (s32)((long)op->optinsn.insn -
  338. ((long)op->kp.addr + RELATIVEJUMP_SIZE));
  339. WARN_ON(kprobe_disabled(&op->kp));
  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. text_poke_bp(op->kp.addr, insn_buf, RELATIVEJUMP_SIZE,
  346. op->optinsn.insn);
  347. list_del_init(&op->list);
  348. }
  349. }
  350. /* Replace a relative jump with a breakpoint (int3). */
  351. void arch_unoptimize_kprobe(struct optimized_kprobe *op)
  352. {
  353. u8 insn_buf[RELATIVEJUMP_SIZE];
  354. /* Set int3 to first byte for kprobes */
  355. insn_buf[0] = BREAKPOINT_INSTRUCTION;
  356. memcpy(insn_buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE);
  357. text_poke_bp(op->kp.addr, insn_buf, RELATIVEJUMP_SIZE,
  358. op->optinsn.insn);
  359. }
  360. /*
  361. * Recover original instructions and breakpoints from relative jumps.
  362. * Caller must call with locking kprobe_mutex.
  363. */
  364. extern void arch_unoptimize_kprobes(struct list_head *oplist,
  365. struct list_head *done_list)
  366. {
  367. struct optimized_kprobe *op, *tmp;
  368. list_for_each_entry_safe(op, tmp, oplist, list) {
  369. arch_unoptimize_kprobe(op);
  370. list_move(&op->list, done_list);
  371. }
  372. }
  373. int setup_detour_execution(struct kprobe *p, struct pt_regs *regs, int reenter)
  374. {
  375. struct optimized_kprobe *op;
  376. if (p->flags & KPROBE_FLAG_OPTIMIZED) {
  377. /* This kprobe is really able to run optimized path. */
  378. op = container_of(p, struct optimized_kprobe, kp);
  379. /* Detour through copied instructions */
  380. regs->ip = (unsigned long)op->optinsn.insn + TMPL_END_IDX;
  381. if (!reenter)
  382. reset_current_kprobe();
  383. preempt_enable_no_resched();
  384. return 1;
  385. }
  386. return 0;
  387. }
  388. NOKPROBE_SYMBOL(setup_detour_execution);