kprobes.c 22 KB

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