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
)
140 int pid
= os_getpid();
142 if(ptrace(PTRACE_TRACEME
, 0, 0, 0) < 0){
144 os_kill_process(pid
, 0);
146 os_stop_process(pid
);
147 _exit(os_getpid() == pid
);
150 static int start_ptraced_child(void **stack_out
)
156 stack
= mmap(NULL
, PAGE_SIZE
, PROT_READ
| PROT_WRITE
| PROT_EXEC
,
157 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
158 if(stack
== MAP_FAILED
)
159 panic("check_ptrace : mmap failed, errno = %d", errno
);
160 sp
= (unsigned long) stack
+ PAGE_SIZE
- sizeof(void *);
161 pid
= clone(ptrace_child
, (void *) sp
, SIGCHLD
, NULL
);
163 panic("check_ptrace : clone failed, errno = %d", errno
);
164 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
166 panic("check_ptrace : wait failed, errno = %d", errno
);
167 if(!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGSTOP
))
168 panic("check_ptrace : expected SIGSTOP, got status = %d",
175 static void stop_ptraced_child(int pid
, void *stack
, int exitcode
)
179 if(ptrace(PTRACE_CONT
, pid
, 0, 0) < 0)
180 panic("check_ptrace : ptrace failed, errno = %d", errno
);
181 CATCH_EINTR(n
= waitpid(pid
, &status
, 0));
182 if(!WIFEXITED(status
) || (WEXITSTATUS(status
) != exitcode
))
183 panic("check_ptrace : child exited with status 0x%x", status
);
185 if(munmap(stack
, PAGE_SIZE
) < 0)
186 panic("check_ptrace : munmap failed, errno = %d", errno
);
189 static int force_sysemu_disabled
= 0;
191 static int __init
nosysemu_cmd_param(char *str
, int* add
)
193 force_sysemu_disabled
= 1;
197 __uml_setup("nosysemu", nosysemu_cmd_param
,
199 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
200 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
201 " behaviour of ptrace() and helps reducing host context switch rate.\n"
202 " To make it working, you need a kernel patch for your host, too.\n"
203 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further information.\n");
205 static void __init
check_sysemu(void)
213 printk("Checking syscall emulation patch for ptrace...");
214 sysemu_supported
= 0;
215 pid
= start_ptraced_child(&stack
);
216 if(ptrace(PTRACE_SYSEMU
, pid
, 0, 0) >= 0) {
217 struct user_regs_struct regs
;
219 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
221 panic("check_ptrace : wait failed, errno = %d", errno
);
222 if(!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGTRAP
))
223 panic("check_ptrace : expected SIGTRAP, "
224 "got status = %d", status
);
226 if (ptrace(PTRACE_GETREGS
, pid
, 0, ®s
) < 0)
227 panic("check_ptrace : failed to read child "
228 "registers, errno = %d", errno
);
230 if (ptrace(PTRACE_SETREGS
, pid
, 0, ®s
) < 0)
231 panic("check_ptrace : failed to modify child "
232 "registers, errno = %d", errno
);
234 stop_ptraced_child(pid
, stack
, 0);
236 sysemu_supported
= 1;
241 stop_ptraced_child(pid
, stack
, 1);
242 sysemu_supported
= 0;
246 set_using_sysemu(!force_sysemu_disabled
);
249 void __init
check_ptrace(void)
252 int pid
, syscall
, n
, status
;
254 printk("Checking that ptrace can change system call numbers...");
255 pid
= start_ptraced_child(&stack
);
258 if(ptrace(PTRACE_SYSCALL
, pid
, 0, 0) < 0)
259 panic("check_ptrace : ptrace failed, errno = %d",
261 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
263 panic("check_ptrace : wait failed, errno = %d", errno
);
264 if(!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGTRAP
))
265 panic("check_ptrace : expected SIGTRAP, "
266 "got status = %d", status
);
268 syscall
= ptrace(PTRACE_PEEKUSER
, pid
, PT_SYSCALL_NR_OFFSET
,
270 if(syscall
== __NR_getpid
){
271 n
= ptrace(PTRACE_POKEUSER
, pid
, PT_SYSCALL_NR_OFFSET
,
274 panic("check_ptrace : failed to modify system "
275 "call, errno = %d", errno
);
279 stop_ptraced_child(pid
, stack
, 0);
284 int run_kernel_thread(int (*fn
)(void *), void *arg
, void **jmp_ptr
)
290 n
= sigsetjmp(buf
, 1);
297 void forward_pending_sigio(int target
)
301 if(sigpending(&sigs
))
302 panic("forward_pending_sigio : sigpending failed");
303 if(sigismember(&sigs
, SIGIO
))
307 int can_do_skas(void)
309 #ifdef UML_CONFIG_MODE_SKAS
310 struct ptrace_faultinfo fi
;
314 printf("Checking for the skas3 patch in the host...");
315 pid
= start_ptraced_child(&stack
);
317 n
= ptrace(PTRACE_FAULTINFO
, pid
, 0, &fi
);
320 printf("not found\n");
321 else printf("No (unexpected errno - %d)\n", errno
);
324 else printf("found\n");
327 stop_ptraced_child(pid
, stack
, 1);
329 printf("Checking for /proc/mm...");
330 if(os_access("/proc/mm", OS_ACC_W_OK
) < 0){
331 printf("not found\n");
334 else printf("found\n");
343 * Overrides for Emacs so that we follow Linus's tabbing style.
344 * Emacs will notice this stuff at the end of the file and automatically
345 * adjust the settings for this buffer only. This must remain at the end
347 * ---------------------------------------------------------------------------
349 * c-file-style: "linux"