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