Merge git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-2.6] / 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 <sys/time.h>
14 #include <sys/ptrace.h>
15 #include <linux/ptrace.h>
16 #include <sys/wait.h>
17 #include <sys/mman.h>
18 #include <asm/ptrace.h>
19 #include <asm/unistd.h>
20 #include <asm/page.h>
21 #include "user_util.h"
22 #include "kern_util.h"
23 #include "user.h"
24 #include "signal_kern.h"
25 #include "sysdep/ptrace.h"
26 #include "sysdep/sigcontext.h"
27 #include "irq_user.h"
28 #include "ptrace_user.h"
29 #include "init.h"
30 #include "os.h"
31 #include "uml-config.h"
32 #include "choose-mode.h"
33 #include "mode.h"
34 #include "tempfile.h"
35
36 int protect_memory(unsigned long addr, unsigned long len, int r, int w, int x,
37                    int must_succeed)
38 {
39         int err;
40
41         err = os_protect_memory((void *) addr, len, r, w, x);
42         if(err < 0){
43                 if(must_succeed)
44                         panic("protect failed, err = %d", -err);
45                 else return(err);
46         }
47         return(0);
48 }
49
50 void kill_child_dead(int pid)
51 {
52         kill(pid, SIGKILL);
53         kill(pid, SIGCONT);
54         do {
55                 int n;
56                 CATCH_EINTR(n = waitpid(pid, NULL, 0));
57                 if (n > 0)
58                         kill(pid, SIGCONT);
59                 else
60                         break;
61         } while(1);
62 }
63
64 void stop(void)
65 {
66         while(1) sleep(1000000);
67 }
68
69 int wait_for_stop(int pid, int sig, int cont_type, void *relay)
70 {
71         sigset_t *relay_signals = relay;
72         int status, ret;
73
74         while(1){
75                 CATCH_EINTR(ret = waitpid(pid, &status, WUNTRACED));
76                 if((ret < 0) ||
77                    !WIFSTOPPED(status) || (WSTOPSIG(status) != sig)){
78                         if(ret < 0){
79                                 printk("wait failed, errno = %d\n",
80                                        errno);
81                         }
82                         else if(WIFEXITED(status))
83                                 printk("process %d exited with status %d\n",
84                                        pid, WEXITSTATUS(status));
85                         else if(WIFSIGNALED(status))
86                                 printk("process %d exited with signal %d\n",
87                                        pid, WTERMSIG(status));
88                         else if((WSTOPSIG(status) == SIGVTALRM) ||
89                                 (WSTOPSIG(status) == SIGALRM) ||
90                                 (WSTOPSIG(status) == SIGIO) ||
91                                 (WSTOPSIG(status) == SIGPROF) ||
92                                 (WSTOPSIG(status) == SIGCHLD) ||
93                                 (WSTOPSIG(status) == SIGWINCH) ||
94                                 (WSTOPSIG(status) == SIGINT)){
95                                 ptrace(cont_type, pid, 0, WSTOPSIG(status));
96                                 continue;
97                         }
98                         else if((relay_signals != NULL) &&
99                                 sigismember(relay_signals, WSTOPSIG(status))){
100                                 ptrace(cont_type, pid, 0, WSTOPSIG(status));
101                                 continue;
102                         }
103                         else printk("process %d stopped with signal %d\n",
104                                     pid, WSTOPSIG(status));
105                         panic("wait_for_stop failed to wait for %d to stop "
106                               "with %d\n", pid, sig);
107                 }
108                 return(status);
109         }
110 }
111
112 void forward_ipi(int fd, int pid)
113 {
114         int err;
115
116         err = os_set_owner(fd, pid);
117         if(err < 0)
118                 printk("forward_ipi: set_owner failed, fd = %d, me = %d, "
119                        "target = %d, err = %d\n", fd, os_getpid(), pid, -err);
120 }
121
122 /*
123  *-------------------------
124  * only for tt mode (will be deleted in future...)
125  *-------------------------
126  */
127
128 struct tramp {
129         int (*tramp)(void *);
130         void *tramp_data;
131         unsigned long temp_stack;
132         int flags;
133         int pid;
134 };
135
136 /* See above for why sigkill is here */
137
138 int sigkill = SIGKILL;
139
140 int outer_tramp(void *arg)
141 {
142         struct tramp *t;
143         int sig = sigkill;
144
145         t = arg;
146         t->pid = clone(t->tramp, (void *) t->temp_stack + page_size()/2,
147                        t->flags, t->tramp_data);
148         if(t->pid > 0) wait_for_stop(t->pid, SIGSTOP, PTRACE_CONT, NULL);
149         kill(os_getpid(), sig);
150         _exit(0);
151 }
152
153 int start_fork_tramp(void *thread_arg, unsigned long temp_stack,
154                      int clone_flags, int (*tramp)(void *))
155 {
156         struct tramp arg;
157         unsigned long sp;
158         int new_pid, status, err;
159
160         /* The trampoline will run on the temporary stack */
161         sp = stack_sp(temp_stack);
162
163         clone_flags |= CLONE_FILES | SIGCHLD;
164
165         arg.tramp = tramp;
166         arg.tramp_data = thread_arg;
167         arg.temp_stack = temp_stack;
168         arg.flags = clone_flags;
169
170         /* Start the process and wait for it to kill itself */
171         new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
172         if(new_pid < 0)
173                 return(new_pid);
174
175         CATCH_EINTR(err = waitpid(new_pid, &status, 0));
176         if(err < 0)
177                 panic("Waiting for outer trampoline failed - errno = %d",
178                       errno);
179
180         if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
181                 panic("outer trampoline didn't exit with SIGKILL, "
182                       "status = %d", status);
183
184         return(arg.pid);
185 }
186
187 void forward_pending_sigio(int target)
188 {
189         sigset_t sigs;
190
191         if(sigpending(&sigs))
192                 panic("forward_pending_sigio : sigpending failed");
193         if(sigismember(&sigs, SIGIO))
194                 kill(target, SIGIO);
195 }
196