init.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. /* These macros are used to mark some functions or
  7. * initialized data (doesn't apply to uninitialized data)
  8. * as `initialization' functions. The kernel can take this
  9. * as hint that the function is used only during the initialization
  10. * phase and free up used memory resources after
  11. *
  12. * Usage:
  13. * For functions:
  14. *
  15. * You should add __init immediately before the function name, like:
  16. *
  17. * static void __init initme(int x, int y)
  18. * {
  19. * extern int z; z = x * y;
  20. * }
  21. *
  22. * If the function has a prototype somewhere, you can also add
  23. * __init between closing brace of the prototype and semicolon:
  24. *
  25. * extern int initialize_foobar_device(int, int, int) __init;
  26. *
  27. * For initialized data:
  28. * You should insert __initdata or __initconst between the variable name
  29. * and equal sign followed by value, e.g.:
  30. *
  31. * static int init_variable __initdata = 0;
  32. * static const char linux_logo[] __initconst = { 0x32, 0x36, ... };
  33. *
  34. * Don't forget to initialize data not at file scope, i.e. within a function,
  35. * as gcc otherwise puts the data into the bss section and not into the init
  36. * section.
  37. */
  38. /* These are for everybody (although not all archs will actually
  39. discard it in modules) */
  40. #define __init __section(.init.text) __cold __latent_entropy
  41. #define __initdata __section(.init.data)
  42. #define __initconst __section(.init.rodata)
  43. #define __exitdata __section(.exit.data)
  44. #define __exit_call __used __section(.exitcall.exit)
  45. /*
  46. * modpost check for section mismatches during the kernel build.
  47. * A section mismatch happens when there are references from a
  48. * code or data section to an init section (both code or data).
  49. * The init sections are (for most archs) discarded by the kernel
  50. * when early init has completed so all such references are potential bugs.
  51. * For exit sections the same issue exists.
  52. *
  53. * The following markers are used for the cases where the reference to
  54. * the *init / *exit section (code or data) is valid and will teach
  55. * modpost not to issue a warning. Intended semantics is that a code or
  56. * data tagged __ref* can reference code or data from init section without
  57. * producing a warning (of course, no warning does not mean code is
  58. * correct, so optimally document why the __ref is needed and why it's OK).
  59. *
  60. * The markers follow same syntax rules as __init / __initdata.
  61. */
  62. #define __ref __section(.ref.text) noinline
  63. #define __refdata __section(.ref.data)
  64. #define __refconst __section(.ref.rodata)
  65. #ifdef MODULE
  66. #define __exitused
  67. #else
  68. #define __exitused __used
  69. #endif
  70. #define __exit __section(.exit.text) __exitused __cold notrace
  71. /* Used for MEMORY_HOTPLUG */
  72. #define __meminit __section(.meminit.text) __cold notrace \
  73. __latent_entropy
  74. #define __meminitdata __section(.meminit.data)
  75. #define __meminitconst __section(.meminit.rodata)
  76. #define __memexit __section(.memexit.text) __exitused __cold notrace
  77. #define __memexitdata __section(.memexit.data)
  78. #define __memexitconst __section(.memexit.rodata)
  79. /* For assembly routines */
  80. #define __HEAD .section ".head.text","ax"
  81. #define __INIT .section ".init.text","ax"
  82. #define __FINIT .previous
  83. #define __INITDATA .section ".init.data","aw",%progbits
  84. #define __INITRODATA .section ".init.rodata","a",%progbits
  85. #define __FINITDATA .previous
  86. #define __MEMINIT .section ".meminit.text", "ax"
  87. #define __MEMINITDATA .section ".meminit.data", "aw"
  88. #define __MEMINITRODATA .section ".meminit.rodata", "a"
  89. /* silence warnings when references are OK */
  90. #define __REF .section ".ref.text", "ax"
  91. #define __REFDATA .section ".ref.data", "aw"
  92. #define __REFCONST .section ".ref.rodata", "a"
  93. #ifndef __ASSEMBLY__
  94. /*
  95. * Used for initialization calls..
  96. */
  97. typedef int (*initcall_t)(void);
  98. typedef void (*exitcall_t)(void);
  99. extern initcall_t __con_initcall_start[], __con_initcall_end[];
  100. extern initcall_t __security_initcall_start[], __security_initcall_end[];
  101. /* Used for contructor calls. */
  102. typedef void (*ctor_fn_t)(void);
  103. /* Defined in init/main.c */
  104. extern int do_one_initcall(initcall_t fn);
  105. extern char __initdata boot_command_line[];
  106. extern char *saved_command_line;
  107. extern unsigned int reset_devices;
  108. /* used by init/main.c */
  109. void setup_arch(char **);
  110. void prepare_namespace(void);
  111. void __init load_default_modules(void);
  112. int __init init_rootfs(void);
  113. #if defined(CONFIG_STRICT_KERNEL_RWX) || defined(CONFIG_STRICT_MODULE_RWX)
  114. extern bool rodata_enabled;
  115. #endif
  116. #ifdef CONFIG_STRICT_KERNEL_RWX
  117. void mark_rodata_ro(void);
  118. #endif
  119. extern void (*late_time_init)(void);
  120. extern bool initcall_debug;
  121. #endif
  122. #ifndef MODULE
  123. #ifndef __ASSEMBLY__
  124. /*
  125. * initcalls are now grouped by functionality into separate
  126. * subsections. Ordering inside the subsections is determined
  127. * by link order.
  128. * For backwards compatibility, initcall() puts the call in
  129. * the device init subsection.
  130. *
  131. * The `id' arg to __define_initcall() is needed so that multiple initcalls
  132. * can point at the same handler without causing duplicate-symbol build errors.
  133. *
  134. * Initcalls are run by placing pointers in initcall sections that the
  135. * kernel iterates at runtime. The linker can do dead code / data elimination
  136. * and remove that completely, so the initcall sections have to be marked
  137. * as KEEP() in the linker script.
  138. */
  139. #define __define_initcall(fn, id) \
  140. static initcall_t __initcall_##fn##id __used \
  141. __attribute__((__section__(".initcall" #id ".init"))) = fn;
  142. /*
  143. * Early initcalls run before initializing SMP.
  144. *
  145. * Only for built-in code, not modules.
  146. */
  147. #define early_initcall(fn) __define_initcall(fn, early)
  148. /*
  149. * A "pure" initcall has no dependencies on anything else, and purely
  150. * initializes variables that couldn't be statically initialized.
  151. *
  152. * This only exists for built-in code, not for modules.
  153. * Keep main.c:initcall_level_names[] in sync.
  154. */
  155. #define pure_initcall(fn) __define_initcall(fn, 0)
  156. #define core_initcall(fn) __define_initcall(fn, 1)
  157. #define core_initcall_sync(fn) __define_initcall(fn, 1s)
  158. #define postcore_initcall(fn) __define_initcall(fn, 2)
  159. #define postcore_initcall_sync(fn) __define_initcall(fn, 2s)
  160. #define arch_initcall(fn) __define_initcall(fn, 3)
  161. #define arch_initcall_sync(fn) __define_initcall(fn, 3s)
  162. #define subsys_initcall(fn) __define_initcall(fn, 4)
  163. #define subsys_initcall_sync(fn) __define_initcall(fn, 4s)
  164. #define fs_initcall(fn) __define_initcall(fn, 5)
  165. #define fs_initcall_sync(fn) __define_initcall(fn, 5s)
  166. #define rootfs_initcall(fn) __define_initcall(fn, rootfs)
  167. #define device_initcall(fn) __define_initcall(fn, 6)
  168. #define device_initcall_sync(fn) __define_initcall(fn, 6s)
  169. #define late_initcall(fn) __define_initcall(fn, 7)
  170. #define late_initcall_sync(fn) __define_initcall(fn, 7s)
  171. #define __initcall(fn) device_initcall(fn)
  172. #define __exitcall(fn) \
  173. static exitcall_t __exitcall_##fn __exit_call = fn
  174. #define console_initcall(fn) \
  175. static initcall_t __initcall_##fn \
  176. __used __section(.con_initcall.init) = fn
  177. #define security_initcall(fn) \
  178. static initcall_t __initcall_##fn \
  179. __used __section(.security_initcall.init) = fn
  180. struct obs_kernel_param {
  181. const char *str;
  182. int (*setup_func)(char *);
  183. int early;
  184. };
  185. /*
  186. * Only for really core code. See moduleparam.h for the normal way.
  187. *
  188. * Force the alignment so the compiler doesn't space elements of the
  189. * obs_kernel_param "array" too far apart in .init.setup.
  190. */
  191. #define __setup_param(str, unique_id, fn, early) \
  192. static const char __setup_str_##unique_id[] __initconst \
  193. __aligned(1) = str; \
  194. static struct obs_kernel_param __setup_##unique_id \
  195. __used __section(.init.setup) \
  196. __attribute__((aligned((sizeof(long))))) \
  197. = { __setup_str_##unique_id, fn, early }
  198. #define __setup(str, fn) \
  199. __setup_param(str, fn, fn, 0)
  200. /*
  201. * NOTE: fn is as per module_param, not __setup!
  202. * Emits warning if fn returns non-zero.
  203. */
  204. #define early_param(str, fn) \
  205. __setup_param(str, fn, fn, 1)
  206. #define early_param_on_off(str_on, str_off, var, config) \
  207. \
  208. int var = IS_ENABLED(config); \
  209. \
  210. static int __init parse_##var##_on(char *arg) \
  211. { \
  212. var = 1; \
  213. return 0; \
  214. } \
  215. __setup_param(str_on, parse_##var##_on, parse_##var##_on, 1); \
  216. \
  217. static int __init parse_##var##_off(char *arg) \
  218. { \
  219. var = 0; \
  220. return 0; \
  221. } \
  222. __setup_param(str_off, parse_##var##_off, parse_##var##_off, 1)
  223. /* Relies on boot_command_line being set */
  224. void __init parse_early_param(void);
  225. void __init parse_early_options(char *cmdline);
  226. #endif /* __ASSEMBLY__ */
  227. #else /* MODULE */
  228. #define __setup_param(str, unique_id, fn) /* nothing */
  229. #define __setup(str, func) /* nothing */
  230. #endif
  231. /* Data marked not to be saved by software suspend */
  232. #define __nosavedata __section(.data..nosave)
  233. #ifdef MODULE
  234. #define __exit_p(x) x
  235. #else
  236. #define __exit_p(x) NULL
  237. #endif
  238. #endif /* _LINUX_INIT_H */