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>
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 void fatal_perror(char *str)
83 static void fatal(char *fmt, ...)
95 static void non_fatal(char *fmt, ...)
105 static int start_ptraced_child(void **stack_out)
111 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
112 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
113 if(stack == MAP_FAILED)
114 fatal_perror("check_ptrace : mmap failed");
115 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
116 pid = clone(ptrace_child, (void *) sp, SIGCHLD, NULL);
118 fatal_perror("start_ptraced_child : clone failed");
119 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
121 fatal_perror("check_ptrace : clone failed");
122 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
123 fatal("check_ptrace : expected SIGSTOP, got status = %d",
130 /* When testing for SYSEMU support, if it is one of the broken versions, we
131 * must just avoid using sysemu, not panic, but only if SYSEMU features are
133 * So only for SYSEMU features we test mustpanic, while normal host features
136 static int stop_ptraced_child(int pid, void *stack, int exitcode,
139 int status, n, ret = 0;
141 if(ptrace(PTRACE_CONT, pid, 0, 0) < 0)
142 fatal_perror("stop_ptraced_child : ptrace failed");
143 CATCH_EINTR(n = waitpid(pid, &status, 0));
144 if(!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
145 int exit_with = WEXITSTATUS(status);
147 non_fatal("check_ptrace : child exited with status 2. "
148 "Serious trouble happening! Try updating "
149 "your host skas patch!\nDisabling SYSEMU "
151 non_fatal("check_ptrace : child exited with exitcode %d, while "
152 "expecting %d; status 0x%x\n", exit_with,
159 if(munmap(stack, PAGE_SIZE) < 0)
160 fatal_perror("check_ptrace : munmap failed");
164 /* Changed only during early boot */
165 int ptrace_faultinfo = 1;
168 int skas_needs_stub = 0;
170 static int __init skas0_cmd_param(char *str, int* add)
172 ptrace_faultinfo = proc_mm = 0;
176 /* The two __uml_setup would conflict, without this stupid alias. */
178 static int __init mode_skas0_cmd_param(char *str, int* add)
179 __attribute__((alias("skas0_cmd_param")));
181 __uml_setup("skas0", skas0_cmd_param,
183 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
184 " you specify mode=tt.\n\n");
186 __uml_setup("mode=skas0", mode_skas0_cmd_param,
188 " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
189 " specify mode=tt. Note that this was recently added - on \n"
190 " older kernels you must use simply \"skas0\".\n\n");
192 /* Changed only during early boot */
193 static int force_sysemu_disabled = 0;
195 static int __init nosysemu_cmd_param(char *str, int* add)
197 force_sysemu_disabled = 1;
201 __uml_setup("nosysemu", nosysemu_cmd_param,
203 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
204 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
205 " behaviour of ptrace() and helps reducing host context switch rate.\n"
206 " To make it working, you need a kernel patch for your host, too.\n"
207 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
208 " information.\n\n");
210 static void __init check_sysemu(void)
213 int pid, n, status, count=0;
215 non_fatal("Checking syscall emulation patch for ptrace...");
216 sysemu_supported = 0;
217 pid = start_ptraced_child(&stack);
219 if(ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
222 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
224 fatal_perror("check_sysemu : wait failed");
225 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
226 fatal("check_sysemu : expected SIGTRAP, got status = %d",
229 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
232 fatal_perror("check_sysemu : failed to modify system call "
235 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
238 sysemu_supported = 1;
240 set_using_sysemu(!force_sysemu_disabled);
242 non_fatal("Checking advanced syscall emulation patch for ptrace...");
243 pid = start_ptraced_child(&stack);
245 if((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
246 (void *) PTRACE_O_TRACESYSGOOD) < 0))
247 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
251 if(ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
253 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
255 fatal_perror("check_ptrace : wait failed");
257 if(WIFSTOPPED(status) && (WSTOPSIG(status) == (SIGTRAP|0x80))){
259 fatal("check_ptrace : SYSEMU_SINGLESTEP "
260 "doesn't singlestep");
261 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
264 fatal_perror("check_sysemu : failed to modify "
265 "system call return");
268 else if(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
271 fatal("check_ptrace : expected SIGTRAP or "
272 "(SIGTRAP | 0x80), got status = %d", status);
274 if (stop_ptraced_child(pid, stack, 0, 0) < 0)
277 sysemu_supported = 2;
280 if ( !force_sysemu_disabled )
281 set_using_sysemu(sysemu_supported);
285 stop_ptraced_child(pid, stack, 1, 0);
287 non_fatal("missing\n");
290 static void __init check_ptrace(void)
293 int pid, syscall, n, status;
295 non_fatal("Checking that ptrace can change system call numbers...");
296 pid = start_ptraced_child(&stack);
298 if((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
299 (void *) PTRACE_O_TRACESYSGOOD) < 0))
300 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
303 if(ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
304 fatal_perror("check_ptrace : ptrace failed");
306 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
308 fatal_perror("check_ptrace : wait failed");
310 if(!WIFSTOPPED(status) ||
311 (WSTOPSIG(status) != (SIGTRAP | 0x80)))
312 fatal("check_ptrace : expected (SIGTRAP|0x80), "
313 "got status = %d", status);
315 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
317 if(syscall == __NR_getpid){
318 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
321 fatal_perror("check_ptrace : failed to modify "
326 stop_ptraced_child(pid, stack, 0, 1);
331 extern void check_tmpexec(void);
333 static void check_coredump_limit(void)
336 int err = getrlimit(RLIMIT_CORE, &lim);
339 perror("Getting core dump limit");
343 printf("Core dump limits :\n\tsoft - ");
344 if(lim.rlim_cur == RLIM_INFINITY)
346 else printf("%lu\n", lim.rlim_cur);
349 if(lim.rlim_max == RLIM_INFINITY)
351 else printf("%lu\n", lim.rlim_max);
354 void os_early_checks(void)
356 /* Print out the core dump limits early */
357 check_coredump_limit();
361 /* Need to check this early because mmapping happens before the
367 static int __init noprocmm_cmd_param(char *str, int* add)
373 __uml_setup("noprocmm", noprocmm_cmd_param,
375 " Turns off usage of /proc/mm, even if host supports it.\n"
376 " To support /proc/mm, the host needs to be patched using\n"
377 " the current skas3 patch.\n\n");
379 static int __init noptracefaultinfo_cmd_param(char *str, int* add)
381 ptrace_faultinfo = 0;
385 __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
386 "noptracefaultinfo\n"
387 " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
388 " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
389 " using the current skas3 patch.\n\n");
391 static int __init noptraceldt_cmd_param(char *str, int* add)
397 __uml_setup("noptraceldt", noptraceldt_cmd_param,
399 " Turns off usage of PTRACE_LDT, even if host supports it.\n"
400 " To support PTRACE_LDT, the host needs to be patched using\n"
401 " the current skas3 patch.\n\n");
403 #ifdef UML_CONFIG_MODE_SKAS
404 static inline void check_skas3_ptrace_faultinfo(void)
406 struct ptrace_faultinfo fi;
410 non_fatal(" - PTRACE_FAULTINFO...");
411 pid = start_ptraced_child(&stack);
413 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
415 ptrace_faultinfo = 0;
417 non_fatal("not found\n");
422 if (!ptrace_faultinfo)
423 non_fatal("found but disabled on command line\n");
425 non_fatal("found\n");
429 stop_ptraced_child(pid, stack, 1, 1);
432 static inline void check_skas3_ptrace_ldt(void)
437 unsigned char ldtbuf[40];
438 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
439 .func = 2, /* read default ldt */
441 .bytecount = sizeof(ldtbuf)};
443 non_fatal(" - PTRACE_LDT...");
444 pid = start_ptraced_child(&stack);
446 n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
449 non_fatal("not found\n");
457 non_fatal("found\n");
459 non_fatal("found, but use is disabled\n");
462 stop_ptraced_child(pid, stack, 1, 1);
464 /* PTRACE_LDT might be disabled via cmdline option.
465 * We want to override this, else we might use the stub
472 static inline void check_skas3_proc_mm(void)
474 non_fatal(" - /proc/mm...");
475 if (access("/proc/mm", W_OK) < 0) {
481 non_fatal("found but disabled on command line\n");
483 non_fatal("found\n");
487 int can_do_skas(void)
489 non_fatal("Checking for the skas3 patch in the host:\n");
491 check_skas3_proc_mm();
492 check_skas3_ptrace_faultinfo();
493 check_skas3_ptrace_ldt();
495 if(!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
501 int can_do_skas(void)
507 int __init parse_iomem(char *str, int *add)
509 struct iomem_region *new;
515 file = strchr(str,',');
517 printf("parse_iomem : failed to parse iomem\n");
522 fd = open(file, O_RDWR, 0);
524 os_print_error(fd, "parse_iomem - Couldn't open io file");
528 if(fstat64(fd, &buf) < 0){
529 perror("parse_iomem - cannot stat_fd file");
533 new = malloc(sizeof(*new));
535 perror("Couldn't allocate iomem_region struct");
539 size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
541 *new = ((struct iomem_region) { .next = iomem_regions,
548 iomem_size += new->size + UM_KERN_PAGE_SIZE;