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