]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - init/main.c
tools/bootconfig: Show the number of bootconfig nodes
[mirror_ubuntu-hirsute-kernel.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>
8a293be0 16#include <linux/extable.h>
1da177e4
LT
17#include <linux/module.h>
18#include <linux/proc_fs.h>
5c2c5c55 19#include <linux/binfmts.h>
1da177e4
LT
20#include <linux/kernel.h>
21#include <linux/syscalls.h>
9b5609fd 22#include <linux/stackprotector.h>
1da177e4
LT
23#include <linux/string.h>
24#include <linux/ctype.h>
25#include <linux/delay.h>
1da177e4
LT
26#include <linux/ioport.h>
27#include <linux/init.h>
1da177e4 28#include <linux/initrd.h>
57c8a661 29#include <linux/memblock.h>
4a7a16dc 30#include <linux/acpi.h>
7684b858 31#include <linux/bootconfig.h>
0c688614 32#include <linux/console.h>
38b8d208 33#include <linux/nmi.h>
1da177e4
LT
34#include <linux/percpu.h>
35#include <linux/kmod.h>
db64fe02 36#include <linux/vmalloc.h>
1da177e4 37#include <linux/kernel_stat.h>
d7cd5611 38#include <linux/start_kernel.h>
1da177e4 39#include <linux/security.h>
3d442233 40#include <linux/smp.h>
1da177e4
LT
41#include <linux/profile.h>
42#include <linux/rcupdate.h>
43#include <linux/moduleparam.h>
44#include <linux/kallsyms.h>
45#include <linux/writeback.h>
46#include <linux/cpu.h>
47#include <linux/cpuset.h>
ddbcc7e8 48#include <linux/cgroup.h>
1da177e4 49#include <linux/efi.h>
906568c9 50#include <linux/tick.h>
78634061 51#include <linux/sched/isolation.h>
6168a702 52#include <linux/interrupt.h>
c757249a 53#include <linux/taskstats_kern.h>
ca74e92b 54#include <linux/delayacct.h>
1da177e4 55#include <linux/unistd.h>
3ea056c5 56#include <linux/utsname.h>
1da177e4
LT
57#include <linux/rmap.h>
58#include <linux/mempolicy.h>
59#include <linux/key.h>
b6cd0b77 60#include <linux/buffer_head.h>
eefa864b 61#include <linux/page_ext.h>
9a11b49a 62#include <linux/debug_locks.h>
3ac7fe5a 63#include <linux/debugobjects.h>
fbb9ce95 64#include <linux/lockdep.h>
3c7b4e6b 65#include <linux/kmemleak.h>
84d73786 66#include <linux/pid_namespace.h>
1f21782e 67#include <linux/device.h>
73c27992 68#include <linux/kthread.h>
e6fe6649 69#include <linux/sched.h>
1777e463 70#include <linux/sched/init.h>
a1c9eea9 71#include <linux/signal.h>
199f0ca5 72#include <linux/idr.h>
0b4b3827 73#include <linux/kgdb.h>
68bf21aa 74#include <linux/ftrace.h>
22a9d645 75#include <linux/async.h>
6ae6996a 76#include <linux/sfi.h>
2b2af54a 77#include <linux/shmem_fs.h>
5a0e3ad6 78#include <linux/slab.h>
24a24bb6 79#include <linux/perf_event.h>
a74fb73c 80#include <linux/ptrace.h>
aa8c6248 81#include <linux/pti.h>
bb813f4c
TH
82#include <linux/blkdev.h>
83#include <linux/elevator.h>
5d2a4e91 84#include <linux/sched/clock.h>
29930025 85#include <linux/sched/task.h>
68db0cf1 86#include <linux/sched/task_stack.h>
65f382fd 87#include <linux/context_tracking.h>
47d06e53 88#include <linux/random.h>
7b0b73d7 89#include <linux/list.h>
c9cd2ce2 90#include <linux/integrity.h>
e149ed2b 91#include <linux/proc_ns.h>
0ddab1d2 92#include <linux/io.h>
39290b38 93#include <linux/cache.h>
2959a5f7 94#include <linux/rodata_test.h>
33352244 95#include <linux/jump_label.h>
ae67d58d 96#include <linux/mem_encrypt.h>
1da177e4
LT
97
98#include <asm/io.h>
99#include <asm/bugs.h>
100#include <asm/setup.h>
a940199f 101#include <asm/sections.h>
37b73c82 102#include <asm/cacheflush.h>
1da177e4 103
4ee7c60d
SRV
104#define CREATE_TRACE_POINTS
105#include <trace/events/initcall.h>
106
aae5f662 107static int kernel_init(void *);
1da177e4
LT
108
109extern void init_IRQ(void);
1da177e4 110extern void radix_tree_init(void);
1da177e4 111
2ce802f6
TH
112/*
113 * Debug helper: via this flag we know that we are in 'early bootup code'
114 * where only the boot processor is running with IRQ disabled. This means
115 * two things - IRQ must not be enabled before the flag is cleared and some
116 * operations which are not allowed with IRQ disabled are allowed while the
117 * flag is set.
118 */
119bool early_boot_irqs_disabled __read_mostly;
120
a6826048 121enum system_states system_state __read_mostly;
1da177e4
LT
122EXPORT_SYMBOL(system_state);
123
124/*
125 * Boot command-line arguments
126 */
127#define MAX_INIT_ARGS CONFIG_INIT_ENV_ARG_LIMIT
128#define MAX_INIT_ENVS CONFIG_INIT_ENV_ARG_LIMIT
129
130extern void time_init(void);
131/* Default late time init is NULL. archs can override this later. */
d2e3192b 132void (*__initdata late_time_init)(void);
1da177e4 133
30d7e0d4
ABL
134/* Untouched command line saved by arch-specific code. */
135char __initdata boot_command_line[COMMAND_LINE_SIZE];
136/* Untouched saved command line (eg. for /proc) */
137char *saved_command_line;
138/* Command line for parameter parsing */
139static char *static_command_line;
51887d03
MH
140/* Untouched extra command line */
141static char *extra_command_line;
13199162
MH
142/* Extra init arguments */
143static char *extra_init_args;
1da177e4
LT
144
145static char *execute_command;
ffdfc409 146static char *ramdisk_execute_command;
1da177e4 147
c4b2c0c5
HFS
148/*
149 * Used to generate warnings if static_key manipulation functions are used
150 * before jump_label_init is called.
151 */
dd4d9fec 152bool static_key_initialized __read_mostly;
c4b2c0c5
HFS
153EXPORT_SYMBOL_GPL(static_key_initialized);
154
8b3b2955
JB
155/*
156 * If set, this is an indication to the drivers that reset the underlying
157 * device before going ahead with the initialization otherwise driver might
158 * rely on the BIOS and skip the reset operation.
159 *
160 * This is useful if kernel is booting in an unreliable environment.
fc454fdc 161 * For ex. kdump situation where previous kernel has crashed, BIOS has been
8b3b2955
JB
162 * skipped and devices will be in unknown state.
163 */
164unsigned int reset_devices;
165EXPORT_SYMBOL(reset_devices);
1da177e4 166
7e96287d
VG
167static int __init set_reset_devices(char *str)
168{
169 reset_devices = 1;
170 return 1;
171}
172
173__setup("reset_devices", set_reset_devices);
174
dd4d9fec
FF
175static const char *argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
176const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
1da177e4
LT
177static const char *panic_later, *panic_param;
178
914dcaa8 179extern const struct obs_kernel_param __setup_start[], __setup_end[];
1da177e4 180
31c025b5 181static bool __init obsolete_checksetup(char *line)
1da177e4 182{
914dcaa8 183 const struct obs_kernel_param *p;
31c025b5 184 bool had_early_param = false;
1da177e4
LT
185
186 p = __setup_start;
187 do {
188 int n = strlen(p->str);
b1e4d20c 189 if (parameqn(line, p->str, n)) {
1da177e4 190 if (p->early) {
33df0d19
RR
191 /* Already done in parse_early_param?
192 * (Needs exact match on param part).
193 * Keep iterating, as we can have early
194 * params and __setups of same names 8( */
1da177e4 195 if (line[n] == '\0' || line[n] == '=')
31c025b5 196 had_early_param = true;
1da177e4 197 } else if (!p->setup_func) {
ea676e84
AM
198 pr_warn("Parameter %s is obsolete, ignored\n",
199 p->str);
31c025b5 200 return true;
1da177e4 201 } else if (p->setup_func(line + n))
31c025b5 202 return true;
1da177e4
LT
203 }
204 p++;
205 } while (p < __setup_end);
33df0d19
RR
206
207 return had_early_param;
1da177e4
LT
208}
209
210/*
211 * This should be approx 2 Bo*oMips to start (note initial shift), and will
212 * still work even if initially too large, it will just take slightly longer
213 */
214unsigned long loops_per_jiffy = (1<<12);
1da177e4
LT
215EXPORT_SYMBOL(loops_per_jiffy);
216
217static int __init debug_kernel(char *str)
218{
a8fe19eb 219 console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
f6f21c81 220 return 0;
1da177e4
LT
221}
222
223static int __init quiet_kernel(char *str)
224{
a8fe19eb 225 console_loglevel = CONSOLE_LOGLEVEL_QUIET;
f6f21c81 226 return 0;
1da177e4
LT
227}
228
f6f21c81
YL
229early_param("debug", debug_kernel);
230early_param("quiet", quiet_kernel);
1da177e4
LT
231
232static int __init loglevel(char *str)
233{
808bf29b
AS
234 int newlevel;
235
236 /*
237 * Only update loglevel value when a correct setting was passed,
238 * to prevent blind crashes (when loglevel being set to 0) that
239 * are quite hard to debug
240 */
241 if (get_option(&str, &newlevel)) {
242 console_loglevel = newlevel;
243 return 0;
244 }
245
246 return -EINVAL;
1da177e4
LT
247}
248
f6f21c81 249early_param("loglevel", loglevel);
1da177e4 250
7684b858 251#ifdef CONFIG_BOOT_CONFIG
51887d03
MH
252
253char xbc_namebuf[XBC_KEYLEN_MAX] __initdata;
254
255#define rest(dst, end) ((end) > (dst) ? (end) - (dst) : 0)
256
257static int __init xbc_snprint_cmdline(char *buf, size_t size,
258 struct xbc_node *root)
259{
260 struct xbc_node *knode, *vnode;
261 char *end = buf + size;
262 char c = '\"';
263 const char *val;
264 int ret;
265
266 xbc_node_for_each_key_value(root, knode, val) {
267 ret = xbc_node_compose_key_after(root, knode,
268 xbc_namebuf, XBC_KEYLEN_MAX);
269 if (ret < 0)
270 return ret;
271
272 vnode = xbc_node_get_child(knode);
273 ret = snprintf(buf, rest(buf, end), "%s%c", xbc_namebuf,
274 vnode ? '=' : ' ');
275 if (ret < 0)
276 return ret;
277 buf += ret;
278 if (!vnode)
279 continue;
280
281 c = '\"';
282 xbc_array_for_each_value(vnode, val) {
283 ret = snprintf(buf, rest(buf, end), "%c%s", c, val);
284 if (ret < 0)
285 return ret;
286 buf += ret;
287 c = ',';
288 }
289 if (rest(buf, end) > 2)
290 strcpy(buf, "\" ");
291 buf += 2;
292 }
293
294 return buf - (end - size);
295}
296#undef rest
297
298/* Make an extra command line under given key word */
299static char * __init xbc_make_cmdline(const char *key)
300{
301 struct xbc_node *root;
302 char *new_cmdline;
303 int ret, len = 0;
304
305 root = xbc_find_node(key);
306 if (!root)
307 return NULL;
308
309 /* Count required buffer size */
310 len = xbc_snprint_cmdline(NULL, 0, root);
311 if (len <= 0)
312 return NULL;
313
314 new_cmdline = memblock_alloc(len + 1, SMP_CACHE_BYTES);
315 if (!new_cmdline) {
316 pr_err("Failed to allocate memory for extra kernel cmdline.\n");
317 return NULL;
318 }
319
320 ret = xbc_snprint_cmdline(new_cmdline, len + 1, root);
321 if (ret < 0 || ret > len) {
322 pr_err("Failed to print extra kernel cmdline.\n");
323 return NULL;
324 }
325
326 return new_cmdline;
327}
328
7684b858
MH
329u32 boot_config_checksum(unsigned char *p, u32 size)
330{
331 u32 ret = 0;
332
333 while (size--)
334 ret += *p++;
335
336 return ret;
337}
338
7495e092 339static void __init setup_boot_config(const char *cmdline)
7684b858
MH
340{
341 u32 size, csum;
342 char *data, *copy;
7495e092 343 const char *p;
7684b858
MH
344 u32 *hdr;
345
7495e092
SRV
346 p = strstr(cmdline, "bootconfig");
347 if (!p || (p != cmdline && !isspace(*(p-1))) ||
348 (p[10] && !isspace(p[10])))
7684b858
MH
349 return;
350
7495e092
SRV
351 if (!initrd_end)
352 goto not_found;
353
7684b858
MH
354 hdr = (u32 *)(initrd_end - 8);
355 size = hdr[0];
356 csum = hdr[1];
357
7495e092
SRV
358 if (size >= XBC_DATA_MAX) {
359 pr_err("bootconfig size %d greater than max size %d\n",
360 size, XBC_DATA_MAX);
7684b858 361 return;
7495e092 362 }
7684b858
MH
363
364 data = ((void *)hdr) - size;
365 if ((unsigned long)data < initrd_start)
7495e092 366 goto not_found;
7684b858 367
7495e092
SRV
368 if (boot_config_checksum((unsigned char *)data, size) != csum) {
369 pr_err("bootconfig checksum failed\n");
7684b858 370 return;
7495e092 371 }
7684b858
MH
372
373 copy = memblock_alloc(size + 1, SMP_CACHE_BYTES);
374 if (!copy) {
e241d14a 375 pr_err("Failed to allocate memory for bootconfig\n");
7684b858
MH
376 return;
377 }
378
379 memcpy(copy, data, size);
380 copy[size] = '\0';
381
382 if (xbc_init(copy) < 0)
e241d14a 383 pr_err("Failed to parse bootconfig\n");
51887d03 384 else {
e241d14a 385 pr_info("Load bootconfig: %d bytes\n", size);
51887d03
MH
386 /* keys starting with "kernel." are passed via cmdline */
387 extra_command_line = xbc_make_cmdline("kernel");
13199162
MH
388 /* Also, "init." keys are init arguments */
389 extra_init_args = xbc_make_cmdline("init");
51887d03 390 }
7495e092
SRV
391 return;
392not_found:
393 pr_err("'bootconfig' found on command line, but no bootconfig found\n");
7684b858
MH
394}
395#else
7495e092 396#define setup_boot_config(cmdline) do { } while (0)
7684b858
MH
397#endif
398
a99cd112 399/* Change NUL term back to "=", to make "param" the whole string. */
ecc86170
LR
400static int __init repair_env_string(char *param, char *val,
401 const char *unused, void *arg)
1da177e4 402{
1da177e4
LT
403 if (val) {
404 /* param=val or param="val"? */
405 if (val == param+strlen(param)+1)
406 val[-1] = '=';
407 else if (val == param+strlen(param)+2) {
408 val[-2] = '=';
409 memmove(val-1, val, strlen(val)+1);
410 val--;
411 } else
412 BUG();
413 }
a99cd112
CM
414 return 0;
415}
416
51e158c1 417/* Anything after -- gets handed straight to init. */
ecc86170
LR
418static int __init set_init_arg(char *param, char *val,
419 const char *unused, void *arg)
51e158c1
RR
420{
421 unsigned int i;
422
423 if (panic_later)
424 return 0;
425
ecc86170 426 repair_env_string(param, val, unused, NULL);
51e158c1
RR
427
428 for (i = 0; argv_init[i]; i++) {
429 if (i == MAX_INIT_ARGS) {
430 panic_later = "init";
431 panic_param = param;
432 return 0;
433 }
434 }
435 argv_init[i] = param;
436 return 0;
437}
438
a99cd112
CM
439/*
440 * Unknown boot options get handed to init, unless they look like
441 * unused parameters (modprobe will find them in /proc/cmdline).
442 */
ecc86170
LR
443static int __init unknown_bootoption(char *param, char *val,
444 const char *unused, void *arg)
a99cd112 445{
ecc86170 446 repair_env_string(param, val, unused, NULL);
1da177e4
LT
447
448 /* Handle obsolete-style parameters */
449 if (obsolete_checksetup(param))
450 return 0;
451
f066a4f6
RR
452 /* Unused module parameter. */
453 if (strchr(param, '.') && (!val || strchr(param, '.') < val))
1da177e4 454 return 0;
1da177e4
LT
455
456 if (panic_later)
457 return 0;
458
459 if (val) {
460 /* Environment option */
461 unsigned int i;
462 for (i = 0; envp_init[i]; i++) {
463 if (i == MAX_INIT_ENVS) {
499a4584 464 panic_later = "env";
1da177e4
LT
465 panic_param = param;
466 }
467 if (!strncmp(param, envp_init[i], val - param))
468 break;
469 }
470 envp_init[i] = param;
471 } else {
472 /* Command line option */
473 unsigned int i;
474 for (i = 0; argv_init[i]; i++) {
475 if (i == MAX_INIT_ARGS) {
499a4584 476 panic_later = "init";
1da177e4
LT
477 panic_param = param;
478 }
479 }
480 argv_init[i] = param;
481 }
482 return 0;
483}
484
485static int __init init_setup(char *str)
486{
487 unsigned int i;
488
489 execute_command = str;
490 /*
491 * In case LILO is going to boot us with default command line,
492 * it prepends "auto" before the whole cmdline which makes
493 * the shell think it should execute a script with such name.
494 * So we ignore all arguments entered _before_ init=... [MJ]
495 */
496 for (i = 1; i < MAX_INIT_ARGS; i++)
497 argv_init[i] = NULL;
498 return 1;
499}
500__setup("init=", init_setup);
501
ffdfc409
OJ
502static int __init rdinit_setup(char *str)
503{
504 unsigned int i;
505
506 ramdisk_execute_command = str;
507 /* See "auto" comment in init_setup */
508 for (i = 1; i < MAX_INIT_ARGS; i++)
509 argv_init[i] = NULL;
510 return 1;
511}
512__setup("rdinit=", rdinit_setup);
513
1da177e4 514#ifndef CONFIG_SMP
34db18a0 515static const unsigned int setup_max_cpus = NR_CPUS;
e0982e90 516static inline void setup_nr_cpu_ids(void) { }
1da177e4 517static inline void smp_prepare_cpus(unsigned int maxcpus) { }
1da177e4
LT
518#endif
519
30d7e0d4
ABL
520/*
521 * We need to store the untouched command line for future reference.
522 * We also need to store the touched command line since the parameter
523 * parsing is performed in place, and we should allow a component to
524 * store reference of name/value for future reference.
525 */
526static void __init setup_command_line(char *command_line)
527{
13199162 528 size_t len, xlen = 0, ilen = 0;
51887d03
MH
529
530 if (extra_command_line)
531 xlen = strlen(extra_command_line);
13199162
MH
532 if (extra_init_args)
533 ilen = strlen(extra_init_args) + 4; /* for " -- " */
51887d03
MH
534
535 len = xlen + strlen(boot_command_line) + 1;
f5c7310a 536
13199162 537 saved_command_line = memblock_alloc(len + ilen, SMP_CACHE_BYTES);
f5c7310a 538 if (!saved_command_line)
13199162 539 panic("%s: Failed to allocate %zu bytes\n", __func__, len + ilen);
f5c7310a 540
f5c7310a
MR
541 static_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
542 if (!static_command_line)
543 panic("%s: Failed to allocate %zu bytes\n", __func__, len);
544
51887d03
MH
545 if (xlen) {
546 /*
547 * We have to put extra_command_line before boot command
548 * lines because there could be dashes (separator of init
549 * command line) in the command lines.
550 */
551 strcpy(saved_command_line, extra_command_line);
552 strcpy(static_command_line, extra_command_line);
553 }
554 strcpy(saved_command_line + xlen, boot_command_line);
555 strcpy(static_command_line + xlen, command_line);
13199162
MH
556
557 if (ilen) {
558 /*
559 * Append supplemental init boot args to saved_command_line
560 * so that user can check what command line options passed
561 * to init.
562 */
563 len = strlen(saved_command_line);
564 if (!strstr(boot_command_line, " -- ")) {
565 strcpy(saved_command_line + len, " -- ");
566 len += 4;
567 } else
568 saved_command_line[len++] = ' ';
569
570 strcpy(saved_command_line + len, extra_init_args);
571 }
30d7e0d4
ABL
572}
573
1da177e4
LT
574/*
575 * We need to finalize in a non-__init function or else race conditions
576 * between the root thread and the init thread may cause start_kernel to
577 * be reaped by free_initmem before the root thread has proceeded to
578 * cpu_idle.
579 *
580 * gcc-3.4 accidentally inlines this function, so use noinline.
581 */
582
b433c3d4
PZ
583static __initdata DECLARE_COMPLETION(kthreadd_done);
584
53c99bd6 585noinline void __ref rest_init(void)
1da177e4 586{
8fb12156 587 struct task_struct *tsk;
73c27992
EB
588 int pid;
589
7db905e6 590 rcu_scheduler_starting();
b433c3d4 591 /*
97158569 592 * We need to spawn init first so that it obtains pid 1, however
b433c3d4
PZ
593 * the init task will end up wanting to create kthreads, which, if
594 * we schedule it before we create kthreadd, will OOPS.
595 */
8fb12156
TG
596 pid = kernel_thread(kernel_init, NULL, CLONE_FS);
597 /*
598 * Pin init on the boot CPU. Task migration is not properly working
599 * until sched_init_smp() has been run. It will set the allowed
600 * CPUs for init to the non isolated CPUs.
601 */
602 rcu_read_lock();
603 tsk = find_task_by_pid_ns(pid, &init_pid_ns);
604 set_cpus_allowed_ptr(tsk, cpumask_of(smp_processor_id()));
605 rcu_read_unlock();
606
1da177e4 607 numa_default_policy();
73c27992 608 pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
d11c563d 609 rcu_read_lock();
5cd20455 610 kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
d11c563d 611 rcu_read_unlock();
1c3c5eab
TG
612
613 /*
614 * Enable might_sleep() and smp_processor_id() checks.
c1a280b6 615 * They cannot be enabled earlier because with CONFIG_PREEMPTION=y
1c3c5eab
TG
616 * kernel_thread() would trigger might_sleep() splats. With
617 * CONFIG_PREEMPT_VOLUNTARY=y the init task might have scheduled
618 * already, but it's stuck on the kthreadd_done completion.
619 */
620 system_state = SYSTEM_SCHEDULING;
621
b433c3d4 622 complete(&kthreadd_done);
f340c0d1
IM
623
624 /*
625 * The boot idle thread must execute schedule()
1df21055 626 * at least once to get things moving:
f340c0d1 627 */
bd2f5536 628 schedule_preempt_disabled();
5bfb5d69 629 /* Call into cpu_idle with preempt disabled */
a1a04ec3 630 cpu_startup_entry(CPUHP_ONLINE);
1df21055 631}
1da177e4
LT
632
633/* Check for early params. */
ecc86170
LR
634static int __init do_early_param(char *param, char *val,
635 const char *unused, void *arg)
1da177e4 636{
914dcaa8 637 const struct obs_kernel_param *p;
1da177e4
LT
638
639 for (p = __setup_start; p < __setup_end; p++) {
b1e4d20c 640 if ((p->early && parameq(param, p->str)) ||
18a8bd94
YL
641 (strcmp(param, "console") == 0 &&
642 strcmp(p->str, "earlycon") == 0)
643 ) {
1da177e4 644 if (p->setup_func(val) != 0)
ea676e84 645 pr_warn("Malformed early option '%s'\n", param);
1da177e4
LT
646 }
647 }
648 /* We accept everything at this stage. */
649 return 0;
650}
651
13977091
MD
652void __init parse_early_options(char *cmdline)
653{
ecc86170
LR
654 parse_args("early options", cmdline, NULL, 0, 0, 0, NULL,
655 do_early_param);
13977091
MD
656}
657
1da177e4
LT
658/* Arch code calls this early on, or if not, just before other parsing. */
659void __init parse_early_param(void)
660{
dd4d9fec
FF
661 static int done __initdata;
662 static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata;
1da177e4
LT
663
664 if (done)
665 return;
666
667 /* All fall through to do_early_param. */
30d7e0d4 668 strlcpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);
13977091 669 parse_early_options(tmp_cmdline);
1da177e4
LT
670 done = 1;
671}
672
e7ff3a47
TG
673void __init __weak arch_post_acpi_subsys_init(void) { }
674
839ad62e 675void __init __weak smp_setup_processor_id(void)
033ab7f8
AM
676{
677}
678
eded09cc 679# if THREAD_SIZE >= PAGE_SIZE
b235beea 680void __init __weak thread_stack_cache_init(void)
8c9843e5
BH
681{
682}
eded09cc 683#endif
8c9843e5 684
c7753208
TL
685void __init __weak mem_encrypt_init(void) { }
686
4fc19708
NA
687void __init __weak poking_init(void) { }
688
782de70c 689void __init __weak pgtable_cache_init(void) { }
caa84136 690
4e37958d
SRV
691bool initcall_debug;
692core_param(initcall_debug, initcall_debug, bool, 0644);
b0dc52f1
SRV
693
694#ifdef TRACEPOINTS_ENABLED
4e37958d 695static void __init initcall_debug_enable(void);
b0dc52f1
SRV
696#else
697static inline void initcall_debug_enable(void)
698{
699}
700#endif
4e37958d 701
23a5c8cb
AP
702/* Report memory auto-initialization states for this boot. */
703static void __init report_meminit(void)
704{
705 const char *stack;
706
707 if (IS_ENABLED(CONFIG_INIT_STACK_ALL))
708 stack = "all";
709 else if (IS_ENABLED(CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF_ALL))
710 stack = "byref_all";
711 else if (IS_ENABLED(CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF))
712 stack = "byref";
713 else if (IS_ENABLED(CONFIG_GCC_PLUGIN_STRUCTLEAK_USER))
714 stack = "__user";
715 else
716 stack = "off";
717
718 pr_info("mem auto-init: stack:%s, heap alloc:%s, heap free:%s\n",
719 stack, want_init_on_alloc(GFP_KERNEL) ? "on" : "off",
720 want_init_on_free() ? "on" : "off");
721 if (want_init_on_free())
722 pr_info("mem auto-init: clearing system memory may take some time...\n");
723}
724
444f478f
PE
725/*
726 * Set up kernel memory allocators
727 */
728static void __init mm_init(void)
729{
eefa864b
JK
730 /*
731 * page_ext requires contiguous pages,
732 * bigger than MAX_ORDER unless SPARSEMEM.
733 */
734 page_ext_init_flatmem();
23a5c8cb 735 report_meminit();
444f478f
PE
736 mem_init();
737 kmem_cache_init();
c5665868 738 kmemleak_init();
b35f1819 739 pgtable_init();
a9ee3a63 740 debug_objects_mem_init();
444f478f 741 vmalloc_init();
0ddab1d2 742 ioremap_huge_init();
613e396b
TG
743 /* Should be run before the first non-init thread is created */
744 init_espfix_bsp();
aa8c6248
TG
745 /* Should be run after espfix64 is set up. */
746 pti_init();
444f478f
PE
747}
748
53c99bd6
MS
749void __init __weak arch_call_rest_init(void)
750{
751 rest_init();
752}
753
722a9f92 754asmlinkage __visible void __init start_kernel(void)
1da177e4 755{
dd4d9fec
FF
756 char *command_line;
757 char *after_dashes;
033ab7f8 758
d4311ff1 759 set_task_stack_end_magic(&init_task);
73839c5b 760 smp_setup_processor_id();
3ac7fe5a 761 debug_objects_early_init();
42059429 762
ddbcc7e8 763 cgroup_init_early();
fbb9ce95
IM
764
765 local_irq_disable();
2ce802f6 766 early_boot_irqs_disabled = true;
fbb9ce95 767
1b3b3b49
VK
768 /*
769 * Interrupts are still disabled. Do necessary setups, then
770 * enable them.
771 */
44fd2299 772 boot_cpu_init();
1da177e4 773 page_address_init();
ea676e84 774 pr_notice("%s", linux_banner);
e6b1db98 775 early_security_init();
1da177e4 776 setup_arch(&command_line);
7495e092 777 setup_boot_config(command_line);
30d7e0d4 778 setup_command_line(command_line);
e0982e90 779 setup_nr_cpu_ids();
d6647bdf 780 setup_per_cpu_areas();
44fd2299 781 smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
b5b1404d 782 boot_cpu_hotplug_init();
1da177e4 783
72675e13 784 build_all_zonelists(NULL);
83b519e8
PE
785 page_alloc_init();
786
51887d03 787 pr_notice("Kernel command line: %s\n", saved_command_line);
6041186a
DW
788 /* parameters may set static keys */
789 jump_label_init();
83b519e8 790 parse_early_param();
51e158c1
RR
791 after_dashes = parse_args("Booting kernel",
792 static_command_line, __start___param,
793 __stop___param - __start___param,
ecc86170 794 -1, -1, NULL, &unknown_bootoption);
3438cf54 795 if (!IS_ERR_OR_NULL(after_dashes))
51e158c1 796 parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
ecc86170 797 NULL, set_init_arg);
13199162
MH
798 if (extra_init_args)
799 parse_args("Setting extra init args", extra_init_args,
800 NULL, 0, -1, -1, NULL, set_init_arg);
97ce2c88 801
83b519e8
PE
802 /*
803 * These use large bootmem allocations and must precede
804 * kmem_cache_init()
805 */
162a7e75 806 setup_log_buf(0);
83b519e8
PE
807 vfs_caches_init_early();
808 sort_main_extable();
809 trap_init();
444f478f 810 mm_init();
de03c72c 811
f631718d
SRV
812 ftrace_init();
813
e725c731
SRV
814 /* trace_printk can be enabled here */
815 early_trace_init();
816
1da177e4
LT
817 /*
818 * Set up the scheduler prior starting any interrupts (such as the
819 * timer interrupt). Full topology setup happens at smp_init()
820 * time - but meanwhile we still have a functioning scheduler.
821 */
822 sched_init();
823 /*
824 * Disable preemption - early bootup scheduling is extremely
825 * fragile until we cpu_idle() for the first time.
826 */
827 preempt_disable();
dd4d9fec
FF
828 if (WARN(!irqs_disabled(),
829 "Interrupts were enabled *very* early, fixing it\n"))
c4a68306 830 local_irq_disable();
0a835c4f 831 radix_tree_init();
3347fa09 832
7d229c66
TS
833 /*
834 * Set up housekeeping before setting up workqueues to allow the unbound
835 * workqueue to take non-housekeeping into account.
836 */
837 housekeeping_init();
838
3347fa09
TH
839 /*
840 * Allow workqueue creation and work item queueing/cancelling
841 * early. Work item execution depends on kthreads and starts after
842 * workqueue_init().
843 */
844 workqueue_init_early();
845
1da177e4 846 rcu_init();
5f893b26 847
e725c731 848 /* Trace events are available after this */
5f893b26
SRRH
849 trace_init();
850
4e37958d
SRV
851 if (initcall_debug)
852 initcall_debug_enable();
853
65f382fd 854 context_tracking_init();
0b8f1efa
YL
855 /* init some links before init_ISA_irqs() */
856 early_irq_init();
1da177e4 857 init_IRQ();
ad2b1353 858 tick_init();
d6dd50e0 859 rcu_init_nohz();
1da177e4 860 init_timers();
c0a31329 861 hrtimers_init();
1da177e4 862 softirq_init();
ad596171 863 timekeeping_init();
d5553523
KC
864
865 /*
866 * For best initial stack canary entropy, prepare it after:
867 * - setup_arch() for any UEFI RNG entropy and boot cmdline access
868 * - timekeeping_init() for ktime entropy used in rand_initialize()
869 * - rand_initialize() to get any arch-specific entropy like RDRAND
870 * - add_latent_entropy() to get any latent entropy
871 * - adding command line entropy
872 */
873 rand_initialize();
874 add_latent_entropy();
875 add_device_randomness(command_line, strlen(command_line));
876 boot_init_stack_canary();
877
88fecaa2 878 time_init();
f92bac3b 879 printk_safe_init();
9e630205 880 perf_event_init();
93e02814 881 profile_init();
d8ad7d11 882 call_function_init();
f91eb62f 883 WARN(!irqs_disabled(), "Interrupts were enabled early\n");
c3bc8fd6 884
2ce802f6 885 early_boot_irqs_disabled = false;
93e02814 886 local_irq_enable();
dcce284a 887
7e85ee0c 888 kmem_cache_init_late();
1da177e4
LT
889
890 /*
891 * HACK ALERT! This is early. We're enabling the console before
892 * we've done PCI setups etc, and console_init() must be aware of
893 * this. But we do want output early, in case something goes wrong.
894 */
895 console_init();
896 if (panic_later)
499a4584
TH
897 panic("Too many boot %s vars at `%s'", panic_later,
898 panic_param);
fbb9ce95 899
c3bc8fd6 900 lockdep_init();
fbb9ce95 901
9a11b49a
IM
902 /*
903 * Need to run this when irqs are enabled, because it wants
904 * to self-test [hard/soft]-irqs on/off lock inversion bugs
905 * too:
906 */
907 locking_selftest();
908
c7753208
TL
909 /*
910 * This needs to be called before any devices perform DMA
911 * operations that might use the SWIOTLB bounce buffers. It will
912 * mark the bounce buffers as decrypted so that their usage will
913 * not cause "plain-text" data to be decrypted when accessed.
914 */
915 mem_encrypt_init();
916
1da177e4
LT
917#ifdef CONFIG_BLK_DEV_INITRD
918 if (initrd_start && !initrd_below_start_ok &&
bd673c7c 919 page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
ea676e84 920 pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n",
bd673c7c
GU
921 page_to_pfn(virt_to_page((void *)initrd_start)),
922 min_low_pfn);
1da177e4
LT
923 initrd_start = 0;
924 }
925#endif
e7c8d5c9 926 setup_per_cpu_pageset();
1da177e4 927 numa_policy_init();
9c71206d 928 acpi_early_init();
1da177e4
LT
929 if (late_time_init)
930 late_time_init();
857baa87 931 sched_clock_init();
1da177e4 932 calibrate_delay();
95846ecf 933 pid_idr_init();
1da177e4 934 anon_vma_init();
11520e5e 935#ifdef CONFIG_X86
83e68189 936 if (efi_enabled(EFI_RUNTIME_SERVICES))
11520e5e
LT
937 efi_enter_virtual_mode();
938#endif
b235beea 939 thread_stack_cache_init();
d84f4f99 940 cred_init();
ff691f6e 941 fork_init();
1da177e4 942 proc_caches_init();
3ea056c5 943 uts_ns_init();
1da177e4 944 buffer_init();
1da177e4
LT
945 key_init();
946 security_init();
0b4b3827 947 dbg_late_init();
4248b0da 948 vfs_caches_init();
62906027 949 pagecache_init();
1da177e4 950 signals_init();
09652320 951 seq_file_init();
1da177e4 952 proc_root_init();
e149ed2b 953 nsfs_init();
1da177e4 954 cpuset_init();
695df213 955 cgroup_init();
c757249a 956 taskstats_init_early();
ca74e92b 957 delayacct_init();
1da177e4 958
4fc19708 959 poking_init();
1da177e4
LT
960 check_bugs();
961
b064a8fa 962 acpi_subsystem_init();
e7ff3a47 963 arch_post_acpi_subsys_init();
6ae6996a 964 sfi_init_late();
1da177e4
LT
965
966 /* Do the rest non-__init'ed, we're now alive */
53c99bd6 967 arch_call_rest_init();
1da177e4
LT
968}
969
b99b87f7
PO
970/* Call all constructor functions linked into the kernel. */
971static void __init do_ctors(void)
972{
973#ifdef CONFIG_CONSTRUCTORS
196a15b4 974 ctor_fn_t *fn = (ctor_fn_t *) __ctors_start;
b99b87f7 975
196a15b4
HS
976 for (; fn < (ctor_fn_t *) __ctors_end; fn++)
977 (*fn)();
b99b87f7
PO
978#endif
979}
980
7b0b73d7
PB
981#ifdef CONFIG_KALLSYMS
982struct blacklist_entry {
983 struct list_head next;
984 char *buf;
985};
986
987static __initdata_or_module LIST_HEAD(blacklisted_initcalls);
988
989static int __init initcall_blacklist(char *str)
990{
991 char *str_entry;
992 struct blacklist_entry *entry;
993
994 /* str argument is a comma-separated list of functions */
995 do {
996 str_entry = strsep(&str, ",");
997 if (str_entry) {
998 pr_debug("blacklisting initcall %s\n", str_entry);
7e1c4e27
MR
999 entry = memblock_alloc(sizeof(*entry),
1000 SMP_CACHE_BYTES);
f5c7310a
MR
1001 if (!entry)
1002 panic("%s: Failed to allocate %zu bytes\n",
1003 __func__, sizeof(*entry));
7e1c4e27
MR
1004 entry->buf = memblock_alloc(strlen(str_entry) + 1,
1005 SMP_CACHE_BYTES);
f5c7310a
MR
1006 if (!entry->buf)
1007 panic("%s: Failed to allocate %zu bytes\n",
1008 __func__, strlen(str_entry) + 1);
7b0b73d7
PB
1009 strcpy(entry->buf, str_entry);
1010 list_add(&entry->next, &blacklisted_initcalls);
1011 }
1012 } while (str_entry);
1013
1014 return 0;
1015}
1016
1017static bool __init_or_module initcall_blacklisted(initcall_t fn)
1018{
7b0b73d7 1019 struct blacklist_entry *entry;
c8cdd2be 1020 char fn_name[KSYM_SYMBOL_LEN];
0fd5ed8d 1021 unsigned long addr;
7b0b73d7 1022
c8cdd2be 1023 if (list_empty(&blacklisted_initcalls))
7b0b73d7
PB
1024 return false;
1025
0fd5ed8d
RV
1026 addr = (unsigned long) dereference_function_descriptor(fn);
1027 sprint_symbol_no_offset(fn_name, addr);
c8cdd2be 1028
841c06d7
PB
1029 /*
1030 * fn will be "function_name [module_name]" where [module_name] is not
1031 * displayed for built-in init functions. Strip off the [module_name].
1032 */
1033 strreplace(fn_name, ' ', '\0');
1034
e6fd1fb3 1035 list_for_each_entry(entry, &blacklisted_initcalls, next) {
7b0b73d7
PB
1036 if (!strcmp(fn_name, entry->buf)) {
1037 pr_debug("initcall %s blacklisted\n", fn_name);
7b0b73d7
PB
1038 return true;
1039 }
1040 }
1041
7b0b73d7
PB
1042 return false;
1043}
1044#else
1045static int __init initcall_blacklist(char *str)
1046{
1047 pr_warn("initcall_blacklist requires CONFIG_KALLSYMS\n");
1048 return 0;
1049}
1050
1051static bool __init_or_module initcall_blacklisted(initcall_t fn)
1052{
1053 return false;
1054}
1055#endif
1056__setup("initcall_blacklist=", initcall_blacklist);
1057
4e37958d
SRV
1058static __init_or_module void
1059trace_initcall_start_cb(void *data, initcall_t fn)
1da177e4 1060{
4e37958d 1061 ktime_t *calltime = (ktime_t *)data;
1da177e4 1062
d75f773c 1063 printk(KERN_DEBUG "calling %pS @ %i\n", fn, task_pid_nr(current));
4e37958d
SRV
1064 *calltime = ktime_get();
1065}
1066
1067static __init_or_module void
1068trace_initcall_finish_cb(void *data, initcall_t fn, int ret)
1069{
1070 ktime_t *calltime = (ktime_t *)data;
1071 ktime_t delta, rettime;
1072 unsigned long long duration;
1073
22c5c03b 1074 rettime = ktime_get();
4e37958d 1075 delta = ktime_sub(rettime, *calltime);
22c5c03b 1076 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
d75f773c 1077 printk(KERN_DEBUG "initcall %pS returned %d after %lld usecs\n",
ea676e84 1078 fn, ret, duration);
4e37958d 1079}
1da177e4 1080
4e37958d
SRV
1081static ktime_t initcall_calltime;
1082
b0dc52f1 1083#ifdef TRACEPOINTS_ENABLED
4e37958d
SRV
1084static void __init initcall_debug_enable(void)
1085{
1086 int ret;
1087
1088 ret = register_trace_initcall_start(trace_initcall_start_cb,
1089 &initcall_calltime);
1090 ret |= register_trace_initcall_finish(trace_initcall_finish_cb,
1091 &initcall_calltime);
1092 WARN(ret, "Failed to register initcall tracepoints\n");
22c5c03b 1093}
b0dc52f1
SRV
1094# define do_trace_initcall_start trace_initcall_start
1095# define do_trace_initcall_finish trace_initcall_finish
1096#else
1097static inline void do_trace_initcall_start(initcall_t fn)
1098{
1099 if (!initcall_debug)
1100 return;
1101 trace_initcall_start_cb(&initcall_calltime, fn);
1102}
1103static inline void do_trace_initcall_finish(initcall_t fn, int ret)
1104{
1105 if (!initcall_debug)
1106 return;
1107 trace_initcall_finish_cb(&initcall_calltime, fn, ret);
1108}
1109#endif /* !TRACEPOINTS_ENABLED */
22c5c03b 1110
e4461271 1111int __init_or_module do_one_initcall(initcall_t fn)
22c5c03b
KW
1112{
1113 int count = preempt_count();
ff1c8fac 1114 char msgbuf[64];
4e37958d 1115 int ret;
22c5c03b 1116
7b0b73d7
PB
1117 if (initcall_blacklisted(fn))
1118 return -EPERM;
1119
b0dc52f1 1120 do_trace_initcall_start(fn);
4e37958d 1121 ret = fn();
b0dc52f1 1122 do_trace_initcall_finish(fn, ret);
8f0c45cd 1123
e0df154f 1124 msgbuf[0] = 0;
e662e1cf 1125
e0df154f 1126 if (preempt_count() != count) {
bf5d770b 1127 sprintf(msgbuf, "preemption imbalance ");
4a2b4b22 1128 preempt_count_set(count);
1da177e4 1129 }
e0df154f 1130 if (irqs_disabled()) {
a76bfd0d 1131 strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
e0df154f
LT
1132 local_irq_enable();
1133 }
d75f773c 1134 WARN(msgbuf[0], "initcall %pS returned with %s\n", fn, msgbuf);
59f9415f 1135
38addce8 1136 add_latent_entropy();
30dbb20e 1137 return ret;
e0df154f
LT
1138}
1139
1140
1b1eeca7
AB
1141extern initcall_entry_t __initcall_start[];
1142extern initcall_entry_t __initcall0_start[];
1143extern initcall_entry_t __initcall1_start[];
1144extern initcall_entry_t __initcall2_start[];
1145extern initcall_entry_t __initcall3_start[];
1146extern initcall_entry_t __initcall4_start[];
1147extern initcall_entry_t __initcall5_start[];
1148extern initcall_entry_t __initcall6_start[];
1149extern initcall_entry_t __initcall7_start[];
1150extern initcall_entry_t __initcall_end[];
1151
1152static initcall_entry_t *initcall_levels[] __initdata = {
026cee00
PM
1153 __initcall0_start,
1154 __initcall1_start,
1155 __initcall2_start,
1156 __initcall3_start,
1157 __initcall4_start,
1158 __initcall5_start,
1159 __initcall6_start,
1160 __initcall7_start,
1161 __initcall_end,
1162};
1163
96263d28 1164/* Keep these in sync with initcalls in include/linux/init.h */
7c8f7193 1165static const char *initcall_level_names[] __initdata = {
a6fb6012 1166 "pure",
9fb48c74
JC
1167 "core",
1168 "postcore",
1169 "arch",
1170 "subsys",
1171 "fs",
1172 "device",
1173 "late",
026cee00
PM
1174};
1175
0068c92a 1176static void __init do_initcall_level(int level, char *command_line)
e0df154f 1177{
1b1eeca7 1178 initcall_entry_t *fn;
e0df154f 1179
026cee00 1180 parse_args(initcall_level_names[level],
0068c92a 1181 command_line, __start___param,
026cee00
PM
1182 __stop___param - __start___param,
1183 level, level,
ecc86170 1184 NULL, &repair_env_string);
026cee00 1185
4ee7c60d 1186 trace_initcall_level(initcall_level_names[level]);
026cee00 1187 for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++)
1b1eeca7 1188 do_one_initcall(initcall_from_entry(fn));
1da177e4
LT
1189}
1190
026cee00
PM
1191static void __init do_initcalls(void)
1192{
1193 int level;
0068c92a
MH
1194 size_t len = strlen(saved_command_line) + 1;
1195 char *command_line;
1196
1197 command_line = kzalloc(len, GFP_KERNEL);
1198 if (!command_line)
1199 panic("%s: Failed to allocate %zu bytes\n", __func__, len);
1200
1201 for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++) {
1202 /* Parser modifies command_line, restore it each time */
1203 strcpy(command_line, saved_command_line);
1204 do_initcall_level(level, command_line);
1205 }
026cee00 1206
0068c92a 1207 kfree(command_line);
026cee00
PM
1208}
1209
1da177e4
LT
1210/*
1211 * Ok, the machine is now initialized. None of the devices
1212 * have been touched yet, but the CPU subsystem is up and
1213 * running, and memory and process management works.
1214 *
1215 * Now we can finally start doing some real work..
1216 */
1217static void __init do_basic_setup(void)
1218{
759ee091 1219 cpuset_init_smp();
1da177e4 1220 driver_init();
b04c3afb 1221 init_irq_proc();
b99b87f7 1222 do_ctors();
d5767c53 1223 usermodehelper_enable();
b0f84374 1224 do_initcalls();
1da177e4
LT
1225}
1226
7babe8db 1227static void __init do_pre_smp_initcalls(void)
c2147a50 1228{
1b1eeca7 1229 initcall_entry_t *fn;
c2147a50 1230
4ee7c60d 1231 trace_initcall_level("early");
026cee00 1232 for (fn = __initcall_start; fn < __initcall0_start; fn++)
1b1eeca7 1233 do_one_initcall(initcall_from_entry(fn));
c2147a50
EGM
1234}
1235
a74fb73c 1236static int run_init_process(const char *init_filename)
1da177e4
LT
1237{
1238 argv_init[0] = init_filename;
3f5c15d8 1239 pr_info("Run %s as init process\n", init_filename);
c4ad8f98 1240 return do_execve(getname_kernel(init_filename),
ae903caa
AV
1241 (const char __user *const __user *)argv_init,
1242 (const char __user *const __user *)envp_init);
1da177e4
LT
1243}
1244
ba24762b
MO
1245static int try_to_run_init_process(const char *init_filename)
1246{
1247 int ret;
1248
1249 ret = run_init_process(init_filename);
1250
1251 if (ret && ret != -ENOENT) {
1252 pr_err("Starting init: %s exists but couldn't execute it (error %d)\n",
1253 init_filename, ret);
1254 }
1255
1256 return ret;
1257}
1258
f80b0c90 1259static noinline void __init kernel_init_freeable(void);
d6b21238 1260
0f5bf6d0 1261#if defined(CONFIG_STRICT_KERNEL_RWX) || defined(CONFIG_STRICT_MODULE_RWX)
39290b38 1262bool rodata_enabled __ro_after_init = true;
d2aa1aca
KC
1263static int __init set_debug_rodata(char *str)
1264{
1265 return strtobool(str, &rodata_enabled);
1266}
1267__setup("rodata=", set_debug_rodata);
39290b38 1268#endif
d2aa1aca 1269
0f5bf6d0 1270#ifdef CONFIG_STRICT_KERNEL_RWX
d2aa1aca
KC
1271static void mark_readonly(void)
1272{
2959a5f7 1273 if (rodata_enabled) {
ae646f0b 1274 /*
ba180314
PM
1275 * load_module() results in W+X mappings, which are cleaned
1276 * up with call_rcu(). Let's make sure that queued work is
ae646f0b
JH
1277 * flushed so that we don't hit false positives looking for
1278 * insecure pages which are W+X.
1279 */
ba180314 1280 rcu_barrier();
d2aa1aca 1281 mark_rodata_ro();
2959a5f7
JP
1282 rodata_test();
1283 } else
d2aa1aca
KC
1284 pr_info("Kernel memory protection disabled.\n");
1285}
1286#else
1287static inline void mark_readonly(void)
1288{
1289 pr_warn("This architecture does not have kernel memory protection.\n");
1290}
1291#endif
1292
997aef68
MR
1293void __weak free_initmem(void)
1294{
f4039999 1295 free_initmem_default(POISON_FREE_INITMEM);
997aef68
MR
1296}
1297
d6b21238 1298static int __ref kernel_init(void *unused)
ee5bfa64 1299{
ba24762b
MO
1300 int ret;
1301
d6b21238 1302 kernel_init_freeable();
22a9d645
AV
1303 /* need to finish all async __init code before freeing the memory */
1304 async_synchronize_full();
b80f0f6c 1305 ftrace_free_init_mem();
ee5bfa64 1306 free_initmem();
d2aa1aca 1307 mark_readonly();
b976690f
JR
1308
1309 /*
1310 * Kernel mappings are now finalized - update the userspace page-table
1311 * to finalize PTI.
1312 */
1313 pti_finalize();
1314
ee5bfa64
VG
1315 system_state = SYSTEM_RUNNING;
1316 numa_default_policy();
1317
967dcb8f
PM
1318 rcu_end_inkernel_boot();
1319
ee5bfa64 1320 if (ramdisk_execute_command) {
ba24762b
MO
1321 ret = run_init_process(ramdisk_execute_command);
1322 if (!ret)
a74fb73c 1323 return 0;
ba24762b
MO
1324 pr_err("Failed to execute %s (error %d)\n",
1325 ramdisk_execute_command, ret);
ee5bfa64
VG
1326 }
1327
1328 /*
1329 * We try each of these until one succeeds.
1330 *
1331 * The Bourne shell can be used instead of init if we are
1332 * trying to recover a really broken machine.
1333 */
1334 if (execute_command) {
ba24762b
MO
1335 ret = run_init_process(execute_command);
1336 if (!ret)
a74fb73c 1337 return 0;
6ef4536e
AL
1338 panic("Requested init %s failed (error %d).",
1339 execute_command, ret);
ee5bfa64 1340 }
ba24762b
MO
1341 if (!try_to_run_init_process("/sbin/init") ||
1342 !try_to_run_init_process("/etc/init") ||
1343 !try_to_run_init_process("/bin/init") ||
1344 !try_to_run_init_process("/bin/sh"))
a74fb73c 1345 return 0;
ee5bfa64 1346
ba24762b 1347 panic("No working init found. Try passing init= option to kernel. "
8c27ceff 1348 "See Linux Documentation/admin-guide/init.rst for guidance.");
ee5bfa64
VG
1349}
1350
b49a733d
DB
1351void console_on_rootfs(void)
1352{
74f1a299
DB
1353 /* Open the /dev/console as stdin, this should never fail */
1354 if (ksys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
1355 pr_err("Warning: unable to open an initial console.\n");
b49a733d 1356
74f1a299
DB
1357 /* create stdout/stderr */
1358 (void) ksys_dup(0);
1359 (void) ksys_dup(0);
b49a733d
DB
1360}
1361
f80b0c90 1362static noinline void __init kernel_init_freeable(void)
1da177e4 1363{
b433c3d4
PZ
1364 /*
1365 * Wait until kthreadd is all set-up.
1366 */
1367 wait_for_completion(&kthreadd_done);
31a67102
LT
1368
1369 /* Now the scheduler is fully set up and can do blocking allocations */
1370 gfp_allowed_mask = __GFP_BITS_MASK;
1371
58568d2a
MX
1372 /*
1373 * init can allocate pages on any node
1374 */
3c466d46 1375 set_mems_allowed(node_states[N_MEMORY]);
1da177e4 1376
9ec52099
CLG
1377 cad_pid = task_pid(current);
1378
ca74a6f8 1379 smp_prepare_cpus(setup_max_cpus);
1da177e4 1380
3347fa09
TH
1381 workqueue_init();
1382
597b7305
MH
1383 init_mm_internals();
1384
1da177e4 1385 do_pre_smp_initcalls();
004417a6 1386 lockup_detector_init();
1da177e4 1387
1da177e4
LT
1388 smp_init();
1389 sched_init_smp();
1390
0e1cc95b 1391 page_alloc_init_late();
2f1ee091
QC
1392 /* Initialize page ext after all struct pages are initialized. */
1393 page_ext_init();
0e1cc95b 1394
1da177e4
LT
1395 do_basic_setup();
1396
b49a733d 1397 console_on_rootfs();
2bd3a997 1398
1da177e4
LT
1399 /*
1400 * check if there is an early userspace init. If yes, let it do all
1401 * the work
1402 */
ffdfc409
OJ
1403
1404 if (!ramdisk_execute_command)
1405 ramdisk_execute_command = "/init";
1406
cbfe20f5
DB
1407 if (ksys_access((const char __user *)
1408 ramdisk_execute_command, 0) != 0) {
ffdfc409 1409 ramdisk_execute_command = NULL;
1da177e4 1410 prepare_namespace();
ffdfc409 1411 }
1da177e4
LT
1412
1413 /*
1414 * Ok, we have completed the initial bootup, and
1415 * we're essentially up and running. Get rid of the
1416 * initmem segments and start the user-mode stuff..
c9cd2ce2
DK
1417 *
1418 * rootfs is available now, try loading the public keys
1419 * and default modules
1da177e4 1420 */
bb813f4c 1421
c9cd2ce2 1422 integrity_load_keys();
1da177e4 1423}