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