blob: e59f581526782adca0763540a98bc80505a7a79a [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "os.h"
28
29/* Set in set_stklim, which is called from main and __wrap_malloc.
30 * __wrap_malloc only calls it if main hasn't started.
31 */
32unsigned long stacksizelim;
33
34/* Set in main */
35char *linux_prog;
36
37#define PGD_BOUND (4 * 1024 * 1024)
38#define STACKSIZE (8 * 1024 * 1024)
39#define THREAD_NAME_LEN (256)
40
41static void set_stklim(void)
42{
43 struct rlimit lim;
44
45 if(getrlimit(RLIMIT_STACK, &lim) < 0){
46 perror("getrlimit");
47 exit(1);
48 }
49 if((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)){
50 lim.rlim_cur = STACKSIZE;
51 if(setrlimit(RLIMIT_STACK, &lim) < 0){
52 perror("setrlimit");
53 exit(1);
54 }
55 }
56 stacksizelim = (lim.rlim_cur + PGD_BOUND - 1) & ~(PGD_BOUND - 1);
57}
58
59static __init void do_uml_initcalls(void)
60{
61 initcall_t *call;
62
63 call = &__uml_initcall_start;
64 while (call < &__uml_initcall_end){;
65 (*call)();
66 call++;
67 }
68}
69
70static void last_ditch_exit(int sig)
71{
Jeff Dike6770cb62005-05-28 15:51:54 -070072 kmalloc_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 signal(SIGINT, SIG_DFL);
74 signal(SIGTERM, SIG_DFL);
75 signal(SIGHUP, SIG_DFL);
76 uml_cleanup();
77 exit(1);
78}
79
80extern int uml_exitcode;
81
82extern void scan_elf_aux( char **envp);
83
84int main(int argc, char **argv, char **envp)
85{
86 char **new_argv;
87 sigset_t mask;
Jeff Dike92515da2005-05-28 15:51:56 -070088 int ret, i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
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 scan_elf_aux( envp);
151
152 do_uml_initcalls();
153 ret = linux_main(argc, argv);
154
155 /* Disable SIGPROF - I have no idea why libc doesn't do this or turn
156 * off the profiling time, but UML dies with a SIGPROF just before
157 * exiting when profiling is active.
158 */
159 change_sig(SIGPROF, 0);
160
Jeff Dike92515da2005-05-28 15:51:56 -0700161 /* This signal stuff used to be in the reboot case. However,
162 * sometimes a SIGVTALRM can come in when we're halting (reproducably
163 * when writing out gcov information, presumably because that takes
164 * some time) and cause a segfault.
165 */
166
167 /* stop timers and set SIG*ALRM to be ignored */
168 disable_timer();
169
170 /* disable SIGIO for the fds and set SIGIO to be ignored */
171 err = deactivate_all_fds();
172 if(err)
173 printf("deactivate_all_fds failed, errno = %d\n", -err);
174
175 /* Let any pending signals fire now. This ensures
176 * that they won't be delivered after the exec, when
177 * they are definitely not expected.
178 */
179 unblock_signals();
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /* Reboot */
182 if(ret){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 printf("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 execvp(new_argv[0], new_argv);
185 perror("Failed to exec kernel");
186 ret = 1;
187 }
188 printf("\n");
189 return(uml_exitcode);
190}
191
192#define CAN_KMALLOC() \
193 (kmalloc_ok && CHOOSE_MODE((os_getpid() != tracing_pid), 1))
194
195extern void *__real_malloc(int);
196
197void *__wrap_malloc(int size)
198{
199 void *ret;
200
201 if(!CAN_KMALLOC())
202 return(__real_malloc(size));
203 else if(size <= PAGE_SIZE) /* finding contiguos pages can be hard*/
204 ret = um_kmalloc(size);
205 else ret = um_vmalloc(size);
206
207 /* glibc people insist that if malloc fails, errno should be
208 * set by malloc as well. So we do.
209 */
210 if(ret == NULL)
211 errno = ENOMEM;
212
213 return(ret);
214}
215
216void *__wrap_calloc(int n, int size)
217{
218 void *ptr = __wrap_malloc(n * size);
219
220 if(ptr == NULL) return(NULL);
221 memset(ptr, 0, n * size);
222 return(ptr);
223}
224
225extern void __real_free(void *);
226
227extern unsigned long high_physmem;
228
229void __wrap_free(void *ptr)
230{
231 unsigned long addr = (unsigned long) ptr;
232
233 /* We need to know how the allocation happened, so it can be correctly
234 * freed. This is done by seeing what region of memory the pointer is
235 * in -
236 * physical memory - kmalloc/kfree
237 * kernel virtual memory - vmalloc/vfree
238 * anywhere else - malloc/free
239 * If kmalloc is not yet possible, then either high_physmem and/or
240 * end_vm are still 0 (as at startup), in which case we call free, or
241 * we have set them, but anyway addr has not been allocated from those
242 * areas. So, in both cases __real_free is called.
243 *
244 * CAN_KMALLOC is checked because it would be bad to free a buffer
245 * with kmalloc/vmalloc after they have been turned off during
246 * shutdown.
247 * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
248 * there is a possibility for memory leaks.
249 */
250
251 if((addr >= uml_physmem) && (addr < high_physmem)){
252 if(CAN_KMALLOC())
253 kfree(ptr);
254 }
255 else if((addr >= start_vm) && (addr < end_vm)){
256 if(CAN_KMALLOC())
257 vfree(ptr);
258 }
259 else __real_free(ptr);
260}
261
262/*
263 * Overrides for Emacs so that we follow Linus's tabbing style.
264 * Emacs will notice this stuff at the end of the file and automatically
265 * adjust the settings for this buffer only. This must remain at the end
266 * of the file.
267 * ---------------------------------------------------------------------------
268 * Local variables:
269 * c-file-style: "linux"
270 * End:
271 */