xref: /linux-6.15/include/linux/kernel.h (revision 776cfebb)
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 <asm/byteorder.h>
17 #include <asm/bug.h>
18 
19 extern const char linux_banner[];
20 
21 #define INT_MAX		((int)(~0U>>1))
22 #define INT_MIN		(-INT_MAX - 1)
23 #define UINT_MAX	(~0U)
24 #define LONG_MAX	((long)(~0UL>>1))
25 #define LONG_MIN	(-LONG_MAX - 1)
26 #define ULONG_MAX	(~0UL)
27 
28 #define STACK_MAGIC	0xdeadbeef
29 
30 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
31 #define ALIGN(x,a) (((x)+(a)-1)&~((a)-1))
32 
33 #define	KERN_EMERG	"<0>"	/* system is unusable			*/
34 #define	KERN_ALERT	"<1>"	/* action must be taken immediately	*/
35 #define	KERN_CRIT	"<2>"	/* critical conditions			*/
36 #define	KERN_ERR	"<3>"	/* error conditions			*/
37 #define	KERN_WARNING	"<4>"	/* warning conditions			*/
38 #define	KERN_NOTICE	"<5>"	/* normal but significant condition	*/
39 #define	KERN_INFO	"<6>"	/* informational			*/
40 #define	KERN_DEBUG	"<7>"	/* debug-level messages			*/
41 
42 extern int console_printk[];
43 
44 #define console_loglevel (console_printk[0])
45 #define default_message_loglevel (console_printk[1])
46 #define minimum_console_loglevel (console_printk[2])
47 #define default_console_loglevel (console_printk[3])
48 
49 struct completion;
50 
51 /**
52  * might_sleep - annotation for functions that can sleep
53  *
54  * this macro will print a stack trace if it is executed in an atomic
55  * context (spinlock, irq-handler, ...).
56  *
57  * This is a useful debugging help to be able to catch problems early and not
58  * be biten later when the calling function happens to sleep when it is not
59  * supposed to.
60  */
61 #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
62 #define might_sleep() __might_sleep(__FILE__, __LINE__)
63 #define might_sleep_if(cond) do { if (unlikely(cond)) might_sleep(); } while (0)
64 void __might_sleep(char *file, int line);
65 #else
66 #define might_sleep() do {} while(0)
67 #define might_sleep_if(cond) do {} while (0)
68 #endif
69 
70 #define abs(x) ({				\
71 		int __x = (x);			\
72 		(__x < 0) ? -__x : __x;		\
73 	})
74 
75 #define labs(x) ({				\
76 		long __x = (x);			\
77 		(__x < 0) ? -__x : __x;		\
78 	})
79 
80 extern struct notifier_block *panic_notifier_list;
81 extern long (*panic_blink)(long time);
82 NORET_TYPE void panic(const char * fmt, ...)
83 	__attribute__ ((NORET_AND format (printf, 1, 2)));
84 fastcall NORET_TYPE void do_exit(long error_code)
85 	ATTRIB_NORET;
86 NORET_TYPE void complete_and_exit(struct completion *, long)
87 	ATTRIB_NORET;
88 extern unsigned long simple_strtoul(const char *,char **,unsigned int);
89 extern long simple_strtol(const char *,char **,unsigned int);
90 extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
91 extern long long simple_strtoll(const char *,char **,unsigned int);
92 extern int sprintf(char * buf, const char * fmt, ...)
93 	__attribute__ ((format (printf, 2, 3)));
94 extern int vsprintf(char *buf, const char *, va_list)
95 	__attribute__ ((format (printf, 2, 0)));
96 extern int snprintf(char * buf, size_t size, const char * fmt, ...)
97 	__attribute__ ((format (printf, 3, 4)));
98 extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
99 	__attribute__ ((format (printf, 3, 0)));
100 extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
101 	__attribute__ ((format (printf, 3, 4)));
102 extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
103 	__attribute__ ((format (printf, 3, 0)));
104 
105 extern int sscanf(const char *, const char *, ...)
106 	__attribute__ ((format (scanf, 2, 3)));
107 extern int vsscanf(const char *, const char *, va_list)
108 	__attribute__ ((format (scanf, 2, 0)));
109 
110 extern int get_option(char **str, int *pint);
111 extern char *get_options(const char *str, int nints, int *ints);
112 extern unsigned long long memparse(char *ptr, char **retptr);
113 
114 extern int __kernel_text_address(unsigned long addr);
115 extern int kernel_text_address(unsigned long addr);
116 extern int session_of_pgrp(int pgrp);
117 
118 #ifdef CONFIG_PRINTK
119 asmlinkage int vprintk(const char *fmt, va_list args)
120 	__attribute__ ((format (printf, 1, 0)));
121 asmlinkage int printk(const char * fmt, ...)
122 	__attribute__ ((format (printf, 1, 2)));
123 #else
124 static inline int vprintk(const char *s, va_list args)
125 	__attribute__ ((format (printf, 1, 0)));
126 static inline int vprintk(const char *s, va_list args) { return 0; }
127 static inline int printk(const char *s, ...)
128 	__attribute__ ((format (printf, 1, 2)));
129 static inline int printk(const char *s, ...) { return 0; }
130 #endif
131 
132 unsigned long int_sqrt(unsigned long);
133 
134 static inline int __attribute_pure__ long_log2(unsigned long x)
135 {
136 	int r = 0;
137 	for (x >>= 1; x > 0; x >>= 1)
138 		r++;
139 	return r;
140 }
141 
142 static inline unsigned long __attribute_const__ roundup_pow_of_two(unsigned long x)
143 {
144 	return (1UL << fls(x - 1));
145 }
146 
147 extern int printk_ratelimit(void);
148 extern int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst);
149 
150 static inline void console_silent(void)
151 {
152 	console_loglevel = 0;
153 }
154 
155 static inline void console_verbose(void)
156 {
157 	if (console_loglevel)
158 		console_loglevel = 15;
159 }
160 
161 extern void bust_spinlocks(int yes);
162 extern int oops_in_progress;		/* If set, an oops, panic(), BUG() or die() is in progress */
163 extern int panic_timeout;
164 extern int panic_on_oops;
165 extern int tainted;
166 extern const char *print_tainted(void);
167 extern void add_taint(unsigned);
168 
169 /* Values used for system_state */
170 extern enum system_states {
171 	SYSTEM_BOOTING,
172 	SYSTEM_RUNNING,
173 	SYSTEM_HALT,
174 	SYSTEM_POWER_OFF,
175 	SYSTEM_RESTART,
176 } system_state;
177 
178 #define TAINT_PROPRIETARY_MODULE	(1<<0)
179 #define TAINT_FORCED_MODULE		(1<<1)
180 #define TAINT_UNSAFE_SMP		(1<<2)
181 #define TAINT_FORCED_RMMOD		(1<<3)
182 #define TAINT_MACHINE_CHECK		(1<<4)
183 #define TAINT_BAD_PAGE			(1<<5)
184 
185 extern void dump_stack(void);
186 
187 #ifdef DEBUG
188 #define pr_debug(fmt,arg...) \
189 	printk(KERN_DEBUG fmt,##arg)
190 #else
191 #define pr_debug(fmt,arg...) \
192 	do { } while (0)
193 #endif
194 
195 #define pr_info(fmt,arg...) \
196 	printk(KERN_INFO fmt,##arg)
197 
198 /*
199  *      Display an IP address in readable format.
200  */
201 
202 #define NIPQUAD(addr) \
203 	((unsigned char *)&addr)[0], \
204 	((unsigned char *)&addr)[1], \
205 	((unsigned char *)&addr)[2], \
206 	((unsigned char *)&addr)[3]
207 
208 #define NIP6(addr) \
209 	ntohs((addr).s6_addr16[0]), \
210 	ntohs((addr).s6_addr16[1]), \
211 	ntohs((addr).s6_addr16[2]), \
212 	ntohs((addr).s6_addr16[3]), \
213 	ntohs((addr).s6_addr16[4]), \
214 	ntohs((addr).s6_addr16[5]), \
215 	ntohs((addr).s6_addr16[6]), \
216 	ntohs((addr).s6_addr16[7])
217 
218 #if defined(__LITTLE_ENDIAN)
219 #define HIPQUAD(addr) \
220 	((unsigned char *)&addr)[3], \
221 	((unsigned char *)&addr)[2], \
222 	((unsigned char *)&addr)[1], \
223 	((unsigned char *)&addr)[0]
224 #elif defined(__BIG_ENDIAN)
225 #define HIPQUAD	NIPQUAD
226 #else
227 #error "Please fix asm/byteorder.h"
228 #endif /* __LITTLE_ENDIAN */
229 
230 /*
231  * min()/max() macros that also do
232  * strict type-checking.. See the
233  * "unnecessary" pointer comparison.
234  */
235 #define min(x,y) ({ \
236 	typeof(x) _x = (x);	\
237 	typeof(y) _y = (y);	\
238 	(void) (&_x == &_y);		\
239 	_x < _y ? _x : _y; })
240 
241 #define max(x,y) ({ \
242 	typeof(x) _x = (x);	\
243 	typeof(y) _y = (y);	\
244 	(void) (&_x == &_y);		\
245 	_x > _y ? _x : _y; })
246 
247 /*
248  * ..and if you can't take the strict
249  * types, you can specify one yourself.
250  *
251  * Or not use min/max at all, of course.
252  */
253 #define min_t(type,x,y) \
254 	({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
255 #define max_t(type,x,y) \
256 	({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
257 
258 
259 /**
260  * container_of - cast a member of a structure out to the containing structure
261  *
262  * @ptr:	the pointer to the member.
263  * @type:	the type of the container struct this is embedded in.
264  * @member:	the name of the member within the struct.
265  *
266  */
267 #define container_of(ptr, type, member) ({			\
268         const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
269         (type *)( (char *)__mptr - offsetof(type,member) );})
270 
271 /*
272  * Check at compile time that something is of a particular type.
273  * Always evaluates to 1 so you may use it easily in comparisons.
274  */
275 #define typecheck(type,x) \
276 ({	type __dummy; \
277 	typeof(x) __dummy2; \
278 	(void)(&__dummy == &__dummy2); \
279 	1; \
280 })
281 
282 #endif /* __KERNEL__ */
283 
284 #define SI_LOAD_SHIFT	16
285 struct sysinfo {
286 	long uptime;			/* Seconds since boot */
287 	unsigned long loads[3];		/* 1, 5, and 15 minute load averages */
288 	unsigned long totalram;		/* Total usable main memory size */
289 	unsigned long freeram;		/* Available memory size */
290 	unsigned long sharedram;	/* Amount of shared memory */
291 	unsigned long bufferram;	/* Memory used by buffers */
292 	unsigned long totalswap;	/* Total swap space size */
293 	unsigned long freeswap;		/* swap space still available */
294 	unsigned short procs;		/* Number of current processes */
295 	unsigned short pad;		/* explicit padding for m68k */
296 	unsigned long totalhigh;	/* Total high memory size */
297 	unsigned long freehigh;		/* Available high memory size */
298 	unsigned int mem_unit;		/* Memory unit size in bytes */
299 	char _f[20-2*sizeof(long)-sizeof(int)];	/* Padding: libc5 uses this.. */
300 };
301 
302 extern void BUILD_BUG(void);
303 #define BUILD_BUG_ON(condition) do { if (condition) BUILD_BUG(); } while(0)
304 
305 #ifdef CONFIG_SYSCTL
306 extern int randomize_va_space;
307 #else
308 #define randomize_va_space 1
309 #endif
310 
311 /* Trap pasters of __FUNCTION__ at compile-time */
312 #if __GNUC__ > 2 || __GNUC_MINOR__ >= 95
313 #define __FUNCTION__ (__func__)
314 #endif
315 
316 #endif
317