2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
15 #include <sys/ptrace.h>
18 #include <asm/ptrace.h>
19 #include <asm/sigcontext.h>
20 #include <asm/unistd.h>
23 #include "user_util.h"
24 #include "kern_util.h"
27 #include "signal_kern.h"
28 #include "signal_user.h"
29 #include "sysdep/ptrace.h"
30 #include "sysdep/sigcontext.h"
32 #include "ptrace_user.h"
33 #include "time_user.h"
36 #include "uml-config.h"
37 #include "choose-mode.h"
39 #ifdef UML_CONFIG_MODE_SKAS
41 #include "skas_ptrace.h"
44 void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
48 if(sig_stack != NULL){
49 pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER);
50 set_sigstack(sig_stack, pages * page_size());
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);
82 unsigned long temp_stack;
87 /* See above for why sigkill is here */
89 int sigkill = SIGKILL;
91 int outer_tramp(void *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);
104 int start_fork_tramp(void *thread_arg, unsigned long temp_stack,
105 int clone_flags, int (*tramp)(void *))
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;
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);
126 CATCH_EINTR(err = waitpid(new_pid, &status, 0));
128 panic("Waiting for outer trampoline failed - errno = %d",
131 if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
132 panic("outer trampoline didn't exit with SIGKILL, "
133 "status = %d", status);
138 static int ptrace_child(void *arg)
141 int pid = os_getpid(), ppid = getppid();
144 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
146 os_kill_process(pid, 0);
148 os_stop_process(pid);
150 /*This syscall will be intercepted by the parent. Don't call more than
152 sc_result = os_getpid();
154 if (sc_result == pid)
155 ret = 1; /*Nothing modified by the parent, we are running
157 else if (sc_result == ppid)
158 ret = 0; /*Expected in check_ptrace and check_sysemu when they
159 succeed in modifying the stack frame*/
161 ret = 2; /*Serious trouble! This could be caused by a bug in
162 host 2.6 SKAS3/2.6 patch before release -V6, together
163 with a bug in the UML code itself.*/
167 static int start_ptraced_child(void **stack_out)
173 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
174 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
175 if(stack == MAP_FAILED)
176 panic("check_ptrace : mmap failed, errno = %d", errno);
177 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
178 pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
180 panic("check_ptrace : clone failed, errno = %d", errno);
181 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
183 panic("check_ptrace : wait failed, errno = %d", errno);
184 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
185 panic("check_ptrace : expected SIGSTOP, got status = %d",
192 /* When testing for SYSEMU support, if it is one of the broken versions, we must
193 * just avoid using sysemu, not panic, but only if SYSEMU features are broken.
194 * So only for SYSEMU features we test mustpanic, while normal host features
195 * must work anyway!*/
196 static int stop_ptraced_child(int pid, void *stack, int exitcode, int mustpanic)
198 int status, n, ret = 0;
200 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
201 panic("check_ptrace : ptrace failed, errno = %d", errno);
202 CATCH_EINTR(n = waitpid(pid, &status, 0));
203 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
204 int exit_with = WEXITSTATUS(status);
206 printk("check_ptrace : child exited with status 2. "
207 "Serious trouble happening! Try updating your "
208 "host skas patch!\nDisabling SYSEMU support.");
209 printk("check_ptrace : child exited with exitcode %d, while "
210 "expecting %d; status 0x%x", exit_with,
219 if(munmap(stack, PAGE_SIZE) < 0)
220 panic("check_ptrace : munmap failed, errno = %d", errno);
224 static int force_sysemu_disabled = 0;
226 static int __init nosysemu_cmd_param(char *str, int* add)
228 force_sysemu_disabled = 1;
232 __uml_setup("nosysemu", nosysemu_cmd_param,
234 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
235 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
236 " behaviour of ptrace() and helps reducing host context switch rate.\n"
237 " To make it working, you need a kernel patch for your host, too.\n"
238 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further information.\n\n");
240 static void __init check_sysemu(void)
245 printk("Checking syscall emulation patch for ptrace...");
246 sysemu_supported = 0;
247 pid = start_ptraced_child(&stack);
249 if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
252 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
254 panic("check_sysemu : wait failed, errno = %d", errno);
255 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
256 panic("check_sysemu : expected SIGTRAP, "
257 "got status = %d", status);
259 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_RET_OFFSET,
262 panic("check_sysemu : failed to modify system "
263 "call return, errno = %d", errno);
265 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
268 sysemu_supported = 1;
270 set_using_sysemu(!force_sysemu_disabled);
274 stop_ptraced_child(pid, stack, 1, 0);
276 sysemu_supported = 0;
280 void __init check_ptrace(void)
283 int pid, syscall, n, status;
285 printk("Checking that ptrace can change system call numbers...");
286 pid = start_ptraced_child(&stack);
289 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
290 panic("check_ptrace : ptrace failed, errno = %d",
292 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
294 panic("check_ptrace : wait failed, errno = %d", errno);
295 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
296 panic("check_ptrace : expected SIGTRAP, "
297 "got status = %d", status);
299 syscall = ptrace(PTRACE_PEEKUSER, pid, PT_SYSCALL_NR_OFFSET,
301 if(syscall == __NR_getpid){
302 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
305 panic("check_ptrace : failed to modify system "
306 "call, errno = %d", errno);
310 stop_ptraced_child(pid, stack, 0, 1);
315 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
321 n = sigsetjmp(buf, 1);
328 void forward_pending_sigio(int target)
332 if(sigpending(&sigs))
333 panic("forward_pending_sigio : sigpending failed");
334 if(sigismember(&sigs, SIGIO))
338 int can_do_skas(void)
340 #ifdef UML_CONFIG_MODE_SKAS
341 struct ptrace_faultinfo fi;
345 printf("Checking for the skas3 patch in the host...");
346 pid = start_ptraced_child(&stack);
348 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
351 printf("not found\n");
352 else printf("No (unexpected errno - %d)\n", errno);
355 else printf("found\n");
358 stop_ptraced_child(pid, stack, 1, 1);
360 printf("Checking for /proc/mm...");
361 if(os_access("/proc/mm", OS_ACC_W_OK) < 0){
362 printf("not found\n");
365 else printf("found\n");
374 * Overrides for Emacs so that we follow Linus's tabbing style.
375 * Emacs will notice this stuff at the end of the file and automatically
376 * adjust the settings for this buffer only. This must remain at the end
378 * ---------------------------------------------------------------------------
380 * c-file-style: "linux"