kprobes.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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/spinlock.h>
  37. #include <linux/string.h>
  38. #include <linux/slab.h>
  39. #include <linux/preempt.h>
  40. #include <asm/cacheflush.h>
  41. #include <asm/pgtable.h>
  42. #include <asm/kdebug.h>
  43. static DECLARE_MUTEX(kprobe_mutex);
  44. static struct kprobe *current_kprobe;
  45. static unsigned long kprobe_status, kprobe_old_rflags, kprobe_saved_rflags;
  46. static struct kprobe *kprobe_prev;
  47. static unsigned long kprobe_status_prev, kprobe_old_rflags_prev, kprobe_saved_rflags_prev;
  48. static struct pt_regs jprobe_saved_regs;
  49. static long *jprobe_saved_rsp;
  50. void jprobe_return_end(void);
  51. /* copy of the kernel stack at the probe fire time */
  52. static kprobe_opcode_t jprobes_stack[MAX_STACK_SIZE];
  53. /*
  54. * returns non-zero if opcode modifies the interrupt flag.
  55. */
  56. static inline int is_IF_modifier(kprobe_opcode_t *insn)
  57. {
  58. switch (*insn) {
  59. case 0xfa: /* cli */
  60. case 0xfb: /* sti */
  61. case 0xcf: /* iret/iretd */
  62. case 0x9d: /* popf/popfd */
  63. return 1;
  64. }
  65. if (*insn >= 0x40 && *insn <= 0x4f && *++insn == 0xcf)
  66. return 1;
  67. return 0;
  68. }
  69. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  70. {
  71. /* insn: must be on special executable page on x86_64. */
  72. up(&kprobe_mutex);
  73. p->ainsn.insn = get_insn_slot();
  74. down(&kprobe_mutex);
  75. if (!p->ainsn.insn) {
  76. return -ENOMEM;
  77. }
  78. return 0;
  79. }
  80. /*
  81. * Determine if the instruction uses the %rip-relative addressing mode.
  82. * If it does, return the address of the 32-bit displacement word.
  83. * If not, return null.
  84. */
  85. static inline s32 *is_riprel(u8 *insn)
  86. {
  87. #define W(row,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb,bc,bd,be,bf) \
  88. (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
  89. (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
  90. (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
  91. (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
  92. << (row % 64))
  93. static const u64 onebyte_has_modrm[256 / 64] = {
  94. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  95. /* ------------------------------- */
  96. W(0x00, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 00 */
  97. W(0x10, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 10 */
  98. W(0x20, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 20 */
  99. W(0x30, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0), /* 30 */
  100. W(0x40, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 40 */
  101. W(0x50, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 50 */
  102. W(0x60, 0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0)| /* 60 */
  103. W(0x70, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 70 */
  104. W(0x80, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 80 */
  105. W(0x90, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 90 */
  106. W(0xa0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* a0 */
  107. W(0xb0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* b0 */
  108. W(0xc0, 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0)| /* c0 */
  109. W(0xd0, 1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1)| /* d0 */
  110. W(0xe0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* e0 */
  111. W(0xf0, 0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1) /* f0 */
  112. /* ------------------------------- */
  113. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  114. };
  115. static const u64 twobyte_has_modrm[256 / 64] = {
  116. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  117. /* ------------------------------- */
  118. W(0x00, 1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,1)| /* 0f */
  119. W(0x10, 1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0)| /* 1f */
  120. W(0x20, 1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1)| /* 2f */
  121. W(0x30, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 3f */
  122. W(0x40, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 4f */
  123. W(0x50, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 5f */
  124. W(0x60, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 6f */
  125. W(0x70, 1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1), /* 7f */
  126. W(0x80, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 8f */
  127. W(0x90, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 9f */
  128. W(0xa0, 0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1)| /* af */
  129. W(0xb0, 1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1), /* bf */
  130. W(0xc0, 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0)| /* cf */
  131. W(0xd0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* df */
  132. W(0xe0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* ef */
  133. W(0xf0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0) /* ff */
  134. /* ------------------------------- */
  135. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  136. };
  137. #undef W
  138. int need_modrm;
  139. /* Skip legacy instruction prefixes. */
  140. while (1) {
  141. switch (*insn) {
  142. case 0x66:
  143. case 0x67:
  144. case 0x2e:
  145. case 0x3e:
  146. case 0x26:
  147. case 0x64:
  148. case 0x65:
  149. case 0x36:
  150. case 0xf0:
  151. case 0xf3:
  152. case 0xf2:
  153. ++insn;
  154. continue;
  155. }
  156. break;
  157. }
  158. /* Skip REX instruction prefix. */
  159. if ((*insn & 0xf0) == 0x40)
  160. ++insn;
  161. if (*insn == 0x0f) { /* Two-byte opcode. */
  162. ++insn;
  163. need_modrm = test_bit(*insn, twobyte_has_modrm);
  164. } else { /* One-byte opcode. */
  165. need_modrm = test_bit(*insn, onebyte_has_modrm);
  166. }
  167. if (need_modrm) {
  168. u8 modrm = *++insn;
  169. if ((modrm & 0xc7) == 0x05) { /* %rip+disp32 addressing mode */
  170. /* Displacement follows ModRM byte. */
  171. return (s32 *) ++insn;
  172. }
  173. }
  174. /* No %rip-relative addressing mode here. */
  175. return NULL;
  176. }
  177. void __kprobes arch_copy_kprobe(struct kprobe *p)
  178. {
  179. s32 *ripdisp;
  180. memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE);
  181. ripdisp = is_riprel(p->ainsn.insn);
  182. if (ripdisp) {
  183. /*
  184. * The copied instruction uses the %rip-relative
  185. * addressing mode. Adjust the displacement for the
  186. * difference between the original location of this
  187. * instruction and the location of the copy that will
  188. * actually be run. The tricky bit here is making sure
  189. * that the sign extension happens correctly in this
  190. * calculation, since we need a signed 32-bit result to
  191. * be sign-extended to 64 bits when it's added to the
  192. * %rip value and yield the same 64-bit result that the
  193. * sign-extension of the original signed 32-bit
  194. * displacement would have given.
  195. */
  196. s64 disp = (u8 *) p->addr + *ripdisp - (u8 *) p->ainsn.insn;
  197. BUG_ON((s64) (s32) disp != disp); /* Sanity check. */
  198. *ripdisp = disp;
  199. }
  200. p->opcode = *p->addr;
  201. }
  202. void __kprobes arch_arm_kprobe(struct kprobe *p)
  203. {
  204. *p->addr = BREAKPOINT_INSTRUCTION;
  205. flush_icache_range((unsigned long) p->addr,
  206. (unsigned long) p->addr + sizeof(kprobe_opcode_t));
  207. }
  208. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  209. {
  210. *p->addr = p->opcode;
  211. flush_icache_range((unsigned long) p->addr,
  212. (unsigned long) p->addr + sizeof(kprobe_opcode_t));
  213. }
  214. void __kprobes arch_remove_kprobe(struct kprobe *p)
  215. {
  216. up(&kprobe_mutex);
  217. free_insn_slot(p->ainsn.insn);
  218. down(&kprobe_mutex);
  219. }
  220. static inline void save_previous_kprobe(void)
  221. {
  222. kprobe_prev = current_kprobe;
  223. kprobe_status_prev = kprobe_status;
  224. kprobe_old_rflags_prev = kprobe_old_rflags;
  225. kprobe_saved_rflags_prev = kprobe_saved_rflags;
  226. }
  227. static inline void restore_previous_kprobe(void)
  228. {
  229. current_kprobe = kprobe_prev;
  230. kprobe_status = kprobe_status_prev;
  231. kprobe_old_rflags = kprobe_old_rflags_prev;
  232. kprobe_saved_rflags = kprobe_saved_rflags_prev;
  233. }
  234. static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs)
  235. {
  236. current_kprobe = p;
  237. kprobe_saved_rflags = kprobe_old_rflags
  238. = (regs->eflags & (TF_MASK | IF_MASK));
  239. if (is_IF_modifier(p->ainsn.insn))
  240. kprobe_saved_rflags &= ~IF_MASK;
  241. }
  242. static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  243. {
  244. regs->eflags |= TF_MASK;
  245. regs->eflags &= ~IF_MASK;
  246. /*single step inline if the instruction is an int3*/
  247. if (p->opcode == BREAKPOINT_INSTRUCTION)
  248. regs->rip = (unsigned long)p->addr;
  249. else
  250. regs->rip = (unsigned long)p->ainsn.insn;
  251. }
  252. void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
  253. struct pt_regs *regs)
  254. {
  255. unsigned long *sara = (unsigned long *)regs->rsp;
  256. struct kretprobe_instance *ri;
  257. if ((ri = get_free_rp_inst(rp)) != NULL) {
  258. ri->rp = rp;
  259. ri->task = current;
  260. ri->ret_addr = (kprobe_opcode_t *) *sara;
  261. /* Replace the return addr with trampoline addr */
  262. *sara = (unsigned long) &kretprobe_trampoline;
  263. add_rp_inst(ri);
  264. } else {
  265. rp->nmissed++;
  266. }
  267. }
  268. /*
  269. * Interrupts are disabled on entry as trap3 is an interrupt gate and they
  270. * remain disabled thorough out this function.
  271. */
  272. int __kprobes kprobe_handler(struct pt_regs *regs)
  273. {
  274. struct kprobe *p;
  275. int ret = 0;
  276. kprobe_opcode_t *addr = (kprobe_opcode_t *)(regs->rip - sizeof(kprobe_opcode_t));
  277. /* We're in an interrupt, but this is clear and BUG()-safe. */
  278. preempt_disable();
  279. /* Check we're not actually recursing */
  280. if (kprobe_running()) {
  281. /* We *are* holding lock here, so this is safe.
  282. Disarm the probe we just hit, and ignore it. */
  283. p = get_kprobe(addr);
  284. if (p) {
  285. if (kprobe_status == KPROBE_HIT_SS) {
  286. regs->eflags &= ~TF_MASK;
  287. regs->eflags |= kprobe_saved_rflags;
  288. unlock_kprobes();
  289. goto no_kprobe;
  290. } else if (kprobe_status == KPROBE_HIT_SSDONE) {
  291. /* TODO: Provide re-entrancy from
  292. * post_kprobes_handler() and avoid exception
  293. * stack corruption while single-stepping on
  294. * the instruction of the new probe.
  295. */
  296. arch_disarm_kprobe(p);
  297. regs->rip = (unsigned long)p->addr;
  298. ret = 1;
  299. } else {
  300. /* We have reentered the kprobe_handler(), since
  301. * another probe was hit while within the
  302. * handler. We here save the original kprobe
  303. * variables and just single step on instruction
  304. * of the new probe without calling any user
  305. * handlers.
  306. */
  307. save_previous_kprobe();
  308. set_current_kprobe(p, regs);
  309. p->nmissed++;
  310. prepare_singlestep(p, regs);
  311. kprobe_status = KPROBE_REENTER;
  312. return 1;
  313. }
  314. } else {
  315. p = current_kprobe;
  316. if (p->break_handler && p->break_handler(p, regs)) {
  317. goto ss_probe;
  318. }
  319. }
  320. /* If it's not ours, can't be delete race, (we hold lock). */
  321. goto no_kprobe;
  322. }
  323. lock_kprobes();
  324. p = get_kprobe(addr);
  325. if (!p) {
  326. unlock_kprobes();
  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. kprobe_status = KPROBE_HIT_ACTIVE;
  344. set_current_kprobe(p, regs);
  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. 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 orig_ret_address = 0;
  376. unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
  377. head = kretprobe_inst_table_head(current);
  378. /*
  379. * It is possible to have multiple instances associated with a given
  380. * task either because an multiple functions in the call path
  381. * have a return probe installed on them, and/or more then one return
  382. * return probe was registered for a target function.
  383. *
  384. * We can handle this because:
  385. * - instances are always inserted at the head of the list
  386. * - when multiple return probes are registered for the same
  387. * function, the first instance's ret_addr will point to the
  388. * real return address, and all the rest will point to
  389. * kretprobe_trampoline
  390. */
  391. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  392. if (ri->task != current)
  393. /* another task is sharing our hash bucket */
  394. continue;
  395. if (ri->rp && ri->rp->handler)
  396. ri->rp->handler(ri, regs);
  397. orig_ret_address = (unsigned long)ri->ret_addr;
  398. recycle_rp_inst(ri);
  399. if (orig_ret_address != trampoline_address)
  400. /*
  401. * This is the real return address. Any other
  402. * instances associated with this task are for
  403. * other calls deeper on the call stack
  404. */
  405. break;
  406. }
  407. BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
  408. regs->rip = orig_ret_address;
  409. unlock_kprobes();
  410. preempt_enable_no_resched();
  411. /*
  412. * By returning a non-zero value, we are telling
  413. * kprobe_handler() that we have handled unlocking
  414. * and re-enabling 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, struct pt_regs *regs)
  441. {
  442. unsigned long *tos = (unsigned long *)regs->rsp;
  443. unsigned long next_rip = 0;
  444. unsigned long copy_rip = (unsigned long)p->ainsn.insn;
  445. unsigned long orig_rip = (unsigned long)p->addr;
  446. kprobe_opcode_t *insn = p->ainsn.insn;
  447. /*skip the REX prefix*/
  448. if (*insn >= 0x40 && *insn <= 0x4f)
  449. insn++;
  450. switch (*insn) {
  451. case 0x9c: /* pushfl */
  452. *tos &= ~(TF_MASK | IF_MASK);
  453. *tos |= kprobe_old_rflags;
  454. break;
  455. case 0xc3: /* ret/lret */
  456. case 0xcb:
  457. case 0xc2:
  458. case 0xca:
  459. regs->eflags &= ~TF_MASK;
  460. /* rip is already adjusted, no more changes required*/
  461. return;
  462. case 0xe8: /* call relative - Fix return addr */
  463. *tos = orig_rip + (*tos - copy_rip);
  464. break;
  465. case 0xff:
  466. if ((*insn & 0x30) == 0x10) {
  467. /* call absolute, indirect */
  468. /* Fix return addr; rip is correct. */
  469. next_rip = regs->rip;
  470. *tos = orig_rip + (*tos - copy_rip);
  471. } else if (((*insn & 0x31) == 0x20) || /* jmp near, absolute indirect */
  472. ((*insn & 0x31) == 0x21)) { /* jmp far, absolute indirect */
  473. /* rip is correct. */
  474. next_rip = regs->rip;
  475. }
  476. break;
  477. case 0xea: /* jmp absolute -- rip is correct */
  478. next_rip = regs->rip;
  479. break;
  480. default:
  481. break;
  482. }
  483. regs->eflags &= ~TF_MASK;
  484. if (next_rip) {
  485. regs->rip = next_rip;
  486. } else {
  487. regs->rip = orig_rip + (regs->rip - copy_rip);
  488. }
  489. }
  490. /*
  491. * Interrupts are disabled on entry as trap1 is an interrupt gate and they
  492. * remain disabled thoroughout this function. And we hold kprobe lock.
  493. */
  494. int __kprobes post_kprobe_handler(struct pt_regs *regs)
  495. {
  496. if (!kprobe_running())
  497. return 0;
  498. if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) {
  499. kprobe_status = KPROBE_HIT_SSDONE;
  500. current_kprobe->post_handler(current_kprobe, regs, 0);
  501. }
  502. resume_execution(current_kprobe, regs);
  503. regs->eflags |= kprobe_saved_rflags;
  504. /* Restore the original saved kprobes variables and continue. */
  505. if (kprobe_status == KPROBE_REENTER) {
  506. restore_previous_kprobe();
  507. goto out;
  508. } else {
  509. unlock_kprobes();
  510. }
  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. /* Interrupts disabled, kprobe_lock held. */
  523. int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  524. {
  525. if (current_kprobe->fault_handler
  526. && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
  527. return 1;
  528. if (kprobe_status & KPROBE_HIT_SS) {
  529. resume_execution(current_kprobe, regs);
  530. regs->eflags |= kprobe_old_rflags;
  531. unlock_kprobes();
  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. switch (val) {
  544. case DIE_INT3:
  545. if (kprobe_handler(args->regs))
  546. return NOTIFY_STOP;
  547. break;
  548. case DIE_DEBUG:
  549. if (post_kprobe_handler(args->regs))
  550. return NOTIFY_STOP;
  551. break;
  552. case DIE_GPF:
  553. if (kprobe_running() &&
  554. kprobe_fault_handler(args->regs, args->trapnr))
  555. return NOTIFY_STOP;
  556. break;
  557. case DIE_PAGE_FAULT:
  558. if (kprobe_running() &&
  559. kprobe_fault_handler(args->regs, args->trapnr))
  560. return NOTIFY_STOP;
  561. break;
  562. default:
  563. break;
  564. }
  565. return NOTIFY_DONE;
  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. jprobe_saved_regs = *regs;
  572. jprobe_saved_rsp = (long *) regs->rsp;
  573. addr = (unsigned long)jprobe_saved_rsp;
  574. /*
  575. * As Linus pointed out, gcc assumes that the callee
  576. * owns the argument space and could overwrite it, e.g.
  577. * tailcall optimization. So, to be absolutely safe
  578. * we also save and restore enough stack bytes to cover
  579. * the argument area.
  580. */
  581. memcpy(jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE(addr));
  582. regs->eflags &= ~IF_MASK;
  583. regs->rip = (unsigned long)(jp->entry);
  584. return 1;
  585. }
  586. void __kprobes jprobe_return(void)
  587. {
  588. preempt_enable_no_resched();
  589. asm volatile (" xchg %%rbx,%%rsp \n"
  590. " int3 \n"
  591. " .globl jprobe_return_end \n"
  592. " jprobe_return_end: \n"
  593. " nop \n"::"b"
  594. (jprobe_saved_rsp):"memory");
  595. }
  596. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  597. {
  598. u8 *addr = (u8 *) (regs->rip - 1);
  599. unsigned long stack_addr = (unsigned long)jprobe_saved_rsp;
  600. struct jprobe *jp = container_of(p, struct jprobe, kp);
  601. if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
  602. if ((long *)regs->rsp != jprobe_saved_rsp) {
  603. struct pt_regs *saved_regs =
  604. container_of(jprobe_saved_rsp, struct pt_regs, rsp);
  605. printk("current rsp %p does not match saved rsp %p\n",
  606. (long *)regs->rsp, jprobe_saved_rsp);
  607. printk("Saved registers for jprobe %p\n", jp);
  608. show_registers(saved_regs);
  609. printk("Current registers\n");
  610. show_registers(regs);
  611. BUG();
  612. }
  613. *regs = jprobe_saved_regs;
  614. memcpy((kprobe_opcode_t *) stack_addr, jprobes_stack,
  615. MIN_STACK_SIZE(stack_addr));
  616. return 1;
  617. }
  618. return 0;
  619. }
  620. static struct kprobe trampoline_p = {
  621. .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
  622. .pre_handler = trampoline_probe_handler
  623. };
  624. int __init arch_init_kprobes(void)
  625. {
  626. return register_kprobe(&trampoline_p);
  627. }