kprobes.c 43 KB

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