core.c 33 KB

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