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