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