18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de) 48c2ecf20Sopenharmony_ci * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#include <stdio.h> 88c2ecf20Sopenharmony_ci#include <stdlib.h> 98c2ecf20Sopenharmony_ci#include <unistd.h> 108c2ecf20Sopenharmony_ci#include <errno.h> 118c2ecf20Sopenharmony_ci#include <signal.h> 128c2ecf20Sopenharmony_ci#include <string.h> 138c2ecf20Sopenharmony_ci#include <sys/resource.h> 148c2ecf20Sopenharmony_ci#include <as-layout.h> 158c2ecf20Sopenharmony_ci#include <init.h> 168c2ecf20Sopenharmony_ci#include <kern_util.h> 178c2ecf20Sopenharmony_ci#include <os.h> 188c2ecf20Sopenharmony_ci#include <um_malloc.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#define PGD_BOUND (4 * 1024 * 1024) 218c2ecf20Sopenharmony_ci#define STACKSIZE (8 * 1024 * 1024) 228c2ecf20Sopenharmony_ci#define THREAD_NAME_LEN (256) 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_cilong elf_aux_hwcap; 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_cistatic void set_stklim(void) 278c2ecf20Sopenharmony_ci{ 288c2ecf20Sopenharmony_ci struct rlimit lim; 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci if (getrlimit(RLIMIT_STACK, &lim) < 0) { 318c2ecf20Sopenharmony_ci perror("getrlimit"); 328c2ecf20Sopenharmony_ci exit(1); 338c2ecf20Sopenharmony_ci } 348c2ecf20Sopenharmony_ci if ((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)) { 358c2ecf20Sopenharmony_ci lim.rlim_cur = STACKSIZE; 368c2ecf20Sopenharmony_ci if (setrlimit(RLIMIT_STACK, &lim) < 0) { 378c2ecf20Sopenharmony_ci perror("setrlimit"); 388c2ecf20Sopenharmony_ci exit(1); 398c2ecf20Sopenharmony_ci } 408c2ecf20Sopenharmony_ci } 418c2ecf20Sopenharmony_ci} 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_cistatic void last_ditch_exit(int sig) 448c2ecf20Sopenharmony_ci{ 458c2ecf20Sopenharmony_ci uml_cleanup(); 468c2ecf20Sopenharmony_ci exit(1); 478c2ecf20Sopenharmony_ci} 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_cistatic void install_fatal_handler(int sig) 508c2ecf20Sopenharmony_ci{ 518c2ecf20Sopenharmony_ci struct sigaction action; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci /* All signals are enabled in this handler ... */ 548c2ecf20Sopenharmony_ci sigemptyset(&action.sa_mask); 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci /* 578c2ecf20Sopenharmony_ci * ... including the signal being handled, plus we want the 588c2ecf20Sopenharmony_ci * handler reset to the default behavior, so that if an exit 598c2ecf20Sopenharmony_ci * handler is hanging for some reason, the UML will just die 608c2ecf20Sopenharmony_ci * after this signal is sent a second time. 618c2ecf20Sopenharmony_ci */ 628c2ecf20Sopenharmony_ci action.sa_flags = SA_RESETHAND | SA_NODEFER; 638c2ecf20Sopenharmony_ci action.sa_restorer = NULL; 648c2ecf20Sopenharmony_ci action.sa_handler = last_ditch_exit; 658c2ecf20Sopenharmony_ci if (sigaction(sig, &action, NULL) < 0) { 668c2ecf20Sopenharmony_ci os_warn("failed to install handler for signal %d " 678c2ecf20Sopenharmony_ci "- errno = %d\n", sig, errno); 688c2ecf20Sopenharmony_ci exit(1); 698c2ecf20Sopenharmony_ci } 708c2ecf20Sopenharmony_ci} 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci#define UML_LIB_PATH ":" OS_LIB_PATH "/uml" 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_cistatic void setup_env_path(void) 758c2ecf20Sopenharmony_ci{ 768c2ecf20Sopenharmony_ci char *new_path = NULL; 778c2ecf20Sopenharmony_ci char *old_path = NULL; 788c2ecf20Sopenharmony_ci int path_len = 0; 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci old_path = getenv("PATH"); 818c2ecf20Sopenharmony_ci /* 828c2ecf20Sopenharmony_ci * if no PATH variable is set or it has an empty value 838c2ecf20Sopenharmony_ci * just use the default + /usr/lib/uml 848c2ecf20Sopenharmony_ci */ 858c2ecf20Sopenharmony_ci if (!old_path || (path_len = strlen(old_path)) == 0) { 868c2ecf20Sopenharmony_ci if (putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH)) 878c2ecf20Sopenharmony_ci perror("couldn't putenv"); 888c2ecf20Sopenharmony_ci return; 898c2ecf20Sopenharmony_ci } 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci /* append /usr/lib/uml to the existing path */ 928c2ecf20Sopenharmony_ci path_len += strlen("PATH=" UML_LIB_PATH) + 1; 938c2ecf20Sopenharmony_ci new_path = malloc(path_len); 948c2ecf20Sopenharmony_ci if (!new_path) { 958c2ecf20Sopenharmony_ci perror("couldn't malloc to set a new PATH"); 968c2ecf20Sopenharmony_ci return; 978c2ecf20Sopenharmony_ci } 988c2ecf20Sopenharmony_ci snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path); 998c2ecf20Sopenharmony_ci if (putenv(new_path)) { 1008c2ecf20Sopenharmony_ci perror("couldn't putenv to set a new PATH"); 1018c2ecf20Sopenharmony_ci free(new_path); 1028c2ecf20Sopenharmony_ci } 1038c2ecf20Sopenharmony_ci} 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ciextern void scan_elf_aux( char **envp); 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ciint __init main(int argc, char **argv, char **envp) 1088c2ecf20Sopenharmony_ci{ 1098c2ecf20Sopenharmony_ci char **new_argv; 1108c2ecf20Sopenharmony_ci int ret, i, err; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci set_stklim(); 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci setup_env_path(); 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci setsid(); 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci new_argv = malloc((argc + 1) * sizeof(char *)); 1198c2ecf20Sopenharmony_ci if (new_argv == NULL) { 1208c2ecf20Sopenharmony_ci perror("Mallocing argv"); 1218c2ecf20Sopenharmony_ci exit(1); 1228c2ecf20Sopenharmony_ci } 1238c2ecf20Sopenharmony_ci for (i = 0; i < argc; i++) { 1248c2ecf20Sopenharmony_ci new_argv[i] = strdup(argv[i]); 1258c2ecf20Sopenharmony_ci if (new_argv[i] == NULL) { 1268c2ecf20Sopenharmony_ci perror("Mallocing an arg"); 1278c2ecf20Sopenharmony_ci exit(1); 1288c2ecf20Sopenharmony_ci } 1298c2ecf20Sopenharmony_ci } 1308c2ecf20Sopenharmony_ci new_argv[argc] = NULL; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci /* 1338c2ecf20Sopenharmony_ci * Allow these signals to bring down a UML if all other 1348c2ecf20Sopenharmony_ci * methods of control fail. 1358c2ecf20Sopenharmony_ci */ 1368c2ecf20Sopenharmony_ci install_fatal_handler(SIGINT); 1378c2ecf20Sopenharmony_ci install_fatal_handler(SIGTERM); 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci#ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA 1408c2ecf20Sopenharmony_ci scan_elf_aux(envp); 1418c2ecf20Sopenharmony_ci#endif 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci change_sig(SIGPIPE, 0); 1448c2ecf20Sopenharmony_ci ret = linux_main(argc, argv); 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci /* 1478c2ecf20Sopenharmony_ci * Disable SIGPROF - I have no idea why libc doesn't do this or turn 1488c2ecf20Sopenharmony_ci * off the profiling time, but UML dies with a SIGPROF just before 1498c2ecf20Sopenharmony_ci * exiting when profiling is active. 1508c2ecf20Sopenharmony_ci */ 1518c2ecf20Sopenharmony_ci change_sig(SIGPROF, 0); 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci /* 1548c2ecf20Sopenharmony_ci * This signal stuff used to be in the reboot case. However, 1558c2ecf20Sopenharmony_ci * sometimes a timer signal can come in when we're halting (reproducably 1568c2ecf20Sopenharmony_ci * when writing out gcov information, presumably because that takes 1578c2ecf20Sopenharmony_ci * some time) and cause a segfault. 1588c2ecf20Sopenharmony_ci */ 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci /* stop timers and set timer signal to be ignored */ 1618c2ecf20Sopenharmony_ci os_timer_disable(); 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci /* disable SIGIO for the fds and set SIGIO to be ignored */ 1648c2ecf20Sopenharmony_ci err = deactivate_all_fds(); 1658c2ecf20Sopenharmony_ci if (err) 1668c2ecf20Sopenharmony_ci os_warn("deactivate_all_fds failed, errno = %d\n", -err); 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci /* 1698c2ecf20Sopenharmony_ci * Let any pending signals fire now. This ensures 1708c2ecf20Sopenharmony_ci * that they won't be delivered after the exec, when 1718c2ecf20Sopenharmony_ci * they are definitely not expected. 1728c2ecf20Sopenharmony_ci */ 1738c2ecf20Sopenharmony_ci unblock_signals(); 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci os_info("\n"); 1768c2ecf20Sopenharmony_ci /* Reboot */ 1778c2ecf20Sopenharmony_ci if (ret) { 1788c2ecf20Sopenharmony_ci execvp(new_argv[0], new_argv); 1798c2ecf20Sopenharmony_ci perror("Failed to exec kernel"); 1808c2ecf20Sopenharmony_ci ret = 1; 1818c2ecf20Sopenharmony_ci } 1828c2ecf20Sopenharmony_ci return uml_exitcode; 1838c2ecf20Sopenharmony_ci} 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ciextern void *__real_malloc(int); 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_civoid *__wrap_malloc(int size) 1888c2ecf20Sopenharmony_ci{ 1898c2ecf20Sopenharmony_ci void *ret; 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci if (!kmalloc_ok) 1928c2ecf20Sopenharmony_ci return __real_malloc(size); 1938c2ecf20Sopenharmony_ci else if (size <= UM_KERN_PAGE_SIZE) 1948c2ecf20Sopenharmony_ci /* finding contiguous pages can be hard*/ 1958c2ecf20Sopenharmony_ci ret = uml_kmalloc(size, UM_GFP_KERNEL); 1968c2ecf20Sopenharmony_ci else ret = vmalloc(size); 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci /* 1998c2ecf20Sopenharmony_ci * glibc people insist that if malloc fails, errno should be 2008c2ecf20Sopenharmony_ci * set by malloc as well. So we do. 2018c2ecf20Sopenharmony_ci */ 2028c2ecf20Sopenharmony_ci if (ret == NULL) 2038c2ecf20Sopenharmony_ci errno = ENOMEM; 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci return ret; 2068c2ecf20Sopenharmony_ci} 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_civoid *__wrap_calloc(int n, int size) 2098c2ecf20Sopenharmony_ci{ 2108c2ecf20Sopenharmony_ci void *ptr = __wrap_malloc(n * size); 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci if (ptr == NULL) 2138c2ecf20Sopenharmony_ci return NULL; 2148c2ecf20Sopenharmony_ci memset(ptr, 0, n * size); 2158c2ecf20Sopenharmony_ci return ptr; 2168c2ecf20Sopenharmony_ci} 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ciextern void __real_free(void *); 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ciextern unsigned long high_physmem; 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_civoid __wrap_free(void *ptr) 2238c2ecf20Sopenharmony_ci{ 2248c2ecf20Sopenharmony_ci unsigned long addr = (unsigned long) ptr; 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci /* 2278c2ecf20Sopenharmony_ci * We need to know how the allocation happened, so it can be correctly 2288c2ecf20Sopenharmony_ci * freed. This is done by seeing what region of memory the pointer is 2298c2ecf20Sopenharmony_ci * in - 2308c2ecf20Sopenharmony_ci * physical memory - kmalloc/kfree 2318c2ecf20Sopenharmony_ci * kernel virtual memory - vmalloc/vfree 2328c2ecf20Sopenharmony_ci * anywhere else - malloc/free 2338c2ecf20Sopenharmony_ci * If kmalloc is not yet possible, then either high_physmem and/or 2348c2ecf20Sopenharmony_ci * end_vm are still 0 (as at startup), in which case we call free, or 2358c2ecf20Sopenharmony_ci * we have set them, but anyway addr has not been allocated from those 2368c2ecf20Sopenharmony_ci * areas. So, in both cases __real_free is called. 2378c2ecf20Sopenharmony_ci * 2388c2ecf20Sopenharmony_ci * CAN_KMALLOC is checked because it would be bad to free a buffer 2398c2ecf20Sopenharmony_ci * with kmalloc/vmalloc after they have been turned off during 2408c2ecf20Sopenharmony_ci * shutdown. 2418c2ecf20Sopenharmony_ci * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so 2428c2ecf20Sopenharmony_ci * there is a possibility for memory leaks. 2438c2ecf20Sopenharmony_ci */ 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci if ((addr >= uml_physmem) && (addr < high_physmem)) { 2468c2ecf20Sopenharmony_ci if (kmalloc_ok) 2478c2ecf20Sopenharmony_ci kfree(ptr); 2488c2ecf20Sopenharmony_ci } 2498c2ecf20Sopenharmony_ci else if ((addr >= start_vm) && (addr < end_vm)) { 2508c2ecf20Sopenharmony_ci if (kmalloc_ok) 2518c2ecf20Sopenharmony_ci vfree(ptr); 2528c2ecf20Sopenharmony_ci } 2538c2ecf20Sopenharmony_ci else __real_free(ptr); 2548c2ecf20Sopenharmony_ci} 255