ftrace.c 25 KB

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