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> 1437185b33SAl Viro #include <as-layout.h> 1537185b33SAl Viro #include <init.h> 1637185b33SAl Viro #include <kern_util.h> 1737185b33SAl Viro #include <os.h> 1837185b33SAl Viro #include <um_malloc.h> 19847d3abcSTiwei Bie #include "internal.h" 2052c653b3SJeff Dike 2152c653b3SJeff Dike #define PGD_BOUND (4 * 1024 * 1024) 2252c653b3SJeff Dike #define STACKSIZE (8 * 1024 * 1024) 2352c653b3SJeff Dike #define THREAD_NAME_LEN (256) 2452c653b3SJeff Dike 25b743ac54SRichard Weinberger long elf_aux_hwcap; 26b743ac54SRichard Weinberger 2752c653b3SJeff Dike static void set_stklim(void) 2852c653b3SJeff Dike { 2952c653b3SJeff Dike struct rlimit lim; 3052c653b3SJeff Dike 3152c653b3SJeff Dike if (getrlimit(RLIMIT_STACK, &lim) < 0) { 3252c653b3SJeff Dike perror("getrlimit"); 3352c653b3SJeff Dike exit(1); 3452c653b3SJeff Dike } 3552c653b3SJeff Dike if ((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)) { 3652c653b3SJeff Dike lim.rlim_cur = STACKSIZE; 3752c653b3SJeff Dike if (setrlimit(RLIMIT_STACK, &lim) < 0) { 3852c653b3SJeff Dike perror("setrlimit"); 3952c653b3SJeff Dike exit(1); 4052c653b3SJeff Dike } 4152c653b3SJeff Dike } 4252c653b3SJeff Dike } 4352c653b3SJeff Dike 4452c653b3SJeff Dike static void last_ditch_exit(int sig) 4552c653b3SJeff Dike { 4652c653b3SJeff Dike uml_cleanup(); 4752c653b3SJeff Dike exit(1); 4852c653b3SJeff Dike } 4952c653b3SJeff Dike 504b84c69bSJeff Dike static void install_fatal_handler(int sig) 514b84c69bSJeff Dike { 524b84c69bSJeff Dike struct sigaction action; 534b84c69bSJeff Dike 544b84c69bSJeff Dike /* All signals are enabled in this handler ... */ 554b84c69bSJeff Dike sigemptyset(&action.sa_mask); 564b84c69bSJeff Dike 57ba180fd4SJeff Dike /* 58ba180fd4SJeff Dike * ... including the signal being handled, plus we want the 594b84c69bSJeff Dike * handler reset to the default behavior, so that if an exit 604b84c69bSJeff Dike * handler is hanging for some reason, the UML will just die 614b84c69bSJeff Dike * after this signal is sent a second time. 624b84c69bSJeff Dike */ 634b84c69bSJeff Dike action.sa_flags = SA_RESETHAND | SA_NODEFER; 644b84c69bSJeff Dike action.sa_restorer = NULL; 654b84c69bSJeff Dike action.sa_handler = last_ditch_exit; 664b84c69bSJeff Dike if (sigaction(sig, &action, NULL) < 0) { 670936d4f3SMasami Hiramatsu os_warn("failed to install handler for signal %d " 680936d4f3SMasami Hiramatsu "- errno = %d\n", sig, errno); 694b84c69bSJeff Dike exit(1); 704b84c69bSJeff Dike } 714b84c69bSJeff Dike } 724b84c69bSJeff Dike 730ce451acSRichard Weinberger #define UML_LIB_PATH ":" OS_LIB_PATH "/uml" 74cb98cdcdSMattia Dongili 75cb98cdcdSMattia Dongili static void setup_env_path(void) 76cb98cdcdSMattia Dongili { 77cb98cdcdSMattia Dongili char *new_path = NULL; 78cb98cdcdSMattia Dongili char *old_path = NULL; 79cb98cdcdSMattia Dongili int path_len = 0; 80cb98cdcdSMattia Dongili 81cb98cdcdSMattia Dongili old_path = getenv("PATH"); 82ba180fd4SJeff Dike /* 83ba180fd4SJeff Dike * if no PATH variable is set or it has an empty value 84cb98cdcdSMattia Dongili * just use the default + /usr/lib/uml 85cb98cdcdSMattia Dongili */ 86cb98cdcdSMattia Dongili if (!old_path || (path_len = strlen(old_path)) == 0) { 87c9a3072dSWANG Cong if (putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH)) 88c9a3072dSWANG Cong perror("couldn't putenv"); 89cb98cdcdSMattia Dongili return; 90cb98cdcdSMattia Dongili } 91cb98cdcdSMattia Dongili 92cb98cdcdSMattia Dongili /* append /usr/lib/uml to the existing path */ 93cb98cdcdSMattia Dongili path_len += strlen("PATH=" UML_LIB_PATH) + 1; 94cb98cdcdSMattia Dongili new_path = malloc(path_len); 95cb98cdcdSMattia Dongili if (!new_path) { 96c9a3072dSWANG Cong perror("couldn't malloc to set a new PATH"); 97cb98cdcdSMattia Dongili return; 98cb98cdcdSMattia Dongili } 99cb98cdcdSMattia Dongili snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path); 100c9a3072dSWANG Cong if (putenv(new_path)) { 101c9a3072dSWANG Cong perror("couldn't putenv to set a new PATH"); 102c9a3072dSWANG Cong free(new_path); 103c9a3072dSWANG Cong } 104cb98cdcdSMattia Dongili } 105cb98cdcdSMattia Dongili 10636e45463SJeff Dike int __init main(int argc, char **argv, char **envp) 10752c653b3SJeff Dike { 10852c653b3SJeff Dike char **new_argv; 10952c653b3SJeff Dike int ret, i, err; 11052c653b3SJeff Dike 11152c653b3SJeff Dike set_stklim(); 11252c653b3SJeff Dike 113cb98cdcdSMattia Dongili setup_env_path(); 114cb98cdcdSMattia Dongili 11525012721SRichard Weinberger setsid(); 11625012721SRichard Weinberger 11752c653b3SJeff Dike new_argv = malloc((argc + 1) * sizeof(char *)); 11852c653b3SJeff Dike if (new_argv == NULL) { 11952c653b3SJeff Dike perror("Mallocing argv"); 12052c653b3SJeff Dike exit(1); 12152c653b3SJeff Dike } 12252c653b3SJeff Dike for (i = 0; i < argc; i++) { 12352c653b3SJeff Dike new_argv[i] = strdup(argv[i]); 12452c653b3SJeff Dike if (new_argv[i] == NULL) { 12552c653b3SJeff Dike perror("Mallocing an arg"); 12652c653b3SJeff Dike exit(1); 12752c653b3SJeff Dike } 12852c653b3SJeff Dike } 12952c653b3SJeff Dike new_argv[argc] = NULL; 13052c653b3SJeff Dike 131ba180fd4SJeff Dike /* 132ba180fd4SJeff Dike * Allow these signals to bring down a UML if all other 1334b84c69bSJeff Dike * methods of control fail. 1344b84c69bSJeff Dike */ 1354b84c69bSJeff Dike install_fatal_handler(SIGINT); 1364b84c69bSJeff Dike install_fatal_handler(SIGTERM); 13752c653b3SJeff Dike 138b743ac54SRichard Weinberger #ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA 13952c653b3SJeff Dike scan_elf_aux(envp); 140b743ac54SRichard Weinberger #endif 14152c653b3SJeff Dike 1429fcb663bSAnton Ivanov change_sig(SIGPIPE, 0); 14352c653b3SJeff Dike ret = linux_main(argc, argv); 14452c653b3SJeff Dike 145ba180fd4SJeff Dike /* 146ba180fd4SJeff Dike * Disable SIGPROF - I have no idea why libc doesn't do this or turn 14752c653b3SJeff Dike * off the profiling time, but UML dies with a SIGPROF just before 14852c653b3SJeff Dike * exiting when profiling is active. 14952c653b3SJeff Dike */ 15052c653b3SJeff Dike change_sig(SIGPROF, 0); 15152c653b3SJeff Dike 152ba180fd4SJeff Dike /* 153ba180fd4SJeff Dike * This signal stuff used to be in the reboot case. However, 1542eb5f31bSAnton Ivanov * sometimes a timer signal can come in when we're halting (reproducably 15552c653b3SJeff Dike * when writing out gcov information, presumably because that takes 15652c653b3SJeff Dike * some time) and cause a segfault. 15752c653b3SJeff Dike */ 15852c653b3SJeff Dike 1592eb5f31bSAnton Ivanov /* stop timers and set timer signal to be ignored */ 1602eb5f31bSAnton Ivanov os_timer_disable(); 16152c653b3SJeff Dike 16252c653b3SJeff Dike /* disable SIGIO for the fds and set SIGIO to be ignored */ 16352c653b3SJeff Dike err = deactivate_all_fds(); 16452c653b3SJeff Dike if (err) 1650936d4f3SMasami Hiramatsu os_warn("deactivate_all_fds failed, errno = %d\n", -err); 16652c653b3SJeff Dike 167ba180fd4SJeff Dike /* 168ba180fd4SJeff Dike * Let any pending signals fire now. This ensures 16952c653b3SJeff Dike * that they won't be delivered after the exec, when 17052c653b3SJeff Dike * they are definitely not expected. 17152c653b3SJeff Dike */ 1725c1f33e2SJohannes Berg unblock_signals(); 17352c653b3SJeff Dike 174d3878bb8SMasami Hiramatsu os_info("\n"); 17552c653b3SJeff Dike /* Reboot */ 17652c653b3SJeff Dike if (ret) { 17752c653b3SJeff Dike execvp(new_argv[0], new_argv); 17852c653b3SJeff Dike perror("Failed to exec kernel"); 17952c653b3SJeff Dike ret = 1; 18052c653b3SJeff Dike } 181a5ed1ffaSJeff Dike return uml_exitcode; 18252c653b3SJeff Dike } 18352c653b3SJeff Dike 18452c653b3SJeff Dike extern void *__real_malloc(int); 185*855f6e18STiwei Bie extern void __real_free(void *); 18652c653b3SJeff Dike 18749ff7d87STiwei Bie /* workaround for -Wmissing-prototypes warnings */ 18849ff7d87STiwei Bie void *__wrap_malloc(int size); 18949ff7d87STiwei Bie void *__wrap_calloc(int n, int size); 19049ff7d87STiwei Bie void __wrap_free(void *ptr); 19149ff7d87STiwei Bie 19252c653b3SJeff Dike void *__wrap_malloc(int size) 19352c653b3SJeff Dike { 19452c653b3SJeff Dike void *ret; 19552c653b3SJeff Dike 1966aa802ceSJeff Dike if (!kmalloc_ok) 197a5ed1ffaSJeff Dike return __real_malloc(size); 198c539ab73SJeff Dike else if (size <= UM_KERN_PAGE_SIZE) 199c539ab73SJeff Dike /* finding contiguous pages can be hard*/ 20043f5b308SJeff Dike ret = uml_kmalloc(size, UM_GFP_KERNEL); 201e4c4bf99SJeff Dike else ret = vmalloc(size); 20252c653b3SJeff Dike 203ba180fd4SJeff Dike /* 204ba180fd4SJeff Dike * glibc people insist that if malloc fails, errno should be 20552c653b3SJeff Dike * set by malloc as well. So we do. 20652c653b3SJeff Dike */ 20752c653b3SJeff Dike if (ret == NULL) 20852c653b3SJeff Dike errno = ENOMEM; 20952c653b3SJeff Dike 210a5ed1ffaSJeff Dike return ret; 21152c653b3SJeff Dike } 21252c653b3SJeff Dike 21352c653b3SJeff Dike void *__wrap_calloc(int n, int size) 21452c653b3SJeff Dike { 21552c653b3SJeff Dike void *ptr = __wrap_malloc(n * size); 21652c653b3SJeff Dike 217a5ed1ffaSJeff Dike if (ptr == NULL) 218a5ed1ffaSJeff Dike return NULL; 21952c653b3SJeff Dike memset(ptr, 0, n * size); 220a5ed1ffaSJeff Dike return ptr; 22152c653b3SJeff Dike } 22252c653b3SJeff Dike 22352c653b3SJeff Dike void __wrap_free(void *ptr) 22452c653b3SJeff Dike { 22552c653b3SJeff Dike unsigned long addr = (unsigned long) ptr; 22652c653b3SJeff Dike 227ba180fd4SJeff Dike /* 228ba180fd4SJeff Dike * We need to know how the allocation happened, so it can be correctly 22952c653b3SJeff Dike * freed. This is done by seeing what region of memory the pointer is 23052c653b3SJeff Dike * in - 23152c653b3SJeff Dike * physical memory - kmalloc/kfree 23252c653b3SJeff Dike * kernel virtual memory - vmalloc/vfree 23352c653b3SJeff Dike * anywhere else - malloc/free 23452c653b3SJeff Dike * If kmalloc is not yet possible, then either high_physmem and/or 23552c653b3SJeff Dike * end_vm are still 0 (as at startup), in which case we call free, or 23652c653b3SJeff Dike * we have set them, but anyway addr has not been allocated from those 23752c653b3SJeff Dike * areas. So, in both cases __real_free is called. 23852c653b3SJeff Dike * 23952c653b3SJeff Dike * CAN_KMALLOC is checked because it would be bad to free a buffer 24052c653b3SJeff Dike * with kmalloc/vmalloc after they have been turned off during 24152c653b3SJeff Dike * shutdown. 24252c653b3SJeff Dike * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so 24352c653b3SJeff Dike * there is a possibility for memory leaks. 24452c653b3SJeff Dike */ 24552c653b3SJeff Dike 24652c653b3SJeff Dike if ((addr >= uml_physmem) && (addr < high_physmem)) { 2476aa802ceSJeff Dike if (kmalloc_ok) 24852c653b3SJeff Dike kfree(ptr); 24952c653b3SJeff Dike } 25052c653b3SJeff Dike else if ((addr >= start_vm) && (addr < end_vm)) { 2516aa802ceSJeff Dike if (kmalloc_ok) 25252c653b3SJeff Dike vfree(ptr); 25352c653b3SJeff Dike } 25452c653b3SJeff Dike else __real_free(ptr); 25552c653b3SJeff Dike } 256