kprobes.c 29 KB

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