kprobes.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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 <linux/moduleloader.h>
  41. #include <asm/cacheflush.h>
  42. #include <asm/pgtable.h>
  43. #include <asm/kdebug.h>
  44. static DECLARE_MUTEX(kprobe_mutex);
  45. static struct kprobe *current_kprobe;
  46. static unsigned long kprobe_status, kprobe_old_rflags, kprobe_saved_rflags;
  47. static struct kprobe *kprobe_prev;
  48. static unsigned long kprobe_status_prev, kprobe_old_rflags_prev, kprobe_saved_rflags_prev;
  49. static struct pt_regs jprobe_saved_regs;
  50. static long *jprobe_saved_rsp;
  51. static kprobe_opcode_t *get_insn_slot(void);
  52. static void free_insn_slot(kprobe_opcode_t *slot);
  53. void jprobe_return_end(void);
  54. /* copy of the kernel stack at the probe fire time */
  55. static kprobe_opcode_t jprobes_stack[MAX_STACK_SIZE];
  56. /*
  57. * returns non-zero if opcode modifies the interrupt flag.
  58. */
  59. static inline int is_IF_modifier(kprobe_opcode_t *insn)
  60. {
  61. switch (*insn) {
  62. case 0xfa: /* cli */
  63. case 0xfb: /* sti */
  64. case 0xcf: /* iret/iretd */
  65. case 0x9d: /* popf/popfd */
  66. return 1;
  67. }
  68. if (*insn >= 0x40 && *insn <= 0x4f && *++insn == 0xcf)
  69. return 1;
  70. return 0;
  71. }
  72. int arch_prepare_kprobe(struct kprobe *p)
  73. {
  74. /* insn: must be on special executable page on x86_64. */
  75. up(&kprobe_mutex);
  76. p->ainsn.insn = get_insn_slot();
  77. down(&kprobe_mutex);
  78. if (!p->ainsn.insn) {
  79. return -ENOMEM;
  80. }
  81. return 0;
  82. }
  83. /*
  84. * Determine if the instruction uses the %rip-relative addressing mode.
  85. * If it does, return the address of the 32-bit displacement word.
  86. * If not, return null.
  87. */
  88. static inline s32 *is_riprel(u8 *insn)
  89. {
  90. #define W(row,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb,bc,bd,be,bf) \
  91. (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
  92. (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
  93. (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
  94. (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
  95. << (row % 64))
  96. static const u64 onebyte_has_modrm[256 / 64] = {
  97. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  98. /* ------------------------------- */
  99. W(0x00, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 00 */
  100. W(0x10, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 10 */
  101. W(0x20, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 20 */
  102. W(0x30, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0), /* 30 */
  103. W(0x40, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 40 */
  104. W(0x50, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 50 */
  105. W(0x60, 0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0)| /* 60 */
  106. W(0x70, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 70 */
  107. W(0x80, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 80 */
  108. W(0x90, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 90 */
  109. W(0xa0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* a0 */
  110. W(0xb0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* b0 */
  111. W(0xc0, 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0)| /* c0 */
  112. W(0xd0, 1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1)| /* d0 */
  113. W(0xe0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* e0 */
  114. W(0xf0, 0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1) /* f0 */
  115. /* ------------------------------- */
  116. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  117. };
  118. static const u64 twobyte_has_modrm[256 / 64] = {
  119. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  120. /* ------------------------------- */
  121. W(0x00, 1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,1)| /* 0f */
  122. W(0x10, 1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0)| /* 1f */
  123. W(0x20, 1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1)| /* 2f */
  124. W(0x30, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 3f */
  125. W(0x40, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 4f */
  126. W(0x50, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 5f */
  127. W(0x60, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 6f */
  128. W(0x70, 1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1), /* 7f */
  129. W(0x80, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 8f */
  130. W(0x90, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 9f */
  131. W(0xa0, 0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1)| /* af */
  132. W(0xb0, 1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1), /* bf */
  133. W(0xc0, 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0)| /* cf */
  134. W(0xd0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* df */
  135. W(0xe0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* ef */
  136. W(0xf0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0) /* ff */
  137. /* ------------------------------- */
  138. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  139. };
  140. #undef W
  141. int need_modrm;
  142. /* Skip legacy instruction prefixes. */
  143. while (1) {
  144. switch (*insn) {
  145. case 0x66:
  146. case 0x67:
  147. case 0x2e:
  148. case 0x3e:
  149. case 0x26:
  150. case 0x64:
  151. case 0x65:
  152. case 0x36:
  153. case 0xf0:
  154. case 0xf3:
  155. case 0xf2:
  156. ++insn;
  157. continue;
  158. }
  159. break;
  160. }
  161. /* Skip REX instruction prefix. */
  162. if ((*insn & 0xf0) == 0x40)
  163. ++insn;
  164. if (*insn == 0x0f) { /* Two-byte opcode. */
  165. ++insn;
  166. need_modrm = test_bit(*insn, twobyte_has_modrm);
  167. } else { /* One-byte opcode. */
  168. need_modrm = test_bit(*insn, onebyte_has_modrm);
  169. }
  170. if (need_modrm) {
  171. u8 modrm = *++insn;
  172. if ((modrm & 0xc7) == 0x05) { /* %rip+disp32 addressing mode */
  173. /* Displacement follows ModRM byte. */
  174. return (s32 *) ++insn;
  175. }
  176. }
  177. /* No %rip-relative addressing mode here. */
  178. return NULL;
  179. }
  180. void arch_copy_kprobe(struct kprobe *p)
  181. {
  182. s32 *ripdisp;
  183. memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE);
  184. ripdisp = is_riprel(p->ainsn.insn);
  185. if (ripdisp) {
  186. /*
  187. * The copied instruction uses the %rip-relative
  188. * addressing mode. Adjust the displacement for the
  189. * difference between the original location of this
  190. * instruction and the location of the copy that will
  191. * actually be run. The tricky bit here is making sure
  192. * that the sign extension happens correctly in this
  193. * calculation, since we need a signed 32-bit result to
  194. * be sign-extended to 64 bits when it's added to the
  195. * %rip value and yield the same 64-bit result that the
  196. * sign-extension of the original signed 32-bit
  197. * displacement would have given.
  198. */
  199. s64 disp = (u8 *) p->addr + *ripdisp - (u8 *) p->ainsn.insn;
  200. BUG_ON((s64) (s32) disp != disp); /* Sanity check. */
  201. *ripdisp = disp;
  202. }
  203. p->opcode = *p->addr;
  204. }
  205. void arch_arm_kprobe(struct kprobe *p)
  206. {
  207. *p->addr = BREAKPOINT_INSTRUCTION;
  208. flush_icache_range((unsigned long) p->addr,
  209. (unsigned long) p->addr + sizeof(kprobe_opcode_t));
  210. }
  211. void arch_disarm_kprobe(struct kprobe *p)
  212. {
  213. *p->addr = p->opcode;
  214. flush_icache_range((unsigned long) p->addr,
  215. (unsigned long) p->addr + sizeof(kprobe_opcode_t));
  216. }
  217. void arch_remove_kprobe(struct kprobe *p)
  218. {
  219. up(&kprobe_mutex);
  220. free_insn_slot(p->ainsn.insn);
  221. down(&kprobe_mutex);
  222. }
  223. static inline void save_previous_kprobe(void)
  224. {
  225. kprobe_prev = current_kprobe;
  226. kprobe_status_prev = kprobe_status;
  227. kprobe_old_rflags_prev = kprobe_old_rflags;
  228. kprobe_saved_rflags_prev = kprobe_saved_rflags;
  229. }
  230. static inline void restore_previous_kprobe(void)
  231. {
  232. current_kprobe = kprobe_prev;
  233. kprobe_status = kprobe_status_prev;
  234. kprobe_old_rflags = kprobe_old_rflags_prev;
  235. kprobe_saved_rflags = kprobe_saved_rflags_prev;
  236. }
  237. static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs)
  238. {
  239. current_kprobe = p;
  240. kprobe_saved_rflags = kprobe_old_rflags
  241. = (regs->eflags & (TF_MASK | IF_MASK));
  242. if (is_IF_modifier(p->ainsn.insn))
  243. kprobe_saved_rflags &= ~IF_MASK;
  244. }
  245. static void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  246. {
  247. regs->eflags |= TF_MASK;
  248. regs->eflags &= ~IF_MASK;
  249. /*single step inline if the instruction is an int3*/
  250. if (p->opcode == BREAKPOINT_INSTRUCTION)
  251. regs->rip = (unsigned long)p->addr;
  252. else
  253. regs->rip = (unsigned long)p->ainsn.insn;
  254. }
  255. struct task_struct *arch_get_kprobe_task(void *ptr)
  256. {
  257. return ((struct thread_info *) (((unsigned long) ptr) &
  258. (~(THREAD_SIZE -1))))->task;
  259. }
  260. void arch_prepare_kretprobe(struct kretprobe *rp, struct pt_regs *regs)
  261. {
  262. unsigned long *sara = (unsigned long *)regs->rsp;
  263. struct kretprobe_instance *ri;
  264. static void *orig_ret_addr;
  265. /*
  266. * Save the return address when the return probe hits
  267. * the first time, and use it to populate the (krprobe
  268. * instance)->ret_addr for subsequent return probes at
  269. * the same addrress since stack address would have
  270. * the kretprobe_trampoline by then.
  271. */
  272. if (((void*) *sara) != kretprobe_trampoline)
  273. orig_ret_addr = (void*) *sara;
  274. if ((ri = get_free_rp_inst(rp)) != NULL) {
  275. ri->rp = rp;
  276. ri->stack_addr = sara;
  277. ri->ret_addr = orig_ret_addr;
  278. add_rp_inst(ri);
  279. /* Replace the return addr with trampoline addr */
  280. *sara = (unsigned long) &kretprobe_trampoline;
  281. } else {
  282. rp->nmissed++;
  283. }
  284. }
  285. void arch_kprobe_flush_task(struct task_struct *tk)
  286. {
  287. struct kretprobe_instance *ri;
  288. while ((ri = get_rp_inst_tsk(tk)) != NULL) {
  289. *((unsigned long *)(ri->stack_addr)) =
  290. (unsigned long) ri->ret_addr;
  291. recycle_rp_inst(ri);
  292. }
  293. }
  294. /*
  295. * Interrupts are disabled on entry as trap3 is an interrupt gate and they
  296. * remain disabled thorough out this function.
  297. */
  298. int kprobe_handler(struct pt_regs *regs)
  299. {
  300. struct kprobe *p;
  301. int ret = 0;
  302. kprobe_opcode_t *addr = (kprobe_opcode_t *)(regs->rip - sizeof(kprobe_opcode_t));
  303. /* We're in an interrupt, but this is clear and BUG()-safe. */
  304. preempt_disable();
  305. /* Check we're not actually recursing */
  306. if (kprobe_running()) {
  307. /* We *are* holding lock here, so this is safe.
  308. Disarm the probe we just hit, and ignore it. */
  309. p = get_kprobe(addr);
  310. if (p) {
  311. if (kprobe_status == KPROBE_HIT_SS) {
  312. regs->eflags &= ~TF_MASK;
  313. regs->eflags |= kprobe_saved_rflags;
  314. unlock_kprobes();
  315. goto no_kprobe;
  316. } else if (kprobe_status == KPROBE_HIT_SSDONE) {
  317. /* TODO: Provide re-entrancy from
  318. * post_kprobes_handler() and avoid exception
  319. * stack corruption while single-stepping on
  320. * the instruction of the new probe.
  321. */
  322. arch_disarm_kprobe(p);
  323. regs->rip = (unsigned long)p->addr;
  324. ret = 1;
  325. } else {
  326. /* We have reentered the kprobe_handler(), since
  327. * another probe was hit while within the
  328. * handler. We here save the original kprobe
  329. * variables and just single step on instruction
  330. * of the new probe without calling any user
  331. * handlers.
  332. */
  333. save_previous_kprobe();
  334. set_current_kprobe(p, regs);
  335. p->nmissed++;
  336. prepare_singlestep(p, regs);
  337. kprobe_status = KPROBE_REENTER;
  338. return 1;
  339. }
  340. } else {
  341. p = current_kprobe;
  342. if (p->break_handler && p->break_handler(p, regs)) {
  343. goto ss_probe;
  344. }
  345. }
  346. /* If it's not ours, can't be delete race, (we hold lock). */
  347. goto no_kprobe;
  348. }
  349. lock_kprobes();
  350. p = get_kprobe(addr);
  351. if (!p) {
  352. unlock_kprobes();
  353. if (*addr != BREAKPOINT_INSTRUCTION) {
  354. /*
  355. * The breakpoint instruction was removed right
  356. * after we hit it. Another cpu has removed
  357. * either a probepoint or a debugger breakpoint
  358. * at this address. In either case, no further
  359. * handling of this interrupt is appropriate.
  360. */
  361. ret = 1;
  362. }
  363. /* Not one of ours: let kernel handle it */
  364. goto no_kprobe;
  365. }
  366. kprobe_status = KPROBE_HIT_ACTIVE;
  367. set_current_kprobe(p, regs);
  368. if (p->pre_handler && p->pre_handler(p, regs))
  369. /* handler has already set things up, so skip ss setup */
  370. return 1;
  371. ss_probe:
  372. prepare_singlestep(p, regs);
  373. kprobe_status = KPROBE_HIT_SS;
  374. return 1;
  375. no_kprobe:
  376. preempt_enable_no_resched();
  377. return ret;
  378. }
  379. /*
  380. * For function-return probes, init_kprobes() establishes a probepoint
  381. * here. When a retprobed function returns, this probe is hit and
  382. * trampoline_probe_handler() runs, calling the kretprobe's handler.
  383. */
  384. void kretprobe_trampoline_holder(void)
  385. {
  386. asm volatile ( ".global kretprobe_trampoline\n"
  387. "kretprobe_trampoline: \n"
  388. "nop\n");
  389. }
  390. /*
  391. * Called when we hit the probe point at kretprobe_trampoline
  392. */
  393. int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
  394. {
  395. struct task_struct *tsk;
  396. struct kretprobe_instance *ri;
  397. struct hlist_head *head;
  398. struct hlist_node *node;
  399. unsigned long *sara = (unsigned long *)regs->rsp - 1;
  400. tsk = arch_get_kprobe_task(sara);
  401. head = kretprobe_inst_table_head(tsk);
  402. hlist_for_each_entry(ri, node, head, hlist) {
  403. if (ri->stack_addr == sara && ri->rp) {
  404. if (ri->rp->handler)
  405. ri->rp->handler(ri, regs);
  406. }
  407. }
  408. return 0;
  409. }
  410. void trampoline_post_handler(struct kprobe *p, struct pt_regs *regs,
  411. unsigned long flags)
  412. {
  413. struct kretprobe_instance *ri;
  414. /* RA already popped */
  415. unsigned long *sara = ((unsigned long *)regs->rsp) - 1;
  416. while ((ri = get_rp_inst(sara))) {
  417. regs->rip = (unsigned long)ri->ret_addr;
  418. recycle_rp_inst(ri);
  419. }
  420. regs->eflags &= ~TF_MASK;
  421. }
  422. /*
  423. * Called after single-stepping. p->addr is the address of the
  424. * instruction whose first byte has been replaced by the "int 3"
  425. * instruction. To avoid the SMP problems that can occur when we
  426. * temporarily put back the original opcode to single-step, we
  427. * single-stepped a copy of the instruction. The address of this
  428. * copy is p->ainsn.insn.
  429. *
  430. * This function prepares to return from the post-single-step
  431. * interrupt. We have to fix up the stack as follows:
  432. *
  433. * 0) Except in the case of absolute or indirect jump or call instructions,
  434. * the new rip is relative to the copied instruction. We need to make
  435. * it relative to the original instruction.
  436. *
  437. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  438. * flags are set in the just-pushed eflags, and may need to be cleared.
  439. *
  440. * 2) If the single-stepped instruction was a call, the return address
  441. * that is atop the stack is the address following the copied instruction.
  442. * We need to make it the address following the original instruction.
  443. */
  444. static void resume_execution(struct kprobe *p, struct pt_regs *regs)
  445. {
  446. unsigned long *tos = (unsigned long *)regs->rsp;
  447. unsigned long next_rip = 0;
  448. unsigned long copy_rip = (unsigned long)p->ainsn.insn;
  449. unsigned long orig_rip = (unsigned long)p->addr;
  450. kprobe_opcode_t *insn = p->ainsn.insn;
  451. /*skip the REX prefix*/
  452. if (*insn >= 0x40 && *insn <= 0x4f)
  453. insn++;
  454. switch (*insn) {
  455. case 0x9c: /* pushfl */
  456. *tos &= ~(TF_MASK | IF_MASK);
  457. *tos |= kprobe_old_rflags;
  458. break;
  459. case 0xc3: /* ret/lret */
  460. case 0xcb:
  461. case 0xc2:
  462. case 0xca:
  463. regs->eflags &= ~TF_MASK;
  464. /* rip is already adjusted, no more changes required*/
  465. return;
  466. case 0xe8: /* call relative - Fix return addr */
  467. *tos = orig_rip + (*tos - copy_rip);
  468. break;
  469. case 0xff:
  470. if ((*insn & 0x30) == 0x10) {
  471. /* call absolute, indirect */
  472. /* Fix return addr; rip is correct. */
  473. next_rip = regs->rip;
  474. *tos = orig_rip + (*tos - copy_rip);
  475. } else if (((*insn & 0x31) == 0x20) || /* jmp near, absolute indirect */
  476. ((*insn & 0x31) == 0x21)) { /* jmp far, absolute indirect */
  477. /* rip is correct. */
  478. next_rip = regs->rip;
  479. }
  480. break;
  481. case 0xea: /* jmp absolute -- rip is correct */
  482. next_rip = regs->rip;
  483. break;
  484. default:
  485. break;
  486. }
  487. regs->eflags &= ~TF_MASK;
  488. if (next_rip) {
  489. regs->rip = next_rip;
  490. } else {
  491. regs->rip = orig_rip + (regs->rip - copy_rip);
  492. }
  493. }
  494. /*
  495. * Interrupts are disabled on entry as trap1 is an interrupt gate and they
  496. * remain disabled thoroughout this function. And we hold kprobe lock.
  497. */
  498. int post_kprobe_handler(struct pt_regs *regs)
  499. {
  500. if (!kprobe_running())
  501. return 0;
  502. if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) {
  503. kprobe_status = KPROBE_HIT_SSDONE;
  504. current_kprobe->post_handler(current_kprobe, regs, 0);
  505. }
  506. if (current_kprobe->post_handler != trampoline_post_handler)
  507. resume_execution(current_kprobe, regs);
  508. regs->eflags |= kprobe_saved_rflags;
  509. /* Restore the original saved kprobes variables and continue. */
  510. if (kprobe_status == KPROBE_REENTER) {
  511. restore_previous_kprobe();
  512. goto out;
  513. } else {
  514. unlock_kprobes();
  515. }
  516. out:
  517. preempt_enable_no_resched();
  518. /*
  519. * if somebody else is singlestepping across a probe point, eflags
  520. * will have TF set, in which case, continue the remaining processing
  521. * of do_debug, as if this is not a probe hit.
  522. */
  523. if (regs->eflags & TF_MASK)
  524. return 0;
  525. return 1;
  526. }
  527. /* Interrupts disabled, kprobe_lock held. */
  528. int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  529. {
  530. if (current_kprobe->fault_handler
  531. && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
  532. return 1;
  533. if (kprobe_status & KPROBE_HIT_SS) {
  534. resume_execution(current_kprobe, regs);
  535. regs->eflags |= kprobe_old_rflags;
  536. unlock_kprobes();
  537. preempt_enable_no_resched();
  538. }
  539. return 0;
  540. }
  541. /*
  542. * Wrapper routine for handling exceptions.
  543. */
  544. int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
  545. void *data)
  546. {
  547. struct die_args *args = (struct die_args *)data;
  548. switch (val) {
  549. case DIE_INT3:
  550. if (kprobe_handler(args->regs))
  551. return NOTIFY_STOP;
  552. break;
  553. case DIE_DEBUG:
  554. if (post_kprobe_handler(args->regs))
  555. return NOTIFY_STOP;
  556. break;
  557. case DIE_GPF:
  558. if (kprobe_running() &&
  559. kprobe_fault_handler(args->regs, args->trapnr))
  560. return NOTIFY_STOP;
  561. break;
  562. case DIE_PAGE_FAULT:
  563. if (kprobe_running() &&
  564. kprobe_fault_handler(args->regs, args->trapnr))
  565. return NOTIFY_STOP;
  566. break;
  567. default:
  568. break;
  569. }
  570. return NOTIFY_DONE;
  571. }
  572. int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  573. {
  574. struct jprobe *jp = container_of(p, struct jprobe, kp);
  575. unsigned long addr;
  576. jprobe_saved_regs = *regs;
  577. jprobe_saved_rsp = (long *) regs->rsp;
  578. addr = (unsigned long)jprobe_saved_rsp;
  579. /*
  580. * As Linus pointed out, gcc assumes that the callee
  581. * owns the argument space and could overwrite it, e.g.
  582. * tailcall optimization. So, to be absolutely safe
  583. * we also save and restore enough stack bytes to cover
  584. * the argument area.
  585. */
  586. memcpy(jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE(addr));
  587. regs->eflags &= ~IF_MASK;
  588. regs->rip = (unsigned long)(jp->entry);
  589. return 1;
  590. }
  591. void jprobe_return(void)
  592. {
  593. preempt_enable_no_resched();
  594. asm volatile (" xchg %%rbx,%%rsp \n"
  595. " int3 \n"
  596. " .globl jprobe_return_end \n"
  597. " jprobe_return_end: \n"
  598. " nop \n"::"b"
  599. (jprobe_saved_rsp):"memory");
  600. }
  601. int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  602. {
  603. u8 *addr = (u8 *) (regs->rip - 1);
  604. unsigned long stack_addr = (unsigned long)jprobe_saved_rsp;
  605. struct jprobe *jp = container_of(p, struct jprobe, kp);
  606. if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
  607. if ((long *)regs->rsp != jprobe_saved_rsp) {
  608. struct pt_regs *saved_regs =
  609. container_of(jprobe_saved_rsp, struct pt_regs, rsp);
  610. printk("current rsp %p does not match saved rsp %p\n",
  611. (long *)regs->rsp, jprobe_saved_rsp);
  612. printk("Saved registers for jprobe %p\n", jp);
  613. show_registers(saved_regs);
  614. printk("Current registers\n");
  615. show_registers(regs);
  616. BUG();
  617. }
  618. *regs = jprobe_saved_regs;
  619. memcpy((kprobe_opcode_t *) stack_addr, jprobes_stack,
  620. MIN_STACK_SIZE(stack_addr));
  621. return 1;
  622. }
  623. return 0;
  624. }
  625. /*
  626. * kprobe->ainsn.insn points to the copy of the instruction to be single-stepped.
  627. * By default on x86_64, pages we get from kmalloc or vmalloc are not
  628. * executable. Single-stepping an instruction on such a page yields an
  629. * oops. So instead of storing the instruction copies in their respective
  630. * kprobe objects, we allocate a page, map it executable, and store all the
  631. * instruction copies there. (We can allocate additional pages if somebody
  632. * inserts a huge number of probes.) Each page can hold up to INSNS_PER_PAGE
  633. * instruction slots, each of which is MAX_INSN_SIZE*sizeof(kprobe_opcode_t)
  634. * bytes.
  635. */
  636. #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE*sizeof(kprobe_opcode_t)))
  637. struct kprobe_insn_page {
  638. struct hlist_node hlist;
  639. kprobe_opcode_t *insns; /* page of instruction slots */
  640. char slot_used[INSNS_PER_PAGE];
  641. int nused;
  642. };
  643. static struct hlist_head kprobe_insn_pages;
  644. /**
  645. * get_insn_slot() - Find a slot on an executable page for an instruction.
  646. * We allocate an executable page if there's no room on existing ones.
  647. */
  648. static kprobe_opcode_t *get_insn_slot(void)
  649. {
  650. struct kprobe_insn_page *kip;
  651. struct hlist_node *pos;
  652. hlist_for_each(pos, &kprobe_insn_pages) {
  653. kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
  654. if (kip->nused < INSNS_PER_PAGE) {
  655. int i;
  656. for (i = 0; i < INSNS_PER_PAGE; i++) {
  657. if (!kip->slot_used[i]) {
  658. kip->slot_used[i] = 1;
  659. kip->nused++;
  660. return kip->insns + (i*MAX_INSN_SIZE);
  661. }
  662. }
  663. /* Surprise! No unused slots. Fix kip->nused. */
  664. kip->nused = INSNS_PER_PAGE;
  665. }
  666. }
  667. /* All out of space. Need to allocate a new page. Use slot 0.*/
  668. kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
  669. if (!kip) {
  670. return NULL;
  671. }
  672. /*
  673. * For the %rip-relative displacement fixups to be doable, we
  674. * need our instruction copy to be within +/- 2GB of any data it
  675. * might access via %rip. That is, within 2GB of where the
  676. * kernel image and loaded module images reside. So we allocate
  677. * a page in the module loading area.
  678. */
  679. kip->insns = module_alloc(PAGE_SIZE);
  680. if (!kip->insns) {
  681. kfree(kip);
  682. return NULL;
  683. }
  684. INIT_HLIST_NODE(&kip->hlist);
  685. hlist_add_head(&kip->hlist, &kprobe_insn_pages);
  686. memset(kip->slot_used, 0, INSNS_PER_PAGE);
  687. kip->slot_used[0] = 1;
  688. kip->nused = 1;
  689. return kip->insns;
  690. }
  691. /**
  692. * free_insn_slot() - Free instruction slot obtained from get_insn_slot().
  693. */
  694. static void free_insn_slot(kprobe_opcode_t *slot)
  695. {
  696. struct kprobe_insn_page *kip;
  697. struct hlist_node *pos;
  698. hlist_for_each(pos, &kprobe_insn_pages) {
  699. kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
  700. if (kip->insns <= slot
  701. && slot < kip->insns+(INSNS_PER_PAGE*MAX_INSN_SIZE)) {
  702. int i = (slot - kip->insns) / MAX_INSN_SIZE;
  703. kip->slot_used[i] = 0;
  704. kip->nused--;
  705. if (kip->nused == 0) {
  706. /*
  707. * Page is no longer in use. Free it unless
  708. * it's the last one. We keep the last one
  709. * so as not to have to set it up again the
  710. * next time somebody inserts a probe.
  711. */
  712. hlist_del(&kip->hlist);
  713. if (hlist_empty(&kprobe_insn_pages)) {
  714. INIT_HLIST_NODE(&kip->hlist);
  715. hlist_add_head(&kip->hlist,
  716. &kprobe_insn_pages);
  717. } else {
  718. module_free(NULL, kip->insns);
  719. kfree(kip);
  720. }
  721. }
  722. return;
  723. }
  724. }
  725. }