Merge /spare/repo/linux-2.6/
[pandora-kernel.git] / arch / um / os-Linux / tt.c
1 /*
2  * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <signal.h>
9 #include <sched.h>
10 #include <errno.h>
11 #include <stdarg.h>
12 #include <stdlib.h>
13 #include <setjmp.h>
14 #include <sys/time.h>
15 #include <sys/ptrace.h>
16 #include <linux/ptrace.h>
17 #include <sys/wait.h>
18 #include <sys/mman.h>
19 #include <asm/ptrace.h>
20 #include <asm/unistd.h>
21 #include <asm/page.h>
22 #include "user_util.h"
23 #include "kern_util.h"
24 #include "user.h"
25 #include "signal_kern.h"
26 #include "signal_user.h"
27 #include "sysdep/ptrace.h"
28 #include "sysdep/sigcontext.h"
29 #include "irq_user.h"
30 #include "ptrace_user.h"
31 #include "time_user.h"
32 #include "init.h"
33 #include "os.h"
34 #include "uml-config.h"
35 #include "choose-mode.h"
36 #include "mode.h"
37 #include "tempfile.h"
38
39 int protect_memory(unsigned long addr, unsigned long len, int r, int w, int x,
40                    int must_succeed)
41 {
42         int err;
43
44         err = os_protect_memory((void *) addr, len, r, w, x);
45         if(err < 0){
46                 if(must_succeed)
47                         panic("protect failed, err = %d", -err);
48                 else return(err);
49         }
50         return(0);
51 }
52
53 /*
54  *-------------------------
55  * only for tt mode (will be deleted in future...)
56  *-------------------------
57  */
58
59 struct tramp {
60         int (*tramp)(void *);
61         void *tramp_data;
62         unsigned long temp_stack;
63         int flags;
64         int pid;
65 };
66
67 /* See above for why sigkill is here */
68
69 int sigkill = SIGKILL;
70
71 int outer_tramp(void *arg)
72 {
73         struct tramp *t;
74         int sig = sigkill;
75
76         t = arg;
77         t->pid = clone(t->tramp, (void *) t->temp_stack + page_size()/2,
78                        t->flags, t->tramp_data);
79         if(t->pid > 0) wait_for_stop(t->pid, SIGSTOP, PTRACE_CONT, NULL);
80         kill(os_getpid(), sig);
81         _exit(0);
82 }
83
84 int start_fork_tramp(void *thread_arg, unsigned long temp_stack,
85                      int clone_flags, int (*tramp)(void *))
86 {
87         struct tramp arg;
88         unsigned long sp;
89         int new_pid, status, err;
90
91         /* The trampoline will run on the temporary stack */
92         sp = stack_sp(temp_stack);
93
94         clone_flags |= CLONE_FILES | SIGCHLD;
95
96         arg.tramp = tramp;
97         arg.tramp_data = thread_arg;
98         arg.temp_stack = temp_stack;
99         arg.flags = clone_flags;
100
101         /* Start the process and wait for it to kill itself */
102         new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
103         if(new_pid < 0)
104                 return(new_pid);
105
106         CATCH_EINTR(err = waitpid(new_pid, &status, 0));
107         if(err < 0)
108                 panic("Waiting for outer trampoline failed - errno = %d",
109                       errno);
110
111         if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
112                 panic("outer trampoline didn't exit with SIGKILL, "
113                       "status = %d", status);
114
115         return(arg.pid);
116 }
117
118 void forward_pending_sigio(int target)
119 {
120         sigset_t sigs;
121
122         if(sigpending(&sigs))
123                 panic("forward_pending_sigio : sigpending failed");
124         if(sigismember(&sigs, SIGIO))
125                 kill(target, SIGIO);
126 }
127