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 extern int uml_exitcode;
79 extern void scan_elf_aux( char **envp);
81 int main(int argc, char **argv, char **envp)
87 /* Enable all signals except SIGIO - in some environments, we can
88 * enter with some signals blocked
92 sigaddset(&mask, SIGIO);
93 if(sigprocmask(SIG_SETMASK, &mask, NULL) < 0){
94 perror("sigprocmask");
98 #ifdef UML_CONFIG_CMDLINE_ON_HOST
99 /* Allocate memory for thread command lines */
100 if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
102 char padding[THREAD_NAME_LEN] = {
103 [ 0 ... THREAD_NAME_LEN - 2] = ' ', '\0'
106 new_argv = malloc((argc + 2) * sizeof(char*));
108 perror("Allocating extended argv");
112 new_argv[0] = argv[0];
113 new_argv[1] = padding;
115 for(i = 2; i <= argc; i++)
116 new_argv[i] = argv[i - 1];
117 new_argv[argc + 1] = NULL;
119 execvp(new_argv[0], new_argv);
120 perror("execing with extended args");
125 linux_prog = argv[0];
129 new_argv = malloc((argc + 1) * sizeof(char *));
130 if(new_argv == NULL){
131 perror("Mallocing argv");
135 new_argv[i] = strdup(argv[i]);
136 if(new_argv[i] == NULL){
137 perror("Mallocing an arg");
141 new_argv[argc] = NULL;
143 set_handler(SIGINT, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
144 set_handler(SIGTERM, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
145 set_handler(SIGHUP, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
150 ret = linux_main(argc, argv);
152 /* Disable SIGPROF - I have no idea why libc doesn't do this or turn
153 * off the profiling time, but UML dies with a SIGPROF just before
154 * exiting when profiling is active.
156 change_sig(SIGPROF, 0);
158 /* This signal stuff used to be in the reboot case. However,
159 * sometimes a SIGVTALRM can come in when we're halting (reproducably
160 * when writing out gcov information, presumably because that takes
161 * some time) and cause a segfault.
164 /* stop timers and set SIG*ALRM to be ignored */
167 /* disable SIGIO for the fds and set SIGIO to be ignored */
168 err = deactivate_all_fds();
170 printf("deactivate_all_fds failed, errno = %d\n", -err);
172 /* Let any pending signals fire now. This ensures
173 * that they won't be delivered after the exec, when
174 * they are definitely not expected.
181 execvp(new_argv[0], new_argv);
182 perror("Failed to exec kernel");
186 return(uml_exitcode);
189 #define CAN_KMALLOC() \
190 (kmalloc_ok && CHOOSE_MODE((os_getpid() != tracing_pid), 1))
192 extern void *__real_malloc(int);
194 void *__wrap_malloc(int size)
199 return(__real_malloc(size));
200 else if(size <= PAGE_SIZE) /* finding contiguos pages can be hard*/
201 ret = um_kmalloc(size);
202 else ret = um_vmalloc(size);
204 /* glibc people insist that if malloc fails, errno should be
205 * set by malloc as well. So we do.
213 void *__wrap_calloc(int n, int size)
215 void *ptr = __wrap_malloc(n * size);
217 if(ptr == NULL) return(NULL);
218 memset(ptr, 0, n * size);
222 extern void __real_free(void *);
224 extern unsigned long high_physmem;
226 void __wrap_free(void *ptr)
228 unsigned long addr = (unsigned long) ptr;
230 /* We need to know how the allocation happened, so it can be correctly
231 * freed. This is done by seeing what region of memory the pointer is
233 * physical memory - kmalloc/kfree
234 * kernel virtual memory - vmalloc/vfree
235 * anywhere else - malloc/free
236 * If kmalloc is not yet possible, then either high_physmem and/or
237 * end_vm are still 0 (as at startup), in which case we call free, or
238 * we have set them, but anyway addr has not been allocated from those
239 * areas. So, in both cases __real_free is called.
241 * CAN_KMALLOC is checked because it would be bad to free a buffer
242 * with kmalloc/vmalloc after they have been turned off during
244 * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
245 * there is a possibility for memory leaks.
248 if((addr >= uml_physmem) && (addr < high_physmem)){
252 else if((addr >= start_vm) && (addr < end_vm)){
256 else __real_free(ptr);