kprobes.c 14 KB

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