2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
16 #include <sys/ptrace.h>
17 #include <sys/ioctl.h>
20 #include <asm/ptrace.h>
21 #include <asm/sigcontext.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/sigcontext.h"
33 #include "ptrace_user.h"
34 #include "time_user.h"
37 #include "uml-config.h"
38 #include "choose-mode.h"
40 #ifdef UML_CONFIG_MODE_SKAS
42 #include "skas_ptrace.h"
45 void init_new_thread_stack(void *sig_stack, void (*usr1_handler)(int))
49 if(sig_stack != NULL){
50 pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER) - 2;
51 set_sigstack(sig_stack, pages * page_size());
54 if(usr1_handler) set_handler(SIGUSR1, usr1_handler, flags, -1);
57 void init_new_thread_signals(int altstack)
59 int flags = altstack ? SA_ONSTACK : 0;
61 set_handler(SIGSEGV, (__sighandler_t) sig_handler, flags,
62 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
63 set_handler(SIGTRAP, (__sighandler_t) sig_handler, flags,
64 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
65 set_handler(SIGFPE, (__sighandler_t) sig_handler, flags,
66 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
67 set_handler(SIGILL, (__sighandler_t) sig_handler, flags,
68 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
69 set_handler(SIGBUS, (__sighandler_t) sig_handler, flags,
70 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
71 set_handler(SIGWINCH, (__sighandler_t) sig_handler, flags,
72 SIGUSR1, SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, -1);
73 set_handler(SIGUSR2, (__sighandler_t) sig_handler,
74 SA_NOMASK | flags, -1);
75 (void) CHOOSE_MODE(signal(SIGCHLD, SIG_IGN), (void *) 0);
76 signal(SIGHUP, SIG_IGN);
78 init_irq_signals(altstack);
84 unsigned long temp_stack;
89 /* See above for why sigkill is here */
91 int sigkill = SIGKILL;
93 int outer_tramp(void *arg)
99 t->pid = clone(t->tramp, (void *) t->temp_stack + page_size()/2,
100 t->flags, t->tramp_data);
101 if(t->pid > 0) wait_for_stop(t->pid, SIGSTOP, PTRACE_CONT, NULL);
102 kill(os_getpid(), sig);
106 int start_fork_tramp(void *thread_arg, unsigned long temp_stack,
107 int clone_flags, int (*tramp)(void *))
111 int new_pid, status, err;
113 /* The trampoline will run on the temporary stack */
114 sp = stack_sp(temp_stack);
116 clone_flags |= CLONE_FILES | SIGCHLD;
119 arg.tramp_data = thread_arg;
120 arg.temp_stack = temp_stack;
121 arg.flags = clone_flags;
123 /* Start the process and wait for it to kill itself */
124 new_pid = clone(outer_tramp, (void *) sp, clone_flags, &arg);
125 if(new_pid < 0) return(-errno);
126 while((err = waitpid(new_pid, &status, 0) < 0) && (errno == EINTR)) ;
127 if(err < 0) panic("Waiting for outer trampoline failed - errno = %d",
129 if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGKILL))
130 panic("outer trampoline didn't exit with SIGKILL");
135 void suspend_new_thread(int fd)
139 os_stop_process(os_getpid());
141 if(read(fd, &c, sizeof(c)) != sizeof(c))
142 panic("read failed in suspend_new_thread");
145 static int ptrace_child(void *arg)
147 int pid = os_getpid();
149 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
151 os_kill_process(pid, 0);
153 os_stop_process(pid);
154 _exit(os_getpid() == pid);
157 static int start_ptraced_child(void **stack_out)
163 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
164 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
165 if(stack == MAP_FAILED)
166 panic("check_ptrace : mmap failed, errno = %d", errno);
167 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
168 pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
170 panic("check_ptrace : clone failed, errno = %d", errno);
171 n = waitpid(pid, &status, WUNTRACED);
173 panic("check_ptrace : wait failed, errno = %d", errno);
174 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
175 panic("check_ptrace : expected SIGSTOP, got status = %d",
182 static void stop_ptraced_child(int pid, void *stack, int exitcode)
186 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
187 panic("check_ptrace : ptrace failed, errno = %d", errno);
188 n = waitpid(pid, &status, 0);
189 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode))
190 panic("check_ptrace : child exited with status 0x%x", status);
192 if(munmap(stack, PAGE_SIZE) < 0)
193 panic("check_ptrace : munmap failed, errno = %d", errno);
196 void __init check_ptrace(void)
199 int pid, syscall, n, status;
201 printk("Checking that ptrace can change system call numbers...");
202 pid = start_ptraced_child(&stack);
205 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
206 panic("check_ptrace : ptrace failed, errno = %d",
208 n = waitpid(pid, &status, WUNTRACED);
210 panic("check_ptrace : wait failed, errno = %d", errno);
211 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
212 panic("check_ptrace : expected SIGTRAP, "
213 "got status = %d", status);
215 syscall = ptrace(PTRACE_PEEKUSER, pid, PT_SYSCALL_NR_OFFSET,
217 if(syscall == __NR_getpid){
218 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
221 panic("check_ptrace : failed to modify system "
222 "call, errno = %d", errno);
226 stop_ptraced_child(pid, stack, 0);
230 int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
243 void forward_pending_sigio(int target)
247 if(sigpending(&sigs))
248 panic("forward_pending_sigio : sigpending failed");
249 if(sigismember(&sigs, SIGIO))
253 int can_do_skas(void)
255 #ifdef UML_CONFIG_MODE_SKAS
256 struct ptrace_faultinfo fi;
260 printf("Checking for the skas3 patch in the host...");
261 pid = start_ptraced_child(&stack);
263 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
266 printf("not found\n");
267 else printf("No (unexpected errno - %d)\n", errno);
270 else printf("found\n");
273 stop_ptraced_child(pid, stack, 1);
275 printf("Checking for /proc/mm...");
276 if(access("/proc/mm", W_OK)){
277 printf("not found\n");
280 else printf("found\n");
289 * Overrides for Emacs so that we follow Linus's tabbing style.
290 * Emacs will notice this stuff at the end of the file and automatically
291 * adjust the settings for this buffer only. This must remain at the end
293 * ---------------------------------------------------------------------------
295 * c-file-style: "linux"