kprobes.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  1. /*
  2. * Kernel Probes (KProbes)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2002, 2004
  19. *
  20. * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
  21. * Probes initial implementation ( includes contributions from
  22. * Rusty Russell).
  23. * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
  24. * interface to access function arguments.
  25. * 2004-Oct Jim Keniston <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
  26. * <prasanna@in.ibm.com> adapted for x86_64 from i386.
  27. * 2005-Mar Roland McGrath <roland@redhat.com>
  28. * Fixed to handle %rip-relative addressing mode correctly.
  29. * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
  30. * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
  31. * <prasanna@in.ibm.com> added function-return probes.
  32. * 2005-May Rusty Lynch <rusty.lynch@intel.com>
  33. * Added function return probes functionality
  34. * 2006-Feb Masami Hiramatsu <hiramatu@sdl.hitachi.co.jp> added
  35. * kprobe-booster and kretprobe-booster for i386.
  36. * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster
  37. * and kretprobe-booster for x86-64
  38. * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com>, Arjan van de Ven
  39. * <arjan@infradead.org> and Jim Keniston <jkenisto@us.ibm.com>
  40. * unified x86 kprobes code.
  41. */
  42. #include <linux/kprobes.h>
  43. #include <linux/ptrace.h>
  44. #include <linux/string.h>
  45. #include <linux/slab.h>
  46. #include <linux/hardirq.h>
  47. #include <linux/preempt.h>
  48. #include <linux/module.h>
  49. #include <linux/kdebug.h>
  50. #include <linux/kallsyms.h>
  51. #include <linux/ftrace.h>
  52. #include <asm/cacheflush.h>
  53. #include <asm/desc.h>
  54. #include <asm/pgtable.h>
  55. #include <asm/uaccess.h>
  56. #include <asm/alternative.h>
  57. #include <asm/insn.h>
  58. #include <asm/debugreg.h>
  59. void jprobe_return_end(void);
  60. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  61. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  62. #define stack_addr(regs) ((unsigned long *)kernel_stack_pointer(regs))
  63. #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
  64. (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
  65. (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
  66. (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
  67. (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
  68. << (row % 32))
  69. /*
  70. * Undefined/reserved opcodes, conditional jump, Opcode Extension
  71. * Groups, and some special opcodes can not boost.
  72. */
  73. static const u32 twobyte_is_boostable[256 / 32] = {
  74. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  75. /* ---------------------------------------------- */
  76. W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
  77. W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 10 */
  78. W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
  79. W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
  80. W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
  81. W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
  82. W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
  83. W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
  84. W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
  85. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
  86. W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
  87. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
  88. W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
  89. W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
  90. W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
  91. W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
  92. /* ----------------------------------------------- */
  93. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  94. };
  95. #undef W
  96. struct kretprobe_blackpoint kretprobe_blacklist[] = {
  97. {"__switch_to", }, /* This function switches only current task, but
  98. doesn't switch kernel stack.*/
  99. {NULL, NULL} /* Terminator */
  100. };
  101. const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
  102. static void __kprobes __synthesize_relative_insn(void *from, void *to, u8 op)
  103. {
  104. struct __arch_relative_insn {
  105. u8 op;
  106. s32 raddr;
  107. } __attribute__((packed)) *insn;
  108. insn = (struct __arch_relative_insn *)from;
  109. insn->raddr = (s32)((long)(to) - ((long)(from) + 5));
  110. insn->op = op;
  111. }
  112. /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
  113. static void __kprobes synthesize_reljump(void *from, void *to)
  114. {
  115. __synthesize_relative_insn(from, to, RELATIVEJUMP_OPCODE);
  116. }
  117. /*
  118. * Skip the prefixes of the instruction.
  119. */
  120. static kprobe_opcode_t *__kprobes skip_prefixes(kprobe_opcode_t *insn)
  121. {
  122. insn_attr_t attr;
  123. attr = inat_get_opcode_attribute((insn_byte_t)*insn);
  124. while (inat_is_legacy_prefix(attr)) {
  125. insn++;
  126. attr = inat_get_opcode_attribute((insn_byte_t)*insn);
  127. }
  128. #ifdef CONFIG_X86_64
  129. if (inat_is_rex_prefix(attr))
  130. insn++;
  131. #endif
  132. return insn;
  133. }
  134. /*
  135. * Returns non-zero if opcode is boostable.
  136. * RIP relative instructions are adjusted at copying time in 64 bits mode
  137. */
  138. static int __kprobes can_boost(kprobe_opcode_t *opcodes)
  139. {
  140. kprobe_opcode_t opcode;
  141. kprobe_opcode_t *orig_opcodes = opcodes;
  142. if (search_exception_tables((unsigned long)opcodes))
  143. return 0; /* Page fault may occur on this address. */
  144. retry:
  145. if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
  146. return 0;
  147. opcode = *(opcodes++);
  148. /* 2nd-byte opcode */
  149. if (opcode == 0x0f) {
  150. if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
  151. return 0;
  152. return test_bit(*opcodes,
  153. (unsigned long *)twobyte_is_boostable);
  154. }
  155. switch (opcode & 0xf0) {
  156. #ifdef CONFIG_X86_64
  157. case 0x40:
  158. goto retry; /* REX prefix is boostable */
  159. #endif
  160. case 0x60:
  161. if (0x63 < opcode && opcode < 0x67)
  162. goto retry; /* prefixes */
  163. /* can't boost Address-size override and bound */
  164. return (opcode != 0x62 && opcode != 0x67);
  165. case 0x70:
  166. return 0; /* can't boost conditional jump */
  167. case 0xc0:
  168. /* can't boost software-interruptions */
  169. return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
  170. case 0xd0:
  171. /* can boost AA* and XLAT */
  172. return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
  173. case 0xe0:
  174. /* can boost in/out and absolute jmps */
  175. return ((opcode & 0x04) || opcode == 0xea);
  176. case 0xf0:
  177. if ((opcode & 0x0c) == 0 && opcode != 0xf1)
  178. goto retry; /* lock/rep(ne) prefix */
  179. /* clear and set flags are boostable */
  180. return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
  181. default:
  182. /* segment override prefixes are boostable */
  183. if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
  184. goto retry; /* prefixes */
  185. /* CS override prefix and call are not boostable */
  186. return (opcode != 0x2e && opcode != 0x9a);
  187. }
  188. }
  189. /* Recover the probed instruction at addr for further analysis. */
  190. static int recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
  191. {
  192. struct kprobe *kp;
  193. kp = get_kprobe((void *)addr);
  194. if (!kp)
  195. return -EINVAL;
  196. /*
  197. * Basically, kp->ainsn.insn has an original instruction.
  198. * However, RIP-relative instruction can not do single-stepping
  199. * at different place, __copy_instruction() tweaks the displacement of
  200. * that instruction. In that case, we can't recover the instruction
  201. * from the kp->ainsn.insn.
  202. *
  203. * On the other hand, kp->opcode has a copy of the first byte of
  204. * the probed instruction, which is overwritten by int3. And
  205. * the instruction at kp->addr is not modified by kprobes except
  206. * for the first byte, we can recover the original instruction
  207. * from it and kp->opcode.
  208. */
  209. memcpy(buf, kp->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
  210. buf[0] = kp->opcode;
  211. return 0;
  212. }
  213. /* Dummy buffers for kallsyms_lookup */
  214. static char __dummy_buf[KSYM_NAME_LEN];
  215. /* Check if paddr is at an instruction boundary */
  216. static int __kprobes can_probe(unsigned long paddr)
  217. {
  218. int ret;
  219. unsigned long addr, offset = 0;
  220. struct insn insn;
  221. kprobe_opcode_t buf[MAX_INSN_SIZE];
  222. if (!kallsyms_lookup(paddr, NULL, &offset, NULL, __dummy_buf))
  223. return 0;
  224. /* Decode instructions */
  225. addr = paddr - offset;
  226. while (addr < paddr) {
  227. kernel_insn_init(&insn, (void *)addr);
  228. insn_get_opcode(&insn);
  229. /*
  230. * Check if the instruction has been modified by another
  231. * kprobe, in which case we replace the breakpoint by the
  232. * original instruction in our buffer.
  233. */
  234. if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
  235. ret = recover_probed_instruction(buf, addr);
  236. if (ret)
  237. /*
  238. * Another debugging subsystem might insert
  239. * this breakpoint. In that case, we can't
  240. * recover it.
  241. */
  242. return 0;
  243. kernel_insn_init(&insn, buf);
  244. }
  245. insn_get_length(&insn);
  246. addr += insn.length;
  247. }
  248. return (addr == paddr);
  249. }
  250. /*
  251. * Returns non-zero if opcode modifies the interrupt flag.
  252. */
  253. static int __kprobes is_IF_modifier(kprobe_opcode_t *insn)
  254. {
  255. /* Skip prefixes */
  256. insn = skip_prefixes(insn);
  257. switch (*insn) {
  258. case 0xfa: /* cli */
  259. case 0xfb: /* sti */
  260. case 0xcf: /* iret/iretd */
  261. case 0x9d: /* popf/popfd */
  262. return 1;
  263. }
  264. return 0;
  265. }
  266. /*
  267. * Copy an instruction and adjust the displacement if the instruction
  268. * uses the %rip-relative addressing mode.
  269. * If it does, Return the address of the 32-bit displacement word.
  270. * If not, return null.
  271. * Only applicable to 64-bit x86.
  272. */
  273. static int __kprobes __copy_instruction(u8 *dest, u8 *src, int recover)
  274. {
  275. struct insn insn;
  276. int ret;
  277. kprobe_opcode_t buf[MAX_INSN_SIZE];
  278. kernel_insn_init(&insn, src);
  279. if (recover) {
  280. insn_get_opcode(&insn);
  281. if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
  282. ret = recover_probed_instruction(buf,
  283. (unsigned long)src);
  284. if (ret)
  285. return 0;
  286. kernel_insn_init(&insn, buf);
  287. }
  288. }
  289. insn_get_length(&insn);
  290. memcpy(dest, insn.kaddr, insn.length);
  291. #ifdef CONFIG_X86_64
  292. if (insn_rip_relative(&insn)) {
  293. s64 newdisp;
  294. u8 *disp;
  295. kernel_insn_init(&insn, dest);
  296. insn_get_displacement(&insn);
  297. /*
  298. * The copied instruction uses the %rip-relative addressing
  299. * mode. Adjust the displacement for the difference between
  300. * the original location of this instruction and the location
  301. * of the copy that will actually be run. The tricky bit here
  302. * is making sure that the sign extension happens correctly in
  303. * this calculation, since we need a signed 32-bit result to
  304. * be sign-extended to 64 bits when it's added to the %rip
  305. * value and yield the same 64-bit result that the sign-
  306. * extension of the original signed 32-bit displacement would
  307. * have given.
  308. */
  309. newdisp = (u8 *) src + (s64) insn.displacement.value -
  310. (u8 *) dest;
  311. BUG_ON((s64) (s32) newdisp != newdisp); /* Sanity check. */
  312. disp = (u8 *) dest + insn_offset_displacement(&insn);
  313. *(s32 *) disp = (s32) newdisp;
  314. }
  315. #endif
  316. return insn.length;
  317. }
  318. static void __kprobes arch_copy_kprobe(struct kprobe *p)
  319. {
  320. /*
  321. * Copy an instruction without recovering int3, because it will be
  322. * put by another subsystem.
  323. */
  324. __copy_instruction(p->ainsn.insn, p->addr, 0);
  325. if (can_boost(p->addr))
  326. p->ainsn.boostable = 0;
  327. else
  328. p->ainsn.boostable = -1;
  329. p->opcode = *p->addr;
  330. }
  331. int __kprobes arch_prepare_kprobe(struct kprobe *p)
  332. {
  333. if (alternatives_text_reserved(p->addr, p->addr))
  334. return -EINVAL;
  335. if (!can_probe((unsigned long)p->addr))
  336. return -EILSEQ;
  337. /* insn: must be on special executable page on x86. */
  338. p->ainsn.insn = get_insn_slot();
  339. if (!p->ainsn.insn)
  340. return -ENOMEM;
  341. arch_copy_kprobe(p);
  342. return 0;
  343. }
  344. void __kprobes arch_arm_kprobe(struct kprobe *p)
  345. {
  346. text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
  347. }
  348. void __kprobes arch_disarm_kprobe(struct kprobe *p)
  349. {
  350. text_poke(p->addr, &p->opcode, 1);
  351. }
  352. void __kprobes arch_remove_kprobe(struct kprobe *p)
  353. {
  354. if (p->ainsn.insn) {
  355. free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
  356. p->ainsn.insn = NULL;
  357. }
  358. }
  359. static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
  360. {
  361. kcb->prev_kprobe.kp = kprobe_running();
  362. kcb->prev_kprobe.status = kcb->kprobe_status;
  363. kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
  364. kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
  365. }
  366. static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  367. {
  368. __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
  369. kcb->kprobe_status = kcb->prev_kprobe.status;
  370. kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
  371. kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
  372. }
  373. static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  374. struct kprobe_ctlblk *kcb)
  375. {
  376. __get_cpu_var(current_kprobe) = p;
  377. kcb->kprobe_saved_flags = kcb->kprobe_old_flags
  378. = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
  379. if (is_IF_modifier(p->ainsn.insn))
  380. kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
  381. }
  382. static void __kprobes clear_btf(void)
  383. {
  384. if (test_thread_flag(TIF_BLOCKSTEP)) {
  385. unsigned long debugctl = get_debugctlmsr();
  386. debugctl &= ~DEBUGCTLMSR_BTF;
  387. update_debugctlmsr(debugctl);
  388. }
  389. }
  390. static void __kprobes restore_btf(void)
  391. {
  392. if (test_thread_flag(TIF_BLOCKSTEP)) {
  393. unsigned long debugctl = get_debugctlmsr();
  394. debugctl |= DEBUGCTLMSR_BTF;
  395. update_debugctlmsr(debugctl);
  396. }
  397. }
  398. void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
  399. struct pt_regs *regs)
  400. {
  401. unsigned long *sara = stack_addr(regs);
  402. ri->ret_addr = (kprobe_opcode_t *) *sara;
  403. /* Replace the return addr with trampoline addr */
  404. *sara = (unsigned long) &kretprobe_trampoline;
  405. }
  406. #ifdef CONFIG_OPTPROBES
  407. static int __kprobes setup_detour_execution(struct kprobe *p,
  408. struct pt_regs *regs,
  409. int reenter);
  410. #else
  411. #define setup_detour_execution(p, regs, reenter) (0)
  412. #endif
  413. static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs,
  414. struct kprobe_ctlblk *kcb, int reenter)
  415. {
  416. if (setup_detour_execution(p, regs, reenter))
  417. return;
  418. #if !defined(CONFIG_PREEMPT)
  419. if (p->ainsn.boostable == 1 && !p->post_handler) {
  420. /* Boost up -- we can execute copied instructions directly */
  421. if (!reenter)
  422. reset_current_kprobe();
  423. /*
  424. * Reentering boosted probe doesn't reset current_kprobe,
  425. * nor set current_kprobe, because it doesn't use single
  426. * stepping.
  427. */
  428. regs->ip = (unsigned long)p->ainsn.insn;
  429. preempt_enable_no_resched();
  430. return;
  431. }
  432. #endif
  433. if (reenter) {
  434. save_previous_kprobe(kcb);
  435. set_current_kprobe(p, regs, kcb);
  436. kcb->kprobe_status = KPROBE_REENTER;
  437. } else
  438. kcb->kprobe_status = KPROBE_HIT_SS;
  439. /* Prepare real single stepping */
  440. clear_btf();
  441. regs->flags |= X86_EFLAGS_TF;
  442. regs->flags &= ~X86_EFLAGS_IF;
  443. /* single step inline if the instruction is an int3 */
  444. if (p->opcode == BREAKPOINT_INSTRUCTION)
  445. regs->ip = (unsigned long)p->addr;
  446. else
  447. regs->ip = (unsigned long)p->ainsn.insn;
  448. }
  449. /*
  450. * We have reentered the kprobe_handler(), since another probe was hit while
  451. * within the handler. We save the original kprobes variables and just single
  452. * step on the instruction of the new probe without calling any user handlers.
  453. */
  454. static int __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
  455. struct kprobe_ctlblk *kcb)
  456. {
  457. switch (kcb->kprobe_status) {
  458. case KPROBE_HIT_SSDONE:
  459. case KPROBE_HIT_ACTIVE:
  460. kprobes_inc_nmissed_count(p);
  461. setup_singlestep(p, regs, kcb, 1);
  462. break;
  463. case KPROBE_HIT_SS:
  464. /* A probe has been hit in the codepath leading up to, or just
  465. * after, single-stepping of a probed instruction. This entire
  466. * codepath should strictly reside in .kprobes.text section.
  467. * Raise a BUG or we'll continue in an endless reentering loop
  468. * and eventually a stack overflow.
  469. */
  470. printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n",
  471. p->addr);
  472. dump_kprobe(p);
  473. BUG();
  474. default:
  475. /* impossible cases */
  476. WARN_ON(1);
  477. return 0;
  478. }
  479. return 1;
  480. }
  481. /*
  482. * Interrupts are disabled on entry as trap3 is an interrupt gate and they
  483. * remain disabled throughout this function.
  484. */
  485. static int __kprobes kprobe_handler(struct pt_regs *regs)
  486. {
  487. kprobe_opcode_t *addr;
  488. struct kprobe *p;
  489. struct kprobe_ctlblk *kcb;
  490. addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
  491. /*
  492. * We don't want to be preempted for the entire
  493. * duration of kprobe processing. We conditionally
  494. * re-enable preemption at the end of this function,
  495. * and also in reenter_kprobe() and setup_singlestep().
  496. */
  497. preempt_disable();
  498. kcb = get_kprobe_ctlblk();
  499. p = get_kprobe(addr);
  500. if (p) {
  501. if (kprobe_running()) {
  502. if (reenter_kprobe(p, regs, kcb))
  503. return 1;
  504. } else {
  505. set_current_kprobe(p, regs, kcb);
  506. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  507. /*
  508. * If we have no pre-handler or it returned 0, we
  509. * continue with normal processing. If we have a
  510. * pre-handler and it returned non-zero, it prepped
  511. * for calling the break_handler below on re-entry
  512. * for jprobe processing, so get out doing nothing
  513. * more here.
  514. */
  515. if (!p->pre_handler || !p->pre_handler(p, regs))
  516. setup_singlestep(p, regs, kcb, 0);
  517. return 1;
  518. }
  519. } else if (*addr != BREAKPOINT_INSTRUCTION) {
  520. /*
  521. * The breakpoint instruction was removed right
  522. * after we hit it. Another cpu has removed
  523. * either a probepoint or a debugger breakpoint
  524. * at this address. In either case, no further
  525. * handling of this interrupt is appropriate.
  526. * Back up over the (now missing) int3 and run
  527. * the original instruction.
  528. */
  529. regs->ip = (unsigned long)addr;
  530. preempt_enable_no_resched();
  531. return 1;
  532. } else if (kprobe_running()) {
  533. p = __get_cpu_var(current_kprobe);
  534. if (p->break_handler && p->break_handler(p, regs)) {
  535. setup_singlestep(p, regs, kcb, 0);
  536. return 1;
  537. }
  538. } /* else: not a kprobe fault; let the kernel handle it */
  539. preempt_enable_no_resched();
  540. return 0;
  541. }
  542. #ifdef CONFIG_X86_64
  543. #define SAVE_REGS_STRING \
  544. /* Skip cs, ip, orig_ax. */ \
  545. " subq $24, %rsp\n" \
  546. " pushq %rdi\n" \
  547. " pushq %rsi\n" \
  548. " pushq %rdx\n" \
  549. " pushq %rcx\n" \
  550. " pushq %rax\n" \
  551. " pushq %r8\n" \
  552. " pushq %r9\n" \
  553. " pushq %r10\n" \
  554. " pushq %r11\n" \
  555. " pushq %rbx\n" \
  556. " pushq %rbp\n" \
  557. " pushq %r12\n" \
  558. " pushq %r13\n" \
  559. " pushq %r14\n" \
  560. " pushq %r15\n"
  561. #define RESTORE_REGS_STRING \
  562. " popq %r15\n" \
  563. " popq %r14\n" \
  564. " popq %r13\n" \
  565. " popq %r12\n" \
  566. " popq %rbp\n" \
  567. " popq %rbx\n" \
  568. " popq %r11\n" \
  569. " popq %r10\n" \
  570. " popq %r9\n" \
  571. " popq %r8\n" \
  572. " popq %rax\n" \
  573. " popq %rcx\n" \
  574. " popq %rdx\n" \
  575. " popq %rsi\n" \
  576. " popq %rdi\n" \
  577. /* Skip orig_ax, ip, cs */ \
  578. " addq $24, %rsp\n"
  579. #else
  580. #define SAVE_REGS_STRING \
  581. /* Skip cs, ip, orig_ax and gs. */ \
  582. " subl $16, %esp\n" \
  583. " pushl %fs\n" \
  584. " pushl %es\n" \
  585. " pushl %ds\n" \
  586. " pushl %eax\n" \
  587. " pushl %ebp\n" \
  588. " pushl %edi\n" \
  589. " pushl %esi\n" \
  590. " pushl %edx\n" \
  591. " pushl %ecx\n" \
  592. " pushl %ebx\n"
  593. #define RESTORE_REGS_STRING \
  594. " popl %ebx\n" \
  595. " popl %ecx\n" \
  596. " popl %edx\n" \
  597. " popl %esi\n" \
  598. " popl %edi\n" \
  599. " popl %ebp\n" \
  600. " popl %eax\n" \
  601. /* Skip ds, es, fs, gs, orig_ax, and ip. Note: don't pop cs here*/\
  602. " addl $24, %esp\n"
  603. #endif
  604. /*
  605. * When a retprobed function returns, this code saves registers and
  606. * calls trampoline_handler() runs, which calls the kretprobe's handler.
  607. */
  608. static void __used __kprobes kretprobe_trampoline_holder(void)
  609. {
  610. asm volatile (
  611. ".global kretprobe_trampoline\n"
  612. "kretprobe_trampoline: \n"
  613. #ifdef CONFIG_X86_64
  614. /* We don't bother saving the ss register */
  615. " pushq %rsp\n"
  616. " pushfq\n"
  617. SAVE_REGS_STRING
  618. " movq %rsp, %rdi\n"
  619. " call trampoline_handler\n"
  620. /* Replace saved sp with true return address. */
  621. " movq %rax, 152(%rsp)\n"
  622. RESTORE_REGS_STRING
  623. " popfq\n"
  624. #else
  625. " pushf\n"
  626. SAVE_REGS_STRING
  627. " movl %esp, %eax\n"
  628. " call trampoline_handler\n"
  629. /* Move flags to cs */
  630. " movl 56(%esp), %edx\n"
  631. " movl %edx, 52(%esp)\n"
  632. /* Replace saved flags with true return address. */
  633. " movl %eax, 56(%esp)\n"
  634. RESTORE_REGS_STRING
  635. " popf\n"
  636. #endif
  637. " ret\n");
  638. }
  639. /*
  640. * Called from kretprobe_trampoline
  641. */
  642. static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
  643. {
  644. struct kretprobe_instance *ri = NULL;
  645. struct hlist_head *head, empty_rp;
  646. struct hlist_node *node, *tmp;
  647. unsigned long flags, orig_ret_address = 0;
  648. unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
  649. INIT_HLIST_HEAD(&empty_rp);
  650. kretprobe_hash_lock(current, &head, &flags);
  651. /* fixup registers */
  652. #ifdef CONFIG_X86_64
  653. regs->cs = __KERNEL_CS;
  654. #else
  655. regs->cs = __KERNEL_CS | get_kernel_rpl();
  656. regs->gs = 0;
  657. #endif
  658. regs->ip = trampoline_address;
  659. regs->orig_ax = ~0UL;
  660. /*
  661. * It is possible to have multiple instances associated with a given
  662. * task either because multiple functions in the call path have
  663. * return probes installed on them, and/or more than one
  664. * return probe was registered for a target function.
  665. *
  666. * We can handle this because:
  667. * - instances are always pushed into the head of the list
  668. * - when multiple return probes are registered for the same
  669. * function, the (chronologically) first instance's ret_addr
  670. * will be the real return address, and all the rest will
  671. * point to kretprobe_trampoline.
  672. */
  673. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  674. if (ri->task != current)
  675. /* another task is sharing our hash bucket */
  676. continue;
  677. if (ri->rp && ri->rp->handler) {
  678. __get_cpu_var(current_kprobe) = &ri->rp->kp;
  679. get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
  680. ri->rp->handler(ri, regs);
  681. __get_cpu_var(current_kprobe) = NULL;
  682. }
  683. orig_ret_address = (unsigned long)ri->ret_addr;
  684. recycle_rp_inst(ri, &empty_rp);
  685. if (orig_ret_address != trampoline_address)
  686. /*
  687. * This is the real return address. Any other
  688. * instances associated with this task are for
  689. * other calls deeper on the call stack
  690. */
  691. break;
  692. }
  693. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  694. kretprobe_hash_unlock(current, &flags);
  695. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  696. hlist_del(&ri->hlist);
  697. kfree(ri);
  698. }
  699. return (void *)orig_ret_address;
  700. }
  701. /*
  702. * Called after single-stepping. p->addr is the address of the
  703. * instruction whose first byte has been replaced by the "int 3"
  704. * instruction. To avoid the SMP problems that can occur when we
  705. * temporarily put back the original opcode to single-step, we
  706. * single-stepped a copy of the instruction. The address of this
  707. * copy is p->ainsn.insn.
  708. *
  709. * This function prepares to return from the post-single-step
  710. * interrupt. We have to fix up the stack as follows:
  711. *
  712. * 0) Except in the case of absolute or indirect jump or call instructions,
  713. * the new ip is relative to the copied instruction. We need to make
  714. * it relative to the original instruction.
  715. *
  716. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  717. * flags are set in the just-pushed flags, and may need to be cleared.
  718. *
  719. * 2) If the single-stepped instruction was a call, the return address
  720. * that is atop the stack is the address following the copied instruction.
  721. * We need to make it the address following the original instruction.
  722. *
  723. * If this is the first time we've single-stepped the instruction at
  724. * this probepoint, and the instruction is boostable, boost it: add a
  725. * jump instruction after the copied instruction, that jumps to the next
  726. * instruction after the probepoint.
  727. */
  728. static void __kprobes resume_execution(struct kprobe *p,
  729. struct pt_regs *regs, struct kprobe_ctlblk *kcb)
  730. {
  731. unsigned long *tos = stack_addr(regs);
  732. unsigned long copy_ip = (unsigned long)p->ainsn.insn;
  733. unsigned long orig_ip = (unsigned long)p->addr;
  734. kprobe_opcode_t *insn = p->ainsn.insn;
  735. /* Skip prefixes */
  736. insn = skip_prefixes(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. }