kprobes.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. /*
  2. * Kernel Probes (KProbes)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2002, 2004
  19. *
  20. * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
  21. * Probes initial implementation ( includes contributions from
  22. * Rusty Russell).
  23. * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
  24. * interface to access function arguments.
  25. * 2004-Oct Jim Keniston <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
  26. * <prasanna@in.ibm.com> adapted for x86_64 from i386.
  27. * 2005-Mar Roland McGrath <roland@redhat.com>
  28. * Fixed to handle %rip-relative addressing mode correctly.
  29. * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
  30. * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
  31. * <prasanna@in.ibm.com> added function-return probes.
  32. * 2005-May Rusty Lynch <rusty.lynch@intel.com>
  33. * Added function return probes functionality
  34. * 2006-Feb Masami Hiramatsu <hiramatu@sdl.hitachi.co.jp> added
  35. * kprobe-booster and kretprobe-booster for i386.
  36. * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster
  37. * and kretprobe-booster for x86-64
  38. * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com>, Arjan van de Ven
  39. * <arjan@infradead.org> and Jim Keniston <jkenisto@us.ibm.com>
  40. * unified x86 kprobes code.
  41. */
  42. #include <linux/kprobes.h>
  43. #include <linux/ptrace.h>
  44. #include <linux/string.h>
  45. #include <linux/slab.h>
  46. #include <linux/hardirq.h>
  47. #include <linux/preempt.h>
  48. #include <linux/module.h>
  49. #include <linux/kdebug.h>
  50. #include <linux/kallsyms.h>
  51. #include <asm/cacheflush.h>
  52. #include <asm/desc.h>
  53. #include <asm/pgtable.h>
  54. #include <asm/uaccess.h>
  55. #include <asm/alternative.h>
  56. #include <asm/insn.h>
  57. #include <asm/debugreg.h>
  58. void jprobe_return_end(void);
  59. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  60. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  61. #ifdef CONFIG_X86_64
  62. #define stack_addr(regs) ((unsigned long *)regs->sp)
  63. #else
  64. /*
  65. * "&regs->sp" looks wrong, but it's correct for x86_32. x86_32 CPUs
  66. * don't save the ss and esp registers if the CPU is already in kernel
  67. * mode when it traps. So for kprobes, regs->sp and regs->ss are not
  68. * the [nonexistent] saved stack pointer and ss register, but rather
  69. * the top 8 bytes of the pre-int3 stack. So &regs->sp happens to
  70. * point to the top of the pre-int3 stack.
  71. */
  72. #define stack_addr(regs) ((unsigned long *)&regs->sp)
  73. #endif
  74. #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
  75. (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
  76. (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
  77. (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
  78. (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
  79. << (row % 32))
  80. /*
  81. * Undefined/reserved opcodes, conditional jump, Opcode Extension
  82. * Groups, and some special opcodes can not boost.
  83. */
  84. static const u32 twobyte_is_boostable[256 / 32] = {
  85. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  86. /* ---------------------------------------------- */
  87. W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
  88. W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 10 */
  89. W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
  90. W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
  91. W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
  92. W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
  93. W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
  94. W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
  95. W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
  96. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
  97. W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
  98. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
  99. W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
  100. W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
  101. W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
  102. W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
  103. /* ----------------------------------------------- */
  104. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  105. };
  106. #undef W
  107. struct kretprobe_blackpoint kretprobe_blacklist[] = {
  108. {"__switch_to", }, /* This function switches only current task, but
  109. doesn't switch kernel stack.*/
  110. {NULL, NULL} /* Terminator */
  111. };
  112. const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
  113. /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
  114. static void __kprobes set_jmp_op(void *from, void *to)
  115. {
  116. struct __arch_jmp_op {
  117. char op;
  118. s32 raddr;
  119. } __attribute__((packed)) * jop;
  120. jop = (struct __arch_jmp_op *)from;
  121. jop->raddr = (s32)((long)(to) - ((long)(from) + 5));
  122. jop->op = RELATIVEJUMP_INSTRUCTION;
  123. }
  124. /*
  125. * Check for the REX prefix which can only exist on X86_64
  126. * X86_32 always returns 0
  127. */
  128. static int __kprobes is_REX_prefix(kprobe_opcode_t *insn)
  129. {
  130. #ifdef CONFIG_X86_64
  131. if ((*insn & 0xf0) == 0x40)
  132. return 1;
  133. #endif
  134. return 0;
  135. }
  136. /*
  137. * Returns non-zero if opcode is boostable.
  138. * RIP relative instructions are adjusted at copying time in 64 bits mode
  139. */
  140. static int __kprobes can_boost(kprobe_opcode_t *opcodes)
  141. {
  142. kprobe_opcode_t opcode;
  143. kprobe_opcode_t *orig_opcodes = opcodes;
  144. if (search_exception_tables((unsigned long)opcodes))
  145. return 0; /* Page fault may occur on this address. */
  146. retry:
  147. if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
  148. return 0;
  149. opcode = *(opcodes++);
  150. /* 2nd-byte opcode */
  151. if (opcode == 0x0f) {
  152. if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
  153. return 0;
  154. return test_bit(*opcodes,
  155. (unsigned long *)twobyte_is_boostable);
  156. }
  157. switch (opcode & 0xf0) {
  158. #ifdef CONFIG_X86_64
  159. case 0x40:
  160. goto retry; /* REX prefix is boostable */
  161. #endif
  162. case 0x60:
  163. if (0x63 < opcode && opcode < 0x67)
  164. goto retry; /* prefixes */
  165. /* can't boost Address-size override and bound */
  166. return (opcode != 0x62 && opcode != 0x67);
  167. case 0x70:
  168. return 0; /* can't boost conditional jump */
  169. case 0xc0:
  170. /* can't boost software-interruptions */
  171. return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
  172. case 0xd0:
  173. /* can boost AA* and XLAT */
  174. return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
  175. case 0xe0:
  176. /* can boost in/out and absolute jmps */
  177. return ((opcode & 0x04) || opcode == 0xea);
  178. case 0xf0:
  179. if ((opcode & 0x0c) == 0 && opcode != 0xf1)
  180. goto retry; /* lock/rep(ne) prefix */
  181. /* clear and set flags are boostable */
  182. return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
  183. default:
  184. /* segment override prefixes are boostable */
  185. if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
  186. goto retry; /* prefixes */
  187. /* CS override prefix and call are not boostable */
  188. return (opcode != 0x2e && opcode != 0x9a);
  189. }
  190. }
  191. /* Recover the probed instruction at addr for further analysis. */
  192. static int recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
  193. {
  194. struct kprobe *kp;
  195. kp = get_kprobe((void *)addr);
  196. if (!kp)
  197. return -EINVAL;
  198. /*
  199. * Basically, kp->ainsn.insn has an original instruction.
  200. * However, RIP-relative instruction can not do single-stepping
  201. * at different place, fix_riprel() tweaks the displacement of
  202. * that instruction. In that case, we can't recover the instruction
  203. * from the kp->ainsn.insn.
  204. *
  205. * On the other hand, kp->opcode has a copy of the first byte of
  206. * the probed instruction, which is overwritten by int3. And
  207. * the instruction at kp->addr is not modified by kprobes except
  208. * for the first byte, we can recover the original instruction
  209. * from it and kp->opcode.
  210. */
  211. memcpy(buf, kp->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
  212. buf[0] = kp->opcode;
  213. return 0;
  214. }
  215. /* Dummy buffers for kallsyms_lookup */
  216. static char __dummy_buf[KSYM_NAME_LEN];
  217. /* Check if paddr is at an instruction boundary */
  218. static int __kprobes can_probe(unsigned long paddr)
  219. {
  220. int ret;
  221. unsigned long addr, offset = 0;
  222. struct insn insn;
  223. kprobe_opcode_t buf[MAX_INSN_SIZE];
  224. if (!kallsyms_lookup(paddr, NULL, &offset, NULL, __dummy_buf))
  225. return 0;
  226. /* Decode instructions */
  227. addr = paddr - offset;
  228. while (addr < paddr) {
  229. kernel_insn_init(&insn, (void *)addr);
  230. insn_get_opcode(&insn);
  231. /*
  232. * Check if the instruction has been modified by another
  233. * kprobe, in which case we replace the breakpoint by the
  234. * original instruction in our buffer.
  235. */
  236. if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
  237. ret = recover_probed_instruction(buf, addr);
  238. if (ret)
  239. /*
  240. * Another debugging subsystem might insert
  241. * this breakpoint. In that case, we can't
  242. * recover it.
  243. */
  244. return 0;
  245. kernel_insn_init(&insn, buf);
  246. }
  247. insn_get_length(&insn);
  248. addr += insn.length;
  249. }
  250. return (addr == paddr);
  251. }
  252. /*
  253. * Returns non-zero if opcode modifies the interrupt flag.
  254. */
  255. static int __kprobes is_IF_modifier(kprobe_opcode_t *insn)
  256. {
  257. switch (*insn) {
  258. case 0xfa: /* cli */
  259. case 0xfb: /* sti */
  260. case 0xcf: /* iret/iretd */
  261. case 0x9d: /* popf/popfd */
  262. return 1;
  263. }
  264. /*
  265. * on X86_64, 0x40-0x4f are REX prefixes so we need to look
  266. * at the next byte instead.. but of course not recurse infinitely
  267. */
  268. if (is_REX_prefix(insn))
  269. return is_IF_modifier(++insn);
  270. return 0;
  271. }
  272. /*
  273. * Adjust the displacement if the instruction uses the %rip-relative
  274. * addressing mode.
  275. * If it does, Return the address of the 32-bit displacement word.
  276. * If not, return null.
  277. * Only applicable to 64-bit x86.
  278. */
  279. static void __kprobes fix_riprel(struct kprobe *p)
  280. {
  281. #ifdef CONFIG_X86_64
  282. struct insn insn;
  283. kernel_insn_init(&insn, p->ainsn.insn);
  284. if (insn_rip_relative(&insn)) {
  285. s64 newdisp;
  286. u8 *disp;
  287. insn_get_displacement(&insn);
  288. /*
  289. * The copied instruction uses the %rip-relative addressing
  290. * mode. Adjust the displacement for the difference between
  291. * the original location of this instruction and the location
  292. * of the copy that will actually be run. The tricky bit here
  293. * is making sure that the sign extension happens correctly in
  294. * this calculation, since we need a signed 32-bit result to
  295. * be sign-extended to 64 bits when it's added to the %rip
  296. * value and yield the same 64-bit result that the sign-
  297. * extension of the original signed 32-bit displacement would
  298. * have given.
  299. */
  300. newdisp = (u8 *) p->addr + (s64) insn.displacement.value -
  301. (u8 *) p->ainsn.insn;
  302. BUG_ON((s64) (s32) newdisp != newdisp); /* Sanity check. */
  303. disp = (u8 *) p->ainsn.insn + insn_offset_displacement(&insn);
  304. *(s32 *) disp = (s32) newdisp;
  305. }
  306. #endif
  307. }
  308. static void __kprobes arch_copy_kprobe(struct kprobe *p)
  309. {
  310. memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
  311. fix_riprel(p);
  312. if (can_boost(p->addr))
  313. p->ainsn.boostable = 0;
  314. else
  315. p->ainsn.boostable = -1;
  316. p->opcode = *p->addr;
  317. }
  318. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  319. {
  320. if (!can_probe((unsigned long)p->addr))
  321. return -EILSEQ;
  322. /* insn: must be on special executable page on x86. */
  323. p->ainsn.insn = get_insn_slot();
  324. if (!p->ainsn.insn)
  325. return -ENOMEM;
  326. arch_copy_kprobe(p);
  327. return 0;
  328. }
  329. void __kprobes arch_arm_kprobe(struct kprobe *p)
  330. {
  331. text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
  332. }
  333. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  334. {
  335. text_poke(p->addr, &p->opcode, 1);
  336. }
  337. void __kprobes arch_remove_kprobe(struct kprobe *p)
  338. {
  339. if (p->ainsn.insn) {
  340. free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
  341. p->ainsn.insn = NULL;
  342. }
  343. }
  344. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  345. {
  346. kcb->prev_kprobe.kp = kprobe_running();
  347. kcb->prev_kprobe.status = kcb->kprobe_status;
  348. kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
  349. kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
  350. }
  351. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  352. {
  353. __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
  354. kcb->kprobe_status = kcb->prev_kprobe.status;
  355. kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
  356. kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
  357. }
  358. static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  359. struct kprobe_ctlblk *kcb)
  360. {
  361. __get_cpu_var(current_kprobe) = p;
  362. kcb->kprobe_saved_flags = kcb->kprobe_old_flags
  363. = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
  364. if (is_IF_modifier(p->ainsn.insn))
  365. kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
  366. }
  367. static void __kprobes clear_btf(void)
  368. {
  369. if (test_thread_flag(TIF_DEBUGCTLMSR))
  370. update_debugctlmsr(0);
  371. }
  372. static void __kprobes restore_btf(void)
  373. {
  374. if (test_thread_flag(TIF_DEBUGCTLMSR))
  375. update_debugctlmsr(current->thread.debugctlmsr);
  376. }
  377. static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  378. {
  379. clear_btf();
  380. regs->flags |= X86_EFLAGS_TF;
  381. regs->flags &= ~X86_EFLAGS_IF;
  382. /* single step inline if the instruction is an int3 */
  383. if (p->opcode == BREAKPOINT_INSTRUCTION)
  384. regs->ip = (unsigned long)p->addr;
  385. else
  386. regs->ip = (unsigned long)p->ainsn.insn;
  387. }
  388. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  389. struct pt_regs *regs)
  390. {
  391. unsigned long *sara = stack_addr(regs);
  392. ri->ret_addr = (kprobe_opcode_t *) *sara;
  393. /* Replace the return addr with trampoline addr */
  394. *sara = (unsigned long) &kretprobe_trampoline;
  395. }
  396. static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs,
  397. struct kprobe_ctlblk *kcb)
  398. {
  399. #if !defined(CONFIG_PREEMPT) || defined(CONFIG_FREEZER)
  400. if (p->ainsn.boostable == 1 && !p->post_handler) {
  401. /* Boost up -- we can execute copied instructions directly */
  402. reset_current_kprobe();
  403. regs->ip = (unsigned long)p->ainsn.insn;
  404. preempt_enable_no_resched();
  405. return;
  406. }
  407. #endif
  408. prepare_singlestep(p, regs);
  409. kcb->kprobe_status = KPROBE_HIT_SS;
  410. }
  411. /*
  412. * We have reentered the kprobe_handler(), since another probe was hit while
  413. * within the handler. We save the original kprobes variables and just single
  414. * step on the instruction of the new probe without calling any user handlers.
  415. */
  416. static int __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
  417. struct kprobe_ctlblk *kcb)
  418. {
  419. switch (kcb->kprobe_status) {
  420. case KPROBE_HIT_SSDONE:
  421. case KPROBE_HIT_ACTIVE:
  422. save_previous_kprobe(kcb);
  423. set_current_kprobe(p, regs, kcb);
  424. kprobes_inc_nmissed_count(p);
  425. prepare_singlestep(p, regs);
  426. kcb->kprobe_status = KPROBE_REENTER;
  427. break;
  428. case KPROBE_HIT_SS:
  429. /* A probe has been hit in the codepath leading up to, or just
  430. * after, single-stepping of a probed instruction. This entire
  431. * codepath should strictly reside in .kprobes.text section.
  432. * Raise a BUG or we'll continue in an endless reentering loop
  433. * and eventually a stack overflow.
  434. */
  435. printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n",
  436. p->addr);
  437. dump_kprobe(p);
  438. BUG();
  439. default:
  440. /* impossible cases */
  441. WARN_ON(1);
  442. return 0;
  443. }
  444. return 1;
  445. }
  446. /*
  447. * Interrupts are disabled on entry as trap3 is an interrupt gate and they
  448. * remain disabled thorough out this function.
  449. */
  450. static int __kprobes kprobe_handler(struct pt_regs *regs)
  451. {
  452. kprobe_opcode_t *addr;
  453. struct kprobe *p;
  454. struct kprobe_ctlblk *kcb;
  455. addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
  456. if (*addr != BREAKPOINT_INSTRUCTION) {
  457. /*
  458. * The breakpoint instruction was removed right
  459. * after we hit it. Another cpu has removed
  460. * either a probepoint or a debugger breakpoint
  461. * at this address. In either case, no further
  462. * handling of this interrupt is appropriate.
  463. * Back up over the (now missing) int3 and run
  464. * the original instruction.
  465. */
  466. regs->ip = (unsigned long)addr;
  467. return 1;
  468. }
  469. /*
  470. * We don't want to be preempted for the entire
  471. * duration of kprobe processing. We conditionally
  472. * re-enable preemption at the end of this function,
  473. * and also in reenter_kprobe() and setup_singlestep().
  474. */
  475. preempt_disable();
  476. kcb = get_kprobe_ctlblk();
  477. p = get_kprobe(addr);
  478. if (p) {
  479. if (kprobe_running()) {
  480. if (reenter_kprobe(p, regs, kcb))
  481. return 1;
  482. } else {
  483. set_current_kprobe(p, regs, kcb);
  484. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  485. /*
  486. * If we have no pre-handler or it returned 0, we
  487. * continue with normal processing. If we have a
  488. * pre-handler and it returned non-zero, it prepped
  489. * for calling the break_handler below on re-entry
  490. * for jprobe processing, so get out doing nothing
  491. * more here.
  492. */
  493. if (!p->pre_handler || !p->pre_handler(p, regs))
  494. setup_singlestep(p, regs, kcb);
  495. return 1;
  496. }
  497. } else if (kprobe_running()) {
  498. p = __get_cpu_var(current_kprobe);
  499. if (p->break_handler && p->break_handler(p, regs)) {
  500. setup_singlestep(p, regs, kcb);
  501. return 1;
  502. }
  503. } /* else: not a kprobe fault; let the kernel handle it */
  504. preempt_enable_no_resched();
  505. return 0;
  506. }
  507. /*
  508. * When a retprobed function returns, this code saves registers and
  509. * calls trampoline_handler() runs, which calls the kretprobe's handler.
  510. */
  511. static void __used __kprobes kretprobe_trampoline_holder(void)
  512. {
  513. asm volatile (
  514. ".global kretprobe_trampoline\n"
  515. "kretprobe_trampoline: \n"
  516. #ifdef CONFIG_X86_64
  517. /* We don't bother saving the ss register */
  518. " pushq %rsp\n"
  519. " pushfq\n"
  520. /*
  521. * Skip cs, ip, orig_ax.
  522. * trampoline_handler() will plug in these values
  523. */
  524. " subq $24, %rsp\n"
  525. " pushq %rdi\n"
  526. " pushq %rsi\n"
  527. " pushq %rdx\n"
  528. " pushq %rcx\n"
  529. " pushq %rax\n"
  530. " pushq %r8\n"
  531. " pushq %r9\n"
  532. " pushq %r10\n"
  533. " pushq %r11\n"
  534. " pushq %rbx\n"
  535. " pushq %rbp\n"
  536. " pushq %r12\n"
  537. " pushq %r13\n"
  538. " pushq %r14\n"
  539. " pushq %r15\n"
  540. " movq %rsp, %rdi\n"
  541. " call trampoline_handler\n"
  542. /* Replace saved sp with true return address. */
  543. " movq %rax, 152(%rsp)\n"
  544. " popq %r15\n"
  545. " popq %r14\n"
  546. " popq %r13\n"
  547. " popq %r12\n"
  548. " popq %rbp\n"
  549. " popq %rbx\n"
  550. " popq %r11\n"
  551. " popq %r10\n"
  552. " popq %r9\n"
  553. " popq %r8\n"
  554. " popq %rax\n"
  555. " popq %rcx\n"
  556. " popq %rdx\n"
  557. " popq %rsi\n"
  558. " popq %rdi\n"
  559. /* Skip orig_ax, ip, cs */
  560. " addq $24, %rsp\n"
  561. " popfq\n"
  562. #else
  563. " pushf\n"
  564. /*
  565. * Skip cs, ip, orig_ax and gs.
  566. * trampoline_handler() will plug in these values
  567. */
  568. " subl $16, %esp\n"
  569. " pushl %fs\n"
  570. " pushl %es\n"
  571. " pushl %ds\n"
  572. " pushl %eax\n"
  573. " pushl %ebp\n"
  574. " pushl %edi\n"
  575. " pushl %esi\n"
  576. " pushl %edx\n"
  577. " pushl %ecx\n"
  578. " pushl %ebx\n"
  579. " movl %esp, %eax\n"
  580. " call trampoline_handler\n"
  581. /* Move flags to cs */
  582. " movl 56(%esp), %edx\n"
  583. " movl %edx, 52(%esp)\n"
  584. /* Replace saved flags with true return address. */
  585. " movl %eax, 56(%esp)\n"
  586. " popl %ebx\n"
  587. " popl %ecx\n"
  588. " popl %edx\n"
  589. " popl %esi\n"
  590. " popl %edi\n"
  591. " popl %ebp\n"
  592. " popl %eax\n"
  593. /* Skip ds, es, fs, gs, orig_ax and ip */
  594. " addl $24, %esp\n"
  595. " popf\n"
  596. #endif
  597. " ret\n");
  598. }
  599. /*
  600. * Called from kretprobe_trampoline
  601. */
  602. static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
  603. {
  604. struct kretprobe_instance *ri = NULL;
  605. struct hlist_head *head, empty_rp;
  606. struct hlist_node *node, *tmp;
  607. unsigned long flags, orig_ret_address = 0;
  608. unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
  609. INIT_HLIST_HEAD(&empty_rp);
  610. kretprobe_hash_lock(current, &head, &flags);
  611. /* fixup registers */
  612. #ifdef CONFIG_X86_64
  613. regs->cs = __KERNEL_CS;
  614. #else
  615. regs->cs = __KERNEL_CS | get_kernel_rpl();
  616. regs->gs = 0;
  617. #endif
  618. regs->ip = trampoline_address;
  619. regs->orig_ax = ~0UL;
  620. /*
  621. * It is possible to have multiple instances associated with a given
  622. * task either because multiple functions in the call path have
  623. * return probes installed on them, and/or more than one
  624. * return probe was registered for a target function.
  625. *
  626. * We can handle this because:
  627. * - instances are always pushed into the head of the list
  628. * - when multiple return probes are registered for the same
  629. * function, the (chronologically) first instance's ret_addr
  630. * will be the real return address, and all the rest will
  631. * point to kretprobe_trampoline.
  632. */
  633. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  634. if (ri->task != current)
  635. /* another task is sharing our hash bucket */
  636. continue;
  637. if (ri->rp && ri->rp->handler) {
  638. __get_cpu_var(current_kprobe) = &ri->rp->kp;
  639. get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
  640. ri->rp->handler(ri, regs);
  641. __get_cpu_var(current_kprobe) = NULL;
  642. }
  643. orig_ret_address = (unsigned long)ri->ret_addr;
  644. recycle_rp_inst(ri, &empty_rp);
  645. if (orig_ret_address != trampoline_address)
  646. /*
  647. * This is the real return address. Any other
  648. * instances associated with this task are for
  649. * other calls deeper on the call stack
  650. */
  651. break;
  652. }
  653. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  654. kretprobe_hash_unlock(current, &flags);
  655. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  656. hlist_del(&ri->hlist);
  657. kfree(ri);
  658. }
  659. return (void *)orig_ret_address;
  660. }
  661. /*
  662. * Called after single-stepping. p->addr is the address of the
  663. * instruction whose first byte has been replaced by the "int 3"
  664. * instruction. To avoid the SMP problems that can occur when we
  665. * temporarily put back the original opcode to single-step, we
  666. * single-stepped a copy of the instruction. The address of this
  667. * copy is p->ainsn.insn.
  668. *
  669. * This function prepares to return from the post-single-step
  670. * interrupt. We have to fix up the stack as follows:
  671. *
  672. * 0) Except in the case of absolute or indirect jump or call instructions,
  673. * the new ip is relative to the copied instruction. We need to make
  674. * it relative to the original instruction.
  675. *
  676. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  677. * flags are set in the just-pushed flags, and may need to be cleared.
  678. *
  679. * 2) If the single-stepped instruction was a call, the return address
  680. * that is atop the stack is the address following the copied instruction.
  681. * We need to make it the address following the original instruction.
  682. *
  683. * If this is the first time we've single-stepped the instruction at
  684. * this probepoint, and the instruction is boostable, boost it: add a
  685. * jump instruction after the copied instruction, that jumps to the next
  686. * instruction after the probepoint.
  687. */
  688. static void __kprobes resume_execution(struct kprobe *p,
  689. struct pt_regs *regs, struct kprobe_ctlblk *kcb)
  690. {
  691. unsigned long *tos = stack_addr(regs);
  692. unsigned long copy_ip = (unsigned long)p->ainsn.insn;
  693. unsigned long orig_ip = (unsigned long)p->addr;
  694. kprobe_opcode_t *insn = p->ainsn.insn;
  695. /*skip the REX prefix*/
  696. if (is_REX_prefix(insn))
  697. insn++;
  698. regs->flags &= ~X86_EFLAGS_TF;
  699. switch (*insn) {
  700. case 0x9c: /* pushfl */
  701. *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
  702. *tos |= kcb->kprobe_old_flags;
  703. break;
  704. case 0xc2: /* iret/ret/lret */
  705. case 0xc3:
  706. case 0xca:
  707. case 0xcb:
  708. case 0xcf:
  709. case 0xea: /* jmp absolute -- ip is correct */
  710. /* ip is already adjusted, no more changes required */
  711. p->ainsn.boostable = 1;
  712. goto no_change;
  713. case 0xe8: /* call relative - Fix return addr */
  714. *tos = orig_ip + (*tos - copy_ip);
  715. break;
  716. #ifdef CONFIG_X86_32
  717. case 0x9a: /* call absolute -- same as call absolute, indirect */
  718. *tos = orig_ip + (*tos - copy_ip);
  719. goto no_change;
  720. #endif
  721. case 0xff:
  722. if ((insn[1] & 0x30) == 0x10) {
  723. /*
  724. * call absolute, indirect
  725. * Fix return addr; ip is correct.
  726. * But this is not boostable
  727. */
  728. *tos = orig_ip + (*tos - copy_ip);
  729. goto no_change;
  730. } else if (((insn[1] & 0x31) == 0x20) ||
  731. ((insn[1] & 0x31) == 0x21)) {
  732. /*
  733. * jmp near and far, absolute indirect
  734. * ip is correct. And this is boostable
  735. */
  736. p->ainsn.boostable = 1;
  737. goto no_change;
  738. }
  739. default:
  740. break;
  741. }
  742. if (p->ainsn.boostable == 0) {
  743. if ((regs->ip > copy_ip) &&
  744. (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
  745. /*
  746. * These instructions can be executed directly if it
  747. * jumps back to correct address.
  748. */
  749. set_jmp_op((void *)regs->ip,
  750. (void *)orig_ip + (regs->ip - copy_ip));
  751. p->ainsn.boostable = 1;
  752. } else {
  753. p->ainsn.boostable = -1;
  754. }
  755. }
  756. regs->ip += orig_ip - copy_ip;
  757. no_change:
  758. restore_btf();
  759. }
  760. /*
  761. * Interrupts are disabled on entry as trap1 is an interrupt gate and they
  762. * remain disabled thoroughout this function.
  763. */
  764. static int __kprobes post_kprobe_handler(struct pt_regs *regs)
  765. {
  766. struct kprobe *cur = kprobe_running();
  767. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  768. if (!cur)
  769. return 0;
  770. resume_execution(cur, regs, kcb);
  771. regs->flags |= kcb->kprobe_saved_flags;
  772. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  773. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  774. cur->post_handler(cur, regs, 0);
  775. }
  776. /* Restore back the original saved kprobes variables and continue. */
  777. if (kcb->kprobe_status == KPROBE_REENTER) {
  778. restore_previous_kprobe(kcb);
  779. goto out;
  780. }
  781. reset_current_kprobe();
  782. out:
  783. preempt_enable_no_resched();
  784. /*
  785. * if somebody else is singlestepping across a probe point, flags
  786. * will have TF set, in which case, continue the remaining processing
  787. * of do_debug, as if this is not a probe hit.
  788. */
  789. if (regs->flags & X86_EFLAGS_TF)
  790. return 0;
  791. return 1;
  792. }
  793. int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  794. {
  795. struct kprobe *cur = kprobe_running();
  796. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  797. switch (kcb->kprobe_status) {
  798. case KPROBE_HIT_SS:
  799. case KPROBE_REENTER:
  800. /*
  801. * We are here because the instruction being single
  802. * stepped caused a page fault. We reset the current
  803. * kprobe and the ip points back to the probe address
  804. * and allow the page fault handler to continue as a
  805. * normal page fault.
  806. */
  807. regs->ip = (unsigned long)cur->addr;
  808. regs->flags |= kcb->kprobe_old_flags;
  809. if (kcb->kprobe_status == KPROBE_REENTER)
  810. restore_previous_kprobe(kcb);
  811. else
  812. reset_current_kprobe();
  813. preempt_enable_no_resched();
  814. break;
  815. case KPROBE_HIT_ACTIVE:
  816. case KPROBE_HIT_SSDONE:
  817. /*
  818. * We increment the nmissed count for accounting,
  819. * we can also use npre/npostfault count for accounting
  820. * these specific fault cases.
  821. */
  822. kprobes_inc_nmissed_count(cur);
  823. /*
  824. * We come here because instructions in the pre/post
  825. * handler caused the page_fault, this could happen
  826. * if handler tries to access user space by
  827. * copy_from_user(), get_user() etc. Let the
  828. * user-specified handler try to fix it first.
  829. */
  830. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  831. return 1;
  832. /*
  833. * In case the user-specified fault handler returned
  834. * zero, try to fix up.
  835. */
  836. if (fixup_exception(regs))
  837. return 1;
  838. /*
  839. * fixup routine could not handle it,
  840. * Let do_page_fault() fix it.
  841. */
  842. break;
  843. default:
  844. break;
  845. }
  846. return 0;
  847. }
  848. /*
  849. * Wrapper routine for handling exceptions.
  850. */
  851. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  852. unsigned long val, void *data)
  853. {
  854. struct die_args *args = data;
  855. int ret = NOTIFY_DONE;
  856. if (args->regs && user_mode_vm(args->regs))
  857. return ret;
  858. switch (val) {
  859. case DIE_INT3:
  860. if (kprobe_handler(args->regs))
  861. ret = NOTIFY_STOP;
  862. break;
  863. case DIE_DEBUG:
  864. if (post_kprobe_handler(args->regs)) {
  865. /*
  866. * Reset the BS bit in dr6 (pointed by args->err) to
  867. * denote completion of processing
  868. */
  869. (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
  870. ret = NOTIFY_STOP;
  871. }
  872. break;
  873. case DIE_GPF:
  874. /*
  875. * To be potentially processing a kprobe fault and to
  876. * trust the result from kprobe_running(), we have
  877. * be non-preemptible.
  878. */
  879. if (!preemptible() && kprobe_running() &&
  880. kprobe_fault_handler(args->regs, args->trapnr))
  881. ret = NOTIFY_STOP;
  882. break;
  883. default:
  884. break;
  885. }
  886. return ret;
  887. }
  888. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  889. {
  890. struct jprobe *jp = container_of(p, struct jprobe, kp);
  891. unsigned long addr;
  892. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  893. kcb->jprobe_saved_regs = *regs;
  894. kcb->jprobe_saved_sp = stack_addr(regs);
  895. addr = (unsigned long)(kcb->jprobe_saved_sp);
  896. /*
  897. * As Linus pointed out, gcc assumes that the callee
  898. * owns the argument space and could overwrite it, e.g.
  899. * tailcall optimization. So, to be absolutely safe
  900. * we also save and restore enough stack bytes to cover
  901. * the argument area.
  902. */
  903. memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
  904. MIN_STACK_SIZE(addr));
  905. regs->flags &= ~X86_EFLAGS_IF;
  906. trace_hardirqs_off();
  907. regs->ip = (unsigned long)(jp->entry);
  908. return 1;
  909. }
  910. void __kprobes jprobe_return(void)
  911. {
  912. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  913. asm volatile (
  914. #ifdef CONFIG_X86_64
  915. " xchg %%rbx,%%rsp \n"
  916. #else
  917. " xchgl %%ebx,%%esp \n"
  918. #endif
  919. " int3 \n"
  920. " .globl jprobe_return_end\n"
  921. " jprobe_return_end: \n"
  922. " nop \n"::"b"
  923. (kcb->jprobe_saved_sp):"memory");
  924. }
  925. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  926. {
  927. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  928. u8 *addr = (u8 *) (regs->ip - 1);
  929. struct jprobe *jp = container_of(p, struct jprobe, kp);
  930. if ((addr > (u8 *) jprobe_return) &&
  931. (addr < (u8 *) jprobe_return_end)) {
  932. if (stack_addr(regs) != kcb->jprobe_saved_sp) {
  933. struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
  934. printk(KERN_ERR
  935. "current sp %p does not match saved sp %p\n",
  936. stack_addr(regs), kcb->jprobe_saved_sp);
  937. printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
  938. show_registers(saved_regs);
  939. printk(KERN_ERR "Current registers\n");
  940. show_registers(regs);
  941. BUG();
  942. }
  943. *regs = kcb->jprobe_saved_regs;
  944. memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp),
  945. kcb->jprobes_stack,
  946. MIN_STACK_SIZE(kcb->jprobe_saved_sp));
  947. preempt_enable_no_resched();
  948. return 1;
  949. }
  950. return 0;
  951. }
  952. int __init arch_init_kprobes(void)
  953. {
  954. return 0;
  955. }
  956. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  957. {
  958. return 0;
  959. }