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