kprobes.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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 (*addr != BREAKPOINT_INSTRUCTION) {
  222. /* The breakpoint instruction was removed by
  223. * another cpu right after we hit, no further
  224. * handling of this interrupt is appropriate
  225. */
  226. regs->eip -= sizeof(kprobe_opcode_t);
  227. ret = 1;
  228. goto no_kprobe;
  229. }
  230. p = __get_cpu_var(current_kprobe);
  231. if (p->break_handler && p->break_handler(p, regs)) {
  232. goto ss_probe;
  233. }
  234. }
  235. goto no_kprobe;
  236. }
  237. p = get_kprobe(addr);
  238. if (!p) {
  239. if (*addr != BREAKPOINT_INSTRUCTION) {
  240. /*
  241. * The breakpoint instruction was removed right
  242. * after we hit it. Another cpu has removed
  243. * either a probepoint or a debugger breakpoint
  244. * at this address. In either case, no further
  245. * handling of this interrupt is appropriate.
  246. * Back up over the (now missing) int3 and run
  247. * the original instruction.
  248. */
  249. regs->eip -= sizeof(kprobe_opcode_t);
  250. ret = 1;
  251. }
  252. /* Not one of ours: let kernel handle it */
  253. goto no_kprobe;
  254. }
  255. set_current_kprobe(p, regs, kcb);
  256. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  257. if (p->pre_handler && p->pre_handler(p, regs))
  258. /* handler has already set things up, so skip ss setup */
  259. return 1;
  260. if (p->ainsn.boostable == 1 &&
  261. #ifdef CONFIG_PREEMPT
  262. !(pre_preempt_count) && /*
  263. * This enables booster when the direct
  264. * execution path aren't preempted.
  265. */
  266. #endif /* CONFIG_PREEMPT */
  267. !p->post_handler && !p->break_handler ) {
  268. /* Boost up -- we can execute copied instructions directly */
  269. reset_current_kprobe();
  270. regs->eip = (unsigned long)p->ainsn.insn;
  271. preempt_enable_no_resched();
  272. return 1;
  273. }
  274. ss_probe:
  275. prepare_singlestep(p, regs);
  276. kcb->kprobe_status = KPROBE_HIT_SS;
  277. return 1;
  278. no_kprobe:
  279. preempt_enable_no_resched();
  280. return ret;
  281. }
  282. /*
  283. * For function-return probes, init_kprobes() establishes a probepoint
  284. * here. When a retprobed function returns, this probe is hit and
  285. * trampoline_probe_handler() runs, calling the kretprobe's handler.
  286. */
  287. void __kprobes kretprobe_trampoline_holder(void)
  288. {
  289. asm volatile ( ".global kretprobe_trampoline\n"
  290. "kretprobe_trampoline: \n"
  291. " pushf\n"
  292. /* skip cs, eip, orig_eax, es, ds */
  293. " subl $20, %esp\n"
  294. " pushl %eax\n"
  295. " pushl %ebp\n"
  296. " pushl %edi\n"
  297. " pushl %esi\n"
  298. " pushl %edx\n"
  299. " pushl %ecx\n"
  300. " pushl %ebx\n"
  301. " movl %esp, %eax\n"
  302. " call trampoline_handler\n"
  303. /* move eflags to cs */
  304. " movl 48(%esp), %edx\n"
  305. " movl %edx, 44(%esp)\n"
  306. /* save true return address on eflags */
  307. " movl %eax, 48(%esp)\n"
  308. " popl %ebx\n"
  309. " popl %ecx\n"
  310. " popl %edx\n"
  311. " popl %esi\n"
  312. " popl %edi\n"
  313. " popl %ebp\n"
  314. " popl %eax\n"
  315. /* skip eip, orig_eax, es, ds */
  316. " addl $16, %esp\n"
  317. " popf\n"
  318. " ret\n");
  319. }
  320. /*
  321. * Called from kretprobe_trampoline
  322. */
  323. fastcall void *__kprobes trampoline_handler(struct pt_regs *regs)
  324. {
  325. struct kretprobe_instance *ri = NULL;
  326. struct hlist_head *head;
  327. struct hlist_node *node, *tmp;
  328. unsigned long flags, orig_ret_address = 0;
  329. unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
  330. spin_lock_irqsave(&kretprobe_lock, flags);
  331. head = kretprobe_inst_table_head(current);
  332. /*
  333. * It is possible to have multiple instances associated with a given
  334. * task either because an multiple functions in the call path
  335. * have a return probe installed on them, and/or more then one return
  336. * return probe was registered for a target function.
  337. *
  338. * We can handle this because:
  339. * - instances are always inserted at the head of the list
  340. * - when multiple return probes are registered for the same
  341. * function, the first instance's ret_addr will point to the
  342. * real return address, and all the rest will point to
  343. * kretprobe_trampoline
  344. */
  345. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  346. if (ri->task != current)
  347. /* another task is sharing our hash bucket */
  348. continue;
  349. if (ri->rp && ri->rp->handler){
  350. __get_cpu_var(current_kprobe) = &ri->rp->kp;
  351. ri->rp->handler(ri, regs);
  352. __get_cpu_var(current_kprobe) = NULL;
  353. }
  354. orig_ret_address = (unsigned long)ri->ret_addr;
  355. recycle_rp_inst(ri);
  356. if (orig_ret_address != trampoline_address)
  357. /*
  358. * This is the real return address. Any other
  359. * instances associated with this task are for
  360. * other calls deeper on the call stack
  361. */
  362. break;
  363. }
  364. BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
  365. spin_unlock_irqrestore(&kretprobe_lock, flags);
  366. return (void*)orig_ret_address;
  367. }
  368. /*
  369. * Called after single-stepping. p->addr is the address of the
  370. * instruction whose first byte has been replaced by the "int 3"
  371. * instruction. To avoid the SMP problems that can occur when we
  372. * temporarily put back the original opcode to single-step, we
  373. * single-stepped a copy of the instruction. The address of this
  374. * copy is p->ainsn.insn.
  375. *
  376. * This function prepares to return from the post-single-step
  377. * interrupt. We have to fix up the stack as follows:
  378. *
  379. * 0) Except in the case of absolute or indirect jump or call instructions,
  380. * the new eip is relative to the copied instruction. We need to make
  381. * it relative to the original instruction.
  382. *
  383. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  384. * flags are set in the just-pushed eflags, and may need to be cleared.
  385. *
  386. * 2) If the single-stepped instruction was a call, the return address
  387. * that is atop the stack is the address following the copied instruction.
  388. * We need to make it the address following the original instruction.
  389. *
  390. * This function also checks instruction size for preparing direct execution.
  391. */
  392. static void __kprobes resume_execution(struct kprobe *p,
  393. struct pt_regs *regs, struct kprobe_ctlblk *kcb)
  394. {
  395. unsigned long *tos = (unsigned long *)&regs->esp;
  396. unsigned long copy_eip = (unsigned long)p->ainsn.insn;
  397. unsigned long orig_eip = (unsigned long)p->addr;
  398. regs->eflags &= ~TF_MASK;
  399. switch (p->ainsn.insn[0]) {
  400. case 0x9c: /* pushfl */
  401. *tos &= ~(TF_MASK | IF_MASK);
  402. *tos |= kcb->kprobe_old_eflags;
  403. break;
  404. case 0xc3: /* ret/lret */
  405. case 0xcb:
  406. case 0xc2:
  407. case 0xca:
  408. case 0xea: /* jmp absolute -- eip is correct */
  409. /* eip is already adjusted, no more changes required */
  410. p->ainsn.boostable = 1;
  411. goto no_change;
  412. case 0xe8: /* call relative - Fix return addr */
  413. *tos = orig_eip + (*tos - copy_eip);
  414. break;
  415. case 0xff:
  416. if ((p->ainsn.insn[1] & 0x30) == 0x10) {
  417. /* call absolute, indirect */
  418. /*
  419. * Fix return addr; eip is correct.
  420. * But this is not boostable
  421. */
  422. *tos = orig_eip + (*tos - copy_eip);
  423. goto no_change;
  424. } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
  425. ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
  426. /* eip is correct. And this is boostable */
  427. p->ainsn.boostable = 1;
  428. goto no_change;
  429. }
  430. default:
  431. break;
  432. }
  433. if (p->ainsn.boostable == 0) {
  434. if ((regs->eip > copy_eip) &&
  435. (regs->eip - copy_eip) + 5 < MAX_INSN_SIZE) {
  436. /*
  437. * These instructions can be executed directly if it
  438. * jumps back to correct address.
  439. */
  440. set_jmp_op((void *)regs->eip,
  441. (void *)orig_eip + (regs->eip - copy_eip));
  442. p->ainsn.boostable = 1;
  443. } else {
  444. p->ainsn.boostable = -1;
  445. }
  446. }
  447. regs->eip = orig_eip + (regs->eip - copy_eip);
  448. no_change:
  449. return;
  450. }
  451. /*
  452. * Interrupts are disabled on entry as trap1 is an interrupt gate and they
  453. * remain disabled thoroughout this function.
  454. */
  455. static int __kprobes post_kprobe_handler(struct pt_regs *regs)
  456. {
  457. struct kprobe *cur = kprobe_running();
  458. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  459. if (!cur)
  460. return 0;
  461. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  462. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  463. cur->post_handler(cur, regs, 0);
  464. }
  465. resume_execution(cur, regs, kcb);
  466. regs->eflags |= kcb->kprobe_saved_eflags;
  467. /*Restore back the original saved kprobes variables and continue. */
  468. if (kcb->kprobe_status == KPROBE_REENTER) {
  469. restore_previous_kprobe(kcb);
  470. goto out;
  471. }
  472. reset_current_kprobe();
  473. out:
  474. preempt_enable_no_resched();
  475. /*
  476. * if somebody else is singlestepping across a probe point, eflags
  477. * will have TF set, in which case, continue the remaining processing
  478. * of do_debug, as if this is not a probe hit.
  479. */
  480. if (regs->eflags & TF_MASK)
  481. return 0;
  482. return 1;
  483. }
  484. static int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  485. {
  486. struct kprobe *cur = kprobe_running();
  487. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  488. switch(kcb->kprobe_status) {
  489. case KPROBE_HIT_SS:
  490. case KPROBE_REENTER:
  491. /*
  492. * We are here because the instruction being single
  493. * stepped caused a page fault. We reset the current
  494. * kprobe and the eip points back to the probe address
  495. * and allow the page fault handler to continue as a
  496. * normal page fault.
  497. */
  498. regs->eip = (unsigned long)cur->addr;
  499. regs->eflags |= kcb->kprobe_old_eflags;
  500. if (kcb->kprobe_status == KPROBE_REENTER)
  501. restore_previous_kprobe(kcb);
  502. else
  503. reset_current_kprobe();
  504. preempt_enable_no_resched();
  505. break;
  506. case KPROBE_HIT_ACTIVE:
  507. case KPROBE_HIT_SSDONE:
  508. /*
  509. * We increment the nmissed count for accounting,
  510. * we can also use npre/npostfault count for accouting
  511. * these specific fault cases.
  512. */
  513. kprobes_inc_nmissed_count(cur);
  514. /*
  515. * We come here because instructions in the pre/post
  516. * handler caused the page_fault, this could happen
  517. * if handler tries to access user space by
  518. * copy_from_user(), get_user() etc. Let the
  519. * user-specified handler try to fix it first.
  520. */
  521. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  522. return 1;
  523. /*
  524. * In case the user-specified fault handler returned
  525. * zero, try to fix up.
  526. */
  527. if (fixup_exception(regs))
  528. return 1;
  529. /*
  530. * fixup_exception() could not handle it,
  531. * Let do_page_fault() fix it.
  532. */
  533. break;
  534. default:
  535. break;
  536. }
  537. return 0;
  538. }
  539. /*
  540. * Wrapper routine to for handling exceptions.
  541. */
  542. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  543. unsigned long val, void *data)
  544. {
  545. struct die_args *args = (struct die_args *)data;
  546. int ret = NOTIFY_DONE;
  547. if (args->regs && user_mode(args->regs))
  548. return ret;
  549. switch (val) {
  550. case DIE_INT3:
  551. if (kprobe_handler(args->regs))
  552. ret = NOTIFY_STOP;
  553. break;
  554. case DIE_DEBUG:
  555. if (post_kprobe_handler(args->regs))
  556. ret = NOTIFY_STOP;
  557. break;
  558. case DIE_GPF:
  559. case DIE_PAGE_FAULT:
  560. /* kprobe_running() needs smp_processor_id() */
  561. preempt_disable();
  562. if (kprobe_running() &&
  563. kprobe_fault_handler(args->regs, args->trapnr))
  564. ret = NOTIFY_STOP;
  565. preempt_enable();
  566. break;
  567. default:
  568. break;
  569. }
  570. return ret;
  571. }
  572. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  573. {
  574. struct jprobe *jp = container_of(p, struct jprobe, kp);
  575. unsigned long addr;
  576. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  577. kcb->jprobe_saved_regs = *regs;
  578. kcb->jprobe_saved_esp = &regs->esp;
  579. addr = (unsigned long)(kcb->jprobe_saved_esp);
  580. /*
  581. * TBD: As Linus pointed out, gcc assumes that the callee
  582. * owns the argument space and could overwrite it, e.g.
  583. * tailcall optimization. So, to be absolutely safe
  584. * we also save and restore enough stack bytes to cover
  585. * the argument area.
  586. */
  587. memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
  588. MIN_STACK_SIZE(addr));
  589. regs->eflags &= ~IF_MASK;
  590. regs->eip = (unsigned long)(jp->entry);
  591. return 1;
  592. }
  593. void __kprobes jprobe_return(void)
  594. {
  595. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  596. asm volatile (" xchgl %%ebx,%%esp \n"
  597. " int3 \n"
  598. " .globl jprobe_return_end \n"
  599. " jprobe_return_end: \n"
  600. " nop \n"::"b"
  601. (kcb->jprobe_saved_esp):"memory");
  602. }
  603. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  604. {
  605. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  606. u8 *addr = (u8 *) (regs->eip - 1);
  607. unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_esp);
  608. struct jprobe *jp = container_of(p, struct jprobe, kp);
  609. if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
  610. if (&regs->esp != kcb->jprobe_saved_esp) {
  611. struct pt_regs *saved_regs =
  612. container_of(kcb->jprobe_saved_esp,
  613. struct pt_regs, esp);
  614. printk("current esp %p does not match saved esp %p\n",
  615. &regs->esp, kcb->jprobe_saved_esp);
  616. printk("Saved registers for jprobe %p\n", jp);
  617. show_registers(saved_regs);
  618. printk("Current registers\n");
  619. show_registers(regs);
  620. BUG();
  621. }
  622. *regs = kcb->jprobe_saved_regs;
  623. memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
  624. MIN_STACK_SIZE(stack_addr));
  625. preempt_enable_no_resched();
  626. return 1;
  627. }
  628. return 0;
  629. }
  630. int __init arch_init_kprobes(void)
  631. {
  632. return 0;
  633. }