Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Jeff Dike | f218312 | 2006-06-04 02:51:47 -0700 | [diff] [blame] | 62 | while (call < &__uml_initcall_end){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | (*call)(); |
| 64 | call++; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | static void last_ditch_exit(int sig) |
| 69 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | signal(SIGINT, SIG_DFL); |
| 71 | signal(SIGTERM, SIG_DFL); |
| 72 | signal(SIGHUP, SIG_DFL); |
| 73 | uml_cleanup(); |
| 74 | exit(1); |
| 75 | } |
| 76 | |
Mattia Dongili | cb98cdc | 2006-05-01 12:16:01 -0700 | [diff] [blame] | 77 | #define UML_LIB_PATH ":/usr/lib/uml" |
| 78 | |
| 79 | static void setup_env_path(void) |
| 80 | { |
| 81 | char *new_path = NULL; |
| 82 | char *old_path = NULL; |
| 83 | int path_len = 0; |
| 84 | |
| 85 | old_path = getenv("PATH"); |
| 86 | /* if no PATH variable is set or it has an empty value |
| 87 | * just use the default + /usr/lib/uml |
| 88 | */ |
| 89 | if (!old_path || (path_len = strlen(old_path)) == 0) { |
| 90 | putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | /* append /usr/lib/uml to the existing path */ |
| 95 | path_len += strlen("PATH=" UML_LIB_PATH) + 1; |
| 96 | new_path = malloc(path_len); |
| 97 | if (!new_path) { |
| 98 | perror("coudn't malloc to set a new PATH"); |
| 99 | return; |
| 100 | } |
| 101 | snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path); |
| 102 | putenv(new_path); |
| 103 | } |
| 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | extern int uml_exitcode; |
| 106 | |
| 107 | extern void scan_elf_aux( char **envp); |
| 108 | |
| 109 | int main(int argc, char **argv, char **envp) |
| 110 | { |
| 111 | char **new_argv; |
Jeff Dike | 92515da | 2005-05-28 15:51:56 -0700 | [diff] [blame] | 112 | int ret, i, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Paolo 'Blaisorblade' Giarrusso | 0221575 | 2005-09-03 15:57:23 -0700 | [diff] [blame] | 114 | #ifdef UML_CONFIG_CMDLINE_ON_HOST |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | /* Allocate memory for thread command lines */ |
| 116 | if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){ |
| 117 | |
| 118 | char padding[THREAD_NAME_LEN] = { |
| 119 | [ 0 ... THREAD_NAME_LEN - 2] = ' ', '\0' |
| 120 | }; |
| 121 | |
| 122 | new_argv = malloc((argc + 2) * sizeof(char*)); |
| 123 | if(!new_argv) { |
| 124 | perror("Allocating extended argv"); |
| 125 | exit(1); |
| 126 | } |
| 127 | |
| 128 | new_argv[0] = argv[0]; |
| 129 | new_argv[1] = padding; |
| 130 | |
| 131 | for(i = 2; i <= argc; i++) |
| 132 | new_argv[i] = argv[i - 1]; |
| 133 | new_argv[argc + 1] = NULL; |
| 134 | |
| 135 | execvp(new_argv[0], new_argv); |
| 136 | perror("execing with extended args"); |
| 137 | exit(1); |
| 138 | } |
| 139 | #endif |
| 140 | |
| 141 | linux_prog = argv[0]; |
| 142 | |
| 143 | set_stklim(); |
| 144 | |
Mattia Dongili | cb98cdc | 2006-05-01 12:16:01 -0700 | [diff] [blame] | 145 | setup_env_path(); |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | new_argv = malloc((argc + 1) * sizeof(char *)); |
| 148 | if(new_argv == NULL){ |
| 149 | perror("Mallocing argv"); |
| 150 | exit(1); |
| 151 | } |
| 152 | for(i=0;i<argc;i++){ |
| 153 | new_argv[i] = strdup(argv[i]); |
| 154 | if(new_argv[i] == NULL){ |
| 155 | perror("Mallocing an arg"); |
| 156 | exit(1); |
| 157 | } |
| 158 | } |
| 159 | new_argv[argc] = NULL; |
| 160 | |
| 161 | set_handler(SIGINT, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1); |
| 162 | set_handler(SIGTERM, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1); |
| 163 | set_handler(SIGHUP, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1); |
| 164 | |
| 165 | scan_elf_aux( envp); |
| 166 | |
| 167 | do_uml_initcalls(); |
| 168 | ret = linux_main(argc, argv); |
| 169 | |
| 170 | /* Disable SIGPROF - I have no idea why libc doesn't do this or turn |
| 171 | * off the profiling time, but UML dies with a SIGPROF just before |
| 172 | * exiting when profiling is active. |
| 173 | */ |
| 174 | change_sig(SIGPROF, 0); |
| 175 | |
Jeff Dike | 52c653b | 2005-11-07 00:58:50 -0800 | [diff] [blame] | 176 | /* This signal stuff used to be in the reboot case. However, |
| 177 | * sometimes a SIGVTALRM can come in when we're halting (reproducably |
| 178 | * when writing out gcov information, presumably because that takes |
| 179 | * some time) and cause a segfault. |
| 180 | */ |
Jeff Dike | 92515da | 2005-05-28 15:51:56 -0700 | [diff] [blame] | 181 | |
Jeff Dike | 52c653b | 2005-11-07 00:58:50 -0800 | [diff] [blame] | 182 | /* stop timers and set SIG*ALRM to be ignored */ |
| 183 | disable_timer(); |
Jeff Dike | 92515da | 2005-05-28 15:51:56 -0700 | [diff] [blame] | 184 | |
Jeff Dike | 52c653b | 2005-11-07 00:58:50 -0800 | [diff] [blame] | 185 | /* disable SIGIO for the fds and set SIGIO to be ignored */ |
| 186 | err = deactivate_all_fds(); |
| 187 | if(err) |
| 188 | printf("deactivate_all_fds failed, errno = %d\n", -err); |
Jeff Dike | 92515da | 2005-05-28 15:51:56 -0700 | [diff] [blame] | 189 | |
Jeff Dike | 52c653b | 2005-11-07 00:58:50 -0800 | [diff] [blame] | 190 | /* Let any pending signals fire now. This ensures |
| 191 | * that they won't be delivered after the exec, when |
| 192 | * they are definitely not expected. |
| 193 | */ |
| 194 | unblock_signals(); |
Jeff Dike | 92515da | 2005-05-28 15:51:56 -0700 | [diff] [blame] | 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | /* Reboot */ |
| 197 | if(ret){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | printf("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | execvp(new_argv[0], new_argv); |
| 200 | perror("Failed to exec kernel"); |
| 201 | ret = 1; |
| 202 | } |
| 203 | printf("\n"); |
| 204 | return(uml_exitcode); |
| 205 | } |
| 206 | |
| 207 | #define CAN_KMALLOC() \ |
| 208 | (kmalloc_ok && CHOOSE_MODE((os_getpid() != tracing_pid), 1)) |
| 209 | |
| 210 | extern void *__real_malloc(int); |
| 211 | |
| 212 | void *__wrap_malloc(int size) |
| 213 | { |
| 214 | void *ret; |
| 215 | |
| 216 | if(!CAN_KMALLOC()) |
| 217 | return(__real_malloc(size)); |
| 218 | else if(size <= PAGE_SIZE) /* finding contiguos pages can be hard*/ |
| 219 | ret = um_kmalloc(size); |
| 220 | else ret = um_vmalloc(size); |
| 221 | |
| 222 | /* glibc people insist that if malloc fails, errno should be |
| 223 | * set by malloc as well. So we do. |
| 224 | */ |
| 225 | if(ret == NULL) |
| 226 | errno = ENOMEM; |
| 227 | |
| 228 | return(ret); |
| 229 | } |
| 230 | |
| 231 | void *__wrap_calloc(int n, int size) |
| 232 | { |
| 233 | void *ptr = __wrap_malloc(n * size); |
| 234 | |
| 235 | if(ptr == NULL) return(NULL); |
| 236 | memset(ptr, 0, n * size); |
| 237 | return(ptr); |
| 238 | } |
| 239 | |
| 240 | extern void __real_free(void *); |
| 241 | |
| 242 | extern unsigned long high_physmem; |
| 243 | |
| 244 | void __wrap_free(void *ptr) |
| 245 | { |
| 246 | unsigned long addr = (unsigned long) ptr; |
| 247 | |
| 248 | /* We need to know how the allocation happened, so it can be correctly |
| 249 | * freed. This is done by seeing what region of memory the pointer is |
| 250 | * in - |
| 251 | * physical memory - kmalloc/kfree |
| 252 | * kernel virtual memory - vmalloc/vfree |
| 253 | * anywhere else - malloc/free |
| 254 | * If kmalloc is not yet possible, then either high_physmem and/or |
| 255 | * end_vm are still 0 (as at startup), in which case we call free, or |
| 256 | * we have set them, but anyway addr has not been allocated from those |
| 257 | * areas. So, in both cases __real_free is called. |
| 258 | * |
| 259 | * CAN_KMALLOC is checked because it would be bad to free a buffer |
| 260 | * with kmalloc/vmalloc after they have been turned off during |
| 261 | * shutdown. |
| 262 | * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so |
| 263 | * there is a possibility for memory leaks. |
| 264 | */ |
| 265 | |
| 266 | if((addr >= uml_physmem) && (addr < high_physmem)){ |
| 267 | if(CAN_KMALLOC()) |
| 268 | kfree(ptr); |
| 269 | } |
| 270 | else if((addr >= start_vm) && (addr < end_vm)){ |
| 271 | if(CAN_KMALLOC()) |
| 272 | vfree(ptr); |
| 273 | } |
| 274 | else __real_free(ptr); |
| 275 | } |