kprobes.c 22 KB

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