um_arch.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  4. */
  5. #include <linux/cpu.h>
  6. #include <linux/delay.h>
  7. #include <linux/init.h>
  8. #include <linux/mm.h>
  9. #include <linux/ctype.h>
  10. #include <linux/module.h>
  11. #include <linux/panic_notifier.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/string.h>
  14. #include <linux/utsname.h>
  15. #include <linux/sched.h>
  16. #include <linux/sched/task.h>
  17. #include <linux/kmsg_dump.h>
  18. #include <linux/suspend.h>
  19. #include <linux/random.h>
  20. #include <asm/processor.h>
  21. #include <asm/cpufeature.h>
  22. #include <asm/sections.h>
  23. #include <asm/setup.h>
  24. #include <asm/text-patching.h>
  25. #include <as-layout.h>
  26. #include <arch.h>
  27. #include <init.h>
  28. #include <kern.h>
  29. #include <kern_util.h>
  30. #include <mem_user.h>
  31. #include <os.h>
  32. #include "um_arch.h"
  33. #define DEFAULT_COMMAND_LINE_ROOT "root=98:0"
  34. #define DEFAULT_COMMAND_LINE_CONSOLE "console=tty0"
  35. /* Changed in add_arg and setup_arch, which run before SMP is started */
  36. static char __initdata command_line[COMMAND_LINE_SIZE] = { 0 };
  37. static void __init add_arg(char *arg)
  38. {
  39. if (strlen(command_line) + strlen(arg) + 1 > COMMAND_LINE_SIZE) {
  40. os_warn("add_arg: Too many command line arguments!\n");
  41. exit(1);
  42. }
  43. if (strlen(command_line) > 0)
  44. strcat(command_line, " ");
  45. strcat(command_line, arg);
  46. }
  47. /*
  48. * These fields are initialized at boot time and not changed.
  49. * XXX This structure is used only in the non-SMP case. Maybe this
  50. * should be moved to smp.c.
  51. */
  52. struct cpuinfo_um boot_cpu_data = {
  53. .loops_per_jiffy = 0,
  54. .ipi_pipe = { -1, -1 },
  55. .cache_alignment = L1_CACHE_BYTES,
  56. .x86_capability = { 0 }
  57. };
  58. EXPORT_SYMBOL(boot_cpu_data);
  59. union thread_union cpu0_irqstack
  60. __section(".data..init_irqstack") =
  61. { .thread_info = INIT_THREAD_INFO(init_task) };
  62. /* Changed in setup_arch, which is called in early boot */
  63. static char host_info[(__NEW_UTS_LEN + 1) * 5];
  64. static int show_cpuinfo(struct seq_file *m, void *v)
  65. {
  66. int i = 0;
  67. seq_printf(m, "processor\t: %d\n", i);
  68. seq_printf(m, "vendor_id\t: User Mode Linux\n");
  69. seq_printf(m, "model name\t: UML\n");
  70. seq_printf(m, "mode\t\t: skas\n");
  71. seq_printf(m, "host\t\t: %s\n", host_info);
  72. seq_printf(m, "fpu\t\t: %s\n", cpu_has(&boot_cpu_data, X86_FEATURE_FPU) ? "yes" : "no");
  73. seq_printf(m, "flags\t\t:");
  74. for (i = 0; i < 32*NCAPINTS; i++)
  75. if (cpu_has(&boot_cpu_data, i) && (x86_cap_flags[i] != NULL))
  76. seq_printf(m, " %s", x86_cap_flags[i]);
  77. seq_printf(m, "\n");
  78. seq_printf(m, "cache_alignment\t: %d\n", boot_cpu_data.cache_alignment);
  79. seq_printf(m, "bogomips\t: %lu.%02lu\n",
  80. loops_per_jiffy/(500000/HZ),
  81. (loops_per_jiffy/(5000/HZ)) % 100);
  82. return 0;
  83. }
  84. static void *c_start(struct seq_file *m, loff_t *pos)
  85. {
  86. return *pos < nr_cpu_ids ? &boot_cpu_data + *pos : NULL;
  87. }
  88. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  89. {
  90. ++*pos;
  91. return c_start(m, pos);
  92. }
  93. static void c_stop(struct seq_file *m, void *v)
  94. {
  95. }
  96. const struct seq_operations cpuinfo_op = {
  97. .start = c_start,
  98. .next = c_next,
  99. .stop = c_stop,
  100. .show = show_cpuinfo,
  101. };
  102. /* Set in linux_main */
  103. unsigned long uml_physmem;
  104. EXPORT_SYMBOL(uml_physmem);
  105. unsigned long uml_reserved; /* Also modified in mem_init */
  106. unsigned long start_vm;
  107. unsigned long end_vm;
  108. /* Set in early boot */
  109. static int have_root __initdata;
  110. static int have_console __initdata;
  111. /* Set in uml_mem_setup and modified in linux_main */
  112. long long physmem_size = 64 * 1024 * 1024;
  113. EXPORT_SYMBOL(physmem_size);
  114. static const char *usage_string =
  115. "User Mode Linux v%s\n"
  116. " available at http://user-mode-linux.sourceforge.net/\n\n";
  117. static int __init uml_version_setup(char *line, int *add)
  118. {
  119. /* Explicitly use printf() to show version in stdout */
  120. printf("%s\n", init_utsname()->release);
  121. exit(0);
  122. return 0;
  123. }
  124. __uml_setup("--version", uml_version_setup,
  125. "--version\n"
  126. " Prints the version number of the kernel.\n\n"
  127. );
  128. static int __init uml_root_setup(char *line, int *add)
  129. {
  130. have_root = 1;
  131. return 0;
  132. }
  133. __uml_setup("root=", uml_root_setup,
  134. "root=<file containing the root fs>\n"
  135. " This is actually used by the generic kernel in exactly the same\n"
  136. " way as in any other kernel. If you configure a number of block\n"
  137. " devices and want to boot off something other than ubd0, you \n"
  138. " would use something like:\n"
  139. " root=/dev/ubd5\n\n"
  140. );
  141. static int __init no_skas_debug_setup(char *line, int *add)
  142. {
  143. os_warn("'debug' is not necessary to gdb UML in skas mode - run\n");
  144. os_warn("'gdb linux'\n");
  145. return 0;
  146. }
  147. __uml_setup("debug", no_skas_debug_setup,
  148. "debug\n"
  149. " this flag is not needed to run gdb on UML in skas mode\n\n"
  150. );
  151. static int __init uml_console_setup(char *line, int *add)
  152. {
  153. have_console = 1;
  154. return 0;
  155. }
  156. __uml_setup("console=", uml_console_setup,
  157. "console=<preferred console>\n"
  158. " Specify the preferred console output driver\n\n"
  159. );
  160. static int __init Usage(char *line, int *add)
  161. {
  162. const char **p;
  163. printf(usage_string, init_utsname()->release);
  164. p = &__uml_help_start;
  165. /* Explicitly use printf() to show help in stdout */
  166. while (p < &__uml_help_end) {
  167. printf("%s", *p);
  168. p++;
  169. }
  170. exit(0);
  171. return 0;
  172. }
  173. __uml_setup("--help", Usage,
  174. "--help\n"
  175. " Prints this message.\n\n"
  176. );
  177. static void __init uml_checksetup(char *line, int *add)
  178. {
  179. struct uml_param *p;
  180. p = &__uml_setup_start;
  181. while (p < &__uml_setup_end) {
  182. size_t n;
  183. n = strlen(p->str);
  184. if (!strncmp(line, p->str, n) && p->setup_func(line + n, add))
  185. return;
  186. p++;
  187. }
  188. }
  189. static void __init uml_postsetup(void)
  190. {
  191. initcall_t *p;
  192. p = &__uml_postsetup_start;
  193. while (p < &__uml_postsetup_end) {
  194. (*p)();
  195. p++;
  196. }
  197. return;
  198. }
  199. static int panic_exit(struct notifier_block *self, unsigned long unused1,
  200. void *unused2)
  201. {
  202. kmsg_dump(KMSG_DUMP_PANIC);
  203. bust_spinlocks(1);
  204. bust_spinlocks(0);
  205. uml_exitcode = 1;
  206. os_dump_core();
  207. return NOTIFY_DONE;
  208. }
  209. static struct notifier_block panic_exit_notifier = {
  210. .notifier_call = panic_exit,
  211. .priority = INT_MAX - 1, /* run as 2nd notifier, won't return */
  212. };
  213. void uml_finishsetup(void)
  214. {
  215. atomic_notifier_chain_register(&panic_notifier_list,
  216. &panic_exit_notifier);
  217. uml_postsetup();
  218. new_thread_handler();
  219. }
  220. /* Set during early boot */
  221. unsigned long stub_start;
  222. unsigned long task_size;
  223. EXPORT_SYMBOL(task_size);
  224. unsigned long host_task_size;
  225. unsigned long brk_start;
  226. unsigned long end_iomem;
  227. EXPORT_SYMBOL(end_iomem);
  228. #define MIN_VMALLOC (32 * 1024 * 1024)
  229. static void parse_host_cpu_flags(char *line)
  230. {
  231. int i;
  232. for (i = 0; i < 32*NCAPINTS; i++) {
  233. if ((x86_cap_flags[i] != NULL) && strstr(line, x86_cap_flags[i]))
  234. set_cpu_cap(&boot_cpu_data, i);
  235. }
  236. }
  237. static void parse_cache_line(char *line)
  238. {
  239. long res;
  240. char *to_parse = strstr(line, ":");
  241. if (to_parse) {
  242. to_parse++;
  243. while (*to_parse != 0 && isspace(*to_parse)) {
  244. to_parse++;
  245. }
  246. if (kstrtoul(to_parse, 10, &res) == 0 && is_power_of_2(res))
  247. boot_cpu_data.cache_alignment = res;
  248. else
  249. boot_cpu_data.cache_alignment = L1_CACHE_BYTES;
  250. }
  251. }
  252. int __init linux_main(int argc, char **argv)
  253. {
  254. unsigned long avail, diff;
  255. unsigned long virtmem_size, max_physmem;
  256. unsigned long stack;
  257. unsigned int i;
  258. int add;
  259. for (i = 1; i < argc; i++) {
  260. if ((i == 1) && (argv[i][0] == ' '))
  261. continue;
  262. add = 1;
  263. uml_checksetup(argv[i], &add);
  264. if (add)
  265. add_arg(argv[i]);
  266. }
  267. if (have_root == 0)
  268. add_arg(DEFAULT_COMMAND_LINE_ROOT);
  269. if (have_console == 0)
  270. add_arg(DEFAULT_COMMAND_LINE_CONSOLE);
  271. host_task_size = os_get_top_address();
  272. /* reserve a few pages for the stubs (taking care of data alignment) */
  273. /* align the data portion */
  274. BUILD_BUG_ON(!is_power_of_2(STUB_DATA_PAGES));
  275. stub_start = (host_task_size - 1) & ~(STUB_DATA_PAGES * PAGE_SIZE - 1);
  276. /* another page for the code portion */
  277. stub_start -= PAGE_SIZE;
  278. host_task_size = stub_start;
  279. /*
  280. * TASK_SIZE needs to be PGDIR_SIZE aligned or else exit_mmap craps
  281. * out
  282. */
  283. task_size = host_task_size & PGDIR_MASK;
  284. /* OS sanity checks that need to happen before the kernel runs */
  285. os_early_checks();
  286. get_host_cpu_features(parse_host_cpu_flags, parse_cache_line);
  287. brk_start = (unsigned long) sbrk(0);
  288. /*
  289. * Increase physical memory size for exec-shield users
  290. * so they actually get what they asked for. This should
  291. * add zero for non-exec shield users
  292. */
  293. diff = UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
  294. if (diff > 1024 * 1024) {
  295. os_info("Adding %ld bytes to physical memory to account for "
  296. "exec-shield gap\n", diff);
  297. physmem_size += UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
  298. }
  299. uml_physmem = (unsigned long) __binary_start & PAGE_MASK;
  300. /* Reserve up to 4M after the current brk */
  301. uml_reserved = ROUND_4M(brk_start) + (1 << 22);
  302. setup_machinename(init_utsname()->machine);
  303. highmem = 0;
  304. iomem_size = (iomem_size + PAGE_SIZE - 1) & PAGE_MASK;
  305. max_physmem = TASK_SIZE - uml_physmem - iomem_size - MIN_VMALLOC;
  306. /*
  307. * Zones have to begin on a 1 << MAX_PAGE_ORDER page boundary,
  308. * so this makes sure that's true for highmem
  309. */
  310. max_physmem &= ~((1 << (PAGE_SHIFT + MAX_PAGE_ORDER)) - 1);
  311. if (physmem_size + iomem_size > max_physmem) {
  312. highmem = physmem_size + iomem_size - max_physmem;
  313. physmem_size -= highmem;
  314. }
  315. high_physmem = uml_physmem + physmem_size;
  316. end_iomem = high_physmem + iomem_size;
  317. high_memory = (void *) end_iomem;
  318. start_vm = VMALLOC_START;
  319. virtmem_size = physmem_size;
  320. stack = (unsigned long) argv;
  321. stack &= ~(1024 * 1024 - 1);
  322. avail = stack - start_vm;
  323. if (physmem_size > avail)
  324. virtmem_size = avail;
  325. end_vm = start_vm + virtmem_size;
  326. if (virtmem_size < physmem_size)
  327. os_info("Kernel virtual memory size shrunk to %lu bytes\n",
  328. virtmem_size);
  329. os_flush_stdout();
  330. return start_uml();
  331. }
  332. int __init __weak read_initrd(void)
  333. {
  334. return 0;
  335. }
  336. void __init setup_arch(char **cmdline_p)
  337. {
  338. u8 rng_seed[32];
  339. stack_protections((unsigned long) &init_thread_info);
  340. setup_physmem(uml_physmem, uml_reserved, physmem_size, highmem);
  341. mem_total_pages(physmem_size, iomem_size, highmem);
  342. uml_dtb_init();
  343. read_initrd();
  344. paging_init();
  345. strscpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
  346. *cmdline_p = command_line;
  347. setup_hostinfo(host_info, sizeof host_info);
  348. if (os_getrandom(rng_seed, sizeof(rng_seed), 0) == sizeof(rng_seed)) {
  349. add_bootloader_randomness(rng_seed, sizeof(rng_seed));
  350. memzero_explicit(rng_seed, sizeof(rng_seed));
  351. }
  352. }
  353. void __init arch_cpu_finalize_init(void)
  354. {
  355. arch_check_bugs();
  356. os_check_bugs();
  357. }
  358. void apply_seal_endbr(s32 *start, s32 *end)
  359. {
  360. }
  361. void apply_retpolines(s32 *start, s32 *end)
  362. {
  363. }
  364. void apply_returns(s32 *start, s32 *end)
  365. {
  366. }
  367. void apply_fineibt(s32 *start_retpoline, s32 *end_retpoline,
  368. s32 *start_cfi, s32 *end_cfi)
  369. {
  370. }
  371. void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
  372. {
  373. }
  374. void *text_poke(void *addr, const void *opcode, size_t len)
  375. {
  376. /*
  377. * In UML, the only reference to this function is in
  378. * apply_relocate_add(), which shouldn't ever actually call this
  379. * because UML doesn't have live patching.
  380. */
  381. WARN_ON(1);
  382. return memcpy(addr, opcode, len);
  383. }
  384. void text_poke_sync(void)
  385. {
  386. }
  387. void uml_pm_wake(void)
  388. {
  389. pm_system_wakeup();
  390. }
  391. #ifdef CONFIG_PM_SLEEP
  392. static int um_suspend_valid(suspend_state_t state)
  393. {
  394. return state == PM_SUSPEND_MEM;
  395. }
  396. static int um_suspend_prepare(void)
  397. {
  398. um_irqs_suspend();
  399. return 0;
  400. }
  401. static int um_suspend_enter(suspend_state_t state)
  402. {
  403. if (WARN_ON(state != PM_SUSPEND_MEM))
  404. return -EINVAL;
  405. /*
  406. * This is identical to the idle sleep, but we've just
  407. * (during suspend) turned off all interrupt sources
  408. * except for the ones we want, so now we can only wake
  409. * up on something we actually want to wake up on. All
  410. * timing has also been suspended.
  411. */
  412. um_idle_sleep();
  413. return 0;
  414. }
  415. static void um_suspend_finish(void)
  416. {
  417. um_irqs_resume();
  418. }
  419. const struct platform_suspend_ops um_suspend_ops = {
  420. .valid = um_suspend_valid,
  421. .prepare = um_suspend_prepare,
  422. .enter = um_suspend_enter,
  423. .finish = um_suspend_finish,
  424. };
  425. static int init_pm_wake_signal(void)
  426. {
  427. /*
  428. * In external time-travel mode we can't use signals to wake up
  429. * since that would mess with the scheduling. We'll have to do
  430. * some additional work to support wakeup on virtio devices or
  431. * similar, perhaps implementing a fake RTC controller that can
  432. * trigger wakeup (and request the appropriate scheduling from
  433. * the external scheduler when going to suspend.)
  434. */
  435. if (time_travel_mode != TT_MODE_EXTERNAL)
  436. register_pm_wake_signal();
  437. suspend_set_ops(&um_suspend_ops);
  438. return 0;
  439. }
  440. late_initcall(init_pm_wake_signal);
  441. #endif