Merge branch 'upstream'
[pandora-kernel.git] / arch / um / os-Linux / main.c
1 /*
2  * Copyright (C) 2000, 2001 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include <unistd.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <signal.h>
11 #include <errno.h>
12 #include <sys/resource.h>
13 #include <sys/mman.h>
14 #include <sys/user.h>
15 #include <asm/page.h>
16 #include "user_util.h"
17 #include "kern_util.h"
18 #include "mem_user.h"
19 #include "time_user.h"
20 #include "irq_user.h"
21 #include "user.h"
22 #include "init.h"
23 #include "mode.h"
24 #include "choose-mode.h"
25 #include "uml-config.h"
26 #include "os.h"
27
28 /* Set in set_stklim, which is called from main and __wrap_malloc.
29  * __wrap_malloc only calls it if main hasn't started.
30  */
31 unsigned long stacksizelim;
32
33 /* Set in main */
34 char *linux_prog;
35
36 #define PGD_BOUND (4 * 1024 * 1024)
37 #define STACKSIZE (8 * 1024 * 1024)
38 #define THREAD_NAME_LEN (256)
39
40 static void set_stklim(void)
41 {
42         struct rlimit lim;
43
44         if(getrlimit(RLIMIT_STACK, &lim) < 0){
45                 perror("getrlimit");
46                 exit(1);
47         }
48         if((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)){
49                 lim.rlim_cur = STACKSIZE;
50                 if(setrlimit(RLIMIT_STACK, &lim) < 0){
51                         perror("setrlimit");
52                         exit(1);
53                 }
54         }
55         stacksizelim = (lim.rlim_cur + PGD_BOUND - 1) & ~(PGD_BOUND - 1);
56 }
57
58 static __init void do_uml_initcalls(void)
59 {
60         initcall_t *call;
61
62         call = &__uml_initcall_start;
63         while (call < &__uml_initcall_end){;
64                 (*call)();
65                 call++;
66         }
67 }
68
69 static void last_ditch_exit(int sig)
70 {
71         signal(SIGINT, SIG_DFL);
72         signal(SIGTERM, SIG_DFL);
73         signal(SIGHUP, SIG_DFL);
74         uml_cleanup();
75         exit(1);
76 }
77
78 extern int uml_exitcode;
79
80 extern void scan_elf_aux( char **envp);
81
82 int main(int argc, char **argv, char **envp)
83 {
84         char **new_argv;
85         sigset_t mask;
86         int ret, i, err;
87
88         /* Enable all signals except SIGIO - in some environments, we can
89          * enter with some signals blocked
90          */
91
92         sigemptyset(&mask);
93         sigaddset(&mask, SIGIO);
94         if(sigprocmask(SIG_SETMASK, &mask, NULL) < 0){
95                 perror("sigprocmask");
96                 exit(1);
97         }
98
99 #ifdef UML_CONFIG_CMDLINE_ON_HOST
100         /* Allocate memory for thread command lines */
101         if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
102
103                 char padding[THREAD_NAME_LEN] = {
104                         [ 0 ...  THREAD_NAME_LEN - 2] = ' ', '\0'
105                 };
106
107                 new_argv = malloc((argc + 2) * sizeof(char*));
108                 if(!new_argv) {
109                         perror("Allocating extended argv");
110                         exit(1);
111                 }
112
113                 new_argv[0] = argv[0];
114                 new_argv[1] = padding;
115
116                 for(i = 2; i <= argc; i++)
117                         new_argv[i] = argv[i - 1];
118                 new_argv[argc + 1] = NULL;
119
120                 execvp(new_argv[0], new_argv);
121                 perror("execing with extended args");
122                 exit(1);
123         }
124 #endif
125
126         linux_prog = argv[0];
127
128         set_stklim();
129
130         new_argv = malloc((argc + 1) * sizeof(char *));
131         if(new_argv == NULL){
132                 perror("Mallocing argv");
133                 exit(1);
134         }
135         for(i=0;i<argc;i++){
136                 new_argv[i] = strdup(argv[i]);
137                 if(new_argv[i] == NULL){
138                         perror("Mallocing an arg");
139                         exit(1);
140                 }
141         }
142         new_argv[argc] = NULL;
143
144         set_handler(SIGINT, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
145         set_handler(SIGTERM, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
146         set_handler(SIGHUP, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
147
148         scan_elf_aux( envp);
149
150         do_uml_initcalls();
151         ret = linux_main(argc, argv);
152
153         /* Disable SIGPROF - I have no idea why libc doesn't do this or turn
154          * off the profiling time, but UML dies with a SIGPROF just before
155          * exiting when profiling is active.
156          */
157         change_sig(SIGPROF, 0);
158
159         /* This signal stuff used to be in the reboot case.  However,
160          * sometimes a SIGVTALRM can come in when we're halting (reproducably
161          * when writing out gcov information, presumably because that takes
162          * some time) and cause a segfault.
163          */
164
165         /* stop timers and set SIG*ALRM to be ignored */
166         disable_timer();
167
168         /* disable SIGIO for the fds and set SIGIO to be ignored */
169         err = deactivate_all_fds();
170         if(err)
171                 printf("deactivate_all_fds failed, errno = %d\n", -err);
172
173         /* Let any pending signals fire now.  This ensures
174          * that they won't be delivered after the exec, when
175          * they are definitely not expected.
176          */
177         unblock_signals();
178
179         /* Reboot */
180         if(ret){
181                 printf("\n");
182                 execvp(new_argv[0], new_argv);
183                 perror("Failed to exec kernel");
184                 ret = 1;
185         }
186         printf("\n");
187         return(uml_exitcode);
188 }
189
190 #define CAN_KMALLOC() \
191         (kmalloc_ok && CHOOSE_MODE((os_getpid() != tracing_pid), 1))
192
193 extern void *__real_malloc(int);
194
195 void *__wrap_malloc(int size)
196 {
197         void *ret;
198
199         if(!CAN_KMALLOC())
200                 return(__real_malloc(size));
201         else if(size <= PAGE_SIZE) /* finding contiguos pages can be hard*/
202                 ret = um_kmalloc(size);
203         else ret = um_vmalloc(size);
204
205         /* glibc people insist that if malloc fails, errno should be
206          * set by malloc as well. So we do.
207          */
208         if(ret == NULL)
209                 errno = ENOMEM;
210
211         return(ret);
212 }
213
214 void *__wrap_calloc(int n, int size)
215 {
216         void *ptr = __wrap_malloc(n * size);
217
218         if(ptr == NULL) return(NULL);
219         memset(ptr, 0, n * size);
220         return(ptr);
221 }
222
223 extern void __real_free(void *);
224
225 extern unsigned long high_physmem;
226
227 void __wrap_free(void *ptr)
228 {
229         unsigned long addr = (unsigned long) ptr;
230
231         /* We need to know how the allocation happened, so it can be correctly
232          * freed.  This is done by seeing what region of memory the pointer is
233          * in -
234          *      physical memory - kmalloc/kfree
235          *      kernel virtual memory - vmalloc/vfree
236          *      anywhere else - malloc/free
237          * If kmalloc is not yet possible, then either high_physmem and/or
238          * end_vm are still 0 (as at startup), in which case we call free, or
239          * we have set them, but anyway addr has not been allocated from those
240          * areas. So, in both cases __real_free is called.
241          *
242          * CAN_KMALLOC is checked because it would be bad to free a buffer
243          * with kmalloc/vmalloc after they have been turned off during
244          * shutdown.
245          * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
246          * there is a possibility for memory leaks.
247          */
248
249         if((addr >= uml_physmem) && (addr < high_physmem)){
250                 if(CAN_KMALLOC())
251                         kfree(ptr);
252         }
253         else if((addr >= start_vm) && (addr < end_vm)){
254                 if(CAN_KMALLOC())
255                         vfree(ptr);
256         }
257         else __real_free(ptr);
258 }