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);
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 set_handler(SIGSEGV, (__sighandler_t) sig_handler, flags,
60 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
61 set_handler(SIGTRAP, (__sighandler_t) sig_handler, flags,
62 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
63 set_handler(SIGFPE, (__sighandler_t) sig_handler, flags,
64 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
65 set_handler(SIGILL, (__sighandler_t) sig_handler, flags,
66 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
67 set_handler(SIGBUS, (__sighandler_t) sig_handler, flags,
68 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
69 set_handler(SIGWINCH, (__sighandler_t) sig_handler, flags,
70 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
71 set_handler(SIGUSR2, (__sighandler_t) sig_handler,
72 flags, SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
73 signal(SIGHUP, SIG_IGN);
75 init_irq_signals(altstack);
81 unsigned long temp_stack;
86 /* See above for why sigkill is here */
88 int sigkill = SIGKILL;
90 int outer_tramp(void *arg)
96 t->pid = clone(t->tramp, (void *) t->temp_stack + page_size()/2,
97 t->flags, t->tramp_data);
98 if(t->pid > 0) wait_for_stop(t->pid, SIGSTOP, PTRACE_CONT, NULL);
99 kill(os_getpid(), sig);
103 int start_fork_tramp(void *thread_arg, unsigned long temp_stack,
104 int clone_flags, int (*tramp)(void *))
108 int new_pid, status, err;
110 /* The trampoline will run on the temporary stack */
111 sp = stack_sp(temp_stack);
113 clone_flags |= CLONE_FILES | SIGCHLD;
116 arg.tramp_data = thread_arg;
117 arg.temp_stack = temp_stack;
118 arg.flags = clone_flags;
120 /* Start the process and wait for it to kill itself */
121 new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
125 CATCH_EINTR(err = waitpid(new_pid, &status, 0));
127 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 static int ptrace_child(void *arg)
139 int pid = os_getpid();
141 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
143 os_kill_process(pid, 0);
145 os_stop_process(pid);
146 _exit(os_getpid() == pid);
149 static int start_ptraced_child(void **stack_out)
155 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
156 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
157 if(stack == MAP_FAILED)
158 panic("check_ptrace : mmap failed, errno = %d", errno);
159 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
160 pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
162 panic("check_ptrace : clone failed, errno = %d", errno);
163 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
165 panic("check_ptrace : wait failed, errno = %d", errno);
166 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
167 panic("check_ptrace : expected SIGSTOP, got status = %d",
174 static void stop_ptraced_child(int pid, void *stack, int exitcode)
178 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
179 panic("check_ptrace : ptrace failed, errno = %d", errno);
180 CATCH_EINTR(n = waitpid(pid, &status, 0));
181 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode))
182 panic("check_ptrace : child exited with status 0x%x", status);
184 if(munmap(stack, PAGE_SIZE) < 0)
185 panic("check_ptrace : munmap failed, errno = %d", errno);
188 void __init check_ptrace(void)
191 int pid, syscall, n, status;
193 printk("Checking that ptrace can change system call numbers...");
194 pid = start_ptraced_child(&stack);
197 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
198 panic("check_ptrace : ptrace failed, errno = %d",
200 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
202 panic("check_ptrace : wait failed, errno = %d", errno);
203 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
204 panic("check_ptrace : expected SIGTRAP, "
205 "got status = %d", status);
207 syscall = ptrace(PTRACE_PEEKUSER, pid, PT_SYSCALL_NR_OFFSET,
209 if(syscall == __NR_getpid){
210 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
213 panic("check_ptrace : failed to modify system "
214 "call, errno = %d", errno);
218 stop_ptraced_child(pid, stack, 0);
222 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
228 n = sigsetjmp(buf, 1);
235 void forward_pending_sigio(int target)
239 if(sigpending(&sigs))
240 panic("forward_pending_sigio : sigpending failed");
241 if(sigismember(&sigs, SIGIO))
245 int can_do_skas(void)
247 #ifdef UML_CONFIG_MODE_SKAS
248 struct ptrace_faultinfo fi;
252 printf("Checking for the skas3 patch in the host...");
253 pid = start_ptraced_child(&stack);
255 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
258 printf("not found\n");
259 else printf("No (unexpected errno - %d)\n", errno);
262 else printf("found\n");
265 stop_ptraced_child(pid, stack, 1);
267 printf("Checking for /proc/mm...");
268 if(os_access("/proc/mm", OS_ACC_W_OK) < 0){
269 printf("not found\n");
272 else printf("found\n");
281 * Overrides for Emacs so that we follow Linus's tabbing style.
282 * Emacs will notice this stuff at the end of the file and automatically
283 * adjust the settings for this buffer only. This must remain at the end
285 * ---------------------------------------------------------------------------
287 * c-file-style: "linux"