main.c 40 KB

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