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 <as-layout.h>
30 #include <kern_util.h>
34 #define DEFAULT_COMMAND_LINE_ROOT "root=98:0"
35 #define DEFAULT_COMMAND_LINE_CONSOLE "console=tty0"
37 /* Changed in add_arg and setup_arch, which run before SMP is started */
38 static char __initdata command_line[COMMAND_LINE_SIZE] = { 0 };
40 static void __init add_arg(char *arg)
42 if (strlen(command_line) + strlen(arg) + 1 > COMMAND_LINE_SIZE) {
43 os_warn("add_arg: Too many command line arguments!\n");
46 if (strlen(command_line) > 0)
47 strcat(command_line, " ");
48 strcat(command_line, arg);
52 * These fields are initialized at boot time and not changed.
53 * XXX This structure is used only in the non-SMP case. Maybe this
54 * should be moved to smp.c.
56 struct cpuinfo_um boot_cpu_data = {
58 .ipi_pipe = { -1, -1 },
59 .cache_alignment = L1_CACHE_BYTES,
60 .x86_capability = { 0 }
63 EXPORT_SYMBOL(boot_cpu_data);
65 union thread_union cpu0_irqstack
66 __section(".data..init_irqstack") =
67 { .thread_info = INIT_THREAD_INFO(init_task) };
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 ? 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 uml_ncpus_setup */
129 /* Set in early boot */
130 static int have_root __initdata;
131 static int have_console __initdata;
133 /* Set in uml_mem_setup and modified in linux_main */
134 long long physmem_size = 32 * 1024 * 1024;
135 EXPORT_SYMBOL(physmem_size);
137 static const char *usage_string =
138 "User Mode Linux v%s\n"
139 " available at http://user-mode-linux.sourceforge.net/\n\n";
141 static int __init uml_version_setup(char *line, int *add)
143 /* Explicitly use printf() to show version in stdout */
144 printf("%s\n", init_utsname()->release);
150 __uml_setup("--version", uml_version_setup,
152 " Prints the version number of the kernel.\n\n"
155 static int __init uml_root_setup(char *line, int *add)
161 __uml_setup("root=", uml_root_setup,
162 "root=<file containing the root fs>\n"
163 " This is actually used by the generic kernel in exactly the same\n"
164 " way as in any other kernel. If you configure a number of block\n"
165 " devices and want to boot off something other than ubd0, you \n"
166 " would use something like:\n"
167 " root=/dev/ubd5\n\n"
170 static int __init no_skas_debug_setup(char *line, int *add)
172 os_warn("'debug' is not necessary to gdb UML in skas mode - run\n");
173 os_warn("'gdb linux'\n");
178 __uml_setup("debug", no_skas_debug_setup,
180 " this flag is not needed to run gdb on UML in skas mode\n\n"
183 static int __init uml_console_setup(char *line, int *add)
189 __uml_setup("console=", uml_console_setup,
190 "console=<preferred console>\n"
191 " Specify the preferred console output driver\n\n"
194 static int __init Usage(char *line, int *add)
198 printf(usage_string, init_utsname()->release);
199 p = &__uml_help_start;
200 /* Explicitly use printf() to show help in stdout */
201 while (p < &__uml_help_end) {
209 __uml_setup("--help", Usage,
211 " Prints this message.\n\n"
214 static void __init uml_checksetup(char *line, int *add)
218 p = &__uml_setup_start;
219 while (p < &__uml_setup_end) {
223 if (!strncmp(line, p->str, n) && p->setup_func(line + n, add))
229 static void __init uml_postsetup(void)
233 p = &__uml_postsetup_start;
234 while (p < &__uml_postsetup_end) {
241 static int panic_exit(struct notifier_block *self, unsigned long unused1,
244 kmsg_dump(KMSG_DUMP_PANIC);
252 static struct notifier_block panic_exit_notifier = {
253 .notifier_call = panic_exit,
258 void uml_finishsetup(void)
260 atomic_notifier_chain_register(&panic_notifier_list,
261 &panic_exit_notifier);
265 new_thread_handler();
268 /* Set during early boot */
269 unsigned long stub_start;
270 unsigned long task_size;
271 EXPORT_SYMBOL(task_size);
273 unsigned long host_task_size;
275 unsigned long brk_start;
276 unsigned long end_iomem;
277 EXPORT_SYMBOL(end_iomem);
279 #define MIN_VMALLOC (32 * 1024 * 1024)
281 static void parse_host_cpu_flags(char *line)
284 for (i = 0; i < 32*NCAPINTS; i++) {
285 if ((x86_cap_flags[i] != NULL) && strstr(line, x86_cap_flags[i]))
286 set_cpu_cap(&boot_cpu_data, i);
289 static void parse_cache_line(char *line)
292 char *to_parse = strstr(line, ":");
295 while (*to_parse != 0 && isspace(*to_parse)) {
298 if (kstrtoul(to_parse, 10, &res) == 0 && is_power_of_2(res))
299 boot_cpu_data.cache_alignment = res;
301 boot_cpu_data.cache_alignment = L1_CACHE_BYTES;
305 int __init linux_main(int argc, char **argv)
307 unsigned long avail, diff;
308 unsigned long virtmem_size, max_physmem;
313 for (i = 1; i < argc; i++) {
314 if ((i == 1) && (argv[i][0] == ' '))
317 uml_checksetup(argv[i], &add);
322 add_arg(DEFAULT_COMMAND_LINE_ROOT);
324 if (have_console == 0)
325 add_arg(DEFAULT_COMMAND_LINE_CONSOLE);
327 host_task_size = os_get_top_address();
328 /* reserve two pages for the stubs */
329 host_task_size -= 2 * PAGE_SIZE;
330 stub_start = host_task_size;
333 * TASK_SIZE needs to be PGDIR_SIZE aligned or else exit_mmap craps
336 task_size = host_task_size & PGDIR_MASK;
338 /* OS sanity checks that need to happen before the kernel runs */
341 get_host_cpu_features(parse_host_cpu_flags, parse_cache_line);
343 brk_start = (unsigned long) sbrk(0);
346 * Increase physical memory size for exec-shield users
347 * so they actually get what they asked for. This should
348 * add zero for non-exec shield users
351 diff = UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
352 if (diff > 1024 * 1024) {
353 os_info("Adding %ld bytes to physical memory to account for "
354 "exec-shield gap\n", diff);
355 physmem_size += UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
358 uml_physmem = (unsigned long) __binary_start & PAGE_MASK;
360 /* Reserve up to 4M after the current brk */
361 uml_reserved = ROUND_4M(brk_start) + (1 << 22);
363 setup_machinename(init_utsname()->machine);
366 iomem_size = (iomem_size + PAGE_SIZE - 1) & PAGE_MASK;
367 max_physmem = TASK_SIZE - uml_physmem - iomem_size - MIN_VMALLOC;
370 * Zones have to begin on a 1 << MAX_ORDER page boundary,
371 * so this makes sure that's true for highmem
373 max_physmem &= ~((1 << (PAGE_SHIFT + MAX_ORDER)) - 1);
374 if (physmem_size + iomem_size > max_physmem) {
375 highmem = physmem_size + iomem_size - max_physmem;
376 physmem_size -= highmem;
379 high_physmem = uml_physmem + physmem_size;
380 end_iomem = high_physmem + iomem_size;
381 high_memory = (void *) end_iomem;
383 start_vm = VMALLOC_START;
385 virtmem_size = physmem_size;
386 stack = (unsigned long) argv;
387 stack &= ~(1024 * 1024 - 1);
388 avail = stack - start_vm;
389 if (physmem_size > avail)
390 virtmem_size = avail;
391 end_vm = start_vm + virtmem_size;
393 if (virtmem_size < physmem_size)
394 os_info("Kernel virtual memory size shrunk to %lu bytes\n",
402 int __init __weak read_initrd(void)
407 void __init setup_arch(char **cmdline_p)
411 stack_protections((unsigned long) &init_thread_info);
412 setup_physmem(uml_physmem, uml_reserved, physmem_size, highmem);
413 mem_total_pages(physmem_size, iomem_size, highmem);
417 strlcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
418 *cmdline_p = command_line;
419 setup_hostinfo(host_info, sizeof host_info);
421 if (os_getrandom(rng_seed, sizeof(rng_seed), 0) == sizeof(rng_seed)) {
422 add_bootloader_randomness(rng_seed, sizeof(rng_seed));
423 memzero_explicit(rng_seed, sizeof(rng_seed));
427 void __init arch_cpu_finalize_init(void)
433 void apply_retpolines(s32 *start, s32 *end)
437 void apply_returns(s32 *start, s32 *end)
441 void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
445 void *text_poke(void *addr, const void *opcode, size_t len)
448 * In UML, the only reference to this function is in
449 * apply_relocate_add(), which shouldn't ever actually call this
450 * because UML doesn't have live patching.
454 return memcpy(addr, opcode, len);
457 void text_poke_sync(void)
461 void uml_pm_wake(void)
466 #ifdef CONFIG_PM_SLEEP
467 static int um_suspend_valid(suspend_state_t state)
469 return state == PM_SUSPEND_MEM;
472 static int um_suspend_prepare(void)
478 static int um_suspend_enter(suspend_state_t state)
480 if (WARN_ON(state != PM_SUSPEND_MEM))
484 * This is identical to the idle sleep, but we've just
485 * (during suspend) turned off all interrupt sources
486 * except for the ones we want, so now we can only wake
487 * up on something we actually want to wake up on. All
488 * timing has also been suspended.
494 static void um_suspend_finish(void)
499 const struct platform_suspend_ops um_suspend_ops = {
500 .valid = um_suspend_valid,
501 .prepare = um_suspend_prepare,
502 .enter = um_suspend_enter,
503 .finish = um_suspend_finish,
506 static int init_pm_wake_signal(void)
509 * In external time-travel mode we can't use signals to wake up
510 * since that would mess with the scheduling. We'll have to do
511 * some additional work to support wakeup on virtio devices or
512 * similar, perhaps implementing a fake RTC controller that can
513 * trigger wakeup (and request the appropriate scheduling from
514 * the external scheduler when going to suspend.)
516 if (time_travel_mode != TT_MODE_EXTERNAL)
517 register_pm_wake_signal();
519 suspend_set_ops(&um_suspend_ops);
524 late_initcall(init_pm_wake_signal);