init.h 11 KB

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