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