blob: e42e6364ca130a957c67f066f1779d1d040571a2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
34unsigned long stacksizelim;
35
36/* Set in main */
37char *linux_prog;
38
39#define PGD_BOUND (4 * 1024 * 1024)
40#define STACKSIZE (8 * 1024 * 1024)
41#define THREAD_NAME_LEN (256)
42
43static 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
61static __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
72static void last_ditch_exit(int sig)
73{
Jeff Dike6770cb62005-05-28 15:51:54 -070074 kmalloc_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 signal(SIGINT, SIG_DFL);
76 signal(SIGTERM, SIG_DFL);
77 signal(SIGHUP, SIG_DFL);
78 uml_cleanup();
79 exit(1);
80}
81
82extern int uml_exitcode;
83
84extern void scan_elf_aux( char **envp);
85
86int main(int argc, char **argv, char **envp)
87{
88 char **new_argv;
89 sigset_t mask;
Jeff Dike92515da2005-05-28 15:51:56 -070090 int ret, i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 /* Enable all signals except SIGIO - in some environments, we can
93 * enter with some signals blocked
94 */
95
96 sigemptyset(&mask);
97 sigaddset(&mask, SIGIO);
98 if(sigprocmask(SIG_SETMASK, &mask, NULL) < 0){
99 perror("sigprocmask");
100 exit(1);
101 }
102
103#ifdef UML_CONFIG_MODE_TT
104 /* Allocate memory for thread command lines */
105 if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
106
107 char padding[THREAD_NAME_LEN] = {
108 [ 0 ... THREAD_NAME_LEN - 2] = ' ', '\0'
109 };
110
111 new_argv = malloc((argc + 2) * sizeof(char*));
112 if(!new_argv) {
113 perror("Allocating extended argv");
114 exit(1);
115 }
116
117 new_argv[0] = argv[0];
118 new_argv[1] = padding;
119
120 for(i = 2; i <= argc; i++)
121 new_argv[i] = argv[i - 1];
122 new_argv[argc + 1] = NULL;
123
124 execvp(new_argv[0], new_argv);
125 perror("execing with extended args");
126 exit(1);
127 }
128#endif
129
130 linux_prog = argv[0];
131
132 set_stklim();
133
134 new_argv = malloc((argc + 1) * sizeof(char *));
135 if(new_argv == NULL){
136 perror("Mallocing argv");
137 exit(1);
138 }
139 for(i=0;i<argc;i++){
140 new_argv[i] = strdup(argv[i]);
141 if(new_argv[i] == NULL){
142 perror("Mallocing an arg");
143 exit(1);
144 }
145 }
146 new_argv[argc] = NULL;
147
148 set_handler(SIGINT, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
149 set_handler(SIGTERM, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
150 set_handler(SIGHUP, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
151
152 scan_elf_aux( envp);
153
154 do_uml_initcalls();
155 ret = linux_main(argc, argv);
156
157 /* Disable SIGPROF - I have no idea why libc doesn't do this or turn
158 * off the profiling time, but UML dies with a SIGPROF just before
159 * exiting when profiling is active.
160 */
161 change_sig(SIGPROF, 0);
162
Jeff Dike92515da2005-05-28 15:51:56 -0700163 /* This signal stuff used to be in the reboot case. However,
164 * sometimes a SIGVTALRM can come in when we're halting (reproducably
165 * when writing out gcov information, presumably because that takes
166 * some time) and cause a segfault.
167 */
168
169 /* stop timers and set SIG*ALRM to be ignored */
170 disable_timer();
171
172 /* disable SIGIO for the fds and set SIGIO to be ignored */
173 err = deactivate_all_fds();
174 if(err)
175 printf("deactivate_all_fds failed, errno = %d\n", -err);
176
177 /* Let any pending signals fire now. This ensures
178 * that they won't be delivered after the exec, when
179 * they are definitely not expected.
180 */
181 unblock_signals();
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 /* Reboot */
184 if(ret){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 printf("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 execvp(new_argv[0], new_argv);
187 perror("Failed to exec kernel");
188 ret = 1;
189 }
190 printf("\n");
191 return(uml_exitcode);
192}
193
194#define CAN_KMALLOC() \
195 (kmalloc_ok && CHOOSE_MODE((os_getpid() != tracing_pid), 1))
196
197extern void *__real_malloc(int);
198
199void *__wrap_malloc(int size)
200{
201 void *ret;
202
203 if(!CAN_KMALLOC())
204 return(__real_malloc(size));
205 else if(size <= PAGE_SIZE) /* finding contiguos pages can be hard*/
206 ret = um_kmalloc(size);
207 else ret = um_vmalloc(size);
208
209 /* glibc people insist that if malloc fails, errno should be
210 * set by malloc as well. So we do.
211 */
212 if(ret == NULL)
213 errno = ENOMEM;
214
215 return(ret);
216}
217
218void *__wrap_calloc(int n, int size)
219{
220 void *ptr = __wrap_malloc(n * size);
221
222 if(ptr == NULL) return(NULL);
223 memset(ptr, 0, n * size);
224 return(ptr);
225}
226
227extern void __real_free(void *);
228
229extern unsigned long high_physmem;
230
231void __wrap_free(void *ptr)
232{
233 unsigned long addr = (unsigned long) ptr;
234
235 /* We need to know how the allocation happened, so it can be correctly
236 * freed. This is done by seeing what region of memory the pointer is
237 * in -
238 * physical memory - kmalloc/kfree
239 * kernel virtual memory - vmalloc/vfree
240 * anywhere else - malloc/free
241 * If kmalloc is not yet possible, then either high_physmem and/or
242 * end_vm are still 0 (as at startup), in which case we call free, or
243 * we have set them, but anyway addr has not been allocated from those
244 * areas. So, in both cases __real_free is called.
245 *
246 * CAN_KMALLOC is checked because it would be bad to free a buffer
247 * with kmalloc/vmalloc after they have been turned off during
248 * shutdown.
249 * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
250 * there is a possibility for memory leaks.
251 */
252
253 if((addr >= uml_physmem) && (addr < high_physmem)){
254 if(CAN_KMALLOC())
255 kfree(ptr);
256 }
257 else if((addr >= start_vm) && (addr < end_vm)){
258 if(CAN_KMALLOC())
259 vfree(ptr);
260 }
261 else __real_free(ptr);
262}
263
264/*
265 * Overrides for Emacs so that we follow Linus's tabbing style.
266 * Emacs will notice this stuff at the end of the file and automatically
267 * adjust the settings for this buffer only. This must remain at the end
268 * of the file.
269 * ---------------------------------------------------------------------------
270 * Local variables:
271 * c-file-style: "linux"
272 * End:
273 */