main.c 28 KB

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