kprobes.c 41 KB

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