1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
19 #include <sys/resource.h>
20 #include <asm/unistd.h>
24 #include <ptrace_user.h>
25 #include <registers.h>
28 static void ptrace_child(void)
31 /* Calling os_getpid because some libcs cached getpid incorrectly */
32 int pid = os_getpid(), ppid = getppid();
35 if (change_sig(SIGWINCH, 0) < 0 ||
36 ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
43 * This syscall will be intercepted by the parent. Don't call more than
46 sc_result = os_getpid();
49 /* Nothing modified by the parent, we are running normally. */
51 else if (sc_result == ppid)
53 * Expected in check_ptrace and check_sysemu when they succeed
54 * in modifying the stack frame
58 /* Serious trouble! This could be caused by a bug in host 2.6
59 * SKAS3/2.6 patch before release -V6, together with a bug in
60 * the UML code itself.
67 static void fatal_perror(const char *str)
73 static void fatal(char *fmt, ...)
78 vfprintf(stderr, fmt, list);
84 static void non_fatal(char *fmt, ...)
89 vfprintf(stderr, fmt, list);
93 static int start_ptraced_child(void)
103 fatal_perror("start_ptraced_child : fork failed");
105 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
107 fatal_perror("check_ptrace : waitpid failed");
108 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
109 fatal("check_ptrace : expected SIGSTOP, got status = %d",
115 /* When testing for SYSEMU support, if it is one of the broken versions, we
116 * must just avoid using sysemu, not panic, but only if SYSEMU features are
118 * So only for SYSEMU features we test mustpanic, while normal host features
121 static int stop_ptraced_child(int pid, int exitcode, int mustexit)
123 int status, n, ret = 0;
125 if (ptrace(PTRACE_CONT, pid, 0, 0) < 0) {
126 perror("stop_ptraced_child : ptrace failed");
129 CATCH_EINTR(n = waitpid(pid, &status, 0));
130 if (!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
131 int exit_with = WEXITSTATUS(status);
133 non_fatal("check_ptrace : child exited with status 2. "
134 "\nDisabling SYSEMU support.\n");
135 non_fatal("check_ptrace : child exited with exitcode %d, while "
136 "expecting %d; status 0x%x\n", exit_with,
146 /* Changed only during early boot */
147 static int force_sysemu_disabled = 0;
149 static int __init nosysemu_cmd_param(char *str, int* add)
151 force_sysemu_disabled = 1;
155 __uml_setup("nosysemu", nosysemu_cmd_param,
157 " Turns off syscall emulation patch for ptrace (SYSEMU).\n"
158 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
159 " behaviour of ptrace() and helps reduce host context switch rates.\n"
160 " To make it work, you need a kernel patch for your host, too.\n"
161 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
162 " information.\n\n");
164 static void __init check_sysemu(void)
166 unsigned long regs[MAX_REG_NR];
167 int pid, n, status, count=0;
169 os_info("Checking syscall emulation patch for ptrace...");
170 sysemu_supported = 0;
171 pid = start_ptraced_child();
173 if (ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
176 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
178 fatal_perror("check_sysemu : wait failed");
179 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
180 fatal("check_sysemu : expected SIGTRAP, got status = %d\n",
183 if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
184 fatal_perror("check_sysemu : PTRACE_GETREGS failed");
185 if (PT_SYSCALL_NR(regs) != __NR_getpid) {
186 non_fatal("check_sysemu got system call number %d, "
187 "expected %d...", PT_SYSCALL_NR(regs), __NR_getpid);
191 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_RET_OFFSET, os_getpid());
193 non_fatal("check_sysemu : failed to modify system call "
198 if (stop_ptraced_child(pid, 0, 0) < 0)
201 sysemu_supported = 1;
203 set_using_sysemu(!force_sysemu_disabled);
205 os_info("Checking advanced syscall emulation patch for ptrace...");
206 pid = start_ptraced_child();
208 if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
209 (void *) PTRACE_O_TRACESYSGOOD) < 0))
210 fatal_perror("check_sysemu: PTRACE_OLDSETOPTIONS failed");
214 if (ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
216 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
218 fatal_perror("check_sysemu: wait failed");
220 if (WIFSTOPPED(status) &&
221 (WSTOPSIG(status) == (SIGTRAP|0x80))) {
223 non_fatal("check_sysemu: SYSEMU_SINGLESTEP "
224 "doesn't singlestep");
227 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_RET_OFFSET,
230 fatal_perror("check_sysemu : failed to modify "
231 "system call return");
234 else if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
237 non_fatal("check_sysemu: expected SIGTRAP or "
238 "(SIGTRAP | 0x80), got status = %d\n",
243 if (stop_ptraced_child(pid, 0, 0) < 0)
246 sysemu_supported = 2;
249 if (!force_sysemu_disabled)
250 set_using_sysemu(sysemu_supported);
254 stop_ptraced_child(pid, 1, 0);
256 non_fatal("missing\n");
259 static void __init check_ptrace(void)
261 int pid, syscall, n, status;
263 os_info("Checking that ptrace can change system call numbers...");
264 pid = start_ptraced_child();
266 if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
267 (void *) PTRACE_O_TRACESYSGOOD) < 0))
268 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
271 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
272 fatal_perror("check_ptrace : ptrace failed");
274 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
276 fatal_perror("check_ptrace : wait failed");
278 if (!WIFSTOPPED(status) ||
279 (WSTOPSIG(status) != (SIGTRAP | 0x80)))
280 fatal("check_ptrace : expected (SIGTRAP|0x80), "
281 "got status = %d", status);
283 syscall = ptrace(PTRACE_PEEKUSER, pid, PT_SYSCALL_NR_OFFSET,
285 if (syscall == __NR_getpid) {
286 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
289 fatal_perror("check_ptrace : failed to modify "
294 stop_ptraced_child(pid, 0, 1);
299 extern void check_tmpexec(void);
301 static void __init check_coredump_limit(void)
304 int err = getrlimit(RLIMIT_CORE, &lim);
307 perror("Getting core dump limit");
311 os_info("Core dump limits :\n\tsoft - ");
312 if (lim.rlim_cur == RLIM_INFINITY)
315 os_info("%llu\n", (unsigned long long)lim.rlim_cur);
317 os_info("\thard - ");
318 if (lim.rlim_max == RLIM_INFINITY)
321 os_info("%llu\n", (unsigned long long)lim.rlim_max);
324 void __init get_host_cpu_features(
325 void (*flags_helper_func)(char *line),
326 void (*cache_helper_func)(char *line))
331 int done_parsing = 0;
333 cpuinfo = fopen("/proc/cpuinfo", "r");
334 if (cpuinfo == NULL) {
335 os_info("Failed to get host CPU features\n");
337 while ((getline(&line, &len, cpuinfo)) != -1) {
338 if (strstr(line, "flags")) {
339 flags_helper_func(line);
342 if (strstr(line, "cache_alignment")) {
343 cache_helper_func(line);
348 if (done_parsing > 1)
356 void __init os_early_checks(void)
360 /* Print out the core dump limits early */
361 check_coredump_limit();
365 /* Need to check this early because mmapping happens before the
370 pid = start_ptraced_child();
371 if (init_registers(pid))
372 fatal("Failed to initialize default registers");
373 stop_ptraced_child(pid, 1, 1);
376 int __init parse_iomem(char *str, int *add)
378 struct iomem_region *new;
384 file = strchr(str,',');
386 os_warn("parse_iomem : failed to parse iomem\n");
391 fd = open(file, O_RDWR, 0);
393 perror("parse_iomem - Couldn't open io file");
397 if (fstat64(fd, &buf) < 0) {
398 perror("parse_iomem - cannot stat_fd file");
402 new = malloc(sizeof(*new));
404 perror("Couldn't allocate iomem_region struct");
408 size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
410 *new = ((struct iomem_region) { .next = iomem_regions,
417 iomem_size += new->size + UM_KERN_PAGE_SIZE;