xref: /openbmc/linux/arch/um/kernel/um_arch.c (revision d9f6e12f)
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/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>
17 
18 #include <asm/processor.h>
19 #include <asm/sections.h>
20 #include <asm/setup.h>
21 #include <as-layout.h>
22 #include <arch.h>
23 #include <init.h>
24 #include <kern.h>
25 #include <kern_util.h>
26 #include <mem_user.h>
27 #include <os.h>
28 
29 #define DEFAULT_COMMAND_LINE_ROOT "root=98:0"
30 #define DEFAULT_COMMAND_LINE_CONSOLE "console=tty"
31 
32 /* Changed in add_arg and setup_arch, which run before SMP is started */
33 static char __initdata command_line[COMMAND_LINE_SIZE] = { 0 };
34 
35 static void __init add_arg(char *arg)
36 {
37 	if (strlen(command_line) + strlen(arg) + 1 > COMMAND_LINE_SIZE) {
38 		os_warn("add_arg: Too many command line arguments!\n");
39 		exit(1);
40 	}
41 	if (strlen(command_line) > 0)
42 		strcat(command_line, " ");
43 	strcat(command_line, arg);
44 }
45 
46 /*
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.
50  */
51 struct cpuinfo_um boot_cpu_data = {
52 	.loops_per_jiffy	= 0,
53 	.ipi_pipe		= { -1, -1 }
54 };
55 
56 union thread_union cpu0_irqstack
57 	__section(".data..init_irqstack") =
58 		{ .thread_info = INIT_THREAD_INFO(init_task) };
59 
60 /* Changed in setup_arch, which is called in early boot */
61 static char host_info[(__NEW_UTS_LEN + 1) * 5];
62 
63 static int show_cpuinfo(struct seq_file *m, void *v)
64 {
65 	int index = 0;
66 
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);
75 
76 	return 0;
77 }
78 
79 static void *c_start(struct seq_file *m, loff_t *pos)
80 {
81 	return *pos < NR_CPUS ? cpu_data + *pos : NULL;
82 }
83 
84 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
85 {
86 	++*pos;
87 	return c_start(m, pos);
88 }
89 
90 static void c_stop(struct seq_file *m, void *v)
91 {
92 }
93 
94 const struct seq_operations cpuinfo_op = {
95 	.start	= c_start,
96 	.next	= c_next,
97 	.stop	= c_stop,
98 	.show	= show_cpuinfo,
99 };
100 
101 /* Set in linux_main */
102 unsigned long uml_physmem;
103 EXPORT_SYMBOL(uml_physmem);
104 
105 unsigned long uml_reserved; /* Also modified in mem_init */
106 unsigned long start_vm;
107 unsigned long end_vm;
108 
109 /* Set in uml_ncpus_setup */
110 int ncpus = 1;
111 
112 /* Set in early boot */
113 static int have_root __initdata;
114 static int have_console __initdata;
115 
116 /* Set in uml_mem_setup and modified in linux_main */
117 long long physmem_size = 32 * 1024 * 1024;
118 EXPORT_SYMBOL(physmem_size);
119 
120 static const char *usage_string =
121 "User Mode Linux v%s\n"
122 "	available at http://user-mode-linux.sourceforge.net/\n\n";
123 
124 static int __init uml_version_setup(char *line, int *add)
125 {
126 	/* Explicitly use printf() to show version in stdout */
127 	printf("%s\n", init_utsname()->release);
128 	exit(0);
129 
130 	return 0;
131 }
132 
133 __uml_setup("--version", uml_version_setup,
134 "--version\n"
135 "    Prints the version number of the kernel.\n\n"
136 );
137 
138 static int __init uml_root_setup(char *line, int *add)
139 {
140 	have_root = 1;
141 	return 0;
142 }
143 
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"
151 );
152 
153 static int __init no_skas_debug_setup(char *line, int *add)
154 {
155 	os_warn("'debug' is not necessary to gdb UML in skas mode - run\n");
156 	os_warn("'gdb linux'\n");
157 
158 	return 0;
159 }
160 
161 __uml_setup("debug", no_skas_debug_setup,
162 "debug\n"
163 "    this flag is not needed to run gdb on UML in skas mode\n\n"
164 );
165 
166 static int __init uml_console_setup(char *line, int *add)
167 {
168 	have_console = 1;
169 	return 0;
170 }
171 
172 __uml_setup("console=", uml_console_setup,
173 "console=<preferred console>\n"
174 "    Specify the preferred console output driver\n\n"
175 );
176 
177 static int __init Usage(char *line, int *add)
178 {
179 	const char **p;
180 
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) {
185 		printf("%s", *p);
186 		p++;
187 	}
188 	exit(0);
189 	return 0;
190 }
191 
192 __uml_setup("--help", Usage,
193 "--help\n"
194 "    Prints this message.\n\n"
195 );
196 
197 static void __init uml_checksetup(char *line, int *add)
198 {
199 	struct uml_param *p;
200 
201 	p = &__uml_setup_start;
202 	while (p < &__uml_setup_end) {
203 		size_t n;
204 
205 		n = strlen(p->str);
206 		if (!strncmp(line, p->str, n) && p->setup_func(line + n, add))
207 			return;
208 		p++;
209 	}
210 }
211 
212 static void __init uml_postsetup(void)
213 {
214 	initcall_t *p;
215 
216 	p = &__uml_postsetup_start;
217 	while (p < &__uml_postsetup_end) {
218 		(*p)();
219 		p++;
220 	}
221 	return;
222 }
223 
224 static int panic_exit(struct notifier_block *self, unsigned long unused1,
225 		      void *unused2)
226 {
227 	kmsg_dump(KMSG_DUMP_PANIC);
228 	bust_spinlocks(1);
229 	bust_spinlocks(0);
230 	uml_exitcode = 1;
231 	os_dump_core();
232 	return 0;
233 }
234 
235 static struct notifier_block panic_exit_notifier = {
236 	.notifier_call 		= panic_exit,
237 	.next 			= NULL,
238 	.priority 		= 0
239 };
240 
241 void uml_finishsetup(void)
242 {
243 	atomic_notifier_chain_register(&panic_notifier_list,
244 				       &panic_exit_notifier);
245 
246 	uml_postsetup();
247 
248 	new_thread_handler();
249 }
250 
251 /* Set during early boot */
252 unsigned long stub_start;
253 unsigned long task_size;
254 EXPORT_SYMBOL(task_size);
255 
256 unsigned long host_task_size;
257 
258 unsigned long brk_start;
259 unsigned long end_iomem;
260 EXPORT_SYMBOL(end_iomem);
261 
262 #define MIN_VMALLOC (32 * 1024 * 1024)
263 
264 int __init linux_main(int argc, char **argv)
265 {
266 	unsigned long avail, diff;
267 	unsigned long virtmem_size, max_physmem;
268 	unsigned long stack;
269 	unsigned int i;
270 	int add;
271 
272 	for (i = 1; i < argc; i++) {
273 		if ((i == 1) && (argv[i][0] == ' '))
274 			continue;
275 		add = 1;
276 		uml_checksetup(argv[i], &add);
277 		if (add)
278 			add_arg(argv[i]);
279 	}
280 	if (have_root == 0)
281 		add_arg(DEFAULT_COMMAND_LINE_ROOT);
282 
283 	if (have_console == 0)
284 		add_arg(DEFAULT_COMMAND_LINE_CONSOLE);
285 
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;
290 
291 	/*
292 	 * TASK_SIZE needs to be PGDIR_SIZE aligned or else exit_mmap craps
293 	 * out
294 	 */
295 	task_size = host_task_size & PGDIR_MASK;
296 
297 	/* OS sanity checks that need to happen before the kernel runs */
298 	os_early_checks();
299 
300 	brk_start = (unsigned long) sbrk(0);
301 
302 	/*
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
306 	 */
307 
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);
313 	}
314 
315 	uml_physmem = (unsigned long) __binary_start & PAGE_MASK;
316 
317 	/* Reserve up to 4M after the current brk */
318 	uml_reserved = ROUND_4M(brk_start) + (1 << 22);
319 
320 	setup_machinename(init_utsname()->machine);
321 
322 	highmem = 0;
323 	iomem_size = (iomem_size + PAGE_SIZE - 1) & PAGE_MASK;
324 	max_physmem = TASK_SIZE - uml_physmem - iomem_size - MIN_VMALLOC;
325 
326 	/*
327 	 * Zones have to begin on a 1 << MAX_ORDER page boundary,
328 	 * so this makes sure that's true for highmem
329 	 */
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;
334 	}
335 
336 	high_physmem = uml_physmem + physmem_size;
337 	end_iomem = high_physmem + iomem_size;
338 	high_memory = (void *) end_iomem;
339 
340 	start_vm = VMALLOC_START;
341 
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;
349 
350 	if (virtmem_size < physmem_size)
351 		os_info("Kernel virtual memory size shrunk to %lu bytes\n",
352 			virtmem_size);
353 
354 	os_flush_stdout();
355 
356 	return start_uml();
357 }
358 
359 int __init __weak read_initrd(void)
360 {
361 	return 0;
362 }
363 
364 void __init setup_arch(char **cmdline_p)
365 {
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);
369 	read_initrd();
370 
371 	paging_init();
372 	strlcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
373 	*cmdline_p = command_line;
374 	setup_hostinfo(host_info, sizeof host_info);
375 }
376 
377 void __init check_bugs(void)
378 {
379 	arch_check_bugs();
380 	os_check_bugs();
381 }
382 
383 void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
384 {
385 }
386 
387 void *text_poke(void *addr, const void *opcode, size_t len)
388 {
389 	/*
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.
393 	 */
394 	WARN_ON(1);
395 
396 	return memcpy(addr, opcode, len);
397 }
398 
399 void text_poke_sync(void)
400 {
401 }
402 
403 void uml_pm_wake(void)
404 {
405 	pm_system_wakeup();
406 }
407 
408 #ifdef CONFIG_PM_SLEEP
409 static int um_suspend_valid(suspend_state_t state)
410 {
411 	return state == PM_SUSPEND_MEM;
412 }
413 
414 static int um_suspend_prepare(void)
415 {
416 	um_irqs_suspend();
417 	return 0;
418 }
419 
420 static int um_suspend_enter(suspend_state_t state)
421 {
422 	if (WARN_ON(state != PM_SUSPEND_MEM))
423 		return -EINVAL;
424 
425 	/*
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.
431 	 */
432 	um_idle_sleep();
433 	return 0;
434 }
435 
436 static void um_suspend_finish(void)
437 {
438 	um_irqs_resume();
439 }
440 
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,
446 };
447 
448 static int init_pm_wake_signal(void)
449 {
450 	/*
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.)
457 	 */
458 	if (time_travel_mode != TT_MODE_EXTERNAL)
459 		register_pm_wake_signal();
460 
461 	suspend_set_ops(&um_suspend_ops);
462 
463 	return 0;
464 }
465 
466 late_initcall(init_pm_wake_signal);
467 #endif
468