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 void fatal_perror(char *str)
82 static void fatal(char *fmt, ...)
94 static void non_fatal(char *fmt, ...)
104 static int start_ptraced_child(void **stack_out)
110 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
111 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
112 if(stack == MAP_FAILED)
113 fatal_perror("check_ptrace : mmap failed");
114 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
115 pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
117 fatal_perror("start_ptraced_child : clone failed");
118 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
120 fatal_perror("check_ptrace : clone failed");
121 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
122 fatal("check_ptrace : expected SIGSTOP, got status = %d",
129 /* When testing for SYSEMU support, if it is one of the broken versions, we
130 * must just avoid using sysemu, not panic, but only if SYSEMU features are
132 * So only for SYSEMU features we test mustpanic, while normal host features
135 static int stop_ptraced_child(int pid, void *stack, int exitcode,
138 int status, n, ret = 0;
140 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
141 fatal_perror("stop_ptraced_child : ptrace failed");
142 CATCH_EINTR(n = waitpid(pid, &status, 0));
143 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
144 int exit_with = WEXITSTATUS(status);
146 non_fatal("check_ptrace : child exited with status 2. "
147 "Serious trouble happening! Try updating "
148 "your host skas patch!\nDisabling SYSEMU "
150 non_fatal("check_ptrace : child exited with exitcode %d, while "
151 "expecting %d; status 0x%x\n", exit_with,
158 if(munmap(stack, PAGE_SIZE) < 0)
159 fatal_perror("check_ptrace : munmap failed");
163 /* Changed only during early boot */
164 int ptrace_faultinfo = 1;
167 int skas_needs_stub = 0;
169 static int __init skas0_cmd_param(char *str, int* add)
171 ptrace_faultinfo = proc_mm = 0;
175 /* The two __uml_setup would conflict, without this stupid alias. */
177 static int __init mode_skas0_cmd_param(char *str, int* add)
178 __attribute__((alias("skas0_cmd_param")));
180 __uml_setup("skas0", skas0_cmd_param,
182 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
183 " you specify mode=tt.\n\n");
185 __uml_setup("mode=skas0", mode_skas0_cmd_param,
187 " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
188 " specify mode=tt. Note that this was recently added - on \n"
189 " older kernels you must use simply \"skas0\".\n\n");
191 /* Changed only during early boot */
192 static int force_sysemu_disabled = 0;
194 static int __init nosysemu_cmd_param(char *str, int* add)
196 force_sysemu_disabled = 1;
200 __uml_setup("nosysemu", nosysemu_cmd_param,
202 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
203 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
204 " behaviour of ptrace() and helps reducing host context switch rate.\n"
205 " To make it working, you need a kernel patch for your host, too.\n"
206 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
207 " information.\n\n");
209 static void __init check_sysemu(void)
212 int pid, n, status, count=0;
214 non_fatal("Checking syscall emulation patch for ptrace...");
215 sysemu_supported = 0;
216 pid = start_ptraced_child(&stack);
218 if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
221 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
223 fatal_perror("check_sysemu : wait failed");
224 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
225 fatal("check_sysemu : expected SIGTRAP, got status = %d",
228 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
231 fatal_perror("check_sysemu : failed to modify system call "
234 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
237 sysemu_supported = 1;
239 set_using_sysemu(!force_sysemu_disabled);
241 non_fatal("Checking advanced syscall emulation patch for ptrace...");
242 pid = start_ptraced_child(&stack);
244 if((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
245 (void *) PTRACE_O_TRACESYSGOOD) < 0))
246 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
250 if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
252 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
254 fatal_perror("check_ptrace : wait failed");
256 if(WIFSTOPPED(status) && (WSTOPSIG(status) == (SIGTRAP|0x80))){
258 fatal("check_ptrace : SYSEMU_SINGLESTEP "
259 "doesn't singlestep");
260 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
263 fatal_perror("check_sysemu : failed to modify "
264 "system call return");
267 else if(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
270 fatal("check_ptrace : expected SIGTRAP or "
271 "(SIGTRAP | 0x80), got status = %d", status);
273 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
276 sysemu_supported = 2;
279 if ( !force_sysemu_disabled )
280 set_using_sysemu(sysemu_supported);
284 stop_ptraced_child(pid, stack, 1, 0);
286 non_fatal("missing\n");
289 static void __init check_ptrace(void)
292 int pid, syscall, n, status;
294 non_fatal("Checking that ptrace can change system call numbers...");
295 pid = start_ptraced_child(&stack);
297 if((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
298 (void *) PTRACE_O_TRACESYSGOOD) < 0))
299 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
302 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
303 fatal_perror("check_ptrace : ptrace failed");
305 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
307 fatal_perror("check_ptrace : wait failed");
309 if(!WIFSTOPPED(status) ||
310 (WSTOPSIG(status) != (SIGTRAP | 0x80)))
311 fatal("check_ptrace : expected (SIGTRAP|0x80), "
312 "got status = %d", status);
314 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
316 if(syscall == __NR_getpid){
317 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
320 fatal_perror("check_ptrace : failed to modify "
325 stop_ptraced_child(pid, stack, 0, 1);
330 extern void check_tmpexec(void);
332 void os_early_checks(void)
336 /* Need to check this early because mmapping happens before the
342 static int __init noprocmm_cmd_param(char *str, int* add)
348 __uml_setup("noprocmm", noprocmm_cmd_param,
350 " Turns off usage of /proc/mm, even if host supports it.\n"
351 " To support /proc/mm, the host needs to be patched using\n"
352 " the current skas3 patch.\n\n");
354 static int __init noptracefaultinfo_cmd_param(char *str, int* add)
356 ptrace_faultinfo = 0;
360 __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
361 "noptracefaultinfo\n"
362 " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
363 " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
364 " using the current skas3 patch.\n\n");
366 static int __init noptraceldt_cmd_param(char *str, int* add)
372 __uml_setup("noptraceldt", noptraceldt_cmd_param,
374 " Turns off usage of PTRACE_LDT, even if host supports it.\n"
375 " To support PTRACE_LDT, the host needs to be patched using\n"
376 " the current skas3 patch.\n\n");
378 #ifdef UML_CONFIG_MODE_SKAS
379 static inline void check_skas3_ptrace_faultinfo(void)
381 struct ptrace_faultinfo fi;
385 non_fatal(" - PTRACE_FAULTINFO...");
386 pid = start_ptraced_child(&stack);
388 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
390 ptrace_faultinfo = 0;
392 non_fatal("not found\n");
397 if (!ptrace_faultinfo)
398 non_fatal("found but disabled on command line\n");
400 non_fatal("found\n");
404 stop_ptraced_child(pid, stack, 1, 1);
407 static inline void check_skas3_ptrace_ldt(void)
412 unsigned char ldtbuf[40];
413 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
414 .func = 2, /* read default ldt */
416 .bytecount = sizeof(ldtbuf)};
418 non_fatal(" - PTRACE_LDT...");
419 pid = start_ptraced_child(&stack);
421 n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
424 non_fatal("not found\n");
432 non_fatal("found\n");
434 non_fatal("found, but use is disabled\n");
437 stop_ptraced_child(pid, stack, 1, 1);
439 /* PTRACE_LDT might be disabled via cmdline option.
440 * We want to override this, else we might use the stub
447 static inline void check_skas3_proc_mm(void)
449 non_fatal(" - /proc/mm...");
450 if (access("/proc/mm", W_OK) < 0) {
456 non_fatal("found but disabled on command line\n");
458 non_fatal("found\n");
462 int can_do_skas(void)
464 non_fatal("Checking for the skas3 patch in the host:\n");
466 check_skas3_proc_mm();
467 check_skas3_ptrace_faultinfo();
468 check_skas3_ptrace_ldt();
470 if(!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
476 int can_do_skas(void)
482 int __init parse_iomem(char *str, int *add)
484 struct iomem_region *new;
490 file = strchr(str,',');
492 printf("parse_iomem : failed to parse iomem\n");
497 fd = open(file, O_RDWR, 0);
499 os_print_error(fd, "parse_iomem - Couldn't open io file");
503 if(fstat64(fd, &buf) < 0){
504 perror("parse_iomem - cannot stat_fd file");
508 new = malloc(sizeof(*new));
510 perror("Couldn't allocate iomem_region struct");
514 size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
516 *new = ((struct iomem_region) { .next = iomem_regions,
523 iomem_size += new->size + UM_KERN_PAGE_SIZE;
533 /* Changed during early boot */
534 int pty_output_sigio = 0;
535 int pty_close_sigio = 0;
537 /* Used as a flag during SIGIO testing early in boot */
538 static volatile int got_sigio = 0;
540 static void __init handler(int sig)
551 static void openpty_cb(void *arg)
553 struct openpty_arg *info = arg;
556 if(openpty(&info->master, &info->slave, NULL, NULL, NULL))
560 static int async_pty(int master, int slave)
564 flags = fcntl(master, F_GETFL);
568 if((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
569 (fcntl(master, F_SETOWN, os_getpid()) < 0))
572 if((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0))
578 static void __init check_one_sigio(void (*proc)(int, int))
580 struct sigaction old, new;
581 struct openpty_arg pty = { .master = -1, .slave = -1 };
582 int master, slave, err;
584 initial_thread_cb(openpty_cb, &pty);
586 printk("openpty failed, errno = %d\n", -pty.err);
593 if((master == -1) || (slave == -1)){
594 printk("openpty failed to allocate a pty\n");
598 /* Not now, but complain so we now where we failed. */
601 panic("check_sigio : __raw failed, errno = %d\n", -err);
603 err = async_pty(master, slave);
605 panic("tty_fds : sigio_async failed, err = %d\n", -err);
607 if(sigaction(SIGIO, NULL, &old) < 0)
608 panic("check_sigio : sigaction 1 failed, errno = %d\n", errno);
610 new.sa_handler = handler;
611 if(sigaction(SIGIO, &new, NULL) < 0)
612 panic("check_sigio : sigaction 2 failed, errno = %d\n", errno);
615 (*proc)(master, slave);
620 if(sigaction(SIGIO, &old, NULL) < 0)
621 panic("check_sigio : sigaction 3 failed, errno = %d\n", errno);
624 static void tty_output(int master, int slave)
629 printk("Checking that host ptys support output SIGIO...");
631 memset(buf, 0, sizeof(buf));
633 while(os_write_file(master, buf, sizeof(buf)) > 0) ;
635 panic("check_sigio : write failed, errno = %d\n", errno);
636 while(((n = os_read_file(slave, buf, sizeof(buf))) > 0) && !got_sigio) ;
640 pty_output_sigio = 1;
642 else if(n == -EAGAIN) printk("No, enabling workaround\n");
643 else panic("check_sigio : read failed, err = %d\n", n);
646 static void tty_close(int master, int slave)
648 printk("Checking that host ptys support SIGIO on close...");
655 else printk("No, enabling workaround\n");
658 void __init check_sigio(void)
660 if((os_access("/dev/ptmx", OS_ACC_R_OK) < 0) &&
661 (os_access("/dev/ptyp0", OS_ACC_R_OK) < 0)){
662 printk("No pseudo-terminals available - skipping pty SIGIO "
666 check_one_sigio(tty_output);
667 check_one_sigio(tty_close);
670 void os_check_bugs(void)