Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
0004  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
0005  */
0006 
0007 #include <stdio.h>
0008 #include <stdlib.h>
0009 #include <unistd.h>
0010 #include <errno.h>
0011 #include <signal.h>
0012 #include <string.h>
0013 #include <sys/resource.h>
0014 #include <as-layout.h>
0015 #include <init.h>
0016 #include <kern_util.h>
0017 #include <os.h>
0018 #include <um_malloc.h>
0019 
0020 #define PGD_BOUND (4 * 1024 * 1024)
0021 #define STACKSIZE (8 * 1024 * 1024)
0022 #define THREAD_NAME_LEN (256)
0023 
0024 long elf_aux_hwcap;
0025 
0026 static void set_stklim(void)
0027 {
0028     struct rlimit lim;
0029 
0030     if (getrlimit(RLIMIT_STACK, &lim) < 0) {
0031         perror("getrlimit");
0032         exit(1);
0033     }
0034     if ((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)) {
0035         lim.rlim_cur = STACKSIZE;
0036         if (setrlimit(RLIMIT_STACK, &lim) < 0) {
0037             perror("setrlimit");
0038             exit(1);
0039         }
0040     }
0041 }
0042 
0043 static void last_ditch_exit(int sig)
0044 {
0045     uml_cleanup();
0046     exit(1);
0047 }
0048 
0049 static void install_fatal_handler(int sig)
0050 {
0051     struct sigaction action;
0052 
0053     /* All signals are enabled in this handler ... */
0054     sigemptyset(&action.sa_mask);
0055 
0056     /*
0057      * ... including the signal being handled, plus we want the
0058      * handler reset to the default behavior, so that if an exit
0059      * handler is hanging for some reason, the UML will just die
0060      * after this signal is sent a second time.
0061      */
0062     action.sa_flags = SA_RESETHAND | SA_NODEFER;
0063     action.sa_restorer = NULL;
0064     action.sa_handler = last_ditch_exit;
0065     if (sigaction(sig, &action, NULL) < 0) {
0066         os_warn("failed to install handler for signal %d "
0067             "- errno = %d\n", sig, errno);
0068         exit(1);
0069     }
0070 }
0071 
0072 #define UML_LIB_PATH    ":" OS_LIB_PATH "/uml"
0073 
0074 static void setup_env_path(void)
0075 {
0076     char *new_path = NULL;
0077     char *old_path = NULL;
0078     int path_len = 0;
0079 
0080     old_path = getenv("PATH");
0081     /*
0082      * if no PATH variable is set or it has an empty value
0083      * just use the default + /usr/lib/uml
0084      */
0085     if (!old_path || (path_len = strlen(old_path)) == 0) {
0086         if (putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH))
0087             perror("couldn't putenv");
0088         return;
0089     }
0090 
0091     /* append /usr/lib/uml to the existing path */
0092     path_len += strlen("PATH=" UML_LIB_PATH) + 1;
0093     new_path = malloc(path_len);
0094     if (!new_path) {
0095         perror("couldn't malloc to set a new PATH");
0096         return;
0097     }
0098     snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path);
0099     if (putenv(new_path)) {
0100         perror("couldn't putenv to set a new PATH");
0101         free(new_path);
0102     }
0103 }
0104 
0105 extern void scan_elf_aux( char **envp);
0106 
0107 int __init main(int argc, char **argv, char **envp)
0108 {
0109     char **new_argv;
0110     int ret, i, err;
0111 
0112     set_stklim();
0113 
0114     setup_env_path();
0115 
0116     setsid();
0117 
0118     new_argv = malloc((argc + 1) * sizeof(char *));
0119     if (new_argv == NULL) {
0120         perror("Mallocing argv");
0121         exit(1);
0122     }
0123     for (i = 0; i < argc; i++) {
0124         new_argv[i] = strdup(argv[i]);
0125         if (new_argv[i] == NULL) {
0126             perror("Mallocing an arg");
0127             exit(1);
0128         }
0129     }
0130     new_argv[argc] = NULL;
0131 
0132     /*
0133      * Allow these signals to bring down a UML if all other
0134      * methods of control fail.
0135      */
0136     install_fatal_handler(SIGINT);
0137     install_fatal_handler(SIGTERM);
0138 
0139 #ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA
0140     scan_elf_aux(envp);
0141 #endif
0142 
0143     change_sig(SIGPIPE, 0);
0144     ret = linux_main(argc, argv);
0145 
0146     /*
0147      * Disable SIGPROF - I have no idea why libc doesn't do this or turn
0148      * off the profiling time, but UML dies with a SIGPROF just before
0149      * exiting when profiling is active.
0150      */
0151     change_sig(SIGPROF, 0);
0152 
0153     /*
0154      * This signal stuff used to be in the reboot case.  However,
0155      * sometimes a timer signal can come in when we're halting (reproducably
0156      * when writing out gcov information, presumably because that takes
0157      * some time) and cause a segfault.
0158      */
0159 
0160     /* stop timers and set timer signal to be ignored */
0161     os_timer_disable();
0162 
0163     /* disable SIGIO for the fds and set SIGIO to be ignored */
0164     err = deactivate_all_fds();
0165     if (err)
0166         os_warn("deactivate_all_fds failed, errno = %d\n", -err);
0167 
0168     /*
0169      * Let any pending signals fire now.  This ensures
0170      * that they won't be delivered after the exec, when
0171      * they are definitely not expected.
0172      */
0173     unblock_signals();
0174 
0175     os_info("\n");
0176     /* Reboot */
0177     if (ret) {
0178         execvp(new_argv[0], new_argv);
0179         perror("Failed to exec kernel");
0180         ret = 1;
0181     }
0182     return uml_exitcode;
0183 }
0184 
0185 extern void *__real_malloc(int);
0186 
0187 void *__wrap_malloc(int size)
0188 {
0189     void *ret;
0190 
0191     if (!kmalloc_ok)
0192         return __real_malloc(size);
0193     else if (size <= UM_KERN_PAGE_SIZE)
0194         /* finding contiguous pages can be hard*/
0195         ret = uml_kmalloc(size, UM_GFP_KERNEL);
0196     else ret = vmalloc(size);
0197 
0198     /*
0199      * glibc people insist that if malloc fails, errno should be
0200      * set by malloc as well. So we do.
0201      */
0202     if (ret == NULL)
0203         errno = ENOMEM;
0204 
0205     return ret;
0206 }
0207 
0208 void *__wrap_calloc(int n, int size)
0209 {
0210     void *ptr = __wrap_malloc(n * size);
0211 
0212     if (ptr == NULL)
0213         return NULL;
0214     memset(ptr, 0, n * size);
0215     return ptr;
0216 }
0217 
0218 extern void __real_free(void *);
0219 
0220 extern unsigned long high_physmem;
0221 
0222 void __wrap_free(void *ptr)
0223 {
0224     unsigned long addr = (unsigned long) ptr;
0225 
0226     /*
0227      * We need to know how the allocation happened, so it can be correctly
0228      * freed.  This is done by seeing what region of memory the pointer is
0229      * in -
0230      *  physical memory - kmalloc/kfree
0231      *  kernel virtual memory - vmalloc/vfree
0232      *  anywhere else - malloc/free
0233      * If kmalloc is not yet possible, then either high_physmem and/or
0234      * end_vm are still 0 (as at startup), in which case we call free, or
0235      * we have set them, but anyway addr has not been allocated from those
0236      * areas. So, in both cases __real_free is called.
0237      *
0238      * CAN_KMALLOC is checked because it would be bad to free a buffer
0239      * with kmalloc/vmalloc after they have been turned off during
0240      * shutdown.
0241      * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
0242      * there is a possibility for memory leaks.
0243      */
0244 
0245     if ((addr >= uml_physmem) && (addr < high_physmem)) {
0246         if (kmalloc_ok)
0247             kfree(ptr);
0248     }
0249     else if ((addr >= start_vm) && (addr < end_vm)) {
0250         if (kmalloc_ok)
0251             vfree(ptr);
0252     }
0253     else __real_free(ptr);
0254 }