kprobes.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. /* Check if the application is using LDT entry for its code segment and
  144. * calculate the address by reading the base address from the LDT entry.
  145. */
  146. if ((regs->xcs & 4) && (current->mm)) {
  147. lp = (unsigned long *) ((unsigned long)((regs->xcs >> 3) * 8)
  148. + (char *) current->mm->context.ldt);
  149. addr = (kprobe_opcode_t *) (get_desc_base(lp) + regs->eip -
  150. sizeof(kprobe_opcode_t));
  151. } else {
  152. addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
  153. }
  154. /* Check we're not actually recursing */
  155. if (kprobe_running()) {
  156. /* We *are* holding lock here, so this is safe.
  157. Disarm the probe we just hit, and ignore it. */
  158. p = get_kprobe(addr);
  159. if (p) {
  160. if (kprobe_status == KPROBE_HIT_SS &&
  161. *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
  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. * Back up over the (now missing) int3 and run
  204. * the original instruction.
  205. */
  206. regs->eip -= sizeof(kprobe_opcode_t);
  207. ret = 1;
  208. }
  209. /* Not one of ours: let kernel handle it */
  210. goto no_kprobe;
  211. }
  212. /*
  213. * This preempt_disable() matches the preempt_enable_no_resched()
  214. * in post_kprobe_handler()
  215. */
  216. preempt_disable();
  217. kprobe_status = KPROBE_HIT_ACTIVE;
  218. set_current_kprobe(p, regs);
  219. if (p->pre_handler && p->pre_handler(p, regs))
  220. /* handler has already set things up, so skip ss setup */
  221. return 1;
  222. ss_probe:
  223. prepare_singlestep(p, regs);
  224. kprobe_status = KPROBE_HIT_SS;
  225. return 1;
  226. no_kprobe:
  227. return ret;
  228. }
  229. /*
  230. * For function-return probes, init_kprobes() establishes a probepoint
  231. * here. When a retprobed function returns, this probe is hit and
  232. * trampoline_probe_handler() runs, calling the kretprobe's handler.
  233. */
  234. void kretprobe_trampoline_holder(void)
  235. {
  236. asm volatile ( ".global kretprobe_trampoline\n"
  237. "kretprobe_trampoline: \n"
  238. "nop\n");
  239. }
  240. /*
  241. * Called when we hit the probe point at kretprobe_trampoline
  242. */
  243. int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
  244. {
  245. struct kretprobe_instance *ri = NULL;
  246. struct hlist_head *head;
  247. struct hlist_node *node, *tmp;
  248. unsigned long orig_ret_address = 0;
  249. unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
  250. head = kretprobe_inst_table_head(current);
  251. /*
  252. * It is possible to have multiple instances associated with a given
  253. * task either because an multiple functions in the call path
  254. * have a return probe installed on them, and/or more then one return
  255. * return probe was registered for a target function.
  256. *
  257. * We can handle this because:
  258. * - instances are always inserted at the head of the list
  259. * - when multiple return probes are registered for the same
  260. * function, the first instance's ret_addr will point to the
  261. * real return address, and all the rest will point to
  262. * kretprobe_trampoline
  263. */
  264. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  265. if (ri->task != current)
  266. /* another task is sharing our hash bucket */
  267. continue;
  268. if (ri->rp && ri->rp->handler)
  269. ri->rp->handler(ri, regs);
  270. orig_ret_address = (unsigned long)ri->ret_addr;
  271. recycle_rp_inst(ri);
  272. if (orig_ret_address != trampoline_address)
  273. /*
  274. * This is the real return address. Any other
  275. * instances associated with this task are for
  276. * other calls deeper on the call stack
  277. */
  278. break;
  279. }
  280. BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
  281. regs->eip = orig_ret_address;
  282. unlock_kprobes();
  283. preempt_enable_no_resched();
  284. /*
  285. * By returning a non-zero value, we are telling
  286. * kprobe_handler() that we have handled unlocking
  287. * and re-enabling preemption
  288. */
  289. return 1;
  290. }
  291. /*
  292. * Called after single-stepping. p->addr is the address of the
  293. * instruction whose first byte has been replaced by the "int 3"
  294. * instruction. To avoid the SMP problems that can occur when we
  295. * temporarily put back the original opcode to single-step, we
  296. * single-stepped a copy of the instruction. The address of this
  297. * copy is p->ainsn.insn.
  298. *
  299. * This function prepares to return from the post-single-step
  300. * interrupt. We have to fix up the stack as follows:
  301. *
  302. * 0) Except in the case of absolute or indirect jump or call instructions,
  303. * the new eip is relative to the copied instruction. We need to make
  304. * it relative to the original instruction.
  305. *
  306. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  307. * flags are set in the just-pushed eflags, and may need to be cleared.
  308. *
  309. * 2) If the single-stepped instruction was a call, the return address
  310. * that is atop the stack is the address following the copied instruction.
  311. * We need to make it the address following the original instruction.
  312. */
  313. static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
  314. {
  315. unsigned long *tos = (unsigned long *)&regs->esp;
  316. unsigned long next_eip = 0;
  317. unsigned long copy_eip = (unsigned long)&p->ainsn.insn;
  318. unsigned long orig_eip = (unsigned long)p->addr;
  319. switch (p->ainsn.insn[0]) {
  320. case 0x9c: /* pushfl */
  321. *tos &= ~(TF_MASK | IF_MASK);
  322. *tos |= kprobe_old_eflags;
  323. break;
  324. case 0xc3: /* ret/lret */
  325. case 0xcb:
  326. case 0xc2:
  327. case 0xca:
  328. regs->eflags &= ~TF_MASK;
  329. /* eip is already adjusted, no more changes required*/
  330. return;
  331. case 0xe8: /* call relative - Fix return addr */
  332. *tos = orig_eip + (*tos - copy_eip);
  333. break;
  334. case 0xff:
  335. if ((p->ainsn.insn[1] & 0x30) == 0x10) {
  336. /* call absolute, indirect */
  337. /* Fix return addr; eip is correct. */
  338. next_eip = regs->eip;
  339. *tos = orig_eip + (*tos - copy_eip);
  340. } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
  341. ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
  342. /* eip is correct. */
  343. next_eip = regs->eip;
  344. }
  345. break;
  346. case 0xea: /* jmp absolute -- eip is correct */
  347. next_eip = regs->eip;
  348. break;
  349. default:
  350. break;
  351. }
  352. regs->eflags &= ~TF_MASK;
  353. if (next_eip) {
  354. regs->eip = next_eip;
  355. } else {
  356. regs->eip = orig_eip + (regs->eip - copy_eip);
  357. }
  358. }
  359. /*
  360. * Interrupts are disabled on entry as trap1 is an interrupt gate and they
  361. * remain disabled thoroughout this function. And we hold kprobe lock.
  362. */
  363. static inline int post_kprobe_handler(struct pt_regs *regs)
  364. {
  365. if (!kprobe_running())
  366. return 0;
  367. if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) {
  368. kprobe_status = KPROBE_HIT_SSDONE;
  369. current_kprobe->post_handler(current_kprobe, regs, 0);
  370. }
  371. resume_execution(current_kprobe, regs);
  372. regs->eflags |= kprobe_saved_eflags;
  373. /*Restore back the original saved kprobes variables and continue. */
  374. if (kprobe_status == KPROBE_REENTER) {
  375. restore_previous_kprobe();
  376. goto out;
  377. }
  378. unlock_kprobes();
  379. out:
  380. preempt_enable_no_resched();
  381. /*
  382. * if somebody else is singlestepping across a probe point, eflags
  383. * will have TF set, in which case, continue the remaining processing
  384. * of do_debug, as if this is not a probe hit.
  385. */
  386. if (regs->eflags & TF_MASK)
  387. return 0;
  388. return 1;
  389. }
  390. /* Interrupts disabled, kprobe_lock held. */
  391. static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  392. {
  393. if (current_kprobe->fault_handler
  394. && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
  395. return 1;
  396. if (kprobe_status & KPROBE_HIT_SS) {
  397. resume_execution(current_kprobe, regs);
  398. regs->eflags |= kprobe_old_eflags;
  399. unlock_kprobes();
  400. preempt_enable_no_resched();
  401. }
  402. return 0;
  403. }
  404. /*
  405. * Wrapper routine to for handling exceptions.
  406. */
  407. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  408. unsigned long val, void *data)
  409. {
  410. struct die_args *args = (struct die_args *)data;
  411. int ret = NOTIFY_DONE;
  412. preempt_disable();
  413. switch (val) {
  414. case DIE_INT3:
  415. if (kprobe_handler(args->regs))
  416. ret = NOTIFY_STOP;
  417. break;
  418. case DIE_DEBUG:
  419. if (post_kprobe_handler(args->regs))
  420. ret = NOTIFY_STOP;
  421. break;
  422. case DIE_GPF:
  423. case DIE_PAGE_FAULT:
  424. if (kprobe_running() &&
  425. kprobe_fault_handler(args->regs, args->trapnr))
  426. ret = NOTIFY_STOP;
  427. break;
  428. default:
  429. break;
  430. }
  431. preempt_enable();
  432. return ret;
  433. }
  434. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  435. {
  436. struct jprobe *jp = container_of(p, struct jprobe, kp);
  437. unsigned long addr;
  438. jprobe_saved_regs = *regs;
  439. jprobe_saved_esp = &regs->esp;
  440. addr = (unsigned long)jprobe_saved_esp;
  441. /*
  442. * TBD: As Linus pointed out, gcc assumes that the callee
  443. * owns the argument space and could overwrite it, e.g.
  444. * tailcall optimization. So, to be absolutely safe
  445. * we also save and restore enough stack bytes to cover
  446. * the argument area.
  447. */
  448. memcpy(jprobes_stack, (kprobe_opcode_t *) addr, 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. 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. }