main.c 27 KB

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