kprobes.c 16 KB

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