main.c 25 KB

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