MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / arch / um / kernel / process.c
blob4468f244091c90864c9e0d0fc360b5e4943d16a5
1 /*
2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
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
44 void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
46 int flags = 0, pages;
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;
53 if(usr1_handler) set_handler(SIGUSR1, usr1_handler, flags, -1);
56 void init_new_thread_signals(int altstack)
58 int flags = altstack ? SA_ONSTACK : 0;
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);
76 init_irq_signals(altstack);
79 struct tramp {
80 int (*tramp)(void *);
81 void *tramp_data;
82 unsigned long temp_stack;
83 int flags;
84 int pid;
87 /* See above for why sigkill is here */
89 int sigkill = SIGKILL;
91 int outer_tramp(void *arg)
93 struct tramp *t;
94 int sig = sigkill;
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);
104 int start_fork_tramp(void *thread_arg, unsigned long temp_stack,
105 int clone_flags, int (*tramp)(void *))
107 struct tramp arg;
108 unsigned long sp;
109 int new_pid, status, err;
111 /* The trampoline will run on the temporary stack */
112 sp = stack_sp(temp_stack);
114 clone_flags |= CLONE_FILES | SIGCHLD;
116 arg.tramp = tramp;
117 arg.tramp_data = thread_arg;
118 arg.temp_stack = temp_stack;
119 arg.flags = clone_flags;
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);
126 CATCH_EINTR(err = waitpid(new_pid, &status, 0));
127 if(err < 0)
128 panic("Waiting for outer trampoline failed - errno = %d",
129 errno);
131 if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
132 panic("outer trampoline didn't exit with SIGKILL, "
133 "status = %d", status);
135 return(arg.pid);
138 static int ptrace_child(void *arg)
140 int pid = os_getpid();
142 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
143 perror("ptrace");
144 os_kill_process(pid, 0);
146 os_stop_process(pid);
147 _exit(os_getpid() == pid);
150 static int start_ptraced_child(void **stack_out)
152 void *stack;
153 unsigned long sp;
154 int pid, n, status;
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);
171 *stack_out = stack;
172 return(pid);
175 static void stop_ptraced_child(int pid, void *stack, int exitcode)
177 int status, n;
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);
185 if(munmap(stack, PAGE_SIZE) < 0)
186 panic("check_ptrace : munmap failed, errno = %d", errno);
189 static int force_sysemu_disabled = 0;
191 static int __init nosysemu_cmd_param(char *str, int* add)
193 force_sysemu_disabled = 1;
194 return 0;
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");
205 static void __init check_sysemu(void)
207 void *stack;
208 int pid, n, status;
210 if (mode_tt)
211 return;
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;
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);
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);
234 stop_ptraced_child(pid, stack, 0);
236 sysemu_supported = 1;
237 printk("found\n");
239 else
241 stop_ptraced_child(pid, stack, 1);
242 sysemu_supported = 0;
243 printk("missing\n");
246 set_using_sysemu(!force_sysemu_disabled);
249 void __init check_ptrace(void)
251 void *stack;
252 int pid, syscall, n, status;
254 printk("Checking that ptrace can change system call numbers...");
255 pid = start_ptraced_child(&stack);
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);
268 syscall = ptrace(PTRACE_PEEKUSER, pid, PT_SYSCALL_NR_OFFSET,
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;
279 stop_ptraced_child(pid, stack, 0);
280 printk("OK\n");
281 check_sysemu();
284 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
286 sigjmp_buf buf;
287 int n;
289 *jmp_ptr = &buf;
290 n = sigsetjmp(buf, 1);
291 if(n != 0)
292 return(n);
293 (*fn)(arg);
294 return(0);
297 void forward_pending_sigio(int target)
299 sigset_t sigs;
301 if(sigpending(&sigs))
302 panic("forward_pending_sigio : sigpending failed");
303 if(sigismember(&sigs, SIGIO))
304 kill(target, SIGIO);
307 int can_do_skas(void)
309 #ifdef UML_CONFIG_MODE_SKAS
310 struct ptrace_faultinfo fi;
311 void *stack;
312 int pid, n, ret = 1;
314 printf("Checking for the skas3 patch in the host...");
315 pid = start_ptraced_child(&stack);
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;
324 else printf("found\n");
326 init_registers(pid);
327 stop_ptraced_child(pid, stack, 1);
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;
334 else printf("found\n");
336 return(ret);
337 #else
338 return(0);
339 #endif
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: