Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/arch/um/kernel/um_arch.c
49961 views
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 void __init 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
}
275
276
static void __init parse_cache_line(char *line)
277
{
278
long res;
279
char *to_parse = strstr(line, ":");
280
if (to_parse) {
281
to_parse++;
282
while (*to_parse != 0 && isspace(*to_parse)) {
283
to_parse++;
284
}
285
if (kstrtoul(to_parse, 10, &res) == 0 && is_power_of_2(res))
286
boot_cpu_data.cache_alignment = res;
287
else
288
boot_cpu_data.cache_alignment = L1_CACHE_BYTES;
289
}
290
}
291
292
static unsigned long __init get_top_address(char **envp)
293
{
294
unsigned long top_addr = (unsigned long) &top_addr;
295
int i;
296
297
/* The earliest variable should be after the program name in ELF */
298
for (i = 0; envp[i]; i++) {
299
if ((unsigned long) envp[i] > top_addr)
300
top_addr = (unsigned long) envp[i];
301
}
302
303
return PAGE_ALIGN(top_addr + 1);
304
}
305
306
int __init linux_main(int argc, char **argv, char **envp)
307
{
308
unsigned long avail, diff;
309
unsigned long virtmem_size, max_physmem;
310
unsigned long host_task_size;
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
diff = PAGE_ALIGN(brk_start) - PAGE_ALIGN((unsigned long) &_end);
358
if (diff > 1024 * 1024) {
359
os_info("Adding %ld bytes to physical memory to account for "
360
"exec-shield gap\n", diff);
361
physmem_size += diff;
362
}
363
364
uml_physmem = (unsigned long) __binary_start & PAGE_MASK;
365
366
/* Reserve up to 4M after the current brk */
367
uml_reserved = ROUND_4M(brk_start) + (1 << 22);
368
369
setup_machinename(init_utsname()->machine);
370
371
physmem_size = PAGE_ALIGN(physmem_size);
372
max_physmem = TASK_SIZE - uml_physmem - MIN_VMALLOC;
373
if (physmem_size > max_physmem) {
374
physmem_size = max_physmem;
375
os_info("Physical memory size shrunk to %llu bytes\n",
376
physmem_size);
377
}
378
379
high_physmem = uml_physmem + physmem_size;
380
381
start_vm = VMALLOC_START;
382
383
virtmem_size = physmem_size;
384
stack = (unsigned long) argv;
385
stack &= ~(1024 * 1024 - 1);
386
avail = stack - start_vm;
387
if (physmem_size > avail)
388
virtmem_size = avail;
389
end_vm = start_vm + virtmem_size;
390
391
if (virtmem_size < physmem_size)
392
os_info("Kernel virtual memory size shrunk to %lu bytes\n",
393
virtmem_size);
394
395
arch_task_struct_size = sizeof(struct task_struct) + host_fp_size;
396
397
os_flush_stdout();
398
399
return start_uml();
400
}
401
402
int __init __weak read_initrd(void)
403
{
404
return 0;
405
}
406
407
void __init setup_arch(char **cmdline_p)
408
{
409
u8 rng_seed[32];
410
411
stack_protections((unsigned long) init_task.stack);
412
setup_physmem(uml_physmem, uml_reserved, physmem_size);
413
uml_dtb_init();
414
read_initrd();
415
416
paging_init();
417
strscpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
418
*cmdline_p = command_line;
419
setup_hostinfo(host_info, sizeof host_info);
420
prefill_possible_map();
421
422
if (os_getrandom(rng_seed, sizeof(rng_seed), 0) == sizeof(rng_seed)) {
423
add_bootloader_randomness(rng_seed, sizeof(rng_seed));
424
memzero_explicit(rng_seed, sizeof(rng_seed));
425
}
426
}
427
428
void __init arch_cpu_finalize_init(void)
429
{
430
arch_check_bugs();
431
os_check_bugs();
432
}
433
434
void apply_seal_endbr(s32 *start, s32 *end)
435
{
436
}
437
438
void apply_retpolines(s32 *start, s32 *end)
439
{
440
}
441
442
void apply_returns(s32 *start, s32 *end)
443
{
444
}
445
446
void apply_fineibt(s32 *start_retpoline, s32 *end_retpoline,
447
s32 *start_cfi, s32 *end_cfi)
448
{
449
}
450
451
void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
452
{
453
}
454
455
#if IS_ENABLED(CONFIG_SMP)
456
void alternatives_smp_module_add(struct module *mod, char *name,
457
void *locks, void *locks_end,
458
void *text, void *text_end)
459
{
460
}
461
462
void alternatives_smp_module_del(struct module *mod)
463
{
464
}
465
#endif
466
467
void *text_poke(void *addr, const void *opcode, size_t len)
468
{
469
/*
470
* In UML, the only reference to this function is in
471
* apply_relocate_add(), which shouldn't ever actually call this
472
* because UML doesn't have live patching.
473
*/
474
WARN_ON(1);
475
476
return memcpy(addr, opcode, len);
477
}
478
479
void *text_poke_copy(void *addr, const void *opcode, size_t len)
480
{
481
return text_poke(addr, opcode, len);
482
}
483
484
void smp_text_poke_sync_each_cpu(void)
485
{
486
}
487
488
void uml_pm_wake(void)
489
{
490
pm_system_wakeup();
491
}
492
493
#ifdef CONFIG_PM_SLEEP
494
static int um_suspend_valid(suspend_state_t state)
495
{
496
return state == PM_SUSPEND_MEM;
497
}
498
499
static int um_suspend_prepare(void)
500
{
501
um_irqs_suspend();
502
return 0;
503
}
504
505
static int um_suspend_enter(suspend_state_t state)
506
{
507
if (WARN_ON(state != PM_SUSPEND_MEM))
508
return -EINVAL;
509
510
/*
511
* This is identical to the idle sleep, but we've just
512
* (during suspend) turned off all interrupt sources
513
* except for the ones we want, so now we can only wake
514
* up on something we actually want to wake up on. All
515
* timing has also been suspended.
516
*/
517
um_idle_sleep();
518
return 0;
519
}
520
521
static void um_suspend_finish(void)
522
{
523
um_irqs_resume();
524
}
525
526
const struct platform_suspend_ops um_suspend_ops = {
527
.valid = um_suspend_valid,
528
.prepare = um_suspend_prepare,
529
.enter = um_suspend_enter,
530
.finish = um_suspend_finish,
531
};
532
533
static int init_pm_wake_signal(void)
534
{
535
/*
536
* In external time-travel mode we can't use signals to wake up
537
* since that would mess with the scheduling. We'll have to do
538
* some additional work to support wakeup on virtio devices or
539
* similar, perhaps implementing a fake RTC controller that can
540
* trigger wakeup (and request the appropriate scheduling from
541
* the external scheduler when going to suspend.)
542
*/
543
if (time_travel_mode != TT_MODE_EXTERNAL)
544
register_pm_wake_signal();
545
546
suspend_set_ops(&um_suspend_ops);
547
548
return 0;
549
}
550
551
late_initcall(init_pm_wake_signal);
552
#endif
553
554