blob: e85f4995a011a16a226f0963941b2bbf0355dea0 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "kern_util.h"
Jeff Dike4ff83ce2007-05-06 14:51:08 -070017#include "as-layout.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#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"
Jeff Dikec539ab72007-06-16 10:16:09 -070027#include "kern_constants.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Jeff Dike11100b12007-05-06 14:50:57 -070029/* Set in main, unchanged thereafter */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030char *linux_prog;
31
32#define PGD_BOUND (4 * 1024 * 1024)
33#define STACKSIZE (8 * 1024 * 1024)
34#define THREAD_NAME_LEN (256)
35
36static void set_stklim(void)
37{
38 struct rlimit lim;
39
40 if(getrlimit(RLIMIT_STACK, &lim) < 0){
41 perror("getrlimit");
42 exit(1);
43 }
44 if((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)){
45 lim.rlim_cur = STACKSIZE;
46 if(setrlimit(RLIMIT_STACK, &lim) < 0){
47 perror("setrlimit");
48 exit(1);
49 }
50 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
53static __init void do_uml_initcalls(void)
54{
55 initcall_t *call;
56
57 call = &__uml_initcall_start;
Jeff Dikef2183122006-06-04 02:51:47 -070058 while (call < &__uml_initcall_end){
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 (*call)();
60 call++;
61 }
62}
63
64static void last_ditch_exit(int sig)
65{
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 uml_cleanup();
67 exit(1);
68}
69
Jeff Dike4b84c692006-09-25 23:33:04 -070070static void install_fatal_handler(int sig)
71{
72 struct sigaction action;
73
74 /* All signals are enabled in this handler ... */
75 sigemptyset(&action.sa_mask);
76
77 /* ... including the signal being handled, plus we want the
78 * handler reset to the default behavior, so that if an exit
79 * handler is hanging for some reason, the UML will just die
80 * after this signal is sent a second time.
81 */
82 action.sa_flags = SA_RESETHAND | SA_NODEFER;
83 action.sa_restorer = NULL;
84 action.sa_handler = last_ditch_exit;
85 if(sigaction(sig, &action, NULL) < 0){
86 printf("failed to install handler for signal %d - errno = %d\n",
87 errno);
88 exit(1);
89 }
90}
91
Mattia Dongilicb98cdc2006-05-01 12:16:01 -070092#define UML_LIB_PATH ":/usr/lib/uml"
93
94static void setup_env_path(void)
95{
96 char *new_path = NULL;
97 char *old_path = NULL;
98 int path_len = 0;
99
100 old_path = getenv("PATH");
101 /* if no PATH variable is set or it has an empty value
102 * just use the default + /usr/lib/uml
103 */
104 if (!old_path || (path_len = strlen(old_path)) == 0) {
105 putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH);
106 return;
107 }
108
109 /* append /usr/lib/uml to the existing path */
110 path_len += strlen("PATH=" UML_LIB_PATH) + 1;
111 new_path = malloc(path_len);
112 if (!new_path) {
113 perror("coudn't malloc to set a new PATH");
114 return;
115 }
116 snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path);
117 putenv(new_path);
118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120extern int uml_exitcode;
121
122extern void scan_elf_aux( char **envp);
123
Jeff Dike36e45462007-05-06 14:51:11 -0700124int __init main(int argc, char **argv, char **envp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 char **new_argv;
Jeff Dike92515da2005-05-28 15:51:56 -0700127 int ret, i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Paolo 'Blaisorblade' Giarrusso02215752005-09-03 15:57:23 -0700129#ifdef UML_CONFIG_CMDLINE_ON_HOST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 /* Allocate memory for thread command lines */
131 if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
132
133 char padding[THREAD_NAME_LEN] = {
134 [ 0 ... THREAD_NAME_LEN - 2] = ' ', '\0'
135 };
136
137 new_argv = malloc((argc + 2) * sizeof(char*));
138 if(!new_argv) {
139 perror("Allocating extended argv");
140 exit(1);
141 }
142
143 new_argv[0] = argv[0];
144 new_argv[1] = padding;
145
146 for(i = 2; i <= argc; i++)
147 new_argv[i] = argv[i - 1];
148 new_argv[argc + 1] = NULL;
149
150 execvp(new_argv[0], new_argv);
151 perror("execing with extended args");
152 exit(1);
153 }
154#endif
155
156 linux_prog = argv[0];
157
158 set_stklim();
159
Mattia Dongilicb98cdc2006-05-01 12:16:01 -0700160 setup_env_path();
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 new_argv = malloc((argc + 1) * sizeof(char *));
163 if(new_argv == NULL){
164 perror("Mallocing argv");
165 exit(1);
166 }
167 for(i=0;i<argc;i++){
168 new_argv[i] = strdup(argv[i]);
169 if(new_argv[i] == NULL){
170 perror("Mallocing an arg");
171 exit(1);
172 }
173 }
174 new_argv[argc] = NULL;
175
Jeff Dike4b84c692006-09-25 23:33:04 -0700176 /* Allow these signals to bring down a UML if all other
177 * methods of control fail.
178 */
179 install_fatal_handler(SIGINT);
180 install_fatal_handler(SIGTERM);
181 install_fatal_handler(SIGHUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 scan_elf_aux( envp);
184
185 do_uml_initcalls();
186 ret = linux_main(argc, argv);
187
188 /* Disable SIGPROF - I have no idea why libc doesn't do this or turn
189 * off the profiling time, but UML dies with a SIGPROF just before
190 * exiting when profiling is active.
191 */
192 change_sig(SIGPROF, 0);
193
Jeff Dike52c653b2005-11-07 00:58:50 -0800194 /* This signal stuff used to be in the reboot case. However,
195 * sometimes a SIGVTALRM can come in when we're halting (reproducably
196 * when writing out gcov information, presumably because that takes
197 * some time) and cause a segfault.
198 */
Jeff Dike92515da2005-05-28 15:51:56 -0700199
Jeff Dike52c653b2005-11-07 00:58:50 -0800200 /* stop timers and set SIG*ALRM to be ignored */
201 disable_timer();
Jeff Dike92515da2005-05-28 15:51:56 -0700202
Jeff Dike52c653b2005-11-07 00:58:50 -0800203 /* disable SIGIO for the fds and set SIGIO to be ignored */
204 err = deactivate_all_fds();
205 if(err)
206 printf("deactivate_all_fds failed, errno = %d\n", -err);
Jeff Dike92515da2005-05-28 15:51:56 -0700207
Jeff Dike52c653b2005-11-07 00:58:50 -0800208 /* Let any pending signals fire now. This ensures
209 * that they won't be delivered after the exec, when
210 * they are definitely not expected.
211 */
212 unblock_signals();
Jeff Dike92515da2005-05-28 15:51:56 -0700213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 /* Reboot */
215 if(ret){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 printf("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 execvp(new_argv[0], new_argv);
218 perror("Failed to exec kernel");
219 ret = 1;
220 }
221 printf("\n");
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700222 return uml_exitcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225#define CAN_KMALLOC() \
226 (kmalloc_ok && CHOOSE_MODE((os_getpid() != tracing_pid), 1))
227
228extern void *__real_malloc(int);
229
230void *__wrap_malloc(int size)
231{
232 void *ret;
233
234 if(!CAN_KMALLOC())
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700235 return __real_malloc(size);
Jeff Dikec539ab72007-06-16 10:16:09 -0700236 else if(size <= UM_KERN_PAGE_SIZE)
237 /* finding contiguous pages can be hard*/
Jeff Dikee4c4bf92007-07-15 23:38:56 -0700238 ret = kmalloc(size, UM_GFP_KERNEL);
239 else ret = vmalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 /* glibc people insist that if malloc fails, errno should be
242 * set by malloc as well. So we do.
243 */
244 if(ret == NULL)
245 errno = ENOMEM;
246
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700247 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
250void *__wrap_calloc(int n, int size)
251{
252 void *ptr = __wrap_malloc(n * size);
253
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700254 if(ptr == NULL)
255 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 memset(ptr, 0, n * size);
Jeff Dikea5ed1ff2007-05-06 14:50:58 -0700257 return ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260extern void __real_free(void *);
261
262extern unsigned long high_physmem;
263
264void __wrap_free(void *ptr)
265{
266 unsigned long addr = (unsigned long) ptr;
267
268 /* We need to know how the allocation happened, so it can be correctly
269 * freed. This is done by seeing what region of memory the pointer is
270 * in -
271 * physical memory - kmalloc/kfree
272 * kernel virtual memory - vmalloc/vfree
273 * anywhere else - malloc/free
274 * If kmalloc is not yet possible, then either high_physmem and/or
275 * end_vm are still 0 (as at startup), in which case we call free, or
276 * we have set them, but anyway addr has not been allocated from those
277 * areas. So, in both cases __real_free is called.
278 *
279 * CAN_KMALLOC is checked because it would be bad to free a buffer
280 * with kmalloc/vmalloc after they have been turned off during
281 * shutdown.
282 * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
283 * there is a possibility for memory leaks.
284 */
285
286 if((addr >= uml_physmem) && (addr < high_physmem)){
287 if(CAN_KMALLOC())
288 kfree(ptr);
289 }
290 else if((addr >= start_vm) && (addr < end_vm)){
291 if(CAN_KMALLOC())
292 vfree(ptr);
293 }
294 else __real_free(ptr);
295}