main.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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. schedule_preempt_disabled();
  382. /* Call into cpu_idle with preempt disabled */
  383. cpu_startup_entry(CPUHP_ONLINE);
  384. }
  385. /* Check for early params. */
  386. static int __init do_early_param(char *param, char *val,
  387. const char *unused, void *arg)
  388. {
  389. const struct obs_kernel_param *p;
  390. for (p = __setup_start; p < __setup_end; p++) {
  391. if ((p->early && parameq(param, p->str)) ||
  392. (strcmp(param, "console") == 0 &&
  393. strcmp(p->str, "earlycon") == 0)
  394. ) {
  395. if (p->setup_func(val) != 0)
  396. pr_warn("Malformed early option '%s'\n", param);
  397. }
  398. }
  399. /* We accept everything at this stage. */
  400. return 0;
  401. }
  402. void __init parse_early_options(char *cmdline)
  403. {
  404. parse_args("early options", cmdline, NULL, 0, 0, 0, NULL,
  405. do_early_param);
  406. }
  407. /* Arch code calls this early on, or if not, just before other parsing. */
  408. void __init parse_early_param(void)
  409. {
  410. static int done __initdata;
  411. static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata;
  412. if (done)
  413. return;
  414. /* All fall through to do_early_param. */
  415. strlcpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);
  416. parse_early_options(tmp_cmdline);
  417. done = 1;
  418. }
  419. void __init __weak arch_post_acpi_subsys_init(void) { }
  420. void __init __weak smp_setup_processor_id(void)
  421. {
  422. }
  423. # if THREAD_SIZE >= PAGE_SIZE
  424. void __init __weak thread_stack_cache_init(void)
  425. {
  426. }
  427. #endif
  428. void __init __weak mem_encrypt_init(void) { }
  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. pgtable_init();
  442. vmalloc_init();
  443. ioremap_huge_init();
  444. }
  445. asmlinkage __visible void __init start_kernel(void)
  446. {
  447. char *command_line;
  448. char *after_dashes;
  449. set_task_stack_end_magic(&init_task);
  450. smp_setup_processor_id();
  451. debug_objects_early_init();
  452. cgroup_init_early();
  453. local_irq_disable();
  454. early_boot_irqs_disabled = true;
  455. /*
  456. * Interrupts are still disabled. Do necessary setups, then
  457. * enable them.
  458. */
  459. boot_cpu_init();
  460. page_address_init();
  461. pr_notice("%s", linux_banner);
  462. setup_arch(&command_line);
  463. /*
  464. * Set up the the initial canary and entropy after arch
  465. * and after adding latent and command line entropy.
  466. */
  467. add_latent_entropy();
  468. add_device_randomness(command_line, strlen(command_line));
  469. boot_init_stack_canary();
  470. mm_init_cpumask(&init_mm);
  471. setup_command_line(command_line);
  472. setup_nr_cpu_ids();
  473. setup_per_cpu_areas();
  474. boot_cpu_state_init();
  475. smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
  476. build_all_zonelists(NULL);
  477. page_alloc_init();
  478. pr_notice("Kernel command line: %s\n", boot_command_line);
  479. parse_early_param();
  480. after_dashes = parse_args("Booting kernel",
  481. static_command_line, __start___param,
  482. __stop___param - __start___param,
  483. -1, -1, NULL, &unknown_bootoption);
  484. if (!IS_ERR_OR_NULL(after_dashes))
  485. parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
  486. NULL, set_init_arg);
  487. jump_label_init();
  488. /*
  489. * These use large bootmem allocations and must precede
  490. * kmem_cache_init()
  491. */
  492. setup_log_buf(0);
  493. pidhash_init();
  494. vfs_caches_init_early();
  495. sort_main_extable();
  496. trap_init();
  497. mm_init();
  498. ftrace_init();
  499. /* trace_printk can be enabled here */
  500. early_trace_init();
  501. /*
  502. * Set up the scheduler prior starting any interrupts (such as the
  503. * timer interrupt). Full topology setup happens at smp_init()
  504. * time - but meanwhile we still have a functioning scheduler.
  505. */
  506. sched_init();
  507. /*
  508. * Disable preemption - early bootup scheduling is extremely
  509. * fragile until we cpu_idle() for the first time.
  510. */
  511. preempt_disable();
  512. if (WARN(!irqs_disabled(),
  513. "Interrupts were enabled *very* early, fixing it\n"))
  514. local_irq_disable();
  515. radix_tree_init();
  516. /*
  517. * Allow workqueue creation and work item queueing/cancelling
  518. * early. Work item execution depends on kthreads and starts after
  519. * workqueue_init().
  520. */
  521. workqueue_init_early();
  522. rcu_init();
  523. /* Trace events are available after this */
  524. trace_init();
  525. context_tracking_init();
  526. /* init some links before init_ISA_irqs() */
  527. early_irq_init();
  528. init_IRQ();
  529. tick_init();
  530. rcu_init_nohz();
  531. init_timers();
  532. hrtimers_init();
  533. softirq_init();
  534. timekeeping_init();
  535. time_init();
  536. sched_clock_postinit();
  537. printk_safe_init();
  538. perf_event_init();
  539. profile_init();
  540. call_function_init();
  541. WARN(!irqs_disabled(), "Interrupts were enabled early\n");
  542. early_boot_irqs_disabled = false;
  543. local_irq_enable();
  544. kmem_cache_init_late();
  545. /*
  546. * HACK ALERT! This is early. We're enabling the console before
  547. * we've done PCI setups etc, and console_init() must be aware of
  548. * this. But we do want output early, in case something goes wrong.
  549. */
  550. console_init();
  551. if (panic_later)
  552. panic("Too many boot %s vars at `%s'", panic_later,
  553. panic_param);
  554. lockdep_info();
  555. /*
  556. * Need to run this when irqs are enabled, because it wants
  557. * to self-test [hard/soft]-irqs on/off lock inversion bugs
  558. * too:
  559. */
  560. locking_selftest();
  561. /*
  562. * This needs to be called before any devices perform DMA
  563. * operations that might use the SWIOTLB bounce buffers. It will
  564. * mark the bounce buffers as decrypted so that their usage will
  565. * not cause "plain-text" data to be decrypted when accessed.
  566. */
  567. mem_encrypt_init();
  568. #ifdef CONFIG_BLK_DEV_INITRD
  569. if (initrd_start && !initrd_below_start_ok &&
  570. page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
  571. pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n",
  572. page_to_pfn(virt_to_page((void *)initrd_start)),
  573. min_low_pfn);
  574. initrd_start = 0;
  575. }
  576. #endif
  577. page_ext_init();
  578. kmemleak_init();
  579. debug_objects_mem_init();
  580. setup_per_cpu_pageset();
  581. numa_policy_init();
  582. if (late_time_init)
  583. late_time_init();
  584. calibrate_delay();
  585. pidmap_init();
  586. anon_vma_init();
  587. acpi_early_init();
  588. #ifdef CONFIG_X86
  589. if (efi_enabled(EFI_RUNTIME_SERVICES))
  590. efi_enter_virtual_mode();
  591. #endif
  592. #ifdef CONFIG_X86_ESPFIX64
  593. /* Should be run before the first non-init thread is created */
  594. init_espfix_bsp();
  595. #endif
  596. thread_stack_cache_init();
  597. cred_init();
  598. fork_init();
  599. proc_caches_init();
  600. buffer_init();
  601. key_init();
  602. security_init();
  603. dbg_late_init();
  604. vfs_caches_init();
  605. pagecache_init();
  606. signals_init();
  607. proc_root_init();
  608. nsfs_init();
  609. cpuset_init();
  610. cgroup_init();
  611. taskstats_init_early();
  612. delayacct_init();
  613. check_bugs();
  614. acpi_subsystem_init();
  615. arch_post_acpi_subsys_init();
  616. sfi_init_late();
  617. if (efi_enabled(EFI_RUNTIME_SERVICES)) {
  618. efi_free_boot_services();
  619. }
  620. /* Do the rest non-__init'ed, we're now alive */
  621. rest_init();
  622. }
  623. /* Call all constructor functions linked into the kernel. */
  624. static void __init do_ctors(void)
  625. {
  626. #ifdef CONFIG_CONSTRUCTORS
  627. ctor_fn_t *fn = (ctor_fn_t *) __ctors_start;
  628. for (; fn < (ctor_fn_t *) __ctors_end; fn++)
  629. (*fn)();
  630. #endif
  631. }
  632. bool initcall_debug;
  633. core_param(initcall_debug, initcall_debug, bool, 0644);
  634. #ifdef CONFIG_KALLSYMS
  635. struct blacklist_entry {
  636. struct list_head next;
  637. char *buf;
  638. };
  639. static __initdata_or_module LIST_HEAD(blacklisted_initcalls);
  640. static int __init initcall_blacklist(char *str)
  641. {
  642. char *str_entry;
  643. struct blacklist_entry *entry;
  644. /* str argument is a comma-separated list of functions */
  645. do {
  646. str_entry = strsep(&str, ",");
  647. if (str_entry) {
  648. pr_debug("blacklisting initcall %s\n", str_entry);
  649. entry = alloc_bootmem(sizeof(*entry));
  650. entry->buf = alloc_bootmem(strlen(str_entry) + 1);
  651. strcpy(entry->buf, str_entry);
  652. list_add(&entry->next, &blacklisted_initcalls);
  653. }
  654. } while (str_entry);
  655. return 0;
  656. }
  657. static bool __init_or_module initcall_blacklisted(initcall_t fn)
  658. {
  659. struct blacklist_entry *entry;
  660. char fn_name[KSYM_SYMBOL_LEN];
  661. unsigned long addr;
  662. if (list_empty(&blacklisted_initcalls))
  663. return false;
  664. addr = (unsigned long) dereference_function_descriptor(fn);
  665. sprint_symbol_no_offset(fn_name, addr);
  666. /*
  667. * fn will be "function_name [module_name]" where [module_name] is not
  668. * displayed for built-in init functions. Strip off the [module_name].
  669. */
  670. strreplace(fn_name, ' ', '\0');
  671. list_for_each_entry(entry, &blacklisted_initcalls, next) {
  672. if (!strcmp(fn_name, entry->buf)) {
  673. pr_debug("initcall %s blacklisted\n", fn_name);
  674. return true;
  675. }
  676. }
  677. return false;
  678. }
  679. #else
  680. static int __init initcall_blacklist(char *str)
  681. {
  682. pr_warn("initcall_blacklist requires CONFIG_KALLSYMS\n");
  683. return 0;
  684. }
  685. static bool __init_or_module initcall_blacklisted(initcall_t fn)
  686. {
  687. return false;
  688. }
  689. #endif
  690. __setup("initcall_blacklist=", initcall_blacklist);
  691. static int __init_or_module do_one_initcall_debug(initcall_t fn)
  692. {
  693. ktime_t calltime, delta, rettime;
  694. unsigned long long duration;
  695. int ret;
  696. printk(KERN_DEBUG "calling %pF @ %i\n", fn, task_pid_nr(current));
  697. calltime = ktime_get();
  698. ret = fn();
  699. rettime = ktime_get();
  700. delta = ktime_sub(rettime, calltime);
  701. duration = (unsigned long long) ktime_to_ns(delta) >> 10;
  702. printk(KERN_DEBUG "initcall %pF returned %d after %lld usecs\n",
  703. fn, ret, duration);
  704. return ret;
  705. }
  706. int __init_or_module do_one_initcall(initcall_t fn)
  707. {
  708. int count = preempt_count();
  709. int ret;
  710. char msgbuf[64];
  711. if (initcall_blacklisted(fn))
  712. return -EPERM;
  713. if (initcall_debug)
  714. ret = do_one_initcall_debug(fn);
  715. else
  716. ret = fn();
  717. msgbuf[0] = 0;
  718. if (preempt_count() != count) {
  719. sprintf(msgbuf, "preemption imbalance ");
  720. preempt_count_set(count);
  721. }
  722. if (irqs_disabled()) {
  723. strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
  724. local_irq_enable();
  725. }
  726. WARN(msgbuf[0], "initcall %pF returned with %s\n", fn, msgbuf);
  727. add_latent_entropy();
  728. return ret;
  729. }
  730. extern initcall_t __initcall_start[];
  731. extern initcall_t __initcall0_start[];
  732. extern initcall_t __initcall1_start[];
  733. extern initcall_t __initcall2_start[];
  734. extern initcall_t __initcall3_start[];
  735. extern initcall_t __initcall4_start[];
  736. extern initcall_t __initcall5_start[];
  737. extern initcall_t __initcall6_start[];
  738. extern initcall_t __initcall7_start[];
  739. extern initcall_t __initcall_end[];
  740. static initcall_t *initcall_levels[] __initdata = {
  741. __initcall0_start,
  742. __initcall1_start,
  743. __initcall2_start,
  744. __initcall3_start,
  745. __initcall4_start,
  746. __initcall5_start,
  747. __initcall6_start,
  748. __initcall7_start,
  749. __initcall_end,
  750. };
  751. /* Keep these in sync with initcalls in include/linux/init.h */
  752. static char *initcall_level_names[] __initdata = {
  753. "early",
  754. "core",
  755. "postcore",
  756. "arch",
  757. "subsys",
  758. "fs",
  759. "device",
  760. "late",
  761. };
  762. static void __init do_initcall_level(int level)
  763. {
  764. initcall_t *fn;
  765. strcpy(initcall_command_line, saved_command_line);
  766. parse_args(initcall_level_names[level],
  767. initcall_command_line, __start___param,
  768. __stop___param - __start___param,
  769. level, level,
  770. NULL, &repair_env_string);
  771. for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++)
  772. do_one_initcall(*fn);
  773. }
  774. static void __init do_initcalls(void)
  775. {
  776. int level;
  777. for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++)
  778. do_initcall_level(level);
  779. }
  780. /*
  781. * Ok, the machine is now initialized. None of the devices
  782. * have been touched yet, but the CPU subsystem is up and
  783. * running, and memory and process management works.
  784. *
  785. * Now we can finally start doing some real work..
  786. */
  787. static void __init do_basic_setup(void)
  788. {
  789. cpuset_init_smp();
  790. shmem_init();
  791. driver_init();
  792. init_irq_proc();
  793. do_ctors();
  794. usermodehelper_enable();
  795. do_initcalls();
  796. }
  797. static void __init do_pre_smp_initcalls(void)
  798. {
  799. initcall_t *fn;
  800. for (fn = __initcall_start; fn < __initcall0_start; fn++)
  801. do_one_initcall(*fn);
  802. }
  803. /*
  804. * This function requests modules which should be loaded by default and is
  805. * called twice right after initrd is mounted and right before init is
  806. * exec'd. If such modules are on either initrd or rootfs, they will be
  807. * loaded before control is passed to userland.
  808. */
  809. void __init load_default_modules(void)
  810. {
  811. load_default_elevator_module();
  812. }
  813. static int run_init_process(const char *init_filename)
  814. {
  815. argv_init[0] = init_filename;
  816. return do_execve(getname_kernel(init_filename),
  817. (const char __user *const __user *)argv_init,
  818. (const char __user *const __user *)envp_init);
  819. }
  820. static int try_to_run_init_process(const char *init_filename)
  821. {
  822. int ret;
  823. ret = run_init_process(init_filename);
  824. if (ret && ret != -ENOENT) {
  825. pr_err("Starting init: %s exists but couldn't execute it (error %d)\n",
  826. init_filename, ret);
  827. }
  828. return ret;
  829. }
  830. static noinline void __init kernel_init_freeable(void);
  831. #if defined(CONFIG_STRICT_KERNEL_RWX) || defined(CONFIG_STRICT_MODULE_RWX)
  832. bool rodata_enabled __ro_after_init = true;
  833. static int __init set_debug_rodata(char *str)
  834. {
  835. return strtobool(str, &rodata_enabled);
  836. }
  837. __setup("rodata=", set_debug_rodata);
  838. #endif
  839. #ifdef CONFIG_STRICT_KERNEL_RWX
  840. static void mark_readonly(void)
  841. {
  842. if (rodata_enabled) {
  843. mark_rodata_ro();
  844. rodata_test();
  845. } else
  846. pr_info("Kernel memory protection disabled.\n");
  847. }
  848. #else
  849. static inline void mark_readonly(void)
  850. {
  851. pr_warn("This architecture does not have kernel memory protection.\n");
  852. }
  853. #endif
  854. static int __ref kernel_init(void *unused)
  855. {
  856. int ret;
  857. kernel_init_freeable();
  858. /* need to finish all async __init code before freeing the memory */
  859. async_synchronize_full();
  860. ftrace_free_init_mem();
  861. free_initmem();
  862. mark_readonly();
  863. system_state = SYSTEM_RUNNING;
  864. numa_default_policy();
  865. rcu_end_inkernel_boot();
  866. if (ramdisk_execute_command) {
  867. ret = run_init_process(ramdisk_execute_command);
  868. if (!ret)
  869. return 0;
  870. pr_err("Failed to execute %s (error %d)\n",
  871. ramdisk_execute_command, ret);
  872. }
  873. /*
  874. * We try each of these until one succeeds.
  875. *
  876. * The Bourne shell can be used instead of init if we are
  877. * trying to recover a really broken machine.
  878. */
  879. if (execute_command) {
  880. ret = run_init_process(execute_command);
  881. if (!ret)
  882. return 0;
  883. panic("Requested init %s failed (error %d).",
  884. execute_command, ret);
  885. }
  886. if (!try_to_run_init_process("/sbin/init") ||
  887. !try_to_run_init_process("/etc/init") ||
  888. !try_to_run_init_process("/bin/init") ||
  889. !try_to_run_init_process("/bin/sh"))
  890. return 0;
  891. panic("No working init found. Try passing init= option to kernel. "
  892. "See Linux Documentation/admin-guide/init.rst for guidance.");
  893. }
  894. static noinline void __init kernel_init_freeable(void)
  895. {
  896. /*
  897. * Wait until kthreadd is all set-up.
  898. */
  899. wait_for_completion(&kthreadd_done);
  900. /* Now the scheduler is fully set up and can do blocking allocations */
  901. gfp_allowed_mask = __GFP_BITS_MASK;
  902. /*
  903. * init can allocate pages on any node
  904. */
  905. set_mems_allowed(node_states[N_MEMORY]);
  906. cad_pid = task_pid(current);
  907. smp_prepare_cpus(setup_max_cpus);
  908. workqueue_init();
  909. init_mm_internals();
  910. do_pre_smp_initcalls();
  911. lockup_detector_init();
  912. smp_init();
  913. sched_init_smp();
  914. page_alloc_init_late();
  915. do_basic_setup();
  916. /* Open the /dev/console on the rootfs, this should never fail */
  917. if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
  918. pr_err("Warning: unable to open an initial console.\n");
  919. (void) sys_dup(0);
  920. (void) sys_dup(0);
  921. /*
  922. * check if there is an early userspace init. If yes, let it do all
  923. * the work
  924. */
  925. if (!ramdisk_execute_command)
  926. ramdisk_execute_command = "/init";
  927. if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0) {
  928. ramdisk_execute_command = NULL;
  929. prepare_namespace();
  930. }
  931. /*
  932. * Ok, we have completed the initial bootup, and
  933. * we're essentially up and running. Get rid of the
  934. * initmem segments and start the user-mode stuff..
  935. *
  936. * rootfs is available now, try loading the public keys
  937. * and default modules
  938. */
  939. integrity_load_keys();
  940. load_default_modules();
  941. }