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>
22 #include "user_util.h"
23 #include "kern_util.h"
26 #include "signal_kern.h"
27 #include "signal_user.h"
28 #include "sysdep/ptrace.h"
29 #include "sysdep/sigcontext.h"
31 #include "ptrace_user.h"
32 #include "time_user.h"
35 #include "uml-config.h"
36 #include "choose-mode.h"
38 #ifdef UML_CONFIG_MODE_SKAS
40 #include "skas_ptrace.h"
43 void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
47 if(sig_stack != NULL){
48 pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER) - 2;
49 set_sigstack(sig_stack, pages * page_size());
52 if(usr1_handler) set_handler(SIGUSR1, usr1_handler, flags, -1);
55 void init_new_thread_signals(int altstack)
57 int flags = altstack ? SA_ONSTACK : 0;
59 /* NODEFER is set here because SEGV isn't turned back on when the
60 * handler is ready to receive signals. This causes any segfault
61 * during a copy_user to kill the process because the fault is blocked.
63 set_handler(SIGSEGV, (__sighandler_t) sig_handler, flags | SA_NODEFER,
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 SA_NOMASK | flags, -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);
126 if(new_pid < 0) return(-errno);
127 while(((err = waitpid(new_pid, &status, 0)) < 0) && (errno == EINTR)) ;
128 if(err < 0) panic("Waiting for outer trampoline failed - errno = %d",
130 if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
131 panic("outer trampoline didn't exit with SIGKILL, "
132 "status = %d", status);
137 void suspend_new_thread(int fd)
141 os_stop_process(os_getpid());
143 if(os_read_file(fd, &c, sizeof(c)) != sizeof(c))
144 panic("read failed in suspend_new_thread");
147 static int ptrace_child(void *arg)
149 int pid = os_getpid();
151 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
153 os_kill_process(pid, 0);
155 os_stop_process(pid);
156 _exit(os_getpid() == pid);
159 static int start_ptraced_child(void **stack_out)
165 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
166 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
167 if(stack == MAP_FAILED)
168 panic("check_ptrace : mmap failed, errno = %d", errno);
169 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
170 pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
172 panic("check_ptrace : clone failed, errno = %d", errno);
173 n = waitpid(pid, &status, WUNTRACED);
175 panic("check_ptrace : wait failed, errno = %d", errno);
176 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
177 panic("check_ptrace : expected SIGSTOP, got status = %d",
184 static void stop_ptraced_child(int pid, void *stack, int exitcode)
188 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
189 panic("check_ptrace : ptrace failed, errno = %d", errno);
190 n = waitpid(pid, &status, 0);
191 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode))
192 panic("check_ptrace : child exited with status 0x%x", status);
194 if(munmap(stack, PAGE_SIZE) < 0)
195 panic("check_ptrace : munmap failed, errno = %d", errno);
198 void __init check_ptrace(void)
201 int pid, syscall, n, status;
203 printk("Checking that ptrace can change system call numbers...");
204 pid = start_ptraced_child(&stack);
207 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
208 panic("check_ptrace : ptrace failed, errno = %d",
210 n = waitpid(pid, &status, WUNTRACED);
212 panic("check_ptrace : wait failed, errno = %d", errno);
213 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
214 panic("check_ptrace : expected SIGTRAP, "
215 "got status = %d", status);
217 syscall = ptrace(PTRACE_PEEKUSER, pid, PT_SYSCALL_NR_OFFSET,
219 if(syscall == __NR_getpid){
220 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
223 panic("check_ptrace : failed to modify system "
224 "call, errno = %d", errno);
228 stop_ptraced_child(pid, stack, 0);
232 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
238 n = sigsetjmp(buf, 1);
245 void forward_pending_sigio(int target)
249 if(sigpending(&sigs))
250 panic("forward_pending_sigio : sigpending failed");
251 if(sigismember(&sigs, SIGIO))
255 int can_do_skas(void)
257 #ifdef UML_CONFIG_MODE_SKAS
258 struct ptrace_faultinfo fi;
262 printf("Checking for the skas3 patch in the host...");
263 pid = start_ptraced_child(&stack);
265 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
268 printf("not found\n");
269 else printf("No (unexpected errno - %d)\n", errno);
272 else printf("found\n");
275 stop_ptraced_child(pid, stack, 1);
277 printf("Checking for /proc/mm...");
278 if(os_access("/proc/mm", OS_ACC_W_OK) < 0){
279 printf("not found\n");
282 else printf("found\n");
291 * Overrides for Emacs so that we follow Linus's tabbing style.
292 * Emacs will notice this stuff at the end of the file and automatically
293 * adjust the settings for this buffer only. This must remain at the end
295 * ---------------------------------------------------------------------------
297 * c-file-style: "linux"