main.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  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. /*
  473. * Well-known bootloader identifiers:
  474. * 1. LILO/Grub pass "BOOT_IMAGE=...";
  475. * 2. kexec/kdump (kexec-tools) pass "kexec".
  476. */
  477. const char *bootloader[] = { "BOOT_IMAGE=", "kexec", NULL };
  478. /* Handle params aliased to sysctls */
  479. if (sysctl_is_alias(param))
  480. return 0;
  481. repair_env_string(param, val);
  482. /* Handle bootloader identifier */
  483. for (int i = 0; bootloader[i]; i++) {
  484. if (strstarts(param, bootloader[i]))
  485. return 0;
  486. }
  487. /* Handle obsolete-style parameters */
  488. if (obsolete_checksetup(param))
  489. return 0;
  490. /* Unused module parameter. */
  491. if (strnchr(param, len, '.'))
  492. return 0;
  493. if (panic_later)
  494. return 0;
  495. if (val) {
  496. /* Environment option */
  497. unsigned int i;
  498. for (i = 0; envp_init[i]; i++) {
  499. if (i == MAX_INIT_ENVS) {
  500. panic_later = "env";
  501. panic_param = param;
  502. }
  503. if (!strncmp(param, envp_init[i], len+1))
  504. break;
  505. }
  506. envp_init[i] = param;
  507. } else {
  508. /* Command line option */
  509. unsigned int i;
  510. for (i = 0; argv_init[i]; i++) {
  511. if (i == MAX_INIT_ARGS) {
  512. panic_later = "init";
  513. panic_param = param;
  514. }
  515. }
  516. argv_init[i] = param;
  517. }
  518. return 0;
  519. }
  520. static int __init init_setup(char *str)
  521. {
  522. unsigned int i;
  523. execute_command = str;
  524. /*
  525. * In case LILO is going to boot us with default command line,
  526. * it prepends "auto" before the whole cmdline which makes
  527. * the shell think it should execute a script with such name.
  528. * So we ignore all arguments entered _before_ init=... [MJ]
  529. */
  530. for (i = 1; i < MAX_INIT_ARGS; i++)
  531. argv_init[i] = NULL;
  532. return 1;
  533. }
  534. __setup("init=", init_setup);
  535. static int __init rdinit_setup(char *str)
  536. {
  537. unsigned int i;
  538. ramdisk_execute_command = str;
  539. /* See "auto" comment in init_setup */
  540. for (i = 1; i < MAX_INIT_ARGS; i++)
  541. argv_init[i] = NULL;
  542. return 1;
  543. }
  544. __setup("rdinit=", rdinit_setup);
  545. #ifndef CONFIG_SMP
  546. static inline void setup_nr_cpu_ids(void) { }
  547. static inline void smp_prepare_cpus(unsigned int maxcpus) { }
  548. #endif
  549. /*
  550. * We need to store the untouched command line for future reference.
  551. * We also need to store the touched command line since the parameter
  552. * parsing is performed in place, and we should allow a component to
  553. * store reference of name/value for future reference.
  554. */
  555. static void __init setup_command_line(char *command_line)
  556. {
  557. size_t len, xlen = 0, ilen = 0;
  558. if (extra_command_line)
  559. xlen = strlen(extra_command_line);
  560. if (extra_init_args) {
  561. extra_init_args = strim(extra_init_args); /* remove trailing space */
  562. ilen = strlen(extra_init_args) + 4; /* for " -- " */
  563. }
  564. len = xlen + strlen(boot_command_line) + ilen + 1;
  565. saved_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
  566. if (!saved_command_line)
  567. panic("%s: Failed to allocate %zu bytes\n", __func__, len);
  568. len = xlen + strlen(command_line) + 1;
  569. static_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
  570. if (!static_command_line)
  571. panic("%s: Failed to allocate %zu bytes\n", __func__, len);
  572. if (xlen) {
  573. /*
  574. * We have to put extra_command_line before boot command
  575. * lines because there could be dashes (separator of init
  576. * command line) in the command lines.
  577. */
  578. strcpy(saved_command_line, extra_command_line);
  579. strcpy(static_command_line, extra_command_line);
  580. }
  581. strcpy(saved_command_line + xlen, boot_command_line);
  582. strcpy(static_command_line + xlen, command_line);
  583. if (ilen) {
  584. /*
  585. * Append supplemental init boot args to saved_command_line
  586. * so that user can check what command line options passed
  587. * to init.
  588. * The order should always be
  589. * " -- "[bootconfig init-param][cmdline init-param]
  590. */
  591. if (initargs_offs) {
  592. len = xlen + initargs_offs;
  593. strcpy(saved_command_line + len, extra_init_args);
  594. len += ilen - 4; /* strlen(extra_init_args) */
  595. strcpy(saved_command_line + len,
  596. boot_command_line + initargs_offs - 1);
  597. } else {
  598. len = strlen(saved_command_line);
  599. strcpy(saved_command_line + len, " -- ");
  600. len += 4;
  601. strcpy(saved_command_line + len, extra_init_args);
  602. }
  603. }
  604. saved_command_line_len = strlen(saved_command_line);
  605. }
  606. /*
  607. * We need to finalize in a non-__init function or else race conditions
  608. * between the root thread and the init thread may cause start_kernel to
  609. * be reaped by free_initmem before the root thread has proceeded to
  610. * cpu_idle.
  611. *
  612. * gcc-3.4 accidentally inlines this function, so use noinline.
  613. */
  614. static __initdata DECLARE_COMPLETION(kthreadd_done);
  615. static noinline void __ref __noreturn rest_init(void)
  616. {
  617. struct task_struct *tsk;
  618. int pid;
  619. rcu_scheduler_starting();
  620. /*
  621. * We need to spawn init first so that it obtains pid 1, however
  622. * the init task will end up wanting to create kthreads, which, if
  623. * we schedule it before we create kthreadd, will OOPS.
  624. */
  625. pid = user_mode_thread(kernel_init, NULL, CLONE_FS);
  626. /*
  627. * Pin init on the boot CPU. Task migration is not properly working
  628. * until sched_init_smp() has been run. It will set the allowed
  629. * CPUs for init to the non isolated CPUs.
  630. */
  631. rcu_read_lock();
  632. tsk = find_task_by_pid_ns(pid, &init_pid_ns);
  633. tsk->flags |= PF_NO_SETAFFINITY;
  634. set_cpus_allowed_ptr(tsk, cpumask_of(smp_processor_id()));
  635. rcu_read_unlock();
  636. numa_default_policy();
  637. pid = kernel_thread(kthreadd, NULL, NULL, CLONE_FS | CLONE_FILES);
  638. rcu_read_lock();
  639. kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
  640. rcu_read_unlock();
  641. /*
  642. * Enable might_sleep() and smp_processor_id() checks.
  643. * They cannot be enabled earlier because with CONFIG_PREEMPTION=y
  644. * kernel_thread() would trigger might_sleep() splats. With
  645. * CONFIG_PREEMPT_VOLUNTARY=y the init task might have scheduled
  646. * already, but it's stuck on the kthreadd_done completion.
  647. */
  648. system_state = SYSTEM_SCHEDULING;
  649. complete(&kthreadd_done);
  650. /*
  651. * The boot idle thread must execute schedule()
  652. * at least once to get things moving:
  653. */
  654. schedule_preempt_disabled();
  655. /* Call into cpu_idle with preempt disabled */
  656. cpu_startup_entry(CPUHP_ONLINE);
  657. }
  658. /* Check for early params. */
  659. static int __init do_early_param(char *param, char *val,
  660. const char *unused, void *arg)
  661. {
  662. const struct obs_kernel_param *p;
  663. for (p = __setup_start; p < __setup_end; p++) {
  664. if ((p->early && parameq(param, p->str)) ||
  665. (strcmp(param, "console") == 0 &&
  666. strcmp(p->str, "earlycon") == 0)
  667. ) {
  668. if (p->setup_func(val) != 0)
  669. pr_warn("Malformed early option '%s'\n", param);
  670. }
  671. }
  672. /* We accept everything at this stage. */
  673. return 0;
  674. }
  675. void __init parse_early_options(char *cmdline)
  676. {
  677. parse_args("early options", cmdline, NULL, 0, 0, 0, NULL,
  678. do_early_param);
  679. }
  680. /* Arch code calls this early on, or if not, just before other parsing. */
  681. void __init parse_early_param(void)
  682. {
  683. static int done __initdata;
  684. static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata;
  685. if (done)
  686. return;
  687. /* All fall through to do_early_param. */
  688. strscpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);
  689. parse_early_options(tmp_cmdline);
  690. done = 1;
  691. }
  692. void __init __weak arch_post_acpi_subsys_init(void) { }
  693. void __init __weak smp_setup_processor_id(void)
  694. {
  695. }
  696. void __init __weak smp_prepare_boot_cpu(void)
  697. {
  698. }
  699. # if THREAD_SIZE >= PAGE_SIZE
  700. void __init __weak thread_stack_cache_init(void)
  701. {
  702. }
  703. #endif
  704. void __init __weak poking_init(void) { }
  705. void __init __weak pgtable_cache_init(void) { }
  706. void __init __weak trap_init(void) { }
  707. bool initcall_debug;
  708. core_param(initcall_debug, initcall_debug, bool, 0644);
  709. #ifdef TRACEPOINTS_ENABLED
  710. static void __init initcall_debug_enable(void);
  711. #else
  712. static inline void initcall_debug_enable(void)
  713. {
  714. }
  715. #endif
  716. #ifdef CONFIG_RANDOMIZE_KSTACK_OFFSET
  717. DEFINE_STATIC_KEY_MAYBE_RO(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT,
  718. randomize_kstack_offset);
  719. DEFINE_PER_CPU(u32, kstack_offset);
  720. static int __init early_randomize_kstack_offset(char *buf)
  721. {
  722. int ret;
  723. bool bool_result;
  724. ret = kstrtobool(buf, &bool_result);
  725. if (ret)
  726. return ret;
  727. if (bool_result)
  728. static_branch_enable(&randomize_kstack_offset);
  729. else
  730. static_branch_disable(&randomize_kstack_offset);
  731. return 0;
  732. }
  733. early_param("randomize_kstack_offset", early_randomize_kstack_offset);
  734. #endif
  735. static void __init print_unknown_bootoptions(void)
  736. {
  737. char *unknown_options;
  738. char *end;
  739. const char *const *p;
  740. size_t len;
  741. if (panic_later || (!argv_init[1] && !envp_init[2]))
  742. return;
  743. /*
  744. * Determine how many options we have to print out, plus a space
  745. * before each
  746. */
  747. len = 1; /* null terminator */
  748. for (p = &argv_init[1]; *p; p++) {
  749. len++;
  750. len += strlen(*p);
  751. }
  752. for (p = &envp_init[2]; *p; p++) {
  753. len++;
  754. len += strlen(*p);
  755. }
  756. unknown_options = memblock_alloc(len, SMP_CACHE_BYTES);
  757. if (!unknown_options) {
  758. pr_err("%s: Failed to allocate %zu bytes\n",
  759. __func__, len);
  760. return;
  761. }
  762. end = unknown_options;
  763. for (p = &argv_init[1]; *p; p++)
  764. end += sprintf(end, " %s", *p);
  765. for (p = &envp_init[2]; *p; p++)
  766. end += sprintf(end, " %s", *p);
  767. /* Start at unknown_options[1] to skip the initial space */
  768. pr_notice("Unknown kernel command line parameters \"%s\", will be passed to user space.\n",
  769. &unknown_options[1]);
  770. memblock_free(unknown_options, len);
  771. }
  772. static void __init early_numa_node_init(void)
  773. {
  774. #ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID
  775. #ifndef cpu_to_node
  776. int cpu;
  777. /* The early_cpu_to_node() should be ready here. */
  778. for_each_possible_cpu(cpu)
  779. set_cpu_numa_node(cpu, early_cpu_to_node(cpu));
  780. #endif
  781. #endif
  782. }
  783. asmlinkage __visible __init __no_sanitize_address __noreturn __no_stack_protector
  784. void start_kernel(void)
  785. {
  786. char *command_line;
  787. char *after_dashes;
  788. set_task_stack_end_magic(&init_task);
  789. smp_setup_processor_id();
  790. debug_objects_early_init();
  791. init_vmlinux_build_id();
  792. cgroup_init_early();
  793. local_irq_disable();
  794. early_boot_irqs_disabled = true;
  795. /*
  796. * Interrupts are still disabled. Do necessary setups, then
  797. * enable them.
  798. */
  799. boot_cpu_init();
  800. page_address_init();
  801. pr_notice("%s", linux_banner);
  802. setup_arch(&command_line);
  803. /* Static keys and static calls are needed by LSMs */
  804. jump_label_init();
  805. static_call_init();
  806. early_security_init();
  807. setup_boot_config();
  808. setup_command_line(command_line);
  809. setup_nr_cpu_ids();
  810. setup_per_cpu_areas();
  811. smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
  812. early_numa_node_init();
  813. boot_cpu_hotplug_init();
  814. pr_notice("Kernel command line: %s\n", saved_command_line);
  815. /* parameters may set static keys */
  816. parse_early_param();
  817. after_dashes = parse_args("Booting kernel",
  818. static_command_line, __start___param,
  819. __stop___param - __start___param,
  820. -1, -1, NULL, &unknown_bootoption);
  821. print_unknown_bootoptions();
  822. if (!IS_ERR_OR_NULL(after_dashes))
  823. parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
  824. NULL, set_init_arg);
  825. if (extra_init_args)
  826. parse_args("Setting extra init args", extra_init_args,
  827. NULL, 0, -1, -1, NULL, set_init_arg);
  828. /* Architectural and non-timekeeping rng init, before allocator init */
  829. random_init_early(command_line);
  830. /*
  831. * These use large bootmem allocations and must precede
  832. * initalization of page allocator
  833. */
  834. setup_log_buf(0);
  835. vfs_caches_init_early();
  836. sort_main_extable();
  837. trap_init();
  838. mm_core_init();
  839. poking_init();
  840. ftrace_init();
  841. /* trace_printk can be enabled here */
  842. early_trace_init();
  843. /*
  844. * Set up the scheduler prior starting any interrupts (such as the
  845. * timer interrupt). Full topology setup happens at smp_init()
  846. * time - but meanwhile we still have a functioning scheduler.
  847. */
  848. sched_init();
  849. if (WARN(!irqs_disabled(),
  850. "Interrupts were enabled *very* early, fixing it\n"))
  851. local_irq_disable();
  852. radix_tree_init();
  853. maple_tree_init();
  854. /*
  855. * Set up housekeeping before setting up workqueues to allow the unbound
  856. * workqueue to take non-housekeeping into account.
  857. */
  858. housekeeping_init();
  859. /*
  860. * Allow workqueue creation and work item queueing/cancelling
  861. * early. Work item execution depends on kthreads and starts after
  862. * workqueue_init().
  863. */
  864. workqueue_init_early();
  865. rcu_init();
  866. /* Trace events are available after this */
  867. trace_init();
  868. if (initcall_debug)
  869. initcall_debug_enable();
  870. context_tracking_init();
  871. /* init some links before init_ISA_irqs() */
  872. early_irq_init();
  873. init_IRQ();
  874. tick_init();
  875. rcu_init_nohz();
  876. init_timers();
  877. srcu_init();
  878. hrtimers_init();
  879. softirq_init();
  880. timekeeping_init();
  881. time_init();
  882. /* This must be after timekeeping is initialized */
  883. random_init();
  884. /* These make use of the fully initialized rng */
  885. kfence_init();
  886. boot_init_stack_canary();
  887. perf_event_init();
  888. profile_init();
  889. call_function_init();
  890. WARN(!irqs_disabled(), "Interrupts were enabled early\n");
  891. early_boot_irqs_disabled = false;
  892. local_irq_enable();
  893. kmem_cache_init_late();
  894. /*
  895. * HACK ALERT! This is early. We're enabling the console before
  896. * we've done PCI setups etc, and console_init() must be aware of
  897. * this. But we do want output early, in case something goes wrong.
  898. */
  899. console_init();
  900. if (panic_later)
  901. panic("Too many boot %s vars at `%s'", panic_later,
  902. panic_param);
  903. lockdep_init();
  904. /*
  905. * Need to run this when irqs are enabled, because it wants
  906. * to self-test [hard/soft]-irqs on/off lock inversion bugs
  907. * too:
  908. */
  909. locking_selftest();
  910. #ifdef CONFIG_BLK_DEV_INITRD
  911. if (initrd_start && !initrd_below_start_ok &&
  912. page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
  913. pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n",
  914. page_to_pfn(virt_to_page((void *)initrd_start)),
  915. min_low_pfn);
  916. initrd_start = 0;
  917. }
  918. #endif
  919. setup_per_cpu_pageset();
  920. numa_policy_init();
  921. acpi_early_init();
  922. if (late_time_init)
  923. late_time_init();
  924. sched_clock_init();
  925. calibrate_delay();
  926. arch_cpu_finalize_init();
  927. pid_idr_init();
  928. anon_vma_init();
  929. #ifdef CONFIG_X86
  930. if (efi_enabled(EFI_RUNTIME_SERVICES))
  931. efi_enter_virtual_mode();
  932. #endif
  933. thread_stack_cache_init();
  934. cred_init();
  935. fork_init();
  936. proc_caches_init();
  937. uts_ns_init();
  938. key_init();
  939. security_init();
  940. dbg_late_init();
  941. net_ns_init();
  942. vfs_caches_init();
  943. pagecache_init();
  944. signals_init();
  945. seq_file_init();
  946. proc_root_init();
  947. nsfs_init();
  948. pidfs_init();
  949. cpuset_init();
  950. cgroup_init();
  951. taskstats_init_early();
  952. delayacct_init();
  953. acpi_subsystem_init();
  954. arch_post_acpi_subsys_init();
  955. kcsan_init();
  956. /* Do the rest non-__init'ed, we're now alive */
  957. rest_init();
  958. /*
  959. * Avoid stack canaries in callers of boot_init_stack_canary for gcc-10
  960. * and older.
  961. */
  962. #if !__has_attribute(__no_stack_protector__)
  963. prevent_tail_call_optimization();
  964. #endif
  965. }
  966. /* Call all constructor functions linked into the kernel. */
  967. static void __init do_ctors(void)
  968. {
  969. /*
  970. * For UML, the constructors have already been called by the
  971. * normal setup code as it's just a normal ELF binary, so we
  972. * cannot do it again - but we do need CONFIG_CONSTRUCTORS
  973. * even on UML for modules.
  974. */
  975. #if defined(CONFIG_CONSTRUCTORS) && !defined(CONFIG_UML)
  976. ctor_fn_t *fn = (ctor_fn_t *) __ctors_start;
  977. for (; fn < (ctor_fn_t *) __ctors_end; fn++)
  978. (*fn)();
  979. #endif
  980. }
  981. #ifdef CONFIG_KALLSYMS
  982. struct blacklist_entry {
  983. struct list_head next;
  984. char *buf;
  985. };
  986. static __initdata_or_module LIST_HEAD(blacklisted_initcalls);
  987. static int __init initcall_blacklist(char *str)
  988. {
  989. char *str_entry;
  990. struct blacklist_entry *entry;
  991. /* str argument is a comma-separated list of functions */
  992. do {
  993. str_entry = strsep(&str, ",");
  994. if (str_entry) {
  995. pr_debug("blacklisting initcall %s\n", str_entry);
  996. entry = memblock_alloc(sizeof(*entry),
  997. SMP_CACHE_BYTES);
  998. if (!entry)
  999. panic("%s: Failed to allocate %zu bytes\n",
  1000. __func__, sizeof(*entry));
  1001. entry->buf = memblock_alloc(strlen(str_entry) + 1,
  1002. SMP_CACHE_BYTES);
  1003. if (!entry->buf)
  1004. panic("%s: Failed to allocate %zu bytes\n",
  1005. __func__, strlen(str_entry) + 1);
  1006. strcpy(entry->buf, str_entry);
  1007. list_add(&entry->next, &blacklisted_initcalls);
  1008. }
  1009. } while (str_entry);
  1010. return 1;
  1011. }
  1012. static bool __init_or_module initcall_blacklisted(initcall_t fn)
  1013. {
  1014. struct blacklist_entry *entry;
  1015. char fn_name[KSYM_SYMBOL_LEN];
  1016. unsigned long addr;
  1017. if (list_empty(&blacklisted_initcalls))
  1018. return false;
  1019. addr = (unsigned long) dereference_function_descriptor(fn);
  1020. sprint_symbol_no_offset(fn_name, addr);
  1021. /*
  1022. * fn will be "function_name [module_name]" where [module_name] is not
  1023. * displayed for built-in init functions. Strip off the [module_name].
  1024. */
  1025. strreplace(fn_name, ' ', '\0');
  1026. list_for_each_entry(entry, &blacklisted_initcalls, next) {
  1027. if (!strcmp(fn_name, entry->buf)) {
  1028. pr_debug("initcall %s blacklisted\n", fn_name);
  1029. return true;
  1030. }
  1031. }
  1032. return false;
  1033. }
  1034. #else
  1035. static int __init initcall_blacklist(char *str)
  1036. {
  1037. pr_warn("initcall_blacklist requires CONFIG_KALLSYMS\n");
  1038. return 0;
  1039. }
  1040. static bool __init_or_module initcall_blacklisted(initcall_t fn)
  1041. {
  1042. return false;
  1043. }
  1044. #endif
  1045. __setup("initcall_blacklist=", initcall_blacklist);
  1046. static __init_or_module void
  1047. trace_initcall_start_cb(void *data, initcall_t fn)
  1048. {
  1049. ktime_t *calltime = data;
  1050. printk(KERN_DEBUG "calling %pS @ %i\n", fn, task_pid_nr(current));
  1051. *calltime = ktime_get();
  1052. }
  1053. static __init_or_module void
  1054. trace_initcall_finish_cb(void *data, initcall_t fn, int ret)
  1055. {
  1056. ktime_t rettime, *calltime = data;
  1057. rettime = ktime_get();
  1058. printk(KERN_DEBUG "initcall %pS returned %d after %lld usecs\n",
  1059. fn, ret, (unsigned long long)ktime_us_delta(rettime, *calltime));
  1060. }
  1061. static ktime_t initcall_calltime;
  1062. #ifdef TRACEPOINTS_ENABLED
  1063. static void __init initcall_debug_enable(void)
  1064. {
  1065. int ret;
  1066. ret = register_trace_initcall_start(trace_initcall_start_cb,
  1067. &initcall_calltime);
  1068. ret |= register_trace_initcall_finish(trace_initcall_finish_cb,
  1069. &initcall_calltime);
  1070. WARN(ret, "Failed to register initcall tracepoints\n");
  1071. }
  1072. # define do_trace_initcall_start trace_initcall_start
  1073. # define do_trace_initcall_finish trace_initcall_finish
  1074. #else
  1075. static inline void do_trace_initcall_start(initcall_t fn)
  1076. {
  1077. if (!initcall_debug)
  1078. return;
  1079. trace_initcall_start_cb(&initcall_calltime, fn);
  1080. }
  1081. static inline void do_trace_initcall_finish(initcall_t fn, int ret)
  1082. {
  1083. if (!initcall_debug)
  1084. return;
  1085. trace_initcall_finish_cb(&initcall_calltime, fn, ret);
  1086. }
  1087. #endif /* !TRACEPOINTS_ENABLED */
  1088. int __init_or_module do_one_initcall(initcall_t fn)
  1089. {
  1090. int count = preempt_count();
  1091. char msgbuf[64];
  1092. int ret;
  1093. if (initcall_blacklisted(fn))
  1094. return -EPERM;
  1095. do_trace_initcall_start(fn);
  1096. ret = fn();
  1097. do_trace_initcall_finish(fn, ret);
  1098. msgbuf[0] = 0;
  1099. if (preempt_count() != count) {
  1100. sprintf(msgbuf, "preemption imbalance ");
  1101. preempt_count_set(count);
  1102. }
  1103. if (irqs_disabled()) {
  1104. strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
  1105. local_irq_enable();
  1106. }
  1107. WARN(msgbuf[0], "initcall %pS returned with %s\n", fn, msgbuf);
  1108. add_latent_entropy();
  1109. return ret;
  1110. }
  1111. static initcall_entry_t *initcall_levels[] __initdata = {
  1112. __initcall0_start,
  1113. __initcall1_start,
  1114. __initcall2_start,
  1115. __initcall3_start,
  1116. __initcall4_start,
  1117. __initcall5_start,
  1118. __initcall6_start,
  1119. __initcall7_start,
  1120. __initcall_end,
  1121. };
  1122. /* Keep these in sync with initcalls in include/linux/init.h */
  1123. static const char *initcall_level_names[] __initdata = {
  1124. "pure",
  1125. "core",
  1126. "postcore",
  1127. "arch",
  1128. "subsys",
  1129. "fs",
  1130. "device",
  1131. "late",
  1132. };
  1133. static int __init ignore_unknown_bootoption(char *param, char *val,
  1134. const char *unused, void *arg)
  1135. {
  1136. return 0;
  1137. }
  1138. static void __init do_initcall_level(int level, char *command_line)
  1139. {
  1140. initcall_entry_t *fn;
  1141. parse_args(initcall_level_names[level],
  1142. command_line, __start___param,
  1143. __stop___param - __start___param,
  1144. level, level,
  1145. NULL, ignore_unknown_bootoption);
  1146. trace_initcall_level(initcall_level_names[level]);
  1147. for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++)
  1148. do_one_initcall(initcall_from_entry(fn));
  1149. }
  1150. static void __init do_initcalls(void)
  1151. {
  1152. int level;
  1153. size_t len = saved_command_line_len + 1;
  1154. char *command_line;
  1155. command_line = kzalloc(len, GFP_KERNEL);
  1156. if (!command_line)
  1157. panic("%s: Failed to allocate %zu bytes\n", __func__, len);
  1158. for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++) {
  1159. /* Parser modifies command_line, restore it each time */
  1160. strcpy(command_line, saved_command_line);
  1161. do_initcall_level(level, command_line);
  1162. }
  1163. kfree(command_line);
  1164. }
  1165. /*
  1166. * Ok, the machine is now initialized. None of the devices
  1167. * have been touched yet, but the CPU subsystem is up and
  1168. * running, and memory and process management works.
  1169. *
  1170. * Now we can finally start doing some real work..
  1171. */
  1172. static void __init do_basic_setup(void)
  1173. {
  1174. cpuset_init_smp();
  1175. driver_init();
  1176. init_irq_proc();
  1177. do_ctors();
  1178. do_initcalls();
  1179. }
  1180. static void __init do_pre_smp_initcalls(void)
  1181. {
  1182. initcall_entry_t *fn;
  1183. trace_initcall_level("early");
  1184. for (fn = __initcall_start; fn < __initcall0_start; fn++)
  1185. do_one_initcall(initcall_from_entry(fn));
  1186. }
  1187. static int run_init_process(const char *init_filename)
  1188. {
  1189. const char *const *p;
  1190. argv_init[0] = init_filename;
  1191. pr_info("Run %s as init process\n", init_filename);
  1192. pr_debug(" with arguments:\n");
  1193. for (p = argv_init; *p; p++)
  1194. pr_debug(" %s\n", *p);
  1195. pr_debug(" with environment:\n");
  1196. for (p = envp_init; *p; p++)
  1197. pr_debug(" %s\n", *p);
  1198. return kernel_execve(init_filename, argv_init, envp_init);
  1199. }
  1200. static int try_to_run_init_process(const char *init_filename)
  1201. {
  1202. int ret;
  1203. ret = run_init_process(init_filename);
  1204. if (ret && ret != -ENOENT) {
  1205. pr_err("Starting init: %s exists but couldn't execute it (error %d)\n",
  1206. init_filename, ret);
  1207. }
  1208. return ret;
  1209. }
  1210. static noinline void __init kernel_init_freeable(void);
  1211. #if defined(CONFIG_STRICT_KERNEL_RWX) || defined(CONFIG_STRICT_MODULE_RWX)
  1212. bool rodata_enabled __ro_after_init = true;
  1213. #ifndef arch_parse_debug_rodata
  1214. static inline bool arch_parse_debug_rodata(char *str) { return false; }
  1215. #endif
  1216. static int __init set_debug_rodata(char *str)
  1217. {
  1218. if (arch_parse_debug_rodata(str))
  1219. return 0;
  1220. if (str && !strcmp(str, "on"))
  1221. rodata_enabled = true;
  1222. else if (str && !strcmp(str, "off"))
  1223. rodata_enabled = false;
  1224. else
  1225. pr_warn("Invalid option string for rodata: '%s'\n", str);
  1226. return 0;
  1227. }
  1228. early_param("rodata", set_debug_rodata);
  1229. #endif
  1230. static void mark_readonly(void)
  1231. {
  1232. if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX) && rodata_enabled) {
  1233. /*
  1234. * load_module() results in W+X mappings, which are cleaned
  1235. * up with init_free_wq. Let's make sure that queued work is
  1236. * flushed so that we don't hit false positives looking for
  1237. * insecure pages which are W+X.
  1238. */
  1239. flush_module_init_free_work();
  1240. jump_label_init_ro();
  1241. mark_rodata_ro();
  1242. debug_checkwx();
  1243. rodata_test();
  1244. } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) {
  1245. pr_info("Kernel memory protection disabled.\n");
  1246. } else if (IS_ENABLED(CONFIG_ARCH_HAS_STRICT_KERNEL_RWX)) {
  1247. pr_warn("Kernel memory protection not selected by kernel config.\n");
  1248. } else {
  1249. pr_warn("This architecture does not have kernel memory protection.\n");
  1250. }
  1251. }
  1252. void __weak free_initmem(void)
  1253. {
  1254. free_initmem_default(POISON_FREE_INITMEM);
  1255. }
  1256. static int __ref kernel_init(void *unused)
  1257. {
  1258. int ret;
  1259. /*
  1260. * Wait until kthreadd is all set-up.
  1261. */
  1262. wait_for_completion(&kthreadd_done);
  1263. kernel_init_freeable();
  1264. /* need to finish all async __init code before freeing the memory */
  1265. async_synchronize_full();
  1266. system_state = SYSTEM_FREEING_INITMEM;
  1267. kprobe_free_init_mem();
  1268. ftrace_free_init_mem();
  1269. kgdb_free_init_mem();
  1270. exit_boot_config();
  1271. free_initmem();
  1272. mark_readonly();
  1273. /*
  1274. * Kernel mappings are now finalized - update the userspace page-table
  1275. * to finalize PTI.
  1276. */
  1277. pti_finalize();
  1278. system_state = SYSTEM_RUNNING;
  1279. numa_default_policy();
  1280. rcu_end_inkernel_boot();
  1281. do_sysctl_args();
  1282. if (ramdisk_execute_command) {
  1283. ret = run_init_process(ramdisk_execute_command);
  1284. if (!ret)
  1285. return 0;
  1286. pr_err("Failed to execute %s (error %d)\n",
  1287. ramdisk_execute_command, ret);
  1288. }
  1289. /*
  1290. * We try each of these until one succeeds.
  1291. *
  1292. * The Bourne shell can be used instead of init if we are
  1293. * trying to recover a really broken machine.
  1294. */
  1295. if (execute_command) {
  1296. ret = run_init_process(execute_command);
  1297. if (!ret)
  1298. return 0;
  1299. panic("Requested init %s failed (error %d).",
  1300. execute_command, ret);
  1301. }
  1302. if (CONFIG_DEFAULT_INIT[0] != '\0') {
  1303. ret = run_init_process(CONFIG_DEFAULT_INIT);
  1304. if (ret)
  1305. pr_err("Default init %s failed (error %d)\n",
  1306. CONFIG_DEFAULT_INIT, ret);
  1307. else
  1308. return 0;
  1309. }
  1310. if (!try_to_run_init_process("/sbin/init") ||
  1311. !try_to_run_init_process("/etc/init") ||
  1312. !try_to_run_init_process("/bin/init") ||
  1313. !try_to_run_init_process("/bin/sh"))
  1314. return 0;
  1315. panic("No working init found. Try passing init= option to kernel. "
  1316. "See Linux Documentation/admin-guide/init.rst for guidance.");
  1317. }
  1318. /* Open /dev/console, for stdin/stdout/stderr, this should never fail */
  1319. void __init console_on_rootfs(void)
  1320. {
  1321. struct file *file = filp_open("/dev/console", O_RDWR, 0);
  1322. if (IS_ERR(file)) {
  1323. pr_err("Warning: unable to open an initial console.\n");
  1324. return;
  1325. }
  1326. init_dup(file);
  1327. init_dup(file);
  1328. init_dup(file);
  1329. fput(file);
  1330. }
  1331. static noinline void __init kernel_init_freeable(void)
  1332. {
  1333. /* Now the scheduler is fully set up and can do blocking allocations */
  1334. gfp_allowed_mask = __GFP_BITS_MASK;
  1335. /*
  1336. * init can allocate pages on any node
  1337. */
  1338. set_mems_allowed(node_states[N_MEMORY]);
  1339. cad_pid = get_pid(task_pid(current));
  1340. smp_prepare_cpus(setup_max_cpus);
  1341. workqueue_init();
  1342. init_mm_internals();
  1343. rcu_init_tasks_generic();
  1344. do_pre_smp_initcalls();
  1345. lockup_detector_init();
  1346. smp_init();
  1347. sched_init_smp();
  1348. workqueue_init_topology();
  1349. async_init();
  1350. padata_init();
  1351. page_alloc_init_late();
  1352. do_basic_setup();
  1353. kunit_run_all_tests();
  1354. wait_for_initramfs();
  1355. console_on_rootfs();
  1356. /*
  1357. * check if there is an early userspace init. If yes, let it do all
  1358. * the work
  1359. */
  1360. if (init_eaccess(ramdisk_execute_command) != 0) {
  1361. ramdisk_execute_command = NULL;
  1362. prepare_namespace();
  1363. }
  1364. /*
  1365. * Ok, we have completed the initial bootup, and
  1366. * we're essentially up and running. Get rid of the
  1367. * initmem segments and start the user-mode stuff..
  1368. *
  1369. * rootfs is available now, try loading the public keys
  1370. * and default modules
  1371. */
  1372. integrity_load_keys();
  1373. }