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