Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_INIT_H
0003 #define _LINUX_INIT_H
0004 
0005 #include <linux/compiler.h>
0006 #include <linux/types.h>
0007 
0008 /* Built-in __init functions needn't be compiled with retpoline */
0009 #if defined(__noretpoline) && !defined(MODULE)
0010 #define __noinitretpoline __noretpoline
0011 #else
0012 #define __noinitretpoline
0013 #endif
0014 
0015 /* These macros are used to mark some functions or 
0016  * initialized data (doesn't apply to uninitialized data)
0017  * as `initialization' functions. The kernel can take this
0018  * as hint that the function is used only during the initialization
0019  * phase and free up used memory resources after
0020  *
0021  * Usage:
0022  * For functions:
0023  * 
0024  * You should add __init immediately before the function name, like:
0025  *
0026  * static void __init initme(int x, int y)
0027  * {
0028  *    extern int z; z = x * y;
0029  * }
0030  *
0031  * If the function has a prototype somewhere, you can also add
0032  * __init between closing brace of the prototype and semicolon:
0033  *
0034  * extern int initialize_foobar_device(int, int, int) __init;
0035  *
0036  * For initialized data:
0037  * You should insert __initdata or __initconst between the variable name
0038  * and equal sign followed by value, e.g.:
0039  *
0040  * static int init_variable __initdata = 0;
0041  * static const char linux_logo[] __initconst = { 0x32, 0x36, ... };
0042  *
0043  * Don't forget to initialize data not at file scope, i.e. within a function,
0044  * as gcc otherwise puts the data into the bss section and not into the init
0045  * section.
0046  */
0047 
0048 /* These are for everybody (although not all archs will actually
0049    discard it in modules) */
0050 #define __init      __section(".init.text") __cold  __latent_entropy __noinitretpoline __nocfi
0051 #define __initdata  __section(".init.data")
0052 #define __initconst __section(".init.rodata")
0053 #define __exitdata  __section(".exit.data")
0054 #define __exit_call __used __section(".exitcall.exit")
0055 
0056 /*
0057  * modpost check for section mismatches during the kernel build.
0058  * A section mismatch happens when there are references from a
0059  * code or data section to an init section (both code or data).
0060  * The init sections are (for most archs) discarded by the kernel
0061  * when early init has completed so all such references are potential bugs.
0062  * For exit sections the same issue exists.
0063  *
0064  * The following markers are used for the cases where the reference to
0065  * the *init / *exit section (code or data) is valid and will teach
0066  * modpost not to issue a warning.  Intended semantics is that a code or
0067  * data tagged __ref* can reference code or data from init section without
0068  * producing a warning (of course, no warning does not mean code is
0069  * correct, so optimally document why the __ref is needed and why it's OK).
0070  *
0071  * The markers follow same syntax rules as __init / __initdata.
0072  */
0073 #define __ref            __section(".ref.text") noinline
0074 #define __refdata        __section(".ref.data")
0075 #define __refconst       __section(".ref.rodata")
0076 
0077 #ifdef MODULE
0078 #define __exitused
0079 #else
0080 #define __exitused  __used
0081 #endif
0082 
0083 #define __exit          __section(".exit.text") __exitused __cold notrace
0084 
0085 /* Used for MEMORY_HOTPLUG */
0086 #define __meminit        __section(".meminit.text") __cold notrace \
0087                           __latent_entropy
0088 #define __meminitdata    __section(".meminit.data")
0089 #define __meminitconst   __section(".meminit.rodata")
0090 #define __memexit        __section(".memexit.text") __exitused __cold notrace
0091 #define __memexitdata    __section(".memexit.data")
0092 #define __memexitconst   __section(".memexit.rodata")
0093 
0094 /* For assembly routines */
0095 #define __HEAD      .section    ".head.text","ax"
0096 #define __INIT      .section    ".init.text","ax"
0097 #define __FINIT     .previous
0098 
0099 #define __INITDATA  .section    ".init.data","aw",%progbits
0100 #define __INITRODATA    .section    ".init.rodata","a",%progbits
0101 #define __FINITDATA .previous
0102 
0103 #define __MEMINIT        .section   ".meminit.text", "ax"
0104 #define __MEMINITDATA    .section   ".meminit.data", "aw"
0105 #define __MEMINITRODATA  .section   ".meminit.rodata", "a"
0106 
0107 /* silence warnings when references are OK */
0108 #define __REF            .section       ".ref.text", "ax"
0109 #define __REFDATA        .section       ".ref.data", "aw"
0110 #define __REFCONST       .section       ".ref.rodata", "a"
0111 
0112 #ifndef __ASSEMBLY__
0113 /*
0114  * Used for initialization calls..
0115  */
0116 typedef int (*initcall_t)(void);
0117 typedef void (*exitcall_t)(void);
0118 
0119 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
0120 typedef int initcall_entry_t;
0121 
0122 static inline initcall_t initcall_from_entry(initcall_entry_t *entry)
0123 {
0124     return offset_to_ptr(entry);
0125 }
0126 #else
0127 typedef initcall_t initcall_entry_t;
0128 
0129 static inline initcall_t initcall_from_entry(initcall_entry_t *entry)
0130 {
0131     return *entry;
0132 }
0133 #endif
0134 
0135 extern initcall_entry_t __con_initcall_start[], __con_initcall_end[];
0136 
0137 /* Used for contructor calls. */
0138 typedef void (*ctor_fn_t)(void);
0139 
0140 struct file_system_type;
0141 
0142 /* Defined in init/main.c */
0143 extern int do_one_initcall(initcall_t fn);
0144 extern char __initdata boot_command_line[];
0145 extern char *saved_command_line;
0146 extern unsigned int reset_devices;
0147 
0148 /* used by init/main.c */
0149 void setup_arch(char **);
0150 void prepare_namespace(void);
0151 void __init init_rootfs(void);
0152 extern struct file_system_type rootfs_fs_type;
0153 
0154 #if defined(CONFIG_STRICT_KERNEL_RWX) || defined(CONFIG_STRICT_MODULE_RWX)
0155 extern bool rodata_enabled;
0156 #endif
0157 #ifdef CONFIG_STRICT_KERNEL_RWX
0158 void mark_rodata_ro(void);
0159 #endif
0160 
0161 extern void (*late_time_init)(void);
0162 
0163 extern bool initcall_debug;
0164 
0165 #endif
0166   
0167 #ifndef MODULE
0168 
0169 #ifndef __ASSEMBLY__
0170 
0171 /*
0172  * initcalls are now grouped by functionality into separate
0173  * subsections. Ordering inside the subsections is determined
0174  * by link order. 
0175  * For backwards compatibility, initcall() puts the call in 
0176  * the device init subsection.
0177  *
0178  * The `id' arg to __define_initcall() is needed so that multiple initcalls
0179  * can point at the same handler without causing duplicate-symbol build errors.
0180  *
0181  * Initcalls are run by placing pointers in initcall sections that the
0182  * kernel iterates at runtime. The linker can do dead code / data elimination
0183  * and remove that completely, so the initcall sections have to be marked
0184  * as KEEP() in the linker script.
0185  */
0186 
0187 /* Format: <modname>__<counter>_<line>_<fn> */
0188 #define __initcall_id(fn)                   \
0189     __PASTE(__KBUILD_MODNAME,               \
0190     __PASTE(__,                     \
0191     __PASTE(__COUNTER__,                    \
0192     __PASTE(_,                      \
0193     __PASTE(__LINE__,                   \
0194     __PASTE(_, fn))))))
0195 
0196 /* Format: __<prefix>__<iid><id> */
0197 #define __initcall_name(prefix, __iid, id)          \
0198     __PASTE(__,                     \
0199     __PASTE(prefix,                     \
0200     __PASTE(__,                     \
0201     __PASTE(__iid, id))))
0202 
0203 #ifdef CONFIG_LTO_CLANG
0204 /*
0205  * With LTO, the compiler doesn't necessarily obey link order for
0206  * initcalls. In order to preserve the correct order, we add each
0207  * variable into its own section and generate a linker script (in
0208  * scripts/link-vmlinux.sh) to specify the order of the sections.
0209  */
0210 #define __initcall_section(__sec, __iid)            \
0211     #__sec ".init.." #__iid
0212 
0213 /*
0214  * With LTO, the compiler can rename static functions to avoid
0215  * global naming collisions. We use a global stub function for
0216  * initcalls to create a stable symbol name whose address can be
0217  * taken in inline assembly when PREL32 relocations are used.
0218  */
0219 #define __initcall_stub(fn, __iid, id)              \
0220     __initcall_name(initstub, __iid, id)
0221 
0222 #define __define_initcall_stub(__stub, fn)          \
0223     int __init __cficanonical __stub(void);         \
0224     int __init __cficanonical __stub(void)          \
0225     {                           \
0226         return fn();                    \
0227     }                           \
0228     __ADDRESSABLE(__stub)
0229 #else
0230 #define __initcall_section(__sec, __iid)            \
0231     #__sec ".init"
0232 
0233 #define __initcall_stub(fn, __iid, id)  fn
0234 
0235 #define __define_initcall_stub(__stub, fn)          \
0236     __ADDRESSABLE(fn)
0237 #endif
0238 
0239 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
0240 #define ____define_initcall(fn, __stub, __name, __sec)      \
0241     __define_initcall_stub(__stub, fn)          \
0242     asm(".section   \"" __sec "\", \"a\"        \n" \
0243         __stringify(__name) ":          \n" \
0244         ".long  " __stringify(__stub) " - . \n" \
0245         ".previous                  \n");   \
0246     static_assert(__same_type(initcall_t, &fn));
0247 #else
0248 #define ____define_initcall(fn, __unused, __name, __sec)    \
0249     static initcall_t __name __used             \
0250         __attribute__((__section__(__sec))) = fn;
0251 #endif
0252 
0253 #define __unique_initcall(fn, id, __sec, __iid)         \
0254     ____define_initcall(fn,                 \
0255         __initcall_stub(fn, __iid, id),         \
0256         __initcall_name(initcall, __iid, id),       \
0257         __initcall_section(__sec, __iid))
0258 
0259 #define ___define_initcall(fn, id, __sec)           \
0260     __unique_initcall(fn, id, __sec, __initcall_id(fn))
0261 
0262 #define __define_initcall(fn, id) ___define_initcall(fn, id, .initcall##id)
0263 
0264 /*
0265  * Early initcalls run before initializing SMP.
0266  *
0267  * Only for built-in code, not modules.
0268  */
0269 #define early_initcall(fn)      __define_initcall(fn, early)
0270 
0271 /*
0272  * A "pure" initcall has no dependencies on anything else, and purely
0273  * initializes variables that couldn't be statically initialized.
0274  *
0275  * This only exists for built-in code, not for modules.
0276  * Keep main.c:initcall_level_names[] in sync.
0277  */
0278 #define pure_initcall(fn)       __define_initcall(fn, 0)
0279 
0280 #define core_initcall(fn)       __define_initcall(fn, 1)
0281 #define core_initcall_sync(fn)      __define_initcall(fn, 1s)
0282 #define postcore_initcall(fn)       __define_initcall(fn, 2)
0283 #define postcore_initcall_sync(fn)  __define_initcall(fn, 2s)
0284 #define arch_initcall(fn)       __define_initcall(fn, 3)
0285 #define arch_initcall_sync(fn)      __define_initcall(fn, 3s)
0286 #define subsys_initcall(fn)     __define_initcall(fn, 4)
0287 #define subsys_initcall_sync(fn)    __define_initcall(fn, 4s)
0288 #define fs_initcall(fn)         __define_initcall(fn, 5)
0289 #define fs_initcall_sync(fn)        __define_initcall(fn, 5s)
0290 #define rootfs_initcall(fn)     __define_initcall(fn, rootfs)
0291 #define device_initcall(fn)     __define_initcall(fn, 6)
0292 #define device_initcall_sync(fn)    __define_initcall(fn, 6s)
0293 #define late_initcall(fn)       __define_initcall(fn, 7)
0294 #define late_initcall_sync(fn)      __define_initcall(fn, 7s)
0295 
0296 #define __initcall(fn) device_initcall(fn)
0297 
0298 #define __exitcall(fn)                      \
0299     static exitcall_t __exitcall_##fn __exit_call = fn
0300 
0301 #define console_initcall(fn)    ___define_initcall(fn, con, .con_initcall)
0302 
0303 struct obs_kernel_param {
0304     const char *str;
0305     int (*setup_func)(char *);
0306     int early;
0307 };
0308 
0309 /*
0310  * Only for really core code.  See moduleparam.h for the normal way.
0311  *
0312  * Force the alignment so the compiler doesn't space elements of the
0313  * obs_kernel_param "array" too far apart in .init.setup.
0314  */
0315 #define __setup_param(str, unique_id, fn, early)            \
0316     static const char __setup_str_##unique_id[] __initconst     \
0317         __aligned(1) = str;                     \
0318     static struct obs_kernel_param __setup_##unique_id      \
0319         __used __section(".init.setup")             \
0320         __aligned(__alignof__(struct obs_kernel_param))     \
0321         = { __setup_str_##unique_id, fn, early }
0322 
0323 /*
0324  * NOTE: __setup functions return values:
0325  * @fn returns 1 (or non-zero) if the option argument is "handled"
0326  * and returns 0 if the option argument is "not handled".
0327  */
0328 #define __setup(str, fn)                        \
0329     __setup_param(str, fn, fn, 0)
0330 
0331 /*
0332  * NOTE: @fn is as per module_param, not __setup!
0333  * I.e., @fn returns 0 for no error or non-zero for error
0334  * (possibly @fn returns a -errno value, but it does not matter).
0335  * Emits warning if @fn returns non-zero.
0336  */
0337 #define early_param(str, fn)                        \
0338     __setup_param(str, fn, fn, 1)
0339 
0340 #define early_param_on_off(str_on, str_off, var, config)        \
0341                                     \
0342     int var = IS_ENABLED(config);                   \
0343                                     \
0344     static int __init parse_##var##_on(char *arg)           \
0345     {                               \
0346         var = 1;                        \
0347         return 0;                       \
0348     }                               \
0349     early_param(str_on, parse_##var##_on);              \
0350                                     \
0351     static int __init parse_##var##_off(char *arg)          \
0352     {                               \
0353         var = 0;                        \
0354         return 0;                       \
0355     }                               \
0356     early_param(str_off, parse_##var##_off)
0357 
0358 /* Relies on boot_command_line being set */
0359 void __init parse_early_param(void);
0360 void __init parse_early_options(char *cmdline);
0361 #endif /* __ASSEMBLY__ */
0362 
0363 #else /* MODULE */
0364 
0365 #define __setup_param(str, unique_id, fn)   /* nothing */
0366 #define __setup(str, func)          /* nothing */
0367 #endif
0368 
0369 /* Data marked not to be saved by software suspend */
0370 #define __nosavedata __section(".data..nosave")
0371 
0372 #ifdef MODULE
0373 #define __exit_p(x) x
0374 #else
0375 #define __exit_p(x) NULL
0376 #endif
0377 
0378 #endif /* _LINUX_INIT_H */