opt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. unsigned long recovered_insn;
  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. recovered_insn = recover_probed_instruction(buf, addr);
  236. kernel_insn_init(&insn, (void *)recovered_insn, MAX_INSN_SIZE);
  237. insn_get_length(&insn);
  238. /* Another subsystem puts a breakpoint */
  239. if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
  240. return 0;
  241. /* Recover address */
  242. insn.kaddr = (void *)addr;
  243. insn.next_byte = (void *)(addr + insn.length);
  244. /* Check any instructions don't jump into target */
  245. if (insn_is_indirect_jump(&insn) ||
  246. insn_jump_into_range(&insn, paddr + INT3_SIZE,
  247. RELATIVE_ADDR_SIZE))
  248. return 0;
  249. addr += insn.length;
  250. }
  251. return 1;
  252. }
  253. /* Check optimized_kprobe can actually be optimized. */
  254. int arch_check_optimized_kprobe(struct optimized_kprobe *op)
  255. {
  256. int i;
  257. struct kprobe *p;
  258. for (i = 1; i < op->optinsn.size; i++) {
  259. p = get_kprobe(op->kp.addr + i);
  260. if (p && !kprobe_disabled(p))
  261. return -EEXIST;
  262. }
  263. return 0;
  264. }
  265. /* Check the addr is within the optimized instructions. */
  266. int arch_within_optimized_kprobe(struct optimized_kprobe *op,
  267. unsigned long addr)
  268. {
  269. return ((unsigned long)op->kp.addr <= addr &&
  270. (unsigned long)op->kp.addr + op->optinsn.size > addr);
  271. }
  272. /* Free optimized instruction slot */
  273. static
  274. void __arch_remove_optimized_kprobe(struct optimized_kprobe *op, int dirty)
  275. {
  276. if (op->optinsn.insn) {
  277. free_optinsn_slot(op->optinsn.insn, dirty);
  278. op->optinsn.insn = NULL;
  279. op->optinsn.size = 0;
  280. }
  281. }
  282. void arch_remove_optimized_kprobe(struct optimized_kprobe *op)
  283. {
  284. __arch_remove_optimized_kprobe(op, 1);
  285. }
  286. /*
  287. * Copy replacing target instructions
  288. * Target instructions MUST be relocatable (checked inside)
  289. * This is called when new aggr(opt)probe is allocated or reused.
  290. */
  291. int arch_prepare_optimized_kprobe(struct optimized_kprobe *op)
  292. {
  293. u8 *buf;
  294. int ret;
  295. long rel;
  296. if (!can_optimize((unsigned long)op->kp.addr))
  297. return -EILSEQ;
  298. op->optinsn.insn = get_optinsn_slot();
  299. if (!op->optinsn.insn)
  300. return -ENOMEM;
  301. /*
  302. * Verify if the address gap is in 2GB range, because this uses
  303. * a relative jump.
  304. */
  305. rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE;
  306. if (abs(rel) > 0x7fffffff) {
  307. __arch_remove_optimized_kprobe(op, 0);
  308. return -ERANGE;
  309. }
  310. buf = (u8 *)op->optinsn.insn;
  311. /* Copy instructions into the out-of-line buffer */
  312. ret = copy_optimized_instructions(buf + TMPL_END_IDX, op->kp.addr);
  313. if (ret < 0) {
  314. __arch_remove_optimized_kprobe(op, 0);
  315. return ret;
  316. }
  317. op->optinsn.size = ret;
  318. /* Copy arch-dep-instance from template */
  319. memcpy(buf, &optprobe_template_entry, TMPL_END_IDX);
  320. /* Set probe information */
  321. synthesize_set_arg1(buf + TMPL_MOVE_IDX, (unsigned long)op);
  322. /* Set probe function call */
  323. synthesize_relcall(buf + TMPL_CALL_IDX, optimized_callback);
  324. /* Set returning jmp instruction at the tail of out-of-line buffer */
  325. synthesize_reljump(buf + TMPL_END_IDX + op->optinsn.size,
  326. (u8 *)op->kp.addr + op->optinsn.size);
  327. flush_icache_range((unsigned long) buf,
  328. (unsigned long) buf + TMPL_END_IDX +
  329. op->optinsn.size + RELATIVEJUMP_SIZE);
  330. return 0;
  331. }
  332. /*
  333. * Replace breakpoints (int3) with relative jumps.
  334. * Caller must call with locking kprobe_mutex and text_mutex.
  335. */
  336. void arch_optimize_kprobes(struct list_head *oplist)
  337. {
  338. struct optimized_kprobe *op, *tmp;
  339. u8 insn_buf[RELATIVEJUMP_SIZE];
  340. list_for_each_entry_safe(op, tmp, oplist, list) {
  341. s32 rel = (s32)((long)op->optinsn.insn -
  342. ((long)op->kp.addr + RELATIVEJUMP_SIZE));
  343. WARN_ON(kprobe_disabled(&op->kp));
  344. /* Backup instructions which will be replaced by jump address */
  345. memcpy(op->optinsn.copied_insn, op->kp.addr + INT3_SIZE,
  346. RELATIVE_ADDR_SIZE);
  347. insn_buf[0] = RELATIVEJUMP_OPCODE;
  348. *(s32 *)(&insn_buf[1]) = rel;
  349. text_poke_bp(op->kp.addr, insn_buf, RELATIVEJUMP_SIZE,
  350. op->optinsn.insn);
  351. list_del_init(&op->list);
  352. }
  353. }
  354. /* Replace a relative jump with a breakpoint (int3). */
  355. void arch_unoptimize_kprobe(struct optimized_kprobe *op)
  356. {
  357. u8 insn_buf[RELATIVEJUMP_SIZE];
  358. /* Set int3 to first byte for kprobes */
  359. insn_buf[0] = BREAKPOINT_INSTRUCTION;
  360. memcpy(insn_buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE);
  361. text_poke_bp(op->kp.addr, insn_buf, RELATIVEJUMP_SIZE,
  362. op->optinsn.insn);
  363. }
  364. /*
  365. * Recover original instructions and breakpoints from relative jumps.
  366. * Caller must call with locking kprobe_mutex.
  367. */
  368. extern void arch_unoptimize_kprobes(struct list_head *oplist,
  369. struct list_head *done_list)
  370. {
  371. struct optimized_kprobe *op, *tmp;
  372. list_for_each_entry_safe(op, tmp, oplist, list) {
  373. arch_unoptimize_kprobe(op);
  374. list_move(&op->list, done_list);
  375. }
  376. }
  377. int setup_detour_execution(struct kprobe *p, struct pt_regs *regs, int reenter)
  378. {
  379. struct optimized_kprobe *op;
  380. if (p->flags & KPROBE_FLAG_OPTIMIZED) {
  381. /* This kprobe is really able to run optimized path. */
  382. op = container_of(p, struct optimized_kprobe, kp);
  383. /* Detour through copied instructions */
  384. regs->ip = (unsigned long)op->optinsn.insn + TMPL_END_IDX;
  385. if (!reenter)
  386. reset_current_kprobe();
  387. preempt_enable_no_resched();
  388. return 1;
  389. }
  390. return 0;
  391. }
  392. NOKPROBE_SYMBOL(setup_detour_execution);