152c653b3SJeff Dike /* 2ba180fd4SJeff Dike * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 352c653b3SJeff Dike * Licensed under the GPL 452c653b3SJeff Dike */ 552c653b3SJeff Dike 652c653b3SJeff Dike #include <stdio.h> 752c653b3SJeff Dike #include <stdlib.h> 8ba180fd4SJeff Dike #include <unistd.h> 952c653b3SJeff Dike #include <errno.h> 10ba180fd4SJeff Dike #include <signal.h> 11ba180fd4SJeff Dike #include <string.h> 1252c653b3SJeff Dike #include <sys/resource.h> 134ff83ce1SJeff Dike #include "as-layout.h" 1452c653b3SJeff Dike #include "init.h" 15ba180fd4SJeff Dike #include "kern_constants.h" 16ba180fd4SJeff Dike #include "kern_util.h" 1752c653b3SJeff Dike #include "os.h" 18c13e5690SPaolo 'Blaisorblade' Giarrusso #include "um_malloc.h" 1952c653b3SJeff Dike 2052c653b3SJeff Dike #define PGD_BOUND (4 * 1024 * 1024) 2152c653b3SJeff Dike #define STACKSIZE (8 * 1024 * 1024) 2252c653b3SJeff Dike #define THREAD_NAME_LEN (256) 2352c653b3SJeff Dike 2452c653b3SJeff Dike static void set_stklim(void) 2552c653b3SJeff Dike { 2652c653b3SJeff Dike struct rlimit lim; 2752c653b3SJeff Dike 2852c653b3SJeff Dike if (getrlimit(RLIMIT_STACK, &lim) < 0) { 2952c653b3SJeff Dike perror("getrlimit"); 3052c653b3SJeff Dike exit(1); 3152c653b3SJeff Dike } 3252c653b3SJeff Dike if ((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)) { 3352c653b3SJeff Dike lim.rlim_cur = STACKSIZE; 3452c653b3SJeff Dike if (setrlimit(RLIMIT_STACK, &lim) < 0) { 3552c653b3SJeff Dike perror("setrlimit"); 3652c653b3SJeff Dike exit(1); 3752c653b3SJeff Dike } 3852c653b3SJeff Dike } 3952c653b3SJeff Dike } 4052c653b3SJeff Dike 4152c653b3SJeff Dike static __init void do_uml_initcalls(void) 4252c653b3SJeff Dike { 4352c653b3SJeff Dike initcall_t *call; 4452c653b3SJeff Dike 4552c653b3SJeff Dike call = &__uml_initcall_start; 46f2183125SJeff Dike while (call < &__uml_initcall_end) { 4752c653b3SJeff Dike (*call)(); 4852c653b3SJeff Dike call++; 4952c653b3SJeff Dike } 5052c653b3SJeff Dike } 5152c653b3SJeff Dike 5252c653b3SJeff Dike static void last_ditch_exit(int sig) 5352c653b3SJeff Dike { 5452c653b3SJeff Dike uml_cleanup(); 5552c653b3SJeff Dike exit(1); 5652c653b3SJeff Dike } 5752c653b3SJeff Dike 584b84c69bSJeff Dike static void install_fatal_handler(int sig) 594b84c69bSJeff Dike { 604b84c69bSJeff Dike struct sigaction action; 614b84c69bSJeff Dike 624b84c69bSJeff Dike /* All signals are enabled in this handler ... */ 634b84c69bSJeff Dike sigemptyset(&action.sa_mask); 644b84c69bSJeff Dike 65ba180fd4SJeff Dike /* 66ba180fd4SJeff Dike * ... including the signal being handled, plus we want the 674b84c69bSJeff Dike * handler reset to the default behavior, so that if an exit 684b84c69bSJeff Dike * handler is hanging for some reason, the UML will just die 694b84c69bSJeff Dike * after this signal is sent a second time. 704b84c69bSJeff Dike */ 714b84c69bSJeff Dike action.sa_flags = SA_RESETHAND | SA_NODEFER; 724b84c69bSJeff Dike action.sa_restorer = NULL; 734b84c69bSJeff Dike action.sa_handler = last_ditch_exit; 744b84c69bSJeff Dike if (sigaction(sig, &action, NULL) < 0) { 754b84c69bSJeff Dike printf("failed to install handler for signal %d - errno = %d\n", 76c9a3072dSWANG Cong sig, errno); 774b84c69bSJeff Dike exit(1); 784b84c69bSJeff Dike } 794b84c69bSJeff Dike } 804b84c69bSJeff Dike 81cb98cdcdSMattia Dongili #define UML_LIB_PATH ":/usr/lib/uml" 82cb98cdcdSMattia Dongili 83cb98cdcdSMattia Dongili static void setup_env_path(void) 84cb98cdcdSMattia Dongili { 85cb98cdcdSMattia Dongili char *new_path = NULL; 86cb98cdcdSMattia Dongili char *old_path = NULL; 87cb98cdcdSMattia Dongili int path_len = 0; 88cb98cdcdSMattia Dongili 89cb98cdcdSMattia Dongili old_path = getenv("PATH"); 90ba180fd4SJeff Dike /* 91ba180fd4SJeff Dike * if no PATH variable is set or it has an empty value 92cb98cdcdSMattia Dongili * just use the default + /usr/lib/uml 93cb98cdcdSMattia Dongili */ 94cb98cdcdSMattia Dongili if (!old_path || (path_len = strlen(old_path)) == 0) { 95c9a3072dSWANG Cong if (putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH)) 96c9a3072dSWANG Cong perror("couldn't putenv"); 97cb98cdcdSMattia Dongili return; 98cb98cdcdSMattia Dongili } 99cb98cdcdSMattia Dongili 100cb98cdcdSMattia Dongili /* append /usr/lib/uml to the existing path */ 101cb98cdcdSMattia Dongili path_len += strlen("PATH=" UML_LIB_PATH) + 1; 102cb98cdcdSMattia Dongili new_path = malloc(path_len); 103cb98cdcdSMattia Dongili if (!new_path) { 104c9a3072dSWANG Cong perror("couldn't malloc to set a new PATH"); 105cb98cdcdSMattia Dongili return; 106cb98cdcdSMattia Dongili } 107cb98cdcdSMattia Dongili snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path); 108c9a3072dSWANG Cong if (putenv(new_path)) { 109c9a3072dSWANG Cong perror("couldn't putenv to set a new PATH"); 110c9a3072dSWANG Cong free(new_path); 111c9a3072dSWANG Cong } 112cb98cdcdSMattia Dongili } 113cb98cdcdSMattia Dongili 11452c653b3SJeff Dike extern void scan_elf_aux( char **envp); 11552c653b3SJeff Dike 11636e45463SJeff Dike int __init main(int argc, char **argv, char **envp) 11752c653b3SJeff Dike { 11852c653b3SJeff Dike char **new_argv; 11952c653b3SJeff Dike int ret, i, err; 12052c653b3SJeff Dike 12152c653b3SJeff Dike set_stklim(); 12252c653b3SJeff Dike 123cb98cdcdSMattia Dongili setup_env_path(); 124cb98cdcdSMattia Dongili 12552c653b3SJeff Dike new_argv = malloc((argc + 1) * sizeof(char *)); 12652c653b3SJeff Dike if (new_argv == NULL) { 12752c653b3SJeff Dike perror("Mallocing argv"); 12852c653b3SJeff Dike exit(1); 12952c653b3SJeff Dike } 13052c653b3SJeff Dike for (i = 0; i < argc; i++) { 13152c653b3SJeff Dike new_argv[i] = strdup(argv[i]); 13252c653b3SJeff Dike if (new_argv[i] == NULL) { 13352c653b3SJeff Dike perror("Mallocing an arg"); 13452c653b3SJeff Dike exit(1); 13552c653b3SJeff Dike } 13652c653b3SJeff Dike } 13752c653b3SJeff Dike new_argv[argc] = NULL; 13852c653b3SJeff Dike 139ba180fd4SJeff Dike /* 140ba180fd4SJeff Dike * Allow these signals to bring down a UML if all other 1414b84c69bSJeff Dike * methods of control fail. 1424b84c69bSJeff Dike */ 1434b84c69bSJeff Dike install_fatal_handler(SIGINT); 1444b84c69bSJeff Dike install_fatal_handler(SIGTERM); 1454b84c69bSJeff Dike install_fatal_handler(SIGHUP); 14652c653b3SJeff Dike 14752c653b3SJeff Dike scan_elf_aux(envp); 14852c653b3SJeff Dike 14952c653b3SJeff Dike do_uml_initcalls(); 15052c653b3SJeff Dike ret = linux_main(argc, argv); 15152c653b3SJeff Dike 152ba180fd4SJeff Dike /* 153ba180fd4SJeff Dike * Disable SIGPROF - I have no idea why libc doesn't do this or turn 15452c653b3SJeff Dike * off the profiling time, but UML dies with a SIGPROF just before 15552c653b3SJeff Dike * exiting when profiling is active. 15652c653b3SJeff Dike */ 15752c653b3SJeff Dike change_sig(SIGPROF, 0); 15852c653b3SJeff Dike 159ba180fd4SJeff Dike /* 160ba180fd4SJeff Dike * This signal stuff used to be in the reboot case. However, 16152c653b3SJeff Dike * sometimes a SIGVTALRM can come in when we're halting (reproducably 16252c653b3SJeff Dike * when writing out gcov information, presumably because that takes 16352c653b3SJeff Dike * some time) and cause a segfault. 16452c653b3SJeff Dike */ 16552c653b3SJeff Dike 16661b63c55SJeff Dike /* stop timers and set SIGVTALRM to be ignored */ 16752c653b3SJeff Dike disable_timer(); 16852c653b3SJeff Dike 16952c653b3SJeff Dike /* disable SIGIO for the fds and set SIGIO to be ignored */ 17052c653b3SJeff Dike err = deactivate_all_fds(); 17152c653b3SJeff Dike if (err) 17252c653b3SJeff Dike printf("deactivate_all_fds failed, errno = %d\n", -err); 17352c653b3SJeff Dike 174ba180fd4SJeff Dike /* 175ba180fd4SJeff Dike * Let any pending signals fire now. This ensures 17652c653b3SJeff Dike * that they won't be delivered after the exec, when 17752c653b3SJeff Dike * they are definitely not expected. 17852c653b3SJeff Dike */ 17952c653b3SJeff Dike unblock_signals(); 18052c653b3SJeff Dike 18152c653b3SJeff Dike /* Reboot */ 18252c653b3SJeff Dike if (ret) { 18352c653b3SJeff Dike printf("\n"); 18452c653b3SJeff Dike execvp(new_argv[0], new_argv); 18552c653b3SJeff Dike perror("Failed to exec kernel"); 18652c653b3SJeff Dike ret = 1; 18752c653b3SJeff Dike } 18852c653b3SJeff Dike printf("\n"); 189a5ed1ffaSJeff Dike return uml_exitcode; 19052c653b3SJeff Dike } 19152c653b3SJeff Dike 19252c653b3SJeff Dike extern void *__real_malloc(int); 19352c653b3SJeff Dike 19452c653b3SJeff Dike void *__wrap_malloc(int size) 19552c653b3SJeff Dike { 19652c653b3SJeff Dike void *ret; 19752c653b3SJeff Dike 1986aa802ceSJeff Dike if (!kmalloc_ok) 199a5ed1ffaSJeff Dike return __real_malloc(size); 200c539ab73SJeff Dike else if (size <= UM_KERN_PAGE_SIZE) 201c539ab73SJeff Dike /* finding contiguous pages can be hard*/ 202*43f5b308SJeff Dike ret = uml_kmalloc(size, UM_GFP_KERNEL); 203e4c4bf99SJeff Dike else ret = vmalloc(size); 20452c653b3SJeff Dike 205ba180fd4SJeff Dike /* 206ba180fd4SJeff Dike * glibc people insist that if malloc fails, errno should be 20752c653b3SJeff Dike * set by malloc as well. So we do. 20852c653b3SJeff Dike */ 20952c653b3SJeff Dike if (ret == NULL) 21052c653b3SJeff Dike errno = ENOMEM; 21152c653b3SJeff Dike 212a5ed1ffaSJeff Dike return ret; 21352c653b3SJeff Dike } 21452c653b3SJeff Dike 21552c653b3SJeff Dike void *__wrap_calloc(int n, int size) 21652c653b3SJeff Dike { 21752c653b3SJeff Dike void *ptr = __wrap_malloc(n * size); 21852c653b3SJeff Dike 219a5ed1ffaSJeff Dike if (ptr == NULL) 220a5ed1ffaSJeff Dike return NULL; 22152c653b3SJeff Dike memset(ptr, 0, n * size); 222a5ed1ffaSJeff Dike return ptr; 22352c653b3SJeff Dike } 22452c653b3SJeff Dike 22552c653b3SJeff Dike extern void __real_free(void *); 22652c653b3SJeff Dike 22752c653b3SJeff Dike extern unsigned long high_physmem; 22852c653b3SJeff Dike 22952c653b3SJeff Dike void __wrap_free(void *ptr) 23052c653b3SJeff Dike { 23152c653b3SJeff Dike unsigned long addr = (unsigned long) ptr; 23252c653b3SJeff Dike 233ba180fd4SJeff Dike /* 234ba180fd4SJeff Dike * We need to know how the allocation happened, so it can be correctly 23552c653b3SJeff Dike * freed. This is done by seeing what region of memory the pointer is 23652c653b3SJeff Dike * in - 23752c653b3SJeff Dike * physical memory - kmalloc/kfree 23852c653b3SJeff Dike * kernel virtual memory - vmalloc/vfree 23952c653b3SJeff Dike * anywhere else - malloc/free 24052c653b3SJeff Dike * If kmalloc is not yet possible, then either high_physmem and/or 24152c653b3SJeff Dike * end_vm are still 0 (as at startup), in which case we call free, or 24252c653b3SJeff Dike * we have set them, but anyway addr has not been allocated from those 24352c653b3SJeff Dike * areas. So, in both cases __real_free is called. 24452c653b3SJeff Dike * 24552c653b3SJeff Dike * CAN_KMALLOC is checked because it would be bad to free a buffer 24652c653b3SJeff Dike * with kmalloc/vmalloc after they have been turned off during 24752c653b3SJeff Dike * shutdown. 24852c653b3SJeff Dike * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so 24952c653b3SJeff Dike * there is a possibility for memory leaks. 25052c653b3SJeff Dike */ 25152c653b3SJeff Dike 25252c653b3SJeff Dike if ((addr >= uml_physmem) && (addr < high_physmem)) { 2536aa802ceSJeff Dike if (kmalloc_ok) 25452c653b3SJeff Dike kfree(ptr); 25552c653b3SJeff Dike } 25652c653b3SJeff Dike else if ((addr >= start_vm) && (addr < end_vm)) { 2576aa802ceSJeff Dike if (kmalloc_ok) 25852c653b3SJeff Dike vfree(ptr); 25952c653b3SJeff Dike } 26052c653b3SJeff Dike else __real_free(ptr); 26152c653b3SJeff Dike } 262