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) - 2;
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 /* NODEFER is set here because SEGV isn't turned back on when the
61 * handler is ready to receive signals. This causes any segfault
62 * during a copy_user to kill the process because the fault is blocked.
64 set_handler(SIGSEGV, (__sighandler_t) sig_handler, flags | SA_NODEFER,
65 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
66 set_handler(SIGTRAP, (__sighandler_t) sig_handler, flags,
67 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
68 set_handler(SIGFPE, (__sighandler_t) sig_handler, flags,
69 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
70 set_handler(SIGILL, (__sighandler_t) sig_handler, flags,
71 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
72 set_handler(SIGBUS, (__sighandler_t) sig_handler, flags,
73 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
74 set_handler(SIGWINCH, (__sighandler_t) sig_handler, flags,
75 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
76 set_handler(SIGUSR2, (__sighandler_t) sig_handler,
77 SA_NOMASK | flags, -1);
78 signal(SIGHUP, SIG_IGN);
80 init_irq_signals(altstack);
86 unsigned long temp_stack;
91 /* See above for why sigkill is here */
93 int sigkill = SIGKILL;
95 int outer_tramp(void *arg)
101 t->pid = clone(t->tramp, (void *) t->temp_stack + page_size()/2,
102 t->flags, t->tramp_data);
103 if(t->pid > 0) wait_for_stop(t->pid, SIGSTOP, PTRACE_CONT, NULL);
104 kill(os_getpid(), sig);
108 int start_fork_tramp(void *thread_arg, unsigned long temp_stack,
109 int clone_flags, int (*tramp)(void *))
113 int new_pid, status, err;
115 /* The trampoline will run on the temporary stack */
116 sp = stack_sp(temp_stack);
118 clone_flags |= CLONE_FILES | SIGCHLD;
121 arg.tramp_data = thread_arg;
122 arg.temp_stack = temp_stack;
123 arg.flags = clone_flags;
125 /* Start the process and wait for it to kill itself */
126 new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
127 if(new_pid < 0) return(-errno);
128 while(((err = waitpid(new_pid, &status, 0)) < 0) && (errno == EINTR)) ;
129 if(err < 0) 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 void suspend_new_thread(int fd)
142 os_stop_process(os_getpid());
144 if(os_read_file(fd, &c, sizeof(c)) != sizeof(c))
145 panic("read failed in suspend_new_thread");
148 static int ptrace_child(void *arg)
150 int pid = os_getpid();
152 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
154 os_kill_process(pid, 0);
156 os_stop_process(pid);
157 _exit(os_getpid() == pid);
160 static int start_ptraced_child(void **stack_out)
166 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
167 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
168 if(stack == MAP_FAILED)
169 panic("check_ptrace : mmap failed, errno = %d", errno);
170 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
171 pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
173 panic("check_ptrace : clone failed, errno = %d", errno);
174 n = waitpid(pid, &status, WUNTRACED);
176 panic("check_ptrace : wait failed, errno = %d", errno);
177 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
178 panic("check_ptrace : expected SIGSTOP, got status = %d",
185 static void stop_ptraced_child(int pid, void *stack, int exitcode)
189 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
190 panic("check_ptrace : ptrace failed, errno = %d", errno);
191 n = waitpid(pid, &status, 0);
192 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode))
193 panic("check_ptrace : child exited with status 0x%x", status);
195 if(munmap(stack, PAGE_SIZE) < 0)
196 panic("check_ptrace : munmap failed, errno = %d", errno);
199 static int force_sysemu_disabled = 0;
201 static int __init nosysemu_cmd_param(char *str, int* add)
203 force_sysemu_disabled = 1;
207 __uml_setup("nosysemu", nosysemu_cmd_param,
209 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
210 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
211 " behaviour of ptrace() and helps reducing host context switch rate.\n"
212 " To make it working, you need a kernel patch for your host, too.\n"
213 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further information.\n");
215 void __init check_ptrace(void)
218 int pid, syscall, n, status;
220 printk("Checking that ptrace can change system call numbers...");
221 pid = start_ptraced_child(&stack);
224 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
225 panic("check_ptrace : ptrace failed, errno = %d",
227 n = waitpid(pid, &status, WUNTRACED);
229 panic("check_ptrace : wait failed, errno = %d", errno);
230 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
231 panic("check_ptrace : expected SIGTRAP, "
232 "got status = %d", status);
234 syscall = ptrace(PTRACE_PEEKUSER, pid, PT_SYSCALL_NR_OFFSET,
236 if(syscall == __NR_getpid){
237 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
240 panic("check_ptrace : failed to modify system "
241 "call, errno = %d", errno);
245 stop_ptraced_child(pid, stack, 0);
248 printk("Checking syscall emulation patch for ptrace...");
250 pid = start_ptraced_child(&stack);
251 if(ptrace(PTRACE_SYSEMU, pid, 0, 0) >= 0) {
252 struct user_regs_struct regs;
254 if (waitpid(pid, &status, WUNTRACED) < 0)
255 panic("check_ptrace : wait failed, errno = %d", errno);
256 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
257 panic("check_ptrace : expected SIGTRAP, "
258 "got status = %d", status);
260 if (ptrace(PTRACE_GETREGS, pid, 0, ®s) < 0)
261 panic("check_ptrace : failed to read child "
262 "registers, errno = %d", errno);
264 if (ptrace(PTRACE_SETREGS, pid, 0, ®s) < 0)
265 panic("check_ptrace : failed to modify child "
266 "registers, errno = %d", errno);
268 stop_ptraced_child(pid, stack, 0);
270 if (!force_sysemu_disabled) {
274 printk("found but disabled\n");
280 stop_ptraced_child(pid, stack, 1);
285 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
291 n = sigsetjmp(buf, 1);
298 void forward_pending_sigio(int target)
302 if(sigpending(&sigs))
303 panic("forward_pending_sigio : sigpending failed");
304 if(sigismember(&sigs, SIGIO))
308 int can_do_skas(void)
310 #ifdef UML_CONFIG_MODE_SKAS
311 struct ptrace_faultinfo fi;
315 printf("Checking for the skas3 patch in the host...");
316 pid = start_ptraced_child(&stack);
318 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
321 printf("not found\n");
322 else printf("No (unexpected errno - %d)\n", errno);
325 else printf("found\n");
328 stop_ptraced_child(pid, stack, 1);
330 printf("Checking for /proc/mm...");
331 if(os_access("/proc/mm", OS_ACC_W_OK) < 0){
332 printf("not found\n");
335 else printf("found\n");
344 * Overrides for Emacs so that we follow Linus's tabbing style.
345 * Emacs will notice this stuff at the end of the file and automatically
346 * adjust the settings for this buffer only. This must remain at the end
348 * ---------------------------------------------------------------------------
350 * c-file-style: "linux"