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"
27 #include "skas_ptrace.h"
29 static void ptrace_child(void)
32 /* Calling os_getpid because some libcs cached getpid incorrectly */
33 int pid
= os_getpid(), ppid
= getppid();
36 if (change_sig(SIGWINCH
, 0) < 0 ||
37 ptrace(PTRACE_TRACEME
, 0, 0, 0) < 0) {
44 * This syscall will be intercepted by the parent. Don't call more than
47 sc_result
= os_getpid();
50 /* Nothing modified by the parent, we are running normally. */
52 else if (sc_result
== ppid
)
54 * Expected in check_ptrace and check_sysemu when they succeed
55 * in modifying the stack frame
59 /* Serious trouble! This could be caused by a bug in host 2.6
60 * SKAS3/2.6 patch before release -V6, together with a bug in
61 * the UML code itself.
68 static void fatal_perror(const char *str
)
74 static void fatal(char *fmt
, ...)
79 vfprintf(stderr
, fmt
, list
);
85 static void non_fatal(char *fmt
, ...)
90 vfprintf(stderr
, fmt
, list
);
94 static int start_ptraced_child(void)
102 fatal_perror("start_ptraced_child : fork failed");
104 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
106 fatal_perror("check_ptrace : waitpid failed");
107 if (!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGSTOP
))
108 fatal("check_ptrace : expected SIGSTOP, got status = %d",
114 /* When testing for SYSEMU support, if it is one of the broken versions, we
115 * must just avoid using sysemu, not panic, but only if SYSEMU features are
117 * So only for SYSEMU features we test mustpanic, while normal host features
120 static int stop_ptraced_child(int pid
, int exitcode
, int mustexit
)
122 int status
, n
, ret
= 0;
124 if (ptrace(PTRACE_CONT
, pid
, 0, 0) < 0) {
125 perror("stop_ptraced_child : ptrace failed");
128 CATCH_EINTR(n
= waitpid(pid
, &status
, 0));
129 if (!WIFEXITED(status
) || (WEXITSTATUS(status
) != exitcode
)) {
130 int exit_with
= WEXITSTATUS(status
);
132 non_fatal("check_ptrace : child exited with status 2. "
133 "\nDisabling SYSEMU support.\n");
134 non_fatal("check_ptrace : child exited with exitcode %d, while "
135 "expecting %d; status 0x%x\n", exit_with
,
145 /* Changed only during early boot */
146 int ptrace_faultinfo
;
147 static int disable_ptrace_faultinfo
;
150 static int disable_ptrace_ldt
;
153 static int disable_proc_mm
;
156 static int disable_switch_mm
;
160 static int __init
skas0_cmd_param(char *str
, int* add
)
162 disable_ptrace_faultinfo
= 1;
163 disable_ptrace_ldt
= 1;
165 disable_switch_mm
= 1;
170 /* The two __uml_setup would conflict, without this stupid alias. */
172 static int __init
mode_skas0_cmd_param(char *str
, int* add
)
173 __attribute__((alias("skas0_cmd_param")));
175 __uml_setup("skas0", skas0_cmd_param
,
177 " Disables SKAS3 and SKAS4 usage, so that SKAS0 is used\n\n");
179 __uml_setup("mode=skas0", mode_skas0_cmd_param
,
181 " Disables SKAS3 and SKAS4 usage, so that SKAS0 is used.\n\n");
183 /* Changed only during early boot */
184 static int force_sysemu_disabled
= 0;
186 static int __init
nosysemu_cmd_param(char *str
, int* add
)
188 force_sysemu_disabled
= 1;
192 __uml_setup("nosysemu", nosysemu_cmd_param
,
194 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
195 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
196 " behaviour of ptrace() and helps reducing host context switch rate.\n"
197 " To make it working, you need a kernel patch for your host, too.\n"
198 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
199 " information.\n\n");
201 static void __init
check_sysemu(void)
203 unsigned long regs
[MAX_REG_NR
];
204 int pid
, n
, status
, count
=0;
206 non_fatal("Checking syscall emulation patch for ptrace...");
207 sysemu_supported
= 0;
208 pid
= start_ptraced_child();
210 if (ptrace(PTRACE_SYSEMU
, pid
, 0, 0) < 0)
213 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
215 fatal_perror("check_sysemu : wait failed");
216 if (!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGTRAP
))
217 fatal("check_sysemu : expected SIGTRAP, got status = %d\n",
220 if (ptrace(PTRACE_GETREGS
, pid
, 0, regs
) < 0)
221 fatal_perror("check_sysemu : PTRACE_GETREGS failed");
222 if (PT_SYSCALL_NR(regs
) != __NR_getpid
) {
223 non_fatal("check_sysemu got system call number %d, "
224 "expected %d...", PT_SYSCALL_NR(regs
), __NR_getpid
);
228 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_RET_OFFSET
, os_getpid());
230 non_fatal("check_sysemu : failed to modify system call "
235 if (stop_ptraced_child(pid
, 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();
245 if ((ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0,
246 (void *) PTRACE_O_TRACESYSGOOD
) < 0))
247 fatal_perror("check_sysemu: 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_sysemu: wait failed");
257 if (WIFSTOPPED(status
) &&
258 (WSTOPSIG(status
) == (SIGTRAP
|0x80))) {
260 non_fatal("check_sysemu: SYSEMU_SINGLESTEP "
261 "doesn't singlestep");
264 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_RET_OFFSET
,
267 fatal_perror("check_sysemu : failed to modify "
268 "system call return");
271 else if (WIFSTOPPED(status
) && (WSTOPSIG(status
) == SIGTRAP
))
274 non_fatal("check_sysemu: expected SIGTRAP or "
275 "(SIGTRAP | 0x80), got status = %d\n",
280 if (stop_ptraced_child(pid
, 0, 0) < 0)
283 sysemu_supported
= 2;
286 if (!force_sysemu_disabled
)
287 set_using_sysemu(sysemu_supported
);
291 stop_ptraced_child(pid
, 1, 0);
293 non_fatal("missing\n");
296 static void __init
check_ptrace(void)
298 int pid
, syscall
, n
, status
;
300 non_fatal("Checking that ptrace can change system call numbers...");
301 pid
= start_ptraced_child();
303 if ((ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0,
304 (void *) PTRACE_O_TRACESYSGOOD
) < 0))
305 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
308 if (ptrace(PTRACE_SYSCALL
, pid
, 0, 0) < 0)
309 fatal_perror("check_ptrace : ptrace failed");
311 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
313 fatal_perror("check_ptrace : wait failed");
315 if (!WIFSTOPPED(status
) ||
316 (WSTOPSIG(status
) != (SIGTRAP
| 0x80)))
317 fatal("check_ptrace : expected (SIGTRAP|0x80), "
318 "got status = %d", status
);
320 syscall
= ptrace(PTRACE_PEEKUSR
, pid
, PT_SYSCALL_NR_OFFSET
,
322 if (syscall
== __NR_getpid
) {
323 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_NR_OFFSET
,
326 fatal_perror("check_ptrace : failed to modify "
331 stop_ptraced_child(pid
, 0, 1);
336 extern void check_tmpexec(void);
338 static void __init
check_coredump_limit(void)
341 int err
= getrlimit(RLIMIT_CORE
, &lim
);
344 perror("Getting core dump limit");
348 printf("Core dump limits :\n\tsoft - ");
349 if (lim
.rlim_cur
== RLIM_INFINITY
)
351 else printf("%lu\n", lim
.rlim_cur
);
354 if (lim
.rlim_max
== RLIM_INFINITY
)
356 else printf("%lu\n", lim
.rlim_max
);
359 void __init
os_early_checks(void)
363 /* Print out the core dump limits early */
364 check_coredump_limit();
368 /* Need to check this early because mmapping happens before the
373 pid
= start_ptraced_child();
374 if (init_registers(pid
))
375 fatal("Failed to initialize default registers");
376 stop_ptraced_child(pid
, 1, 1);
379 static int __init
noprocmm_cmd_param(char *str
, int* add
)
385 __uml_setup("noprocmm", noprocmm_cmd_param
,
387 " Turns off usage of /proc/mm, even if host supports it.\n"
388 " To support /proc/mm, the host needs to be patched using\n"
389 " the current skas3 patch.\n\n");
391 static int __init
noptracefaultinfo_cmd_param(char *str
, int* add
)
393 disable_ptrace_faultinfo
= 1;
397 __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param
,
398 "noptracefaultinfo\n"
399 " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
400 " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
401 " using the current skas3 patch.\n\n");
403 static int __init
noptraceldt_cmd_param(char *str
, int* add
)
405 disable_ptrace_ldt
= 1;
409 __uml_setup("noptraceldt", noptraceldt_cmd_param
,
411 " Turns off usage of PTRACE_LDT, even if host supports it.\n"
412 " To support PTRACE_LDT, the host needs to be patched using\n"
413 " the current skas3 patch.\n\n");
415 static inline void check_skas3_ptrace_faultinfo(void)
417 struct ptrace_faultinfo fi
;
420 non_fatal(" - PTRACE_FAULTINFO...");
421 pid
= start_ptraced_child();
423 n
= ptrace(PTRACE_FAULTINFO
, pid
, 0, &fi
);
426 non_fatal("not found\n");
429 } else if (disable_ptrace_faultinfo
)
430 non_fatal("found but disabled on command line\n");
432 ptrace_faultinfo
= 1;
433 non_fatal("found\n");
436 stop_ptraced_child(pid
, 1, 1);
439 static inline void check_skas3_ptrace_ldt(void)
443 unsigned char ldtbuf
[40];
444 struct ptrace_ldt ldt_op
= (struct ptrace_ldt
) {
445 .func
= 2, /* read default ldt */
447 .bytecount
= sizeof(ldtbuf
)};
449 non_fatal(" - PTRACE_LDT...");
450 pid
= start_ptraced_child();
452 n
= ptrace(PTRACE_LDT
, pid
, 0, (unsigned long) &ldt_op
);
455 non_fatal("not found\n");
458 } else if (disable_ptrace_ldt
)
459 non_fatal("found, but use is disabled\n");
462 non_fatal("found\n");
465 stop_ptraced_child(pid
, 1, 1);
469 static inline void check_skas3_proc_mm(void)
471 non_fatal(" - /proc/mm...");
472 if (access("/proc/mm", W_OK
) < 0)
474 else if (disable_proc_mm
)
475 non_fatal("found but disabled on command line\n");
478 non_fatal("found\n");
482 void can_do_skas(void)
484 non_fatal("Checking for the skas3 patch in the host:\n");
486 check_skas3_proc_mm();
487 check_skas3_ptrace_faultinfo();
488 check_skas3_ptrace_ldt();
490 if (!proc_mm
|| !ptrace_faultinfo
|| !ptrace_ldt
)
494 int __init
parse_iomem(char *str
, int *add
)
496 struct iomem_region
*new;
502 file
= strchr(str
,',');
504 fprintf(stderr
, "parse_iomem : failed to parse iomem\n");
509 fd
= open(file
, O_RDWR
, 0);
511 perror("parse_iomem - Couldn't open io file");
515 if (fstat64(fd
, &buf
) < 0) {
516 perror("parse_iomem - cannot stat_fd file");
520 new = malloc(sizeof(*new));
522 perror("Couldn't allocate iomem_region struct");
526 size
= (buf
.st_size
+ UM_KERN_PAGE_SIZE
) & ~(UM_KERN_PAGE_SIZE
- 1);
528 *new = ((struct iomem_region
) { .next
= iomem_regions
,
535 iomem_size
+= new->size
+ UM_KERN_PAGE_SIZE
;