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"
23 #include "signal_kern.h"
24 #include "signal_user.h"
25 #include "sysdep/ptrace.h"
26 #include "sysdep/sigcontext.h"
28 #include "ptrace_user.h"
29 #include "time_user.h"
32 #include "uml-config.h"
33 #include "choose-mode.h"
35 #ifdef UML_CONFIG_MODE_SKAS
37 #include "skas_ptrace.h"
38 #include "registers.h"
41 void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
45 if(sig_stack != NULL){
46 pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER);
47 set_sigstack(sig_stack, pages * page_size());
50 if(usr1_handler) set_handler(SIGUSR1, usr1_handler, flags, -1);
53 void init_new_thread_signals(int altstack)
55 int flags = altstack ? SA_ONSTACK : 0;
57 set_handler(SIGSEGV, (__sighandler_t) sig_handler, flags,
58 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
59 set_handler(SIGTRAP, (__sighandler_t) sig_handler, flags,
60 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
61 set_handler(SIGFPE, (__sighandler_t) sig_handler, flags,
62 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
63 set_handler(SIGILL, (__sighandler_t) sig_handler, flags,
64 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
65 set_handler(SIGBUS, (__sighandler_t) sig_handler, flags,
66 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
67 set_handler(SIGUSR2, (__sighandler_t) sig_handler,
68 flags, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
69 signal(SIGHUP, SIG_IGN);
71 init_irq_signals(altstack);
77 unsigned long temp_stack;
82 /* See above for why sigkill is here */
84 int sigkill = SIGKILL;
86 int outer_tramp(void *arg)
92 t->pid = clone(t->tramp, (void *) t->temp_stack + page_size()/2,
93 t->flags, t->tramp_data);
94 if(t->pid > 0) wait_for_stop(t->pid, SIGSTOP, PTRACE_CONT, NULL);
95 kill(os_getpid(), sig);
99 int start_fork_tramp(void *thread_arg, unsigned long temp_stack,
100 int clone_flags, int (*tramp)(void *))
104 int new_pid, status, err;
106 /* The trampoline will run on the temporary stack */
107 sp = stack_sp(temp_stack);
109 clone_flags |= CLONE_FILES | SIGCHLD;
112 arg.tramp_data = thread_arg;
113 arg.temp_stack = temp_stack;
114 arg.flags = clone_flags;
116 /* Start the process and wait for it to kill itself */
117 new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
121 CATCH_EINTR(err = waitpid(new_pid, &status, 0));
123 panic("Waiting for outer trampoline failed - errno = %d",
126 if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
127 panic("outer trampoline didn't exit with SIGKILL, "
128 "status = %d", status);
133 static int ptrace_child(void *arg)
136 int pid = os_getpid(), ppid = getppid();
139 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
141 os_kill_process(pid, 0);
143 os_stop_process(pid);
145 /*This syscall will be intercepted by the parent. Don't call more than
147 sc_result = os_getpid();
149 if (sc_result == pid)
150 ret = 1; /*Nothing modified by the parent, we are running
152 else if (sc_result == ppid)
153 ret = 0; /*Expected in check_ptrace and check_sysemu when they
154 succeed in modifying the stack frame*/
156 ret = 2; /*Serious trouble! This could be caused by a bug in
157 host 2.6 SKAS3/2.6 patch before release -V6, together
158 with a bug in the UML code itself.*/
162 static int start_ptraced_child(void **stack_out)
168 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
169 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
170 if(stack == MAP_FAILED)
171 panic("check_ptrace : mmap failed, errno = %d", errno);
172 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
173 pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
175 panic("check_ptrace : clone failed, errno = %d", errno);
176 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
178 panic("check_ptrace : wait failed, errno = %d", errno);
179 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
180 panic("check_ptrace : expected SIGSTOP, got status = %d",
187 /* When testing for SYSEMU support, if it is one of the broken versions, we must
188 * just avoid using sysemu, not panic, but only if SYSEMU features are broken.
189 * So only for SYSEMU features we test mustpanic, while normal host features
190 * must work anyway!*/
191 static int stop_ptraced_child(int pid, void *stack, int exitcode, int mustpanic)
193 int status, n, ret = 0;
195 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
196 panic("check_ptrace : ptrace failed, errno = %d", errno);
197 CATCH_EINTR(n = waitpid(pid, &status, 0));
198 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
199 int exit_with = WEXITSTATUS(status);
201 printk("check_ptrace : child exited with status 2. "
202 "Serious trouble happening! Try updating your "
203 "host skas patch!\nDisabling SYSEMU support.");
204 printk("check_ptrace : child exited with exitcode %d, while "
205 "expecting %d; status 0x%x", exit_with,
214 if(munmap(stack, PAGE_SIZE) < 0)
215 panic("check_ptrace : munmap failed, errno = %d", errno);
219 static int force_sysemu_disabled = 0;
221 static int __init nosysemu_cmd_param(char *str, int* add)
223 force_sysemu_disabled = 1;
227 __uml_setup("nosysemu", nosysemu_cmd_param,
229 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
230 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
231 " behaviour of ptrace() and helps reducing host context switch rate.\n"
232 " To make it working, you need a kernel patch for your host, too.\n"
233 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further information.\n\n");
235 static void __init check_sysemu(void)
238 int pid, syscall, n, status, count=0;
240 printk("Checking syscall emulation patch for ptrace...");
241 sysemu_supported = 0;
242 pid = start_ptraced_child(&stack);
244 if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
247 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
249 panic("check_sysemu : wait failed, errno = %d", errno);
250 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
251 panic("check_sysemu : expected SIGTRAP, "
252 "got status = %d", status);
254 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
257 panic("check_sysemu : failed to modify system "
258 "call return, errno = %d", errno);
260 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
263 sysemu_supported = 1;
265 set_using_sysemu(!force_sysemu_disabled);
267 printk("Checking advanced syscall emulation patch for ptrace...");
268 pid = start_ptraced_child(&stack);
271 if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
273 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
275 panic("check_ptrace : wait failed, errno = %d", errno);
276 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
277 panic("check_ptrace : expected (SIGTRAP|SYSCALL_TRAP), "
278 "got status = %d", status);
280 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
282 if(syscall == __NR_getpid){
284 panic("check_ptrace : SYSEMU_SINGLESTEP doesn't singlestep");
285 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
288 panic("check_sysemu : failed to modify system "
289 "call return, errno = %d", errno);
293 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
296 sysemu_supported = 2;
299 if ( !force_sysemu_disabled )
300 set_using_sysemu(sysemu_supported);
304 stop_ptraced_child(pid, stack, 1, 0);
309 void __init check_ptrace(void)
312 int pid, syscall, n, status;
314 printk("Checking that ptrace can change system call numbers...");
315 pid = start_ptraced_child(&stack);
317 if (ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
318 panic("check_ptrace: PTRACE_SETOPTIONS failed, errno = %d", errno);
321 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
322 panic("check_ptrace : ptrace failed, errno = %d",
324 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
326 panic("check_ptrace : wait failed, errno = %d", errno);
327 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP + 0x80))
328 panic("check_ptrace : expected SIGTRAP + 0x80, "
329 "got status = %d", status);
331 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
333 if(syscall == __NR_getpid){
334 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
337 panic("check_ptrace : failed to modify system "
338 "call, errno = %d", errno);
342 stop_ptraced_child(pid, stack, 0, 1);
347 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
353 n = sigsetjmp(buf, 1);
360 void forward_pending_sigio(int target)
364 if(sigpending(&sigs))
365 panic("forward_pending_sigio : sigpending failed");
366 if(sigismember(&sigs, SIGIO))
370 #ifdef UML_CONFIG_MODE_SKAS
371 static inline int check_skas3_ptrace_support(void)
373 struct ptrace_faultinfo fi;
377 printf("Checking for the skas3 patch in the host...");
378 pid = start_ptraced_child(&stack);
380 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
383 printf("not found\n");
393 stop_ptraced_child(pid, stack, 1, 1);
398 int can_do_skas(void)
402 printf("Checking for /proc/mm...");
403 if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
404 printf("not found\n");
411 ret = check_skas3_ptrace_support();
416 int can_do_skas(void)