blob: b9dd13e6f2063c26fa9ec05ef0a07b165e693ab9 [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>
Jeff Dikec5d4bb12008-02-04 22:31:14 -080014#include <asm/pgtable.h>
15#include <asm/processor.h>
16#include <asm/setup.h>
Al Viro37185b32012-10-08 03:27:32 +010017#include <as-layout.h>
18#include <arch.h>
19#include <init.h>
20#include <kern.h>
21#include <kern_util.h>
22#include <mem_user.h>
23#include <os.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#define DEFAULT_COMMAND_LINE "root=98:0"
26
Jeff Dike1d1497e2007-05-06 14:51:26 -070027/* Changed in add_arg and setup_arch, which run before SMP is started */
Alon Bar-Lev7a3a06d02007-02-12 00:54:26 -080028static char __initdata command_line[COMMAND_LINE_SIZE] = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Alon Bar-Lev7a3a06d02007-02-12 00:54:26 -080030static void __init add_arg(char *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
32 if (strlen(command_line) + strlen(arg) + 1 > COMMAND_LINE_SIZE) {
33 printf("add_arg: Too many command line arguments!\n");
34 exit(1);
35 }
Jeff Dikeba180fd2007-10-16 01:27:00 -070036 if (strlen(command_line) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 strcat(command_line, " ");
38 strcat(command_line, arg);
39}
40
Jeff Dike1d1497e2007-05-06 14:51:26 -070041/*
42 * These fields are initialized at boot time and not changed.
43 * XXX This structure is used only in the non-SMP case. Maybe this
44 * should be moved to smp.c.
45 */
46struct cpuinfo_um boot_cpu_data = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 .loops_per_jiffy = 0,
48 .ipi_pipe = { -1, -1 }
49};
50
Thomas Gleixner5b408242012-05-03 09:03:00 +000051union thread_union cpu0_irqstack
52 __attribute__((__section__(".data..init_irqstack"))) =
53 { INIT_THREAD_INFO(init_task) };
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055unsigned long thread_saved_pc(struct task_struct *task)
56{
Jeff Dike77bf4402007-10-16 01:26:58 -070057 /* FIXME: Need to look up userspace_pid by cpu */
58 return os_process_pc(userspace_pid[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Jeff Dikeb4ffb6a2007-05-06 14:50:59 -070061/* Changed in setup_arch, which is called in early boot */
62static char host_info[(__NEW_UTS_LEN + 1) * 5];
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static int show_cpuinfo(struct seq_file *m, void *v)
65{
66 int index = 0;
67
68#ifdef CONFIG_SMP
69 index = (struct cpuinfo_um *) v - cpu_data;
70 if (!cpu_online(index))
71 return 0;
72#endif
73
74 seq_printf(m, "processor\t: %d\n", index);
75 seq_printf(m, "vendor_id\t: User Mode Linux\n");
76 seq_printf(m, "model name\t: UML\n");
Jeff Dike6aa802c2007-10-16 01:26:56 -070077 seq_printf(m, "mode\t\t: skas\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 seq_printf(m, "host\t\t: %s\n", host_info);
79 seq_printf(m, "bogomips\t: %lu.%02lu\n\n",
80 loops_per_jiffy/(500000/HZ),
81 (loops_per_jiffy/(5000/HZ)) % 100);
82
Jeff Dikea5ed1ff2007-05-06 14:50:58 -070083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
86static void *c_start(struct seq_file *m, loff_t *pos)
87{
88 return *pos < NR_CPUS ? cpu_data + *pos : NULL;
89}
90
91static void *c_next(struct seq_file *m, void *v, loff_t *pos)
92{
93 ++*pos;
94 return c_start(m, pos);
95}
96
97static void c_stop(struct seq_file *m, void *v)
98{
99}
100
Jeff Dike5e7672e2006-09-27 01:50:33 -0700101const struct seq_operations cpuinfo_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .start = c_start,
103 .next = c_next,
104 .stop = c_stop,
105 .show = show_cpuinfo,
106};
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/* Set in linux_main */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109unsigned long uml_physmem;
Al Viro73395a02011-08-18 20:14:10 +0100110EXPORT_SYMBOL(uml_physmem);
111
Jeff Dike1d1497e2007-05-06 14:51:26 -0700112unsigned long uml_reserved; /* Also modified in mem_init */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113unsigned long start_vm;
114unsigned long end_vm;
Jeff Dike1d1497e2007-05-06 14:51:26 -0700115
116/* Set in uml_ncpus_setup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117int ncpus = 1;
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/* Set in early boot */
120static int have_root __initdata = 0;
Jeff Dike1d1497e2007-05-06 14:51:26 -0700121
122/* Set in uml_mem_setup and modified in linux_main */
Jeff Dikeae173812005-11-07 00:58:57 -0800123long long physmem_size = 32 * 1024 * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
WANG Cong3af9c5b2008-04-28 02:13:52 -0700125static const char *usage_string =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126"User Mode Linux v%s\n"
127" available at http://user-mode-linux.sourceforge.net/\n\n";
128
129static int __init uml_version_setup(char *line, int *add)
130{
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700131 printf("%s\n", init_utsname()->release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 exit(0);
133
134 return 0;
135}
136
137__uml_setup("--version", uml_version_setup,
138"--version\n"
139" Prints the version number of the kernel.\n\n"
140);
141
142static int __init uml_root_setup(char *line, int *add)
143{
144 have_root = 1;
145 return 0;
146}
147
148__uml_setup("root=", uml_root_setup,
149"root=<file containing the root fs>\n"
150" This is actually used by the generic kernel in exactly the same\n"
151" way as in any other kernel. If you configure a number of block\n"
152" devices and want to boot off something other than ubd0, you \n"
153" would use something like:\n"
154" root=/dev/ubd5\n\n"
155);
156
Jeff Dikefbd55772006-02-07 12:58:40 -0800157static int __init no_skas_debug_setup(char *line, int *add)
158{
159 printf("'debug' is not necessary to gdb UML in skas mode - run \n");
Jeff Dike96cee302008-05-12 14:01:48 -0700160 printf("'gdb linux'\n");
Jeff Dikefbd55772006-02-07 12:58:40 -0800161
162 return 0;
163}
164
165__uml_setup("debug", no_skas_debug_setup,
166"debug\n"
167" this flag is not needed to run gdb on UML in skas mode\n\n"
168);
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170#ifdef CONFIG_SMP
171static int __init uml_ncpus_setup(char *line, int *add)
172{
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700173 if (!sscanf(line, "%d", &ncpus)) {
174 printf("Couldn't parse [%s]\n", line);
175 return -1;
176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181__uml_setup("ncpus=", uml_ncpus_setup,
182"ncpus=<# of desired CPUs>\n"
Jeff Dikeba180fd2007-10-16 01:27:00 -0700183" This tells an SMP kernel how many virtual processors to start.\n\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184);
185#endif
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static int __init Usage(char *line, int *add)
188{
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700189 const char **p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700191 printf(usage_string, init_utsname()->release);
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700192 p = &__uml_help_start;
193 while (p < &__uml_help_end) {
194 printf("%s", *p);
195 p++;
196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return 0;
199}
200
201__uml_setup("--help", Usage,
202"--help\n"
203" Prints this message.\n\n"
204);
205
Karol Swietlicki6b7e9672008-02-04 22:30:50 -0800206static void __init uml_checksetup(char *line, int *add)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 struct uml_param *p;
209
210 p = &__uml_setup_start;
Jeff Dikec5d4bb12008-02-04 22:31:14 -0800211 while (p < &__uml_setup_end) {
WANG Cong3af9c5b2008-04-28 02:13:52 -0700212 size_t n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 n = strlen(p->str);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700215 if (!strncmp(line, p->str, n) && p->setup_func(line + n, add))
Karol Swietlicki6b7e9672008-02-04 22:30:50 -0800216 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 p++;
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221static void __init uml_postsetup(void)
222{
223 initcall_t *p;
224
225 p = &__uml_postsetup_start;
Jeff Dikec5d4bb12008-02-04 22:31:14 -0800226 while (p < &__uml_postsetup_end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 (*p)();
228 p++;
229 }
230 return;
231}
232
Jeff Dike0983a882008-02-04 22:31:08 -0800233static int panic_exit(struct notifier_block *self, unsigned long unused1,
234 void *unused2)
235{
236 bust_spinlocks(1);
Jeff Dike0983a882008-02-04 22:31:08 -0800237 bust_spinlocks(0);
238 uml_exitcode = 1;
239 os_dump_core();
240 return 0;
241}
242
243static struct notifier_block panic_exit_notifier = {
244 .notifier_call = panic_exit,
245 .next = NULL,
246 .priority = 0
247};
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249/* Set during early boot */
Jeff Dike536788f2008-02-08 04:22:07 -0800250unsigned long task_size;
251EXPORT_SYMBOL(task_size);
252
253unsigned long host_task_size;
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255unsigned long brk_start;
256unsigned long end_iomem;
257EXPORT_SYMBOL(end_iomem);
258
259#define MIN_VMALLOC (32 * 1024 * 1024)
260
Jeff Dike23bbd5862006-07-10 04:45:06 -0700261extern char __binary_start;
262
Alon Bar-Lev7a3a06d02007-02-12 00:54:26 -0800263int __init linux_main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 unsigned long avail, diff;
266 unsigned long virtmem_size, max_physmem;
Jeff Dike60a29882008-05-12 14:01:57 -0700267 unsigned long stack;
WANG Cong3af9c5b2008-04-28 02:13:52 -0700268 unsigned int i;
269 int add;
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700270 char * mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Jeff Dikeba180fd2007-10-16 01:27:00 -0700272 for (i = 1; i < argc; i++) {
273 if ((i == 1) && (argv[i][0] == ' '))
274 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 add = 1;
276 uml_checksetup(argv[i], &add);
277 if (add)
278 add_arg(argv[i]);
279 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700280 if (have_root == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 add_arg(DEFAULT_COMMAND_LINE);
282
Tom Spink40fb16a2008-06-05 22:46:12 -0700283 host_task_size = os_get_top_address();
Jeff Dike536788f2008-02-08 04:22:07 -0800284 /*
285 * TASK_SIZE needs to be PGDIR_SIZE aligned or else exit_mmap craps
286 * out
287 */
288 task_size = host_task_size & PGDIR_MASK;
289
Jeff Dikeba180fd2007-10-16 01:27:00 -0700290 /* OS sanity checks that need to happen before the kernel runs */
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700291 os_early_checks();
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700292
Jeff Dike42fda662007-10-16 01:26:50 -0700293 can_do_skas();
294
295 if (proc_mm && ptrace_faultinfo)
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700296 mode = "SKAS3";
297 else
298 mode = "SKAS0";
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700299
300 printf("UML running in %s mode\n", mode);
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 brk_start = (unsigned long) sbrk(0);
Jeff Dike77bf4402007-10-16 01:26:58 -0700303
Jeff Dikeba180fd2007-10-16 01:27:00 -0700304 /*
305 * Increase physical memory size for exec-shield users
306 * so they actually get what they asked for. This should
307 * add zero for non-exec shield users
308 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 diff = UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700311 if (diff > 1024 * 1024) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 printf("Adding %ld bytes to physical memory to account for "
313 "exec-shield gap\n", diff);
314 physmem_size += UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
315 }
316
Jeff Dike1d1497e2007-05-06 14:51:26 -0700317 uml_physmem = (unsigned long) &__binary_start & PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 /* Reserve up to 4M after the current brk */
320 uml_reserved = ROUND_4M(brk_start) + (1 << 22);
321
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700322 setup_machinename(init_utsname()->machine);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 highmem = 0;
325 iomem_size = (iomem_size + PAGE_SIZE - 1) & PAGE_MASK;
Jeff Dike536788f2008-02-08 04:22:07 -0800326 max_physmem = TASK_SIZE - uml_physmem - iomem_size - MIN_VMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Jeff Dikeba180fd2007-10-16 01:27:00 -0700328 /*
329 * Zones have to begin on a 1 << MAX_ORDER page boundary,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 * so this makes sure that's true for highmem
331 */
332 max_physmem &= ~((1 << (PAGE_SHIFT + MAX_ORDER)) - 1);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700333 if (physmem_size + iomem_size > max_physmem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 highmem = physmem_size + iomem_size - max_physmem;
335 physmem_size -= highmem;
336#ifndef CONFIG_HIGHMEM
337 highmem = 0;
338 printf("CONFIG_HIGHMEM not enabled - physical memory shrunk "
Jeff Diked9f8b622006-03-27 01:14:29 -0800339 "to %Lu bytes\n", physmem_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340#endif
341 }
342
343 high_physmem = uml_physmem + physmem_size;
344 end_iomem = high_physmem + iomem_size;
345 high_memory = (void *) end_iomem;
346
347 start_vm = VMALLOC_START;
348
349 setup_physmem(uml_physmem, uml_reserved, physmem_size, highmem);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700350 if (init_maps(physmem_size, iomem_size, highmem)) {
Jeff Diked9f8b622006-03-27 01:14:29 -0800351 printf("Failed to allocate mem_map for %Lu bytes of physical "
352 "memory and %Lu bytes of highmem\n", physmem_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 highmem);
354 exit(1);
355 }
356
357 virtmem_size = physmem_size;
Jeff Dike60a29882008-05-12 14:01:57 -0700358 stack = (unsigned long) argv;
359 stack &= ~(1024 * 1024 - 1);
360 avail = stack - start_vm;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700361 if (physmem_size > avail)
362 virtmem_size = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 end_vm = start_vm + virtmem_size;
364
Jeff Dikeba180fd2007-10-16 01:27:00 -0700365 if (virtmem_size < physmem_size)
Jeff Dikeae173812005-11-07 00:58:57 -0800366 printf("Kernel virtual memory size shrunk to %lu bytes\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 virtmem_size);
368
Jeff Dike0983a882008-02-04 22:31:08 -0800369 atomic_notifier_chain_register(&panic_notifier_list,
370 &panic_exit_notifier);
371
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700372 uml_postsetup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Jeff Dike57598fd2007-05-10 22:22:30 -0700374 stack_protections((unsigned long) &init_thread_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 os_flush_stdout();
376
Jeff Dike77bf4402007-10-16 01:26:58 -0700377 return start_uml();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380void __init setup_arch(char **cmdline_p)
381{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 paging_init();
Alon Bar-Lev19bf7e72007-02-12 00:54:23 -0800383 strlcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700384 *cmdline_p = command_line;
Jeff Dikeb4ffb6a2007-05-06 14:50:59 -0700385 setup_hostinfo(host_info, sizeof host_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388void __init check_bugs(void)
389{
390 arch_check_bugs();
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700391 os_check_bugs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800394void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
395{
396}
397
Theodore Tsoc61a84162006-07-03 00:24:09 -0700398#ifdef CONFIG_SMP
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800399void alternatives_smp_module_add(struct module *mod, char *name,
400 void *locks, void *locks_end,
401 void *text, void *text_end)
402{
403}
404
405void alternatives_smp_module_del(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407}
Theodore Tsoc61a84162006-07-03 00:24:09 -0700408#endif