kprobes.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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 (alternatives_text_reserved(p->addr, p->addr))
  309. return -EINVAL;
  310. if (!can_probe((unsigned long)p->addr))
  311. return -EILSEQ;
  312. /* insn: must be on special executable page on x86. */
  313. p->ainsn.insn = get_insn_slot();
  314. if (!p->ainsn.insn)
  315. return -ENOMEM;
  316. arch_copy_kprobe(p);
  317. return 0;
  318. }
  319. void __kprobes arch_arm_kprobe(struct kprobe *p)
  320. {
  321. text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
  322. }
  323. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  324. {
  325. text_poke(p->addr, &p->opcode, 1);
  326. }
  327. void __kprobes arch_remove_kprobe(struct kprobe *p)
  328. {
  329. if (p->ainsn.insn) {
  330. free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
  331. p->ainsn.insn = NULL;
  332. }
  333. }
  334. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  335. {
  336. kcb->prev_kprobe.kp = kprobe_running();
  337. kcb->prev_kprobe.status = kcb->kprobe_status;
  338. kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
  339. kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
  340. }
  341. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  342. {
  343. __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
  344. kcb->kprobe_status = kcb->prev_kprobe.status;
  345. kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
  346. kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
  347. }
  348. static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  349. struct kprobe_ctlblk *kcb)
  350. {
  351. __get_cpu_var(current_kprobe) = p;
  352. kcb->kprobe_saved_flags = kcb->kprobe_old_flags
  353. = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
  354. if (is_IF_modifier(p->ainsn.insn))
  355. kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
  356. }
  357. static void __kprobes clear_btf(void)
  358. {
  359. if (test_thread_flag(TIF_DEBUGCTLMSR))
  360. update_debugctlmsr(0);
  361. }
  362. static void __kprobes restore_btf(void)
  363. {
  364. if (test_thread_flag(TIF_DEBUGCTLMSR))
  365. update_debugctlmsr(current->thread.debugctlmsr);
  366. }
  367. static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
  368. {
  369. clear_btf();
  370. regs->flags |= X86_EFLAGS_TF;
  371. regs->flags &= ~X86_EFLAGS_IF;
  372. /* single step inline if the instruction is an int3 */
  373. if (p->opcode == BREAKPOINT_INSTRUCTION)
  374. regs->ip = (unsigned long)p->addr;
  375. else
  376. regs->ip = (unsigned long)p->ainsn.insn;
  377. }
  378. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  379. struct pt_regs *regs)
  380. {
  381. unsigned long *sara = stack_addr(regs);
  382. ri->ret_addr = (kprobe_opcode_t *) *sara;
  383. /* Replace the return addr with trampoline addr */
  384. *sara = (unsigned long) &kretprobe_trampoline;
  385. }
  386. static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs,
  387. struct kprobe_ctlblk *kcb)
  388. {
  389. #if !defined(CONFIG_PREEMPT)
  390. if (p->ainsn.boostable == 1 && !p->post_handler) {
  391. /* Boost up -- we can execute copied instructions directly */
  392. reset_current_kprobe();
  393. regs->ip = (unsigned long)p->ainsn.insn;
  394. preempt_enable_no_resched();
  395. return;
  396. }
  397. #endif
  398. prepare_singlestep(p, regs);
  399. kcb->kprobe_status = KPROBE_HIT_SS;
  400. }
  401. /*
  402. * We have reentered the kprobe_handler(), since another probe was hit while
  403. * within the handler. We save the original kprobes variables and just single
  404. * step on the instruction of the new probe without calling any user handlers.
  405. */
  406. static int __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
  407. struct kprobe_ctlblk *kcb)
  408. {
  409. switch (kcb->kprobe_status) {
  410. case KPROBE_HIT_SSDONE:
  411. case KPROBE_HIT_ACTIVE:
  412. save_previous_kprobe(kcb);
  413. set_current_kprobe(p, regs, kcb);
  414. kprobes_inc_nmissed_count(p);
  415. prepare_singlestep(p, regs);
  416. kcb->kprobe_status = KPROBE_REENTER;
  417. break;
  418. case KPROBE_HIT_SS:
  419. /* A probe has been hit in the codepath leading up to, or just
  420. * after, single-stepping of a probed instruction. This entire
  421. * codepath should strictly reside in .kprobes.text section.
  422. * Raise a BUG or we'll continue in an endless reentering loop
  423. * and eventually a stack overflow.
  424. */
  425. printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n",
  426. p->addr);
  427. dump_kprobe(p);
  428. BUG();
  429. default:
  430. /* impossible cases */
  431. WARN_ON(1);
  432. return 0;
  433. }
  434. return 1;
  435. }
  436. /*
  437. * Interrupts are disabled on entry as trap3 is an interrupt gate and they
  438. * remain disabled throughout this function.
  439. */
  440. static int __kprobes kprobe_handler(struct pt_regs *regs)
  441. {
  442. kprobe_opcode_t *addr;
  443. struct kprobe *p;
  444. struct kprobe_ctlblk *kcb;
  445. addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
  446. if (*addr != BREAKPOINT_INSTRUCTION) {
  447. /*
  448. * The breakpoint instruction was removed right
  449. * after we hit it. Another cpu has removed
  450. * either a probepoint or a debugger breakpoint
  451. * at this address. In either case, no further
  452. * handling of this interrupt is appropriate.
  453. * Back up over the (now missing) int3 and run
  454. * the original instruction.
  455. */
  456. regs->ip = (unsigned long)addr;
  457. return 1;
  458. }
  459. /*
  460. * We don't want to be preempted for the entire
  461. * duration of kprobe processing. We conditionally
  462. * re-enable preemption at the end of this function,
  463. * and also in reenter_kprobe() and setup_singlestep().
  464. */
  465. preempt_disable();
  466. kcb = get_kprobe_ctlblk();
  467. p = get_kprobe(addr);
  468. if (p) {
  469. if (kprobe_running()) {
  470. if (reenter_kprobe(p, regs, kcb))
  471. return 1;
  472. } else {
  473. set_current_kprobe(p, regs, kcb);
  474. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  475. /*
  476. * If we have no pre-handler or it returned 0, we
  477. * continue with normal processing. If we have a
  478. * pre-handler and it returned non-zero, it prepped
  479. * for calling the break_handler below on re-entry
  480. * for jprobe processing, so get out doing nothing
  481. * more here.
  482. */
  483. if (!p->pre_handler || !p->pre_handler(p, regs))
  484. setup_singlestep(p, regs, kcb);
  485. return 1;
  486. }
  487. } else if (kprobe_running()) {
  488. p = __get_cpu_var(current_kprobe);
  489. if (p->break_handler && p->break_handler(p, regs)) {
  490. setup_singlestep(p, regs, kcb);
  491. return 1;
  492. }
  493. } /* else: not a kprobe fault; let the kernel handle it */
  494. preempt_enable_no_resched();
  495. return 0;
  496. }
  497. /*
  498. * When a retprobed function returns, this code saves registers and
  499. * calls trampoline_handler() runs, which calls the kretprobe's handler.
  500. */
  501. static void __used __kprobes kretprobe_trampoline_holder(void)
  502. {
  503. asm volatile (
  504. ".global kretprobe_trampoline\n"
  505. "kretprobe_trampoline: \n"
  506. #ifdef CONFIG_X86_64
  507. /* We don't bother saving the ss register */
  508. " pushq %rsp\n"
  509. " pushfq\n"
  510. /*
  511. * Skip cs, ip, orig_ax.
  512. * trampoline_handler() will plug in these values
  513. */
  514. " subq $24, %rsp\n"
  515. " pushq %rdi\n"
  516. " pushq %rsi\n"
  517. " pushq %rdx\n"
  518. " pushq %rcx\n"
  519. " pushq %rax\n"
  520. " pushq %r8\n"
  521. " pushq %r9\n"
  522. " pushq %r10\n"
  523. " pushq %r11\n"
  524. " pushq %rbx\n"
  525. " pushq %rbp\n"
  526. " pushq %r12\n"
  527. " pushq %r13\n"
  528. " pushq %r14\n"
  529. " pushq %r15\n"
  530. " movq %rsp, %rdi\n"
  531. " call trampoline_handler\n"
  532. /* Replace saved sp with true return address. */
  533. " movq %rax, 152(%rsp)\n"
  534. " popq %r15\n"
  535. " popq %r14\n"
  536. " popq %r13\n"
  537. " popq %r12\n"
  538. " popq %rbp\n"
  539. " popq %rbx\n"
  540. " popq %r11\n"
  541. " popq %r10\n"
  542. " popq %r9\n"
  543. " popq %r8\n"
  544. " popq %rax\n"
  545. " popq %rcx\n"
  546. " popq %rdx\n"
  547. " popq %rsi\n"
  548. " popq %rdi\n"
  549. /* Skip orig_ax, ip, cs */
  550. " addq $24, %rsp\n"
  551. " popfq\n"
  552. #else
  553. " pushf\n"
  554. /*
  555. * Skip cs, ip, orig_ax and gs.
  556. * trampoline_handler() will plug in these values
  557. */
  558. " subl $16, %esp\n"
  559. " pushl %fs\n"
  560. " pushl %es\n"
  561. " pushl %ds\n"
  562. " pushl %eax\n"
  563. " pushl %ebp\n"
  564. " pushl %edi\n"
  565. " pushl %esi\n"
  566. " pushl %edx\n"
  567. " pushl %ecx\n"
  568. " pushl %ebx\n"
  569. " movl %esp, %eax\n"
  570. " call trampoline_handler\n"
  571. /* Move flags to cs */
  572. " movl 56(%esp), %edx\n"
  573. " movl %edx, 52(%esp)\n"
  574. /* Replace saved flags with true return address. */
  575. " movl %eax, 56(%esp)\n"
  576. " popl %ebx\n"
  577. " popl %ecx\n"
  578. " popl %edx\n"
  579. " popl %esi\n"
  580. " popl %edi\n"
  581. " popl %ebp\n"
  582. " popl %eax\n"
  583. /* Skip ds, es, fs, gs, orig_ax and ip */
  584. " addl $24, %esp\n"
  585. " popf\n"
  586. #endif
  587. " ret\n");
  588. }
  589. /*
  590. * Called from kretprobe_trampoline
  591. */
  592. static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
  593. {
  594. struct kretprobe_instance *ri = NULL;
  595. struct hlist_head *head, empty_rp;
  596. struct hlist_node *node, *tmp;
  597. unsigned long flags, orig_ret_address = 0;
  598. unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
  599. INIT_HLIST_HEAD(&empty_rp);
  600. kretprobe_hash_lock(current, &head, &flags);
  601. /* fixup registers */
  602. #ifdef CONFIG_X86_64
  603. regs->cs = __KERNEL_CS;
  604. #else
  605. regs->cs = __KERNEL_CS | get_kernel_rpl();
  606. regs->gs = 0;
  607. #endif
  608. regs->ip = trampoline_address;
  609. regs->orig_ax = ~0UL;
  610. /*
  611. * It is possible to have multiple instances associated with a given
  612. * task either because multiple functions in the call path have
  613. * return probes installed on them, and/or more than one
  614. * return probe was registered for a target function.
  615. *
  616. * We can handle this because:
  617. * - instances are always pushed into the head of the list
  618. * - when multiple return probes are registered for the same
  619. * function, the (chronologically) first instance's ret_addr
  620. * will be the real return address, and all the rest will
  621. * point to kretprobe_trampoline.
  622. */
  623. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  624. if (ri->task != current)
  625. /* another task is sharing our hash bucket */
  626. continue;
  627. if (ri->rp && ri->rp->handler) {
  628. __get_cpu_var(current_kprobe) = &ri->rp->kp;
  629. get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
  630. ri->rp->handler(ri, regs);
  631. __get_cpu_var(current_kprobe) = NULL;
  632. }
  633. orig_ret_address = (unsigned long)ri->ret_addr;
  634. recycle_rp_inst(ri, &empty_rp);
  635. if (orig_ret_address != trampoline_address)
  636. /*
  637. * This is the real return address. Any other
  638. * instances associated with this task are for
  639. * other calls deeper on the call stack
  640. */
  641. break;
  642. }
  643. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  644. kretprobe_hash_unlock(current, &flags);
  645. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  646. hlist_del(&ri->hlist);
  647. kfree(ri);
  648. }
  649. return (void *)orig_ret_address;
  650. }
  651. /*
  652. * Called after single-stepping. p->addr is the address of the
  653. * instruction whose first byte has been replaced by the "int 3"
  654. * instruction. To avoid the SMP problems that can occur when we
  655. * temporarily put back the original opcode to single-step, we
  656. * single-stepped a copy of the instruction. The address of this
  657. * copy is p->ainsn.insn.
  658. *
  659. * This function prepares to return from the post-single-step
  660. * interrupt. We have to fix up the stack as follows:
  661. *
  662. * 0) Except in the case of absolute or indirect jump or call instructions,
  663. * the new ip is relative to the copied instruction. We need to make
  664. * it relative to the original instruction.
  665. *
  666. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  667. * flags are set in the just-pushed flags, and may need to be cleared.
  668. *
  669. * 2) If the single-stepped instruction was a call, the return address
  670. * that is atop the stack is the address following the copied instruction.
  671. * We need to make it the address following the original instruction.
  672. *
  673. * If this is the first time we've single-stepped the instruction at
  674. * this probepoint, and the instruction is boostable, boost it: add a
  675. * jump instruction after the copied instruction, that jumps to the next
  676. * instruction after the probepoint.
  677. */
  678. static void __kprobes resume_execution(struct kprobe *p,
  679. struct pt_regs *regs, struct kprobe_ctlblk *kcb)
  680. {
  681. unsigned long *tos = stack_addr(regs);
  682. unsigned long copy_ip = (unsigned long)p->ainsn.insn;
  683. unsigned long orig_ip = (unsigned long)p->addr;
  684. kprobe_opcode_t *insn = p->ainsn.insn;
  685. /*skip the REX prefix*/
  686. if (is_REX_prefix(insn))
  687. insn++;
  688. regs->flags &= ~X86_EFLAGS_TF;
  689. switch (*insn) {
  690. case 0x9c: /* pushfl */
  691. *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
  692. *tos |= kcb->kprobe_old_flags;
  693. break;
  694. case 0xc2: /* iret/ret/lret */
  695. case 0xc3:
  696. case 0xca:
  697. case 0xcb:
  698. case 0xcf:
  699. case 0xea: /* jmp absolute -- ip is correct */
  700. /* ip is already adjusted, no more changes required */
  701. p->ainsn.boostable = 1;
  702. goto no_change;
  703. case 0xe8: /* call relative - Fix return addr */
  704. *tos = orig_ip + (*tos - copy_ip);
  705. break;
  706. #ifdef CONFIG_X86_32
  707. case 0x9a: /* call absolute -- same as call absolute, indirect */
  708. *tos = orig_ip + (*tos - copy_ip);
  709. goto no_change;
  710. #endif
  711. case 0xff:
  712. if ((insn[1] & 0x30) == 0x10) {
  713. /*
  714. * call absolute, indirect
  715. * Fix return addr; ip is correct.
  716. * But this is not boostable
  717. */
  718. *tos = orig_ip + (*tos - copy_ip);
  719. goto no_change;
  720. } else if (((insn[1] & 0x31) == 0x20) ||
  721. ((insn[1] & 0x31) == 0x21)) {
  722. /*
  723. * jmp near and far, absolute indirect
  724. * ip is correct. And this is boostable
  725. */
  726. p->ainsn.boostable = 1;
  727. goto no_change;
  728. }
  729. default:
  730. break;
  731. }
  732. if (p->ainsn.boostable == 0) {
  733. if ((regs->ip > copy_ip) &&
  734. (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
  735. /*
  736. * These instructions can be executed directly if it
  737. * jumps back to correct address.
  738. */
  739. set_jmp_op((void *)regs->ip,
  740. (void *)orig_ip + (regs->ip - copy_ip));
  741. p->ainsn.boostable = 1;
  742. } else {
  743. p->ainsn.boostable = -1;
  744. }
  745. }
  746. regs->ip += orig_ip - copy_ip;
  747. no_change:
  748. restore_btf();
  749. }
  750. /*
  751. * Interrupts are disabled on entry as trap1 is an interrupt gate and they
  752. * remain disabled throughout this function.
  753. */
  754. static int __kprobes post_kprobe_handler(struct pt_regs *regs)
  755. {
  756. struct kprobe *cur = kprobe_running();
  757. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  758. if (!cur)
  759. return 0;
  760. resume_execution(cur, regs, kcb);
  761. regs->flags |= kcb->kprobe_saved_flags;
  762. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  763. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  764. cur->post_handler(cur, regs, 0);
  765. }
  766. /* Restore back the original saved kprobes variables and continue. */
  767. if (kcb->kprobe_status == KPROBE_REENTER) {
  768. restore_previous_kprobe(kcb);
  769. goto out;
  770. }
  771. reset_current_kprobe();
  772. out:
  773. preempt_enable_no_resched();
  774. /*
  775. * if somebody else is singlestepping across a probe point, flags
  776. * will have TF set, in which case, continue the remaining processing
  777. * of do_debug, as if this is not a probe hit.
  778. */
  779. if (regs->flags & X86_EFLAGS_TF)
  780. return 0;
  781. return 1;
  782. }
  783. int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  784. {
  785. struct kprobe *cur = kprobe_running();
  786. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  787. switch (kcb->kprobe_status) {
  788. case KPROBE_HIT_SS:
  789. case KPROBE_REENTER:
  790. /*
  791. * We are here because the instruction being single
  792. * stepped caused a page fault. We reset the current
  793. * kprobe and the ip points back to the probe address
  794. * and allow the page fault handler to continue as a
  795. * normal page fault.
  796. */
  797. regs->ip = (unsigned long)cur->addr;
  798. regs->flags |= kcb->kprobe_old_flags;
  799. if (kcb->kprobe_status == KPROBE_REENTER)
  800. restore_previous_kprobe(kcb);
  801. else
  802. reset_current_kprobe();
  803. preempt_enable_no_resched();
  804. break;
  805. case KPROBE_HIT_ACTIVE:
  806. case KPROBE_HIT_SSDONE:
  807. /*
  808. * We increment the nmissed count for accounting,
  809. * we can also use npre/npostfault count for accounting
  810. * these specific fault cases.
  811. */
  812. kprobes_inc_nmissed_count(cur);
  813. /*
  814. * We come here because instructions in the pre/post
  815. * handler caused the page_fault, this could happen
  816. * if handler tries to access user space by
  817. * copy_from_user(), get_user() etc. Let the
  818. * user-specified handler try to fix it first.
  819. */
  820. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  821. return 1;
  822. /*
  823. * In case the user-specified fault handler returned
  824. * zero, try to fix up.
  825. */
  826. if (fixup_exception(regs))
  827. return 1;
  828. /*
  829. * fixup routine could not handle it,
  830. * Let do_page_fault() fix it.
  831. */
  832. break;
  833. default:
  834. break;
  835. }
  836. return 0;
  837. }
  838. /*
  839. * Wrapper routine for handling exceptions.
  840. */
  841. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  842. unsigned long val, void *data)
  843. {
  844. struct die_args *args = data;
  845. int ret = NOTIFY_DONE;
  846. if (args->regs && user_mode_vm(args->regs))
  847. return ret;
  848. switch (val) {
  849. case DIE_INT3:
  850. if (kprobe_handler(args->regs))
  851. ret = NOTIFY_STOP;
  852. break;
  853. case DIE_DEBUG:
  854. if (post_kprobe_handler(args->regs)) {
  855. /*
  856. * Reset the BS bit in dr6 (pointed by args->err) to
  857. * denote completion of processing
  858. */
  859. (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
  860. ret = NOTIFY_STOP;
  861. }
  862. break;
  863. case DIE_GPF:
  864. /*
  865. * To be potentially processing a kprobe fault and to
  866. * trust the result from kprobe_running(), we have
  867. * be non-preemptible.
  868. */
  869. if (!preemptible() && kprobe_running() &&
  870. kprobe_fault_handler(args->regs, args->trapnr))
  871. ret = NOTIFY_STOP;
  872. break;
  873. default:
  874. break;
  875. }
  876. return ret;
  877. }
  878. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  879. {
  880. struct jprobe *jp = container_of(p, struct jprobe, kp);
  881. unsigned long addr;
  882. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  883. kcb->jprobe_saved_regs = *regs;
  884. kcb->jprobe_saved_sp = stack_addr(regs);
  885. addr = (unsigned long)(kcb->jprobe_saved_sp);
  886. /*
  887. * As Linus pointed out, gcc assumes that the callee
  888. * owns the argument space and could overwrite it, e.g.
  889. * tailcall optimization. So, to be absolutely safe
  890. * we also save and restore enough stack bytes to cover
  891. * the argument area.
  892. */
  893. memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
  894. MIN_STACK_SIZE(addr));
  895. regs->flags &= ~X86_EFLAGS_IF;
  896. trace_hardirqs_off();
  897. regs->ip = (unsigned long)(jp->entry);
  898. return 1;
  899. }
  900. void __kprobes jprobe_return(void)
  901. {
  902. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  903. asm volatile (
  904. #ifdef CONFIG_X86_64
  905. " xchg %%rbx,%%rsp \n"
  906. #else
  907. " xchgl %%ebx,%%esp \n"
  908. #endif
  909. " int3 \n"
  910. " .globl jprobe_return_end\n"
  911. " jprobe_return_end: \n"
  912. " nop \n"::"b"
  913. (kcb->jprobe_saved_sp):"memory");
  914. }
  915. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  916. {
  917. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  918. u8 *addr = (u8 *) (regs->ip - 1);
  919. struct jprobe *jp = container_of(p, struct jprobe, kp);
  920. if ((addr > (u8 *) jprobe_return) &&
  921. (addr < (u8 *) jprobe_return_end)) {
  922. if (stack_addr(regs) != kcb->jprobe_saved_sp) {
  923. struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
  924. printk(KERN_ERR
  925. "current sp %p does not match saved sp %p\n",
  926. stack_addr(regs), kcb->jprobe_saved_sp);
  927. printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
  928. show_registers(saved_regs);
  929. printk(KERN_ERR "Current registers\n");
  930. show_registers(regs);
  931. BUG();
  932. }
  933. *regs = kcb->jprobe_saved_regs;
  934. memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp),
  935. kcb->jprobes_stack,
  936. MIN_STACK_SIZE(kcb->jprobe_saved_sp));
  937. preempt_enable_no_resched();
  938. return 1;
  939. }
  940. return 0;
  941. }
  942. int __init arch_init_kprobes(void)
  943. {
  944. return 0;
  945. }
  946. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  947. {
  948. return 0;
  949. }