kprobes.c 40 KB

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