kprobes.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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/pgtable.h>
  42. #include <asm/kdebug.h>
  43. static DECLARE_MUTEX(kprobe_mutex);
  44. /* kprobe_status settings */
  45. #define KPROBE_HIT_ACTIVE 0x00000001
  46. #define KPROBE_HIT_SS 0x00000002
  47. static struct kprobe *current_kprobe;
  48. static unsigned long kprobe_status, kprobe_old_rflags, kprobe_saved_rflags;
  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. }
  204. void arch_remove_kprobe(struct kprobe *p)
  205. {
  206. up(&kprobe_mutex);
  207. free_insn_slot(p->ainsn.insn);
  208. down(&kprobe_mutex);
  209. }
  210. static inline void disarm_kprobe(struct kprobe *p, struct pt_regs *regs)
  211. {
  212. *p->addr = p->opcode;
  213. regs->rip = (unsigned long)p->addr;
  214. }
  215. static void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  216. {
  217. regs->eflags |= TF_MASK;
  218. regs->eflags &= ~IF_MASK;
  219. /*single step inline if the instruction is an int3*/
  220. if (p->opcode == BREAKPOINT_INSTRUCTION)
  221. regs->rip = (unsigned long)p->addr;
  222. else
  223. regs->rip = (unsigned long)p->ainsn.insn;
  224. }
  225. struct task_struct *arch_get_kprobe_task(void *ptr)
  226. {
  227. return ((struct thread_info *) (((unsigned long) ptr) &
  228. (~(THREAD_SIZE -1))))->task;
  229. }
  230. void arch_prepare_kretprobe(struct kretprobe *rp, struct pt_regs *regs)
  231. {
  232. unsigned long *sara = (unsigned long *)regs->rsp;
  233. struct kretprobe_instance *ri;
  234. static void *orig_ret_addr;
  235. /*
  236. * Save the return address when the return probe hits
  237. * the first time, and use it to populate the (krprobe
  238. * instance)->ret_addr for subsequent return probes at
  239. * the same addrress since stack address would have
  240. * the kretprobe_trampoline by then.
  241. */
  242. if (((void*) *sara) != kretprobe_trampoline)
  243. orig_ret_addr = (void*) *sara;
  244. if ((ri = get_free_rp_inst(rp)) != NULL) {
  245. ri->rp = rp;
  246. ri->stack_addr = sara;
  247. ri->ret_addr = orig_ret_addr;
  248. add_rp_inst(ri);
  249. /* Replace the return addr with trampoline addr */
  250. *sara = (unsigned long) &kretprobe_trampoline;
  251. } else {
  252. rp->nmissed++;
  253. }
  254. }
  255. void arch_kprobe_flush_task(struct task_struct *tk)
  256. {
  257. struct kretprobe_instance *ri;
  258. while ((ri = get_rp_inst_tsk(tk)) != NULL) {
  259. *((unsigned long *)(ri->stack_addr)) =
  260. (unsigned long) ri->ret_addr;
  261. recycle_rp_inst(ri);
  262. }
  263. }
  264. /*
  265. * Interrupts are disabled on entry as trap3 is an interrupt gate and they
  266. * remain disabled thorough out this function.
  267. */
  268. int kprobe_handler(struct pt_regs *regs)
  269. {
  270. struct kprobe *p;
  271. int ret = 0;
  272. kprobe_opcode_t *addr = (kprobe_opcode_t *)(regs->rip - sizeof(kprobe_opcode_t));
  273. /* We're in an interrupt, but this is clear and BUG()-safe. */
  274. preempt_disable();
  275. /* Check we're not actually recursing */
  276. if (kprobe_running()) {
  277. /* We *are* holding lock here, so this is safe.
  278. Disarm the probe we just hit, and ignore it. */
  279. p = get_kprobe(addr);
  280. if (p) {
  281. if (kprobe_status == KPROBE_HIT_SS) {
  282. regs->eflags &= ~TF_MASK;
  283. regs->eflags |= kprobe_saved_rflags;
  284. unlock_kprobes();
  285. goto no_kprobe;
  286. }
  287. disarm_kprobe(p, regs);
  288. ret = 1;
  289. } else {
  290. p = current_kprobe;
  291. if (p->break_handler && p->break_handler(p, regs)) {
  292. goto ss_probe;
  293. }
  294. }
  295. /* If it's not ours, can't be delete race, (we hold lock). */
  296. goto no_kprobe;
  297. }
  298. lock_kprobes();
  299. p = get_kprobe(addr);
  300. if (!p) {
  301. unlock_kprobes();
  302. if (*addr != BREAKPOINT_INSTRUCTION) {
  303. /*
  304. * The breakpoint instruction was removed right
  305. * after we hit it. Another cpu has removed
  306. * either a probepoint or a debugger breakpoint
  307. * at this address. In either case, no further
  308. * handling of this interrupt is appropriate.
  309. */
  310. ret = 1;
  311. }
  312. /* Not one of ours: let kernel handle it */
  313. goto no_kprobe;
  314. }
  315. kprobe_status = KPROBE_HIT_ACTIVE;
  316. current_kprobe = p;
  317. kprobe_saved_rflags = kprobe_old_rflags
  318. = (regs->eflags & (TF_MASK | IF_MASK));
  319. if (is_IF_modifier(p->ainsn.insn))
  320. kprobe_saved_rflags &= ~IF_MASK;
  321. if (p->pre_handler && p->pre_handler(p, regs))
  322. /* handler has already set things up, so skip ss setup */
  323. return 1;
  324. ss_probe:
  325. prepare_singlestep(p, regs);
  326. kprobe_status = KPROBE_HIT_SS;
  327. return 1;
  328. no_kprobe:
  329. preempt_enable_no_resched();
  330. return ret;
  331. }
  332. /*
  333. * For function-return probes, init_kprobes() establishes a probepoint
  334. * here. When a retprobed function returns, this probe is hit and
  335. * trampoline_probe_handler() runs, calling the kretprobe's handler.
  336. */
  337. void kretprobe_trampoline_holder(void)
  338. {
  339. asm volatile ( ".global kretprobe_trampoline\n"
  340. "kretprobe_trampoline: \n"
  341. "nop\n");
  342. }
  343. /*
  344. * Called when we hit the probe point at kretprobe_trampoline
  345. */
  346. int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
  347. {
  348. struct task_struct *tsk;
  349. struct kretprobe_instance *ri;
  350. struct hlist_head *head;
  351. struct hlist_node *node;
  352. unsigned long *sara = (unsigned long *)regs->rsp - 1;
  353. tsk = arch_get_kprobe_task(sara);
  354. head = kretprobe_inst_table_head(tsk);
  355. hlist_for_each_entry(ri, node, head, hlist) {
  356. if (ri->stack_addr == sara && ri->rp) {
  357. if (ri->rp->handler)
  358. ri->rp->handler(ri, regs);
  359. }
  360. }
  361. return 0;
  362. }
  363. void trampoline_post_handler(struct kprobe *p, struct pt_regs *regs,
  364. unsigned long flags)
  365. {
  366. struct kretprobe_instance *ri;
  367. /* RA already popped */
  368. unsigned long *sara = ((unsigned long *)regs->rsp) - 1;
  369. while ((ri = get_rp_inst(sara))) {
  370. regs->rip = (unsigned long)ri->ret_addr;
  371. recycle_rp_inst(ri);
  372. }
  373. regs->eflags &= ~TF_MASK;
  374. }
  375. /*
  376. * Called after single-stepping. p->addr is the address of the
  377. * instruction whose first byte has been replaced by the "int 3"
  378. * instruction. To avoid the SMP problems that can occur when we
  379. * temporarily put back the original opcode to single-step, we
  380. * single-stepped a copy of the instruction. The address of this
  381. * copy is p->ainsn.insn.
  382. *
  383. * This function prepares to return from the post-single-step
  384. * interrupt. We have to fix up the stack as follows:
  385. *
  386. * 0) Except in the case of absolute or indirect jump or call instructions,
  387. * the new rip is relative to the copied instruction. We need to make
  388. * it relative to the original instruction.
  389. *
  390. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  391. * flags are set in the just-pushed eflags, and may need to be cleared.
  392. *
  393. * 2) If the single-stepped instruction was a call, the return address
  394. * that is atop the stack is the address following the copied instruction.
  395. * We need to make it the address following the original instruction.
  396. */
  397. static void resume_execution(struct kprobe *p, struct pt_regs *regs)
  398. {
  399. unsigned long *tos = (unsigned long *)regs->rsp;
  400. unsigned long next_rip = 0;
  401. unsigned long copy_rip = (unsigned long)p->ainsn.insn;
  402. unsigned long orig_rip = (unsigned long)p->addr;
  403. kprobe_opcode_t *insn = p->ainsn.insn;
  404. /*skip the REX prefix*/
  405. if (*insn >= 0x40 && *insn <= 0x4f)
  406. insn++;
  407. switch (*insn) {
  408. case 0x9c: /* pushfl */
  409. *tos &= ~(TF_MASK | IF_MASK);
  410. *tos |= kprobe_old_rflags;
  411. break;
  412. case 0xc3: /* ret/lret */
  413. case 0xcb:
  414. case 0xc2:
  415. case 0xca:
  416. regs->eflags &= ~TF_MASK;
  417. /* rip is already adjusted, no more changes required*/
  418. return;
  419. case 0xe8: /* call relative - Fix return addr */
  420. *tos = orig_rip + (*tos - copy_rip);
  421. break;
  422. case 0xff:
  423. if ((*insn & 0x30) == 0x10) {
  424. /* call absolute, indirect */
  425. /* Fix return addr; rip is correct. */
  426. next_rip = regs->rip;
  427. *tos = orig_rip + (*tos - copy_rip);
  428. } else if (((*insn & 0x31) == 0x20) || /* jmp near, absolute indirect */
  429. ((*insn & 0x31) == 0x21)) { /* jmp far, absolute indirect */
  430. /* rip is correct. */
  431. next_rip = regs->rip;
  432. }
  433. break;
  434. case 0xea: /* jmp absolute -- rip is correct */
  435. next_rip = regs->rip;
  436. break;
  437. default:
  438. break;
  439. }
  440. regs->eflags &= ~TF_MASK;
  441. if (next_rip) {
  442. regs->rip = next_rip;
  443. } else {
  444. regs->rip = orig_rip + (regs->rip - copy_rip);
  445. }
  446. }
  447. /*
  448. * Interrupts are disabled on entry as trap1 is an interrupt gate and they
  449. * remain disabled thoroughout this function. And we hold kprobe lock.
  450. */
  451. int post_kprobe_handler(struct pt_regs *regs)
  452. {
  453. if (!kprobe_running())
  454. return 0;
  455. if (current_kprobe->post_handler)
  456. current_kprobe->post_handler(current_kprobe, regs, 0);
  457. if (current_kprobe->post_handler != trampoline_post_handler)
  458. resume_execution(current_kprobe, regs);
  459. regs->eflags |= kprobe_saved_rflags;
  460. unlock_kprobes();
  461. preempt_enable_no_resched();
  462. /*
  463. * if somebody else is singlestepping across a probe point, eflags
  464. * will have TF set, in which case, continue the remaining processing
  465. * of do_debug, as if this is not a probe hit.
  466. */
  467. if (regs->eflags & TF_MASK)
  468. return 0;
  469. return 1;
  470. }
  471. /* Interrupts disabled, kprobe_lock held. */
  472. int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  473. {
  474. if (current_kprobe->fault_handler
  475. && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
  476. return 1;
  477. if (kprobe_status & KPROBE_HIT_SS) {
  478. resume_execution(current_kprobe, regs);
  479. regs->eflags |= kprobe_old_rflags;
  480. unlock_kprobes();
  481. preempt_enable_no_resched();
  482. }
  483. return 0;
  484. }
  485. /*
  486. * Wrapper routine for handling exceptions.
  487. */
  488. int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
  489. void *data)
  490. {
  491. struct die_args *args = (struct die_args *)data;
  492. switch (val) {
  493. case DIE_INT3:
  494. if (kprobe_handler(args->regs))
  495. return NOTIFY_STOP;
  496. break;
  497. case DIE_DEBUG:
  498. if (post_kprobe_handler(args->regs))
  499. return NOTIFY_STOP;
  500. break;
  501. case DIE_GPF:
  502. if (kprobe_running() &&
  503. kprobe_fault_handler(args->regs, args->trapnr))
  504. return NOTIFY_STOP;
  505. break;
  506. case DIE_PAGE_FAULT:
  507. if (kprobe_running() &&
  508. kprobe_fault_handler(args->regs, args->trapnr))
  509. return NOTIFY_STOP;
  510. break;
  511. default:
  512. break;
  513. }
  514. return NOTIFY_DONE;
  515. }
  516. int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  517. {
  518. struct jprobe *jp = container_of(p, struct jprobe, kp);
  519. unsigned long addr;
  520. jprobe_saved_regs = *regs;
  521. jprobe_saved_rsp = (long *) regs->rsp;
  522. addr = (unsigned long)jprobe_saved_rsp;
  523. /*
  524. * As Linus pointed out, gcc assumes that the callee
  525. * owns the argument space and could overwrite it, e.g.
  526. * tailcall optimization. So, to be absolutely safe
  527. * we also save and restore enough stack bytes to cover
  528. * the argument area.
  529. */
  530. memcpy(jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE(addr));
  531. regs->eflags &= ~IF_MASK;
  532. regs->rip = (unsigned long)(jp->entry);
  533. return 1;
  534. }
  535. void jprobe_return(void)
  536. {
  537. preempt_enable_no_resched();
  538. asm volatile (" xchg %%rbx,%%rsp \n"
  539. " int3 \n"
  540. " .globl jprobe_return_end \n"
  541. " jprobe_return_end: \n"
  542. " nop \n"::"b"
  543. (jprobe_saved_rsp):"memory");
  544. }
  545. int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  546. {
  547. u8 *addr = (u8 *) (regs->rip - 1);
  548. unsigned long stack_addr = (unsigned long)jprobe_saved_rsp;
  549. struct jprobe *jp = container_of(p, struct jprobe, kp);
  550. if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
  551. if ((long *)regs->rsp != jprobe_saved_rsp) {
  552. struct pt_regs *saved_regs =
  553. container_of(jprobe_saved_rsp, struct pt_regs, rsp);
  554. printk("current rsp %p does not match saved rsp %p\n",
  555. (long *)regs->rsp, jprobe_saved_rsp);
  556. printk("Saved registers for jprobe %p\n", jp);
  557. show_registers(saved_regs);
  558. printk("Current registers\n");
  559. show_registers(regs);
  560. BUG();
  561. }
  562. *regs = jprobe_saved_regs;
  563. memcpy((kprobe_opcode_t *) stack_addr, jprobes_stack,
  564. MIN_STACK_SIZE(stack_addr));
  565. return 1;
  566. }
  567. return 0;
  568. }
  569. /*
  570. * kprobe->ainsn.insn points to the copy of the instruction to be single-stepped.
  571. * By default on x86_64, pages we get from kmalloc or vmalloc are not
  572. * executable. Single-stepping an instruction on such a page yields an
  573. * oops. So instead of storing the instruction copies in their respective
  574. * kprobe objects, we allocate a page, map it executable, and store all the
  575. * instruction copies there. (We can allocate additional pages if somebody
  576. * inserts a huge number of probes.) Each page can hold up to INSNS_PER_PAGE
  577. * instruction slots, each of which is MAX_INSN_SIZE*sizeof(kprobe_opcode_t)
  578. * bytes.
  579. */
  580. #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE*sizeof(kprobe_opcode_t)))
  581. struct kprobe_insn_page {
  582. struct hlist_node hlist;
  583. kprobe_opcode_t *insns; /* page of instruction slots */
  584. char slot_used[INSNS_PER_PAGE];
  585. int nused;
  586. };
  587. static struct hlist_head kprobe_insn_pages;
  588. /**
  589. * get_insn_slot() - Find a slot on an executable page for an instruction.
  590. * We allocate an executable page if there's no room on existing ones.
  591. */
  592. static kprobe_opcode_t *get_insn_slot(void)
  593. {
  594. struct kprobe_insn_page *kip;
  595. struct hlist_node *pos;
  596. hlist_for_each(pos, &kprobe_insn_pages) {
  597. kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
  598. if (kip->nused < INSNS_PER_PAGE) {
  599. int i;
  600. for (i = 0; i < INSNS_PER_PAGE; i++) {
  601. if (!kip->slot_used[i]) {
  602. kip->slot_used[i] = 1;
  603. kip->nused++;
  604. return kip->insns + (i*MAX_INSN_SIZE);
  605. }
  606. }
  607. /* Surprise! No unused slots. Fix kip->nused. */
  608. kip->nused = INSNS_PER_PAGE;
  609. }
  610. }
  611. /* All out of space. Need to allocate a new page. Use slot 0.*/
  612. kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
  613. if (!kip) {
  614. return NULL;
  615. }
  616. /*
  617. * For the %rip-relative displacement fixups to be doable, we
  618. * need our instruction copy to be within +/- 2GB of any data it
  619. * might access via %rip. That is, within 2GB of where the
  620. * kernel image and loaded module images reside. So we allocate
  621. * a page in the module loading area.
  622. */
  623. kip->insns = module_alloc(PAGE_SIZE);
  624. if (!kip->insns) {
  625. kfree(kip);
  626. return NULL;
  627. }
  628. INIT_HLIST_NODE(&kip->hlist);
  629. hlist_add_head(&kip->hlist, &kprobe_insn_pages);
  630. memset(kip->slot_used, 0, INSNS_PER_PAGE);
  631. kip->slot_used[0] = 1;
  632. kip->nused = 1;
  633. return kip->insns;
  634. }
  635. /**
  636. * free_insn_slot() - Free instruction slot obtained from get_insn_slot().
  637. */
  638. static void free_insn_slot(kprobe_opcode_t *slot)
  639. {
  640. struct kprobe_insn_page *kip;
  641. struct hlist_node *pos;
  642. hlist_for_each(pos, &kprobe_insn_pages) {
  643. kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
  644. if (kip->insns <= slot
  645. && slot < kip->insns+(INSNS_PER_PAGE*MAX_INSN_SIZE)) {
  646. int i = (slot - kip->insns) / MAX_INSN_SIZE;
  647. kip->slot_used[i] = 0;
  648. kip->nused--;
  649. if (kip->nused == 0) {
  650. /*
  651. * Page is no longer in use. Free it unless
  652. * it's the last one. We keep the last one
  653. * so as not to have to set it up again the
  654. * next time somebody inserts a probe.
  655. */
  656. hlist_del(&kip->hlist);
  657. if (hlist_empty(&kprobe_insn_pages)) {
  658. INIT_HLIST_NODE(&kip->hlist);
  659. hlist_add_head(&kip->hlist,
  660. &kprobe_insn_pages);
  661. } else {
  662. module_free(NULL, kip->insns);
  663. kfree(kip);
  664. }
  665. }
  666. return;
  667. }
  668. }
  669. }