vserver 1.9.3
[linux-2.6.git] / arch / um / kernel / process.c
index 4c3aa45..4468f24 100644 (file)
@@ -9,18 +9,17 @@
 #include <sched.h>
 #include <errno.h>
 #include <stdarg.h>
-#include <fcntl.h>
 #include <stdlib.h>
 #include <setjmp.h>
 #include <sys/time.h>
 #include <sys/ptrace.h>
-#include <sys/ioctl.h>
 #include <sys/wait.h>
 #include <sys/mman.h>
 #include <asm/ptrace.h>
 #include <asm/sigcontext.h>
 #include <asm/unistd.h>
 #include <asm/page.h>
+#include <asm/user.h>
 #include "user_util.h"
 #include "kern_util.h"
 #include "user.h"
@@ -47,7 +46,7 @@ void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
        int flags = 0, pages;
 
        if(sig_stack != NULL){
-               pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER) - 2;
+               pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER);
                set_sigstack(sig_stack, pages * page_size());
                flags = SA_ONSTACK;
        }
@@ -71,8 +70,7 @@ void init_new_thread_signals(int altstack)
        set_handler(SIGWINCH, (__sighandler_t) sig_handler, flags, 
                    SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
        set_handler(SIGUSR2, (__sighandler_t) sig_handler, 
-                   SA_NOMASK | flags, -1);
-       (void) CHOOSE_MODE(signal(SIGCHLD, SIG_IGN), (void *) 0);
+                   flags, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
        signal(SIGHUP, SIG_IGN);
 
        init_irq_signals(altstack);
@@ -122,24 +120,19 @@ int start_fork_tramp(void *thread_arg, unsigned long temp_stack,
 
        /* Start the process and wait for it to kill itself */
        new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
-       if(new_pid < 0) return(-errno);
-       while((err = waitpid(new_pid, &status, 0) < 0) && (errno == EINTR)) ;
-       if(err < 0) panic("Waiting for outer trampoline failed - errno = %d", 
-                         errno);
-       if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
-               panic("outer trampoline didn't exit with SIGKILL");
-
-       return(arg.pid);
-}
+       if(new_pid < 0)
+               return(new_pid);
 
-void suspend_new_thread(int fd)
-{
-       char c;
+       CATCH_EINTR(err = waitpid(new_pid, &status, 0));
+       if(err < 0)
+               panic("Waiting for outer trampoline failed - errno = %d",
+                     errno);
 
-       os_stop_process(os_getpid());
+       if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
+               panic("outer trampoline didn't exit with SIGKILL, "
+                     "status = %d", status);
 
-       if(read(fd, &c, sizeof(c)) != sizeof(c))
-               panic("read failed in suspend_new_thread");
+       return(arg.pid);
 }
 
 static int ptrace_child(void *arg)
@@ -168,7 +161,7 @@ static int start_ptraced_child(void **stack_out)
        pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
        if(pid < 0)
                panic("check_ptrace : clone failed, errno = %d", errno);
-       n = waitpid(pid, &status, WUNTRACED);
+       CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
        if(n < 0)
                panic("check_ptrace : wait failed, errno = %d", errno);
        if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
@@ -185,7 +178,7 @@ static void stop_ptraced_child(int pid, void *stack, int exitcode)
 
        if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
                panic("check_ptrace : ptrace failed, errno = %d", errno);
-       n = waitpid(pid, &status, 0);
+       CATCH_EINTR(n = waitpid(pid, &status, 0));
        if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode))
                panic("check_ptrace : child exited with status 0x%x", status);
 
@@ -193,6 +186,66 @@ static void stop_ptraced_child(int pid, void *stack, int exitcode)
                panic("check_ptrace : munmap failed, errno = %d", errno);
 }
 
+static int force_sysemu_disabled = 0;
+
+static int __init nosysemu_cmd_param(char *str, int* add)
+{
+       force_sysemu_disabled = 1;
+       return 0;
+}
+
+__uml_setup("nosysemu", nosysemu_cmd_param,
+               "nosysemu\n"
+               "    Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
+               "    SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
+               "    behaviour of ptrace() and helps reducing host context switch rate.\n"
+               "    To make it working, you need a kernel patch for your host, too.\n"
+               "    See http://perso.wanadoo.fr/laurent.vivier/UML/ for further information.\n");
+
+static void __init check_sysemu(void)
+{
+       void *stack;
+       int pid, n, status;
+
+       if (mode_tt)
+               return;
+
+       printk("Checking syscall emulation patch for ptrace...");
+       sysemu_supported = 0;
+       pid = start_ptraced_child(&stack);
+       if(ptrace(PTRACE_SYSEMU, pid, 0, 0) >= 0) {
+               struct user_regs_struct regs;
+
+               CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
+               if (n < 0)
+                       panic("check_ptrace : wait failed, errno = %d", errno);
+               if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
+                       panic("check_ptrace : expected SIGTRAP, "
+                             "got status = %d", status);
+
+               if (ptrace(PTRACE_GETREGS, pid, 0, &regs) < 0)
+                       panic("check_ptrace : failed to read child "
+                             "registers, errno = %d", errno);
+               regs.orig_eax = pid;
+               if (ptrace(PTRACE_SETREGS, pid, 0, &regs) < 0)
+                       panic("check_ptrace : failed to modify child "
+                             "registers, errno = %d", errno);
+
+               stop_ptraced_child(pid, stack, 0);
+
+               sysemu_supported = 1;
+               printk("found\n");
+       }
+       else
+       {
+               stop_ptraced_child(pid, stack, 1);
+               sysemu_supported = 0;
+               printk("missing\n");
+       }
+
+       set_using_sysemu(!force_sysemu_disabled);
+}
+
 void __init check_ptrace(void)
 {
        void *stack;
@@ -205,7 +258,7 @@ void __init check_ptrace(void)
                if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
                        panic("check_ptrace : ptrace failed, errno = %d", 
                              errno);
-               n = waitpid(pid, &status, WUNTRACED);
+               CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
                if(n < 0)
                        panic("check_ptrace : wait failed, errno = %d", errno);
                if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
@@ -225,15 +278,16 @@ void __init check_ptrace(void)
        }
        stop_ptraced_child(pid, stack, 0);
        printk("OK\n");
+       check_sysemu();
 }
 
 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
 {
-       jmp_buf buf;
+       sigjmp_buf buf;
        int n;
 
        *jmp_ptr = &buf;
-       n = setjmp(buf);
+       n = sigsetjmp(buf, 1);
        if(n != 0)
                return(n);
        (*fn)(arg);
@@ -273,7 +327,7 @@ int can_do_skas(void)
        stop_ptraced_child(pid, stack, 1);
 
        printf("Checking for /proc/mm...");
-       if(access("/proc/mm", W_OK)){
+       if(os_access("/proc/mm", OS_ACC_W_OK) < 0){
                printf("not found\n");
                ret = 0;
        }