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 extern void scan_elf_aux( char **envp);
86 int main(int argc, char **argv, char **envp)
92 /* Enable all signals except SIGIO - in some environments, we can
93 * enter with some signals blocked
97 sigaddset(&mask, SIGIO);
98 if(sigprocmask(SIG_SETMASK, &mask, NULL) < 0){
99 perror("sigprocmask");
103 #ifdef UML_CONFIG_MODE_TT
104 /* Allocate memory for thread command lines */
105 if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
107 char padding[THREAD_NAME_LEN] = {
108 [ 0 ... THREAD_NAME_LEN - 2] = ' ', '\0'
111 new_argv = malloc((argc + 2) * sizeof(char*));
113 perror("Allocating extended argv");
117 new_argv[0] = argv[0];
118 new_argv[1] = padding;
120 for(i = 2; i <= argc; i++)
121 new_argv[i] = argv[i - 1];
122 new_argv[argc + 1] = NULL;
124 execvp(new_argv[0], new_argv);
125 perror("execing with extended args");
130 linux_prog = argv[0];
134 new_argv = malloc((argc + 1) * sizeof(char *));
135 if(new_argv == NULL){
136 perror("Mallocing argv");
140 new_argv[i] = strdup(argv[i]);
141 if(new_argv[i] == NULL){
142 perror("Mallocing an arg");
146 new_argv[argc] = NULL;
148 set_handler(SIGINT, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
149 set_handler(SIGTERM, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
150 set_handler(SIGHUP, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
155 ret = linux_main(argc, argv);
157 /* Disable SIGPROF - I have no idea why libc doesn't do this or turn
158 * off the profiling time, but UML dies with a SIGPROF just before
159 * exiting when profiling is active.
161 change_sig(SIGPROF, 0);
169 /* stop timers and set SIG*ALRM to be ignored */
172 /* disable SIGIO for the fds and set SIGIO to be ignored */
173 err = deactivate_all_fds();
175 printf("deactivate_all_fds failed, errno = %d\n",
178 /* Let any pending signals fire now. This ensures
179 * that they won't be delivered after the exec, when
180 * they are definitely not expected.
184 execvp(new_argv[0], new_argv);
185 perror("Failed to exec kernel");
189 return(uml_exitcode);
192 #define CAN_KMALLOC() \
193 (kmalloc_ok && CHOOSE_MODE((os_getpid() != tracing_pid), 1))
195 extern void *__real_malloc(int);
197 void *__wrap_malloc(int size)
202 return(__real_malloc(size));
203 else if(size <= PAGE_SIZE) /* finding contiguos pages can be hard*/
204 ret = um_kmalloc(size);
205 else ret = um_vmalloc(size);
207 /* glibc people insist that if malloc fails, errno should be
208 * set by malloc as well. So we do.
216 void *__wrap_calloc(int n, int size)
218 void *ptr = __wrap_malloc(n * size);
220 if(ptr == NULL) return(NULL);
221 memset(ptr, 0, n * size);
225 extern void __real_free(void *);
227 extern unsigned long high_physmem;
229 void __wrap_free(void *ptr)
231 unsigned long addr = (unsigned long) ptr;
233 /* We need to know how the allocation happened, so it can be correctly
234 * freed. This is done by seeing what region of memory the pointer is
236 * physical memory - kmalloc/kfree
237 * kernel virtual memory - vmalloc/vfree
238 * anywhere else - malloc/free
239 * If kmalloc is not yet possible, then either high_physmem and/or
240 * end_vm are still 0 (as at startup), in which case we call free, or
241 * we have set them, but anyway addr has not been allocated from those
242 * areas. So, in both cases __real_free is called.
244 * CAN_KMALLOC is checked because it would be bad to free a buffer
245 * with kmalloc/vmalloc after they have been turned off during
247 * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
248 * there is a possibility for memory leaks.
251 if((addr >= uml_physmem) && (addr < high_physmem)){
255 else if((addr >= start_vm) && (addr < end_vm)){
259 else __real_free(ptr);
263 * Overrides for Emacs so that we follow Linus's tabbing style.
264 * Emacs will notice this stuff at the end of the file and automatically
265 * adjust the settings for this buffer only. This must remain at the end
267 * ---------------------------------------------------------------------------
269 * c-file-style: "linux"