blob: 4db8770906caa90ee3ec29ec5554a13ee8788a74 [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>
Jeff Dikeba180fd2007-10-16 01:27:00 -070017#include "as-layout.h"
Jeff Dikec5d4bb12008-02-04 22:31:14 -080018#include "arch.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070019#include "init.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "kern.h"
Jeff Dikeedea1382008-02-04 22:30:46 -080021#include "kern_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "mem_user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#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);
237 show_regs(&(current->thread.regs));
238 bust_spinlocks(0);
239 uml_exitcode = 1;
240 os_dump_core();
241 return 0;
242}
243
244static struct notifier_block panic_exit_notifier = {
245 .notifier_call = panic_exit,
246 .next = NULL,
247 .priority = 0
248};
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250/* Set during early boot */
Jeff Dike536788f2008-02-08 04:22:07 -0800251unsigned long task_size;
252EXPORT_SYMBOL(task_size);
253
254unsigned long host_task_size;
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256unsigned long brk_start;
257unsigned long end_iomem;
258EXPORT_SYMBOL(end_iomem);
259
260#define MIN_VMALLOC (32 * 1024 * 1024)
261
Jeff Dike23bbd5862006-07-10 04:45:06 -0700262extern char __binary_start;
263
Alon Bar-Lev7a3a06d02007-02-12 00:54:26 -0800264int __init linux_main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 unsigned long avail, diff;
267 unsigned long virtmem_size, max_physmem;
Jeff Dike60a29882008-05-12 14:01:57 -0700268 unsigned long stack;
WANG Cong3af9c5b2008-04-28 02:13:52 -0700269 unsigned int i;
270 int add;
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700271 char * mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Jeff Dikeba180fd2007-10-16 01:27:00 -0700273 for (i = 1; i < argc; i++) {
274 if ((i == 1) && (argv[i][0] == ' '))
275 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 add = 1;
277 uml_checksetup(argv[i], &add);
278 if (add)
279 add_arg(argv[i]);
280 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700281 if (have_root == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 add_arg(DEFAULT_COMMAND_LINE);
283
Tom Spink40fb16a2008-06-05 22:46:12 -0700284 host_task_size = os_get_top_address();
Jeff Dike536788f2008-02-08 04:22:07 -0800285 /*
286 * TASK_SIZE needs to be PGDIR_SIZE aligned or else exit_mmap craps
287 * out
288 */
289 task_size = host_task_size & PGDIR_MASK;
290
Jeff Dikeba180fd2007-10-16 01:27:00 -0700291 /* OS sanity checks that need to happen before the kernel runs */
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700292 os_early_checks();
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700293
Jeff Dike42fda662007-10-16 01:26:50 -0700294 can_do_skas();
295
296 if (proc_mm && ptrace_faultinfo)
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700297 mode = "SKAS3";
298 else
299 mode = "SKAS0";
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700300
301 printf("UML running in %s mode\n", mode);
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 brk_start = (unsigned long) sbrk(0);
Jeff Dike77bf4402007-10-16 01:26:58 -0700304
Jeff Dikeba180fd2007-10-16 01:27:00 -0700305 /*
306 * Increase physical memory size for exec-shield users
307 * so they actually get what they asked for. This should
308 * add zero for non-exec shield users
309 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 diff = UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700312 if (diff > 1024 * 1024) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 printf("Adding %ld bytes to physical memory to account for "
314 "exec-shield gap\n", diff);
315 physmem_size += UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
316 }
317
Jeff Dike1d1497e2007-05-06 14:51:26 -0700318 uml_physmem = (unsigned long) &__binary_start & PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 /* Reserve up to 4M after the current brk */
321 uml_reserved = ROUND_4M(brk_start) + (1 << 22);
322
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700323 setup_machinename(init_utsname()->machine);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 highmem = 0;
326 iomem_size = (iomem_size + PAGE_SIZE - 1) & PAGE_MASK;
Jeff Dike536788f2008-02-08 04:22:07 -0800327 max_physmem = TASK_SIZE - uml_physmem - iomem_size - MIN_VMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Jeff Dikeba180fd2007-10-16 01:27:00 -0700329 /*
330 * Zones have to begin on a 1 << MAX_ORDER page boundary,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 * so this makes sure that's true for highmem
332 */
333 max_physmem &= ~((1 << (PAGE_SHIFT + MAX_ORDER)) - 1);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700334 if (physmem_size + iomem_size > max_physmem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 highmem = physmem_size + iomem_size - max_physmem;
336 physmem_size -= highmem;
337#ifndef CONFIG_HIGHMEM
338 highmem = 0;
339 printf("CONFIG_HIGHMEM not enabled - physical memory shrunk "
Jeff Diked9f8b622006-03-27 01:14:29 -0800340 "to %Lu bytes\n", physmem_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341#endif
342 }
343
344 high_physmem = uml_physmem + physmem_size;
345 end_iomem = high_physmem + iomem_size;
346 high_memory = (void *) end_iomem;
347
348 start_vm = VMALLOC_START;
349
350 setup_physmem(uml_physmem, uml_reserved, physmem_size, highmem);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700351 if (init_maps(physmem_size, iomem_size, highmem)) {
Jeff Diked9f8b622006-03-27 01:14:29 -0800352 printf("Failed to allocate mem_map for %Lu bytes of physical "
353 "memory and %Lu bytes of highmem\n", physmem_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 highmem);
355 exit(1);
356 }
357
358 virtmem_size = physmem_size;
Jeff Dike60a29882008-05-12 14:01:57 -0700359 stack = (unsigned long) argv;
360 stack &= ~(1024 * 1024 - 1);
361 avail = stack - start_vm;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700362 if (physmem_size > avail)
363 virtmem_size = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 end_vm = start_vm + virtmem_size;
365
Jeff Dikeba180fd2007-10-16 01:27:00 -0700366 if (virtmem_size < physmem_size)
Jeff Dikeae173812005-11-07 00:58:57 -0800367 printf("Kernel virtual memory size shrunk to %lu bytes\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 virtmem_size);
369
Jeff Dike0983a882008-02-04 22:31:08 -0800370 atomic_notifier_chain_register(&panic_notifier_list,
371 &panic_exit_notifier);
372
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700373 uml_postsetup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Jeff Dike57598fd2007-05-10 22:22:30 -0700375 stack_protections((unsigned long) &init_thread_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 os_flush_stdout();
377
Jeff Dike77bf4402007-10-16 01:26:58 -0700378 return start_uml();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381void __init setup_arch(char **cmdline_p)
382{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 paging_init();
Alon Bar-Lev19bf7e72007-02-12 00:54:23 -0800384 strlcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700385 *cmdline_p = command_line;
Jeff Dikeb4ffb6a2007-05-06 14:50:59 -0700386 setup_hostinfo(host_info, sizeof host_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389void __init check_bugs(void)
390{
391 arch_check_bugs();
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700392 os_check_bugs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800395void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
396{
397}
398
Theodore Tsoc61a84162006-07-03 00:24:09 -0700399#ifdef CONFIG_SMP
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800400void alternatives_smp_module_add(struct module *mod, char *name,
401 void *locks, void *locks_end,
402 void *text, void *text_end)
403{
404}
405
406void alternatives_smp_module_del(struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408}
Theodore Tsoc61a84162006-07-03 00:24:09 -0700409#endif