kprobes.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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/preempt.h>
  34. #include <asm/cacheflush.h>
  35. #include <asm/kdebug.h>
  36. #include <asm/desc.h>
  37. #include <asm/uaccess.h>
  38. void jprobe_return_end(void);
  39. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  40. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  41. /* insert a jmp code */
  42. static __always_inline void set_jmp_op(void *from, void *to)
  43. {
  44. struct __arch_jmp_op {
  45. char op;
  46. long raddr;
  47. } __attribute__((packed)) *jop;
  48. jop = (struct __arch_jmp_op *)from;
  49. jop->raddr = (long)(to) - ((long)(from) + 5);
  50. jop->op = RELATIVEJUMP_INSTRUCTION;
  51. }
  52. /*
  53. * returns non-zero if opcodes can be boosted.
  54. */
  55. static __always_inline int can_boost(kprobe_opcode_t opcode)
  56. {
  57. switch (opcode & 0xf0 ) {
  58. case 0x70:
  59. return 0; /* can't boost conditional jump */
  60. case 0x90:
  61. /* can't boost call and pushf */
  62. return opcode != 0x9a && opcode != 0x9c;
  63. case 0xc0:
  64. /* can't boost undefined opcodes and soft-interruptions */
  65. return (0xc1 < opcode && opcode < 0xc6) ||
  66. (0xc7 < opcode && opcode < 0xcc) || opcode == 0xcf;
  67. case 0xd0:
  68. /* can boost AA* and XLAT */
  69. return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
  70. case 0xe0:
  71. /* can boost in/out and (may be) jmps */
  72. return (0xe3 < opcode && opcode != 0xe8);
  73. case 0xf0:
  74. /* clear and set flags can be boost */
  75. return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
  76. default:
  77. /* currently, can't boost 2 bytes opcodes */
  78. return opcode != 0x0f;
  79. }
  80. }
  81. /*
  82. * returns non-zero if opcode modifies the interrupt flag.
  83. */
  84. static int __kprobes is_IF_modifier(kprobe_opcode_t opcode)
  85. {
  86. switch (opcode) {
  87. case 0xfa: /* cli */
  88. case 0xfb: /* sti */
  89. case 0xcf: /* iret/iretd */
  90. case 0x9d: /* popf/popfd */
  91. return 1;
  92. }
  93. return 0;
  94. }
  95. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  96. {
  97. /* insn: must be on special executable page on i386. */
  98. p->ainsn.insn = get_insn_slot();
  99. if (!p->ainsn.insn)
  100. return -ENOMEM;
  101. memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
  102. p->opcode = *p->addr;
  103. if (can_boost(p->opcode)) {
  104. p->ainsn.boostable = 0;
  105. } else {
  106. p->ainsn.boostable = -1;
  107. }
  108. return 0;
  109. }
  110. void __kprobes arch_arm_kprobe(struct kprobe *p)
  111. {
  112. *p->addr = BREAKPOINT_INSTRUCTION;
  113. flush_icache_range((unsigned long) p->addr,
  114. (unsigned long) p->addr + sizeof(kprobe_opcode_t));
  115. }
  116. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  117. {
  118. *p->addr = p->opcode;
  119. flush_icache_range((unsigned long) p->addr,
  120. (unsigned long) p->addr + sizeof(kprobe_opcode_t));
  121. }
  122. void __kprobes arch_remove_kprobe(struct kprobe *p)
  123. {
  124. mutex_lock(&kprobe_mutex);
  125. free_insn_slot(p->ainsn.insn);
  126. mutex_unlock(&kprobe_mutex);
  127. }
  128. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  129. {
  130. kcb->prev_kprobe.kp = kprobe_running();
  131. kcb->prev_kprobe.status = kcb->kprobe_status;
  132. kcb->prev_kprobe.old_eflags = kcb->kprobe_old_eflags;
  133. kcb->prev_kprobe.saved_eflags = kcb->kprobe_saved_eflags;
  134. }
  135. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  136. {
  137. __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
  138. kcb->kprobe_status = kcb->prev_kprobe.status;
  139. kcb->kprobe_old_eflags = kcb->prev_kprobe.old_eflags;
  140. kcb->kprobe_saved_eflags = kcb->prev_kprobe.saved_eflags;
  141. }
  142. static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  143. struct kprobe_ctlblk *kcb)
  144. {
  145. __get_cpu_var(current_kprobe) = p;
  146. kcb->kprobe_saved_eflags = kcb->kprobe_old_eflags
  147. = (regs->eflags & (TF_MASK | IF_MASK));
  148. if (is_IF_modifier(p->opcode))
  149. kcb->kprobe_saved_eflags &= ~IF_MASK;
  150. }
  151. static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  152. {
  153. regs->eflags |= TF_MASK;
  154. regs->eflags &= ~IF_MASK;
  155. /*single step inline if the instruction is an int3*/
  156. if (p->opcode == BREAKPOINT_INSTRUCTION)
  157. regs->eip = (unsigned long)p->addr;
  158. else
  159. regs->eip = (unsigned long)p->ainsn.insn;
  160. }
  161. /* Called with kretprobe_lock held */
  162. void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
  163. struct pt_regs *regs)
  164. {
  165. unsigned long *sara = (unsigned long *)&regs->esp;
  166. struct kretprobe_instance *ri;
  167. if ((ri = get_free_rp_inst(rp)) != NULL) {
  168. ri->rp = rp;
  169. ri->task = current;
  170. ri->ret_addr = (kprobe_opcode_t *) *sara;
  171. /* Replace the return addr with trampoline addr */
  172. *sara = (unsigned long) &kretprobe_trampoline;
  173. add_rp_inst(ri);
  174. } else {
  175. rp->nmissed++;
  176. }
  177. }
  178. /*
  179. * Interrupts are disabled on entry as trap3 is an interrupt gate and they
  180. * remain disabled thorough out this function.
  181. */
  182. static int __kprobes kprobe_handler(struct pt_regs *regs)
  183. {
  184. struct kprobe *p;
  185. int ret = 0;
  186. kprobe_opcode_t *addr;
  187. struct kprobe_ctlblk *kcb;
  188. #ifdef CONFIG_PREEMPT
  189. unsigned pre_preempt_count = preempt_count();
  190. #endif /* CONFIG_PREEMPT */
  191. addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
  192. /*
  193. * We don't want to be preempted for the entire
  194. * duration of kprobe processing
  195. */
  196. preempt_disable();
  197. kcb = get_kprobe_ctlblk();
  198. /* Check we're not actually recursing */
  199. if (kprobe_running()) {
  200. p = get_kprobe(addr);
  201. if (p) {
  202. if (kcb->kprobe_status == KPROBE_HIT_SS &&
  203. *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
  204. regs->eflags &= ~TF_MASK;
  205. regs->eflags |= kcb->kprobe_saved_eflags;
  206. goto no_kprobe;
  207. }
  208. /* We have reentered the kprobe_handler(), since
  209. * another probe was hit while within the handler.
  210. * We here save the original kprobes variables and
  211. * just single step on the instruction of the new probe
  212. * without calling any user handlers.
  213. */
  214. save_previous_kprobe(kcb);
  215. set_current_kprobe(p, regs, kcb);
  216. kprobes_inc_nmissed_count(p);
  217. prepare_singlestep(p, regs);
  218. kcb->kprobe_status = KPROBE_REENTER;
  219. return 1;
  220. } else {
  221. if (regs->eflags & VM_MASK) {
  222. /* We are in virtual-8086 mode. Return 0 */
  223. goto no_kprobe;
  224. }
  225. if (*addr != BREAKPOINT_INSTRUCTION) {
  226. /* The breakpoint instruction was removed by
  227. * another cpu right after we hit, no further
  228. * handling of this interrupt is appropriate
  229. */
  230. regs->eip -= sizeof(kprobe_opcode_t);
  231. ret = 1;
  232. goto no_kprobe;
  233. }
  234. p = __get_cpu_var(current_kprobe);
  235. if (p->break_handler && p->break_handler(p, regs)) {
  236. goto ss_probe;
  237. }
  238. }
  239. goto no_kprobe;
  240. }
  241. p = get_kprobe(addr);
  242. if (!p) {
  243. if (regs->eflags & VM_MASK) {
  244. /* We are in virtual-8086 mode. Return 0 */
  245. goto no_kprobe;
  246. }
  247. if (*addr != BREAKPOINT_INSTRUCTION) {
  248. /*
  249. * The breakpoint instruction was removed right
  250. * after we hit it. Another cpu has removed
  251. * either a probepoint or a debugger breakpoint
  252. * at this address. In either case, no further
  253. * handling of this interrupt is appropriate.
  254. * Back up over the (now missing) int3 and run
  255. * the original instruction.
  256. */
  257. regs->eip -= sizeof(kprobe_opcode_t);
  258. ret = 1;
  259. }
  260. /* Not one of ours: let kernel handle it */
  261. goto no_kprobe;
  262. }
  263. set_current_kprobe(p, regs, kcb);
  264. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  265. if (p->pre_handler && p->pre_handler(p, regs))
  266. /* handler has already set things up, so skip ss setup */
  267. return 1;
  268. if (p->ainsn.boostable == 1 &&
  269. #ifdef CONFIG_PREEMPT
  270. !(pre_preempt_count) && /*
  271. * This enables booster when the direct
  272. * execution path aren't preempted.
  273. */
  274. #endif /* CONFIG_PREEMPT */
  275. !p->post_handler && !p->break_handler ) {
  276. /* Boost up -- we can execute copied instructions directly */
  277. reset_current_kprobe();
  278. regs->eip = (unsigned long)p->ainsn.insn;
  279. preempt_enable_no_resched();
  280. return 1;
  281. }
  282. ss_probe:
  283. prepare_singlestep(p, regs);
  284. kcb->kprobe_status = KPROBE_HIT_SS;
  285. return 1;
  286. no_kprobe:
  287. preempt_enable_no_resched();
  288. return ret;
  289. }
  290. /*
  291. * For function-return probes, init_kprobes() establishes a probepoint
  292. * here. When a retprobed function returns, this probe is hit and
  293. * trampoline_probe_handler() runs, calling the kretprobe's handler.
  294. */
  295. void __kprobes kretprobe_trampoline_holder(void)
  296. {
  297. asm volatile ( ".global kretprobe_trampoline\n"
  298. "kretprobe_trampoline: \n"
  299. " pushf\n"
  300. /* skip cs, eip, orig_eax, es, ds */
  301. " subl $20, %esp\n"
  302. " pushl %eax\n"
  303. " pushl %ebp\n"
  304. " pushl %edi\n"
  305. " pushl %esi\n"
  306. " pushl %edx\n"
  307. " pushl %ecx\n"
  308. " pushl %ebx\n"
  309. " movl %esp, %eax\n"
  310. " call trampoline_handler\n"
  311. /* move eflags to cs */
  312. " movl 48(%esp), %edx\n"
  313. " movl %edx, 44(%esp)\n"
  314. /* save true return address on eflags */
  315. " movl %eax, 48(%esp)\n"
  316. " popl %ebx\n"
  317. " popl %ecx\n"
  318. " popl %edx\n"
  319. " popl %esi\n"
  320. " popl %edi\n"
  321. " popl %ebp\n"
  322. " popl %eax\n"
  323. /* skip eip, orig_eax, es, ds */
  324. " addl $16, %esp\n"
  325. " popf\n"
  326. " ret\n");
  327. }
  328. /*
  329. * Called from kretprobe_trampoline
  330. */
  331. fastcall void *__kprobes trampoline_handler(struct pt_regs *regs)
  332. {
  333. struct kretprobe_instance *ri = NULL;
  334. struct hlist_head *head;
  335. struct hlist_node *node, *tmp;
  336. unsigned long flags, orig_ret_address = 0;
  337. unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
  338. spin_lock_irqsave(&kretprobe_lock, flags);
  339. head = kretprobe_inst_table_head(current);
  340. /*
  341. * It is possible to have multiple instances associated with a given
  342. * task either because an multiple functions in the call path
  343. * have a return probe installed on them, and/or more then one return
  344. * return probe was registered for a target function.
  345. *
  346. * We can handle this because:
  347. * - instances are always inserted at the head of the list
  348. * - when multiple return probes are registered for the same
  349. * function, the first instance's ret_addr will point to the
  350. * real return address, and all the rest will point to
  351. * kretprobe_trampoline
  352. */
  353. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  354. if (ri->task != current)
  355. /* another task is sharing our hash bucket */
  356. continue;
  357. if (ri->rp && ri->rp->handler){
  358. __get_cpu_var(current_kprobe) = &ri->rp->kp;
  359. ri->rp->handler(ri, regs);
  360. __get_cpu_var(current_kprobe) = NULL;
  361. }
  362. orig_ret_address = (unsigned long)ri->ret_addr;
  363. recycle_rp_inst(ri);
  364. if (orig_ret_address != trampoline_address)
  365. /*
  366. * This is the real return address. Any other
  367. * instances associated with this task are for
  368. * other calls deeper on the call stack
  369. */
  370. break;
  371. }
  372. BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
  373. spin_unlock_irqrestore(&kretprobe_lock, flags);
  374. return (void*)orig_ret_address;
  375. }
  376. /*
  377. * Called after single-stepping. p->addr is the address of the
  378. * instruction whose first byte has been replaced by the "int 3"
  379. * instruction. To avoid the SMP problems that can occur when we
  380. * temporarily put back the original opcode to single-step, we
  381. * single-stepped a copy of the instruction. The address of this
  382. * copy is p->ainsn.insn.
  383. *
  384. * This function prepares to return from the post-single-step
  385. * interrupt. We have to fix up the stack as follows:
  386. *
  387. * 0) Except in the case of absolute or indirect jump or call instructions,
  388. * the new eip is relative to the copied instruction. We need to make
  389. * it relative to the original instruction.
  390. *
  391. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  392. * flags are set in the just-pushed eflags, and may need to be cleared.
  393. *
  394. * 2) If the single-stepped instruction was a call, the return address
  395. * that is atop the stack is the address following the copied instruction.
  396. * We need to make it the address following the original instruction.
  397. *
  398. * This function also checks instruction size for preparing direct execution.
  399. */
  400. static void __kprobes resume_execution(struct kprobe *p,
  401. struct pt_regs *regs, struct kprobe_ctlblk *kcb)
  402. {
  403. unsigned long *tos = (unsigned long *)&regs->esp;
  404. unsigned long copy_eip = (unsigned long)p->ainsn.insn;
  405. unsigned long orig_eip = (unsigned long)p->addr;
  406. regs->eflags &= ~TF_MASK;
  407. switch (p->ainsn.insn[0]) {
  408. case 0x9c: /* pushfl */
  409. *tos &= ~(TF_MASK | IF_MASK);
  410. *tos |= kcb->kprobe_old_eflags;
  411. break;
  412. case 0xc3: /* ret/lret */
  413. case 0xcb:
  414. case 0xc2:
  415. case 0xca:
  416. case 0xea: /* jmp absolute -- eip is correct */
  417. /* eip is already adjusted, no more changes required */
  418. p->ainsn.boostable = 1;
  419. goto no_change;
  420. case 0xe8: /* call relative - Fix return addr */
  421. *tos = orig_eip + (*tos - copy_eip);
  422. break;
  423. case 0xff:
  424. if ((p->ainsn.insn[1] & 0x30) == 0x10) {
  425. /* call absolute, indirect */
  426. /*
  427. * Fix return addr; eip is correct.
  428. * But this is not boostable
  429. */
  430. *tos = orig_eip + (*tos - copy_eip);
  431. goto no_change;
  432. } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
  433. ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
  434. /* eip is correct. And this is boostable */
  435. p->ainsn.boostable = 1;
  436. goto no_change;
  437. }
  438. default:
  439. break;
  440. }
  441. if (p->ainsn.boostable == 0) {
  442. if ((regs->eip > copy_eip) &&
  443. (regs->eip - copy_eip) + 5 < MAX_INSN_SIZE) {
  444. /*
  445. * These instructions can be executed directly if it
  446. * jumps back to correct address.
  447. */
  448. set_jmp_op((void *)regs->eip,
  449. (void *)orig_eip + (regs->eip - copy_eip));
  450. p->ainsn.boostable = 1;
  451. } else {
  452. p->ainsn.boostable = -1;
  453. }
  454. }
  455. regs->eip = orig_eip + (regs->eip - copy_eip);
  456. no_change:
  457. return;
  458. }
  459. /*
  460. * Interrupts are disabled on entry as trap1 is an interrupt gate and they
  461. * remain disabled thoroughout this function.
  462. */
  463. static int __kprobes post_kprobe_handler(struct pt_regs *regs)
  464. {
  465. struct kprobe *cur = kprobe_running();
  466. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  467. if (!cur)
  468. return 0;
  469. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  470. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  471. cur->post_handler(cur, regs, 0);
  472. }
  473. resume_execution(cur, regs, kcb);
  474. regs->eflags |= kcb->kprobe_saved_eflags;
  475. /*Restore back the original saved kprobes variables and continue. */
  476. if (kcb->kprobe_status == KPROBE_REENTER) {
  477. restore_previous_kprobe(kcb);
  478. goto out;
  479. }
  480. reset_current_kprobe();
  481. out:
  482. preempt_enable_no_resched();
  483. /*
  484. * if somebody else is singlestepping across a probe point, eflags
  485. * will have TF set, in which case, continue the remaining processing
  486. * of do_debug, as if this is not a probe hit.
  487. */
  488. if (regs->eflags & TF_MASK)
  489. return 0;
  490. return 1;
  491. }
  492. static int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  493. {
  494. struct kprobe *cur = kprobe_running();
  495. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  496. switch(kcb->kprobe_status) {
  497. case KPROBE_HIT_SS:
  498. case KPROBE_REENTER:
  499. /*
  500. * We are here because the instruction being single
  501. * stepped caused a page fault. We reset the current
  502. * kprobe and the eip points back to the probe address
  503. * and allow the page fault handler to continue as a
  504. * normal page fault.
  505. */
  506. regs->eip = (unsigned long)cur->addr;
  507. regs->eflags |= kcb->kprobe_old_eflags;
  508. if (kcb->kprobe_status == KPROBE_REENTER)
  509. restore_previous_kprobe(kcb);
  510. else
  511. reset_current_kprobe();
  512. preempt_enable_no_resched();
  513. break;
  514. case KPROBE_HIT_ACTIVE:
  515. case KPROBE_HIT_SSDONE:
  516. /*
  517. * We increment the nmissed count for accounting,
  518. * we can also use npre/npostfault count for accouting
  519. * these specific fault cases.
  520. */
  521. kprobes_inc_nmissed_count(cur);
  522. /*
  523. * We come here because instructions in the pre/post
  524. * handler caused the page_fault, this could happen
  525. * if handler tries to access user space by
  526. * copy_from_user(), get_user() etc. Let the
  527. * user-specified handler try to fix it first.
  528. */
  529. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  530. return 1;
  531. /*
  532. * In case the user-specified fault handler returned
  533. * zero, try to fix up.
  534. */
  535. if (fixup_exception(regs))
  536. return 1;
  537. /*
  538. * fixup_exception() could not handle it,
  539. * Let do_page_fault() fix it.
  540. */
  541. break;
  542. default:
  543. break;
  544. }
  545. return 0;
  546. }
  547. /*
  548. * Wrapper routine to for handling exceptions.
  549. */
  550. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  551. unsigned long val, void *data)
  552. {
  553. struct die_args *args = (struct die_args *)data;
  554. int ret = NOTIFY_DONE;
  555. if (args->regs && user_mode(args->regs))
  556. return ret;
  557. switch (val) {
  558. case DIE_INT3:
  559. if (kprobe_handler(args->regs))
  560. ret = NOTIFY_STOP;
  561. break;
  562. case DIE_DEBUG:
  563. if (post_kprobe_handler(args->regs))
  564. ret = NOTIFY_STOP;
  565. break;
  566. case DIE_GPF:
  567. case DIE_PAGE_FAULT:
  568. /* kprobe_running() needs smp_processor_id() */
  569. preempt_disable();
  570. if (kprobe_running() &&
  571. kprobe_fault_handler(args->regs, args->trapnr))
  572. ret = NOTIFY_STOP;
  573. preempt_enable();
  574. break;
  575. default:
  576. break;
  577. }
  578. return ret;
  579. }
  580. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  581. {
  582. struct jprobe *jp = container_of(p, struct jprobe, kp);
  583. unsigned long addr;
  584. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  585. kcb->jprobe_saved_regs = *regs;
  586. kcb->jprobe_saved_esp = &regs->esp;
  587. addr = (unsigned long)(kcb->jprobe_saved_esp);
  588. /*
  589. * TBD: As Linus pointed out, gcc assumes that the callee
  590. * owns the argument space and could overwrite it, e.g.
  591. * tailcall optimization. So, to be absolutely safe
  592. * we also save and restore enough stack bytes to cover
  593. * the argument area.
  594. */
  595. memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
  596. MIN_STACK_SIZE(addr));
  597. regs->eflags &= ~IF_MASK;
  598. regs->eip = (unsigned long)(jp->entry);
  599. return 1;
  600. }
  601. void __kprobes jprobe_return(void)
  602. {
  603. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  604. asm volatile (" xchgl %%ebx,%%esp \n"
  605. " int3 \n"
  606. " .globl jprobe_return_end \n"
  607. " jprobe_return_end: \n"
  608. " nop \n"::"b"
  609. (kcb->jprobe_saved_esp):"memory");
  610. }
  611. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  612. {
  613. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  614. u8 *addr = (u8 *) (regs->eip - 1);
  615. unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_esp);
  616. struct jprobe *jp = container_of(p, struct jprobe, kp);
  617. if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
  618. if (&regs->esp != kcb->jprobe_saved_esp) {
  619. struct pt_regs *saved_regs =
  620. container_of(kcb->jprobe_saved_esp,
  621. struct pt_regs, esp);
  622. printk("current esp %p does not match saved esp %p\n",
  623. &regs->esp, kcb->jprobe_saved_esp);
  624. printk("Saved registers for jprobe %p\n", jp);
  625. show_registers(saved_regs);
  626. printk("Current registers\n");
  627. show_registers(regs);
  628. BUG();
  629. }
  630. *regs = kcb->jprobe_saved_regs;
  631. memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
  632. MIN_STACK_SIZE(stack_addr));
  633. preempt_enable_no_resched();
  634. return 1;
  635. }
  636. return 0;
  637. }
  638. int __init arch_init_kprobes(void)
  639. {
  640. return 0;
  641. }