MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / arch / um / os-Linux / main.c
blob685feaab65d239efa4931af9838e63b54f62d702
1 /*
2 * Copyright (C) 2000, 2001 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
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 "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"
25 #include "os.h"
26 #include "um_malloc.h"
28 /* Set in set_stklim, which is called from main and __wrap_malloc.
29 * __wrap_malloc only calls it if main hasn't started.
31 unsigned long stacksizelim;
33 /* Set in main */
34 char *linux_prog;
36 #define PGD_BOUND (4 * 1024 * 1024)
37 #define STACKSIZE (8 * 1024 * 1024)
38 #define THREAD_NAME_LEN (256)
40 static void set_stklim(void)
42 struct rlimit lim;
44 if(getrlimit(RLIMIT_STACK, &lim) < 0){
45 perror("getrlimit");
46 exit(1);
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);
55 stacksizelim = (lim.rlim_cur + PGD_BOUND - 1) & ~(PGD_BOUND - 1);
58 static __init void do_uml_initcalls(void)
60 initcall_t *call;
62 call = &__uml_initcall_start;
63 while (call < &__uml_initcall_end){
64 (*call)();
65 call++;
69 static void last_ditch_exit(int sig)
71 uml_cleanup();
72 exit(1);
75 static void install_fatal_handler(int sig)
77 struct sigaction action;
79 /* All signals are enabled in this handler ... */
80 sigemptyset(&action.sa_mask);
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.
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);
97 #define UML_LIB_PATH ":/usr/lib/uml"
99 static void setup_env_path(void)
101 char *new_path = NULL;
102 char *old_path = NULL;
103 int path_len = 0;
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
109 if (!old_path || (path_len = strlen(old_path)) == 0) {
110 putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH);
111 return;
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;
121 snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path);
122 putenv(new_path);
125 extern int uml_exitcode;
127 extern void scan_elf_aux( char **envp);
129 int main(int argc, char **argv, char **envp)
131 char **new_argv;
132 int ret, i, err;
134 #ifdef UML_CONFIG_CMDLINE_ON_HOST
135 /* Allocate memory for thread command lines */
136 if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
138 char padding[THREAD_NAME_LEN] = {
139 [ 0 ... THREAD_NAME_LEN - 2] = ' ', '\0'
142 new_argv = malloc((argc + 2) * sizeof(char*));
143 if(!new_argv) {
144 perror("Allocating extended argv");
145 exit(1);
148 new_argv[0] = argv[0];
149 new_argv[1] = padding;
151 for(i = 2; i <= argc; i++)
152 new_argv[i] = argv[i - 1];
153 new_argv[argc + 1] = NULL;
155 execvp(new_argv[0], new_argv);
156 perror("execing with extended args");
157 exit(1);
159 #endif
161 linux_prog = argv[0];
163 set_stklim();
165 setup_env_path();
167 new_argv = malloc((argc + 1) * sizeof(char *));
168 if(new_argv == NULL){
169 perror("Mallocing argv");
170 exit(1);
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);
179 new_argv[argc] = NULL;
181 /* Allow these signals to bring down a UML if all other
182 * methods of control fail.
184 install_fatal_handler(SIGINT);
185 install_fatal_handler(SIGTERM);
186 install_fatal_handler(SIGHUP);
188 scan_elf_aux( envp);
190 do_uml_initcalls();
191 ret = linux_main(argc, argv);
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.
197 change_sig(SIGPROF, 0);
199 /* 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.
205 /* stop timers and set SIG*ALRM to be ignored */
206 disable_timer();
208 /* 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);
213 /* 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.
217 unblock_signals();
219 /* Reboot */
220 if(ret){
221 printf("\n");
222 execvp(new_argv[0], new_argv);
223 perror("Failed to exec kernel");
224 ret = 1;
226 printf("\n");
227 return(uml_exitcode);
230 #define CAN_KMALLOC() \
231 (kmalloc_ok && CHOOSE_MODE((os_getpid() != tracing_pid), 1))
233 extern void *__real_malloc(int);
235 void *__wrap_malloc(int size)
237 void *ret;
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);
245 /* glibc people insist that if malloc fails, errno should be
246 * set by malloc as well. So we do.
248 if(ret == NULL)
249 errno = ENOMEM;
251 return(ret);
254 void *__wrap_calloc(int n, int size)
256 void *ptr = __wrap_malloc(n * size);
258 if(ptr == NULL) return(NULL);
259 memset(ptr, 0, n * size);
260 return(ptr);
263 extern void __real_free(void *);
265 extern unsigned long high_physmem;
267 void __wrap_free(void *ptr)
269 unsigned long addr = (unsigned long) ptr;
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.
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.
289 if((addr >= uml_physmem) && (addr < high_physmem)){
290 if(CAN_KMALLOC())
291 kfree(ptr);
293 else if((addr >= start_vm) && (addr < end_vm)){
294 if(CAN_KMALLOC())
295 vfree(ptr);
297 else __real_free(ptr);