fedora core 2.6.10-1.12-FC2
[linux-2.6.git] / arch / um / kernel / 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 "signal_user.h"
20 #include "time_user.h"
21 #include "irq_user.h"
22 #include "user.h"
23 #include "init.h"
24 #include "mode.h"
25 #include "choose-mode.h"
26 #include "uml-config.h"
27 #include "irq_user.h"
28 #include "time_user.h"
29 #include "os.h"
30
31 /* Set in set_stklim, which is called from main and __wrap_malloc.
32  * __wrap_malloc only calls it if main hasn't started.
33  */
34 unsigned long stacksizelim;
35
36 /* Set in main */
37 char *linux_prog;
38
39 #define PGD_BOUND (4 * 1024 * 1024)
40 #define STACKSIZE (8 * 1024 * 1024)
41 #define THREAD_NAME_LEN (256)
42
43 static void set_stklim(void)
44 {
45         struct rlimit lim;
46
47         if(getrlimit(RLIMIT_STACK, &lim) < 0){
48                 perror("getrlimit");
49                 exit(1);
50         }
51         if((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)){
52                 lim.rlim_cur = STACKSIZE;
53                 if(setrlimit(RLIMIT_STACK, &lim) < 0){
54                         perror("setrlimit");
55                         exit(1);
56                 }
57         }
58         stacksizelim = (lim.rlim_cur + PGD_BOUND - 1) & ~(PGD_BOUND - 1);
59 }
60
61 static __init void do_uml_initcalls(void)
62 {
63         initcall_t *call;
64
65         call = &__uml_initcall_start;
66         while (call < &__uml_initcall_end){;
67                 (*call)();
68                 call++;
69         }
70 }
71
72 static void last_ditch_exit(int sig)
73 {
74         CHOOSE_MODE(kmalloc_ok = 0, (void) 0);
75         signal(SIGINT, SIG_DFL);
76         signal(SIGTERM, SIG_DFL);
77         signal(SIGHUP, SIG_DFL);
78         uml_cleanup();
79         exit(1);
80 }
81
82 extern int uml_exitcode;
83
84 int main(int argc, char **argv, char **envp)
85 {
86         char **new_argv;
87         sigset_t mask;
88         int ret, i;
89
90         /* Enable all signals except SIGIO - in some environments, we can
91          * enter with some signals blocked
92          */
93
94         sigemptyset(&mask);
95         sigaddset(&mask, SIGIO);
96         if(sigprocmask(SIG_SETMASK, &mask, NULL) < 0){
97                 perror("sigprocmask");
98                 exit(1);
99         }
100
101 #ifdef UML_CONFIG_MODE_TT
102         /* Allocate memory for thread command lines */
103         if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
104
105                 char padding[THREAD_NAME_LEN] = {
106                         [ 0 ...  THREAD_NAME_LEN - 2] = ' ', '\0'
107                 };
108
109                 new_argv = malloc((argc + 2) * sizeof(char*));
110                 if(!new_argv) {
111                         perror("Allocating extended argv");
112                         exit(1);
113                 }
114
115                 new_argv[0] = argv[0];
116                 new_argv[1] = padding;
117
118                 for(i = 2; i <= argc; i++)
119                         new_argv[i] = argv[i - 1];
120                 new_argv[argc + 1] = NULL;
121
122                 execvp(new_argv[0], new_argv);
123                 perror("execing with extended args");
124                 exit(1);
125         }
126 #endif
127
128         linux_prog = argv[0];
129
130         set_stklim();
131
132         new_argv = malloc((argc + 1) * sizeof(char *));
133         if(new_argv == NULL){
134                 perror("Mallocing argv");
135                 exit(1);
136         }
137         for(i=0;i<argc;i++){
138                 new_argv[i] = strdup(argv[i]);
139                 if(new_argv[i] == NULL){
140                         perror("Mallocing an arg");
141                         exit(1);
142                 }
143         }
144         new_argv[argc] = NULL;
145
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);
149
150         do_uml_initcalls();
151         ret = linux_main(argc, argv);
152
153         /* Reboot */
154         if(ret){
155                 int err;
156
157                 printf("\n");
158
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.
162                  */
163                 unblock_signals();
164                 disable_timer();
165                 err = deactivate_all_fds();
166                 if(err)
167                         printf("deactivate_all_fds failed, errno = %d\n",
168                                -err);
169
170                 execvp(new_argv[0], new_argv);
171                 perror("Failed to exec kernel");
172                 ret = 1;
173         }
174         printf("\n");
175         return(uml_exitcode);
176 }
177
178 #define CAN_KMALLOC() \
179         (kmalloc_ok && CHOOSE_MODE((os_getpid() != tracing_pid), 1))
180
181 extern void *__real_malloc(int);
182
183 void *__wrap_malloc(int size)
184 {
185         void *ret;
186
187         if(!CAN_KMALLOC())
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);
192
193         /* glibc people insist that if malloc fails, errno should be
194          * set by malloc as well. So we do.
195          */
196         if(ret == NULL)
197                 errno = ENOMEM;
198
199         return(ret);
200 }
201
202 void *__wrap_calloc(int n, int size)
203 {
204         void *ptr = __wrap_malloc(n * size);
205
206         if(ptr == NULL) return(NULL);
207         memset(ptr, 0, n * size);
208         return(ptr);
209 }
210
211 extern void __real_free(void *);
212
213 extern unsigned long high_physmem;
214
215 void __wrap_free(void *ptr)
216 {
217         unsigned long addr = (unsigned long) ptr;
218
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
221          * in -
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,
227          * free is called.
228          *
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
231          * shutdown.
232          */
233
234         if((addr >= uml_physmem) && (addr < high_physmem)){
235                 if(CAN_KMALLOC())
236                         kfree(ptr);
237         }
238         else if((addr >= start_vm) && (addr < end_vm)){
239                 if(CAN_KMALLOC())
240                         vfree(ptr);
241         }
242         else __real_free(ptr);
243 }
244
245 /*
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
249  * of the file.
250  * ---------------------------------------------------------------------------
251  * Local variables:
252  * c-file-style: "linux"
253  * End:
254  */