2 * Copyright (C) 2000, 2001 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
12 #include <sys/resource.h>
16 #include "user_util.h"
17 #include "kern_util.h"
23 #include "choose-mode.h"
24 #include "uml-config.h"
27 /* Set in set_stklim, which is called from main and __wrap_malloc.
28 * __wrap_malloc only calls it if main hasn't started.
30 unsigned long stacksizelim;
35 #define PGD_BOUND (4 * 1024 * 1024)
36 #define STACKSIZE (8 * 1024 * 1024)
37 #define THREAD_NAME_LEN (256)
39 static void set_stklim(void)
43 if(getrlimit(RLIMIT_STACK, &lim) < 0){
47 if((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)){
48 lim.rlim_cur = STACKSIZE;
49 if(setrlimit(RLIMIT_STACK, &lim) < 0){
54 stacksizelim = (lim.rlim_cur + PGD_BOUND - 1) & ~(PGD_BOUND - 1);
57 static __init void do_uml_initcalls(void)
61 call = &__uml_initcall_start;
62 while (call < &__uml_initcall_end){
68 static void last_ditch_exit(int sig)
70 signal(SIGINT, SIG_DFL);
71 signal(SIGTERM, SIG_DFL);
72 signal(SIGHUP, SIG_DFL);
77 #define UML_LIB_PATH ":/usr/lib/uml"
79 static void setup_env_path(void)
81 char *new_path = NULL;
82 char *old_path = NULL;
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
89 if (!old_path || (path_len = strlen(old_path)) == 0) {
90 putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH);
94 /* append /usr/lib/uml to the existing path */
95 path_len += strlen("PATH=" UML_LIB_PATH) + 1;
96 new_path = malloc(path_len);
98 perror("coudn't malloc to set a new PATH");
101 snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path);
105 extern int uml_exitcode;
107 extern void scan_elf_aux( char **envp);
109 int main(int argc, char **argv, char **envp)
114 #ifdef UML_CONFIG_CMDLINE_ON_HOST
115 /* Allocate memory for thread command lines */
116 if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
118 char padding[THREAD_NAME_LEN] = {
119 [ 0 ... THREAD_NAME_LEN - 2] = ' ', '\0'
122 new_argv = malloc((argc + 2) * sizeof(char*));
124 perror("Allocating extended argv");
128 new_argv[0] = argv[0];
129 new_argv[1] = padding;
131 for(i = 2; i <= argc; i++)
132 new_argv[i] = argv[i - 1];
133 new_argv[argc + 1] = NULL;
135 execvp(new_argv[0], new_argv);
136 perror("execing with extended args");
141 linux_prog = argv[0];
147 new_argv = malloc((argc + 1) * sizeof(char *));
148 if(new_argv == NULL){
149 perror("Mallocing argv");
153 new_argv[i] = strdup(argv[i]);
154 if(new_argv[i] == NULL){
155 perror("Mallocing an arg");
159 new_argv[argc] = NULL;
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);
168 ret = linux_main(argc, argv);
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.
174 change_sig(SIGPROF, 0);
176 /* 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.
182 /* stop timers and set SIG*ALRM to be ignored */
185 /* disable SIGIO for the fds and set SIGIO to be ignored */
186 err = deactivate_all_fds();
188 printf("deactivate_all_fds failed, errno = %d\n", -err);
190 /* 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.
199 execvp(new_argv[0], new_argv);
200 perror("Failed to exec kernel");
204 return(uml_exitcode);
207 #define CAN_KMALLOC() \
208 (kmalloc_ok && CHOOSE_MODE((os_getpid() != tracing_pid), 1))
210 extern void *__real_malloc(int);
212 void *__wrap_malloc(int size)
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);
222 /* glibc people insist that if malloc fails, errno should be
223 * set by malloc as well. So we do.
231 void *__wrap_calloc(int n, int size)
233 void *ptr = __wrap_malloc(n * size);
235 if(ptr == NULL) return(NULL);
236 memset(ptr, 0, n * size);
240 extern void __real_free(void *);
242 extern unsigned long high_physmem;
244 void __wrap_free(void *ptr)
246 unsigned long addr = (unsigned long) ptr;
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
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.
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
262 * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
263 * there is a possibility for memory leaks.
266 if((addr >= uml_physmem) && (addr < high_physmem)){
270 else if((addr >= start_vm) && (addr < end_vm)){
274 else __real_free(ptr);