um_arch.c 9.0 KB
Newer Older
1
/*
J
Jeff Dike 已提交
2
 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
L
Linus Torvalds 已提交
3 4 5 6
 * Licensed under the GPL
 */

#include "linux/delay.h"
J
Jeff Dike 已提交
7
#include "linux/mm.h"
L
Linus Torvalds 已提交
8
#include "linux/module.h"
J
Jeff Dike 已提交
9 10
#include "linux/seq_file.h"
#include "linux/string.h"
J
Jeff Dike 已提交
11
#include "linux/utsname.h"
L
Linus Torvalds 已提交
12
#include "asm/pgtable.h"
J
Jeff Dike 已提交
13
#include "asm/processor.h"
14
#include "asm/setup.h"
J
Jeff Dike 已提交
15
#include "arch.h"
J
Jeff Dike 已提交
16 17
#include "as-layout.h"
#include "init.h"
L
Linus Torvalds 已提交
18
#include "kern.h"
J
Jeff Dike 已提交
19
#include "kern_util.h"
L
Linus Torvalds 已提交
20 21
#include "mem_user.h"
#include "os.h"
22
#include "skas.h"
L
Linus Torvalds 已提交
23 24 25

#define DEFAULT_COMMAND_LINE "root=98:0"

J
Jeff Dike 已提交
26
/* Changed in add_arg and setup_arch, which run before SMP is started */
27
static char __initdata command_line[COMMAND_LINE_SIZE] = { 0 };
L
Linus Torvalds 已提交
28

29
static void __init add_arg(char *arg)
L
Linus Torvalds 已提交
30 31 32 33 34
{
	if (strlen(command_line) + strlen(arg) + 1 > COMMAND_LINE_SIZE) {
		printf("add_arg: Too many command line arguments!\n");
		exit(1);
	}
J
Jeff Dike 已提交
35
	if (strlen(command_line) > 0)
L
Linus Torvalds 已提交
36 37 38 39
		strcat(command_line, " ");
	strcat(command_line, arg);
}

J
Jeff Dike 已提交
40 41 42 43 44 45
/*
 * These fields are initialized at boot time and not changed.
 * XXX This structure is used only in the non-SMP case.  Maybe this
 * should be moved to smp.c.
 */
struct cpuinfo_um boot_cpu_data = {
L
Linus Torvalds 已提交
46 47 48 49 50 51
	.loops_per_jiffy	= 0,
	.ipi_pipe		= { -1, -1 }
};

unsigned long thread_saved_pc(struct task_struct *task)
{
52 53
	/* FIXME: Need to look up userspace_pid by cpu */
	return os_process_pc(userspace_pid[0]);
L
Linus Torvalds 已提交
54 55
}

J
Jeff Dike 已提交
56 57 58
/* Changed in setup_arch, which is called in early boot */
static char host_info[(__NEW_UTS_LEN + 1) * 5];

L
Linus Torvalds 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71
static int show_cpuinfo(struct seq_file *m, void *v)
{
	int index = 0;

#ifdef CONFIG_SMP
	index = (struct cpuinfo_um *) v - cpu_data;
	if (!cpu_online(index))
		return 0;
#endif

	seq_printf(m, "processor\t: %d\n", index);
	seq_printf(m, "vendor_id\t: User Mode Linux\n");
	seq_printf(m, "model name\t: UML\n");
J
Jeff Dike 已提交
72
	seq_printf(m, "mode\t\t: skas\n");
L
Linus Torvalds 已提交
73 74 75 76 77
	seq_printf(m, "host\t\t: %s\n", host_info);
	seq_printf(m, "bogomips\t: %lu.%02lu\n\n",
		   loops_per_jiffy/(500000/HZ),
		   (loops_per_jiffy/(5000/HZ)) % 100);

J
Jeff Dike 已提交
78
	return 0;
L
Linus Torvalds 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
}

static void *c_start(struct seq_file *m, loff_t *pos)
{
	return *pos < NR_CPUS ? cpu_data + *pos : NULL;
}

static void *c_next(struct seq_file *m, void *v, loff_t *pos)
{
	++*pos;
	return c_start(m, pos);
}

static void c_stop(struct seq_file *m, void *v)
{
}

J
Jeff Dike 已提交
96
const struct seq_operations cpuinfo_op = {
L
Linus Torvalds 已提交
97 98 99 100 101 102 103 104 105 106
	.start	= c_start,
	.next	= c_next,
	.stop	= c_stop,
	.show	= show_cpuinfo,
};

/* Set in linux_main */
unsigned long host_task_size;
unsigned long task_size;
unsigned long uml_physmem;
J
Jeff Dike 已提交
107
unsigned long uml_reserved; /* Also modified in mem_init */
L
Linus Torvalds 已提交
108 109
unsigned long start_vm;
unsigned long end_vm;
J
Jeff Dike 已提交
110 111

/* Set in uml_ncpus_setup */
L
Linus Torvalds 已提交
112 113 114 115
int ncpus = 1;

/* Set in early boot */
static int have_root __initdata = 0;
J
Jeff Dike 已提交
116 117

/* Set in uml_mem_setup and modified in linux_main */
J
Jeff Dike 已提交
118
long long physmem_size = 32 * 1024 * 1024;
L
Linus Torvalds 已提交
119

J
Jeff Dike 已提交
120
static char *usage_string =
L
Linus Torvalds 已提交
121 122 123 124 125
"User Mode Linux v%s\n"
"	available at http://user-mode-linux.sourceforge.net/\n\n";

static int __init uml_version_setup(char *line, int *add)
{
126
	printf("%s\n", init_utsname()->release);
L
Linus Torvalds 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
	exit(0);

	return 0;
}

__uml_setup("--version", uml_version_setup,
"--version\n"
"    Prints the version number of the kernel.\n\n"
);

static int __init uml_root_setup(char *line, int *add)
{
	have_root = 1;
	return 0;
}

__uml_setup("root=", uml_root_setup,
"root=<file containing the root fs>\n"
"    This is actually used by the generic kernel in exactly the same\n"
"    way as in any other kernel. If you configure a number of block\n"
"    devices and want to boot off something other than ubd0, you \n"
"    would use something like:\n"
"        root=/dev/ubd5\n\n"
);

152 153 154
static int __init no_skas_debug_setup(char *line, int *add)
{
	printf("'debug' is not necessary to gdb UML in skas mode - run \n");
J
Jeff Dike 已提交
155
	printf("'gdb linux'");
156 157 158 159 160 161 162 163 164

	return 0;
}

__uml_setup("debug", no_skas_debug_setup,
"debug\n"
"    this flag is not needed to run gdb on UML in skas mode\n\n"
);

L
Linus Torvalds 已提交
165 166 167
#ifdef CONFIG_SMP
static int __init uml_ncpus_setup(char *line, int *add)
{
J
Jeff Dike 已提交
168 169 170 171
	if (!sscanf(line, "%d", &ncpus)) {
		printf("Couldn't parse [%s]\n", line);
		return -1;
	}
L
Linus Torvalds 已提交
172

J
Jeff Dike 已提交
173
	return 0;
L
Linus Torvalds 已提交
174 175 176 177
}

__uml_setup("ncpus=", uml_ncpus_setup,
"ncpus=<# of desired CPUs>\n"
J
Jeff Dike 已提交
178
"    This tells an SMP kernel how many virtual processors to start.\n\n"
L
Linus Torvalds 已提交
179 180 181 182 183
);
#endif

static int __init Usage(char *line, int *add)
{
J
Jeff Dike 已提交
184
	const char **p;
L
Linus Torvalds 已提交
185

186
	printf(usage_string, init_utsname()->release);
J
Jeff Dike 已提交
187 188 189 190 191
	p = &__uml_help_start;
	while (p < &__uml_help_end) {
		printf("%s", *p);
		p++;
	}
L
Linus Torvalds 已提交
192 193 194 195 196 197 198 199 200
	exit(0);
	return 0;
}

__uml_setup("--help", Usage,
"--help\n"
"    Prints this message.\n\n"
);

201
static void __init uml_checksetup(char *line, int *add)
L
Linus Torvalds 已提交
202 203 204 205 206 207 208 209
{
	struct uml_param *p;

	p = &__uml_setup_start;
	while(p < &__uml_setup_end) {
		int n;

		n = strlen(p->str);
J
Jeff Dike 已提交
210
		if (!strncmp(line, p->str, n) && p->setup_func(line + n, add))
211
			return;
L
Linus Torvalds 已提交
212 213 214 215 216 217 218 219 220
		p++;
	}
}

static void __init uml_postsetup(void)
{
	initcall_t *p;

	p = &__uml_postsetup_start;
J
Jeff Dike 已提交
221
	while(p < &__uml_postsetup_end) {
L
Linus Torvalds 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234
		(*p)();
		p++;
	}
	return;
}

/* Set during early boot */
unsigned long brk_start;
unsigned long end_iomem;
EXPORT_SYMBOL(end_iomem);

#define MIN_VMALLOC (32 * 1024 * 1024)

235 236
extern char __binary_start;

237 238 239 240 241 242 243 244
static unsigned long set_task_sizes_skas(unsigned long *task_size_out)
{
	/* Round up to the nearest 4M */
	unsigned long host_task_size = ROUND_4M((unsigned long)
						&host_task_size);

	if (!skas_needs_stub)
		*task_size_out = host_task_size;
J
Jeff Dike 已提交
245 246
	else
		*task_size_out = STUB_START & PGDIR_MASK;
247 248 249 250

	return host_task_size;
}

251
int __init linux_main(int argc, char **argv)
L
Linus Torvalds 已提交
252 253 254 255
{
	unsigned long avail, diff;
	unsigned long virtmem_size, max_physmem;
	unsigned int i, add;
256
	char * mode;
L
Linus Torvalds 已提交
257

J
Jeff Dike 已提交
258 259 260
	for (i = 1; i < argc; i++) {
		if ((i == 1) && (argv[i][0] == ' '))
			continue;
L
Linus Torvalds 已提交
261 262 263 264 265
		add = 1;
		uml_checksetup(argv[i], &add);
		if (add)
			add_arg(argv[i]);
	}
J
Jeff Dike 已提交
266
	if (have_root == 0)
L
Linus Torvalds 已提交
267 268
		add_arg(DEFAULT_COMMAND_LINE);

J
Jeff Dike 已提交
269
	/* OS sanity checks that need to happen before the kernel runs */
270
	os_early_checks();
271

J
Jeff Dike 已提交
272 273 274
	can_do_skas();

	if (proc_mm && ptrace_faultinfo)
275 276 277 278 279 280
		mode = "SKAS3";
	else
		mode = "SKAS0";

	printf("UML running in %s mode\n", mode);

J
Jeff Dike 已提交
281
	host_task_size = set_task_sizes_skas(&task_size);
L
Linus Torvalds 已提交
282 283

	brk_start = (unsigned long) sbrk(0);
284

J
Jeff Dike 已提交
285 286 287 288 289
	/*
	 * Increase physical memory size for exec-shield users
	 * so they actually get what they asked for. This should
	 * add zero for non-exec shield users
	 */
L
Linus Torvalds 已提交
290 291

	diff = UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
J
Jeff Dike 已提交
292
	if (diff > 1024 * 1024) {
L
Linus Torvalds 已提交
293 294 295 296 297
		printf("Adding %ld bytes to physical memory to account for "
		       "exec-shield gap\n", diff);
		physmem_size += UML_ROUND_UP(brk_start) - UML_ROUND_UP(&_end);
	}

J
Jeff Dike 已提交
298
	uml_physmem = (unsigned long) &__binary_start & PAGE_MASK;
L
Linus Torvalds 已提交
299 300 301 302

	/* Reserve up to 4M after the current brk */
	uml_reserved = ROUND_4M(brk_start) + (1 << 22);

303
	setup_machinename(init_utsname()->machine);
L
Linus Torvalds 已提交
304 305 306 307 308

	highmem = 0;
	iomem_size = (iomem_size + PAGE_SIZE - 1) & PAGE_MASK;
	max_physmem = get_kmem_end() - uml_physmem - iomem_size - MIN_VMALLOC;

J
Jeff Dike 已提交
309 310
	/*
	 * Zones have to begin on a 1 << MAX_ORDER page boundary,
L
Linus Torvalds 已提交
311 312 313
	 * so this makes sure that's true for highmem
	 */
	max_physmem &= ~((1 << (PAGE_SHIFT + MAX_ORDER)) - 1);
J
Jeff Dike 已提交
314
	if (physmem_size + iomem_size > max_physmem) {
L
Linus Torvalds 已提交
315 316 317 318 319
		highmem = physmem_size + iomem_size - max_physmem;
		physmem_size -= highmem;
#ifndef CONFIG_HIGHMEM
		highmem = 0;
		printf("CONFIG_HIGHMEM not enabled - physical memory shrunk "
J
Jeff Dike 已提交
320
		       "to %Lu bytes\n", physmem_size);
L
Linus Torvalds 已提交
321 322 323 324 325 326 327 328 329 330
#endif
	}

	high_physmem = uml_physmem + physmem_size;
	end_iomem = high_physmem + iomem_size;
	high_memory = (void *) end_iomem;

	start_vm = VMALLOC_START;

	setup_physmem(uml_physmem, uml_reserved, physmem_size, highmem);
J
Jeff Dike 已提交
331
	if (init_maps(physmem_size, iomem_size, highmem)) {
J
Jeff Dike 已提交
332 333
		printf("Failed to allocate mem_map for %Lu bytes of physical "
		       "memory and %Lu bytes of highmem\n", physmem_size,
L
Linus Torvalds 已提交
334 335 336 337 338 339
		       highmem);
		exit(1);
	}

	virtmem_size = physmem_size;
	avail = get_kmem_end() - start_vm;
J
Jeff Dike 已提交
340 341
	if (physmem_size > avail)
		virtmem_size = avail;
L
Linus Torvalds 已提交
342 343
	end_vm = start_vm + virtmem_size;

J
Jeff Dike 已提交
344
	if (virtmem_size < physmem_size)
J
Jeff Dike 已提交
345
		printf("Kernel virtual memory size shrunk to %lu bytes\n",
L
Linus Torvalds 已提交
346 347
		       virtmem_size);

J
Jeff Dike 已提交
348
	uml_postsetup();
L
Linus Torvalds 已提交
349

J
Jeff Dike 已提交
350
	stack_protections((unsigned long) &init_thread_info);
L
Linus Torvalds 已提交
351 352
	os_flush_stdout();

353
	return start_uml();
L
Linus Torvalds 已提交
354 355 356 357 358 359 360 361 362 363 364
}

extern int uml_exitcode;

static int panic_exit(struct notifier_block *self, unsigned long unused1,
		      void *unused2)
{
	bust_spinlocks(1);
	show_regs(&(current->thread.regs));
	bust_spinlocks(0);
	uml_exitcode = 1;
J
Jeff Dike 已提交
365
	os_dump_core();
J
Jeff Dike 已提交
366
	return 0;
L
Linus Torvalds 已提交
367 368 369 370 371 372 373 374 375 376
}

static struct notifier_block panic_exit_notifier = {
	.notifier_call 		= panic_exit,
	.next 			= NULL,
	.priority 		= 0
};

void __init setup_arch(char **cmdline_p)
{
377 378
	atomic_notifier_chain_register(&panic_notifier_list,
			&panic_exit_notifier);
L
Linus Torvalds 已提交
379
	paging_init();
380
	strlcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
J
Jeff Dike 已提交
381
	*cmdline_p = command_line;
J
Jeff Dike 已提交
382
	setup_hostinfo(host_info, sizeof host_info);
L
Linus Torvalds 已提交
383 384 385 386 387
}

void __init check_bugs(void)
{
	arch_check_bugs();
J
Jeff Dike 已提交
388
	os_check_bugs();
L
Linus Torvalds 已提交
389 390
}

G
Gerd Hoffmann 已提交
391 392 393 394
void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
{
}

T
Theodore Tso 已提交
395
#ifdef CONFIG_SMP
G
Gerd Hoffmann 已提交
396 397 398 399 400 401 402
void alternatives_smp_module_add(struct module *mod, char *name,
				 void *locks, void *locks_end,
				 void *text,  void *text_end)
{
}

void alternatives_smp_module_del(struct module *mod)
L
Linus Torvalds 已提交
403 404
{
}
T
Theodore Tso 已提交
405
#endif