ftrace.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. /*
  2. * Dynamic function tracing support.
  3. *
  4. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  5. *
  6. * Thanks goes to Ingo Molnar, for suggesting the idea.
  7. * Mathieu Desnoyers, for suggesting postponing the modifications.
  8. * Arjan van de Ven, for keeping me straight, and explaining to me
  9. * the dangers of modifying code on the run.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/spinlock.h>
  13. #include <linux/hardirq.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/ftrace.h>
  16. #include <linux/percpu.h>
  17. #include <linux/sched.h>
  18. #include <linux/slab.h>
  19. #include <linux/init.h>
  20. #include <linux/list.h>
  21. #include <linux/module.h>
  22. #include <trace/syscall.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/kprobes.h>
  25. #include <asm/ftrace.h>
  26. #include <asm/nops.h>
  27. #if defined(CONFIG_FUNCTION_GRAPH_TRACER) && \
  28. !defined(CC_USING_FENTRY) && \
  29. !defined(CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE)
  30. # error The following combination is not supported: ((compiler missing -mfentry) || (CONFIG_X86_32 and !CONFIG_DYNAMIC_FTRACE)) && CONFIG_FUNCTION_GRAPH_TRACER && CONFIG_CC_OPTIMIZE_FOR_SIZE
  31. #endif
  32. #ifdef CONFIG_DYNAMIC_FTRACE
  33. int ftrace_arch_code_modify_prepare(void)
  34. {
  35. set_kernel_text_rw();
  36. set_all_modules_text_rw();
  37. return 0;
  38. }
  39. int ftrace_arch_code_modify_post_process(void)
  40. {
  41. set_all_modules_text_ro();
  42. set_kernel_text_ro();
  43. return 0;
  44. }
  45. union ftrace_code_union {
  46. char code[MCOUNT_INSN_SIZE];
  47. struct {
  48. unsigned char e8;
  49. int offset;
  50. } __attribute__((packed));
  51. };
  52. static int ftrace_calc_offset(long ip, long addr)
  53. {
  54. return (int)(addr - ip);
  55. }
  56. static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
  57. {
  58. static union ftrace_code_union calc;
  59. calc.e8 = 0xe8;
  60. calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
  61. /*
  62. * No locking needed, this must be called via kstop_machine
  63. * which in essence is like running on a uniprocessor machine.
  64. */
  65. return calc.code;
  66. }
  67. static inline int
  68. within(unsigned long addr, unsigned long start, unsigned long end)
  69. {
  70. return addr >= start && addr < end;
  71. }
  72. static unsigned long text_ip_addr(unsigned long ip)
  73. {
  74. /*
  75. * On x86_64, kernel text mappings are mapped read-only, so we use
  76. * the kernel identity mapping instead of the kernel text mapping
  77. * to modify the kernel text.
  78. *
  79. * For 32bit kernels, these mappings are same and we can use
  80. * kernel identity mapping to modify code.
  81. */
  82. if (within(ip, (unsigned long)_text, (unsigned long)_etext))
  83. ip = (unsigned long)__va(__pa_symbol(ip));
  84. return ip;
  85. }
  86. static const unsigned char *ftrace_nop_replace(void)
  87. {
  88. return ideal_nops[NOP_ATOMIC5];
  89. }
  90. static int
  91. ftrace_modify_code_direct(unsigned long ip, unsigned const char *old_code,
  92. unsigned const char *new_code)
  93. {
  94. unsigned char replaced[MCOUNT_INSN_SIZE];
  95. ftrace_expected = old_code;
  96. /*
  97. * Note:
  98. * We are paranoid about modifying text, as if a bug was to happen, it
  99. * could cause us to read or write to someplace that could cause harm.
  100. * Carefully read and modify the code with probe_kernel_*(), and make
  101. * sure what we read is what we expected it to be before modifying it.
  102. */
  103. /* read the text we want to modify */
  104. if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
  105. return -EFAULT;
  106. /* Make sure it is what we expect it to be */
  107. if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
  108. return -EINVAL;
  109. ip = text_ip_addr(ip);
  110. /* replace the text with the new text */
  111. if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE))
  112. return -EPERM;
  113. sync_core();
  114. return 0;
  115. }
  116. int ftrace_make_nop(struct module *mod,
  117. struct dyn_ftrace *rec, unsigned long addr)
  118. {
  119. unsigned const char *new, *old;
  120. unsigned long ip = rec->ip;
  121. old = ftrace_call_replace(ip, addr);
  122. new = ftrace_nop_replace();
  123. /*
  124. * On boot up, and when modules are loaded, the MCOUNT_ADDR
  125. * is converted to a nop, and will never become MCOUNT_ADDR
  126. * again. This code is either running before SMP (on boot up)
  127. * or before the code will ever be executed (module load).
  128. * We do not want to use the breakpoint version in this case,
  129. * just modify the code directly.
  130. */
  131. if (addr == MCOUNT_ADDR)
  132. return ftrace_modify_code_direct(rec->ip, old, new);
  133. ftrace_expected = NULL;
  134. /* Normal cases use add_brk_on_nop */
  135. WARN_ONCE(1, "invalid use of ftrace_make_nop");
  136. return -EINVAL;
  137. }
  138. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  139. {
  140. unsigned const char *new, *old;
  141. unsigned long ip = rec->ip;
  142. old = ftrace_nop_replace();
  143. new = ftrace_call_replace(ip, addr);
  144. /* Should only be called when module is loaded */
  145. return ftrace_modify_code_direct(rec->ip, old, new);
  146. }
  147. /*
  148. * The modifying_ftrace_code is used to tell the breakpoint
  149. * handler to call ftrace_int3_handler(). If it fails to
  150. * call this handler for a breakpoint added by ftrace, then
  151. * the kernel may crash.
  152. *
  153. * As atomic_writes on x86 do not need a barrier, we do not
  154. * need to add smp_mb()s for this to work. It is also considered
  155. * that we can not read the modifying_ftrace_code before
  156. * executing the breakpoint. That would be quite remarkable if
  157. * it could do that. Here's the flow that is required:
  158. *
  159. * CPU-0 CPU-1
  160. *
  161. * atomic_inc(mfc);
  162. * write int3s
  163. * <trap-int3> // implicit (r)mb
  164. * if (atomic_read(mfc))
  165. * call ftrace_int3_handler()
  166. *
  167. * Then when we are finished:
  168. *
  169. * atomic_dec(mfc);
  170. *
  171. * If we hit a breakpoint that was not set by ftrace, it does not
  172. * matter if ftrace_int3_handler() is called or not. It will
  173. * simply be ignored. But it is crucial that a ftrace nop/caller
  174. * breakpoint is handled. No other user should ever place a
  175. * breakpoint on an ftrace nop/caller location. It must only
  176. * be done by this code.
  177. */
  178. atomic_t modifying_ftrace_code __read_mostly;
  179. static int
  180. ftrace_modify_code(unsigned long ip, unsigned const char *old_code,
  181. unsigned const char *new_code);
  182. /*
  183. * Should never be called:
  184. * As it is only called by __ftrace_replace_code() which is called by
  185. * ftrace_replace_code() that x86 overrides, and by ftrace_update_code()
  186. * which is called to turn mcount into nops or nops into function calls
  187. * but not to convert a function from not using regs to one that uses
  188. * regs, which ftrace_modify_call() is for.
  189. */
  190. int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
  191. unsigned long addr)
  192. {
  193. WARN_ON(1);
  194. ftrace_expected = NULL;
  195. return -EINVAL;
  196. }
  197. static unsigned long ftrace_update_func;
  198. static int update_ftrace_func(unsigned long ip, void *new)
  199. {
  200. unsigned char old[MCOUNT_INSN_SIZE];
  201. int ret;
  202. memcpy(old, (void *)ip, MCOUNT_INSN_SIZE);
  203. ftrace_update_func = ip;
  204. /* Make sure the breakpoints see the ftrace_update_func update */
  205. smp_wmb();
  206. /* See comment above by declaration of modifying_ftrace_code */
  207. atomic_inc(&modifying_ftrace_code);
  208. ret = ftrace_modify_code(ip, old, new);
  209. atomic_dec(&modifying_ftrace_code);
  210. return ret;
  211. }
  212. int ftrace_update_ftrace_func(ftrace_func_t func)
  213. {
  214. unsigned long ip = (unsigned long)(&ftrace_call);
  215. unsigned char *new;
  216. int ret;
  217. new = ftrace_call_replace(ip, (unsigned long)func);
  218. ret = update_ftrace_func(ip, new);
  219. /* Also update the regs callback function */
  220. if (!ret) {
  221. ip = (unsigned long)(&ftrace_regs_call);
  222. new = ftrace_call_replace(ip, (unsigned long)func);
  223. ret = update_ftrace_func(ip, new);
  224. }
  225. return ret;
  226. }
  227. static int is_ftrace_caller(unsigned long ip)
  228. {
  229. if (ip == ftrace_update_func)
  230. return 1;
  231. return 0;
  232. }
  233. /*
  234. * A breakpoint was added to the code address we are about to
  235. * modify, and this is the handle that will just skip over it.
  236. * We are either changing a nop into a trace call, or a trace
  237. * call to a nop. While the change is taking place, we treat
  238. * it just like it was a nop.
  239. */
  240. int ftrace_int3_handler(struct pt_regs *regs)
  241. {
  242. unsigned long ip;
  243. if (WARN_ON_ONCE(!regs))
  244. return 0;
  245. ip = regs->ip - 1;
  246. if (!ftrace_location(ip) && !is_ftrace_caller(ip))
  247. return 0;
  248. regs->ip += MCOUNT_INSN_SIZE - 1;
  249. return 1;
  250. }
  251. static int ftrace_write(unsigned long ip, const char *val, int size)
  252. {
  253. ip = text_ip_addr(ip);
  254. if (probe_kernel_write((void *)ip, val, size))
  255. return -EPERM;
  256. return 0;
  257. }
  258. static int add_break(unsigned long ip, const char *old)
  259. {
  260. unsigned char replaced[MCOUNT_INSN_SIZE];
  261. unsigned char brk = BREAKPOINT_INSTRUCTION;
  262. if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
  263. return -EFAULT;
  264. ftrace_expected = old;
  265. /* Make sure it is what we expect it to be */
  266. if (memcmp(replaced, old, MCOUNT_INSN_SIZE) != 0)
  267. return -EINVAL;
  268. return ftrace_write(ip, &brk, 1);
  269. }
  270. static int add_brk_on_call(struct dyn_ftrace *rec, unsigned long addr)
  271. {
  272. unsigned const char *old;
  273. unsigned long ip = rec->ip;
  274. old = ftrace_call_replace(ip, addr);
  275. return add_break(rec->ip, old);
  276. }
  277. static int add_brk_on_nop(struct dyn_ftrace *rec)
  278. {
  279. unsigned const char *old;
  280. old = ftrace_nop_replace();
  281. return add_break(rec->ip, old);
  282. }
  283. static int add_breakpoints(struct dyn_ftrace *rec, int enable)
  284. {
  285. unsigned long ftrace_addr;
  286. int ret;
  287. ftrace_addr = ftrace_get_addr_curr(rec);
  288. ret = ftrace_test_record(rec, enable);
  289. switch (ret) {
  290. case FTRACE_UPDATE_IGNORE:
  291. return 0;
  292. case FTRACE_UPDATE_MAKE_CALL:
  293. /* converting nop to call */
  294. return add_brk_on_nop(rec);
  295. case FTRACE_UPDATE_MODIFY_CALL:
  296. case FTRACE_UPDATE_MAKE_NOP:
  297. /* converting a call to a nop */
  298. return add_brk_on_call(rec, ftrace_addr);
  299. }
  300. return 0;
  301. }
  302. /*
  303. * On error, we need to remove breakpoints. This needs to
  304. * be done caefully. If the address does not currently have a
  305. * breakpoint, we know we are done. Otherwise, we look at the
  306. * remaining 4 bytes of the instruction. If it matches a nop
  307. * we replace the breakpoint with the nop. Otherwise we replace
  308. * it with the call instruction.
  309. */
  310. static int remove_breakpoint(struct dyn_ftrace *rec)
  311. {
  312. unsigned char ins[MCOUNT_INSN_SIZE];
  313. unsigned char brk = BREAKPOINT_INSTRUCTION;
  314. const unsigned char *nop;
  315. unsigned long ftrace_addr;
  316. unsigned long ip = rec->ip;
  317. /* If we fail the read, just give up */
  318. if (probe_kernel_read(ins, (void *)ip, MCOUNT_INSN_SIZE))
  319. return -EFAULT;
  320. /* If this does not have a breakpoint, we are done */
  321. if (ins[0] != brk)
  322. return 0;
  323. nop = ftrace_nop_replace();
  324. /*
  325. * If the last 4 bytes of the instruction do not match
  326. * a nop, then we assume that this is a call to ftrace_addr.
  327. */
  328. if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0) {
  329. /*
  330. * For extra paranoidism, we check if the breakpoint is on
  331. * a call that would actually jump to the ftrace_addr.
  332. * If not, don't touch the breakpoint, we make just create
  333. * a disaster.
  334. */
  335. ftrace_addr = ftrace_get_addr_new(rec);
  336. nop = ftrace_call_replace(ip, ftrace_addr);
  337. if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) == 0)
  338. goto update;
  339. /* Check both ftrace_addr and ftrace_old_addr */
  340. ftrace_addr = ftrace_get_addr_curr(rec);
  341. nop = ftrace_call_replace(ip, ftrace_addr);
  342. ftrace_expected = nop;
  343. if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0)
  344. return -EINVAL;
  345. }
  346. update:
  347. return ftrace_write(ip, nop, 1);
  348. }
  349. static int add_update_code(unsigned long ip, unsigned const char *new)
  350. {
  351. /* skip breakpoint */
  352. ip++;
  353. new++;
  354. return ftrace_write(ip, new, MCOUNT_INSN_SIZE - 1);
  355. }
  356. static int add_update_call(struct dyn_ftrace *rec, unsigned long addr)
  357. {
  358. unsigned long ip = rec->ip;
  359. unsigned const char *new;
  360. new = ftrace_call_replace(ip, addr);
  361. return add_update_code(ip, new);
  362. }
  363. static int add_update_nop(struct dyn_ftrace *rec)
  364. {
  365. unsigned long ip = rec->ip;
  366. unsigned const char *new;
  367. new = ftrace_nop_replace();
  368. return add_update_code(ip, new);
  369. }
  370. static int add_update(struct dyn_ftrace *rec, int enable)
  371. {
  372. unsigned long ftrace_addr;
  373. int ret;
  374. ret = ftrace_test_record(rec, enable);
  375. ftrace_addr = ftrace_get_addr_new(rec);
  376. switch (ret) {
  377. case FTRACE_UPDATE_IGNORE:
  378. return 0;
  379. case FTRACE_UPDATE_MODIFY_CALL:
  380. case FTRACE_UPDATE_MAKE_CALL:
  381. /* converting nop to call */
  382. return add_update_call(rec, ftrace_addr);
  383. case FTRACE_UPDATE_MAKE_NOP:
  384. /* converting a call to a nop */
  385. return add_update_nop(rec);
  386. }
  387. return 0;
  388. }
  389. static int finish_update_call(struct dyn_ftrace *rec, unsigned long addr)
  390. {
  391. unsigned long ip = rec->ip;
  392. unsigned const char *new;
  393. new = ftrace_call_replace(ip, addr);
  394. return ftrace_write(ip, new, 1);
  395. }
  396. static int finish_update_nop(struct dyn_ftrace *rec)
  397. {
  398. unsigned long ip = rec->ip;
  399. unsigned const char *new;
  400. new = ftrace_nop_replace();
  401. return ftrace_write(ip, new, 1);
  402. }
  403. static int finish_update(struct dyn_ftrace *rec, int enable)
  404. {
  405. unsigned long ftrace_addr;
  406. int ret;
  407. ret = ftrace_update_record(rec, enable);
  408. ftrace_addr = ftrace_get_addr_new(rec);
  409. switch (ret) {
  410. case FTRACE_UPDATE_IGNORE:
  411. return 0;
  412. case FTRACE_UPDATE_MODIFY_CALL:
  413. case FTRACE_UPDATE_MAKE_CALL:
  414. /* converting nop to call */
  415. return finish_update_call(rec, ftrace_addr);
  416. case FTRACE_UPDATE_MAKE_NOP:
  417. /* converting a call to a nop */
  418. return finish_update_nop(rec);
  419. }
  420. return 0;
  421. }
  422. static void do_sync_core(void *data)
  423. {
  424. sync_core();
  425. }
  426. static void run_sync(void)
  427. {
  428. int enable_irqs = irqs_disabled();
  429. /* We may be called with interrupts disabled (on bootup). */
  430. if (enable_irqs)
  431. local_irq_enable();
  432. on_each_cpu(do_sync_core, NULL, 1);
  433. if (enable_irqs)
  434. local_irq_disable();
  435. }
  436. void ftrace_replace_code(int enable)
  437. {
  438. struct ftrace_rec_iter *iter;
  439. struct dyn_ftrace *rec;
  440. const char *report = "adding breakpoints";
  441. int count = 0;
  442. int ret;
  443. for_ftrace_rec_iter(iter) {
  444. rec = ftrace_rec_iter_record(iter);
  445. ret = add_breakpoints(rec, enable);
  446. if (ret)
  447. goto remove_breakpoints;
  448. count++;
  449. }
  450. run_sync();
  451. report = "updating code";
  452. count = 0;
  453. for_ftrace_rec_iter(iter) {
  454. rec = ftrace_rec_iter_record(iter);
  455. ret = add_update(rec, enable);
  456. if (ret)
  457. goto remove_breakpoints;
  458. count++;
  459. }
  460. run_sync();
  461. report = "removing breakpoints";
  462. count = 0;
  463. for_ftrace_rec_iter(iter) {
  464. rec = ftrace_rec_iter_record(iter);
  465. ret = finish_update(rec, enable);
  466. if (ret)
  467. goto remove_breakpoints;
  468. count++;
  469. }
  470. run_sync();
  471. return;
  472. remove_breakpoints:
  473. pr_warn("Failed on %s (%d):\n", report, count);
  474. ftrace_bug(ret, rec);
  475. for_ftrace_rec_iter(iter) {
  476. rec = ftrace_rec_iter_record(iter);
  477. /*
  478. * Breakpoints are handled only when this function is in
  479. * progress. The system could not work with them.
  480. */
  481. if (remove_breakpoint(rec))
  482. BUG();
  483. }
  484. run_sync();
  485. }
  486. static int
  487. ftrace_modify_code(unsigned long ip, unsigned const char *old_code,
  488. unsigned const char *new_code)
  489. {
  490. int ret;
  491. ret = add_break(ip, old_code);
  492. if (ret)
  493. goto out;
  494. run_sync();
  495. ret = add_update_code(ip, new_code);
  496. if (ret)
  497. goto fail_update;
  498. run_sync();
  499. ret = ftrace_write(ip, new_code, 1);
  500. /*
  501. * The breakpoint is handled only when this function is in progress.
  502. * The system could not work if we could not remove it.
  503. */
  504. BUG_ON(ret);
  505. out:
  506. run_sync();
  507. return ret;
  508. fail_update:
  509. /* Also here the system could not work with the breakpoint */
  510. if (ftrace_write(ip, old_code, 1))
  511. BUG();
  512. goto out;
  513. }
  514. void arch_ftrace_update_code(int command)
  515. {
  516. /* See comment above by declaration of modifying_ftrace_code */
  517. atomic_inc(&modifying_ftrace_code);
  518. ftrace_modify_all_code(command);
  519. atomic_dec(&modifying_ftrace_code);
  520. }
  521. int __init ftrace_dyn_arch_init(void)
  522. {
  523. return 0;
  524. }
  525. #if defined(CONFIG_X86_64) || defined(CONFIG_FUNCTION_GRAPH_TRACER)
  526. static unsigned char *ftrace_jmp_replace(unsigned long ip, unsigned long addr)
  527. {
  528. static union ftrace_code_union calc;
  529. /* Jmp not a call (ignore the .e8) */
  530. calc.e8 = 0xe9;
  531. calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
  532. /*
  533. * ftrace external locks synchronize the access to the static variable.
  534. */
  535. return calc.code;
  536. }
  537. #endif
  538. /* Currently only x86_64 supports dynamic trampolines */
  539. #ifdef CONFIG_X86_64
  540. #ifdef CONFIG_MODULES
  541. #include <linux/moduleloader.h>
  542. /* Module allocation simplifies allocating memory for code */
  543. static inline void *alloc_tramp(unsigned long size)
  544. {
  545. return module_alloc(size);
  546. }
  547. static inline void tramp_free(void *tramp)
  548. {
  549. module_memfree(tramp);
  550. }
  551. #else
  552. /* Trampolines can only be created if modules are supported */
  553. static inline void *alloc_tramp(unsigned long size)
  554. {
  555. return NULL;
  556. }
  557. static inline void tramp_free(void *tramp) { }
  558. #endif
  559. /* Defined as markers to the end of the ftrace default trampolines */
  560. extern void ftrace_regs_caller_end(void);
  561. extern void ftrace_epilogue(void);
  562. extern void ftrace_caller_op_ptr(void);
  563. extern void ftrace_regs_caller_op_ptr(void);
  564. /* movq function_trace_op(%rip), %rdx */
  565. /* 0x48 0x8b 0x15 <offset-to-ftrace_trace_op (4 bytes)> */
  566. #define OP_REF_SIZE 7
  567. /*
  568. * The ftrace_ops is passed to the function callback. Since the
  569. * trampoline only services a single ftrace_ops, we can pass in
  570. * that ops directly.
  571. *
  572. * The ftrace_op_code_union is used to create a pointer to the
  573. * ftrace_ops that will be passed to the callback function.
  574. */
  575. union ftrace_op_code_union {
  576. char code[OP_REF_SIZE];
  577. struct {
  578. char op[3];
  579. int offset;
  580. } __attribute__((packed));
  581. };
  582. static unsigned long
  583. create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
  584. {
  585. unsigned const char *jmp;
  586. unsigned long start_offset;
  587. unsigned long end_offset;
  588. unsigned long op_offset;
  589. unsigned long offset;
  590. unsigned long size;
  591. unsigned long ip;
  592. unsigned long *ptr;
  593. void *trampoline;
  594. /* 48 8b 15 <offset> is movq <offset>(%rip), %rdx */
  595. unsigned const char op_ref[] = { 0x48, 0x8b, 0x15 };
  596. union ftrace_op_code_union op_ptr;
  597. int ret;
  598. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
  599. start_offset = (unsigned long)ftrace_regs_caller;
  600. end_offset = (unsigned long)ftrace_regs_caller_end;
  601. op_offset = (unsigned long)ftrace_regs_caller_op_ptr;
  602. } else {
  603. start_offset = (unsigned long)ftrace_caller;
  604. end_offset = (unsigned long)ftrace_epilogue;
  605. op_offset = (unsigned long)ftrace_caller_op_ptr;
  606. }
  607. size = end_offset - start_offset;
  608. /*
  609. * Allocate enough size to store the ftrace_caller code,
  610. * the jmp to ftrace_epilogue, as well as the address of
  611. * the ftrace_ops this trampoline is used for.
  612. */
  613. trampoline = alloc_tramp(size + MCOUNT_INSN_SIZE + sizeof(void *));
  614. if (!trampoline)
  615. return 0;
  616. *tramp_size = size + MCOUNT_INSN_SIZE + sizeof(void *);
  617. /* Copy ftrace_caller onto the trampoline memory */
  618. ret = probe_kernel_read(trampoline, (void *)start_offset, size);
  619. if (WARN_ON(ret < 0)) {
  620. tramp_free(trampoline);
  621. return 0;
  622. }
  623. ip = (unsigned long)trampoline + size;
  624. /* The trampoline ends with a jmp to ftrace_epilogue */
  625. jmp = ftrace_jmp_replace(ip, (unsigned long)ftrace_epilogue);
  626. memcpy(trampoline + size, jmp, MCOUNT_INSN_SIZE);
  627. /*
  628. * The address of the ftrace_ops that is used for this trampoline
  629. * is stored at the end of the trampoline. This will be used to
  630. * load the third parameter for the callback. Basically, that
  631. * location at the end of the trampoline takes the place of
  632. * the global function_trace_op variable.
  633. */
  634. ptr = (unsigned long *)(trampoline + size + MCOUNT_INSN_SIZE);
  635. *ptr = (unsigned long)ops;
  636. op_offset -= start_offset;
  637. memcpy(&op_ptr, trampoline + op_offset, OP_REF_SIZE);
  638. /* Are we pointing to the reference? */
  639. if (WARN_ON(memcmp(op_ptr.op, op_ref, 3) != 0)) {
  640. tramp_free(trampoline);
  641. return 0;
  642. }
  643. /* Load the contents of ptr into the callback parameter */
  644. offset = (unsigned long)ptr;
  645. offset -= (unsigned long)trampoline + op_offset + OP_REF_SIZE;
  646. op_ptr.offset = offset;
  647. /* put in the new offset to the ftrace_ops */
  648. memcpy(trampoline + op_offset, &op_ptr, OP_REF_SIZE);
  649. /* ALLOC_TRAMP flags lets us know we created it */
  650. ops->flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
  651. return (unsigned long)trampoline;
  652. }
  653. static unsigned long calc_trampoline_call_offset(bool save_regs)
  654. {
  655. unsigned long start_offset;
  656. unsigned long call_offset;
  657. if (save_regs) {
  658. start_offset = (unsigned long)ftrace_regs_caller;
  659. call_offset = (unsigned long)ftrace_regs_call;
  660. } else {
  661. start_offset = (unsigned long)ftrace_caller;
  662. call_offset = (unsigned long)ftrace_call;
  663. }
  664. return call_offset - start_offset;
  665. }
  666. void arch_ftrace_update_trampoline(struct ftrace_ops *ops)
  667. {
  668. ftrace_func_t func;
  669. unsigned char *new;
  670. unsigned long offset;
  671. unsigned long ip;
  672. unsigned int size;
  673. int ret;
  674. if (ops->trampoline) {
  675. /*
  676. * The ftrace_ops caller may set up its own trampoline.
  677. * In such a case, this code must not modify it.
  678. */
  679. if (!(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
  680. return;
  681. } else {
  682. ops->trampoline = create_trampoline(ops, &size);
  683. if (!ops->trampoline)
  684. return;
  685. ops->trampoline_size = size;
  686. }
  687. offset = calc_trampoline_call_offset(ops->flags & FTRACE_OPS_FL_SAVE_REGS);
  688. ip = ops->trampoline + offset;
  689. func = ftrace_ops_get_func(ops);
  690. /* Do a safe modify in case the trampoline is executing */
  691. new = ftrace_call_replace(ip, (unsigned long)func);
  692. ret = update_ftrace_func(ip, new);
  693. /* The update should never fail */
  694. WARN_ON(ret);
  695. }
  696. /* Return the address of the function the trampoline calls */
  697. static void *addr_from_call(void *ptr)
  698. {
  699. union ftrace_code_union calc;
  700. int ret;
  701. ret = probe_kernel_read(&calc, ptr, MCOUNT_INSN_SIZE);
  702. if (WARN_ON_ONCE(ret < 0))
  703. return NULL;
  704. /* Make sure this is a call */
  705. if (WARN_ON_ONCE(calc.e8 != 0xe8)) {
  706. pr_warn("Expected e8, got %x\n", calc.e8);
  707. return NULL;
  708. }
  709. return ptr + MCOUNT_INSN_SIZE + calc.offset;
  710. }
  711. void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent,
  712. unsigned long frame_pointer);
  713. /*
  714. * If the ops->trampoline was not allocated, then it probably
  715. * has a static trampoline func, or is the ftrace caller itself.
  716. */
  717. static void *static_tramp_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
  718. {
  719. unsigned long offset;
  720. bool save_regs = rec->flags & FTRACE_FL_REGS_EN;
  721. void *ptr;
  722. if (ops && ops->trampoline) {
  723. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  724. /*
  725. * We only know about function graph tracer setting as static
  726. * trampoline.
  727. */
  728. if (ops->trampoline == FTRACE_GRAPH_ADDR)
  729. return (void *)prepare_ftrace_return;
  730. #endif
  731. return NULL;
  732. }
  733. offset = calc_trampoline_call_offset(save_regs);
  734. if (save_regs)
  735. ptr = (void *)FTRACE_REGS_ADDR + offset;
  736. else
  737. ptr = (void *)FTRACE_ADDR + offset;
  738. return addr_from_call(ptr);
  739. }
  740. void *arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
  741. {
  742. unsigned long offset;
  743. /* If we didn't allocate this trampoline, consider it static */
  744. if (!ops || !(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
  745. return static_tramp_func(ops, rec);
  746. offset = calc_trampoline_call_offset(ops->flags & FTRACE_OPS_FL_SAVE_REGS);
  747. return addr_from_call((void *)ops->trampoline + offset);
  748. }
  749. void arch_ftrace_trampoline_free(struct ftrace_ops *ops)
  750. {
  751. if (!ops || !(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
  752. return;
  753. tramp_free((void *)ops->trampoline);
  754. ops->trampoline = 0;
  755. }
  756. #endif /* CONFIG_X86_64 */
  757. #endif /* CONFIG_DYNAMIC_FTRACE */
  758. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  759. #ifdef CONFIG_DYNAMIC_FTRACE
  760. extern void ftrace_graph_call(void);
  761. static int ftrace_mod_jmp(unsigned long ip, void *func)
  762. {
  763. unsigned char *new;
  764. new = ftrace_jmp_replace(ip, (unsigned long)func);
  765. return update_ftrace_func(ip, new);
  766. }
  767. int ftrace_enable_ftrace_graph_caller(void)
  768. {
  769. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  770. return ftrace_mod_jmp(ip, &ftrace_graph_caller);
  771. }
  772. int ftrace_disable_ftrace_graph_caller(void)
  773. {
  774. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  775. return ftrace_mod_jmp(ip, &ftrace_stub);
  776. }
  777. #endif /* !CONFIG_DYNAMIC_FTRACE */
  778. /*
  779. * Hook the return address and push it in the stack of return addrs
  780. * in current thread info.
  781. */
  782. void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent,
  783. unsigned long frame_pointer)
  784. {
  785. unsigned long old;
  786. int faulted;
  787. struct ftrace_graph_ent trace;
  788. unsigned long return_hooker = (unsigned long)
  789. &return_to_handler;
  790. if (unlikely(ftrace_graph_is_dead()))
  791. return;
  792. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  793. return;
  794. /*
  795. * Protect against fault, even if it shouldn't
  796. * happen. This tool is too much intrusive to
  797. * ignore such a protection.
  798. */
  799. asm volatile(
  800. "1: " _ASM_MOV " (%[parent]), %[old]\n"
  801. "2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
  802. " movl $0, %[faulted]\n"
  803. "3:\n"
  804. ".section .fixup, \"ax\"\n"
  805. "4: movl $1, %[faulted]\n"
  806. " jmp 3b\n"
  807. ".previous\n"
  808. _ASM_EXTABLE(1b, 4b)
  809. _ASM_EXTABLE(2b, 4b)
  810. : [old] "=&r" (old), [faulted] "=r" (faulted)
  811. : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
  812. : "memory"
  813. );
  814. if (unlikely(faulted)) {
  815. ftrace_graph_stop();
  816. WARN_ON(1);
  817. return;
  818. }
  819. trace.func = self_addr;
  820. trace.depth = current->curr_ret_stack + 1;
  821. /* Only trace if the calling function expects to */
  822. if (!ftrace_graph_entry(&trace)) {
  823. *parent = old;
  824. return;
  825. }
  826. if (ftrace_push_return_trace(old, self_addr, &trace.depth,
  827. frame_pointer, parent) == -EBUSY) {
  828. *parent = old;
  829. return;
  830. }
  831. }
  832. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */