2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
20 #include <asm/unistd.h>
22 #include <sys/types.h>
23 #include "user_util.h"
24 #include "kern_util.h"
26 #include "signal_kern.h"
27 #include "sysdep/ptrace.h"
28 #include "sysdep/sigcontext.h"
30 #include "ptrace_user.h"
34 #include "uml-config.h"
35 #include "choose-mode.h"
38 #include "kern_constants.h"
40 #ifdef UML_CONFIG_MODE_SKAS
42 #include "skas_ptrace.h"
43 #include "registers.h"
46 static int ptrace_child(void *arg)
49 int pid = os_getpid(), ppid = getppid();
52 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
54 os_kill_process(pid, 0);
58 /*This syscall will be intercepted by the parent. Don't call more than
60 sc_result = os_getpid();
63 ret = 1; /*Nothing modified by the parent, we are running
65 else if (sc_result == ppid)
66 ret = 0; /*Expected in check_ptrace and check_sysemu when they
67 succeed in modifying the stack frame*/
69 ret = 2; /*Serious trouble! This could be caused by a bug in
70 host 2.6 SKAS3/2.6 patch before release -V6, together
71 with a bug in the UML code itself.*/
75 static int start_ptraced_child(void **stack_out)
81 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
82 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
83 if(stack == MAP_FAILED)
84 panic("check_ptrace : mmap failed, errno = %d", errno);
85 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
86 pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
88 panic("start_ptraced_child : clone failed, errno = %d", errno);
89 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
91 panic("check_ptrace : clone failed, errno = %d", errno);
92 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
93 panic("check_ptrace : expected SIGSTOP, got status = %d",
100 /* When testing for SYSEMU support, if it is one of the broken versions, we
101 * must just avoid using sysemu, not panic, but only if SYSEMU features are
103 * So only for SYSEMU features we test mustpanic, while normal host features
106 static int stop_ptraced_child(int pid, void *stack, int exitcode,
109 int status, n, ret = 0;
111 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
112 panic("check_ptrace : ptrace failed, errno = %d", errno);
113 CATCH_EINTR(n = waitpid(pid, &status, 0));
114 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
115 int exit_with = WEXITSTATUS(status);
117 printf("check_ptrace : child exited with status 2. "
118 "Serious trouble happening! Try updating your "
119 "host skas patch!\nDisabling SYSEMU support.");
120 printf("check_ptrace : child exited with exitcode %d, while "
121 "expecting %d; status 0x%x", exit_with,
130 if(munmap(stack, PAGE_SIZE) < 0)
131 panic("check_ptrace : munmap failed, errno = %d", errno);
135 int ptrace_faultinfo = 1;
138 int skas_needs_stub = 0;
140 static int __init skas0_cmd_param(char *str, int* add)
142 ptrace_faultinfo = proc_mm = 0;
146 /* The two __uml_setup would conflict, without this stupid alias. */
148 static int __init mode_skas0_cmd_param(char *str, int* add)
149 __attribute__((alias("skas0_cmd_param")));
151 __uml_setup("skas0", skas0_cmd_param,
153 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
154 " you specify mode=tt.\n\n");
156 __uml_setup("mode=skas0", mode_skas0_cmd_param,
158 " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
159 " specify mode=tt. Note that this was recently added - on \n"
160 " older kernels you must use simply \"skas0\".\n\n");
162 static int force_sysemu_disabled = 0;
164 static int __init nosysemu_cmd_param(char *str, int* add)
166 force_sysemu_disabled = 1;
170 __uml_setup("nosysemu", nosysemu_cmd_param,
172 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
173 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
174 " behaviour of ptrace() and helps reducing host context switch rate.\n"
175 " To make it working, you need a kernel patch for your host, too.\n"
176 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
177 " information.\n\n");
179 static void __init check_sysemu(void)
182 int pid, n, status, count=0;
184 printf("Checking syscall emulation patch for ptrace...");
185 sysemu_supported = 0;
186 pid = start_ptraced_child(&stack);
188 if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
191 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
193 panic("check_sysemu : wait failed, errno = %d", errno);
194 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
195 panic("check_sysemu : expected SIGTRAP, "
196 "got status = %d", status);
198 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
201 panic("check_sysemu : failed to modify system "
202 "call return, errno = %d", errno);
204 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
207 sysemu_supported = 1;
209 set_using_sysemu(!force_sysemu_disabled);
211 printf("Checking advanced syscall emulation patch for ptrace...");
212 pid = start_ptraced_child(&stack);
214 if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
215 (void *) PTRACE_O_TRACESYSGOOD) < 0)
216 panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d",
221 if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
223 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
225 panic("check_ptrace : wait failed, errno = %d", errno);
226 if(WIFSTOPPED(status) && (WSTOPSIG(status) == (SIGTRAP|0x80))){
228 panic("check_ptrace : SYSEMU_SINGLESTEP "
229 "doesn't singlestep");
230 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
233 panic("check_sysemu : failed to modify system "
234 "call return, errno = %d", errno);
237 else if(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
240 panic("check_ptrace : expected SIGTRAP or "
241 "(SIGTRAP|0x80), got status = %d", status);
243 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
246 sysemu_supported = 2;
249 if ( !force_sysemu_disabled )
250 set_using_sysemu(sysemu_supported);
254 stop_ptraced_child(pid, stack, 1, 0);
259 static void __init check_ptrace(void)
262 int pid, syscall, n, status;
264 printf("Checking that ptrace can change system call numbers...");
265 pid = start_ptraced_child(&stack);
267 if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
268 panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d", errno);
271 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
272 panic("check_ptrace : ptrace failed, errno = %d",
274 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
276 panic("check_ptrace : wait failed, errno = %d", errno);
277 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != (SIGTRAP|0x80)))
278 panic("check_ptrace : expected (SIGTRAP|0x80), "
279 "got status = %d", status);
281 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
283 if(syscall == __NR_getpid){
284 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
287 panic("check_ptrace : failed to modify system "
288 "call, errno = %d", errno);
292 stop_ptraced_child(pid, stack, 0, 1);
297 extern int create_tmp_file(unsigned long long len);
299 static void check_tmpexec(void)
302 int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
304 addr = mmap(NULL, UM_KERN_PAGE_SIZE,
305 PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
306 printf("Checking PROT_EXEC mmap in /tmp...");
308 if(addr == MAP_FAILED){
312 printf("/tmp must be not mounted noexec\n");
316 munmap(addr, UM_KERN_PAGE_SIZE);
321 void os_early_checks(void)
325 /* Need to check this early because mmapping happens before the
331 static int __init noprocmm_cmd_param(char *str, int* add)
337 __uml_setup("noprocmm", noprocmm_cmd_param,
339 " Turns off usage of /proc/mm, even if host supports it.\n"
340 " To support /proc/mm, the host needs to be patched using\n"
341 " the current skas3 patch.\n\n");
343 static int __init noptracefaultinfo_cmd_param(char *str, int* add)
345 ptrace_faultinfo = 0;
349 __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
350 "noptracefaultinfo\n"
351 " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
352 " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
353 " using the current skas3 patch.\n\n");
355 static int __init noptraceldt_cmd_param(char *str, int* add)
361 __uml_setup("noptraceldt", noptraceldt_cmd_param,
363 " Turns off usage of PTRACE_LDT, even if host supports it.\n"
364 " To support PTRACE_LDT, the host needs to be patched using\n"
365 " the current skas3 patch.\n\n");
367 #ifdef UML_CONFIG_MODE_SKAS
368 static inline void check_skas3_ptrace_faultinfo(void)
370 struct ptrace_faultinfo fi;
374 printf(" - PTRACE_FAULTINFO...");
375 pid = start_ptraced_child(&stack);
377 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
379 ptrace_faultinfo = 0;
381 printf("not found\n");
386 if (!ptrace_faultinfo)
387 printf("found but disabled on command line\n");
393 stop_ptraced_child(pid, stack, 1, 1);
396 static inline void check_skas3_ptrace_ldt(void)
401 unsigned char ldtbuf[40];
402 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
403 .func = 2, /* read default ldt */
405 .bytecount = sizeof(ldtbuf)};
407 printf(" - PTRACE_LDT...");
408 pid = start_ptraced_child(&stack);
410 n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
413 printf("not found\n");
423 printf("found, but use is disabled\n");
426 stop_ptraced_child(pid, stack, 1, 1);
428 /* PTRACE_LDT might be disabled via cmdline option.
429 * We want to override this, else we might use the stub
436 static inline void check_skas3_proc_mm(void)
438 printf(" - /proc/mm...");
439 if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
441 printf("not found\n");
445 printf("found but disabled on command line\n");
451 int can_do_skas(void)
453 printf("Checking for the skas3 patch in the host:\n");
455 check_skas3_proc_mm();
456 check_skas3_ptrace_faultinfo();
457 check_skas3_ptrace_ldt();
459 if(!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
465 int can_do_skas(void)
471 int have_devanon = 0;
473 /* Runs on boot kernel stack - already safe to use printk. */
475 void check_devanon(void)
479 printk("Checking for /dev/anon on the host...");
480 fd = open("/dev/anon", O_RDWR);
482 printk("Not available (open failed with errno %d)\n", errno);
490 int __init parse_iomem(char *str, int *add)
492 struct iomem_region *new;
498 file = strchr(str,',');
500 printf("parse_iomem : failed to parse iomem\n");
505 fd = os_open_file(file, of_rdwr(OPENFLAGS()), 0);
507 os_print_error(fd, "parse_iomem - Couldn't open io file");
511 err = os_stat_fd(fd, &buf);
513 os_print_error(err, "parse_iomem - cannot stat_fd file");
517 new = malloc(sizeof(*new));
519 perror("Couldn't allocate iomem_region struct");
523 size = (buf.ust_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
525 *new = ((struct iomem_region) { .next = iomem_regions,
532 iomem_size += new->size + UM_KERN_PAGE_SIZE;