2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
19 #include <sys/resource.h>
20 #include <asm/unistd.h>
24 #include <ptrace_user.h>
25 #include <registers.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 perror("stop_ptraced_child : ptrace failed");
127 CATCH_EINTR(n
= waitpid(pid
, &status
, 0));
128 if (!WIFEXITED(status
) || (WEXITSTATUS(status
) != exitcode
)) {
129 int exit_with
= WEXITSTATUS(status
);
131 non_fatal("check_ptrace : child exited with status 2. "
132 "\nDisabling SYSEMU support.\n");
133 non_fatal("check_ptrace : child exited with exitcode %d, while "
134 "expecting %d; status 0x%x\n", exit_with
,
144 /* Changed only during early boot */
145 static int force_sysemu_disabled
= 0;
147 static int __init
nosysemu_cmd_param(char *str
, int* add
)
149 force_sysemu_disabled
= 1;
153 __uml_setup("nosysemu", nosysemu_cmd_param
,
155 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
156 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
157 " behaviour of ptrace() and helps reducing host context switch rate.\n"
158 " To make it working, you need a kernel patch for your host, too.\n"
159 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
160 " information.\n\n");
162 static void __init
check_sysemu(void)
164 unsigned long regs
[MAX_REG_NR
];
165 int pid
, n
, status
, count
=0;
167 non_fatal("Checking syscall emulation patch for ptrace...");
168 sysemu_supported
= 0;
169 pid
= start_ptraced_child();
171 if (ptrace(PTRACE_SYSEMU
, pid
, 0, 0) < 0)
174 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
176 fatal_perror("check_sysemu : wait failed");
177 if (!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGTRAP
))
178 fatal("check_sysemu : expected SIGTRAP, got status = %d\n",
181 if (ptrace(PTRACE_GETREGS
, pid
, 0, regs
) < 0)
182 fatal_perror("check_sysemu : PTRACE_GETREGS failed");
183 if (PT_SYSCALL_NR(regs
) != __NR_getpid
) {
184 non_fatal("check_sysemu got system call number %d, "
185 "expected %d...", PT_SYSCALL_NR(regs
), __NR_getpid
);
189 n
= ptrace(PTRACE_POKEUSER
, pid
, PT_SYSCALL_RET_OFFSET
, os_getpid());
191 non_fatal("check_sysemu : failed to modify system call "
196 if (stop_ptraced_child(pid
, 0, 0) < 0)
199 sysemu_supported
= 1;
201 set_using_sysemu(!force_sysemu_disabled
);
203 non_fatal("Checking advanced syscall emulation patch for ptrace...");
204 pid
= start_ptraced_child();
206 if ((ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0,
207 (void *) PTRACE_O_TRACESYSGOOD
) < 0))
208 fatal_perror("check_sysemu: PTRACE_OLDSETOPTIONS failed");
212 if (ptrace(PTRACE_SYSEMU_SINGLESTEP
, pid
, 0, 0) < 0)
214 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
216 fatal_perror("check_sysemu: wait failed");
218 if (WIFSTOPPED(status
) &&
219 (WSTOPSIG(status
) == (SIGTRAP
|0x80))) {
221 non_fatal("check_sysemu: SYSEMU_SINGLESTEP "
222 "doesn't singlestep");
225 n
= ptrace(PTRACE_POKEUSER
, pid
, PT_SYSCALL_RET_OFFSET
,
228 fatal_perror("check_sysemu : failed to modify "
229 "system call return");
232 else if (WIFSTOPPED(status
) && (WSTOPSIG(status
) == SIGTRAP
))
235 non_fatal("check_sysemu: expected SIGTRAP or "
236 "(SIGTRAP | 0x80), got status = %d\n",
241 if (stop_ptraced_child(pid
, 0, 0) < 0)
244 sysemu_supported
= 2;
247 if (!force_sysemu_disabled
)
248 set_using_sysemu(sysemu_supported
);
252 stop_ptraced_child(pid
, 1, 0);
254 non_fatal("missing\n");
257 static void __init
check_ptrace(void)
259 int pid
, syscall
, n
, status
;
261 non_fatal("Checking that ptrace can change system call numbers...");
262 pid
= start_ptraced_child();
264 if ((ptrace(PTRACE_OLDSETOPTIONS
, pid
, 0,
265 (void *) PTRACE_O_TRACESYSGOOD
) < 0))
266 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
269 if (ptrace(PTRACE_SYSCALL
, pid
, 0, 0) < 0)
270 fatal_perror("check_ptrace : ptrace failed");
272 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
274 fatal_perror("check_ptrace : wait failed");
276 if (!WIFSTOPPED(status
) ||
277 (WSTOPSIG(status
) != (SIGTRAP
| 0x80)))
278 fatal("check_ptrace : expected (SIGTRAP|0x80), "
279 "got status = %d", status
);
281 syscall
= ptrace(PTRACE_PEEKUSER
, pid
, PT_SYSCALL_NR_OFFSET
,
283 if (syscall
== __NR_getpid
) {
284 n
= ptrace(PTRACE_POKEUSER
, pid
, PT_SYSCALL_NR_OFFSET
,
287 fatal_perror("check_ptrace : failed to modify "
292 stop_ptraced_child(pid
, 0, 1);
297 extern void check_tmpexec(void);
299 static void __init
check_coredump_limit(void)
302 int err
= getrlimit(RLIMIT_CORE
, &lim
);
305 perror("Getting core dump limit");
309 printf("Core dump limits :\n\tsoft - ");
310 if (lim
.rlim_cur
== RLIM_INFINITY
)
312 else printf("%lu\n", lim
.rlim_cur
);
315 if (lim
.rlim_max
== RLIM_INFINITY
)
317 else printf("%lu\n", lim
.rlim_max
);
320 void __init
os_early_checks(void)
324 /* Print out the core dump limits early */
325 check_coredump_limit();
329 /* Need to check this early because mmapping happens before the
334 pid
= start_ptraced_child();
335 if (init_registers(pid
))
336 fatal("Failed to initialize default registers");
337 stop_ptraced_child(pid
, 1, 1);
340 int __init
parse_iomem(char *str
, int *add
)
342 struct iomem_region
*new;
348 file
= strchr(str
,',');
350 fprintf(stderr
, "parse_iomem : failed to parse iomem\n");
355 fd
= open(file
, O_RDWR
, 0);
357 perror("parse_iomem - Couldn't open io file");
361 if (fstat64(fd
, &buf
) < 0) {
362 perror("parse_iomem - cannot stat_fd file");
366 new = malloc(sizeof(*new));
368 perror("Couldn't allocate iomem_region struct");
372 size
= (buf
.st_size
+ UM_KERN_PAGE_SIZE
) & ~(UM_KERN_PAGE_SIZE
- 1);
374 *new = ((struct iomem_region
) { .next
= iomem_regions
,
381 iomem_size
+= new->size
+ UM_KERN_PAGE_SIZE
;