0001
0002
0003
0004
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
0054 sigemptyset(&action.sa_mask);
0055
0056
0057
0058
0059
0060
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
0083
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
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
0134
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
0148
0149
0150
0151 change_sig(SIGPROF, 0);
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161 os_timer_disable();
0162
0163
0164 err = deactivate_all_fds();
0165 if (err)
0166 os_warn("deactivate_all_fds failed, errno = %d\n", -err);
0167
0168
0169
0170
0171
0172
0173 unblock_signals();
0174
0175 os_info("\n");
0176
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
0195 ret = uml_kmalloc(size, UM_GFP_KERNEL);
0196 else ret = vmalloc(size);
0197
0198
0199
0200
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
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
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 }