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"
19 #include "signal_user.h"
23 #include "choose-mode.h"
24 #include "uml-config.h"
26 /* Set in set_stklim, which is called from main and __wrap_malloc.
27 * __wrap_malloc only calls it if main hasn't started.
29 unsigned long stacksizelim;
34 #define PGD_BOUND (4 * 1024 * 1024)
35 #define STACKSIZE (8 * 1024 * 1024)
36 #define THREAD_NAME_LEN (256)
38 static void set_stklim(void)
42 if(getrlimit(RLIMIT_STACK, &lim) < 0){
46 if((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)){
47 lim.rlim_cur = STACKSIZE;
48 if(setrlimit(RLIMIT_STACK, &lim) < 0){
53 stacksizelim = (lim.rlim_cur + PGD_BOUND - 1) & ~(PGD_BOUND - 1);
56 static __init void do_uml_initcalls(void)
60 call = &__uml_initcall_start;
61 while (call < &__uml_initcall_end){;
67 static void last_ditch_exit(int sig)
69 CHOOSE_MODE(kmalloc_ok = 0, (void) 0);
70 signal(SIGINT, SIG_DFL);
71 signal(SIGTERM, SIG_DFL);
72 signal(SIGHUP, SIG_DFL);
77 extern int uml_exitcode;
79 int main(int argc, char **argv, char **envp)
85 /* Enable all signals except SIGIO - in some environments, we can
86 * enter with some signals blocked
90 sigaddset(&mask, SIGIO);
91 if(sigprocmask(SIG_SETMASK, &mask, NULL) < 0){
92 perror("sigprocmask");
96 #ifdef UML_CONFIG_MODE_TT
97 /* Allocate memory for thread command lines */
98 if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
100 char padding[THREAD_NAME_LEN] = {
101 [ 0 ... THREAD_NAME_LEN - 2] = ' ', '\0'
104 new_argv = malloc((argc + 2) * sizeof(char*));
106 perror("Allocating extended argv");
110 new_argv[0] = argv[0];
111 new_argv[1] = padding;
113 for(i = 2; i <= argc; i++)
114 new_argv[i] = argv[i - 1];
115 new_argv[argc + 1] = NULL;
117 execvp(new_argv[0], new_argv);
118 perror("execing with extended args");
123 linux_prog = argv[0];
127 new_argv = malloc((argc + 1) * sizeof(char *));
128 if(new_argv == NULL){
129 perror("Mallocing argv");
133 new_argv[i] = strdup(argv[i]);
134 if(new_argv[i] == NULL){
135 perror("Mallocing an arg");
139 new_argv[argc] = NULL;
141 set_handler(SIGINT, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
142 set_handler(SIGTERM, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
143 set_handler(SIGHUP, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
146 ret = linux_main(argc, argv);
154 /* Let any pending signals fire, then disable them. This
155 * ensures that they won't be delivered after the exec, when
156 * they are definitely not expected.
160 err = deactivate_all_fds();
162 printf("deactivate_all_fds failed, errno = %d\n",
165 execvp(new_argv[0], new_argv);
166 perror("Failed to exec kernel");
170 return(uml_exitcode);
173 #define CAN_KMALLOC() \
174 (kmalloc_ok && CHOOSE_MODE((getpid() != tracing_pid), 1))
176 extern void *__real_malloc(int);
178 void *__wrap_malloc(int size)
183 return(__real_malloc(size));
184 else if(size <= PAGE_SIZE) /* finding contiguos pages can be hard*/
185 ret = um_kmalloc(size);
186 else ret = um_vmalloc(size);
188 /* glibc people insist that if malloc fails, errno should be
189 * set by malloc as well. So we do.
197 void *__wrap_calloc(int n, int size)
199 void *ptr = __wrap_malloc(n * size);
201 if(ptr == NULL) return(NULL);
202 memset(ptr, 0, n * size);
206 extern void __real_free(void *);
208 extern unsigned long high_physmem;
210 void __wrap_free(void *ptr)
212 unsigned long addr = (unsigned long) ptr;
214 /* We need to know how the allocation happened, so it can be correctly
215 * freed. This is done by seeing what region of memory the pointer is
217 * physical memory - kmalloc/kfree
218 * kernel virtual memory - vmalloc/vfree
219 * anywhere else - malloc/free
220 * If kmalloc is not yet possible, then the kernel memory regions
221 * may not be set up yet, and the variables not initialized. So,
224 * CAN_KMALLOC is checked because it would be bad to free a buffer
225 * with kmalloc/vmalloc after they have been turned off during
229 if((addr >= uml_physmem) && (addr < high_physmem)){
233 else if((addr >= start_vm) && (addr < end_vm)){
237 else __real_free(ptr);
241 * Overrides for Emacs so that we follow Linus's tabbing style.
242 * Emacs will notice this stuff at the end of the file and automatically
243 * adjust the settings for this buffer only. This must remain at the end
245 * ---------------------------------------------------------------------------
247 * c-file-style: "linux"