2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
18 #include <asm/unistd.h>
22 #include "ptrace_user.h"
23 #include "registers.h"
25 #include "skas_ptrace.h"
27 static void ptrace_child(void)
30 /* Calling os_getpid because some libcs cached getpid incorrectly */
31 int pid
= os_getpid(), ppid
= getppid();
34 if (change_sig(SIGWINCH
, 0) < 0 ||
35 ptrace(PTRACE_TRACEME
, 0, 0, 0) < 0) {
42 * This syscall will be intercepted by the parent. Don't call more than
45 sc_result
= os_getpid();
48 /* Nothing modified by the parent, we are running normally. */
50 else if (sc_result
== ppid
)
52 * Expected in check_ptrace and check_sysemu when they succeed
53 * in modifying the stack frame
57 /* Serious trouble! This could be caused by a bug in host 2.6
58 * SKAS3/2.6 patch before release -V6, together with a bug in
59 * the UML code itself.
66 static void fatal_perror(const char *str
)
72 static void fatal(char *fmt
, ...)
77 vfprintf(stderr
, fmt
, list
);
83 static void non_fatal(char *fmt
, ...)
88 vfprintf(stderr
, fmt
, list
);
92 static int start_ptraced_child(void)
100 fatal_perror("start_ptraced_child : fork failed");
102 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
104 fatal_perror("check_ptrace : waitpid failed");
105 if (!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGSTOP
))
106 fatal("check_ptrace : expected SIGSTOP, got status = %d",
112 /* When testing for SYSEMU support, if it is one of the broken versions, we
113 * must just avoid using sysemu, not panic, but only if SYSEMU features are
115 * So only for SYSEMU features we test mustpanic, while normal host features
118 static int stop_ptraced_child(int pid
, int exitcode
, int mustexit
)
120 int status
, n
, ret
= 0;
122 if (ptrace(PTRACE_CONT
, pid
, 0, 0) < 0) {
123 perror("stop_ptraced_child : ptrace failed");
126 CATCH_EINTR(n
= waitpid(pid
, &status
, 0));
127 if (!WIFEXITED(status
) || (WEXITSTATUS(status
) != exitcode
)) {
128 int exit_with
= WEXITSTATUS(status
);
130 non_fatal("check_ptrace : child exited with status 2. "
131 "\nDisabling SYSEMU support.\n");
132 non_fatal("check_ptrace : child exited with exitcode %d, while "
133 "expecting %d; status 0x%x\n", exit_with
,
143 /* Changed only during early boot */
144 int ptrace_faultinfo
;
145 static int disable_ptrace_faultinfo
;
148 static int disable_ptrace_ldt
;
151 static int disable_proc_mm
;
154 static int disable_switch_mm
;
158 static int __init
skas0_cmd_param(char *str
, int* add
)
160 disable_ptrace_faultinfo
= 1;
161 disable_ptrace_ldt
= 1;
163 disable_switch_mm
= 1;
168 /* The two __uml_setup would conflict, without this stupid alias. */
170 static int __init
mode_skas0_cmd_param(char *str
, int* add
)
171 __attribute__((alias("skas0_cmd_param")));
173 __uml_setup("skas0", skas0_cmd_param
,
175 " Disables SKAS3 and SKAS4 usage, so that SKAS0 is used\n\n");
177 __uml_setup("mode=skas0", mode_skas0_cmd_param
,
179 " Disables SKAS3 and SKAS4 usage, so that SKAS0 is used.\n\n");
181 /* Changed only during early boot */
182 static int force_sysemu_disabled
= 0;
184 static int __init
nosysemu_cmd_param(char *str
, int* add
)
186 force_sysemu_disabled
= 1;
190 __uml_setup("nosysemu", nosysemu_cmd_param
,
192 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
193 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
194 " behaviour of ptrace() and helps reducing host context switch rate.\n"
195 " To make it working, you need a kernel patch for your host, too.\n"
196 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
197 " information.\n\n");
199 static void __init
check_sysemu(void)
201 unsigned long regs
[MAX_REG_NR
];
202 int pid
, n
, status
, count
=0;
204 non_fatal("Checking syscall emulation patch for ptrace...");
205 sysemu_supported
= 0;
206 pid
= start_ptraced_child();
208 if (ptrace(PTRACE_SYSEMU
, pid
, 0, 0) < 0)
211 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
213 fatal_perror("check_sysemu : wait failed");
214 if (!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGTRAP
))
215 fatal("check_sysemu : expected SIGTRAP, got status = %d\n",
218 if (ptrace(PTRACE_GETREGS
, pid
, 0, regs
) < 0)
219 fatal_perror("check_sysemu : PTRACE_GETREGS failed");
220 if (PT_SYSCALL_NR(regs
) != __NR_getpid
) {
221 non_fatal("check_sysemu got system call number %d, "
222 "expected %d...", PT_SYSCALL_NR(regs
), __NR_getpid
);
226 n
= ptrace(PTRACE_POKEUSER
, pid
, PT_SYSCALL_RET_OFFSET
, os_getpid());
228 non_fatal("check_sysemu : failed to modify system call "
233 if (stop_ptraced_child(pid
, 0, 0) < 0)
236 sysemu_supported
= 1;
238 set_using_sysemu(!force_sysemu_disabled
);
240 non_fatal("Checking advanced syscall emulation patch for ptrace...");
241 pid
= start_ptraced_child();
243 if ((ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0,
244 (void *) PTRACE_O_TRACESYSGOOD
) < 0))
245 fatal_perror("check_sysemu: PTRACE_OLDSETOPTIONS failed");
249 if (ptrace(PTRACE_SYSEMU_SINGLESTEP
, pid
, 0, 0) < 0)
251 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
253 fatal_perror("check_sysemu: wait failed");
255 if (WIFSTOPPED(status
) &&
256 (WSTOPSIG(status
) == (SIGTRAP
|0x80))) {
258 non_fatal("check_sysemu: SYSEMU_SINGLESTEP "
259 "doesn't singlestep");
262 n
= ptrace(PTRACE_POKEUSER
, pid
, PT_SYSCALL_RET_OFFSET
,
265 fatal_perror("check_sysemu : failed to modify "
266 "system call return");
269 else if (WIFSTOPPED(status
) && (WSTOPSIG(status
) == SIGTRAP
))
272 non_fatal("check_sysemu: expected SIGTRAP or "
273 "(SIGTRAP | 0x80), got status = %d\n",
278 if (stop_ptraced_child(pid
, 0, 0) < 0)
281 sysemu_supported
= 2;
284 if (!force_sysemu_disabled
)
285 set_using_sysemu(sysemu_supported
);
289 stop_ptraced_child(pid
, 1, 0);
291 non_fatal("missing\n");
294 static void __init
check_ptrace(void)
296 int pid
, syscall
, n
, status
;
298 non_fatal("Checking that ptrace can change system call numbers...");
299 pid
= start_ptraced_child();
301 if ((ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0,
302 (void *) PTRACE_O_TRACESYSGOOD
) < 0))
303 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
306 if (ptrace(PTRACE_SYSCALL
, pid
, 0, 0) < 0)
307 fatal_perror("check_ptrace : ptrace failed");
309 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
311 fatal_perror("check_ptrace : wait failed");
313 if (!WIFSTOPPED(status
) ||
314 (WSTOPSIG(status
) != (SIGTRAP
| 0x80)))
315 fatal("check_ptrace : expected (SIGTRAP|0x80), "
316 "got status = %d", status
);
318 syscall
= ptrace(PTRACE_PEEKUSER
, pid
, PT_SYSCALL_NR_OFFSET
,
320 if (syscall
== __NR_getpid
) {
321 n
= ptrace(PTRACE_POKEUSER
, pid
, PT_SYSCALL_NR_OFFSET
,
324 fatal_perror("check_ptrace : failed to modify "
329 stop_ptraced_child(pid
, 0, 1);
334 extern void check_tmpexec(void);
336 static void __init
check_coredump_limit(void)
339 int err
= getrlimit(RLIMIT_CORE
, &lim
);
342 perror("Getting core dump limit");
346 printf("Core dump limits :\n\tsoft - ");
347 if (lim
.rlim_cur
== RLIM_INFINITY
)
349 else printf("%lu\n", lim
.rlim_cur
);
352 if (lim
.rlim_max
== RLIM_INFINITY
)
354 else printf("%lu\n", lim
.rlim_max
);
357 void __init
os_early_checks(void)
361 /* Print out the core dump limits early */
362 check_coredump_limit();
366 /* Need to check this early because mmapping happens before the
371 pid
= start_ptraced_child();
372 if (init_registers(pid
))
373 fatal("Failed to initialize default registers");
374 stop_ptraced_child(pid
, 1, 1);
377 static int __init
noprocmm_cmd_param(char *str
, int* add
)
383 __uml_setup("noprocmm", noprocmm_cmd_param
,
385 " Turns off usage of /proc/mm, even if host supports it.\n"
386 " To support /proc/mm, the host needs to be patched using\n"
387 " the current skas3 patch.\n\n");
389 static int __init
noptracefaultinfo_cmd_param(char *str
, int* add
)
391 disable_ptrace_faultinfo
= 1;
395 __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param
,
396 "noptracefaultinfo\n"
397 " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
398 " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
399 " using the current skas3 patch.\n\n");
401 static int __init
noptraceldt_cmd_param(char *str
, int* add
)
403 disable_ptrace_ldt
= 1;
407 __uml_setup("noptraceldt", noptraceldt_cmd_param
,
409 " Turns off usage of PTRACE_LDT, even if host supports it.\n"
410 " To support PTRACE_LDT, the host needs to be patched using\n"
411 " the current skas3 patch.\n\n");
413 static inline void check_skas3_ptrace_faultinfo(void)
415 struct ptrace_faultinfo fi
;
418 non_fatal(" - PTRACE_FAULTINFO...");
419 pid
= start_ptraced_child();
421 n
= ptrace(PTRACE_FAULTINFO
, pid
, 0, &fi
);
424 non_fatal("not found\n");
427 } else if (disable_ptrace_faultinfo
)
428 non_fatal("found but disabled on command line\n");
430 ptrace_faultinfo
= 1;
431 non_fatal("found\n");
434 stop_ptraced_child(pid
, 1, 1);
437 static inline void check_skas3_ptrace_ldt(void)
441 unsigned char ldtbuf
[40];
442 struct ptrace_ldt ldt_op
= (struct ptrace_ldt
) {
443 .func
= 2, /* read default ldt */
445 .bytecount
= sizeof(ldtbuf
)};
447 non_fatal(" - PTRACE_LDT...");
448 pid
= start_ptraced_child();
450 n
= ptrace(PTRACE_LDT
, pid
, 0, (unsigned long) &ldt_op
);
453 non_fatal("not found\n");
456 } else if (disable_ptrace_ldt
)
457 non_fatal("found, but use is disabled\n");
460 non_fatal("found\n");
463 stop_ptraced_child(pid
, 1, 1);
467 static inline void check_skas3_proc_mm(void)
469 non_fatal(" - /proc/mm...");
470 if (access("/proc/mm", W_OK
) < 0)
472 else if (disable_proc_mm
)
473 non_fatal("found but disabled on command line\n");
476 non_fatal("found\n");
480 void can_do_skas(void)
482 non_fatal("Checking for the skas3 patch in the host:\n");
484 check_skas3_proc_mm();
485 check_skas3_ptrace_faultinfo();
486 check_skas3_ptrace_ldt();
488 if (!proc_mm
|| !ptrace_faultinfo
|| !ptrace_ldt
)
492 int __init
parse_iomem(char *str
, int *add
)
494 struct iomem_region
*new;
500 file
= strchr(str
,',');
502 fprintf(stderr
, "parse_iomem : failed to parse iomem\n");
507 fd
= open(file
, O_RDWR
, 0);
509 perror("parse_iomem - Couldn't open io file");
513 if (fstat64(fd
, &buf
) < 0) {
514 perror("parse_iomem - cannot stat_fd file");
518 new = malloc(sizeof(*new));
520 perror("Couldn't allocate iomem_region struct");
524 size
= (buf
.st_size
+ UM_KERN_PAGE_SIZE
) & ~(UM_KERN_PAGE_SIZE
- 1);
526 *new = ((struct iomem_region
) { .next
= iomem_regions
,
533 iomem_size
+= new->size
+ UM_KERN_PAGE_SIZE
;