vserver 2.0 rc7
[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/wait.h>
16 #include <sys/mman.h>
17 #include <asm/unistd.h>
18 #include <asm/page.h>
19 #include "user_util.h"
20 #include "kern_util.h"
21 #include "user.h"
22 #include "process.h"
23 #include "signal_kern.h"
24 #include "signal_user.h"
25 #include "sysdep/ptrace.h"
26 #include "sysdep/sigcontext.h"
27 #include "irq_user.h"
28 #include "ptrace_user.h"
29 #include "time_user.h"
30 #include "init.h"
31 #include "os.h"
32 #include "uml-config.h"
33 #include "choose-mode.h"
34 #include "mode.h"
35 #ifdef UML_CONFIG_MODE_SKAS
36 #include "skas.h"
37 #include "skas_ptrace.h"
38 #include "registers.h"
39 #endif
40
41 void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
42 {
43         int flags = 0, pages;
44
45         if(sig_stack != NULL){
46                 pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER);
47                 set_sigstack(sig_stack, pages * page_size());
48                 flags = SA_ONSTACK;
49         }
50         if(usr1_handler) set_handler(SIGUSR1, usr1_handler, flags, -1);
51 }
52
53 void init_new_thread_signals(int altstack)
54 {
55         int flags = altstack ? SA_ONSTACK : 0;
56
57         set_handler(SIGSEGV, (__sighandler_t) sig_handler, flags,
58                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
59         set_handler(SIGTRAP, (__sighandler_t) sig_handler, flags, 
60                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
61         set_handler(SIGFPE, (__sighandler_t) sig_handler, flags, 
62                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
63         set_handler(SIGILL, (__sighandler_t) sig_handler, flags, 
64                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
65         set_handler(SIGBUS, (__sighandler_t) sig_handler, flags, 
66                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
67         set_handler(SIGUSR2, (__sighandler_t) sig_handler, 
68                     flags, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
69         signal(SIGHUP, SIG_IGN);
70
71         init_irq_signals(altstack);
72 }
73
74 struct tramp {
75         int (*tramp)(void *);
76         void *tramp_data;
77         unsigned long temp_stack;
78         int flags;
79         int pid;
80 };
81
82 /* See above for why sigkill is here */
83
84 int sigkill = SIGKILL;
85
86 int outer_tramp(void *arg)
87 {
88         struct tramp *t;
89         int sig = sigkill;
90
91         t = arg;
92         t->pid = clone(t->tramp, (void *) t->temp_stack + page_size()/2,
93                        t->flags, t->tramp_data);
94         if(t->pid > 0) wait_for_stop(t->pid, SIGSTOP, PTRACE_CONT, NULL);
95         kill(os_getpid(), sig);
96         _exit(0);
97 }
98
99 int start_fork_tramp(void *thread_arg, unsigned long temp_stack, 
100                      int clone_flags, int (*tramp)(void *))
101 {
102         struct tramp arg;
103         unsigned long sp;
104         int new_pid, status, err;
105
106         /* The trampoline will run on the temporary stack */
107         sp = stack_sp(temp_stack);
108
109         clone_flags |= CLONE_FILES | SIGCHLD;
110
111         arg.tramp = tramp;
112         arg.tramp_data = thread_arg;
113         arg.temp_stack = temp_stack;
114         arg.flags = clone_flags;
115
116         /* Start the process and wait for it to kill itself */
117         new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
118         if(new_pid < 0)
119                 return(new_pid);
120
121         CATCH_EINTR(err = waitpid(new_pid, &status, 0));
122         if(err < 0)
123                 panic("Waiting for outer trampoline failed - errno = %d",
124                       errno);
125
126         if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
127                 panic("outer trampoline didn't exit with SIGKILL, "
128                       "status = %d", status);
129
130         return(arg.pid);
131 }
132
133 static int ptrace_child(void)
134 {
135         int ret;
136         int pid = os_getpid(), ppid = getppid();
137         int sc_result;
138
139         if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
140                 perror("ptrace");
141                 os_kill_process(pid, 0);
142         }
143         os_stop_process(pid);
144
145         /*This syscall will be intercepted by the parent. Don't call more than
146          * once, please.*/
147         sc_result = os_getpid();
148
149         if (sc_result == pid)
150                 ret = 1; /*Nothing modified by the parent, we are running
151                            normally.*/
152         else if (sc_result == ppid)
153                 ret = 0; /*Expected in check_ptrace and check_sysemu when they
154                            succeed in modifying the stack frame*/
155         else
156                 ret = 2; /*Serious trouble! This could be caused by a bug in
157                            host 2.6 SKAS3/2.6 patch before release -V6, together
158                            with a bug in the UML code itself.*/
159         _exit(ret);
160 }
161
162 static int start_ptraced_child(void)
163 {
164         int pid, n, status;
165         
166         pid = fork();
167         if(pid == 0)
168                 ptrace_child();
169
170         if(pid < 0)
171                 panic("check_ptrace : fork failed, errno = %d", errno);
172         CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
173         if(n < 0)
174                 panic("check_ptrace : wait failed, errno = %d", errno);
175         if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
176                 panic("check_ptrace : expected SIGSTOP, got status = %d",
177                       status);
178
179         return(pid);
180 }
181
182 /* When testing for SYSEMU support, if it is one of the broken versions, we must
183  * just avoid using sysemu, not panic, but only if SYSEMU features are broken.
184  * So only for SYSEMU features we test mustpanic, while normal host features
185  * must work anyway!*/
186 static int stop_ptraced_child(int pid, int exitcode, int mustexit)
187 {
188         int status, n, ret = 0;
189
190         if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
191                 panic("stop_ptraced_child : ptrace failed, errno = %d", errno);
192         CATCH_EINTR(n = waitpid(pid, &status, 0));
193         if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
194                 int exit_with = WEXITSTATUS(status);
195                 if (exit_with == 2)
196                         printk("check_ptrace : child exited with status 2. "
197                                "Serious trouble happening! Try updating your "
198                                "host skas patch!\nDisabling SYSEMU support.");
199                 printk("check_ptrace : child exited with exitcode %d, while "
200                       "expecting %d; status 0x%x", exit_with,
201                       exitcode, status);
202                 if (mustexit)
203                         panic("\n");
204                 else
205                         printk("\n");
206                 ret = -1;
207         }
208
209         return ret;
210 }
211
212 static int force_sysemu_disabled = 0;
213
214 static int __init nosysemu_cmd_param(char *str, int* add)
215 {
216         force_sysemu_disabled = 1;
217         return 0;
218 }
219
220 __uml_setup("nosysemu", nosysemu_cmd_param,
221                 "nosysemu\n"
222                 "    Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
223                 "    SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
224                 "    behaviour of ptrace() and helps reducing host context switch rate.\n"
225                 "    To make it working, you need a kernel patch for your host, too.\n"
226                 "    See http://perso.wanadoo.fr/laurent.vivier/UML/ for further information.\n\n");
227
228 static void __init check_sysemu(void)
229 {
230         int pid, syscall, n, status, count=0;
231
232         printk("Checking syscall emulation patch for ptrace...");
233         sysemu_supported = 0;
234         pid = start_ptraced_child();
235
236         if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
237                 goto fail;
238
239         CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
240         if (n < 0)
241                 panic("check_sysemu : wait failed, errno = %d", errno);
242         if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
243                 panic("check_sysemu : expected SIGTRAP, "
244                       "got status = %d", status);
245
246         n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
247                    os_getpid());
248         if(n < 0)
249                 panic("check_sysemu : failed to modify system "
250                       "call return, errno = %d", errno);
251
252         if (stop_ptraced_child(pid, 0, 0) < 0)
253                 goto fail_stopped;
254
255         sysemu_supported = 1;
256         printk("OK\n");
257         set_using_sysemu(!force_sysemu_disabled);
258
259         printk("Checking advanced syscall emulation patch for ptrace...");
260         pid = start_ptraced_child();
261         while(1){
262                 count++;
263                 if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
264                         goto fail;
265                 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
266                 if(n < 0)
267                         panic("check_ptrace : wait failed, errno = %d", errno);
268                 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
269                         panic("check_ptrace : expected (SIGTRAP|SYSCALL_TRAP), "
270                               "got status = %d", status);
271
272                 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
273                                  0);
274                 if(syscall == __NR_getpid){
275                         if (!count)
276                                 panic("check_ptrace : SYSEMU_SINGLESTEP doesn't singlestep");
277                         n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
278                                    os_getpid());
279                         if(n < 0)
280                                 panic("check_sysemu : failed to modify system "
281                                       "call return, errno = %d", errno);
282                         break;
283                 }
284         }
285         if (stop_ptraced_child(pid, 0, 0) < 0)
286                 goto fail_stopped;
287
288         sysemu_supported = 2;
289         printk("OK\n");
290
291         if ( !force_sysemu_disabled )
292                 set_using_sysemu(sysemu_supported);
293         return;
294
295 fail:
296         stop_ptraced_child(pid, 1, 0);
297 fail_stopped:
298         printk("missing\n");
299 }
300
301 void __init check_ptrace(void)
302 {
303         int pid, syscall, n, status;
304
305         printk("Checking that ptrace can change system call numbers...");
306         pid = start_ptraced_child();
307
308         if (ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
309                 panic("check_ptrace: PTRACE_SETOPTIONS failed, errno = %d", errno);
310
311         while(1){
312                 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
313                         panic("check_ptrace : ptrace failed, errno = %d", 
314                               errno);
315                 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
316                 if(n < 0)
317                         panic("check_ptrace : wait failed, errno = %d", errno);
318                 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP + 0x80))
319                         panic("check_ptrace : expected SIGTRAP + 0x80, "
320                               "got status = %d", status);
321                 
322                 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
323                                  0);
324                 if(syscall == __NR_getpid){
325                         n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
326                                    __NR_getppid);
327                         if(n < 0)
328                                 panic("check_ptrace : failed to modify system "
329                                       "call, errno = %d", errno);
330                         break;
331                 }
332         }
333         stop_ptraced_child(pid, 0, 1);
334         printk("OK\n");
335         check_sysemu();
336 }
337
338 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
339 {
340         sigjmp_buf buf;
341         int n;
342
343         *jmp_ptr = &buf;
344         n = sigsetjmp(buf, 1);
345         if(n != 0)
346                 return(n);
347         (*fn)(arg);
348         return(0);
349 }
350
351 void forward_pending_sigio(int target)
352 {
353         sigset_t sigs;
354
355         if(sigpending(&sigs)) 
356                 panic("forward_pending_sigio : sigpending failed");
357         if(sigismember(&sigs, SIGIO))
358                 kill(target, SIGIO);
359 }
360
361 #ifdef UML_CONFIG_MODE_SKAS
362 static inline int check_skas3_ptrace_support(void)
363 {
364         struct ptrace_faultinfo fi;
365         int pid, n, ret = 1;
366
367         printf("Checking for the skas3 patch in the host...");
368         pid = start_ptraced_child();
369
370         n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
371         if (n < 0) {
372                 if(errno == EIO)
373                         printf("not found\n");
374                 else {
375                         perror("not found");
376                 }
377                 ret = 0;
378         } else {
379                 printf("found\n");
380         }
381
382         init_registers(pid);
383         stop_ptraced_child(pid, 1, 1);
384
385         return(ret);
386 }
387
388 int can_do_skas(void)
389 {
390         int ret = 1;
391
392         printf("Checking for /proc/mm...");
393         if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
394                 printf("not found\n");
395                 ret = 0;
396                 goto out;
397         } else {
398                 printf("found\n");
399         }
400
401         ret = check_skas3_ptrace_support();
402 out:
403         return ret;
404 }
405 #else
406 int can_do_skas(void)
407 {
408         return(0);
409 }
410 #endif