kprobes.c 40 KB

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