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"
20 #include "time_user.h"
25 #include "choose-mode.h"
26 #include "uml-config.h"
28 #include "time_user.h"
31 /* Set in set_stklim, which is called from main and __wrap_malloc.
32 * __wrap_malloc only calls it if main hasn't started.
34 unsigned long stacksizelim;
39 #define PGD_BOUND (4 * 1024 * 1024)
40 #define STACKSIZE (8 * 1024 * 1024)
41 #define THREAD_NAME_LEN (256)
43 static void set_stklim(void)
47 if(getrlimit(RLIMIT_STACK, &lim) < 0){
51 if((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)){
52 lim.rlim_cur = STACKSIZE;
53 if(setrlimit(RLIMIT_STACK, &lim) < 0){
58 stacksizelim = (lim.rlim_cur + PGD_BOUND - 1) & ~(PGD_BOUND - 1);
61 static __init void do_uml_initcalls(void)
65 call = &__uml_initcall_start;
66 while (call < &__uml_initcall_end){;
72 static void last_ditch_exit(int sig)
74 CHOOSE_MODE(kmalloc_ok = 0, (void) 0);
75 signal(SIGINT, SIG_DFL);
76 signal(SIGTERM, SIG_DFL);
77 signal(SIGHUP, SIG_DFL);
82 extern int uml_exitcode;
84 int main(int argc, char **argv, char **envp)
90 /* Enable all signals except SIGIO - in some environments, we can
91 * enter with some signals blocked
95 sigaddset(&mask, SIGIO);
96 if(sigprocmask(SIG_SETMASK, &mask, NULL) < 0){
97 perror("sigprocmask");
101 #ifdef UML_CONFIG_MODE_TT
102 /* Allocate memory for thread command lines */
103 if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
105 char padding[THREAD_NAME_LEN] = {
106 [ 0 ... THREAD_NAME_LEN - 2] = ' ', '\0'
109 new_argv = malloc((argc + 2) * sizeof(char*));
111 perror("Allocating extended argv");
115 new_argv[0] = argv[0];
116 new_argv[1] = padding;
118 for(i = 2; i <= argc; i++)
119 new_argv[i] = argv[i - 1];
120 new_argv[argc + 1] = NULL;
122 execvp(new_argv[0], new_argv);
123 perror("execing with extended args");
128 linux_prog = argv[0];
132 new_argv = malloc((argc + 1) * sizeof(char *));
133 if(new_argv == NULL){
134 perror("Mallocing argv");
138 new_argv[i] = strdup(argv[i]);
139 if(new_argv[i] == NULL){
140 perror("Mallocing an arg");
144 new_argv[argc] = NULL;
146 set_handler(SIGINT, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
147 set_handler(SIGTERM, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
148 set_handler(SIGHUP, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
151 ret = linux_main(argc, argv);
159 /* Let any pending signals fire, then disable them. This
160 * ensures that they won't be delivered after the exec, when
161 * they are definitely not expected.
165 err = deactivate_all_fds();
167 printf("deactivate_all_fds failed, errno = %d\n",
170 execvp(new_argv[0], new_argv);
171 perror("Failed to exec kernel");
175 return(uml_exitcode);
178 #define CAN_KMALLOC() \
179 (kmalloc_ok && CHOOSE_MODE((os_getpid() != tracing_pid), 1))
181 extern void *__real_malloc(int);
183 void *__wrap_malloc(int size)
188 return(__real_malloc(size));
189 else if(size <= PAGE_SIZE) /* finding contiguos pages can be hard*/
190 ret = um_kmalloc(size);
191 else ret = um_vmalloc(size);
193 /* glibc people insist that if malloc fails, errno should be
194 * set by malloc as well. So we do.
202 void *__wrap_calloc(int n, int size)
204 void *ptr = __wrap_malloc(n * size);
206 if(ptr == NULL) return(NULL);
207 memset(ptr, 0, n * size);
211 extern void __real_free(void *);
213 extern unsigned long high_physmem;
215 void __wrap_free(void *ptr)
217 unsigned long addr = (unsigned long) ptr;
219 /* We need to know how the allocation happened, so it can be correctly
220 * freed. This is done by seeing what region of memory the pointer is
222 * physical memory - kmalloc/kfree
223 * kernel virtual memory - vmalloc/vfree
224 * anywhere else - malloc/free
225 * If kmalloc is not yet possible, then the kernel memory regions
226 * may not be set up yet, and the variables not initialized. So,
229 * CAN_KMALLOC is checked because it would be bad to free a buffer
230 * with kmalloc/vmalloc after they have been turned off during
234 if((addr >= uml_physmem) && (addr < high_physmem)){
238 else if((addr >= start_vm) && (addr < end_vm)){
242 else __real_free(ptr);
246 * Overrides for Emacs so that we follow Linus's tabbing style.
247 * Emacs will notice this stuff at the end of the file and automatically
248 * adjust the settings for this buffer only. This must remain at the end
250 * ---------------------------------------------------------------------------
252 * c-file-style: "linux"