kjlau0112

0001-Stargazer-OS-Kernel-Enablement.patch

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