ftrace.c 25 KB

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