2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
20 #include <sys/resource.h>
21 #include <asm/unistd.h>
22 #include <sys/types.h>
23 #include "kern_util.h"
25 #include "signal_kern.h"
26 #include "sysdep/ptrace.h"
27 #include "sysdep/sigcontext.h"
29 #include "ptrace_user.h"
33 #include "uml-config.h"
35 #include "kern_constants.h"
37 #include "skas_ptrace.h"
38 #include "registers.h"
40 static int ptrace_child(void *arg)
43 int pid = os_getpid(), ppid = getppid();
46 change_sig(SIGWINCH, 0);
47 if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0){
49 os_kill_process(pid, 0);
53 /*This syscall will be intercepted by the parent. Don't call more than
55 sc_result = os_getpid();
58 ret = 1; /*Nothing modified by the parent, we are running
60 else if (sc_result == ppid)
61 ret = 0; /*Expected in check_ptrace and check_sysemu when they
62 succeed in modifying the stack frame*/
64 ret = 2; /*Serious trouble! This could be caused by a bug in
65 host 2.6 SKAS3/2.6 patch before release -V6, together
66 with a bug in the UML code itself.*/
70 static void fatal_perror(char *str)
76 static void fatal(char *fmt, ...)
88 static void non_fatal(char *fmt, ...)
98 static int start_ptraced_child(void **stack_out)
104 stack = mmap(NULL, UM_KERN_PAGE_SIZE,
105 PROT_READ | PROT_WRITE | PROT_EXEC,
106 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
107 if(stack == MAP_FAILED)
108 fatal_perror("check_ptrace : mmap failed");
109 sp = (unsigned long) stack + UM_KERN_PAGE_SIZE - sizeof(void *);
110 pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
112 fatal_perror("start_ptraced_child : clone failed");
113 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
115 fatal_perror("check_ptrace : clone failed");
116 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
117 fatal("check_ptrace : expected SIGSTOP, got status = %d",
124 /* When testing for SYSEMU support, if it is one of the broken versions, we
125 * must just avoid using sysemu, not panic, but only if SYSEMU features are
127 * So only for SYSEMU features we test mustpanic, while normal host features
130 static int stop_ptraced_child(int pid, void *stack, int exitcode,
133 int status, n, ret = 0;
135 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
136 fatal_perror("stop_ptraced_child : ptrace failed");
137 CATCH_EINTR(n = waitpid(pid, &status, 0));
138 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
139 int exit_with = WEXITSTATUS(status);
141 non_fatal("check_ptrace : child exited with status 2. "
142 "\nDisabling SYSEMU support.\n");
143 non_fatal("check_ptrace : child exited with exitcode %d, while "
144 "expecting %d; status 0x%x\n", exit_with,
151 if(munmap(stack, UM_KERN_PAGE_SIZE) < 0)
152 fatal_perror("check_ptrace : munmap failed");
156 /* Changed only during early boot */
157 int ptrace_faultinfo = 1;
160 int skas_needs_stub = 0;
162 static int __init skas0_cmd_param(char *str, int* add)
164 ptrace_faultinfo = proc_mm = 0;
168 /* The two __uml_setup would conflict, without this stupid alias. */
170 static int __init mode_skas0_cmd_param(char *str, int* add)
171 __attribute__((alias("skas0_cmd_param")));
173 __uml_setup("skas0", skas0_cmd_param,
175 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
176 " you specify mode=tt.\n\n");
178 __uml_setup("mode=skas0", mode_skas0_cmd_param,
180 " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
181 " specify mode=tt. Note that this was recently added - on \n"
182 " older kernels you must use simply \"skas0\".\n\n");
184 /* Changed only during early boot */
185 static int force_sysemu_disabled = 0;
187 static int __init nosysemu_cmd_param(char *str, int* add)
189 force_sysemu_disabled = 1;
193 __uml_setup("nosysemu", nosysemu_cmd_param,
195 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
196 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
197 " behaviour of ptrace() and helps reducing host context switch rate.\n"
198 " To make it working, you need a kernel patch for your host, too.\n"
199 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
200 " information.\n\n");
202 static void __init check_sysemu(void)
205 unsigned long regs[MAX_REG_NR];
206 int pid, n, status, count=0;
208 non_fatal("Checking syscall emulation patch for ptrace...");
209 sysemu_supported = 0;
210 pid = start_ptraced_child(&stack);
212 if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
215 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
217 fatal_perror("check_sysemu : wait failed");
218 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
219 fatal("check_sysemu : expected SIGTRAP, got status = %d",
222 if(ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
223 fatal_perror("check_sysemu : PTRACE_GETREGS failed");
224 if(PT_SYSCALL_NR(regs) != __NR_getpid){
225 non_fatal("check_sysemu got system call number %d, "
226 "expected %d...", PT_SYSCALL_NR(regs), __NR_getpid);
230 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET, os_getpid());
232 non_fatal("check_sysemu : failed to modify system call "
237 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
240 sysemu_supported = 1;
242 set_using_sysemu(!force_sysemu_disabled);
244 non_fatal("Checking advanced syscall emulation patch for ptrace...");
245 pid = start_ptraced_child(&stack);
247 if((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
248 (void *) PTRACE_O_TRACESYSGOOD) < 0))
249 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
253 if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
255 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
257 fatal_perror("check_ptrace : wait failed");
259 if(WIFSTOPPED(status) && (WSTOPSIG(status) == (SIGTRAP|0x80))){
261 fatal("check_ptrace : SYSEMU_SINGLESTEP "
262 "doesn't singlestep");
263 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
266 fatal_perror("check_sysemu : failed to modify "
267 "system call return");
270 else if(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
273 fatal("check_ptrace : expected SIGTRAP or "
274 "(SIGTRAP | 0x80), got status = %d", status);
276 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
279 sysemu_supported = 2;
282 if ( !force_sysemu_disabled )
283 set_using_sysemu(sysemu_supported);
287 stop_ptraced_child(pid, stack, 1, 0);
289 non_fatal("missing\n");
292 static void __init check_ptrace(void)
295 int pid, syscall, n, status;
297 non_fatal("Checking that ptrace can change system call numbers...");
298 pid = start_ptraced_child(&stack);
300 if((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
301 (void *) PTRACE_O_TRACESYSGOOD) < 0))
302 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
305 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
306 fatal_perror("check_ptrace : ptrace failed");
308 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
310 fatal_perror("check_ptrace : wait failed");
312 if(!WIFSTOPPED(status) ||
313 (WSTOPSIG(status) != (SIGTRAP | 0x80)))
314 fatal("check_ptrace : expected (SIGTRAP|0x80), "
315 "got status = %d", status);
317 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
319 if(syscall == __NR_getpid){
320 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
323 fatal_perror("check_ptrace : failed to modify "
328 stop_ptraced_child(pid, stack, 0, 1);
333 extern void check_tmpexec(void);
335 static void __init check_coredump_limit(void)
338 int err = getrlimit(RLIMIT_CORE, &lim);
341 perror("Getting core dump limit");
345 printf("Core dump limits :\n\tsoft - ");
346 if(lim.rlim_cur == RLIM_INFINITY)
348 else printf("%lu\n", lim.rlim_cur);
351 if(lim.rlim_max == RLIM_INFINITY)
353 else printf("%lu\n", lim.rlim_max);
356 void __init os_early_checks(void)
358 /* Print out the core dump limits early */
359 check_coredump_limit();
363 /* Need to check this early because mmapping happens before the
369 static int __init noprocmm_cmd_param(char *str, int* add)
375 __uml_setup("noprocmm", noprocmm_cmd_param,
377 " Turns off usage of /proc/mm, even if host supports it.\n"
378 " To support /proc/mm, the host needs to be patched using\n"
379 " the current skas3 patch.\n\n");
381 static int __init noptracefaultinfo_cmd_param(char *str, int* add)
383 ptrace_faultinfo = 0;
387 __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
388 "noptracefaultinfo\n"
389 " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
390 " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
391 " using the current skas3 patch.\n\n");
393 static int __init noptraceldt_cmd_param(char *str, int* add)
399 __uml_setup("noptraceldt", noptraceldt_cmd_param,
401 " Turns off usage of PTRACE_LDT, even if host supports it.\n"
402 " To support PTRACE_LDT, the host needs to be patched using\n"
403 " the current skas3 patch.\n\n");
405 static inline void check_skas3_ptrace_faultinfo(void)
407 struct ptrace_faultinfo fi;
411 non_fatal(" - PTRACE_FAULTINFO...");
412 pid = start_ptraced_child(&stack);
414 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
416 ptrace_faultinfo = 0;
418 non_fatal("not found\n");
423 if (!ptrace_faultinfo)
424 non_fatal("found but disabled on command line\n");
426 non_fatal("found\n");
430 stop_ptraced_child(pid, stack, 1, 1);
433 static inline void check_skas3_ptrace_ldt(void)
438 unsigned char ldtbuf[40];
439 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
440 .func = 2, /* read default ldt */
442 .bytecount = sizeof(ldtbuf)};
444 non_fatal(" - PTRACE_LDT...");
445 pid = start_ptraced_child(&stack);
447 n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
450 non_fatal("not found\n");
458 non_fatal("found\n");
460 non_fatal("found, but use is disabled\n");
463 stop_ptraced_child(pid, stack, 1, 1);
465 /* PTRACE_LDT might be disabled via cmdline option.
466 * We want to override this, else we might use the stub
473 static inline void check_skas3_proc_mm(void)
475 non_fatal(" - /proc/mm...");
476 if (access("/proc/mm", W_OK) < 0) {
482 non_fatal("found but disabled on command line\n");
484 non_fatal("found\n");
488 int can_do_skas(void)
490 non_fatal("Checking for the skas3 patch in the host:\n");
492 check_skas3_proc_mm();
493 check_skas3_ptrace_faultinfo();
494 check_skas3_ptrace_ldt();
496 if(!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
502 int __init parse_iomem(char *str, int *add)
504 struct iomem_region *new;
510 file = strchr(str,',');
512 printf("parse_iomem : failed to parse iomem\n");
517 fd = open(file, O_RDWR, 0);
519 os_print_error(fd, "parse_iomem - Couldn't open io file");
523 if(fstat64(fd, &buf) < 0){
524 perror("parse_iomem - cannot stat_fd file");
528 new = malloc(sizeof(*new));
530 perror("Couldn't allocate iomem_region struct");
534 size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
536 *new = ((struct iomem_region) { .next = iomem_regions,
543 iomem_size += new->size + UM_KERN_PAGE_SIZE;