kprobes.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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/kdebug.h>
  36. #include <asm/desc.h>
  37. /* kprobe_status settings */
  38. #define KPROBE_HIT_ACTIVE 0x00000001
  39. #define KPROBE_HIT_SS 0x00000002
  40. static struct kprobe *current_kprobe;
  41. static unsigned long kprobe_status, kprobe_old_eflags, kprobe_saved_eflags;
  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. }
  69. void arch_remove_kprobe(struct kprobe *p)
  70. {
  71. }
  72. static inline void disarm_kprobe(struct kprobe *p, struct pt_regs *regs)
  73. {
  74. *p->addr = p->opcode;
  75. regs->eip = (unsigned long)p->addr;
  76. }
  77. static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  78. {
  79. regs->eflags |= TF_MASK;
  80. regs->eflags &= ~IF_MASK;
  81. /*single step inline if the instruction is an int3*/
  82. if (p->opcode == BREAKPOINT_INSTRUCTION)
  83. regs->eip = (unsigned long)p->addr;
  84. else
  85. regs->eip = (unsigned long)&p->ainsn.insn;
  86. }
  87. struct task_struct *arch_get_kprobe_task(void *ptr)
  88. {
  89. return ((struct thread_info *) (((unsigned long) ptr) &
  90. (~(THREAD_SIZE -1))))->task;
  91. }
  92. void arch_prepare_kretprobe(struct kretprobe *rp, struct pt_regs *regs)
  93. {
  94. unsigned long *sara = (unsigned long *)&regs->esp;
  95. struct kretprobe_instance *ri;
  96. static void *orig_ret_addr;
  97. /*
  98. * Save the return address when the return probe hits
  99. * the first time, and use it to populate the (krprobe
  100. * instance)->ret_addr for subsequent return probes at
  101. * the same addrress since stack address would have
  102. * the kretprobe_trampoline by then.
  103. */
  104. if (((void*) *sara) != kretprobe_trampoline)
  105. orig_ret_addr = (void*) *sara;
  106. if ((ri = get_free_rp_inst(rp)) != NULL) {
  107. ri->rp = rp;
  108. ri->stack_addr = sara;
  109. ri->ret_addr = orig_ret_addr;
  110. add_rp_inst(ri);
  111. /* Replace the return addr with trampoline addr */
  112. *sara = (unsigned long) &kretprobe_trampoline;
  113. } else {
  114. rp->nmissed++;
  115. }
  116. }
  117. void arch_kprobe_flush_task(struct task_struct *tk, spinlock_t *kp_lock)
  118. {
  119. unsigned long flags = 0;
  120. struct kretprobe_instance *ri;
  121. spin_lock_irqsave(kp_lock, flags);
  122. while ((ri = get_rp_inst_tsk(tk)) != NULL) {
  123. *((unsigned long *)(ri->stack_addr)) =
  124. (unsigned long) ri->ret_addr;
  125. recycle_rp_inst(ri);
  126. }
  127. spin_unlock_irqrestore(kp_lock, flags);
  128. }
  129. /*
  130. * Interrupts are disabled on entry as trap3 is an interrupt gate and they
  131. * remain disabled thorough out this function.
  132. */
  133. static int kprobe_handler(struct pt_regs *regs)
  134. {
  135. struct kprobe *p;
  136. int ret = 0;
  137. kprobe_opcode_t *addr = NULL;
  138. unsigned long *lp;
  139. /* We're in an interrupt, but this is clear and BUG()-safe. */
  140. preempt_disable();
  141. /* Check if the application is using LDT entry for its code segment and
  142. * calculate the address by reading the base address from the LDT entry.
  143. */
  144. if ((regs->xcs & 4) && (current->mm)) {
  145. lp = (unsigned long *) ((unsigned long)((regs->xcs >> 3) * 8)
  146. + (char *) current->mm->context.ldt);
  147. addr = (kprobe_opcode_t *) (get_desc_base(lp) + regs->eip -
  148. sizeof(kprobe_opcode_t));
  149. } else {
  150. addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
  151. }
  152. /* Check we're not actually recursing */
  153. if (kprobe_running()) {
  154. /* We *are* holding lock here, so this is safe.
  155. Disarm the probe we just hit, and ignore it. */
  156. p = get_kprobe(addr);
  157. if (p) {
  158. if (kprobe_status == KPROBE_HIT_SS) {
  159. regs->eflags &= ~TF_MASK;
  160. regs->eflags |= kprobe_saved_eflags;
  161. unlock_kprobes();
  162. goto no_kprobe;
  163. }
  164. disarm_kprobe(p, regs);
  165. ret = 1;
  166. } else {
  167. p = current_kprobe;
  168. if (p->break_handler && p->break_handler(p, regs)) {
  169. goto ss_probe;
  170. }
  171. }
  172. /* If it's not ours, can't be delete race, (we hold lock). */
  173. goto no_kprobe;
  174. }
  175. lock_kprobes();
  176. p = get_kprobe(addr);
  177. if (!p) {
  178. unlock_kprobes();
  179. if (regs->eflags & VM_MASK) {
  180. /* We are in virtual-8086 mode. Return 0 */
  181. goto no_kprobe;
  182. }
  183. if (*addr != BREAKPOINT_INSTRUCTION) {
  184. /*
  185. * The breakpoint instruction was removed right
  186. * after we hit it. Another cpu has removed
  187. * either a probepoint or a debugger breakpoint
  188. * at this address. In either case, no further
  189. * handling of this interrupt is appropriate.
  190. */
  191. ret = 1;
  192. }
  193. /* Not one of ours: let kernel handle it */
  194. goto no_kprobe;
  195. }
  196. kprobe_status = KPROBE_HIT_ACTIVE;
  197. current_kprobe = p;
  198. kprobe_saved_eflags = kprobe_old_eflags
  199. = (regs->eflags & (TF_MASK | IF_MASK));
  200. if (is_IF_modifier(p->opcode))
  201. kprobe_saved_eflags &= ~IF_MASK;
  202. if (p->pre_handler && p->pre_handler(p, regs))
  203. /* handler has already set things up, so skip ss setup */
  204. return 1;
  205. ss_probe:
  206. prepare_singlestep(p, regs);
  207. kprobe_status = KPROBE_HIT_SS;
  208. return 1;
  209. no_kprobe:
  210. preempt_enable_no_resched();
  211. return ret;
  212. }
  213. /*
  214. * For function-return probes, init_kprobes() establishes a probepoint
  215. * here. When a retprobed function returns, this probe is hit and
  216. * trampoline_probe_handler() runs, calling the kretprobe's handler.
  217. */
  218. void kretprobe_trampoline_holder(void)
  219. {
  220. asm volatile ( ".global kretprobe_trampoline\n"
  221. "kretprobe_trampoline: \n"
  222. "nop\n");
  223. }
  224. /*
  225. * Called when we hit the probe point at kretprobe_trampoline
  226. */
  227. int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
  228. {
  229. struct task_struct *tsk;
  230. struct kretprobe_instance *ri;
  231. struct hlist_head *head;
  232. struct hlist_node *node;
  233. unsigned long *sara = ((unsigned long *) &regs->esp) - 1;
  234. tsk = arch_get_kprobe_task(sara);
  235. head = kretprobe_inst_table_head(tsk);
  236. hlist_for_each_entry(ri, node, head, hlist) {
  237. if (ri->stack_addr == sara && ri->rp) {
  238. if (ri->rp->handler)
  239. ri->rp->handler(ri, regs);
  240. }
  241. }
  242. return 0;
  243. }
  244. void trampoline_post_handler(struct kprobe *p, struct pt_regs *regs,
  245. unsigned long flags)
  246. {
  247. struct kretprobe_instance *ri;
  248. /* RA already popped */
  249. unsigned long *sara = ((unsigned long *)&regs->esp) - 1;
  250. while ((ri = get_rp_inst(sara))) {
  251. regs->eip = (unsigned long)ri->ret_addr;
  252. recycle_rp_inst(ri);
  253. }
  254. regs->eflags &= ~TF_MASK;
  255. }
  256. /*
  257. * Called after single-stepping. p->addr is the address of the
  258. * instruction whose first byte has been replaced by the "int 3"
  259. * instruction. To avoid the SMP problems that can occur when we
  260. * temporarily put back the original opcode to single-step, we
  261. * single-stepped a copy of the instruction. The address of this
  262. * copy is p->ainsn.insn.
  263. *
  264. * This function prepares to return from the post-single-step
  265. * interrupt. We have to fix up the stack as follows:
  266. *
  267. * 0) Except in the case of absolute or indirect jump or call instructions,
  268. * the new eip is relative to the copied instruction. We need to make
  269. * it relative to the original instruction.
  270. *
  271. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  272. * flags are set in the just-pushed eflags, and may need to be cleared.
  273. *
  274. * 2) If the single-stepped instruction was a call, the return address
  275. * that is atop the stack is the address following the copied instruction.
  276. * We need to make it the address following the original instruction.
  277. */
  278. static void resume_execution(struct kprobe *p, struct pt_regs *regs)
  279. {
  280. unsigned long *tos = (unsigned long *)&regs->esp;
  281. unsigned long next_eip = 0;
  282. unsigned long copy_eip = (unsigned long)&p->ainsn.insn;
  283. unsigned long orig_eip = (unsigned long)p->addr;
  284. switch (p->ainsn.insn[0]) {
  285. case 0x9c: /* pushfl */
  286. *tos &= ~(TF_MASK | IF_MASK);
  287. *tos |= kprobe_old_eflags;
  288. break;
  289. case 0xc3: /* ret/lret */
  290. case 0xcb:
  291. case 0xc2:
  292. case 0xca:
  293. regs->eflags &= ~TF_MASK;
  294. /* eip is already adjusted, no more changes required*/
  295. return;
  296. case 0xe8: /* call relative - Fix return addr */
  297. *tos = orig_eip + (*tos - copy_eip);
  298. break;
  299. case 0xff:
  300. if ((p->ainsn.insn[1] & 0x30) == 0x10) {
  301. /* call absolute, indirect */
  302. /* Fix return addr; eip is correct. */
  303. next_eip = regs->eip;
  304. *tos = orig_eip + (*tos - copy_eip);
  305. } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
  306. ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
  307. /* eip is correct. */
  308. next_eip = regs->eip;
  309. }
  310. break;
  311. case 0xea: /* jmp absolute -- eip is correct */
  312. next_eip = regs->eip;
  313. break;
  314. default:
  315. break;
  316. }
  317. regs->eflags &= ~TF_MASK;
  318. if (next_eip) {
  319. regs->eip = next_eip;
  320. } else {
  321. regs->eip = orig_eip + (regs->eip - copy_eip);
  322. }
  323. }
  324. /*
  325. * Interrupts are disabled on entry as trap1 is an interrupt gate and they
  326. * remain disabled thoroughout this function. And we hold kprobe lock.
  327. */
  328. static inline int post_kprobe_handler(struct pt_regs *regs)
  329. {
  330. if (!kprobe_running())
  331. return 0;
  332. if (current_kprobe->post_handler)
  333. current_kprobe->post_handler(current_kprobe, regs, 0);
  334. if (current_kprobe->post_handler != trampoline_post_handler)
  335. resume_execution(current_kprobe, regs);
  336. regs->eflags |= kprobe_saved_eflags;
  337. unlock_kprobes();
  338. preempt_enable_no_resched();
  339. /*
  340. * if somebody else is singlestepping across a probe point, eflags
  341. * will have TF set, in which case, continue the remaining processing
  342. * of do_debug, as if this is not a probe hit.
  343. */
  344. if (regs->eflags & TF_MASK)
  345. return 0;
  346. return 1;
  347. }
  348. /* Interrupts disabled, kprobe_lock held. */
  349. static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  350. {
  351. if (current_kprobe->fault_handler
  352. && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
  353. return 1;
  354. if (kprobe_status & KPROBE_HIT_SS) {
  355. resume_execution(current_kprobe, regs);
  356. regs->eflags |= kprobe_old_eflags;
  357. unlock_kprobes();
  358. preempt_enable_no_resched();
  359. }
  360. return 0;
  361. }
  362. /*
  363. * Wrapper routine to for handling exceptions.
  364. */
  365. int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
  366. void *data)
  367. {
  368. struct die_args *args = (struct die_args *)data;
  369. switch (val) {
  370. case DIE_INT3:
  371. if (kprobe_handler(args->regs))
  372. return NOTIFY_STOP;
  373. break;
  374. case DIE_DEBUG:
  375. if (post_kprobe_handler(args->regs))
  376. return NOTIFY_STOP;
  377. break;
  378. case DIE_GPF:
  379. if (kprobe_running() &&
  380. kprobe_fault_handler(args->regs, args->trapnr))
  381. return NOTIFY_STOP;
  382. break;
  383. case DIE_PAGE_FAULT:
  384. if (kprobe_running() &&
  385. kprobe_fault_handler(args->regs, args->trapnr))
  386. return NOTIFY_STOP;
  387. break;
  388. default:
  389. break;
  390. }
  391. return NOTIFY_DONE;
  392. }
  393. int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  394. {
  395. struct jprobe *jp = container_of(p, struct jprobe, kp);
  396. unsigned long addr;
  397. jprobe_saved_regs = *regs;
  398. jprobe_saved_esp = &regs->esp;
  399. addr = (unsigned long)jprobe_saved_esp;
  400. /*
  401. * TBD: As Linus pointed out, gcc assumes that the callee
  402. * owns the argument space and could overwrite it, e.g.
  403. * tailcall optimization. So, to be absolutely safe
  404. * we also save and restore enough stack bytes to cover
  405. * the argument area.
  406. */
  407. memcpy(jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE(addr));
  408. regs->eflags &= ~IF_MASK;
  409. regs->eip = (unsigned long)(jp->entry);
  410. return 1;
  411. }
  412. void jprobe_return(void)
  413. {
  414. preempt_enable_no_resched();
  415. asm volatile (" xchgl %%ebx,%%esp \n"
  416. " int3 \n"
  417. " .globl jprobe_return_end \n"
  418. " jprobe_return_end: \n"
  419. " nop \n"::"b"
  420. (jprobe_saved_esp):"memory");
  421. }
  422. int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  423. {
  424. u8 *addr = (u8 *) (regs->eip - 1);
  425. unsigned long stack_addr = (unsigned long)jprobe_saved_esp;
  426. struct jprobe *jp = container_of(p, struct jprobe, kp);
  427. if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
  428. if (&regs->esp != jprobe_saved_esp) {
  429. struct pt_regs *saved_regs =
  430. container_of(jprobe_saved_esp, struct pt_regs, esp);
  431. printk("current esp %p does not match saved esp %p\n",
  432. &regs->esp, jprobe_saved_esp);
  433. printk("Saved registers for jprobe %p\n", jp);
  434. show_registers(saved_regs);
  435. printk("Current registers\n");
  436. show_registers(regs);
  437. BUG();
  438. }
  439. *regs = jprobe_saved_regs;
  440. memcpy((kprobe_opcode_t *) stack_addr, jprobes_stack,
  441. MIN_STACK_SIZE(stack_addr));
  442. return 1;
  443. }
  444. return 0;
  445. }