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