kprobes.c 41 KB

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