]>
Commit | Line | Data |
---|---|---|
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> | |
16 | #include "user_util.h" | |
17 | #include "kern_util.h" | |
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 LT |
25 | #include "os.h" |
26 | ||
27 | /* Set in set_stklim, which is called from main and __wrap_malloc. | |
28 | * __wrap_malloc only calls it if main hasn't started. | |
29 | */ | |
30 | unsigned long stacksizelim; | |
31 | ||
32 | /* Set in main */ | |
33 | char *linux_prog; | |
34 | ||
35 | #define PGD_BOUND (4 * 1024 * 1024) | |
36 | #define STACKSIZE (8 * 1024 * 1024) | |
37 | #define THREAD_NAME_LEN (256) | |
38 | ||
39 | static void set_stklim(void) | |
40 | { | |
41 | struct rlimit lim; | |
42 | ||
43 | if(getrlimit(RLIMIT_STACK, &lim) < 0){ | |
44 | perror("getrlimit"); | |
45 | exit(1); | |
46 | } | |
47 | if((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)){ | |
48 | lim.rlim_cur = STACKSIZE; | |
49 | if(setrlimit(RLIMIT_STACK, &lim) < 0){ | |
50 | perror("setrlimit"); | |
51 | exit(1); | |
52 | } | |
53 | } | |
54 | stacksizelim = (lim.rlim_cur + PGD_BOUND - 1) & ~(PGD_BOUND - 1); | |
55 | } | |
56 | ||
57 | static __init void do_uml_initcalls(void) | |
58 | { | |
59 | initcall_t *call; | |
60 | ||
61 | call = &__uml_initcall_start; | |
62 | while (call < &__uml_initcall_end){; | |
63 | (*call)(); | |
64 | call++; | |
65 | } | |
66 | } | |
67 | ||
68 | static void last_ditch_exit(int sig) | |
69 | { | |
1da177e4 LT |
70 | signal(SIGINT, SIG_DFL); |
71 | signal(SIGTERM, SIG_DFL); | |
72 | signal(SIGHUP, SIG_DFL); | |
73 | uml_cleanup(); | |
74 | exit(1); | |
75 | } | |
76 | ||
77 | extern int uml_exitcode; | |
78 | ||
79 | extern void scan_elf_aux( char **envp); | |
80 | ||
81 | int main(int argc, char **argv, char **envp) | |
82 | { | |
83 | char **new_argv; | |
92515da7 | 84 | int ret, i, err; |
1da177e4 | 85 | |
02215759 | 86 | #ifdef UML_CONFIG_CMDLINE_ON_HOST |
1da177e4 LT |
87 | /* Allocate memory for thread command lines */ |
88 | if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){ | |
89 | ||
90 | char padding[THREAD_NAME_LEN] = { | |
91 | [ 0 ... THREAD_NAME_LEN - 2] = ' ', '\0' | |
92 | }; | |
93 | ||
94 | new_argv = malloc((argc + 2) * sizeof(char*)); | |
95 | if(!new_argv) { | |
96 | perror("Allocating extended argv"); | |
97 | exit(1); | |
98 | } | |
99 | ||
100 | new_argv[0] = argv[0]; | |
101 | new_argv[1] = padding; | |
102 | ||
103 | for(i = 2; i <= argc; i++) | |
104 | new_argv[i] = argv[i - 1]; | |
105 | new_argv[argc + 1] = NULL; | |
106 | ||
107 | execvp(new_argv[0], new_argv); | |
108 | perror("execing with extended args"); | |
109 | exit(1); | |
110 | } | |
111 | #endif | |
112 | ||
113 | linux_prog = argv[0]; | |
114 | ||
115 | set_stklim(); | |
116 | ||
117 | new_argv = malloc((argc + 1) * sizeof(char *)); | |
118 | if(new_argv == NULL){ | |
119 | perror("Mallocing argv"); | |
120 | exit(1); | |
121 | } | |
122 | for(i=0;i<argc;i++){ | |
123 | new_argv[i] = strdup(argv[i]); | |
124 | if(new_argv[i] == NULL){ | |
125 | perror("Mallocing an arg"); | |
126 | exit(1); | |
127 | } | |
128 | } | |
129 | new_argv[argc] = NULL; | |
130 | ||
131 | set_handler(SIGINT, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1); | |
132 | set_handler(SIGTERM, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1); | |
133 | set_handler(SIGHUP, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1); | |
134 | ||
135 | scan_elf_aux( envp); | |
136 | ||
137 | do_uml_initcalls(); | |
138 | ret = linux_main(argc, argv); | |
139 | ||
140 | /* Disable SIGPROF - I have no idea why libc doesn't do this or turn | |
141 | * off the profiling time, but UML dies with a SIGPROF just before | |
142 | * exiting when profiling is active. | |
143 | */ | |
144 | change_sig(SIGPROF, 0); | |
145 | ||
52c653b3 JD |
146 | /* This signal stuff used to be in the reboot case. However, |
147 | * sometimes a SIGVTALRM can come in when we're halting (reproducably | |
148 | * when writing out gcov information, presumably because that takes | |
149 | * some time) and cause a segfault. | |
150 | */ | |
151 | ||
152 | /* stop timers and set SIG*ALRM to be ignored */ | |
153 | disable_timer(); | |
154 | ||
155 | /* disable SIGIO for the fds and set SIGIO to be ignored */ | |
156 | err = deactivate_all_fds(); | |
157 | if(err) | |
158 | printf("deactivate_all_fds failed, errno = %d\n", -err); | |
159 | ||
160 | /* Let any pending signals fire now. This ensures | |
161 | * that they won't be delivered after the exec, when | |
162 | * they are definitely not expected. | |
163 | */ | |
164 | unblock_signals(); | |
1da177e4 | 165 | |
92515da7 JD |
166 | /* Reboot */ |
167 | if(ret){ | |
168 | printf("\n"); | |
1da177e4 LT |
169 | execvp(new_argv[0], new_argv); |
170 | perror("Failed to exec kernel"); | |
171 | ret = 1; | |
172 | } | |
173 | printf("\n"); | |
174 | return(uml_exitcode); | |
175 | } | |
176 | ||
177 | #define CAN_KMALLOC() \ | |
178 | (kmalloc_ok && CHOOSE_MODE((os_getpid() != tracing_pid), 1)) | |
179 | ||
180 | extern void *__real_malloc(int); | |
181 | ||
182 | void *__wrap_malloc(int size) | |
183 | { | |
184 | void *ret; | |
185 | ||
186 | if(!CAN_KMALLOC()) | |
187 | return(__real_malloc(size)); | |
188 | else if(size <= PAGE_SIZE) /* finding contiguos pages can be hard*/ | |
189 | ret = um_kmalloc(size); | |
190 | else ret = um_vmalloc(size); | |
191 | ||
192 | /* glibc people insist that if malloc fails, errno should be | |
193 | * set by malloc as well. So we do. | |
194 | */ | |
195 | if(ret == NULL) | |
196 | errno = ENOMEM; | |
197 | ||
198 | return(ret); | |
199 | } | |
200 | ||
201 | void *__wrap_calloc(int n, int size) | |
202 | { | |
203 | void *ptr = __wrap_malloc(n * size); | |
204 | ||
205 | if(ptr == NULL) return(NULL); | |
206 | memset(ptr, 0, n * size); | |
207 | return(ptr); | |
208 | } | |
209 | ||
210 | extern void __real_free(void *); | |
211 | ||
212 | extern unsigned long high_physmem; | |
213 | ||
214 | void __wrap_free(void *ptr) | |
215 | { | |
216 | unsigned long addr = (unsigned long) ptr; | |
217 | ||
218 | /* We need to know how the allocation happened, so it can be correctly | |
219 | * freed. This is done by seeing what region of memory the pointer is | |
220 | * in - | |
221 | * physical memory - kmalloc/kfree | |
222 | * kernel virtual memory - vmalloc/vfree | |
223 | * anywhere else - malloc/free | |
224 | * If kmalloc is not yet possible, then either high_physmem and/or | |
225 | * end_vm are still 0 (as at startup), in which case we call free, or | |
226 | * we have set them, but anyway addr has not been allocated from those | |
227 | * areas. So, in both cases __real_free is called. | |
228 | * | |
229 | * CAN_KMALLOC is checked because it would be bad to free a buffer | |
230 | * with kmalloc/vmalloc after they have been turned off during | |
231 | * shutdown. | |
232 | * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so | |
233 | * there is a possibility for memory leaks. | |
234 | */ | |
235 | ||
236 | if((addr >= uml_physmem) && (addr < high_physmem)){ | |
237 | if(CAN_KMALLOC()) | |
238 | kfree(ptr); | |
239 | } | |
240 | else if((addr >= start_vm) && (addr < end_vm)){ | |
241 | if(CAN_KMALLOC()) | |
242 | vfree(ptr); | |
243 | } | |
244 | else __real_free(ptr); | |
245 | } |