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