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 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 fatal_perror("stop_ptraced_child : ptrace failed");
125 CATCH_EINTR(n
= waitpid(pid
, &status
, 0));
126 if (!WIFEXITED(status
) || (WEXITSTATUS(status
) != exitcode
)) {
127 int exit_with
= WEXITSTATUS(status
);
129 non_fatal("check_ptrace : child exited with status 2. "
130 "\nDisabling SYSEMU support.\n");
131 non_fatal("check_ptrace : child exited with exitcode %d, while "
132 "expecting %d; status 0x%x\n", exit_with
,
142 /* Changed only during early boot */
143 int ptrace_faultinfo
= 1;
146 int skas_needs_stub
= 0;
148 static int __init
skas0_cmd_param(char *str
, int* add
)
150 ptrace_faultinfo
= proc_mm
= 0;
154 /* The two __uml_setup would conflict, without this stupid alias. */
156 static int __init
mode_skas0_cmd_param(char *str
, int* add
)
157 __attribute__((alias("skas0_cmd_param")));
159 __uml_setup("skas0", skas0_cmd_param
,
161 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
162 " you specify mode=tt.\n\n");
164 __uml_setup("mode=skas0", mode_skas0_cmd_param
,
166 " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
167 " specify mode=tt. Note that this was recently added - on \n"
168 " older kernels you must use simply \"skas0\".\n\n");
170 /* Changed only during early boot */
171 static int force_sysemu_disabled
= 0;
173 static int __init
nosysemu_cmd_param(char *str
, int* add
)
175 force_sysemu_disabled
= 1;
179 __uml_setup("nosysemu", nosysemu_cmd_param
,
181 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
182 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
183 " behaviour of ptrace() and helps reducing host context switch rate.\n"
184 " To make it working, you need a kernel patch for your host, too.\n"
185 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
186 " information.\n\n");
188 static void __init
check_sysemu(void)
190 unsigned long regs
[MAX_REG_NR
];
191 int pid
, n
, status
, count
=0;
193 non_fatal("Checking syscall emulation patch for ptrace...");
194 sysemu_supported
= 0;
195 pid
= start_ptraced_child();
197 if (ptrace(PTRACE_SYSEMU
, pid
, 0, 0) < 0)
200 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
202 fatal_perror("check_sysemu : wait failed");
203 if (!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGTRAP
))
204 fatal("check_sysemu : expected SIGTRAP, got status = %d",
207 if (ptrace(PTRACE_GETREGS
, pid
, 0, regs
) < 0)
208 fatal_perror("check_sysemu : PTRACE_GETREGS failed");
209 if (PT_SYSCALL_NR(regs
) != __NR_getpid
) {
210 non_fatal("check_sysemu got system call number %d, "
211 "expected %d...", PT_SYSCALL_NR(regs
), __NR_getpid
);
215 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_RET_OFFSET
, os_getpid());
217 non_fatal("check_sysemu : failed to modify system call "
222 if (stop_ptraced_child(pid
, 0, 0) < 0)
225 sysemu_supported
= 1;
227 set_using_sysemu(!force_sysemu_disabled
);
229 non_fatal("Checking advanced syscall emulation patch for ptrace...");
230 pid
= start_ptraced_child();
232 if ((ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0,
233 (void *) PTRACE_O_TRACESYSGOOD
) < 0))
234 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
238 if (ptrace(PTRACE_SYSEMU_SINGLESTEP
, pid
, 0, 0) < 0)
240 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
242 fatal_perror("check_ptrace : wait failed");
244 if (WIFSTOPPED(status
) &&
245 (WSTOPSIG(status
) == (SIGTRAP
|0x80))) {
247 fatal("check_ptrace : SYSEMU_SINGLESTEP "
248 "doesn't singlestep");
249 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_RET_OFFSET
,
252 fatal_perror("check_sysemu : failed to modify "
253 "system call return");
256 else if (WIFSTOPPED(status
) && (WSTOPSIG(status
) == SIGTRAP
))
259 fatal("check_ptrace : expected SIGTRAP or "
260 "(SIGTRAP | 0x80), got status = %d", status
);
262 if (stop_ptraced_child(pid
, 0, 0) < 0)
265 sysemu_supported
= 2;
268 if (!force_sysemu_disabled
)
269 set_using_sysemu(sysemu_supported
);
273 stop_ptraced_child(pid
, 1, 0);
275 non_fatal("missing\n");
278 static void __init
check_ptrace(void)
280 int pid
, syscall
, n
, status
;
282 non_fatal("Checking that ptrace can change system call numbers...");
283 pid
= start_ptraced_child();
285 if ((ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0,
286 (void *) PTRACE_O_TRACESYSGOOD
) < 0))
287 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
290 if (ptrace(PTRACE_SYSCALL
, pid
, 0, 0) < 0)
291 fatal_perror("check_ptrace : ptrace failed");
293 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
295 fatal_perror("check_ptrace : wait failed");
297 if (!WIFSTOPPED(status
) ||
298 (WSTOPSIG(status
) != (SIGTRAP
| 0x80)))
299 fatal("check_ptrace : expected (SIGTRAP|0x80), "
300 "got status = %d", status
);
302 syscall
= ptrace(PTRACE_PEEKUSR
, pid
, PT_SYSCALL_NR_OFFSET
,
304 if (syscall
== __NR_getpid
) {
305 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_NR_OFFSET
,
308 fatal_perror("check_ptrace : failed to modify "
313 stop_ptraced_child(pid
, 0, 1);
318 extern void check_tmpexec(void);
320 static void __init
check_coredump_limit(void)
323 int err
= getrlimit(RLIMIT_CORE
, &lim
);
326 perror("Getting core dump limit");
330 printf("Core dump limits :\n\tsoft - ");
331 if (lim
.rlim_cur
== RLIM_INFINITY
)
333 else printf("%lu\n", lim
.rlim_cur
);
336 if (lim
.rlim_max
== RLIM_INFINITY
)
338 else printf("%lu\n", lim
.rlim_max
);
341 void __init
os_early_checks(void)
345 /* Print out the core dump limits early */
346 check_coredump_limit();
350 /* Need to check this early because mmapping happens before the
355 pid
= start_ptraced_child();
356 if (init_registers(pid
))
357 fatal("Failed to initialize default registers");
358 stop_ptraced_child(pid
, 1, 1);
361 static int __init
noprocmm_cmd_param(char *str
, int* add
)
367 __uml_setup("noprocmm", noprocmm_cmd_param
,
369 " Turns off usage of /proc/mm, even if host supports it.\n"
370 " To support /proc/mm, the host needs to be patched using\n"
371 " the current skas3 patch.\n\n");
373 static int __init
noptracefaultinfo_cmd_param(char *str
, int* add
)
375 ptrace_faultinfo
= 0;
379 __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param
,
380 "noptracefaultinfo\n"
381 " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
382 " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
383 " using the current skas3 patch.\n\n");
385 static int __init
noptraceldt_cmd_param(char *str
, int* add
)
391 __uml_setup("noptraceldt", noptraceldt_cmd_param
,
393 " Turns off usage of PTRACE_LDT, even if host supports it.\n"
394 " To support PTRACE_LDT, the host needs to be patched using\n"
395 " the current skas3 patch.\n\n");
397 static inline void check_skas3_ptrace_faultinfo(void)
399 struct ptrace_faultinfo fi
;
402 non_fatal(" - PTRACE_FAULTINFO...");
403 pid
= start_ptraced_child();
405 n
= ptrace(PTRACE_FAULTINFO
, pid
, 0, &fi
);
407 ptrace_faultinfo
= 0;
409 non_fatal("not found\n");
414 if (!ptrace_faultinfo
)
415 non_fatal("found but disabled on command line\n");
417 non_fatal("found\n");
420 stop_ptraced_child(pid
, 1, 1);
423 static inline void check_skas3_ptrace_ldt(void)
427 unsigned char ldtbuf
[40];
428 struct ptrace_ldt ldt_op
= (struct ptrace_ldt
) {
429 .func
= 2, /* read default ldt */
431 .bytecount
= sizeof(ldtbuf
)};
433 non_fatal(" - PTRACE_LDT...");
434 pid
= start_ptraced_child();
436 n
= ptrace(PTRACE_LDT
, pid
, 0, (unsigned long) &ldt_op
);
439 non_fatal("not found\n");
447 non_fatal("found\n");
449 non_fatal("found, but use is disabled\n");
452 stop_ptraced_child(pid
, 1, 1);
454 /* PTRACE_LDT might be disabled via cmdline option.
455 * We want to override this, else we might use the stub
462 static inline void check_skas3_proc_mm(void)
464 non_fatal(" - /proc/mm...");
465 if (access("/proc/mm", W_OK
) < 0) {
470 non_fatal("found but disabled on command line\n");
471 else non_fatal("found\n");
474 void can_do_skas(void)
476 non_fatal("Checking for the skas3 patch in the host:\n");
478 check_skas3_proc_mm();
479 check_skas3_ptrace_faultinfo();
480 check_skas3_ptrace_ldt();
482 if (!proc_mm
|| !ptrace_faultinfo
|| !ptrace_ldt
)
486 int __init
parse_iomem(char *str
, int *add
)
488 struct iomem_region
*new;
494 file
= strchr(str
,',');
496 fprintf(stderr
, "parse_iomem : failed to parse iomem\n");
501 fd
= open(file
, O_RDWR
, 0);
503 perror("parse_iomem - Couldn't open io file");
507 if (fstat64(fd
, &buf
) < 0) {
508 perror("parse_iomem - cannot stat_fd file");
512 new = malloc(sizeof(*new));
514 perror("Couldn't allocate iomem_region struct");
518 size
= (buf
.st_size
+ UM_KERN_PAGE_SIZE
) & ~(UM_KERN_PAGE_SIZE
- 1);
520 *new = ((struct iomem_region
) { .next
= iomem_regions
,
527 iomem_size
+= new->size
+ UM_KERN_PAGE_SIZE
;