blob: 9f3a207eb81f2ff2d055fdb6f18ae94680f011d4 [file] [log] [blame]
Gennady Sharapovea2ba7d2006-01-08 01:01:31 -08001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2000, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "linux/kernel.h"
7#include "linux/sched.h"
8#include "linux/notifier.h"
9#include "linux/mm.h"
10#include "linux/types.h"
11#include "linux/tty.h"
12#include "linux/init.h"
13#include "linux/bootmem.h"
14#include "linux/spinlock.h"
15#include "linux/utsname.h"
16#include "linux/sysrq.h"
17#include "linux/seq_file.h"
18#include "linux/delay.h"
19#include "linux/module.h"
Jeff Dikeb4ffb6a2007-05-06 14:50:59 -070020#include "linux/utsname.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "asm/page.h"
22#include "asm/pgtable.h"
23#include "asm/ptrace.h"
24#include "asm/elf.h"
25#include "asm/user.h"
Jeff Dike16c11162005-05-06 21:30:45 -070026#include "asm/setup.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "ubd_user.h"
28#include "asm/current.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "kern_util.h"
Jeff Dike4ff83ce2007-05-06 14:51:08 -070030#include "as-layout.h"
Jeff Dikeeb830752007-05-06 14:51:07 -070031#include "arch.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "kern.h"
33#include "mem_user.h"
34#include "mem.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "initrd.h"
36#include "init.h"
37#include "os.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "mode_kern.h"
39#include "mode.h"
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -070040#include "skas.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#define DEFAULT_COMMAND_LINE "root=98:0"
43
Jeff Dike1d1497e2007-05-06 14:51:26 -070044/* Changed in add_arg and setup_arch, which run before SMP is started */
Alon Bar-Lev7a3a06d02007-02-12 00:54:26 -080045static char __initdata command_line[COMMAND_LINE_SIZE] = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Alon Bar-Lev7a3a06d02007-02-12 00:54:26 -080047static void __init add_arg(char *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 if (strlen(command_line) + strlen(arg) + 1 > COMMAND_LINE_SIZE) {
50 printf("add_arg: Too many command line arguments!\n");
51 exit(1);
52 }
53 if(strlen(command_line) > 0)
54 strcat(command_line, " ");
55 strcat(command_line, arg);
56}
57
Jeff Dike1d1497e2007-05-06 14:51:26 -070058/*
59 * These fields are initialized at boot time and not changed.
60 * XXX This structure is used only in the non-SMP case. Maybe this
61 * should be moved to smp.c.
62 */
63struct cpuinfo_um boot_cpu_data = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 .loops_per_jiffy = 0,
65 .ipi_pipe = { -1, -1 }
66};
67
68unsigned long thread_saved_pc(struct task_struct *task)
69{
Jeff Dike6aa802c2007-10-16 01:26:56 -070070 return os_process_pc(thread_pid_skas(task));
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
Jeff Dikeb4ffb6a2007-05-06 14:50:59 -070073/* Changed in setup_arch, which is called in early boot */
74static char host_info[(__NEW_UTS_LEN + 1) * 5];
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static int show_cpuinfo(struct seq_file *m, void *v)
77{
78 int index = 0;
79
80#ifdef CONFIG_SMP
81 index = (struct cpuinfo_um *) v - cpu_data;
82 if (!cpu_online(index))
83 return 0;
84#endif
85
86 seq_printf(m, "processor\t: %d\n", index);
87 seq_printf(m, "vendor_id\t: User Mode Linux\n");
88 seq_printf(m, "model name\t: UML\n");
Jeff Dike6aa802c2007-10-16 01:26:56 -070089 seq_printf(m, "mode\t\t: skas\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 seq_printf(m, "host\t\t: %s\n", host_info);
91 seq_printf(m, "bogomips\t: %lu.%02lu\n\n",
92 loops_per_jiffy/(500000/HZ),
93 (loops_per_jiffy/(5000/HZ)) % 100);
94
Jeff Dikea5ed1ff2007-05-06 14:50:58 -070095 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
98static void *c_start(struct seq_file *m, loff_t *pos)
99{
100 return *pos < NR_CPUS ? cpu_data + *pos : NULL;
101}
102
103static void *c_next(struct seq_file *m, void *v, loff_t *pos)
104{
105 ++*pos;
106 return c_start(m, pos);
107}
108
109static void c_stop(struct seq_file *m, void *v)
110{
111}
112
Jeff Dike5e7672e2006-09-27 01:50:33 -0700113const struct seq_operations cpuinfo_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .start = c_start,
115 .next = c_next,
116 .stop = c_stop,
117 .show = show_cpuinfo,
118};
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/* Set in linux_main */
121unsigned long host_task_size;
122unsigned long task_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123unsigned long uml_physmem;
Jeff Dike1d1497e2007-05-06 14:51:26 -0700124unsigned long uml_reserved; /* Also modified in mem_init */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125unsigned long start_vm;
126unsigned long end_vm;
Jeff Dike1d1497e2007-05-06 14:51:26 -0700127
128/* Set in uml_ncpus_setup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129int ncpus = 1;
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/* Set in early boot */
132static int have_root __initdata = 0;
Jeff Dike1d1497e2007-05-06 14:51:26 -0700133
134/* Set in uml_mem_setup and modified in linux_main */
Jeff Dikeae173812005-11-07 00:58:57 -0800135long long physmem_size = 32 * 1024 * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static char *usage_string =
138"User Mode Linux v%s\n"
139" available at http://user-mode-linux.sourceforge.net/\n\n";
140
141static int __init uml_version_setup(char *line, int *add)
142{
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700143 printf("%s\n", init_utsname()->release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 exit(0);
145
146 return 0;
147}
148
149__uml_setup("--version", uml_version_setup,
150"--version\n"
151" Prints the version number of the kernel.\n\n"
152);
153
154static int __init uml_root_setup(char *line, int *add)
155{
156 have_root = 1;
157 return 0;
158}
159
160__uml_setup("root=", uml_root_setup,
161"root=<file containing the root fs>\n"
162" This is actually used by the generic kernel in exactly the same\n"
163" way as in any other kernel. If you configure a number of block\n"
164" devices and want to boot off something other than ubd0, you \n"
165" would use something like:\n"
166" root=/dev/ubd5\n\n"
167);
168
Jeff Dikefbd55772006-02-07 12:58:40 -0800169static int __init no_skas_debug_setup(char *line, int *add)
170{
171 printf("'debug' is not necessary to gdb UML in skas mode - run \n");
Jeff Dike42fda662007-10-16 01:26:50 -0700172 printf("'gdb linux'");
Jeff Dikefbd55772006-02-07 12:58:40 -0800173
174 return 0;
175}
176
177__uml_setup("debug", no_skas_debug_setup,
178"debug\n"
179" this flag is not needed to run gdb on UML in skas mode\n\n"
180);
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182#ifdef CONFIG_SMP
183static int __init uml_ncpus_setup(char *line, int *add)
184{
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700185 if (!sscanf(line, "%d", &ncpus)) {
186 printf("Couldn't parse [%s]\n", line);
187 return -1;
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700190 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
193__uml_setup("ncpus=", uml_ncpus_setup,
194"ncpus=<# of desired CPUs>\n"
195" This tells an SMP kernel how many virtual processors to start.\n\n"
196);
197#endif
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static int __init Usage(char *line, int *add)
200{
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700201 const char **p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700203 printf(usage_string, init_utsname()->release);
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700204 p = &__uml_help_start;
205 while (p < &__uml_help_end) {
206 printf("%s", *p);
207 p++;
208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return 0;
211}
212
213__uml_setup("--help", Usage,
214"--help\n"
215" Prints this message.\n\n"
216);
217
218static int __init uml_checksetup(char *line, int *add)
219{
220 struct uml_param *p;
221
222 p = &__uml_setup_start;
223 while(p < &__uml_setup_end) {
224 int n;
225
226 n = strlen(p->str);
227 if(!strncmp(line, p->str, n)){
228 if (p->setup_func(line + n, add)) return 1;
229 }
230 p++;
231 }
232 return 0;
233}
234
235static void __init uml_postsetup(void)
236{
237 initcall_t *p;
238
239 p = &__uml_postsetup_start;
240 while(p < &__uml_postsetup_end){
241 (*p)();
242 p++;
243 }
244 return;
245}
246
247/* Set during early boot */
248unsigned long brk_start;
249unsigned long end_iomem;
250EXPORT_SYMBOL(end_iomem);
251
252#define MIN_VMALLOC (32 * 1024 * 1024)
253
Jeff Dike23bbd582006-07-10 04:45:06 -0700254extern char __binary_start;
255
Alon Bar-Lev7a3a06d02007-02-12 00:54:26 -0800256int __init linux_main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 unsigned long avail, diff;
259 unsigned long virtmem_size, max_physmem;
260 unsigned int i, add;
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700261 char * mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 for (i = 1; i < argc; i++){
264 if((i == 1) && (argv[i][0] == ' ')) continue;
265 add = 1;
266 uml_checksetup(argv[i], &add);
267 if (add)
268 add_arg(argv[i]);
269 }
270 if(have_root == 0)
271 add_arg(DEFAULT_COMMAND_LINE);
272
Gennady Sharapov60d339f2005-09-03 15:57:47 -0700273 os_early_checks();
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700274
Jeff Dike42fda662007-10-16 01:26:50 -0700275 can_do_skas();
276
277 if (proc_mm && ptrace_faultinfo)
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700278 mode = "SKAS3";
279 else
280 mode = "SKAS0";
Paolo 'Blaisorblade' Giarrussocb665042005-07-27 11:43:31 -0700281
282 printf("UML running in %s mode\n", mode);
283
Jeff Dike6aa802c2007-10-16 01:26:56 -0700284 host_task_size = set_task_sizes_skas(&task_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Gennady Sharapovea2ba7d2006-01-08 01:01:31 -0800286 /*
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700287 * Setting up handlers to 'sig_info' struct
288 */
Gennady Sharapovea2ba7d2006-01-08 01:01:31 -0800289 os_fill_handlinfo(handlinfo_kern);
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 brk_start = (unsigned long) sbrk(0);
Jeff Dike6aa802c2007-10-16 01:26:56 -0700292 before_mem_skas(brk_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 /* Increase physical memory size for exec-shield users
294 so they actually get what they asked for. This should
295 add zero for non-exec shield users */
296
297 diff = UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
298 if(diff > 1024 * 1024){
299 printf("Adding %ld bytes to physical memory to account for "
300 "exec-shield gap\n", diff);
301 physmem_size += UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
302 }
303
Jeff Dike1d1497e2007-05-06 14:51:26 -0700304 uml_physmem = (unsigned long) &__binary_start & PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /* Reserve up to 4M after the current brk */
307 uml_reserved = ROUND_4M(brk_start) + (1 << 22);
308
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700309 setup_machinename(init_utsname()->machine);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 highmem = 0;
312 iomem_size = (iomem_size + PAGE_SIZE - 1) & PAGE_MASK;
313 max_physmem = get_kmem_end() - uml_physmem - iomem_size - MIN_VMALLOC;
314
315 /* Zones have to begin on a 1 << MAX_ORDER page boundary,
316 * so this makes sure that's true for highmem
317 */
318 max_physmem &= ~((1 << (PAGE_SHIFT + MAX_ORDER)) - 1);
319 if(physmem_size + iomem_size > max_physmem){
320 highmem = physmem_size + iomem_size - max_physmem;
321 physmem_size -= highmem;
322#ifndef CONFIG_HIGHMEM
323 highmem = 0;
324 printf("CONFIG_HIGHMEM not enabled - physical memory shrunk "
Jeff Diked9f8b622006-03-27 01:14:29 -0800325 "to %Lu bytes\n", physmem_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326#endif
327 }
328
329 high_physmem = uml_physmem + physmem_size;
330 end_iomem = high_physmem + iomem_size;
331 high_memory = (void *) end_iomem;
332
333 start_vm = VMALLOC_START;
334
335 setup_physmem(uml_physmem, uml_reserved, physmem_size, highmem);
336 if(init_maps(physmem_size, iomem_size, highmem)){
Jeff Diked9f8b622006-03-27 01:14:29 -0800337 printf("Failed to allocate mem_map for %Lu bytes of physical "
338 "memory and %Lu bytes of highmem\n", physmem_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 highmem);
340 exit(1);
341 }
342
343 virtmem_size = physmem_size;
344 avail = get_kmem_end() - start_vm;
345 if(physmem_size > avail) virtmem_size = avail;
346 end_vm = start_vm + virtmem_size;
347
348 if(virtmem_size < physmem_size)
Jeff Dikeae173812005-11-07 00:58:57 -0800349 printf("Kernel virtual memory size shrunk to %lu bytes\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 virtmem_size);
351
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700352 uml_postsetup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Jeff Dike57598fd2007-05-10 22:22:30 -0700354 stack_protections((unsigned long) &init_thread_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 os_flush_stdout();
356
Jeff Dike6aa802c2007-10-16 01:26:56 -0700357 return start_uml_skas();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
360extern int uml_exitcode;
361
362static int panic_exit(struct notifier_block *self, unsigned long unused1,
363 void *unused2)
364{
365 bust_spinlocks(1);
366 show_regs(&(current->thread.regs));
367 bust_spinlocks(0);
368 uml_exitcode = 1;
Jeff Dike63843c22007-05-06 14:51:39 -0700369 os_dump_core();
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373static struct notifier_block panic_exit_notifier = {
374 .notifier_call = panic_exit,
375 .next = NULL,
376 .priority = 0
377};
378
379void __init setup_arch(char **cmdline_p)
380{
Alan Sterne041c682006-03-27 01:16:30 -0800381 atomic_notifier_chain_register(&panic_notifier_list,
382 &panic_exit_notifier);
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 Tsoc61a8412006-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 Tsoc61a8412006-07-03 00:24:09 -0700409#endif