162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de) 462306a36Sopenharmony_ci * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 562306a36Sopenharmony_ci */ 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci#include <stdio.h> 862306a36Sopenharmony_ci#include <stdlib.h> 962306a36Sopenharmony_ci#include <unistd.h> 1062306a36Sopenharmony_ci#include <errno.h> 1162306a36Sopenharmony_ci#include <signal.h> 1262306a36Sopenharmony_ci#include <string.h> 1362306a36Sopenharmony_ci#include <sys/resource.h> 1462306a36Sopenharmony_ci#include <as-layout.h> 1562306a36Sopenharmony_ci#include <init.h> 1662306a36Sopenharmony_ci#include <kern_util.h> 1762306a36Sopenharmony_ci#include <os.h> 1862306a36Sopenharmony_ci#include <um_malloc.h> 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ci#define PGD_BOUND (4 * 1024 * 1024) 2162306a36Sopenharmony_ci#define STACKSIZE (8 * 1024 * 1024) 2262306a36Sopenharmony_ci#define THREAD_NAME_LEN (256) 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_cilong elf_aux_hwcap; 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_cistatic void set_stklim(void) 2762306a36Sopenharmony_ci{ 2862306a36Sopenharmony_ci struct rlimit lim; 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ci if (getrlimit(RLIMIT_STACK, &lim) < 0) { 3162306a36Sopenharmony_ci perror("getrlimit"); 3262306a36Sopenharmony_ci exit(1); 3362306a36Sopenharmony_ci } 3462306a36Sopenharmony_ci if ((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)) { 3562306a36Sopenharmony_ci lim.rlim_cur = STACKSIZE; 3662306a36Sopenharmony_ci if (setrlimit(RLIMIT_STACK, &lim) < 0) { 3762306a36Sopenharmony_ci perror("setrlimit"); 3862306a36Sopenharmony_ci exit(1); 3962306a36Sopenharmony_ci } 4062306a36Sopenharmony_ci } 4162306a36Sopenharmony_ci} 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_cistatic void last_ditch_exit(int sig) 4462306a36Sopenharmony_ci{ 4562306a36Sopenharmony_ci uml_cleanup(); 4662306a36Sopenharmony_ci exit(1); 4762306a36Sopenharmony_ci} 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_cistatic void install_fatal_handler(int sig) 5062306a36Sopenharmony_ci{ 5162306a36Sopenharmony_ci struct sigaction action; 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ci /* All signals are enabled in this handler ... */ 5462306a36Sopenharmony_ci sigemptyset(&action.sa_mask); 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ci /* 5762306a36Sopenharmony_ci * ... including the signal being handled, plus we want the 5862306a36Sopenharmony_ci * handler reset to the default behavior, so that if an exit 5962306a36Sopenharmony_ci * handler is hanging for some reason, the UML will just die 6062306a36Sopenharmony_ci * after this signal is sent a second time. 6162306a36Sopenharmony_ci */ 6262306a36Sopenharmony_ci action.sa_flags = SA_RESETHAND | SA_NODEFER; 6362306a36Sopenharmony_ci action.sa_restorer = NULL; 6462306a36Sopenharmony_ci action.sa_handler = last_ditch_exit; 6562306a36Sopenharmony_ci if (sigaction(sig, &action, NULL) < 0) { 6662306a36Sopenharmony_ci os_warn("failed to install handler for signal %d " 6762306a36Sopenharmony_ci "- errno = %d\n", sig, errno); 6862306a36Sopenharmony_ci exit(1); 6962306a36Sopenharmony_ci } 7062306a36Sopenharmony_ci} 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci#define UML_LIB_PATH ":" OS_LIB_PATH "/uml" 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_cistatic void setup_env_path(void) 7562306a36Sopenharmony_ci{ 7662306a36Sopenharmony_ci char *new_path = NULL; 7762306a36Sopenharmony_ci char *old_path = NULL; 7862306a36Sopenharmony_ci int path_len = 0; 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci old_path = getenv("PATH"); 8162306a36Sopenharmony_ci /* 8262306a36Sopenharmony_ci * if no PATH variable is set or it has an empty value 8362306a36Sopenharmony_ci * just use the default + /usr/lib/uml 8462306a36Sopenharmony_ci */ 8562306a36Sopenharmony_ci if (!old_path || (path_len = strlen(old_path)) == 0) { 8662306a36Sopenharmony_ci if (putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH)) 8762306a36Sopenharmony_ci perror("couldn't putenv"); 8862306a36Sopenharmony_ci return; 8962306a36Sopenharmony_ci } 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci /* append /usr/lib/uml to the existing path */ 9262306a36Sopenharmony_ci path_len += strlen("PATH=" UML_LIB_PATH) + 1; 9362306a36Sopenharmony_ci new_path = malloc(path_len); 9462306a36Sopenharmony_ci if (!new_path) { 9562306a36Sopenharmony_ci perror("couldn't malloc to set a new PATH"); 9662306a36Sopenharmony_ci return; 9762306a36Sopenharmony_ci } 9862306a36Sopenharmony_ci snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path); 9962306a36Sopenharmony_ci if (putenv(new_path)) { 10062306a36Sopenharmony_ci perror("couldn't putenv to set a new PATH"); 10162306a36Sopenharmony_ci free(new_path); 10262306a36Sopenharmony_ci } 10362306a36Sopenharmony_ci} 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_ciextern void scan_elf_aux( char **envp); 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ciint __init main(int argc, char **argv, char **envp) 10862306a36Sopenharmony_ci{ 10962306a36Sopenharmony_ci char **new_argv; 11062306a36Sopenharmony_ci int ret, i, err; 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci set_stklim(); 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci setup_env_path(); 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci setsid(); 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci new_argv = malloc((argc + 1) * sizeof(char *)); 11962306a36Sopenharmony_ci if (new_argv == NULL) { 12062306a36Sopenharmony_ci perror("Mallocing argv"); 12162306a36Sopenharmony_ci exit(1); 12262306a36Sopenharmony_ci } 12362306a36Sopenharmony_ci for (i = 0; i < argc; i++) { 12462306a36Sopenharmony_ci new_argv[i] = strdup(argv[i]); 12562306a36Sopenharmony_ci if (new_argv[i] == NULL) { 12662306a36Sopenharmony_ci perror("Mallocing an arg"); 12762306a36Sopenharmony_ci exit(1); 12862306a36Sopenharmony_ci } 12962306a36Sopenharmony_ci } 13062306a36Sopenharmony_ci new_argv[argc] = NULL; 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci /* 13362306a36Sopenharmony_ci * Allow these signals to bring down a UML if all other 13462306a36Sopenharmony_ci * methods of control fail. 13562306a36Sopenharmony_ci */ 13662306a36Sopenharmony_ci install_fatal_handler(SIGINT); 13762306a36Sopenharmony_ci install_fatal_handler(SIGTERM); 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci#ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA 14062306a36Sopenharmony_ci scan_elf_aux(envp); 14162306a36Sopenharmony_ci#endif 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_ci change_sig(SIGPIPE, 0); 14462306a36Sopenharmony_ci ret = linux_main(argc, argv); 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_ci /* 14762306a36Sopenharmony_ci * Disable SIGPROF - I have no idea why libc doesn't do this or turn 14862306a36Sopenharmony_ci * off the profiling time, but UML dies with a SIGPROF just before 14962306a36Sopenharmony_ci * exiting when profiling is active. 15062306a36Sopenharmony_ci */ 15162306a36Sopenharmony_ci change_sig(SIGPROF, 0); 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ci /* 15462306a36Sopenharmony_ci * This signal stuff used to be in the reboot case. However, 15562306a36Sopenharmony_ci * sometimes a timer signal can come in when we're halting (reproducably 15662306a36Sopenharmony_ci * when writing out gcov information, presumably because that takes 15762306a36Sopenharmony_ci * some time) and cause a segfault. 15862306a36Sopenharmony_ci */ 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_ci /* stop timers and set timer signal to be ignored */ 16162306a36Sopenharmony_ci os_timer_disable(); 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci /* disable SIGIO for the fds and set SIGIO to be ignored */ 16462306a36Sopenharmony_ci err = deactivate_all_fds(); 16562306a36Sopenharmony_ci if (err) 16662306a36Sopenharmony_ci os_warn("deactivate_all_fds failed, errno = %d\n", -err); 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ci /* 16962306a36Sopenharmony_ci * Let any pending signals fire now. This ensures 17062306a36Sopenharmony_ci * that they won't be delivered after the exec, when 17162306a36Sopenharmony_ci * they are definitely not expected. 17262306a36Sopenharmony_ci */ 17362306a36Sopenharmony_ci unblock_signals(); 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ci os_info("\n"); 17662306a36Sopenharmony_ci /* Reboot */ 17762306a36Sopenharmony_ci if (ret) { 17862306a36Sopenharmony_ci execvp(new_argv[0], new_argv); 17962306a36Sopenharmony_ci perror("Failed to exec kernel"); 18062306a36Sopenharmony_ci ret = 1; 18162306a36Sopenharmony_ci } 18262306a36Sopenharmony_ci return uml_exitcode; 18362306a36Sopenharmony_ci} 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ciextern void *__real_malloc(int); 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_civoid *__wrap_malloc(int size) 18862306a36Sopenharmony_ci{ 18962306a36Sopenharmony_ci void *ret; 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_ci if (!kmalloc_ok) 19262306a36Sopenharmony_ci return __real_malloc(size); 19362306a36Sopenharmony_ci else if (size <= UM_KERN_PAGE_SIZE) 19462306a36Sopenharmony_ci /* finding contiguous pages can be hard*/ 19562306a36Sopenharmony_ci ret = uml_kmalloc(size, UM_GFP_KERNEL); 19662306a36Sopenharmony_ci else ret = vmalloc(size); 19762306a36Sopenharmony_ci 19862306a36Sopenharmony_ci /* 19962306a36Sopenharmony_ci * glibc people insist that if malloc fails, errno should be 20062306a36Sopenharmony_ci * set by malloc as well. So we do. 20162306a36Sopenharmony_ci */ 20262306a36Sopenharmony_ci if (ret == NULL) 20362306a36Sopenharmony_ci errno = ENOMEM; 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ci return ret; 20662306a36Sopenharmony_ci} 20762306a36Sopenharmony_ci 20862306a36Sopenharmony_civoid *__wrap_calloc(int n, int size) 20962306a36Sopenharmony_ci{ 21062306a36Sopenharmony_ci void *ptr = __wrap_malloc(n * size); 21162306a36Sopenharmony_ci 21262306a36Sopenharmony_ci if (ptr == NULL) 21362306a36Sopenharmony_ci return NULL; 21462306a36Sopenharmony_ci memset(ptr, 0, n * size); 21562306a36Sopenharmony_ci return ptr; 21662306a36Sopenharmony_ci} 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ciextern void __real_free(void *); 21962306a36Sopenharmony_ci 22062306a36Sopenharmony_ciextern unsigned long high_physmem; 22162306a36Sopenharmony_ci 22262306a36Sopenharmony_civoid __wrap_free(void *ptr) 22362306a36Sopenharmony_ci{ 22462306a36Sopenharmony_ci unsigned long addr = (unsigned long) ptr; 22562306a36Sopenharmony_ci 22662306a36Sopenharmony_ci /* 22762306a36Sopenharmony_ci * We need to know how the allocation happened, so it can be correctly 22862306a36Sopenharmony_ci * freed. This is done by seeing what region of memory the pointer is 22962306a36Sopenharmony_ci * in - 23062306a36Sopenharmony_ci * physical memory - kmalloc/kfree 23162306a36Sopenharmony_ci * kernel virtual memory - vmalloc/vfree 23262306a36Sopenharmony_ci * anywhere else - malloc/free 23362306a36Sopenharmony_ci * If kmalloc is not yet possible, then either high_physmem and/or 23462306a36Sopenharmony_ci * end_vm are still 0 (as at startup), in which case we call free, or 23562306a36Sopenharmony_ci * we have set them, but anyway addr has not been allocated from those 23662306a36Sopenharmony_ci * areas. So, in both cases __real_free is called. 23762306a36Sopenharmony_ci * 23862306a36Sopenharmony_ci * CAN_KMALLOC is checked because it would be bad to free a buffer 23962306a36Sopenharmony_ci * with kmalloc/vmalloc after they have been turned off during 24062306a36Sopenharmony_ci * shutdown. 24162306a36Sopenharmony_ci * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so 24262306a36Sopenharmony_ci * there is a possibility for memory leaks. 24362306a36Sopenharmony_ci */ 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_ci if ((addr >= uml_physmem) && (addr < high_physmem)) { 24662306a36Sopenharmony_ci if (kmalloc_ok) 24762306a36Sopenharmony_ci kfree(ptr); 24862306a36Sopenharmony_ci } 24962306a36Sopenharmony_ci else if ((addr >= start_vm) && (addr < end_vm)) { 25062306a36Sopenharmony_ci if (kmalloc_ok) 25162306a36Sopenharmony_ci vfree(ptr); 25262306a36Sopenharmony_ci } 25362306a36Sopenharmony_ci else __real_free(ptr); 25462306a36Sopenharmony_ci} 255