37828e5b35269903eff04e79f6885cee56c4e6b8
[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 "sysdep/ptrace.h"
27 #include "sysdep/sigcontext.h"
28 #include "irq_user.h"
29 #include "ptrace_user.h"
30 #include "time_user.h"
31 #include "init.h"
32 #include "os.h"
33 #include "uml-config.h"
34 #include "choose-mode.h"
35 #include "mode.h"
36 #include "tempfile.h"
37
38 int protect_memory(unsigned long addr, unsigned long len, int r, int w, int x,
39                    int must_succeed)
40 {
41         int err;
42
43         err = os_protect_memory((void *) addr, len, r, w, x);
44         if(err < 0){
45                 if(must_succeed)
46                         panic("protect failed, err = %d", -err);
47                 else return(err);
48         }
49         return(0);
50 }
51
52 /*
53  *-------------------------
54  * only for tt mode (will be deleted in future...)
55  *-------------------------
56  */
57
58 struct tramp {
59         int (*tramp)(void *);
60         void *tramp_data;
61         unsigned long temp_stack;
62         int flags;
63         int pid;
64 };
65
66 /* See above for why sigkill is here */
67
68 int sigkill = SIGKILL;
69
70 int outer_tramp(void *arg)
71 {
72         struct tramp *t;
73         int sig = sigkill;
74
75         t = arg;
76         t->pid = clone(t->tramp, (void *) t->temp_stack + page_size()/2,
77                        t->flags, t->tramp_data);
78         if(t->pid > 0) wait_for_stop(t->pid, SIGSTOP, PTRACE_CONT, NULL);
79         kill(os_getpid(), sig);
80         _exit(0);
81 }
82
83 int start_fork_tramp(void *thread_arg, unsigned long temp_stack,
84                      int clone_flags, int (*tramp)(void *))
85 {
86         struct tramp arg;
87         unsigned long sp;
88         int new_pid, status, err;
89
90         /* The trampoline will run on the temporary stack */
91         sp = stack_sp(temp_stack);
92
93         clone_flags |= CLONE_FILES | SIGCHLD;
94
95         arg.tramp = tramp;
96         arg.tramp_data = thread_arg;
97         arg.temp_stack = temp_stack;
98         arg.flags = clone_flags;
99
100         /* Start the process and wait for it to kill itself */
101         new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
102         if(new_pid < 0)
103                 return(new_pid);
104
105         CATCH_EINTR(err = waitpid(new_pid, &status, 0));
106         if(err < 0)
107                 panic("Waiting for outer trampoline failed - errno = %d",
108                       errno);
109
110         if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
111                 panic("outer trampoline didn't exit with SIGKILL, "
112                       "status = %d", status);
113
114         return(arg.pid);
115 }
116
117 void forward_pending_sigio(int target)
118 {
119         sigset_t sigs;
120
121         if(sigpending(&sigs))
122                 panic("forward_pending_sigio : sigpending failed");
123         if(sigismember(&sigs, SIGIO))
124                 kill(target, SIGIO);
125 }
126