Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
0004  */
0005 
0006 #include <linux/delay.h>
0007 #include <linux/init.h>
0008 #include <linux/mm.h>
0009 #include <linux/ctype.h>
0010 #include <linux/module.h>
0011 #include <linux/panic_notifier.h>
0012 #include <linux/seq_file.h>
0013 #include <linux/string.h>
0014 #include <linux/utsname.h>
0015 #include <linux/sched.h>
0016 #include <linux/sched/task.h>
0017 #include <linux/kmsg_dump.h>
0018 #include <linux/suspend.h>
0019 #include <linux/random.h>
0020 
0021 #include <asm/processor.h>
0022 #include <asm/cpufeature.h>
0023 #include <asm/sections.h>
0024 #include <asm/setup.h>
0025 #include <as-layout.h>
0026 #include <arch.h>
0027 #include <init.h>
0028 #include <kern.h>
0029 #include <kern_util.h>
0030 #include <mem_user.h>
0031 #include <os.h>
0032 
0033 #include "um_arch.h"
0034 
0035 #define DEFAULT_COMMAND_LINE_ROOT "root=98:0"
0036 #define DEFAULT_COMMAND_LINE_CONSOLE "console=tty0"
0037 
0038 /* Changed in add_arg and setup_arch, which run before SMP is started */
0039 static char __initdata command_line[COMMAND_LINE_SIZE] = { 0 };
0040 
0041 static void __init add_arg(char *arg)
0042 {
0043     if (strlen(command_line) + strlen(arg) + 1 > COMMAND_LINE_SIZE) {
0044         os_warn("add_arg: Too many command line arguments!\n");
0045         exit(1);
0046     }
0047     if (strlen(command_line) > 0)
0048         strcat(command_line, " ");
0049     strcat(command_line, arg);
0050 }
0051 
0052 /*
0053  * These fields are initialized at boot time and not changed.
0054  * XXX This structure is used only in the non-SMP case.  Maybe this
0055  * should be moved to smp.c.
0056  */
0057 struct cpuinfo_um boot_cpu_data = {
0058     .loops_per_jiffy    = 0,
0059     .ipi_pipe       = { -1, -1 },
0060     .cache_alignment    = L1_CACHE_BYTES,
0061     .x86_capability     = { 0 }
0062 };
0063 
0064 EXPORT_SYMBOL(boot_cpu_data);
0065 
0066 union thread_union cpu0_irqstack
0067     __section(".data..init_irqstack") =
0068         { .thread_info = INIT_THREAD_INFO(init_task) };
0069 
0070 /* Changed in setup_arch, which is called in early boot */
0071 static char host_info[(__NEW_UTS_LEN + 1) * 5];
0072 
0073 static int show_cpuinfo(struct seq_file *m, void *v)
0074 {
0075     int i = 0;
0076 
0077     seq_printf(m, "processor\t: %d\n", i);
0078     seq_printf(m, "vendor_id\t: User Mode Linux\n");
0079     seq_printf(m, "model name\t: UML\n");
0080     seq_printf(m, "mode\t\t: skas\n");
0081     seq_printf(m, "host\t\t: %s\n", host_info);
0082     seq_printf(m, "fpu\t\t: %s\n", cpu_has(&boot_cpu_data, X86_FEATURE_FPU) ? "yes" : "no");
0083     seq_printf(m, "flags\t\t:");
0084     for (i = 0; i < 32*NCAPINTS; i++)
0085         if (cpu_has(&boot_cpu_data, i) && (x86_cap_flags[i] != NULL))
0086             seq_printf(m, " %s", x86_cap_flags[i]);
0087     seq_printf(m, "\n");
0088     seq_printf(m, "cache_alignment\t: %d\n", boot_cpu_data.cache_alignment);
0089     seq_printf(m, "bogomips\t: %lu.%02lu\n",
0090            loops_per_jiffy/(500000/HZ),
0091            (loops_per_jiffy/(5000/HZ)) % 100);
0092 
0093 
0094     return 0;
0095 }
0096 
0097 static void *c_start(struct seq_file *m, loff_t *pos)
0098 {
0099     return *pos < NR_CPUS ? cpu_data + *pos : NULL;
0100 }
0101 
0102 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
0103 {
0104     ++*pos;
0105     return c_start(m, pos);
0106 }
0107 
0108 static void c_stop(struct seq_file *m, void *v)
0109 {
0110 }
0111 
0112 const struct seq_operations cpuinfo_op = {
0113     .start  = c_start,
0114     .next   = c_next,
0115     .stop   = c_stop,
0116     .show   = show_cpuinfo,
0117 };
0118 
0119 /* Set in linux_main */
0120 unsigned long uml_physmem;
0121 EXPORT_SYMBOL(uml_physmem);
0122 
0123 unsigned long uml_reserved; /* Also modified in mem_init */
0124 unsigned long start_vm;
0125 unsigned long end_vm;
0126 
0127 /* Set in uml_ncpus_setup */
0128 int ncpus = 1;
0129 
0130 /* Set in early boot */
0131 static int have_root __initdata;
0132 static int have_console __initdata;
0133 
0134 /* Set in uml_mem_setup and modified in linux_main */
0135 long long physmem_size = 32 * 1024 * 1024;
0136 EXPORT_SYMBOL(physmem_size);
0137 
0138 static const char *usage_string =
0139 "User Mode Linux v%s\n"
0140 "   available at http://user-mode-linux.sourceforge.net/\n\n";
0141 
0142 static int __init uml_version_setup(char *line, int *add)
0143 {
0144     /* Explicitly use printf() to show version in stdout */
0145     printf("%s\n", init_utsname()->release);
0146     exit(0);
0147 
0148     return 0;
0149 }
0150 
0151 __uml_setup("--version", uml_version_setup,
0152 "--version\n"
0153 "    Prints the version number of the kernel.\n\n"
0154 );
0155 
0156 static int __init uml_root_setup(char *line, int *add)
0157 {
0158     have_root = 1;
0159     return 0;
0160 }
0161 
0162 __uml_setup("root=", uml_root_setup,
0163 "root=<file containing the root fs>\n"
0164 "    This is actually used by the generic kernel in exactly the same\n"
0165 "    way as in any other kernel. If you configure a number of block\n"
0166 "    devices and want to boot off something other than ubd0, you \n"
0167 "    would use something like:\n"
0168 "        root=/dev/ubd5\n\n"
0169 );
0170 
0171 static int __init no_skas_debug_setup(char *line, int *add)
0172 {
0173     os_warn("'debug' is not necessary to gdb UML in skas mode - run\n");
0174     os_warn("'gdb linux'\n");
0175 
0176     return 0;
0177 }
0178 
0179 __uml_setup("debug", no_skas_debug_setup,
0180 "debug\n"
0181 "    this flag is not needed to run gdb on UML in skas mode\n\n"
0182 );
0183 
0184 static int __init uml_console_setup(char *line, int *add)
0185 {
0186     have_console = 1;
0187     return 0;
0188 }
0189 
0190 __uml_setup("console=", uml_console_setup,
0191 "console=<preferred console>\n"
0192 "    Specify the preferred console output driver\n\n"
0193 );
0194 
0195 static int __init Usage(char *line, int *add)
0196 {
0197     const char **p;
0198 
0199     printf(usage_string, init_utsname()->release);
0200     p = &__uml_help_start;
0201     /* Explicitly use printf() to show help in stdout */
0202     while (p < &__uml_help_end) {
0203         printf("%s", *p);
0204         p++;
0205     }
0206     exit(0);
0207     return 0;
0208 }
0209 
0210 __uml_setup("--help", Usage,
0211 "--help\n"
0212 "    Prints this message.\n\n"
0213 );
0214 
0215 static void __init uml_checksetup(char *line, int *add)
0216 {
0217     struct uml_param *p;
0218 
0219     p = &__uml_setup_start;
0220     while (p < &__uml_setup_end) {
0221         size_t n;
0222 
0223         n = strlen(p->str);
0224         if (!strncmp(line, p->str, n) && p->setup_func(line + n, add))
0225             return;
0226         p++;
0227     }
0228 }
0229 
0230 static void __init uml_postsetup(void)
0231 {
0232     initcall_t *p;
0233 
0234     p = &__uml_postsetup_start;
0235     while (p < &__uml_postsetup_end) {
0236         (*p)();
0237         p++;
0238     }
0239     return;
0240 }
0241 
0242 static int panic_exit(struct notifier_block *self, unsigned long unused1,
0243               void *unused2)
0244 {
0245     kmsg_dump(KMSG_DUMP_PANIC);
0246     bust_spinlocks(1);
0247     bust_spinlocks(0);
0248     uml_exitcode = 1;
0249     os_dump_core();
0250     return 0;
0251 }
0252 
0253 static struct notifier_block panic_exit_notifier = {
0254     .notifier_call      = panic_exit,
0255     .next           = NULL,
0256     .priority       = 0
0257 };
0258 
0259 void uml_finishsetup(void)
0260 {
0261     atomic_notifier_chain_register(&panic_notifier_list,
0262                        &panic_exit_notifier);
0263 
0264     uml_postsetup();
0265 
0266     new_thread_handler();
0267 }
0268 
0269 /* Set during early boot */
0270 unsigned long stub_start;
0271 unsigned long task_size;
0272 EXPORT_SYMBOL(task_size);
0273 
0274 unsigned long host_task_size;
0275 
0276 unsigned long brk_start;
0277 unsigned long end_iomem;
0278 EXPORT_SYMBOL(end_iomem);
0279 
0280 #define MIN_VMALLOC (32 * 1024 * 1024)
0281 
0282 static void parse_host_cpu_flags(char *line)
0283 {
0284     int i;
0285     for (i = 0; i < 32*NCAPINTS; i++) {
0286         if ((x86_cap_flags[i] != NULL) && strstr(line, x86_cap_flags[i]))
0287             set_cpu_cap(&boot_cpu_data, i);
0288     }
0289 }
0290 static void parse_cache_line(char *line)
0291 {
0292     long res;
0293     char *to_parse = strstr(line, ":");
0294     if (to_parse) {
0295         to_parse++;
0296         while (*to_parse != 0 && isspace(*to_parse)) {
0297             to_parse++;
0298         }
0299         if (kstrtoul(to_parse, 10, &res) == 0 && is_power_of_2(res))
0300             boot_cpu_data.cache_alignment = res;
0301         else
0302             boot_cpu_data.cache_alignment = L1_CACHE_BYTES;
0303     }
0304 }
0305 
0306 int __init linux_main(int argc, char **argv)
0307 {
0308     unsigned long avail, diff;
0309     unsigned long virtmem_size, max_physmem;
0310     unsigned long stack;
0311     unsigned int i;
0312     int add;
0313 
0314     for (i = 1; i < argc; i++) {
0315         if ((i == 1) && (argv[i][0] == ' '))
0316             continue;
0317         add = 1;
0318         uml_checksetup(argv[i], &add);
0319         if (add)
0320             add_arg(argv[i]);
0321     }
0322     if (have_root == 0)
0323         add_arg(DEFAULT_COMMAND_LINE_ROOT);
0324 
0325     if (have_console == 0)
0326         add_arg(DEFAULT_COMMAND_LINE_CONSOLE);
0327 
0328     host_task_size = os_get_top_address();
0329     /* reserve two pages for the stubs */
0330     host_task_size -= 2 * PAGE_SIZE;
0331     stub_start = host_task_size;
0332 
0333     /*
0334      * TASK_SIZE needs to be PGDIR_SIZE aligned or else exit_mmap craps
0335      * out
0336      */
0337     task_size = host_task_size & PGDIR_MASK;
0338 
0339     /* OS sanity checks that need to happen before the kernel runs */
0340     os_early_checks();
0341 
0342     get_host_cpu_features(parse_host_cpu_flags, parse_cache_line);
0343 
0344     brk_start = (unsigned long) sbrk(0);
0345 
0346     /*
0347      * Increase physical memory size for exec-shield users
0348      * so they actually get what they asked for. This should
0349      * add zero for non-exec shield users
0350      */
0351 
0352     diff = UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
0353     if (diff > 1024 * 1024) {
0354         os_info("Adding %ld bytes to physical memory to account for "
0355             "exec-shield gap\n", diff);
0356         physmem_size += UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
0357     }
0358 
0359     uml_physmem = (unsigned long) __binary_start & PAGE_MASK;
0360 
0361     /* Reserve up to 4M after the current brk */
0362     uml_reserved = ROUND_4M(brk_start) + (1 << 22);
0363 
0364     setup_machinename(init_utsname()->machine);
0365 
0366     highmem = 0;
0367     iomem_size = (iomem_size + PAGE_SIZE - 1) & PAGE_MASK;
0368     max_physmem = TASK_SIZE - uml_physmem - iomem_size - MIN_VMALLOC;
0369 
0370     /*
0371      * Zones have to begin on a 1 << MAX_ORDER page boundary,
0372      * so this makes sure that's true for highmem
0373      */
0374     max_physmem &= ~((1 << (PAGE_SHIFT + MAX_ORDER)) - 1);
0375     if (physmem_size + iomem_size > max_physmem) {
0376         highmem = physmem_size + iomem_size - max_physmem;
0377         physmem_size -= highmem;
0378     }
0379 
0380     high_physmem = uml_physmem + physmem_size;
0381     end_iomem = high_physmem + iomem_size;
0382     high_memory = (void *) end_iomem;
0383 
0384     start_vm = VMALLOC_START;
0385 
0386     virtmem_size = physmem_size;
0387     stack = (unsigned long) argv;
0388     stack &= ~(1024 * 1024 - 1);
0389     avail = stack - start_vm;
0390     if (physmem_size > avail)
0391         virtmem_size = avail;
0392     end_vm = start_vm + virtmem_size;
0393 
0394     if (virtmem_size < physmem_size)
0395         os_info("Kernel virtual memory size shrunk to %lu bytes\n",
0396             virtmem_size);
0397 
0398     os_flush_stdout();
0399 
0400     return start_uml();
0401 }
0402 
0403 int __init __weak read_initrd(void)
0404 {
0405     return 0;
0406 }
0407 
0408 void __init setup_arch(char **cmdline_p)
0409 {
0410     u8 rng_seed[32];
0411 
0412     stack_protections((unsigned long) &init_thread_info);
0413     setup_physmem(uml_physmem, uml_reserved, physmem_size, highmem);
0414     mem_total_pages(physmem_size, iomem_size, highmem);
0415     uml_dtb_init();
0416     read_initrd();
0417 
0418     paging_init();
0419     strlcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
0420     *cmdline_p = command_line;
0421     setup_hostinfo(host_info, sizeof host_info);
0422 
0423     if (os_getrandom(rng_seed, sizeof(rng_seed), 0) == sizeof(rng_seed)) {
0424         add_bootloader_randomness(rng_seed, sizeof(rng_seed));
0425         memzero_explicit(rng_seed, sizeof(rng_seed));
0426     }
0427 }
0428 
0429 void __init check_bugs(void)
0430 {
0431     arch_check_bugs();
0432     os_check_bugs();
0433 }
0434 
0435 void apply_ibt_endbr(s32 *start, s32 *end)
0436 {
0437 }
0438 
0439 void apply_retpolines(s32 *start, s32 *end)
0440 {
0441 }
0442 
0443 void apply_returns(s32 *start, s32 *end)
0444 {
0445 }
0446 
0447 void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
0448 {
0449 }
0450 
0451 void *text_poke(void *addr, const void *opcode, size_t len)
0452 {
0453     /*
0454      * In UML, the only reference to this function is in
0455      * apply_relocate_add(), which shouldn't ever actually call this
0456      * because UML doesn't have live patching.
0457      */
0458     WARN_ON(1);
0459 
0460     return memcpy(addr, opcode, len);
0461 }
0462 
0463 void text_poke_sync(void)
0464 {
0465 }
0466 
0467 void uml_pm_wake(void)
0468 {
0469     pm_system_wakeup();
0470 }
0471 
0472 #ifdef CONFIG_PM_SLEEP
0473 static int um_suspend_valid(suspend_state_t state)
0474 {
0475     return state == PM_SUSPEND_MEM;
0476 }
0477 
0478 static int um_suspend_prepare(void)
0479 {
0480     um_irqs_suspend();
0481     return 0;
0482 }
0483 
0484 static int um_suspend_enter(suspend_state_t state)
0485 {
0486     if (WARN_ON(state != PM_SUSPEND_MEM))
0487         return -EINVAL;
0488 
0489     /*
0490      * This is identical to the idle sleep, but we've just
0491      * (during suspend) turned off all interrupt sources
0492      * except for the ones we want, so now we can only wake
0493      * up on something we actually want to wake up on. All
0494      * timing has also been suspended.
0495      */
0496     um_idle_sleep();
0497     return 0;
0498 }
0499 
0500 static void um_suspend_finish(void)
0501 {
0502     um_irqs_resume();
0503 }
0504 
0505 const struct platform_suspend_ops um_suspend_ops = {
0506     .valid = um_suspend_valid,
0507     .prepare = um_suspend_prepare,
0508     .enter = um_suspend_enter,
0509     .finish = um_suspend_finish,
0510 };
0511 
0512 static int init_pm_wake_signal(void)
0513 {
0514     /*
0515      * In external time-travel mode we can't use signals to wake up
0516      * since that would mess with the scheduling. We'll have to do
0517      * some additional work to support wakeup on virtio devices or
0518      * similar, perhaps implementing a fake RTC controller that can
0519      * trigger wakeup (and request the appropriate scheduling from
0520      * the external scheduler when going to suspend.)
0521      */
0522     if (time_travel_mode != TT_MODE_EXTERNAL)
0523         register_pm_wake_signal();
0524 
0525     suspend_set_ops(&um_suspend_ops);
0526 
0527     return 0;
0528 }
0529 
0530 late_initcall(init_pm_wake_signal);
0531 #endif