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