516f7356e041ba52bf882417d0c42d2cc0e8f7f0
[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) - 2;
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         /* NODEFER is set here because SEGV isn't turned back on when the 
61          * handler is ready to receive signals.  This causes any segfault
62          * during a copy_user to kill the process because the fault is blocked.
63          */
64         set_handler(SIGSEGV, (__sighandler_t) sig_handler, flags | SA_NODEFER,
65                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
66         set_handler(SIGTRAP, (__sighandler_t) sig_handler, flags, 
67                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
68         set_handler(SIGFPE, (__sighandler_t) sig_handler, flags, 
69                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
70         set_handler(SIGILL, (__sighandler_t) sig_handler, flags, 
71                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
72         set_handler(SIGBUS, (__sighandler_t) sig_handler, flags, 
73                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
74         set_handler(SIGWINCH, (__sighandler_t) sig_handler, flags, 
75                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
76         set_handler(SIGUSR2, (__sighandler_t) sig_handler, 
77                     SA_NOMASK | flags, -1);
78         signal(SIGHUP, SIG_IGN);
79
80         init_irq_signals(altstack);
81 }
82
83 struct tramp {
84         int (*tramp)(void *);
85         void *tramp_data;
86         unsigned long temp_stack;
87         int flags;
88         int pid;
89 };
90
91 /* See above for why sigkill is here */
92
93 int sigkill = SIGKILL;
94
95 int outer_tramp(void *arg)
96 {
97         struct tramp *t;
98         int sig = sigkill;
99
100         t = arg;
101         t->pid = clone(t->tramp, (void *) t->temp_stack + page_size()/2,
102                        t->flags, t->tramp_data);
103         if(t->pid > 0) wait_for_stop(t->pid, SIGSTOP, PTRACE_CONT, NULL);
104         kill(os_getpid(), sig);
105         _exit(0);
106 }
107
108 int start_fork_tramp(void *thread_arg, unsigned long temp_stack, 
109                      int clone_flags, int (*tramp)(void *))
110 {
111         struct tramp arg;
112         unsigned long sp;
113         int new_pid, status, err;
114
115         /* The trampoline will run on the temporary stack */
116         sp = stack_sp(temp_stack);
117
118         clone_flags |= CLONE_FILES | SIGCHLD;
119
120         arg.tramp = tramp;
121         arg.tramp_data = thread_arg;
122         arg.temp_stack = temp_stack;
123         arg.flags = clone_flags;
124
125         /* Start the process and wait for it to kill itself */
126         new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
127         if(new_pid < 0) return(-errno);
128         while(((err = waitpid(new_pid, &status, 0)) < 0) && (errno == EINTR)) ;
129         if(err < 0) panic("Waiting for outer trampoline failed - errno = %d", 
130                           errno);
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 void suspend_new_thread(int fd)
139 {
140         char c;
141
142         os_stop_process(os_getpid());
143
144         if(os_read_file(fd, &c, sizeof(c)) != sizeof(c))
145                 panic("read failed in suspend_new_thread");
146 }
147
148 static int ptrace_child(void *arg)
149 {
150         int pid = os_getpid();
151
152         if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
153                 perror("ptrace");
154                 os_kill_process(pid, 0);
155         }
156         os_stop_process(pid);
157         _exit(os_getpid() == pid);
158 }
159
160 static int start_ptraced_child(void **stack_out)
161 {
162         void *stack;
163         unsigned long sp;
164         int pid, n, status;
165         
166         stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
167                      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
168         if(stack == MAP_FAILED)
169                 panic("check_ptrace : mmap failed, errno = %d", errno);
170         sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
171         pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
172         if(pid < 0)
173                 panic("check_ptrace : clone failed, errno = %d", errno);
174         n = waitpid(pid, &status, WUNTRACED);
175         if(n < 0)
176                 panic("check_ptrace : wait failed, errno = %d", errno);
177         if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
178                 panic("check_ptrace : expected SIGSTOP, got status = %d",
179                       status);
180
181         *stack_out = stack;
182         return(pid);
183 }
184
185 static void stop_ptraced_child(int pid, void *stack, int exitcode)
186 {
187         int status, n;
188
189         if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
190                 panic("check_ptrace : ptrace failed, errno = %d", errno);
191         n = waitpid(pid, &status, 0);
192         if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode))
193                 panic("check_ptrace : child exited with status 0x%x", status);
194
195         if(munmap(stack, PAGE_SIZE) < 0)
196                 panic("check_ptrace : munmap failed, errno = %d", errno);
197 }
198
199 static int force_sysemu_disabled = 0;
200
201 static int __init nosysemu_cmd_param(char *str, int* add)
202 {
203         force_sysemu_disabled = 1;
204         return 0;
205 }
206
207 __uml_setup("nosysemu", nosysemu_cmd_param,
208                 "nosysemu\n"
209                 "    Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
210                 "    SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
211                 "    behaviour of ptrace() and helps reducing host context switch rate.\n"
212                 "    To make it working, you need a kernel patch for your host, too.\n"
213                 "    See http://perso.wanadoo.fr/laurent.vivier/UML/ for further information.\n");
214
215 void __init check_ptrace(void)
216 {
217         void *stack;
218         int pid, syscall, n, status;
219
220         printk("Checking that ptrace can change system call numbers...");
221         pid = start_ptraced_child(&stack);
222
223         while(1){
224                 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
225                         panic("check_ptrace : ptrace failed, errno = %d", 
226                               errno);
227                 n = waitpid(pid, &status, WUNTRACED);
228                 if(n < 0)
229                         panic("check_ptrace : wait failed, errno = %d", errno);
230                 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
231                         panic("check_ptrace : expected SIGTRAP, "
232                               "got status = %d", status);
233                 
234                 syscall = ptrace(PTRACE_PEEKUSER, pid, PT_SYSCALL_NR_OFFSET,
235                                  0);
236                 if(syscall == __NR_getpid){
237                         n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
238                                    __NR_getppid);
239                         if(n < 0)
240                                 panic("check_ptrace : failed to modify system "
241                                       "call, errno = %d", errno);
242                         break;
243                 }
244         }
245         stop_ptraced_child(pid, stack, 0);
246         printk("OK\n");
247
248         printk("Checking syscall emulation patch for ptrace...");
249         set_using_sysemu(0);
250         pid = start_ptraced_child(&stack);
251         if(ptrace(PTRACE_SYSEMU, pid, 0, 0) >= 0) {
252                 struct user_regs_struct regs;
253
254                 if (waitpid(pid, &status, WUNTRACED) < 0)
255                         panic("check_ptrace : wait failed, errno = %d", errno);
256                 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
257                         panic("check_ptrace : expected SIGTRAP, "
258                               "got status = %d", status);
259
260                 if (ptrace(PTRACE_GETREGS, pid, 0, &regs) < 0)
261                         panic("check_ptrace : failed to read child "
262                               "registers, errno = %d", errno);
263                 regs.orig_eax = pid;
264                 if (ptrace(PTRACE_SETREGS, pid, 0, &regs) < 0)
265                         panic("check_ptrace : failed to modify child "
266                               "registers, errno = %d", errno);
267
268                 stop_ptraced_child(pid, stack, 0);
269
270                 if (!force_sysemu_disabled) {
271                         printk("found\n");
272                         set_using_sysemu(1);
273                 } else {
274                         printk("found but disabled\n");
275                 }
276         }
277         else
278         {
279                 printk("missing\n");
280                 stop_ptraced_child(pid, stack, 1);
281         }
282
283 }
284
285 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
286 {
287         jmp_buf buf;
288         int n;
289
290         *jmp_ptr = &buf;
291         n = sigsetjmp(buf, 1);
292         if(n != 0)
293                 return(n);
294         (*fn)(arg);
295         return(0);
296 }
297
298 void forward_pending_sigio(int target)
299 {
300         sigset_t sigs;
301
302         if(sigpending(&sigs)) 
303                 panic("forward_pending_sigio : sigpending failed");
304         if(sigismember(&sigs, SIGIO))
305                 kill(target, SIGIO);
306 }
307
308 int can_do_skas(void)
309 {
310 #ifdef UML_CONFIG_MODE_SKAS
311         struct ptrace_faultinfo fi;
312         void *stack;
313         int pid, n, ret = 1;
314
315         printf("Checking for the skas3 patch in the host...");
316         pid = start_ptraced_child(&stack);
317
318         n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
319         if(n < 0){
320                 if(errno == EIO)
321                         printf("not found\n");
322                 else printf("No (unexpected errno - %d)\n", errno);
323                 ret = 0;
324         }
325         else printf("found\n");
326
327         init_registers(pid);
328         stop_ptraced_child(pid, stack, 1);
329
330         printf("Checking for /proc/mm...");
331         if(os_access("/proc/mm", OS_ACC_W_OK) < 0){
332                 printf("not found\n");
333                 ret = 0;
334         }
335         else printf("found\n");
336
337         return(ret);
338 #else
339         return(0);
340 #endif
341 }
342
343 /*
344  * Overrides for Emacs so that we follow Linus's tabbing style.
345  * Emacs will notice this stuff at the end of the file and automatically
346  * adjust the settings for this buffer only.  This must remain at the end
347  * of the file.
348  * ---------------------------------------------------------------------------
349  * Local variables:
350  * c-file-style: "linux"
351  * End:
352  */