opt.c 12 KB

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