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>
21 #include "kern_constants.h"
24 #include "ptrace_user.h"
25 #include "registers.h"
26 #include "skas_ptrace.h"
28 static int ptrace_child(void)
31 /* Calling os_getpid because some libcs cached getpid incorrectly */
32 int pid
= os_getpid(), ppid
= getppid();
35 change_sig(SIGWINCH
, 0);
36 if (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
, ...)
85 static void non_fatal(char *fmt
, ...)
95 static int start_ptraced_child(void)
103 fatal_perror("start_ptraced_child : fork failed");
105 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
107 fatal_perror("check_ptrace : waitpid failed");
108 if (!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGSTOP
))
109 fatal("check_ptrace : expected SIGSTOP, got status = %d",
115 /* When testing for SYSEMU support, if it is one of the broken versions, we
116 * must just avoid using sysemu, not panic, but only if SYSEMU features are
118 * So only for SYSEMU features we test mustpanic, while normal host features
121 static int stop_ptraced_child(int pid
, int exitcode
, int mustexit
)
123 int status
, n
, ret
= 0;
125 if (ptrace(PTRACE_CONT
, pid
, 0, 0) < 0)
126 fatal_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
= 1;
148 int skas_needs_stub
= 0;
150 static int __init
skas0_cmd_param(char *str
, int* add
)
152 ptrace_faultinfo
= proc_mm
= 0;
156 /* The two __uml_setup would conflict, without this stupid alias. */
158 static int __init
mode_skas0_cmd_param(char *str
, int* add
)
159 __attribute__((alias("skas0_cmd_param")));
161 __uml_setup("skas0", skas0_cmd_param
,
163 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
164 " you specify mode=tt.\n\n");
166 __uml_setup("mode=skas0", mode_skas0_cmd_param
,
168 " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
169 " specify mode=tt. Note that this was recently added - on \n"
170 " older kernels you must use simply \"skas0\".\n\n");
172 /* Changed only during early boot */
173 static int force_sysemu_disabled
= 0;
175 static int __init
nosysemu_cmd_param(char *str
, int* add
)
177 force_sysemu_disabled
= 1;
181 __uml_setup("nosysemu", nosysemu_cmd_param
,
183 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
184 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
185 " behaviour of ptrace() and helps reducing host context switch rate.\n"
186 " To make it working, you need a kernel patch for your host, too.\n"
187 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
188 " information.\n\n");
190 static void __init
check_sysemu(void)
192 unsigned long regs
[MAX_REG_NR
];
193 int pid
, n
, status
, count
=0;
195 non_fatal("Checking syscall emulation patch for ptrace...");
196 sysemu_supported
= 0;
197 pid
= start_ptraced_child();
199 if (ptrace(PTRACE_SYSEMU
, pid
, 0, 0) < 0)
202 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
204 fatal_perror("check_sysemu : wait failed");
205 if (!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGTRAP
))
206 fatal("check_sysemu : expected SIGTRAP, got status = %d",
209 if (ptrace(PTRACE_GETREGS
, pid
, 0, regs
) < 0)
210 fatal_perror("check_sysemu : PTRACE_GETREGS failed");
211 if (PT_SYSCALL_NR(regs
) != __NR_getpid
) {
212 non_fatal("check_sysemu got system call number %d, "
213 "expected %d...", PT_SYSCALL_NR(regs
), __NR_getpid
);
217 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_RET_OFFSET
, os_getpid());
219 non_fatal("check_sysemu : failed to modify system call "
224 if (stop_ptraced_child(pid
, 0, 0) < 0)
227 sysemu_supported
= 1;
229 set_using_sysemu(!force_sysemu_disabled
);
231 non_fatal("Checking advanced syscall emulation patch for ptrace...");
232 pid
= start_ptraced_child();
234 if ((ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0,
235 (void *) PTRACE_O_TRACESYSGOOD
) < 0))
236 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
240 if (ptrace(PTRACE_SYSEMU_SINGLESTEP
, pid
, 0, 0) < 0)
242 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
244 fatal_perror("check_ptrace : wait failed");
246 if (WIFSTOPPED(status
) &&
247 (WSTOPSIG(status
) == (SIGTRAP
|0x80))) {
249 fatal("check_ptrace : SYSEMU_SINGLESTEP "
250 "doesn't singlestep");
251 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_RET_OFFSET
,
254 fatal_perror("check_sysemu : failed to modify "
255 "system call return");
258 else if (WIFSTOPPED(status
) && (WSTOPSIG(status
) == SIGTRAP
))
261 fatal("check_ptrace : expected SIGTRAP or "
262 "(SIGTRAP | 0x80), got status = %d", status
);
264 if (stop_ptraced_child(pid
, 0, 0) < 0)
267 sysemu_supported
= 2;
270 if (!force_sysemu_disabled
)
271 set_using_sysemu(sysemu_supported
);
275 stop_ptraced_child(pid
, 1, 0);
277 non_fatal("missing\n");
280 static void __init
check_ptrace(void)
282 int pid
, syscall
, n
, status
;
284 non_fatal("Checking that ptrace can change system call numbers...");
285 pid
= start_ptraced_child();
287 if ((ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0,
288 (void *) PTRACE_O_TRACESYSGOOD
) < 0))
289 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
292 if (ptrace(PTRACE_SYSCALL
, pid
, 0, 0) < 0)
293 fatal_perror("check_ptrace : ptrace failed");
295 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
297 fatal_perror("check_ptrace : wait failed");
299 if (!WIFSTOPPED(status
) ||
300 (WSTOPSIG(status
) != (SIGTRAP
| 0x80)))
301 fatal("check_ptrace : expected (SIGTRAP|0x80), "
302 "got status = %d", status
);
304 syscall
= ptrace(PTRACE_PEEKUSR
, pid
, PT_SYSCALL_NR_OFFSET
,
306 if (syscall
== __NR_getpid
) {
307 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_NR_OFFSET
,
310 fatal_perror("check_ptrace : failed to modify "
315 stop_ptraced_child(pid
, 0, 1);
320 extern void check_tmpexec(void);
322 static void __init
check_coredump_limit(void)
325 int err
= getrlimit(RLIMIT_CORE
, &lim
);
328 perror("Getting core dump limit");
332 printf("Core dump limits :\n\tsoft - ");
333 if (lim
.rlim_cur
== RLIM_INFINITY
)
335 else printf("%lu\n", lim
.rlim_cur
);
338 if (lim
.rlim_max
== RLIM_INFINITY
)
340 else printf("%lu\n", lim
.rlim_max
);
343 void __init
os_early_checks(void)
347 /* Print out the core dump limits early */
348 check_coredump_limit();
352 /* Need to check this early because mmapping happens before the
357 pid
= start_ptraced_child();
358 if (init_registers(pid
))
359 fatal("Failed to initialize default registers");
360 stop_ptraced_child(pid
, 1, 1);
363 static int __init
noprocmm_cmd_param(char *str
, int* add
)
369 __uml_setup("noprocmm", noprocmm_cmd_param
,
371 " Turns off usage of /proc/mm, even if host supports it.\n"
372 " To support /proc/mm, the host needs to be patched using\n"
373 " the current skas3 patch.\n\n");
375 static int __init
noptracefaultinfo_cmd_param(char *str
, int* add
)
377 ptrace_faultinfo
= 0;
381 __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param
,
382 "noptracefaultinfo\n"
383 " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
384 " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
385 " using the current skas3 patch.\n\n");
387 static int __init
noptraceldt_cmd_param(char *str
, int* add
)
393 __uml_setup("noptraceldt", noptraceldt_cmd_param
,
395 " Turns off usage of PTRACE_LDT, even if host supports it.\n"
396 " To support PTRACE_LDT, the host needs to be patched using\n"
397 " the current skas3 patch.\n\n");
399 static inline void check_skas3_ptrace_faultinfo(void)
401 struct ptrace_faultinfo fi
;
404 non_fatal(" - PTRACE_FAULTINFO...");
405 pid
= start_ptraced_child();
407 n
= ptrace(PTRACE_FAULTINFO
, pid
, 0, &fi
);
409 ptrace_faultinfo
= 0;
411 non_fatal("not found\n");
416 if (!ptrace_faultinfo
)
417 non_fatal("found but disabled on command line\n");
419 non_fatal("found\n");
422 stop_ptraced_child(pid
, 1, 1);
425 static inline void check_skas3_ptrace_ldt(void)
429 unsigned char ldtbuf
[40];
430 struct ptrace_ldt ldt_op
= (struct ptrace_ldt
) {
431 .func
= 2, /* read default ldt */
433 .bytecount
= sizeof(ldtbuf
)};
435 non_fatal(" - PTRACE_LDT...");
436 pid
= start_ptraced_child();
438 n
= ptrace(PTRACE_LDT
, pid
, 0, (unsigned long) &ldt_op
);
441 non_fatal("not found\n");
449 non_fatal("found\n");
451 non_fatal("found, but use is disabled\n");
454 stop_ptraced_child(pid
, 1, 1);
456 /* PTRACE_LDT might be disabled via cmdline option.
457 * We want to override this, else we might use the stub
464 static inline void check_skas3_proc_mm(void)
466 non_fatal(" - /proc/mm...");
467 if (access("/proc/mm", W_OK
) < 0) {
472 non_fatal("found but disabled on command line\n");
473 else non_fatal("found\n");
476 void can_do_skas(void)
478 non_fatal("Checking for the skas3 patch in the host:\n");
480 check_skas3_proc_mm();
481 check_skas3_ptrace_faultinfo();
482 check_skas3_ptrace_ldt();
484 if (!proc_mm
|| !ptrace_faultinfo
|| !ptrace_ldt
)
488 int __init
parse_iomem(char *str
, int *add
)
490 struct iomem_region
*new;
496 file
= strchr(str
,',');
498 printf("parse_iomem : failed to parse iomem\n");
503 fd
= open(file
, O_RDWR
, 0);
505 perror("parse_iomem - Couldn't open io file");
509 if (fstat64(fd
, &buf
) < 0) {
510 perror("parse_iomem - cannot stat_fd file");
514 new = malloc(sizeof(*new));
516 perror("Couldn't allocate iomem_region struct");
520 size
= (buf
.st_size
+ UM_KERN_PAGE_SIZE
) & ~(UM_KERN_PAGE_SIZE
- 1);
522 *new = ((struct iomem_region
) { .next
= iomem_regions
,
529 iomem_size
+= new->size
+ UM_KERN_PAGE_SIZE
;