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

#include <stdio.h>
#include <stdlib.h>
J
Jeff Dike 已提交
8
#include <unistd.h>
L
Linus Torvalds 已提交
9
#include <errno.h>
J
Jeff Dike 已提交
10 11
#include <signal.h>
#include <string.h>
L
Linus Torvalds 已提交
12
#include <sys/resource.h>
J
Jeff Dike 已提交
13
#include "as-layout.h"
L
Linus Torvalds 已提交
14
#include "init.h"
J
Jeff Dike 已提交
15 16
#include "kern_constants.h"
#include "kern_util.h"
L
Linus Torvalds 已提交
17
#include "os.h"
18
#include "um_malloc.h"
L
Linus Torvalds 已提交
19 20 21 22 23

#define PGD_BOUND (4 * 1024 * 1024)
#define STACKSIZE (8 * 1024 * 1024)
#define THREAD_NAME_LEN (256)

24 25
long elf_aux_hwcap;

L
Linus Torvalds 已提交
26 27 28 29
static void set_stklim(void)
{
	struct rlimit lim;

J
Jeff Dike 已提交
30
	if (getrlimit(RLIMIT_STACK, &lim) < 0) {
L
Linus Torvalds 已提交
31 32 33
		perror("getrlimit");
		exit(1);
	}
J
Jeff Dike 已提交
34
	if ((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)) {
L
Linus Torvalds 已提交
35
		lim.rlim_cur = STACKSIZE;
J
Jeff Dike 已提交
36
		if (setrlimit(RLIMIT_STACK, &lim) < 0) {
L
Linus Torvalds 已提交
37 38 39 40 41 42 43 44 45 46 47
			perror("setrlimit");
			exit(1);
		}
	}
}

static __init void do_uml_initcalls(void)
{
	initcall_t *call;

	call = &__uml_initcall_start;
J
Jeff Dike 已提交
48
	while (call < &__uml_initcall_end) {
L
Linus Torvalds 已提交
49 50 51 52 53 54 55 56 57 58 59
		(*call)();
		call++;
	}
}

static void last_ditch_exit(int sig)
{
	uml_cleanup();
	exit(1);
}

60 61 62 63 64 65 66
static void install_fatal_handler(int sig)
{
	struct sigaction action;

	/* All signals are enabled in this handler ... */
	sigemptyset(&action.sa_mask);

J
Jeff Dike 已提交
67 68
	/*
	 * ... including the signal being handled, plus we want the
69 70 71 72 73 74 75
	 * handler reset to the default behavior, so that if an exit
	 * handler is hanging for some reason, the UML will just die
	 * after this signal is sent a second time.
	 */
	action.sa_flags = SA_RESETHAND | SA_NODEFER;
	action.sa_restorer = NULL;
	action.sa_handler = last_ditch_exit;
J
Jeff Dike 已提交
76
	if (sigaction(sig, &action, NULL) < 0) {
77
		printf("failed to install handler for signal %d - errno = %d\n",
78
		       sig, errno);
79 80 81 82
		exit(1);
	}
}

R
Richard Weinberger 已提交
83
#define UML_LIB_PATH	":" OS_LIB_PATH "/uml"
84 85 86 87 88 89 90 91

static void setup_env_path(void)
{
	char *new_path = NULL;
	char *old_path = NULL;
	int path_len = 0;

	old_path = getenv("PATH");
J
Jeff Dike 已提交
92 93
	/*
	 * if no PATH variable is set or it has an empty value
94 95 96
	 * just use the default + /usr/lib/uml
	 */
	if (!old_path || (path_len = strlen(old_path)) == 0) {
97 98
		if (putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH))
			perror("couldn't putenv");
99 100 101 102 103 104 105
		return;
	}

	/* append /usr/lib/uml to the existing path */
	path_len += strlen("PATH=" UML_LIB_PATH) + 1;
	new_path = malloc(path_len);
	if (!new_path) {
106
		perror("couldn't malloc to set a new PATH");
107 108 109
		return;
	}
	snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path);
110 111 112 113
	if (putenv(new_path)) {
		perror("couldn't putenv to set a new PATH");
		free(new_path);
	}
114 115
}

L
Linus Torvalds 已提交
116 117
extern void scan_elf_aux( char **envp);

J
Jeff Dike 已提交
118
int __init main(int argc, char **argv, char **envp)
L
Linus Torvalds 已提交
119 120
{
	char **new_argv;
121
	int ret, i, err;
L
Linus Torvalds 已提交
122 123 124

	set_stklim();

125 126
	setup_env_path();

L
Linus Torvalds 已提交
127
	new_argv = malloc((argc + 1) * sizeof(char *));
J
Jeff Dike 已提交
128
	if (new_argv == NULL) {
L
Linus Torvalds 已提交
129 130 131
		perror("Mallocing argv");
		exit(1);
	}
J
Jeff Dike 已提交
132
	for (i = 0; i < argc; i++) {
L
Linus Torvalds 已提交
133
		new_argv[i] = strdup(argv[i]);
J
Jeff Dike 已提交
134
		if (new_argv[i] == NULL) {
L
Linus Torvalds 已提交
135 136 137 138 139 140
			perror("Mallocing an arg");
			exit(1);
		}
	}
	new_argv[argc] = NULL;

J
Jeff Dike 已提交
141 142
	/*
	 * Allow these signals to bring down a UML if all other
143 144 145 146
	 * methods of control fail.
	 */
	install_fatal_handler(SIGINT);
	install_fatal_handler(SIGTERM);
L
Linus Torvalds 已提交
147

148
#ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA
J
Jeff Dike 已提交
149
	scan_elf_aux(envp);
150
#endif
L
Linus Torvalds 已提交
151 152 153 154

	do_uml_initcalls();
	ret = linux_main(argc, argv);

J
Jeff Dike 已提交
155 156
	/*
	 * Disable SIGPROF - I have no idea why libc doesn't do this or turn
L
Linus Torvalds 已提交
157 158 159 160 161
	 * off the profiling time, but UML dies with a SIGPROF just before
	 * exiting when profiling is active.
	 */
	change_sig(SIGPROF, 0);

J
Jeff Dike 已提交
162 163
	/*
	 * This signal stuff used to be in the reboot case.  However,
164 165 166 167 168
	 * sometimes a SIGVTALRM can come in when we're halting (reproducably
	 * when writing out gcov information, presumably because that takes
	 * some time) and cause a segfault.
	 */

J
Jeff Dike 已提交
169
	/* stop timers and set SIGVTALRM to be ignored */
170 171 172 173
	disable_timer();

	/* disable SIGIO for the fds and set SIGIO to be ignored */
	err = deactivate_all_fds();
J
Jeff Dike 已提交
174
	if (err)
175 176
		printf("deactivate_all_fds failed, errno = %d\n", -err);

J
Jeff Dike 已提交
177 178
	/*
	 * Let any pending signals fire now.  This ensures
179 180 181 182
	 * that they won't be delivered after the exec, when
	 * they are definitely not expected.
	 */
	unblock_signals();
L
Linus Torvalds 已提交
183

184
	/* Reboot */
J
Jeff Dike 已提交
185
	if (ret) {
186
		printf("\n");
L
Linus Torvalds 已提交
187 188 189 190 191
		execvp(new_argv[0], new_argv);
		perror("Failed to exec kernel");
		ret = 1;
	}
	printf("\n");
J
Jeff Dike 已提交
192
	return uml_exitcode;
L
Linus Torvalds 已提交
193 194 195 196 197 198 199 200
}

extern void *__real_malloc(int);

void *__wrap_malloc(int size)
{
	void *ret;

J
Jeff Dike 已提交
201
	if (!kmalloc_ok)
J
Jeff Dike 已提交
202
		return __real_malloc(size);
J
Jeff Dike 已提交
203
	else if (size <= UM_KERN_PAGE_SIZE)
J
Jeff Dike 已提交
204
		/* finding contiguous pages can be hard*/
J
Jeff Dike 已提交
205
		ret = uml_kmalloc(size, UM_GFP_KERNEL);
206
	else ret = vmalloc(size);
L
Linus Torvalds 已提交
207

J
Jeff Dike 已提交
208 209
	/*
	 * glibc people insist that if malloc fails, errno should be
L
Linus Torvalds 已提交
210 211
	 * set by malloc as well. So we do.
	 */
J
Jeff Dike 已提交
212
	if (ret == NULL)
L
Linus Torvalds 已提交
213 214
		errno = ENOMEM;

J
Jeff Dike 已提交
215
	return ret;
L
Linus Torvalds 已提交
216 217 218 219 220 221
}

void *__wrap_calloc(int n, int size)
{
	void *ptr = __wrap_malloc(n * size);

J
Jeff Dike 已提交
222
	if (ptr == NULL)
J
Jeff Dike 已提交
223
		return NULL;
L
Linus Torvalds 已提交
224
	memset(ptr, 0, n * size);
J
Jeff Dike 已提交
225
	return ptr;
L
Linus Torvalds 已提交
226 227 228 229 230 231 232 233 234 235
}

extern void __real_free(void *);

extern unsigned long high_physmem;

void __wrap_free(void *ptr)
{
	unsigned long addr = (unsigned long) ptr;

J
Jeff Dike 已提交
236 237
	/*
	 * We need to know how the allocation happened, so it can be correctly
L
Linus Torvalds 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	 * freed.  This is done by seeing what region of memory the pointer is
	 * in -
	 * 	physical memory - kmalloc/kfree
	 *	kernel virtual memory - vmalloc/vfree
	 * 	anywhere else - malloc/free
	 * If kmalloc is not yet possible, then either high_physmem and/or
	 * end_vm are still 0 (as at startup), in which case we call free, or
	 * we have set them, but anyway addr has not been allocated from those
	 * areas. So, in both cases __real_free is called.
	 *
	 * CAN_KMALLOC is checked because it would be bad to free a buffer
	 * with kmalloc/vmalloc after they have been turned off during
	 * shutdown.
	 * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
	 * there is a possibility for memory leaks.
	 */

J
Jeff Dike 已提交
255 256
	if ((addr >= uml_physmem) && (addr < high_physmem)) {
		if (kmalloc_ok)
L
Linus Torvalds 已提交
257 258
			kfree(ptr);
	}
J
Jeff Dike 已提交
259 260
	else if ((addr >= start_vm) && (addr < end_vm)) {
		if (kmalloc_ok)
L
Linus Torvalds 已提交
261 262 263 264
			vfree(ptr);
	}
	else __real_free(ptr);
}