kprobes.c 19 KB

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