main.c 29 KB

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