2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
17 #include <asm/unistd.h>
19 #include "user_util.h"
20 #include "kern_util.h"
23 #include "signal_kern.h"
24 #include "signal_user.h"
25 #include "sysdep/ptrace.h"
26 #include "sysdep/sigcontext.h"
28 #include "ptrace_user.h"
29 #include "time_user.h"
32 #include "uml-config.h"
33 #include "choose-mode.h"
35 #ifdef UML_CONFIG_MODE_SKAS
37 #include "skas_ptrace.h"
38 #include "registers.h"
41 void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
45 if(sig_stack != NULL){
46 pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER);
47 set_sigstack(sig_stack, pages * page_size());
50 if(usr1_handler) set_handler(SIGUSR1, usr1_handler, flags, -1);
53 void init_new_thread_signals(int altstack)
55 int flags = altstack ? SA_ONSTACK : 0;
57 set_handler(SIGSEGV, (__sighandler_t) sig_handler, flags,
58 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
59 set_handler(SIGTRAP, (__sighandler_t) sig_handler, flags,
60 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
61 set_handler(SIGFPE, (__sighandler_t) sig_handler, flags,
62 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
63 set_handler(SIGILL, (__sighandler_t) sig_handler, flags,
64 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
65 set_handler(SIGBUS, (__sighandler_t) sig_handler, flags,
66 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
67 set_handler(SIGUSR2, (__sighandler_t) sig_handler,
68 flags, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
69 signal(SIGHUP, SIG_IGN);
71 init_irq_signals(altstack);
77 unsigned long temp_stack;
82 /* See above for why sigkill is here */
84 int sigkill = SIGKILL;
86 int outer_tramp(void *arg)
92 t->pid = clone(t->tramp, (void *) t->temp_stack + page_size()/2,
93 t->flags, t->tramp_data);
94 if(t->pid > 0) wait_for_stop(t->pid, SIGSTOP, PTRACE_CONT, NULL);
95 kill(os_getpid(), sig);
99 int start_fork_tramp(void *thread_arg, unsigned long temp_stack,
100 int clone_flags, int (*tramp)(void *))
104 int new_pid, status, err;
106 /* The trampoline will run on the temporary stack */
107 sp = stack_sp(temp_stack);
109 clone_flags |= CLONE_FILES | SIGCHLD;
112 arg.tramp_data = thread_arg;
113 arg.temp_stack = temp_stack;
114 arg.flags = clone_flags;
116 /* Start the process and wait for it to kill itself */
117 new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
121 CATCH_EINTR(err = waitpid(new_pid, &status, 0));
123 panic("Waiting for outer trampoline failed - errno = %d",
126 if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
127 panic("outer trampoline didn't exit with SIGKILL, "
128 "status = %d", status);
133 static int ptrace_child(void)
136 int pid = os_getpid(), ppid = getppid();
139 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
141 os_kill_process(pid, 0);
143 os_stop_process(pid);
145 /*This syscall will be intercepted by the parent. Don't call more than
147 sc_result = os_getpid();
149 if (sc_result == pid)
150 ret = 1; /*Nothing modified by the parent, we are running
152 else if (sc_result == ppid)
153 ret = 0; /*Expected in check_ptrace and check_sysemu when they
154 succeed in modifying the stack frame*/
156 ret = 2; /*Serious trouble! This could be caused by a bug in
157 host 2.6 SKAS3/2.6 patch before release -V6, together
158 with a bug in the UML code itself.*/
162 static int start_ptraced_child(void)
171 panic("check_ptrace : fork failed, errno = %d", errno);
172 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
174 panic("check_ptrace : wait failed, errno = %d", errno);
175 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
176 panic("check_ptrace : expected SIGSTOP, got status = %d",
182 /* When testing for SYSEMU support, if it is one of the broken versions, we must
183 * just avoid using sysemu, not panic, but only if SYSEMU features are broken.
184 * So only for SYSEMU features we test mustpanic, while normal host features
185 * must work anyway!*/
186 static int stop_ptraced_child(int pid, int exitcode, int mustexit)
188 int status, n, ret = 0;
190 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
191 panic("stop_ptraced_child : ptrace failed, errno = %d", errno);
192 CATCH_EINTR(n = waitpid(pid, &status, 0));
193 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
194 int exit_with = WEXITSTATUS(status);
196 printk("check_ptrace : child exited with status 2. "
197 "Serious trouble happening! Try updating your "
198 "host skas patch!\nDisabling SYSEMU support.");
199 printk("check_ptrace : child exited with exitcode %d, while "
200 "expecting %d; status 0x%x", exit_with,
212 static int force_sysemu_disabled = 0;
214 static int __init nosysemu_cmd_param(char *str, int* add)
216 force_sysemu_disabled = 1;
220 __uml_setup("nosysemu", nosysemu_cmd_param,
222 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
223 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
224 " behaviour of ptrace() and helps reducing host context switch rate.\n"
225 " To make it working, you need a kernel patch for your host, too.\n"
226 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further information.\n\n");
228 static void __init check_sysemu(void)
230 int pid, syscall, n, status, count=0;
232 printk("Checking syscall emulation patch for ptrace...");
233 sysemu_supported = 0;
234 pid = start_ptraced_child();
236 if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
239 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
241 panic("check_sysemu : wait failed, errno = %d", errno);
242 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
243 panic("check_sysemu : expected SIGTRAP, "
244 "got status = %d", status);
246 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
249 panic("check_sysemu : failed to modify system "
250 "call return, errno = %d", errno);
252 if (stop_ptraced_child(pid, 0, 0) < 0)
255 sysemu_supported = 1;
257 set_using_sysemu(!force_sysemu_disabled);
259 printk("Checking advanced syscall emulation patch for ptrace...");
260 pid = start_ptraced_child();
263 if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
265 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
267 panic("check_ptrace : wait failed, errno = %d", errno);
268 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
269 panic("check_ptrace : expected (SIGTRAP|SYSCALL_TRAP), "
270 "got status = %d", status);
272 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
274 if(syscall == __NR_getpid){
276 panic("check_ptrace : SYSEMU_SINGLESTEP doesn't singlestep");
277 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
280 panic("check_sysemu : failed to modify system "
281 "call return, errno = %d", errno);
285 if (stop_ptraced_child(pid, 0, 0) < 0)
288 sysemu_supported = 2;
291 if ( !force_sysemu_disabled )
292 set_using_sysemu(sysemu_supported);
296 stop_ptraced_child(pid, 1, 0);
301 void __init check_ptrace(void)
303 int pid, syscall, n, status;
305 printk("Checking that ptrace can change system call numbers...");
306 pid = start_ptraced_child();
308 if (ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
309 panic("check_ptrace: PTRACE_SETOPTIONS failed, errno = %d", errno);
312 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
313 panic("check_ptrace : ptrace failed, errno = %d",
315 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
317 panic("check_ptrace : wait failed, errno = %d", errno);
318 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP + 0x80))
319 panic("check_ptrace : expected SIGTRAP + 0x80, "
320 "got status = %d", status);
322 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
324 if(syscall == __NR_getpid){
325 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
328 panic("check_ptrace : failed to modify system "
329 "call, errno = %d", errno);
333 stop_ptraced_child(pid, 0, 1);
338 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
344 n = sigsetjmp(buf, 1);
351 void forward_pending_sigio(int target)
355 if(sigpending(&sigs))
356 panic("forward_pending_sigio : sigpending failed");
357 if(sigismember(&sigs, SIGIO))
361 #ifdef UML_CONFIG_MODE_SKAS
362 static inline int check_skas3_ptrace_support(void)
364 struct ptrace_faultinfo fi;
367 printf("Checking for the skas3 patch in the host...");
368 pid = start_ptraced_child();
370 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
373 printf("not found\n");
383 stop_ptraced_child(pid, 1, 1);
388 int can_do_skas(void)
392 printf("Checking for /proc/mm...");
393 if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
394 printf("not found\n");
401 ret = check_skas3_ptrace_support();
406 int can_do_skas(void)