core.c 32 KB

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