2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
17 #include <asm/unistd.h>
19 #include "user_util.h"
20 #include "kern_util.h"
22 #include "signal_kern.h"
23 #include "signal_user.h"
24 #include "sysdep/ptrace.h"
25 #include "sysdep/sigcontext.h"
27 #include "ptrace_user.h"
28 #include "time_user.h"
31 #include "uml-config.h"
32 #include "choose-mode.h"
35 #ifdef UML_CONFIG_MODE_SKAS
37 #include "skas_ptrace.h"
38 #include "registers.h"
41 static int ptrace_child(void *arg)
44 int pid = os_getpid(), ppid = getppid();
47 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
49 os_kill_process(pid, 0);
53 /*This syscall will be intercepted by the parent. Don't call more than
55 sc_result = os_getpid();
58 ret = 1; /*Nothing modified by the parent, we are running
60 else if (sc_result == ppid)
61 ret = 0; /*Expected in check_ptrace and check_sysemu when they
62 succeed in modifying the stack frame*/
64 ret = 2; /*Serious trouble! This could be caused by a bug in
65 host 2.6 SKAS3/2.6 patch before release -V6, together
66 with a bug in the UML code itself.*/
70 static int start_ptraced_child(void **stack_out)
76 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
77 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
78 if(stack == MAP_FAILED)
79 panic("check_ptrace : mmap failed, errno = %d", errno);
80 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
81 pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
83 panic("start_ptraced_child : clone failed, errno = %d", errno);
84 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
86 panic("check_ptrace : clone failed, errno = %d", errno);
87 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
88 panic("check_ptrace : expected SIGSTOP, got status = %d",
95 /* When testing for SYSEMU support, if it is one of the broken versions, we
96 * must just avoid using sysemu, not panic, but only if SYSEMU features are
98 * So only for SYSEMU features we test mustpanic, while normal host features
101 static int stop_ptraced_child(int pid, void *stack, int exitcode,
104 int status, n, ret = 0;
106 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
107 panic("check_ptrace : ptrace failed, errno = %d", errno);
108 CATCH_EINTR(n = waitpid(pid, &status, 0));
109 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
110 int exit_with = WEXITSTATUS(status);
112 printk("check_ptrace : child exited with status 2. "
113 "Serious trouble happening! Try updating your "
114 "host skas patch!\nDisabling SYSEMU support.");
115 printk("check_ptrace : child exited with exitcode %d, while "
116 "expecting %d; status 0x%x", exit_with,
125 if(munmap(stack, PAGE_SIZE) < 0)
126 panic("check_ptrace : munmap failed, errno = %d", errno);
130 int ptrace_faultinfo = 1;
133 static int __init skas0_cmd_param(char *str, int* add)
135 ptrace_faultinfo = proc_mm = 0;
139 __uml_setup("skas0", skas0_cmd_param,
141 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
142 " you specify mode=tt.\n\n");
144 static int force_sysemu_disabled = 0;
146 static int __init nosysemu_cmd_param(char *str, int* add)
148 force_sysemu_disabled = 1;
152 __uml_setup("nosysemu", nosysemu_cmd_param,
154 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
155 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
156 " behaviour of ptrace() and helps reducing host context switch rate.\n"
157 " To make it working, you need a kernel patch for your host, too.\n"
158 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
159 " information.\n\n");
161 static void __init check_sysemu(void)
164 int pid, syscall, n, status, count=0;
166 printk("Checking syscall emulation patch for ptrace...");
167 sysemu_supported = 0;
168 pid = start_ptraced_child(&stack);
170 if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
173 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
175 panic("check_sysemu : wait failed, errno = %d", errno);
176 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
177 panic("check_sysemu : expected SIGTRAP, "
178 "got status = %d", status);
180 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
183 panic("check_sysemu : failed to modify system "
184 "call return, errno = %d", errno);
186 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
189 sysemu_supported = 1;
191 set_using_sysemu(!force_sysemu_disabled);
193 printk("Checking advanced syscall emulation patch for ptrace...");
194 pid = start_ptraced_child(&stack);
197 if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
199 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
201 panic("check_ptrace : wait failed, errno = %d", errno);
202 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
203 panic("check_ptrace : expected (SIGTRAP|SYSCALL_TRAP), "
204 "got status = %d", status);
206 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
208 if(syscall == __NR_getpid){
210 panic("check_ptrace : SYSEMU_SINGLESTEP doesn't singlestep");
211 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
214 panic("check_sysemu : failed to modify system "
215 "call return, errno = %d", errno);
219 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
222 sysemu_supported = 2;
225 if ( !force_sysemu_disabled )
226 set_using_sysemu(sysemu_supported);
230 stop_ptraced_child(pid, stack, 1, 0);
235 static void __init check_ptrace(void)
238 int pid, syscall, n, status;
240 printk("Checking that ptrace can change system call numbers...");
241 pid = start_ptraced_child(&stack);
243 if(ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
244 panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d", errno);
247 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
248 panic("check_ptrace : ptrace failed, errno = %d",
250 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
252 panic("check_ptrace : wait failed, errno = %d", errno);
253 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP + 0x80))
254 panic("check_ptrace : expected SIGTRAP + 0x80, "
255 "got status = %d", status);
257 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
259 if(syscall == __NR_getpid){
260 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
263 panic("check_ptrace : failed to modify system "
264 "call, errno = %d", errno);
268 stop_ptraced_child(pid, stack, 0, 1);
273 void os_early_checks(void)
278 #ifdef UML_CONFIG_MODE_SKAS
279 static inline void check_skas3_ptrace_support(void)
281 struct ptrace_faultinfo fi;
285 printf("Checking for the skas3 patch in the host...");
286 pid = start_ptraced_child(&stack);
288 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
290 ptrace_faultinfo = 0;
292 printf("not found\n");
297 if (!ptrace_faultinfo)
298 printf("found but disabled on command line\n");
304 stop_ptraced_child(pid, stack, 1, 1);
307 int can_do_skas(void)
309 printf("Checking for /proc/mm...");
310 if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
312 printf("not found\n");
316 printf("found but disabled on command line\n");
321 check_skas3_ptrace_support();
325 int can_do_skas(void)