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 "user_util.h"
23 #include "kern_util.h"
24 #include "user.h"
25 #include "process.h"
26 #include "signal_kern.h"
27 #include "signal_user.h"
28 #include "sysdep/ptrace.h"
29 #include "sysdep/sigcontext.h"
30 #include "irq_user.h"
31 #include "ptrace_user.h"
32 #include "time_user.h"
33 #include "init.h"
34 #include "os.h"
35 #include "uml-config.h"
36 #include "choose-mode.h"
37 #include "mode.h"
38 #ifdef UML_CONFIG_MODE_SKAS
39 #include "skas.h"
40 #include "skas_ptrace.h"
41 #endif
42
43 void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
44 {
45         int flags = 0, pages;
46
47         if(sig_stack != NULL){
48                 pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER) - 2;
49                 set_sigstack(sig_stack, pages * page_size());
50                 flags = SA_ONSTACK;
51         }
52         if(usr1_handler) set_handler(SIGUSR1, usr1_handler, flags, -1);
53 }
54
55 void init_new_thread_signals(int altstack)
56 {
57         int flags = altstack ? SA_ONSTACK : 0;
58
59         /* NODEFER is set here because SEGV isn't turned back on when the 
60          * handler is ready to receive signals.  This causes any segfault
61          * during a copy_user to kill the process because the fault is blocked.
62          */
63         set_handler(SIGSEGV, (__sighandler_t) sig_handler, flags | SA_NODEFER,
64                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
65         set_handler(SIGTRAP, (__sighandler_t) sig_handler, flags, 
66                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
67         set_handler(SIGFPE, (__sighandler_t) sig_handler, flags, 
68                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
69         set_handler(SIGILL, (__sighandler_t) sig_handler, flags, 
70                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
71         set_handler(SIGBUS, (__sighandler_t) sig_handler, flags, 
72                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
73         set_handler(SIGWINCH, (__sighandler_t) sig_handler, flags, 
74                     SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
75         set_handler(SIGUSR2, (__sighandler_t) sig_handler, 
76                     SA_NOMASK | flags, -1);
77         signal(SIGHUP, SIG_IGN);
78
79         init_irq_signals(altstack);
80 }
81
82 struct tramp {
83         int (*tramp)(void *);
84         void *tramp_data;
85         unsigned long temp_stack;
86         int flags;
87         int pid;
88 };
89
90 /* See above for why sigkill is here */
91
92 int sigkill = SIGKILL;
93
94 int outer_tramp(void *arg)
95 {
96         struct tramp *t;
97         int sig = sigkill;
98
99         t = arg;
100         t->pid = clone(t->tramp, (void *) t->temp_stack + page_size()/2,
101                        t->flags, t->tramp_data);
102         if(t->pid > 0) wait_for_stop(t->pid, SIGSTOP, PTRACE_CONT, NULL);
103         kill(os_getpid(), sig);
104         _exit(0);
105 }
106
107 int start_fork_tramp(void *thread_arg, unsigned long temp_stack, 
108                      int clone_flags, int (*tramp)(void *))
109 {
110         struct tramp arg;
111         unsigned long sp;
112         int new_pid, status, err;
113
114         /* The trampoline will run on the temporary stack */
115         sp = stack_sp(temp_stack);
116
117         clone_flags |= CLONE_FILES | SIGCHLD;
118
119         arg.tramp = tramp;
120         arg.tramp_data = thread_arg;
121         arg.temp_stack = temp_stack;
122         arg.flags = clone_flags;
123
124         /* Start the process and wait for it to kill itself */
125         new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
126         if(new_pid < 0) return(-errno);
127         while(((err = waitpid(new_pid, &status, 0)) < 0) && (errno == EINTR)) ;
128         if(err < 0) panic("Waiting for outer trampoline failed - errno = %d", 
129                           errno);
130         if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
131                 panic("outer trampoline didn't exit with SIGKILL, "
132                       "status = %d", status);
133
134         return(arg.pid);
135 }
136
137 void suspend_new_thread(int fd)
138 {
139         char c;
140
141         os_stop_process(os_getpid());
142
143         if(os_read_file(fd, &c, sizeof(c)) != sizeof(c))
144                 panic("read failed in suspend_new_thread");
145 }
146
147 static int ptrace_child(void *arg)
148 {
149         int pid = os_getpid();
150
151         if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
152                 perror("ptrace");
153                 os_kill_process(pid, 0);
154         }
155         os_stop_process(pid);
156         _exit(os_getpid() == pid);
157 }
158
159 static int start_ptraced_child(void **stack_out)
160 {
161         void *stack;
162         unsigned long sp;
163         int pid, n, status;
164         
165         stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
166                      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
167         if(stack == MAP_FAILED)
168                 panic("check_ptrace : mmap failed, errno = %d", errno);
169         sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
170         pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
171         if(pid < 0)
172                 panic("check_ptrace : clone failed, errno = %d", errno);
173         n = waitpid(pid, &status, WUNTRACED);
174         if(n < 0)
175                 panic("check_ptrace : wait failed, errno = %d", errno);
176         if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
177                 panic("check_ptrace : expected SIGSTOP, got status = %d",
178                       status);
179
180         *stack_out = stack;
181         return(pid);
182 }
183
184 static void stop_ptraced_child(int pid, void *stack, int exitcode)
185 {
186         int status, n;
187
188         if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
189                 panic("check_ptrace : ptrace failed, errno = %d", errno);
190         n = waitpid(pid, &status, 0);
191         if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode))
192                 panic("check_ptrace : child exited with status 0x%x", status);
193
194         if(munmap(stack, PAGE_SIZE) < 0)
195                 panic("check_ptrace : munmap failed, errno = %d", errno);
196 }
197
198 void __init check_ptrace(void)
199 {
200         void *stack;
201         int pid, syscall, n, status;
202
203         printk("Checking that ptrace can change system call numbers...");
204         pid = start_ptraced_child(&stack);
205
206         while(1){
207                 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
208                         panic("check_ptrace : ptrace failed, errno = %d", 
209                               errno);
210                 n = waitpid(pid, &status, WUNTRACED);
211                 if(n < 0)
212                         panic("check_ptrace : wait failed, errno = %d", errno);
213                 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
214                         panic("check_ptrace : expected SIGTRAP, "
215                               "got status = %d", status);
216                 
217                 syscall = ptrace(PTRACE_PEEKUSER, pid, PT_SYSCALL_NR_OFFSET,
218                                  0);
219                 if(syscall == __NR_getpid){
220                         n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
221                                    __NR_getppid);
222                         if(n < 0)
223                                 panic("check_ptrace : failed to modify system "
224                                       "call, errno = %d", errno);
225                         break;
226                 }
227         }
228         stop_ptraced_child(pid, stack, 0);
229         printk("OK\n");
230 }
231
232 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
233 {
234         jmp_buf buf;
235         int n;
236
237         *jmp_ptr = &buf;
238         n = sigsetjmp(buf, 1);
239         if(n != 0)
240                 return(n);
241         (*fn)(arg);
242         return(0);
243 }
244
245 void forward_pending_sigio(int target)
246 {
247         sigset_t sigs;
248
249         if(sigpending(&sigs)) 
250                 panic("forward_pending_sigio : sigpending failed");
251         if(sigismember(&sigs, SIGIO))
252                 kill(target, SIGIO);
253 }
254
255 int can_do_skas(void)
256 {
257 #ifdef UML_CONFIG_MODE_SKAS
258         struct ptrace_faultinfo fi;
259         void *stack;
260         int pid, n, ret = 1;
261
262         printf("Checking for the skas3 patch in the host...");
263         pid = start_ptraced_child(&stack);
264
265         n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
266         if(n < 0){
267                 if(errno == EIO)
268                         printf("not found\n");
269                 else printf("No (unexpected errno - %d)\n", errno);
270                 ret = 0;
271         }
272         else printf("found\n");
273
274         init_registers(pid);
275         stop_ptraced_child(pid, stack, 1);
276
277         printf("Checking for /proc/mm...");
278         if(os_access("/proc/mm", OS_ACC_W_OK) < 0){
279                 printf("not found\n");
280                 ret = 0;
281         }
282         else printf("found\n");
283
284         return(ret);
285 #else
286         return(0);
287 #endif
288 }
289
290 /*
291  * Overrides for Emacs so that we follow Linus's tabbing style.
292  * Emacs will notice this stuff at the end of the file and automatically
293  * adjust the settings for this buffer only.  This must remain at the end
294  * of the file.
295  * ---------------------------------------------------------------------------
296  * Local variables:
297  * c-file-style: "linux"
298  * End:
299  */