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