kprobes_32.c 22 KB

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