1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
6 #include <linux/delay.h>
7 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/seq_file.h>
11 #include <linux/string.h>
12 #include <linux/utsname.h>
13 #include <linux/sched.h>
14 #include <linux/sched/task.h>
15 #include <linux/kmsg_dump.h>
16 #include <linux/suspend.h>
18 #include <asm/processor.h>
19 #include <asm/sections.h>
20 #include <asm/setup.h>
21 #include <as-layout.h>
25 #include <kern_util.h>
29 #define DEFAULT_COMMAND_LINE_ROOT "root=98:0"
30 #define DEFAULT_COMMAND_LINE_CONSOLE "console=tty"
32 /* Changed in add_arg and setup_arch, which run before SMP is started */
33 static char __initdata command_line[COMMAND_LINE_SIZE] = { 0 };
35 static void __init add_arg(char *arg)
37 if (strlen(command_line) + strlen(arg) + 1 > COMMAND_LINE_SIZE) {
38 os_warn("add_arg: Too many command line arguments!\n");
41 if (strlen(command_line) > 0)
42 strcat(command_line, " ");
43 strcat(command_line, arg);
47 * These fields are initialized at boot time and not changed.
48 * XXX This structure is used only in the non-SMP case. Maybe this
49 * should be moved to smp.c.
51 struct cpuinfo_um boot_cpu_data = {
53 .ipi_pipe = { -1, -1 }
56 union thread_union cpu0_irqstack
57 __section(".data..init_irqstack") =
58 { .thread_info = INIT_THREAD_INFO(init_task) };
60 /* Changed in setup_arch, which is called in early boot */
61 static char host_info[(__NEW_UTS_LEN + 1) * 5];
63 static int show_cpuinfo(struct seq_file *m, void *v)
67 seq_printf(m, "processor\t: %d\n", index);
68 seq_printf(m, "vendor_id\t: User Mode Linux\n");
69 seq_printf(m, "model name\t: UML\n");
70 seq_printf(m, "mode\t\t: skas\n");
71 seq_printf(m, "host\t\t: %s\n", host_info);
72 seq_printf(m, "bogomips\t: %lu.%02lu\n\n",
73 loops_per_jiffy/(500000/HZ),
74 (loops_per_jiffy/(5000/HZ)) % 100);
79 static void *c_start(struct seq_file *m, loff_t *pos)
81 return *pos < NR_CPUS ? cpu_data + *pos : NULL;
84 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
87 return c_start(m, pos);
90 static void c_stop(struct seq_file *m, void *v)
94 const struct seq_operations cpuinfo_op = {
101 /* Set in linux_main */
102 unsigned long uml_physmem;
103 EXPORT_SYMBOL(uml_physmem);
105 unsigned long uml_reserved; /* Also modified in mem_init */
106 unsigned long start_vm;
107 unsigned long end_vm;
109 /* Set in uml_ncpus_setup */
112 /* Set in early boot */
113 static int have_root __initdata;
114 static int have_console __initdata;
116 /* Set in uml_mem_setup and modified in linux_main */
117 long long physmem_size = 32 * 1024 * 1024;
118 EXPORT_SYMBOL(physmem_size);
120 static const char *usage_string =
121 "User Mode Linux v%s\n"
122 " available at http://user-mode-linux.sourceforge.net/\n\n";
124 static int __init uml_version_setup(char *line, int *add)
126 /* Explicitly use printf() to show version in stdout */
127 printf("%s\n", init_utsname()->release);
133 __uml_setup("--version", uml_version_setup,
135 " Prints the version number of the kernel.\n\n"
138 static int __init uml_root_setup(char *line, int *add)
144 __uml_setup("root=", uml_root_setup,
145 "root=<file containing the root fs>\n"
146 " This is actually used by the generic kernel in exactly the same\n"
147 " way as in any other kernel. If you configure a number of block\n"
148 " devices and want to boot off something other than ubd0, you \n"
149 " would use something like:\n"
150 " root=/dev/ubd5\n\n"
153 static int __init no_skas_debug_setup(char *line, int *add)
155 os_warn("'debug' is not necessary to gdb UML in skas mode - run\n");
156 os_warn("'gdb linux'\n");
161 __uml_setup("debug", no_skas_debug_setup,
163 " this flag is not needed to run gdb on UML in skas mode\n\n"
166 static int __init uml_console_setup(char *line, int *add)
172 __uml_setup("console=", uml_console_setup,
173 "console=<preferred console>\n"
174 " Specify the preferred console output driver\n\n"
177 static int __init Usage(char *line, int *add)
181 printf(usage_string, init_utsname()->release);
182 p = &__uml_help_start;
183 /* Explicitly use printf() to show help in stdout */
184 while (p < &__uml_help_end) {
192 __uml_setup("--help", Usage,
194 " Prints this message.\n\n"
197 static void __init uml_checksetup(char *line, int *add)
201 p = &__uml_setup_start;
202 while (p < &__uml_setup_end) {
206 if (!strncmp(line, p->str, n) && p->setup_func(line + n, add))
212 static void __init uml_postsetup(void)
216 p = &__uml_postsetup_start;
217 while (p < &__uml_postsetup_end) {
224 static int panic_exit(struct notifier_block *self, unsigned long unused1,
227 kmsg_dump(KMSG_DUMP_PANIC);
235 static struct notifier_block panic_exit_notifier = {
236 .notifier_call = panic_exit,
241 void uml_finishsetup(void)
243 atomic_notifier_chain_register(&panic_notifier_list,
244 &panic_exit_notifier);
248 new_thread_handler();
251 /* Set during early boot */
252 unsigned long stub_start;
253 unsigned long task_size;
254 EXPORT_SYMBOL(task_size);
256 unsigned long host_task_size;
258 unsigned long brk_start;
259 unsigned long end_iomem;
260 EXPORT_SYMBOL(end_iomem);
262 #define MIN_VMALLOC (32 * 1024 * 1024)
264 int __init linux_main(int argc, char **argv)
266 unsigned long avail, diff;
267 unsigned long virtmem_size, max_physmem;
272 for (i = 1; i < argc; i++) {
273 if ((i == 1) && (argv[i][0] == ' '))
276 uml_checksetup(argv[i], &add);
281 add_arg(DEFAULT_COMMAND_LINE_ROOT);
283 if (have_console == 0)
284 add_arg(DEFAULT_COMMAND_LINE_CONSOLE);
286 host_task_size = os_get_top_address();
287 /* reserve two pages for the stubs */
288 host_task_size -= 2 * PAGE_SIZE;
289 stub_start = host_task_size;
292 * TASK_SIZE needs to be PGDIR_SIZE aligned or else exit_mmap craps
295 task_size = host_task_size & PGDIR_MASK;
297 /* OS sanity checks that need to happen before the kernel runs */
300 brk_start = (unsigned long) sbrk(0);
303 * Increase physical memory size for exec-shield users
304 * so they actually get what they asked for. This should
305 * add zero for non-exec shield users
308 diff = UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
309 if (diff > 1024 * 1024) {
310 os_info("Adding %ld bytes to physical memory to account for "
311 "exec-shield gap\n", diff);
312 physmem_size += UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
315 uml_physmem = (unsigned long) __binary_start & PAGE_MASK;
317 /* Reserve up to 4M after the current brk */
318 uml_reserved = ROUND_4M(brk_start) + (1 << 22);
320 setup_machinename(init_utsname()->machine);
323 iomem_size = (iomem_size + PAGE_SIZE - 1) & PAGE_MASK;
324 max_physmem = TASK_SIZE - uml_physmem - iomem_size - MIN_VMALLOC;
327 * Zones have to begin on a 1 << MAX_ORDER page boundary,
328 * so this makes sure that's true for highmem
330 max_physmem &= ~((1 << (PAGE_SHIFT + MAX_ORDER)) - 1);
331 if (physmem_size + iomem_size > max_physmem) {
332 highmem = physmem_size + iomem_size - max_physmem;
333 physmem_size -= highmem;
336 high_physmem = uml_physmem + physmem_size;
337 end_iomem = high_physmem + iomem_size;
338 high_memory = (void *) end_iomem;
340 start_vm = VMALLOC_START;
342 virtmem_size = physmem_size;
343 stack = (unsigned long) argv;
344 stack &= ~(1024 * 1024 - 1);
345 avail = stack - start_vm;
346 if (physmem_size > avail)
347 virtmem_size = avail;
348 end_vm = start_vm + virtmem_size;
350 if (virtmem_size < physmem_size)
351 os_info("Kernel virtual memory size shrunk to %lu bytes\n",
359 int __init __weak read_initrd(void)
364 void __init setup_arch(char **cmdline_p)
366 stack_protections((unsigned long) &init_thread_info);
367 setup_physmem(uml_physmem, uml_reserved, physmem_size, highmem);
368 mem_total_pages(physmem_size, iomem_size, highmem);
372 strlcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
373 *cmdline_p = command_line;
374 setup_hostinfo(host_info, sizeof host_info);
377 void __init check_bugs(void)
383 void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
387 void *text_poke(void *addr, const void *opcode, size_t len)
390 * In UML, the only reference to this function is in
391 * apply_relocate_add(), which shouldn't ever actually call this
392 * because UML doesn't have live patching.
396 return memcpy(addr, opcode, len);
399 void text_poke_sync(void)
403 void uml_pm_wake(void)
408 #ifdef CONFIG_PM_SLEEP
409 static int um_suspend_valid(suspend_state_t state)
411 return state == PM_SUSPEND_MEM;
414 static int um_suspend_prepare(void)
420 static int um_suspend_enter(suspend_state_t state)
422 if (WARN_ON(state != PM_SUSPEND_MEM))
426 * This is identical to the idle sleep, but we've just
427 * (during suspend) turned off all interrupt sources
428 * except for the ones we want, so now we can only wake
429 * up on something we actually want to wake up on. All
430 * timing has also been suspended.
436 static void um_suspend_finish(void)
441 const struct platform_suspend_ops um_suspend_ops = {
442 .valid = um_suspend_valid,
443 .prepare = um_suspend_prepare,
444 .enter = um_suspend_enter,
445 .finish = um_suspend_finish,
448 static int init_pm_wake_signal(void)
451 * In external time-travel mode we can't use signals to wake up
452 * since that would mess with the scheduling. We'll have to do
453 * some additional work to support wakeup on virtio devices or
454 * similar, perhaps implementing a fake RTC controller that can
455 * trigger wakeup (and request the appropriate scheduling from
456 * the external scheduler when going to suspend.)
458 if (time_travel_mode != TT_MODE_EXTERNAL)
459 register_pm_wake_signal();
461 suspend_set_ops(&um_suspend_ops);
466 late_initcall(init_pm_wake_signal);