kprobes_64.c 22 KB

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