xref: /linux-6.15/include/linux/kernel.h (revision 43f5b308)
1 #ifndef _LINUX_KERNEL_H
2 #define _LINUX_KERNEL_H
3 
4 /*
5  * 'kernel.h' contains some often-used function prototypes etc
6  */
7 
8 #ifdef __KERNEL__
9 
10 #include <stdarg.h>
11 #include <linux/linkage.h>
12 #include <linux/stddef.h>
13 #include <linux/types.h>
14 #include <linux/compiler.h>
15 #include <linux/bitops.h>
16 #include <linux/log2.h>
17 #include <asm/byteorder.h>
18 #include <asm/bug.h>
19 
20 extern const char linux_banner[];
21 extern const char linux_proc_banner[];
22 
23 #define USHORT_MAX	((u16)(~0U))
24 #define SHORT_MAX	((s16)(USHORT_MAX>>1))
25 #define SHORT_MIN	(-SHORT_MAX - 1)
26 #define INT_MAX		((int)(~0U>>1))
27 #define INT_MIN		(-INT_MAX - 1)
28 #define UINT_MAX	(~0U)
29 #define LONG_MAX	((long)(~0UL>>1))
30 #define LONG_MIN	(-LONG_MAX - 1)
31 #define ULONG_MAX	(~0UL)
32 #define LLONG_MAX	((long long)(~0ULL>>1))
33 #define LLONG_MIN	(-LLONG_MAX - 1)
34 #define ULLONG_MAX	(~0ULL)
35 
36 #define STACK_MAGIC	0xdeadbeef
37 
38 #define ALIGN(x,a)		__ALIGN_MASK(x,(typeof(x))(a)-1)
39 #define __ALIGN_MASK(x,mask)	(((x)+(mask))&~(mask))
40 #define PTR_ALIGN(p, a)		((typeof(p))ALIGN((unsigned long)(p), (a)))
41 #define IS_ALIGNED(x, a)		(((x) & ((typeof(x))(a) - 1)) == 0)
42 
43 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
44 
45 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
46 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
47 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
48 
49 #ifdef CONFIG_LBD
50 # include <asm/div64.h>
51 # define sector_div(a, b) do_div(a, b)
52 #else
53 # define sector_div(n, b)( \
54 { \
55 	int _res; \
56 	_res = (n) % (b); \
57 	(n) /= (b); \
58 	_res; \
59 } \
60 )
61 #endif
62 
63 /**
64  * upper_32_bits - return bits 32-63 of a number
65  * @n: the number we're accessing
66  *
67  * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
68  * the "right shift count >= width of type" warning when that quantity is
69  * 32-bits.
70  */
71 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
72 
73 #define	KERN_EMERG	"<0>"	/* system is unusable			*/
74 #define	KERN_ALERT	"<1>"	/* action must be taken immediately	*/
75 #define	KERN_CRIT	"<2>"	/* critical conditions			*/
76 #define	KERN_ERR	"<3>"	/* error conditions			*/
77 #define	KERN_WARNING	"<4>"	/* warning conditions			*/
78 #define	KERN_NOTICE	"<5>"	/* normal but significant condition	*/
79 #define	KERN_INFO	"<6>"	/* informational			*/
80 #define	KERN_DEBUG	"<7>"	/* debug-level messages			*/
81 
82 /*
83  * Annotation for a "continued" line of log printout (only done after a
84  * line that had no enclosing \n). Only to be used by core/arch code
85  * during early bootup (a continued line is not SMP-safe otherwise).
86  */
87 #define	KERN_CONT	""
88 
89 extern int console_printk[];
90 
91 #define console_loglevel (console_printk[0])
92 #define default_message_loglevel (console_printk[1])
93 #define minimum_console_loglevel (console_printk[2])
94 #define default_console_loglevel (console_printk[3])
95 
96 struct completion;
97 struct pt_regs;
98 struct user;
99 
100 /**
101  * might_sleep - annotation for functions that can sleep
102  *
103  * this macro will print a stack trace if it is executed in an atomic
104  * context (spinlock, irq-handler, ...).
105  *
106  * This is a useful debugging help to be able to catch problems early and not
107  * be bitten later when the calling function happens to sleep when it is not
108  * supposed to.
109  */
110 #ifdef CONFIG_PREEMPT_VOLUNTARY
111 extern int _cond_resched(void);
112 # define might_resched() _cond_resched()
113 #else
114 # define might_resched() do { } while (0)
115 #endif
116 
117 #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
118   void __might_sleep(char *file, int line);
119 # define might_sleep() \
120 	do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0)
121 #else
122 # define might_sleep() do { might_resched(); } while (0)
123 #endif
124 
125 #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
126 
127 #define abs(x) ({				\
128 		int __x = (x);			\
129 		(__x < 0) ? -__x : __x;		\
130 	})
131 
132 extern struct atomic_notifier_head panic_notifier_list;
133 extern long (*panic_blink)(long time);
134 NORET_TYPE void panic(const char * fmt, ...)
135 	__attribute__ ((NORET_AND format (printf, 1, 2))) __cold;
136 extern void oops_enter(void);
137 extern void oops_exit(void);
138 extern int oops_may_print(void);
139 NORET_TYPE void do_exit(long error_code)
140 	ATTRIB_NORET;
141 NORET_TYPE void complete_and_exit(struct completion *, long)
142 	ATTRIB_NORET;
143 extern unsigned long simple_strtoul(const char *,char **,unsigned int);
144 extern long simple_strtol(const char *,char **,unsigned int);
145 extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
146 extern long long simple_strtoll(const char *,char **,unsigned int);
147 extern int strict_strtoul(const char *, unsigned int, unsigned long *);
148 extern int strict_strtol(const char *, unsigned int, long *);
149 extern int strict_strtoull(const char *, unsigned int, unsigned long long *);
150 extern int strict_strtoll(const char *, unsigned int, long long *);
151 extern int sprintf(char * buf, const char * fmt, ...)
152 	__attribute__ ((format (printf, 2, 3)));
153 extern int vsprintf(char *buf, const char *, va_list)
154 	__attribute__ ((format (printf, 2, 0)));
155 extern int snprintf(char * buf, size_t size, const char * fmt, ...)
156 	__attribute__ ((format (printf, 3, 4)));
157 extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
158 	__attribute__ ((format (printf, 3, 0)));
159 extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
160 	__attribute__ ((format (printf, 3, 4)));
161 extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
162 	__attribute__ ((format (printf, 3, 0)));
163 extern char *kasprintf(gfp_t gfp, const char *fmt, ...)
164 	__attribute__ ((format (printf, 2, 3)));
165 extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
166 
167 extern int sscanf(const char *, const char *, ...)
168 	__attribute__ ((format (scanf, 2, 3)));
169 extern int vsscanf(const char *, const char *, va_list)
170 	__attribute__ ((format (scanf, 2, 0)));
171 
172 extern int get_option(char **str, int *pint);
173 extern char *get_options(const char *str, int nints, int *ints);
174 extern unsigned long long memparse(char *ptr, char **retptr);
175 
176 extern int core_kernel_text(unsigned long addr);
177 extern int __kernel_text_address(unsigned long addr);
178 extern int kernel_text_address(unsigned long addr);
179 struct pid;
180 extern struct pid *session_of_pgrp(struct pid *pgrp);
181 
182 #ifdef CONFIG_PRINTK
183 asmlinkage int vprintk(const char *fmt, va_list args)
184 	__attribute__ ((format (printf, 1, 0)));
185 asmlinkage int printk(const char * fmt, ...)
186 	__attribute__ ((format (printf, 1, 2))) __cold;
187 extern int log_buf_get_len(void);
188 extern int log_buf_read(int idx);
189 extern int log_buf_copy(char *dest, int idx, int len);
190 
191 extern int printk_ratelimit_jiffies;
192 extern int printk_ratelimit_burst;
193 extern int printk_ratelimit(void);
194 extern int __ratelimit(int ratelimit_jiffies, int ratelimit_burst);
195 extern int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst);
196 extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
197 				   unsigned int interval_msec);
198 #else
199 static inline int vprintk(const char *s, va_list args)
200 	__attribute__ ((format (printf, 1, 0)));
201 static inline int vprintk(const char *s, va_list args) { return 0; }
202 static inline int printk(const char *s, ...)
203 	__attribute__ ((format (printf, 1, 2)));
204 static inline int __cold printk(const char *s, ...) { return 0; }
205 static inline int log_buf_get_len(void) { return 0; }
206 static inline int log_buf_read(int idx) { return 0; }
207 static inline int log_buf_copy(char *dest, int idx, int len) { return 0; }
208 static inline int printk_ratelimit(void) { return 0; }
209 static inline int __printk_ratelimit(int ratelimit_jiffies, \
210 				     int ratelimit_burst) { return 0; }
211 static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \
212 					  unsigned int interval_msec)	\
213 		{ return false; }
214 #endif
215 
216 extern void __attribute__((format(printf, 1, 2)))
217 	early_printk(const char *fmt, ...);
218 
219 unsigned long int_sqrt(unsigned long);
220 
221 static inline void console_silent(void)
222 {
223 	console_loglevel = 0;
224 }
225 
226 static inline void console_verbose(void)
227 {
228 	if (console_loglevel)
229 		console_loglevel = 15;
230 }
231 
232 extern void bust_spinlocks(int yes);
233 extern void wake_up_klogd(void);
234 extern int oops_in_progress;		/* If set, an oops, panic(), BUG() or die() is in progress */
235 extern int panic_timeout;
236 extern int panic_on_oops;
237 extern int panic_on_unrecovered_nmi;
238 extern int tainted;
239 extern const char *print_tainted(void);
240 extern void add_taint(unsigned);
241 extern int root_mountflags;
242 
243 /* Values used for system_state */
244 extern enum system_states {
245 	SYSTEM_BOOTING,
246 	SYSTEM_RUNNING,
247 	SYSTEM_HALT,
248 	SYSTEM_POWER_OFF,
249 	SYSTEM_RESTART,
250 	SYSTEM_SUSPEND_DISK,
251 } system_state;
252 
253 #define TAINT_PROPRIETARY_MODULE	(1<<0)
254 #define TAINT_FORCED_MODULE		(1<<1)
255 #define TAINT_UNSAFE_SMP		(1<<2)
256 #define TAINT_FORCED_RMMOD		(1<<3)
257 #define TAINT_MACHINE_CHECK		(1<<4)
258 #define TAINT_BAD_PAGE			(1<<5)
259 #define TAINT_USER			(1<<6)
260 #define TAINT_DIE			(1<<7)
261 #define TAINT_OVERRIDDEN_ACPI_TABLE	(1<<8)
262 #define TAINT_WARN			(1<<9)
263 
264 extern void dump_stack(void) __cold;
265 
266 enum {
267 	DUMP_PREFIX_NONE,
268 	DUMP_PREFIX_ADDRESS,
269 	DUMP_PREFIX_OFFSET
270 };
271 extern void hex_dump_to_buffer(const void *buf, size_t len,
272 				int rowsize, int groupsize,
273 				char *linebuf, size_t linebuflen, bool ascii);
274 extern void print_hex_dump(const char *level, const char *prefix_str,
275 				int prefix_type, int rowsize, int groupsize,
276 				const void *buf, size_t len, bool ascii);
277 extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
278 			const void *buf, size_t len);
279 #define hex_asc(x)	"0123456789abcdef"[x]
280 
281 #define pr_emerg(fmt, arg...) \
282 	printk(KERN_EMERG fmt, ##arg)
283 #define pr_alert(fmt, arg...) \
284 	printk(KERN_ALERT fmt, ##arg)
285 #define pr_crit(fmt, arg...) \
286 	printk(KERN_CRIT fmt, ##arg)
287 #define pr_err(fmt, arg...) \
288 	printk(KERN_ERR fmt, ##arg)
289 #define pr_warning(fmt, arg...) \
290 	printk(KERN_WARNING fmt, ##arg)
291 #define pr_notice(fmt, arg...) \
292 	printk(KERN_NOTICE fmt, ##arg)
293 #define pr_info(fmt, arg...) \
294 	printk(KERN_INFO fmt, ##arg)
295 
296 #ifdef DEBUG
297 /* If you are writing a driver, please use dev_dbg instead */
298 #define pr_debug(fmt, arg...) \
299 	printk(KERN_DEBUG fmt, ##arg)
300 #else
301 #define pr_debug(fmt, arg...) \
302 	({ if (0) printk(KERN_DEBUG fmt, ##arg); 0; })
303 #endif
304 
305 /*
306  *      Display an IP address in readable format.
307  */
308 
309 #define NIPQUAD(addr) \
310 	((unsigned char *)&addr)[0], \
311 	((unsigned char *)&addr)[1], \
312 	((unsigned char *)&addr)[2], \
313 	((unsigned char *)&addr)[3]
314 #define NIPQUAD_FMT "%u.%u.%u.%u"
315 
316 #define NIP6(addr) \
317 	ntohs((addr).s6_addr16[0]), \
318 	ntohs((addr).s6_addr16[1]), \
319 	ntohs((addr).s6_addr16[2]), \
320 	ntohs((addr).s6_addr16[3]), \
321 	ntohs((addr).s6_addr16[4]), \
322 	ntohs((addr).s6_addr16[5]), \
323 	ntohs((addr).s6_addr16[6]), \
324 	ntohs((addr).s6_addr16[7])
325 #define NIP6_FMT "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x"
326 #define NIP6_SEQFMT "%04x%04x%04x%04x%04x%04x%04x%04x"
327 
328 #if defined(__LITTLE_ENDIAN)
329 #define HIPQUAD(addr) \
330 	((unsigned char *)&addr)[3], \
331 	((unsigned char *)&addr)[2], \
332 	((unsigned char *)&addr)[1], \
333 	((unsigned char *)&addr)[0]
334 #elif defined(__BIG_ENDIAN)
335 #define HIPQUAD	NIPQUAD
336 #else
337 #error "Please fix asm/byteorder.h"
338 #endif /* __LITTLE_ENDIAN */
339 
340 /*
341  * min()/max()/clamp() macros that also do
342  * strict type-checking.. See the
343  * "unnecessary" pointer comparison.
344  */
345 #define min(x, y) ({				\
346 	typeof(x) _min1 = (x);			\
347 	typeof(y) _min2 = (y);			\
348 	(void) (&_min1 == &_min2);		\
349 	_min1 < _min2 ? _min1 : _min2; })
350 
351 #define max(x, y) ({				\
352 	typeof(x) _max1 = (x);			\
353 	typeof(y) _max2 = (y);			\
354 	(void) (&_max1 == &_max2);		\
355 	_max1 > _max2 ? _max1 : _max2; })
356 
357 /**
358  * clamp - return a value clamped to a given range with strict typechecking
359  * @val: current value
360  * @min: minimum allowable value
361  * @max: maximum allowable value
362  *
363  * This macro does strict typechecking of min/max to make sure they are of the
364  * same type as val.  See the unnecessary pointer comparisons.
365  */
366 #define clamp(val, min, max) ({			\
367 	typeof(val) __val = (val);		\
368 	typeof(min) __min = (min);		\
369 	typeof(max) __max = (max);		\
370 	(void) (&__val == &__min);		\
371 	(void) (&__val == &__max);		\
372 	__val = __val < __min ? __min: __val;	\
373 	__val > __max ? __max: __val; })
374 
375 /*
376  * ..and if you can't take the strict
377  * types, you can specify one yourself.
378  *
379  * Or not use min/max/clamp at all, of course.
380  */
381 #define min_t(type, x, y) ({			\
382 	type __min1 = (x);			\
383 	type __min2 = (y);			\
384 	__min1 < __min2 ? __min1: __min2; })
385 
386 #define max_t(type, x, y) ({			\
387 	type __max1 = (x);			\
388 	type __max2 = (y);			\
389 	__max1 > __max2 ? __max1: __max2; })
390 
391 /**
392  * clamp_t - return a value clamped to a given range using a given type
393  * @type: the type of variable to use
394  * @val: current value
395  * @min: minimum allowable value
396  * @max: maximum allowable value
397  *
398  * This macro does no typechecking and uses temporary variables of type
399  * 'type' to make all the comparisons.
400  */
401 #define clamp_t(type, val, min, max) ({		\
402 	type __val = (val);			\
403 	type __min = (min);			\
404 	type __max = (max);			\
405 	__val = __val < __min ? __min: __val;	\
406 	__val > __max ? __max: __val; })
407 
408 /**
409  * clamp_val - return a value clamped to a given range using val's type
410  * @val: current value
411  * @min: minimum allowable value
412  * @max: maximum allowable value
413  *
414  * This macro does no typechecking and uses temporary variables of whatever
415  * type the input argument 'val' is.  This is useful when val is an unsigned
416  * type and min and max are literals that will otherwise be assigned a signed
417  * integer type.
418  */
419 #define clamp_val(val, min, max) ({		\
420 	typeof(val) __val = (val);		\
421 	typeof(val) __min = (min);		\
422 	typeof(val) __max = (max);		\
423 	__val = __val < __min ? __min: __val;	\
424 	__val > __max ? __max: __val; })
425 
426 /**
427  * container_of - cast a member of a structure out to the containing structure
428  * @ptr:	the pointer to the member.
429  * @type:	the type of the container struct this is embedded in.
430  * @member:	the name of the member within the struct.
431  *
432  */
433 #define container_of(ptr, type, member) ({			\
434 	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
435 	(type *)( (char *)__mptr - offsetof(type,member) );})
436 
437 /*
438  * Check at compile time that something is of a particular type.
439  * Always evaluates to 1 so you may use it easily in comparisons.
440  */
441 #define typecheck(type,x) \
442 ({	type __dummy; \
443 	typeof(x) __dummy2; \
444 	(void)(&__dummy == &__dummy2); \
445 	1; \
446 })
447 
448 /*
449  * Check at compile time that 'function' is a certain type, or is a pointer
450  * to that type (needs to use typedef for the function type.)
451  */
452 #define typecheck_fn(type,function) \
453 ({	typeof(type) __tmp = function; \
454 	(void)__tmp; \
455 })
456 
457 struct sysinfo;
458 extern int do_sysinfo(struct sysinfo *info);
459 
460 #endif /* __KERNEL__ */
461 
462 #define SI_LOAD_SHIFT	16
463 struct sysinfo {
464 	long uptime;			/* Seconds since boot */
465 	unsigned long loads[3];		/* 1, 5, and 15 minute load averages */
466 	unsigned long totalram;		/* Total usable main memory size */
467 	unsigned long freeram;		/* Available memory size */
468 	unsigned long sharedram;	/* Amount of shared memory */
469 	unsigned long bufferram;	/* Memory used by buffers */
470 	unsigned long totalswap;	/* Total swap space size */
471 	unsigned long freeswap;		/* swap space still available */
472 	unsigned short procs;		/* Number of current processes */
473 	unsigned short pad;		/* explicit padding for m68k */
474 	unsigned long totalhigh;	/* Total high memory size */
475 	unsigned long freehigh;		/* Available high memory size */
476 	unsigned int mem_unit;		/* Memory unit size in bytes */
477 	char _f[20-2*sizeof(long)-sizeof(int)];	/* Padding: libc5 uses this.. */
478 };
479 
480 /* Force a compilation error if condition is true */
481 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
482 
483 /* Force a compilation error if condition is true, but also produce a
484    result (of value 0 and type size_t), so the expression can be used
485    e.g. in a structure initializer (or where-ever else comma expressions
486    aren't permitted). */
487 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
488 
489 /* Trap pasters of __FUNCTION__ at compile-time */
490 #define __FUNCTION__ (__func__)
491 
492 /* This helps us to avoid #ifdef CONFIG_NUMA */
493 #ifdef CONFIG_NUMA
494 #define NUMA_BUILD 1
495 #else
496 #define NUMA_BUILD 0
497 #endif
498 
499 #endif
500