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