kprobes.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. /*
  2. * Kernel Probes (KProbes)
  3. * arch/x86_64/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. * 2004-Oct Jim Keniston <kenistoj@us.ibm.com> and Prasanna S Panchamukhi
  27. * <prasanna@in.ibm.com> adapted for x86_64
  28. * 2005-Mar Roland McGrath <roland@redhat.com>
  29. * Fixed to handle %rip-relative addressing mode correctly.
  30. * 2005-May Rusty Lynch <rusty.lynch@intel.com>
  31. * Added function return probes functionality
  32. */
  33. #include <linux/kprobes.h>
  34. #include <linux/ptrace.h>
  35. #include <linux/string.h>
  36. #include <linux/slab.h>
  37. #include <linux/preempt.h>
  38. #include <linux/module.h>
  39. #include <linux/kdebug.h>
  40. #include <asm/cacheflush.h>
  41. #include <asm/pgtable.h>
  42. #include <asm/uaccess.h>
  43. void jprobe_return_end(void);
  44. static void __kprobes arch_copy_kprobe(struct kprobe *p);
  45. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  46. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  47. /*
  48. * returns non-zero if opcode modifies the interrupt flag.
  49. */
  50. static __always_inline int is_IF_modifier(kprobe_opcode_t *insn)
  51. {
  52. switch (*insn) {
  53. case 0xfa: /* cli */
  54. case 0xfb: /* sti */
  55. case 0xcf: /* iret/iretd */
  56. case 0x9d: /* popf/popfd */
  57. return 1;
  58. }
  59. if (*insn >= 0x40 && *insn <= 0x4f && *++insn == 0xcf)
  60. return 1;
  61. return 0;
  62. }
  63. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  64. {
  65. /* insn: must be on special executable page on x86_64. */
  66. p->ainsn.insn = get_insn_slot();
  67. if (!p->ainsn.insn) {
  68. return -ENOMEM;
  69. }
  70. arch_copy_kprobe(p);
  71. return 0;
  72. }
  73. /*
  74. * Determine if the instruction uses the %rip-relative addressing mode.
  75. * If it does, return the address of the 32-bit displacement word.
  76. * If not, return null.
  77. */
  78. static s32 __kprobes *is_riprel(u8 *insn)
  79. {
  80. #define W(row,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb,bc,bd,be,bf) \
  81. (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
  82. (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
  83. (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
  84. (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
  85. << (row % 64))
  86. static const u64 onebyte_has_modrm[256 / 64] = {
  87. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  88. /* ------------------------------- */
  89. W(0x00, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 00 */
  90. W(0x10, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 10 */
  91. W(0x20, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 20 */
  92. W(0x30, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0), /* 30 */
  93. W(0x40, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 40 */
  94. W(0x50, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 50 */
  95. W(0x60, 0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0)| /* 60 */
  96. W(0x70, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 70 */
  97. W(0x80, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 80 */
  98. W(0x90, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 90 */
  99. W(0xa0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* a0 */
  100. W(0xb0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* b0 */
  101. W(0xc0, 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0)| /* c0 */
  102. W(0xd0, 1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1)| /* d0 */
  103. W(0xe0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* e0 */
  104. W(0xf0, 0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1) /* f0 */
  105. /* ------------------------------- */
  106. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  107. };
  108. static const u64 twobyte_has_modrm[256 / 64] = {
  109. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  110. /* ------------------------------- */
  111. W(0x00, 1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,1)| /* 0f */
  112. W(0x10, 1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0)| /* 1f */
  113. W(0x20, 1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1)| /* 2f */
  114. W(0x30, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 3f */
  115. W(0x40, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 4f */
  116. W(0x50, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 5f */
  117. W(0x60, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 6f */
  118. W(0x70, 1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1), /* 7f */
  119. W(0x80, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 8f */
  120. W(0x90, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 9f */
  121. W(0xa0, 0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1)| /* af */
  122. W(0xb0, 1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1), /* bf */
  123. W(0xc0, 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0)| /* cf */
  124. W(0xd0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* df */
  125. W(0xe0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* ef */
  126. W(0xf0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0) /* ff */
  127. /* ------------------------------- */
  128. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  129. };
  130. #undef W
  131. int need_modrm;
  132. /* Skip legacy instruction prefixes. */
  133. while (1) {
  134. switch (*insn) {
  135. case 0x66:
  136. case 0x67:
  137. case 0x2e:
  138. case 0x3e:
  139. case 0x26:
  140. case 0x64:
  141. case 0x65:
  142. case 0x36:
  143. case 0xf0:
  144. case 0xf3:
  145. case 0xf2:
  146. ++insn;
  147. continue;
  148. }
  149. break;
  150. }
  151. /* Skip REX instruction prefix. */
  152. if ((*insn & 0xf0) == 0x40)
  153. ++insn;
  154. if (*insn == 0x0f) { /* Two-byte opcode. */
  155. ++insn;
  156. need_modrm = test_bit(*insn, twobyte_has_modrm);
  157. } else { /* One-byte opcode. */
  158. need_modrm = test_bit(*insn, onebyte_has_modrm);
  159. }
  160. if (need_modrm) {
  161. u8 modrm = *++insn;
  162. if ((modrm & 0xc7) == 0x05) { /* %rip+disp32 addressing mode */
  163. /* Displacement follows ModRM byte. */
  164. return (s32 *) ++insn;
  165. }
  166. }
  167. /* No %rip-relative addressing mode here. */
  168. return NULL;
  169. }
  170. static void __kprobes arch_copy_kprobe(struct kprobe *p)
  171. {
  172. s32 *ripdisp;
  173. memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE);
  174. ripdisp = is_riprel(p->ainsn.insn);
  175. if (ripdisp) {
  176. /*
  177. * The copied instruction uses the %rip-relative
  178. * addressing mode. Adjust the displacement for the
  179. * difference between the original location of this
  180. * instruction and the location of the copy that will
  181. * actually be run. The tricky bit here is making sure
  182. * that the sign extension happens correctly in this
  183. * calculation, since we need a signed 32-bit result to
  184. * be sign-extended to 64 bits when it's added to the
  185. * %rip value and yield the same 64-bit result that the
  186. * sign-extension of the original signed 32-bit
  187. * displacement would have given.
  188. */
  189. s64 disp = (u8 *) p->addr + *ripdisp - (u8 *) p->ainsn.insn;
  190. BUG_ON((s64) (s32) disp != disp); /* Sanity check. */
  191. *ripdisp = disp;
  192. }
  193. p->opcode = *p->addr;
  194. }
  195. void __kprobes arch_arm_kprobe(struct kprobe *p)
  196. {
  197. *p->addr = BREAKPOINT_INSTRUCTION;
  198. flush_icache_range((unsigned long) p->addr,
  199. (unsigned long) p->addr + sizeof(kprobe_opcode_t));
  200. }
  201. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  202. {
  203. *p->addr = p->opcode;
  204. flush_icache_range((unsigned long) p->addr,
  205. (unsigned long) p->addr + sizeof(kprobe_opcode_t));
  206. }
  207. void __kprobes arch_remove_kprobe(struct kprobe *p)
  208. {
  209. mutex_lock(&kprobe_mutex);
  210. free_insn_slot(p->ainsn.insn, 0);
  211. mutex_unlock(&kprobe_mutex);
  212. }
  213. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  214. {
  215. kcb->prev_kprobe.kp = kprobe_running();
  216. kcb->prev_kprobe.status = kcb->kprobe_status;
  217. kcb->prev_kprobe.old_rflags = kcb->kprobe_old_rflags;
  218. kcb->prev_kprobe.saved_rflags = kcb->kprobe_saved_rflags;
  219. }
  220. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  221. {
  222. __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
  223. kcb->kprobe_status = kcb->prev_kprobe.status;
  224. kcb->kprobe_old_rflags = kcb->prev_kprobe.old_rflags;
  225. kcb->kprobe_saved_rflags = kcb->prev_kprobe.saved_rflags;
  226. }
  227. static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  228. struct kprobe_ctlblk *kcb)
  229. {
  230. __get_cpu_var(current_kprobe) = p;
  231. kcb->kprobe_saved_rflags = kcb->kprobe_old_rflags
  232. = (regs->eflags & (TF_MASK | IF_MASK));
  233. if (is_IF_modifier(p->ainsn.insn))
  234. kcb->kprobe_saved_rflags &= ~IF_MASK;
  235. }
  236. static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  237. {
  238. regs->eflags |= TF_MASK;
  239. regs->eflags &= ~IF_MASK;
  240. /*single step inline if the instruction is an int3*/
  241. if (p->opcode == BREAKPOINT_INSTRUCTION)
  242. regs->rip = (unsigned long)p->addr;
  243. else
  244. regs->rip = (unsigned long)p->ainsn.insn;
  245. }
  246. /* Called with kretprobe_lock held */
  247. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  248. struct pt_regs *regs)
  249. {
  250. unsigned long *sara = (unsigned long *)regs->rsp;
  251. ri->ret_addr = (kprobe_opcode_t *) *sara;
  252. /* Replace the return addr with trampoline addr */
  253. *sara = (unsigned long) &kretprobe_trampoline;
  254. }
  255. int __kprobes kprobe_handler(struct pt_regs *regs)
  256. {
  257. struct kprobe *p;
  258. int ret = 0;
  259. kprobe_opcode_t *addr = (kprobe_opcode_t *)(regs->rip - sizeof(kprobe_opcode_t));
  260. struct kprobe_ctlblk *kcb;
  261. /*
  262. * We don't want to be preempted for the entire
  263. * duration of kprobe processing
  264. */
  265. preempt_disable();
  266. kcb = get_kprobe_ctlblk();
  267. /* Check we're not actually recursing */
  268. if (kprobe_running()) {
  269. p = get_kprobe(addr);
  270. if (p) {
  271. if (kcb->kprobe_status == KPROBE_HIT_SS &&
  272. *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
  273. regs->eflags &= ~TF_MASK;
  274. regs->eflags |= kcb->kprobe_saved_rflags;
  275. goto no_kprobe;
  276. } else if (kcb->kprobe_status == KPROBE_HIT_SSDONE) {
  277. /* TODO: Provide re-entrancy from
  278. * post_kprobes_handler() and avoid exception
  279. * stack corruption while single-stepping on
  280. * the instruction of the new probe.
  281. */
  282. arch_disarm_kprobe(p);
  283. regs->rip = (unsigned long)p->addr;
  284. reset_current_kprobe();
  285. ret = 1;
  286. } else {
  287. /* We have reentered the kprobe_handler(), since
  288. * another probe was hit while within the
  289. * handler. We here save the original kprobe
  290. * variables and just single step on instruction
  291. * of the new probe without calling any user
  292. * handlers.
  293. */
  294. save_previous_kprobe(kcb);
  295. set_current_kprobe(p, regs, kcb);
  296. kprobes_inc_nmissed_count(p);
  297. prepare_singlestep(p, regs);
  298. kcb->kprobe_status = KPROBE_REENTER;
  299. return 1;
  300. }
  301. } else {
  302. if (*addr != BREAKPOINT_INSTRUCTION) {
  303. /* The breakpoint instruction was removed by
  304. * another cpu right after we hit, no further
  305. * handling of this interrupt is appropriate
  306. */
  307. regs->rip = (unsigned long)addr;
  308. ret = 1;
  309. goto no_kprobe;
  310. }
  311. p = __get_cpu_var(current_kprobe);
  312. if (p->break_handler && p->break_handler(p, regs)) {
  313. goto ss_probe;
  314. }
  315. }
  316. goto no_kprobe;
  317. }
  318. p = get_kprobe(addr);
  319. if (!p) {
  320. if (*addr != BREAKPOINT_INSTRUCTION) {
  321. /*
  322. * The breakpoint instruction was removed right
  323. * after we hit it. Another cpu has removed
  324. * either a probepoint or a debugger breakpoint
  325. * at this address. In either case, no further
  326. * handling of this interrupt is appropriate.
  327. * Back up over the (now missing) int3 and run
  328. * the original instruction.
  329. */
  330. regs->rip = (unsigned long)addr;
  331. ret = 1;
  332. }
  333. /* Not one of ours: let kernel handle it */
  334. goto no_kprobe;
  335. }
  336. set_current_kprobe(p, regs, kcb);
  337. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  338. if (p->pre_handler && p->pre_handler(p, regs))
  339. /* handler has already set things up, so skip ss setup */
  340. return 1;
  341. ss_probe:
  342. prepare_singlestep(p, regs);
  343. kcb->kprobe_status = KPROBE_HIT_SS;
  344. return 1;
  345. no_kprobe:
  346. preempt_enable_no_resched();
  347. return ret;
  348. }
  349. /*
  350. * For function-return probes, init_kprobes() establishes a probepoint
  351. * here. When a retprobed function returns, this probe is hit and
  352. * trampoline_probe_handler() runs, calling the kretprobe's handler.
  353. */
  354. void kretprobe_trampoline_holder(void)
  355. {
  356. asm volatile ( ".global kretprobe_trampoline\n"
  357. "kretprobe_trampoline: \n"
  358. "nop\n");
  359. }
  360. /*
  361. * Called when we hit the probe point at kretprobe_trampoline
  362. */
  363. int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
  364. {
  365. struct kretprobe_instance *ri = NULL;
  366. struct hlist_head *head, empty_rp;
  367. struct hlist_node *node, *tmp;
  368. unsigned long flags, orig_ret_address = 0;
  369. unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
  370. INIT_HLIST_HEAD(&empty_rp);
  371. spin_lock_irqsave(&kretprobe_lock, flags);
  372. head = kretprobe_inst_table_head(current);
  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. ri->rp->handler(ri, regs);
  392. orig_ret_address = (unsigned long)ri->ret_addr;
  393. recycle_rp_inst(ri, &empty_rp);
  394. if (orig_ret_address != trampoline_address)
  395. /*
  396. * This is the real return address. Any other
  397. * instances associated with this task are for
  398. * other calls deeper on the call stack
  399. */
  400. break;
  401. }
  402. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  403. regs->rip = orig_ret_address;
  404. reset_current_kprobe();
  405. spin_unlock_irqrestore(&kretprobe_lock, flags);
  406. preempt_enable_no_resched();
  407. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  408. hlist_del(&ri->hlist);
  409. kfree(ri);
  410. }
  411. /*
  412. * By returning a non-zero value, we are telling
  413. * kprobe_handler() that we don't want the post_handler
  414. * to run (and have re-enabled preemption)
  415. */
  416. return 1;
  417. }
  418. /*
  419. * Called after single-stepping. p->addr is the address of the
  420. * instruction whose first byte has been replaced by the "int 3"
  421. * instruction. To avoid the SMP problems that can occur when we
  422. * temporarily put back the original opcode to single-step, we
  423. * single-stepped a copy of the instruction. The address of this
  424. * copy is p->ainsn.insn.
  425. *
  426. * This function prepares to return from the post-single-step
  427. * interrupt. We have to fix up the stack as follows:
  428. *
  429. * 0) Except in the case of absolute or indirect jump or call instructions,
  430. * the new rip is relative to the copied instruction. We need to make
  431. * it relative to the original instruction.
  432. *
  433. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  434. * flags are set in the just-pushed eflags, and may need to be cleared.
  435. *
  436. * 2) If the single-stepped instruction was a call, the return address
  437. * that is atop the stack is the address following the copied instruction.
  438. * We need to make it the address following the original instruction.
  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->rsp;
  444. unsigned long next_rip = 0;
  445. unsigned long copy_rip = (unsigned long)p->ainsn.insn;
  446. unsigned long orig_rip = (unsigned long)p->addr;
  447. kprobe_opcode_t *insn = p->ainsn.insn;
  448. /*skip the REX prefix*/
  449. if (*insn >= 0x40 && *insn <= 0x4f)
  450. insn++;
  451. switch (*insn) {
  452. case 0x9c: /* pushfl */
  453. *tos &= ~(TF_MASK | IF_MASK);
  454. *tos |= kcb->kprobe_old_rflags;
  455. break;
  456. case 0xc3: /* ret/lret */
  457. case 0xcb:
  458. case 0xc2:
  459. case 0xca:
  460. regs->eflags &= ~TF_MASK;
  461. /* rip is already adjusted, no more changes required*/
  462. return;
  463. case 0xe8: /* call relative - Fix return addr */
  464. *tos = orig_rip + (*tos - copy_rip);
  465. break;
  466. case 0xff:
  467. if ((insn[1] & 0x30) == 0x10) {
  468. /* call absolute, indirect */
  469. /* Fix return addr; rip is correct. */
  470. next_rip = regs->rip;
  471. *tos = orig_rip + (*tos - copy_rip);
  472. } else if (((insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
  473. ((insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
  474. /* rip is correct. */
  475. next_rip = regs->rip;
  476. }
  477. break;
  478. case 0xea: /* jmp absolute -- rip is correct */
  479. next_rip = regs->rip;
  480. break;
  481. default:
  482. break;
  483. }
  484. regs->eflags &= ~TF_MASK;
  485. if (next_rip) {
  486. regs->rip = next_rip;
  487. } else {
  488. regs->rip = orig_rip + (regs->rip - copy_rip);
  489. }
  490. }
  491. int __kprobes post_kprobe_handler(struct pt_regs *regs)
  492. {
  493. struct kprobe *cur = kprobe_running();
  494. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  495. if (!cur)
  496. return 0;
  497. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  498. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  499. cur->post_handler(cur, regs, 0);
  500. }
  501. resume_execution(cur, regs, kcb);
  502. regs->eflags |= kcb->kprobe_saved_rflags;
  503. /* Restore the original saved kprobes variables and continue. */
  504. if (kcb->kprobe_status == KPROBE_REENTER) {
  505. restore_previous_kprobe(kcb);
  506. goto out;
  507. }
  508. reset_current_kprobe();
  509. out:
  510. preempt_enable_no_resched();
  511. /*
  512. * if somebody else is singlestepping across a probe point, eflags
  513. * will have TF set, in which case, continue the remaining processing
  514. * of do_debug, as if this is not a probe hit.
  515. */
  516. if (regs->eflags & TF_MASK)
  517. return 0;
  518. return 1;
  519. }
  520. int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  521. {
  522. struct kprobe *cur = kprobe_running();
  523. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  524. const struct exception_table_entry *fixup;
  525. switch(kcb->kprobe_status) {
  526. case KPROBE_HIT_SS:
  527. case KPROBE_REENTER:
  528. /*
  529. * We are here because the instruction being single
  530. * stepped caused a page fault. We reset the current
  531. * kprobe and the rip points back to the probe address
  532. * and allow the page fault handler to continue as a
  533. * normal page fault.
  534. */
  535. regs->rip = (unsigned long)cur->addr;
  536. regs->eflags |= kcb->kprobe_old_rflags;
  537. if (kcb->kprobe_status == KPROBE_REENTER)
  538. restore_previous_kprobe(kcb);
  539. else
  540. reset_current_kprobe();
  541. preempt_enable_no_resched();
  542. break;
  543. case KPROBE_HIT_ACTIVE:
  544. case KPROBE_HIT_SSDONE:
  545. /*
  546. * We increment the nmissed count for accounting,
  547. * we can also use npre/npostfault count for accouting
  548. * these specific fault cases.
  549. */
  550. kprobes_inc_nmissed_count(cur);
  551. /*
  552. * We come here because instructions in the pre/post
  553. * handler caused the page_fault, this could happen
  554. * if handler tries to access user space by
  555. * copy_from_user(), get_user() etc. Let the
  556. * user-specified handler try to fix it first.
  557. */
  558. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  559. return 1;
  560. /*
  561. * In case the user-specified fault handler returned
  562. * zero, try to fix up.
  563. */
  564. fixup = search_exception_tables(regs->rip);
  565. if (fixup) {
  566. regs->rip = fixup->fixup;
  567. return 1;
  568. }
  569. /*
  570. * fixup() could not handle it,
  571. * Let do_page_fault() fix it.
  572. */
  573. break;
  574. default:
  575. break;
  576. }
  577. return 0;
  578. }
  579. /*
  580. * Wrapper routine for handling exceptions.
  581. */
  582. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  583. unsigned long val, void *data)
  584. {
  585. struct die_args *args = (struct die_args *)data;
  586. int ret = NOTIFY_DONE;
  587. if (args->regs && user_mode(args->regs))
  588. return ret;
  589. switch (val) {
  590. case DIE_INT3:
  591. if (kprobe_handler(args->regs))
  592. ret = NOTIFY_STOP;
  593. break;
  594. case DIE_DEBUG:
  595. if (post_kprobe_handler(args->regs))
  596. ret = NOTIFY_STOP;
  597. break;
  598. case DIE_GPF:
  599. case DIE_PAGE_FAULT:
  600. /* kprobe_running() needs smp_processor_id() */
  601. preempt_disable();
  602. if (kprobe_running() &&
  603. kprobe_fault_handler(args->regs, args->trapnr))
  604. ret = NOTIFY_STOP;
  605. preempt_enable();
  606. break;
  607. default:
  608. break;
  609. }
  610. return ret;
  611. }
  612. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  613. {
  614. struct jprobe *jp = container_of(p, struct jprobe, kp);
  615. unsigned long addr;
  616. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  617. kcb->jprobe_saved_regs = *regs;
  618. kcb->jprobe_saved_rsp = (long *) regs->rsp;
  619. addr = (unsigned long)(kcb->jprobe_saved_rsp);
  620. /*
  621. * As Linus pointed out, gcc assumes that the callee
  622. * owns the argument space and could overwrite it, e.g.
  623. * tailcall optimization. So, to be absolutely safe
  624. * we also save and restore enough stack bytes to cover
  625. * the argument area.
  626. */
  627. memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
  628. MIN_STACK_SIZE(addr));
  629. regs->eflags &= ~IF_MASK;
  630. regs->rip = (unsigned long)(jp->entry);
  631. return 1;
  632. }
  633. void __kprobes jprobe_return(void)
  634. {
  635. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  636. asm volatile (" xchg %%rbx,%%rsp \n"
  637. " int3 \n"
  638. " .globl jprobe_return_end \n"
  639. " jprobe_return_end: \n"
  640. " nop \n"::"b"
  641. (kcb->jprobe_saved_rsp):"memory");
  642. }
  643. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  644. {
  645. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  646. u8 *addr = (u8 *) (regs->rip - 1);
  647. unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_rsp);
  648. struct jprobe *jp = container_of(p, struct jprobe, kp);
  649. if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
  650. if ((long *)regs->rsp != kcb->jprobe_saved_rsp) {
  651. struct pt_regs *saved_regs =
  652. container_of(kcb->jprobe_saved_rsp,
  653. struct pt_regs, rsp);
  654. printk("current rsp %p does not match saved rsp %p\n",
  655. (long *)regs->rsp, kcb->jprobe_saved_rsp);
  656. printk("Saved registers for jprobe %p\n", jp);
  657. show_registers(saved_regs);
  658. printk("Current registers\n");
  659. show_registers(regs);
  660. BUG();
  661. }
  662. *regs = kcb->jprobe_saved_regs;
  663. memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
  664. MIN_STACK_SIZE(stack_addr));
  665. preempt_enable_no_resched();
  666. return 1;
  667. }
  668. return 0;
  669. }
  670. static struct kprobe trampoline_p = {
  671. .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
  672. .pre_handler = trampoline_probe_handler
  673. };
  674. int __init arch_init_kprobes(void)
  675. {
  676. return register_kprobe(&trampoline_p);
  677. }
  678. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  679. {
  680. if (p->addr == (kprobe_opcode_t *)&kretprobe_trampoline)
  681. return 1;
  682. return 0;
  683. }