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 */
109 typedef int (*initcall_t)(void);
110 typedef void (*exitcall_t)(void);
111
112 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
113 typedef int initcall_entry_t;
114
initcall_from_entry(initcall_entry_t * entry)115 static inline initcall_t initcall_from_entry(initcall_entry_t *entry)
116 {
117 return offset_to_ptr(entry);
118 }
119 #else
120 typedef initcall_t initcall_entry_t;
121
initcall_from_entry(initcall_entry_t * entry)122 static inline initcall_t initcall_from_entry(initcall_entry_t *entry)
123 {
124 return *entry;
125 }
126 #endif
127
128 extern initcall_entry_t __con_initcall_start[], __con_initcall_end[];
129
130 /* Used for constructor calls. */
131 typedef void (*ctor_fn_t)(void);
132
133 struct file_system_type;
134
135 /* Defined in init/main.c */
136 extern int do_one_initcall(initcall_t fn);
137 extern char __initdata boot_command_line[];
138 extern char *saved_command_line;
139 extern unsigned int saved_command_line_len;
140 extern unsigned int reset_devices;
141
142 /* used by init/main.c */
143 void setup_arch(char **);
144 void prepare_namespace(void);
145 void __init init_rootfs(void);
146
147 void init_IRQ(void);
148 void time_init(void);
149 void poking_init(void);
150 void pgtable_cache_init(void);
151
152 extern initcall_entry_t __initcall_start[];
153 extern initcall_entry_t __initcall0_start[];
154 extern initcall_entry_t __initcall1_start[];
155 extern initcall_entry_t __initcall2_start[];
156 extern initcall_entry_t __initcall3_start[];
157 extern initcall_entry_t __initcall4_start[];
158 extern initcall_entry_t __initcall5_start[];
159 extern initcall_entry_t __initcall6_start[];
160 extern initcall_entry_t __initcall7_start[];
161 extern initcall_entry_t __initcall_end[];
162
163 extern struct file_system_type rootfs_fs_type;
164
165 extern bool rodata_enabled;
166 void mark_rodata_ro(void);
167
168 extern void (*late_time_init)(void);
169
170 extern bool initcall_debug;
171
172 #ifdef MODULE
173 extern 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(__KBUILD_MODNAME, \
204 __PASTE(__, \
205 __PASTE(__COUNTER__, \
206 __PASTE(_, \
207 __PASTE(__LINE__, \
208 __PASTE(_, fn))))))
209
210 /* Format: __<prefix>__<iid><id> */
211 #define __initcall_name(prefix, __iid, id) \
212 __PASTE(__, \
213 __PASTE(prefix, \
214 __PASTE(__, \
215 __PASTE(__iid, id))))
216
217 #ifdef CONFIG_LTO_CLANG
218 /*
219 * With LTO, the compiler doesn't necessarily obey link order for
220 * initcalls. In order to preserve the correct order, we add each
221 * variable into its own section and generate a linker script (in
222 * scripts/link-vmlinux.sh) to specify the order of the sections.
223 */
224 #define __initcall_section(__sec, __iid) \
225 #__sec ".init.." #__iid
226
227 /*
228 * With LTO, the compiler can rename static functions to avoid
229 * global naming collisions. We use a global stub function for
230 * initcalls to create a stable symbol name whose address can be
231 * taken in inline assembly when PREL32 relocations are used.
232 */
233 #define __initcall_stub(fn, __iid, id) \
234 __initcall_name(initstub, __iid, id)
235
236 #define __define_initcall_stub(__stub, fn) \
237 int __init __stub(void); \
238 int __init __stub(void) \
239 { \
240 return fn(); \
241 } \
242 __ADDRESSABLE(__stub)
243 #else
244 #define __initcall_section(__sec, __iid) \
245 #__sec ".init"
246
247 #define __initcall_stub(fn, __iid, id) fn
248
249 #define __define_initcall_stub(__stub, fn) \
250 __ADDRESSABLE(fn)
251 #endif
252
253 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
254 #define ____define_initcall(fn, __stub, __name, __sec) \
255 __define_initcall_stub(__stub, fn) \
256 asm(".section \"" __sec "\", \"a\" \n" \
257 __stringify(__name) ": \n" \
258 ".long " __stringify(__stub) " - . \n" \
259 ".previous \n"); \
260 static_assert(__same_type(initcall_t, &fn));
261 #else
262 #define ____define_initcall(fn, __unused, __name, __sec) \
263 static initcall_t __name __used \
264 __attribute__((__section__(__sec))) = fn;
265 #endif
266
267 #define __unique_initcall(fn, id, __sec, __iid) \
268 ____define_initcall(fn, \
269 __initcall_stub(fn, __iid, id), \
270 __initcall_name(initcall, __iid, id), \
271 __initcall_section(__sec, __iid))
272
273 #define ___define_initcall(fn, id, __sec) \
274 __unique_initcall(fn, id, __sec, __initcall_id(fn))
275
276 #define __define_initcall(fn, id) ___define_initcall(fn, id, .initcall##id)
277
278 /*
279 * Early initcalls run before initializing SMP.
280 *
281 * Only for built-in code, not modules.
282 */
283 #define early_initcall(fn) __define_initcall(fn, early)
284
285 /*
286 * A "pure" initcall has no dependencies on anything else, and purely
287 * initializes variables that couldn't be statically initialized.
288 *
289 * This only exists for built-in code, not for modules.
290 * Keep main.c:initcall_level_names[] in sync.
291 */
292 #define pure_initcall(fn) __define_initcall(fn, 0)
293
294 #define core_initcall(fn) __define_initcall(fn, 1)
295 #define core_initcall_sync(fn) __define_initcall(fn, 1s)
296 #define postcore_initcall(fn) __define_initcall(fn, 2)
297 #define postcore_initcall_sync(fn) __define_initcall(fn, 2s)
298 #define arch_initcall(fn) __define_initcall(fn, 3)
299 #define arch_initcall_sync(fn) __define_initcall(fn, 3s)
300 #define subsys_initcall(fn) __define_initcall(fn, 4)
301 #define subsys_initcall_sync(fn) __define_initcall(fn, 4s)
302 #define fs_initcall(fn) __define_initcall(fn, 5)
303 #define fs_initcall_sync(fn) __define_initcall(fn, 5s)
304 #define rootfs_initcall(fn) __define_initcall(fn, rootfs)
305 #define device_initcall(fn) __define_initcall(fn, 6)
306 #define device_initcall_sync(fn) __define_initcall(fn, 6s)
307 #define late_initcall(fn) __define_initcall(fn, 7)
308 #define late_initcall_sync(fn) __define_initcall(fn, 7s)
309
310 #define __initcall(fn) device_initcall(fn)
311
312 #define __exitcall(fn) \
313 static exitcall_t __exitcall_##fn __exit_call = fn
314
315 #define console_initcall(fn) ___define_initcall(fn, con, .con_initcall)
316
317 struct obs_kernel_param {
318 const char *str;
319 int (*setup_func)(char *);
320 int early;
321 };
322
323 extern const struct obs_kernel_param __setup_start[], __setup_end[];
324
325 /*
326 * Only for really core code. See moduleparam.h for the normal way.
327 *
328 * Force the alignment so the compiler doesn't space elements of the
329 * obs_kernel_param "array" too far apart in .init.setup.
330 */
331 #define __setup_param(str, unique_id, fn, early) \
332 static const char __setup_str_##unique_id[] __initconst \
333 __aligned(1) = str; \
334 static struct obs_kernel_param __setup_##unique_id \
335 __used __section(".init.setup") \
336 __aligned(__alignof__(struct obs_kernel_param)) \
337 = { __setup_str_##unique_id, fn, early }
338
339 /*
340 * NOTE: __setup functions return values:
341 * @fn returns 1 (or non-zero) if the option argument is "handled"
342 * and returns 0 if the option argument is "not handled".
343 */
344 #define __setup(str, fn) \
345 __setup_param(str, fn, fn, 0)
346
347 /*
348 * NOTE: @fn is as per module_param, not __setup!
349 * I.e., @fn returns 0 for no error or non-zero for error
350 * (possibly @fn returns a -errno value, but it does not matter).
351 * Emits warning if @fn returns non-zero.
352 */
353 #define early_param(str, fn) \
354 __setup_param(str, fn, fn, 1)
355
356 #define early_param_on_off(str_on, str_off, var, config) \
357 \
358 int var = IS_ENABLED(config); \
359 \
360 static int __init parse_##var##_on(char *arg) \
361 { \
362 var = 1; \
363 return 0; \
364 } \
365 early_param(str_on, parse_##var##_on); \
366 \
367 static int __init parse_##var##_off(char *arg) \
368 { \
369 var = 0; \
370 return 0; \
371 } \
372 early_param(str_off, parse_##var##_off)
373
374 /* Relies on boot_command_line being set */
375 void __init parse_early_param(void);
376 void __init parse_early_options(char *cmdline);
377 #endif /* __ASSEMBLY__ */
378
379 #else /* MODULE */
380
381 #define __setup_param(str, unique_id, fn) /* nothing */
382 #define __setup(str, func) /* nothing */
383 #endif
384
385 /* Data marked not to be saved by software suspend */
386 #define __nosavedata __section(".data..nosave")
387
388 #ifdef MODULE
389 #define __exit_p(x) x
390 #else
391 #define __exit_p(x) NULL
392 #endif
393
394 #endif /* _LINUX_INIT_H */
395