2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
21 #include <asm/unistd.h>
23 #include <sys/types.h>
24 #include "user_util.h"
25 #include "kern_util.h"
27 #include "signal_kern.h"
28 #include "sysdep/ptrace.h"
29 #include "sysdep/sigcontext.h"
31 #include "ptrace_user.h"
35 #include "uml-config.h"
36 #include "choose-mode.h"
39 #include "kern_constants.h"
41 #ifdef UML_CONFIG_MODE_SKAS
43 #include "skas_ptrace.h"
44 #include "registers.h"
47 static int ptrace_child(void *arg
)
50 int pid
= os_getpid(), ppid
= getppid();
53 change_sig(SIGWINCH
, 0);
54 if(ptrace(PTRACE_TRACEME
, 0, 0, 0) < 0){
56 os_kill_process(pid
, 0);
60 /*This syscall will be intercepted by the parent. Don't call more than
62 sc_result
= os_getpid();
65 ret
= 1; /*Nothing modified by the parent, we are running
67 else if (sc_result
== ppid
)
68 ret
= 0; /*Expected in check_ptrace and check_sysemu when they
69 succeed in modifying the stack frame*/
71 ret
= 2; /*Serious trouble! This could be caused by a bug in
72 host 2.6 SKAS3/2.6 patch before release -V6, together
73 with a bug in the UML code itself.*/
77 static int start_ptraced_child(void **stack_out
)
83 stack
= mmap(NULL
, PAGE_SIZE
, PROT_READ
| PROT_WRITE
| PROT_EXEC
,
84 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
85 if(stack
== MAP_FAILED
)
86 panic("check_ptrace : mmap failed, errno = %d", errno
);
87 sp
= (unsigned long) stack
+ PAGE_SIZE
- sizeof(void *);
88 pid
= clone(ptrace_child
, (void *) sp
, SIGCHLD
, NULL
);
90 panic("start_ptraced_child : clone failed, errno = %d", errno
);
91 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
93 panic("check_ptrace : clone failed, errno = %d", errno
);
94 if(!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGSTOP
))
95 panic("check_ptrace : expected SIGSTOP, got status = %d",
102 /* When testing for SYSEMU support, if it is one of the broken versions, we
103 * must just avoid using sysemu, not panic, but only if SYSEMU features are
105 * So only for SYSEMU features we test mustpanic, while normal host features
108 static int stop_ptraced_child(int pid
, void *stack
, int exitcode
,
111 int status
, n
, ret
= 0;
113 if(ptrace(PTRACE_CONT
, pid
, 0, 0) < 0)
114 panic("check_ptrace : ptrace failed, errno = %d", errno
);
115 CATCH_EINTR(n
= waitpid(pid
, &status
, 0));
116 if(!WIFEXITED(status
) || (WEXITSTATUS(status
) != exitcode
)) {
117 int exit_with
= WEXITSTATUS(status
);
119 printf("check_ptrace : child exited with status 2. "
120 "Serious trouble happening! Try updating your "
121 "host skas patch!\nDisabling SYSEMU support.");
122 printf("check_ptrace : child exited with exitcode %d, while "
123 "expecting %d; status 0x%x", exit_with
,
132 if(munmap(stack
, PAGE_SIZE
) < 0)
133 panic("check_ptrace : munmap failed, errno = %d", errno
);
137 int ptrace_faultinfo
= 1;
140 int skas_needs_stub
= 0;
142 static int __init
skas0_cmd_param(char *str
, int* add
)
144 ptrace_faultinfo
= proc_mm
= 0;
148 /* The two __uml_setup would conflict, without this stupid alias. */
150 static int __init
mode_skas0_cmd_param(char *str
, int* add
)
151 __attribute__((alias("skas0_cmd_param")));
153 __uml_setup("skas0", skas0_cmd_param
,
155 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
156 " you specify mode=tt.\n\n");
158 __uml_setup("mode=skas0", mode_skas0_cmd_param
,
160 " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
161 " specify mode=tt. Note that this was recently added - on \n"
162 " older kernels you must use simply \"skas0\".\n\n");
164 static int force_sysemu_disabled
= 0;
166 static int __init
nosysemu_cmd_param(char *str
, int* add
)
168 force_sysemu_disabled
= 1;
172 __uml_setup("nosysemu", nosysemu_cmd_param
,
174 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
175 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
176 " behaviour of ptrace() and helps reducing host context switch rate.\n"
177 " To make it working, you need a kernel patch for your host, too.\n"
178 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
179 " information.\n\n");
181 static void __init
check_sysemu(void)
184 int pid
, n
, status
, count
=0;
186 printf("Checking syscall emulation patch for ptrace...");
187 sysemu_supported
= 0;
188 pid
= start_ptraced_child(&stack
);
190 if(ptrace(PTRACE_SYSEMU
, pid
, 0, 0) < 0)
193 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
195 panic("check_sysemu : wait failed, errno = %d", errno
);
196 if(!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGTRAP
))
197 panic("check_sysemu : expected SIGTRAP, "
198 "got status = %d", status
);
200 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_RET_OFFSET
,
203 panic("check_sysemu : failed to modify system "
204 "call return, errno = %d", errno
);
206 if (stop_ptraced_child(pid
, stack
, 0, 0) < 0)
209 sysemu_supported
= 1;
211 set_using_sysemu(!force_sysemu_disabled
);
213 printf("Checking advanced syscall emulation patch for ptrace...");
214 pid
= start_ptraced_child(&stack
);
216 if(ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0,
217 (void *) PTRACE_O_TRACESYSGOOD
) < 0)
218 panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d",
223 if(ptrace(PTRACE_SYSEMU_SINGLESTEP
, pid
, 0, 0) < 0)
225 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
227 panic("check_ptrace : wait failed, errno = %d", errno
);
228 if(WIFSTOPPED(status
) && (WSTOPSIG(status
) == (SIGTRAP
|0x80))){
230 panic("check_ptrace : SYSEMU_SINGLESTEP "
231 "doesn't singlestep");
232 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_RET_OFFSET
,
235 panic("check_sysemu : failed to modify system "
236 "call return, errno = %d", errno
);
239 else if(WIFSTOPPED(status
) && (WSTOPSIG(status
) == SIGTRAP
))
242 panic("check_ptrace : expected SIGTRAP or "
243 "(SIGTRAP|0x80), got status = %d", status
);
245 if (stop_ptraced_child(pid
, stack
, 0, 0) < 0)
248 sysemu_supported
= 2;
251 if ( !force_sysemu_disabled
)
252 set_using_sysemu(sysemu_supported
);
256 stop_ptraced_child(pid
, stack
, 1, 0);
261 static void __init
check_ptrace(void)
264 int pid
, syscall
, n
, status
;
266 printf("Checking that ptrace can change system call numbers...");
267 pid
= start_ptraced_child(&stack
);
269 if(ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0, (void *)PTRACE_O_TRACESYSGOOD
) < 0)
270 panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d", errno
);
273 if(ptrace(PTRACE_SYSCALL
, pid
, 0, 0) < 0)
274 panic("check_ptrace : ptrace failed, errno = %d",
276 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
278 panic("check_ptrace : wait failed, errno = %d", errno
);
279 if(!WIFSTOPPED(status
) || (WSTOPSIG(status
) != (SIGTRAP
|0x80)))
280 panic("check_ptrace : expected (SIGTRAP|0x80), "
281 "got status = %d", status
);
283 syscall
= ptrace(PTRACE_PEEKUSR
, pid
, PT_SYSCALL_NR_OFFSET
,
285 if(syscall
== __NR_getpid
){
286 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_NR_OFFSET
,
289 panic("check_ptrace : failed to modify system "
290 "call, errno = %d", errno
);
294 stop_ptraced_child(pid
, stack
, 0, 1);
299 extern void check_tmpexec(void);
301 void os_early_checks(void)
305 /* Need to check this early because mmapping happens before the
311 static int __init
noprocmm_cmd_param(char *str
, int* add
)
317 __uml_setup("noprocmm", noprocmm_cmd_param
,
319 " Turns off usage of /proc/mm, even if host supports it.\n"
320 " To support /proc/mm, the host needs to be patched using\n"
321 " the current skas3 patch.\n\n");
323 static int __init
noptracefaultinfo_cmd_param(char *str
, int* add
)
325 ptrace_faultinfo
= 0;
329 __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param
,
330 "noptracefaultinfo\n"
331 " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
332 " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
333 " using the current skas3 patch.\n\n");
335 static int __init
noptraceldt_cmd_param(char *str
, int* add
)
341 __uml_setup("noptraceldt", noptraceldt_cmd_param
,
343 " Turns off usage of PTRACE_LDT, even if host supports it.\n"
344 " To support PTRACE_LDT, the host needs to be patched using\n"
345 " the current skas3 patch.\n\n");
347 #ifdef UML_CONFIG_MODE_SKAS
348 static inline void check_skas3_ptrace_faultinfo(void)
350 struct ptrace_faultinfo fi
;
354 printf(" - PTRACE_FAULTINFO...");
355 pid
= start_ptraced_child(&stack
);
357 n
= ptrace(PTRACE_FAULTINFO
, pid
, 0, &fi
);
359 ptrace_faultinfo
= 0;
361 printf("not found\n");
366 if (!ptrace_faultinfo
)
367 printf("found but disabled on command line\n");
373 stop_ptraced_child(pid
, stack
, 1, 1);
376 static inline void check_skas3_ptrace_ldt(void)
381 unsigned char ldtbuf
[40];
382 struct ptrace_ldt ldt_op
= (struct ptrace_ldt
) {
383 .func
= 2, /* read default ldt */
385 .bytecount
= sizeof(ldtbuf
)};
387 printf(" - PTRACE_LDT...");
388 pid
= start_ptraced_child(&stack
);
390 n
= ptrace(PTRACE_LDT
, pid
, 0, (unsigned long) &ldt_op
);
393 printf("not found\n");
403 printf("found, but use is disabled\n");
406 stop_ptraced_child(pid
, stack
, 1, 1);
408 /* PTRACE_LDT might be disabled via cmdline option.
409 * We want to override this, else we might use the stub
416 static inline void check_skas3_proc_mm(void)
418 printf(" - /proc/mm...");
419 if (os_access("/proc/mm", OS_ACC_W_OK
) < 0) {
421 printf("not found\n");
425 printf("found but disabled on command line\n");
431 int can_do_skas(void)
433 printf("Checking for the skas3 patch in the host:\n");
435 check_skas3_proc_mm();
436 check_skas3_ptrace_faultinfo();
437 check_skas3_ptrace_ldt();
439 if(!proc_mm
|| !ptrace_faultinfo
|| !ptrace_ldt
)
445 int can_do_skas(void)
451 int __init
parse_iomem(char *str
, int *add
)
453 struct iomem_region
*new;
459 file
= strchr(str
,',');
461 printf("parse_iomem : failed to parse iomem\n");
466 fd
= os_open_file(file
, of_rdwr(OPENFLAGS()), 0);
468 os_print_error(fd
, "parse_iomem - Couldn't open io file");
472 err
= os_stat_fd(fd
, &buf
);
474 os_print_error(err
, "parse_iomem - cannot stat_fd file");
478 new = malloc(sizeof(*new));
480 perror("Couldn't allocate iomem_region struct");
484 size
= (buf
.ust_size
+ UM_KERN_PAGE_SIZE
) & ~(UM_KERN_PAGE_SIZE
- 1);
486 *new = ((struct iomem_region
) { .next
= iomem_regions
,
493 iomem_size
+= new->size
+ UM_KERN_PAGE_SIZE
;
503 /* Changed during early boot */
504 int pty_output_sigio
= 0;
505 int pty_close_sigio
= 0;
507 /* Used as a flag during SIGIO testing early in boot */
508 static volatile int got_sigio
= 0;
510 static void __init
handler(int sig
)
521 static void openpty_cb(void *arg
)
523 struct openpty_arg
*info
= arg
;
526 if(openpty(&info
->master
, &info
->slave
, NULL
, NULL
, NULL
))
530 static void __init
check_one_sigio(void (*proc
)(int, int))
532 struct sigaction old
, new;
533 struct openpty_arg pty
= { .master
= -1, .slave
= -1 };
534 int master
, slave
, err
;
536 initial_thread_cb(openpty_cb
, &pty
);
538 printk("openpty failed, errno = %d\n", -pty
.err
);
545 if((master
== -1) || (slave
== -1)){
546 printk("openpty failed to allocate a pty\n");
550 /* Not now, but complain so we now where we failed. */
553 panic("check_sigio : __raw failed, errno = %d\n", -err
);
555 err
= os_sigio_async(master
, slave
);
557 panic("tty_fds : sigio_async failed, err = %d\n", -err
);
559 if(sigaction(SIGIO
, NULL
, &old
) < 0)
560 panic("check_sigio : sigaction 1 failed, errno = %d\n", errno
);
562 new.sa_handler
= handler
;
563 if(sigaction(SIGIO
, &new, NULL
) < 0)
564 panic("check_sigio : sigaction 2 failed, errno = %d\n", errno
);
567 (*proc
)(master
, slave
);
572 if(sigaction(SIGIO
, &old
, NULL
) < 0)
573 panic("check_sigio : sigaction 3 failed, errno = %d\n", errno
);
576 static void tty_output(int master
, int slave
)
581 printk("Checking that host ptys support output SIGIO...");
583 memset(buf
, 0, sizeof(buf
));
585 while(os_write_file(master
, buf
, sizeof(buf
)) > 0) ;
587 panic("check_sigio : write failed, errno = %d\n", errno
);
588 while(((n
= os_read_file(slave
, buf
, sizeof(buf
))) > 0) && !got_sigio
) ;
592 pty_output_sigio
= 1;
594 else if(n
== -EAGAIN
) printk("No, enabling workaround\n");
595 else panic("check_sigio : read failed, err = %d\n", n
);
598 static void tty_close(int master
, int slave
)
600 printk("Checking that host ptys support SIGIO on close...");
607 else printk("No, enabling workaround\n");
610 void __init
check_sigio(void)
612 if((os_access("/dev/ptmx", OS_ACC_R_OK
) < 0) &&
613 (os_access("/dev/ptyp0", OS_ACC_R_OK
) < 0)){
614 printk("No pseudo-terminals available - skipping pty SIGIO "
618 check_one_sigio(tty_output
);
619 check_one_sigio(tty_close
);
622 void os_check_bugs(void)