2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
15 #include <sys/ptrace.h>
17 /*Userspace header, must be after sys/ptrace.h, and both must be included. */
18 #include <linux/ptrace.h>
22 #include <asm/unistd.h>
24 #include "user_util.h"
25 #include "kern_util.h"
28 #include "signal_kern.h"
29 #include "signal_user.h"
30 #include "sysdep/ptrace.h"
31 #include "sysdep/ptrace_user.h"
32 #include "sysdep/sigcontext.h"
34 #include "ptrace_user.h"
35 #include "time_user.h"
38 #include "uml-config.h"
39 #include "choose-mode.h"
41 #ifdef UML_CONFIG_MODE_SKAS
43 #include "skas_ptrace.h"
44 #include "registers.h"
47 void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
51 if(sig_stack != NULL){
52 pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER);
53 set_sigstack(sig_stack, pages * page_size());
56 if(usr1_handler) set_handler(SIGUSR1, usr1_handler, flags, -1);
59 void init_new_thread_signals(int altstack)
61 int flags = altstack ? SA_ONSTACK : 0;
63 set_handler(SIGSEGV, (__sighandler_t) sig_handler, flags,
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 flags, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
77 signal(SIGHUP, SIG_IGN);
79 init_irq_signals(altstack);
85 unsigned long temp_stack;
90 /* See above for why sigkill is here */
92 int sigkill = SIGKILL;
94 int outer_tramp(void *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);
107 int start_fork_tramp(void *thread_arg, unsigned long temp_stack,
108 int clone_flags, int (*tramp)(void *))
112 int new_pid, status, err;
114 /* The trampoline will run on the temporary stack */
115 sp = stack_sp(temp_stack);
117 clone_flags |= CLONE_FILES | SIGCHLD;
120 arg.tramp_data = thread_arg;
121 arg.temp_stack = temp_stack;
122 arg.flags = clone_flags;
124 /* Start the process and wait for it to kill itself */
125 new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
129 CATCH_EINTR(err = waitpid(new_pid, &status, 0));
131 panic("Waiting for outer trampoline failed - errno = %d",
134 if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
135 panic("outer trampoline didn't exit with SIGKILL, "
136 "status = %d", status);
141 static int ptrace_child(void *arg)
144 int pid = os_getpid(), ppid = getppid();
147 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
149 os_kill_process(pid, 0);
151 os_stop_process(pid);
153 /*This syscall will be intercepted by the parent. Don't call more than
155 sc_result = os_getpid();
157 if (sc_result == pid)
158 ret = 1; /*Nothing modified by the parent, we are running
160 else if (sc_result == ppid)
161 ret = 0; /*Expected in check_ptrace and check_sysemu when they
162 succeed in modifying the stack frame*/
164 ret = 2; /*Serious trouble! This could be caused by a bug in
165 host 2.6 SKAS3/2.6 patch before release -V6, together
166 with a bug in the UML code itself.*/
170 static int start_ptraced_child(void **stack_out)
176 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
177 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
178 if(stack == MAP_FAILED)
179 panic("check_ptrace : mmap failed, errno = %d", errno);
180 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
181 pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
183 panic("check_ptrace : clone failed, errno = %d", errno);
184 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
186 panic("check_ptrace : wait failed, errno = %d", errno);
187 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
188 panic("check_ptrace : expected SIGSTOP, got status = %d",
195 /* When testing for SYSEMU support, if it is one of the broken versions, we must
196 * just avoid using sysemu, not panic, but only if SYSEMU features are broken.
197 * So only for SYSEMU features we test mustpanic, while normal host features
198 * must work anyway!*/
199 static int stop_ptraced_child(int pid, void *stack, int exitcode, int mustpanic)
201 int status, n, ret = 0;
203 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
204 panic("check_ptrace : ptrace failed, errno = %d", errno);
205 CATCH_EINTR(n = waitpid(pid, &status, 0));
206 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
207 int exit_with = WEXITSTATUS(status);
209 printk("check_ptrace : child exited with status 2. "
210 "Serious trouble happening! Try updating your "
211 "host skas patch!\nDisabling SYSEMU support.");
212 printk("check_ptrace : child exited with exitcode %d, while "
213 "expecting %d; status 0x%x", exit_with,
222 if(munmap(stack, PAGE_SIZE) < 0)
223 panic("check_ptrace : munmap failed, errno = %d", errno);
227 static int force_sysemu_disabled = 0;
229 static int __init nosysemu_cmd_param(char *str, int* add)
231 force_sysemu_disabled = 1;
235 __uml_setup("nosysemu", nosysemu_cmd_param,
237 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
238 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
239 " behaviour of ptrace() and helps reducing host context switch rate.\n"
240 " To make it working, you need a kernel patch for your host, too.\n"
241 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further information.\n\n");
243 static void __init check_sysemu(void)
246 int pid, syscall, n, status, count=0;
248 printk("Checking syscall emulation patch for ptrace...");
249 sysemu_supported = 0;
250 pid = start_ptraced_child(&stack);
252 if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
255 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
257 panic("check_sysemu : wait failed, errno = %d", errno);
258 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
259 panic("check_sysemu : expected SIGTRAP, "
260 "got status = %d", status);
262 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_RET_OFFSET,
265 panic("check_sysemu : failed to modify system "
266 "call return, errno = %d", errno);
268 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
271 sysemu_supported = 1;
273 set_using_sysemu(!force_sysemu_disabled);
275 printk("Checking advanced syscall emulation patch for ptrace...");
276 pid = start_ptraced_child(&stack);
279 if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
281 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
283 panic("check_ptrace : wait failed, errno = %d", errno);
284 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
285 panic("check_ptrace : expected (SIGTRAP|SYSCALL_TRAP), "
286 "got status = %d", status);
288 syscall = ptrace(PTRACE_PEEKUSER, pid, PT_SYSCALL_NR_OFFSET,
290 if(syscall == __NR_getpid){
292 panic("check_ptrace : SYSEMU_SINGLESTEP doesn't singlestep");
293 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_RET_OFFSET,
296 panic("check_sysemu : failed to modify system "
297 "call return, errno = %d", errno);
301 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
304 sysemu_supported = 2;
307 if ( !force_sysemu_disabled )
308 set_using_sysemu(sysemu_supported);
312 stop_ptraced_child(pid, stack, 1, 0);
317 void __init check_ptrace(void)
320 int pid, syscall, n, status;
322 printk("Checking that ptrace can change system call numbers...");
323 pid = start_ptraced_child(&stack);
325 if (ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)PTRACE_O_TRACESYSGOOD) < 0)
326 panic("check_ptrace: PTRACE_SETOPTIONS failed, errno = %d", errno);
329 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
330 panic("check_ptrace : ptrace failed, errno = %d",
332 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
334 panic("check_ptrace : wait failed, errno = %d", errno);
335 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP + 0x80))
336 panic("check_ptrace : expected SIGTRAP + 0x80, "
337 "got status = %d", status);
339 syscall = ptrace(PTRACE_PEEKUSER, pid, PT_SYSCALL_NR_OFFSET,
341 if(syscall == __NR_getpid){
342 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
345 panic("check_ptrace : failed to modify system "
346 "call, errno = %d", errno);
350 stop_ptraced_child(pid, stack, 0, 1);
355 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
361 n = sigsetjmp(buf, 1);
368 void forward_pending_sigio(int target)
372 if(sigpending(&sigs))
373 panic("forward_pending_sigio : sigpending failed");
374 if(sigismember(&sigs, SIGIO))
378 #ifdef UML_CONFIG_MODE_SKAS
379 static inline int check_skas3_ptrace_support(void)
381 struct ptrace_faultinfo fi;
385 printf("Checking for the skas3 patch in the host...");
386 pid = start_ptraced_child(&stack);
388 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
391 printf("not found\n");
401 stop_ptraced_child(pid, stack, 1, 1);
406 int can_do_skas(void)
410 printf("Checking for /proc/mm...");
411 if (os_access("/proc/mm", OS_ACC_W_OK) < 0) {
412 printf("not found\n");
419 ret = check_skas3_ptrace_support();
424 int can_do_skas(void)