ftrace.c 25 KB

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