main.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/init/main.c
  4. *
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. *
  7. * GK 2/5/95 - Changed to support mounting root fs via NFS
  8. * Added initrd & change_root: Werner Almesberger & Hans Lermen, Feb '96
  9. * Moan early if gcc is old, avoiding bogus kernels - Paul Gortmaker, May '96
  10. * Simplified starting of init: Michael A. Griffith <grif@acm.org>
  11. */
  12. #define DEBUG /* Enable initcall_debug */
  13. #include <linux/types.h>
  14. #include <linux/extable.h>
  15. #include <linux/module.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/binfmts.h>
  18. #include <linux/kernel.h>
  19. #include <linux/syscalls.h>
  20. #include <linux/stackprotector.h>
  21. #include <linux/string.h>
  22. #include <linux/ctype.h>
  23. #include <linux/delay.h>
  24. #include <linux/ioport.h>
  25. #include <linux/init.h>
  26. #include <linux/initrd.h>
  27. #include <linux/memblock.h>
  28. #include <linux/acpi.h>
  29. #include <linux/bootconfig.h>
  30. #include <linux/console.h>
  31. #include <linux/nmi.h>
  32. #include <linux/percpu.h>
  33. #include <linux/kmod.h>
  34. #include <linux/kprobes.h>
  35. #include <linux/kmsan.h>
  36. #include <linux/vmalloc.h>
  37. #include <linux/kernel_stat.h>
  38. #include <linux/start_kernel.h>
  39. #include <linux/security.h>
  40. #include <linux/smp.h>
  41. #include <linux/profile.h>
  42. #include <linux/kfence.h>
  43. #include <linux/rcupdate.h>
  44. #include <linux/srcu.h>
  45. #include <linux/moduleparam.h>
  46. #include <linux/kallsyms.h>
  47. #include <linux/buildid.h>
  48. #include <linux/writeback.h>
  49. #include <linux/cpu.h>
  50. #include <linux/cpuset.h>
  51. #include <linux/cgroup.h>
  52. #include <linux/efi.h>
  53. #include <linux/tick.h>
  54. #include <linux/sched/isolation.h>
  55. #include <linux/interrupt.h>
  56. #include <linux/taskstats_kern.h>
  57. #include <linux/delayacct.h>
  58. #include <linux/unistd.h>
  59. #include <linux/utsname.h>
  60. #include <linux/rmap.h>
  61. #include <linux/mempolicy.h>
  62. #include <linux/key.h>
  63. #include <linux/debug_locks.h>
  64. #include <linux/debugobjects.h>
  65. #include <linux/lockdep.h>
  66. #include <linux/kmemleak.h>
  67. #include <linux/padata.h>
  68. #include <linux/pid_namespace.h>
  69. #include <linux/device/driver.h>
  70. #include <linux/kthread.h>
  71. #include <linux/sched.h>
  72. #include <linux/sched/init.h>
  73. #include <linux/signal.h>
  74. #include <linux/idr.h>
  75. #include <linux/kgdb.h>
  76. #include <linux/ftrace.h>
  77. #include <linux/async.h>
  78. #include <linux/shmem_fs.h>
  79. #include <linux/slab.h>
  80. #include <linux/perf_event.h>
  81. #include <linux/ptrace.h>
  82. #include <linux/pti.h>
  83. #include <linux/blkdev.h>
  84. #include <linux/sched/clock.h>
  85. #include <linux/sched/task.h>
  86. #include <linux/sched/task_stack.h>
  87. #include <linux/context_tracking.h>
  88. #include <linux/random.h>
  89. #include <linux/moduleloader.h>
  90. #include <linux/list.h>
  91. #include <linux/integrity.h>
  92. #include <linux/proc_ns.h>
  93. #include <linux/io.h>
  94. #include <linux/cache.h>
  95. #include <linux/rodata_test.h>
  96. #include <linux/jump_label.h>
  97. #include <linux/kcsan.h>
  98. #include <linux/init_syscalls.h>
  99. #include <linux/stackdepot.h>
  100. #include <linux/randomize_kstack.h>
  101. #include <linux/pidfs.h>
  102. #include <linux/ptdump.h>
  103. #include <net/net_namespace.h>
  104. #include <asm/io.h>
  105. #include <asm/setup.h>
  106. #include <asm/sections.h>
  107. #include <asm/cacheflush.h>
  108. #define CREATE_TRACE_POINTS
  109. #include <trace/events/initcall.h>
  110. #include <kunit/test.h>
  111. static int kernel_init(void *);
  112. /*
  113. * Debug helper: via this flag we know that we are in 'early bootup code'
  114. * where only the boot processor is running with IRQ disabled. This means
  115. * two things - IRQ must not be enabled before the flag is cleared and some
  116. * operations which are not allowed with IRQ disabled are allowed while the
  117. * flag is set.
  118. */
  119. bool early_boot_irqs_disabled __read_mostly;
  120. enum system_states system_state __read_mostly;
  121. EXPORT_SYMBOL(system_state);
  122. /*
  123. * Boot command-line arguments
  124. */
  125. #define MAX_INIT_ARGS CONFIG_INIT_ENV_ARG_LIMIT
  126. #define MAX_INIT_ENVS CONFIG_INIT_ENV_ARG_LIMIT
  127. /* Default late time init is NULL. archs can override this later. */
  128. void (*__initdata late_time_init)(void);
  129. /* Untouched command line saved by arch-specific code. */
  130. char __initdata boot_command_line[COMMAND_LINE_SIZE];
  131. /* Untouched saved command line (eg. for /proc) */
  132. char *saved_command_line __ro_after_init;
  133. unsigned int saved_command_line_len __ro_after_init;
  134. /* Command line for parameter parsing */
  135. static char *static_command_line;
  136. /* Untouched extra command line */
  137. static char *extra_command_line;
  138. /* Extra init arguments */
  139. static char *extra_init_args;
  140. #ifdef CONFIG_BOOT_CONFIG
  141. /* Is bootconfig on command line? */
  142. static bool bootconfig_found;
  143. static size_t initargs_offs;
  144. #else
  145. # define bootconfig_found false
  146. # define initargs_offs 0
  147. #endif
  148. static char *execute_command;
  149. static char *ramdisk_execute_command = "/init";
  150. /*
  151. * Used to generate warnings if static_key manipulation functions are used
  152. * before jump_label_init is called.
  153. */
  154. bool static_key_initialized __read_mostly;
  155. EXPORT_SYMBOL_GPL(static_key_initialized);
  156. /*
  157. * If set, this is an indication to the drivers that reset the underlying
  158. * device before going ahead with the initialization otherwise driver might
  159. * rely on the BIOS and skip the reset operation.
  160. *
  161. * This is useful if kernel is booting in an unreliable environment.
  162. * For ex. kdump situation where previous kernel has crashed, BIOS has been
  163. * skipped and devices will be in unknown state.
  164. */
  165. unsigned int reset_devices;
  166. EXPORT_SYMBOL(reset_devices);
  167. static int __init set_reset_devices(char *str)
  168. {
  169. reset_devices = 1;
  170. return 1;
  171. }
  172. __setup("reset_devices", set_reset_devices);
  173. static const char *argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
  174. const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
  175. static const char *panic_later, *panic_param;
  176. static bool __init obsolete_checksetup(char *line)
  177. {
  178. const struct obs_kernel_param *p;
  179. bool had_early_param = false;
  180. p = __setup_start;
  181. do {
  182. int n = strlen(p->str);
  183. if (parameqn(line, p->str, n)) {
  184. if (p->early) {
  185. /* Already done in parse_early_param?
  186. * (Needs exact match on param part).
  187. * Keep iterating, as we can have early
  188. * params and __setups of same names 8( */
  189. if (line[n] == '\0' || line[n] == '=')
  190. had_early_param = true;
  191. } else if (!p->setup_func) {
  192. pr_warn("Parameter %s is obsolete, ignored\n",
  193. p->str);
  194. return true;
  195. } else if (p->setup_func(line + n))
  196. return true;
  197. }
  198. p++;
  199. } while (p < __setup_end);
  200. return had_early_param;
  201. }
  202. /*
  203. * This should be approx 2 Bo*oMips to start (note initial shift), and will
  204. * still work even if initially too large, it will just take slightly longer
  205. */
  206. unsigned long loops_per_jiffy = (1<<12);
  207. EXPORT_SYMBOL(loops_per_jiffy);
  208. static int __init debug_kernel(char *str)
  209. {
  210. console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
  211. return 0;
  212. }
  213. static int __init quiet_kernel(char *str)
  214. {
  215. console_loglevel = CONSOLE_LOGLEVEL_QUIET;
  216. return 0;
  217. }
  218. early_param("debug", debug_kernel);
  219. early_param("quiet", quiet_kernel);
  220. static int __init loglevel(char *str)
  221. {
  222. int newlevel;
  223. /*
  224. * Only update loglevel value when a correct setting was passed,
  225. * to prevent blind crashes (when loglevel being set to 0) that
  226. * are quite hard to debug
  227. */
  228. if (get_option(&str, &newlevel)) {
  229. console_loglevel = newlevel;
  230. return 0;
  231. }
  232. return -EINVAL;
  233. }
  234. early_param("loglevel", loglevel);
  235. #ifdef CONFIG_BLK_DEV_INITRD
  236. static void * __init get_boot_config_from_initrd(size_t *_size)
  237. {
  238. u32 size, csum;
  239. char *data;
  240. u32 *hdr;
  241. int i;
  242. if (!initrd_end)
  243. return NULL;
  244. data = (char *)initrd_end - BOOTCONFIG_MAGIC_LEN;
  245. /*
  246. * Since Grub may align the size of initrd to 4, we must
  247. * check the preceding 3 bytes as well.
  248. */
  249. for (i = 0; i < 4; i++) {
  250. if (!memcmp(data, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN))
  251. goto found;
  252. data--;
  253. }
  254. return NULL;
  255. found:
  256. hdr = (u32 *)(data - 8);
  257. size = le32_to_cpu(hdr[0]);
  258. csum = le32_to_cpu(hdr[1]);
  259. data = ((void *)hdr) - size;
  260. if ((unsigned long)data < initrd_start) {
  261. pr_err("bootconfig size %d is greater than initrd size %ld\n",
  262. size, initrd_end - initrd_start);
  263. return NULL;
  264. }
  265. if (xbc_calc_checksum(data, size) != csum) {
  266. pr_err("bootconfig checksum failed\n");
  267. return NULL;
  268. }
  269. /* Remove bootconfig from initramfs/initrd */
  270. initrd_end = (unsigned long)data;
  271. if (_size)
  272. *_size = size;
  273. return data;
  274. }
  275. #else
  276. static void * __init get_boot_config_from_initrd(size_t *_size)
  277. {
  278. return NULL;
  279. }
  280. #endif
  281. #ifdef CONFIG_BOOT_CONFIG
  282. static char xbc_namebuf[XBC_KEYLEN_MAX] __initdata;
  283. #define rest(dst, end) ((end) > (dst) ? (end) - (dst) : 0)
  284. static int __init xbc_snprint_cmdline(char *buf, size_t size,
  285. struct xbc_node *root)
  286. {
  287. struct xbc_node *knode, *vnode;
  288. char *end = buf + size;
  289. const char *val, *q;
  290. int ret;
  291. xbc_node_for_each_key_value(root, knode, val) {
  292. ret = xbc_node_compose_key_after(root, knode,
  293. xbc_namebuf, XBC_KEYLEN_MAX);
  294. if (ret < 0)
  295. return ret;
  296. vnode = xbc_node_get_child(knode);
  297. if (!vnode) {
  298. ret = snprintf(buf, rest(buf, end), "%s ", xbc_namebuf);
  299. if (ret < 0)
  300. return ret;
  301. buf += ret;
  302. continue;
  303. }
  304. xbc_array_for_each_value(vnode, val) {
  305. /*
  306. * For prettier and more readable /proc/cmdline, only
  307. * quote the value when necessary, i.e. when it contains
  308. * whitespace.
  309. */
  310. q = strpbrk(val, " \t\r\n") ? "\"" : "";
  311. ret = snprintf(buf, rest(buf, end), "%s=%s%s%s ",
  312. xbc_namebuf, q, val, q);
  313. if (ret < 0)
  314. return ret;
  315. buf += ret;
  316. }
  317. }
  318. return buf - (end - size);
  319. }
  320. #undef rest
  321. /* Make an extra command line under given key word */
  322. static char * __init xbc_make_cmdline(const char *key)
  323. {
  324. struct xbc_node *root;
  325. char *new_cmdline;
  326. int ret, len = 0;
  327. root = xbc_find_node(key);
  328. if (!root)
  329. return NULL;
  330. /* Count required buffer size */
  331. len = xbc_snprint_cmdline(NULL, 0, root);
  332. if (len <= 0)
  333. return NULL;
  334. new_cmdline = memblock_alloc(len + 1, SMP_CACHE_BYTES);
  335. if (!new_cmdline) {
  336. pr_err("Failed to allocate memory for extra kernel cmdline.\n");
  337. return NULL;
  338. }
  339. ret = xbc_snprint_cmdline(new_cmdline, len + 1, root);
  340. if (ret < 0 || ret > len) {
  341. pr_err("Failed to print extra kernel cmdline.\n");
  342. memblock_free(new_cmdline, len + 1);
  343. return NULL;
  344. }
  345. return new_cmdline;
  346. }
  347. static int __init bootconfig_params(char *param, char *val,
  348. const char *unused, void *arg)
  349. {
  350. if (strcmp(param, "bootconfig") == 0) {
  351. bootconfig_found = true;
  352. }
  353. return 0;
  354. }
  355. static int __init warn_bootconfig(char *str)
  356. {
  357. /* The 'bootconfig' has been handled by bootconfig_params(). */
  358. return 0;
  359. }
  360. static void __init setup_boot_config(void)
  361. {
  362. static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata;
  363. const char *msg, *data;
  364. int pos, ret;
  365. size_t size;
  366. char *err;
  367. /* Cut out the bootconfig data even if we have no bootconfig option */
  368. data = get_boot_config_from_initrd(&size);
  369. /* If there is no bootconfig in initrd, try embedded one. */
  370. if (!data)
  371. data = xbc_get_embedded_bootconfig(&size);
  372. strscpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);
  373. err = parse_args("bootconfig", tmp_cmdline, NULL, 0, 0, 0, NULL,
  374. bootconfig_params);
  375. if (IS_ERR(err) || !(bootconfig_found || IS_ENABLED(CONFIG_BOOT_CONFIG_FORCE)))
  376. return;
  377. /* parse_args() stops at the next param of '--' and returns an address */
  378. if (err)
  379. initargs_offs = err - tmp_cmdline;
  380. if (!data) {
  381. /* If user intended to use bootconfig, show an error level message */
  382. if (bootconfig_found)
  383. pr_err("'bootconfig' found on command line, but no bootconfig found\n");
  384. else
  385. pr_info("No bootconfig data provided, so skipping bootconfig");
  386. return;
  387. }
  388. if (size >= XBC_DATA_MAX) {
  389. pr_err("bootconfig size %ld greater than max size %d\n",
  390. (long)size, XBC_DATA_MAX);
  391. return;
  392. }
  393. ret = xbc_init(data, size, &msg, &pos);
  394. if (ret < 0) {
  395. if (pos < 0)
  396. pr_err("Failed to init bootconfig: %s.\n", msg);
  397. else
  398. pr_err("Failed to parse bootconfig: %s at %d.\n",
  399. msg, pos);
  400. } else {
  401. xbc_get_info(&ret, NULL);
  402. pr_info("Load bootconfig: %ld bytes %d nodes\n", (long)size, ret);
  403. /* keys starting with "kernel." are passed via cmdline */
  404. extra_command_line = xbc_make_cmdline("kernel");
  405. /* Also, "init." keys are init arguments */
  406. extra_init_args = xbc_make_cmdline("init");
  407. }
  408. return;
  409. }
  410. static void __init exit_boot_config(void)
  411. {
  412. xbc_exit();
  413. }
  414. #else /* !CONFIG_BOOT_CONFIG */
  415. static void __init setup_boot_config(void)
  416. {
  417. /* Remove bootconfig data from initrd */
  418. get_boot_config_from_initrd(NULL);
  419. }
  420. static int __init warn_bootconfig(char *str)
  421. {
  422. pr_warn("WARNING: 'bootconfig' found on the kernel command line but CONFIG_BOOT_CONFIG is not set.\n");
  423. return 0;
  424. }
  425. #define exit_boot_config() do {} while (0)
  426. #endif /* CONFIG_BOOT_CONFIG */
  427. early_param("bootconfig", warn_bootconfig);
  428. bool __init cmdline_has_extra_options(void)
  429. {
  430. return extra_command_line || extra_init_args;
  431. }
  432. /* Change NUL term back to "=", to make "param" the whole string. */
  433. static void __init repair_env_string(char *param, char *val)
  434. {
  435. if (val) {
  436. /* param=val or param="val"? */
  437. if (val == param+strlen(param)+1)
  438. val[-1] = '=';
  439. else if (val == param+strlen(param)+2) {
  440. val[-2] = '=';
  441. memmove(val-1, val, strlen(val)+1);
  442. } else
  443. BUG();
  444. }
  445. }
  446. /* Anything after -- gets handed straight to init. */
  447. static int __init set_init_arg(char *param, char *val,
  448. const char *unused, void *arg)
  449. {
  450. unsigned int i;
  451. if (panic_later)
  452. return 0;
  453. repair_env_string(param, val);
  454. for (i = 0; argv_init[i]; i++) {
  455. if (i == MAX_INIT_ARGS) {
  456. panic_later = "init";
  457. panic_param = param;
  458. return 0;
  459. }
  460. }
  461. argv_init[i] = param;
  462. return 0;
  463. }
  464. /*
  465. * Unknown boot options get handed to init, unless they look like
  466. * unused parameters (modprobe will find them in /proc/cmdline).
  467. */
  468. static int __init unknown_bootoption(char *param, char *val,
  469. const char *unused, void *arg)
  470. {
  471. size_t len = strlen(param);
  472. /* Handle params aliased to sysctls */
  473. if (sysctl_is_alias(param))
  474. return 0;
  475. repair_env_string(param, val);
  476. /* Handle obsolete-style parameters */
  477. if (obsolete_checksetup(param))
  478. return 0;
  479. /* Unused module parameter. */
  480. if (strnchr(param, len, '.'))
  481. return 0;
  482. if (panic_later)
  483. return 0;
  484. if (val) {
  485. /* Environment option */
  486. unsigned int i;
  487. for (i = 0; envp_init[i]; i++) {
  488. if (i == MAX_INIT_ENVS) {
  489. panic_later = "env";
  490. panic_param = param;
  491. }
  492. if (!strncmp(param, envp_init[i], len+1))
  493. break;
  494. }
  495. envp_init[i] = param;
  496. } else {
  497. /* Command line option */
  498. unsigned int i;
  499. for (i = 0; argv_init[i]; i++) {
  500. if (i == MAX_INIT_ARGS) {
  501. panic_later = "init";
  502. panic_param = param;
  503. }
  504. }
  505. argv_init[i] = param;
  506. }
  507. return 0;
  508. }
  509. static int __init init_setup(char *str)
  510. {
  511. unsigned int i;
  512. execute_command = str;
  513. /*
  514. * In case LILO is going to boot us with default command line,
  515. * it prepends "auto" before the whole cmdline which makes
  516. * the shell think it should execute a script with such name.
  517. * So we ignore all arguments entered _before_ init=... [MJ]
  518. */
  519. for (i = 1; i < MAX_INIT_ARGS; i++)
  520. argv_init[i] = NULL;
  521. return 1;
  522. }
  523. __setup("init=", init_setup);
  524. static int __init rdinit_setup(char *str)
  525. {
  526. unsigned int i;
  527. ramdisk_execute_command = str;
  528. /* See "auto" comment in init_setup */
  529. for (i = 1; i < MAX_INIT_ARGS; i++)
  530. argv_init[i] = NULL;
  531. return 1;
  532. }
  533. __setup("rdinit=", rdinit_setup);
  534. #ifndef CONFIG_SMP
  535. static inline void setup_nr_cpu_ids(void) { }
  536. static inline void smp_prepare_cpus(unsigned int maxcpus) { }
  537. #endif
  538. /*
  539. * We need to store the untouched command line for future reference.
  540. * We also need to store the touched command line since the parameter
  541. * parsing is performed in place, and we should allow a component to
  542. * store reference of name/value for future reference.
  543. */
  544. static void __init setup_command_line(char *command_line)
  545. {
  546. size_t len, xlen = 0, ilen = 0;
  547. if (extra_command_line)
  548. xlen = strlen(extra_command_line);
  549. if (extra_init_args) {
  550. extra_init_args = strim(extra_init_args); /* remove trailing space */
  551. ilen = strlen(extra_init_args) + 4; /* for " -- " */
  552. }
  553. len = xlen + strlen(boot_command_line) + ilen + 1;
  554. saved_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
  555. if (!saved_command_line)
  556. panic("%s: Failed to allocate %zu bytes\n", __func__, len);
  557. len = xlen + strlen(command_line) + 1;
  558. static_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
  559. if (!static_command_line)
  560. panic("%s: Failed to allocate %zu bytes\n", __func__, len);
  561. if (xlen) {
  562. /*
  563. * We have to put extra_command_line before boot command
  564. * lines because there could be dashes (separator of init
  565. * command line) in the command lines.
  566. */
  567. strcpy(saved_command_line, extra_command_line);
  568. strcpy(static_command_line, extra_command_line);
  569. }
  570. strcpy(saved_command_line + xlen, boot_command_line);
  571. strcpy(static_command_line + xlen, command_line);
  572. if (ilen) {
  573. /*
  574. * Append supplemental init boot args to saved_command_line
  575. * so that user can check what command line options passed
  576. * to init.
  577. * The order should always be
  578. * " -- "[bootconfig init-param][cmdline init-param]
  579. */
  580. if (initargs_offs) {
  581. len = xlen + initargs_offs;
  582. strcpy(saved_command_line + len, extra_init_args);
  583. len += ilen - 4; /* strlen(extra_init_args) */
  584. strcpy(saved_command_line + len,
  585. boot_command_line + initargs_offs - 1);
  586. } else {
  587. len = strlen(saved_command_line);
  588. strcpy(saved_command_line + len, " -- ");
  589. len += 4;
  590. strcpy(saved_command_line + len, extra_init_args);
  591. }
  592. }
  593. saved_command_line_len = strlen(saved_command_line);
  594. }
  595. /*
  596. * We need to finalize in a non-__init function or else race conditions
  597. * between the root thread and the init thread may cause start_kernel to
  598. * be reaped by free_initmem before the root thread has proceeded to
  599. * cpu_idle.
  600. *
  601. * gcc-3.4 accidentally inlines this function, so use noinline.
  602. */
  603. static __initdata DECLARE_COMPLETION(kthreadd_done);
  604. static noinline void __ref __noreturn rest_init(void)
  605. {
  606. struct task_struct *tsk;
  607. int pid;
  608. rcu_scheduler_starting();
  609. /*
  610. * We need to spawn init first so that it obtains pid 1, however
  611. * the init task will end up wanting to create kthreads, which, if
  612. * we schedule it before we create kthreadd, will OOPS.
  613. */
  614. pid = user_mode_thread(kernel_init, NULL, CLONE_FS);
  615. /*
  616. * Pin init on the boot CPU. Task migration is not properly working
  617. * until sched_init_smp() has been run. It will set the allowed
  618. * CPUs for init to the non isolated CPUs.
  619. */
  620. rcu_read_lock();
  621. tsk = find_task_by_pid_ns(pid, &init_pid_ns);
  622. tsk->flags |= PF_NO_SETAFFINITY;
  623. set_cpus_allowed_ptr(tsk, cpumask_of(smp_processor_id()));
  624. rcu_read_unlock();
  625. numa_default_policy();
  626. pid = kernel_thread(kthreadd, NULL, NULL, CLONE_FS | CLONE_FILES);
  627. rcu_read_lock();
  628. kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
  629. rcu_read_unlock();
  630. /*
  631. * Enable might_sleep() and smp_processor_id() checks.
  632. * They cannot be enabled earlier because with CONFIG_PREEMPTION=y
  633. * kernel_thread() would trigger might_sleep() splats. With
  634. * CONFIG_PREEMPT_VOLUNTARY=y the init task might have scheduled
  635. * already, but it's stuck on the kthreadd_done completion.
  636. */
  637. system_state = SYSTEM_SCHEDULING;
  638. complete(&kthreadd_done);
  639. /*
  640. * The boot idle thread must execute schedule()
  641. * at least once to get things moving:
  642. */
  643. schedule_preempt_disabled();
  644. /* Call into cpu_idle with preempt disabled */
  645. cpu_startup_entry(CPUHP_ONLINE);
  646. }
  647. /* Check for early params. */
  648. static int __init do_early_param(char *param, char *val,
  649. const char *unused, void *arg)
  650. {
  651. const struct obs_kernel_param *p;
  652. for (p = __setup_start; p < __setup_end; p++) {
  653. if ((p->early && parameq(param, p->str)) ||
  654. (strcmp(param, "console") == 0 &&
  655. strcmp(p->str, "earlycon") == 0)
  656. ) {
  657. if (p->setup_func(val) != 0)
  658. pr_warn("Malformed early option '%s'\n", param);
  659. }
  660. }
  661. /* We accept everything at this stage. */
  662. return 0;
  663. }
  664. void __init parse_early_options(char *cmdline)
  665. {
  666. parse_args("early options", cmdline, NULL, 0, 0, 0, NULL,
  667. do_early_param);
  668. }
  669. /* Arch code calls this early on, or if not, just before other parsing. */
  670. void __init parse_early_param(void)
  671. {
  672. static int done __initdata;
  673. static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata;
  674. if (done)
  675. return;
  676. /* All fall through to do_early_param. */
  677. strscpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);
  678. parse_early_options(tmp_cmdline);
  679. done = 1;
  680. }
  681. void __init __weak arch_post_acpi_subsys_init(void) { }
  682. void __init __weak smp_setup_processor_id(void)
  683. {
  684. }
  685. void __init __weak smp_prepare_boot_cpu(void)
  686. {
  687. }
  688. # if THREAD_SIZE >= PAGE_SIZE
  689. void __init __weak thread_stack_cache_init(void)
  690. {
  691. }
  692. #endif
  693. void __init __weak poking_init(void) { }
  694. void __init __weak pgtable_cache_init(void) { }
  695. void __init __weak trap_init(void) { }
  696. bool initcall_debug;
  697. core_param(initcall_debug, initcall_debug, bool, 0644);
  698. #ifdef TRACEPOINTS_ENABLED
  699. static void __init initcall_debug_enable(void);
  700. #else
  701. static inline void initcall_debug_enable(void)
  702. {
  703. }
  704. #endif
  705. #ifdef CONFIG_RANDOMIZE_KSTACK_OFFSET
  706. DEFINE_STATIC_KEY_MAYBE_RO(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT,
  707. randomize_kstack_offset);
  708. DEFINE_PER_CPU(u32, kstack_offset);
  709. static int __init early_randomize_kstack_offset(char *buf)
  710. {
  711. int ret;
  712. bool bool_result;
  713. ret = kstrtobool(buf, &bool_result);
  714. if (ret)
  715. return ret;
  716. if (bool_result)
  717. static_branch_enable(&randomize_kstack_offset);
  718. else
  719. static_branch_disable(&randomize_kstack_offset);
  720. return 0;
  721. }
  722. early_param("randomize_kstack_offset", early_randomize_kstack_offset);
  723. #endif
  724. static void __init print_unknown_bootoptions(void)
  725. {
  726. char *unknown_options;
  727. char *end;
  728. const char *const *p;
  729. size_t len;
  730. if (panic_later || (!argv_init[1] && !envp_init[2]))
  731. return;
  732. /*
  733. * Determine how many options we have to print out, plus a space
  734. * before each
  735. */
  736. len = 1; /* null terminator */
  737. for (p = &argv_init[1]; *p; p++) {
  738. len++;
  739. len += strlen(*p);
  740. }
  741. for (p = &envp_init[2]; *p; p++) {
  742. len++;
  743. len += strlen(*p);
  744. }
  745. unknown_options = memblock_alloc(len, SMP_CACHE_BYTES);
  746. if (!unknown_options) {
  747. pr_err("%s: Failed to allocate %zu bytes\n",
  748. __func__, len);
  749. return;
  750. }
  751. end = unknown_options;
  752. for (p = &argv_init[1]; *p; p++)
  753. end += sprintf(end, " %s", *p);
  754. for (p = &envp_init[2]; *p; p++)
  755. end += sprintf(end, " %s", *p);
  756. /* Start at unknown_options[1] to skip the initial space */
  757. pr_notice("Unknown kernel command line parameters \"%s\", will be passed to user space.\n",
  758. &unknown_options[1]);
  759. memblock_free(unknown_options, len);
  760. }
  761. static void __init early_numa_node_init(void)
  762. {
  763. #ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID
  764. #ifndef cpu_to_node
  765. int cpu;
  766. /* The early_cpu_to_node() should be ready here. */
  767. for_each_possible_cpu(cpu)
  768. set_cpu_numa_node(cpu, early_cpu_to_node(cpu));
  769. #endif
  770. #endif
  771. }
  772. asmlinkage __visible __init __no_sanitize_address __noreturn __no_stack_protector
  773. void start_kernel(void)
  774. {
  775. char *command_line;
  776. char *after_dashes;
  777. set_task_stack_end_magic(&init_task);
  778. smp_setup_processor_id();
  779. debug_objects_early_init();
  780. init_vmlinux_build_id();
  781. cgroup_init_early();
  782. local_irq_disable();
  783. early_boot_irqs_disabled = true;
  784. /*
  785. * Interrupts are still disabled. Do necessary setups, then
  786. * enable them.
  787. */
  788. boot_cpu_init();
  789. page_address_init();
  790. pr_notice("%s", linux_banner);
  791. setup_arch(&command_line);
  792. /* Static keys and static calls are needed by LSMs */
  793. jump_label_init();
  794. static_call_init();
  795. early_security_init();
  796. setup_boot_config();
  797. setup_command_line(command_line);
  798. setup_nr_cpu_ids();
  799. setup_per_cpu_areas();
  800. smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
  801. early_numa_node_init();
  802. boot_cpu_hotplug_init();
  803. pr_notice("Kernel command line: %s\n", saved_command_line);
  804. /* parameters may set static keys */
  805. parse_early_param();
  806. after_dashes = parse_args("Booting kernel",
  807. static_command_line, __start___param,
  808. __stop___param - __start___param,
  809. -1, -1, NULL, &unknown_bootoption);
  810. print_unknown_bootoptions();
  811. if (!IS_ERR_OR_NULL(after_dashes))
  812. parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
  813. NULL, set_init_arg);
  814. if (extra_init_args)
  815. parse_args("Setting extra init args", extra_init_args,
  816. NULL, 0, -1, -1, NULL, set_init_arg);
  817. /* Architectural and non-timekeeping rng init, before allocator init */
  818. random_init_early(command_line);
  819. /*
  820. * These use large bootmem allocations and must precede
  821. * initalization of page allocator
  822. */
  823. setup_log_buf(0);
  824. vfs_caches_init_early();
  825. sort_main_extable();
  826. trap_init();
  827. mm_core_init();
  828. poking_init();
  829. ftrace_init();
  830. /* trace_printk can be enabled here */
  831. early_trace_init();
  832. /*
  833. * Set up the scheduler prior starting any interrupts (such as the
  834. * timer interrupt). Full topology setup happens at smp_init()
  835. * time - but meanwhile we still have a functioning scheduler.
  836. */
  837. sched_init();
  838. if (WARN(!irqs_disabled(),
  839. "Interrupts were enabled *very* early, fixing it\n"))
  840. local_irq_disable();
  841. radix_tree_init();
  842. maple_tree_init();
  843. /*
  844. * Set up housekeeping before setting up workqueues to allow the unbound
  845. * workqueue to take non-housekeeping into account.
  846. */
  847. housekeeping_init();
  848. /*
  849. * Allow workqueue creation and work item queueing/cancelling
  850. * early. Work item execution depends on kthreads and starts after
  851. * workqueue_init().
  852. */
  853. workqueue_init_early();
  854. rcu_init();
  855. /* Trace events are available after this */
  856. trace_init();
  857. if (initcall_debug)
  858. initcall_debug_enable();
  859. context_tracking_init();
  860. /* init some links before init_ISA_irqs() */
  861. early_irq_init();
  862. init_IRQ();
  863. tick_init();
  864. rcu_init_nohz();
  865. init_timers();
  866. srcu_init();
  867. hrtimers_init();
  868. softirq_init();
  869. timekeeping_init();
  870. time_init();
  871. /* This must be after timekeeping is initialized */
  872. random_init();
  873. /* These make use of the fully initialized rng */
  874. kfence_init();
  875. boot_init_stack_canary();
  876. perf_event_init();
  877. profile_init();
  878. call_function_init();
  879. WARN(!irqs_disabled(), "Interrupts were enabled early\n");
  880. early_boot_irqs_disabled = false;
  881. local_irq_enable();
  882. kmem_cache_init_late();
  883. /*
  884. * HACK ALERT! This is early. We're enabling the console before
  885. * we've done PCI setups etc, and console_init() must be aware of
  886. * this. But we do want output early, in case something goes wrong.
  887. */
  888. console_init();
  889. if (panic_later)
  890. panic("Too many boot %s vars at `%s'", panic_later,
  891. panic_param);
  892. lockdep_init();
  893. /*
  894. * Need to run this when irqs are enabled, because it wants
  895. * to self-test [hard/soft]-irqs on/off lock inversion bugs
  896. * too:
  897. */
  898. locking_selftest();
  899. #ifdef CONFIG_BLK_DEV_INITRD
  900. if (initrd_start && !initrd_below_start_ok &&
  901. page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
  902. pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n",
  903. page_to_pfn(virt_to_page((void *)initrd_start)),
  904. min_low_pfn);
  905. initrd_start = 0;
  906. }
  907. #endif
  908. setup_per_cpu_pageset();
  909. numa_policy_init();
  910. acpi_early_init();
  911. if (late_time_init)
  912. late_time_init();
  913. sched_clock_init();
  914. calibrate_delay();
  915. arch_cpu_finalize_init();
  916. pid_idr_init();
  917. anon_vma_init();
  918. #ifdef CONFIG_X86
  919. if (efi_enabled(EFI_RUNTIME_SERVICES))
  920. efi_enter_virtual_mode();
  921. #endif
  922. thread_stack_cache_init();
  923. cred_init();
  924. fork_init();
  925. proc_caches_init();
  926. uts_ns_init();
  927. key_init();
  928. security_init();
  929. dbg_late_init();
  930. net_ns_init();
  931. vfs_caches_init();
  932. pagecache_init();
  933. signals_init();
  934. seq_file_init();
  935. proc_root_init();
  936. nsfs_init();
  937. pidfs_init();
  938. cpuset_init();
  939. cgroup_init();
  940. taskstats_init_early();
  941. delayacct_init();
  942. acpi_subsystem_init();
  943. arch_post_acpi_subsys_init();
  944. kcsan_init();
  945. /* Do the rest non-__init'ed, we're now alive */
  946. rest_init();
  947. /*
  948. * Avoid stack canaries in callers of boot_init_stack_canary for gcc-10
  949. * and older.
  950. */
  951. #if !__has_attribute(__no_stack_protector__)
  952. prevent_tail_call_optimization();
  953. #endif
  954. }
  955. /* Call all constructor functions linked into the kernel. */
  956. static void __init do_ctors(void)
  957. {
  958. /*
  959. * For UML, the constructors have already been called by the
  960. * normal setup code as it's just a normal ELF binary, so we
  961. * cannot do it again - but we do need CONFIG_CONSTRUCTORS
  962. * even on UML for modules.
  963. */
  964. #if defined(CONFIG_CONSTRUCTORS) && !defined(CONFIG_UML)
  965. ctor_fn_t *fn = (ctor_fn_t *) __ctors_start;
  966. for (; fn < (ctor_fn_t *) __ctors_end; fn++)
  967. (*fn)();
  968. #endif
  969. }
  970. #ifdef CONFIG_KALLSYMS
  971. struct blacklist_entry {
  972. struct list_head next;
  973. char *buf;
  974. };
  975. static __initdata_or_module LIST_HEAD(blacklisted_initcalls);
  976. static int __init initcall_blacklist(char *str)
  977. {
  978. char *str_entry;
  979. struct blacklist_entry *entry;
  980. /* str argument is a comma-separated list of functions */
  981. do {
  982. str_entry = strsep(&str, ",");
  983. if (str_entry) {
  984. pr_debug("blacklisting initcall %s\n", str_entry);
  985. entry = memblock_alloc(sizeof(*entry),
  986. SMP_CACHE_BYTES);
  987. if (!entry)
  988. panic("%s: Failed to allocate %zu bytes\n",
  989. __func__, sizeof(*entry));
  990. entry->buf = memblock_alloc(strlen(str_entry) + 1,
  991. SMP_CACHE_BYTES);
  992. if (!entry->buf)
  993. panic("%s: Failed to allocate %zu bytes\n",
  994. __func__, strlen(str_entry) + 1);
  995. strcpy(entry->buf, str_entry);
  996. list_add(&entry->next, &blacklisted_initcalls);
  997. }
  998. } while (str_entry);
  999. return 1;
  1000. }
  1001. static bool __init_or_module initcall_blacklisted(initcall_t fn)
  1002. {
  1003. struct blacklist_entry *entry;
  1004. char fn_name[KSYM_SYMBOL_LEN];
  1005. unsigned long addr;
  1006. if (list_empty(&blacklisted_initcalls))
  1007. return false;
  1008. addr = (unsigned long) dereference_function_descriptor(fn);
  1009. sprint_symbol_no_offset(fn_name, addr);
  1010. /*
  1011. * fn will be "function_name [module_name]" where [module_name] is not
  1012. * displayed for built-in init functions. Strip off the [module_name].
  1013. */
  1014. strreplace(fn_name, ' ', '\0');
  1015. list_for_each_entry(entry, &blacklisted_initcalls, next) {
  1016. if (!strcmp(fn_name, entry->buf)) {
  1017. pr_debug("initcall %s blacklisted\n", fn_name);
  1018. return true;
  1019. }
  1020. }
  1021. return false;
  1022. }
  1023. #else
  1024. static int __init initcall_blacklist(char *str)
  1025. {
  1026. pr_warn("initcall_blacklist requires CONFIG_KALLSYMS\n");
  1027. return 0;
  1028. }
  1029. static bool __init_or_module initcall_blacklisted(initcall_t fn)
  1030. {
  1031. return false;
  1032. }
  1033. #endif
  1034. __setup("initcall_blacklist=", initcall_blacklist);
  1035. static __init_or_module void
  1036. trace_initcall_start_cb(void *data, initcall_t fn)
  1037. {
  1038. ktime_t *calltime = data;
  1039. printk(KERN_DEBUG "calling %pS @ %i\n", fn, task_pid_nr(current));
  1040. *calltime = ktime_get();
  1041. }
  1042. static __init_or_module void
  1043. trace_initcall_finish_cb(void *data, initcall_t fn, int ret)
  1044. {
  1045. ktime_t rettime, *calltime = data;
  1046. rettime = ktime_get();
  1047. printk(KERN_DEBUG "initcall %pS returned %d after %lld usecs\n",
  1048. fn, ret, (unsigned long long)ktime_us_delta(rettime, *calltime));
  1049. }
  1050. static ktime_t initcall_calltime;
  1051. #ifdef TRACEPOINTS_ENABLED
  1052. static void __init initcall_debug_enable(void)
  1053. {
  1054. int ret;
  1055. ret = register_trace_initcall_start(trace_initcall_start_cb,
  1056. &initcall_calltime);
  1057. ret |= register_trace_initcall_finish(trace_initcall_finish_cb,
  1058. &initcall_calltime);
  1059. WARN(ret, "Failed to register initcall tracepoints\n");
  1060. }
  1061. # define do_trace_initcall_start trace_initcall_start
  1062. # define do_trace_initcall_finish trace_initcall_finish
  1063. #else
  1064. static inline void do_trace_initcall_start(initcall_t fn)
  1065. {
  1066. if (!initcall_debug)
  1067. return;
  1068. trace_initcall_start_cb(&initcall_calltime, fn);
  1069. }
  1070. static inline void do_trace_initcall_finish(initcall_t fn, int ret)
  1071. {
  1072. if (!initcall_debug)
  1073. return;
  1074. trace_initcall_finish_cb(&initcall_calltime, fn, ret);
  1075. }
  1076. #endif /* !TRACEPOINTS_ENABLED */
  1077. int __init_or_module do_one_initcall(initcall_t fn)
  1078. {
  1079. int count = preempt_count();
  1080. char msgbuf[64];
  1081. int ret;
  1082. if (initcall_blacklisted(fn))
  1083. return -EPERM;
  1084. do_trace_initcall_start(fn);
  1085. ret = fn();
  1086. do_trace_initcall_finish(fn, ret);
  1087. msgbuf[0] = 0;
  1088. if (preempt_count() != count) {
  1089. sprintf(msgbuf, "preemption imbalance ");
  1090. preempt_count_set(count);
  1091. }
  1092. if (irqs_disabled()) {
  1093. strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
  1094. local_irq_enable();
  1095. }
  1096. WARN(msgbuf[0], "initcall %pS returned with %s\n", fn, msgbuf);
  1097. add_latent_entropy();
  1098. return ret;
  1099. }
  1100. static initcall_entry_t *initcall_levels[] __initdata = {
  1101. __initcall0_start,
  1102. __initcall1_start,
  1103. __initcall2_start,
  1104. __initcall3_start,
  1105. __initcall4_start,
  1106. __initcall5_start,
  1107. __initcall6_start,
  1108. __initcall7_start,
  1109. __initcall_end,
  1110. };
  1111. /* Keep these in sync with initcalls in include/linux/init.h */
  1112. static const char *initcall_level_names[] __initdata = {
  1113. "pure",
  1114. "core",
  1115. "postcore",
  1116. "arch",
  1117. "subsys",
  1118. "fs",
  1119. "device",
  1120. "late",
  1121. };
  1122. static int __init ignore_unknown_bootoption(char *param, char *val,
  1123. const char *unused, void *arg)
  1124. {
  1125. return 0;
  1126. }
  1127. static void __init do_initcall_level(int level, char *command_line)
  1128. {
  1129. initcall_entry_t *fn;
  1130. parse_args(initcall_level_names[level],
  1131. command_line, __start___param,
  1132. __stop___param - __start___param,
  1133. level, level,
  1134. NULL, ignore_unknown_bootoption);
  1135. trace_initcall_level(initcall_level_names[level]);
  1136. for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++)
  1137. do_one_initcall(initcall_from_entry(fn));
  1138. }
  1139. static void __init do_initcalls(void)
  1140. {
  1141. int level;
  1142. size_t len = saved_command_line_len + 1;
  1143. char *command_line;
  1144. command_line = kzalloc(len, GFP_KERNEL);
  1145. if (!command_line)
  1146. panic("%s: Failed to allocate %zu bytes\n", __func__, len);
  1147. for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++) {
  1148. /* Parser modifies command_line, restore it each time */
  1149. strcpy(command_line, saved_command_line);
  1150. do_initcall_level(level, command_line);
  1151. }
  1152. kfree(command_line);
  1153. }
  1154. /*
  1155. * Ok, the machine is now initialized. None of the devices
  1156. * have been touched yet, but the CPU subsystem is up and
  1157. * running, and memory and process management works.
  1158. *
  1159. * Now we can finally start doing some real work..
  1160. */
  1161. static void __init do_basic_setup(void)
  1162. {
  1163. cpuset_init_smp();
  1164. driver_init();
  1165. init_irq_proc();
  1166. do_ctors();
  1167. do_initcalls();
  1168. }
  1169. static void __init do_pre_smp_initcalls(void)
  1170. {
  1171. initcall_entry_t *fn;
  1172. trace_initcall_level("early");
  1173. for (fn = __initcall_start; fn < __initcall0_start; fn++)
  1174. do_one_initcall(initcall_from_entry(fn));
  1175. }
  1176. static int run_init_process(const char *init_filename)
  1177. {
  1178. const char *const *p;
  1179. argv_init[0] = init_filename;
  1180. pr_info("Run %s as init process\n", init_filename);
  1181. pr_debug(" with arguments:\n");
  1182. for (p = argv_init; *p; p++)
  1183. pr_debug(" %s\n", *p);
  1184. pr_debug(" with environment:\n");
  1185. for (p = envp_init; *p; p++)
  1186. pr_debug(" %s\n", *p);
  1187. return kernel_execve(init_filename, argv_init, envp_init);
  1188. }
  1189. static int try_to_run_init_process(const char *init_filename)
  1190. {
  1191. int ret;
  1192. ret = run_init_process(init_filename);
  1193. if (ret && ret != -ENOENT) {
  1194. pr_err("Starting init: %s exists but couldn't execute it (error %d)\n",
  1195. init_filename, ret);
  1196. }
  1197. return ret;
  1198. }
  1199. static noinline void __init kernel_init_freeable(void);
  1200. #if defined(CONFIG_STRICT_KERNEL_RWX) || defined(CONFIG_STRICT_MODULE_RWX)
  1201. bool rodata_enabled __ro_after_init = true;
  1202. #ifndef arch_parse_debug_rodata
  1203. static inline bool arch_parse_debug_rodata(char *str) { return false; }
  1204. #endif
  1205. static int __init set_debug_rodata(char *str)
  1206. {
  1207. if (arch_parse_debug_rodata(str))
  1208. return 0;
  1209. if (str && !strcmp(str, "on"))
  1210. rodata_enabled = true;
  1211. else if (str && !strcmp(str, "off"))
  1212. rodata_enabled = false;
  1213. else
  1214. pr_warn("Invalid option string for rodata: '%s'\n", str);
  1215. return 0;
  1216. }
  1217. early_param("rodata", set_debug_rodata);
  1218. #endif
  1219. static void mark_readonly(void)
  1220. {
  1221. if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX) && rodata_enabled) {
  1222. /*
  1223. * load_module() results in W+X mappings, which are cleaned
  1224. * up with init_free_wq. Let's make sure that queued work is
  1225. * flushed so that we don't hit false positives looking for
  1226. * insecure pages which are W+X.
  1227. */
  1228. flush_module_init_free_work();
  1229. jump_label_init_ro();
  1230. mark_rodata_ro();
  1231. debug_checkwx();
  1232. rodata_test();
  1233. } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) {
  1234. pr_info("Kernel memory protection disabled.\n");
  1235. } else if (IS_ENABLED(CONFIG_ARCH_HAS_STRICT_KERNEL_RWX)) {
  1236. pr_warn("Kernel memory protection not selected by kernel config.\n");
  1237. } else {
  1238. pr_warn("This architecture does not have kernel memory protection.\n");
  1239. }
  1240. }
  1241. void __weak free_initmem(void)
  1242. {
  1243. free_initmem_default(POISON_FREE_INITMEM);
  1244. }
  1245. static int __ref kernel_init(void *unused)
  1246. {
  1247. int ret;
  1248. /*
  1249. * Wait until kthreadd is all set-up.
  1250. */
  1251. wait_for_completion(&kthreadd_done);
  1252. kernel_init_freeable();
  1253. /* need to finish all async __init code before freeing the memory */
  1254. async_synchronize_full();
  1255. system_state = SYSTEM_FREEING_INITMEM;
  1256. kprobe_free_init_mem();
  1257. ftrace_free_init_mem();
  1258. kgdb_free_init_mem();
  1259. exit_boot_config();
  1260. free_initmem();
  1261. mark_readonly();
  1262. /*
  1263. * Kernel mappings are now finalized - update the userspace page-table
  1264. * to finalize PTI.
  1265. */
  1266. pti_finalize();
  1267. system_state = SYSTEM_RUNNING;
  1268. numa_default_policy();
  1269. rcu_end_inkernel_boot();
  1270. do_sysctl_args();
  1271. if (ramdisk_execute_command) {
  1272. ret = run_init_process(ramdisk_execute_command);
  1273. if (!ret)
  1274. return 0;
  1275. pr_err("Failed to execute %s (error %d)\n",
  1276. ramdisk_execute_command, ret);
  1277. }
  1278. /*
  1279. * We try each of these until one succeeds.
  1280. *
  1281. * The Bourne shell can be used instead of init if we are
  1282. * trying to recover a really broken machine.
  1283. */
  1284. if (execute_command) {
  1285. ret = run_init_process(execute_command);
  1286. if (!ret)
  1287. return 0;
  1288. panic("Requested init %s failed (error %d).",
  1289. execute_command, ret);
  1290. }
  1291. if (CONFIG_DEFAULT_INIT[0] != '\0') {
  1292. ret = run_init_process(CONFIG_DEFAULT_INIT);
  1293. if (ret)
  1294. pr_err("Default init %s failed (error %d)\n",
  1295. CONFIG_DEFAULT_INIT, ret);
  1296. else
  1297. return 0;
  1298. }
  1299. if (!try_to_run_init_process("/sbin/init") ||
  1300. !try_to_run_init_process("/etc/init") ||
  1301. !try_to_run_init_process("/bin/init") ||
  1302. !try_to_run_init_process("/bin/sh"))
  1303. return 0;
  1304. panic("No working init found. Try passing init= option to kernel. "
  1305. "See Linux Documentation/admin-guide/init.rst for guidance.");
  1306. }
  1307. /* Open /dev/console, for stdin/stdout/stderr, this should never fail */
  1308. void __init console_on_rootfs(void)
  1309. {
  1310. struct file *file = filp_open("/dev/console", O_RDWR, 0);
  1311. if (IS_ERR(file)) {
  1312. pr_err("Warning: unable to open an initial console.\n");
  1313. return;
  1314. }
  1315. init_dup(file);
  1316. init_dup(file);
  1317. init_dup(file);
  1318. fput(file);
  1319. }
  1320. static noinline void __init kernel_init_freeable(void)
  1321. {
  1322. /* Now the scheduler is fully set up and can do blocking allocations */
  1323. gfp_allowed_mask = __GFP_BITS_MASK;
  1324. /*
  1325. * init can allocate pages on any node
  1326. */
  1327. set_mems_allowed(node_states[N_MEMORY]);
  1328. cad_pid = get_pid(task_pid(current));
  1329. smp_prepare_cpus(setup_max_cpus);
  1330. workqueue_init();
  1331. init_mm_internals();
  1332. rcu_init_tasks_generic();
  1333. do_pre_smp_initcalls();
  1334. lockup_detector_init();
  1335. smp_init();
  1336. sched_init_smp();
  1337. workqueue_init_topology();
  1338. async_init();
  1339. padata_init();
  1340. page_alloc_init_late();
  1341. do_basic_setup();
  1342. kunit_run_all_tests();
  1343. wait_for_initramfs();
  1344. console_on_rootfs();
  1345. /*
  1346. * check if there is an early userspace init. If yes, let it do all
  1347. * the work
  1348. */
  1349. if (init_eaccess(ramdisk_execute_command) != 0) {
  1350. ramdisk_execute_command = NULL;
  1351. prepare_namespace();
  1352. }
  1353. /*
  1354. * Ok, we have completed the initial bootup, and
  1355. * we're essentially up and running. Get rid of the
  1356. * initmem segments and start the user-mode stuff..
  1357. *
  1358. * rootfs is available now, try loading the public keys
  1359. * and default modules
  1360. */
  1361. integrity_load_keys();
  1362. }