This commit was manufactured by cvs2svn to create branch 'vserver'.
[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 "user.h"
21 #include "init.h"
22 #include "mode.h"
23 #include "choose-mode.h"
24 #include "uml-config.h"
25
26 /* Set in set_stklim, which is called from main and __wrap_malloc.
27  * __wrap_malloc only calls it if main hasn't started.
28  */
29 unsigned long stacksizelim;
30
31 /* Set in main */
32 char *linux_prog;
33
34 #define PGD_BOUND (4 * 1024 * 1024)
35 #define STACKSIZE (8 * 1024 * 1024)
36 #define THREAD_NAME_LEN (256)
37
38 static void set_stklim(void)
39 {
40         struct rlimit lim;
41
42         if(getrlimit(RLIMIT_STACK, &lim) < 0){
43                 perror("getrlimit");
44                 exit(1);
45         }
46         if((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)){
47                 lim.rlim_cur = STACKSIZE;
48                 if(setrlimit(RLIMIT_STACK, &lim) < 0){
49                         perror("setrlimit");
50                         exit(1);
51                 }
52         }
53         stacksizelim = (lim.rlim_cur + PGD_BOUND - 1) & ~(PGD_BOUND - 1);
54 }
55
56 static __init void do_uml_initcalls(void)
57 {
58         initcall_t *call;
59
60         call = &__uml_initcall_start;
61         while (call < &__uml_initcall_end){;
62                 (*call)();
63                 call++;
64         }
65 }
66
67 static void last_ditch_exit(int sig)
68 {
69         CHOOSE_MODE(kmalloc_ok = 0, (void) 0);
70         signal(SIGINT, SIG_DFL);
71         signal(SIGTERM, SIG_DFL);
72         signal(SIGHUP, SIG_DFL);
73         uml_cleanup();
74         exit(1);
75 }
76
77 extern int uml_exitcode;
78
79 int main(int argc, char **argv, char **envp)
80 {
81         char **new_argv;
82         sigset_t mask;
83         int ret, i;
84
85         /* Enable all signals except SIGIO - in some environments, we can
86          * enter with some signals blocked
87          */
88
89         sigemptyset(&mask);
90         sigaddset(&mask, SIGIO);
91         if(sigprocmask(SIG_SETMASK, &mask, NULL) < 0){
92                 perror("sigprocmask");
93                 exit(1);
94         }
95
96 #ifdef UML_CONFIG_MODE_TT
97         /* Allocate memory for thread command lines */
98         if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
99
100                 char padding[THREAD_NAME_LEN] = {
101                         [ 0 ...  THREAD_NAME_LEN - 2] = ' ', '\0'
102                 };
103
104                 new_argv = malloc((argc + 2) * sizeof(char*));
105                 if(!new_argv) {
106                         perror("Allocating extended argv");
107                         exit(1);
108                 }
109
110                 new_argv[0] = argv[0];
111                 new_argv[1] = padding;
112
113                 for(i = 2; i <= argc; i++)
114                         new_argv[i] = argv[i - 1];
115                 new_argv[argc + 1] = NULL;
116
117                 execvp(new_argv[0], new_argv);
118                 perror("execing with extended args");
119                 exit(1);
120         }
121 #endif
122
123         linux_prog = argv[0];
124
125         set_stklim();
126
127         new_argv = malloc((argc + 1) * sizeof(char *));
128         if(new_argv == NULL){
129                 perror("Mallocing argv");
130                 exit(1);
131         }
132         for(i=0;i<argc;i++){
133                 new_argv[i] = strdup(argv[i]);
134                 if(new_argv[i] == NULL){
135                         perror("Mallocing an arg");
136                         exit(1);
137                 }
138         }
139         new_argv[argc] = NULL;
140
141         set_handler(SIGINT, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
142         set_handler(SIGTERM, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
143         set_handler(SIGHUP, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
144
145         do_uml_initcalls();
146         ret = linux_main(argc, argv);
147
148         /* Reboot */
149         if(ret){
150                 int err;
151
152                 printf("\n");
153
154                 /* Let any pending signals fire, then disable them.  This
155                  * ensures that they won't be delivered after the exec, when
156                  * they are definitely not expected.
157                  */
158                 unblock_signals();
159                 disable_timer();
160                 err = deactivate_all_fds();
161                 if(err)
162                         printf("deactivate_all_fds failed, errno = %d\n",
163                                -err);
164
165                 execvp(new_argv[0], new_argv);
166                 perror("Failed to exec kernel");
167                 ret = 1;
168         }
169         printf("\n");
170         return(uml_exitcode);
171 }
172
173 #define CAN_KMALLOC() \
174         (kmalloc_ok && CHOOSE_MODE((getpid() != tracing_pid), 1))
175
176 extern void *__real_malloc(int);
177
178 void *__wrap_malloc(int size)
179 {
180         void *ret;
181
182         if(!CAN_KMALLOC())
183                 return(__real_malloc(size));
184         else if(size <= PAGE_SIZE) /* finding contiguos pages can be hard*/
185                 ret = um_kmalloc(size);
186         else ret = um_vmalloc(size);
187
188         /* glibc people insist that if malloc fails, errno should be
189          * set by malloc as well. So we do.
190          */
191         if(ret == NULL)
192                 errno = ENOMEM;
193
194         return(ret);
195 }
196
197 void *__wrap_calloc(int n, int size)
198 {
199         void *ptr = __wrap_malloc(n * size);
200
201         if(ptr == NULL) return(NULL);
202         memset(ptr, 0, n * size);
203         return(ptr);
204 }
205
206 extern void __real_free(void *);
207
208 extern unsigned long high_physmem;
209
210 void __wrap_free(void *ptr)
211 {
212         unsigned long addr = (unsigned long) ptr;
213
214         /* We need to know how the allocation happened, so it can be correctly
215          * freed.  This is done by seeing what region of memory the pointer is
216          * in -
217          *      physical memory - kmalloc/kfree
218          *      kernel virtual memory - vmalloc/vfree
219          *      anywhere else - malloc/free
220          * If kmalloc is not yet possible, then the kernel memory regions
221          * may not be set up yet, and the variables not initialized.  So,
222          * free is called.
223          *
224          * CAN_KMALLOC is checked because it would be bad to free a buffer
225          * with kmalloc/vmalloc after they have been turned off during
226          * shutdown.
227          */
228
229         if((addr >= uml_physmem) && (addr < high_physmem)){
230                 if(CAN_KMALLOC())
231                         kfree(ptr);
232         }
233         else if((addr >= start_vm) && (addr < end_vm)){
234                 if(CAN_KMALLOC())
235                         vfree(ptr);
236         }
237         else __real_free(ptr);
238 }
239
240 /*
241  * Overrides for Emacs so that we follow Linus's tabbing style.
242  * Emacs will notice this stuff at the end of the file and automatically
243  * adjust the settings for this buffer only.  This must remain at the end
244  * of the file.
245  * ---------------------------------------------------------------------------
246  * Local variables:
247  * c-file-style: "linux"
248  * End:
249  */