ftrace.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. /*
  2. * Code for replacing ftrace calls with jumps.
  3. *
  4. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  5. *
  6. * Thanks goes to Ingo Molnar, for suggesting the idea.
  7. * Mathieu Desnoyers, for suggesting postponing the modifications.
  8. * Arjan van de Ven, for keeping me straight, and explaining to me
  9. * the dangers of modifying code on the run.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/spinlock.h>
  13. #include <linux/hardirq.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/ftrace.h>
  16. #include <linux/percpu.h>
  17. #include <linux/sched.h>
  18. #include <linux/slab.h>
  19. #include <linux/init.h>
  20. #include <linux/list.h>
  21. #include <linux/module.h>
  22. #include <trace/syscall.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/kprobes.h>
  25. #include <asm/ftrace.h>
  26. #include <asm/nops.h>
  27. #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 with
  71. * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead
  72. * of the kernel text mapping 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. /*
  91. * Note: Due to modules and __init, code can
  92. * disappear and change, we need to protect against faulting
  93. * as well as code changing. We do this by using the
  94. * probe_kernel_* functions.
  95. *
  96. * No real locking needed, this code is run through
  97. * kstop_machine, or before SMP starts.
  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. /* 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. return -EINVAL;
  190. }
  191. static unsigned long ftrace_update_func;
  192. static int update_ftrace_func(unsigned long ip, void *new)
  193. {
  194. unsigned char old[MCOUNT_INSN_SIZE];
  195. int ret;
  196. memcpy(old, (void *)ip, MCOUNT_INSN_SIZE);
  197. ftrace_update_func = ip;
  198. /* Make sure the breakpoints see the ftrace_update_func update */
  199. smp_wmb();
  200. /* See comment above by declaration of modifying_ftrace_code */
  201. atomic_inc(&modifying_ftrace_code);
  202. ret = ftrace_modify_code(ip, old, new);
  203. atomic_dec(&modifying_ftrace_code);
  204. return ret;
  205. }
  206. int ftrace_update_ftrace_func(ftrace_func_t func)
  207. {
  208. unsigned long ip = (unsigned long)(&ftrace_call);
  209. unsigned char *new;
  210. int ret;
  211. new = ftrace_call_replace(ip, (unsigned long)func);
  212. ret = update_ftrace_func(ip, new);
  213. /* Also update the regs callback function */
  214. if (!ret) {
  215. ip = (unsigned long)(&ftrace_regs_call);
  216. new = ftrace_call_replace(ip, (unsigned long)func);
  217. ret = update_ftrace_func(ip, new);
  218. }
  219. return ret;
  220. }
  221. static int is_ftrace_caller(unsigned long ip)
  222. {
  223. if (ip == ftrace_update_func)
  224. return 1;
  225. return 0;
  226. }
  227. /*
  228. * A breakpoint was added to the code address we are about to
  229. * modify, and this is the handle that will just skip over it.
  230. * We are either changing a nop into a trace call, or a trace
  231. * call to a nop. While the change is taking place, we treat
  232. * it just like it was a nop.
  233. */
  234. int ftrace_int3_handler(struct pt_regs *regs)
  235. {
  236. unsigned long ip;
  237. if (WARN_ON_ONCE(!regs))
  238. return 0;
  239. ip = regs->ip - 1;
  240. if (!ftrace_location(ip) && !is_ftrace_caller(ip))
  241. return 0;
  242. regs->ip += MCOUNT_INSN_SIZE - 1;
  243. return 1;
  244. }
  245. static int ftrace_write(unsigned long ip, const char *val, int size)
  246. {
  247. ip = text_ip_addr(ip);
  248. if (probe_kernel_write((void *)ip, val, size))
  249. return -EPERM;
  250. return 0;
  251. }
  252. static int add_break(unsigned long ip, const char *old)
  253. {
  254. unsigned char replaced[MCOUNT_INSN_SIZE];
  255. unsigned char brk = BREAKPOINT_INSTRUCTION;
  256. if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
  257. return -EFAULT;
  258. /* Make sure it is what we expect it to be */
  259. if (memcmp(replaced, old, MCOUNT_INSN_SIZE) != 0)
  260. return -EINVAL;
  261. return ftrace_write(ip, &brk, 1);
  262. }
  263. static int add_brk_on_call(struct dyn_ftrace *rec, unsigned long addr)
  264. {
  265. unsigned const char *old;
  266. unsigned long ip = rec->ip;
  267. old = ftrace_call_replace(ip, addr);
  268. return add_break(rec->ip, old);
  269. }
  270. static int add_brk_on_nop(struct dyn_ftrace *rec)
  271. {
  272. unsigned const char *old;
  273. old = ftrace_nop_replace();
  274. return add_break(rec->ip, old);
  275. }
  276. static int add_breakpoints(struct dyn_ftrace *rec, int enable)
  277. {
  278. unsigned long ftrace_addr;
  279. int ret;
  280. ftrace_addr = ftrace_get_addr_curr(rec);
  281. ret = ftrace_test_record(rec, enable);
  282. switch (ret) {
  283. case FTRACE_UPDATE_IGNORE:
  284. return 0;
  285. case FTRACE_UPDATE_MAKE_CALL:
  286. /* converting nop to call */
  287. return add_brk_on_nop(rec);
  288. case FTRACE_UPDATE_MODIFY_CALL:
  289. case FTRACE_UPDATE_MAKE_NOP:
  290. /* converting a call to a nop */
  291. return add_brk_on_call(rec, ftrace_addr);
  292. }
  293. return 0;
  294. }
  295. /*
  296. * On error, we need to remove breakpoints. This needs to
  297. * be done caefully. If the address does not currently have a
  298. * breakpoint, we know we are done. Otherwise, we look at the
  299. * remaining 4 bytes of the instruction. If it matches a nop
  300. * we replace the breakpoint with the nop. Otherwise we replace
  301. * it with the call instruction.
  302. */
  303. static int remove_breakpoint(struct dyn_ftrace *rec)
  304. {
  305. unsigned char ins[MCOUNT_INSN_SIZE];
  306. unsigned char brk = BREAKPOINT_INSTRUCTION;
  307. const unsigned char *nop;
  308. unsigned long ftrace_addr;
  309. unsigned long ip = rec->ip;
  310. /* If we fail the read, just give up */
  311. if (probe_kernel_read(ins, (void *)ip, MCOUNT_INSN_SIZE))
  312. return -EFAULT;
  313. /* If this does not have a breakpoint, we are done */
  314. if (ins[0] != brk)
  315. return 0;
  316. nop = ftrace_nop_replace();
  317. /*
  318. * If the last 4 bytes of the instruction do not match
  319. * a nop, then we assume that this is a call to ftrace_addr.
  320. */
  321. if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0) {
  322. /*
  323. * For extra paranoidism, we check if the breakpoint is on
  324. * a call that would actually jump to the ftrace_addr.
  325. * If not, don't touch the breakpoint, we make just create
  326. * a disaster.
  327. */
  328. ftrace_addr = ftrace_get_addr_new(rec);
  329. nop = ftrace_call_replace(ip, ftrace_addr);
  330. if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) == 0)
  331. goto update;
  332. /* Check both ftrace_addr and ftrace_old_addr */
  333. ftrace_addr = ftrace_get_addr_curr(rec);
  334. nop = ftrace_call_replace(ip, ftrace_addr);
  335. if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0)
  336. return -EINVAL;
  337. }
  338. update:
  339. return ftrace_write(ip, nop, 1);
  340. }
  341. static int add_update_code(unsigned long ip, unsigned const char *new)
  342. {
  343. /* skip breakpoint */
  344. ip++;
  345. new++;
  346. return ftrace_write(ip, new, MCOUNT_INSN_SIZE - 1);
  347. }
  348. static int add_update_call(struct dyn_ftrace *rec, unsigned long addr)
  349. {
  350. unsigned long ip = rec->ip;
  351. unsigned const char *new;
  352. new = ftrace_call_replace(ip, addr);
  353. return add_update_code(ip, new);
  354. }
  355. static int add_update_nop(struct dyn_ftrace *rec)
  356. {
  357. unsigned long ip = rec->ip;
  358. unsigned const char *new;
  359. new = ftrace_nop_replace();
  360. return add_update_code(ip, new);
  361. }
  362. static int add_update(struct dyn_ftrace *rec, int enable)
  363. {
  364. unsigned long ftrace_addr;
  365. int ret;
  366. ret = ftrace_test_record(rec, enable);
  367. ftrace_addr = ftrace_get_addr_new(rec);
  368. switch (ret) {
  369. case FTRACE_UPDATE_IGNORE:
  370. return 0;
  371. case FTRACE_UPDATE_MODIFY_CALL:
  372. case FTRACE_UPDATE_MAKE_CALL:
  373. /* converting nop to call */
  374. return add_update_call(rec, ftrace_addr);
  375. case FTRACE_UPDATE_MAKE_NOP:
  376. /* converting a call to a nop */
  377. return add_update_nop(rec);
  378. }
  379. return 0;
  380. }
  381. static int finish_update_call(struct dyn_ftrace *rec, unsigned long addr)
  382. {
  383. unsigned long ip = rec->ip;
  384. unsigned const char *new;
  385. new = ftrace_call_replace(ip, addr);
  386. return ftrace_write(ip, new, 1);
  387. }
  388. static int finish_update_nop(struct dyn_ftrace *rec)
  389. {
  390. unsigned long ip = rec->ip;
  391. unsigned const char *new;
  392. new = ftrace_nop_replace();
  393. return ftrace_write(ip, new, 1);
  394. }
  395. static int finish_update(struct dyn_ftrace *rec, int enable)
  396. {
  397. unsigned long ftrace_addr;
  398. int ret;
  399. ret = ftrace_update_record(rec, enable);
  400. ftrace_addr = ftrace_get_addr_new(rec);
  401. switch (ret) {
  402. case FTRACE_UPDATE_IGNORE:
  403. return 0;
  404. case FTRACE_UPDATE_MODIFY_CALL:
  405. case FTRACE_UPDATE_MAKE_CALL:
  406. /* converting nop to call */
  407. return finish_update_call(rec, ftrace_addr);
  408. case FTRACE_UPDATE_MAKE_NOP:
  409. /* converting a call to a nop */
  410. return finish_update_nop(rec);
  411. }
  412. return 0;
  413. }
  414. static void do_sync_core(void *data)
  415. {
  416. sync_core();
  417. }
  418. static void run_sync(void)
  419. {
  420. int enable_irqs = irqs_disabled();
  421. /* We may be called with interrupts disbled (on bootup). */
  422. if (enable_irqs)
  423. local_irq_enable();
  424. on_each_cpu(do_sync_core, NULL, 1);
  425. if (enable_irqs)
  426. local_irq_disable();
  427. }
  428. void ftrace_replace_code(int enable)
  429. {
  430. struct ftrace_rec_iter *iter;
  431. struct dyn_ftrace *rec;
  432. const char *report = "adding breakpoints";
  433. int count = 0;
  434. int ret;
  435. for_ftrace_rec_iter(iter) {
  436. rec = ftrace_rec_iter_record(iter);
  437. ret = add_breakpoints(rec, enable);
  438. if (ret)
  439. goto remove_breakpoints;
  440. count++;
  441. }
  442. run_sync();
  443. report = "updating code";
  444. count = 0;
  445. for_ftrace_rec_iter(iter) {
  446. rec = ftrace_rec_iter_record(iter);
  447. ret = add_update(rec, enable);
  448. if (ret)
  449. goto remove_breakpoints;
  450. count++;
  451. }
  452. run_sync();
  453. report = "removing breakpoints";
  454. count = 0;
  455. for_ftrace_rec_iter(iter) {
  456. rec = ftrace_rec_iter_record(iter);
  457. ret = finish_update(rec, enable);
  458. if (ret)
  459. goto remove_breakpoints;
  460. count++;
  461. }
  462. run_sync();
  463. return;
  464. remove_breakpoints:
  465. pr_warn("Failed on %s (%d):\n", report, count);
  466. ftrace_bug(ret, rec);
  467. for_ftrace_rec_iter(iter) {
  468. rec = ftrace_rec_iter_record(iter);
  469. /*
  470. * Breakpoints are handled only when this function is in
  471. * progress. The system could not work with them.
  472. */
  473. if (remove_breakpoint(rec))
  474. BUG();
  475. }
  476. run_sync();
  477. }
  478. static int
  479. ftrace_modify_code(unsigned long ip, unsigned const char *old_code,
  480. unsigned const char *new_code)
  481. {
  482. int ret;
  483. ret = add_break(ip, old_code);
  484. if (ret)
  485. goto out;
  486. run_sync();
  487. ret = add_update_code(ip, new_code);
  488. if (ret)
  489. goto fail_update;
  490. run_sync();
  491. ret = ftrace_write(ip, new_code, 1);
  492. /*
  493. * The breakpoint is handled only when this function is in progress.
  494. * The system could not work if we could not remove it.
  495. */
  496. BUG_ON(ret);
  497. out:
  498. run_sync();
  499. return ret;
  500. fail_update:
  501. /* Also here the system could not work with the breakpoint */
  502. if (ftrace_write(ip, old_code, 1))
  503. BUG();
  504. goto out;
  505. }
  506. void arch_ftrace_update_code(int command)
  507. {
  508. /* See comment above by declaration of modifying_ftrace_code */
  509. atomic_inc(&modifying_ftrace_code);
  510. ftrace_modify_all_code(command);
  511. atomic_dec(&modifying_ftrace_code);
  512. }
  513. int __init ftrace_dyn_arch_init(void)
  514. {
  515. return 0;
  516. }
  517. #if defined(CONFIG_X86_64) || defined(CONFIG_FUNCTION_GRAPH_TRACER)
  518. static unsigned char *ftrace_jmp_replace(unsigned long ip, unsigned long addr)
  519. {
  520. static union ftrace_code_union calc;
  521. /* Jmp not a call (ignore the .e8) */
  522. calc.e8 = 0xe9;
  523. calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
  524. /*
  525. * ftrace external locks synchronize the access to the static variable.
  526. */
  527. return calc.code;
  528. }
  529. #endif
  530. /* Currently only x86_64 supports dynamic trampolines */
  531. #ifdef CONFIG_X86_64
  532. #ifdef CONFIG_MODULES
  533. #include <linux/moduleloader.h>
  534. /* Module allocation simplifies allocating memory for code */
  535. static inline void *alloc_tramp(unsigned long size)
  536. {
  537. return module_alloc(size);
  538. }
  539. static inline void tramp_free(void *tramp)
  540. {
  541. module_memfree(tramp);
  542. }
  543. #else
  544. /* Trampolines can only be created if modules are supported */
  545. static inline void *alloc_tramp(unsigned long size)
  546. {
  547. return NULL;
  548. }
  549. static inline void tramp_free(void *tramp) { }
  550. #endif
  551. /* Defined as markers to the end of the ftrace default trampolines */
  552. extern void ftrace_caller_end(void);
  553. extern void ftrace_regs_caller_end(void);
  554. extern void ftrace_return(void);
  555. extern void ftrace_caller_op_ptr(void);
  556. extern void ftrace_regs_caller_op_ptr(void);
  557. /* movq function_trace_op(%rip), %rdx */
  558. /* 0x48 0x8b 0x15 <offset-to-ftrace_trace_op (4 bytes)> */
  559. #define OP_REF_SIZE 7
  560. /*
  561. * The ftrace_ops is passed to the function callback. Since the
  562. * trampoline only services a single ftrace_ops, we can pass in
  563. * that ops directly.
  564. *
  565. * The ftrace_op_code_union is used to create a pointer to the
  566. * ftrace_ops that will be passed to the callback function.
  567. */
  568. union ftrace_op_code_union {
  569. char code[OP_REF_SIZE];
  570. struct {
  571. char op[3];
  572. int offset;
  573. } __attribute__((packed));
  574. };
  575. static unsigned long
  576. create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
  577. {
  578. unsigned const char *jmp;
  579. unsigned long start_offset;
  580. unsigned long end_offset;
  581. unsigned long op_offset;
  582. unsigned long offset;
  583. unsigned long size;
  584. unsigned long ip;
  585. unsigned long *ptr;
  586. void *trampoline;
  587. /* 48 8b 15 <offset> is movq <offset>(%rip), %rdx */
  588. unsigned const char op_ref[] = { 0x48, 0x8b, 0x15 };
  589. union ftrace_op_code_union op_ptr;
  590. int ret;
  591. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
  592. start_offset = (unsigned long)ftrace_regs_caller;
  593. end_offset = (unsigned long)ftrace_regs_caller_end;
  594. op_offset = (unsigned long)ftrace_regs_caller_op_ptr;
  595. } else {
  596. start_offset = (unsigned long)ftrace_caller;
  597. end_offset = (unsigned long)ftrace_caller_end;
  598. op_offset = (unsigned long)ftrace_caller_op_ptr;
  599. }
  600. size = end_offset - start_offset;
  601. /*
  602. * Allocate enough size to store the ftrace_caller code,
  603. * the jmp to ftrace_return, as well as the address of
  604. * the ftrace_ops this trampoline is used for.
  605. */
  606. trampoline = alloc_tramp(size + MCOUNT_INSN_SIZE + sizeof(void *));
  607. if (!trampoline)
  608. return 0;
  609. *tramp_size = size + MCOUNT_INSN_SIZE + sizeof(void *);
  610. /* Copy ftrace_caller onto the trampoline memory */
  611. ret = probe_kernel_read(trampoline, (void *)start_offset, size);
  612. if (WARN_ON(ret < 0)) {
  613. tramp_free(trampoline);
  614. return 0;
  615. }
  616. ip = (unsigned long)trampoline + size;
  617. /* The trampoline ends with a jmp to ftrace_return */
  618. jmp = ftrace_jmp_replace(ip, (unsigned long)ftrace_return);
  619. memcpy(trampoline + size, jmp, MCOUNT_INSN_SIZE);
  620. /*
  621. * The address of the ftrace_ops that is used for this trampoline
  622. * is stored at the end of the trampoline. This will be used to
  623. * load the third parameter for the callback. Basically, that
  624. * location at the end of the trampoline takes the place of
  625. * the global function_trace_op variable.
  626. */
  627. ptr = (unsigned long *)(trampoline + size + MCOUNT_INSN_SIZE);
  628. *ptr = (unsigned long)ops;
  629. op_offset -= start_offset;
  630. memcpy(&op_ptr, trampoline + op_offset, OP_REF_SIZE);
  631. /* Are we pointing to the reference? */
  632. if (WARN_ON(memcmp(op_ptr.op, op_ref, 3) != 0)) {
  633. tramp_free(trampoline);
  634. return 0;
  635. }
  636. /* Load the contents of ptr into the callback parameter */
  637. offset = (unsigned long)ptr;
  638. offset -= (unsigned long)trampoline + op_offset + OP_REF_SIZE;
  639. op_ptr.offset = offset;
  640. /* put in the new offset to the ftrace_ops */
  641. memcpy(trampoline + op_offset, &op_ptr, OP_REF_SIZE);
  642. /* ALLOC_TRAMP flags lets us know we created it */
  643. ops->flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
  644. return (unsigned long)trampoline;
  645. }
  646. static unsigned long calc_trampoline_call_offset(bool save_regs)
  647. {
  648. unsigned long start_offset;
  649. unsigned long call_offset;
  650. if (save_regs) {
  651. start_offset = (unsigned long)ftrace_regs_caller;
  652. call_offset = (unsigned long)ftrace_regs_call;
  653. } else {
  654. start_offset = (unsigned long)ftrace_caller;
  655. call_offset = (unsigned long)ftrace_call;
  656. }
  657. return call_offset - start_offset;
  658. }
  659. void arch_ftrace_update_trampoline(struct ftrace_ops *ops)
  660. {
  661. ftrace_func_t func;
  662. unsigned char *new;
  663. unsigned long offset;
  664. unsigned long ip;
  665. unsigned int size;
  666. int ret;
  667. if (ops->trampoline) {
  668. /*
  669. * The ftrace_ops caller may set up its own trampoline.
  670. * In such a case, this code must not modify it.
  671. */
  672. if (!(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
  673. return;
  674. } else {
  675. ops->trampoline = create_trampoline(ops, &size);
  676. if (!ops->trampoline)
  677. return;
  678. ops->trampoline_size = size;
  679. }
  680. offset = calc_trampoline_call_offset(ops->flags & FTRACE_OPS_FL_SAVE_REGS);
  681. ip = ops->trampoline + offset;
  682. func = ftrace_ops_get_func(ops);
  683. /* Do a safe modify in case the trampoline is executing */
  684. new = ftrace_call_replace(ip, (unsigned long)func);
  685. ret = update_ftrace_func(ip, new);
  686. /* The update should never fail */
  687. WARN_ON(ret);
  688. }
  689. /* Return the address of the function the trampoline calls */
  690. static void *addr_from_call(void *ptr)
  691. {
  692. union ftrace_code_union calc;
  693. int ret;
  694. ret = probe_kernel_read(&calc, ptr, MCOUNT_INSN_SIZE);
  695. if (WARN_ON_ONCE(ret < 0))
  696. return NULL;
  697. /* Make sure this is a call */
  698. if (WARN_ON_ONCE(calc.e8 != 0xe8)) {
  699. pr_warn("Expected e8, got %x\n", calc.e8);
  700. return NULL;
  701. }
  702. return ptr + MCOUNT_INSN_SIZE + calc.offset;
  703. }
  704. void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent,
  705. unsigned long frame_pointer);
  706. /*
  707. * If the ops->trampoline was not allocated, then it probably
  708. * has a static trampoline func, or is the ftrace caller itself.
  709. */
  710. static void *static_tramp_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
  711. {
  712. unsigned long offset;
  713. bool save_regs = rec->flags & FTRACE_FL_REGS_EN;
  714. void *ptr;
  715. if (ops && ops->trampoline) {
  716. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  717. /*
  718. * We only know about function graph tracer setting as static
  719. * trampoline.
  720. */
  721. if (ops->trampoline == FTRACE_GRAPH_ADDR)
  722. return (void *)prepare_ftrace_return;
  723. #endif
  724. return NULL;
  725. }
  726. offset = calc_trampoline_call_offset(save_regs);
  727. if (save_regs)
  728. ptr = (void *)FTRACE_REGS_ADDR + offset;
  729. else
  730. ptr = (void *)FTRACE_ADDR + offset;
  731. return addr_from_call(ptr);
  732. }
  733. void *arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
  734. {
  735. unsigned long offset;
  736. /* If we didn't allocate this trampoline, consider it static */
  737. if (!ops || !(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
  738. return static_tramp_func(ops, rec);
  739. offset = calc_trampoline_call_offset(ops->flags & FTRACE_OPS_FL_SAVE_REGS);
  740. return addr_from_call((void *)ops->trampoline + offset);
  741. }
  742. void arch_ftrace_trampoline_free(struct ftrace_ops *ops)
  743. {
  744. if (!ops || !(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
  745. return;
  746. tramp_free((void *)ops->trampoline);
  747. ops->trampoline = 0;
  748. }
  749. #endif /* CONFIG_X86_64 */
  750. #endif /* CONFIG_DYNAMIC_FTRACE */
  751. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  752. #ifdef CONFIG_DYNAMIC_FTRACE
  753. extern void ftrace_graph_call(void);
  754. static int ftrace_mod_jmp(unsigned long ip, void *func)
  755. {
  756. unsigned char *new;
  757. new = ftrace_jmp_replace(ip, (unsigned long)func);
  758. return update_ftrace_func(ip, new);
  759. }
  760. int ftrace_enable_ftrace_graph_caller(void)
  761. {
  762. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  763. return ftrace_mod_jmp(ip, &ftrace_graph_caller);
  764. }
  765. int ftrace_disable_ftrace_graph_caller(void)
  766. {
  767. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  768. return ftrace_mod_jmp(ip, &ftrace_stub);
  769. }
  770. #endif /* !CONFIG_DYNAMIC_FTRACE */
  771. /*
  772. * Hook the return address and push it in the stack of return addrs
  773. * in current thread info.
  774. */
  775. void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent,
  776. unsigned long frame_pointer)
  777. {
  778. unsigned long old;
  779. int faulted;
  780. struct ftrace_graph_ent trace;
  781. unsigned long return_hooker = (unsigned long)
  782. &return_to_handler;
  783. /*
  784. * When resuming from suspend-to-ram, this function can be indirectly
  785. * called from early CPU startup code while the CPU is in real mode,
  786. * which would fail miserably. Make sure the stack pointer is a
  787. * virtual address.
  788. *
  789. * This check isn't as accurate as virt_addr_valid(), but it should be
  790. * good enough for this purpose, and it's fast.
  791. */
  792. if (unlikely((long)__builtin_frame_address(0) >= 0))
  793. return;
  794. if (unlikely(ftrace_graph_is_dead()))
  795. return;
  796. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  797. return;
  798. /*
  799. * Protect against fault, even if it shouldn't
  800. * happen. This tool is too much intrusive to
  801. * ignore such a protection.
  802. */
  803. asm volatile(
  804. "1: " _ASM_MOV " (%[parent]), %[old]\n"
  805. "2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
  806. " movl $0, %[faulted]\n"
  807. "3:\n"
  808. ".section .fixup, \"ax\"\n"
  809. "4: movl $1, %[faulted]\n"
  810. " jmp 3b\n"
  811. ".previous\n"
  812. _ASM_EXTABLE(1b, 4b)
  813. _ASM_EXTABLE(2b, 4b)
  814. : [old] "=&r" (old), [faulted] "=r" (faulted)
  815. : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
  816. : "memory"
  817. );
  818. if (unlikely(faulted)) {
  819. ftrace_graph_stop();
  820. WARN_ON(1);
  821. return;
  822. }
  823. trace.func = self_addr;
  824. trace.depth = current->curr_ret_stack + 1;
  825. /* Only trace if the calling function expects to */
  826. if (!ftrace_graph_entry(&trace)) {
  827. *parent = old;
  828. return;
  829. }
  830. if (ftrace_push_return_trace(old, self_addr, &trace.depth,
  831. frame_pointer) == -EBUSY) {
  832. *parent = old;
  833. return;
  834. }
  835. }
  836. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */