xref: /linux/arch/um/kernel/um_arch.c (revision 12d5fc96ebbb3b948861805b32a4c3a96105ccd9)
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/string_choices.h>
16 #include <linux/utsname.h>
17 #include <linux/sched.h>
18 #include <linux/sched/task.h>
19 #include <linux/kmsg_dump.h>
20 #include <linux/suspend.h>
21 #include <linux/random.h>
22 #include <linux/smp-internal.h>
23 
24 #include <asm/processor.h>
25 #include <asm/cpufeature.h>
26 #include <asm/sections.h>
27 #include <asm/setup.h>
28 #include <asm/text-patching.h>
29 #include <as-layout.h>
30 #include <arch.h>
31 #include <init.h>
32 #include <kern.h>
33 #include <kern_util.h>
34 #include <mem_user.h>
35 #include <os.h>
36 
37 #include "um_arch.h"
38 
39 #define DEFAULT_COMMAND_LINE_ROOT "root=98:0"
40 #define DEFAULT_COMMAND_LINE_CONSOLE "console=tty0"
41 
42 /* Changed in add_arg and setup_arch, which run before SMP is started */
43 static char __initdata command_line[COMMAND_LINE_SIZE] = { 0 };
44 
45 static void __init add_arg(char *arg)
46 {
47 	if (strlen(command_line) + strlen(arg) + 1 > COMMAND_LINE_SIZE) {
48 		os_warn("add_arg: Too many command line arguments!\n");
49 		exit(1);
50 	}
51 	if (strlen(command_line) > 0)
52 		strcat(command_line, " ");
53 	strcat(command_line, arg);
54 }
55 
56 /*
57  * These fields are initialized at boot time and not changed.
58  */
59 struct cpuinfo_um boot_cpu_data = {
60 	.loops_per_jiffy	= 0,
61 	.cache_alignment	= L1_CACHE_BYTES,
62 	.x86_capability		= { 0 }
63 };
64 
65 EXPORT_SYMBOL(boot_cpu_data);
66 
67 
68 /* Changed in setup_arch, which is called in early boot */
69 static char host_info[(__NEW_UTS_LEN + 1) * 5];
70 
71 static int show_cpuinfo(struct seq_file *m, void *v)
72 {
73 	int i = 0;
74 
75 #if IS_ENABLED(CONFIG_SMP)
76 	i = (uintptr_t) v - 1;
77 	if (!cpu_online(i))
78 		return 0;
79 #endif
80 
81 	seq_printf(m, "processor\t: %d\n", i);
82 	seq_printf(m, "vendor_id\t: User Mode Linux\n");
83 	seq_printf(m, "model name\t: UML\n");
84 	seq_printf(m, "mode\t\t: skas\n");
85 	seq_printf(m, "host\t\t: %s\n", host_info);
86 	seq_printf(m, "fpu\t\t: %s\n", str_yes_no(cpu_has(&boot_cpu_data, X86_FEATURE_FPU)));
87 	seq_printf(m, "flags\t\t:");
88 	for (i = 0; i < 32*NCAPINTS; i++)
89 		if (cpu_has(&boot_cpu_data, i) && (x86_cap_flags[i] != NULL))
90 			seq_printf(m, " %s", x86_cap_flags[i]);
91 	seq_printf(m, "\n");
92 	seq_printf(m, "cache_alignment\t: %d\n", boot_cpu_data.cache_alignment);
93 	seq_printf(m, "bogomips\t: %lu.%02lu\n",
94 		   loops_per_jiffy/(500000/HZ),
95 		   (loops_per_jiffy/(5000/HZ)) % 100);
96 
97 	return 0;
98 }
99 
100 static void *c_start(struct seq_file *m, loff_t *pos)
101 {
102 	if (*pos < nr_cpu_ids)
103 		return (void *)(uintptr_t)(*pos + 1);
104 	return NULL;
105 }
106 
107 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
108 {
109 	++*pos;
110 	return c_start(m, pos);
111 }
112 
113 static void c_stop(struct seq_file *m, void *v)
114 {
115 }
116 
117 const struct seq_operations cpuinfo_op = {
118 	.start	= c_start,
119 	.next	= c_next,
120 	.stop	= c_stop,
121 	.show	= show_cpuinfo,
122 };
123 
124 /* Set in linux_main */
125 unsigned long uml_physmem;
126 EXPORT_SYMBOL(uml_physmem);
127 
128 unsigned long uml_reserved; /* Also modified in mem_init */
129 unsigned long start_vm;
130 unsigned long end_vm;
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 unsigned 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 uml_console_setup(char *line, int *add)
174 {
175 	have_console = 1;
176 	return 0;
177 }
178 
179 __uml_setup("console=", uml_console_setup,
180 "console=<preferred console>\n"
181 "    Specify the preferred console output driver\n\n"
182 );
183 
184 static int __init Usage(char *line, int *add)
185 {
186 	const char **p;
187 
188 	printf(usage_string, init_utsname()->release);
189 	p = &__uml_help_start;
190 	/* Explicitly use printf() to show help in stdout */
191 	while (p < &__uml_help_end) {
192 		printf("%s", *p);
193 		p++;
194 	}
195 	exit(0);
196 	return 0;
197 }
198 
199 __uml_setup("--help", Usage,
200 "--help\n"
201 "    Prints this message.\n\n"
202 );
203 
204 static void __init uml_checksetup(char *line, int *add)
205 {
206 	struct uml_param *p;
207 
208 	p = &__uml_setup_start;
209 	while (p < &__uml_setup_end) {
210 		size_t n;
211 
212 		n = strlen(p->str);
213 		if (!strncmp(line, p->str, n) && p->setup_func(line + n, add))
214 			return;
215 		p++;
216 	}
217 }
218 
219 static void __init uml_postsetup(void)
220 {
221 	initcall_t *p;
222 
223 	p = &__uml_postsetup_start;
224 	while (p < &__uml_postsetup_end) {
225 		(*p)();
226 		p++;
227 	}
228 	return;
229 }
230 
231 static int panic_exit(struct notifier_block *self, unsigned long unused1,
232 		      void *unused2)
233 {
234 	kmsg_dump(KMSG_DUMP_PANIC);
235 	bust_spinlocks(1);
236 	bust_spinlocks(0);
237 	uml_exitcode = 1;
238 	os_dump_core();
239 
240 	return NOTIFY_DONE;
241 }
242 
243 static struct notifier_block panic_exit_notifier = {
244 	.notifier_call	= panic_exit,
245 	.priority	= INT_MAX - 1, /* run as 2nd notifier, won't return */
246 };
247 
248 void uml_finishsetup(void)
249 {
250 	atomic_notifier_chain_register(&panic_notifier_list,
251 				       &panic_exit_notifier);
252 
253 	uml_postsetup();
254 
255 	new_thread_handler();
256 }
257 
258 /* Set during early boot */
259 unsigned long stub_start;
260 unsigned long task_size;
261 EXPORT_SYMBOL(task_size);
262 
263 unsigned long brk_start;
264 
265 #define MIN_VMALLOC (32 * 1024 * 1024)
266 
267 static u64 __init read_xcr0(void)
268 {
269 	u32 a, b, c, d;
270 
271 	asm volatile("cpuid"
272 		     : "=a"(a), "=b"(b), "=c"(c), "=d"(d)
273 		     : "a"(0), "c"(0));
274 	if (a >= 1) { /* max_leaf >= 1 */
275 		asm volatile("cpuid"
276 			     : "=a"(a), "=b"(b), "=c"(c), "=d"(d)
277 			     : "a"(1), "c"(0));
278 		if (c & (1 << 27)) { /* XSAVE enabled by OS */
279 			asm volatile("xgetbv" : "=d"(d), "=a"(a) : "c"(0));
280 			return ((u64)d << 32) | a;
281 		}
282 	}
283 	return 0;
284 }
285 
286 static void __init validate_and_set_cpu_cap(int cap, u64 xcr0)
287 {
288 	/*
289 	 * Check for missing xstate features right away, so that there's no
290 	 * perceived need for all optimized code in the kernel to do so.
291 	 */
292 	switch (cap) {
293 	case X86_FEATURE_AVX:
294 	case X86_FEATURE_AVX2:
295 	case X86_FEATURE_AVX_VNNI:
296 	case X86_FEATURE_FMA:
297 	case X86_FEATURE_VAES:
298 	case X86_FEATURE_VPCLMULQDQ:
299 		if ((xcr0 & 0x7) != 0x7) {
300 			static bool warned;
301 
302 			if (!warned) {
303 				os_warn("Disabling AVX support due to missing xstate features\n");
304 				warned = true;
305 			}
306 			return;
307 		}
308 		break;
309 	case X86_FEATURE_AVX512F:
310 	case X86_FEATURE_AVX512BW:
311 	case X86_FEATURE_AVX512CD:
312 	case X86_FEATURE_AVX512DQ:
313 	case X86_FEATURE_AVX512ER:
314 	case X86_FEATURE_AVX512IFMA:
315 	case X86_FEATURE_AVX512PF:
316 	case X86_FEATURE_AVX512VBMI:
317 	case X86_FEATURE_AVX512VL:
318 	case X86_FEATURE_AVX512_4FMAPS:
319 	case X86_FEATURE_AVX512_4VNNIW:
320 	case X86_FEATURE_AVX512_BF16:
321 	case X86_FEATURE_AVX512_BITALG:
322 	case X86_FEATURE_AVX512_FP16:
323 	case X86_FEATURE_AVX512_VBMI2:
324 	case X86_FEATURE_AVX512_VNNI:
325 	case X86_FEATURE_AVX512_VP2INTERSECT:
326 	case X86_FEATURE_AVX512_VPOPCNTDQ:
327 		if ((xcr0 & 0xe7) != 0xe7) {
328 			static bool warned;
329 
330 			if (!warned) {
331 				os_warn("Disabling AVX-512 support due to missing xstate features\n");
332 				warned = true;
333 			}
334 			return;
335 		}
336 		break;
337 	}
338 	set_cpu_cap(&boot_cpu_data, cap);
339 }
340 
341 static void __init parse_host_cpu_flags(char *line)
342 {
343 	u64 xcr0 = read_xcr0();
344 	int i;
345 
346 	for (i = 0; i < 32*NCAPINTS; i++) {
347 		if ((x86_cap_flags[i] != NULL) && strstr(line, x86_cap_flags[i]))
348 			validate_and_set_cpu_cap(i, xcr0);
349 	}
350 }
351 
352 static void __init parse_cache_line(char *line)
353 {
354 	long res;
355 	char *to_parse = strstr(line, ":");
356 	if (to_parse) {
357 		to_parse++;
358 		while (*to_parse != 0 && isspace(*to_parse)) {
359 			to_parse++;
360 		}
361 		if (kstrtoul(to_parse, 10, &res) == 0 && is_power_of_2(res))
362 			boot_cpu_data.cache_alignment = res;
363 		else
364 			boot_cpu_data.cache_alignment = L1_CACHE_BYTES;
365 	}
366 }
367 
368 static unsigned long __init get_top_address(char **envp)
369 {
370 	unsigned long top_addr = (unsigned long) &top_addr;
371 	int i;
372 
373 	/* The earliest variable should be after the program name in ELF */
374 	for (i = 0; envp[i]; i++) {
375 		if ((unsigned long) envp[i] > top_addr)
376 			top_addr = (unsigned long) envp[i];
377 	}
378 
379 	return PAGE_ALIGN(top_addr + 1);
380 }
381 
382 int __init linux_main(int argc, char **argv, char **envp)
383 {
384 	unsigned long avail, diff;
385 	unsigned long virtmem_size, max_physmem;
386 	unsigned long host_task_size;
387 	unsigned long stack;
388 	unsigned int i;
389 	int add;
390 
391 	for (i = 1; i < argc; i++) {
392 		if ((i == 1) && (argv[i][0] == ' '))
393 			continue;
394 		add = 1;
395 		uml_checksetup(argv[i], &add);
396 		if (add)
397 			add_arg(argv[i]);
398 	}
399 	if (have_root == 0)
400 		add_arg(DEFAULT_COMMAND_LINE_ROOT);
401 
402 	if (have_console == 0)
403 		add_arg(DEFAULT_COMMAND_LINE_CONSOLE);
404 
405 	host_task_size = get_top_address(envp);
406 	/* reserve a few pages for the stubs */
407 	stub_start = host_task_size - STUB_SIZE;
408 	host_task_size = stub_start;
409 
410 	/* Limit TASK_SIZE to what is addressable by the page table */
411 	task_size = host_task_size;
412 	if (task_size > (unsigned long long) PTRS_PER_PGD * PGDIR_SIZE)
413 		task_size = PTRS_PER_PGD * PGDIR_SIZE;
414 
415 	/*
416 	 * TASK_SIZE needs to be PGDIR_SIZE aligned or else exit_mmap craps
417 	 * out
418 	 */
419 	task_size = task_size & PGDIR_MASK;
420 
421 	/* OS sanity checks that need to happen before the kernel runs */
422 	os_early_checks();
423 
424 	get_host_cpu_features(parse_host_cpu_flags, parse_cache_line);
425 
426 	brk_start = (unsigned long) sbrk(0);
427 
428 	/*
429 	 * Increase physical memory size for exec-shield users
430 	 * so they actually get what they asked for. This should
431 	 * add zero for non-exec shield users
432 	 */
433 	diff = PAGE_ALIGN(brk_start) - PAGE_ALIGN((unsigned long) &_end);
434 	if (diff > 1024 * 1024) {
435 		os_info("Adding %ld bytes to physical memory to account for "
436 			"exec-shield gap\n", diff);
437 		physmem_size += diff;
438 	}
439 
440 	uml_physmem = (unsigned long) __binary_start & PAGE_MASK;
441 
442 	/* Reserve up to 4M after the current brk */
443 	uml_reserved = ROUND_4M(brk_start) + (1 << 22);
444 
445 	setup_machinename(init_utsname()->machine);
446 
447 	physmem_size = PAGE_ALIGN(physmem_size);
448 	max_physmem = TASK_SIZE - uml_physmem - MIN_VMALLOC;
449 	if (physmem_size > max_physmem) {
450 		physmem_size = max_physmem;
451 		os_info("Physical memory size shrunk to %llu bytes\n",
452 			physmem_size);
453 	}
454 
455 	high_physmem = uml_physmem + physmem_size;
456 
457 	start_vm = VMALLOC_START;
458 
459 	virtmem_size = physmem_size;
460 	stack = (unsigned long) argv;
461 	stack &= ~(1024 * 1024 - 1);
462 	avail = stack - start_vm;
463 	if (physmem_size > avail)
464 		virtmem_size = avail;
465 	end_vm = start_vm + virtmem_size;
466 
467 	if (virtmem_size < physmem_size)
468 		os_info("Kernel virtual memory size shrunk to %lu bytes\n",
469 			virtmem_size);
470 
471 	arch_task_struct_size = sizeof(struct task_struct) + host_fp_size;
472 
473 	os_flush_stdout();
474 
475 	return start_uml();
476 }
477 
478 int __init __weak read_initrd(void)
479 {
480 	return 0;
481 }
482 
483 void __init setup_arch(char **cmdline_p)
484 {
485 	u8 rng_seed[32];
486 
487 	stack_protections((unsigned long) init_task.stack);
488 	setup_physmem(uml_physmem, uml_reserved, physmem_size);
489 	uml_dtb_init();
490 	read_initrd();
491 
492 	strscpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
493 	*cmdline_p = command_line;
494 	setup_hostinfo(host_info, sizeof host_info);
495 	prefill_possible_map();
496 
497 	if (os_getrandom(rng_seed, sizeof(rng_seed), 0) == sizeof(rng_seed)) {
498 		add_bootloader_randomness(rng_seed, sizeof(rng_seed));
499 		memzero_explicit(rng_seed, sizeof(rng_seed));
500 	}
501 }
502 
503 void __init arch_cpu_finalize_init(void)
504 {
505 	arch_check_bugs();
506 	os_check_bugs();
507 }
508 
509 void apply_seal_endbr(s32 *start, s32 *end)
510 {
511 }
512 
513 void apply_retpolines(s32 *start, s32 *end)
514 {
515 }
516 
517 void apply_returns(s32 *start, s32 *end)
518 {
519 }
520 
521 void apply_fineibt(s32 *start_retpoline, s32 *end_retpoline,
522 		   s32 *start_cfi, s32 *end_cfi)
523 {
524 }
525 
526 void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
527 {
528 }
529 
530 void *text_poke(void *addr, const void *opcode, size_t len)
531 {
532 	/*
533 	 * In UML, the only reference to this function is in
534 	 * apply_relocate_add(), which shouldn't ever actually call this
535 	 * because UML doesn't have live patching.
536 	 */
537 	WARN_ON(1);
538 
539 	return memcpy(addr, opcode, len);
540 }
541 
542 void *text_poke_copy(void *addr, const void *opcode, size_t len)
543 {
544 	return text_poke(addr, opcode, len);
545 }
546 
547 void smp_text_poke_sync_each_cpu(void)
548 {
549 }
550 
551 void uml_pm_wake(void)
552 {
553 	pm_system_wakeup();
554 }
555 
556 #ifdef CONFIG_PM_SLEEP
557 static int um_suspend_valid(suspend_state_t state)
558 {
559 	return state == PM_SUSPEND_MEM;
560 }
561 
562 static int um_suspend_prepare(void)
563 {
564 	um_irqs_suspend();
565 	return 0;
566 }
567 
568 static int um_suspend_enter(suspend_state_t state)
569 {
570 	if (WARN_ON(state != PM_SUSPEND_MEM))
571 		return -EINVAL;
572 
573 	/*
574 	 * This is identical to the idle sleep, but we've just
575 	 * (during suspend) turned off all interrupt sources
576 	 * except for the ones we want, so now we can only wake
577 	 * up on something we actually want to wake up on. All
578 	 * timing has also been suspended.
579 	 */
580 	um_idle_sleep();
581 	return 0;
582 }
583 
584 static void um_suspend_finish(void)
585 {
586 	um_irqs_resume();
587 }
588 
589 const struct platform_suspend_ops um_suspend_ops = {
590 	.valid = um_suspend_valid,
591 	.prepare = um_suspend_prepare,
592 	.enter = um_suspend_enter,
593 	.finish = um_suspend_finish,
594 };
595 
596 static int init_pm_wake_signal(void)
597 {
598 	/*
599 	 * In external time-travel mode we can't use signals to wake up
600 	 * since that would mess with the scheduling. We'll have to do
601 	 * some additional work to support wakeup on virtio devices or
602 	 * similar, perhaps implementing a fake RTC controller that can
603 	 * trigger wakeup (and request the appropriate scheduling from
604 	 * the external scheduler when going to suspend.)
605 	 */
606 	if (time_travel_mode != TT_MODE_EXTERNAL)
607 		register_pm_wake_signal();
608 
609 	suspend_set_ops(&um_suspend_ops);
610 
611 	return 0;
612 }
613 
614 late_initcall(init_pm_wake_signal);
615 #endif
616