2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
16 #include <sys/ptrace.h>
19 #include <asm/unistd.h>
23 #include "ptrace_user.h"
24 #include "registers.h"
26 #include "skas_ptrace.h"
28 static void ptrace_child(void)
31 /* Calling os_getpid because some libcs cached getpid incorrectly */
32 int pid = os_getpid(), ppid = getppid();
35 if (change_sig(SIGWINCH, 0) < 0 ||
36 ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
43 * This syscall will be intercepted by the parent. Don't call more than
46 sc_result = os_getpid();
49 /* Nothing modified by the parent, we are running normally. */
51 else if (sc_result == ppid)
53 * Expected in check_ptrace and check_sysemu when they succeed
54 * in modifying the stack frame
58 /* Serious trouble! This could be caused by a bug in host 2.6
59 * SKAS3/2.6 patch before release -V6, together with a bug in
60 * the UML code itself.
67 static void fatal_perror(const char *str)
73 static void fatal(char *fmt, ...)
78 vfprintf(stderr, fmt, list);
84 static void non_fatal(char *fmt, ...)
89 vfprintf(stderr, fmt, list);
93 static int start_ptraced_child(void)
101 fatal_perror("start_ptraced_child : fork failed");
103 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
105 fatal_perror("check_ptrace : waitpid failed");
106 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
107 fatal("check_ptrace : expected SIGSTOP, got status = %d",
113 /* When testing for SYSEMU support, if it is one of the broken versions, we
114 * must just avoid using sysemu, not panic, but only if SYSEMU features are
116 * So only for SYSEMU features we test mustpanic, while normal host features
119 static int stop_ptraced_child(int pid, int exitcode, int mustexit)
121 int status, n, ret = 0;
123 if (ptrace(PTRACE_CONT, pid, 0, 0) < 0) {
124 perror("stop_ptraced_child : ptrace failed");
127 CATCH_EINTR(n = waitpid(pid, &status, 0));
128 if (!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
129 int exit_with = WEXITSTATUS(status);
131 non_fatal("check_ptrace : child exited with status 2. "
132 "\nDisabling SYSEMU support.\n");
133 non_fatal("check_ptrace : child exited with exitcode %d, while "
134 "expecting %d; status 0x%x\n", exit_with,
144 /* Changed only during early boot */
145 int ptrace_faultinfo;
146 static int disable_ptrace_faultinfo;
149 static int disable_ptrace_ldt;
152 static int disable_proc_mm;
155 static int disable_switch_mm;
159 static int __init skas0_cmd_param(char *str, int* add)
161 disable_ptrace_faultinfo = 1;
162 disable_ptrace_ldt = 1;
164 disable_switch_mm = 1;
169 /* The two __uml_setup would conflict, without this stupid alias. */
171 static int __init mode_skas0_cmd_param(char *str, int* add)
172 __attribute__((alias("skas0_cmd_param")));
174 __uml_setup("skas0", skas0_cmd_param,
176 " Disables SKAS3 and SKAS4 usage, so that SKAS0 is used\n\n");
178 __uml_setup("mode=skas0", mode_skas0_cmd_param,
180 " Disables SKAS3 and SKAS4 usage, so that SKAS0 is used.\n\n");
182 /* Changed only during early boot */
183 static int force_sysemu_disabled = 0;
185 static int __init nosysemu_cmd_param(char *str, int* add)
187 force_sysemu_disabled = 1;
191 __uml_setup("nosysemu", nosysemu_cmd_param,
193 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
194 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
195 " behaviour of ptrace() and helps reducing host context switch rate.\n"
196 " To make it working, you need a kernel patch for your host, too.\n"
197 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
198 " information.\n\n");
200 static void __init check_sysemu(void)
202 unsigned long regs[MAX_REG_NR];
203 int pid, n, status, count=0;
205 non_fatal("Checking syscall emulation patch for ptrace...");
206 sysemu_supported = 0;
207 pid = start_ptraced_child();
209 if (ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
212 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
214 fatal_perror("check_sysemu : wait failed");
215 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
216 fatal("check_sysemu : expected SIGTRAP, got status = %d\n",
219 if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
220 fatal_perror("check_sysemu : PTRACE_GETREGS failed");
221 if (PT_SYSCALL_NR(regs) != __NR_getpid) {
222 non_fatal("check_sysemu got system call number %d, "
223 "expected %d...", PT_SYSCALL_NR(regs), __NR_getpid);
227 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET, os_getpid());
229 non_fatal("check_sysemu : failed to modify system call "
234 if (stop_ptraced_child(pid, 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();
244 if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
245 (void *) PTRACE_O_TRACESYSGOOD) < 0))
246 fatal_perror("check_sysemu: 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_sysemu: wait failed");
256 if (WIFSTOPPED(status) &&
257 (WSTOPSIG(status) == (SIGTRAP|0x80))) {
259 non_fatal("check_sysemu: SYSEMU_SINGLESTEP "
260 "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 non_fatal("check_sysemu: expected SIGTRAP or "
274 "(SIGTRAP | 0x80), got status = %d\n",
279 if (stop_ptraced_child(pid, 0, 0) < 0)
282 sysemu_supported = 2;
285 if (!force_sysemu_disabled)
286 set_using_sysemu(sysemu_supported);
290 stop_ptraced_child(pid, 1, 0);
292 non_fatal("missing\n");
295 static void __init check_ptrace(void)
297 int pid, syscall, n, status;
299 non_fatal("Checking that ptrace can change system call numbers...");
300 pid = start_ptraced_child();
302 if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
303 (void *) PTRACE_O_TRACESYSGOOD) < 0))
304 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
307 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
308 fatal_perror("check_ptrace : ptrace failed");
310 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
312 fatal_perror("check_ptrace : wait failed");
314 if (!WIFSTOPPED(status) ||
315 (WSTOPSIG(status) != (SIGTRAP | 0x80)))
316 fatal("check_ptrace : expected (SIGTRAP|0x80), "
317 "got status = %d", status);
319 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
321 if (syscall == __NR_getpid) {
322 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
325 fatal_perror("check_ptrace : failed to modify "
330 stop_ptraced_child(pid, 0, 1);
335 extern void check_tmpexec(void);
337 static void __init check_coredump_limit(void)
340 int err = getrlimit(RLIMIT_CORE, &lim);
343 perror("Getting core dump limit");
347 printf("Core dump limits :\n\tsoft - ");
348 if (lim.rlim_cur == RLIM_INFINITY)
350 else printf("%lu\n", lim.rlim_cur);
353 if (lim.rlim_max == RLIM_INFINITY)
355 else printf("%lu\n", lim.rlim_max);
358 void __init os_early_checks(void)
362 /* Print out the core dump limits early */
363 check_coredump_limit();
367 /* Need to check this early because mmapping happens before the
372 pid = start_ptraced_child();
373 if (init_registers(pid))
374 fatal("Failed to initialize default registers");
375 stop_ptraced_child(pid, 1, 1);
378 static int __init noprocmm_cmd_param(char *str, int* add)
384 __uml_setup("noprocmm", noprocmm_cmd_param,
386 " Turns off usage of /proc/mm, even if host supports it.\n"
387 " To support /proc/mm, the host needs to be patched using\n"
388 " the current skas3 patch.\n\n");
390 static int __init noptracefaultinfo_cmd_param(char *str, int* add)
392 disable_ptrace_faultinfo = 1;
396 __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
397 "noptracefaultinfo\n"
398 " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
399 " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
400 " using the current skas3 patch.\n\n");
402 static int __init noptraceldt_cmd_param(char *str, int* add)
404 disable_ptrace_ldt = 1;
408 __uml_setup("noptraceldt", noptraceldt_cmd_param,
410 " Turns off usage of PTRACE_LDT, even if host supports it.\n"
411 " To support PTRACE_LDT, the host needs to be patched using\n"
412 " the current skas3 patch.\n\n");
414 static inline void check_skas3_ptrace_faultinfo(void)
416 struct ptrace_faultinfo fi;
419 non_fatal(" - PTRACE_FAULTINFO...");
420 pid = start_ptraced_child();
422 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
425 non_fatal("not found\n");
428 } else if (disable_ptrace_faultinfo)
429 non_fatal("found but disabled on command line\n");
431 ptrace_faultinfo = 1;
432 non_fatal("found\n");
435 stop_ptraced_child(pid, 1, 1);
438 static inline void check_skas3_ptrace_ldt(void)
442 unsigned char ldtbuf[40];
443 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
444 .func = 2, /* read default ldt */
446 .bytecount = sizeof(ldtbuf)};
448 non_fatal(" - PTRACE_LDT...");
449 pid = start_ptraced_child();
451 n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
454 non_fatal("not found\n");
457 } else if (disable_ptrace_ldt)
458 non_fatal("found, but use is disabled\n");
461 non_fatal("found\n");
464 stop_ptraced_child(pid, 1, 1);
468 static inline void check_skas3_proc_mm(void)
470 non_fatal(" - /proc/mm...");
471 if (access("/proc/mm", W_OK) < 0)
473 else if (disable_proc_mm)
474 non_fatal("found but disabled on command line\n");
477 non_fatal("found\n");
481 void can_do_skas(void)
483 non_fatal("Checking for the skas3 patch in the host:\n");
485 check_skas3_proc_mm();
486 check_skas3_ptrace_faultinfo();
487 check_skas3_ptrace_ldt();
489 if (!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
493 int __init parse_iomem(char *str, int *add)
495 struct iomem_region *new;
501 file = strchr(str,',');
503 fprintf(stderr, "parse_iomem : failed to parse iomem\n");
508 fd = open(file, O_RDWR, 0);
510 perror("parse_iomem - Couldn't open io file");
514 if (fstat64(fd, &buf) < 0) {
515 perror("parse_iomem - cannot stat_fd file");
519 new = malloc(sizeof(*new));
521 perror("Couldn't allocate iomem_region struct");
525 size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
527 *new = ((struct iomem_region) { .next = iomem_regions,
534 iomem_size += new->size + UM_KERN_PAGE_SIZE;