1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 4 */ 5 6 #include <linux/cpu.h> 7 #include <linux/delay.h> 8 #include <linux/init.h> 9 #include <linux/mm.h> 10 #include <linux/ctype.h> 11 #include <linux/module.h> 12 #include <linux/panic_notifier.h> 13 #include <linux/seq_file.h> 14 #include <linux/string.h> 15 #include <linux/string_choices.h> 16 #include <linux/utsname.h> 17 #include <linux/sched.h> 18 #include <linux/sched/task.h> 19 #include <linux/kmsg_dump.h> 20 #include <linux/suspend.h> 21 #include <linux/random.h> 22 #include <linux/smp-internal.h> 23 24 #include <asm/processor.h> 25 #include <asm/cpufeature.h> 26 #include <asm/sections.h> 27 #include <asm/setup.h> 28 #include <asm/text-patching.h> 29 #include <as-layout.h> 30 #include <arch.h> 31 #include <init.h> 32 #include <kern.h> 33 #include <kern_util.h> 34 #include <mem_user.h> 35 #include <os.h> 36 37 #include "um_arch.h" 38 39 #define DEFAULT_COMMAND_LINE_ROOT "root=98:0" 40 #define DEFAULT_COMMAND_LINE_CONSOLE "console=tty0" 41 42 /* Changed in add_arg and setup_arch, which run before SMP is started */ 43 static char __initdata command_line[COMMAND_LINE_SIZE] = { 0 }; 44 45 static void __init add_arg(char *arg) 46 { 47 size_t len = strlen(command_line) + strlen(arg) + 1; 48 49 if (command_line[0]) 50 len++; 51 52 if (len > COMMAND_LINE_SIZE) { 53 os_warn("add_arg: Too many command line arguments!\n"); 54 exit(1); 55 } 56 if (strlen(command_line) > 0) 57 strcat(command_line, " "); 58 strcat(command_line, arg); 59 } 60 61 /* 62 * These fields are initialized at boot time and not changed. 63 */ 64 struct cpuinfo_um boot_cpu_data = { 65 .loops_per_jiffy = 0, 66 .cache_alignment = L1_CACHE_BYTES, 67 .x86_capability = { 0 } 68 }; 69 70 EXPORT_SYMBOL(boot_cpu_data); 71 72 73 /* Changed in setup_arch, which is called in early boot */ 74 static char host_info[(__NEW_UTS_LEN + 1) * 5]; 75 76 static int show_cpuinfo(struct seq_file *m, void *v) 77 { 78 int i = 0; 79 80 #if IS_ENABLED(CONFIG_SMP) 81 i = (uintptr_t) v - 1; 82 if (!cpu_online(i)) 83 return 0; 84 #endif 85 86 seq_printf(m, "processor\t: %d\n", i); 87 seq_printf(m, "vendor_id\t: User Mode Linux\n"); 88 seq_printf(m, "model name\t: UML\n"); 89 seq_printf(m, "mode\t\t: skas\n"); 90 seq_printf(m, "host\t\t: %s\n", host_info); 91 seq_printf(m, "fpu\t\t: %s\n", str_yes_no(cpu_has(&boot_cpu_data, X86_FEATURE_FPU))); 92 seq_printf(m, "flags\t\t:"); 93 for (i = 0; i < 32*NCAPINTS; i++) 94 if (cpu_has(&boot_cpu_data, i) && (x86_cap_flags[i] != NULL)) 95 seq_printf(m, " %s", x86_cap_flags[i]); 96 seq_printf(m, "\n"); 97 seq_printf(m, "cache_alignment\t: %d\n", boot_cpu_data.cache_alignment); 98 seq_printf(m, "bogomips\t: %lu.%02lu\n", 99 loops_per_jiffy/(500000/HZ), 100 (loops_per_jiffy/(5000/HZ)) % 100); 101 102 return 0; 103 } 104 105 static void *c_start(struct seq_file *m, loff_t *pos) 106 { 107 if (*pos < nr_cpu_ids) 108 return (void *)(uintptr_t)(*pos + 1); 109 return NULL; 110 } 111 112 static void *c_next(struct seq_file *m, void *v, loff_t *pos) 113 { 114 ++*pos; 115 return c_start(m, pos); 116 } 117 118 static void c_stop(struct seq_file *m, void *v) 119 { 120 } 121 122 const struct seq_operations cpuinfo_op = { 123 .start = c_start, 124 .next = c_next, 125 .stop = c_stop, 126 .show = show_cpuinfo, 127 }; 128 129 /* Set in linux_main */ 130 unsigned long uml_physmem; 131 EXPORT_SYMBOL(uml_physmem); 132 133 unsigned long uml_reserved; /* Also modified in mem_init */ 134 unsigned long start_vm; 135 unsigned long end_vm; 136 137 /* Set in early boot */ 138 static int have_root __initdata; 139 static int have_console __initdata; 140 141 /* Set in uml_mem_setup and modified in linux_main */ 142 unsigned long long physmem_size = 64 * 1024 * 1024; 143 EXPORT_SYMBOL(physmem_size); 144 145 static const char *usage_string = 146 "User Mode Linux v%s\n" 147 " available at http://user-mode-linux.sourceforge.net/\n\n"; 148 149 static int __init uml_version_setup(char *line, int *add) 150 { 151 /* Explicitly use printf() to show version in stdout */ 152 printf("%s\n", init_utsname()->release); 153 exit(0); 154 155 return 0; 156 } 157 158 __uml_setup("--version", uml_version_setup, 159 "--version\n" 160 " Prints the version number of the kernel.\n\n" 161 ); 162 163 static int __init uml_root_setup(char *line, int *add) 164 { 165 have_root = 1; 166 return 0; 167 } 168 169 __uml_setup("root=", uml_root_setup, 170 "root=<file containing the root fs>\n" 171 " This is actually used by the generic kernel in exactly the same\n" 172 " way as in any other kernel. If you configure a number of block\n" 173 " devices and want to boot off something other than ubd0, you \n" 174 " would use something like:\n" 175 " root=/dev/ubd5\n\n" 176 ); 177 178 static int __init uml_console_setup(char *line, int *add) 179 { 180 have_console = 1; 181 return 0; 182 } 183 184 __uml_setup("console=", uml_console_setup, 185 "console=<preferred console>\n" 186 " Specify the preferred console output driver\n\n" 187 ); 188 189 static int __init Usage(char *line, int *add) 190 { 191 const char **p; 192 193 printf(usage_string, init_utsname()->release); 194 p = &__uml_help_start; 195 /* Explicitly use printf() to show help in stdout */ 196 while (p < &__uml_help_end) { 197 printf("%s", *p); 198 p++; 199 } 200 exit(0); 201 return 0; 202 } 203 204 __uml_setup("--help", Usage, 205 "--help\n" 206 " Prints this message.\n\n" 207 ); 208 209 static void __init uml_checksetup(char *line, int *add) 210 { 211 struct uml_param *p; 212 213 p = &__uml_setup_start; 214 while (p < &__uml_setup_end) { 215 size_t n; 216 217 n = strlen(p->str); 218 if (!strncmp(line, p->str, n) && p->setup_func(line + n, add)) 219 return; 220 p++; 221 } 222 } 223 224 static void __init uml_postsetup(void) 225 { 226 initcall_t *p; 227 228 p = &__uml_postsetup_start; 229 while (p < &__uml_postsetup_end) { 230 (*p)(); 231 p++; 232 } 233 return; 234 } 235 236 static int panic_exit(struct notifier_block *self, unsigned long unused1, 237 void *unused2) 238 { 239 kmsg_dump(KMSG_DUMP_PANIC); 240 bust_spinlocks(1); 241 bust_spinlocks(0); 242 uml_exitcode = 1; 243 os_dump_core(); 244 245 return NOTIFY_DONE; 246 } 247 248 static struct notifier_block panic_exit_notifier = { 249 .notifier_call = panic_exit, 250 .priority = INT_MAX - 1, /* run as 2nd notifier, won't return */ 251 }; 252 253 void uml_finishsetup(void) 254 { 255 atomic_notifier_chain_register(&panic_notifier_list, 256 &panic_exit_notifier); 257 258 uml_postsetup(); 259 260 new_thread_handler(); 261 } 262 263 /* Set during early boot */ 264 unsigned long stub_start; 265 unsigned long task_size; 266 EXPORT_SYMBOL(task_size); 267 268 unsigned long brk_start; 269 270 #define MIN_VMALLOC (32 * 1024 * 1024) 271 272 static u64 __init read_xcr0(void) 273 { 274 u32 a, b, c, d; 275 276 asm volatile("cpuid" 277 : "=a"(a), "=b"(b), "=c"(c), "=d"(d) 278 : "a"(0), "c"(0)); 279 if (a >= 1) { /* max_leaf >= 1 */ 280 asm volatile("cpuid" 281 : "=a"(a), "=b"(b), "=c"(c), "=d"(d) 282 : "a"(1), "c"(0)); 283 if (c & (1 << 27)) { /* XSAVE enabled by OS */ 284 asm volatile("xgetbv" : "=d"(d), "=a"(a) : "c"(0)); 285 return ((u64)d << 32) | a; 286 } 287 } 288 return 0; 289 } 290 291 static void __init validate_and_set_cpu_cap(int cap, u64 xcr0) 292 { 293 /* 294 * Check for missing xstate features right away, so that there's no 295 * perceived need for all optimized code in the kernel to do so. 296 */ 297 switch (cap) { 298 case X86_FEATURE_AVX: 299 case X86_FEATURE_AVX2: 300 case X86_FEATURE_AVX_VNNI: 301 case X86_FEATURE_FMA: 302 case X86_FEATURE_VAES: 303 case X86_FEATURE_VPCLMULQDQ: 304 if ((xcr0 & 0x7) != 0x7) { 305 static bool warned; 306 307 if (!warned) { 308 os_warn("Disabling AVX support due to missing xstate features\n"); 309 warned = true; 310 } 311 return; 312 } 313 break; 314 case X86_FEATURE_AVX512F: 315 case X86_FEATURE_AVX512BW: 316 case X86_FEATURE_AVX512CD: 317 case X86_FEATURE_AVX512DQ: 318 case X86_FEATURE_AVX512ER: 319 case X86_FEATURE_AVX512IFMA: 320 case X86_FEATURE_AVX512PF: 321 case X86_FEATURE_AVX512VBMI: 322 case X86_FEATURE_AVX512VL: 323 case X86_FEATURE_AVX512_4FMAPS: 324 case X86_FEATURE_AVX512_4VNNIW: 325 case X86_FEATURE_AVX512_BF16: 326 case X86_FEATURE_AVX512_BITALG: 327 case X86_FEATURE_AVX512_FP16: 328 case X86_FEATURE_AVX512_VBMI2: 329 case X86_FEATURE_AVX512_VNNI: 330 case X86_FEATURE_AVX512_VP2INTERSECT: 331 case X86_FEATURE_AVX512_VPOPCNTDQ: 332 if ((xcr0 & 0xe7) != 0xe7) { 333 static bool warned; 334 335 if (!warned) { 336 os_warn("Disabling AVX-512 support due to missing xstate features\n"); 337 warned = true; 338 } 339 return; 340 } 341 break; 342 } 343 set_cpu_cap(&boot_cpu_data, cap); 344 } 345 346 static void __init parse_host_cpu_flags(char *line) 347 { 348 u64 xcr0 = read_xcr0(); 349 int i; 350 351 for (i = 0; i < 32*NCAPINTS; i++) { 352 if ((x86_cap_flags[i] != NULL) && strstr(line, x86_cap_flags[i])) 353 validate_and_set_cpu_cap(i, xcr0); 354 } 355 } 356 357 static void __init parse_cache_line(char *line) 358 { 359 long res; 360 char *to_parse = strstr(line, ":"); 361 if (to_parse) { 362 to_parse++; 363 while (*to_parse != 0 && isspace(*to_parse)) { 364 to_parse++; 365 } 366 if (kstrtoul(to_parse, 10, &res) == 0 && is_power_of_2(res)) 367 boot_cpu_data.cache_alignment = res; 368 else 369 boot_cpu_data.cache_alignment = L1_CACHE_BYTES; 370 } 371 } 372 373 static unsigned long __init get_top_address(char **envp) 374 { 375 unsigned long top_addr = (unsigned long) &top_addr; 376 int i; 377 378 /* The earliest variable should be after the program name in ELF */ 379 for (i = 0; envp[i]; i++) { 380 if ((unsigned long) envp[i] > top_addr) 381 top_addr = (unsigned long) envp[i]; 382 } 383 384 return PAGE_ALIGN(top_addr + 1); 385 } 386 387 void __init linux_main(int argc, char **argv, char **envp) 388 { 389 unsigned long avail, diff; 390 unsigned long virtmem_size, max_physmem; 391 unsigned long host_task_size; 392 unsigned long stack; 393 unsigned int i; 394 int add; 395 396 for (i = 1; i < argc; i++) { 397 if ((i == 1) && (argv[i][0] == ' ')) 398 continue; 399 add = 1; 400 uml_checksetup(argv[i], &add); 401 if (add) 402 add_arg(argv[i]); 403 } 404 if (have_root == 0) 405 add_arg(DEFAULT_COMMAND_LINE_ROOT); 406 407 if (have_console == 0) 408 add_arg(DEFAULT_COMMAND_LINE_CONSOLE); 409 410 host_task_size = get_top_address(envp); 411 /* reserve a few pages for the stubs */ 412 stub_start = host_task_size - STUB_SIZE; 413 host_task_size = stub_start; 414 415 /* Limit TASK_SIZE to what is addressable by the page table */ 416 task_size = host_task_size; 417 if (task_size > (unsigned long long) PTRS_PER_PGD * PGDIR_SIZE) 418 task_size = PTRS_PER_PGD * PGDIR_SIZE; 419 420 /* 421 * TASK_SIZE needs to be PGDIR_SIZE aligned or else exit_mmap craps 422 * out 423 */ 424 task_size = task_size & PGDIR_MASK; 425 426 /* OS sanity checks that need to happen before the kernel runs */ 427 os_early_checks(); 428 429 get_host_cpu_features(parse_host_cpu_flags, parse_cache_line); 430 431 brk_start = (unsigned long) sbrk(0); 432 433 /* 434 * Increase physical memory size for exec-shield users 435 * so they actually get what they asked for. This should 436 * add zero for non-exec shield users 437 */ 438 diff = PAGE_ALIGN(brk_start) - PAGE_ALIGN((unsigned long) &_end); 439 if (diff > 1024 * 1024) { 440 os_info("Adding %ld bytes to physical memory to account for " 441 "exec-shield gap\n", diff); 442 physmem_size += diff; 443 } 444 445 uml_physmem = (unsigned long) __binary_start & PAGE_MASK; 446 447 /* Reserve up to 4M after the current brk */ 448 uml_reserved = ROUND_4M(brk_start) + (1 << 22); 449 450 setup_machinename(init_utsname()->machine); 451 452 physmem_size = PAGE_ALIGN(physmem_size); 453 max_physmem = TASK_SIZE - uml_physmem - MIN_VMALLOC; 454 if (physmem_size > max_physmem) { 455 physmem_size = max_physmem; 456 os_info("Physical memory size shrunk to %llu bytes\n", 457 physmem_size); 458 } 459 460 high_physmem = uml_physmem + physmem_size; 461 462 start_vm = VMALLOC_START; 463 464 virtmem_size = physmem_size; 465 stack = (unsigned long) argv; 466 stack &= ~(1024 * 1024 - 1); 467 avail = stack - start_vm; 468 if (physmem_size > avail) 469 virtmem_size = avail; 470 end_vm = start_vm + virtmem_size; 471 472 if (virtmem_size < physmem_size) 473 os_info("Kernel virtual memory size shrunk to %lu bytes\n", 474 virtmem_size); 475 476 arch_task_struct_size = sizeof(struct task_struct) + host_fp_size; 477 478 os_flush_stdout(); 479 480 start_uml(); 481 } 482 483 int __init __weak read_initrd(void) 484 { 485 return 0; 486 } 487 488 void __init setup_arch(char **cmdline_p) 489 { 490 u8 rng_seed[32]; 491 492 stack_protections((unsigned long) init_task.stack); 493 setup_physmem(uml_physmem, uml_reserved, physmem_size); 494 uml_dtb_init(); 495 read_initrd(); 496 497 strscpy(boot_command_line, command_line, COMMAND_LINE_SIZE); 498 *cmdline_p = command_line; 499 setup_hostinfo(host_info, sizeof host_info); 500 prefill_possible_map(); 501 502 if (os_getrandom(rng_seed, sizeof(rng_seed), 0) == sizeof(rng_seed)) { 503 add_bootloader_randomness(rng_seed, sizeof(rng_seed)); 504 memzero_explicit(rng_seed, sizeof(rng_seed)); 505 } 506 } 507 508 void __init arch_cpu_finalize_init(void) 509 { 510 arch_check_bugs(); 511 os_check_bugs(); 512 } 513 514 void apply_seal_endbr(s32 *start, s32 *end) 515 { 516 } 517 518 void apply_retpolines(s32 *start, s32 *end) 519 { 520 } 521 522 void apply_returns(s32 *start, s32 *end) 523 { 524 } 525 526 void apply_fineibt(s32 *start_retpoline, s32 *end_retpoline, 527 s32 *start_cfi, s32 *end_cfi) 528 { 529 } 530 531 void apply_alternatives(struct alt_instr *start, struct alt_instr *end) 532 { 533 } 534 535 void *text_poke(void *addr, const void *opcode, size_t len) 536 { 537 /* 538 * In UML, the only reference to this function is in 539 * apply_relocate_add(), which shouldn't ever actually call this 540 * because UML doesn't have live patching. 541 */ 542 WARN_ON(1); 543 544 return memcpy(addr, opcode, len); 545 } 546 547 void *text_poke_copy(void *addr, const void *opcode, size_t len) 548 { 549 return text_poke(addr, opcode, len); 550 } 551 552 void smp_text_poke_sync_each_cpu(void) 553 { 554 } 555 556 void uml_pm_wake(void) 557 { 558 pm_system_wakeup(); 559 } 560 561 #ifdef CONFIG_PM_SLEEP 562 static int um_suspend_valid(suspend_state_t state) 563 { 564 return state == PM_SUSPEND_MEM; 565 } 566 567 static int um_suspend_prepare(void) 568 { 569 um_irqs_suspend(); 570 return 0; 571 } 572 573 static int um_suspend_enter(suspend_state_t state) 574 { 575 if (WARN_ON(state != PM_SUSPEND_MEM)) 576 return -EINVAL; 577 578 /* 579 * This is identical to the idle sleep, but we've just 580 * (during suspend) turned off all interrupt sources 581 * except for the ones we want, so now we can only wake 582 * up on something we actually want to wake up on. All 583 * timing has also been suspended. 584 */ 585 um_idle_sleep(); 586 return 0; 587 } 588 589 static void um_suspend_finish(void) 590 { 591 um_irqs_resume(); 592 } 593 594 const struct platform_suspend_ops um_suspend_ops = { 595 .valid = um_suspend_valid, 596 .prepare = um_suspend_prepare, 597 .enter = um_suspend_enter, 598 .finish = um_suspend_finish, 599 }; 600 601 static int init_pm_wake_signal(void) 602 { 603 /* 604 * In external time-travel mode we can't use signals to wake up 605 * since that would mess with the scheduling. We'll have to do 606 * some additional work to support wakeup on virtio devices or 607 * similar, perhaps implementing a fake RTC controller that can 608 * trigger wakeup (and request the appropriate scheduling from 609 * the external scheduler when going to suspend.) 610 */ 611 if (time_travel_mode != TT_MODE_EXTERNAL) 612 register_pm_wake_signal(); 613 614 suspend_set_ops(&um_suspend_ops); 615 616 return 0; 617 } 618 619 late_initcall(init_pm_wake_signal); 620 #endif 621