kprobes.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*
  2. * Kernel Probes (KProbes)
  3. * arch/i386/kernel/kprobes.c
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. *
  19. * Copyright (C) IBM Corporation, 2002, 2004
  20. *
  21. * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
  22. * Probes initial implementation ( includes contributions from
  23. * Rusty Russell).
  24. * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
  25. * interface to access function arguments.
  26. * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
  27. * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
  28. * <prasanna@in.ibm.com> added function-return probes.
  29. */
  30. #include <linux/config.h>
  31. #include <linux/kprobes.h>
  32. #include <linux/ptrace.h>
  33. #include <linux/preempt.h>
  34. #include <asm/cacheflush.h>
  35. #include <asm/kdebug.h>
  36. #include <asm/desc.h>
  37. void jprobe_return_end(void);
  38. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  39. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  40. /*
  41. * returns non-zero if opcode modifies the interrupt flag.
  42. */
  43. static inline int is_IF_modifier(kprobe_opcode_t opcode)
  44. {
  45. switch (opcode) {
  46. case 0xfa: /* cli */
  47. case 0xfb: /* sti */
  48. case 0xcf: /* iret/iretd */
  49. case 0x9d: /* popf/popfd */
  50. return 1;
  51. }
  52. return 0;
  53. }
  54. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  55. {
  56. /* insn: must be on special executable page on i386. */
  57. p->ainsn.insn = get_insn_slot();
  58. if (!p->ainsn.insn)
  59. return -ENOMEM;
  60. memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
  61. p->opcode = *p->addr;
  62. return 0;
  63. }
  64. void __kprobes arch_arm_kprobe(struct kprobe *p)
  65. {
  66. *p->addr = BREAKPOINT_INSTRUCTION;
  67. flush_icache_range((unsigned long) p->addr,
  68. (unsigned long) p->addr + sizeof(kprobe_opcode_t));
  69. }
  70. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  71. {
  72. *p->addr = p->opcode;
  73. flush_icache_range((unsigned long) p->addr,
  74. (unsigned long) p->addr + sizeof(kprobe_opcode_t));
  75. }
  76. void __kprobes arch_remove_kprobe(struct kprobe *p)
  77. {
  78. mutex_lock(&kprobe_mutex);
  79. free_insn_slot(p->ainsn.insn);
  80. mutex_unlock(&kprobe_mutex);
  81. }
  82. static inline void save_previous_kprobe(struct kprobe_ctlblk *kcb)
  83. {
  84. kcb->prev_kprobe.kp = kprobe_running();
  85. kcb->prev_kprobe.status = kcb->kprobe_status;
  86. kcb->prev_kprobe.old_eflags = kcb->kprobe_old_eflags;
  87. kcb->prev_kprobe.saved_eflags = kcb->kprobe_saved_eflags;
  88. }
  89. static inline void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  90. {
  91. __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
  92. kcb->kprobe_status = kcb->prev_kprobe.status;
  93. kcb->kprobe_old_eflags = kcb->prev_kprobe.old_eflags;
  94. kcb->kprobe_saved_eflags = kcb->prev_kprobe.saved_eflags;
  95. }
  96. static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  97. struct kprobe_ctlblk *kcb)
  98. {
  99. __get_cpu_var(current_kprobe) = p;
  100. kcb->kprobe_saved_eflags = kcb->kprobe_old_eflags
  101. = (regs->eflags & (TF_MASK | IF_MASK));
  102. if (is_IF_modifier(p->opcode))
  103. kcb->kprobe_saved_eflags &= ~IF_MASK;
  104. }
  105. static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  106. {
  107. regs->eflags |= TF_MASK;
  108. regs->eflags &= ~IF_MASK;
  109. /*single step inline if the instruction is an int3*/
  110. if (p->opcode == BREAKPOINT_INSTRUCTION)
  111. regs->eip = (unsigned long)p->addr;
  112. else
  113. regs->eip = (unsigned long)p->ainsn.insn;
  114. }
  115. /* Called with kretprobe_lock held */
  116. void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
  117. struct pt_regs *regs)
  118. {
  119. unsigned long *sara = (unsigned long *)&regs->esp;
  120. struct kretprobe_instance *ri;
  121. if ((ri = get_free_rp_inst(rp)) != NULL) {
  122. ri->rp = rp;
  123. ri->task = current;
  124. ri->ret_addr = (kprobe_opcode_t *) *sara;
  125. /* Replace the return addr with trampoline addr */
  126. *sara = (unsigned long) &kretprobe_trampoline;
  127. add_rp_inst(ri);
  128. } else {
  129. rp->nmissed++;
  130. }
  131. }
  132. /*
  133. * Interrupts are disabled on entry as trap3 is an interrupt gate and they
  134. * remain disabled thorough out this function.
  135. */
  136. static int __kprobes kprobe_handler(struct pt_regs *regs)
  137. {
  138. struct kprobe *p;
  139. int ret = 0;
  140. kprobe_opcode_t *addr = NULL;
  141. unsigned long *lp;
  142. struct kprobe_ctlblk *kcb;
  143. /*
  144. * We don't want to be preempted for the entire
  145. * duration of kprobe processing
  146. */
  147. preempt_disable();
  148. kcb = get_kprobe_ctlblk();
  149. /* Check if the application is using LDT entry for its code segment and
  150. * calculate the address by reading the base address from the LDT entry.
  151. */
  152. if ((regs->xcs & 4) && (current->mm)) {
  153. lp = (unsigned long *) ((unsigned long)((regs->xcs >> 3) * 8)
  154. + (char *) current->mm->context.ldt);
  155. addr = (kprobe_opcode_t *) (get_desc_base(lp) + regs->eip -
  156. sizeof(kprobe_opcode_t));
  157. } else {
  158. addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
  159. }
  160. /* Check we're not actually recursing */
  161. if (kprobe_running()) {
  162. p = get_kprobe(addr);
  163. if (p) {
  164. if (kcb->kprobe_status == KPROBE_HIT_SS &&
  165. *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
  166. regs->eflags &= ~TF_MASK;
  167. regs->eflags |= kcb->kprobe_saved_eflags;
  168. goto no_kprobe;
  169. }
  170. /* We have reentered the kprobe_handler(), since
  171. * another probe was hit while within the handler.
  172. * We here save the original kprobes variables and
  173. * just single step on the instruction of the new probe
  174. * without calling any user handlers.
  175. */
  176. save_previous_kprobe(kcb);
  177. set_current_kprobe(p, regs, kcb);
  178. kprobes_inc_nmissed_count(p);
  179. prepare_singlestep(p, regs);
  180. kcb->kprobe_status = KPROBE_REENTER;
  181. return 1;
  182. } else {
  183. if (regs->eflags & VM_MASK) {
  184. /* We are in virtual-8086 mode. Return 0 */
  185. goto no_kprobe;
  186. }
  187. if (*addr != BREAKPOINT_INSTRUCTION) {
  188. /* The breakpoint instruction was removed by
  189. * another cpu right after we hit, no further
  190. * handling of this interrupt is appropriate
  191. */
  192. regs->eip -= sizeof(kprobe_opcode_t);
  193. ret = 1;
  194. goto no_kprobe;
  195. }
  196. p = __get_cpu_var(current_kprobe);
  197. if (p->break_handler && p->break_handler(p, regs)) {
  198. goto ss_probe;
  199. }
  200. }
  201. goto no_kprobe;
  202. }
  203. p = get_kprobe(addr);
  204. if (!p) {
  205. if (regs->eflags & VM_MASK) {
  206. /* We are in virtual-8086 mode. Return 0 */
  207. goto no_kprobe;
  208. }
  209. if (*addr != BREAKPOINT_INSTRUCTION) {
  210. /*
  211. * The breakpoint instruction was removed right
  212. * after we hit it. Another cpu has removed
  213. * either a probepoint or a debugger breakpoint
  214. * at this address. In either case, no further
  215. * handling of this interrupt is appropriate.
  216. * Back up over the (now missing) int3 and run
  217. * the original instruction.
  218. */
  219. regs->eip -= sizeof(kprobe_opcode_t);
  220. ret = 1;
  221. }
  222. /* Not one of ours: let kernel handle it */
  223. goto no_kprobe;
  224. }
  225. set_current_kprobe(p, regs, kcb);
  226. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  227. if (p->pre_handler && p->pre_handler(p, regs))
  228. /* handler has already set things up, so skip ss setup */
  229. return 1;
  230. ss_probe:
  231. prepare_singlestep(p, regs);
  232. kcb->kprobe_status = KPROBE_HIT_SS;
  233. return 1;
  234. no_kprobe:
  235. preempt_enable_no_resched();
  236. return ret;
  237. }
  238. /*
  239. * For function-return probes, init_kprobes() establishes a probepoint
  240. * here. When a retprobed function returns, this probe is hit and
  241. * trampoline_probe_handler() runs, calling the kretprobe's handler.
  242. */
  243. void kretprobe_trampoline_holder(void)
  244. {
  245. asm volatile ( ".global kretprobe_trampoline\n"
  246. "kretprobe_trampoline: \n"
  247. "nop\n");
  248. }
  249. /*
  250. * Called when we hit the probe point at kretprobe_trampoline
  251. */
  252. int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
  253. {
  254. struct kretprobe_instance *ri = NULL;
  255. struct hlist_head *head;
  256. struct hlist_node *node, *tmp;
  257. unsigned long flags, orig_ret_address = 0;
  258. unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
  259. spin_lock_irqsave(&kretprobe_lock, flags);
  260. head = kretprobe_inst_table_head(current);
  261. /*
  262. * It is possible to have multiple instances associated with a given
  263. * task either because an multiple functions in the call path
  264. * have a return probe installed on them, and/or more then one return
  265. * return probe was registered for a target function.
  266. *
  267. * We can handle this because:
  268. * - instances are always inserted at the head of the list
  269. * - when multiple return probes are registered for the same
  270. * function, the first instance's ret_addr will point to the
  271. * real return address, and all the rest will point to
  272. * kretprobe_trampoline
  273. */
  274. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  275. if (ri->task != current)
  276. /* another task is sharing our hash bucket */
  277. continue;
  278. if (ri->rp && ri->rp->handler)
  279. ri->rp->handler(ri, regs);
  280. orig_ret_address = (unsigned long)ri->ret_addr;
  281. recycle_rp_inst(ri);
  282. if (orig_ret_address != trampoline_address)
  283. /*
  284. * This is the real return address. Any other
  285. * instances associated with this task are for
  286. * other calls deeper on the call stack
  287. */
  288. break;
  289. }
  290. BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
  291. regs->eip = orig_ret_address;
  292. reset_current_kprobe();
  293. spin_unlock_irqrestore(&kretprobe_lock, flags);
  294. preempt_enable_no_resched();
  295. /*
  296. * By returning a non-zero value, we are telling
  297. * kprobe_handler() that we don't want the post_handler
  298. * to run (and have re-enabled preemption)
  299. */
  300. return 1;
  301. }
  302. /*
  303. * Called after single-stepping. p->addr is the address of the
  304. * instruction whose first byte has been replaced by the "int 3"
  305. * instruction. To avoid the SMP problems that can occur when we
  306. * temporarily put back the original opcode to single-step, we
  307. * single-stepped a copy of the instruction. The address of this
  308. * copy is p->ainsn.insn.
  309. *
  310. * This function prepares to return from the post-single-step
  311. * interrupt. We have to fix up the stack as follows:
  312. *
  313. * 0) Except in the case of absolute or indirect jump or call instructions,
  314. * the new eip is relative to the copied instruction. We need to make
  315. * it relative to the original instruction.
  316. *
  317. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  318. * flags are set in the just-pushed eflags, and may need to be cleared.
  319. *
  320. * 2) If the single-stepped instruction was a call, the return address
  321. * that is atop the stack is the address following the copied instruction.
  322. * We need to make it the address following the original instruction.
  323. */
  324. static void __kprobes resume_execution(struct kprobe *p,
  325. struct pt_regs *regs, struct kprobe_ctlblk *kcb)
  326. {
  327. unsigned long *tos = (unsigned long *)&regs->esp;
  328. unsigned long next_eip = 0;
  329. unsigned long copy_eip = (unsigned long)p->ainsn.insn;
  330. unsigned long orig_eip = (unsigned long)p->addr;
  331. switch (p->ainsn.insn[0]) {
  332. case 0x9c: /* pushfl */
  333. *tos &= ~(TF_MASK | IF_MASK);
  334. *tos |= kcb->kprobe_old_eflags;
  335. break;
  336. case 0xc3: /* ret/lret */
  337. case 0xcb:
  338. case 0xc2:
  339. case 0xca:
  340. regs->eflags &= ~TF_MASK;
  341. /* eip is already adjusted, no more changes required*/
  342. return;
  343. case 0xe8: /* call relative - Fix return addr */
  344. *tos = orig_eip + (*tos - copy_eip);
  345. break;
  346. case 0xff:
  347. if ((p->ainsn.insn[1] & 0x30) == 0x10) {
  348. /* call absolute, indirect */
  349. /* Fix return addr; eip is correct. */
  350. next_eip = regs->eip;
  351. *tos = orig_eip + (*tos - copy_eip);
  352. } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
  353. ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
  354. /* eip is correct. */
  355. next_eip = regs->eip;
  356. }
  357. break;
  358. case 0xea: /* jmp absolute -- eip is correct */
  359. next_eip = regs->eip;
  360. break;
  361. default:
  362. break;
  363. }
  364. regs->eflags &= ~TF_MASK;
  365. if (next_eip) {
  366. regs->eip = next_eip;
  367. } else {
  368. regs->eip = orig_eip + (regs->eip - copy_eip);
  369. }
  370. }
  371. /*
  372. * Interrupts are disabled on entry as trap1 is an interrupt gate and they
  373. * remain disabled thoroughout this function.
  374. */
  375. static inline int post_kprobe_handler(struct pt_regs *regs)
  376. {
  377. struct kprobe *cur = kprobe_running();
  378. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  379. if (!cur)
  380. return 0;
  381. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  382. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  383. cur->post_handler(cur, regs, 0);
  384. }
  385. resume_execution(cur, regs, kcb);
  386. regs->eflags |= kcb->kprobe_saved_eflags;
  387. /*Restore back the original saved kprobes variables and continue. */
  388. if (kcb->kprobe_status == KPROBE_REENTER) {
  389. restore_previous_kprobe(kcb);
  390. goto out;
  391. }
  392. reset_current_kprobe();
  393. out:
  394. preempt_enable_no_resched();
  395. /*
  396. * if somebody else is singlestepping across a probe point, eflags
  397. * will have TF set, in which case, continue the remaining processing
  398. * of do_debug, as if this is not a probe hit.
  399. */
  400. if (regs->eflags & TF_MASK)
  401. return 0;
  402. return 1;
  403. }
  404. static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  405. {
  406. struct kprobe *cur = kprobe_running();
  407. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  408. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  409. return 1;
  410. if (kcb->kprobe_status & KPROBE_HIT_SS) {
  411. resume_execution(cur, regs, kcb);
  412. regs->eflags |= kcb->kprobe_old_eflags;
  413. reset_current_kprobe();
  414. preempt_enable_no_resched();
  415. }
  416. return 0;
  417. }
  418. /*
  419. * Wrapper routine to for handling exceptions.
  420. */
  421. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  422. unsigned long val, void *data)
  423. {
  424. struct die_args *args = (struct die_args *)data;
  425. int ret = NOTIFY_DONE;
  426. switch (val) {
  427. case DIE_INT3:
  428. if (kprobe_handler(args->regs))
  429. ret = NOTIFY_STOP;
  430. break;
  431. case DIE_DEBUG:
  432. if (post_kprobe_handler(args->regs))
  433. ret = NOTIFY_STOP;
  434. break;
  435. case DIE_GPF:
  436. case DIE_PAGE_FAULT:
  437. /* kprobe_running() needs smp_processor_id() */
  438. preempt_disable();
  439. if (kprobe_running() &&
  440. kprobe_fault_handler(args->regs, args->trapnr))
  441. ret = NOTIFY_STOP;
  442. preempt_enable();
  443. break;
  444. default:
  445. break;
  446. }
  447. return ret;
  448. }
  449. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  450. {
  451. struct jprobe *jp = container_of(p, struct jprobe, kp);
  452. unsigned long addr;
  453. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  454. kcb->jprobe_saved_regs = *regs;
  455. kcb->jprobe_saved_esp = &regs->esp;
  456. addr = (unsigned long)(kcb->jprobe_saved_esp);
  457. /*
  458. * TBD: As Linus pointed out, gcc assumes that the callee
  459. * owns the argument space and could overwrite it, e.g.
  460. * tailcall optimization. So, to be absolutely safe
  461. * we also save and restore enough stack bytes to cover
  462. * the argument area.
  463. */
  464. memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
  465. MIN_STACK_SIZE(addr));
  466. regs->eflags &= ~IF_MASK;
  467. regs->eip = (unsigned long)(jp->entry);
  468. return 1;
  469. }
  470. void __kprobes jprobe_return(void)
  471. {
  472. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  473. asm volatile (" xchgl %%ebx,%%esp \n"
  474. " int3 \n"
  475. " .globl jprobe_return_end \n"
  476. " jprobe_return_end: \n"
  477. " nop \n"::"b"
  478. (kcb->jprobe_saved_esp):"memory");
  479. }
  480. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  481. {
  482. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  483. u8 *addr = (u8 *) (regs->eip - 1);
  484. unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_esp);
  485. struct jprobe *jp = container_of(p, struct jprobe, kp);
  486. if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
  487. if (&regs->esp != kcb->jprobe_saved_esp) {
  488. struct pt_regs *saved_regs =
  489. container_of(kcb->jprobe_saved_esp,
  490. struct pt_regs, esp);
  491. printk("current esp %p does not match saved esp %p\n",
  492. &regs->esp, kcb->jprobe_saved_esp);
  493. printk("Saved registers for jprobe %p\n", jp);
  494. show_registers(saved_regs);
  495. printk("Current registers\n");
  496. show_registers(regs);
  497. BUG();
  498. }
  499. *regs = kcb->jprobe_saved_regs;
  500. memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
  501. MIN_STACK_SIZE(stack_addr));
  502. preempt_enable_no_resched();
  503. return 1;
  504. }
  505. return 0;
  506. }
  507. static struct kprobe trampoline_p = {
  508. .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
  509. .pre_handler = trampoline_probe_handler
  510. };
  511. int __init arch_init_kprobes(void)
  512. {
  513. return register_kprobe(&trampoline_p);
  514. }