kprobes.c 45 KB

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