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