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