ftrace.c 25 KB

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