]> git.ipfire.org Git - thirdparty/linux.git/blame - init/main.c
Merge tag 'io_uring-6.16-20250630' of git://git.kernel.dk/linux
[thirdparty/linux.git] / init / main.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * linux/init/main.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 *
7 * GK 2/5/95 - Changed to support mounting root fs via NFS
8 * Added initrd & change_root: Werner Almesberger & Hans Lermen, Feb '96
9 * Moan early if gcc is old, avoiding bogus kernels - Paul Gortmaker, May '96
dd4d9fec 10 * Simplified starting of init: Michael A. Griffith <grif@acm.org>
1da177e4
LT
11 */
12
ea676e84
AM
13#define DEBUG /* Enable initcall_debug */
14
1da177e4 15#include <linux/types.h>
66ac1a4d 16#include <linux/export.h>
8a293be0 17#include <linux/extable.h>
1da177e4
LT
18#include <linux/module.h>
19#include <linux/proc_fs.h>
5c2c5c55 20#include <linux/binfmts.h>
1da177e4
LT
21#include <linux/kernel.h>
22#include <linux/syscalls.h>
9b5609fd 23#include <linux/stackprotector.h>
1da177e4
LT
24#include <linux/string.h>
25#include <linux/ctype.h>
26#include <linux/delay.h>
1da177e4
LT
27#include <linux/ioport.h>
28#include <linux/init.h>
1da177e4 29#include <linux/initrd.h>
57c8a661 30#include <linux/memblock.h>
4a7a16dc 31#include <linux/acpi.h>
7684b858 32#include <linux/bootconfig.h>
0c688614 33#include <linux/console.h>
38b8d208 34#include <linux/nmi.h>
1da177e4
LT
35#include <linux/percpu.h>
36#include <linux/kmod.h>
82d083ab 37#include <linux/kprobes.h>
3c206509 38#include <linux/kmsan.h>
db64fe02 39#include <linux/vmalloc.h>
1da177e4 40#include <linux/kernel_stat.h>
d7cd5611 41#include <linux/start_kernel.h>
1da177e4 42#include <linux/security.h>
3d442233 43#include <linux/smp.h>
1da177e4 44#include <linux/profile.h>
0ce20dd8 45#include <linux/kfence.h>
1da177e4 46#include <linux/rcupdate.h>
8e9c01c7 47#include <linux/srcu.h>
1da177e4
LT
48#include <linux/moduleparam.h>
49#include <linux/kallsyms.h>
83cc6fa0 50#include <linux/buildid.h>
1da177e4
LT
51#include <linux/writeback.h>
52#include <linux/cpu.h>
53#include <linux/cpuset.h>
bc9817bb 54#include <linux/memcontrol.h>
ddbcc7e8 55#include <linux/cgroup.h>
1da177e4 56#include <linux/efi.h>
906568c9 57#include <linux/tick.h>
78634061 58#include <linux/sched/isolation.h>
6168a702 59#include <linux/interrupt.h>
c757249a 60#include <linux/taskstats_kern.h>
ca74e92b 61#include <linux/delayacct.h>
1da177e4 62#include <linux/unistd.h>
3ea056c5 63#include <linux/utsname.h>
1da177e4
LT
64#include <linux/rmap.h>
65#include <linux/mempolicy.h>
66#include <linux/key.h>
9a11b49a 67#include <linux/debug_locks.h>
3ac7fe5a 68#include <linux/debugobjects.h>
fbb9ce95 69#include <linux/lockdep.h>
3c7b4e6b 70#include <linux/kmemleak.h>
f1b192b1 71#include <linux/padata.h>
84d73786 72#include <linux/pid_namespace.h>
4c002c97 73#include <linux/device/driver.h>
73c27992 74#include <linux/kthread.h>
e6fe6649 75#include <linux/sched.h>
1777e463 76#include <linux/sched/init.h>
a1c9eea9 77#include <linux/signal.h>
199f0ca5 78#include <linux/idr.h>
0b4b3827 79#include <linux/kgdb.h>
68bf21aa 80#include <linux/ftrace.h>
22a9d645 81#include <linux/async.h>
2b2af54a 82#include <linux/shmem_fs.h>
5a0e3ad6 83#include <linux/slab.h>
24a24bb6 84#include <linux/perf_event.h>
a74fb73c 85#include <linux/ptrace.h>
aa8c6248 86#include <linux/pti.h>
bb813f4c 87#include <linux/blkdev.h>
5d2a4e91 88#include <linux/sched/clock.h>
29930025 89#include <linux/sched/task.h>
68db0cf1 90#include <linux/sched/task_stack.h>
65f382fd 91#include <linux/context_tracking.h>
47d06e53 92#include <linux/random.h>
8f8cd6c0 93#include <linux/moduleloader.h>
7b0b73d7 94#include <linux/list.h>
c9cd2ce2 95#include <linux/integrity.h>
e149ed2b 96#include <linux/proc_ns.h>
0ddab1d2 97#include <linux/io.h>
39290b38 98#include <linux/cache.h>
2959a5f7 99#include <linux/rodata_test.h>
33352244 100#include <linux/jump_label.h>
dfd402a4 101#include <linux/kcsan.h>
eb9d7d39 102#include <linux/init_syscalls.h>
e1fdc403 103#include <linux/stackdepot.h>
375561bd 104#include <linux/randomize_kstack.h>
cb12fd8e 105#include <linux/pidfs.h>
a5e8131a 106#include <linux/ptdump.h>
9c1be193 107#include <net/net_namespace.h>
1da177e4
LT
108
109#include <asm/io.h>
1da177e4 110#include <asm/setup.h>
a940199f 111#include <asm/sections.h>
37b73c82 112#include <asm/cacheflush.h>
1da177e4 113
4ee7c60d
SRV
114#define CREATE_TRACE_POINTS
115#include <trace/events/initcall.h>
116
8c0d8849
BH
117#include <kunit/test.h>
118
aae5f662 119static int kernel_init(void *);
1da177e4 120
2ce802f6
TH
121/*
122 * Debug helper: via this flag we know that we are in 'early bootup code'
123 * where only the boot processor is running with IRQ disabled. This means
124 * two things - IRQ must not be enabled before the flag is cleared and some
125 * operations which are not allowed with IRQ disabled are allowed while the
126 * flag is set.
127 */
128bool early_boot_irqs_disabled __read_mostly;
129
a6826048 130enum system_states system_state __read_mostly;
1da177e4
LT
131EXPORT_SYMBOL(system_state);
132
133/*
134 * Boot command-line arguments
135 */
136#define MAX_INIT_ARGS CONFIG_INIT_ENV_ARG_LIMIT
137#define MAX_INIT_ENVS CONFIG_INIT_ENV_ARG_LIMIT
138
1da177e4 139/* Default late time init is NULL. archs can override this later. */
d2e3192b 140void (*__initdata late_time_init)(void);
1da177e4 141
30d7e0d4
ABL
142/* Untouched command line saved by arch-specific code. */
143char __initdata boot_command_line[COMMAND_LINE_SIZE];
144/* Untouched saved command line (eg. for /proc) */
941baf6f
AD
145char *saved_command_line __ro_after_init;
146unsigned int saved_command_line_len __ro_after_init;
30d7e0d4
ABL
147/* Command line for parameter parsing */
148static char *static_command_line;
51887d03
MH
149/* Untouched extra command line */
150static char *extra_command_line;
13199162
MH
151/* Extra init arguments */
152static char *extra_init_args;
1da177e4 153
f61872bb
SRV
154#ifdef CONFIG_BOOT_CONFIG
155/* Is bootconfig on command line? */
caa0708a 156static bool bootconfig_found;
b66fbbe8 157static size_t initargs_offs;
f61872bb
SRV
158#else
159# define bootconfig_found false
b66fbbe8 160# define initargs_offs 0
f61872bb
SRV
161#endif
162
1da177e4 163static char *execute_command;
916db733 164static char *ramdisk_execute_command = "/init";
1da177e4 165
c4b2c0c5
HFS
166/*
167 * Used to generate warnings if static_key manipulation functions are used
168 * before jump_label_init is called.
169 */
dd4d9fec 170bool static_key_initialized __read_mostly;
c4b2c0c5
HFS
171EXPORT_SYMBOL_GPL(static_key_initialized);
172
8b3b2955
JB
173/*
174 * If set, this is an indication to the drivers that reset the underlying
175 * device before going ahead with the initialization otherwise driver might
176 * rely on the BIOS and skip the reset operation.
177 *
178 * This is useful if kernel is booting in an unreliable environment.
fc454fdc 179 * For ex. kdump situation where previous kernel has crashed, BIOS has been
8b3b2955
JB
180 * skipped and devices will be in unknown state.
181 */
182unsigned int reset_devices;
183EXPORT_SYMBOL(reset_devices);
1da177e4 184
7e96287d
VG
185static int __init set_reset_devices(char *str)
186{
187 reset_devices = 1;
188 return 1;
189}
190
191__setup("reset_devices", set_reset_devices);
192
dd4d9fec
FF
193static const char *argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
194const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
1da177e4
LT
195static const char *panic_later, *panic_param;
196
31c025b5 197static bool __init obsolete_checksetup(char *line)
1da177e4 198{
914dcaa8 199 const struct obs_kernel_param *p;
31c025b5 200 bool had_early_param = false;
1da177e4
LT
201
202 p = __setup_start;
203 do {
204 int n = strlen(p->str);
b1e4d20c 205 if (parameqn(line, p->str, n)) {
1da177e4 206 if (p->early) {
33df0d19
RR
207 /* Already done in parse_early_param?
208 * (Needs exact match on param part).
209 * Keep iterating, as we can have early
210 * params and __setups of same names 8( */
1da177e4 211 if (line[n] == '\0' || line[n] == '=')
31c025b5 212 had_early_param = true;
1da177e4 213 } else if (!p->setup_func) {
ea676e84
AM
214 pr_warn("Parameter %s is obsolete, ignored\n",
215 p->str);
31c025b5 216 return true;
1da177e4 217 } else if (p->setup_func(line + n))
31c025b5 218 return true;
1da177e4
LT
219 }
220 p++;
221 } while (p < __setup_end);
33df0d19
RR
222
223 return had_early_param;
1da177e4
LT
224}
225
226/*
227 * This should be approx 2 Bo*oMips to start (note initial shift), and will
228 * still work even if initially too large, it will just take slightly longer
229 */
230unsigned long loops_per_jiffy = (1<<12);
1da177e4
LT
231EXPORT_SYMBOL(loops_per_jiffy);
232
233static int __init debug_kernel(char *str)
234{
a8fe19eb 235 console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
f6f21c81 236 return 0;
1da177e4
LT
237}
238
239static int __init quiet_kernel(char *str)
240{
a8fe19eb 241 console_loglevel = CONSOLE_LOGLEVEL_QUIET;
f6f21c81 242 return 0;
1da177e4
LT
243}
244
f6f21c81
YL
245early_param("debug", debug_kernel);
246early_param("quiet", quiet_kernel);
1da177e4
LT
247
248static int __init loglevel(char *str)
249{
808bf29b
AS
250 int newlevel;
251
252 /*
253 * Only update loglevel value when a correct setting was passed,
254 * to prevent blind crashes (when loglevel being set to 0) that
255 * are quite hard to debug
256 */
257 if (get_option(&str, &newlevel)) {
258 console_loglevel = newlevel;
259 return 0;
260 }
261
262 return -EINVAL;
1da177e4
LT
263}
264
f6f21c81 265early_param("loglevel", loglevel);
1da177e4 266
de462e5f 267#ifdef CONFIG_BLK_DEV_INITRD
a2a9d67a 268static void * __init get_boot_config_from_initrd(size_t *_size)
de462e5f
MH
269{
270 u32 size, csum;
271 char *data;
272 u32 *hdr;
50b8a742 273 int i;
de462e5f
MH
274
275 if (!initrd_end)
276 return NULL;
277
278 data = (char *)initrd_end - BOOTCONFIG_MAGIC_LEN;
50b8a742
MH
279 /*
280 * Since Grub may align the size of initrd to 4, we must
281 * check the preceding 3 bytes as well.
282 */
283 for (i = 0; i < 4; i++) {
284 if (!memcmp(data, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN))
285 goto found;
286 data--;
287 }
288 return NULL;
de462e5f 289
50b8a742 290found:
de462e5f 291 hdr = (u32 *)(data - 8);
24aed094
MH
292 size = le32_to_cpu(hdr[0]);
293 csum = le32_to_cpu(hdr[1]);
de462e5f
MH
294
295 data = ((void *)hdr) - size;
296 if ((unsigned long)data < initrd_start) {
297 pr_err("bootconfig size %d is greater than initrd size %ld\n",
298 size, initrd_end - initrd_start);
299 return NULL;
300 }
301
765b8552
MH
302 if (xbc_calc_checksum(data, size) != csum) {
303 pr_err("bootconfig checksum failed\n");
304 return NULL;
305 }
306
de462e5f
MH
307 /* Remove bootconfig from initramfs/initrd */
308 initrd_end = (unsigned long)data;
309 if (_size)
310 *_size = size;
de462e5f
MH
311
312 return data;
313}
314#else
a2a9d67a 315static void * __init get_boot_config_from_initrd(size_t *_size)
de462e5f
MH
316{
317 return NULL;
318}
319#endif
320
7684b858 321#ifdef CONFIG_BOOT_CONFIG
51887d03 322
a27026e9 323static char xbc_namebuf[XBC_KEYLEN_MAX] __initdata;
51887d03
MH
324
325#define rest(dst, end) ((end) > (dst) ? (end) - (dst) : 0)
326
327static int __init xbc_snprint_cmdline(char *buf, size_t size,
328 struct xbc_node *root)
329{
330 struct xbc_node *knode, *vnode;
331 char *end = buf + size;
efee03a5 332 const char *val, *q;
51887d03
MH
333 int ret;
334
335 xbc_node_for_each_key_value(root, knode, val) {
336 ret = xbc_node_compose_key_after(root, knode,
337 xbc_namebuf, XBC_KEYLEN_MAX);
338 if (ret < 0)
339 return ret;
340
341 vnode = xbc_node_get_child(knode);
88b91371
MH
342 if (!vnode) {
343 ret = snprintf(buf, rest(buf, end), "%s ", xbc_namebuf);
344 if (ret < 0)
345 return ret;
346 buf += ret;
51887d03 347 continue;
88b91371 348 }
51887d03 349 xbc_array_for_each_value(vnode, val) {
212f863f
RV
350 /*
351 * For prettier and more readable /proc/cmdline, only
352 * quote the value when necessary, i.e. when it contains
353 * whitespace.
354 */
efee03a5
RV
355 q = strpbrk(val, " \t\r\n") ? "\"" : "";
356 ret = snprintf(buf, rest(buf, end), "%s=%s%s%s ",
357 xbc_namebuf, q, val, q);
51887d03
MH
358 if (ret < 0)
359 return ret;
360 buf += ret;
51887d03 361 }
51887d03
MH
362 }
363
364 return buf - (end - size);
365}
366#undef rest
367
368/* Make an extra command line under given key word */
369static char * __init xbc_make_cmdline(const char *key)
370{
371 struct xbc_node *root;
372 char *new_cmdline;
373 int ret, len = 0;
374
375 root = xbc_find_node(key);
376 if (!root)
377 return NULL;
378
379 /* Count required buffer size */
380 len = xbc_snprint_cmdline(NULL, 0, root);
381 if (len <= 0)
382 return NULL;
383
384 new_cmdline = memblock_alloc(len + 1, SMP_CACHE_BYTES);
385 if (!new_cmdline) {
386 pr_err("Failed to allocate memory for extra kernel cmdline.\n");
387 return NULL;
388 }
389
390 ret = xbc_snprint_cmdline(new_cmdline, len + 1, root);
391 if (ret < 0 || ret > len) {
392 pr_err("Failed to print extra kernel cmdline.\n");
4421cca0 393 memblock_free(new_cmdline, len + 1);
51887d03
MH
394 return NULL;
395 }
396
397 return new_cmdline;
398}
399
f61872bb
SRV
400static int __init bootconfig_params(char *param, char *val,
401 const char *unused, void *arg)
402{
403 if (strcmp(param, "bootconfig") == 0) {
404 bootconfig_found = true;
f61872bb
SRV
405 }
406 return 0;
407}
408
d0ac5fba
MH
409static int __init warn_bootconfig(char *str)
410{
411 /* The 'bootconfig' has been handled by bootconfig_params(). */
412 return 0;
413}
414
2b7d2fe7 415static void __init setup_boot_config(void)
7684b858 416{
f61872bb 417 static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata;
a2a9d67a
MH
418 const char *msg, *data;
419 int pos, ret;
420 size_t size;
421 char *err;
7684b858 422
611d0a95 423 /* Cut out the bootconfig data even if we have no bootconfig option */
765b8552 424 data = get_boot_config_from_initrd(&size);
a2a9d67a
MH
425 /* If there is no bootconfig in initrd, try embedded one. */
426 if (!data)
427 data = xbc_get_embedded_bootconfig(&size);
de462e5f 428
a1d3a6d9 429 strscpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);
477d0847
MH
430 err = parse_args("bootconfig", tmp_cmdline, NULL, 0, 0, 0, NULL,
431 bootconfig_params);
f61872bb 432
caa0708a 433 if (IS_ERR(err) || !(bootconfig_found || IS_ENABLED(CONFIG_BOOT_CONFIG_FORCE)))
7684b858
MH
434 return;
435
b66fbbe8 436 /* parse_args() stops at the next param of '--' and returns an address */
477d0847 437 if (err)
b66fbbe8 438 initargs_offs = err - tmp_cmdline;
477d0847 439
611d0a95 440 if (!data) {
caa0708a
MHG
441 /* If user intended to use bootconfig, show an error level message */
442 if (bootconfig_found)
443 pr_err("'bootconfig' found on command line, but no bootconfig found\n");
444 else
445 pr_info("No bootconfig data provided, so skipping bootconfig");
611d0a95
MH
446 return;
447 }
448
7495e092 449 if (size >= XBC_DATA_MAX) {
a2a9d67a
MH
450 pr_err("bootconfig size %ld greater than max size %d\n",
451 (long)size, XBC_DATA_MAX);
7684b858 452 return;
7495e092 453 }
7684b858 454
bdac5c2b 455 ret = xbc_init(data, size, &msg, &pos);
89b74cac
MH
456 if (ret < 0) {
457 if (pos < 0)
458 pr_err("Failed to init bootconfig: %s.\n", msg);
459 else
460 pr_err("Failed to parse bootconfig: %s at %d.\n",
461 msg, pos);
462 } else {
e306220c 463 xbc_get_info(&ret, NULL);
a2a9d67a 464 pr_info("Load bootconfig: %ld bytes %d nodes\n", (long)size, ret);
51887d03
MH
465 /* keys starting with "kernel." are passed via cmdline */
466 extra_command_line = xbc_make_cmdline("kernel");
13199162
MH
467 /* Also, "init." keys are init arguments */
468 extra_init_args = xbc_make_cmdline("init");
51887d03 469 }
7495e092 470 return;
7684b858 471}
de462e5f 472
40caa127
MH
473static void __init exit_boot_config(void)
474{
115d4d08 475 xbc_exit();
40caa127
MH
476}
477
478#else /* !CONFIG_BOOT_CONFIG */
de462e5f 479
2b7d2fe7 480static void __init setup_boot_config(void)
de462e5f
MH
481{
482 /* Remove bootconfig data from initrd */
765b8552 483 get_boot_config_from_initrd(NULL);
de462e5f 484}
d8a953dd
MH
485
486static int __init warn_bootconfig(char *str)
487{
36c6aa26 488 pr_warn("WARNING: 'bootconfig' found on the kernel command line but CONFIG_BOOT_CONFIG is not set.\n");
d8a953dd
MH
489 return 0;
490}
40caa127
MH
491
492#define exit_boot_config() do {} while (0)
493
494#endif /* CONFIG_BOOT_CONFIG */
495
d0ac5fba 496early_param("bootconfig", warn_bootconfig);
7684b858 497
c722cea2
MH
498bool __init cmdline_has_extra_options(void)
499{
500 return extra_command_line || extra_init_args;
501}
502
a99cd112 503/* Change NUL term back to "=", to make "param" the whole string. */
7e2762e1 504static void __init repair_env_string(char *param, char *val)
1da177e4 505{
1da177e4
LT
506 if (val) {
507 /* param=val or param="val"? */
508 if (val == param+strlen(param)+1)
509 val[-1] = '=';
510 else if (val == param+strlen(param)+2) {
511 val[-2] = '=';
512 memmove(val-1, val, strlen(val)+1);
1da177e4
LT
513 } else
514 BUG();
515 }
a99cd112
CM
516}
517
51e158c1 518/* Anything after -- gets handed straight to init. */
ecc86170
LR
519static int __init set_init_arg(char *param, char *val,
520 const char *unused, void *arg)
51e158c1
RR
521{
522 unsigned int i;
523
524 if (panic_later)
525 return 0;
526
7e2762e1 527 repair_env_string(param, val);
51e158c1
RR
528
529 for (i = 0; argv_init[i]; i++) {
530 if (i == MAX_INIT_ARGS) {
531 panic_later = "init";
532 panic_param = param;
533 return 0;
534 }
535 }
536 argv_init[i] = param;
537 return 0;
538}
539
a99cd112
CM
540/*
541 * Unknown boot options get handed to init, unless they look like
542 * unused parameters (modprobe will find them in /proc/cmdline).
543 */
ecc86170
LR
544static int __init unknown_bootoption(char *param, char *val,
545 const char *unused, void *arg)
a99cd112 546{
283900e8
AS
547 size_t len = strlen(param);
548
8001f493
KJ
549 /* Handle params aliased to sysctls */
550 if (sysctl_is_alias(param))
551 return 0;
552
7e2762e1 553 repair_env_string(param, val);
1da177e4
LT
554
555 /* Handle obsolete-style parameters */
556 if (obsolete_checksetup(param))
557 return 0;
558
f066a4f6 559 /* Unused module parameter. */
283900e8 560 if (strnchr(param, len, '.'))
1da177e4 561 return 0;
1da177e4
LT
562
563 if (panic_later)
564 return 0;
565
566 if (val) {
567 /* Environment option */
568 unsigned int i;
569 for (i = 0; envp_init[i]; i++) {
570 if (i == MAX_INIT_ENVS) {
499a4584 571 panic_later = "env";
1da177e4
LT
572 panic_param = param;
573 }
283900e8 574 if (!strncmp(param, envp_init[i], len+1))
1da177e4
LT
575 break;
576 }
577 envp_init[i] = param;
578 } else {
579 /* Command line option */
580 unsigned int i;
581 for (i = 0; argv_init[i]; i++) {
582 if (i == MAX_INIT_ARGS) {
499a4584 583 panic_later = "init";
1da177e4
LT
584 panic_param = param;
585 }
586 }
587 argv_init[i] = param;
588 }
589 return 0;
590}
591
592static int __init init_setup(char *str)
593{
594 unsigned int i;
595
596 execute_command = str;
597 /*
598 * In case LILO is going to boot us with default command line,
599 * it prepends "auto" before the whole cmdline which makes
600 * the shell think it should execute a script with such name.
601 * So we ignore all arguments entered _before_ init=... [MJ]
602 */
603 for (i = 1; i < MAX_INIT_ARGS; i++)
604 argv_init[i] = NULL;
605 return 1;
606}
607__setup("init=", init_setup);
608
ffdfc409
OJ
609static int __init rdinit_setup(char *str)
610{
611 unsigned int i;
612
613 ramdisk_execute_command = str;
614 /* See "auto" comment in init_setup */
615 for (i = 1; i < MAX_INIT_ARGS; i++)
616 argv_init[i] = NULL;
617 return 1;
618}
619__setup("rdinit=", rdinit_setup);
620
1da177e4 621#ifndef CONFIG_SMP
e0982e90 622static inline void setup_nr_cpu_ids(void) { }
1da177e4 623static inline void smp_prepare_cpus(unsigned int maxcpus) { }
1da177e4
LT
624#endif
625
30d7e0d4
ABL
626/*
627 * We need to store the untouched command line for future reference.
628 * We also need to store the touched command line since the parameter
629 * parsing is performed in place, and we should allow a component to
630 * store reference of name/value for future reference.
631 */
632static void __init setup_command_line(char *command_line)
633{
13199162 634 size_t len, xlen = 0, ilen = 0;
f5c7310a 635
51887d03
MH
636 if (extra_command_line)
637 xlen = strlen(extra_command_line);
cd24bdb0
YW
638 if (extra_init_args) {
639 extra_init_args = strim(extra_init_args); /* remove trailing space */
13199162 640 ilen = strlen(extra_init_args) + 4; /* for " -- " */
cd24bdb0 641 }
f5c7310a 642
ddd53363 643 len = xlen + strlen(boot_command_line) + ilen + 1;
f5c7310a 644
c6f23979 645 saved_command_line = memblock_alloc_or_panic(len, SMP_CACHE_BYTES);
f5c7310a 646
46dad3c1
YW
647 len = xlen + strlen(command_line) + 1;
648
c6f23979 649 static_command_line = memblock_alloc_or_panic(len, SMP_CACHE_BYTES);
f5c7310a 650
51887d03
MH
651 if (xlen) {
652 /*
653 * We have to put extra_command_line before boot command
654 * lines because there could be dashes (separator of init
655 * command line) in the command lines.
656 */
657 strcpy(saved_command_line, extra_command_line);
658 strcpy(static_command_line, extra_command_line);
659 }
660 strcpy(saved_command_line + xlen, boot_command_line);
661 strcpy(static_command_line + xlen, command_line);
13199162
MH
662
663 if (ilen) {
664 /*
665 * Append supplemental init boot args to saved_command_line
666 * so that user can check what command line options passed
667 * to init.
b66fbbe8
MH
668 * The order should always be
669 * " -- "[bootconfig init-param][cmdline init-param]
13199162 670 */
b66fbbe8
MH
671 if (initargs_offs) {
672 len = xlen + initargs_offs;
673 strcpy(saved_command_line + len, extra_init_args);
674 len += ilen - 4; /* strlen(extra_init_args) */
675 strcpy(saved_command_line + len,
676 boot_command_line + initargs_offs - 1);
f61872bb 677 } else {
b66fbbe8 678 len = strlen(saved_command_line);
13199162
MH
679 strcpy(saved_command_line + len, " -- ");
680 len += 4;
b66fbbe8 681 strcpy(saved_command_line + len, extra_init_args);
f61872bb 682 }
13199162 683 }
941baf6f
AD
684
685 saved_command_line_len = strlen(saved_command_line);
30d7e0d4
ABL
686}
687
1da177e4
LT
688/*
689 * We need to finalize in a non-__init function or else race conditions
690 * between the root thread and the init thread may cause start_kernel to
691 * be reaped by free_initmem before the root thread has proceeded to
692 * cpu_idle.
693 *
694 * gcc-3.4 accidentally inlines this function, so use noinline.
695 */
696
b433c3d4
PZ
697static __initdata DECLARE_COMPLETION(kthreadd_done);
698
ac4db926 699static noinline void __ref __noreturn rest_init(void)
1da177e4 700{
8fb12156 701 struct task_struct *tsk;
73c27992
EB
702 int pid;
703
7db905e6 704 rcu_scheduler_starting();
b433c3d4 705 /*
97158569 706 * We need to spawn init first so that it obtains pid 1, however
b433c3d4
PZ
707 * the init task will end up wanting to create kthreads, which, if
708 * we schedule it before we create kthreadd, will OOPS.
709 */
343f4c49 710 pid = user_mode_thread(kernel_init, NULL, CLONE_FS);
8fb12156
TG
711 /*
712 * Pin init on the boot CPU. Task migration is not properly working
713 * until sched_init_smp() has been run. It will set the allowed
714 * CPUs for init to the non isolated CPUs.
715 */
716 rcu_read_lock();
717 tsk = find_task_by_pid_ns(pid, &init_pid_ns);
15faafc6 718 tsk->flags |= PF_NO_SETAFFINITY;
8fb12156
TG
719 set_cpus_allowed_ptr(tsk, cpumask_of(smp_processor_id()));
720 rcu_read_unlock();
721
1da177e4 722 numa_default_policy();
cf587db2 723 pid = kernel_thread(kthreadd, NULL, NULL, CLONE_FS | CLONE_FILES);
d11c563d 724 rcu_read_lock();
5cd20455 725 kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
d11c563d 726 rcu_read_unlock();
1c3c5eab
TG
727
728 /*
729 * Enable might_sleep() and smp_processor_id() checks.
c1a280b6 730 * They cannot be enabled earlier because with CONFIG_PREEMPTION=y
1c3c5eab
TG
731 * kernel_thread() would trigger might_sleep() splats. With
732 * CONFIG_PREEMPT_VOLUNTARY=y the init task might have scheduled
733 * already, but it's stuck on the kthreadd_done completion.
734 */
735 system_state = SYSTEM_SCHEDULING;
736
b433c3d4 737 complete(&kthreadd_done);
f340c0d1
IM
738
739 /*
740 * The boot idle thread must execute schedule()
1df21055 741 * at least once to get things moving:
f340c0d1 742 */
bd2f5536 743 schedule_preempt_disabled();
5bfb5d69 744 /* Call into cpu_idle with preempt disabled */
a1a04ec3 745 cpu_startup_entry(CPUHP_ONLINE);
1df21055 746}
1da177e4
LT
747
748/* Check for early params. */
ecc86170
LR
749static int __init do_early_param(char *param, char *val,
750 const char *unused, void *arg)
1da177e4 751{
914dcaa8 752 const struct obs_kernel_param *p;
1da177e4
LT
753
754 for (p = __setup_start; p < __setup_end; p++) {
17b65575 755 if (p->early && parameq(param, p->str)) {
1da177e4 756 if (p->setup_func(val) != 0)
ea676e84 757 pr_warn("Malformed early option '%s'\n", param);
1da177e4
LT
758 }
759 }
760 /* We accept everything at this stage. */
761 return 0;
762}
763
13977091
MD
764void __init parse_early_options(char *cmdline)
765{
ecc86170
LR
766 parse_args("early options", cmdline, NULL, 0, 0, 0, NULL,
767 do_early_param);
13977091
MD
768}
769
1da177e4
LT
770/* Arch code calls this early on, or if not, just before other parsing. */
771void __init parse_early_param(void)
772{
dd4d9fec
FF
773 static int done __initdata;
774 static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata;
1da177e4
LT
775
776 if (done)
777 return;
778
779 /* All fall through to do_early_param. */
a1d3a6d9 780 strscpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);
13977091 781 parse_early_options(tmp_cmdline);
1da177e4
LT
782 done = 1;
783}
784
e7ff3a47
TG
785void __init __weak arch_post_acpi_subsys_init(void) { }
786
839ad62e 787void __init __weak smp_setup_processor_id(void)
033ab7f8
AM
788{
789}
790
71261072
TG
791void __init __weak smp_prepare_boot_cpu(void)
792{
793}
794
eded09cc 795# if THREAD_SIZE >= PAGE_SIZE
b235beea 796void __init __weak thread_stack_cache_init(void)
8c9843e5
BH
797{
798}
eded09cc 799#endif
8c9843e5 800
4fc19708
NA
801void __init __weak poking_init(void) { }
802
782de70c 803void __init __weak pgtable_cache_init(void) { }
caa84136 804
8b097881
KW
805void __init __weak trap_init(void) { }
806
4e37958d
SRV
807bool initcall_debug;
808core_param(initcall_debug, initcall_debug, bool, 0644);
b0dc52f1
SRV
809
810#ifdef TRACEPOINTS_ENABLED
4e37958d 811static void __init initcall_debug_enable(void);
b0dc52f1
SRV
812#else
813static inline void initcall_debug_enable(void)
814{
815}
816#endif
4e37958d 817
8cb37a59 818#ifdef CONFIG_RANDOMIZE_KSTACK_OFFSET
39218ff4
KC
819DEFINE_STATIC_KEY_MAYBE_RO(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT,
820 randomize_kstack_offset);
821DEFINE_PER_CPU(u32, kstack_offset);
822
823static int __init early_randomize_kstack_offset(char *buf)
824{
825 int ret;
826 bool bool_result;
827
828 ret = kstrtobool(buf, &bool_result);
829 if (ret)
830 return ret;
831
832 if (bool_result)
833 static_branch_enable(&randomize_kstack_offset);
834 else
835 static_branch_disable(&randomize_kstack_offset);
836 return 0;
837}
838early_param("randomize_kstack_offset", early_randomize_kstack_offset);
839#endif
840
86d1919a
AH
841static void __init print_unknown_bootoptions(void)
842{
843 char *unknown_options;
844 char *end;
845 const char *const *p;
846 size_t len;
847
848 if (panic_later || (!argv_init[1] && !envp_init[2]))
849 return;
850
851 /*
852 * Determine how many options we have to print out, plus a space
853 * before each
854 */
855 len = 1; /* null terminator */
856 for (p = &argv_init[1]; *p; p++) {
857 len++;
858 len += strlen(*p);
859 }
860 for (p = &envp_init[2]; *p; p++) {
861 len++;
862 len += strlen(*p);
863 }
864
865 unknown_options = memblock_alloc(len, SMP_CACHE_BYTES);
866 if (!unknown_options) {
867 pr_err("%s: Failed to allocate %zu bytes\n",
868 __func__, len);
869 return;
870 }
871 end = unknown_options;
872
873 for (p = &argv_init[1]; *p; p++)
874 end += sprintf(end, " %s", *p);
875 for (p = &envp_init[2]; *p; p++)
876 end += sprintf(end, " %s", *p);
877
8bc2b3dc
AH
878 /* Start at unknown_options[1] to skip the initial space */
879 pr_notice("Unknown kernel command line parameters \"%s\", will be passed to user space.\n",
880 &unknown_options[1]);
4421cca0 881 memblock_free(unknown_options, len);
86d1919a
AH
882}
883
f9899c02
HS
884static void __init early_numa_node_init(void)
885{
886#ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID
887#ifndef cpu_to_node
888 int cpu;
889
890 /* The early_cpu_to_node() should be ready here. */
891 for_each_possible_cpu(cpu)
892 set_cpu_numa_node(cpu, early_cpu_to_node(cpu));
893#endif
894#endif
895}
896
514ca14e
ND
897asmlinkage __visible __init __no_sanitize_address __noreturn __no_stack_protector
898void start_kernel(void)
1da177e4 899{
dd4d9fec
FF
900 char *command_line;
901 char *after_dashes;
033ab7f8 902
d4311ff1 903 set_task_stack_end_magic(&init_task);
73839c5b 904 smp_setup_processor_id();
3ac7fe5a 905 debug_objects_early_init();
83cc6fa0 906 init_vmlinux_build_id();
42059429 907
ddbcc7e8 908 cgroup_init_early();
fbb9ce95
IM
909
910 local_irq_disable();
2ce802f6 911 early_boot_irqs_disabled = true;
fbb9ce95 912
1b3b3b49
VK
913 /*
914 * Interrupts are still disabled. Do necessary setups, then
915 * enable them.
916 */
44fd2299 917 boot_cpu_init();
1da177e4 918 page_address_init();
ea676e84 919 pr_notice("%s", linux_banner);
1da177e4 920 setup_arch(&command_line);
77b644c3
KS
921 /* Static keys and static calls are needed by LSMs */
922 jump_label_init();
923 static_call_init();
924 early_security_init();
2b7d2fe7 925 setup_boot_config();
30d7e0d4 926 setup_command_line(command_line);
e0982e90 927 setup_nr_cpu_ids();
d6647bdf 928 setup_per_cpu_areas();
44fd2299 929 smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
f9899c02 930 early_numa_node_init();
b5b1404d 931 boot_cpu_hotplug_init();
1da177e4 932
51887d03 933 pr_notice("Kernel command line: %s\n", saved_command_line);
6041186a 934 /* parameters may set static keys */
83b519e8 935 parse_early_param();
51e158c1
RR
936 after_dashes = parse_args("Booting kernel",
937 static_command_line, __start___param,
938 __stop___param - __start___param,
ecc86170 939 -1, -1, NULL, &unknown_bootoption);
86d1919a 940 print_unknown_bootoptions();
3438cf54 941 if (!IS_ERR_OR_NULL(after_dashes))
51e158c1 942 parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
ecc86170 943 NULL, set_init_arg);
13199162
MH
944 if (extra_init_args)
945 parse_args("Setting extra init args", extra_init_args,
946 NULL, 0, -1, -1, NULL, set_init_arg);
97ce2c88 947
f6238499
JD
948 /* Architectural and non-timekeeping rng init, before allocator init */
949 random_init_early(command_line);
950
83b519e8
PE
951 /*
952 * These use large bootmem allocations and must precede
b7ec1bf3 953 * initalization of page allocator
83b519e8 954 */
162a7e75 955 setup_log_buf(0);
83b519e8
PE
956 vfs_caches_init_early();
957 sort_main_extable();
958 trap_init();
b7ec1bf3 959 mm_core_init();
5b93a836 960 poking_init();
f631718d
SRV
961 ftrace_init();
962
e725c731
SRV
963 /* trace_printk can be enabled here */
964 early_trace_init();
965
1da177e4
LT
966 /*
967 * Set up the scheduler prior starting any interrupts (such as the
968 * timer interrupt). Full topology setup happens at smp_init()
969 * time - but meanwhile we still have a functioning scheduler.
970 */
971 sched_init();
f1a0a376 972
dd4d9fec
FF
973 if (WARN(!irqs_disabled(),
974 "Interrupts were enabled *very* early, fixing it\n"))
c4a68306 975 local_irq_disable();
0a835c4f 976 radix_tree_init();
54a611b6 977 maple_tree_init();
3347fa09 978
7d229c66
TS
979 /*
980 * Set up housekeeping before setting up workqueues to allow the unbound
981 * workqueue to take non-housekeeping into account.
982 */
983 housekeeping_init();
984
3347fa09
TH
985 /*
986 * Allow workqueue creation and work item queueing/cancelling
987 * early. Work item execution depends on kthreads and starts after
988 * workqueue_init().
989 */
990 workqueue_init_early();
991
1da177e4 992 rcu_init();
0f52b4db 993 kvfree_rcu_init();
5f893b26 994
e725c731 995 /* Trace events are available after this */
5f893b26
SRRH
996 trace_init();
997
4e37958d
SRV
998 if (initcall_debug)
999 initcall_debug_enable();
1000
65f382fd 1001 context_tracking_init();
0b8f1efa
YL
1002 /* init some links before init_ISA_irqs() */
1003 early_irq_init();
1da177e4 1004 init_IRQ();
ad2b1353 1005 tick_init();
d6dd50e0 1006 rcu_init_nohz();
751e6a39 1007 timers_init();
8e9c01c7 1008 srcu_init();
c0a31329 1009 hrtimers_init();
1da177e4 1010 softirq_init();
ad596171 1011 timekeeping_init();
fe222a6c 1012 time_init();
d5553523 1013
f6238499
JD
1014 /* This must be after timekeeping is initialized */
1015 random_init();
1016
1017 /* These make use of the fully initialized rng */
1018 kfence_init();
d5553523
KC
1019 boot_init_stack_canary();
1020
9e630205 1021 perf_event_init();
93e02814 1022 profile_init();
d8ad7d11 1023 call_function_init();
f91eb62f 1024 WARN(!irqs_disabled(), "Interrupts were enabled early\n");
c3bc8fd6 1025
2ce802f6 1026 early_boot_irqs_disabled = false;
93e02814 1027 local_irq_enable();
dcce284a 1028
7e85ee0c 1029 kmem_cache_init_late();
1da177e4
LT
1030
1031 /*
1032 * HACK ALERT! This is early. We're enabling the console before
1033 * we've done PCI setups etc, and console_init() must be aware of
1034 * this. But we do want output early, in case something goes wrong.
1035 */
1036 console_init();
1037 if (panic_later)
499a4584
TH
1038 panic("Too many boot %s vars at `%s'", panic_later,
1039 panic_param);
fbb9ce95 1040
c3bc8fd6 1041 lockdep_init();
fbb9ce95 1042
9a11b49a
IM
1043 /*
1044 * Need to run this when irqs are enabled, because it wants
1045 * to self-test [hard/soft]-irqs on/off lock inversion bugs
1046 * too:
1047 */
1048 locking_selftest();
1049
1da177e4
LT
1050#ifdef CONFIG_BLK_DEV_INITRD
1051 if (initrd_start && !initrd_below_start_ok &&
bd673c7c 1052 page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
ea676e84 1053 pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n",
bd673c7c
GU
1054 page_to_pfn(virt_to_page((void *)initrd_start)),
1055 min_low_pfn);
1da177e4
LT
1056 initrd_start = 0;
1057 }
1058#endif
e7c8d5c9 1059 setup_per_cpu_pageset();
1da177e4 1060 numa_policy_init();
9c71206d 1061 acpi_early_init();
1da177e4
LT
1062 if (late_time_init)
1063 late_time_init();
857baa87 1064 sched_clock_init();
1da177e4 1065 calibrate_delay();
e45e761b 1066
9df9d2f0
TG
1067 arch_cpu_finalize_init();
1068
95846ecf 1069 pid_idr_init();
1da177e4 1070 anon_vma_init();
11520e5e 1071#ifdef CONFIG_X86
83e68189 1072 if (efi_enabled(EFI_RUNTIME_SERVICES))
11520e5e
LT
1073 efi_enter_virtual_mode();
1074#endif
b235beea 1075 thread_stack_cache_init();
d84f4f99 1076 cred_init();
ff691f6e 1077 fork_init();
1da177e4 1078 proc_caches_init();
3ea056c5 1079 uts_ns_init();
1da177e4
LT
1080 key_init();
1081 security_init();
0b4b3827 1082 dbg_late_init();
9c1be193 1083 net_ns_init();
4248b0da 1084 vfs_caches_init();
62906027 1085 pagecache_init();
1da177e4 1086 signals_init();
09652320 1087 seq_file_init();
1da177e4 1088 proc_root_init();
e149ed2b 1089 nsfs_init();
cb12fd8e 1090 pidfs_init();
1da177e4 1091 cpuset_init();
bc9817bb 1092 mem_cgroup_init();
695df213 1093 cgroup_init();
c757249a 1094 taskstats_init_early();
ca74e92b 1095 delayacct_init();
1da177e4 1096
b064a8fa 1097 acpi_subsystem_init();
e7ff3a47 1098 arch_post_acpi_subsys_init();
dfd402a4 1099 kcsan_init();
1da177e4
LT
1100
1101 /* Do the rest non-__init'ed, we're now alive */
ac4db926 1102 rest_init();
a9a3ed1e 1103
dc1d0553
ND
1104 /*
1105 * Avoid stack canaries in callers of boot_init_stack_canary for gcc-10
1106 * and older.
1107 */
1108#if !__has_attribute(__no_stack_protector__)
a9a3ed1e 1109 prevent_tail_call_optimization();
dc1d0553 1110#endif
1da177e4
LT
1111}
1112
b99b87f7
PO
1113/* Call all constructor functions linked into the kernel. */
1114static void __init do_ctors(void)
1115{
55b6f763
JB
1116/*
1117 * For UML, the constructors have already been called by the
1118 * normal setup code as it's just a normal ELF binary, so we
1119 * cannot do it again - but we do need CONFIG_CONSTRUCTORS
1120 * even on UML for modules.
1121 */
1122#if defined(CONFIG_CONSTRUCTORS) && !defined(CONFIG_UML)
196a15b4 1123 ctor_fn_t *fn = (ctor_fn_t *) __ctors_start;
b99b87f7 1124
196a15b4
HS
1125 for (; fn < (ctor_fn_t *) __ctors_end; fn++)
1126 (*fn)();
b99b87f7
PO
1127#endif
1128}
1129
7b0b73d7
PB
1130#ifdef CONFIG_KALLSYMS
1131struct blacklist_entry {
1132 struct list_head next;
1133 char *buf;
1134};
1135
1136static __initdata_or_module LIST_HEAD(blacklisted_initcalls);
1137
1138static int __init initcall_blacklist(char *str)
1139{
1140 char *str_entry;
1141 struct blacklist_entry *entry;
1142
1143 /* str argument is a comma-separated list of functions */
1144 do {
1145 str_entry = strsep(&str, ",");
1146 if (str_entry) {
1147 pr_debug("blacklisting initcall %s\n", str_entry);
c6f23979 1148 entry = memblock_alloc_or_panic(sizeof(*entry),
7e1c4e27 1149 SMP_CACHE_BYTES);
c6f23979 1150 entry->buf = memblock_alloc_or_panic(strlen(str_entry) + 1,
7e1c4e27 1151 SMP_CACHE_BYTES);
7b0b73d7
PB
1152 strcpy(entry->buf, str_entry);
1153 list_add(&entry->next, &blacklisted_initcalls);
1154 }
1155 } while (str_entry);
1156
f9a40b08 1157 return 1;
7b0b73d7
PB
1158}
1159
1160static bool __init_or_module initcall_blacklisted(initcall_t fn)
1161{
7b0b73d7 1162 struct blacklist_entry *entry;
c8cdd2be 1163 char fn_name[KSYM_SYMBOL_LEN];
0fd5ed8d 1164 unsigned long addr;
7b0b73d7 1165
c8cdd2be 1166 if (list_empty(&blacklisted_initcalls))
7b0b73d7
PB
1167 return false;
1168
0fd5ed8d
RV
1169 addr = (unsigned long) dereference_function_descriptor(fn);
1170 sprint_symbol_no_offset(fn_name, addr);
c8cdd2be 1171
841c06d7
PB
1172 /*
1173 * fn will be "function_name [module_name]" where [module_name] is not
1174 * displayed for built-in init functions. Strip off the [module_name].
1175 */
1176 strreplace(fn_name, ' ', '\0');
1177
e6fd1fb3 1178 list_for_each_entry(entry, &blacklisted_initcalls, next) {
7b0b73d7
PB
1179 if (!strcmp(fn_name, entry->buf)) {
1180 pr_debug("initcall %s blacklisted\n", fn_name);
7b0b73d7
PB
1181 return true;
1182 }
1183 }
1184
7b0b73d7
PB
1185 return false;
1186}
1187#else
1188static int __init initcall_blacklist(char *str)
1189{
1190 pr_warn("initcall_blacklist requires CONFIG_KALLSYMS\n");
1191 return 0;
1192}
1193
1194static bool __init_or_module initcall_blacklisted(initcall_t fn)
1195{
1196 return false;
1197}
1198#endif
1199__setup("initcall_blacklist=", initcall_blacklist);
1200
4e37958d
SRV
1201static __init_or_module void
1202trace_initcall_start_cb(void *data, initcall_t fn)
1da177e4 1203{
374d6cda 1204 ktime_t *calltime = data;
1da177e4 1205
58e2cf5d 1206 printk(KERN_DEBUG "calling %pS @ %i\n", fn, task_pid_nr(current));
4e37958d
SRV
1207 *calltime = ktime_get();
1208}
1209
1210static __init_or_module void
1211trace_initcall_finish_cb(void *data, initcall_t fn, int ret)
1212{
374d6cda 1213 ktime_t rettime, *calltime = data;
4e37958d 1214
22c5c03b 1215 rettime = ktime_get();
58e2cf5d 1216 printk(KERN_DEBUG "initcall %pS returned %d after %lld usecs\n",
105e8c2e 1217 fn, ret, (unsigned long long)ktime_us_delta(rettime, *calltime));
4e37958d 1218}
1da177e4 1219
3330dc1b
FV
1220static __init_or_module void
1221trace_initcall_level_cb(void *data, const char *level)
1222{
1223 printk(KERN_DEBUG "entering initcall level: %s\n", level);
1224}
1225
4e37958d
SRV
1226static ktime_t initcall_calltime;
1227
b0dc52f1 1228#ifdef TRACEPOINTS_ENABLED
4e37958d
SRV
1229static void __init initcall_debug_enable(void)
1230{
1231 int ret;
1232
1233 ret = register_trace_initcall_start(trace_initcall_start_cb,
1234 &initcall_calltime);
1235 ret |= register_trace_initcall_finish(trace_initcall_finish_cb,
1236 &initcall_calltime);
3330dc1b 1237 ret |= register_trace_initcall_level(trace_initcall_level_cb, NULL);
4e37958d 1238 WARN(ret, "Failed to register initcall tracepoints\n");
22c5c03b 1239}
b0dc52f1
SRV
1240# define do_trace_initcall_start trace_initcall_start
1241# define do_trace_initcall_finish trace_initcall_finish
3330dc1b 1242# define do_trace_initcall_level trace_initcall_level
b0dc52f1
SRV
1243#else
1244static inline void do_trace_initcall_start(initcall_t fn)
1245{
1246 if (!initcall_debug)
1247 return;
1248 trace_initcall_start_cb(&initcall_calltime, fn);
1249}
1250static inline void do_trace_initcall_finish(initcall_t fn, int ret)
1251{
1252 if (!initcall_debug)
1253 return;
1254 trace_initcall_finish_cb(&initcall_calltime, fn, ret);
1255}
3330dc1b
FV
1256static inline void do_trace_initcall_level(const char *level)
1257{
1258 if (!initcall_debug)
1259 return;
1260 trace_initcall_level_cb(NULL, level);
1261}
b0dc52f1 1262#endif /* !TRACEPOINTS_ENABLED */
22c5c03b 1263
e4461271 1264int __init_or_module do_one_initcall(initcall_t fn)
22c5c03b
KW
1265{
1266 int count = preempt_count();
ff1c8fac 1267 char msgbuf[64];
4e37958d 1268 int ret;
22c5c03b 1269
7b0b73d7
PB
1270 if (initcall_blacklisted(fn))
1271 return -EPERM;
1272
b0dc52f1 1273 do_trace_initcall_start(fn);
4e37958d 1274 ret = fn();
b0dc52f1 1275 do_trace_initcall_finish(fn, ret);
8f0c45cd 1276
e0df154f 1277 msgbuf[0] = 0;
e662e1cf 1278
e0df154f 1279 if (preempt_count() != count) {
bf5d770b 1280 sprintf(msgbuf, "preemption imbalance ");
4a2b4b22 1281 preempt_count_set(count);
1da177e4 1282 }
e0df154f 1283 if (irqs_disabled()) {
a76bfd0d 1284 strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
e0df154f
LT
1285 local_irq_enable();
1286 }
d75f773c 1287 WARN(msgbuf[0], "initcall %pS returned with %s\n", fn, msgbuf);
59f9415f 1288
38addce8 1289 add_latent_entropy();
30dbb20e 1290 return ret;
e0df154f
LT
1291}
1292
1293
1b1eeca7 1294static initcall_entry_t *initcall_levels[] __initdata = {
026cee00
PM
1295 __initcall0_start,
1296 __initcall1_start,
1297 __initcall2_start,
1298 __initcall3_start,
1299 __initcall4_start,
1300 __initcall5_start,
1301 __initcall6_start,
1302 __initcall7_start,
1303 __initcall_end,
1304};
1305
96263d28 1306/* Keep these in sync with initcalls in include/linux/init.h */
7c8f7193 1307static const char *initcall_level_names[] __initdata = {
a6fb6012 1308 "pure",
9fb48c74
JC
1309 "core",
1310 "postcore",
1311 "arch",
1312 "subsys",
1313 "fs",
1314 "device",
1315 "late",
026cee00
PM
1316};
1317
7e2762e1
AS
1318static int __init ignore_unknown_bootoption(char *param, char *val,
1319 const char *unused, void *arg)
1320{
1321 return 0;
1322}
1323
0068c92a 1324static void __init do_initcall_level(int level, char *command_line)
e0df154f 1325{
1b1eeca7 1326 initcall_entry_t *fn;
e0df154f 1327
026cee00 1328 parse_args(initcall_level_names[level],
0068c92a 1329 command_line, __start___param,
026cee00
PM
1330 __stop___param - __start___param,
1331 level, level,
7e2762e1 1332 NULL, ignore_unknown_bootoption);
026cee00 1333
3330dc1b 1334 do_trace_initcall_level(initcall_level_names[level]);
026cee00 1335 for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++)
1b1eeca7 1336 do_one_initcall(initcall_from_entry(fn));
1da177e4
LT
1337}
1338
026cee00
PM
1339static void __init do_initcalls(void)
1340{
1341 int level;
941baf6f 1342 size_t len = saved_command_line_len + 1;
0068c92a
MH
1343 char *command_line;
1344
1345 command_line = kzalloc(len, GFP_KERNEL);
1346 if (!command_line)
1347 panic("%s: Failed to allocate %zu bytes\n", __func__, len);
1348
1349 for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++) {
1350 /* Parser modifies command_line, restore it each time */
1351 strcpy(command_line, saved_command_line);
1352 do_initcall_level(level, command_line);
1353 }
026cee00 1354
0068c92a 1355 kfree(command_line);
026cee00
PM
1356}
1357
1da177e4
LT
1358/*
1359 * Ok, the machine is now initialized. None of the devices
1360 * have been touched yet, but the CPU subsystem is up and
1361 * running, and memory and process management works.
1362 *
1363 * Now we can finally start doing some real work..
1364 */
1365static void __init do_basic_setup(void)
1366{
759ee091 1367 cpuset_init_smp();
1da177e4 1368 driver_init();
b04c3afb 1369 init_irq_proc();
b99b87f7 1370 do_ctors();
b0f84374 1371 do_initcalls();
1da177e4
LT
1372}
1373
7babe8db 1374static void __init do_pre_smp_initcalls(void)
c2147a50 1375{
1b1eeca7 1376 initcall_entry_t *fn;
c2147a50 1377
3330dc1b 1378 do_trace_initcall_level("early");
026cee00 1379 for (fn = __initcall_start; fn < __initcall0_start; fn++)
1b1eeca7 1380 do_one_initcall(initcall_from_entry(fn));
c2147a50
EGM
1381}
1382
a74fb73c 1383static int run_init_process(const char *init_filename)
1da177e4 1384{
b88c50ac
AS
1385 const char *const *p;
1386
1da177e4 1387 argv_init[0] = init_filename;
3f5c15d8 1388 pr_info("Run %s as init process\n", init_filename);
b88c50ac
AS
1389 pr_debug(" with arguments:\n");
1390 for (p = argv_init; *p; p++)
1391 pr_debug(" %s\n", *p);
1392 pr_debug(" with environment:\n");
1393 for (p = envp_init; *p; p++)
1394 pr_debug(" %s\n", *p);
be619f7f 1395 return kernel_execve(init_filename, argv_init, envp_init);
1da177e4
LT
1396}
1397
ba24762b
MO
1398static int try_to_run_init_process(const char *init_filename)
1399{
1400 int ret;
1401
1402 ret = run_init_process(init_filename);
1403
1404 if (ret && ret != -ENOENT) {
1405 pr_err("Starting init: %s exists but couldn't execute it (error %d)\n",
1406 init_filename, ret);
1407 }
1408
1409 return ret;
1410}
1411
f80b0c90 1412static noinline void __init kernel_init_freeable(void);
d6b21238 1413
0f5bf6d0 1414#if defined(CONFIG_STRICT_KERNEL_RWX) || defined(CONFIG_STRICT_MODULE_RWX)
39290b38 1415bool rodata_enabled __ro_after_init = true;
2e8cff0a
MR
1416
1417#ifndef arch_parse_debug_rodata
1418static inline bool arch_parse_debug_rodata(char *str) { return false; }
1419#endif
1420
d2aa1aca
KC
1421static int __init set_debug_rodata(char *str)
1422{
2e8cff0a
MR
1423 if (arch_parse_debug_rodata(str))
1424 return 0;
1425
1426 if (str && !strcmp(str, "on"))
1427 rodata_enabled = true;
1428 else if (str && !strcmp(str, "off"))
1429 rodata_enabled = false;
1430 else
f9a40b08 1431 pr_warn("Invalid option string for rodata: '%s'\n", str);
2e8cff0a 1432 return 0;
d2aa1aca 1433}
2e8cff0a 1434early_param("rodata", set_debug_rodata);
39290b38 1435#endif
d2aa1aca
KC
1436
1437static void mark_readonly(void)
1438{
398ec3e9 1439 if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX) && rodata_enabled) {
ae646f0b 1440 /*
ba180314 1441 * load_module() results in W+X mappings, which are cleaned
8f8cd6c0 1442 * up with init_free_wq. Let's make sure that queued work is
ae646f0b
JH
1443 * flushed so that we don't hit false positives looking for
1444 * insecure pages which are W+X.
1445 */
8f8cd6c0 1446 flush_module_init_free_work();
91a1d97e 1447 jump_label_init_ro();
d2aa1aca 1448 mark_rodata_ro();
a5e8131a 1449 debug_checkwx();
2959a5f7 1450 rodata_test();
398ec3e9 1451 } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) {
d2aa1aca 1452 pr_info("Kernel memory protection disabled.\n");
398ec3e9
CL
1453 } else if (IS_ENABLED(CONFIG_ARCH_HAS_STRICT_KERNEL_RWX)) {
1454 pr_warn("Kernel memory protection not selected by kernel config.\n");
1455 } else {
1456 pr_warn("This architecture does not have kernel memory protection.\n");
1457 }
d2aa1aca 1458}
d2aa1aca 1459
997aef68
MR
1460void __weak free_initmem(void)
1461{
f4039999 1462 free_initmem_default(POISON_FREE_INITMEM);
997aef68
MR
1463}
1464
d6b21238 1465static int __ref kernel_init(void *unused)
ee5bfa64 1466{
ba24762b
MO
1467 int ret;
1468
15faafc6
PZ
1469 /*
1470 * Wait until kthreadd is all set-up.
1471 */
1472 wait_for_completion(&kthreadd_done);
1473
d6b21238 1474 kernel_init_freeable();
22a9d645
AV
1475 /* need to finish all async __init code before freeing the memory */
1476 async_synchronize_full();
d2635f20
CL
1477
1478 system_state = SYSTEM_FREEING_INITMEM;
82d083ab 1479 kprobe_free_init_mem();
b80f0f6c 1480 ftrace_free_init_mem();
d54ce615 1481 kgdb_free_init_mem();
40caa127 1482 exit_boot_config();
ee5bfa64 1483 free_initmem();
d2aa1aca 1484 mark_readonly();
b976690f
JR
1485
1486 /*
1487 * Kernel mappings are now finalized - update the userspace page-table
1488 * to finalize PTI.
1489 */
1490 pti_finalize();
1491
ee5bfa64
VG
1492 system_state = SYSTEM_RUNNING;
1493 numa_default_policy();
1494
967dcb8f
PM
1495 rcu_end_inkernel_boot();
1496
3db978d4
VB
1497 do_sysctl_args();
1498
ee5bfa64 1499 if (ramdisk_execute_command) {
ba24762b
MO
1500 ret = run_init_process(ramdisk_execute_command);
1501 if (!ret)
a74fb73c 1502 return 0;
ba24762b
MO
1503 pr_err("Failed to execute %s (error %d)\n",
1504 ramdisk_execute_command, ret);
ee5bfa64
VG
1505 }
1506
1507 /*
1508 * We try each of these until one succeeds.
1509 *
1510 * The Bourne shell can be used instead of init if we are
1511 * trying to recover a really broken machine.
1512 */
1513 if (execute_command) {
ba24762b
MO
1514 ret = run_init_process(execute_command);
1515 if (!ret)
a74fb73c 1516 return 0;
6ef4536e
AL
1517 panic("Requested init %s failed (error %d).",
1518 execute_command, ret);
ee5bfa64 1519 }
ada4ab7a
CD
1520
1521 if (CONFIG_DEFAULT_INIT[0] != '\0') {
1522 ret = run_init_process(CONFIG_DEFAULT_INIT);
1523 if (ret)
1524 pr_err("Default init %s failed (error %d)\n",
1525 CONFIG_DEFAULT_INIT, ret);
1526 else
1527 return 0;
1528 }
1529
ba24762b
MO
1530 if (!try_to_run_init_process("/sbin/init") ||
1531 !try_to_run_init_process("/etc/init") ||
1532 !try_to_run_init_process("/bin/init") ||
1533 !try_to_run_init_process("/bin/sh"))
a74fb73c 1534 return 0;
ee5bfa64 1535
ba24762b 1536 panic("No working init found. Try passing init= option to kernel. "
8c27ceff 1537 "See Linux Documentation/admin-guide/init.rst for guidance.");
ee5bfa64
VG
1538}
1539
8f740636 1540/* Open /dev/console, for stdin/stdout/stderr, this should never fail */
a94b5214 1541void __init console_on_rootfs(void)
b49a733d 1542{
8f740636 1543 struct file *file = filp_open("/dev/console", O_RDWR, 0);
b49a733d 1544
8f740636 1545 if (IS_ERR(file)) {
a91bd622
PM
1546 pr_err("Warning: unable to open an initial console.\n");
1547 return;
8f740636 1548 }
f0735310
CH
1549 init_dup(file);
1550 init_dup(file);
1551 init_dup(file);
1552 fput(file);
b49a733d
DB
1553}
1554
f80b0c90 1555static noinline void __init kernel_init_freeable(void)
1da177e4 1556{
31a67102
LT
1557 /* Now the scheduler is fully set up and can do blocking allocations */
1558 gfp_allowed_mask = __GFP_BITS_MASK;
1559
58568d2a
MX
1560 /*
1561 * init can allocate pages on any node
1562 */
3c466d46 1563 set_mems_allowed(node_states[N_MEMORY]);
1da177e4 1564
0711f0d7 1565 cad_pid = get_pid(task_pid(current));
9ec52099 1566
ca74a6f8 1567 smp_prepare_cpus(setup_max_cpus);
1da177e4 1568
3347fa09
TH
1569 workqueue_init();
1570
597b7305
MH
1571 init_mm_internals();
1572
1da177e4 1573 do_pre_smp_initcalls();
004417a6 1574 lockup_detector_init();
1da177e4 1575
1da177e4
LT
1576 smp_init();
1577 sched_init_smp();
1578
2930155b 1579 workqueue_init_topology();
bf52b1ac 1580 async_init();
f1b192b1 1581 padata_init();
0e1cc95b
MG
1582 page_alloc_init_late();
1583
1da177e4
LT
1584 do_basic_setup();
1585
8c0d8849
BH
1586 kunit_run_all_tests();
1587
e7cb072e 1588 wait_for_initramfs();
b49a733d 1589 console_on_rootfs();
2bd3a997 1590
1da177e4
LT
1591 /*
1592 * check if there is an early userspace init. If yes, let it do all
1593 * the work
1594 */
eb9d7d39 1595 if (init_eaccess(ramdisk_execute_command) != 0) {
ffdfc409 1596 ramdisk_execute_command = NULL;
1da177e4 1597 prepare_namespace();
ffdfc409 1598 }
1da177e4
LT
1599
1600 /*
1601 * Ok, we have completed the initial bootup, and
1602 * we're essentially up and running. Get rid of the
1603 * initmem segments and start the user-mode stuff..
c9cd2ce2
DK
1604 *
1605 * rootfs is available now, try loading the public keys
1606 * and default modules
1da177e4 1607 */
bb813f4c 1608
c9cd2ce2 1609 integrity_load_keys();
1da177e4 1610}