]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blame - arch/um/os-Linux/main.c
uml: remove user_util.h
[mirror_ubuntu-kernels.git] / arch / um / os-Linux / main.c
CommitLineData
1da177e4
LT
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>
1da177e4 16#include "kern_util.h"
4ff83ce1 17#include "as-layout.h"
1da177e4 18#include "mem_user.h"
1da177e4
LT
19#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"
1da177e4 25#include "os.h"
c13e5690 26#include "um_malloc.h"
1da177e4 27
11100b1d 28/* Set in main, unchanged thereafter */
1da177e4
LT
29char *linux_prog;
30
31#define PGD_BOUND (4 * 1024 * 1024)
32#define STACKSIZE (8 * 1024 * 1024)
33#define THREAD_NAME_LEN (256)
34
35static void set_stklim(void)
36{
37 struct rlimit lim;
38
39 if(getrlimit(RLIMIT_STACK, &lim) < 0){
40 perror("getrlimit");
41 exit(1);
42 }
43 if((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)){
44 lim.rlim_cur = STACKSIZE;
45 if(setrlimit(RLIMIT_STACK, &lim) < 0){
46 perror("setrlimit");
47 exit(1);
48 }
49 }
1da177e4
LT
50}
51
52static __init void do_uml_initcalls(void)
53{
54 initcall_t *call;
55
56 call = &__uml_initcall_start;
f2183125 57 while (call < &__uml_initcall_end){
1da177e4
LT
58 (*call)();
59 call++;
60 }
61}
62
63static void last_ditch_exit(int sig)
64{
1da177e4
LT
65 uml_cleanup();
66 exit(1);
67}
68
4b84c69b
JD
69static void install_fatal_handler(int sig)
70{
71 struct sigaction action;
72
73 /* All signals are enabled in this handler ... */
74 sigemptyset(&action.sa_mask);
75
76 /* ... including the signal being handled, plus we want the
77 * handler reset to the default behavior, so that if an exit
78 * handler is hanging for some reason, the UML will just die
79 * after this signal is sent a second time.
80 */
81 action.sa_flags = SA_RESETHAND | SA_NODEFER;
82 action.sa_restorer = NULL;
83 action.sa_handler = last_ditch_exit;
84 if(sigaction(sig, &action, NULL) < 0){
85 printf("failed to install handler for signal %d - errno = %d\n",
86 errno);
87 exit(1);
88 }
89}
90
cb98cdcd
MD
91#define UML_LIB_PATH ":/usr/lib/uml"
92
93static void setup_env_path(void)
94{
95 char *new_path = NULL;
96 char *old_path = NULL;
97 int path_len = 0;
98
99 old_path = getenv("PATH");
100 /* if no PATH variable is set or it has an empty value
101 * just use the default + /usr/lib/uml
102 */
103 if (!old_path || (path_len = strlen(old_path)) == 0) {
104 putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH);
105 return;
106 }
107
108 /* append /usr/lib/uml to the existing path */
109 path_len += strlen("PATH=" UML_LIB_PATH) + 1;
110 new_path = malloc(path_len);
111 if (!new_path) {
112 perror("coudn't malloc to set a new PATH");
113 return;
114 }
115 snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path);
116 putenv(new_path);
117}
118
1da177e4
LT
119extern int uml_exitcode;
120
121extern void scan_elf_aux( char **envp);
122
123int main(int argc, char **argv, char **envp)
124{
125 char **new_argv;
92515da7 126 int ret, i, err;
1da177e4 127
02215759 128#ifdef UML_CONFIG_CMDLINE_ON_HOST
1da177e4
LT
129 /* Allocate memory for thread command lines */
130 if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
131
132 char padding[THREAD_NAME_LEN] = {
133 [ 0 ... THREAD_NAME_LEN - 2] = ' ', '\0'
134 };
135
136 new_argv = malloc((argc + 2) * sizeof(char*));
137 if(!new_argv) {
138 perror("Allocating extended argv");
139 exit(1);
140 }
141
142 new_argv[0] = argv[0];
143 new_argv[1] = padding;
144
145 for(i = 2; i <= argc; i++)
146 new_argv[i] = argv[i - 1];
147 new_argv[argc + 1] = NULL;
148
149 execvp(new_argv[0], new_argv);
150 perror("execing with extended args");
151 exit(1);
152 }
153#endif
154
155 linux_prog = argv[0];
156
157 set_stklim();
158
cb98cdcd
MD
159 setup_env_path();
160
1da177e4
LT
161 new_argv = malloc((argc + 1) * sizeof(char *));
162 if(new_argv == NULL){
163 perror("Mallocing argv");
164 exit(1);
165 }
166 for(i=0;i<argc;i++){
167 new_argv[i] = strdup(argv[i]);
168 if(new_argv[i] == NULL){
169 perror("Mallocing an arg");
170 exit(1);
171 }
172 }
173 new_argv[argc] = NULL;
174
4b84c69b
JD
175 /* Allow these signals to bring down a UML if all other
176 * methods of control fail.
177 */
178 install_fatal_handler(SIGINT);
179 install_fatal_handler(SIGTERM);
180 install_fatal_handler(SIGHUP);
1da177e4
LT
181
182 scan_elf_aux( envp);
183
184 do_uml_initcalls();
185 ret = linux_main(argc, argv);
186
187 /* Disable SIGPROF - I have no idea why libc doesn't do this or turn
188 * off the profiling time, but UML dies with a SIGPROF just before
189 * exiting when profiling is active.
190 */
191 change_sig(SIGPROF, 0);
192
52c653b3
JD
193 /* This signal stuff used to be in the reboot case. However,
194 * sometimes a SIGVTALRM can come in when we're halting (reproducably
195 * when writing out gcov information, presumably because that takes
196 * some time) and cause a segfault.
197 */
198
199 /* stop timers and set SIG*ALRM to be ignored */
200 disable_timer();
201
202 /* disable SIGIO for the fds and set SIGIO to be ignored */
203 err = deactivate_all_fds();
204 if(err)
205 printf("deactivate_all_fds failed, errno = %d\n", -err);
206
207 /* Let any pending signals fire now. This ensures
208 * that they won't be delivered after the exec, when
209 * they are definitely not expected.
210 */
211 unblock_signals();
1da177e4 212
92515da7
JD
213 /* Reboot */
214 if(ret){
215 printf("\n");
1da177e4
LT
216 execvp(new_argv[0], new_argv);
217 perror("Failed to exec kernel");
218 ret = 1;
219 }
220 printf("\n");
a5ed1ffa 221 return uml_exitcode;
1da177e4
LT
222}
223
224#define CAN_KMALLOC() \
225 (kmalloc_ok && CHOOSE_MODE((os_getpid() != tracing_pid), 1))
226
227extern void *__real_malloc(int);
228
229void *__wrap_malloc(int size)
230{
231 void *ret;
232
233 if(!CAN_KMALLOC())
a5ed1ffa 234 return __real_malloc(size);
1da177e4
LT
235 else if(size <= PAGE_SIZE) /* finding contiguos pages can be hard*/
236 ret = um_kmalloc(size);
237 else ret = um_vmalloc(size);
238
239 /* glibc people insist that if malloc fails, errno should be
240 * set by malloc as well. So we do.
241 */
242 if(ret == NULL)
243 errno = ENOMEM;
244
a5ed1ffa 245 return ret;
1da177e4
LT
246}
247
248void *__wrap_calloc(int n, int size)
249{
250 void *ptr = __wrap_malloc(n * size);
251
a5ed1ffa
JD
252 if(ptr == NULL)
253 return NULL;
1da177e4 254 memset(ptr, 0, n * size);
a5ed1ffa 255 return ptr;
1da177e4
LT
256}
257
258extern void __real_free(void *);
259
260extern unsigned long high_physmem;
261
262void __wrap_free(void *ptr)
263{
264 unsigned long addr = (unsigned long) ptr;
265
266 /* We need to know how the allocation happened, so it can be correctly
267 * freed. This is done by seeing what region of memory the pointer is
268 * in -
269 * physical memory - kmalloc/kfree
270 * kernel virtual memory - vmalloc/vfree
271 * anywhere else - malloc/free
272 * If kmalloc is not yet possible, then either high_physmem and/or
273 * end_vm are still 0 (as at startup), in which case we call free, or
274 * we have set them, but anyway addr has not been allocated from those
275 * areas. So, in both cases __real_free is called.
276 *
277 * CAN_KMALLOC is checked because it would be bad to free a buffer
278 * with kmalloc/vmalloc after they have been turned off during
279 * shutdown.
280 * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
281 * there is a possibility for memory leaks.
282 */
283
284 if((addr >= uml_physmem) && (addr < high_physmem)){
285 if(CAN_KMALLOC())
286 kfree(ptr);
287 }
288 else if((addr >= start_vm) && (addr < end_vm)){
289 if(CAN_KMALLOC())
290 vfree(ptr);
291 }
292 else __real_free(ptr);
293}