1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
7 #include <linux/delay.h>
8 #include <linux/init.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/utsname.h>
16 #include <linux/sched.h>
17 #include <linux/sched/task.h>
18 #include <linux/kmsg_dump.h>
19 #include <linux/suspend.h>
20 #include <linux/random.h>
22 #include <asm/processor.h>
23 #include <asm/cpufeature.h>
24 #include <asm/sections.h>
25 #include <asm/setup.h>
26 #include <asm/text-patching.h>
27 #include <as-layout.h>
31 #include <kern_util.h>
37 #define DEFAULT_COMMAND_LINE_ROOT "root=98:0"
38 #define DEFAULT_COMMAND_LINE_CONSOLE "console=tty0"
40 /* Changed in add_arg and setup_arch, which run before SMP is started */
41 static char __initdata command_line[COMMAND_LINE_SIZE] = { 0 };
43 static void __init add_arg(char *arg)
45 if (strlen(command_line) + strlen(arg) + 1 > COMMAND_LINE_SIZE) {
46 os_warn("add_arg: Too many command line arguments!\n");
49 if (strlen(command_line) > 0)
50 strcat(command_line, " ");
51 strcat(command_line, arg);
55 * These fields are initialized at boot time and not changed.
56 * XXX This structure is used only in the non-SMP case. Maybe this
57 * should be moved to smp.c.
59 struct cpuinfo_um boot_cpu_data = {
61 .ipi_pipe = { -1, -1 },
62 .cache_alignment = L1_CACHE_BYTES,
63 .x86_capability = { 0 }
66 EXPORT_SYMBOL(boot_cpu_data);
69 /* Changed in setup_arch, which is called in early boot */
70 static char host_info[(__NEW_UTS_LEN + 1) * 5];
72 static int show_cpuinfo(struct seq_file *m, void *v)
76 seq_printf(m, "processor\t: %d\n", i);
77 seq_printf(m, "vendor_id\t: User Mode Linux\n");
78 seq_printf(m, "model name\t: UML\n");
79 seq_printf(m, "mode\t\t: skas\n");
80 seq_printf(m, "host\t\t: %s\n", host_info);
81 seq_printf(m, "fpu\t\t: %s\n", cpu_has(&boot_cpu_data, X86_FEATURE_FPU) ? "yes" : "no");
82 seq_printf(m, "flags\t\t:");
83 for (i = 0; i < 32*NCAPINTS; i++)
84 if (cpu_has(&boot_cpu_data, i) && (x86_cap_flags[i] != NULL))
85 seq_printf(m, " %s", x86_cap_flags[i]);
87 seq_printf(m, "cache_alignment\t: %d\n", boot_cpu_data.cache_alignment);
88 seq_printf(m, "bogomips\t: %lu.%02lu\n",
89 loops_per_jiffy/(500000/HZ),
90 (loops_per_jiffy/(5000/HZ)) % 100);
96 static void *c_start(struct seq_file *m, loff_t *pos)
98 return *pos < nr_cpu_ids ? &boot_cpu_data + *pos : NULL;
101 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
104 return c_start(m, pos);
107 static void c_stop(struct seq_file *m, void *v)
111 const struct seq_operations cpuinfo_op = {
115 .show = show_cpuinfo,
118 /* Set in linux_main */
119 unsigned long uml_physmem;
120 EXPORT_SYMBOL(uml_physmem);
122 unsigned long uml_reserved; /* Also modified in mem_init */
123 unsigned long start_vm;
124 unsigned long end_vm;
126 /* Set in early boot */
127 static int have_root __initdata;
128 static int have_console __initdata;
130 /* Set in uml_mem_setup and modified in linux_main */
131 unsigned long long physmem_size = 64 * 1024 * 1024;
132 EXPORT_SYMBOL(physmem_size);
134 static const char *usage_string =
135 "User Mode Linux v%s\n"
136 " available at http://user-mode-linux.sourceforge.net/\n\n";
138 static int __init uml_version_setup(char *line, int *add)
140 /* Explicitly use printf() to show version in stdout */
141 printf("%s\n", init_utsname()->release);
147 __uml_setup("--version", uml_version_setup,
149 " Prints the version number of the kernel.\n\n"
152 static int __init uml_root_setup(char *line, int *add)
158 __uml_setup("root=", uml_root_setup,
159 "root=<file containing the root fs>\n"
160 " This is actually used by the generic kernel in exactly the same\n"
161 " way as in any other kernel. If you configure a number of block\n"
162 " devices and want to boot off something other than ubd0, you \n"
163 " would use something like:\n"
164 " root=/dev/ubd5\n\n"
167 static int __init uml_console_setup(char *line, int *add)
173 __uml_setup("console=", uml_console_setup,
174 "console=<preferred console>\n"
175 " Specify the preferred console output driver\n\n"
178 static int __init Usage(char *line, int *add)
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) {
193 __uml_setup("--help", Usage,
195 " Prints this message.\n\n"
198 static void __init uml_checksetup(char *line, int *add)
202 p = &__uml_setup_start;
203 while (p < &__uml_setup_end) {
207 if (!strncmp(line, p->str, n) && p->setup_func(line + n, add))
213 static void __init uml_postsetup(void)
217 p = &__uml_postsetup_start;
218 while (p < &__uml_postsetup_end) {
225 static int panic_exit(struct notifier_block *self, unsigned long unused1,
228 kmsg_dump(KMSG_DUMP_PANIC);
237 static struct notifier_block panic_exit_notifier = {
238 .notifier_call = panic_exit,
239 .priority = INT_MAX - 1, /* run as 2nd notifier, won't return */
242 void uml_finishsetup(void)
244 cpu_tasks[0] = &init_task;
246 atomic_notifier_chain_register(&panic_notifier_list,
247 &panic_exit_notifier);
251 new_thread_handler();
254 /* Set during early boot */
255 unsigned long stub_start;
256 unsigned long task_size;
257 EXPORT_SYMBOL(task_size);
259 unsigned long host_task_size;
261 unsigned long brk_start;
262 unsigned long end_iomem;
263 EXPORT_SYMBOL(end_iomem);
265 #define MIN_VMALLOC (32 * 1024 * 1024)
267 static void __init parse_host_cpu_flags(char *line)
270 for (i = 0; i < 32*NCAPINTS; i++) {
271 if ((x86_cap_flags[i] != NULL) && strstr(line, x86_cap_flags[i]))
272 set_cpu_cap(&boot_cpu_data, i);
276 static void __init parse_cache_line(char *line)
279 char *to_parse = strstr(line, ":");
282 while (*to_parse != 0 && isspace(*to_parse)) {
285 if (kstrtoul(to_parse, 10, &res) == 0 && is_power_of_2(res))
286 boot_cpu_data.cache_alignment = res;
288 boot_cpu_data.cache_alignment = L1_CACHE_BYTES;
292 static unsigned long __init get_top_address(char **envp)
294 unsigned long top_addr = (unsigned long) &top_addr;
297 /* The earliest variable should be after the program name in ELF */
298 for (i = 0; envp[i]; i++) {
299 if ((unsigned long) envp[i] > top_addr)
300 top_addr = (unsigned long) envp[i];
303 top_addr &= ~(UM_KERN_PAGE_SIZE - 1);
304 top_addr += UM_KERN_PAGE_SIZE;
309 int __init linux_main(int argc, char **argv, char **envp)
311 unsigned long avail, diff;
312 unsigned long virtmem_size, max_physmem;
317 for (i = 1; i < argc; i++) {
318 if ((i == 1) && (argv[i][0] == ' '))
321 uml_checksetup(argv[i], &add);
326 add_arg(DEFAULT_COMMAND_LINE_ROOT);
328 if (have_console == 0)
329 add_arg(DEFAULT_COMMAND_LINE_CONSOLE);
331 host_task_size = get_top_address(envp);
332 /* reserve a few pages for the stubs */
333 stub_start = host_task_size - STUB_DATA_PAGES * PAGE_SIZE;
334 /* another page for the code portion */
335 stub_start -= PAGE_SIZE;
336 host_task_size = stub_start;
338 /* Limit TASK_SIZE to what is addressable by the page table */
339 task_size = host_task_size;
340 if (task_size > (unsigned long long) PTRS_PER_PGD * PGDIR_SIZE)
341 task_size = PTRS_PER_PGD * PGDIR_SIZE;
344 * TASK_SIZE needs to be PGDIR_SIZE aligned or else exit_mmap craps
347 task_size = task_size & PGDIR_MASK;
349 /* OS sanity checks that need to happen before the kernel runs */
352 get_host_cpu_features(parse_host_cpu_flags, parse_cache_line);
354 brk_start = (unsigned long) sbrk(0);
357 * Increase physical memory size for exec-shield users
358 * so they actually get what they asked for. This should
359 * add zero for non-exec shield users
362 diff = UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
363 if (diff > 1024 * 1024) {
364 os_info("Adding %ld bytes to physical memory to account for "
365 "exec-shield gap\n", diff);
366 physmem_size += UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
369 uml_physmem = (unsigned long) __binary_start & PAGE_MASK;
371 /* Reserve up to 4M after the current brk */
372 uml_reserved = ROUND_4M(brk_start) + (1 << 22);
374 setup_machinename(init_utsname()->machine);
376 physmem_size = (physmem_size + PAGE_SIZE - 1) & PAGE_MASK;
377 iomem_size = (iomem_size + PAGE_SIZE - 1) & PAGE_MASK;
379 max_physmem = TASK_SIZE - uml_physmem - iomem_size - MIN_VMALLOC;
380 if (physmem_size > max_physmem) {
381 physmem_size = max_physmem;
382 os_info("Physical memory size shrunk to %llu bytes\n",
386 high_physmem = uml_physmem + physmem_size;
387 end_iomem = high_physmem + iomem_size;
388 high_memory = (void *) end_iomem;
390 start_vm = VMALLOC_START;
392 virtmem_size = physmem_size;
393 stack = (unsigned long) argv;
394 stack &= ~(1024 * 1024 - 1);
395 avail = stack - start_vm;
396 if (physmem_size > avail)
397 virtmem_size = avail;
398 end_vm = start_vm + virtmem_size;
400 if (virtmem_size < physmem_size)
401 os_info("Kernel virtual memory size shrunk to %lu bytes\n",
404 arch_task_struct_size = sizeof(struct task_struct) + host_fp_size;
411 int __init __weak read_initrd(void)
416 void __init setup_arch(char **cmdline_p)
420 stack_protections((unsigned long) init_task.stack);
421 setup_physmem(uml_physmem, uml_reserved, physmem_size);
422 mem_total_pages(physmem_size, iomem_size);
427 strscpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
428 *cmdline_p = command_line;
429 setup_hostinfo(host_info, sizeof host_info);
431 if (os_getrandom(rng_seed, sizeof(rng_seed), 0) == sizeof(rng_seed)) {
432 add_bootloader_randomness(rng_seed, sizeof(rng_seed));
433 memzero_explicit(rng_seed, sizeof(rng_seed));
437 void __init arch_cpu_finalize_init(void)
443 void apply_seal_endbr(s32 *start, s32 *end, struct module *mod)
447 void apply_retpolines(s32 *start, s32 *end, struct module *mod)
451 void apply_returns(s32 *start, s32 *end, struct module *mod)
455 void apply_fineibt(s32 *start_retpoline, s32 *end_retpoline,
456 s32 *start_cfi, s32 *end_cfi, struct module *mod)
460 void apply_alternatives(struct alt_instr *start, struct alt_instr *end,
465 void *text_poke(void *addr, const void *opcode, size_t len)
468 * In UML, the only reference to this function is in
469 * apply_relocate_add(), which shouldn't ever actually call this
470 * because UML doesn't have live patching.
474 return memcpy(addr, opcode, len);
477 void *text_poke_copy(void *addr, const void *opcode, size_t len)
479 return text_poke(addr, opcode, len);
482 void text_poke_sync(void)
486 void uml_pm_wake(void)
491 #ifdef CONFIG_PM_SLEEP
492 static int um_suspend_valid(suspend_state_t state)
494 return state == PM_SUSPEND_MEM;
497 static int um_suspend_prepare(void)
503 static int um_suspend_enter(suspend_state_t state)
505 if (WARN_ON(state != PM_SUSPEND_MEM))
509 * This is identical to the idle sleep, but we've just
510 * (during suspend) turned off all interrupt sources
511 * except for the ones we want, so now we can only wake
512 * up on something we actually want to wake up on. All
513 * timing has also been suspended.
519 static void um_suspend_finish(void)
524 const struct platform_suspend_ops um_suspend_ops = {
525 .valid = um_suspend_valid,
526 .prepare = um_suspend_prepare,
527 .enter = um_suspend_enter,
528 .finish = um_suspend_finish,
531 static int init_pm_wake_signal(void)
534 * In external time-travel mode we can't use signals to wake up
535 * since that would mess with the scheduling. We'll have to do
536 * some additional work to support wakeup on virtio devices or
537 * similar, perhaps implementing a fake RTC controller that can
538 * trigger wakeup (and request the appropriate scheduling from
539 * the external scheduler when going to suspend.)
541 if (time_travel_mode != TT_MODE_EXTERNAL)
542 register_pm_wake_signal();
544 suspend_set_ops(&um_suspend_ops);
549 late_initcall(init_pm_wake_signal);