694a9c9f4a75392e8c9dbde8cd4bb6a3b0c9ebfb
[linux-2.6.git] / arch / um / kernel / process.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 <sys/wait.h>
17 #include <sys/mman.h>
18 #include <asm/ptrace.h>
19 #include <asm/sigcontext.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 "process.h"
26 #include "signal_kern.h"
27 #include "signal_user.h"
28 #include "sysdep/ptrace.h"
29 #include "sysdep/sigcontext.h"
30 #include "irq_user.h"
31 #include "ptrace_user.h"
32 #include "time_user.h"
33 #include "init.h"
34 #include "os.h"
35 #include "uml-config.h"
36 #include "choose-mode.h"
37 #include "mode.h"
38 #ifdef UML_CONFIG_MODE_SKAS
39 #include "skas.h"
40 #include "skas_ptrace.h"
41 #endif
42
43 void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
44 {
45         int flags = 0, pages;
46
47         if(sig_stack != NULL){
48                 pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER);
49                 set_sigstack(sig_stack, pages * page_size());
50                 flags = SA_ONSTACK;
51         }
52         if(usr1_handler) set_handler(SIGUSR1, usr1_handler, flags, -1);
53 }
54
55 void init_new_thread_signals(int altstack)
56 {
57         int flags = altstack ? SA_ONSTACK : 0;
58
59         set_handler(SIGSEGV, (__sighandler_t) sig_handler, flags, 
60                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
61         set_handler(SIGTRAP, (__sighandler_t) sig_handler, flags, 
62                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
63         set_handler(SIGFPE, (__sighandler_t) sig_handler, flags, 
64                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
65         set_handler(SIGILL, (__sighandler_t) sig_handler, flags, 
66                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
67         set_handler(SIGBUS, (__sighandler_t) sig_handler, flags, 
68                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
69         set_handler(SIGWINCH, (__sighandler_t) sig_handler, flags, 
70                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
71         set_handler(SIGUSR2, (__sighandler_t) sig_handler, 
72                     flags, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
73         signal(SIGHUP, SIG_IGN);
74
75         init_irq_signals(altstack);
76 }
77
78 struct tramp {
79         int (*tramp)(void *);
80         void *tramp_data;
81         unsigned long temp_stack;
82         int flags;
83         int pid;
84 };
85
86 /* See above for why sigkill is here */
87
88 int sigkill = SIGKILL;
89
90 int outer_tramp(void *arg)
91 {
92         struct tramp *t;
93         int sig = sigkill;
94
95         t = arg;
96         t->pid = clone(t->tramp, (void *) t->temp_stack + page_size()/2,
97                        t->flags, t->tramp_data);
98         if(t->pid > 0) wait_for_stop(t->pid, SIGSTOP, PTRACE_CONT, NULL);
99         kill(os_getpid(), sig);
100         _exit(0);
101 }
102
103 int start_fork_tramp(void *thread_arg, unsigned long temp_stack, 
104                      int clone_flags, int (*tramp)(void *))
105 {
106         struct tramp arg;
107         unsigned long sp;
108         int new_pid, status, err;
109
110         /* The trampoline will run on the temporary stack */
111         sp = stack_sp(temp_stack);
112
113         clone_flags |= CLONE_FILES | SIGCHLD;
114
115         arg.tramp = tramp;
116         arg.tramp_data = thread_arg;
117         arg.temp_stack = temp_stack;
118         arg.flags = clone_flags;
119
120         /* Start the process and wait for it to kill itself */
121         new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
122         if(new_pid < 0) 
123                 return(new_pid);
124
125         CATCH_EINTR(err = waitpid(new_pid, &status, 0));
126         if(err < 0) 
127                 panic("Waiting for outer trampoline failed - errno = %d", 
128                       errno);
129
130         if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
131                 panic("outer trampoline didn't exit with SIGKILL, "
132                       "status = %d", status);
133
134         return(arg.pid);
135 }
136
137 static int ptrace_child(void *arg)
138 {
139         int pid = os_getpid();
140
141         if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
142                 perror("ptrace");
143                 os_kill_process(pid, 0);
144         }
145         os_stop_process(pid);
146         _exit(os_getpid() == pid);
147 }
148
149 static int start_ptraced_child(void **stack_out)
150 {
151         void *stack;
152         unsigned long sp;
153         int pid, n, status;
154         
155         stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
156                      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
157         if(stack == MAP_FAILED)
158                 panic("check_ptrace : mmap failed, errno = %d", errno);
159         sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
160         pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
161         if(pid < 0)
162                 panic("check_ptrace : clone failed, errno = %d", errno);
163         CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
164         if(n < 0)
165                 panic("check_ptrace : wait failed, errno = %d", errno);
166         if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
167                 panic("check_ptrace : expected SIGSTOP, got status = %d",
168                       status);
169
170         *stack_out = stack;
171         return(pid);
172 }
173
174 static void stop_ptraced_child(int pid, void *stack, int exitcode)
175 {
176         int status, n;
177
178         if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
179                 panic("check_ptrace : ptrace failed, errno = %d", errno);
180         CATCH_EINTR(n = waitpid(pid, &status, 0));
181         if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode))
182                 panic("check_ptrace : child exited with status 0x%x", status);
183
184         if(munmap(stack, PAGE_SIZE) < 0)
185                 panic("check_ptrace : munmap failed, errno = %d", errno);
186 }
187
188 void __init check_ptrace(void)
189 {
190         void *stack;
191         int pid, syscall, n, status;
192
193         printk("Checking that ptrace can change system call numbers...");
194         pid = start_ptraced_child(&stack);
195
196         while(1){
197                 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
198                         panic("check_ptrace : ptrace failed, errno = %d", 
199                               errno);
200                 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
201                 if(n < 0)
202                         panic("check_ptrace : wait failed, errno = %d", errno);
203                 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
204                         panic("check_ptrace : expected SIGTRAP, "
205                               "got status = %d", status);
206                 
207                 syscall = ptrace(PTRACE_PEEKUSER, pid, PT_SYSCALL_NR_OFFSET,
208                                  0);
209                 if(syscall == __NR_getpid){
210                         n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
211                                    __NR_getppid);
212                         if(n < 0)
213                                 panic("check_ptrace : failed to modify system "
214                                       "call, errno = %d", errno);
215                         break;
216                 }
217         }
218         stop_ptraced_child(pid, stack, 0);
219         printk("OK\n");
220 }
221
222 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
223 {
224         sigjmp_buf buf;
225         int n;
226
227         *jmp_ptr = &buf;
228         n = sigsetjmp(buf, 1);
229         if(n != 0)
230                 return(n);
231         (*fn)(arg);
232         return(0);
233 }
234
235 void forward_pending_sigio(int target)
236 {
237         sigset_t sigs;
238
239         if(sigpending(&sigs)) 
240                 panic("forward_pending_sigio : sigpending failed");
241         if(sigismember(&sigs, SIGIO))
242                 kill(target, SIGIO);
243 }
244
245 int can_do_skas(void)
246 {
247 #ifdef UML_CONFIG_MODE_SKAS
248         struct ptrace_faultinfo fi;
249         void *stack;
250         int pid, n, ret = 1;
251
252         printf("Checking for the skas3 patch in the host...");
253         pid = start_ptraced_child(&stack);
254
255         n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
256         if(n < 0){
257                 if(errno == EIO)
258                         printf("not found\n");
259                 else printf("No (unexpected errno - %d)\n", errno);
260                 ret = 0;
261         }
262         else printf("found\n");
263
264         init_registers(pid);
265         stop_ptraced_child(pid, stack, 1);
266
267         printf("Checking for /proc/mm...");
268         if(os_access("/proc/mm", OS_ACC_W_OK) < 0){
269                 printf("not found\n");
270                 ret = 0;
271         }
272         else printf("found\n");
273
274         return(ret);
275 #else
276         return(0);
277 #endif
278 }
279
280 /*
281  * Overrides for Emacs so that we follow Linus's tabbing style.
282  * Emacs will notice this stuff at the end of the file and automatically
283  * adjust the settings for this buffer only.  This must remain at the end
284  * of the file.
285  * ---------------------------------------------------------------------------
286  * Local variables:
287  * c-file-style: "linux"
288  * End:
289  */