197870c34SAlex Dewar // SPDX-License-Identifier: GPL-2.0 252c653b3SJeff Dike /* 32eb5f31bSAnton Ivanov * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de) 4ba180fd4SJeff Dike * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 552c653b3SJeff Dike */ 652c653b3SJeff Dike 752c653b3SJeff Dike #include <stdio.h> 852c653b3SJeff Dike #include <stdlib.h> 9ba180fd4SJeff Dike #include <unistd.h> 1052c653b3SJeff Dike #include <errno.h> 11ba180fd4SJeff Dike #include <signal.h> 12ba180fd4SJeff Dike #include <string.h> 1352c653b3SJeff Dike #include <sys/resource.h> 1468b9883cSBenjamin Berg #include <sys/personality.h> 1537185b33SAl Viro #include <as-layout.h> 1637185b33SAl Viro #include <init.h> 1737185b33SAl Viro #include <kern_util.h> 1837185b33SAl Viro #include <os.h> 1937185b33SAl Viro #include <um_malloc.h> 20847d3abcSTiwei Bie #include "internal.h" 2152c653b3SJeff Dike 2252c653b3SJeff Dike #define STACKSIZE (8 * 1024 * 1024) 2352c653b3SJeff Dike 24b743ac54SRichard Weinberger long elf_aux_hwcap; 25b743ac54SRichard Weinberger 26f0c76bc8STiwei Bie static void __init set_stklim(void) 2752c653b3SJeff Dike { 2852c653b3SJeff Dike struct rlimit lim; 2952c653b3SJeff Dike 3052c653b3SJeff Dike if (getrlimit(RLIMIT_STACK, &lim) < 0) { 3152c653b3SJeff Dike perror("getrlimit"); 3252c653b3SJeff Dike exit(1); 3352c653b3SJeff Dike } 3452c653b3SJeff Dike if ((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)) { 3552c653b3SJeff Dike lim.rlim_cur = STACKSIZE; 3652c653b3SJeff Dike if (setrlimit(RLIMIT_STACK, &lim) < 0) { 3752c653b3SJeff Dike perror("setrlimit"); 3852c653b3SJeff Dike exit(1); 3952c653b3SJeff Dike } 4052c653b3SJeff Dike } 4152c653b3SJeff Dike } 4252c653b3SJeff Dike 4352c653b3SJeff Dike static void last_ditch_exit(int sig) 4452c653b3SJeff Dike { 4552c653b3SJeff Dike uml_cleanup(); 4652c653b3SJeff Dike exit(1); 4752c653b3SJeff Dike } 4852c653b3SJeff Dike 49c2fdfd77STiwei Bie static void __init install_fatal_handler(int sig) 504b84c69bSJeff Dike { 514b84c69bSJeff Dike struct sigaction action; 524b84c69bSJeff Dike 534b84c69bSJeff Dike /* All signals are enabled in this handler ... */ 544b84c69bSJeff Dike sigemptyset(&action.sa_mask); 554b84c69bSJeff Dike 56ba180fd4SJeff Dike /* 57ba180fd4SJeff Dike * ... including the signal being handled, plus we want the 584b84c69bSJeff Dike * handler reset to the default behavior, so that if an exit 594b84c69bSJeff Dike * handler is hanging for some reason, the UML will just die 604b84c69bSJeff Dike * after this signal is sent a second time. 614b84c69bSJeff Dike */ 624b84c69bSJeff Dike action.sa_flags = SA_RESETHAND | SA_NODEFER; 634b84c69bSJeff Dike action.sa_restorer = NULL; 644b84c69bSJeff Dike action.sa_handler = last_ditch_exit; 654b84c69bSJeff Dike if (sigaction(sig, &action, NULL) < 0) { 660936d4f3SMasami Hiramatsu os_warn("failed to install handler for signal %d " 670936d4f3SMasami Hiramatsu "- errno = %d\n", sig, errno); 684b84c69bSJeff Dike exit(1); 694b84c69bSJeff Dike } 704b84c69bSJeff Dike } 714b84c69bSJeff Dike 720ce451acSRichard Weinberger #define UML_LIB_PATH ":" OS_LIB_PATH "/uml" 73cb98cdcdSMattia Dongili 74*0c5258efSTiwei Bie static void __init setup_env_path(void) 75cb98cdcdSMattia Dongili { 76cb98cdcdSMattia Dongili char *new_path = NULL; 77cb98cdcdSMattia Dongili char *old_path = NULL; 78cb98cdcdSMattia Dongili int path_len = 0; 79cb98cdcdSMattia Dongili 80cb98cdcdSMattia Dongili old_path = getenv("PATH"); 81ba180fd4SJeff Dike /* 82ba180fd4SJeff Dike * if no PATH variable is set or it has an empty value 83cb98cdcdSMattia Dongili * just use the default + /usr/lib/uml 84cb98cdcdSMattia Dongili */ 85cb98cdcdSMattia Dongili if (!old_path || (path_len = strlen(old_path)) == 0) { 86c9a3072dSWANG Cong if (putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH)) 87c9a3072dSWANG Cong perror("couldn't putenv"); 88cb98cdcdSMattia Dongili return; 89cb98cdcdSMattia Dongili } 90cb98cdcdSMattia Dongili 91cb98cdcdSMattia Dongili /* append /usr/lib/uml to the existing path */ 92cb98cdcdSMattia Dongili path_len += strlen("PATH=" UML_LIB_PATH) + 1; 93cb98cdcdSMattia Dongili new_path = malloc(path_len); 94cb98cdcdSMattia Dongili if (!new_path) { 95c9a3072dSWANG Cong perror("couldn't malloc to set a new PATH"); 96cb98cdcdSMattia Dongili return; 97cb98cdcdSMattia Dongili } 98cb98cdcdSMattia Dongili snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path); 99c9a3072dSWANG Cong if (putenv(new_path)) { 100c9a3072dSWANG Cong perror("couldn't putenv to set a new PATH"); 101c9a3072dSWANG Cong free(new_path); 102c9a3072dSWANG Cong } 103cb98cdcdSMattia Dongili } 104cb98cdcdSMattia Dongili 10536e45463SJeff Dike int __init main(int argc, char **argv, char **envp) 10652c653b3SJeff Dike { 10752c653b3SJeff Dike char **new_argv; 10852c653b3SJeff Dike int ret, i, err; 10952c653b3SJeff Dike 11068b9883cSBenjamin Berg /* Disable randomization and re-exec if it was changed successfully */ 11168b9883cSBenjamin Berg ret = personality(PER_LINUX | ADDR_NO_RANDOMIZE); 11268b9883cSBenjamin Berg if (ret >= 0 && (ret & (PER_LINUX | ADDR_NO_RANDOMIZE)) != 113031acdcfSJohannes Berg (PER_LINUX | ADDR_NO_RANDOMIZE)) { 114d61ac4a7SJohannes Berg char buf[4096] = {}; 115031acdcfSJohannes Berg ssize_t ret; 116031acdcfSJohannes Berg 117031acdcfSJohannes Berg ret = readlink("/proc/self/exe", buf, sizeof(buf)); 118031acdcfSJohannes Berg if (ret < 0 || ret >= sizeof(buf)) { 119031acdcfSJohannes Berg perror("readlink failure"); 120031acdcfSJohannes Berg exit(1); 121031acdcfSJohannes Berg } 122031acdcfSJohannes Berg execve(buf, argv, envp); 123031acdcfSJohannes Berg } 12468b9883cSBenjamin Berg 12552c653b3SJeff Dike set_stklim(); 12652c653b3SJeff Dike 127cb98cdcdSMattia Dongili setup_env_path(); 128cb98cdcdSMattia Dongili 12925012721SRichard Weinberger setsid(); 13025012721SRichard Weinberger 13152c653b3SJeff Dike new_argv = malloc((argc + 1) * sizeof(char *)); 13252c653b3SJeff Dike if (new_argv == NULL) { 13352c653b3SJeff Dike perror("Mallocing argv"); 13452c653b3SJeff Dike exit(1); 13552c653b3SJeff Dike } 13652c653b3SJeff Dike for (i = 0; i < argc; i++) { 13752c653b3SJeff Dike new_argv[i] = strdup(argv[i]); 13852c653b3SJeff Dike if (new_argv[i] == NULL) { 13952c653b3SJeff Dike perror("Mallocing an arg"); 14052c653b3SJeff Dike exit(1); 14152c653b3SJeff Dike } 14252c653b3SJeff Dike } 14352c653b3SJeff Dike new_argv[argc] = NULL; 14452c653b3SJeff Dike 145ba180fd4SJeff Dike /* 146ba180fd4SJeff Dike * Allow these signals to bring down a UML if all other 1474b84c69bSJeff Dike * methods of control fail. 1484b84c69bSJeff Dike */ 1494b84c69bSJeff Dike install_fatal_handler(SIGINT); 1504b84c69bSJeff Dike install_fatal_handler(SIGTERM); 15152c653b3SJeff Dike 152b743ac54SRichard Weinberger #ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA 15352c653b3SJeff Dike scan_elf_aux(envp); 154b743ac54SRichard Weinberger #endif 15552c653b3SJeff Dike 1569fcb663bSAnton Ivanov change_sig(SIGPIPE, 0); 15768b9883cSBenjamin Berg ret = linux_main(argc, argv, envp); 15852c653b3SJeff Dike 159ba180fd4SJeff Dike /* 160ba180fd4SJeff Dike * Disable SIGPROF - I have no idea why libc doesn't do this or turn 16152c653b3SJeff Dike * off the profiling time, but UML dies with a SIGPROF just before 16252c653b3SJeff Dike * exiting when profiling is active. 16352c653b3SJeff Dike */ 16452c653b3SJeff Dike change_sig(SIGPROF, 0); 16552c653b3SJeff Dike 166ba180fd4SJeff Dike /* 167ba180fd4SJeff Dike * This signal stuff used to be in the reboot case. However, 1682eb5f31bSAnton Ivanov * sometimes a timer signal can come in when we're halting (reproducably 16952c653b3SJeff Dike * when writing out gcov information, presumably because that takes 17052c653b3SJeff Dike * some time) and cause a segfault. 17152c653b3SJeff Dike */ 17252c653b3SJeff Dike 1732eb5f31bSAnton Ivanov /* stop timers and set timer signal to be ignored */ 1742eb5f31bSAnton Ivanov os_timer_disable(); 17552c653b3SJeff Dike 17652c653b3SJeff Dike /* disable SIGIO for the fds and set SIGIO to be ignored */ 17752c653b3SJeff Dike err = deactivate_all_fds(); 17852c653b3SJeff Dike if (err) 1790936d4f3SMasami Hiramatsu os_warn("deactivate_all_fds failed, errno = %d\n", -err); 18052c653b3SJeff Dike 181ba180fd4SJeff Dike /* 182ba180fd4SJeff Dike * Let any pending signals fire now. This ensures 18352c653b3SJeff Dike * that they won't be delivered after the exec, when 18452c653b3SJeff Dike * they are definitely not expected. 18552c653b3SJeff Dike */ 1865c1f33e2SJohannes Berg unblock_signals(); 18752c653b3SJeff Dike 188d3878bb8SMasami Hiramatsu os_info("\n"); 18952c653b3SJeff Dike /* Reboot */ 19052c653b3SJeff Dike if (ret) { 19152c653b3SJeff Dike execvp(new_argv[0], new_argv); 19252c653b3SJeff Dike perror("Failed to exec kernel"); 19352c653b3SJeff Dike ret = 1; 19452c653b3SJeff Dike } 195a5ed1ffaSJeff Dike return uml_exitcode; 19652c653b3SJeff Dike } 19752c653b3SJeff Dike 19852c653b3SJeff Dike extern void *__real_malloc(int); 199855f6e18STiwei Bie extern void __real_free(void *); 20052c653b3SJeff Dike 20149ff7d87STiwei Bie /* workaround for -Wmissing-prototypes warnings */ 20249ff7d87STiwei Bie void *__wrap_malloc(int size); 20349ff7d87STiwei Bie void *__wrap_calloc(int n, int size); 20449ff7d87STiwei Bie void __wrap_free(void *ptr); 20549ff7d87STiwei Bie 20652c653b3SJeff Dike void *__wrap_malloc(int size) 20752c653b3SJeff Dike { 20852c653b3SJeff Dike void *ret; 20952c653b3SJeff Dike 2106aa802ceSJeff Dike if (!kmalloc_ok) 211a5ed1ffaSJeff Dike return __real_malloc(size); 212c539ab73SJeff Dike else if (size <= UM_KERN_PAGE_SIZE) 213c539ab73SJeff Dike /* finding contiguous pages can be hard*/ 21443f5b308SJeff Dike ret = uml_kmalloc(size, UM_GFP_KERNEL); 215e4c4bf99SJeff Dike else ret = vmalloc(size); 21652c653b3SJeff Dike 217ba180fd4SJeff Dike /* 218ba180fd4SJeff Dike * glibc people insist that if malloc fails, errno should be 21952c653b3SJeff Dike * set by malloc as well. So we do. 22052c653b3SJeff Dike */ 22152c653b3SJeff Dike if (ret == NULL) 22252c653b3SJeff Dike errno = ENOMEM; 22352c653b3SJeff Dike 224a5ed1ffaSJeff Dike return ret; 22552c653b3SJeff Dike } 22652c653b3SJeff Dike 22752c653b3SJeff Dike void *__wrap_calloc(int n, int size) 22852c653b3SJeff Dike { 22952c653b3SJeff Dike void *ptr = __wrap_malloc(n * size); 23052c653b3SJeff Dike 231a5ed1ffaSJeff Dike if (ptr == NULL) 232a5ed1ffaSJeff Dike return NULL; 23352c653b3SJeff Dike memset(ptr, 0, n * size); 234a5ed1ffaSJeff Dike return ptr; 23552c653b3SJeff Dike } 23652c653b3SJeff Dike 23752c653b3SJeff Dike void __wrap_free(void *ptr) 23852c653b3SJeff Dike { 23952c653b3SJeff Dike unsigned long addr = (unsigned long) ptr; 24052c653b3SJeff Dike 241ba180fd4SJeff Dike /* 242ba180fd4SJeff Dike * We need to know how the allocation happened, so it can be correctly 24352c653b3SJeff Dike * freed. This is done by seeing what region of memory the pointer is 24452c653b3SJeff Dike * in - 24552c653b3SJeff Dike * physical memory - kmalloc/kfree 24652c653b3SJeff Dike * kernel virtual memory - vmalloc/vfree 24752c653b3SJeff Dike * anywhere else - malloc/free 24852c653b3SJeff Dike * If kmalloc is not yet possible, then either high_physmem and/or 24952c653b3SJeff Dike * end_vm are still 0 (as at startup), in which case we call free, or 25052c653b3SJeff Dike * we have set them, but anyway addr has not been allocated from those 25152c653b3SJeff Dike * areas. So, in both cases __real_free is called. 25252c653b3SJeff Dike * 25352c653b3SJeff Dike * CAN_KMALLOC is checked because it would be bad to free a buffer 25452c653b3SJeff Dike * with kmalloc/vmalloc after they have been turned off during 25552c653b3SJeff Dike * shutdown. 25652c653b3SJeff Dike * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so 25752c653b3SJeff Dike * there is a possibility for memory leaks. 25852c653b3SJeff Dike */ 25952c653b3SJeff Dike 26052c653b3SJeff Dike if ((addr >= uml_physmem) && (addr < high_physmem)) { 2616aa802ceSJeff Dike if (kmalloc_ok) 26252c653b3SJeff Dike kfree(ptr); 26352c653b3SJeff Dike } 26452c653b3SJeff Dike else if ((addr >= start_vm) && (addr < end_vm)) { 2656aa802ceSJeff Dike if (kmalloc_ok) 26652c653b3SJeff Dike vfree(ptr); 26752c653b3SJeff Dike } 26852c653b3SJeff Dike else __real_free(ptr); 26952c653b3SJeff Dike } 270