main.c 42 KB

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