blob: a818ccef30ca2a4a3685c4bc1d89d58ea4922236 [file] [log] [blame]
Gennady Sharapovea2ba7d2006-01-08 01:01:31 -08001/*
Jeff Dikeba180fd2007-10-16 01:27:00 -07002 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Jeff Dikec5d4bb12008-02-04 22:31:14 -08006#include <linux/delay.h>
7#include <linux/init.h>
8#include <linux/mm.h>
9#include <linux/module.h>
10#include <linux/seq_file.h>
11#include <linux/string.h>
12#include <linux/utsname.h>
Thomas Gleixner5b408242012-05-03 09:03:00 +000013#include <linux/sched.h>
Ingo Molnar9164bb42017-02-04 01:20:53 +010014#include <linux/sched/task.h>
Thomas Meyer04a41842015-03-28 10:07:37 +010015#include <linux/kmsg_dump.h>
Ingo Molnar9164bb42017-02-04 01:20:53 +010016
Jeff Dikec5d4bb12008-02-04 22:31:14 -080017#include <asm/pgtable.h>
18#include <asm/processor.h>
Geert Uytterhoeven33a7d422013-11-12 20:42:27 +010019#include <asm/sections.h>
Jeff Dikec5d4bb12008-02-04 22:31:14 -080020#include <asm/setup.h>
Al Viro37185b32012-10-08 03:27:32 +010021#include <as-layout.h>
22#include <arch.h>
23#include <init.h>
24#include <kern.h>
25#include <kern_util.h>
26#include <mem_user.h>
27#include <os.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#define DEFAULT_COMMAND_LINE "root=98:0"
30
Jeff Dike1d1497e2007-05-06 14:51:26 -070031/* Changed in add_arg and setup_arch, which run before SMP is started */
Alon Bar-Lev7a3a06d02007-02-12 00:54:26 -080032static char __initdata command_line[COMMAND_LINE_SIZE] = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Alon Bar-Lev7a3a06d02007-02-12 00:54:26 -080034static void __init add_arg(char *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
36 if (strlen(command_line) + strlen(arg) + 1 > COMMAND_LINE_SIZE) {
Masami Hiramatsu0936d4f2017-05-18 02:19:31 +090037 os_warn("add_arg: Too many command line arguments!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 exit(1);
39 }
Jeff Dikeba180fd2007-10-16 01:27:00 -070040 if (strlen(command_line) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 strcat(command_line, " ");
42 strcat(command_line, arg);
43}
44
Jeff Dike1d1497e2007-05-06 14:51:26 -070045/*
46 * These fields are initialized at boot time and not changed.
47 * XXX This structure is used only in the non-SMP case. Maybe this
48 * should be moved to smp.c.
49 */
50struct cpuinfo_um boot_cpu_data = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 .loops_per_jiffy = 0,
52 .ipi_pipe = { -1, -1 }
53};
54
Thomas Gleixner5b408242012-05-03 09:03:00 +000055union thread_union cpu0_irqstack
56 __attribute__((__section__(".data..init_irqstack"))) =
David Howells05008712018-01-02 15:12:01 +000057 { .thread_info = INIT_THREAD_INFO(init_task) };
Thomas Gleixner5b408242012-05-03 09:03:00 +000058
Jeff Dikeb4ffb6a2007-05-06 14:50:59 -070059/* Changed in setup_arch, which is called in early boot */
60static char host_info[(__NEW_UTS_LEN + 1) * 5];
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static int show_cpuinfo(struct seq_file *m, void *v)
63{
64 int index = 0;
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 seq_printf(m, "processor\t: %d\n", index);
67 seq_printf(m, "vendor_id\t: User Mode Linux\n");
68 seq_printf(m, "model name\t: UML\n");
Jeff Dike6aa802c2007-10-16 01:26:56 -070069 seq_printf(m, "mode\t\t: skas\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 seq_printf(m, "host\t\t: %s\n", host_info);
71 seq_printf(m, "bogomips\t: %lu.%02lu\n\n",
72 loops_per_jiffy/(500000/HZ),
73 (loops_per_jiffy/(5000/HZ)) % 100);
74
Jeff Dikea5ed1ff2007-05-06 14:50:58 -070075 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
78static void *c_start(struct seq_file *m, loff_t *pos)
79{
80 return *pos < NR_CPUS ? cpu_data + *pos : NULL;
81}
82
83static void *c_next(struct seq_file *m, void *v, loff_t *pos)
84{
85 ++*pos;
86 return c_start(m, pos);
87}
88
89static void c_stop(struct seq_file *m, void *v)
90{
91}
92
Jeff Dike5e7672e2006-09-27 01:50:33 -070093const struct seq_operations cpuinfo_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 .start = c_start,
95 .next = c_next,
96 .stop = c_stop,
97 .show = show_cpuinfo,
98};
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/* Set in linux_main */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101unsigned long uml_physmem;
Al Viro73395a02011-08-18 20:14:10 +0100102EXPORT_SYMBOL(uml_physmem);
103
Jeff Dike1d1497e2007-05-06 14:51:26 -0700104unsigned long uml_reserved; /* Also modified in mem_init */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105unsigned long start_vm;
106unsigned long end_vm;
Jeff Dike1d1497e2007-05-06 14:51:26 -0700107
108/* Set in uml_ncpus_setup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109int ncpus = 1;
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/* Set in early boot */
112static int have_root __initdata = 0;
Jeff Dike1d1497e2007-05-06 14:51:26 -0700113
114/* Set in uml_mem_setup and modified in linux_main */
Jeff Dikeae173812005-11-07 00:58:57 -0800115long long physmem_size = 32 * 1024 * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
WANG Cong3af9c5b2008-04-28 02:13:52 -0700117static const char *usage_string =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118"User Mode Linux v%s\n"
119" available at http://user-mode-linux.sourceforge.net/\n\n";
120
121static int __init uml_version_setup(char *line, int *add)
122{
Masami Hiramatsu0936d4f2017-05-18 02:19:31 +0900123 /* Explicitly use printf() to show version in stdout */
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700124 printf("%s\n", init_utsname()->release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 exit(0);
126
127 return 0;
128}
129
130__uml_setup("--version", uml_version_setup,
131"--version\n"
132" Prints the version number of the kernel.\n\n"
133);
134
135static int __init uml_root_setup(char *line, int *add)
136{
137 have_root = 1;
138 return 0;
139}
140
141__uml_setup("root=", uml_root_setup,
142"root=<file containing the root fs>\n"
143" This is actually used by the generic kernel in exactly the same\n"
144" way as in any other kernel. If you configure a number of block\n"
145" devices and want to boot off something other than ubd0, you \n"
146" would use something like:\n"
147" root=/dev/ubd5\n\n"
148);
149
Jeff Dikefbd55772006-02-07 12:58:40 -0800150static int __init no_skas_debug_setup(char *line, int *add)
151{
Masami Hiramatsu0936d4f2017-05-18 02:19:31 +0900152 os_warn("'debug' is not necessary to gdb UML in skas mode - run\n");
153 os_warn("'gdb linux'\n");
Jeff Dikefbd55772006-02-07 12:58:40 -0800154
155 return 0;
156}
157
158__uml_setup("debug", no_skas_debug_setup,
159"debug\n"
160" this flag is not needed to run gdb on UML in skas mode\n\n"
161);
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163static int __init Usage(char *line, int *add)
164{
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700165 const char **p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700167 printf(usage_string, init_utsname()->release);
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700168 p = &__uml_help_start;
Masami Hiramatsu0936d4f2017-05-18 02:19:31 +0900169 /* Explicitly use printf() to show help in stdout */
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700170 while (p < &__uml_help_end) {
171 printf("%s", *p);
172 p++;
173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return 0;
176}
177
178__uml_setup("--help", Usage,
179"--help\n"
180" Prints this message.\n\n"
181);
182
Karol Swietlicki6b7e9672008-02-04 22:30:50 -0800183static void __init uml_checksetup(char *line, int *add)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 struct uml_param *p;
186
187 p = &__uml_setup_start;
Jeff Dikec5d4bb12008-02-04 22:31:14 -0800188 while (p < &__uml_setup_end) {
WANG Cong3af9c5b2008-04-28 02:13:52 -0700189 size_t n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 n = strlen(p->str);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700192 if (!strncmp(line, p->str, n) && p->setup_func(line + n, add))
Karol Swietlicki6b7e9672008-02-04 22:30:50 -0800193 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 p++;
195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198static void __init uml_postsetup(void)
199{
200 initcall_t *p;
201
202 p = &__uml_postsetup_start;
Jeff Dikec5d4bb12008-02-04 22:31:14 -0800203 while (p < &__uml_postsetup_end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 (*p)();
205 p++;
206 }
207 return;
208}
209
Jeff Dike0983a882008-02-04 22:31:08 -0800210static int panic_exit(struct notifier_block *self, unsigned long unused1,
211 void *unused2)
212{
Thomas Meyer04a41842015-03-28 10:07:37 +0100213 kmsg_dump(KMSG_DUMP_PANIC);
Jeff Dike0983a882008-02-04 22:31:08 -0800214 bust_spinlocks(1);
Jeff Dike0983a882008-02-04 22:31:08 -0800215 bust_spinlocks(0);
216 uml_exitcode = 1;
217 os_dump_core();
218 return 0;
219}
220
221static struct notifier_block panic_exit_notifier = {
222 .notifier_call = panic_exit,
223 .next = NULL,
224 .priority = 0
225};
226
Thomas Meyer33bbc302015-03-28 09:59:46 +0100227void uml_finishsetup(void)
228{
229 atomic_notifier_chain_register(&panic_notifier_list,
230 &panic_exit_notifier);
231
232 uml_postsetup();
233
234 new_thread_handler();
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/* Set during early boot */
Jeff Dike536788f2008-02-08 04:22:07 -0800238unsigned long task_size;
239EXPORT_SYMBOL(task_size);
240
241unsigned long host_task_size;
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243unsigned long brk_start;
244unsigned long end_iomem;
245EXPORT_SYMBOL(end_iomem);
246
247#define MIN_VMALLOC (32 * 1024 * 1024)
248
Alon Bar-Lev7a3a06d02007-02-12 00:54:26 -0800249int __init linux_main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 unsigned long avail, diff;
252 unsigned long virtmem_size, max_physmem;
Jeff Dike60a29882008-05-12 14:01:57 -0700253 unsigned long stack;
WANG Cong3af9c5b2008-04-28 02:13:52 -0700254 unsigned int i;
255 int add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Jeff Dikeba180fd2007-10-16 01:27:00 -0700257 for (i = 1; i < argc; i++) {
258 if ((i == 1) && (argv[i][0] == ' '))
259 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 add = 1;
261 uml_checksetup(argv[i], &add);
262 if (add)
263 add_arg(argv[i]);
264 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700265 if (have_root == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 add_arg(DEFAULT_COMMAND_LINE);
267
Tom Spink40fb16a2008-06-05 22:46:12 -0700268 host_task_size = os_get_top_address();
Jeff Dike536788f2008-02-08 04:22:07 -0800269 /*
270 * TASK_SIZE needs to be PGDIR_SIZE aligned or else exit_mmap craps
271 * out
272 */
273 task_size = host_task_size & PGDIR_MASK;
274
Jeff Dikeba180fd2007-10-16 01:27:00 -0700275 /* OS sanity checks that need to happen before the kernel runs */
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700276 os_early_checks();
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 brk_start = (unsigned long) sbrk(0);
Jeff Dike77bf4402007-10-16 01:26:58 -0700279
Jeff Dikeba180fd2007-10-16 01:27:00 -0700280 /*
281 * Increase physical memory size for exec-shield users
282 * so they actually get what they asked for. This should
283 * add zero for non-exec shield users
284 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 diff = UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700287 if (diff > 1024 * 1024) {
Masami Hiramatsud3878bb2017-05-18 02:17:14 +0900288 os_info("Adding %ld bytes to physical memory to account for "
289 "exec-shield gap\n", diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 physmem_size += UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
291 }
292
Nicolas Iooss05eacfd2014-10-12 13:02:12 +0200293 uml_physmem = (unsigned long) __binary_start & PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 /* Reserve up to 4M after the current brk */
296 uml_reserved = ROUND_4M(brk_start) + (1 << 22);
297
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700298 setup_machinename(init_utsname()->machine);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 highmem = 0;
301 iomem_size = (iomem_size + PAGE_SIZE - 1) & PAGE_MASK;
Jeff Dike536788f2008-02-08 04:22:07 -0800302 max_physmem = TASK_SIZE - uml_physmem - iomem_size - MIN_VMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Jeff Dikeba180fd2007-10-16 01:27:00 -0700304 /*
305 * Zones have to begin on a 1 << MAX_ORDER page boundary,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 * so this makes sure that's true for highmem
307 */
308 max_physmem &= ~((1 << (PAGE_SHIFT + MAX_ORDER)) - 1);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700309 if (physmem_size + iomem_size > max_physmem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 highmem = physmem_size + iomem_size - max_physmem;
311 physmem_size -= highmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313
314 high_physmem = uml_physmem + physmem_size;
315 end_iomem = high_physmem + iomem_size;
316 high_memory = (void *) end_iomem;
317
318 start_vm = VMALLOC_START;
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 virtmem_size = physmem_size;
Jeff Dike60a29882008-05-12 14:01:57 -0700321 stack = (unsigned long) argv;
322 stack &= ~(1024 * 1024 - 1);
323 avail = stack - start_vm;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700324 if (physmem_size > avail)
325 virtmem_size = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 end_vm = start_vm + virtmem_size;
327
Jeff Dikeba180fd2007-10-16 01:27:00 -0700328 if (virtmem_size < physmem_size)
Masami Hiramatsud3878bb2017-05-18 02:17:14 +0900329 os_info("Kernel virtual memory size shrunk to %lu bytes\n",
330 virtmem_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 os_flush_stdout();
333
Jeff Dike77bf4402007-10-16 01:26:58 -0700334 return start_uml();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Masami Hiramatsu5b4236e2017-04-27 12:15:10 +0900337int __init __weak read_initrd(void)
338{
339 return 0;
340}
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342void __init setup_arch(char **cmdline_p)
343{
Richard Weinbergerb6323692016-06-12 21:56:42 +0200344 stack_protections((unsigned long) &init_thread_info);
345 setup_physmem(uml_physmem, uml_reserved, physmem_size, highmem);
346 mem_total_pages(physmem_size, iomem_size, highmem);
Masami Hiramatsu5b4236e2017-04-27 12:15:10 +0900347 read_initrd();
Richard Weinbergerb6323692016-06-12 21:56:42 +0200348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 paging_init();
Alon Bar-Lev19bf7e72007-02-12 00:54:23 -0800350 strlcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700351 *cmdline_p = command_line;
Jeff Dikeb4ffb6a2007-05-06 14:50:59 -0700352 setup_hostinfo(host_info, sizeof host_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
355void __init check_bugs(void)
356{
357 arch_check_bugs();
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700358 os_check_bugs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800361void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
362{
363}