2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
20 #include <sys/resource.h>
21 #include <asm/unistd.h>
22 #include <sys/types.h>
23 #include "kern_util.h"
25 #include "signal_kern.h"
26 #include "sysdep/ptrace.h"
27 #include "sysdep/sigcontext.h"
29 #include "ptrace_user.h"
33 #include "uml-config.h"
34 #include "choose-mode.h"
37 #include "kern_constants.h"
39 #ifdef UML_CONFIG_MODE_SKAS
41 #include "skas_ptrace.h"
42 #include "registers.h"
45 static int ptrace_child(void *arg
)
48 int pid
= os_getpid(), ppid
= getppid();
51 change_sig(SIGWINCH
, 0);
52 if(ptrace(PTRACE_TRACEME
, 0, 0, 0) < 0){
54 os_kill_process(pid
, 0);
58 /*This syscall will be intercepted by the parent. Don't call more than
60 sc_result
= os_getpid();
63 ret
= 1; /*Nothing modified by the parent, we are running
65 else if (sc_result
== ppid
)
66 ret
= 0; /*Expected in check_ptrace and check_sysemu when they
67 succeed in modifying the stack frame*/
69 ret
= 2; /*Serious trouble! This could be caused by a bug in
70 host 2.6 SKAS3/2.6 patch before release -V6, together
71 with a bug in the UML code itself.*/
75 static void fatal_perror(char *str
)
81 static void fatal(char *fmt
, ...)
93 static void non_fatal(char *fmt
, ...)
103 static int start_ptraced_child(void **stack_out
)
109 stack
= mmap(NULL
, UM_KERN_PAGE_SIZE
,
110 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
111 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
112 if(stack
== MAP_FAILED
)
113 fatal_perror("check_ptrace : mmap failed");
114 sp
= (unsigned long) stack
+ UM_KERN_PAGE_SIZE
- sizeof(void *);
115 pid
= clone(ptrace_child
, (void *) sp
, SIGCHLD
, NULL
);
117 fatal_perror("start_ptraced_child : clone failed");
118 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
120 fatal_perror("check_ptrace : clone failed");
121 if(!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGSTOP
))
122 fatal("check_ptrace : expected SIGSTOP, got status = %d",
129 /* When testing for SYSEMU support, if it is one of the broken versions, we
130 * must just avoid using sysemu, not panic, but only if SYSEMU features are
132 * So only for SYSEMU features we test mustpanic, while normal host features
135 static int stop_ptraced_child(int pid
, void *stack
, int exitcode
,
138 int status
, n
, ret
= 0;
140 if(ptrace(PTRACE_CONT
, pid
, 0, 0) < 0)
141 fatal_perror("stop_ptraced_child : ptrace failed");
142 CATCH_EINTR(n
= waitpid(pid
, &status
, 0));
143 if(!WIFEXITED(status
) || (WEXITSTATUS(status
) != exitcode
)) {
144 int exit_with
= WEXITSTATUS(status
);
146 non_fatal("check_ptrace : child exited with status 2. "
147 "\nDisabling SYSEMU support.\n");
148 non_fatal("check_ptrace : child exited with exitcode %d, while "
149 "expecting %d; status 0x%x\n", exit_with
,
156 if(munmap(stack
, UM_KERN_PAGE_SIZE
) < 0)
157 fatal_perror("check_ptrace : munmap failed");
161 /* Changed only during early boot */
162 int ptrace_faultinfo
= 1;
165 int skas_needs_stub
= 0;
167 static int __init
skas0_cmd_param(char *str
, int* add
)
169 ptrace_faultinfo
= proc_mm
= 0;
173 /* The two __uml_setup would conflict, without this stupid alias. */
175 static int __init
mode_skas0_cmd_param(char *str
, int* add
)
176 __attribute__((alias("skas0_cmd_param")));
178 __uml_setup("skas0", skas0_cmd_param
,
180 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
181 " you specify mode=tt.\n\n");
183 __uml_setup("mode=skas0", mode_skas0_cmd_param
,
185 " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
186 " specify mode=tt. Note that this was recently added - on \n"
187 " older kernels you must use simply \"skas0\".\n\n");
189 /* Changed only during early boot */
190 static int force_sysemu_disabled
= 0;
192 static int __init
nosysemu_cmd_param(char *str
, int* add
)
194 force_sysemu_disabled
= 1;
198 __uml_setup("nosysemu", nosysemu_cmd_param
,
200 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
201 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
202 " behaviour of ptrace() and helps reducing host context switch rate.\n"
203 " To make it working, you need a kernel patch for your host, too.\n"
204 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
205 " information.\n\n");
207 static void __init
check_sysemu(void)
210 unsigned long regs
[MAX_REG_NR
];
211 int pid
, n
, status
, count
=0;
213 non_fatal("Checking syscall emulation patch for ptrace...");
214 sysemu_supported
= 0;
215 pid
= start_ptraced_child(&stack
);
217 if(ptrace(PTRACE_SYSEMU
, pid
, 0, 0) < 0)
220 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
222 fatal_perror("check_sysemu : wait failed");
223 if(!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGTRAP
))
224 fatal("check_sysemu : expected SIGTRAP, got status = %d",
227 if(ptrace(PTRACE_GETREGS
, pid
, 0, regs
) < 0)
228 fatal_perror("check_sysemu : PTRACE_GETREGS failed");
229 if(PT_SYSCALL_NR(regs
) != __NR_getpid
){
230 non_fatal("check_sysemu got system call number %d, "
231 "expected %d...", PT_SYSCALL_NR(regs
), __NR_getpid
);
235 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_RET_OFFSET
, os_getpid());
237 non_fatal("check_sysemu : failed to modify system call "
242 if (stop_ptraced_child(pid
, stack
, 0, 0) < 0)
245 sysemu_supported
= 1;
247 set_using_sysemu(!force_sysemu_disabled
);
249 non_fatal("Checking advanced syscall emulation patch for ptrace...");
250 pid
= start_ptraced_child(&stack
);
252 if((ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0,
253 (void *) PTRACE_O_TRACESYSGOOD
) < 0))
254 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
258 if(ptrace(PTRACE_SYSEMU_SINGLESTEP
, pid
, 0, 0) < 0)
260 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
262 fatal_perror("check_ptrace : wait failed");
264 if(WIFSTOPPED(status
) && (WSTOPSIG(status
) == (SIGTRAP
|0x80))){
266 fatal("check_ptrace : SYSEMU_SINGLESTEP "
267 "doesn't singlestep");
268 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_RET_OFFSET
,
271 fatal_perror("check_sysemu : failed to modify "
272 "system call return");
275 else if(WIFSTOPPED(status
) && (WSTOPSIG(status
) == SIGTRAP
))
278 fatal("check_ptrace : expected SIGTRAP or "
279 "(SIGTRAP | 0x80), got status = %d", status
);
281 if (stop_ptraced_child(pid
, stack
, 0, 0) < 0)
284 sysemu_supported
= 2;
287 if ( !force_sysemu_disabled
)
288 set_using_sysemu(sysemu_supported
);
292 stop_ptraced_child(pid
, stack
, 1, 0);
294 non_fatal("missing\n");
297 static void __init
check_ptrace(void)
300 int pid
, syscall
, n
, status
;
302 non_fatal("Checking that ptrace can change system call numbers...");
303 pid
= start_ptraced_child(&stack
);
305 if((ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0,
306 (void *) PTRACE_O_TRACESYSGOOD
) < 0))
307 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
310 if(ptrace(PTRACE_SYSCALL
, pid
, 0, 0) < 0)
311 fatal_perror("check_ptrace : ptrace failed");
313 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
315 fatal_perror("check_ptrace : wait failed");
317 if(!WIFSTOPPED(status
) ||
318 (WSTOPSIG(status
) != (SIGTRAP
| 0x80)))
319 fatal("check_ptrace : expected (SIGTRAP|0x80), "
320 "got status = %d", status
);
322 syscall
= ptrace(PTRACE_PEEKUSR
, pid
, PT_SYSCALL_NR_OFFSET
,
324 if(syscall
== __NR_getpid
){
325 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_NR_OFFSET
,
328 fatal_perror("check_ptrace : failed to modify "
333 stop_ptraced_child(pid
, stack
, 0, 1);
338 extern void check_tmpexec(void);
340 static void __init
check_coredump_limit(void)
343 int err
= getrlimit(RLIMIT_CORE
, &lim
);
346 perror("Getting core dump limit");
350 printf("Core dump limits :\n\tsoft - ");
351 if(lim
.rlim_cur
== RLIM_INFINITY
)
353 else printf("%lu\n", lim
.rlim_cur
);
356 if(lim
.rlim_max
== RLIM_INFINITY
)
358 else printf("%lu\n", lim
.rlim_max
);
361 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
374 static int __init
noprocmm_cmd_param(char *str
, int* add
)
380 __uml_setup("noprocmm", noprocmm_cmd_param
,
382 " Turns off usage of /proc/mm, even if host supports it.\n"
383 " To support /proc/mm, the host needs to be patched using\n"
384 " the current skas3 patch.\n\n");
386 static int __init
noptracefaultinfo_cmd_param(char *str
, int* add
)
388 ptrace_faultinfo
= 0;
392 __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param
,
393 "noptracefaultinfo\n"
394 " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
395 " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
396 " using the current skas3 patch.\n\n");
398 static int __init
noptraceldt_cmd_param(char *str
, int* add
)
404 __uml_setup("noptraceldt", noptraceldt_cmd_param
,
406 " Turns off usage of PTRACE_LDT, even if host supports it.\n"
407 " To support PTRACE_LDT, the host needs to be patched using\n"
408 " the current skas3 patch.\n\n");
410 #ifdef UML_CONFIG_MODE_SKAS
411 static inline void check_skas3_ptrace_faultinfo(void)
413 struct ptrace_faultinfo fi
;
417 non_fatal(" - PTRACE_FAULTINFO...");
418 pid
= start_ptraced_child(&stack
);
420 n
= ptrace(PTRACE_FAULTINFO
, pid
, 0, &fi
);
422 ptrace_faultinfo
= 0;
424 non_fatal("not found\n");
429 if (!ptrace_faultinfo
)
430 non_fatal("found but disabled on command line\n");
432 non_fatal("found\n");
436 stop_ptraced_child(pid
, stack
, 1, 1);
439 static inline void check_skas3_ptrace_ldt(void)
444 unsigned char ldtbuf
[40];
445 struct ptrace_ldt ldt_op
= (struct ptrace_ldt
) {
446 .func
= 2, /* read default ldt */
448 .bytecount
= sizeof(ldtbuf
)};
450 non_fatal(" - PTRACE_LDT...");
451 pid
= start_ptraced_child(&stack
);
453 n
= ptrace(PTRACE_LDT
, pid
, 0, (unsigned long) &ldt_op
);
456 non_fatal("not found\n");
464 non_fatal("found\n");
466 non_fatal("found, but use is disabled\n");
469 stop_ptraced_child(pid
, stack
, 1, 1);
471 /* PTRACE_LDT might be disabled via cmdline option.
472 * We want to override this, else we might use the stub
479 static inline void check_skas3_proc_mm(void)
481 non_fatal(" - /proc/mm...");
482 if (access("/proc/mm", W_OK
) < 0) {
488 non_fatal("found but disabled on command line\n");
490 non_fatal("found\n");
494 int can_do_skas(void)
496 non_fatal("Checking for the skas3 patch in the host:\n");
498 check_skas3_proc_mm();
499 check_skas3_ptrace_faultinfo();
500 check_skas3_ptrace_ldt();
502 if(!proc_mm
|| !ptrace_faultinfo
|| !ptrace_ldt
)
508 int can_do_skas(void)
514 int __init
parse_iomem(char *str
, int *add
)
516 struct iomem_region
*new;
522 file
= strchr(str
,',');
524 printf("parse_iomem : failed to parse iomem\n");
529 fd
= open(file
, O_RDWR
, 0);
531 os_print_error(fd
, "parse_iomem - Couldn't open io file");
535 if(fstat64(fd
, &buf
) < 0){
536 perror("parse_iomem - cannot stat_fd file");
540 new = malloc(sizeof(*new));
542 perror("Couldn't allocate iomem_region struct");
546 size
= (buf
.st_size
+ UM_KERN_PAGE_SIZE
) & ~(UM_KERN_PAGE_SIZE
- 1);
548 *new = ((struct iomem_region
) { .next
= iomem_regions
,
555 iomem_size
+= new->size
+ UM_KERN_PAGE_SIZE
;