main.c 37 KB

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