2 * Copyright (C) 2000, 2001 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
12 #include <sys/resource.h>
16 #include "kern_util.h"
17 #include "as-layout.h"
23 #include "choose-mode.h"
24 #include "uml-config.h"
26 #include "um_malloc.h"
27 #include "kern_constants.h"
29 /* Set in main, unchanged thereafter */
32 #define PGD_BOUND (4 * 1024 * 1024)
33 #define STACKSIZE (8 * 1024 * 1024)
34 #define THREAD_NAME_LEN (256)
36 static void set_stklim(void)
40 if(getrlimit(RLIMIT_STACK, &lim) < 0){
44 if((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)){
45 lim.rlim_cur = STACKSIZE;
46 if(setrlimit(RLIMIT_STACK, &lim) < 0){
53 static __init void do_uml_initcalls(void)
57 call = &__uml_initcall_start;
58 while (call < &__uml_initcall_end){
64 static void last_ditch_exit(int sig)
70 static void install_fatal_handler(int sig)
72 struct sigaction action;
74 /* All signals are enabled in this handler ... */
75 sigemptyset(&action.sa_mask);
77 /* ... including the signal being handled, plus we want the
78 * handler reset to the default behavior, so that if an exit
79 * handler is hanging for some reason, the UML will just die
80 * after this signal is sent a second time.
82 action.sa_flags = SA_RESETHAND | SA_NODEFER;
83 action.sa_restorer = NULL;
84 action.sa_handler = last_ditch_exit;
85 if(sigaction(sig, &action, NULL) < 0){
86 printf("failed to install handler for signal %d - errno = %d\n",
92 #define UML_LIB_PATH ":/usr/lib/uml"
94 static void setup_env_path(void)
96 char *new_path = NULL;
97 char *old_path = NULL;
100 old_path = getenv("PATH");
101 /* if no PATH variable is set or it has an empty value
102 * just use the default + /usr/lib/uml
104 if (!old_path || (path_len = strlen(old_path)) == 0) {
105 putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH);
109 /* append /usr/lib/uml to the existing path */
110 path_len += strlen("PATH=" UML_LIB_PATH) + 1;
111 new_path = malloc(path_len);
113 perror("coudn't malloc to set a new PATH");
116 snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path);
120 extern int uml_exitcode;
122 extern void scan_elf_aux( char **envp);
124 int __init main(int argc, char **argv, char **envp)
129 #ifdef UML_CONFIG_CMDLINE_ON_HOST
130 /* Allocate memory for thread command lines */
131 if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
133 char padding[THREAD_NAME_LEN] = {
134 [ 0 ... THREAD_NAME_LEN - 2] = ' ', '\0'
137 new_argv = malloc((argc + 2) * sizeof(char*));
139 perror("Allocating extended argv");
143 new_argv[0] = argv[0];
144 new_argv[1] = padding;
146 for(i = 2; i <= argc; i++)
147 new_argv[i] = argv[i - 1];
148 new_argv[argc + 1] = NULL;
150 execvp(new_argv[0], new_argv);
151 perror("execing with extended args");
156 linux_prog = argv[0];
162 new_argv = malloc((argc + 1) * sizeof(char *));
163 if(new_argv == NULL){
164 perror("Mallocing argv");
168 new_argv[i] = strdup(argv[i]);
169 if(new_argv[i] == NULL){
170 perror("Mallocing an arg");
174 new_argv[argc] = NULL;
176 /* Allow these signals to bring down a UML if all other
177 * methods of control fail.
179 install_fatal_handler(SIGINT);
180 install_fatal_handler(SIGTERM);
181 install_fatal_handler(SIGHUP);
186 ret = linux_main(argc, argv);
188 /* Disable SIGPROF - I have no idea why libc doesn't do this or turn
189 * off the profiling time, but UML dies with a SIGPROF just before
190 * exiting when profiling is active.
192 change_sig(SIGPROF, 0);
194 /* This signal stuff used to be in the reboot case. However,
195 * sometimes a SIGVTALRM can come in when we're halting (reproducably
196 * when writing out gcov information, presumably because that takes
197 * some time) and cause a segfault.
200 /* stop timers and set SIG*ALRM to be ignored */
203 /* disable SIGIO for the fds and set SIGIO to be ignored */
204 err = deactivate_all_fds();
206 printf("deactivate_all_fds failed, errno = %d\n", -err);
208 /* Let any pending signals fire now. This ensures
209 * that they won't be delivered after the exec, when
210 * they are definitely not expected.
217 execvp(new_argv[0], new_argv);
218 perror("Failed to exec kernel");
225 #define CAN_KMALLOC() \
226 (kmalloc_ok && CHOOSE_MODE((os_getpid() != tracing_pid), 1))
228 extern void *__real_malloc(int);
230 void *__wrap_malloc(int size)
235 return __real_malloc(size);
236 else if(size <= UM_KERN_PAGE_SIZE)
237 /* finding contiguous pages can be hard*/
238 ret = kmalloc(size, UM_GFP_KERNEL);
239 else ret = vmalloc(size);
241 /* glibc people insist that if malloc fails, errno should be
242 * set by malloc as well. So we do.
250 void *__wrap_calloc(int n, int size)
252 void *ptr = __wrap_malloc(n * size);
256 memset(ptr, 0, n * size);
260 extern void __real_free(void *);
262 extern unsigned long high_physmem;
264 void __wrap_free(void *ptr)
266 unsigned long addr = (unsigned long) ptr;
268 /* We need to know how the allocation happened, so it can be correctly
269 * freed. This is done by seeing what region of memory the pointer is
271 * physical memory - kmalloc/kfree
272 * kernel virtual memory - vmalloc/vfree
273 * anywhere else - malloc/free
274 * If kmalloc is not yet possible, then either high_physmem and/or
275 * end_vm are still 0 (as at startup), in which case we call free, or
276 * we have set them, but anyway addr has not been allocated from those
277 * areas. So, in both cases __real_free is called.
279 * CAN_KMALLOC is checked because it would be bad to free a buffer
280 * with kmalloc/vmalloc after they have been turned off during
282 * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
283 * there is a possibility for memory leaks.
286 if((addr >= uml_physmem) && (addr < high_physmem)){
290 else if((addr >= start_vm) && (addr < end_vm)){
294 else __real_free(ptr);