tracing: Use linker magic instead of recasting ftrace_ops_list_func()
[linux-2.6-microblaze.git] / include / linux / ftrace.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Ftrace header.  For implementation details beyond the random comments
4  * scattered below, see: Documentation/trace/ftrace-design.rst
5  */
6
7 #ifndef _LINUX_FTRACE_H
8 #define _LINUX_FTRACE_H
9
10 #include <linux/trace_recursion.h>
11 #include <linux/trace_clock.h>
12 #include <linux/kallsyms.h>
13 #include <linux/linkage.h>
14 #include <linux/bitops.h>
15 #include <linux/ptrace.h>
16 #include <linux/ktime.h>
17 #include <linux/sched.h>
18 #include <linux/types.h>
19 #include <linux/init.h>
20 #include <linux/fs.h>
21
22 #include <asm/ftrace.h>
23
24 /*
25  * If the arch supports passing the variable contents of
26  * function_trace_op as the third parameter back from the
27  * mcount call, then the arch should define this as 1.
28  */
29 #ifndef ARCH_SUPPORTS_FTRACE_OPS
30 #define ARCH_SUPPORTS_FTRACE_OPS 0
31 #endif
32
33 #ifdef CONFIG_FUNCTION_TRACER
34 struct ftrace_ops;
35 struct ftrace_regs;
36 /*
37  * If the arch's mcount caller does not support all of ftrace's
38  * features, then it must call an indirect function that
39  * does. Or at least does enough to prevent any unwelcome side effects.
40  *
41  * Also define the function prototype that these architectures use
42  * to call the ftrace_ops_list_func().
43  */
44 #if !ARCH_SUPPORTS_FTRACE_OPS
45 # define FTRACE_FORCE_LIST_FUNC 1
46 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
47 #else
48 # define FTRACE_FORCE_LIST_FUNC 0
49 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
50                                struct ftrace_ops *op, struct ftrace_regs *fregs);
51 #endif
52 #endif /* CONFIG_FUNCTION_TRACER */
53
54 /* Main tracing buffer and events set up */
55 #ifdef CONFIG_TRACING
56 void trace_init(void);
57 void early_trace_init(void);
58 #else
59 static inline void trace_init(void) { }
60 static inline void early_trace_init(void) { }
61 #endif
62
63 struct module;
64 struct ftrace_hash;
65 struct ftrace_direct_func;
66
67 #if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_MODULES) && \
68         defined(CONFIG_DYNAMIC_FTRACE)
69 const char *
70 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
71                    unsigned long *off, char **modname, char *sym);
72 #else
73 static inline const char *
74 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
75                    unsigned long *off, char **modname, char *sym)
76 {
77         return NULL;
78 }
79 #endif
80
81 #if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_DYNAMIC_FTRACE)
82 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
83                            char *type, char *name,
84                            char *module_name, int *exported);
85 #else
86 static inline int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
87                                          char *type, char *name,
88                                          char *module_name, int *exported)
89 {
90         return -1;
91 }
92 #endif
93
94 #ifdef CONFIG_FUNCTION_TRACER
95
96 extern int ftrace_enabled;
97 extern int
98 ftrace_enable_sysctl(struct ctl_table *table, int write,
99                      void *buffer, size_t *lenp, loff_t *ppos);
100
101 #ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
102
103 struct ftrace_regs {
104         struct pt_regs          regs;
105 };
106 #define arch_ftrace_get_regs(fregs) (&(fregs)->regs)
107
108 /*
109  * ftrace_instruction_pointer_set() is to be defined by the architecture
110  * if to allow setting of the instruction pointer from the ftrace_regs
111  * when HAVE_DYNAMIC_FTRACE_WITH_ARGS is set and it supports
112  * live kernel patching.
113  */
114 #define ftrace_instruction_pointer_set(fregs, ip) do { } while (0)
115 #endif /* CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS */
116
117 static __always_inline struct pt_regs *ftrace_get_regs(struct ftrace_regs *fregs)
118 {
119         if (!fregs)
120                 return NULL;
121
122         return arch_ftrace_get_regs(fregs);
123 }
124
125 typedef void (*ftrace_func_t)(unsigned long ip, unsigned long parent_ip,
126                               struct ftrace_ops *op, struct ftrace_regs *fregs);
127
128 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops);
129
130 /*
131  * FTRACE_OPS_FL_* bits denote the state of ftrace_ops struct and are
132  * set in the flags member.
133  * CONTROL, SAVE_REGS, SAVE_REGS_IF_SUPPORTED, RECURSION, STUB and
134  * IPMODIFY are a kind of attribute flags which can be set only before
135  * registering the ftrace_ops, and can not be modified while registered.
136  * Changing those attribute flags after registering ftrace_ops will
137  * cause unexpected results.
138  *
139  * ENABLED - set/unset when ftrace_ops is registered/unregistered
140  * DYNAMIC - set when ftrace_ops is registered to denote dynamically
141  *           allocated ftrace_ops which need special care
142  * SAVE_REGS - The ftrace_ops wants regs saved at each function called
143  *            and passed to the callback. If this flag is set, but the
144  *            architecture does not support passing regs
145  *            (CONFIG_DYNAMIC_FTRACE_WITH_REGS is not defined), then the
146  *            ftrace_ops will fail to register, unless the next flag
147  *            is set.
148  * SAVE_REGS_IF_SUPPORTED - This is the same as SAVE_REGS, but if the
149  *            handler can handle an arch that does not save regs
150  *            (the handler tests if regs == NULL), then it can set
151  *            this flag instead. It will not fail registering the ftrace_ops
152  *            but, the regs field will be NULL if the arch does not support
153  *            passing regs to the handler.
154  *            Note, if this flag is set, the SAVE_REGS flag will automatically
155  *            get set upon registering the ftrace_ops, if the arch supports it.
156  * RECURSION - The ftrace_ops can set this to tell the ftrace infrastructure
157  *            that the call back needs recursion protection. If it does
158  *            not set this, then the ftrace infrastructure will assume
159  *            that the callback can handle recursion on its own.
160  * STUB   - The ftrace_ops is just a place holder.
161  * INITIALIZED - The ftrace_ops has already been initialized (first use time
162  *            register_ftrace_function() is called, it will initialized the ops)
163  * DELETED - The ops are being deleted, do not let them be registered again.
164  * ADDING  - The ops is in the process of being added.
165  * REMOVING - The ops is in the process of being removed.
166  * MODIFYING - The ops is in the process of changing its filter functions.
167  * ALLOC_TRAMP - A dynamic trampoline was allocated by the core code.
168  *            The arch specific code sets this flag when it allocated a
169  *            trampoline. This lets the arch know that it can update the
170  *            trampoline in case the callback function changes.
171  *            The ftrace_ops trampoline can be set by the ftrace users, and
172  *            in such cases the arch must not modify it. Only the arch ftrace
173  *            core code should set this flag.
174  * IPMODIFY - The ops can modify the IP register. This can only be set with
175  *            SAVE_REGS. If another ops with this flag set is already registered
176  *            for any of the functions that this ops will be registered for, then
177  *            this ops will fail to register or set_filter_ip.
178  * PID     - Is affected by set_ftrace_pid (allows filtering on those pids)
179  * RCU     - Set when the ops can only be called when RCU is watching.
180  * TRACE_ARRAY - The ops->private points to a trace_array descriptor.
181  * PERMANENT - Set when the ops is permanent and should not be affected by
182  *             ftrace_enabled.
183  * DIRECT - Used by the direct ftrace_ops helper for direct functions
184  *            (internal ftrace only, should not be used by others)
185  */
186 enum {
187         FTRACE_OPS_FL_ENABLED                   = BIT(0),
188         FTRACE_OPS_FL_DYNAMIC                   = BIT(1),
189         FTRACE_OPS_FL_SAVE_REGS                 = BIT(2),
190         FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED    = BIT(3),
191         FTRACE_OPS_FL_RECURSION                 = BIT(4),
192         FTRACE_OPS_FL_STUB                      = BIT(5),
193         FTRACE_OPS_FL_INITIALIZED               = BIT(6),
194         FTRACE_OPS_FL_DELETED                   = BIT(7),
195         FTRACE_OPS_FL_ADDING                    = BIT(8),
196         FTRACE_OPS_FL_REMOVING                  = BIT(9),
197         FTRACE_OPS_FL_MODIFYING                 = BIT(10),
198         FTRACE_OPS_FL_ALLOC_TRAMP               = BIT(11),
199         FTRACE_OPS_FL_IPMODIFY                  = BIT(12),
200         FTRACE_OPS_FL_PID                       = BIT(13),
201         FTRACE_OPS_FL_RCU                       = BIT(14),
202         FTRACE_OPS_FL_TRACE_ARRAY               = BIT(15),
203         FTRACE_OPS_FL_PERMANENT                 = BIT(16),
204         FTRACE_OPS_FL_DIRECT                    = BIT(17),
205 };
206
207 #ifdef CONFIG_DYNAMIC_FTRACE
208 /* The hash used to know what functions callbacks trace */
209 struct ftrace_ops_hash {
210         struct ftrace_hash __rcu        *notrace_hash;
211         struct ftrace_hash __rcu        *filter_hash;
212         struct mutex                    regex_lock;
213 };
214
215 void ftrace_free_init_mem(void);
216 void ftrace_free_mem(struct module *mod, void *start, void *end);
217 #else
218 static inline void ftrace_free_init_mem(void) { }
219 static inline void ftrace_free_mem(struct module *mod, void *start, void *end) { }
220 #endif
221
222 /*
223  * Note, ftrace_ops can be referenced outside of RCU protection, unless
224  * the RCU flag is set. If ftrace_ops is allocated and not part of kernel
225  * core data, the unregistering of it will perform a scheduling on all CPUs
226  * to make sure that there are no more users. Depending on the load of the
227  * system that may take a bit of time.
228  *
229  * Any private data added must also take care not to be freed and if private
230  * data is added to a ftrace_ops that is in core code, the user of the
231  * ftrace_ops must perform a schedule_on_each_cpu() before freeing it.
232  */
233 struct ftrace_ops {
234         ftrace_func_t                   func;
235         struct ftrace_ops __rcu         *next;
236         unsigned long                   flags;
237         void                            *private;
238         ftrace_func_t                   saved_func;
239 #ifdef CONFIG_DYNAMIC_FTRACE
240         struct ftrace_ops_hash          local_hash;
241         struct ftrace_ops_hash          *func_hash;
242         struct ftrace_ops_hash          old_hash;
243         unsigned long                   trampoline;
244         unsigned long                   trampoline_size;
245         struct list_head                list;
246 #endif
247 };
248
249 extern struct ftrace_ops __rcu *ftrace_ops_list;
250 extern struct ftrace_ops ftrace_list_end;
251
252 /*
253  * Traverse the ftrace_ops_list, invoking all entries.  The reason that we
254  * can use rcu_dereference_raw_check() is that elements removed from this list
255  * are simply leaked, so there is no need to interact with a grace-period
256  * mechanism.  The rcu_dereference_raw_check() calls are needed to handle
257  * concurrent insertions into the ftrace_ops_list.
258  *
259  * Silly Alpha and silly pointer-speculation compiler optimizations!
260  */
261 #define do_for_each_ftrace_op(op, list)                 \
262         op = rcu_dereference_raw_check(list);                   \
263         do
264
265 /*
266  * Optimized for just a single item in the list (as that is the normal case).
267  */
268 #define while_for_each_ftrace_op(op)                            \
269         while (likely(op = rcu_dereference_raw_check((op)->next)) &&    \
270                unlikely((op) != &ftrace_list_end))
271
272 /*
273  * Type of the current tracing.
274  */
275 enum ftrace_tracing_type_t {
276         FTRACE_TYPE_ENTER = 0, /* Hook the call of the function */
277         FTRACE_TYPE_RETURN,     /* Hook the return of the function */
278 };
279
280 /* Current tracing type, default is FTRACE_TYPE_ENTER */
281 extern enum ftrace_tracing_type_t ftrace_tracing_type;
282
283 /*
284  * The ftrace_ops must be a static and should also
285  * be read_mostly.  These functions do modify read_mostly variables
286  * so use them sparely. Never free an ftrace_op or modify the
287  * next pointer after it has been registered. Even after unregistering
288  * it, the next pointer may still be used internally.
289  */
290 int register_ftrace_function(struct ftrace_ops *ops);
291 int unregister_ftrace_function(struct ftrace_ops *ops);
292
293 extern void ftrace_stub(unsigned long a0, unsigned long a1,
294                         struct ftrace_ops *op, struct ftrace_regs *fregs);
295
296 #else /* !CONFIG_FUNCTION_TRACER */
297 /*
298  * (un)register_ftrace_function must be a macro since the ops parameter
299  * must not be evaluated.
300  */
301 #define register_ftrace_function(ops) ({ 0; })
302 #define unregister_ftrace_function(ops) ({ 0; })
303 static inline void ftrace_kill(void) { }
304 static inline void ftrace_free_init_mem(void) { }
305 static inline void ftrace_free_mem(struct module *mod, void *start, void *end) { }
306 #endif /* CONFIG_FUNCTION_TRACER */
307
308 struct ftrace_func_entry {
309         struct hlist_node hlist;
310         unsigned long ip;
311         unsigned long direct; /* for direct lookup only */
312 };
313
314 struct dyn_ftrace;
315
316 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
317 extern int ftrace_direct_func_count;
318 int register_ftrace_direct(unsigned long ip, unsigned long addr);
319 int unregister_ftrace_direct(unsigned long ip, unsigned long addr);
320 int modify_ftrace_direct(unsigned long ip, unsigned long old_addr, unsigned long new_addr);
321 struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr);
322 int ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
323                                 struct dyn_ftrace *rec,
324                                 unsigned long old_addr,
325                                 unsigned long new_addr);
326 unsigned long ftrace_find_rec_direct(unsigned long ip);
327 #else
328 # define ftrace_direct_func_count 0
329 static inline int register_ftrace_direct(unsigned long ip, unsigned long addr)
330 {
331         return -ENOTSUPP;
332 }
333 static inline int unregister_ftrace_direct(unsigned long ip, unsigned long addr)
334 {
335         return -ENOTSUPP;
336 }
337 static inline int modify_ftrace_direct(unsigned long ip,
338                                        unsigned long old_addr, unsigned long new_addr)
339 {
340         return -ENOTSUPP;
341 }
342 static inline struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr)
343 {
344         return NULL;
345 }
346 static inline int ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
347                                               struct dyn_ftrace *rec,
348                                               unsigned long old_addr,
349                                               unsigned long new_addr)
350 {
351         return -ENODEV;
352 }
353 static inline unsigned long ftrace_find_rec_direct(unsigned long ip)
354 {
355         return 0;
356 }
357 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
358
359 #ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
360 /*
361  * This must be implemented by the architecture.
362  * It is the way the ftrace direct_ops helper, when called
363  * via ftrace (because there's other callbacks besides the
364  * direct call), can inform the architecture's trampoline that this
365  * routine has a direct caller, and what the caller is.
366  *
367  * For example, in x86, it returns the direct caller
368  * callback function via the regs->orig_ax parameter.
369  * Then in the ftrace trampoline, if this is set, it makes
370  * the return from the trampoline jump to the direct caller
371  * instead of going back to the function it just traced.
372  */
373 static inline void arch_ftrace_set_direct_caller(struct pt_regs *regs,
374                                                  unsigned long addr) { }
375 #endif /* CONFIG_HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
376
377 #ifdef CONFIG_STACK_TRACER
378
379 extern int stack_tracer_enabled;
380
381 int stack_trace_sysctl(struct ctl_table *table, int write, void *buffer,
382                        size_t *lenp, loff_t *ppos);
383
384 /* DO NOT MODIFY THIS VARIABLE DIRECTLY! */
385 DECLARE_PER_CPU(int, disable_stack_tracer);
386
387 /**
388  * stack_tracer_disable - temporarily disable the stack tracer
389  *
390  * There's a few locations (namely in RCU) where stack tracing
391  * cannot be executed. This function is used to disable stack
392  * tracing during those critical sections.
393  *
394  * This function must be called with preemption or interrupts
395  * disabled and stack_tracer_enable() must be called shortly after
396  * while preemption or interrupts are still disabled.
397  */
398 static inline void stack_tracer_disable(void)
399 {
400         /* Preemption or interrupts must be disabled */
401         if (IS_ENABLED(CONFIG_DEBUG_PREEMPT))
402                 WARN_ON_ONCE(!preempt_count() || !irqs_disabled());
403         this_cpu_inc(disable_stack_tracer);
404 }
405
406 /**
407  * stack_tracer_enable - re-enable the stack tracer
408  *
409  * After stack_tracer_disable() is called, stack_tracer_enable()
410  * must be called shortly afterward.
411  */
412 static inline void stack_tracer_enable(void)
413 {
414         if (IS_ENABLED(CONFIG_DEBUG_PREEMPT))
415                 WARN_ON_ONCE(!preempt_count() || !irqs_disabled());
416         this_cpu_dec(disable_stack_tracer);
417 }
418 #else
419 static inline void stack_tracer_disable(void) { }
420 static inline void stack_tracer_enable(void) { }
421 #endif
422
423 #ifdef CONFIG_DYNAMIC_FTRACE
424
425 int ftrace_arch_code_modify_prepare(void);
426 int ftrace_arch_code_modify_post_process(void);
427
428 enum ftrace_bug_type {
429         FTRACE_BUG_UNKNOWN,
430         FTRACE_BUG_INIT,
431         FTRACE_BUG_NOP,
432         FTRACE_BUG_CALL,
433         FTRACE_BUG_UPDATE,
434 };
435 extern enum ftrace_bug_type ftrace_bug_type;
436
437 /*
438  * Archs can set this to point to a variable that holds the value that was
439  * expected at the call site before calling ftrace_bug().
440  */
441 extern const void *ftrace_expected;
442
443 void ftrace_bug(int err, struct dyn_ftrace *rec);
444
445 struct seq_file;
446
447 extern int ftrace_text_reserved(const void *start, const void *end);
448
449 struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr);
450
451 bool is_ftrace_trampoline(unsigned long addr);
452
453 /*
454  * The dyn_ftrace record's flags field is split into two parts.
455  * the first part which is '0-FTRACE_REF_MAX' is a counter of
456  * the number of callbacks that have registered the function that
457  * the dyn_ftrace descriptor represents.
458  *
459  * The second part is a mask:
460  *  ENABLED - the function is being traced
461  *  REGS    - the record wants the function to save regs
462  *  REGS_EN - the function is set up to save regs.
463  *  IPMODIFY - the record allows for the IP address to be changed.
464  *  DISABLED - the record is not ready to be touched yet
465  *  DIRECT   - there is a direct function to call
466  *
467  * When a new ftrace_ops is registered and wants a function to save
468  * pt_regs, the rec->flags REGS is set. When the function has been
469  * set up to save regs, the REG_EN flag is set. Once a function
470  * starts saving regs it will do so until all ftrace_ops are removed
471  * from tracing that function.
472  */
473 enum {
474         FTRACE_FL_ENABLED       = (1UL << 31),
475         FTRACE_FL_REGS          = (1UL << 30),
476         FTRACE_FL_REGS_EN       = (1UL << 29),
477         FTRACE_FL_TRAMP         = (1UL << 28),
478         FTRACE_FL_TRAMP_EN      = (1UL << 27),
479         FTRACE_FL_IPMODIFY      = (1UL << 26),
480         FTRACE_FL_DISABLED      = (1UL << 25),
481         FTRACE_FL_DIRECT        = (1UL << 24),
482         FTRACE_FL_DIRECT_EN     = (1UL << 23),
483 };
484
485 #define FTRACE_REF_MAX_SHIFT    23
486 #define FTRACE_REF_MAX          ((1UL << FTRACE_REF_MAX_SHIFT) - 1)
487
488 #define ftrace_rec_count(rec)   ((rec)->flags & FTRACE_REF_MAX)
489
490 struct dyn_ftrace {
491         unsigned long           ip; /* address of mcount call-site */
492         unsigned long           flags;
493         struct dyn_arch_ftrace  arch;
494 };
495
496 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
497                          int remove, int reset);
498 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
499                        int len, int reset);
500 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
501                         int len, int reset);
502 void ftrace_set_global_filter(unsigned char *buf, int len, int reset);
503 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset);
504 void ftrace_free_filter(struct ftrace_ops *ops);
505 void ftrace_ops_set_global_filter(struct ftrace_ops *ops);
506
507 enum {
508         FTRACE_UPDATE_CALLS             = (1 << 0),
509         FTRACE_DISABLE_CALLS            = (1 << 1),
510         FTRACE_UPDATE_TRACE_FUNC        = (1 << 2),
511         FTRACE_START_FUNC_RET           = (1 << 3),
512         FTRACE_STOP_FUNC_RET            = (1 << 4),
513         FTRACE_MAY_SLEEP                = (1 << 5),
514 };
515
516 /*
517  * The FTRACE_UPDATE_* enum is used to pass information back
518  * from the ftrace_update_record() and ftrace_test_record()
519  * functions. These are called by the code update routines
520  * to find out what is to be done for a given function.
521  *
522  *  IGNORE           - The function is already what we want it to be
523  *  MAKE_CALL        - Start tracing the function
524  *  MODIFY_CALL      - Stop saving regs for the function
525  *  MAKE_NOP         - Stop tracing the function
526  */
527 enum {
528         FTRACE_UPDATE_IGNORE,
529         FTRACE_UPDATE_MAKE_CALL,
530         FTRACE_UPDATE_MODIFY_CALL,
531         FTRACE_UPDATE_MAKE_NOP,
532 };
533
534 enum {
535         FTRACE_ITER_FILTER      = (1 << 0),
536         FTRACE_ITER_NOTRACE     = (1 << 1),
537         FTRACE_ITER_PRINTALL    = (1 << 2),
538         FTRACE_ITER_DO_PROBES   = (1 << 3),
539         FTRACE_ITER_PROBE       = (1 << 4),
540         FTRACE_ITER_MOD         = (1 << 5),
541         FTRACE_ITER_ENABLED     = (1 << 6),
542 };
543
544 void arch_ftrace_update_code(int command);
545 void arch_ftrace_update_trampoline(struct ftrace_ops *ops);
546 void *arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec);
547 void arch_ftrace_trampoline_free(struct ftrace_ops *ops);
548
549 struct ftrace_rec_iter;
550
551 struct ftrace_rec_iter *ftrace_rec_iter_start(void);
552 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter);
553 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter);
554
555 #define for_ftrace_rec_iter(iter)               \
556         for (iter = ftrace_rec_iter_start();    \
557              iter;                              \
558              iter = ftrace_rec_iter_next(iter))
559
560
561 int ftrace_update_record(struct dyn_ftrace *rec, bool enable);
562 int ftrace_test_record(struct dyn_ftrace *rec, bool enable);
563 void ftrace_run_stop_machine(int command);
564 unsigned long ftrace_location(unsigned long ip);
565 unsigned long ftrace_location_range(unsigned long start, unsigned long end);
566 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec);
567 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec);
568
569 extern ftrace_func_t ftrace_trace_function;
570
571 int ftrace_regex_open(struct ftrace_ops *ops, int flag,
572                   struct inode *inode, struct file *file);
573 ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
574                             size_t cnt, loff_t *ppos);
575 ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf,
576                              size_t cnt, loff_t *ppos);
577 int ftrace_regex_release(struct inode *inode, struct file *file);
578
579 void __init
580 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable);
581
582 /* defined in arch */
583 extern int ftrace_ip_converted(unsigned long ip);
584 extern int ftrace_dyn_arch_init(void);
585 extern void ftrace_replace_code(int enable);
586 extern int ftrace_update_ftrace_func(ftrace_func_t func);
587 extern void ftrace_caller(void);
588 extern void ftrace_regs_caller(void);
589 extern void ftrace_call(void);
590 extern void ftrace_regs_call(void);
591 extern void mcount_call(void);
592
593 void ftrace_modify_all_code(int command);
594
595 #ifndef FTRACE_ADDR
596 #define FTRACE_ADDR ((unsigned long)ftrace_caller)
597 #endif
598
599 #ifndef FTRACE_GRAPH_ADDR
600 #define FTRACE_GRAPH_ADDR ((unsigned long)ftrace_graph_caller)
601 #endif
602
603 #ifndef FTRACE_REGS_ADDR
604 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
605 # define FTRACE_REGS_ADDR ((unsigned long)ftrace_regs_caller)
606 #else
607 # define FTRACE_REGS_ADDR FTRACE_ADDR
608 #endif
609 #endif
610
611 /*
612  * If an arch would like functions that are only traced
613  * by the function graph tracer to jump directly to its own
614  * trampoline, then they can define FTRACE_GRAPH_TRAMP_ADDR
615  * to be that address to jump to.
616  */
617 #ifndef FTRACE_GRAPH_TRAMP_ADDR
618 #define FTRACE_GRAPH_TRAMP_ADDR ((unsigned long) 0)
619 #endif
620
621 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
622 extern void ftrace_graph_caller(void);
623 extern int ftrace_enable_ftrace_graph_caller(void);
624 extern int ftrace_disable_ftrace_graph_caller(void);
625 #else
626 static inline int ftrace_enable_ftrace_graph_caller(void) { return 0; }
627 static inline int ftrace_disable_ftrace_graph_caller(void) { return 0; }
628 #endif
629
630 /**
631  * ftrace_make_nop - convert code into nop
632  * @mod: module structure if called by module load initialization
633  * @rec: the call site record (e.g. mcount/fentry)
634  * @addr: the address that the call site should be calling
635  *
636  * This is a very sensitive operation and great care needs
637  * to be taken by the arch.  The operation should carefully
638  * read the location, check to see if what is read is indeed
639  * what we expect it to be, and then on success of the compare,
640  * it should write to the location.
641  *
642  * The code segment at @rec->ip should be a caller to @addr
643  *
644  * Return must be:
645  *  0 on success
646  *  -EFAULT on error reading the location
647  *  -EINVAL on a failed compare of the contents
648  *  -EPERM  on error writing to the location
649  * Any other value will be considered a failure.
650  */
651 extern int ftrace_make_nop(struct module *mod,
652                            struct dyn_ftrace *rec, unsigned long addr);
653
654 /**
655  * ftrace_need_init_nop - return whether nop call sites should be initialized
656  *
657  * Normally the compiler's -mnop-mcount generates suitable nops, so we don't
658  * need to call ftrace_init_nop() if the code is built with that flag.
659  * Architectures where this is not always the case may define their own
660  * condition.
661  *
662  * Return must be:
663  *  0       if ftrace_init_nop() should be called
664  *  Nonzero if ftrace_init_nop() should not be called
665  */
666
667 #ifndef ftrace_need_init_nop
668 #define ftrace_need_init_nop() (!__is_defined(CC_USING_NOP_MCOUNT))
669 #endif
670
671 /**
672  * ftrace_init_nop - initialize a nop call site
673  * @mod: module structure if called by module load initialization
674  * @rec: the call site record (e.g. mcount/fentry)
675  *
676  * This is a very sensitive operation and great care needs
677  * to be taken by the arch.  The operation should carefully
678  * read the location, check to see if what is read is indeed
679  * what we expect it to be, and then on success of the compare,
680  * it should write to the location.
681  *
682  * The code segment at @rec->ip should contain the contents created by
683  * the compiler
684  *
685  * Return must be:
686  *  0 on success
687  *  -EFAULT on error reading the location
688  *  -EINVAL on a failed compare of the contents
689  *  -EPERM  on error writing to the location
690  * Any other value will be considered a failure.
691  */
692 #ifndef ftrace_init_nop
693 static inline int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
694 {
695         return ftrace_make_nop(mod, rec, MCOUNT_ADDR);
696 }
697 #endif
698
699 /**
700  * ftrace_make_call - convert a nop call site into a call to addr
701  * @rec: the call site record (e.g. mcount/fentry)
702  * @addr: the address that the call site should call
703  *
704  * This is a very sensitive operation and great care needs
705  * to be taken by the arch.  The operation should carefully
706  * read the location, check to see if what is read is indeed
707  * what we expect it to be, and then on success of the compare,
708  * it should write to the location.
709  *
710  * The code segment at @rec->ip should be a nop
711  *
712  * Return must be:
713  *  0 on success
714  *  -EFAULT on error reading the location
715  *  -EINVAL on a failed compare of the contents
716  *  -EPERM  on error writing to the location
717  * Any other value will be considered a failure.
718  */
719 extern int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr);
720
721 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
722 /**
723  * ftrace_modify_call - convert from one addr to another (no nop)
724  * @rec: the call site record (e.g. mcount/fentry)
725  * @old_addr: the address expected to be currently called to
726  * @addr: the address to change to
727  *
728  * This is a very sensitive operation and great care needs
729  * to be taken by the arch.  The operation should carefully
730  * read the location, check to see if what is read is indeed
731  * what we expect it to be, and then on success of the compare,
732  * it should write to the location.
733  *
734  * The code segment at @rec->ip should be a caller to @old_addr
735  *
736  * Return must be:
737  *  0 on success
738  *  -EFAULT on error reading the location
739  *  -EINVAL on a failed compare of the contents
740  *  -EPERM  on error writing to the location
741  * Any other value will be considered a failure.
742  */
743 extern int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
744                               unsigned long addr);
745 #else
746 /* Should never be called */
747 static inline int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
748                                      unsigned long addr)
749 {
750         return -EINVAL;
751 }
752 #endif
753
754 /* May be defined in arch */
755 extern int ftrace_arch_read_dyn_info(char *buf, int size);
756
757 extern int skip_trace(unsigned long ip);
758 extern void ftrace_module_init(struct module *mod);
759 extern void ftrace_module_enable(struct module *mod);
760 extern void ftrace_release_mod(struct module *mod);
761
762 extern void ftrace_disable_daemon(void);
763 extern void ftrace_enable_daemon(void);
764 #else /* CONFIG_DYNAMIC_FTRACE */
765 static inline int skip_trace(unsigned long ip) { return 0; }
766 static inline void ftrace_disable_daemon(void) { }
767 static inline void ftrace_enable_daemon(void) { }
768 static inline void ftrace_module_init(struct module *mod) { }
769 static inline void ftrace_module_enable(struct module *mod) { }
770 static inline void ftrace_release_mod(struct module *mod) { }
771 static inline int ftrace_text_reserved(const void *start, const void *end)
772 {
773         return 0;
774 }
775 static inline unsigned long ftrace_location(unsigned long ip)
776 {
777         return 0;
778 }
779
780 /*
781  * Again users of functions that have ftrace_ops may not
782  * have them defined when ftrace is not enabled, but these
783  * functions may still be called. Use a macro instead of inline.
784  */
785 #define ftrace_regex_open(ops, flag, inod, file) ({ -ENODEV; })
786 #define ftrace_set_early_filter(ops, buf, enable) do { } while (0)
787 #define ftrace_set_filter_ip(ops, ip, remove, reset) ({ -ENODEV; })
788 #define ftrace_set_filter(ops, buf, len, reset) ({ -ENODEV; })
789 #define ftrace_set_notrace(ops, buf, len, reset) ({ -ENODEV; })
790 #define ftrace_free_filter(ops) do { } while (0)
791 #define ftrace_ops_set_global_filter(ops) do { } while (0)
792
793 static inline ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
794                             size_t cnt, loff_t *ppos) { return -ENODEV; }
795 static inline ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf,
796                              size_t cnt, loff_t *ppos) { return -ENODEV; }
797 static inline int
798 ftrace_regex_release(struct inode *inode, struct file *file) { return -ENODEV; }
799
800 static inline bool is_ftrace_trampoline(unsigned long addr)
801 {
802         return false;
803 }
804 #endif /* CONFIG_DYNAMIC_FTRACE */
805
806 /* totally disable ftrace - can not re-enable after this */
807 void ftrace_kill(void);
808
809 static inline void tracer_disable(void)
810 {
811 #ifdef CONFIG_FUNCTION_TRACER
812         ftrace_enabled = 0;
813 #endif
814 }
815
816 /*
817  * Ftrace disable/restore without lock. Some synchronization mechanism
818  * must be used to prevent ftrace_enabled to be changed between
819  * disable/restore.
820  */
821 static inline int __ftrace_enabled_save(void)
822 {
823 #ifdef CONFIG_FUNCTION_TRACER
824         int saved_ftrace_enabled = ftrace_enabled;
825         ftrace_enabled = 0;
826         return saved_ftrace_enabled;
827 #else
828         return 0;
829 #endif
830 }
831
832 static inline void __ftrace_enabled_restore(int enabled)
833 {
834 #ifdef CONFIG_FUNCTION_TRACER
835         ftrace_enabled = enabled;
836 #endif
837 }
838
839 /* All archs should have this, but we define it for consistency */
840 #ifndef ftrace_return_address0
841 # define ftrace_return_address0 __builtin_return_address(0)
842 #endif
843
844 /* Archs may use other ways for ADDR1 and beyond */
845 #ifndef ftrace_return_address
846 # ifdef CONFIG_FRAME_POINTER
847 #  define ftrace_return_address(n) __builtin_return_address(n)
848 # else
849 #  define ftrace_return_address(n) 0UL
850 # endif
851 #endif
852
853 #define CALLER_ADDR0 ((unsigned long)ftrace_return_address0)
854 #define CALLER_ADDR1 ((unsigned long)ftrace_return_address(1))
855 #define CALLER_ADDR2 ((unsigned long)ftrace_return_address(2))
856 #define CALLER_ADDR3 ((unsigned long)ftrace_return_address(3))
857 #define CALLER_ADDR4 ((unsigned long)ftrace_return_address(4))
858 #define CALLER_ADDR5 ((unsigned long)ftrace_return_address(5))
859 #define CALLER_ADDR6 ((unsigned long)ftrace_return_address(6))
860
861 static inline unsigned long get_lock_parent_ip(void)
862 {
863         unsigned long addr = CALLER_ADDR0;
864
865         if (!in_lock_functions(addr))
866                 return addr;
867         addr = CALLER_ADDR1;
868         if (!in_lock_functions(addr))
869                 return addr;
870         return CALLER_ADDR2;
871 }
872
873 #ifdef CONFIG_TRACE_PREEMPT_TOGGLE
874   extern void trace_preempt_on(unsigned long a0, unsigned long a1);
875   extern void trace_preempt_off(unsigned long a0, unsigned long a1);
876 #else
877 /*
878  * Use defines instead of static inlines because some arches will make code out
879  * of the CALLER_ADDR, when we really want these to be a real nop.
880  */
881 # define trace_preempt_on(a0, a1) do { } while (0)
882 # define trace_preempt_off(a0, a1) do { } while (0)
883 #endif
884
885 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
886 extern void ftrace_init(void);
887 #ifdef CC_USING_PATCHABLE_FUNCTION_ENTRY
888 #define FTRACE_CALLSITE_SECTION "__patchable_function_entries"
889 #else
890 #define FTRACE_CALLSITE_SECTION "__mcount_loc"
891 #endif
892 #else
893 static inline void ftrace_init(void) { }
894 #endif
895
896 /*
897  * Structure that defines an entry function trace.
898  * It's already packed but the attribute "packed" is needed
899  * to remove extra padding at the end.
900  */
901 struct ftrace_graph_ent {
902         unsigned long func; /* Current function */
903         int depth;
904 } __packed;
905
906 /*
907  * Structure that defines a return function trace.
908  * It's already packed but the attribute "packed" is needed
909  * to remove extra padding at the end.
910  */
911 struct ftrace_graph_ret {
912         unsigned long func; /* Current function */
913         int depth;
914         /* Number of functions that overran the depth limit for current task */
915         unsigned int overrun;
916         unsigned long long calltime;
917         unsigned long long rettime;
918 } __packed;
919
920 /* Type of the callback handlers for tracing function graph*/
921 typedef void (*trace_func_graph_ret_t)(struct ftrace_graph_ret *); /* return */
922 typedef int (*trace_func_graph_ent_t)(struct ftrace_graph_ent *); /* entry */
923
924 extern int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace);
925
926 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
927
928 struct fgraph_ops {
929         trace_func_graph_ent_t          entryfunc;
930         trace_func_graph_ret_t          retfunc;
931 };
932
933 /*
934  * Stack of return addresses for functions
935  * of a thread.
936  * Used in struct thread_info
937  */
938 struct ftrace_ret_stack {
939         unsigned long ret;
940         unsigned long func;
941         unsigned long long calltime;
942 #ifdef CONFIG_FUNCTION_PROFILER
943         unsigned long long subtime;
944 #endif
945 #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
946         unsigned long fp;
947 #endif
948 #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
949         unsigned long *retp;
950 #endif
951 };
952
953 /*
954  * Primary handler of a function return.
955  * It relays on ftrace_return_to_handler.
956  * Defined in entry_32/64.S
957  */
958 extern void return_to_handler(void);
959
960 extern int
961 function_graph_enter(unsigned long ret, unsigned long func,
962                      unsigned long frame_pointer, unsigned long *retp);
963
964 struct ftrace_ret_stack *
965 ftrace_graph_get_ret_stack(struct task_struct *task, int idx);
966
967 unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
968                                     unsigned long ret, unsigned long *retp);
969
970 /*
971  * Sometimes we don't want to trace a function with the function
972  * graph tracer but we want them to keep traced by the usual function
973  * tracer if the function graph tracer is not configured.
974  */
975 #define __notrace_funcgraph             notrace
976
977 #define FTRACE_RETFUNC_DEPTH 50
978 #define FTRACE_RETSTACK_ALLOC_SIZE 32
979
980 extern int register_ftrace_graph(struct fgraph_ops *ops);
981 extern void unregister_ftrace_graph(struct fgraph_ops *ops);
982
983 extern bool ftrace_graph_is_dead(void);
984 extern void ftrace_graph_stop(void);
985
986 /* The current handlers in use */
987 extern trace_func_graph_ret_t ftrace_graph_return;
988 extern trace_func_graph_ent_t ftrace_graph_entry;
989
990 extern void ftrace_graph_init_task(struct task_struct *t);
991 extern void ftrace_graph_exit_task(struct task_struct *t);
992 extern void ftrace_graph_init_idle_task(struct task_struct *t, int cpu);
993
994 static inline void pause_graph_tracing(void)
995 {
996         atomic_inc(&current->tracing_graph_pause);
997 }
998
999 static inline void unpause_graph_tracing(void)
1000 {
1001         atomic_dec(&current->tracing_graph_pause);
1002 }
1003 #else /* !CONFIG_FUNCTION_GRAPH_TRACER */
1004
1005 #define __notrace_funcgraph
1006
1007 static inline void ftrace_graph_init_task(struct task_struct *t) { }
1008 static inline void ftrace_graph_exit_task(struct task_struct *t) { }
1009 static inline void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) { }
1010
1011 /* Define as macros as fgraph_ops may not be defined */
1012 #define register_ftrace_graph(ops) ({ -1; })
1013 #define unregister_ftrace_graph(ops) do { } while (0)
1014
1015 static inline unsigned long
1016 ftrace_graph_ret_addr(struct task_struct *task, int *idx, unsigned long ret,
1017                       unsigned long *retp)
1018 {
1019         return ret;
1020 }
1021
1022 static inline void pause_graph_tracing(void) { }
1023 static inline void unpause_graph_tracing(void) { }
1024 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1025
1026 #ifdef CONFIG_TRACING
1027
1028 /* flags for current->trace */
1029 enum {
1030         TSK_TRACE_FL_TRACE_BIT  = 0,
1031         TSK_TRACE_FL_GRAPH_BIT  = 1,
1032 };
1033 enum {
1034         TSK_TRACE_FL_TRACE      = 1 << TSK_TRACE_FL_TRACE_BIT,
1035         TSK_TRACE_FL_GRAPH      = 1 << TSK_TRACE_FL_GRAPH_BIT,
1036 };
1037
1038 static inline void set_tsk_trace_trace(struct task_struct *tsk)
1039 {
1040         set_bit(TSK_TRACE_FL_TRACE_BIT, &tsk->trace);
1041 }
1042
1043 static inline void clear_tsk_trace_trace(struct task_struct *tsk)
1044 {
1045         clear_bit(TSK_TRACE_FL_TRACE_BIT, &tsk->trace);
1046 }
1047
1048 static inline int test_tsk_trace_trace(struct task_struct *tsk)
1049 {
1050         return tsk->trace & TSK_TRACE_FL_TRACE;
1051 }
1052
1053 static inline void set_tsk_trace_graph(struct task_struct *tsk)
1054 {
1055         set_bit(TSK_TRACE_FL_GRAPH_BIT, &tsk->trace);
1056 }
1057
1058 static inline void clear_tsk_trace_graph(struct task_struct *tsk)
1059 {
1060         clear_bit(TSK_TRACE_FL_GRAPH_BIT, &tsk->trace);
1061 }
1062
1063 static inline int test_tsk_trace_graph(struct task_struct *tsk)
1064 {
1065         return tsk->trace & TSK_TRACE_FL_GRAPH;
1066 }
1067
1068 enum ftrace_dump_mode;
1069
1070 extern enum ftrace_dump_mode ftrace_dump_on_oops;
1071 extern int tracepoint_printk;
1072
1073 extern void disable_trace_on_warning(void);
1074 extern int __disable_trace_on_warning;
1075
1076 int tracepoint_printk_sysctl(struct ctl_table *table, int write,
1077                              void *buffer, size_t *lenp, loff_t *ppos);
1078
1079 #else /* CONFIG_TRACING */
1080 static inline void  disable_trace_on_warning(void) { }
1081 #endif /* CONFIG_TRACING */
1082
1083 #ifdef CONFIG_FTRACE_SYSCALLS
1084
1085 unsigned long arch_syscall_addr(int nr);
1086
1087 #endif /* CONFIG_FTRACE_SYSCALLS */
1088
1089 #endif /* _LINUX_FTRACE_H */