uml: throw out CHOOSE_MODE
[linux-2.6.git] / arch / um / kernel / um_arch.c
1 /*
2  * Copyright (C) 2000, 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #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"
20 #include "linux/utsname.h"
21 #include "asm/page.h"
22 #include "asm/pgtable.h"
23 #include "asm/ptrace.h"
24 #include "asm/elf.h"
25 #include "asm/user.h"
26 #include "asm/setup.h"
27 #include "ubd_user.h"
28 #include "asm/current.h"
29 #include "kern_util.h"
30 #include "as-layout.h"
31 #include "arch.h"
32 #include "kern.h"
33 #include "mem_user.h"
34 #include "mem.h"
35 #include "initrd.h"
36 #include "init.h"
37 #include "os.h"
38 #include "mode_kern.h"
39 #include "mode.h"
40 #include "skas.h"
41
42 #define DEFAULT_COMMAND_LINE "root=98:0"
43
44 /* Changed in add_arg and setup_arch, which run before SMP is started */
45 static char __initdata command_line[COMMAND_LINE_SIZE] = { 0 };
46
47 static void __init add_arg(char *arg)
48 {
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
58 /*
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  */
63 struct cpuinfo_um boot_cpu_data = {
64         .loops_per_jiffy        = 0,
65         .ipi_pipe               = { -1, -1 }
66 };
67
68 unsigned long thread_saved_pc(struct task_struct *task)
69 {
70         return os_process_pc(thread_pid_skas(task));
71 }
72
73 /* Changed in setup_arch, which is called in early boot */
74 static char host_info[(__NEW_UTS_LEN + 1) * 5];
75
76 static 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");
89         seq_printf(m, "mode\t\t: skas\n");
90         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
95         return 0;
96 }
97
98 static void *c_start(struct seq_file *m, loff_t *pos)
99 {
100         return *pos < NR_CPUS ? cpu_data + *pos : NULL;
101 }
102
103 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
104 {
105         ++*pos;
106         return c_start(m, pos);
107 }
108
109 static void c_stop(struct seq_file *m, void *v)
110 {
111 }
112
113 const struct seq_operations cpuinfo_op = {
114         .start  = c_start,
115         .next   = c_next,
116         .stop   = c_stop,
117         .show   = show_cpuinfo,
118 };
119
120 /* Set in linux_main */
121 unsigned long host_task_size;
122 unsigned long task_size;
123 unsigned long uml_physmem;
124 unsigned long uml_reserved; /* Also modified in mem_init */
125 unsigned long start_vm;
126 unsigned long end_vm;
127
128 /* Set in uml_ncpus_setup */
129 int ncpus = 1;
130
131 /* Set in early boot */
132 static int have_root __initdata = 0;
133
134 /* Set in uml_mem_setup and modified in linux_main */
135 long long physmem_size = 32 * 1024 * 1024;
136
137 static char *usage_string = 
138 "User Mode Linux v%s\n"
139 "       available at http://user-mode-linux.sourceforge.net/\n\n";
140
141 static int __init uml_version_setup(char *line, int *add)
142 {
143         printf("%s\n", init_utsname()->release);
144         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
154 static 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
169 static 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");
172         printf("'gdb linux'");
173
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
182 #ifdef CONFIG_SMP
183 static int __init uml_ncpus_setup(char *line, int *add)
184 {
185         if (!sscanf(line, "%d", &ncpus)) {
186                 printf("Couldn't parse [%s]\n", line);
187                 return -1;
188         }
189
190         return 0;
191 }
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
199 static int __init Usage(char *line, int *add)
200 {
201         const char **p;
202
203         printf(usage_string, init_utsname()->release);
204         p = &__uml_help_start;
205         while (p < &__uml_help_end) {
206                 printf("%s", *p);
207                 p++;
208         }
209         exit(0);
210         return 0;
211 }
212
213 __uml_setup("--help", Usage,
214 "--help\n"
215 "    Prints this message.\n\n"
216 );
217
218 static 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
235 static 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 */
248 unsigned long brk_start;
249 unsigned long end_iomem;
250 EXPORT_SYMBOL(end_iomem);
251
252 #define MIN_VMALLOC (32 * 1024 * 1024)
253
254 extern char __binary_start;
255
256 int __init linux_main(int argc, char **argv)
257 {
258         unsigned long avail, diff;
259         unsigned long virtmem_size, max_physmem;
260         unsigned int i, add;
261         char * mode;
262
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
273         os_early_checks();
274
275         can_do_skas();
276
277         if (proc_mm && ptrace_faultinfo)
278                 mode = "SKAS3";
279         else
280                 mode = "SKAS0";
281
282         printf("UML running in %s mode\n", mode);
283
284         host_task_size = set_task_sizes_skas(&task_size);
285
286         /*
287          * Setting up handlers to 'sig_info' struct
288          */
289         os_fill_handlinfo(handlinfo_kern);
290
291         brk_start = (unsigned long) sbrk(0);
292         before_mem_skas(brk_start);
293         /* 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
304         uml_physmem = (unsigned long) &__binary_start & PAGE_MASK;
305
306         /* Reserve up to 4M after the current brk */
307         uml_reserved = ROUND_4M(brk_start) + (1 << 22);
308
309         setup_machinename(init_utsname()->machine);
310
311         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 "
325                        "to %Lu bytes\n", physmem_size);
326 #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)){
337                 printf("Failed to allocate mem_map for %Lu bytes of physical "
338                        "memory and %Lu bytes of highmem\n", physmem_size,
339                        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)
349                 printf("Kernel virtual memory size shrunk to %lu bytes\n",
350                        virtmem_size);
351
352         uml_postsetup();
353
354         stack_protections((unsigned long) &init_thread_info);
355         os_flush_stdout();
356
357         return start_uml_skas();
358 }
359
360 extern int uml_exitcode;
361
362 static 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;
369         os_dump_core();
370         return 0;
371 }
372
373 static struct notifier_block panic_exit_notifier = {
374         .notifier_call          = panic_exit,
375         .next                   = NULL,
376         .priority               = 0
377 };
378
379 void __init setup_arch(char **cmdline_p)
380 {
381         atomic_notifier_chain_register(&panic_notifier_list,
382                         &panic_exit_notifier);
383         paging_init();
384         strlcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
385         *cmdline_p = command_line;
386         setup_hostinfo(host_info, sizeof host_info);
387 }
388
389 void __init check_bugs(void)
390 {
391         arch_check_bugs();
392         os_check_bugs();
393 }
394
395 void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
396 {
397 }
398
399 #ifdef CONFIG_SMP
400 void alternatives_smp_module_add(struct module *mod, char *name,
401                                  void *locks, void *locks_end,
402                                  void *text,  void *text_end)
403 {
404 }
405
406 void alternatives_smp_module_del(struct module *mod)
407 {
408 }
409 #endif