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"
26 #include "um_malloc.h"
28 /* Set in set_stklim, which is called from main and __wrap_malloc.
29 * __wrap_malloc only calls it if main hasn't started.
31 unsigned long stacksizelim;
36 #define PGD_BOUND (4 * 1024 * 1024)
37 #define STACKSIZE (8 * 1024 * 1024)
38 #define THREAD_NAME_LEN (256)
40 static void set_stklim(void)
44 if(getrlimit(RLIMIT_STACK, &lim) < 0){
48 if((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)){
49 lim.rlim_cur = STACKSIZE;
50 if(setrlimit(RLIMIT_STACK, &lim) < 0){
55 stacksizelim = (lim.rlim_cur + PGD_BOUND - 1) & ~(PGD_BOUND - 1);
58 static __init void do_uml_initcalls(void)
62 call = &__uml_initcall_start;
63 while (call < &__uml_initcall_end){
69 static void last_ditch_exit(int sig)
75 static void install_fatal_handler(int sig)
77 struct sigaction action;
79 /* All signals are enabled in this handler ... */
80 sigemptyset(&action.sa_mask);
82 /* ... including the signal being handled, plus we want the
83 * handler reset to the default behavior, so that if an exit
84 * handler is hanging for some reason, the UML will just die
85 * after this signal is sent a second time.
87 action.sa_flags = SA_RESETHAND | SA_NODEFER;
88 action.sa_restorer = NULL;
89 action.sa_handler = last_ditch_exit;
90 if(sigaction(sig, &action, NULL) < 0){
91 printf("failed to install handler for signal %d - errno = %d\n",
97 #define UML_LIB_PATH ":/usr/lib/uml"
99 static void setup_env_path(void)
101 char *new_path = NULL;
102 char *old_path = NULL;
105 old_path = getenv("PATH");
106 /* if no PATH variable is set or it has an empty value
107 * just use the default + /usr/lib/uml
109 if (!old_path || (path_len = strlen(old_path)) == 0) {
110 putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH);
114 /* append /usr/lib/uml to the existing path */
115 path_len += strlen("PATH=" UML_LIB_PATH) + 1;
116 new_path = malloc(path_len);
118 perror("coudn't malloc to set a new PATH");
121 snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path);
125 extern int uml_exitcode;
127 extern void scan_elf_aux( char **envp);
129 int main(int argc, char **argv, char **envp)
134 #ifdef UML_CONFIG_CMDLINE_ON_HOST
135 /* Allocate memory for thread command lines */
136 if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
138 char padding[THREAD_NAME_LEN] = {
139 [ 0 ... THREAD_NAME_LEN - 2] = ' ', '\0'
142 new_argv = malloc((argc + 2) * sizeof(char*));
144 perror("Allocating extended argv");
148 new_argv[0] = argv[0];
149 new_argv[1] = padding;
151 for(i = 2; i <= argc; i++)
152 new_argv[i] = argv[i - 1];
153 new_argv[argc + 1] = NULL;
155 execvp(new_argv[0], new_argv);
156 perror("execing with extended args");
161 linux_prog = argv[0];
167 new_argv = malloc((argc + 1) * sizeof(char *));
168 if(new_argv == NULL){
169 perror("Mallocing argv");
173 new_argv[i] = strdup(argv[i]);
174 if(new_argv[i] == NULL){
175 perror("Mallocing an arg");
179 new_argv[argc] = NULL;
181 /* Allow these signals to bring down a UML if all other
182 * methods of control fail.
184 install_fatal_handler(SIGINT);
185 install_fatal_handler(SIGTERM);
186 install_fatal_handler(SIGHUP);
191 ret = linux_main(argc, argv);
193 /* Disable SIGPROF - I have no idea why libc doesn't do this or turn
194 * off the profiling time, but UML dies with a SIGPROF just before
195 * exiting when profiling is active.
197 change_sig(SIGPROF, 0);
199 /* This signal stuff used to be in the reboot case. However,
200 * sometimes a SIGVTALRM can come in when we're halting (reproducably
201 * when writing out gcov information, presumably because that takes
202 * some time) and cause a segfault.
205 /* stop timers and set SIG*ALRM to be ignored */
208 /* disable SIGIO for the fds and set SIGIO to be ignored */
209 err = deactivate_all_fds();
211 printf("deactivate_all_fds failed, errno = %d\n", -err);
213 /* Let any pending signals fire now. This ensures
214 * that they won't be delivered after the exec, when
215 * they are definitely not expected.
222 execvp(new_argv[0], new_argv);
223 perror("Failed to exec kernel");
227 return(uml_exitcode);
230 #define CAN_KMALLOC() \
231 (kmalloc_ok && CHOOSE_MODE((os_getpid() != tracing_pid), 1))
233 extern void *__real_malloc(int);
235 void *__wrap_malloc(int size)
240 return(__real_malloc(size));
241 else if(size <= PAGE_SIZE) /* finding contiguos pages can be hard*/
242 ret = um_kmalloc(size);
243 else ret = um_vmalloc(size);
245 /* glibc people insist that if malloc fails, errno should be
246 * set by malloc as well. So we do.
254 void *__wrap_calloc(int n, int size)
256 void *ptr = __wrap_malloc(n * size);
258 if(ptr == NULL) return(NULL);
259 memset(ptr, 0, n * size);
263 extern void __real_free(void *);
265 extern unsigned long high_physmem;
267 void __wrap_free(void *ptr)
269 unsigned long addr = (unsigned long) ptr;
271 /* We need to know how the allocation happened, so it can be correctly
272 * freed. This is done by seeing what region of memory the pointer is
274 * physical memory - kmalloc/kfree
275 * kernel virtual memory - vmalloc/vfree
276 * anywhere else - malloc/free
277 * If kmalloc is not yet possible, then either high_physmem and/or
278 * end_vm are still 0 (as at startup), in which case we call free, or
279 * we have set them, but anyway addr has not been allocated from those
280 * areas. So, in both cases __real_free is called.
282 * CAN_KMALLOC is checked because it would be bad to free a buffer
283 * with kmalloc/vmalloc after they have been turned off during
285 * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
286 * there is a possibility for memory leaks.
289 if((addr >= uml_physmem) && (addr < high_physmem)){
293 else if((addr >= start_vm) && (addr < end_vm)){
297 else __real_free(ptr);