core.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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, 1) , /* 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. unsigned long faddr;
  207. kp = get_kprobe((void *)addr);
  208. faddr = ftrace_location(addr);
  209. /*
  210. * Addresses inside the ftrace location are refused by
  211. * arch_check_ftrace_location(). Something went terribly wrong
  212. * if such an address is checked here.
  213. */
  214. if (WARN_ON(faddr && faddr != addr))
  215. return 0UL;
  216. /*
  217. * Use the current code if it is not modified by Kprobe
  218. * and it cannot be modified by ftrace.
  219. */
  220. if (!kp && !faddr)
  221. return addr;
  222. /*
  223. * Basically, kp->ainsn.insn has an original instruction.
  224. * However, RIP-relative instruction can not do single-stepping
  225. * at different place, __copy_instruction() tweaks the displacement of
  226. * that instruction. In that case, we can't recover the instruction
  227. * from the kp->ainsn.insn.
  228. *
  229. * On the other hand, in case on normal Kprobe, kp->opcode has a copy
  230. * of the first byte of the probed instruction, which is overwritten
  231. * by int3. And the instruction at kp->addr is not modified by kprobes
  232. * except for the first byte, we can recover the original instruction
  233. * from it and kp->opcode.
  234. *
  235. * In case of Kprobes using ftrace, we do not have a copy of
  236. * the original instruction. In fact, the ftrace location might
  237. * be modified at anytime and even could be in an inconsistent state.
  238. * Fortunately, we know that the original code is the ideal 5-byte
  239. * long NOP.
  240. */
  241. memcpy(buf, (void *)addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
  242. if (faddr)
  243. memcpy(buf, ideal_nops[NOP_ATOMIC5], 5);
  244. else
  245. buf[0] = kp->opcode;
  246. return (unsigned long)buf;
  247. }
  248. /*
  249. * Recover the probed instruction at addr for further analysis.
  250. * Caller must lock kprobes by kprobe_mutex, or disable preemption
  251. * for preventing to release referencing kprobes.
  252. * Returns zero if the instruction can not get recovered.
  253. */
  254. unsigned long recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
  255. {
  256. unsigned long __addr;
  257. __addr = __recover_optprobed_insn(buf, addr);
  258. if (__addr != addr)
  259. return __addr;
  260. return __recover_probed_insn(buf, addr);
  261. }
  262. /* Check if paddr is at an instruction boundary */
  263. static int can_probe(unsigned long paddr)
  264. {
  265. unsigned long addr, __addr, offset = 0;
  266. struct insn insn;
  267. kprobe_opcode_t buf[MAX_INSN_SIZE];
  268. if (!kallsyms_lookup_size_offset(paddr, NULL, &offset))
  269. return 0;
  270. /* Decode instructions */
  271. addr = paddr - offset;
  272. while (addr < paddr) {
  273. /*
  274. * Check if the instruction has been modified by another
  275. * kprobe, in which case we replace the breakpoint by the
  276. * original instruction in our buffer.
  277. * Also, jump optimization will change the breakpoint to
  278. * relative-jump. Since the relative-jump itself is
  279. * normally used, we just go through if there is no kprobe.
  280. */
  281. __addr = recover_probed_instruction(buf, addr);
  282. if (!__addr)
  283. return 0;
  284. kernel_insn_init(&insn, (void *)__addr, MAX_INSN_SIZE);
  285. insn_get_length(&insn);
  286. /*
  287. * Another debugging subsystem might insert this breakpoint.
  288. * In that case, we can't recover it.
  289. */
  290. if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
  291. return 0;
  292. addr += insn.length;
  293. }
  294. return (addr == paddr);
  295. }
  296. /*
  297. * Returns non-zero if opcode modifies the interrupt flag.
  298. */
  299. static int is_IF_modifier(kprobe_opcode_t *insn)
  300. {
  301. /* Skip prefixes */
  302. insn = skip_prefixes(insn);
  303. switch (*insn) {
  304. case 0xfa: /* cli */
  305. case 0xfb: /* sti */
  306. case 0xcf: /* iret/iretd */
  307. case 0x9d: /* popf/popfd */
  308. return 1;
  309. }
  310. return 0;
  311. }
  312. /*
  313. * Copy an instruction and adjust the displacement if the instruction
  314. * uses the %rip-relative addressing mode.
  315. * If it does, Return the address of the 32-bit displacement word.
  316. * If not, return null.
  317. * Only applicable to 64-bit x86.
  318. */
  319. int __copy_instruction(u8 *dest, u8 *src)
  320. {
  321. struct insn insn;
  322. kprobe_opcode_t buf[MAX_INSN_SIZE];
  323. int length;
  324. unsigned long recovered_insn =
  325. recover_probed_instruction(buf, (unsigned long)src);
  326. if (!recovered_insn)
  327. return 0;
  328. kernel_insn_init(&insn, (void *)recovered_insn, MAX_INSN_SIZE);
  329. insn_get_length(&insn);
  330. length = insn.length;
  331. /* Another subsystem puts a breakpoint, failed to recover */
  332. if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
  333. return 0;
  334. memcpy(dest, insn.kaddr, length);
  335. #ifdef CONFIG_X86_64
  336. if (insn_rip_relative(&insn)) {
  337. s64 newdisp;
  338. u8 *disp;
  339. kernel_insn_init(&insn, dest, length);
  340. insn_get_displacement(&insn);
  341. /*
  342. * The copied instruction uses the %rip-relative addressing
  343. * mode. Adjust the displacement for the difference between
  344. * the original location of this instruction and the location
  345. * of the copy that will actually be run. The tricky bit here
  346. * is making sure that the sign extension happens correctly in
  347. * this calculation, since we need a signed 32-bit result to
  348. * be sign-extended to 64 bits when it's added to the %rip
  349. * value and yield the same 64-bit result that the sign-
  350. * extension of the original signed 32-bit displacement would
  351. * have given.
  352. */
  353. newdisp = (u8 *) src + (s64) insn.displacement.value - (u8 *) dest;
  354. if ((s64) (s32) newdisp != newdisp) {
  355. pr_err("Kprobes error: new displacement does not fit into s32 (%llx)\n", newdisp);
  356. pr_err("\tSrc: %p, Dest: %p, old disp: %x\n", src, dest, insn.displacement.value);
  357. return 0;
  358. }
  359. disp = (u8 *) dest + insn_offset_displacement(&insn);
  360. *(s32 *) disp = (s32) newdisp;
  361. }
  362. #endif
  363. return length;
  364. }
  365. static int arch_copy_kprobe(struct kprobe *p)
  366. {
  367. int ret;
  368. /* Copy an instruction with recovering if other optprobe modifies it.*/
  369. ret = __copy_instruction(p->ainsn.insn, p->addr);
  370. if (!ret)
  371. return -EINVAL;
  372. /*
  373. * __copy_instruction can modify the displacement of the instruction,
  374. * but it doesn't affect boostable check.
  375. */
  376. if (can_boost(p->ainsn.insn))
  377. p->ainsn.boostable = 0;
  378. else
  379. p->ainsn.boostable = -1;
  380. /* Check whether the instruction modifies Interrupt Flag or not */
  381. p->ainsn.if_modifier = is_IF_modifier(p->ainsn.insn);
  382. /* Also, displacement change doesn't affect the first byte */
  383. p->opcode = p->ainsn.insn[0];
  384. return 0;
  385. }
  386. int arch_prepare_kprobe(struct kprobe *p)
  387. {
  388. if (alternatives_text_reserved(p->addr, p->addr))
  389. return -EINVAL;
  390. if (!can_probe((unsigned long)p->addr))
  391. return -EILSEQ;
  392. /* insn: must be on special executable page on x86. */
  393. p->ainsn.insn = get_insn_slot();
  394. if (!p->ainsn.insn)
  395. return -ENOMEM;
  396. return arch_copy_kprobe(p);
  397. }
  398. void arch_arm_kprobe(struct kprobe *p)
  399. {
  400. text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
  401. }
  402. void arch_disarm_kprobe(struct kprobe *p)
  403. {
  404. text_poke(p->addr, &p->opcode, 1);
  405. }
  406. void arch_remove_kprobe(struct kprobe *p)
  407. {
  408. if (p->ainsn.insn) {
  409. free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
  410. p->ainsn.insn = NULL;
  411. }
  412. }
  413. static nokprobe_inline void
  414. save_previous_kprobe(struct kprobe_ctlblk *kcb)
  415. {
  416. kcb->prev_kprobe.kp = kprobe_running();
  417. kcb->prev_kprobe.status = kcb->kprobe_status;
  418. kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
  419. kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
  420. }
  421. static nokprobe_inline void
  422. restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  423. {
  424. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  425. kcb->kprobe_status = kcb->prev_kprobe.status;
  426. kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
  427. kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
  428. }
  429. static nokprobe_inline void
  430. set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  431. struct kprobe_ctlblk *kcb)
  432. {
  433. __this_cpu_write(current_kprobe, p);
  434. kcb->kprobe_saved_flags = kcb->kprobe_old_flags
  435. = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
  436. if (p->ainsn.if_modifier)
  437. kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
  438. }
  439. static nokprobe_inline void clear_btf(void)
  440. {
  441. if (test_thread_flag(TIF_BLOCKSTEP)) {
  442. unsigned long debugctl = get_debugctlmsr();
  443. debugctl &= ~DEBUGCTLMSR_BTF;
  444. update_debugctlmsr(debugctl);
  445. }
  446. }
  447. static nokprobe_inline void restore_btf(void)
  448. {
  449. if (test_thread_flag(TIF_BLOCKSTEP)) {
  450. unsigned long debugctl = get_debugctlmsr();
  451. debugctl |= DEBUGCTLMSR_BTF;
  452. update_debugctlmsr(debugctl);
  453. }
  454. }
  455. void arch_prepare_kretprobe(struct kretprobe_instance *ri, struct pt_regs *regs)
  456. {
  457. unsigned long *sara = stack_addr(regs);
  458. ri->ret_addr = (kprobe_opcode_t *) *sara;
  459. /* Replace the return addr with trampoline addr */
  460. *sara = (unsigned long) &kretprobe_trampoline;
  461. }
  462. NOKPROBE_SYMBOL(arch_prepare_kretprobe);
  463. static void setup_singlestep(struct kprobe *p, struct pt_regs *regs,
  464. struct kprobe_ctlblk *kcb, int reenter)
  465. {
  466. if (setup_detour_execution(p, regs, reenter))
  467. return;
  468. #if !defined(CONFIG_PREEMPT)
  469. if (p->ainsn.boostable == 1 && !p->post_handler) {
  470. /* Boost up -- we can execute copied instructions directly */
  471. if (!reenter)
  472. reset_current_kprobe();
  473. /*
  474. * Reentering boosted probe doesn't reset current_kprobe,
  475. * nor set current_kprobe, because it doesn't use single
  476. * stepping.
  477. */
  478. regs->ip = (unsigned long)p->ainsn.insn;
  479. preempt_enable_no_resched();
  480. return;
  481. }
  482. #endif
  483. if (reenter) {
  484. save_previous_kprobe(kcb);
  485. set_current_kprobe(p, regs, kcb);
  486. kcb->kprobe_status = KPROBE_REENTER;
  487. } else
  488. kcb->kprobe_status = KPROBE_HIT_SS;
  489. /* Prepare real single stepping */
  490. clear_btf();
  491. regs->flags |= X86_EFLAGS_TF;
  492. regs->flags &= ~X86_EFLAGS_IF;
  493. /* single step inline if the instruction is an int3 */
  494. if (p->opcode == BREAKPOINT_INSTRUCTION)
  495. regs->ip = (unsigned long)p->addr;
  496. else
  497. regs->ip = (unsigned long)p->ainsn.insn;
  498. }
  499. NOKPROBE_SYMBOL(setup_singlestep);
  500. /*
  501. * We have reentered the kprobe_handler(), since another probe was hit while
  502. * within the handler. We save the original kprobes variables and just single
  503. * step on the instruction of the new probe without calling any user handlers.
  504. */
  505. static int reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
  506. struct kprobe_ctlblk *kcb)
  507. {
  508. switch (kcb->kprobe_status) {
  509. case KPROBE_HIT_SSDONE:
  510. case KPROBE_HIT_ACTIVE:
  511. case KPROBE_HIT_SS:
  512. kprobes_inc_nmissed_count(p);
  513. setup_singlestep(p, regs, kcb, 1);
  514. break;
  515. case KPROBE_REENTER:
  516. /* A probe has been hit in the codepath leading up to, or just
  517. * after, single-stepping of a probed instruction. This entire
  518. * codepath should strictly reside in .kprobes.text section.
  519. * Raise a BUG or we'll continue in an endless reentering loop
  520. * and eventually a stack overflow.
  521. */
  522. printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n",
  523. p->addr);
  524. dump_kprobe(p);
  525. BUG();
  526. default:
  527. /* impossible cases */
  528. WARN_ON(1);
  529. return 0;
  530. }
  531. return 1;
  532. }
  533. NOKPROBE_SYMBOL(reenter_kprobe);
  534. /*
  535. * Interrupts are disabled on entry as trap3 is an interrupt gate and they
  536. * remain disabled throughout this function.
  537. */
  538. int kprobe_int3_handler(struct pt_regs *regs)
  539. {
  540. kprobe_opcode_t *addr;
  541. struct kprobe *p;
  542. struct kprobe_ctlblk *kcb;
  543. if (user_mode(regs))
  544. return 0;
  545. addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
  546. /*
  547. * We don't want to be preempted for the entire
  548. * duration of kprobe processing. We conditionally
  549. * re-enable preemption at the end of this function,
  550. * and also in reenter_kprobe() and setup_singlestep().
  551. */
  552. preempt_disable();
  553. kcb = get_kprobe_ctlblk();
  554. p = get_kprobe(addr);
  555. if (p) {
  556. if (kprobe_running()) {
  557. if (reenter_kprobe(p, regs, kcb))
  558. return 1;
  559. } else {
  560. set_current_kprobe(p, regs, kcb);
  561. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  562. /*
  563. * If we have no pre-handler or it returned 0, we
  564. * continue with normal processing. If we have a
  565. * pre-handler and it returned non-zero, it prepped
  566. * for calling the break_handler below on re-entry
  567. * for jprobe processing, so get out doing nothing
  568. * more here.
  569. */
  570. if (!p->pre_handler || !p->pre_handler(p, regs))
  571. setup_singlestep(p, regs, kcb, 0);
  572. return 1;
  573. }
  574. } else if (*addr != BREAKPOINT_INSTRUCTION) {
  575. /*
  576. * The breakpoint instruction was removed right
  577. * after we hit it. Another cpu has removed
  578. * either a probepoint or a debugger breakpoint
  579. * at this address. In either case, no further
  580. * handling of this interrupt is appropriate.
  581. * Back up over the (now missing) int3 and run
  582. * the original instruction.
  583. */
  584. regs->ip = (unsigned long)addr;
  585. preempt_enable_no_resched();
  586. return 1;
  587. } else if (kprobe_running()) {
  588. p = __this_cpu_read(current_kprobe);
  589. if (p->break_handler && p->break_handler(p, regs)) {
  590. if (!skip_singlestep(p, regs, kcb))
  591. setup_singlestep(p, regs, kcb, 0);
  592. return 1;
  593. }
  594. } /* else: not a kprobe fault; let the kernel handle it */
  595. preempt_enable_no_resched();
  596. return 0;
  597. }
  598. NOKPROBE_SYMBOL(kprobe_int3_handler);
  599. /*
  600. * When a retprobed function returns, this code saves registers and
  601. * calls trampoline_handler() runs, which calls the kretprobe's handler.
  602. */
  603. static void __used kretprobe_trampoline_holder(void)
  604. {
  605. asm volatile (
  606. ".global kretprobe_trampoline\n"
  607. "kretprobe_trampoline: \n"
  608. #ifdef CONFIG_X86_64
  609. /* We don't bother saving the ss register */
  610. " pushq %rsp\n"
  611. " pushfq\n"
  612. SAVE_REGS_STRING
  613. " movq %rsp, %rdi\n"
  614. " call trampoline_handler\n"
  615. /* Replace saved sp with true return address. */
  616. " movq %rax, 152(%rsp)\n"
  617. RESTORE_REGS_STRING
  618. " popfq\n"
  619. #else
  620. " pushf\n"
  621. SAVE_REGS_STRING
  622. " movl %esp, %eax\n"
  623. " call trampoline_handler\n"
  624. /* Move flags to cs */
  625. " movl 56(%esp), %edx\n"
  626. " movl %edx, 52(%esp)\n"
  627. /* Replace saved flags with true return address. */
  628. " movl %eax, 56(%esp)\n"
  629. RESTORE_REGS_STRING
  630. " popf\n"
  631. #endif
  632. " ret\n");
  633. }
  634. NOKPROBE_SYMBOL(kretprobe_trampoline_holder);
  635. NOKPROBE_SYMBOL(kretprobe_trampoline);
  636. /*
  637. * Called from kretprobe_trampoline
  638. */
  639. __visible __used void *trampoline_handler(struct pt_regs *regs)
  640. {
  641. struct kretprobe_instance *ri = NULL;
  642. struct hlist_head *head, empty_rp;
  643. struct hlist_node *tmp;
  644. unsigned long flags, orig_ret_address = 0;
  645. unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
  646. kprobe_opcode_t *correct_ret_addr = NULL;
  647. INIT_HLIST_HEAD(&empty_rp);
  648. kretprobe_hash_lock(current, &head, &flags);
  649. /* fixup registers */
  650. #ifdef CONFIG_X86_64
  651. regs->cs = __KERNEL_CS;
  652. #else
  653. regs->cs = __KERNEL_CS | get_kernel_rpl();
  654. regs->gs = 0;
  655. #endif
  656. regs->ip = trampoline_address;
  657. regs->orig_ax = ~0UL;
  658. /*
  659. * It is possible to have multiple instances associated with a given
  660. * task either because multiple functions in the call path have
  661. * return probes installed on them, and/or more than one
  662. * return probe was registered for a target function.
  663. *
  664. * We can handle this because:
  665. * - instances are always pushed into the head of the list
  666. * - when multiple return probes are registered for the same
  667. * function, the (chronologically) first instance's ret_addr
  668. * will be the real return address, and all the rest will
  669. * point to kretprobe_trampoline.
  670. */
  671. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  672. if (ri->task != current)
  673. /* another task is sharing our hash bucket */
  674. continue;
  675. orig_ret_address = (unsigned long)ri->ret_addr;
  676. if (orig_ret_address != trampoline_address)
  677. /*
  678. * This is the real return address. Any other
  679. * instances associated with this task are for
  680. * other calls deeper on the call stack
  681. */
  682. break;
  683. }
  684. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  685. correct_ret_addr = ri->ret_addr;
  686. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  687. if (ri->task != current)
  688. /* another task is sharing our hash bucket */
  689. continue;
  690. orig_ret_address = (unsigned long)ri->ret_addr;
  691. if (ri->rp && ri->rp->handler) {
  692. __this_cpu_write(current_kprobe, &ri->rp->kp);
  693. get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
  694. ri->ret_addr = correct_ret_addr;
  695. ri->rp->handler(ri, regs);
  696. __this_cpu_write(current_kprobe, NULL);
  697. }
  698. recycle_rp_inst(ri, &empty_rp);
  699. if (orig_ret_address != trampoline_address)
  700. /*
  701. * This is the real return address. Any other
  702. * instances associated with this task are for
  703. * other calls deeper on the call stack
  704. */
  705. break;
  706. }
  707. kretprobe_hash_unlock(current, &flags);
  708. hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
  709. hlist_del(&ri->hlist);
  710. kfree(ri);
  711. }
  712. return (void *)orig_ret_address;
  713. }
  714. NOKPROBE_SYMBOL(trampoline_handler);
  715. /*
  716. * Called after single-stepping. p->addr is the address of the
  717. * instruction whose first byte has been replaced by the "int 3"
  718. * instruction. To avoid the SMP problems that can occur when we
  719. * temporarily put back the original opcode to single-step, we
  720. * single-stepped a copy of the instruction. The address of this
  721. * copy is p->ainsn.insn.
  722. *
  723. * This function prepares to return from the post-single-step
  724. * interrupt. We have to fix up the stack as follows:
  725. *
  726. * 0) Except in the case of absolute or indirect jump or call instructions,
  727. * the new ip is relative to the copied instruction. We need to make
  728. * it relative to the original instruction.
  729. *
  730. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  731. * flags are set in the just-pushed flags, and may need to be cleared.
  732. *
  733. * 2) If the single-stepped instruction was a call, the return address
  734. * that is atop the stack is the address following the copied instruction.
  735. * We need to make it the address following the original instruction.
  736. *
  737. * If this is the first time we've single-stepped the instruction at
  738. * this probepoint, and the instruction is boostable, boost it: add a
  739. * jump instruction after the copied instruction, that jumps to the next
  740. * instruction after the probepoint.
  741. */
  742. static void resume_execution(struct kprobe *p, struct pt_regs *regs,
  743. struct kprobe_ctlblk *kcb)
  744. {
  745. unsigned long *tos = stack_addr(regs);
  746. unsigned long copy_ip = (unsigned long)p->ainsn.insn;
  747. unsigned long orig_ip = (unsigned long)p->addr;
  748. kprobe_opcode_t *insn = p->ainsn.insn;
  749. /* Skip prefixes */
  750. insn = skip_prefixes(insn);
  751. regs->flags &= ~X86_EFLAGS_TF;
  752. switch (*insn) {
  753. case 0x9c: /* pushfl */
  754. *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
  755. *tos |= kcb->kprobe_old_flags;
  756. break;
  757. case 0xc2: /* iret/ret/lret */
  758. case 0xc3:
  759. case 0xca:
  760. case 0xcb:
  761. case 0xcf:
  762. case 0xea: /* jmp absolute -- ip is correct */
  763. /* ip is already adjusted, no more changes required */
  764. p->ainsn.boostable = 1;
  765. goto no_change;
  766. case 0xe8: /* call relative - Fix return addr */
  767. *tos = orig_ip + (*tos - copy_ip);
  768. break;
  769. #ifdef CONFIG_X86_32
  770. case 0x9a: /* call absolute -- same as call absolute, indirect */
  771. *tos = orig_ip + (*tos - copy_ip);
  772. goto no_change;
  773. #endif
  774. case 0xff:
  775. if ((insn[1] & 0x30) == 0x10) {
  776. /*
  777. * call absolute, indirect
  778. * Fix return addr; ip is correct.
  779. * But this is not boostable
  780. */
  781. *tos = orig_ip + (*tos - copy_ip);
  782. goto no_change;
  783. } else if (((insn[1] & 0x31) == 0x20) ||
  784. ((insn[1] & 0x31) == 0x21)) {
  785. /*
  786. * jmp near and far, absolute indirect
  787. * ip is correct. And this is boostable
  788. */
  789. p->ainsn.boostable = 1;
  790. goto no_change;
  791. }
  792. default:
  793. break;
  794. }
  795. if (p->ainsn.boostable == 0) {
  796. if ((regs->ip > copy_ip) &&
  797. (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
  798. /*
  799. * These instructions can be executed directly if it
  800. * jumps back to correct address.
  801. */
  802. synthesize_reljump((void *)regs->ip,
  803. (void *)orig_ip + (regs->ip - copy_ip));
  804. p->ainsn.boostable = 1;
  805. } else {
  806. p->ainsn.boostable = -1;
  807. }
  808. }
  809. regs->ip += orig_ip - copy_ip;
  810. no_change:
  811. restore_btf();
  812. }
  813. NOKPROBE_SYMBOL(resume_execution);
  814. /*
  815. * Interrupts are disabled on entry as trap1 is an interrupt gate and they
  816. * remain disabled throughout this function.
  817. */
  818. int kprobe_debug_handler(struct pt_regs *regs)
  819. {
  820. struct kprobe *cur = kprobe_running();
  821. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  822. if (!cur)
  823. return 0;
  824. resume_execution(cur, regs, kcb);
  825. regs->flags |= kcb->kprobe_saved_flags;
  826. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  827. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  828. cur->post_handler(cur, regs, 0);
  829. }
  830. /* Restore back the original saved kprobes variables and continue. */
  831. if (kcb->kprobe_status == KPROBE_REENTER) {
  832. restore_previous_kprobe(kcb);
  833. goto out;
  834. }
  835. reset_current_kprobe();
  836. out:
  837. preempt_enable_no_resched();
  838. /*
  839. * if somebody else is singlestepping across a probe point, flags
  840. * will have TF set, in which case, continue the remaining processing
  841. * of do_debug, as if this is not a probe hit.
  842. */
  843. if (regs->flags & X86_EFLAGS_TF)
  844. return 0;
  845. return 1;
  846. }
  847. NOKPROBE_SYMBOL(kprobe_debug_handler);
  848. int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  849. {
  850. struct kprobe *cur = kprobe_running();
  851. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  852. if (unlikely(regs->ip == (unsigned long)cur->ainsn.insn)) {
  853. /* This must happen on single-stepping */
  854. WARN_ON(kcb->kprobe_status != KPROBE_HIT_SS &&
  855. kcb->kprobe_status != KPROBE_REENTER);
  856. /*
  857. * We are here because the instruction being single
  858. * stepped caused a page fault. We reset the current
  859. * kprobe and the ip points back to the probe address
  860. * and allow the page fault handler to continue as a
  861. * normal page fault.
  862. */
  863. regs->ip = (unsigned long)cur->addr;
  864. regs->flags |= kcb->kprobe_old_flags;
  865. if (kcb->kprobe_status == KPROBE_REENTER)
  866. restore_previous_kprobe(kcb);
  867. else
  868. reset_current_kprobe();
  869. preempt_enable_no_resched();
  870. } else if (kcb->kprobe_status == KPROBE_HIT_ACTIVE ||
  871. kcb->kprobe_status == KPROBE_HIT_SSDONE) {
  872. /*
  873. * We increment the nmissed count for accounting,
  874. * we can also use npre/npostfault count for accounting
  875. * these specific fault cases.
  876. */
  877. kprobes_inc_nmissed_count(cur);
  878. /*
  879. * We come here because instructions in the pre/post
  880. * handler caused the page_fault, this could happen
  881. * if handler tries to access user space by
  882. * copy_from_user(), get_user() etc. Let the
  883. * user-specified handler try to fix it first.
  884. */
  885. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  886. return 1;
  887. /*
  888. * In case the user-specified fault handler returned
  889. * zero, try to fix up.
  890. */
  891. if (fixup_exception(regs))
  892. return 1;
  893. /*
  894. * fixup routine could not handle it,
  895. * Let do_page_fault() fix it.
  896. */
  897. }
  898. return 0;
  899. }
  900. NOKPROBE_SYMBOL(kprobe_fault_handler);
  901. /*
  902. * Wrapper routine for handling exceptions.
  903. */
  904. int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
  905. void *data)
  906. {
  907. struct die_args *args = data;
  908. int ret = NOTIFY_DONE;
  909. if (args->regs && user_mode(args->regs))
  910. return ret;
  911. if (val == DIE_GPF) {
  912. /*
  913. * To be potentially processing a kprobe fault and to
  914. * trust the result from kprobe_running(), we have
  915. * be non-preemptible.
  916. */
  917. if (!preemptible() && kprobe_running() &&
  918. kprobe_fault_handler(args->regs, args->trapnr))
  919. ret = NOTIFY_STOP;
  920. }
  921. return ret;
  922. }
  923. NOKPROBE_SYMBOL(kprobe_exceptions_notify);
  924. int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  925. {
  926. struct jprobe *jp = container_of(p, struct jprobe, kp);
  927. unsigned long addr;
  928. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  929. kcb->jprobe_saved_regs = *regs;
  930. kcb->jprobe_saved_sp = stack_addr(regs);
  931. addr = (unsigned long)(kcb->jprobe_saved_sp);
  932. /*
  933. * As Linus pointed out, gcc assumes that the callee
  934. * owns the argument space and could overwrite it, e.g.
  935. * tailcall optimization. So, to be absolutely safe
  936. * we also save and restore enough stack bytes to cover
  937. * the argument area.
  938. */
  939. memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
  940. MIN_STACK_SIZE(addr));
  941. regs->flags &= ~X86_EFLAGS_IF;
  942. trace_hardirqs_off();
  943. regs->ip = (unsigned long)(jp->entry);
  944. /*
  945. * jprobes use jprobe_return() which skips the normal return
  946. * path of the function, and this messes up the accounting of the
  947. * function graph tracer to get messed up.
  948. *
  949. * Pause function graph tracing while performing the jprobe function.
  950. */
  951. pause_graph_tracing();
  952. return 1;
  953. }
  954. NOKPROBE_SYMBOL(setjmp_pre_handler);
  955. void jprobe_return(void)
  956. {
  957. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  958. asm volatile (
  959. #ifdef CONFIG_X86_64
  960. " xchg %%rbx,%%rsp \n"
  961. #else
  962. " xchgl %%ebx,%%esp \n"
  963. #endif
  964. " int3 \n"
  965. " .globl jprobe_return_end\n"
  966. " jprobe_return_end: \n"
  967. " nop \n"::"b"
  968. (kcb->jprobe_saved_sp):"memory");
  969. }
  970. NOKPROBE_SYMBOL(jprobe_return);
  971. NOKPROBE_SYMBOL(jprobe_return_end);
  972. int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  973. {
  974. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  975. u8 *addr = (u8 *) (regs->ip - 1);
  976. struct jprobe *jp = container_of(p, struct jprobe, kp);
  977. void *saved_sp = kcb->jprobe_saved_sp;
  978. if ((addr > (u8 *) jprobe_return) &&
  979. (addr < (u8 *) jprobe_return_end)) {
  980. if (stack_addr(regs) != saved_sp) {
  981. struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
  982. printk(KERN_ERR
  983. "current sp %p does not match saved sp %p\n",
  984. stack_addr(regs), saved_sp);
  985. printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
  986. show_regs(saved_regs);
  987. printk(KERN_ERR "Current registers\n");
  988. show_regs(regs);
  989. BUG();
  990. }
  991. /* It's OK to start function graph tracing again */
  992. unpause_graph_tracing();
  993. *regs = kcb->jprobe_saved_regs;
  994. memcpy(saved_sp, kcb->jprobes_stack, MIN_STACK_SIZE(saved_sp));
  995. preempt_enable_no_resched();
  996. return 1;
  997. }
  998. return 0;
  999. }
  1000. NOKPROBE_SYMBOL(longjmp_break_handler);
  1001. bool arch_within_kprobe_blacklist(unsigned long addr)
  1002. {
  1003. return (addr >= (unsigned long)__kprobes_text_start &&
  1004. addr < (unsigned long)__kprobes_text_end) ||
  1005. (addr >= (unsigned long)__entry_text_start &&
  1006. addr < (unsigned long)__entry_text_end);
  1007. }
  1008. int __init arch_init_kprobes(void)
  1009. {
  1010. return 0;
  1011. }
  1012. int arch_trampoline_kprobe(struct kprobe *p)
  1013. {
  1014. return 0;
  1015. }