MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / arch / um / kernel / main.c
blobe1fd2c5830d7db74af0c8b9ce656149c033d322f
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 "signal_user.h"
20 #include "user.h"
21 #include "init.h"
22 #include "mode.h"
23 #include "choose-mode.h"
24 #include "uml-config.h"
26 /* Set in set_stklim, which is called from main and __wrap_malloc.
27 * __wrap_malloc only calls it if main hasn't started.
29 unsigned long stacksizelim;
31 /* Set in main */
32 char *linux_prog;
34 #define PGD_BOUND (4 * 1024 * 1024)
35 #define STACKSIZE (8 * 1024 * 1024)
36 #define THREAD_NAME_LEN (256)
38 static void set_stklim(void)
40 struct rlimit lim;
42 if(getrlimit(RLIMIT_STACK, &lim) < 0){
43 perror("getrlimit");
44 exit(1);
46 if((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)){
47 lim.rlim_cur = STACKSIZE;
48 if(setrlimit(RLIMIT_STACK, &lim) < 0){
49 perror("setrlimit");
50 exit(1);
53 stacksizelim = (lim.rlim_cur + PGD_BOUND - 1) & ~(PGD_BOUND - 1);
56 static __init void do_uml_initcalls(void)
58 initcall_t *call;
60 call = &__uml_initcall_start;
61 while (call < &__uml_initcall_end){;
62 (*call)();
63 call++;
67 static void last_ditch_exit(int sig)
69 CHOOSE_MODE(kmalloc_ok = 0, (void) 0);
70 signal(SIGINT, SIG_DFL);
71 signal(SIGTERM, SIG_DFL);
72 signal(SIGHUP, SIG_DFL);
73 uml_cleanup();
74 exit(1);
77 extern int uml_exitcode;
79 int main(int argc, char **argv, char **envp)
81 char **new_argv;
82 sigset_t mask;
83 int ret, i;
85 /* Enable all signals except SIGIO - in some environments, we can
86 * enter with some signals blocked
89 sigemptyset(&mask);
90 sigaddset(&mask, SIGIO);
91 if(sigprocmask(SIG_SETMASK, &mask, NULL) < 0){
92 perror("sigprocmask");
93 exit(1);
96 #ifdef UML_CONFIG_MODE_TT
97 /* Allocate memory for thread command lines */
98 if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
100 char padding[THREAD_NAME_LEN] = {
101 [ 0 ... THREAD_NAME_LEN - 2] = ' ', '\0'
104 new_argv = malloc((argc + 2) * sizeof(char*));
105 if(!new_argv) {
106 perror("Allocating extended argv");
107 exit(1);
110 new_argv[0] = argv[0];
111 new_argv[1] = padding;
113 for(i = 2; i <= argc; i++)
114 new_argv[i] = argv[i - 1];
115 new_argv[argc + 1] = NULL;
117 execvp(new_argv[0], new_argv);
118 perror("execing with extended args");
119 exit(1);
121 #endif
123 linux_prog = argv[0];
125 set_stklim();
127 new_argv = malloc((argc + 1) * sizeof(char *));
128 if(new_argv == NULL){
129 perror("Mallocing argv");
130 exit(1);
132 for(i=0;i<argc;i++){
133 new_argv[i] = strdup(argv[i]);
134 if(new_argv[i] == NULL){
135 perror("Mallocing an arg");
136 exit(1);
139 new_argv[argc] = NULL;
141 set_handler(SIGINT, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
142 set_handler(SIGTERM, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
143 set_handler(SIGHUP, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
145 do_uml_initcalls();
146 ret = linux_main(argc, argv);
148 /* Reboot */
149 if(ret){
150 int err;
152 printf("\n");
154 /* Let any pending signals fire, then disable them. This
155 * ensures that they won't be delivered after the exec, when
156 * they are definitely not expected.
158 unblock_signals();
159 disable_timer();
160 err = deactivate_all_fds();
161 if(err)
162 printf("deactivate_all_fds failed, errno = %d\n",
163 -err);
165 execvp(new_argv[0], new_argv);
166 perror("Failed to exec kernel");
167 ret = 1;
169 printf("\n");
170 return(uml_exitcode);
173 #define CAN_KMALLOC() \
174 (kmalloc_ok && CHOOSE_MODE((getpid() != tracing_pid), 1))
176 extern void *__real_malloc(int);
178 void *__wrap_malloc(int size)
180 void *ret;
182 if(!CAN_KMALLOC())
183 return(__real_malloc(size));
184 else if(size <= PAGE_SIZE) /* finding contiguos pages can be hard*/
185 ret = um_kmalloc(size);
186 else ret = um_vmalloc(size);
188 /* glibc people insist that if malloc fails, errno should be
189 * set by malloc as well. So we do.
191 if(ret == NULL)
192 errno = ENOMEM;
194 return(ret);
197 void *__wrap_calloc(int n, int size)
199 void *ptr = __wrap_malloc(n * size);
201 if(ptr == NULL) return(NULL);
202 memset(ptr, 0, n * size);
203 return(ptr);
206 extern void __real_free(void *);
208 extern unsigned long high_physmem;
210 void __wrap_free(void *ptr)
212 unsigned long addr = (unsigned long) ptr;
214 /* We need to know how the allocation happened, so it can be correctly
215 * freed. This is done by seeing what region of memory the pointer is
216 * in -
217 * physical memory - kmalloc/kfree
218 * kernel virtual memory - vmalloc/vfree
219 * anywhere else - malloc/free
220 * If kmalloc is not yet possible, then the kernel memory regions
221 * may not be set up yet, and the variables not initialized. So,
222 * free is called.
224 * CAN_KMALLOC is checked because it would be bad to free a buffer
225 * with kmalloc/vmalloc after they have been turned off during
226 * shutdown.
229 if((addr >= uml_physmem) && (addr < high_physmem)){
230 if(CAN_KMALLOC())
231 kfree(ptr);
233 else if((addr >= start_vm) && (addr < end_vm)){
234 if(CAN_KMALLOC())
235 vfree(ptr);
237 else __real_free(ptr);
241 * Overrides for Emacs so that we follow Linus's tabbing style.
242 * Emacs will notice this stuff at the end of the file and automatically
243 * adjust the settings for this buffer only. This must remain at the end
244 * of the file.
245 * ---------------------------------------------------------------------------
246 * Local variables:
247 * c-file-style: "linux"
248 * End: