162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * NOTE: 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * This header has combined a lot of unrelated to each other stuff. 662306a36Sopenharmony_ci * The process of splitting its content is in progress while keeping 762306a36Sopenharmony_ci * backward compatibility. That's why it's highly recommended NOT to 862306a36Sopenharmony_ci * include this header inside another header file, especially under 962306a36Sopenharmony_ci * generic or architectural include/ directory. 1062306a36Sopenharmony_ci */ 1162306a36Sopenharmony_ci#ifndef _LINUX_KERNEL_H 1262306a36Sopenharmony_ci#define _LINUX_KERNEL_H 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci#include <linux/stdarg.h> 1562306a36Sopenharmony_ci#include <linux/align.h> 1662306a36Sopenharmony_ci#include <linux/limits.h> 1762306a36Sopenharmony_ci#include <linux/linkage.h> 1862306a36Sopenharmony_ci#include <linux/stddef.h> 1962306a36Sopenharmony_ci#include <linux/types.h> 2062306a36Sopenharmony_ci#include <linux/compiler.h> 2162306a36Sopenharmony_ci#include <linux/container_of.h> 2262306a36Sopenharmony_ci#include <linux/bitops.h> 2362306a36Sopenharmony_ci#include <linux/hex.h> 2462306a36Sopenharmony_ci#include <linux/kstrtox.h> 2562306a36Sopenharmony_ci#include <linux/log2.h> 2662306a36Sopenharmony_ci#include <linux/math.h> 2762306a36Sopenharmony_ci#include <linux/minmax.h> 2862306a36Sopenharmony_ci#include <linux/typecheck.h> 2962306a36Sopenharmony_ci#include <linux/panic.h> 3062306a36Sopenharmony_ci#include <linux/printk.h> 3162306a36Sopenharmony_ci#include <linux/build_bug.h> 3262306a36Sopenharmony_ci#include <linux/sprintf.h> 3362306a36Sopenharmony_ci#include <linux/static_call_types.h> 3462306a36Sopenharmony_ci#include <linux/instruction_pointer.h> 3562306a36Sopenharmony_ci#include <asm/byteorder.h> 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci#include <uapi/linux/kernel.h> 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ci#define STACK_MAGIC 0xdeadbeef 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci/** 4262306a36Sopenharmony_ci * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value 4362306a36Sopenharmony_ci * @x: value to repeat 4462306a36Sopenharmony_ci * 4562306a36Sopenharmony_ci * NOTE: @x is not checked for > 0xff; larger values produce odd results. 4662306a36Sopenharmony_ci */ 4762306a36Sopenharmony_ci#define REPEAT_BYTE(x) ((~0ul / 0xff) * (x)) 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci/* generic data direction definitions */ 5062306a36Sopenharmony_ci#define READ 0 5162306a36Sopenharmony_ci#define WRITE 1 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ci/** 5462306a36Sopenharmony_ci * ARRAY_SIZE - get the number of elements in array @arr 5562306a36Sopenharmony_ci * @arr: array to be sized 5662306a36Sopenharmony_ci */ 5762306a36Sopenharmony_ci#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ci#define PTR_IF(cond, ptr) ((cond) ? (ptr) : NULL) 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ci#define u64_to_user_ptr(x) ( \ 6262306a36Sopenharmony_ci{ \ 6362306a36Sopenharmony_ci typecheck(u64, (x)); \ 6462306a36Sopenharmony_ci (void __user *)(uintptr_t)(x); \ 6562306a36Sopenharmony_ci} \ 6662306a36Sopenharmony_ci) 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci/** 6962306a36Sopenharmony_ci * upper_32_bits - return bits 32-63 of a number 7062306a36Sopenharmony_ci * @n: the number we're accessing 7162306a36Sopenharmony_ci * 7262306a36Sopenharmony_ci * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress 7362306a36Sopenharmony_ci * the "right shift count >= width of type" warning when that quantity is 7462306a36Sopenharmony_ci * 32-bits. 7562306a36Sopenharmony_ci */ 7662306a36Sopenharmony_ci#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci/** 7962306a36Sopenharmony_ci * lower_32_bits - return bits 0-31 of a number 8062306a36Sopenharmony_ci * @n: the number we're accessing 8162306a36Sopenharmony_ci */ 8262306a36Sopenharmony_ci#define lower_32_bits(n) ((u32)((n) & 0xffffffff)) 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci/** 8562306a36Sopenharmony_ci * upper_16_bits - return bits 16-31 of a number 8662306a36Sopenharmony_ci * @n: the number we're accessing 8762306a36Sopenharmony_ci */ 8862306a36Sopenharmony_ci#define upper_16_bits(n) ((u16)((n) >> 16)) 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci/** 9162306a36Sopenharmony_ci * lower_16_bits - return bits 0-15 of a number 9262306a36Sopenharmony_ci * @n: the number we're accessing 9362306a36Sopenharmony_ci */ 9462306a36Sopenharmony_ci#define lower_16_bits(n) ((u16)((n) & 0xffff)) 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_cistruct completion; 9762306a36Sopenharmony_cistruct user; 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci#ifdef CONFIG_PREEMPT_VOLUNTARY_BUILD 10062306a36Sopenharmony_ci 10162306a36Sopenharmony_ciextern int __cond_resched(void); 10262306a36Sopenharmony_ci# define might_resched() __cond_resched() 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ci#elif defined(CONFIG_PREEMPT_DYNAMIC) && defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL) 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ciextern int __cond_resched(void); 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ciDECLARE_STATIC_CALL(might_resched, __cond_resched); 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_cistatic __always_inline void might_resched(void) 11162306a36Sopenharmony_ci{ 11262306a36Sopenharmony_ci static_call_mod(might_resched)(); 11362306a36Sopenharmony_ci} 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci#elif defined(CONFIG_PREEMPT_DYNAMIC) && defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY) 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ciextern int dynamic_might_resched(void); 11862306a36Sopenharmony_ci# define might_resched() dynamic_might_resched() 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ci#else 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci# define might_resched() do { } while (0) 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci#endif /* CONFIG_PREEMPT_* */ 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ci#ifdef CONFIG_DEBUG_ATOMIC_SLEEP 12762306a36Sopenharmony_ciextern void __might_resched(const char *file, int line, unsigned int offsets); 12862306a36Sopenharmony_ciextern void __might_sleep(const char *file, int line); 12962306a36Sopenharmony_ciextern void __cant_sleep(const char *file, int line, int preempt_offset); 13062306a36Sopenharmony_ciextern void __cant_migrate(const char *file, int line); 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci/** 13362306a36Sopenharmony_ci * might_sleep - annotation for functions that can sleep 13462306a36Sopenharmony_ci * 13562306a36Sopenharmony_ci * this macro will print a stack trace if it is executed in an atomic 13662306a36Sopenharmony_ci * context (spinlock, irq-handler, ...). Additional sections where blocking is 13762306a36Sopenharmony_ci * not allowed can be annotated with non_block_start() and non_block_end() 13862306a36Sopenharmony_ci * pairs. 13962306a36Sopenharmony_ci * 14062306a36Sopenharmony_ci * This is a useful debugging help to be able to catch problems early and not 14162306a36Sopenharmony_ci * be bitten later when the calling function happens to sleep when it is not 14262306a36Sopenharmony_ci * supposed to. 14362306a36Sopenharmony_ci */ 14462306a36Sopenharmony_ci# define might_sleep() \ 14562306a36Sopenharmony_ci do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0) 14662306a36Sopenharmony_ci/** 14762306a36Sopenharmony_ci * cant_sleep - annotation for functions that cannot sleep 14862306a36Sopenharmony_ci * 14962306a36Sopenharmony_ci * this macro will print a stack trace if it is executed with preemption enabled 15062306a36Sopenharmony_ci */ 15162306a36Sopenharmony_ci# define cant_sleep() \ 15262306a36Sopenharmony_ci do { __cant_sleep(__FILE__, __LINE__, 0); } while (0) 15362306a36Sopenharmony_ci# define sched_annotate_sleep() (current->task_state_change = 0) 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci/** 15662306a36Sopenharmony_ci * cant_migrate - annotation for functions that cannot migrate 15762306a36Sopenharmony_ci * 15862306a36Sopenharmony_ci * Will print a stack trace if executed in code which is migratable 15962306a36Sopenharmony_ci */ 16062306a36Sopenharmony_ci# define cant_migrate() \ 16162306a36Sopenharmony_ci do { \ 16262306a36Sopenharmony_ci if (IS_ENABLED(CONFIG_SMP)) \ 16362306a36Sopenharmony_ci __cant_migrate(__FILE__, __LINE__); \ 16462306a36Sopenharmony_ci } while (0) 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_ci/** 16762306a36Sopenharmony_ci * non_block_start - annotate the start of section where sleeping is prohibited 16862306a36Sopenharmony_ci * 16962306a36Sopenharmony_ci * This is on behalf of the oom reaper, specifically when it is calling the mmu 17062306a36Sopenharmony_ci * notifiers. The problem is that if the notifier were to block on, for example, 17162306a36Sopenharmony_ci * mutex_lock() and if the process which holds that mutex were to perform a 17262306a36Sopenharmony_ci * sleeping memory allocation, the oom reaper is now blocked on completion of 17362306a36Sopenharmony_ci * that memory allocation. Other blocking calls like wait_event() pose similar 17462306a36Sopenharmony_ci * issues. 17562306a36Sopenharmony_ci */ 17662306a36Sopenharmony_ci# define non_block_start() (current->non_block_count++) 17762306a36Sopenharmony_ci/** 17862306a36Sopenharmony_ci * non_block_end - annotate the end of section where sleeping is prohibited 17962306a36Sopenharmony_ci * 18062306a36Sopenharmony_ci * Closes a section opened by non_block_start(). 18162306a36Sopenharmony_ci */ 18262306a36Sopenharmony_ci# define non_block_end() WARN_ON(current->non_block_count-- == 0) 18362306a36Sopenharmony_ci#else 18462306a36Sopenharmony_ci static inline void __might_resched(const char *file, int line, 18562306a36Sopenharmony_ci unsigned int offsets) { } 18662306a36Sopenharmony_cistatic inline void __might_sleep(const char *file, int line) { } 18762306a36Sopenharmony_ci# define might_sleep() do { might_resched(); } while (0) 18862306a36Sopenharmony_ci# define cant_sleep() do { } while (0) 18962306a36Sopenharmony_ci# define cant_migrate() do { } while (0) 19062306a36Sopenharmony_ci# define sched_annotate_sleep() do { } while (0) 19162306a36Sopenharmony_ci# define non_block_start() do { } while (0) 19262306a36Sopenharmony_ci# define non_block_end() do { } while (0) 19362306a36Sopenharmony_ci#endif 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_ci#define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0) 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ci#if defined(CONFIG_MMU) && \ 19862306a36Sopenharmony_ci (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)) 19962306a36Sopenharmony_ci#define might_fault() __might_fault(__FILE__, __LINE__) 20062306a36Sopenharmony_civoid __might_fault(const char *file, int line); 20162306a36Sopenharmony_ci#else 20262306a36Sopenharmony_cistatic inline void might_fault(void) { } 20362306a36Sopenharmony_ci#endif 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_civoid do_exit(long error_code) __noreturn; 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ciextern int get_option(char **str, int *pint); 20862306a36Sopenharmony_ciextern char *get_options(const char *str, int nints, int *ints); 20962306a36Sopenharmony_ciextern unsigned long long memparse(const char *ptr, char **retptr); 21062306a36Sopenharmony_ciextern bool parse_option_str(const char *str, const char *option); 21162306a36Sopenharmony_ciextern char *next_arg(char *args, char **param, char **val); 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_ciextern int core_kernel_text(unsigned long addr); 21462306a36Sopenharmony_ciextern int __kernel_text_address(unsigned long addr); 21562306a36Sopenharmony_ciextern int kernel_text_address(unsigned long addr); 21662306a36Sopenharmony_ciextern int func_ptr_is_kernel_text(void *ptr); 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ciextern void bust_spinlocks(int yes); 21962306a36Sopenharmony_ci 22062306a36Sopenharmony_ciextern int root_mountflags; 22162306a36Sopenharmony_ci 22262306a36Sopenharmony_ciextern bool early_boot_irqs_disabled; 22362306a36Sopenharmony_ci 22462306a36Sopenharmony_ci/* 22562306a36Sopenharmony_ci * Values used for system_state. Ordering of the states must not be changed 22662306a36Sopenharmony_ci * as code checks for <, <=, >, >= STATE. 22762306a36Sopenharmony_ci */ 22862306a36Sopenharmony_ciextern enum system_states { 22962306a36Sopenharmony_ci SYSTEM_BOOTING, 23062306a36Sopenharmony_ci SYSTEM_SCHEDULING, 23162306a36Sopenharmony_ci SYSTEM_FREEING_INITMEM, 23262306a36Sopenharmony_ci SYSTEM_RUNNING, 23362306a36Sopenharmony_ci SYSTEM_HALT, 23462306a36Sopenharmony_ci SYSTEM_POWER_OFF, 23562306a36Sopenharmony_ci SYSTEM_RESTART, 23662306a36Sopenharmony_ci SYSTEM_SUSPEND, 23762306a36Sopenharmony_ci} system_state; 23862306a36Sopenharmony_ci 23962306a36Sopenharmony_ci/* 24062306a36Sopenharmony_ci * General tracing related utility functions - trace_printk(), 24162306a36Sopenharmony_ci * tracing_on/tracing_off and tracing_start()/tracing_stop 24262306a36Sopenharmony_ci * 24362306a36Sopenharmony_ci * Use tracing_on/tracing_off when you want to quickly turn on or off 24462306a36Sopenharmony_ci * tracing. It simply enables or disables the recording of the trace events. 24562306a36Sopenharmony_ci * This also corresponds to the user space /sys/kernel/tracing/tracing_on 24662306a36Sopenharmony_ci * file, which gives a means for the kernel and userspace to interact. 24762306a36Sopenharmony_ci * Place a tracing_off() in the kernel where you want tracing to end. 24862306a36Sopenharmony_ci * From user space, examine the trace, and then echo 1 > tracing_on 24962306a36Sopenharmony_ci * to continue tracing. 25062306a36Sopenharmony_ci * 25162306a36Sopenharmony_ci * tracing_stop/tracing_start has slightly more overhead. It is used 25262306a36Sopenharmony_ci * by things like suspend to ram where disabling the recording of the 25362306a36Sopenharmony_ci * trace is not enough, but tracing must actually stop because things 25462306a36Sopenharmony_ci * like calling smp_processor_id() may crash the system. 25562306a36Sopenharmony_ci * 25662306a36Sopenharmony_ci * Most likely, you want to use tracing_on/tracing_off. 25762306a36Sopenharmony_ci */ 25862306a36Sopenharmony_ci 25962306a36Sopenharmony_cienum ftrace_dump_mode { 26062306a36Sopenharmony_ci DUMP_NONE, 26162306a36Sopenharmony_ci DUMP_ALL, 26262306a36Sopenharmony_ci DUMP_ORIG, 26362306a36Sopenharmony_ci}; 26462306a36Sopenharmony_ci 26562306a36Sopenharmony_ci#ifdef CONFIG_TRACING 26662306a36Sopenharmony_civoid tracing_on(void); 26762306a36Sopenharmony_civoid tracing_off(void); 26862306a36Sopenharmony_ciint tracing_is_on(void); 26962306a36Sopenharmony_civoid tracing_snapshot(void); 27062306a36Sopenharmony_civoid tracing_snapshot_alloc(void); 27162306a36Sopenharmony_ci 27262306a36Sopenharmony_ciextern void tracing_start(void); 27362306a36Sopenharmony_ciextern void tracing_stop(void); 27462306a36Sopenharmony_ci 27562306a36Sopenharmony_cistatic inline __printf(1, 2) 27662306a36Sopenharmony_civoid ____trace_printk_check_format(const char *fmt, ...) 27762306a36Sopenharmony_ci{ 27862306a36Sopenharmony_ci} 27962306a36Sopenharmony_ci#define __trace_printk_check_format(fmt, args...) \ 28062306a36Sopenharmony_cido { \ 28162306a36Sopenharmony_ci if (0) \ 28262306a36Sopenharmony_ci ____trace_printk_check_format(fmt, ##args); \ 28362306a36Sopenharmony_ci} while (0) 28462306a36Sopenharmony_ci 28562306a36Sopenharmony_ci/** 28662306a36Sopenharmony_ci * trace_printk - printf formatting in the ftrace buffer 28762306a36Sopenharmony_ci * @fmt: the printf format for printing 28862306a36Sopenharmony_ci * 28962306a36Sopenharmony_ci * Note: __trace_printk is an internal function for trace_printk() and 29062306a36Sopenharmony_ci * the @ip is passed in via the trace_printk() macro. 29162306a36Sopenharmony_ci * 29262306a36Sopenharmony_ci * This function allows a kernel developer to debug fast path sections 29362306a36Sopenharmony_ci * that printk is not appropriate for. By scattering in various 29462306a36Sopenharmony_ci * printk like tracing in the code, a developer can quickly see 29562306a36Sopenharmony_ci * where problems are occurring. 29662306a36Sopenharmony_ci * 29762306a36Sopenharmony_ci * This is intended as a debugging tool for the developer only. 29862306a36Sopenharmony_ci * Please refrain from leaving trace_printks scattered around in 29962306a36Sopenharmony_ci * your code. (Extra memory is used for special buffers that are 30062306a36Sopenharmony_ci * allocated when trace_printk() is used.) 30162306a36Sopenharmony_ci * 30262306a36Sopenharmony_ci * A little optimization trick is done here. If there's only one 30362306a36Sopenharmony_ci * argument, there's no need to scan the string for printf formats. 30462306a36Sopenharmony_ci * The trace_puts() will suffice. But how can we take advantage of 30562306a36Sopenharmony_ci * using trace_puts() when trace_printk() has only one argument? 30662306a36Sopenharmony_ci * By stringifying the args and checking the size we can tell 30762306a36Sopenharmony_ci * whether or not there are args. __stringify((__VA_ARGS__)) will 30862306a36Sopenharmony_ci * turn into "()\0" with a size of 3 when there are no args, anything 30962306a36Sopenharmony_ci * else will be bigger. All we need to do is define a string to this, 31062306a36Sopenharmony_ci * and then take its size and compare to 3. If it's bigger, use 31162306a36Sopenharmony_ci * do_trace_printk() otherwise, optimize it to trace_puts(). Then just 31262306a36Sopenharmony_ci * let gcc optimize the rest. 31362306a36Sopenharmony_ci */ 31462306a36Sopenharmony_ci 31562306a36Sopenharmony_ci#define trace_printk(fmt, ...) \ 31662306a36Sopenharmony_cido { \ 31762306a36Sopenharmony_ci char _______STR[] = __stringify((__VA_ARGS__)); \ 31862306a36Sopenharmony_ci if (sizeof(_______STR) > 3) \ 31962306a36Sopenharmony_ci do_trace_printk(fmt, ##__VA_ARGS__); \ 32062306a36Sopenharmony_ci else \ 32162306a36Sopenharmony_ci trace_puts(fmt); \ 32262306a36Sopenharmony_ci} while (0) 32362306a36Sopenharmony_ci 32462306a36Sopenharmony_ci#define do_trace_printk(fmt, args...) \ 32562306a36Sopenharmony_cido { \ 32662306a36Sopenharmony_ci static const char *trace_printk_fmt __used \ 32762306a36Sopenharmony_ci __section("__trace_printk_fmt") = \ 32862306a36Sopenharmony_ci __builtin_constant_p(fmt) ? fmt : NULL; \ 32962306a36Sopenharmony_ci \ 33062306a36Sopenharmony_ci __trace_printk_check_format(fmt, ##args); \ 33162306a36Sopenharmony_ci \ 33262306a36Sopenharmony_ci if (__builtin_constant_p(fmt)) \ 33362306a36Sopenharmony_ci __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \ 33462306a36Sopenharmony_ci else \ 33562306a36Sopenharmony_ci __trace_printk(_THIS_IP_, fmt, ##args); \ 33662306a36Sopenharmony_ci} while (0) 33762306a36Sopenharmony_ci 33862306a36Sopenharmony_ciextern __printf(2, 3) 33962306a36Sopenharmony_ciint __trace_bprintk(unsigned long ip, const char *fmt, ...); 34062306a36Sopenharmony_ci 34162306a36Sopenharmony_ciextern __printf(2, 3) 34262306a36Sopenharmony_ciint __trace_printk(unsigned long ip, const char *fmt, ...); 34362306a36Sopenharmony_ci 34462306a36Sopenharmony_ci/** 34562306a36Sopenharmony_ci * trace_puts - write a string into the ftrace buffer 34662306a36Sopenharmony_ci * @str: the string to record 34762306a36Sopenharmony_ci * 34862306a36Sopenharmony_ci * Note: __trace_bputs is an internal function for trace_puts and 34962306a36Sopenharmony_ci * the @ip is passed in via the trace_puts macro. 35062306a36Sopenharmony_ci * 35162306a36Sopenharmony_ci * This is similar to trace_printk() but is made for those really fast 35262306a36Sopenharmony_ci * paths that a developer wants the least amount of "Heisenbug" effects, 35362306a36Sopenharmony_ci * where the processing of the print format is still too much. 35462306a36Sopenharmony_ci * 35562306a36Sopenharmony_ci * This function allows a kernel developer to debug fast path sections 35662306a36Sopenharmony_ci * that printk is not appropriate for. By scattering in various 35762306a36Sopenharmony_ci * printk like tracing in the code, a developer can quickly see 35862306a36Sopenharmony_ci * where problems are occurring. 35962306a36Sopenharmony_ci * 36062306a36Sopenharmony_ci * This is intended as a debugging tool for the developer only. 36162306a36Sopenharmony_ci * Please refrain from leaving trace_puts scattered around in 36262306a36Sopenharmony_ci * your code. (Extra memory is used for special buffers that are 36362306a36Sopenharmony_ci * allocated when trace_puts() is used.) 36462306a36Sopenharmony_ci * 36562306a36Sopenharmony_ci * Returns: 0 if nothing was written, positive # if string was. 36662306a36Sopenharmony_ci * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used) 36762306a36Sopenharmony_ci */ 36862306a36Sopenharmony_ci 36962306a36Sopenharmony_ci#define trace_puts(str) ({ \ 37062306a36Sopenharmony_ci static const char *trace_printk_fmt __used \ 37162306a36Sopenharmony_ci __section("__trace_printk_fmt") = \ 37262306a36Sopenharmony_ci __builtin_constant_p(str) ? str : NULL; \ 37362306a36Sopenharmony_ci \ 37462306a36Sopenharmony_ci if (__builtin_constant_p(str)) \ 37562306a36Sopenharmony_ci __trace_bputs(_THIS_IP_, trace_printk_fmt); \ 37662306a36Sopenharmony_ci else \ 37762306a36Sopenharmony_ci __trace_puts(_THIS_IP_, str, strlen(str)); \ 37862306a36Sopenharmony_ci}) 37962306a36Sopenharmony_ciextern int __trace_bputs(unsigned long ip, const char *str); 38062306a36Sopenharmony_ciextern int __trace_puts(unsigned long ip, const char *str, int size); 38162306a36Sopenharmony_ci 38262306a36Sopenharmony_ciextern void trace_dump_stack(int skip); 38362306a36Sopenharmony_ci 38462306a36Sopenharmony_ci/* 38562306a36Sopenharmony_ci * The double __builtin_constant_p is because gcc will give us an error 38662306a36Sopenharmony_ci * if we try to allocate the static variable to fmt if it is not a 38762306a36Sopenharmony_ci * constant. Even with the outer if statement. 38862306a36Sopenharmony_ci */ 38962306a36Sopenharmony_ci#define ftrace_vprintk(fmt, vargs) \ 39062306a36Sopenharmony_cido { \ 39162306a36Sopenharmony_ci if (__builtin_constant_p(fmt)) { \ 39262306a36Sopenharmony_ci static const char *trace_printk_fmt __used \ 39362306a36Sopenharmony_ci __section("__trace_printk_fmt") = \ 39462306a36Sopenharmony_ci __builtin_constant_p(fmt) ? fmt : NULL; \ 39562306a36Sopenharmony_ci \ 39662306a36Sopenharmony_ci __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \ 39762306a36Sopenharmony_ci } else \ 39862306a36Sopenharmony_ci __ftrace_vprintk(_THIS_IP_, fmt, vargs); \ 39962306a36Sopenharmony_ci} while (0) 40062306a36Sopenharmony_ci 40162306a36Sopenharmony_ciextern __printf(2, 0) int 40262306a36Sopenharmony_ci__ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap); 40362306a36Sopenharmony_ci 40462306a36Sopenharmony_ciextern __printf(2, 0) int 40562306a36Sopenharmony_ci__ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap); 40662306a36Sopenharmony_ci 40762306a36Sopenharmony_ciextern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode); 40862306a36Sopenharmony_ci#else 40962306a36Sopenharmony_cistatic inline void tracing_start(void) { } 41062306a36Sopenharmony_cistatic inline void tracing_stop(void) { } 41162306a36Sopenharmony_cistatic inline void trace_dump_stack(int skip) { } 41262306a36Sopenharmony_ci 41362306a36Sopenharmony_cistatic inline void tracing_on(void) { } 41462306a36Sopenharmony_cistatic inline void tracing_off(void) { } 41562306a36Sopenharmony_cistatic inline int tracing_is_on(void) { return 0; } 41662306a36Sopenharmony_cistatic inline void tracing_snapshot(void) { } 41762306a36Sopenharmony_cistatic inline void tracing_snapshot_alloc(void) { } 41862306a36Sopenharmony_ci 41962306a36Sopenharmony_cistatic inline __printf(1, 2) 42062306a36Sopenharmony_ciint trace_printk(const char *fmt, ...) 42162306a36Sopenharmony_ci{ 42262306a36Sopenharmony_ci return 0; 42362306a36Sopenharmony_ci} 42462306a36Sopenharmony_cistatic __printf(1, 0) inline int 42562306a36Sopenharmony_ciftrace_vprintk(const char *fmt, va_list ap) 42662306a36Sopenharmony_ci{ 42762306a36Sopenharmony_ci return 0; 42862306a36Sopenharmony_ci} 42962306a36Sopenharmony_cistatic inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { } 43062306a36Sopenharmony_ci#endif /* CONFIG_TRACING */ 43162306a36Sopenharmony_ci 43262306a36Sopenharmony_ci/* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */ 43362306a36Sopenharmony_ci#ifdef CONFIG_FTRACE_MCOUNT_RECORD 43462306a36Sopenharmony_ci# define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD 43562306a36Sopenharmony_ci#endif 43662306a36Sopenharmony_ci 43762306a36Sopenharmony_ci/* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */ 43862306a36Sopenharmony_ci#define VERIFY_OCTAL_PERMISSIONS(perms) \ 43962306a36Sopenharmony_ci (BUILD_BUG_ON_ZERO((perms) < 0) + \ 44062306a36Sopenharmony_ci BUILD_BUG_ON_ZERO((perms) > 0777) + \ 44162306a36Sopenharmony_ci /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */ \ 44262306a36Sopenharmony_ci BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \ 44362306a36Sopenharmony_ci BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \ 44462306a36Sopenharmony_ci /* USER_WRITABLE >= GROUP_WRITABLE */ \ 44562306a36Sopenharmony_ci BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \ 44662306a36Sopenharmony_ci /* OTHER_WRITABLE? Generally considered a bad idea. */ \ 44762306a36Sopenharmony_ci BUILD_BUG_ON_ZERO((perms) & 2) + \ 44862306a36Sopenharmony_ci (perms)) 44962306a36Sopenharmony_ci#endif 450