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>
23 #include <sys/types.h>
24 #include "kern_util.h"
26 #include "signal_kern.h"
27 #include "sysdep/ptrace.h"
28 #include "sysdep/sigcontext.h"
30 #include "ptrace_user.h"
34 #include "uml-config.h"
35 #include "choose-mode.h"
38 #include "kern_constants.h"
40 #ifdef UML_CONFIG_MODE_SKAS
42 #include "skas_ptrace.h"
43 #include "registers.h"
46 static int ptrace_child(void *arg
)
49 int pid
= os_getpid(), ppid
= getppid();
52 change_sig(SIGWINCH
, 0);
53 if(ptrace(PTRACE_TRACEME
, 0, 0, 0) < 0){
55 os_kill_process(pid
, 0);
59 /*This syscall will be intercepted by the parent. Don't call more than
61 sc_result
= os_getpid();
64 ret
= 1; /*Nothing modified by the parent, we are running
66 else if (sc_result
== ppid
)
67 ret
= 0; /*Expected in check_ptrace and check_sysemu when they
68 succeed in modifying the stack frame*/
70 ret
= 2; /*Serious trouble! This could be caused by a bug in
71 host 2.6 SKAS3/2.6 patch before release -V6, together
72 with a bug in the UML code itself.*/
76 static void fatal_perror(char *str
)
82 static void fatal(char *fmt
, ...)
94 static void non_fatal(char *fmt
, ...)
104 static int start_ptraced_child(void **stack_out
)
110 stack
= mmap(NULL
, UM_KERN_PAGE_SIZE
,
111 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
112 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
113 if(stack
== MAP_FAILED
)
114 fatal_perror("check_ptrace : mmap failed");
115 sp
= (unsigned long) stack
+ UM_KERN_PAGE_SIZE
- sizeof(void *);
116 pid
= clone(ptrace_child
, (void *) sp
, SIGCHLD
, NULL
);
118 fatal_perror("start_ptraced_child : clone failed");
119 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
121 fatal_perror("check_ptrace : clone failed");
122 if(!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGSTOP
))
123 fatal("check_ptrace : expected SIGSTOP, got status = %d",
130 /* When testing for SYSEMU support, if it is one of the broken versions, we
131 * must just avoid using sysemu, not panic, but only if SYSEMU features are
133 * So only for SYSEMU features we test mustpanic, while normal host features
136 static int stop_ptraced_child(int pid
, void *stack
, int exitcode
,
139 int status
, n
, ret
= 0;
141 if(ptrace(PTRACE_CONT
, pid
, 0, 0) < 0)
142 fatal_perror("stop_ptraced_child : ptrace failed");
143 CATCH_EINTR(n
= waitpid(pid
, &status
, 0));
144 if(!WIFEXITED(status
) || (WEXITSTATUS(status
) != exitcode
)) {
145 int exit_with
= WEXITSTATUS(status
);
147 non_fatal("check_ptrace : child exited with status 2. "
148 "\nDisabling SYSEMU support.\n");
149 non_fatal("check_ptrace : child exited with exitcode %d, while "
150 "expecting %d; status 0x%x\n", exit_with
,
157 if(munmap(stack
, UM_KERN_PAGE_SIZE
) < 0)
158 fatal_perror("check_ptrace : munmap failed");
162 /* Changed only during early boot */
163 int ptrace_faultinfo
= 1;
166 int skas_needs_stub
= 0;
168 static int __init
skas0_cmd_param(char *str
, int* add
)
170 ptrace_faultinfo
= proc_mm
= 0;
174 /* The two __uml_setup would conflict, without this stupid alias. */
176 static int __init
mode_skas0_cmd_param(char *str
, int* add
)
177 __attribute__((alias("skas0_cmd_param")));
179 __uml_setup("skas0", skas0_cmd_param
,
181 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
182 " you specify mode=tt.\n\n");
184 __uml_setup("mode=skas0", mode_skas0_cmd_param
,
186 " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
187 " specify mode=tt. Note that this was recently added - on \n"
188 " older kernels you must use simply \"skas0\".\n\n");
190 /* Changed only during early boot */
191 static int force_sysemu_disabled
= 0;
193 static int __init
nosysemu_cmd_param(char *str
, int* add
)
195 force_sysemu_disabled
= 1;
199 __uml_setup("nosysemu", nosysemu_cmd_param
,
201 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
202 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
203 " behaviour of ptrace() and helps reducing host context switch rate.\n"
204 " To make it working, you need a kernel patch for your host, too.\n"
205 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
206 " information.\n\n");
208 static void __init
check_sysemu(void)
211 unsigned long regs
[MAX_REG_NR
];
212 int pid
, n
, status
, count
=0;
214 non_fatal("Checking syscall emulation patch for ptrace...");
215 sysemu_supported
= 0;
216 pid
= start_ptraced_child(&stack
);
218 if(ptrace(PTRACE_SYSEMU
, pid
, 0, 0) < 0)
221 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
223 fatal_perror("check_sysemu : wait failed");
224 if(!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGTRAP
))
225 fatal("check_sysemu : expected SIGTRAP, got status = %d",
228 if(ptrace(PTRACE_GETREGS
, pid
, 0, regs
) < 0)
229 fatal_perror("check_sysemu : PTRACE_GETREGS failed");
230 if(PT_SYSCALL_NR(regs
) != __NR_getpid
){
231 non_fatal("check_sysemu got system call number %d, "
232 "expected %d...", PT_SYSCALL_NR(regs
), __NR_getpid
);
236 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_RET_OFFSET
, os_getpid());
238 non_fatal("check_sysemu : failed to modify system call "
243 if (stop_ptraced_child(pid
, stack
, 0, 0) < 0)
246 sysemu_supported
= 1;
248 set_using_sysemu(!force_sysemu_disabled
);
250 non_fatal("Checking advanced syscall emulation patch for ptrace...");
251 pid
= start_ptraced_child(&stack
);
253 if((ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0,
254 (void *) PTRACE_O_TRACESYSGOOD
) < 0))
255 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
259 if(ptrace(PTRACE_SYSEMU_SINGLESTEP
, pid
, 0, 0) < 0)
261 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
263 fatal_perror("check_ptrace : wait failed");
265 if(WIFSTOPPED(status
) && (WSTOPSIG(status
) == (SIGTRAP
|0x80))){
267 fatal("check_ptrace : SYSEMU_SINGLESTEP "
268 "doesn't singlestep");
269 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_RET_OFFSET
,
272 fatal_perror("check_sysemu : failed to modify "
273 "system call return");
276 else if(WIFSTOPPED(status
) && (WSTOPSIG(status
) == SIGTRAP
))
279 fatal("check_ptrace : expected SIGTRAP or "
280 "(SIGTRAP | 0x80), got status = %d", status
);
282 if (stop_ptraced_child(pid
, stack
, 0, 0) < 0)
285 sysemu_supported
= 2;
288 if ( !force_sysemu_disabled
)
289 set_using_sysemu(sysemu_supported
);
293 stop_ptraced_child(pid
, stack
, 1, 0);
295 non_fatal("missing\n");
298 static void __init
check_ptrace(void)
301 int pid
, syscall
, n
, status
;
303 non_fatal("Checking that ptrace can change system call numbers...");
304 pid
= start_ptraced_child(&stack
);
306 if((ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0,
307 (void *) PTRACE_O_TRACESYSGOOD
) < 0))
308 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
311 if(ptrace(PTRACE_SYSCALL
, pid
, 0, 0) < 0)
312 fatal_perror("check_ptrace : ptrace failed");
314 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
316 fatal_perror("check_ptrace : wait failed");
318 if(!WIFSTOPPED(status
) ||
319 (WSTOPSIG(status
) != (SIGTRAP
| 0x80)))
320 fatal("check_ptrace : expected (SIGTRAP|0x80), "
321 "got status = %d", status
);
323 syscall
= ptrace(PTRACE_PEEKUSR
, pid
, PT_SYSCALL_NR_OFFSET
,
325 if(syscall
== __NR_getpid
){
326 n
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_NR_OFFSET
,
329 fatal_perror("check_ptrace : failed to modify "
334 stop_ptraced_child(pid
, stack
, 0, 1);
339 extern void check_tmpexec(void);
341 static void __init
check_coredump_limit(void)
344 int err
= getrlimit(RLIMIT_CORE
, &lim
);
347 perror("Getting core dump limit");
351 printf("Core dump limits :\n\tsoft - ");
352 if(lim
.rlim_cur
== RLIM_INFINITY
)
354 else printf("%lu\n", lim
.rlim_cur
);
357 if(lim
.rlim_max
== RLIM_INFINITY
)
359 else printf("%lu\n", lim
.rlim_max
);
362 void __init
os_early_checks(void)
364 /* Print out the core dump limits early */
365 check_coredump_limit();
369 /* Need to check this early because mmapping happens before the
375 static int __init
noprocmm_cmd_param(char *str
, int* add
)
381 __uml_setup("noprocmm", noprocmm_cmd_param
,
383 " Turns off usage of /proc/mm, even if host supports it.\n"
384 " To support /proc/mm, the host needs to be patched using\n"
385 " the current skas3 patch.\n\n");
387 static int __init
noptracefaultinfo_cmd_param(char *str
, int* add
)
389 ptrace_faultinfo
= 0;
393 __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param
,
394 "noptracefaultinfo\n"
395 " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
396 " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
397 " using the current skas3 patch.\n\n");
399 static int __init
noptraceldt_cmd_param(char *str
, int* add
)
405 __uml_setup("noptraceldt", noptraceldt_cmd_param
,
407 " Turns off usage of PTRACE_LDT, even if host supports it.\n"
408 " To support PTRACE_LDT, the host needs to be patched using\n"
409 " the current skas3 patch.\n\n");
411 #ifdef UML_CONFIG_MODE_SKAS
412 static inline void check_skas3_ptrace_faultinfo(void)
414 struct ptrace_faultinfo fi
;
418 non_fatal(" - PTRACE_FAULTINFO...");
419 pid
= start_ptraced_child(&stack
);
421 n
= ptrace(PTRACE_FAULTINFO
, pid
, 0, &fi
);
423 ptrace_faultinfo
= 0;
425 non_fatal("not found\n");
430 if (!ptrace_faultinfo
)
431 non_fatal("found but disabled on command line\n");
433 non_fatal("found\n");
437 stop_ptraced_child(pid
, stack
, 1, 1);
440 static inline void check_skas3_ptrace_ldt(void)
445 unsigned char ldtbuf
[40];
446 struct ptrace_ldt ldt_op
= (struct ptrace_ldt
) {
447 .func
= 2, /* read default ldt */
449 .bytecount
= sizeof(ldtbuf
)};
451 non_fatal(" - PTRACE_LDT...");
452 pid
= start_ptraced_child(&stack
);
454 n
= ptrace(PTRACE_LDT
, pid
, 0, (unsigned long) &ldt_op
);
457 non_fatal("not found\n");
465 non_fatal("found\n");
467 non_fatal("found, but use is disabled\n");
470 stop_ptraced_child(pid
, stack
, 1, 1);
472 /* PTRACE_LDT might be disabled via cmdline option.
473 * We want to override this, else we might use the stub
480 static inline void check_skas3_proc_mm(void)
482 non_fatal(" - /proc/mm...");
483 if (access("/proc/mm", W_OK
) < 0) {
489 non_fatal("found but disabled on command line\n");
491 non_fatal("found\n");
495 int can_do_skas(void)
497 non_fatal("Checking for the skas3 patch in the host:\n");
499 check_skas3_proc_mm();
500 check_skas3_ptrace_faultinfo();
501 check_skas3_ptrace_ldt();
503 if(!proc_mm
|| !ptrace_faultinfo
|| !ptrace_ldt
)
509 int can_do_skas(void)
515 int __init
parse_iomem(char *str
, int *add
)
517 struct iomem_region
*new;
523 file
= strchr(str
,',');
525 printf("parse_iomem : failed to parse iomem\n");
530 fd
= open(file
, O_RDWR
, 0);
532 os_print_error(fd
, "parse_iomem - Couldn't open io file");
536 if(fstat64(fd
, &buf
) < 0){
537 perror("parse_iomem - cannot stat_fd file");
541 new = malloc(sizeof(*new));
543 perror("Couldn't allocate iomem_region struct");
547 size
= (buf
.st_size
+ UM_KERN_PAGE_SIZE
) & ~(UM_KERN_PAGE_SIZE
- 1);
549 *new = ((struct iomem_region
) { .next
= iomem_regions
,
556 iomem_size
+= new->size
+ UM_KERN_PAGE_SIZE
;