kprobes.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  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. void jprobe_return_end(void);
  60. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  61. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  62. #define stack_addr(regs) ((unsigned long *)kernel_stack_pointer(regs))
  63. #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
  64. (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
  65. (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
  66. (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
  67. (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
  68. << (row % 32))
  69. /*
  70. * Undefined/reserved opcodes, conditional jump, Opcode Extension
  71. * Groups, and some special opcodes can not boost.
  72. * This is non-const and volatile to keep gcc from statically
  73. * optimizing it out, as variable_test_bit makes gcc think only
  74. * *(unsigned long*) is used.
  75. */
  76. static volatile u32 twobyte_is_boostable[256 / 32] = {
  77. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  78. /* ---------------------------------------------- */
  79. W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
  80. W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 10 */
  81. W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
  82. W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
  83. W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
  84. W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
  85. W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
  86. W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
  87. W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
  88. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
  89. W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
  90. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
  91. W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
  92. W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
  93. W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
  94. W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
  95. /* ----------------------------------------------- */
  96. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  97. };
  98. #undef W
  99. struct kretprobe_blackpoint kretprobe_blacklist[] = {
  100. {"__switch_to", }, /* This function switches only current task, but
  101. doesn't switch kernel stack.*/
  102. {NULL, NULL} /* Terminator */
  103. };
  104. const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
  105. static void __kprobes __synthesize_relative_insn(void *from, void *to, u8 op)
  106. {
  107. struct __arch_relative_insn {
  108. u8 op;
  109. s32 raddr;
  110. } __attribute__((packed)) *insn;
  111. insn = (struct __arch_relative_insn *)from;
  112. insn->raddr = (s32)((long)(to) - ((long)(from) + 5));
  113. insn->op = op;
  114. }
  115. /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
  116. static void __kprobes synthesize_reljump(void *from, void *to)
  117. {
  118. __synthesize_relative_insn(from, to, RELATIVEJUMP_OPCODE);
  119. }
  120. /*
  121. * Skip the prefixes of the instruction.
  122. */
  123. static kprobe_opcode_t *__kprobes skip_prefixes(kprobe_opcode_t *insn)
  124. {
  125. insn_attr_t attr;
  126. attr = inat_get_opcode_attribute((insn_byte_t)*insn);
  127. while (inat_is_legacy_prefix(attr)) {
  128. insn++;
  129. attr = inat_get_opcode_attribute((insn_byte_t)*insn);
  130. }
  131. #ifdef CONFIG_X86_64
  132. if (inat_is_rex_prefix(attr))
  133. insn++;
  134. #endif
  135. return insn;
  136. }
  137. /*
  138. * Returns non-zero if opcode is boostable.
  139. * RIP relative instructions are adjusted at copying time in 64 bits mode
  140. */
  141. static int __kprobes can_boost(kprobe_opcode_t *opcodes)
  142. {
  143. kprobe_opcode_t opcode;
  144. kprobe_opcode_t *orig_opcodes = opcodes;
  145. if (search_exception_tables((unsigned long)opcodes))
  146. return 0; /* Page fault may occur on this address. */
  147. retry:
  148. if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
  149. return 0;
  150. opcode = *(opcodes++);
  151. /* 2nd-byte opcode */
  152. if (opcode == 0x0f) {
  153. if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
  154. return 0;
  155. return test_bit(*opcodes,
  156. (unsigned long *)twobyte_is_boostable);
  157. }
  158. switch (opcode & 0xf0) {
  159. #ifdef CONFIG_X86_64
  160. case 0x40:
  161. goto retry; /* REX prefix is boostable */
  162. #endif
  163. case 0x60:
  164. if (0x63 < opcode && opcode < 0x67)
  165. goto retry; /* prefixes */
  166. /* can't boost Address-size override and bound */
  167. return (opcode != 0x62 && opcode != 0x67);
  168. case 0x70:
  169. return 0; /* can't boost conditional jump */
  170. case 0xc0:
  171. /* can't boost software-interruptions */
  172. return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
  173. case 0xd0:
  174. /* can boost AA* and XLAT */
  175. return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
  176. case 0xe0:
  177. /* can boost in/out and absolute jmps */
  178. return ((opcode & 0x04) || opcode == 0xea);
  179. case 0xf0:
  180. if ((opcode & 0x0c) == 0 && opcode != 0xf1)
  181. goto retry; /* lock/rep(ne) prefix */
  182. /* clear and set flags are boostable */
  183. return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
  184. default:
  185. /* segment override prefixes are boostable */
  186. if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
  187. goto retry; /* prefixes */
  188. /* CS override prefix and call are not boostable */
  189. return (opcode != 0x2e && opcode != 0x9a);
  190. }
  191. }
  192. /* Recover the probed instruction at addr for further analysis. */
  193. static int recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
  194. {
  195. struct kprobe *kp;
  196. kp = get_kprobe((void *)addr);
  197. if (!kp)
  198. return -EINVAL;
  199. /*
  200. * Basically, kp->ainsn.insn has an original instruction.
  201. * However, RIP-relative instruction can not do single-stepping
  202. * at different place, __copy_instruction() tweaks the displacement of
  203. * that instruction. In that case, we can't recover the instruction
  204. * from the kp->ainsn.insn.
  205. *
  206. * On the other hand, kp->opcode has a copy of the first byte of
  207. * the probed instruction, which is overwritten by int3. And
  208. * the instruction at kp->addr is not modified by kprobes except
  209. * for the first byte, we can recover the original instruction
  210. * from it and kp->opcode.
  211. */
  212. memcpy(buf, kp->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
  213. buf[0] = kp->opcode;
  214. return 0;
  215. }
  216. /* Check if paddr is at an instruction boundary */
  217. static int __kprobes can_probe(unsigned long paddr)
  218. {
  219. int ret;
  220. unsigned long addr, offset = 0;
  221. struct insn insn;
  222. kprobe_opcode_t buf[MAX_INSN_SIZE];
  223. if (!kallsyms_lookup_size_offset(paddr, NULL, &offset))
  224. return 0;
  225. /* Decode instructions */
  226. addr = paddr - offset;
  227. while (addr < paddr) {
  228. kernel_insn_init(&insn, (void *)addr);
  229. insn_get_opcode(&insn);
  230. /*
  231. * Check if the instruction has been modified by another
  232. * kprobe, in which case we replace the breakpoint by the
  233. * original instruction in our buffer.
  234. */
  235. if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
  236. ret = recover_probed_instruction(buf, addr);
  237. if (ret)
  238. /*
  239. * Another debugging subsystem might insert
  240. * this breakpoint. In that case, we can't
  241. * recover it.
  242. */
  243. return 0;
  244. kernel_insn_init(&insn, buf);
  245. }
  246. insn_get_length(&insn);
  247. addr += insn.length;
  248. }
  249. return (addr == paddr);
  250. }
  251. /*
  252. * Returns non-zero if opcode modifies the interrupt flag.
  253. */
  254. static int __kprobes is_IF_modifier(kprobe_opcode_t *insn)
  255. {
  256. /* Skip prefixes */
  257. insn = skip_prefixes(insn);
  258. switch (*insn) {
  259. case 0xfa: /* cli */
  260. case 0xfb: /* sti */
  261. case 0xcf: /* iret/iretd */
  262. case 0x9d: /* popf/popfd */
  263. return 1;
  264. }
  265. return 0;
  266. }
  267. /*
  268. * Copy an instruction and adjust the displacement if the instruction
  269. * uses the %rip-relative addressing mode.
  270. * If it does, Return the address of the 32-bit displacement word.
  271. * If not, return null.
  272. * Only applicable to 64-bit x86.
  273. */
  274. static int __kprobes __copy_instruction(u8 *dest, u8 *src, int recover)
  275. {
  276. struct insn insn;
  277. int ret;
  278. kprobe_opcode_t buf[MAX_INSN_SIZE];
  279. kernel_insn_init(&insn, src);
  280. if (recover) {
  281. insn_get_opcode(&insn);
  282. if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
  283. ret = recover_probed_instruction(buf,
  284. (unsigned long)src);
  285. if (ret)
  286. return 0;
  287. kernel_insn_init(&insn, buf);
  288. }
  289. }
  290. insn_get_length(&insn);
  291. memcpy(dest, insn.kaddr, insn.length);
  292. #ifdef CONFIG_X86_64
  293. if (insn_rip_relative(&insn)) {
  294. s64 newdisp;
  295. u8 *disp;
  296. kernel_insn_init(&insn, dest);
  297. insn_get_displacement(&insn);
  298. /*
  299. * The copied instruction uses the %rip-relative addressing
  300. * mode. Adjust the displacement for the difference between
  301. * the original location of this instruction and the location
  302. * of the copy that will actually be run. The tricky bit here
  303. * is making sure that the sign extension happens correctly in
  304. * this calculation, since we need a signed 32-bit result to
  305. * be sign-extended to 64 bits when it's added to the %rip
  306. * value and yield the same 64-bit result that the sign-
  307. * extension of the original signed 32-bit displacement would
  308. * have given.
  309. */
  310. newdisp = (u8 *) src + (s64) insn.displacement.value -
  311. (u8 *) dest;
  312. BUG_ON((s64) (s32) newdisp != newdisp); /* Sanity check. */
  313. disp = (u8 *) dest + insn_offset_displacement(&insn);
  314. *(s32 *) disp = (s32) newdisp;
  315. }
  316. #endif
  317. return insn.length;
  318. }
  319. static void __kprobes arch_copy_kprobe(struct kprobe *p)
  320. {
  321. /*
  322. * Copy an instruction without recovering int3, because it will be
  323. * put by another subsystem.
  324. */
  325. __copy_instruction(p->ainsn.insn, p->addr, 0);
  326. if (can_boost(p->addr))
  327. p->ainsn.boostable = 0;
  328. else
  329. p->ainsn.boostable = -1;
  330. p->opcode = *p->addr;
  331. }
  332. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  333. {
  334. if (alternatives_text_reserved(p->addr, p->addr))
  335. return -EINVAL;
  336. if (!can_probe((unsigned long)p->addr))
  337. return -EILSEQ;
  338. /* insn: must be on special executable page on x86. */
  339. p->ainsn.insn = get_insn_slot();
  340. if (!p->ainsn.insn)
  341. return -ENOMEM;
  342. arch_copy_kprobe(p);
  343. return 0;
  344. }
  345. void __kprobes arch_arm_kprobe(struct kprobe *p)
  346. {
  347. text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
  348. }
  349. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  350. {
  351. text_poke(p->addr, &p->opcode, 1);
  352. }
  353. void __kprobes arch_remove_kprobe(struct kprobe *p)
  354. {
  355. if (p->ainsn.insn) {
  356. free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
  357. p->ainsn.insn = NULL;
  358. }
  359. }
  360. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  361. {
  362. kcb->prev_kprobe.kp = kprobe_running();
  363. kcb->prev_kprobe.status = kcb->kprobe_status;
  364. kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
  365. kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
  366. }
  367. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  368. {
  369. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  370. kcb->kprobe_status = kcb->prev_kprobe.status;
  371. kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
  372. kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
  373. }
  374. static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  375. struct kprobe_ctlblk *kcb)
  376. {
  377. __this_cpu_write(current_kprobe, p);
  378. kcb->kprobe_saved_flags = kcb->kprobe_old_flags
  379. = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
  380. if (is_IF_modifier(p->ainsn.insn))
  381. kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
  382. }
  383. static void __kprobes clear_btf(void)
  384. {
  385. if (test_thread_flag(TIF_BLOCKSTEP)) {
  386. unsigned long debugctl = get_debugctlmsr();
  387. debugctl &= ~DEBUGCTLMSR_BTF;
  388. update_debugctlmsr(debugctl);
  389. }
  390. }
  391. static void __kprobes restore_btf(void)
  392. {
  393. if (test_thread_flag(TIF_BLOCKSTEP)) {
  394. unsigned long debugctl = get_debugctlmsr();
  395. debugctl |= DEBUGCTLMSR_BTF;
  396. update_debugctlmsr(debugctl);
  397. }
  398. }
  399. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  400. struct pt_regs *regs)
  401. {
  402. unsigned long *sara = stack_addr(regs);
  403. ri->ret_addr = (kprobe_opcode_t *) *sara;
  404. /* Replace the return addr with trampoline addr */
  405. *sara = (unsigned long) &kretprobe_trampoline;
  406. }
  407. #ifdef CONFIG_OPTPROBES
  408. static int __kprobes setup_detour_execution(struct kprobe *p,
  409. struct pt_regs *regs,
  410. int reenter);
  411. #else
  412. #define setup_detour_execution(p, regs, reenter) (0)
  413. #endif
  414. static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs,
  415. struct kprobe_ctlblk *kcb, int reenter)
  416. {
  417. if (setup_detour_execution(p, regs, reenter))
  418. return;
  419. #if !defined(CONFIG_PREEMPT)
  420. if (p->ainsn.boostable == 1 && !p->post_handler) {
  421. /* Boost up -- we can execute copied instructions directly */
  422. if (!reenter)
  423. reset_current_kprobe();
  424. /*
  425. * Reentering boosted probe doesn't reset current_kprobe,
  426. * nor set current_kprobe, because it doesn't use single
  427. * stepping.
  428. */
  429. regs->ip = (unsigned long)p->ainsn.insn;
  430. preempt_enable_no_resched();
  431. return;
  432. }
  433. #endif
  434. if (reenter) {
  435. save_previous_kprobe(kcb);
  436. set_current_kprobe(p, regs, kcb);
  437. kcb->kprobe_status = KPROBE_REENTER;
  438. } else
  439. kcb->kprobe_status = KPROBE_HIT_SS;
  440. /* Prepare real single stepping */
  441. clear_btf();
  442. regs->flags |= X86_EFLAGS_TF;
  443. regs->flags &= ~X86_EFLAGS_IF;
  444. /* single step inline if the instruction is an int3 */
  445. if (p->opcode == BREAKPOINT_INSTRUCTION)
  446. regs->ip = (unsigned long)p->addr;
  447. else
  448. regs->ip = (unsigned long)p->ainsn.insn;
  449. }
  450. /*
  451. * We have reentered the kprobe_handler(), since another probe was hit while
  452. * within the handler. We save the original kprobes variables and just single
  453. * step on the instruction of the new probe without calling any user handlers.
  454. */
  455. static int __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
  456. struct kprobe_ctlblk *kcb)
  457. {
  458. switch (kcb->kprobe_status) {
  459. case KPROBE_HIT_SSDONE:
  460. case KPROBE_HIT_ACTIVE:
  461. kprobes_inc_nmissed_count(p);
  462. setup_singlestep(p, regs, kcb, 1);
  463. break;
  464. case KPROBE_HIT_SS:
  465. /* A probe has been hit in the codepath leading up to, or just
  466. * after, single-stepping of a probed instruction. This entire
  467. * codepath should strictly reside in .kprobes.text section.
  468. * Raise a BUG or we'll continue in an endless reentering loop
  469. * and eventually a stack overflow.
  470. */
  471. printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n",
  472. p->addr);
  473. dump_kprobe(p);
  474. BUG();
  475. default:
  476. /* impossible cases */
  477. WARN_ON(1);
  478. return 0;
  479. }
  480. return 1;
  481. }
  482. /*
  483. * Interrupts are disabled on entry as trap3 is an interrupt gate and they
  484. * remain disabled throughout this function.
  485. */
  486. static int __kprobes kprobe_handler(struct pt_regs *regs)
  487. {
  488. kprobe_opcode_t *addr;
  489. struct kprobe *p;
  490. struct kprobe_ctlblk *kcb;
  491. addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
  492. /*
  493. * We don't want to be preempted for the entire
  494. * duration of kprobe processing. We conditionally
  495. * re-enable preemption at the end of this function,
  496. * and also in reenter_kprobe() and setup_singlestep().
  497. */
  498. preempt_disable();
  499. kcb = get_kprobe_ctlblk();
  500. p = get_kprobe(addr);
  501. if (p) {
  502. if (kprobe_running()) {
  503. if (reenter_kprobe(p, regs, kcb))
  504. return 1;
  505. } else {
  506. set_current_kprobe(p, regs, kcb);
  507. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  508. /*
  509. * If we have no pre-handler or it returned 0, we
  510. * continue with normal processing. If we have a
  511. * pre-handler and it returned non-zero, it prepped
  512. * for calling the break_handler below on re-entry
  513. * for jprobe processing, so get out doing nothing
  514. * more here.
  515. */
  516. if (!p->pre_handler || !p->pre_handler(p, regs))
  517. setup_singlestep(p, regs, kcb, 0);
  518. return 1;
  519. }
  520. } else if (*addr != BREAKPOINT_INSTRUCTION) {
  521. /*
  522. * The breakpoint instruction was removed right
  523. * after we hit it. Another cpu has removed
  524. * either a probepoint or a debugger breakpoint
  525. * at this address. In either case, no further
  526. * handling of this interrupt is appropriate.
  527. * Back up over the (now missing) int3 and run
  528. * the original instruction.
  529. */
  530. regs->ip = (unsigned long)addr;
  531. preempt_enable_no_resched();
  532. return 1;
  533. } else if (kprobe_running()) {
  534. p = __this_cpu_read(current_kprobe);
  535. if (p->break_handler && p->break_handler(p, regs)) {
  536. setup_singlestep(p, regs, kcb, 0);
  537. return 1;
  538. }
  539. } /* else: not a kprobe fault; let the kernel handle it */
  540. preempt_enable_no_resched();
  541. return 0;
  542. }
  543. #ifdef CONFIG_X86_64
  544. #define SAVE_REGS_STRING \
  545. /* Skip cs, ip, orig_ax. */ \
  546. " subq $24, %rsp\n" \
  547. " pushq %rdi\n" \
  548. " pushq %rsi\n" \
  549. " pushq %rdx\n" \
  550. " pushq %rcx\n" \
  551. " pushq %rax\n" \
  552. " pushq %r8\n" \
  553. " pushq %r9\n" \
  554. " pushq %r10\n" \
  555. " pushq %r11\n" \
  556. " pushq %rbx\n" \
  557. " pushq %rbp\n" \
  558. " pushq %r12\n" \
  559. " pushq %r13\n" \
  560. " pushq %r14\n" \
  561. " pushq %r15\n"
  562. #define RESTORE_REGS_STRING \
  563. " popq %r15\n" \
  564. " popq %r14\n" \
  565. " popq %r13\n" \
  566. " popq %r12\n" \
  567. " popq %rbp\n" \
  568. " popq %rbx\n" \
  569. " popq %r11\n" \
  570. " popq %r10\n" \
  571. " popq %r9\n" \
  572. " popq %r8\n" \
  573. " popq %rax\n" \
  574. " popq %rcx\n" \
  575. " popq %rdx\n" \
  576. " popq %rsi\n" \
  577. " popq %rdi\n" \
  578. /* Skip orig_ax, ip, cs */ \
  579. " addq $24, %rsp\n"
  580. #else
  581. #define SAVE_REGS_STRING \
  582. /* Skip cs, ip, orig_ax and gs. */ \
  583. " subl $16, %esp\n" \
  584. " pushl %fs\n" \
  585. " pushl %es\n" \
  586. " pushl %ds\n" \
  587. " pushl %eax\n" \
  588. " pushl %ebp\n" \
  589. " pushl %edi\n" \
  590. " pushl %esi\n" \
  591. " pushl %edx\n" \
  592. " pushl %ecx\n" \
  593. " pushl %ebx\n"
  594. #define RESTORE_REGS_STRING \
  595. " popl %ebx\n" \
  596. " popl %ecx\n" \
  597. " popl %edx\n" \
  598. " popl %esi\n" \
  599. " popl %edi\n" \
  600. " popl %ebp\n" \
  601. " popl %eax\n" \
  602. /* Skip ds, es, fs, gs, orig_ax, and ip. Note: don't pop cs here*/\
  603. " addl $24, %esp\n"
  604. #endif
  605. /*
  606. * When a retprobed function returns, this code saves registers and
  607. * calls trampoline_handler() runs, which calls the kretprobe's handler.
  608. */
  609. static void __used __kprobes kretprobe_trampoline_holder(void)
  610. {
  611. asm volatile (
  612. ".global kretprobe_trampoline\n"
  613. "kretprobe_trampoline: \n"
  614. #ifdef CONFIG_X86_64
  615. /* We don't bother saving the ss register */
  616. " pushq %rsp\n"
  617. " pushfq\n"
  618. SAVE_REGS_STRING
  619. " movq %rsp, %rdi\n"
  620. " call trampoline_handler\n"
  621. /* Replace saved sp with true return address. */
  622. " movq %rax, 152(%rsp)\n"
  623. RESTORE_REGS_STRING
  624. " popfq\n"
  625. #else
  626. " pushf\n"
  627. SAVE_REGS_STRING
  628. " movl %esp, %eax\n"
  629. " call trampoline_handler\n"
  630. /* Move flags to cs */
  631. " movl 56(%esp), %edx\n"
  632. " movl %edx, 52(%esp)\n"
  633. /* Replace saved flags with true return address. */
  634. " movl %eax, 56(%esp)\n"
  635. RESTORE_REGS_STRING
  636. " popf\n"
  637. #endif
  638. " ret\n");
  639. }
  640. /*
  641. * Called from kretprobe_trampoline
  642. */
  643. static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
  644. {
  645. struct kretprobe_instance *ri = NULL;
  646. struct hlist_head *head, empty_rp;
  647. struct hlist_node *node, *tmp;
  648. unsigned long flags, orig_ret_address = 0;
  649. unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
  650. kprobe_opcode_t *correct_ret_addr = NULL;
  651. INIT_HLIST_HEAD(&empty_rp);
  652. kretprobe_hash_lock(current, &head, &flags);
  653. /* fixup registers */
  654. #ifdef CONFIG_X86_64
  655. regs->cs = __KERNEL_CS;
  656. #else
  657. regs->cs = __KERNEL_CS | get_kernel_rpl();
  658. regs->gs = 0;
  659. #endif
  660. regs->ip = trampoline_address;
  661. regs->orig_ax = ~0UL;
  662. /*
  663. * It is possible to have multiple instances associated with a given
  664. * task either because multiple functions in the call path have
  665. * return probes installed on them, and/or more than one
  666. * return probe was registered for a target function.
  667. *
  668. * We can handle this because:
  669. * - instances are always pushed into the head of the list
  670. * - when multiple return probes are registered for the same
  671. * function, the (chronologically) first instance's ret_addr
  672. * will be the real return address, and all the rest will
  673. * point to kretprobe_trampoline.
  674. */
  675. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  676. if (ri->task != current)
  677. /* another task is sharing our hash bucket */
  678. continue;
  679. orig_ret_address = (unsigned long)ri->ret_addr;
  680. if (orig_ret_address != trampoline_address)
  681. /*
  682. * This is the real return address. Any other
  683. * instances associated with this task are for
  684. * other calls deeper on the call stack
  685. */
  686. break;
  687. }
  688. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  689. correct_ret_addr = ri->ret_addr;
  690. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  691. if (ri->task != current)
  692. /* another task is sharing our hash bucket */
  693. continue;
  694. orig_ret_address = (unsigned long)ri->ret_addr;
  695. if (ri->rp && ri->rp->handler) {
  696. __this_cpu_write(current_kprobe, &ri->rp->kp);
  697. get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
  698. ri->ret_addr = correct_ret_addr;
  699. ri->rp->handler(ri, regs);
  700. __this_cpu_write(current_kprobe, NULL);
  701. }
  702. recycle_rp_inst(ri, &empty_rp);
  703. if (orig_ret_address != trampoline_address)
  704. /*
  705. * This is the real return address. Any other
  706. * instances associated with this task are for
  707. * other calls deeper on the call stack
  708. */
  709. break;
  710. }
  711. kretprobe_hash_unlock(current, &flags);
  712. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  713. hlist_del(&ri->hlist);
  714. kfree(ri);
  715. }
  716. return (void *)orig_ret_address;
  717. }
  718. /*
  719. * Called after single-stepping. p->addr is the address of the
  720. * instruction whose first byte has been replaced by the "int 3"
  721. * instruction. To avoid the SMP problems that can occur when we
  722. * temporarily put back the original opcode to single-step, we
  723. * single-stepped a copy of the instruction. The address of this
  724. * copy is p->ainsn.insn.
  725. *
  726. * This function prepares to return from the post-single-step
  727. * interrupt. We have to fix up the stack as follows:
  728. *
  729. * 0) Except in the case of absolute or indirect jump or call instructions,
  730. * the new ip is relative to the copied instruction. We need to make
  731. * it relative to the original instruction.
  732. *
  733. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  734. * flags are set in the just-pushed flags, and may need to be cleared.
  735. *
  736. * 2) If the single-stepped instruction was a call, the return address
  737. * that is atop the stack is the address following the copied instruction.
  738. * We need to make it the address following the original instruction.
  739. *
  740. * If this is the first time we've single-stepped the instruction at
  741. * this probepoint, and the instruction is boostable, boost it: add a
  742. * jump instruction after the copied instruction, that jumps to the next
  743. * instruction after the probepoint.
  744. */
  745. static void __kprobes resume_execution(struct kprobe *p,
  746. struct pt_regs *regs, struct kprobe_ctlblk *kcb)
  747. {
  748. unsigned long *tos = stack_addr(regs);
  749. unsigned long copy_ip = (unsigned long)p->ainsn.insn;
  750. unsigned long orig_ip = (unsigned long)p->addr;
  751. kprobe_opcode_t *insn = p->ainsn.insn;
  752. /* Skip prefixes */
  753. insn = skip_prefixes(insn);
  754. regs->flags &= ~X86_EFLAGS_TF;
  755. switch (*insn) {
  756. case 0x9c: /* pushfl */
  757. *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
  758. *tos |= kcb->kprobe_old_flags;
  759. break;
  760. case 0xc2: /* iret/ret/lret */
  761. case 0xc3:
  762. case 0xca:
  763. case 0xcb:
  764. case 0xcf:
  765. case 0xea: /* jmp absolute -- ip is correct */
  766. /* ip is already adjusted, no more changes required */
  767. p->ainsn.boostable = 1;
  768. goto no_change;
  769. case 0xe8: /* call relative - Fix return addr */
  770. *tos = orig_ip + (*tos - copy_ip);
  771. break;
  772. #ifdef CONFIG_X86_32
  773. case 0x9a: /* call absolute -- same as call absolute, indirect */
  774. *tos = orig_ip + (*tos - copy_ip);
  775. goto no_change;
  776. #endif
  777. case 0xff:
  778. if ((insn[1] & 0x30) == 0x10) {
  779. /*
  780. * call absolute, indirect
  781. * Fix return addr; ip is correct.
  782. * But this is not boostable
  783. */
  784. *tos = orig_ip + (*tos - copy_ip);
  785. goto no_change;
  786. } else if (((insn[1] & 0x31) == 0x20) ||
  787. ((insn[1] & 0x31) == 0x21)) {
  788. /*
  789. * jmp near and far, absolute indirect
  790. * ip is correct. And this is boostable
  791. */
  792. p->ainsn.boostable = 1;
  793. goto no_change;
  794. }
  795. default:
  796. break;
  797. }
  798. if (p->ainsn.boostable == 0) {
  799. if ((regs->ip > copy_ip) &&
  800. (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
  801. /*
  802. * These instructions can be executed directly if it
  803. * jumps back to correct address.
  804. */
  805. synthesize_reljump((void *)regs->ip,
  806. (void *)orig_ip + (regs->ip - copy_ip));
  807. p->ainsn.boostable = 1;
  808. } else {
  809. p->ainsn.boostable = -1;
  810. }
  811. }
  812. regs->ip += orig_ip - copy_ip;
  813. no_change:
  814. restore_btf();
  815. }
  816. /*
  817. * Interrupts are disabled on entry as trap1 is an interrupt gate and they
  818. * remain disabled throughout this function.
  819. */
  820. static int __kprobes post_kprobe_handler(struct pt_regs *regs)
  821. {
  822. struct kprobe *cur = kprobe_running();
  823. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  824. if (!cur)
  825. return 0;
  826. resume_execution(cur, regs, kcb);
  827. regs->flags |= kcb->kprobe_saved_flags;
  828. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  829. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  830. cur->post_handler(cur, regs, 0);
  831. }
  832. /* Restore back the original saved kprobes variables and continue. */
  833. if (kcb->kprobe_status == KPROBE_REENTER) {
  834. restore_previous_kprobe(kcb);
  835. goto out;
  836. }
  837. reset_current_kprobe();
  838. out:
  839. preempt_enable_no_resched();
  840. /*
  841. * if somebody else is singlestepping across a probe point, flags
  842. * will have TF set, in which case, continue the remaining processing
  843. * of do_debug, as if this is not a probe hit.
  844. */
  845. if (regs->flags & X86_EFLAGS_TF)
  846. return 0;
  847. return 1;
  848. }
  849. int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  850. {
  851. struct kprobe *cur = kprobe_running();
  852. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  853. switch (kcb->kprobe_status) {
  854. case KPROBE_HIT_SS:
  855. case 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. break;
  871. case KPROBE_HIT_ACTIVE:
  872. case KPROBE_HIT_SSDONE:
  873. /*
  874. * We increment the nmissed count for accounting,
  875. * we can also use npre/npostfault count for accounting
  876. * these specific fault cases.
  877. */
  878. kprobes_inc_nmissed_count(cur);
  879. /*
  880. * We come here because instructions in the pre/post
  881. * handler caused the page_fault, this could happen
  882. * if handler tries to access user space by
  883. * copy_from_user(), get_user() etc. Let the
  884. * user-specified handler try to fix it first.
  885. */
  886. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  887. return 1;
  888. /*
  889. * In case the user-specified fault handler returned
  890. * zero, try to fix up.
  891. */
  892. if (fixup_exception(regs))
  893. return 1;
  894. /*
  895. * fixup routine could not handle it,
  896. * Let do_page_fault() fix it.
  897. */
  898. break;
  899. default:
  900. break;
  901. }
  902. return 0;
  903. }
  904. /*
  905. * Wrapper routine for handling exceptions.
  906. */
  907. int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
  908. unsigned long val, void *data)
  909. {
  910. struct die_args *args = data;
  911. int ret = NOTIFY_DONE;
  912. if (args->regs && user_mode_vm(args->regs))
  913. return ret;
  914. switch (val) {
  915. case DIE_INT3:
  916. if (kprobe_handler(args->regs))
  917. ret = NOTIFY_STOP;
  918. break;
  919. case DIE_DEBUG:
  920. if (post_kprobe_handler(args->regs)) {
  921. /*
  922. * Reset the BS bit in dr6 (pointed by args->err) to
  923. * denote completion of processing
  924. */
  925. (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
  926. ret = NOTIFY_STOP;
  927. }
  928. break;
  929. case DIE_GPF:
  930. /*
  931. * To be potentially processing a kprobe fault and to
  932. * trust the result from kprobe_running(), we have
  933. * be non-preemptible.
  934. */
  935. if (!preemptible() && kprobe_running() &&
  936. kprobe_fault_handler(args->regs, args->trapnr))
  937. ret = NOTIFY_STOP;
  938. break;
  939. default:
  940. break;
  941. }
  942. return ret;
  943. }
  944. int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  945. {
  946. struct jprobe *jp = container_of(p, struct jprobe, kp);
  947. unsigned long addr;
  948. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  949. kcb->jprobe_saved_regs = *regs;
  950. kcb->jprobe_saved_sp = stack_addr(regs);
  951. addr = (unsigned long)(kcb->jprobe_saved_sp);
  952. /*
  953. * As Linus pointed out, gcc assumes that the callee
  954. * owns the argument space and could overwrite it, e.g.
  955. * tailcall optimization. So, to be absolutely safe
  956. * we also save and restore enough stack bytes to cover
  957. * the argument area.
  958. */
  959. memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
  960. MIN_STACK_SIZE(addr));
  961. regs->flags &= ~X86_EFLAGS_IF;
  962. trace_hardirqs_off();
  963. regs->ip = (unsigned long)(jp->entry);
  964. return 1;
  965. }
  966. void __kprobes jprobe_return(void)
  967. {
  968. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  969. asm volatile (
  970. #ifdef CONFIG_X86_64
  971. " xchg %%rbx,%%rsp \n"
  972. #else
  973. " xchgl %%ebx,%%esp \n"
  974. #endif
  975. " int3 \n"
  976. " .globl jprobe_return_end\n"
  977. " jprobe_return_end: \n"
  978. " nop \n"::"b"
  979. (kcb->jprobe_saved_sp):"memory");
  980. }
  981. int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  982. {
  983. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  984. u8 *addr = (u8 *) (regs->ip - 1);
  985. struct jprobe *jp = container_of(p, struct jprobe, kp);
  986. if ((addr > (u8 *) jprobe_return) &&
  987. (addr < (u8 *) jprobe_return_end)) {
  988. if (stack_addr(regs) != kcb->jprobe_saved_sp) {
  989. struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
  990. printk(KERN_ERR
  991. "current sp %p does not match saved sp %p\n",
  992. stack_addr(regs), kcb->jprobe_saved_sp);
  993. printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
  994. show_registers(saved_regs);
  995. printk(KERN_ERR "Current registers\n");
  996. show_registers(regs);
  997. BUG();
  998. }
  999. *regs = kcb->jprobe_saved_regs;
  1000. memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp),
  1001. kcb->jprobes_stack,
  1002. MIN_STACK_SIZE(kcb->jprobe_saved_sp));
  1003. preempt_enable_no_resched();
  1004. return 1;
  1005. }
  1006. return 0;
  1007. }
  1008. #ifdef CONFIG_OPTPROBES
  1009. /* Insert a call instruction at address 'from', which calls address 'to'.*/
  1010. static void __kprobes synthesize_relcall(void *from, void *to)
  1011. {
  1012. __synthesize_relative_insn(from, to, RELATIVECALL_OPCODE);
  1013. }
  1014. /* Insert a move instruction which sets a pointer to eax/rdi (1st arg). */
  1015. static void __kprobes synthesize_set_arg1(kprobe_opcode_t *addr,
  1016. unsigned long val)
  1017. {
  1018. #ifdef CONFIG_X86_64
  1019. *addr++ = 0x48;
  1020. *addr++ = 0xbf;
  1021. #else
  1022. *addr++ = 0xb8;
  1023. #endif
  1024. *(unsigned long *)addr = val;
  1025. }
  1026. static void __used __kprobes kprobes_optinsn_template_holder(void)
  1027. {
  1028. asm volatile (
  1029. ".global optprobe_template_entry\n"
  1030. "optprobe_template_entry: \n"
  1031. #ifdef CONFIG_X86_64
  1032. /* We don't bother saving the ss register */
  1033. " pushq %rsp\n"
  1034. " pushfq\n"
  1035. SAVE_REGS_STRING
  1036. " movq %rsp, %rsi\n"
  1037. ".global optprobe_template_val\n"
  1038. "optprobe_template_val: \n"
  1039. ASM_NOP5
  1040. ASM_NOP5
  1041. ".global optprobe_template_call\n"
  1042. "optprobe_template_call: \n"
  1043. ASM_NOP5
  1044. /* Move flags to rsp */
  1045. " movq 144(%rsp), %rdx\n"
  1046. " movq %rdx, 152(%rsp)\n"
  1047. RESTORE_REGS_STRING
  1048. /* Skip flags entry */
  1049. " addq $8, %rsp\n"
  1050. " popfq\n"
  1051. #else /* CONFIG_X86_32 */
  1052. " pushf\n"
  1053. SAVE_REGS_STRING
  1054. " movl %esp, %edx\n"
  1055. ".global optprobe_template_val\n"
  1056. "optprobe_template_val: \n"
  1057. ASM_NOP5
  1058. ".global optprobe_template_call\n"
  1059. "optprobe_template_call: \n"
  1060. ASM_NOP5
  1061. RESTORE_REGS_STRING
  1062. " addl $4, %esp\n" /* skip cs */
  1063. " popf\n"
  1064. #endif
  1065. ".global optprobe_template_end\n"
  1066. "optprobe_template_end: \n");
  1067. }
  1068. #define TMPL_MOVE_IDX \
  1069. ((long)&optprobe_template_val - (long)&optprobe_template_entry)
  1070. #define TMPL_CALL_IDX \
  1071. ((long)&optprobe_template_call - (long)&optprobe_template_entry)
  1072. #define TMPL_END_IDX \
  1073. ((long)&optprobe_template_end - (long)&optprobe_template_entry)
  1074. #define INT3_SIZE sizeof(kprobe_opcode_t)
  1075. /* Optimized kprobe call back function: called from optinsn */
  1076. static void __kprobes optimized_callback(struct optimized_kprobe *op,
  1077. struct pt_regs *regs)
  1078. {
  1079. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  1080. unsigned long flags;
  1081. /* This is possible if op is under delayed unoptimizing */
  1082. if (kprobe_disabled(&op->kp))
  1083. return;
  1084. local_irq_save(flags);
  1085. if (kprobe_running()) {
  1086. kprobes_inc_nmissed_count(&op->kp);
  1087. } else {
  1088. /* Save skipped registers */
  1089. #ifdef CONFIG_X86_64
  1090. regs->cs = __KERNEL_CS;
  1091. #else
  1092. regs->cs = __KERNEL_CS | get_kernel_rpl();
  1093. regs->gs = 0;
  1094. #endif
  1095. regs->ip = (unsigned long)op->kp.addr + INT3_SIZE;
  1096. regs->orig_ax = ~0UL;
  1097. __this_cpu_write(current_kprobe, &op->kp);
  1098. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  1099. opt_pre_handler(&op->kp, regs);
  1100. __this_cpu_write(current_kprobe, NULL);
  1101. }
  1102. local_irq_restore(flags);
  1103. }
  1104. static int __kprobes copy_optimized_instructions(u8 *dest, u8 *src)
  1105. {
  1106. int len = 0, ret;
  1107. while (len < RELATIVEJUMP_SIZE) {
  1108. ret = __copy_instruction(dest + len, src + len, 1);
  1109. if (!ret || !can_boost(dest + len))
  1110. return -EINVAL;
  1111. len += ret;
  1112. }
  1113. /* Check whether the address range is reserved */
  1114. if (ftrace_text_reserved(src, src + len - 1) ||
  1115. alternatives_text_reserved(src, src + len - 1) ||
  1116. jump_label_text_reserved(src, src + len - 1))
  1117. return -EBUSY;
  1118. return len;
  1119. }
  1120. /* Check whether insn is indirect jump */
  1121. static int __kprobes insn_is_indirect_jump(struct insn *insn)
  1122. {
  1123. return ((insn->opcode.bytes[0] == 0xff &&
  1124. (X86_MODRM_REG(insn->modrm.value) & 6) == 4) || /* Jump */
  1125. insn->opcode.bytes[0] == 0xea); /* Segment based jump */
  1126. }
  1127. /* Check whether insn jumps into specified address range */
  1128. static int insn_jump_into_range(struct insn *insn, unsigned long start, int len)
  1129. {
  1130. unsigned long target = 0;
  1131. switch (insn->opcode.bytes[0]) {
  1132. case 0xe0: /* loopne */
  1133. case 0xe1: /* loope */
  1134. case 0xe2: /* loop */
  1135. case 0xe3: /* jcxz */
  1136. case 0xe9: /* near relative jump */
  1137. case 0xeb: /* short relative jump */
  1138. break;
  1139. case 0x0f:
  1140. if ((insn->opcode.bytes[1] & 0xf0) == 0x80) /* jcc near */
  1141. break;
  1142. return 0;
  1143. default:
  1144. if ((insn->opcode.bytes[0] & 0xf0) == 0x70) /* jcc short */
  1145. break;
  1146. return 0;
  1147. }
  1148. target = (unsigned long)insn->next_byte + insn->immediate.value;
  1149. return (start <= target && target <= start + len);
  1150. }
  1151. /* Decode whole function to ensure any instructions don't jump into target */
  1152. static int __kprobes can_optimize(unsigned long paddr)
  1153. {
  1154. int ret;
  1155. unsigned long addr, size = 0, offset = 0;
  1156. struct insn insn;
  1157. kprobe_opcode_t buf[MAX_INSN_SIZE];
  1158. /* Lookup symbol including addr */
  1159. if (!kallsyms_lookup_size_offset(paddr, &size, &offset))
  1160. return 0;
  1161. /*
  1162. * Do not optimize in the entry code due to the unstable
  1163. * stack handling.
  1164. */
  1165. if ((paddr >= (unsigned long )__entry_text_start) &&
  1166. (paddr < (unsigned long )__entry_text_end))
  1167. return 0;
  1168. /* Check there is enough space for a relative jump. */
  1169. if (size - offset < RELATIVEJUMP_SIZE)
  1170. return 0;
  1171. /* Decode instructions */
  1172. addr = paddr - offset;
  1173. while (addr < paddr - offset + size) { /* Decode until function end */
  1174. if (search_exception_tables(addr))
  1175. /*
  1176. * Since some fixup code will jumps into this function,
  1177. * we can't optimize kprobe in this function.
  1178. */
  1179. return 0;
  1180. kernel_insn_init(&insn, (void *)addr);
  1181. insn_get_opcode(&insn);
  1182. if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
  1183. ret = recover_probed_instruction(buf, addr);
  1184. if (ret)
  1185. return 0;
  1186. kernel_insn_init(&insn, buf);
  1187. }
  1188. insn_get_length(&insn);
  1189. /* Recover address */
  1190. insn.kaddr = (void *)addr;
  1191. insn.next_byte = (void *)(addr + insn.length);
  1192. /* Check any instructions don't jump into target */
  1193. if (insn_is_indirect_jump(&insn) ||
  1194. insn_jump_into_range(&insn, paddr + INT3_SIZE,
  1195. RELATIVE_ADDR_SIZE))
  1196. return 0;
  1197. addr += insn.length;
  1198. }
  1199. return 1;
  1200. }
  1201. /* Check optimized_kprobe can actually be optimized. */
  1202. int __kprobes arch_check_optimized_kprobe(struct optimized_kprobe *op)
  1203. {
  1204. int i;
  1205. struct kprobe *p;
  1206. for (i = 1; i < op->optinsn.size; i++) {
  1207. p = get_kprobe(op->kp.addr + i);
  1208. if (p && !kprobe_disabled(p))
  1209. return -EEXIST;
  1210. }
  1211. return 0;
  1212. }
  1213. /* Check the addr is within the optimized instructions. */
  1214. int __kprobes arch_within_optimized_kprobe(struct optimized_kprobe *op,
  1215. unsigned long addr)
  1216. {
  1217. return ((unsigned long)op->kp.addr <= addr &&
  1218. (unsigned long)op->kp.addr + op->optinsn.size > addr);
  1219. }
  1220. /* Free optimized instruction slot */
  1221. static __kprobes
  1222. void __arch_remove_optimized_kprobe(struct optimized_kprobe *op, int dirty)
  1223. {
  1224. if (op->optinsn.insn) {
  1225. free_optinsn_slot(op->optinsn.insn, dirty);
  1226. op->optinsn.insn = NULL;
  1227. op->optinsn.size = 0;
  1228. }
  1229. }
  1230. void __kprobes arch_remove_optimized_kprobe(struct optimized_kprobe *op)
  1231. {
  1232. __arch_remove_optimized_kprobe(op, 1);
  1233. }
  1234. /*
  1235. * Copy replacing target instructions
  1236. * Target instructions MUST be relocatable (checked inside)
  1237. */
  1238. int __kprobes arch_prepare_optimized_kprobe(struct optimized_kprobe *op)
  1239. {
  1240. u8 *buf;
  1241. int ret;
  1242. long rel;
  1243. if (!can_optimize((unsigned long)op->kp.addr))
  1244. return -EILSEQ;
  1245. op->optinsn.insn = get_optinsn_slot();
  1246. if (!op->optinsn.insn)
  1247. return -ENOMEM;
  1248. /*
  1249. * Verify if the address gap is in 2GB range, because this uses
  1250. * a relative jump.
  1251. */
  1252. rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE;
  1253. if (abs(rel) > 0x7fffffff)
  1254. return -ERANGE;
  1255. buf = (u8 *)op->optinsn.insn;
  1256. /* Copy instructions into the out-of-line buffer */
  1257. ret = copy_optimized_instructions(buf + TMPL_END_IDX, op->kp.addr);
  1258. if (ret < 0) {
  1259. __arch_remove_optimized_kprobe(op, 0);
  1260. return ret;
  1261. }
  1262. op->optinsn.size = ret;
  1263. /* Copy arch-dep-instance from template */
  1264. memcpy(buf, &optprobe_template_entry, TMPL_END_IDX);
  1265. /* Set probe information */
  1266. synthesize_set_arg1(buf + TMPL_MOVE_IDX, (unsigned long)op);
  1267. /* Set probe function call */
  1268. synthesize_relcall(buf + TMPL_CALL_IDX, optimized_callback);
  1269. /* Set returning jmp instruction at the tail of out-of-line buffer */
  1270. synthesize_reljump(buf + TMPL_END_IDX + op->optinsn.size,
  1271. (u8 *)op->kp.addr + op->optinsn.size);
  1272. flush_icache_range((unsigned long) buf,
  1273. (unsigned long) buf + TMPL_END_IDX +
  1274. op->optinsn.size + RELATIVEJUMP_SIZE);
  1275. return 0;
  1276. }
  1277. #define MAX_OPTIMIZE_PROBES 256
  1278. static struct text_poke_param *jump_poke_params;
  1279. static struct jump_poke_buffer {
  1280. u8 buf[RELATIVEJUMP_SIZE];
  1281. } *jump_poke_bufs;
  1282. static void __kprobes setup_optimize_kprobe(struct text_poke_param *tprm,
  1283. u8 *insn_buf,
  1284. struct optimized_kprobe *op)
  1285. {
  1286. s32 rel = (s32)((long)op->optinsn.insn -
  1287. ((long)op->kp.addr + RELATIVEJUMP_SIZE));
  1288. /* Backup instructions which will be replaced by jump address */
  1289. memcpy(op->optinsn.copied_insn, op->kp.addr + INT3_SIZE,
  1290. RELATIVE_ADDR_SIZE);
  1291. insn_buf[0] = RELATIVEJUMP_OPCODE;
  1292. *(s32 *)(&insn_buf[1]) = rel;
  1293. tprm->addr = op->kp.addr;
  1294. tprm->opcode = insn_buf;
  1295. tprm->len = RELATIVEJUMP_SIZE;
  1296. }
  1297. /*
  1298. * Replace breakpoints (int3) with relative jumps.
  1299. * Caller must call with locking kprobe_mutex and text_mutex.
  1300. */
  1301. void __kprobes arch_optimize_kprobes(struct list_head *oplist)
  1302. {
  1303. struct optimized_kprobe *op, *tmp;
  1304. int c = 0;
  1305. list_for_each_entry_safe(op, tmp, oplist, list) {
  1306. WARN_ON(kprobe_disabled(&op->kp));
  1307. /* Setup param */
  1308. setup_optimize_kprobe(&jump_poke_params[c],
  1309. jump_poke_bufs[c].buf, op);
  1310. list_del_init(&op->list);
  1311. if (++c >= MAX_OPTIMIZE_PROBES)
  1312. break;
  1313. }
  1314. /*
  1315. * text_poke_smp doesn't support NMI/MCE code modifying.
  1316. * However, since kprobes itself also doesn't support NMI/MCE
  1317. * code probing, it's not a problem.
  1318. */
  1319. text_poke_smp_batch(jump_poke_params, c);
  1320. }
  1321. static void __kprobes setup_unoptimize_kprobe(struct text_poke_param *tprm,
  1322. u8 *insn_buf,
  1323. struct optimized_kprobe *op)
  1324. {
  1325. /* Set int3 to first byte for kprobes */
  1326. insn_buf[0] = BREAKPOINT_INSTRUCTION;
  1327. memcpy(insn_buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE);
  1328. tprm->addr = op->kp.addr;
  1329. tprm->opcode = insn_buf;
  1330. tprm->len = RELATIVEJUMP_SIZE;
  1331. }
  1332. /*
  1333. * Recover original instructions and breakpoints from relative jumps.
  1334. * Caller must call with locking kprobe_mutex.
  1335. */
  1336. extern void arch_unoptimize_kprobes(struct list_head *oplist,
  1337. struct list_head *done_list)
  1338. {
  1339. struct optimized_kprobe *op, *tmp;
  1340. int c = 0;
  1341. list_for_each_entry_safe(op, tmp, oplist, list) {
  1342. /* Setup param */
  1343. setup_unoptimize_kprobe(&jump_poke_params[c],
  1344. jump_poke_bufs[c].buf, op);
  1345. list_move(&op->list, done_list);
  1346. if (++c >= MAX_OPTIMIZE_PROBES)
  1347. break;
  1348. }
  1349. /*
  1350. * text_poke_smp doesn't support NMI/MCE code modifying.
  1351. * However, since kprobes itself also doesn't support NMI/MCE
  1352. * code probing, it's not a problem.
  1353. */
  1354. text_poke_smp_batch(jump_poke_params, c);
  1355. }
  1356. /* Replace a relative jump with a breakpoint (int3). */
  1357. void __kprobes arch_unoptimize_kprobe(struct optimized_kprobe *op)
  1358. {
  1359. u8 buf[RELATIVEJUMP_SIZE];
  1360. /* Set int3 to first byte for kprobes */
  1361. buf[0] = BREAKPOINT_INSTRUCTION;
  1362. memcpy(buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE);
  1363. text_poke_smp(op->kp.addr, buf, RELATIVEJUMP_SIZE);
  1364. }
  1365. static int __kprobes setup_detour_execution(struct kprobe *p,
  1366. struct pt_regs *regs,
  1367. int reenter)
  1368. {
  1369. struct optimized_kprobe *op;
  1370. if (p->flags & KPROBE_FLAG_OPTIMIZED) {
  1371. /* This kprobe is really able to run optimized path. */
  1372. op = container_of(p, struct optimized_kprobe, kp);
  1373. /* Detour through copied instructions */
  1374. regs->ip = (unsigned long)op->optinsn.insn + TMPL_END_IDX;
  1375. if (!reenter)
  1376. reset_current_kprobe();
  1377. preempt_enable_no_resched();
  1378. return 1;
  1379. }
  1380. return 0;
  1381. }
  1382. static int __kprobes init_poke_params(void)
  1383. {
  1384. /* Allocate code buffer and parameter array */
  1385. jump_poke_bufs = kmalloc(sizeof(struct jump_poke_buffer) *
  1386. MAX_OPTIMIZE_PROBES, GFP_KERNEL);
  1387. if (!jump_poke_bufs)
  1388. return -ENOMEM;
  1389. jump_poke_params = kmalloc(sizeof(struct text_poke_param) *
  1390. MAX_OPTIMIZE_PROBES, GFP_KERNEL);
  1391. if (!jump_poke_params) {
  1392. kfree(jump_poke_bufs);
  1393. jump_poke_bufs = NULL;
  1394. return -ENOMEM;
  1395. }
  1396. return 0;
  1397. }
  1398. #else /* !CONFIG_OPTPROBES */
  1399. static int __kprobes init_poke_params(void)
  1400. {
  1401. return 0;
  1402. }
  1403. #endif
  1404. int __init arch_init_kprobes(void)
  1405. {
  1406. return init_poke_params();
  1407. }
  1408. int __kprobes arch_trampoline_kprobe(struct kprobe *p)
  1409. {
  1410. return 0;
  1411. }