kprobes.c 21 KB

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