1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
4 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <signal.h>
12 #include <string.h>
13 #include <sys/resource.h>
14 #include <as-layout.h>
15 #include <init.h>
16 #include <kern_util.h>
17 #include <os.h>
18 #include <um_malloc.h>
19 #include "internal.h"
20
21 #define PGD_BOUND (4 * 1024 * 1024)
22 #define STACKSIZE (8 * 1024 * 1024)
23 #define THREAD_NAME_LEN (256)
24
25 long elf_aux_hwcap;
26
set_stklim(void)27 static void set_stklim(void)
28 {
29 struct rlimit lim;
30
31 if (getrlimit(RLIMIT_STACK, &lim) < 0) {
32 perror("getrlimit");
33 exit(1);
34 }
35 if ((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)) {
36 lim.rlim_cur = STACKSIZE;
37 if (setrlimit(RLIMIT_STACK, &lim) < 0) {
38 perror("setrlimit");
39 exit(1);
40 }
41 }
42 }
43
last_ditch_exit(int sig)44 static void last_ditch_exit(int sig)
45 {
46 uml_cleanup();
47 exit(1);
48 }
49
install_fatal_handler(int sig)50 static void install_fatal_handler(int sig)
51 {
52 struct sigaction action;
53
54 /* All signals are enabled in this handler ... */
55 sigemptyset(&action.sa_mask);
56
57 /*
58 * ... including the signal being handled, plus we want the
59 * handler reset to the default behavior, so that if an exit
60 * handler is hanging for some reason, the UML will just die
61 * after this signal is sent a second time.
62 */
63 action.sa_flags = SA_RESETHAND | SA_NODEFER;
64 action.sa_restorer = NULL;
65 action.sa_handler = last_ditch_exit;
66 if (sigaction(sig, &action, NULL) < 0) {
67 os_warn("failed to install handler for signal %d "
68 "- errno = %d\n", sig, errno);
69 exit(1);
70 }
71 }
72
73 #define UML_LIB_PATH ":" OS_LIB_PATH "/uml"
74
setup_env_path(void)75 static void setup_env_path(void)
76 {
77 char *new_path = NULL;
78 char *old_path = NULL;
79 int path_len = 0;
80
81 old_path = getenv("PATH");
82 /*
83 * if no PATH variable is set or it has an empty value
84 * just use the default + /usr/lib/uml
85 */
86 if (!old_path || (path_len = strlen(old_path)) == 0) {
87 if (putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH))
88 perror("couldn't putenv");
89 return;
90 }
91
92 /* append /usr/lib/uml to the existing path */
93 path_len += strlen("PATH=" UML_LIB_PATH) + 1;
94 new_path = malloc(path_len);
95 if (!new_path) {
96 perror("couldn't malloc to set a new PATH");
97 return;
98 }
99 snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path);
100 if (putenv(new_path)) {
101 perror("couldn't putenv to set a new PATH");
102 free(new_path);
103 }
104 }
105
main(int argc,char ** argv,char ** envp)106 int __init main(int argc, char **argv, char **envp)
107 {
108 char **new_argv;
109 int ret, i, err;
110
111 set_stklim();
112
113 setup_env_path();
114
115 setsid();
116
117 new_argv = malloc((argc + 1) * sizeof(char *));
118 if (new_argv == NULL) {
119 perror("Mallocing argv");
120 exit(1);
121 }
122 for (i = 0; i < argc; i++) {
123 new_argv[i] = strdup(argv[i]);
124 if (new_argv[i] == NULL) {
125 perror("Mallocing an arg");
126 exit(1);
127 }
128 }
129 new_argv[argc] = NULL;
130
131 /*
132 * Allow these signals to bring down a UML if all other
133 * methods of control fail.
134 */
135 install_fatal_handler(SIGINT);
136 install_fatal_handler(SIGTERM);
137
138 #ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA
139 scan_elf_aux(envp);
140 #endif
141
142 change_sig(SIGPIPE, 0);
143 ret = linux_main(argc, argv);
144
145 /*
146 * Disable SIGPROF - I have no idea why libc doesn't do this or turn
147 * off the profiling time, but UML dies with a SIGPROF just before
148 * exiting when profiling is active.
149 */
150 change_sig(SIGPROF, 0);
151
152 /*
153 * This signal stuff used to be in the reboot case. However,
154 * sometimes a timer signal can come in when we're halting (reproducably
155 * when writing out gcov information, presumably because that takes
156 * some time) and cause a segfault.
157 */
158
159 /* stop timers and set timer signal to be ignored */
160 os_timer_disable();
161
162 /* disable SIGIO for the fds and set SIGIO to be ignored */
163 err = deactivate_all_fds();
164 if (err)
165 os_warn("deactivate_all_fds failed, errno = %d\n", -err);
166
167 /*
168 * Let any pending signals fire now. This ensures
169 * that they won't be delivered after the exec, when
170 * they are definitely not expected.
171 */
172 unblock_signals();
173
174 os_info("\n");
175 /* Reboot */
176 if (ret) {
177 execvp(new_argv[0], new_argv);
178 perror("Failed to exec kernel");
179 ret = 1;
180 }
181 return uml_exitcode;
182 }
183
184 extern void *__real_malloc(int);
185
186 /* workaround for -Wmissing-prototypes warnings */
187 void *__wrap_malloc(int size);
188 void *__wrap_calloc(int n, int size);
189 void __wrap_free(void *ptr);
190
__wrap_malloc(int size)191 void *__wrap_malloc(int size)
192 {
193 void *ret;
194
195 if (!kmalloc_ok)
196 return __real_malloc(size);
197 else if (size <= UM_KERN_PAGE_SIZE)
198 /* finding contiguous pages can be hard*/
199 ret = uml_kmalloc(size, UM_GFP_KERNEL);
200 else ret = vmalloc(size);
201
202 /*
203 * glibc people insist that if malloc fails, errno should be
204 * set by malloc as well. So we do.
205 */
206 if (ret == NULL)
207 errno = ENOMEM;
208
209 return ret;
210 }
211
__wrap_calloc(int n,int size)212 void *__wrap_calloc(int n, int size)
213 {
214 void *ptr = __wrap_malloc(n * size);
215
216 if (ptr == NULL)
217 return NULL;
218 memset(ptr, 0, n * size);
219 return ptr;
220 }
221
222 extern void __real_free(void *);
223
224 extern unsigned long high_physmem;
225
__wrap_free(void * ptr)226 void __wrap_free(void *ptr)
227 {
228 unsigned long addr = (unsigned long) ptr;
229
230 /*
231 * We need to know how the allocation happened, so it can be correctly
232 * freed. This is done by seeing what region of memory the pointer is
233 * in -
234 * physical memory - kmalloc/kfree
235 * kernel virtual memory - vmalloc/vfree
236 * anywhere else - malloc/free
237 * If kmalloc is not yet possible, then either high_physmem and/or
238 * end_vm are still 0 (as at startup), in which case we call free, or
239 * we have set them, but anyway addr has not been allocated from those
240 * areas. So, in both cases __real_free is called.
241 *
242 * CAN_KMALLOC is checked because it would be bad to free a buffer
243 * with kmalloc/vmalloc after they have been turned off during
244 * shutdown.
245 * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
246 * there is a possibility for memory leaks.
247 */
248
249 if ((addr >= uml_physmem) && (addr < high_physmem)) {
250 if (kmalloc_ok)
251 kfree(ptr);
252 }
253 else if ((addr >= start_vm) && (addr < end_vm)) {
254 if (kmalloc_ok)
255 vfree(ptr);
256 }
257 else __real_free(ptr);
258 }
259