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