vserver 1.9.3
[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 pid = os_getpid();
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         _exit(os_getpid() == pid);
148 }
149
150 static int start_ptraced_child(void **stack_out)
151 {
152         void *stack;
153         unsigned long sp;
154         int pid, n, status;
155         
156         stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
157                      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
158         if(stack == MAP_FAILED)
159                 panic("check_ptrace : mmap failed, errno = %d", errno);
160         sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
161         pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
162         if(pid < 0)
163                 panic("check_ptrace : clone failed, errno = %d", errno);
164         CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
165         if(n < 0)
166                 panic("check_ptrace : wait failed, errno = %d", errno);
167         if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
168                 panic("check_ptrace : expected SIGSTOP, got status = %d",
169                       status);
170
171         *stack_out = stack;
172         return(pid);
173 }
174
175 static void stop_ptraced_child(int pid, void *stack, int exitcode)
176 {
177         int status, n;
178
179         if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
180                 panic("check_ptrace : ptrace failed, errno = %d", errno);
181         CATCH_EINTR(n = waitpid(pid, &status, 0));
182         if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode))
183                 panic("check_ptrace : child exited with status 0x%x", status);
184
185         if(munmap(stack, PAGE_SIZE) < 0)
186                 panic("check_ptrace : munmap failed, errno = %d", errno);
187 }
188
189 static int force_sysemu_disabled = 0;
190
191 static int __init nosysemu_cmd_param(char *str, int* add)
192 {
193         force_sysemu_disabled = 1;
194         return 0;
195 }
196
197 __uml_setup("nosysemu", nosysemu_cmd_param,
198                 "nosysemu\n"
199                 "    Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
200                 "    SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
201                 "    behaviour of ptrace() and helps reducing host context switch rate.\n"
202                 "    To make it working, you need a kernel patch for your host, too.\n"
203                 "    See http://perso.wanadoo.fr/laurent.vivier/UML/ for further information.\n");
204
205 static void __init check_sysemu(void)
206 {
207         void *stack;
208         int pid, n, status;
209
210         if (mode_tt)
211                 return;
212
213         printk("Checking syscall emulation patch for ptrace...");
214         sysemu_supported = 0;
215         pid = start_ptraced_child(&stack);
216         if(ptrace(PTRACE_SYSEMU, pid, 0, 0) >= 0) {
217                 struct user_regs_struct regs;
218
219                 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
220                 if (n < 0)
221                         panic("check_ptrace : wait failed, errno = %d", errno);
222                 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
223                         panic("check_ptrace : expected SIGTRAP, "
224                               "got status = %d", status);
225
226                 if (ptrace(PTRACE_GETREGS, pid, 0, &regs) < 0)
227                         panic("check_ptrace : failed to read child "
228                               "registers, errno = %d", errno);
229                 regs.orig_eax = pid;
230                 if (ptrace(PTRACE_SETREGS, pid, 0, &regs) < 0)
231                         panic("check_ptrace : failed to modify child "
232                               "registers, errno = %d", errno);
233
234                 stop_ptraced_child(pid, stack, 0);
235
236                 sysemu_supported = 1;
237                 printk("found\n");
238         }
239         else
240         {
241                 stop_ptraced_child(pid, stack, 1);
242                 sysemu_supported = 0;
243                 printk("missing\n");
244         }
245
246         set_using_sysemu(!force_sysemu_disabled);
247 }
248
249 void __init check_ptrace(void)
250 {
251         void *stack;
252         int pid, syscall, n, status;
253
254         printk("Checking that ptrace can change system call numbers...");
255         pid = start_ptraced_child(&stack);
256
257         while(1){
258                 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
259                         panic("check_ptrace : ptrace failed, errno = %d", 
260                               errno);
261                 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
262                 if(n < 0)
263                         panic("check_ptrace : wait failed, errno = %d", errno);
264                 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
265                         panic("check_ptrace : expected SIGTRAP, "
266                               "got status = %d", status);
267                 
268                 syscall = ptrace(PTRACE_PEEKUSER, pid, PT_SYSCALL_NR_OFFSET,
269                                  0);
270                 if(syscall == __NR_getpid){
271                         n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
272                                    __NR_getppid);
273                         if(n < 0)
274                                 panic("check_ptrace : failed to modify system "
275                                       "call, errno = %d", errno);
276                         break;
277                 }
278         }
279         stop_ptraced_child(pid, stack, 0);
280         printk("OK\n");
281         check_sysemu();
282 }
283
284 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
285 {
286         sigjmp_buf buf;
287         int n;
288
289         *jmp_ptr = &buf;
290         n = sigsetjmp(buf, 1);
291         if(n != 0)
292                 return(n);
293         (*fn)(arg);
294         return(0);
295 }
296
297 void forward_pending_sigio(int target)
298 {
299         sigset_t sigs;
300
301         if(sigpending(&sigs)) 
302                 panic("forward_pending_sigio : sigpending failed");
303         if(sigismember(&sigs, SIGIO))
304                 kill(target, SIGIO);
305 }
306
307 int can_do_skas(void)
308 {
309 #ifdef UML_CONFIG_MODE_SKAS
310         struct ptrace_faultinfo fi;
311         void *stack;
312         int pid, n, ret = 1;
313
314         printf("Checking for the skas3 patch in the host...");
315         pid = start_ptraced_child(&stack);
316
317         n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
318         if(n < 0){
319                 if(errno == EIO)
320                         printf("not found\n");
321                 else printf("No (unexpected errno - %d)\n", errno);
322                 ret = 0;
323         }
324         else printf("found\n");
325
326         init_registers(pid);
327         stop_ptraced_child(pid, stack, 1);
328
329         printf("Checking for /proc/mm...");
330         if(os_access("/proc/mm", OS_ACC_W_OK) < 0){
331                 printf("not found\n");
332                 ret = 0;
333         }
334         else printf("found\n");
335
336         return(ret);
337 #else
338         return(0);
339 #endif
340 }
341
342 /*
343  * Overrides for Emacs so that we follow Linus's tabbing style.
344  * Emacs will notice this stuff at the end of the file and automatically
345  * adjust the settings for this buffer only.  This must remain at the end
346  * of the file.
347  * ---------------------------------------------------------------------------
348  * Local variables:
349  * c-file-style: "linux"
350  * End:
351  */