kprobes.c 16 KB

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