kprobes.c 30 KB

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