main.c 26 KB

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