core.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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 <linux/frame.h>
  53. #include <asm/text-patching.h>
  54. #include <asm/cacheflush.h>
  55. #include <asm/desc.h>
  56. #include <asm/pgtable.h>
  57. #include <asm/uaccess.h>
  58. #include <asm/alternative.h>
  59. #include <asm/insn.h>
  60. #include <asm/debugreg.h>
  61. #include "common.h"
  62. void jprobe_return_end(void);
  63. DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
  64. DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  65. #define stack_addr(regs) ((unsigned long *)kernel_stack_pointer(regs))
  66. #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
  67. (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
  68. (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
  69. (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
  70. (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
  71. << (row % 32))
  72. /*
  73. * Undefined/reserved opcodes, conditional jump, Opcode Extension
  74. * Groups, and some special opcodes can not boost.
  75. * This is non-const and volatile to keep gcc from statically
  76. * optimizing it out, as variable_test_bit makes gcc think only
  77. * *(unsigned long*) is used.
  78. */
  79. static volatile u32 twobyte_is_boostable[256 / 32] = {
  80. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  81. /* ---------------------------------------------- */
  82. W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
  83. W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1) , /* 10 */
  84. W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
  85. W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
  86. W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
  87. W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
  88. W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
  89. W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
  90. W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
  91. W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
  92. W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
  93. W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
  94. W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
  95. W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
  96. W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
  97. W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
  98. /* ----------------------------------------------- */
  99. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  100. };
  101. #undef W
  102. struct kretprobe_blackpoint kretprobe_blacklist[] = {
  103. {"__switch_to", }, /* This function switches only current task, but
  104. doesn't switch kernel stack.*/
  105. {NULL, NULL} /* Terminator */
  106. };
  107. const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
  108. static nokprobe_inline void
  109. __synthesize_relative_insn(void *from, void *to, u8 op)
  110. {
  111. struct __arch_relative_insn {
  112. u8 op;
  113. s32 raddr;
  114. } __packed *insn;
  115. insn = (struct __arch_relative_insn *)from;
  116. insn->raddr = (s32)((long)(to) - ((long)(from) + 5));
  117. insn->op = op;
  118. }
  119. /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
  120. void synthesize_reljump(void *from, void *to)
  121. {
  122. __synthesize_relative_insn(from, to, RELATIVEJUMP_OPCODE);
  123. }
  124. NOKPROBE_SYMBOL(synthesize_reljump);
  125. /* Insert a call instruction at address 'from', which calls address 'to'.*/
  126. void synthesize_relcall(void *from, void *to)
  127. {
  128. __synthesize_relative_insn(from, to, RELATIVECALL_OPCODE);
  129. }
  130. NOKPROBE_SYMBOL(synthesize_relcall);
  131. /*
  132. * Skip the prefixes of the instruction.
  133. */
  134. static kprobe_opcode_t *skip_prefixes(kprobe_opcode_t *insn)
  135. {
  136. insn_attr_t attr;
  137. attr = inat_get_opcode_attribute((insn_byte_t)*insn);
  138. while (inat_is_legacy_prefix(attr)) {
  139. insn++;
  140. attr = inat_get_opcode_attribute((insn_byte_t)*insn);
  141. }
  142. #ifdef CONFIG_X86_64
  143. if (inat_is_rex_prefix(attr))
  144. insn++;
  145. #endif
  146. return insn;
  147. }
  148. NOKPROBE_SYMBOL(skip_prefixes);
  149. /*
  150. * Returns non-zero if opcode is boostable.
  151. * RIP relative instructions are adjusted at copying time in 64 bits mode
  152. */
  153. int can_boost(kprobe_opcode_t *opcodes)
  154. {
  155. kprobe_opcode_t opcode;
  156. kprobe_opcode_t *orig_opcodes = opcodes;
  157. if (search_exception_tables((unsigned long)opcodes))
  158. return 0; /* Page fault may occur on this address. */
  159. retry:
  160. if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
  161. return 0;
  162. opcode = *(opcodes++);
  163. /* 2nd-byte opcode */
  164. if (opcode == 0x0f) {
  165. if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
  166. return 0;
  167. return test_bit(*opcodes,
  168. (unsigned long *)twobyte_is_boostable);
  169. }
  170. switch (opcode & 0xf0) {
  171. #ifdef CONFIG_X86_64
  172. case 0x40:
  173. goto retry; /* REX prefix is boostable */
  174. #endif
  175. case 0x60:
  176. if (0x63 < opcode && opcode < 0x67)
  177. goto retry; /* prefixes */
  178. /* can't boost Address-size override and bound */
  179. return (opcode != 0x62 && opcode != 0x67);
  180. case 0x70:
  181. return 0; /* can't boost conditional jump */
  182. case 0xc0:
  183. /* can't boost software-interruptions */
  184. return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
  185. case 0xd0:
  186. /* can boost AA* and XLAT */
  187. return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
  188. case 0xe0:
  189. /* can boost in/out and absolute jmps */
  190. return ((opcode & 0x04) || opcode == 0xea);
  191. case 0xf0:
  192. if ((opcode & 0x0c) == 0 && opcode != 0xf1)
  193. goto retry; /* lock/rep(ne) prefix */
  194. /* clear and set flags are boostable */
  195. return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
  196. default:
  197. /* segment override prefixes are boostable */
  198. if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
  199. goto retry; /* prefixes */
  200. /* CS override prefix and call are not boostable */
  201. return (opcode != 0x2e && opcode != 0x9a);
  202. }
  203. }
  204. static unsigned long
  205. __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr)
  206. {
  207. struct kprobe *kp;
  208. unsigned long faddr;
  209. kp = get_kprobe((void *)addr);
  210. faddr = ftrace_location(addr);
  211. /*
  212. * Addresses inside the ftrace location are refused by
  213. * arch_check_ftrace_location(). Something went terribly wrong
  214. * if such an address is checked here.
  215. */
  216. if (WARN_ON(faddr && faddr != addr))
  217. return 0UL;
  218. /*
  219. * Use the current code if it is not modified by Kprobe
  220. * and it cannot be modified by ftrace.
  221. */
  222. if (!kp && !faddr)
  223. return addr;
  224. /*
  225. * Basically, kp->ainsn.insn has an original instruction.
  226. * However, RIP-relative instruction can not do single-stepping
  227. * at different place, __copy_instruction() tweaks the displacement of
  228. * that instruction. In that case, we can't recover the instruction
  229. * from the kp->ainsn.insn.
  230. *
  231. * On the other hand, in case on normal Kprobe, kp->opcode has a copy
  232. * of the first byte of the probed instruction, which is overwritten
  233. * by int3. And the instruction at kp->addr is not modified by kprobes
  234. * except for the first byte, we can recover the original instruction
  235. * from it and kp->opcode.
  236. *
  237. * In case of Kprobes using ftrace, we do not have a copy of
  238. * the original instruction. In fact, the ftrace location might
  239. * be modified at anytime and even could be in an inconsistent state.
  240. * Fortunately, we know that the original code is the ideal 5-byte
  241. * long NOP.
  242. */
  243. memcpy(buf, (void *)addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
  244. if (faddr)
  245. memcpy(buf, ideal_nops[NOP_ATOMIC5], 5);
  246. else
  247. buf[0] = kp->opcode;
  248. return (unsigned long)buf;
  249. }
  250. /*
  251. * Recover the probed instruction at addr for further analysis.
  252. * Caller must lock kprobes by kprobe_mutex, or disable preemption
  253. * for preventing to release referencing kprobes.
  254. * Returns zero if the instruction can not get recovered.
  255. */
  256. unsigned long recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
  257. {
  258. unsigned long __addr;
  259. __addr = __recover_optprobed_insn(buf, addr);
  260. if (__addr != addr)
  261. return __addr;
  262. return __recover_probed_insn(buf, addr);
  263. }
  264. /* Check if paddr is at an instruction boundary */
  265. static int can_probe(unsigned long paddr)
  266. {
  267. unsigned long addr, __addr, offset = 0;
  268. struct insn insn;
  269. kprobe_opcode_t buf[MAX_INSN_SIZE];
  270. if (!kallsyms_lookup_size_offset(paddr, NULL, &offset))
  271. return 0;
  272. /* Decode instructions */
  273. addr = paddr - offset;
  274. while (addr < paddr) {
  275. /*
  276. * Check if the instruction has been modified by another
  277. * kprobe, in which case we replace the breakpoint by the
  278. * original instruction in our buffer.
  279. * Also, jump optimization will change the breakpoint to
  280. * relative-jump. Since the relative-jump itself is
  281. * normally used, we just go through if there is no kprobe.
  282. */
  283. __addr = recover_probed_instruction(buf, addr);
  284. if (!__addr)
  285. return 0;
  286. kernel_insn_init(&insn, (void *)__addr, MAX_INSN_SIZE);
  287. insn_get_length(&insn);
  288. /*
  289. * Another debugging subsystem might insert this breakpoint.
  290. * In that case, we can't recover it.
  291. */
  292. if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
  293. return 0;
  294. addr += insn.length;
  295. }
  296. return (addr == paddr);
  297. }
  298. /*
  299. * Returns non-zero if opcode modifies the interrupt flag.
  300. */
  301. static int is_IF_modifier(kprobe_opcode_t *insn)
  302. {
  303. /* Skip prefixes */
  304. insn = skip_prefixes(insn);
  305. switch (*insn) {
  306. case 0xfa: /* cli */
  307. case 0xfb: /* sti */
  308. case 0xcf: /* iret/iretd */
  309. case 0x9d: /* popf/popfd */
  310. return 1;
  311. }
  312. return 0;
  313. }
  314. /*
  315. * Copy an instruction and adjust the displacement if the instruction
  316. * uses the %rip-relative addressing mode.
  317. * If it does, Return the address of the 32-bit displacement word.
  318. * If not, return null.
  319. * Only applicable to 64-bit x86.
  320. */
  321. int __copy_instruction(u8 *dest, u8 *src)
  322. {
  323. struct insn insn;
  324. kprobe_opcode_t buf[MAX_INSN_SIZE];
  325. int length;
  326. unsigned long recovered_insn =
  327. recover_probed_instruction(buf, (unsigned long)src);
  328. if (!recovered_insn)
  329. return 0;
  330. kernel_insn_init(&insn, (void *)recovered_insn, MAX_INSN_SIZE);
  331. insn_get_length(&insn);
  332. length = insn.length;
  333. /* Another subsystem puts a breakpoint, failed to recover */
  334. if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
  335. return 0;
  336. memcpy(dest, insn.kaddr, length);
  337. #ifdef CONFIG_X86_64
  338. if (insn_rip_relative(&insn)) {
  339. s64 newdisp;
  340. u8 *disp;
  341. kernel_insn_init(&insn, dest, length);
  342. insn_get_displacement(&insn);
  343. /*
  344. * The copied instruction uses the %rip-relative addressing
  345. * mode. Adjust the displacement for the difference between
  346. * the original location of this instruction and the location
  347. * of the copy that will actually be run. The tricky bit here
  348. * is making sure that the sign extension happens correctly in
  349. * this calculation, since we need a signed 32-bit result to
  350. * be sign-extended to 64 bits when it's added to the %rip
  351. * value and yield the same 64-bit result that the sign-
  352. * extension of the original signed 32-bit displacement would
  353. * have given.
  354. */
  355. newdisp = (u8 *) src + (s64) insn.displacement.value - (u8 *) dest;
  356. if ((s64) (s32) newdisp != newdisp) {
  357. pr_err("Kprobes error: new displacement does not fit into s32 (%llx)\n", newdisp);
  358. pr_err("\tSrc: %p, Dest: %p, old disp: %x\n", src, dest, insn.displacement.value);
  359. return 0;
  360. }
  361. disp = (u8 *) dest + insn_offset_displacement(&insn);
  362. *(s32 *) disp = (s32) newdisp;
  363. }
  364. #endif
  365. return length;
  366. }
  367. static int arch_copy_kprobe(struct kprobe *p)
  368. {
  369. int ret;
  370. /* Copy an instruction with recovering if other optprobe modifies it.*/
  371. ret = __copy_instruction(p->ainsn.insn, p->addr);
  372. if (!ret)
  373. return -EINVAL;
  374. /*
  375. * __copy_instruction can modify the displacement of the instruction,
  376. * but it doesn't affect boostable check.
  377. */
  378. if (can_boost(p->ainsn.insn))
  379. p->ainsn.boostable = 0;
  380. else
  381. p->ainsn.boostable = -1;
  382. /* Check whether the instruction modifies Interrupt Flag or not */
  383. p->ainsn.if_modifier = is_IF_modifier(p->ainsn.insn);
  384. /* Also, displacement change doesn't affect the first byte */
  385. p->opcode = p->ainsn.insn[0];
  386. return 0;
  387. }
  388. int arch_prepare_kprobe(struct kprobe *p)
  389. {
  390. if (alternatives_text_reserved(p->addr, p->addr))
  391. return -EINVAL;
  392. if (!can_probe((unsigned long)p->addr))
  393. return -EILSEQ;
  394. /* insn: must be on special executable page on x86. */
  395. p->ainsn.insn = get_insn_slot();
  396. if (!p->ainsn.insn)
  397. return -ENOMEM;
  398. return arch_copy_kprobe(p);
  399. }
  400. void arch_arm_kprobe(struct kprobe *p)
  401. {
  402. text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
  403. }
  404. void arch_disarm_kprobe(struct kprobe *p)
  405. {
  406. text_poke(p->addr, &p->opcode, 1);
  407. }
  408. void arch_remove_kprobe(struct kprobe *p)
  409. {
  410. if (p->ainsn.insn) {
  411. free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
  412. p->ainsn.insn = NULL;
  413. }
  414. }
  415. static nokprobe_inline void
  416. save_previous_kprobe(struct kprobe_ctlblk *kcb)
  417. {
  418. kcb->prev_kprobe.kp = kprobe_running();
  419. kcb->prev_kprobe.status = kcb->kprobe_status;
  420. kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
  421. kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
  422. }
  423. static nokprobe_inline void
  424. restore_previous_kprobe(struct kprobe_ctlblk *kcb)
  425. {
  426. __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
  427. kcb->kprobe_status = kcb->prev_kprobe.status;
  428. kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
  429. kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
  430. }
  431. static nokprobe_inline void
  432. set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
  433. struct kprobe_ctlblk *kcb)
  434. {
  435. __this_cpu_write(current_kprobe, p);
  436. kcb->kprobe_saved_flags = kcb->kprobe_old_flags
  437. = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
  438. if (p->ainsn.if_modifier)
  439. kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
  440. }
  441. static nokprobe_inline void clear_btf(void)
  442. {
  443. if (test_thread_flag(TIF_BLOCKSTEP)) {
  444. unsigned long debugctl = get_debugctlmsr();
  445. debugctl &= ~DEBUGCTLMSR_BTF;
  446. update_debugctlmsr(debugctl);
  447. }
  448. }
  449. static nokprobe_inline void restore_btf(void)
  450. {
  451. if (test_thread_flag(TIF_BLOCKSTEP)) {
  452. unsigned long debugctl = get_debugctlmsr();
  453. debugctl |= DEBUGCTLMSR_BTF;
  454. update_debugctlmsr(debugctl);
  455. }
  456. }
  457. void arch_prepare_kretprobe(struct kretprobe_instance *ri, struct pt_regs *regs)
  458. {
  459. unsigned long *sara = stack_addr(regs);
  460. ri->ret_addr = (kprobe_opcode_t *) *sara;
  461. /* Replace the return addr with trampoline addr */
  462. *sara = (unsigned long) &kretprobe_trampoline;
  463. }
  464. NOKPROBE_SYMBOL(arch_prepare_kretprobe);
  465. static void setup_singlestep(struct kprobe *p, struct pt_regs *regs,
  466. struct kprobe_ctlblk *kcb, int reenter)
  467. {
  468. if (setup_detour_execution(p, regs, reenter))
  469. return;
  470. #if !defined(CONFIG_PREEMPT)
  471. if (p->ainsn.boostable == 1 && !p->post_handler) {
  472. /* Boost up -- we can execute copied instructions directly */
  473. if (!reenter)
  474. reset_current_kprobe();
  475. /*
  476. * Reentering boosted probe doesn't reset current_kprobe,
  477. * nor set current_kprobe, because it doesn't use single
  478. * stepping.
  479. */
  480. regs->ip = (unsigned long)p->ainsn.insn;
  481. preempt_enable_no_resched();
  482. return;
  483. }
  484. #endif
  485. if (reenter) {
  486. save_previous_kprobe(kcb);
  487. set_current_kprobe(p, regs, kcb);
  488. kcb->kprobe_status = KPROBE_REENTER;
  489. } else
  490. kcb->kprobe_status = KPROBE_HIT_SS;
  491. /* Prepare real single stepping */
  492. clear_btf();
  493. regs->flags |= X86_EFLAGS_TF;
  494. regs->flags &= ~X86_EFLAGS_IF;
  495. /* single step inline if the instruction is an int3 */
  496. if (p->opcode == BREAKPOINT_INSTRUCTION)
  497. regs->ip = (unsigned long)p->addr;
  498. else
  499. regs->ip = (unsigned long)p->ainsn.insn;
  500. }
  501. NOKPROBE_SYMBOL(setup_singlestep);
  502. /*
  503. * We have reentered the kprobe_handler(), since another probe was hit while
  504. * within the handler. We save the original kprobes variables and just single
  505. * step on the instruction of the new probe without calling any user handlers.
  506. */
  507. static int reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
  508. struct kprobe_ctlblk *kcb)
  509. {
  510. switch (kcb->kprobe_status) {
  511. case KPROBE_HIT_SSDONE:
  512. case KPROBE_HIT_ACTIVE:
  513. case KPROBE_HIT_SS:
  514. kprobes_inc_nmissed_count(p);
  515. setup_singlestep(p, regs, kcb, 1);
  516. break;
  517. case KPROBE_REENTER:
  518. /* A probe has been hit in the codepath leading up to, or just
  519. * after, single-stepping of a probed instruction. This entire
  520. * codepath should strictly reside in .kprobes.text section.
  521. * Raise a BUG or we'll continue in an endless reentering loop
  522. * and eventually a stack overflow.
  523. */
  524. printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n",
  525. p->addr);
  526. dump_kprobe(p);
  527. BUG();
  528. default:
  529. /* impossible cases */
  530. WARN_ON(1);
  531. return 0;
  532. }
  533. return 1;
  534. }
  535. NOKPROBE_SYMBOL(reenter_kprobe);
  536. /*
  537. * Interrupts are disabled on entry as trap3 is an interrupt gate and they
  538. * remain disabled throughout this function.
  539. */
  540. int kprobe_int3_handler(struct pt_regs *regs)
  541. {
  542. kprobe_opcode_t *addr;
  543. struct kprobe *p;
  544. struct kprobe_ctlblk *kcb;
  545. if (user_mode(regs))
  546. return 0;
  547. addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
  548. /*
  549. * We don't want to be preempted for the entire
  550. * duration of kprobe processing. We conditionally
  551. * re-enable preemption at the end of this function,
  552. * and also in reenter_kprobe() and setup_singlestep().
  553. */
  554. preempt_disable();
  555. kcb = get_kprobe_ctlblk();
  556. p = get_kprobe(addr);
  557. if (p) {
  558. if (kprobe_running()) {
  559. if (reenter_kprobe(p, regs, kcb))
  560. return 1;
  561. } else {
  562. set_current_kprobe(p, regs, kcb);
  563. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  564. /*
  565. * If we have no pre-handler or it returned 0, we
  566. * continue with normal processing. If we have a
  567. * pre-handler and it returned non-zero, it prepped
  568. * for calling the break_handler below on re-entry
  569. * for jprobe processing, so get out doing nothing
  570. * more here.
  571. */
  572. if (!p->pre_handler || !p->pre_handler(p, regs))
  573. setup_singlestep(p, regs, kcb, 0);
  574. return 1;
  575. }
  576. } else if (*addr != BREAKPOINT_INSTRUCTION) {
  577. /*
  578. * The breakpoint instruction was removed right
  579. * after we hit it. Another cpu has removed
  580. * either a probepoint or a debugger breakpoint
  581. * at this address. In either case, no further
  582. * handling of this interrupt is appropriate.
  583. * Back up over the (now missing) int3 and run
  584. * the original instruction.
  585. */
  586. regs->ip = (unsigned long)addr;
  587. preempt_enable_no_resched();
  588. return 1;
  589. } else if (kprobe_running()) {
  590. p = __this_cpu_read(current_kprobe);
  591. if (p->break_handler && p->break_handler(p, regs)) {
  592. if (!skip_singlestep(p, regs, kcb))
  593. setup_singlestep(p, regs, kcb, 0);
  594. return 1;
  595. }
  596. } /* else: not a kprobe fault; let the kernel handle it */
  597. preempt_enable_no_resched();
  598. return 0;
  599. }
  600. NOKPROBE_SYMBOL(kprobe_int3_handler);
  601. /*
  602. * When a retprobed function returns, this code saves registers and
  603. * calls trampoline_handler() runs, which calls the kretprobe's handler.
  604. */
  605. asm(
  606. ".global kretprobe_trampoline\n"
  607. ".type kretprobe_trampoline, @function\n"
  608. "kretprobe_trampoline:\n"
  609. #ifdef CONFIG_X86_64
  610. /* We don't bother saving the ss register */
  611. " pushq %rsp\n"
  612. " pushfq\n"
  613. SAVE_REGS_STRING
  614. " movq %rsp, %rdi\n"
  615. " call trampoline_handler\n"
  616. /* Replace saved sp with true return address. */
  617. " movq %rax, 152(%rsp)\n"
  618. RESTORE_REGS_STRING
  619. " popfq\n"
  620. #else
  621. " pushf\n"
  622. SAVE_REGS_STRING
  623. " movl %esp, %eax\n"
  624. " call trampoline_handler\n"
  625. /* Move flags to cs */
  626. " movl 56(%esp), %edx\n"
  627. " movl %edx, 52(%esp)\n"
  628. /* Replace saved flags with true return address. */
  629. " movl %eax, 56(%esp)\n"
  630. RESTORE_REGS_STRING
  631. " popf\n"
  632. #endif
  633. " ret\n"
  634. ".size kretprobe_trampoline, .-kretprobe_trampoline\n"
  635. );
  636. NOKPROBE_SYMBOL(kretprobe_trampoline);
  637. STACK_FRAME_NON_STANDARD(kretprobe_trampoline);
  638. /*
  639. * Called from kretprobe_trampoline
  640. */
  641. __visible __used void *trampoline_handler(struct pt_regs *regs)
  642. {
  643. struct kretprobe_instance *ri = NULL;
  644. struct hlist_head *head, empty_rp;
  645. struct hlist_node *tmp;
  646. unsigned long flags, orig_ret_address = 0;
  647. unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
  648. kprobe_opcode_t *correct_ret_addr = NULL;
  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, tmp, head, hlist) {
  674. if (ri->task != current)
  675. /* another task is sharing our hash bucket */
  676. continue;
  677. orig_ret_address = (unsigned long)ri->ret_addr;
  678. if (orig_ret_address != trampoline_address)
  679. /*
  680. * This is the real return address. Any other
  681. * instances associated with this task are for
  682. * other calls deeper on the call stack
  683. */
  684. break;
  685. }
  686. kretprobe_assert(ri, orig_ret_address, trampoline_address);
  687. correct_ret_addr = ri->ret_addr;
  688. hlist_for_each_entry_safe(ri, tmp, head, hlist) {
  689. if (ri->task != current)
  690. /* another task is sharing our hash bucket */
  691. continue;
  692. orig_ret_address = (unsigned long)ri->ret_addr;
  693. if (ri->rp && ri->rp->handler) {
  694. __this_cpu_write(current_kprobe, &ri->rp->kp);
  695. get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
  696. ri->ret_addr = correct_ret_addr;
  697. ri->rp->handler(ri, regs);
  698. __this_cpu_write(current_kprobe, NULL);
  699. }
  700. recycle_rp_inst(ri, &empty_rp);
  701. if (orig_ret_address != trampoline_address)
  702. /*
  703. * This is the real return address. Any other
  704. * instances associated with this task are for
  705. * other calls deeper on the call stack
  706. */
  707. break;
  708. }
  709. kretprobe_hash_unlock(current, &flags);
  710. hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
  711. hlist_del(&ri->hlist);
  712. kfree(ri);
  713. }
  714. return (void *)orig_ret_address;
  715. }
  716. NOKPROBE_SYMBOL(trampoline_handler);
  717. /*
  718. * Called after single-stepping. p->addr is the address of the
  719. * instruction whose first byte has been replaced by the "int 3"
  720. * instruction. To avoid the SMP problems that can occur when we
  721. * temporarily put back the original opcode to single-step, we
  722. * single-stepped a copy of the instruction. The address of this
  723. * copy is p->ainsn.insn.
  724. *
  725. * This function prepares to return from the post-single-step
  726. * interrupt. We have to fix up the stack as follows:
  727. *
  728. * 0) Except in the case of absolute or indirect jump or call instructions,
  729. * the new ip is relative to the copied instruction. We need to make
  730. * it relative to the original instruction.
  731. *
  732. * 1) If the single-stepped instruction was pushfl, then the TF and IF
  733. * flags are set in the just-pushed flags, and may need to be cleared.
  734. *
  735. * 2) If the single-stepped instruction was a call, the return address
  736. * that is atop the stack is the address following the copied instruction.
  737. * We need to make it the address following the original instruction.
  738. *
  739. * If this is the first time we've single-stepped the instruction at
  740. * this probepoint, and the instruction is boostable, boost it: add a
  741. * jump instruction after the copied instruction, that jumps to the next
  742. * instruction after the probepoint.
  743. */
  744. static void resume_execution(struct kprobe *p, struct pt_regs *regs,
  745. struct kprobe_ctlblk *kcb)
  746. {
  747. unsigned long *tos = stack_addr(regs);
  748. unsigned long copy_ip = (unsigned long)p->ainsn.insn;
  749. unsigned long orig_ip = (unsigned long)p->addr;
  750. kprobe_opcode_t *insn = p->ainsn.insn;
  751. /* Skip prefixes */
  752. insn = skip_prefixes(insn);
  753. regs->flags &= ~X86_EFLAGS_TF;
  754. switch (*insn) {
  755. case 0x9c: /* pushfl */
  756. *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
  757. *tos |= kcb->kprobe_old_flags;
  758. break;
  759. case 0xc2: /* iret/ret/lret */
  760. case 0xc3:
  761. case 0xca:
  762. case 0xcb:
  763. case 0xcf:
  764. case 0xea: /* jmp absolute -- ip is correct */
  765. /* ip is already adjusted, no more changes required */
  766. p->ainsn.boostable = 1;
  767. goto no_change;
  768. case 0xe8: /* call relative - Fix return addr */
  769. *tos = orig_ip + (*tos - copy_ip);
  770. break;
  771. #ifdef CONFIG_X86_32
  772. case 0x9a: /* call absolute -- same as call absolute, indirect */
  773. *tos = orig_ip + (*tos - copy_ip);
  774. goto no_change;
  775. #endif
  776. case 0xff:
  777. if ((insn[1] & 0x30) == 0x10) {
  778. /*
  779. * call absolute, indirect
  780. * Fix return addr; ip is correct.
  781. * But this is not boostable
  782. */
  783. *tos = orig_ip + (*tos - copy_ip);
  784. goto no_change;
  785. } else if (((insn[1] & 0x31) == 0x20) ||
  786. ((insn[1] & 0x31) == 0x21)) {
  787. /*
  788. * jmp near and far, absolute indirect
  789. * ip is correct. And this is boostable
  790. */
  791. p->ainsn.boostable = 1;
  792. goto no_change;
  793. }
  794. default:
  795. break;
  796. }
  797. if (p->ainsn.boostable == 0) {
  798. if ((regs->ip > copy_ip) &&
  799. (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
  800. /*
  801. * These instructions can be executed directly if it
  802. * jumps back to correct address.
  803. */
  804. synthesize_reljump((void *)regs->ip,
  805. (void *)orig_ip + (regs->ip - copy_ip));
  806. p->ainsn.boostable = 1;
  807. } else {
  808. p->ainsn.boostable = -1;
  809. }
  810. }
  811. regs->ip += orig_ip - copy_ip;
  812. no_change:
  813. restore_btf();
  814. }
  815. NOKPROBE_SYMBOL(resume_execution);
  816. /*
  817. * Interrupts are disabled on entry as trap1 is an interrupt gate and they
  818. * remain disabled throughout this function.
  819. */
  820. int kprobe_debug_handler(struct pt_regs *regs)
  821. {
  822. struct kprobe *cur = kprobe_running();
  823. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  824. if (!cur)
  825. return 0;
  826. resume_execution(cur, regs, kcb);
  827. regs->flags |= kcb->kprobe_saved_flags;
  828. if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
  829. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  830. cur->post_handler(cur, regs, 0);
  831. }
  832. /* Restore back the original saved kprobes variables and continue. */
  833. if (kcb->kprobe_status == KPROBE_REENTER) {
  834. restore_previous_kprobe(kcb);
  835. goto out;
  836. }
  837. reset_current_kprobe();
  838. out:
  839. preempt_enable_no_resched();
  840. /*
  841. * if somebody else is singlestepping across a probe point, flags
  842. * will have TF set, in which case, continue the remaining processing
  843. * of do_debug, as if this is not a probe hit.
  844. */
  845. if (regs->flags & X86_EFLAGS_TF)
  846. return 0;
  847. return 1;
  848. }
  849. NOKPROBE_SYMBOL(kprobe_debug_handler);
  850. int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
  851. {
  852. struct kprobe *cur = kprobe_running();
  853. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  854. if (unlikely(regs->ip == (unsigned long)cur->ainsn.insn)) {
  855. /* This must happen on single-stepping */
  856. WARN_ON(kcb->kprobe_status != KPROBE_HIT_SS &&
  857. kcb->kprobe_status != KPROBE_REENTER);
  858. /*
  859. * We are here because the instruction being single
  860. * stepped caused a page fault. We reset the current
  861. * kprobe and the ip points back to the probe address
  862. * and allow the page fault handler to continue as a
  863. * normal page fault.
  864. */
  865. regs->ip = (unsigned long)cur->addr;
  866. /*
  867. * Trap flag (TF) has been set here because this fault
  868. * happened where the single stepping will be done.
  869. * So clear it by resetting the current kprobe:
  870. */
  871. regs->flags &= ~X86_EFLAGS_TF;
  872. /*
  873. * If the TF flag was set before the kprobe hit,
  874. * don't touch it:
  875. */
  876. regs->flags |= kcb->kprobe_old_flags;
  877. if (kcb->kprobe_status == KPROBE_REENTER)
  878. restore_previous_kprobe(kcb);
  879. else
  880. reset_current_kprobe();
  881. preempt_enable_no_resched();
  882. } else if (kcb->kprobe_status == KPROBE_HIT_ACTIVE ||
  883. kcb->kprobe_status == KPROBE_HIT_SSDONE) {
  884. /*
  885. * We increment the nmissed count for accounting,
  886. * we can also use npre/npostfault count for accounting
  887. * these specific fault cases.
  888. */
  889. kprobes_inc_nmissed_count(cur);
  890. /*
  891. * We come here because instructions in the pre/post
  892. * handler caused the page_fault, this could happen
  893. * if handler tries to access user space by
  894. * copy_from_user(), get_user() etc. Let the
  895. * user-specified handler try to fix it first.
  896. */
  897. if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
  898. return 1;
  899. /*
  900. * In case the user-specified fault handler returned
  901. * zero, try to fix up.
  902. */
  903. if (fixup_exception(regs, trapnr))
  904. return 1;
  905. /*
  906. * fixup routine could not handle it,
  907. * Let do_page_fault() fix it.
  908. */
  909. }
  910. return 0;
  911. }
  912. NOKPROBE_SYMBOL(kprobe_fault_handler);
  913. /*
  914. * Wrapper routine for handling exceptions.
  915. */
  916. int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
  917. void *data)
  918. {
  919. struct die_args *args = data;
  920. int ret = NOTIFY_DONE;
  921. if (args->regs && user_mode(args->regs))
  922. return ret;
  923. if (val == DIE_GPF) {
  924. /*
  925. * To be potentially processing a kprobe fault and to
  926. * trust the result from kprobe_running(), we have
  927. * be non-preemptible.
  928. */
  929. if (!preemptible() && kprobe_running() &&
  930. kprobe_fault_handler(args->regs, args->trapnr))
  931. ret = NOTIFY_STOP;
  932. }
  933. return ret;
  934. }
  935. NOKPROBE_SYMBOL(kprobe_exceptions_notify);
  936. int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
  937. {
  938. struct jprobe *jp = container_of(p, struct jprobe, kp);
  939. unsigned long addr;
  940. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  941. kcb->jprobe_saved_regs = *regs;
  942. kcb->jprobe_saved_sp = stack_addr(regs);
  943. addr = (unsigned long)(kcb->jprobe_saved_sp);
  944. /*
  945. * As Linus pointed out, gcc assumes that the callee
  946. * owns the argument space and could overwrite it, e.g.
  947. * tailcall optimization. So, to be absolutely safe
  948. * we also save and restore enough stack bytes to cover
  949. * the argument area.
  950. */
  951. memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
  952. MIN_STACK_SIZE(addr));
  953. regs->flags &= ~X86_EFLAGS_IF;
  954. trace_hardirqs_off();
  955. regs->ip = (unsigned long)(jp->entry);
  956. /*
  957. * jprobes use jprobe_return() which skips the normal return
  958. * path of the function, and this messes up the accounting of the
  959. * function graph tracer to get messed up.
  960. *
  961. * Pause function graph tracing while performing the jprobe function.
  962. */
  963. pause_graph_tracing();
  964. return 1;
  965. }
  966. NOKPROBE_SYMBOL(setjmp_pre_handler);
  967. void jprobe_return(void)
  968. {
  969. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  970. asm volatile (
  971. #ifdef CONFIG_X86_64
  972. " xchg %%rbx,%%rsp \n"
  973. #else
  974. " xchgl %%ebx,%%esp \n"
  975. #endif
  976. " int3 \n"
  977. " .globl jprobe_return_end\n"
  978. " jprobe_return_end: \n"
  979. " nop \n"::"b"
  980. (kcb->jprobe_saved_sp):"memory");
  981. }
  982. NOKPROBE_SYMBOL(jprobe_return);
  983. NOKPROBE_SYMBOL(jprobe_return_end);
  984. int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
  985. {
  986. struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
  987. u8 *addr = (u8 *) (regs->ip - 1);
  988. struct jprobe *jp = container_of(p, struct jprobe, kp);
  989. void *saved_sp = kcb->jprobe_saved_sp;
  990. if ((addr > (u8 *) jprobe_return) &&
  991. (addr < (u8 *) jprobe_return_end)) {
  992. if (stack_addr(regs) != saved_sp) {
  993. struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
  994. printk(KERN_ERR
  995. "current sp %p does not match saved sp %p\n",
  996. stack_addr(regs), saved_sp);
  997. printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
  998. show_regs(saved_regs);
  999. printk(KERN_ERR "Current registers\n");
  1000. show_regs(regs);
  1001. BUG();
  1002. }
  1003. /* It's OK to start function graph tracing again */
  1004. unpause_graph_tracing();
  1005. *regs = kcb->jprobe_saved_regs;
  1006. memcpy(saved_sp, kcb->jprobes_stack, MIN_STACK_SIZE(saved_sp));
  1007. preempt_enable_no_resched();
  1008. return 1;
  1009. }
  1010. return 0;
  1011. }
  1012. NOKPROBE_SYMBOL(longjmp_break_handler);
  1013. bool arch_within_kprobe_blacklist(unsigned long addr)
  1014. {
  1015. return (addr >= (unsigned long)__kprobes_text_start &&
  1016. addr < (unsigned long)__kprobes_text_end) ||
  1017. (addr >= (unsigned long)__entry_text_start &&
  1018. addr < (unsigned long)__entry_text_end);
  1019. }
  1020. int __init arch_init_kprobes(void)
  1021. {
  1022. return 0;
  1023. }
  1024. int arch_trampoline_kprobe(struct kprobe *p)
  1025. {
  1026. return 0;
  1027. }