blob: 685feaab65d239efa4931af9838e63b54f62d702 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "irq_user.h"
20#include "user.h"
21#include "init.h"
22#include "mode.h"
23#include "choose-mode.h"
24#include "uml-config.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "os.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070026#include "um_malloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
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 */
31unsigned long stacksizelim;
32
33/* Set in main */
34char *linux_prog;
35
36#define PGD_BOUND (4 * 1024 * 1024)
37#define STACKSIZE (8 * 1024 * 1024)
38#define THREAD_NAME_LEN (256)
39
40static 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
58static __init void do_uml_initcalls(void)
59{
60 initcall_t *call;
61
62 call = &__uml_initcall_start;
Jeff Dikef2183122006-06-04 02:51:47 -070063 while (call < &__uml_initcall_end){
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 (*call)();
65 call++;
66 }
67}
68
69static void last_ditch_exit(int sig)
70{
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 uml_cleanup();
72 exit(1);
73}
74
Jeff Dike4b84c692006-09-25 23:33:04 -070075static void install_fatal_handler(int sig)
76{
77 struct sigaction action;
78
79 /* All signals are enabled in this handler ... */
80 sigemptyset(&action.sa_mask);
81
82 /* ... including the signal being handled, plus we want the
83 * handler reset to the default behavior, so that if an exit
84 * handler is hanging for some reason, the UML will just die
85 * after this signal is sent a second time.
86 */
87 action.sa_flags = SA_RESETHAND | SA_NODEFER;
88 action.sa_restorer = NULL;
89 action.sa_handler = last_ditch_exit;
90 if(sigaction(sig, &action, NULL) < 0){
91 printf("failed to install handler for signal %d - errno = %d\n",
92 errno);
93 exit(1);
94 }
95}
96
Mattia Dongilicb98cdc2006-05-01 12:16:01 -070097#define UML_LIB_PATH ":/usr/lib/uml"
98
99static void setup_env_path(void)
100{
101 char *new_path = NULL;
102 char *old_path = NULL;
103 int path_len = 0;
104
105 old_path = getenv("PATH");
106 /* if no PATH variable is set or it has an empty value
107 * just use the default + /usr/lib/uml
108 */
109 if (!old_path || (path_len = strlen(old_path)) == 0) {
110 putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH);
111 return;
112 }
113
114 /* append /usr/lib/uml to the existing path */
115 path_len += strlen("PATH=" UML_LIB_PATH) + 1;
116 new_path = malloc(path_len);
117 if (!new_path) {
118 perror("coudn't malloc to set a new PATH");
119 return;
120 }
121 snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path);
122 putenv(new_path);
123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125extern int uml_exitcode;
126
127extern void scan_elf_aux( char **envp);
128
129int main(int argc, char **argv, char **envp)
130{
131 char **new_argv;
Jeff Dike92515da2005-05-28 15:51:56 -0700132 int ret, i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Paolo 'Blaisorblade' Giarrusso02215752005-09-03 15:57:23 -0700134#ifdef UML_CONFIG_CMDLINE_ON_HOST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 /* Allocate memory for thread command lines */
136 if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
137
138 char padding[THREAD_NAME_LEN] = {
139 [ 0 ... THREAD_NAME_LEN - 2] = ' ', '\0'
140 };
141
142 new_argv = malloc((argc + 2) * sizeof(char*));
143 if(!new_argv) {
144 perror("Allocating extended argv");
145 exit(1);
146 }
147
148 new_argv[0] = argv[0];
149 new_argv[1] = padding;
150
151 for(i = 2; i <= argc; i++)
152 new_argv[i] = argv[i - 1];
153 new_argv[argc + 1] = NULL;
154
155 execvp(new_argv[0], new_argv);
156 perror("execing with extended args");
157 exit(1);
158 }
159#endif
160
161 linux_prog = argv[0];
162
163 set_stklim();
164
Mattia Dongilicb98cdc2006-05-01 12:16:01 -0700165 setup_env_path();
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 new_argv = malloc((argc + 1) * sizeof(char *));
168 if(new_argv == NULL){
169 perror("Mallocing argv");
170 exit(1);
171 }
172 for(i=0;i<argc;i++){
173 new_argv[i] = strdup(argv[i]);
174 if(new_argv[i] == NULL){
175 perror("Mallocing an arg");
176 exit(1);
177 }
178 }
179 new_argv[argc] = NULL;
180
Jeff Dike4b84c692006-09-25 23:33:04 -0700181 /* Allow these signals to bring down a UML if all other
182 * methods of control fail.
183 */
184 install_fatal_handler(SIGINT);
185 install_fatal_handler(SIGTERM);
186 install_fatal_handler(SIGHUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 scan_elf_aux( envp);
189
190 do_uml_initcalls();
191 ret = linux_main(argc, argv);
192
193 /* Disable SIGPROF - I have no idea why libc doesn't do this or turn
194 * off the profiling time, but UML dies with a SIGPROF just before
195 * exiting when profiling is active.
196 */
197 change_sig(SIGPROF, 0);
198
Jeff Dike52c653b2005-11-07 00:58:50 -0800199 /* This signal stuff used to be in the reboot case. However,
200 * sometimes a SIGVTALRM can come in when we're halting (reproducably
201 * when writing out gcov information, presumably because that takes
202 * some time) and cause a segfault.
203 */
Jeff Dike92515da2005-05-28 15:51:56 -0700204
Jeff Dike52c653b2005-11-07 00:58:50 -0800205 /* stop timers and set SIG*ALRM to be ignored */
206 disable_timer();
Jeff Dike92515da2005-05-28 15:51:56 -0700207
Jeff Dike52c653b2005-11-07 00:58:50 -0800208 /* disable SIGIO for the fds and set SIGIO to be ignored */
209 err = deactivate_all_fds();
210 if(err)
211 printf("deactivate_all_fds failed, errno = %d\n", -err);
Jeff Dike92515da2005-05-28 15:51:56 -0700212
Jeff Dike52c653b2005-11-07 00:58:50 -0800213 /* Let any pending signals fire now. This ensures
214 * that they won't be delivered after the exec, when
215 * they are definitely not expected.
216 */
217 unblock_signals();
Jeff Dike92515da2005-05-28 15:51:56 -0700218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 /* Reboot */
220 if(ret){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 printf("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 execvp(new_argv[0], new_argv);
223 perror("Failed to exec kernel");
224 ret = 1;
225 }
226 printf("\n");
227 return(uml_exitcode);
228}
229
230#define CAN_KMALLOC() \
231 (kmalloc_ok && CHOOSE_MODE((os_getpid() != tracing_pid), 1))
232
233extern void *__real_malloc(int);
234
235void *__wrap_malloc(int size)
236{
237 void *ret;
238
239 if(!CAN_KMALLOC())
240 return(__real_malloc(size));
241 else if(size <= PAGE_SIZE) /* finding contiguos pages can be hard*/
242 ret = um_kmalloc(size);
243 else ret = um_vmalloc(size);
244
245 /* glibc people insist that if malloc fails, errno should be
246 * set by malloc as well. So we do.
247 */
248 if(ret == NULL)
249 errno = ENOMEM;
250
251 return(ret);
252}
253
254void *__wrap_calloc(int n, int size)
255{
256 void *ptr = __wrap_malloc(n * size);
257
258 if(ptr == NULL) return(NULL);
259 memset(ptr, 0, n * size);
260 return(ptr);
261}
262
263extern void __real_free(void *);
264
265extern unsigned long high_physmem;
266
267void __wrap_free(void *ptr)
268{
269 unsigned long addr = (unsigned long) ptr;
270
271 /* We need to know how the allocation happened, so it can be correctly
272 * freed. This is done by seeing what region of memory the pointer is
273 * in -
274 * physical memory - kmalloc/kfree
275 * kernel virtual memory - vmalloc/vfree
276 * anywhere else - malloc/free
277 * If kmalloc is not yet possible, then either high_physmem and/or
278 * end_vm are still 0 (as at startup), in which case we call free, or
279 * we have set them, but anyway addr has not been allocated from those
280 * areas. So, in both cases __real_free is called.
281 *
282 * CAN_KMALLOC is checked because it would be bad to free a buffer
283 * with kmalloc/vmalloc after they have been turned off during
284 * shutdown.
285 * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
286 * there is a possibility for memory leaks.
287 */
288
289 if((addr >= uml_physmem) && (addr < high_physmem)){
290 if(CAN_KMALLOC())
291 kfree(ptr);
292 }
293 else if((addr >= start_vm) && (addr < end_vm)){
294 if(CAN_KMALLOC())
295 vfree(ptr);
296 }
297 else __real_free(ptr);
298}