Merge branch 'devel' of master.kernel.org:/home/rmk/linux-2.6-mmc
[sfrench/cifs-2.6.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 "irq_user.h"
20 #include "user.h"
21 #include "init.h"
22 #include "mode.h"
23 #include "choose-mode.h"
24 #include "uml-config.h"
25 #include "os.h"
26
27 /* Set in set_stklim, which is called from main and __wrap_malloc.
28  * __wrap_malloc only calls it if main hasn't started.
29  */
30 unsigned long stacksizelim;
31
32 /* Set in main */
33 char *linux_prog;
34
35 #define PGD_BOUND (4 * 1024 * 1024)
36 #define STACKSIZE (8 * 1024 * 1024)
37 #define THREAD_NAME_LEN (256)
38
39 static void set_stklim(void)
40 {
41         struct rlimit lim;
42
43         if(getrlimit(RLIMIT_STACK, &lim) < 0){
44                 perror("getrlimit");
45                 exit(1);
46         }
47         if((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)){
48                 lim.rlim_cur = STACKSIZE;
49                 if(setrlimit(RLIMIT_STACK, &lim) < 0){
50                         perror("setrlimit");
51                         exit(1);
52                 }
53         }
54         stacksizelim = (lim.rlim_cur + PGD_BOUND - 1) & ~(PGD_BOUND - 1);
55 }
56
57 static __init void do_uml_initcalls(void)
58 {
59         initcall_t *call;
60
61         call = &__uml_initcall_start;
62         while (call < &__uml_initcall_end){
63                 (*call)();
64                 call++;
65         }
66 }
67
68 static void last_ditch_exit(int sig)
69 {
70         uml_cleanup();
71         exit(1);
72 }
73
74 static void install_fatal_handler(int sig)
75 {
76         struct sigaction action;
77
78         /* All signals are enabled in this handler ... */
79         sigemptyset(&action.sa_mask);
80
81         /* ... including the signal being handled, plus we want the
82          * handler reset to the default behavior, so that if an exit
83          * handler is hanging for some reason, the UML will just die
84          * after this signal is sent a second time.
85          */
86         action.sa_flags = SA_RESETHAND | SA_NODEFER;
87         action.sa_restorer = NULL;
88         action.sa_handler = last_ditch_exit;
89         if(sigaction(sig, &action, NULL) < 0){
90                 printf("failed to install handler for signal %d - errno = %d\n",
91                        errno);
92                 exit(1);
93         }
94 }
95
96 #define UML_LIB_PATH    ":/usr/lib/uml"
97
98 static void setup_env_path(void)
99 {
100         char *new_path = NULL;
101         char *old_path = NULL;
102         int path_len = 0;
103
104         old_path = getenv("PATH");
105         /* if no PATH variable is set or it has an empty value
106          * just use the default + /usr/lib/uml
107          */
108         if (!old_path || (path_len = strlen(old_path)) == 0) {
109                 putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH);
110                 return;
111         }
112
113         /* append /usr/lib/uml to the existing path */
114         path_len += strlen("PATH=" UML_LIB_PATH) + 1;
115         new_path = malloc(path_len);
116         if (!new_path) {
117                 perror("coudn't malloc to set a new PATH");
118                 return;
119         }
120         snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path);
121         putenv(new_path);
122 }
123
124 extern int uml_exitcode;
125
126 extern void scan_elf_aux( char **envp);
127
128 int main(int argc, char **argv, char **envp)
129 {
130         char **new_argv;
131         int ret, i, err;
132
133 #ifdef UML_CONFIG_CMDLINE_ON_HOST
134         /* Allocate memory for thread command lines */
135         if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
136
137                 char padding[THREAD_NAME_LEN] = {
138                         [ 0 ...  THREAD_NAME_LEN - 2] = ' ', '\0'
139                 };
140
141                 new_argv = malloc((argc + 2) * sizeof(char*));
142                 if(!new_argv) {
143                         perror("Allocating extended argv");
144                         exit(1);
145                 }
146
147                 new_argv[0] = argv[0];
148                 new_argv[1] = padding;
149
150                 for(i = 2; i <= argc; i++)
151                         new_argv[i] = argv[i - 1];
152                 new_argv[argc + 1] = NULL;
153
154                 execvp(new_argv[0], new_argv);
155                 perror("execing with extended args");
156                 exit(1);
157         }
158 #endif
159
160         linux_prog = argv[0];
161
162         set_stklim();
163
164         setup_env_path();
165
166         new_argv = malloc((argc + 1) * sizeof(char *));
167         if(new_argv == NULL){
168                 perror("Mallocing argv");
169                 exit(1);
170         }
171         for(i=0;i<argc;i++){
172                 new_argv[i] = strdup(argv[i]);
173                 if(new_argv[i] == NULL){
174                         perror("Mallocing an arg");
175                         exit(1);
176                 }
177         }
178         new_argv[argc] = NULL;
179
180         /* Allow these signals to bring down a UML if all other
181          * methods of control fail.
182          */
183         install_fatal_handler(SIGINT);
184         install_fatal_handler(SIGTERM);
185         install_fatal_handler(SIGHUP);
186
187         scan_elf_aux( envp);
188
189         do_uml_initcalls();
190         ret = linux_main(argc, argv);
191
192         /* Disable SIGPROF - I have no idea why libc doesn't do this or turn
193          * off the profiling time, but UML dies with a SIGPROF just before
194          * exiting when profiling is active.
195          */
196         change_sig(SIGPROF, 0);
197
198         /* This signal stuff used to be in the reboot case.  However,
199          * sometimes a SIGVTALRM can come in when we're halting (reproducably
200          * when writing out gcov information, presumably because that takes
201          * some time) and cause a segfault.
202          */
203
204         /* stop timers and set SIG*ALRM to be ignored */
205         disable_timer();
206
207         /* disable SIGIO for the fds and set SIGIO to be ignored */
208         err = deactivate_all_fds();
209         if(err)
210                 printf("deactivate_all_fds failed, errno = %d\n", -err);
211
212         /* Let any pending signals fire now.  This ensures
213          * that they won't be delivered after the exec, when
214          * they are definitely not expected.
215          */
216         unblock_signals();
217
218         /* Reboot */
219         if(ret){
220                 printf("\n");
221                 execvp(new_argv[0], new_argv);
222                 perror("Failed to exec kernel");
223                 ret = 1;
224         }
225         printf("\n");
226         return(uml_exitcode);
227 }
228
229 #define CAN_KMALLOC() \
230         (kmalloc_ok && CHOOSE_MODE((os_getpid() != tracing_pid), 1))
231
232 extern void *__real_malloc(int);
233
234 void *__wrap_malloc(int size)
235 {
236         void *ret;
237
238         if(!CAN_KMALLOC())
239                 return(__real_malloc(size));
240         else if(size <= PAGE_SIZE) /* finding contiguos pages can be hard*/
241                 ret = um_kmalloc(size);
242         else ret = um_vmalloc(size);
243
244         /* glibc people insist that if malloc fails, errno should be
245          * set by malloc as well. So we do.
246          */
247         if(ret == NULL)
248                 errno = ENOMEM;
249
250         return(ret);
251 }
252
253 void *__wrap_calloc(int n, int size)
254 {
255         void *ptr = __wrap_malloc(n * size);
256
257         if(ptr == NULL) return(NULL);
258         memset(ptr, 0, n * size);
259         return(ptr);
260 }
261
262 extern void __real_free(void *);
263
264 extern unsigned long high_physmem;
265
266 void __wrap_free(void *ptr)
267 {
268         unsigned long addr = (unsigned long) ptr;
269
270         /* We need to know how the allocation happened, so it can be correctly
271          * freed.  This is done by seeing what region of memory the pointer is
272          * in -
273          *      physical memory - kmalloc/kfree
274          *      kernel virtual memory - vmalloc/vfree
275          *      anywhere else - malloc/free
276          * If kmalloc is not yet possible, then either high_physmem and/or
277          * end_vm are still 0 (as at startup), in which case we call free, or
278          * we have set them, but anyway addr has not been allocated from those
279          * areas. So, in both cases __real_free is called.
280          *
281          * CAN_KMALLOC is checked because it would be bad to free a buffer
282          * with kmalloc/vmalloc after they have been turned off during
283          * shutdown.
284          * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
285          * there is a possibility for memory leaks.
286          */
287
288         if((addr >= uml_physmem) && (addr < high_physmem)){
289                 if(CAN_KMALLOC())
290                         kfree(ptr);
291         }
292         else if((addr >= start_vm) && (addr < end_vm)){
293                 if(CAN_KMALLOC())
294                         vfree(ptr);
295         }
296         else __real_free(ptr);
297 }