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"
36 #ifdef UML_CONFIG_MODE_SKAS
38 #include "skas_ptrace.h"
39 #include "registers.h"
42 void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
46 if(sig_stack != NULL){
47 pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER);
48 set_sigstack(sig_stack, pages * page_size());
51 if(usr1_handler) set_handler(SIGUSR1, usr1_handler, flags, -1);
54 void init_new_thread_signals(int altstack)
56 int flags = altstack ? SA_ONSTACK : 0;
58 set_handler(SIGSEGV, (__sighandler_t) sig_handler, flags,
59 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
60 set_handler(SIGTRAP, (__sighandler_t) sig_handler, flags,
61 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
62 set_handler(SIGFPE, (__sighandler_t) sig_handler, flags,
63 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
64 set_handler(SIGILL, (__sighandler_t) sig_handler, flags,
65 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
66 set_handler(SIGBUS, (__sighandler_t) sig_handler, flags,
67 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
68 set_handler(SIGUSR2, (__sighandler_t) sig_handler,
69 flags, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
70 signal(SIGHUP, SIG_IGN);
72 init_irq_signals(altstack);
78 unsigned long temp_stack;
83 /* See above for why sigkill is here */
85 int sigkill = SIGKILL;
87 int outer_tramp(void *arg)
93 t->pid = clone(t->tramp, (void *) t->temp_stack + page_size()/2,
94 t->flags, t->tramp_data);
95 if(t->pid > 0) wait_for_stop(t->pid, SIGSTOP, PTRACE_CONT, NULL);
96 kill(os_getpid(), sig);
100 int start_fork_tramp(void *thread_arg, unsigned long temp_stack,
101 int clone_flags, int (*tramp)(void *))
105 int new_pid, status, err;
107 /* The trampoline will run on the temporary stack */
108 sp = stack_sp(temp_stack);
110 clone_flags |= CLONE_FILES | SIGCHLD;
113 arg.tramp_data = thread_arg;
114 arg.temp_stack = temp_stack;
115 arg.flags = clone_flags;
117 /* Start the process and wait for it to kill itself */
118 new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
122 CATCH_EINTR(err = waitpid(new_pid, &status, 0));
124 panic("Waiting for outer trampoline failed - errno = %d",
127 if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
128 panic("outer trampoline didn't exit with SIGKILL, "
129 "status = %d", status);
134 static int ptrace_child(void)
137 int pid = os_getpid(), ppid = getppid();
140 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
142 os_kill_process(pid, 0);
144 os_stop_process(pid);
146 /*This syscall will be intercepted by the parent. Don't call more than
148 sc_result = os_getpid();
150 if (sc_result == pid)
151 ret = 1; /*Nothing modified by the parent, we are running
153 else if (sc_result == ppid)
154 ret = 0; /*Expected in check_ptrace and check_sysemu when they
155 succeed in modifying the stack frame*/
157 ret = 2; /*Serious trouble! This could be caused by a bug in
158 host 2.6 SKAS3/2.6 patch before release -V6, together
159 with a bug in the UML code itself.*/
163 static int start_ptraced_child(void)
172 panic("check_ptrace : fork failed, errno = %d", errno);
173 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
175 panic("check_ptrace : wait failed, errno = %d", errno);
176 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
177 panic("check_ptrace : expected SIGSTOP, got status = %d",
183 /* When testing for SYSEMU support, if it is one of the broken versions, we must
184 * just avoid using sysemu, not panic, but only if SYSEMU features are broken.
185 * So only for SYSEMU features we test mustpanic, while normal host features
186 * must work anyway!*/
187 static int stop_ptraced_child(int pid, int exitcode, int mustexit)
189 int status, n, ret = 0;
191 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
192 panic("stop_ptraced_child : ptrace failed, errno = %d", errno);
193 CATCH_EINTR(n = waitpid(pid, &status, 0));
194 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
195 int exit_with = WEXITSTATUS(status);
197 printk("check_ptrace : child exited with status 2. "
198 "Serious trouble happening! Try updating your "
199 "host skas patch!\nDisabling SYSEMU support.");
200 printk("check_ptrace : child exited with exitcode %d, while "
201 "expecting %d; status 0x%x", exit_with,
213 static int force_sysemu_disabled = 0;
215 int ptrace_faultinfo = 1;
218 static int __init skas0_cmd_param(char *str, int* add)
220 ptrace_faultinfo = proc_mm = 0;
224 static int __init nosysemu_cmd_param(char *str, int* add)
226 force_sysemu_disabled = 1;
230 __uml_setup("skas0", skas0_cmd_param,
232 " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
233 " specify mode=tt.\n\n");
235 __uml_setup("nosysemu", nosysemu_cmd_param,
237 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
238 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
239 " behaviour of ptrace() and helps reducing host context switch rate.\n"
240 " To make it working, you need a kernel patch for your host, too.\n"
241 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further information.\n\n");
243 static void __init check_sysemu(void)
245 int pid, syscall, n, status, count=0;
247 printk("Checking syscall emulation patch for ptrace...");
248 sysemu_supported = 0;
249 pid = start_ptraced_child();
251 if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
254 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
256 panic("check_sysemu : wait failed, errno = %d", errno);
257 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
258 panic("check_sysemu : expected SIGTRAP, "
259 "got status = %d", status);
261 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
264 panic("check_sysemu : failed to modify system "
265 "call return, errno = %d", errno);
267 if (stop_ptraced_child(pid, 0, 0) < 0)
270 sysemu_supported = 1;
272 set_using_sysemu(!force_sysemu_disabled);
274 printk("Checking advanced syscall emulation patch for ptrace...");
275 pid = start_ptraced_child();
278 if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
280 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
282 panic("check_ptrace : wait failed, errno = %d", errno);
283 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
284 panic("check_ptrace : expected (SIGTRAP|SYSCALL_TRAP), "
285 "got status = %d", status);
287 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
289 if(syscall == __NR_getpid){
291 panic("check_ptrace : SYSEMU_SINGLESTEP doesn't singlestep");
292 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
295 panic("check_sysemu : failed to modify system "
296 "call return, errno = %d", errno);
300 if (stop_ptraced_child(pid, 0, 0) < 0)
303 sysemu_supported = 2;
306 if ( !force_sysemu_disabled )
307 set_using_sysemu(sysemu_supported);
311 stop_ptraced_child(pid, 1, 0);
316 void __init check_ptrace(void)
318 int pid, syscall, n, status;
320 printk("Checking that ptrace can change system call numbers...");
321 pid = start_ptraced_child();
323 if (ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
324 panic("check_ptrace: PTRACE_SETOPTIONS failed, errno = %d", errno);
327 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
328 panic("check_ptrace : ptrace failed, errno = %d",
330 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
332 panic("check_ptrace : wait failed, errno = %d", errno);
333 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP + 0x80))
334 panic("check_ptrace : expected SIGTRAP + 0x80, "
335 "got status = %d", status);
337 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
339 if(syscall == __NR_getpid){
340 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
343 panic("check_ptrace : failed to modify system "
344 "call, errno = %d", errno);
348 stop_ptraced_child(pid, 0, 1);
353 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
359 n = sigsetjmp(buf, 1);
366 void forward_pending_sigio(int target)
370 if(sigpending(&sigs))
371 panic("forward_pending_sigio : sigpending failed");
372 if(sigismember(&sigs, SIGIO))
376 extern void *__syscall_stub_start, __syscall_stub_end;
378 #ifdef UML_CONFIG_MODE_SKAS
380 static inline void check_skas3_ptrace_support(void)
382 struct ptrace_faultinfo fi;
385 printf("Checking for the skas3 patch in the host...");
386 pid = start_ptraced_child();
388 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
390 ptrace_faultinfo = 0;
392 printf("not found\n");
398 if (!ptrace_faultinfo)
399 printf("found but disabled on command line\n");
405 stop_ptraced_child(pid, 1, 1);
408 int can_do_skas(void)
410 printf("Checking for /proc/mm...");
411 if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
413 printf("not found\n");
416 printf("found but disabled on command line\n");
421 check_skas3_ptrace_support();
425 int can_do_skas(void)