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