2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
20 #include <asm/unistd.h>
22 #include <sys/types.h>
23 #include "user_util.h"
24 #include "kern_util.h"
26 #include "signal_kern.h"
27 #include "sysdep/ptrace.h"
28 #include "sysdep/sigcontext.h"
30 #include "ptrace_user.h"
34 #include "uml-config.h"
35 #include "choose-mode.h"
38 #include "kern_constants.h"
40 #ifdef UML_CONFIG_MODE_SKAS
42 #include "skas_ptrace.h"
43 #include "registers.h"
46 static int ptrace_child(void *arg
)
49 int pid
= os_getpid(), ppid
= getppid();
52 change_sig(SIGWINCH
, 0);
53 if(ptrace(PTRACE_TRACEME
, 0, 0, 0) < 0){
55 os_kill_process(pid
, 0);
59 /*This syscall will be intercepted by the parent. Don't call more than
61 sc_result
= os_getpid();
64 ret
= 1; /*Nothing modified by the parent, we are running
66 else if (sc_result
== ppid
)
67 ret
= 0; /*Expected in check_ptrace and check_sysemu when they
68 succeed in modifying the stack frame*/
70 ret
= 2; /*Serious trouble! This could be caused by a bug in
71 host 2.6 SKAS3/2.6 patch before release -V6, together
72 with a bug in the UML code itself.*/
76 static int start_ptraced_child(void **stack_out
)
82 stack
= mmap(NULL
, PAGE_SIZE
, PROT_READ
| PROT_WRITE
| PROT_EXEC
,
83 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
84 if(stack
== MAP_FAILED
)
85 panic("check_ptrace : mmap failed, errno = %d", errno
);
86 sp
= (unsigned long) stack
+ PAGE_SIZE
- sizeof(void *);
87 pid
= clone(ptrace_child
, (void *) sp
, SIGCHLD
, NULL
);
89 panic("start_ptraced_child : clone failed, errno = %d", errno
);
90 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
92 panic("check_ptrace : clone failed, errno = %d", errno
);
93 if(!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGSTOP
))
94 panic("check_ptrace : expected SIGSTOP, got status = %d",
101 /* When testing for SYSEMU support, if it is one of the broken versions, we
102 * must just avoid using sysemu, not panic, but only if SYSEMU features are
104 * So only for SYSEMU features we test mustpanic, while normal host features
107 static int stop_ptraced_child(int pid
, void *stack
, int exitcode
,
110 int status
, n
, ret
= 0;
112 if(ptrace(PTRACE_CONT
, pid
, 0, 0) < 0)
113 panic("check_ptrace : ptrace failed, errno = %d", errno
);
114 CATCH_EINTR(n
= waitpid(pid
, &status
, 0));
115 if(!WIFEXITED(status
) || (WEXITSTATUS(status
) != exitcode
)) {
116 int exit_with
= WEXITSTATUS(status
);
118 printf("check_ptrace : child exited with status 2. "
119 "Serious trouble happening! Try updating your "
120 "host skas patch!\nDisabling SYSEMU support.");
121 printf("check_ptrace : child exited with exitcode %d, while "
122 "expecting %d; status 0x%x", exit_with
,
131 if(munmap(stack
, PAGE_SIZE
) < 0)
132 panic("check_ptrace : munmap failed, errno = %d", errno
);
136 /* Changed only during early boot */
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 /* Changed only during early boot */
165 static int force_sysemu_disabled
= 0;
167 static int __init
nosysemu_cmd_param(char *str
, int* add
)
169 force_sysemu_disabled
= 1;
173 __uml_setup("nosysemu", nosysemu_cmd_param
,
175 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
176 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
177 " behaviour of ptrace() and helps reducing host context switch rate.\n"
178 " To make it working, you need a kernel patch for your host, too.\n"
179 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
180 " information.\n\n");
182 static void __init
check_sysemu(void)
185 int pid
, n
, status
, count
=0;
187 printf("Checking syscall emulation patch for ptrace...");
188 sysemu_supported
= 0;
189 pid
= start_ptraced_child(&stack
);
191 if(ptrace(PTRACE_SYSEMU
, pid
, 0, 0) < 0)
194 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
196 panic("check_sysemu : wait failed, errno = %d", errno
);
197 if(!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGTRAP
))
198 panic("check_sysemu : expected SIGTRAP, "
199 "got status = %d", status
);
201 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_RET_OFFSET
,
204 panic("check_sysemu : failed to modify system "
205 "call return, errno = %d", errno
);
207 if (stop_ptraced_child(pid
, stack
, 0, 0) < 0)
210 sysemu_supported
= 1;
212 set_using_sysemu(!force_sysemu_disabled
);
214 printf("Checking advanced syscall emulation patch for ptrace...");
215 pid
= start_ptraced_child(&stack
);
217 if(ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0,
218 (void *) PTRACE_O_TRACESYSGOOD
) < 0)
219 panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d",
224 if(ptrace(PTRACE_SYSEMU_SINGLESTEP
, pid
, 0, 0) < 0)
226 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
228 panic("check_ptrace : wait failed, errno = %d", errno
);
229 if(WIFSTOPPED(status
) && (WSTOPSIG(status
) == (SIGTRAP
|0x80))){
231 panic("check_ptrace : SYSEMU_SINGLESTEP "
232 "doesn't singlestep");
233 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_RET_OFFSET
,
236 panic("check_sysemu : failed to modify system "
237 "call return, errno = %d", errno
);
240 else if(WIFSTOPPED(status
) && (WSTOPSIG(status
) == SIGTRAP
))
243 panic("check_ptrace : expected SIGTRAP or "
244 "(SIGTRAP|0x80), got status = %d", status
);
246 if (stop_ptraced_child(pid
, stack
, 0, 0) < 0)
249 sysemu_supported
= 2;
252 if ( !force_sysemu_disabled
)
253 set_using_sysemu(sysemu_supported
);
257 stop_ptraced_child(pid
, stack
, 1, 0);
262 static void __init
check_ptrace(void)
265 int pid
, syscall
, n
, status
;
267 printf("Checking that ptrace can change system call numbers...");
268 pid
= start_ptraced_child(&stack
);
270 if(ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0, (void *)PTRACE_O_TRACESYSGOOD
) < 0)
271 panic("check_ptrace: PTRACE_OLDSETOPTIONS failed, errno = %d", errno
);
274 if(ptrace(PTRACE_SYSCALL
, pid
, 0, 0) < 0)
275 panic("check_ptrace : ptrace failed, errno = %d",
277 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
279 panic("check_ptrace : wait failed, errno = %d", errno
);
280 if(!WIFSTOPPED(status
) || (WSTOPSIG(status
) != (SIGTRAP
|0x80)))
281 panic("check_ptrace : expected (SIGTRAP|0x80), "
282 "got status = %d", status
);
284 syscall
= ptrace(PTRACE_PEEKUSR
, pid
, PT_SYSCALL_NR_OFFSET
,
286 if(syscall
== __NR_getpid
){
287 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_NR_OFFSET
,
290 panic("check_ptrace : failed to modify system "
291 "call, errno = %d", errno
);
295 stop_ptraced_child(pid
, stack
, 0, 1);
300 extern void check_tmpexec(void);
302 void os_early_checks(void)
306 /* Need to check this early because mmapping happens before the
312 static int __init
noprocmm_cmd_param(char *str
, int* add
)
318 __uml_setup("noprocmm", noprocmm_cmd_param
,
320 " Turns off usage of /proc/mm, even if host supports it.\n"
321 " To support /proc/mm, the host needs to be patched using\n"
322 " the current skas3 patch.\n\n");
324 static int __init
noptracefaultinfo_cmd_param(char *str
, int* add
)
326 ptrace_faultinfo
= 0;
330 __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param
,
331 "noptracefaultinfo\n"
332 " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
333 " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
334 " using the current skas3 patch.\n\n");
336 static int __init
noptraceldt_cmd_param(char *str
, int* add
)
342 __uml_setup("noptraceldt", noptraceldt_cmd_param
,
344 " Turns off usage of PTRACE_LDT, even if host supports it.\n"
345 " To support PTRACE_LDT, the host needs to be patched using\n"
346 " the current skas3 patch.\n\n");
348 #ifdef UML_CONFIG_MODE_SKAS
349 static inline void check_skas3_ptrace_faultinfo(void)
351 struct ptrace_faultinfo fi
;
355 printf(" - PTRACE_FAULTINFO...");
356 pid
= start_ptraced_child(&stack
);
358 n
= ptrace(PTRACE_FAULTINFO
, pid
, 0, &fi
);
360 ptrace_faultinfo
= 0;
362 printf("not found\n");
367 if (!ptrace_faultinfo
)
368 printf("found but disabled on command line\n");
374 stop_ptraced_child(pid
, stack
, 1, 1);
377 static inline void check_skas3_ptrace_ldt(void)
382 unsigned char ldtbuf
[40];
383 struct ptrace_ldt ldt_op
= (struct ptrace_ldt
) {
384 .func
= 2, /* read default ldt */
386 .bytecount
= sizeof(ldtbuf
)};
388 printf(" - PTRACE_LDT...");
389 pid
= start_ptraced_child(&stack
);
391 n
= ptrace(PTRACE_LDT
, pid
, 0, (unsigned long) &ldt_op
);
394 printf("not found\n");
404 printf("found, but use is disabled\n");
407 stop_ptraced_child(pid
, stack
, 1, 1);
409 /* PTRACE_LDT might be disabled via cmdline option.
410 * We want to override this, else we might use the stub
417 static inline void check_skas3_proc_mm(void)
419 printf(" - /proc/mm...");
420 if (os_access("/proc/mm", OS_ACC_W_OK
) < 0) {
422 printf("not found\n");
426 printf("found but disabled on command line\n");
432 int can_do_skas(void)
434 printf("Checking for the skas3 patch in the host:\n");
436 check_skas3_proc_mm();
437 check_skas3_ptrace_faultinfo();
438 check_skas3_ptrace_ldt();
440 if(!proc_mm
|| !ptrace_faultinfo
|| !ptrace_ldt
)
446 int can_do_skas(void)
452 int __init
parse_iomem(char *str
, int *add
)
454 struct iomem_region
*new;
460 file
= strchr(str
,',');
462 printf("parse_iomem : failed to parse iomem\n");
467 fd
= os_open_file(file
, of_rdwr(OPENFLAGS()), 0);
469 os_print_error(fd
, "parse_iomem - Couldn't open io file");
473 err
= os_stat_fd(fd
, &buf
);
475 os_print_error(err
, "parse_iomem - cannot stat_fd file");
479 new = malloc(sizeof(*new));
481 perror("Couldn't allocate iomem_region struct");
485 size
= (buf
.ust_size
+ UM_KERN_PAGE_SIZE
) & ~(UM_KERN_PAGE_SIZE
- 1);
487 *new = ((struct iomem_region
) { .next
= iomem_regions
,
494 iomem_size
+= new->size
+ UM_KERN_PAGE_SIZE
;
504 /* Changed during early boot */
505 int pty_output_sigio
= 0;
506 int pty_close_sigio
= 0;
508 /* Used as a flag during SIGIO testing early in boot */
509 static volatile int got_sigio
= 0;
511 static void __init
handler(int sig
)
522 static void openpty_cb(void *arg
)
524 struct openpty_arg
*info
= arg
;
527 if(openpty(&info
->master
, &info
->slave
, NULL
, NULL
, NULL
))
531 static void __init
check_one_sigio(void (*proc
)(int, int))
533 struct sigaction old
, new;
534 struct openpty_arg pty
= { .master
= -1, .slave
= -1 };
535 int master
, slave
, err
;
537 initial_thread_cb(openpty_cb
, &pty
);
539 printk("openpty failed, errno = %d\n", -pty
.err
);
546 if((master
== -1) || (slave
== -1)){
547 printk("openpty failed to allocate a pty\n");
551 /* Not now, but complain so we now where we failed. */
554 panic("check_sigio : __raw failed, errno = %d\n", -err
);
556 err
= os_sigio_async(master
, slave
);
558 panic("tty_fds : sigio_async failed, err = %d\n", -err
);
560 if(sigaction(SIGIO
, NULL
, &old
) < 0)
561 panic("check_sigio : sigaction 1 failed, errno = %d\n", errno
);
563 new.sa_handler
= handler
;
564 if(sigaction(SIGIO
, &new, NULL
) < 0)
565 panic("check_sigio : sigaction 2 failed, errno = %d\n", errno
);
568 (*proc
)(master
, slave
);
573 if(sigaction(SIGIO
, &old
, NULL
) < 0)
574 panic("check_sigio : sigaction 3 failed, errno = %d\n", errno
);
577 static void tty_output(int master
, int slave
)
582 printk("Checking that host ptys support output SIGIO...");
584 memset(buf
, 0, sizeof(buf
));
586 while(os_write_file(master
, buf
, sizeof(buf
)) > 0) ;
588 panic("check_sigio : write failed, errno = %d\n", errno
);
589 while(((n
= os_read_file(slave
, buf
, sizeof(buf
))) > 0) && !got_sigio
) ;
593 pty_output_sigio
= 1;
595 else if(n
== -EAGAIN
) printk("No, enabling workaround\n");
596 else panic("check_sigio : read failed, err = %d\n", n
);
599 static void tty_close(int master
, int slave
)
601 printk("Checking that host ptys support SIGIO on close...");
608 else printk("No, enabling workaround\n");
611 void __init
check_sigio(void)
613 if((os_access("/dev/ptmx", OS_ACC_R_OK
) < 0) &&
614 (os_access("/dev/ptyp0", OS_ACC_R_OK
) < 0)){
615 printk("No pseudo-terminals available - skipping pty SIGIO "
619 check_one_sigio(tty_output
);
620 check_one_sigio(tty_close
);
623 void os_check_bugs(void)