at v6.19-rc2 12 kB view raw
1/* SPDX-License-Identifier: GPL-2.0 */ 2#ifndef _LINUX_INIT_H 3#define _LINUX_INIT_H 4 5#include <linux/build_bug.h> 6#include <linux/compiler.h> 7#include <linux/stringify.h> 8#include <linux/types.h> 9 10/* These macros are used to mark some functions or 11 * initialized data (doesn't apply to uninitialized data) 12 * as `initialization' functions. The kernel can take this 13 * as hint that the function is used only during the initialization 14 * phase and free up used memory resources after 15 * 16 * Usage: 17 * For functions: 18 * 19 * You should add __init immediately before the function name, like: 20 * 21 * static void __init initme(int x, int y) 22 * { 23 * extern int z; z = x * y; 24 * } 25 * 26 * If the function has a prototype somewhere, you can also add 27 * __init between closing brace of the prototype and semicolon: 28 * 29 * extern int initialize_foobar_device(int, int, int) __init; 30 * 31 * For initialized data: 32 * You should insert __initdata or __initconst between the variable name 33 * and equal sign followed by value, e.g.: 34 * 35 * static int init_variable __initdata = 0; 36 * static const char linux_logo[] __initconst = { 0x32, 0x36, ... }; 37 * 38 * Don't forget to initialize data not at file scope, i.e. within a function, 39 * as gcc otherwise puts the data into the bss section and not into the init 40 * section. 41 */ 42 43/* These are for everybody (although not all archs will actually 44 discard it in modules) */ 45#define __init __section(".init.text") __cold __latent_entropy \ 46 __no_kstack_erase 47#define __initdata __section(".init.data") 48#define __initconst __section(".init.rodata") 49#define __exitdata __section(".exit.data") 50#define __exit_call __used __section(".exitcall.exit") 51 52/* 53 * modpost check for section mismatches during the kernel build. 54 * A section mismatch happens when there are references from a 55 * code or data section to an init section (both code or data). 56 * The init sections are (for most archs) discarded by the kernel 57 * when early init has completed so all such references are potential bugs. 58 * For exit sections the same issue exists. 59 * 60 * The following markers are used for the cases where the reference to 61 * the *init / *exit section (code or data) is valid and will teach 62 * modpost not to issue a warning. Intended semantics is that a code or 63 * data tagged __ref* can reference code or data from init section without 64 * producing a warning (of course, no warning does not mean code is 65 * correct, so optimally document why the __ref is needed and why it's OK). 66 * 67 * The markers follow same syntax rules as __init / __initdata. 68 */ 69#define __ref __section(".ref.text") noinline 70#define __refdata __section(".ref.data") 71#define __refconst __section(".ref.rodata") 72 73#ifdef MODULE 74#define __exitused 75#else 76#define __exitused __used 77#endif 78 79#define __exit __section(".exit.text") __exitused __cold notrace 80 81#ifdef CONFIG_MEMORY_HOTPLUG 82#define __meminit 83#define __meminitdata 84#define __meminitconst 85#else 86#define __meminit __init 87#define __meminitdata __initdata 88#define __meminitconst __initconst 89#endif 90 91/* For assembly routines */ 92#define __HEAD .section ".head.text","ax" 93#define __INIT .section ".init.text","ax" 94#define __FINIT .previous 95 96#define __INITDATA .section ".init.data","aw",%progbits 97#define __INITRODATA .section ".init.rodata","a",%progbits 98#define __FINITDATA .previous 99 100/* silence warnings when references are OK */ 101#define __REF .section ".ref.text", "ax" 102#define __REFDATA .section ".ref.data", "aw" 103#define __REFCONST .section ".ref.rodata", "a" 104 105#ifndef __ASSEMBLY__ 106/* 107 * Used for initialization calls.. 108 */ 109typedef int (*initcall_t)(void); 110typedef void (*exitcall_t)(void); 111 112#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS 113typedef int initcall_entry_t; 114 115static inline initcall_t initcall_from_entry(initcall_entry_t *entry) 116{ 117 return offset_to_ptr(entry); 118} 119#else 120typedef initcall_t initcall_entry_t; 121 122static inline initcall_t initcall_from_entry(initcall_entry_t *entry) 123{ 124 return *entry; 125} 126#endif 127 128extern initcall_entry_t __con_initcall_start[], __con_initcall_end[]; 129 130/* Used for constructor calls. */ 131typedef void (*ctor_fn_t)(void); 132 133struct file_system_type; 134 135/* Defined in init/main.c */ 136extern int do_one_initcall(initcall_t fn); 137extern char __initdata boot_command_line[]; 138extern char *saved_command_line; 139extern unsigned int saved_command_line_len; 140extern unsigned int reset_devices; 141 142/* used by init/main.c */ 143void setup_arch(char **); 144void prepare_namespace(void); 145void __init init_rootfs(void); 146 147void init_IRQ(void); 148void time_init(void); 149void poking_init(void); 150void pgtable_cache_init(void); 151 152extern initcall_entry_t __initcall_start[]; 153extern initcall_entry_t __initcall0_start[]; 154extern initcall_entry_t __initcall1_start[]; 155extern initcall_entry_t __initcall2_start[]; 156extern initcall_entry_t __initcall3_start[]; 157extern initcall_entry_t __initcall4_start[]; 158extern initcall_entry_t __initcall5_start[]; 159extern initcall_entry_t __initcall6_start[]; 160extern initcall_entry_t __initcall7_start[]; 161extern initcall_entry_t __initcall_end[]; 162 163extern struct file_system_type rootfs_fs_type; 164 165extern bool rodata_enabled; 166void mark_rodata_ro(void); 167 168extern void (*late_time_init)(void); 169 170extern bool initcall_debug; 171 172#ifdef MODULE 173extern struct module __this_module; 174#define THIS_MODULE (&__this_module) 175#else 176#define THIS_MODULE ((struct module *)0) 177#endif 178 179#endif 180 181#ifndef MODULE 182 183#ifndef __ASSEMBLY__ 184 185/* 186 * initcalls are now grouped by functionality into separate 187 * subsections. Ordering inside the subsections is determined 188 * by link order. 189 * For backwards compatibility, initcall() puts the call in 190 * the device init subsection. 191 * 192 * The `id' arg to __define_initcall() is needed so that multiple initcalls 193 * can point at the same handler without causing duplicate-symbol build errors. 194 * 195 * Initcalls are run by placing pointers in initcall sections that the 196 * kernel iterates at runtime. The linker can do dead code / data elimination 197 * and remove that completely, so the initcall sections have to be marked 198 * as KEEP() in the linker script. 199 */ 200 201/* Format: <modname>__<counter>_<line>_<fn> */ 202#define __initcall_id(fn) \ 203 __PASTE(kmod_, \ 204 __PASTE(__KBUILD_MODNAME, \ 205 __PASTE(__, \ 206 __PASTE(__COUNTER__, \ 207 __PASTE(_, \ 208 __PASTE(__LINE__, \ 209 __PASTE(_, fn))))))) 210 211/* Format: __<prefix>__<iid><id> */ 212#define __initcall_name(prefix, __iid, id) \ 213 __PASTE(__, \ 214 __PASTE(prefix, \ 215 __PASTE(__, \ 216 __PASTE(__iid, id)))) 217 218#ifdef CONFIG_LTO_CLANG 219/* 220 * With LTO, the compiler doesn't necessarily obey link order for 221 * initcalls. In order to preserve the correct order, we add each 222 * variable into its own section and generate a linker script (in 223 * scripts/link-vmlinux.sh) to specify the order of the sections. 224 */ 225#define __initcall_section(__sec, __iid) \ 226 #__sec ".init.." #__iid 227 228/* 229 * With LTO, the compiler can rename static functions to avoid 230 * global naming collisions. We use a global stub function for 231 * initcalls to create a stable symbol name whose address can be 232 * taken in inline assembly when PREL32 relocations are used. 233 */ 234#define __initcall_stub(fn, __iid, id) \ 235 __initcall_name(initstub, __iid, id) 236 237#define __define_initcall_stub(__stub, fn) \ 238 int __init __stub(void); \ 239 int __init __stub(void) \ 240 { \ 241 return fn(); \ 242 } \ 243 __ADDRESSABLE(__stub) 244#else 245#define __initcall_section(__sec, __iid) \ 246 #__sec ".init" 247 248#define __initcall_stub(fn, __iid, id) fn 249 250#define __define_initcall_stub(__stub, fn) \ 251 __ADDRESSABLE(fn) 252#endif 253 254#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS 255#define ____define_initcall(fn, __stub, __name, __sec) \ 256 __define_initcall_stub(__stub, fn) \ 257 asm(".section \"" __sec "\", \"a\" \n" \ 258 __stringify(__name) ": \n" \ 259 ".long " __stringify(__stub) " - . \n" \ 260 ".previous \n"); \ 261 static_assert(__same_type(initcall_t, &fn)); 262#else 263#define ____define_initcall(fn, __unused, __name, __sec) \ 264 static initcall_t __name __used \ 265 __attribute__((__section__(__sec))) = fn; 266#endif 267 268#define __unique_initcall(fn, id, __sec, __iid) \ 269 ____define_initcall(fn, \ 270 __initcall_stub(fn, __iid, id), \ 271 __initcall_name(initcall, __iid, id), \ 272 __initcall_section(__sec, __iid)) 273 274#define ___define_initcall(fn, id, __sec) \ 275 __unique_initcall(fn, id, __sec, __initcall_id(fn)) 276 277#define __define_initcall(fn, id) ___define_initcall(fn, id, .initcall##id) 278 279/* 280 * Early initcalls run before initializing SMP. 281 * 282 * Only for built-in code, not modules. 283 */ 284#define early_initcall(fn) __define_initcall(fn, early) 285 286/* 287 * A "pure" initcall has no dependencies on anything else, and purely 288 * initializes variables that couldn't be statically initialized. 289 * 290 * This only exists for built-in code, not for modules. 291 * Keep main.c:initcall_level_names[] in sync. 292 */ 293#define pure_initcall(fn) __define_initcall(fn, 0) 294 295#define core_initcall(fn) __define_initcall(fn, 1) 296#define core_initcall_sync(fn) __define_initcall(fn, 1s) 297#define postcore_initcall(fn) __define_initcall(fn, 2) 298#define postcore_initcall_sync(fn) __define_initcall(fn, 2s) 299#define arch_initcall(fn) __define_initcall(fn, 3) 300#define arch_initcall_sync(fn) __define_initcall(fn, 3s) 301#define subsys_initcall(fn) __define_initcall(fn, 4) 302#define subsys_initcall_sync(fn) __define_initcall(fn, 4s) 303#define fs_initcall(fn) __define_initcall(fn, 5) 304#define fs_initcall_sync(fn) __define_initcall(fn, 5s) 305#define rootfs_initcall(fn) __define_initcall(fn, rootfs) 306#define device_initcall(fn) __define_initcall(fn, 6) 307#define device_initcall_sync(fn) __define_initcall(fn, 6s) 308#define late_initcall(fn) __define_initcall(fn, 7) 309#define late_initcall_sync(fn) __define_initcall(fn, 7s) 310 311#define __initcall(fn) device_initcall(fn) 312 313#define __exitcall(fn) \ 314 static exitcall_t __exitcall_##fn __exit_call = fn 315 316#define console_initcall(fn) ___define_initcall(fn, con, .con_initcall) 317 318struct obs_kernel_param { 319 const char *str; 320 int (*setup_func)(char *); 321 int early; 322}; 323 324extern const struct obs_kernel_param __setup_start[], __setup_end[]; 325 326/* 327 * Only for really core code. See moduleparam.h for the normal way. 328 * 329 * Force the alignment so the compiler doesn't space elements of the 330 * obs_kernel_param "array" too far apart in .init.setup. 331 */ 332#define __setup_param(str, unique_id, fn, early) \ 333 static const char __setup_str_##unique_id[] __initconst \ 334 __aligned(1) = str; \ 335 static struct obs_kernel_param __setup_##unique_id \ 336 __used __section(".init.setup") \ 337 __aligned(__alignof__(struct obs_kernel_param)) \ 338 = { __setup_str_##unique_id, fn, early } 339 340/* 341 * NOTE: __setup functions return values: 342 * @fn returns 1 (or non-zero) if the option argument is "handled" 343 * and returns 0 if the option argument is "not handled". 344 */ 345#define __setup(str, fn) \ 346 __setup_param(str, fn, fn, 0) 347 348/* 349 * NOTE: @fn is as per module_param, not __setup! 350 * I.e., @fn returns 0 for no error or non-zero for error 351 * (possibly @fn returns a -errno value, but it does not matter). 352 * Emits warning if @fn returns non-zero. 353 */ 354#define early_param(str, fn) \ 355 __setup_param(str, fn, fn, 1) 356 357#define early_param_on_off(str_on, str_off, var, config) \ 358 \ 359 int var = IS_ENABLED(config); \ 360 \ 361 static int __init parse_##var##_on(char *arg) \ 362 { \ 363 var = 1; \ 364 return 0; \ 365 } \ 366 early_param(str_on, parse_##var##_on); \ 367 \ 368 static int __init parse_##var##_off(char *arg) \ 369 { \ 370 var = 0; \ 371 return 0; \ 372 } \ 373 early_param(str_off, parse_##var##_off) 374 375/* Relies on boot_command_line being set */ 376void __init parse_early_param(void); 377void __init parse_early_options(char *cmdline); 378#endif /* __ASSEMBLY__ */ 379 380#else /* MODULE */ 381 382#define __setup_param(str, unique_id, fn) /* nothing */ 383#define __setup(str, func) /* nothing */ 384#endif 385 386/* Data marked not to be saved by software suspend */ 387#define __nosavedata __section(".data..nosave") 388 389#ifdef MODULE 390#define __exit_p(x) x 391#else 392#define __exit_p(x) NULL 393#endif 394 395#endif /* _LINUX_INIT_H */