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