[PATCH] uml: make TT mode compile after setjmp-related changes
[linux-2.6/libata-dev.git] / arch / um / os-Linux / tt.c
blob3dc3a02d626318ba0337c8471237387099df5729
1 /*
2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
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"
36 int protect_memory(unsigned long addr, unsigned long len, int r, int w, int x,
37 int must_succeed)
39 int err;
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);
47 return(0);
50 void kill_child_dead(int pid)
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);
64 void stop(void)
66 while(1) sleep(1000000);
69 int wait_for_stop(int pid, int sig, int cont_type, void *relay)
71 sigset_t *relay_signals = relay;
72 int status, ret;
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);
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;
98 else if((relay_signals != NULL) &&
99 sigismember(relay_signals, WSTOPSIG(status))){
100 ptrace(cont_type, pid, 0, WSTOPSIG(status));
101 continue;
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);
108 return(status);
112 void forward_ipi(int fd, int pid)
114 int err;
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);
123 *-------------------------
124 * only for tt mode (will be deleted in future...)
125 *-------------------------
128 struct tramp {
129 int (*tramp)(void *);
130 void *tramp_data;
131 unsigned long temp_stack;
132 int flags;
133 int pid;
136 /* See above for why sigkill is here */
138 int sigkill = SIGKILL;
140 int outer_tramp(void *arg)
142 struct tramp *t;
143 int sig = sigkill;
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);
153 int start_fork_tramp(void *thread_arg, unsigned long temp_stack,
154 int clone_flags, int (*tramp)(void *))
156 struct tramp arg;
157 unsigned long sp;
158 int new_pid, status, err;
160 /* The trampoline will run on the temporary stack */
161 sp = stack_sp(temp_stack);
163 clone_flags |= CLONE_FILES | SIGCHLD;
165 arg.tramp = tramp;
166 arg.tramp_data = thread_arg;
167 arg.temp_stack = temp_stack;
168 arg.flags = clone_flags;
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);
175 CATCH_EINTR(err = waitpid(new_pid, &status, 0));
176 if(err < 0)
177 panic("Waiting for outer trampoline failed - errno = %d",
178 errno);
180 if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
181 panic("outer trampoline didn't exit with SIGKILL, "
182 "status = %d", status);
184 return(arg.pid);
187 void forward_pending_sigio(int target)
189 sigset_t sigs;
191 if(sigpending(&sigs))
192 panic("forward_pending_sigio : sigpending failed");
193 if(sigismember(&sigs, SIGIO))
194 kill(target, SIGIO);