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