kprobes.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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/spinlock.h>
  34. #include <linux/preempt.h>
  35. #include <asm/cacheflush.h>
  36. #include <asm/kdebug.h>
  37. #include <asm/desc.h>
  38. static struct kprobe *current_kprobe;
  39. static unsigned long kprobe_status, kprobe_old_eflags, kprobe_saved_eflags;
  40. static struct kprobe *kprobe_prev;
  41. static unsigned long kprobe_status_prev, kprobe_old_eflags_prev, kprobe_saved_eflags_prev;
  42. static struct pt_regs jprobe_saved_regs;
  43. static long *jprobe_saved_esp;
  44. /* copy of the kernel stack at the probe fire time */
  45. static kprobe_opcode_t jprobes_stack[MAX_STACK_SIZE];
  46. void jprobe_return_end(void);
  47. /*
  48. * returns non-zero if opcode modifies the interrupt flag.
  49. */
  50. static inline int is_IF_modifier(kprobe_opcode_t opcode)
  51. {
  52. switch (opcode) {
  53. case 0xfa: /* cli */
  54. case 0xfb: /* sti */
  55. case 0xcf: /* iret/iretd */
  56. case 0x9d: /* popf/popfd */
  57. return 1;
  58. }
  59. return 0;
  60. }
  61. int arch_prepare_kprobe(struct kprobe *p)
  62. {
  63. return 0;
  64. }
  65. void arch_copy_kprobe(struct kprobe *p)
  66. {
  67. memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
  68. p->opcode = *p->addr;
  69. }
  70. void arch_arm_kprobe(struct kprobe *p)
  71. {
  72. *p->addr = BREAKPOINT_INSTRUCTION;
  73. flush_icache_range((unsigned long) p->addr,
  74. (unsigned long) p->addr + sizeof(kprobe_opcode_t));
  75. }
  76. void arch_disarm_kprobe(struct kprobe *p)
  77. {
  78. *p->addr = p->opcode;
  79. flush_icache_range((unsigned long) p->addr,
  80. (unsigned long) p->addr + sizeof(kprobe_opcode_t));
  81. }
  82. void arch_remove_kprobe(struct kprobe *p)
  83. {
  84. }
  85. static inline void save_previous_kprobe(void)
  86. {
  87. kprobe_prev = current_kprobe;
  88. kprobe_status_prev = kprobe_status;
  89. kprobe_old_eflags_prev = kprobe_old_eflags;
  90. kprobe_saved_eflags_prev = kprobe_saved_eflags;
  91. }
  92. static inline void restore_previous_kprobe(void)
  93. {
  94. current_kprobe = kprobe_prev;
  95. kprobe_status = kprobe_status_prev;
  96. kprobe_old_eflags = kprobe_old_eflags_prev;
  97. kprobe_saved_eflags = kprobe_saved_eflags_prev;
  98. }
  99. static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs)
  100. {
  101. current_kprobe = p;
  102. kprobe_saved_eflags = kprobe_old_eflags
  103. = (regs->eflags & (TF_MASK | IF_MASK));
  104. if (is_IF_modifier(p->opcode))
  105. kprobe_saved_eflags &= ~IF_MASK;
  106. }
  107. static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  108. {
  109. regs->eflags |= TF_MASK;
  110. regs->eflags &= ~IF_MASK;
  111. /*single step inline if the instruction is an int3*/
  112. if (p->opcode == BREAKPOINT_INSTRUCTION)
  113. regs->eip = (unsigned long)p->addr;
  114. else
  115. regs->eip = (unsigned long)&p->ainsn.insn;
  116. }
  117. void arch_prepare_kretprobe(struct kretprobe *rp, 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 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. /* We're in an interrupt, but this is clear and BUG()-safe. */
  143. preempt_disable();
  144. /* Check if the application is using LDT entry for its code segment and
  145. * calculate the address by reading the base address from the LDT entry.
  146. */
  147. if ((regs->xcs & 4) && (current->mm)) {
  148. lp = (unsigned long *) ((unsigned long)((regs->xcs >> 3) * 8)
  149. + (char *) current->mm->context.ldt);
  150. addr = (kprobe_opcode_t *) (get_desc_base(lp) + regs->eip -
  151. sizeof(kprobe_opcode_t));
  152. } else {
  153. addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
  154. }
  155. /* Check we're not actually recursing */
  156. if (kprobe_running()) {
  157. /* We *are* holding lock here, so this is safe.
  158. Disarm the probe we just hit, and ignore it. */
  159. p = get_kprobe(addr);
  160. if (p) {
  161. if (kprobe_status == KPROBE_HIT_SS) {
  162. regs->eflags &= ~TF_MASK;
  163. regs->eflags |= kprobe_saved_eflags;
  164. unlock_kprobes();
  165. goto no_kprobe;
  166. }
  167. /* We have reentered the kprobe_handler(), since
  168. * another probe was hit while within the handler.
  169. * We here save the original kprobes variables and
  170. * just single step on the instruction of the new probe
  171. * without calling any user handlers.
  172. */
  173. save_previous_kprobe();
  174. set_current_kprobe(p, regs);
  175. p->nmissed++;
  176. prepare_singlestep(p, regs);
  177. kprobe_status = KPROBE_REENTER;
  178. return 1;
  179. } else {
  180. p = current_kprobe;
  181. if (p->break_handler && p->break_handler(p, regs)) {
  182. goto ss_probe;
  183. }
  184. }
  185. /* If it's not ours, can't be delete race, (we hold lock). */
  186. goto no_kprobe;
  187. }
  188. lock_kprobes();
  189. p = get_kprobe(addr);
  190. if (!p) {
  191. unlock_kprobes();
  192. if (regs->eflags & VM_MASK) {
  193. /* We are in virtual-8086 mode. Return 0 */
  194. goto no_kprobe;
  195. }
  196. if (*addr != BREAKPOINT_INSTRUCTION) {
  197. /*
  198. * The breakpoint instruction was removed right
  199. * after we hit it. Another cpu has removed
  200. * either a probepoint or a debugger breakpoint
  201. * at this address. In either case, no further
  202. * handling of this interrupt is appropriate.
  203. */
  204. ret = 1;
  205. }
  206. /* Not one of ours: let kernel handle it */
  207. goto no_kprobe;
  208. }
  209. kprobe_status = KPROBE_HIT_ACTIVE;
  210. set_current_kprobe(p, regs);
  211. if (p->pre_handler && p->pre_handler(p, regs))
  212. /* handler has already set things up, so skip ss setup */
  213. return 1;
  214. ss_probe:
  215. prepare_singlestep(p, regs);
  216. kprobe_status = KPROBE_HIT_SS;
  217. return 1;
  218. no_kprobe:
  219. preempt_enable_no_resched();
  220. return ret;
  221. }
  222. /*
  223. * For function-return probes, init_kprobes() establishes a probepoint
  224. * here. When a retprobed function returns, this probe is hit and
  225. * trampoline_probe_handler() runs, calling the kretprobe's handler.
  226. */
  227. void kretprobe_trampoline_holder(void)
  228. {
  229. asm volatile ( ".global kretprobe_trampoline\n"
  230. "kretprobe_trampoline: \n"
  231. "nop\n");
  232. }
  233. /*
  234. * Called when we hit the probe point at kretprobe_trampoline
  235. */
  236. int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
  237. {
  238. struct kretprobe_instance *ri = NULL;
  239. struct hlist_head *head;
  240. struct hlist_node *node, *tmp;
  241. unsigned long orig_ret_address = 0;
  242. unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
  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. unlock_kprobes();
  276. preempt_enable_no_resched();
  277. /*
  278. * By returning a non-zero value, we are telling
  279. * kprobe_handler() that we have handled unlocking
  280. * and re-enabling preemption.
  281. */
  282. return 1;
  283. }
  284. /*
  285. * Called after single-stepping. p->addr is the address of the
  286. * instruction whose first byte has been replaced by the "int 3"
  287. * instruction. To avoid the SMP problems that can occur when we
  288. * temporarily put back the original opcode to single-step, we
  289. * single-stepped a copy of the instruction. The address of this
  290. * copy is p->ainsn.insn.
  291. *
  292. * This function prepares to return from the post-single-step
  293. * interrupt. We have to fix up the stack as follows:
  294. *
  295. * 0) Except in the case of absolute or indirect jump or call instructions,
  296. * the new eip is relative to the copied instruction. We need to make
  297. * it relative to the original instruction.
  298. *
  299. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  300. * flags are set in the just-pushed eflags, and may need to be cleared.
  301. *
  302. * 2) If the single-stepped instruction was a call, the return address
  303. * that is atop the stack is the address following the copied instruction.
  304. * We need to make it the address following the original instruction.
  305. */
  306. static void resume_execution(struct kprobe *p, struct pt_regs *regs)
  307. {
  308. unsigned long *tos = (unsigned long *)&regs->esp;
  309. unsigned long next_eip = 0;
  310. unsigned long copy_eip = (unsigned long)&p->ainsn.insn;
  311. unsigned long orig_eip = (unsigned long)p->addr;
  312. switch (p->ainsn.insn[0]) {
  313. case 0x9c: /* pushfl */
  314. *tos &= ~(TF_MASK | IF_MASK);
  315. *tos |= kprobe_old_eflags;
  316. break;
  317. case 0xc3: /* ret/lret */
  318. case 0xcb:
  319. case 0xc2:
  320. case 0xca:
  321. regs->eflags &= ~TF_MASK;
  322. /* eip is already adjusted, no more changes required*/
  323. return;
  324. case 0xe8: /* call relative - Fix return addr */
  325. *tos = orig_eip + (*tos - copy_eip);
  326. break;
  327. case 0xff:
  328. if ((p->ainsn.insn[1] & 0x30) == 0x10) {
  329. /* call absolute, indirect */
  330. /* Fix return addr; eip is correct. */
  331. next_eip = regs->eip;
  332. *tos = orig_eip + (*tos - copy_eip);
  333. } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
  334. ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
  335. /* eip is correct. */
  336. next_eip = regs->eip;
  337. }
  338. break;
  339. case 0xea: /* jmp absolute -- eip is correct */
  340. next_eip = regs->eip;
  341. break;
  342. default:
  343. break;
  344. }
  345. regs->eflags &= ~TF_MASK;
  346. if (next_eip) {
  347. regs->eip = next_eip;
  348. } else {
  349. regs->eip = orig_eip + (regs->eip - copy_eip);
  350. }
  351. }
  352. /*
  353. * Interrupts are disabled on entry as trap1 is an interrupt gate and they
  354. * remain disabled thoroughout this function. And we hold kprobe lock.
  355. */
  356. static inline int post_kprobe_handler(struct pt_regs *regs)
  357. {
  358. if (!kprobe_running())
  359. return 0;
  360. if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) {
  361. kprobe_status = KPROBE_HIT_SSDONE;
  362. current_kprobe->post_handler(current_kprobe, regs, 0);
  363. }
  364. resume_execution(current_kprobe, regs);
  365. regs->eflags |= kprobe_saved_eflags;
  366. /*Restore back the original saved kprobes variables and continue. */
  367. if (kprobe_status == KPROBE_REENTER) {
  368. restore_previous_kprobe();
  369. goto out;
  370. }
  371. unlock_kprobes();
  372. out:
  373. preempt_enable_no_resched();
  374. /*
  375. * if somebody else is singlestepping across a probe point, eflags
  376. * will have TF set, in which case, continue the remaining processing
  377. * of do_debug, as if this is not a probe hit.
  378. */
  379. if (regs->eflags & TF_MASK)
  380. return 0;
  381. return 1;
  382. }
  383. /* Interrupts disabled, kprobe_lock held. */
  384. static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  385. {
  386. if (current_kprobe->fault_handler
  387. && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
  388. return 1;
  389. if (kprobe_status & KPROBE_HIT_SS) {
  390. resume_execution(current_kprobe, regs);
  391. regs->eflags |= kprobe_old_eflags;
  392. unlock_kprobes();
  393. preempt_enable_no_resched();
  394. }
  395. return 0;
  396. }
  397. /*
  398. * Wrapper routine to for handling exceptions.
  399. */
  400. int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
  401. void *data)
  402. {
  403. struct die_args *args = (struct die_args *)data;
  404. switch (val) {
  405. case DIE_INT3:
  406. if (kprobe_handler(args->regs))
  407. return NOTIFY_STOP;
  408. break;
  409. case DIE_DEBUG:
  410. if (post_kprobe_handler(args->regs))
  411. return NOTIFY_STOP;
  412. break;
  413. case DIE_GPF:
  414. if (kprobe_running() &&
  415. kprobe_fault_handler(args->regs, args->trapnr))
  416. return NOTIFY_STOP;
  417. break;
  418. case DIE_PAGE_FAULT:
  419. if (kprobe_running() &&
  420. kprobe_fault_handler(args->regs, args->trapnr))
  421. return NOTIFY_STOP;
  422. break;
  423. default:
  424. break;
  425. }
  426. return NOTIFY_DONE;
  427. }
  428. int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  429. {
  430. struct jprobe *jp = container_of(p, struct jprobe, kp);
  431. unsigned long addr;
  432. jprobe_saved_regs = *regs;
  433. jprobe_saved_esp = &regs->esp;
  434. addr = (unsigned long)jprobe_saved_esp;
  435. /*
  436. * TBD: As Linus pointed out, gcc assumes that the callee
  437. * owns the argument space and could overwrite it, e.g.
  438. * tailcall optimization. So, to be absolutely safe
  439. * we also save and restore enough stack bytes to cover
  440. * the argument area.
  441. */
  442. memcpy(jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE(addr));
  443. regs->eflags &= ~IF_MASK;
  444. regs->eip = (unsigned long)(jp->entry);
  445. return 1;
  446. }
  447. void jprobe_return(void)
  448. {
  449. preempt_enable_no_resched();
  450. asm volatile (" xchgl %%ebx,%%esp \n"
  451. " int3 \n"
  452. " .globl jprobe_return_end \n"
  453. " jprobe_return_end: \n"
  454. " nop \n"::"b"
  455. (jprobe_saved_esp):"memory");
  456. }
  457. int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  458. {
  459. u8 *addr = (u8 *) (regs->eip - 1);
  460. unsigned long stack_addr = (unsigned long)jprobe_saved_esp;
  461. struct jprobe *jp = container_of(p, struct jprobe, kp);
  462. if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
  463. if (&regs->esp != jprobe_saved_esp) {
  464. struct pt_regs *saved_regs =
  465. container_of(jprobe_saved_esp, struct pt_regs, esp);
  466. printk("current esp %p does not match saved esp %p\n",
  467. &regs->esp, jprobe_saved_esp);
  468. printk("Saved registers for jprobe %p\n", jp);
  469. show_registers(saved_regs);
  470. printk("Current registers\n");
  471. show_registers(regs);
  472. BUG();
  473. }
  474. *regs = jprobe_saved_regs;
  475. memcpy((kprobe_opcode_t *) stack_addr, jprobes_stack,
  476. MIN_STACK_SIZE(stack_addr));
  477. return 1;
  478. }
  479. return 0;
  480. }
  481. static struct kprobe trampoline_p = {
  482. .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
  483. .pre_handler = trampoline_probe_handler
  484. };
  485. int __init arch_init(void)
  486. {
  487. return register_kprobe(&trampoline_p);
  488. }