main.c 29 KB

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