main.c 25 KB

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