upgrade to fedora-2.6.12-1.1398.FC4 + 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 *arg)
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 **stack_out)
163 {
164         void *stack;
165         unsigned long sp;
166         int pid, n, status;
167         
168         stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
169                      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
170         if(stack == MAP_FAILED)
171                 panic("check_ptrace : mmap failed, errno = %d", errno);
172         sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
173         pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
174         if(pid < 0)
175                 panic("check_ptrace : clone failed, errno = %d", errno);
176         CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
177         if(n < 0)
178                 panic("check_ptrace : wait failed, errno = %d", errno);
179         if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
180                 panic("check_ptrace : expected SIGSTOP, got status = %d",
181                       status);
182
183         *stack_out = stack;
184         return(pid);
185 }
186
187 /* When testing for SYSEMU support, if it is one of the broken versions, we must
188  * just avoid using sysemu, not panic, but only if SYSEMU features are broken.
189  * So only for SYSEMU features we test mustpanic, while normal host features
190  * must work anyway!*/
191 static int stop_ptraced_child(int pid, void *stack, int exitcode, int mustpanic)
192 {
193         int status, n, ret = 0;
194
195         if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
196                 panic("check_ptrace : ptrace failed, errno = %d", errno);
197         CATCH_EINTR(n = waitpid(pid, &status, 0));
198         if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
199                 int exit_with = WEXITSTATUS(status);
200                 if (exit_with == 2)
201                         printk("check_ptrace : child exited with status 2. "
202                                "Serious trouble happening! Try updating your "
203                                "host skas patch!\nDisabling SYSEMU support.");
204                 printk("check_ptrace : child exited with exitcode %d, while "
205                       "expecting %d; status 0x%x", exit_with,
206                       exitcode, status);
207                 if (mustpanic)
208                         panic("\n");
209                 else
210                         printk("\n");
211                 ret = -1;
212         }
213
214         if(munmap(stack, PAGE_SIZE) < 0)
215                 panic("check_ptrace : munmap failed, errno = %d", errno);
216         return ret;
217 }
218
219 static int force_sysemu_disabled = 0;
220
221 static int __init nosysemu_cmd_param(char *str, int* add)
222 {
223         force_sysemu_disabled = 1;
224         return 0;
225 }
226
227 __uml_setup("nosysemu", nosysemu_cmd_param,
228                 "nosysemu\n"
229                 "    Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
230                 "    SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
231                 "    behaviour of ptrace() and helps reducing host context switch rate.\n"
232                 "    To make it working, you need a kernel patch for your host, too.\n"
233                 "    See http://perso.wanadoo.fr/laurent.vivier/UML/ for further information.\n\n");
234
235 static void __init check_sysemu(void)
236 {
237         void *stack;
238         int pid, syscall, n, status, count=0;
239
240         printk("Checking syscall emulation patch for ptrace...");
241         sysemu_supported = 0;
242         pid = start_ptraced_child(&stack);
243
244         if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
245                 goto fail;
246
247         CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
248         if (n < 0)
249                 panic("check_sysemu : wait failed, errno = %d", errno);
250         if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
251                 panic("check_sysemu : expected SIGTRAP, "
252                       "got status = %d", status);
253
254         n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
255                    os_getpid());
256         if(n < 0)
257                 panic("check_sysemu : failed to modify system "
258                       "call return, errno = %d", errno);
259
260         if (stop_ptraced_child(pid, stack, 0, 0) < 0)
261                 goto fail_stopped;
262
263         sysemu_supported = 1;
264         printk("OK\n");
265         set_using_sysemu(!force_sysemu_disabled);
266
267         printk("Checking advanced syscall emulation patch for ptrace...");
268         pid = start_ptraced_child(&stack);
269         while(1){
270                 count++;
271                 if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
272                         goto fail;
273                 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
274                 if(n < 0)
275                         panic("check_ptrace : wait failed, errno = %d", errno);
276                 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
277                         panic("check_ptrace : expected (SIGTRAP|SYSCALL_TRAP), "
278                               "got status = %d", status);
279
280                 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
281                                  0);
282                 if(syscall == __NR_getpid){
283                         if (!count)
284                                 panic("check_ptrace : SYSEMU_SINGLESTEP doesn't singlestep");
285                         n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
286                                    os_getpid());
287                         if(n < 0)
288                                 panic("check_sysemu : failed to modify system "
289                                       "call return, errno = %d", errno);
290                         break;
291                 }
292         }
293         if (stop_ptraced_child(pid, stack, 0, 0) < 0)
294                 goto fail_stopped;
295
296         sysemu_supported = 2;
297         printk("OK\n");
298
299         if ( !force_sysemu_disabled )
300                 set_using_sysemu(sysemu_supported);
301         return;
302
303 fail:
304         stop_ptraced_child(pid, stack, 1, 0);
305 fail_stopped:
306         printk("missing\n");
307 }
308
309 void __init check_ptrace(void)
310 {
311         void *stack;
312         int pid, syscall, n, status;
313
314         printk("Checking that ptrace can change system call numbers...");
315         pid = start_ptraced_child(&stack);
316
317         if (ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
318                 panic("check_ptrace: PTRACE_SETOPTIONS failed, errno = %d", errno);
319
320         while(1){
321                 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
322                         panic("check_ptrace : ptrace failed, errno = %d", 
323                               errno);
324                 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
325                 if(n < 0)
326                         panic("check_ptrace : wait failed, errno = %d", errno);
327                 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP + 0x80))
328                         panic("check_ptrace : expected SIGTRAP + 0x80, "
329                               "got status = %d", status);
330                 
331                 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
332                                  0);
333                 if(syscall == __NR_getpid){
334                         n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
335                                    __NR_getppid);
336                         if(n < 0)
337                                 panic("check_ptrace : failed to modify system "
338                                       "call, errno = %d", errno);
339                         break;
340                 }
341         }
342         stop_ptraced_child(pid, stack, 0, 1);
343         printk("OK\n");
344         check_sysemu();
345 }
346
347 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
348 {
349         sigjmp_buf buf;
350         int n;
351
352         *jmp_ptr = &buf;
353         n = sigsetjmp(buf, 1);
354         if(n != 0)
355                 return(n);
356         (*fn)(arg);
357         return(0);
358 }
359
360 void forward_pending_sigio(int target)
361 {
362         sigset_t sigs;
363
364         if(sigpending(&sigs)) 
365                 panic("forward_pending_sigio : sigpending failed");
366         if(sigismember(&sigs, SIGIO))
367                 kill(target, SIGIO);
368 }
369
370 #ifdef UML_CONFIG_MODE_SKAS
371 static inline int check_skas3_ptrace_support(void)
372 {
373         struct ptrace_faultinfo fi;
374         void *stack;
375         int pid, n, ret = 1;
376
377         printf("Checking for the skas3 patch in the host...");
378         pid = start_ptraced_child(&stack);
379
380         n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
381         if (n < 0) {
382                 if(errno == EIO)
383                         printf("not found\n");
384                 else {
385                         perror("not found");
386                 }
387                 ret = 0;
388         } else {
389                 printf("found\n");
390         }
391
392         init_registers(pid);
393         stop_ptraced_child(pid, stack, 1, 1);
394
395         return(ret);
396 }
397
398 int can_do_skas(void)
399 {
400         int ret = 1;
401
402         printf("Checking for /proc/mm...");
403         if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
404                 printf("not found\n");
405                 ret = 0;
406                 goto out;
407         } else {
408                 printf("found\n");
409         }
410
411         ret = check_skas3_ptrace_support();
412 out:
413         return ret;
414 }
415 #else
416 int can_do_skas(void)
417 {
418         return(0);
419 }
420 #endif