main.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /*
  2. * linux/init/main.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. *
  6. * GK 2/5/95 - Changed to support mounting root fs via NFS
  7. * Added initrd & change_root: Werner Almesberger & Hans Lermen, Feb '96
  8. * Moan early if gcc is old, avoiding bogus kernels - Paul Gortmaker, May '96
  9. * Simplified starting of init: Michael A. Griffith <grif@acm.org>
  10. */
  11. #define DEBUG /* Enable initcall_debug */
  12. #include <linux/types.h>
  13. #include <linux/extable.h>
  14. #include <linux/module.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/binfmts.h>
  17. #include <linux/kernel.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/stackprotector.h>
  20. #include <linux/string.h>
  21. #include <linux/ctype.h>
  22. #include <linux/delay.h>
  23. #include <linux/ioport.h>
  24. #include <linux/init.h>
  25. #include <linux/initrd.h>
  26. #include <linux/bootmem.h>
  27. #include <linux/acpi.h>
  28. #include <linux/console.h>
  29. #include <linux/nmi.h>
  30. #include <linux/percpu.h>
  31. #include <linux/kmod.h>
  32. #include <linux/vmalloc.h>
  33. #include <linux/kernel_stat.h>
  34. #include <linux/start_kernel.h>
  35. #include <linux/security.h>
  36. #include <linux/smp.h>
  37. #include <linux/profile.h>
  38. #include <linux/rcupdate.h>
  39. #include <linux/moduleparam.h>
  40. #include <linux/kallsyms.h>
  41. #include <linux/writeback.h>
  42. #include <linux/cpu.h>
  43. #include <linux/cpuset.h>
  44. #include <linux/cgroup.h>
  45. #include <linux/efi.h>
  46. #include <linux/tick.h>
  47. #include <linux/sched/isolation.h>
  48. #include <linux/interrupt.h>
  49. #include <linux/taskstats_kern.h>
  50. #include <linux/delayacct.h>
  51. #include <linux/unistd.h>
  52. #include <linux/utsname.h>
  53. #include <linux/rmap.h>
  54. #include <linux/mempolicy.h>
  55. #include <linux/key.h>
  56. #include <linux/buffer_head.h>
  57. #include <linux/page_ext.h>
  58. #include <linux/debug_locks.h>
  59. #include <linux/debugobjects.h>
  60. #include <linux/lockdep.h>
  61. #include <linux/kmemleak.h>
  62. #include <linux/pid_namespace.h>
  63. #include <linux/device.h>
  64. #include <linux/kthread.h>
  65. #include <linux/sched.h>
  66. #include <linux/sched/init.h>
  67. #include <linux/signal.h>
  68. #include <linux/idr.h>
  69. #include <linux/kgdb.h>
  70. #include <linux/ftrace.h>
  71. #include <linux/async.h>
  72. #include <linux/sfi.h>
  73. #include <linux/shmem_fs.h>
  74. #include <linux/slab.h>
  75. #include <linux/perf_event.h>
  76. #include <linux/ptrace.h>
  77. #include <linux/pti.h>
  78. #include <linux/blkdev.h>
  79. #include <linux/elevator.h>
  80. #include <linux/sched/clock.h>
  81. #include <linux/sched/task.h>
  82. #include <linux/sched/task_stack.h>
  83. #include <linux/context_tracking.h>
  84. #include <linux/random.h>
  85. #include <linux/list.h>
  86. #include <linux/integrity.h>
  87. #include <linux/proc_ns.h>
  88. #include <linux/io.h>
  89. #include <linux/cache.h>
  90. #include <linux/rodata_test.h>
  91. #include <linux/jump_label.h>
  92. #include <linux/mem_encrypt.h>
  93. #include <asm/io.h>
  94. #include <asm/bugs.h>
  95. #include <asm/setup.h>
  96. #include <asm/sections.h>
  97. #include <asm/cacheflush.h>
  98. #define CREATE_TRACE_POINTS
  99. #include <trace/events/initcall.h>
  100. static int kernel_init(void *);
  101. extern void init_IRQ(void);
  102. extern void fork_init(void);
  103. extern void radix_tree_init(void);
  104. /*
  105. * Debug helper: via this flag we know that we are in 'early bootup code'
  106. * where only the boot processor is running with IRQ disabled. This means
  107. * two things - IRQ must not be enabled before the flag is cleared and some
  108. * operations which are not allowed with IRQ disabled are allowed while the
  109. * flag is set.
  110. */
  111. bool early_boot_irqs_disabled __read_mostly;
  112. enum system_states system_state __read_mostly;
  113. EXPORT_SYMBOL(system_state);
  114. /*
  115. * Boot command-line arguments
  116. */
  117. #define MAX_INIT_ARGS CONFIG_INIT_ENV_ARG_LIMIT
  118. #define MAX_INIT_ENVS CONFIG_INIT_ENV_ARG_LIMIT
  119. extern void time_init(void);
  120. /* Default late time init is NULL. archs can override this later. */
  121. void (*__initdata late_time_init)(void);
  122. /* Untouched command line saved by arch-specific code. */
  123. char __initdata boot_command_line[COMMAND_LINE_SIZE];
  124. /* Untouched saved command line (eg. for /proc) */
  125. char *saved_command_line;
  126. /* Command line for parameter parsing */
  127. static char *static_command_line;
  128. /* Command line for per-initcall parameter parsing */
  129. static char *initcall_command_line;
  130. static char *execute_command;
  131. static char *ramdisk_execute_command;
  132. /*
  133. * Used to generate warnings if static_key manipulation functions are used
  134. * before jump_label_init is called.
  135. */
  136. bool static_key_initialized __read_mostly;
  137. EXPORT_SYMBOL_GPL(static_key_initialized);
  138. /*
  139. * If set, this is an indication to the drivers that reset the underlying
  140. * device before going ahead with the initialization otherwise driver might
  141. * rely on the BIOS and skip the reset operation.
  142. *
  143. * This is useful if kernel is booting in an unreliable environment.
  144. * For ex. kdump situation where previous kernel has crashed, BIOS has been
  145. * skipped and devices will be in unknown state.
  146. */
  147. unsigned int reset_devices;
  148. EXPORT_SYMBOL(reset_devices);
  149. static int __init set_reset_devices(char *str)
  150. {
  151. reset_devices = 1;
  152. return 1;
  153. }
  154. __setup("reset_devices", set_reset_devices);
  155. static const char *argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
  156. const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
  157. static const char *panic_later, *panic_param;
  158. extern const struct obs_kernel_param __setup_start[], __setup_end[];
  159. static bool __init obsolete_checksetup(char *line)
  160. {
  161. const struct obs_kernel_param *p;
  162. bool had_early_param = false;
  163. p = __setup_start;
  164. do {
  165. int n = strlen(p->str);
  166. if (parameqn(line, p->str, n)) {
  167. if (p->early) {
  168. /* Already done in parse_early_param?
  169. * (Needs exact match on param part).
  170. * Keep iterating, as we can have early
  171. * params and __setups of same names 8( */
  172. if (line[n] == '\0' || line[n] == '=')
  173. had_early_param = true;
  174. } else if (!p->setup_func) {
  175. pr_warn("Parameter %s is obsolete, ignored\n",
  176. p->str);
  177. return true;
  178. } else if (p->setup_func(line + n))
  179. return true;
  180. }
  181. p++;
  182. } while (p < __setup_end);
  183. return had_early_param;
  184. }
  185. /*
  186. * This should be approx 2 Bo*oMips to start (note initial shift), and will
  187. * still work even if initially too large, it will just take slightly longer
  188. */
  189. unsigned long loops_per_jiffy = (1<<12);
  190. EXPORT_SYMBOL(loops_per_jiffy);
  191. static int __init debug_kernel(char *str)
  192. {
  193. console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
  194. return 0;
  195. }
  196. static int __init quiet_kernel(char *str)
  197. {
  198. console_loglevel = CONSOLE_LOGLEVEL_QUIET;
  199. return 0;
  200. }
  201. early_param("debug", debug_kernel);
  202. early_param("quiet", quiet_kernel);
  203. static int __init loglevel(char *str)
  204. {
  205. int newlevel;
  206. /*
  207. * Only update loglevel value when a correct setting was passed,
  208. * to prevent blind crashes (when loglevel being set to 0) that
  209. * are quite hard to debug
  210. */
  211. if (get_option(&str, &newlevel)) {
  212. console_loglevel = newlevel;
  213. return 0;
  214. }
  215. return -EINVAL;
  216. }
  217. early_param("loglevel", loglevel);
  218. /* Change NUL term back to "=", to make "param" the whole string. */
  219. static int __init repair_env_string(char *param, char *val,
  220. const char *unused, void *arg)
  221. {
  222. if (val) {
  223. /* param=val or param="val"? */
  224. if (val == param+strlen(param)+1)
  225. val[-1] = '=';
  226. else if (val == param+strlen(param)+2) {
  227. val[-2] = '=';
  228. memmove(val-1, val, strlen(val)+1);
  229. val--;
  230. } else
  231. BUG();
  232. }
  233. return 0;
  234. }
  235. /* Anything after -- gets handed straight to init. */
  236. static int __init set_init_arg(char *param, char *val,
  237. const char *unused, void *arg)
  238. {
  239. unsigned int i;
  240. if (panic_later)
  241. return 0;
  242. repair_env_string(param, val, unused, NULL);
  243. for (i = 0; argv_init[i]; i++) {
  244. if (i == MAX_INIT_ARGS) {
  245. panic_later = "init";
  246. panic_param = param;
  247. return 0;
  248. }
  249. }
  250. argv_init[i] = param;
  251. return 0;
  252. }
  253. /*
  254. * Unknown boot options get handed to init, unless they look like
  255. * unused parameters (modprobe will find them in /proc/cmdline).
  256. */
  257. static int __init unknown_bootoption(char *param, char *val,
  258. const char *unused, void *arg)
  259. {
  260. repair_env_string(param, val, unused, NULL);
  261. /* Handle obsolete-style parameters */
  262. if (obsolete_checksetup(param))
  263. return 0;
  264. /* Unused module parameter. */
  265. if (strchr(param, '.') && (!val || strchr(param, '.') < val))
  266. return 0;
  267. if (panic_later)
  268. return 0;
  269. if (val) {
  270. /* Environment option */
  271. unsigned int i;
  272. for (i = 0; envp_init[i]; i++) {
  273. if (i == MAX_INIT_ENVS) {
  274. panic_later = "env";
  275. panic_param = param;
  276. }
  277. if (!strncmp(param, envp_init[i], val - param))
  278. break;
  279. }
  280. envp_init[i] = param;
  281. } else {
  282. /* Command line option */
  283. unsigned int i;
  284. for (i = 0; argv_init[i]; i++) {
  285. if (i == MAX_INIT_ARGS) {
  286. panic_later = "init";
  287. panic_param = param;
  288. }
  289. }
  290. argv_init[i] = param;
  291. }
  292. return 0;
  293. }
  294. static int __init init_setup(char *str)
  295. {
  296. unsigned int i;
  297. execute_command = str;
  298. /*
  299. * In case LILO is going to boot us with default command line,
  300. * it prepends "auto" before the whole cmdline which makes
  301. * the shell think it should execute a script with such name.
  302. * So we ignore all arguments entered _before_ init=... [MJ]
  303. */
  304. for (i = 1; i < MAX_INIT_ARGS; i++)
  305. argv_init[i] = NULL;
  306. return 1;
  307. }
  308. __setup("init=", init_setup);
  309. static int __init rdinit_setup(char *str)
  310. {
  311. unsigned int i;
  312. ramdisk_execute_command = str;
  313. /* See "auto" comment in init_setup */
  314. for (i = 1; i < MAX_INIT_ARGS; i++)
  315. argv_init[i] = NULL;
  316. return 1;
  317. }
  318. __setup("rdinit=", rdinit_setup);
  319. #ifndef CONFIG_SMP
  320. static const unsigned int setup_max_cpus = NR_CPUS;
  321. static inline void setup_nr_cpu_ids(void) { }
  322. static inline void smp_prepare_cpus(unsigned int maxcpus) { }
  323. #endif
  324. /*
  325. * We need to store the untouched command line for future reference.
  326. * We also need to store the touched command line since the parameter
  327. * parsing is performed in place, and we should allow a component to
  328. * store reference of name/value for future reference.
  329. */
  330. static void __init setup_command_line(char *command_line)
  331. {
  332. saved_command_line =
  333. memblock_virt_alloc(strlen(boot_command_line) + 1, 0);
  334. initcall_command_line =
  335. memblock_virt_alloc(strlen(boot_command_line) + 1, 0);
  336. static_command_line = memblock_virt_alloc(strlen(command_line) + 1, 0);
  337. strcpy(saved_command_line, boot_command_line);
  338. strcpy(static_command_line, command_line);
  339. }
  340. /*
  341. * We need to finalize in a non-__init function or else race conditions
  342. * between the root thread and the init thread may cause start_kernel to
  343. * be reaped by free_initmem before the root thread has proceeded to
  344. * cpu_idle.
  345. *
  346. * gcc-3.4 accidentally inlines this function, so use noinline.
  347. */
  348. static __initdata DECLARE_COMPLETION(kthreadd_done);
  349. static noinline void __ref rest_init(void)
  350. {
  351. struct task_struct *tsk;
  352. int pid;
  353. rcu_scheduler_starting();
  354. /*
  355. * We need to spawn init first so that it obtains pid 1, however
  356. * the init task will end up wanting to create kthreads, which, if
  357. * we schedule it before we create kthreadd, will OOPS.
  358. */
  359. pid = kernel_thread(kernel_init, NULL, CLONE_FS);
  360. /*
  361. * Pin init on the boot CPU. Task migration is not properly working
  362. * until sched_init_smp() has been run. It will set the allowed
  363. * CPUs for init to the non isolated CPUs.
  364. */
  365. rcu_read_lock();
  366. tsk = find_task_by_pid_ns(pid, &init_pid_ns);
  367. set_cpus_allowed_ptr(tsk, cpumask_of(smp_processor_id()));
  368. rcu_read_unlock();
  369. numa_default_policy();
  370. pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
  371. rcu_read_lock();
  372. kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
  373. rcu_read_unlock();
  374. /*
  375. * Enable might_sleep() and smp_processor_id() checks.
  376. * They cannot be enabled earlier because with CONFIG_PREEMPT=y
  377. * kernel_thread() would trigger might_sleep() splats. With
  378. * CONFIG_PREEMPT_VOLUNTARY=y the init task might have scheduled
  379. * already, but it's stuck on the kthreadd_done completion.
  380. */
  381. system_state = SYSTEM_SCHEDULING;
  382. complete(&kthreadd_done);
  383. /*
  384. * The boot idle thread must execute schedule()
  385. * at least once to get things moving:
  386. */
  387. schedule_preempt_disabled();
  388. /* Call into cpu_idle with preempt disabled */
  389. cpu_startup_entry(CPUHP_ONLINE);
  390. }
  391. /* Check for early params. */
  392. static int __init do_early_param(char *param, char *val,
  393. const char *unused, void *arg)
  394. {
  395. const struct obs_kernel_param *p;
  396. for (p = __setup_start; p < __setup_end; p++) {
  397. if ((p->early && parameq(param, p->str)) ||
  398. (strcmp(param, "console") == 0 &&
  399. strcmp(p->str, "earlycon") == 0)
  400. ) {
  401. if (p->setup_func(val) != 0)
  402. pr_warn("Malformed early option '%s'\n", param);
  403. }
  404. }
  405. /* We accept everything at this stage. */
  406. return 0;
  407. }
  408. void __init parse_early_options(char *cmdline)
  409. {
  410. parse_args("early options", cmdline, NULL, 0, 0, 0, NULL,
  411. do_early_param);
  412. }
  413. /* Arch code calls this early on, or if not, just before other parsing. */
  414. void __init parse_early_param(void)
  415. {
  416. static int done __initdata;
  417. static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata;
  418. if (done)
  419. return;
  420. /* All fall through to do_early_param. */
  421. strlcpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);
  422. parse_early_options(tmp_cmdline);
  423. done = 1;
  424. }
  425. void __init __weak arch_post_acpi_subsys_init(void) { }
  426. void __init __weak smp_setup_processor_id(void)
  427. {
  428. }
  429. # if THREAD_SIZE >= PAGE_SIZE
  430. void __init __weak thread_stack_cache_init(void)
  431. {
  432. }
  433. #endif
  434. void __init __weak mem_encrypt_init(void) { }
  435. bool initcall_debug;
  436. core_param(initcall_debug, initcall_debug, bool, 0644);
  437. #ifdef TRACEPOINTS_ENABLED
  438. static void __init initcall_debug_enable(void);
  439. #else
  440. static inline void initcall_debug_enable(void)
  441. {
  442. }
  443. #endif
  444. /*
  445. * Set up kernel memory allocators
  446. */
  447. static void __init mm_init(void)
  448. {
  449. /*
  450. * page_ext requires contiguous pages,
  451. * bigger than MAX_ORDER unless SPARSEMEM.
  452. */
  453. page_ext_init_flatmem();
  454. mem_init();
  455. kmem_cache_init();
  456. pgtable_init();
  457. vmalloc_init();
  458. ioremap_huge_init();
  459. /* Should be run before the first non-init thread is created */
  460. init_espfix_bsp();
  461. /* Should be run after espfix64 is set up. */
  462. pti_init();
  463. }
  464. asmlinkage __visible void __init start_kernel(void)
  465. {
  466. char *command_line;
  467. char *after_dashes;
  468. set_task_stack_end_magic(&init_task);
  469. smp_setup_processor_id();
  470. debug_objects_early_init();
  471. cgroup_init_early();
  472. local_irq_disable();
  473. early_boot_irqs_disabled = true;
  474. /*
  475. * Interrupts are still disabled. Do necessary setups, then
  476. * enable them.
  477. */
  478. boot_cpu_init();
  479. page_address_init();
  480. pr_notice("%s", linux_banner);
  481. setup_arch(&command_line);
  482. /*
  483. * Set up the the initial canary and entropy after arch
  484. * and after adding latent and command line entropy.
  485. */
  486. add_latent_entropy();
  487. add_device_randomness(command_line, strlen(command_line));
  488. boot_init_stack_canary();
  489. mm_init_cpumask(&init_mm);
  490. setup_command_line(command_line);
  491. setup_nr_cpu_ids();
  492. setup_per_cpu_areas();
  493. smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
  494. boot_cpu_hotplug_init();
  495. build_all_zonelists(NULL);
  496. page_alloc_init();
  497. pr_notice("Kernel command line: %s\n", boot_command_line);
  498. parse_early_param();
  499. after_dashes = parse_args("Booting kernel",
  500. static_command_line, __start___param,
  501. __stop___param - __start___param,
  502. -1, -1, NULL, &unknown_bootoption);
  503. if (!IS_ERR_OR_NULL(after_dashes))
  504. parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
  505. NULL, set_init_arg);
  506. jump_label_init();
  507. /*
  508. * These use large bootmem allocations and must precede
  509. * kmem_cache_init()
  510. */
  511. setup_log_buf(0);
  512. vfs_caches_init_early();
  513. sort_main_extable();
  514. trap_init();
  515. mm_init();
  516. ftrace_init();
  517. /* trace_printk can be enabled here */
  518. early_trace_init();
  519. /*
  520. * Set up the scheduler prior starting any interrupts (such as the
  521. * timer interrupt). Full topology setup happens at smp_init()
  522. * time - but meanwhile we still have a functioning scheduler.
  523. */
  524. sched_init();
  525. /*
  526. * Disable preemption - early bootup scheduling is extremely
  527. * fragile until we cpu_idle() for the first time.
  528. */
  529. preempt_disable();
  530. if (WARN(!irqs_disabled(),
  531. "Interrupts were enabled *very* early, fixing it\n"))
  532. local_irq_disable();
  533. radix_tree_init();
  534. /*
  535. * Set up housekeeping before setting up workqueues to allow the unbound
  536. * workqueue to take non-housekeeping into account.
  537. */
  538. housekeeping_init();
  539. /*
  540. * Allow workqueue creation and work item queueing/cancelling
  541. * early. Work item execution depends on kthreads and starts after
  542. * workqueue_init().
  543. */
  544. workqueue_init_early();
  545. rcu_init();
  546. /* Trace events are available after this */
  547. trace_init();
  548. if (initcall_debug)
  549. initcall_debug_enable();
  550. context_tracking_init();
  551. /* init some links before init_ISA_irqs() */
  552. early_irq_init();
  553. init_IRQ();
  554. tick_init();
  555. rcu_init_nohz();
  556. init_timers();
  557. hrtimers_init();
  558. softirq_init();
  559. timekeeping_init();
  560. time_init();
  561. printk_safe_init();
  562. perf_event_init();
  563. profile_init();
  564. call_function_init();
  565. WARN(!irqs_disabled(), "Interrupts were enabled early\n");
  566. early_boot_irqs_disabled = false;
  567. local_irq_enable();
  568. kmem_cache_init_late();
  569. /*
  570. * HACK ALERT! This is early. We're enabling the console before
  571. * we've done PCI setups etc, and console_init() must be aware of
  572. * this. But we do want output early, in case something goes wrong.
  573. */
  574. console_init();
  575. if (panic_later)
  576. panic("Too many boot %s vars at `%s'", panic_later,
  577. panic_param);
  578. lockdep_init();
  579. /*
  580. * Need to run this when irqs are enabled, because it wants
  581. * to self-test [hard/soft]-irqs on/off lock inversion bugs
  582. * too:
  583. */
  584. locking_selftest();
  585. /*
  586. * This needs to be called before any devices perform DMA
  587. * operations that might use the SWIOTLB bounce buffers. It will
  588. * mark the bounce buffers as decrypted so that their usage will
  589. * not cause "plain-text" data to be decrypted when accessed.
  590. */
  591. mem_encrypt_init();
  592. #ifdef CONFIG_BLK_DEV_INITRD
  593. if (initrd_start && !initrd_below_start_ok &&
  594. page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
  595. pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n",
  596. page_to_pfn(virt_to_page((void *)initrd_start)),
  597. min_low_pfn);
  598. initrd_start = 0;
  599. }
  600. #endif
  601. page_ext_init();
  602. kmemleak_init();
  603. debug_objects_mem_init();
  604. setup_per_cpu_pageset();
  605. numa_policy_init();
  606. acpi_early_init();
  607. if (late_time_init)
  608. late_time_init();
  609. sched_clock_init();
  610. calibrate_delay();
  611. pid_idr_init();
  612. anon_vma_init();
  613. #ifdef CONFIG_X86
  614. if (efi_enabled(EFI_RUNTIME_SERVICES))
  615. efi_enter_virtual_mode();
  616. #endif
  617. thread_stack_cache_init();
  618. cred_init();
  619. fork_init();
  620. proc_caches_init();
  621. uts_ns_init();
  622. buffer_init();
  623. key_init();
  624. security_init();
  625. dbg_late_init();
  626. vfs_caches_init();
  627. pagecache_init();
  628. signals_init();
  629. seq_file_init();
  630. proc_root_init();
  631. nsfs_init();
  632. cpuset_init();
  633. cgroup_init();
  634. taskstats_init_early();
  635. delayacct_init();
  636. check_bugs();
  637. acpi_subsystem_init();
  638. arch_post_acpi_subsys_init();
  639. sfi_init_late();
  640. if (efi_enabled(EFI_RUNTIME_SERVICES)) {
  641. efi_free_boot_services();
  642. }
  643. /* Do the rest non-__init'ed, we're now alive */
  644. rest_init();
  645. }
  646. /* Call all constructor functions linked into the kernel. */
  647. static void __init do_ctors(void)
  648. {
  649. #ifdef CONFIG_CONSTRUCTORS
  650. ctor_fn_t *fn = (ctor_fn_t *) __ctors_start;
  651. for (; fn < (ctor_fn_t *) __ctors_end; fn++)
  652. (*fn)();
  653. #endif
  654. }
  655. #ifdef CONFIG_KALLSYMS
  656. struct blacklist_entry {
  657. struct list_head next;
  658. char *buf;
  659. };
  660. static __initdata_or_module LIST_HEAD(blacklisted_initcalls);
  661. static int __init initcall_blacklist(char *str)
  662. {
  663. char *str_entry;
  664. struct blacklist_entry *entry;
  665. /* str argument is a comma-separated list of functions */
  666. do {
  667. str_entry = strsep(&str, ",");
  668. if (str_entry) {
  669. pr_debug("blacklisting initcall %s\n", str_entry);
  670. entry = alloc_bootmem(sizeof(*entry));
  671. entry->buf = alloc_bootmem(strlen(str_entry) + 1);
  672. strcpy(entry->buf, str_entry);
  673. list_add(&entry->next, &blacklisted_initcalls);
  674. }
  675. } while (str_entry);
  676. return 0;
  677. }
  678. static bool __init_or_module initcall_blacklisted(initcall_t fn)
  679. {
  680. struct blacklist_entry *entry;
  681. char fn_name[KSYM_SYMBOL_LEN];
  682. unsigned long addr;
  683. if (list_empty(&blacklisted_initcalls))
  684. return false;
  685. addr = (unsigned long) dereference_function_descriptor(fn);
  686. sprint_symbol_no_offset(fn_name, addr);
  687. /*
  688. * fn will be "function_name [module_name]" where [module_name] is not
  689. * displayed for built-in init functions. Strip off the [module_name].
  690. */
  691. strreplace(fn_name, ' ', '\0');
  692. list_for_each_entry(entry, &blacklisted_initcalls, next) {
  693. if (!strcmp(fn_name, entry->buf)) {
  694. pr_debug("initcall %s blacklisted\n", fn_name);
  695. return true;
  696. }
  697. }
  698. return false;
  699. }
  700. #else
  701. static int __init initcall_blacklist(char *str)
  702. {
  703. pr_warn("initcall_blacklist requires CONFIG_KALLSYMS\n");
  704. return 0;
  705. }
  706. static bool __init_or_module initcall_blacklisted(initcall_t fn)
  707. {
  708. return false;
  709. }
  710. #endif
  711. __setup("initcall_blacklist=", initcall_blacklist);
  712. static __init_or_module void
  713. trace_initcall_start_cb(void *data, initcall_t fn)
  714. {
  715. ktime_t *calltime = (ktime_t *)data;
  716. printk(KERN_DEBUG "calling %pF @ %i\n", fn, task_pid_nr(current));
  717. *calltime = ktime_get();
  718. }
  719. static __init_or_module void
  720. trace_initcall_finish_cb(void *data, initcall_t fn, int ret)
  721. {
  722. ktime_t *calltime = (ktime_t *)data;
  723. ktime_t delta, rettime;
  724. unsigned long long duration;
  725. rettime = ktime_get();
  726. delta = ktime_sub(rettime, *calltime);
  727. duration = (unsigned long long) ktime_to_ns(delta) >> 10;
  728. printk(KERN_DEBUG "initcall %pF returned %d after %lld usecs\n",
  729. fn, ret, duration);
  730. }
  731. static ktime_t initcall_calltime;
  732. #ifdef TRACEPOINTS_ENABLED
  733. static void __init initcall_debug_enable(void)
  734. {
  735. int ret;
  736. ret = register_trace_initcall_start(trace_initcall_start_cb,
  737. &initcall_calltime);
  738. ret |= register_trace_initcall_finish(trace_initcall_finish_cb,
  739. &initcall_calltime);
  740. WARN(ret, "Failed to register initcall tracepoints\n");
  741. }
  742. # define do_trace_initcall_start trace_initcall_start
  743. # define do_trace_initcall_finish trace_initcall_finish
  744. #else
  745. static inline void do_trace_initcall_start(initcall_t fn)
  746. {
  747. if (!initcall_debug)
  748. return;
  749. trace_initcall_start_cb(&initcall_calltime, fn);
  750. }
  751. static inline void do_trace_initcall_finish(initcall_t fn, int ret)
  752. {
  753. if (!initcall_debug)
  754. return;
  755. trace_initcall_finish_cb(&initcall_calltime, fn, ret);
  756. }
  757. #endif /* !TRACEPOINTS_ENABLED */
  758. int __init_or_module do_one_initcall(initcall_t fn)
  759. {
  760. int count = preempt_count();
  761. char msgbuf[64];
  762. int ret;
  763. if (initcall_blacklisted(fn))
  764. return -EPERM;
  765. do_trace_initcall_start(fn);
  766. ret = fn();
  767. do_trace_initcall_finish(fn, ret);
  768. msgbuf[0] = 0;
  769. if (preempt_count() != count) {
  770. sprintf(msgbuf, "preemption imbalance ");
  771. preempt_count_set(count);
  772. }
  773. if (irqs_disabled()) {
  774. strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
  775. local_irq_enable();
  776. }
  777. WARN(msgbuf[0], "initcall %pF returned with %s\n", fn, msgbuf);
  778. add_latent_entropy();
  779. return ret;
  780. }
  781. extern initcall_entry_t __initcall_start[];
  782. extern initcall_entry_t __initcall0_start[];
  783. extern initcall_entry_t __initcall1_start[];
  784. extern initcall_entry_t __initcall2_start[];
  785. extern initcall_entry_t __initcall3_start[];
  786. extern initcall_entry_t __initcall4_start[];
  787. extern initcall_entry_t __initcall5_start[];
  788. extern initcall_entry_t __initcall6_start[];
  789. extern initcall_entry_t __initcall7_start[];
  790. extern initcall_entry_t __initcall_end[];
  791. static initcall_entry_t *initcall_levels[] __initdata = {
  792. __initcall0_start,
  793. __initcall1_start,
  794. __initcall2_start,
  795. __initcall3_start,
  796. __initcall4_start,
  797. __initcall5_start,
  798. __initcall6_start,
  799. __initcall7_start,
  800. __initcall_end,
  801. };
  802. /* Keep these in sync with initcalls in include/linux/init.h */
  803. static char *initcall_level_names[] __initdata = {
  804. "pure",
  805. "core",
  806. "postcore",
  807. "arch",
  808. "subsys",
  809. "fs",
  810. "device",
  811. "late",
  812. };
  813. static void __init do_initcall_level(int level)
  814. {
  815. initcall_entry_t *fn;
  816. strcpy(initcall_command_line, saved_command_line);
  817. parse_args(initcall_level_names[level],
  818. initcall_command_line, __start___param,
  819. __stop___param - __start___param,
  820. level, level,
  821. NULL, &repair_env_string);
  822. trace_initcall_level(initcall_level_names[level]);
  823. for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++)
  824. do_one_initcall(initcall_from_entry(fn));
  825. }
  826. static void __init do_initcalls(void)
  827. {
  828. int level;
  829. for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++)
  830. do_initcall_level(level);
  831. }
  832. /*
  833. * Ok, the machine is now initialized. None of the devices
  834. * have been touched yet, but the CPU subsystem is up and
  835. * running, and memory and process management works.
  836. *
  837. * Now we can finally start doing some real work..
  838. */
  839. static void __init do_basic_setup(void)
  840. {
  841. cpuset_init_smp();
  842. shmem_init();
  843. driver_init();
  844. init_irq_proc();
  845. do_ctors();
  846. usermodehelper_enable();
  847. do_initcalls();
  848. }
  849. static void __init do_pre_smp_initcalls(void)
  850. {
  851. initcall_entry_t *fn;
  852. trace_initcall_level("early");
  853. for (fn = __initcall_start; fn < __initcall0_start; fn++)
  854. do_one_initcall(initcall_from_entry(fn));
  855. }
  856. /*
  857. * This function requests modules which should be loaded by default and is
  858. * called twice right after initrd is mounted and right before init is
  859. * exec'd. If such modules are on either initrd or rootfs, they will be
  860. * loaded before control is passed to userland.
  861. */
  862. void __init load_default_modules(void)
  863. {
  864. load_default_elevator_module();
  865. }
  866. static int run_init_process(const char *init_filename)
  867. {
  868. argv_init[0] = init_filename;
  869. pr_info("Run %s as init process\n", init_filename);
  870. return do_execve(getname_kernel(init_filename),
  871. (const char __user *const __user *)argv_init,
  872. (const char __user *const __user *)envp_init);
  873. }
  874. static int try_to_run_init_process(const char *init_filename)
  875. {
  876. int ret;
  877. ret = run_init_process(init_filename);
  878. if (ret && ret != -ENOENT) {
  879. pr_err("Starting init: %s exists but couldn't execute it (error %d)\n",
  880. init_filename, ret);
  881. }
  882. return ret;
  883. }
  884. static noinline void __init kernel_init_freeable(void);
  885. #if defined(CONFIG_STRICT_KERNEL_RWX) || defined(CONFIG_STRICT_MODULE_RWX)
  886. bool rodata_enabled __ro_after_init = true;
  887. static int __init set_debug_rodata(char *str)
  888. {
  889. return strtobool(str, &rodata_enabled);
  890. }
  891. __setup("rodata=", set_debug_rodata);
  892. #endif
  893. #ifdef CONFIG_STRICT_KERNEL_RWX
  894. static void mark_readonly(void)
  895. {
  896. if (rodata_enabled) {
  897. /*
  898. * load_module() results in W+X mappings, which are cleaned up
  899. * with call_rcu_sched(). Let's make sure that queued work is
  900. * flushed so that we don't hit false positives looking for
  901. * insecure pages which are W+X.
  902. */
  903. rcu_barrier_sched();
  904. mark_rodata_ro();
  905. rodata_test();
  906. } else
  907. pr_info("Kernel memory protection disabled.\n");
  908. }
  909. #else
  910. static inline void mark_readonly(void)
  911. {
  912. pr_warn("This architecture does not have kernel memory protection.\n");
  913. }
  914. #endif
  915. static int __ref kernel_init(void *unused)
  916. {
  917. int ret;
  918. kernel_init_freeable();
  919. /* need to finish all async __init code before freeing the memory */
  920. async_synchronize_full();
  921. ftrace_free_init_mem();
  922. jump_label_invalidate_initmem();
  923. free_initmem();
  924. mark_readonly();
  925. /*
  926. * Kernel mappings are now finalized - update the userspace page-table
  927. * to finalize PTI.
  928. */
  929. pti_finalize();
  930. system_state = SYSTEM_RUNNING;
  931. numa_default_policy();
  932. rcu_end_inkernel_boot();
  933. if (ramdisk_execute_command) {
  934. ret = run_init_process(ramdisk_execute_command);
  935. if (!ret)
  936. return 0;
  937. pr_err("Failed to execute %s (error %d)\n",
  938. ramdisk_execute_command, ret);
  939. }
  940. /*
  941. * We try each of these until one succeeds.
  942. *
  943. * The Bourne shell can be used instead of init if we are
  944. * trying to recover a really broken machine.
  945. */
  946. if (execute_command) {
  947. ret = run_init_process(execute_command);
  948. if (!ret)
  949. return 0;
  950. panic("Requested init %s failed (error %d).",
  951. execute_command, ret);
  952. }
  953. if (!try_to_run_init_process("/sbin/init") ||
  954. !try_to_run_init_process("/etc/init") ||
  955. !try_to_run_init_process("/bin/init") ||
  956. !try_to_run_init_process("/bin/sh"))
  957. return 0;
  958. panic("No working init found. Try passing init= option to kernel. "
  959. "See Linux Documentation/admin-guide/init.rst for guidance.");
  960. }
  961. static noinline void __init kernel_init_freeable(void)
  962. {
  963. /*
  964. * Wait until kthreadd is all set-up.
  965. */
  966. wait_for_completion(&kthreadd_done);
  967. /* Now the scheduler is fully set up and can do blocking allocations */
  968. gfp_allowed_mask = __GFP_BITS_MASK;
  969. /*
  970. * init can allocate pages on any node
  971. */
  972. set_mems_allowed(node_states[N_MEMORY]);
  973. cad_pid = task_pid(current);
  974. smp_prepare_cpus(setup_max_cpus);
  975. workqueue_init();
  976. init_mm_internals();
  977. do_pre_smp_initcalls();
  978. lockup_detector_init();
  979. smp_init();
  980. sched_init_smp();
  981. page_alloc_init_late();
  982. do_basic_setup();
  983. /* Open the /dev/console on the rootfs, this should never fail */
  984. if (ksys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
  985. pr_err("Warning: unable to open an initial console.\n");
  986. (void) ksys_dup(0);
  987. (void) ksys_dup(0);
  988. /*
  989. * check if there is an early userspace init. If yes, let it do all
  990. * the work
  991. */
  992. if (!ramdisk_execute_command)
  993. ramdisk_execute_command = "/init";
  994. if (ksys_access((const char __user *)
  995. ramdisk_execute_command, 0) != 0) {
  996. ramdisk_execute_command = NULL;
  997. prepare_namespace();
  998. }
  999. /*
  1000. * Ok, we have completed the initial bootup, and
  1001. * we're essentially up and running. Get rid of the
  1002. * initmem segments and start the user-mode stuff..
  1003. *
  1004. * rootfs is available now, try loading the public keys
  1005. * and default modules
  1006. */
  1007. integrity_load_keys();
  1008. load_default_modules();
  1009. }