blob: 90912aaca7aa154ef577e63bbc0d9c6aa6d87800 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2000, 2001 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include <unistd.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <signal.h>
11#include <errno.h>
12#include <sys/resource.h>
13#include <sys/mman.h>
14#include <sys/user.h>
15#include <asm/page.h>
16#include "user_util.h"
17#include "kern_util.h"
18#include "mem_user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "irq_user.h"
20#include "user.h"
21#include "init.h"
22#include "mode.h"
23#include "choose-mode.h"
24#include "uml-config.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "os.h"
26
27/* Set in set_stklim, which is called from main and __wrap_malloc.
28 * __wrap_malloc only calls it if main hasn't started.
29 */
30unsigned long stacksizelim;
31
32/* Set in main */
33char *linux_prog;
34
35#define PGD_BOUND (4 * 1024 * 1024)
36#define STACKSIZE (8 * 1024 * 1024)
37#define THREAD_NAME_LEN (256)
38
39static void set_stklim(void)
40{
41 struct rlimit lim;
42
43 if(getrlimit(RLIMIT_STACK, &lim) < 0){
44 perror("getrlimit");
45 exit(1);
46 }
47 if((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)){
48 lim.rlim_cur = STACKSIZE;
49 if(setrlimit(RLIMIT_STACK, &lim) < 0){
50 perror("setrlimit");
51 exit(1);
52 }
53 }
54 stacksizelim = (lim.rlim_cur + PGD_BOUND - 1) & ~(PGD_BOUND - 1);
55}
56
57static __init void do_uml_initcalls(void)
58{
59 initcall_t *call;
60
61 call = &__uml_initcall_start;
Jeff Dikef2183122006-06-04 02:51:47 -070062 while (call < &__uml_initcall_end){
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 (*call)();
64 call++;
65 }
66}
67
68static void last_ditch_exit(int sig)
69{
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 signal(SIGINT, SIG_DFL);
71 signal(SIGTERM, SIG_DFL);
72 signal(SIGHUP, SIG_DFL);
73 uml_cleanup();
74 exit(1);
75}
76
Mattia Dongilicb98cdc2006-05-01 12:16:01 -070077#define UML_LIB_PATH ":/usr/lib/uml"
78
79static void setup_env_path(void)
80{
81 char *new_path = NULL;
82 char *old_path = NULL;
83 int path_len = 0;
84
85 old_path = getenv("PATH");
86 /* if no PATH variable is set or it has an empty value
87 * just use the default + /usr/lib/uml
88 */
89 if (!old_path || (path_len = strlen(old_path)) == 0) {
90 putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH);
91 return;
92 }
93
94 /* append /usr/lib/uml to the existing path */
95 path_len += strlen("PATH=" UML_LIB_PATH) + 1;
96 new_path = malloc(path_len);
97 if (!new_path) {
98 perror("coudn't malloc to set a new PATH");
99 return;
100 }
101 snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path);
102 putenv(new_path);
103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105extern int uml_exitcode;
106
107extern void scan_elf_aux( char **envp);
108
109int main(int argc, char **argv, char **envp)
110{
111 char **new_argv;
Jeff Dike92515da2005-05-28 15:51:56 -0700112 int ret, i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Paolo 'Blaisorblade' Giarrusso02215752005-09-03 15:57:23 -0700114#ifdef UML_CONFIG_CMDLINE_ON_HOST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 /* Allocate memory for thread command lines */
116 if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
117
118 char padding[THREAD_NAME_LEN] = {
119 [ 0 ... THREAD_NAME_LEN - 2] = ' ', '\0'
120 };
121
122 new_argv = malloc((argc + 2) * sizeof(char*));
123 if(!new_argv) {
124 perror("Allocating extended argv");
125 exit(1);
126 }
127
128 new_argv[0] = argv[0];
129 new_argv[1] = padding;
130
131 for(i = 2; i <= argc; i++)
132 new_argv[i] = argv[i - 1];
133 new_argv[argc + 1] = NULL;
134
135 execvp(new_argv[0], new_argv);
136 perror("execing with extended args");
137 exit(1);
138 }
139#endif
140
141 linux_prog = argv[0];
142
143 set_stklim();
144
Mattia Dongilicb98cdc2006-05-01 12:16:01 -0700145 setup_env_path();
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 new_argv = malloc((argc + 1) * sizeof(char *));
148 if(new_argv == NULL){
149 perror("Mallocing argv");
150 exit(1);
151 }
152 for(i=0;i<argc;i++){
153 new_argv[i] = strdup(argv[i]);
154 if(new_argv[i] == NULL){
155 perror("Mallocing an arg");
156 exit(1);
157 }
158 }
159 new_argv[argc] = NULL;
160
161 set_handler(SIGINT, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
162 set_handler(SIGTERM, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
163 set_handler(SIGHUP, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
164
165 scan_elf_aux( envp);
166
167 do_uml_initcalls();
168 ret = linux_main(argc, argv);
169
170 /* Disable SIGPROF - I have no idea why libc doesn't do this or turn
171 * off the profiling time, but UML dies with a SIGPROF just before
172 * exiting when profiling is active.
173 */
174 change_sig(SIGPROF, 0);
175
Jeff Dike52c653b2005-11-07 00:58:50 -0800176 /* This signal stuff used to be in the reboot case. However,
177 * sometimes a SIGVTALRM can come in when we're halting (reproducably
178 * when writing out gcov information, presumably because that takes
179 * some time) and cause a segfault.
180 */
Jeff Dike92515da2005-05-28 15:51:56 -0700181
Jeff Dike52c653b2005-11-07 00:58:50 -0800182 /* stop timers and set SIG*ALRM to be ignored */
183 disable_timer();
Jeff Dike92515da2005-05-28 15:51:56 -0700184
Jeff Dike52c653b2005-11-07 00:58:50 -0800185 /* disable SIGIO for the fds and set SIGIO to be ignored */
186 err = deactivate_all_fds();
187 if(err)
188 printf("deactivate_all_fds failed, errno = %d\n", -err);
Jeff Dike92515da2005-05-28 15:51:56 -0700189
Jeff Dike52c653b2005-11-07 00:58:50 -0800190 /* Let any pending signals fire now. This ensures
191 * that they won't be delivered after the exec, when
192 * they are definitely not expected.
193 */
194 unblock_signals();
Jeff Dike92515da2005-05-28 15:51:56 -0700195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /* Reboot */
197 if(ret){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 printf("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 execvp(new_argv[0], new_argv);
200 perror("Failed to exec kernel");
201 ret = 1;
202 }
203 printf("\n");
204 return(uml_exitcode);
205}
206
207#define CAN_KMALLOC() \
208 (kmalloc_ok && CHOOSE_MODE((os_getpid() != tracing_pid), 1))
209
210extern void *__real_malloc(int);
211
212void *__wrap_malloc(int size)
213{
214 void *ret;
215
216 if(!CAN_KMALLOC())
217 return(__real_malloc(size));
218 else if(size <= PAGE_SIZE) /* finding contiguos pages can be hard*/
219 ret = um_kmalloc(size);
220 else ret = um_vmalloc(size);
221
222 /* glibc people insist that if malloc fails, errno should be
223 * set by malloc as well. So we do.
224 */
225 if(ret == NULL)
226 errno = ENOMEM;
227
228 return(ret);
229}
230
231void *__wrap_calloc(int n, int size)
232{
233 void *ptr = __wrap_malloc(n * size);
234
235 if(ptr == NULL) return(NULL);
236 memset(ptr, 0, n * size);
237 return(ptr);
238}
239
240extern void __real_free(void *);
241
242extern unsigned long high_physmem;
243
244void __wrap_free(void *ptr)
245{
246 unsigned long addr = (unsigned long) ptr;
247
248 /* We need to know how the allocation happened, so it can be correctly
249 * freed. This is done by seeing what region of memory the pointer is
250 * in -
251 * physical memory - kmalloc/kfree
252 * kernel virtual memory - vmalloc/vfree
253 * anywhere else - malloc/free
254 * If kmalloc is not yet possible, then either high_physmem and/or
255 * end_vm are still 0 (as at startup), in which case we call free, or
256 * we have set them, but anyway addr has not been allocated from those
257 * areas. So, in both cases __real_free is called.
258 *
259 * CAN_KMALLOC is checked because it would be bad to free a buffer
260 * with kmalloc/vmalloc after they have been turned off during
261 * shutdown.
262 * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
263 * there is a possibility for memory leaks.
264 */
265
266 if((addr >= uml_physmem) && (addr < high_physmem)){
267 if(CAN_KMALLOC())
268 kfree(ptr);
269 }
270 else if((addr >= start_vm) && (addr < end_vm)){
271 if(CAN_KMALLOC())
272 vfree(ptr);
273 }
274 else __real_free(ptr);
275}