init.h 9.3 KB

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