Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / arch / um / os-Linux / start_up.c
blobb616e15638fbc8c1aa5a8bdf710862715124de42
1 /*
2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdarg.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <sched.h>
13 #include <signal.h>
14 #include <string.h>
15 #include <sys/mman.h>
16 #include <sys/ptrace.h>
17 #include <sys/stat.h>
18 #include <sys/wait.h>
19 #include <asm/unistd.h>
20 #include "init.h"
21 #include "kern_constants.h"
22 #include "os.h"
23 #include "mem_user.h"
24 #include "ptrace_user.h"
25 #include "registers.h"
26 #include "skas_ptrace.h"
28 static int ptrace_child(void)
30 int ret;
31 /* Calling os_getpid because some libcs cached getpid incorrectly */
32 int pid = os_getpid(), ppid = getppid();
33 int sc_result;
35 change_sig(SIGWINCH, 0);
36 if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
37 perror("ptrace");
38 kill(pid, SIGKILL);
40 kill(pid, SIGSTOP);
43 * This syscall will be intercepted by the parent. Don't call more than
44 * once, please.
46 sc_result = os_getpid();
48 if (sc_result == pid)
49 /* Nothing modified by the parent, we are running normally. */
50 ret = 1;
51 else if (sc_result == ppid)
53 * Expected in check_ptrace and check_sysemu when they succeed
54 * in modifying the stack frame
56 ret = 0;
57 else
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.
62 ret = 2;
64 exit(ret);
67 static void fatal_perror(const char *str)
69 perror(str);
70 exit(1);
73 static void fatal(char *fmt, ...)
75 va_list list;
77 va_start(list, fmt);
78 vprintf(fmt, list);
79 va_end(list);
80 fflush(stdout);
82 exit(1);
85 static void non_fatal(char *fmt, ...)
87 va_list list;
89 va_start(list, fmt);
90 vprintf(fmt, list);
91 va_end(list);
92 fflush(stdout);
95 static int start_ptraced_child(void)
97 int pid, n, status;
99 pid = fork();
100 if (pid == 0)
101 ptrace_child();
102 else if (pid < 0)
103 fatal_perror("start_ptraced_child : fork failed");
105 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
106 if (n < 0)
107 fatal_perror("check_ptrace : waitpid failed");
108 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
109 fatal("check_ptrace : expected SIGSTOP, got status = %d",
110 status);
112 return pid;
115 /* When testing for SYSEMU support, if it is one of the broken versions, we
116 * must just avoid using sysemu, not panic, but only if SYSEMU features are
117 * broken.
118 * So only for SYSEMU features we test mustpanic, while normal host features
119 * must work anyway!
121 static int stop_ptraced_child(int pid, int exitcode, int mustexit)
123 int status, n, ret = 0;
125 if (ptrace(PTRACE_CONT, pid, 0, 0) < 0)
126 fatal_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);
130 if (exit_with == 2)
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,
135 exitcode, status);
136 if (mustexit)
137 exit(1);
138 ret = -1;
141 return ret;
144 /* Changed only during early boot */
145 int ptrace_faultinfo = 1;
146 int ptrace_ldt = 1;
147 int proc_mm = 1;
148 int skas_needs_stub = 0;
150 static int __init skas0_cmd_param(char *str, int* add)
152 ptrace_faultinfo = proc_mm = 0;
153 return 0;
156 /* The two __uml_setup would conflict, without this stupid alias. */
158 static int __init mode_skas0_cmd_param(char *str, int* add)
159 __attribute__((alias("skas0_cmd_param")));
161 __uml_setup("skas0", skas0_cmd_param,
162 "skas0\n"
163 " Disables SKAS3 usage, so that SKAS0 is used, unless \n"
164 " you specify mode=tt.\n\n");
166 __uml_setup("mode=skas0", mode_skas0_cmd_param,
167 "mode=skas0\n"
168 " Disables SKAS3 usage, so that SKAS0 is used, unless you \n"
169 " specify mode=tt. Note that this was recently added - on \n"
170 " older kernels you must use simply \"skas0\".\n\n");
172 /* Changed only during early boot */
173 static int force_sysemu_disabled = 0;
175 static int __init nosysemu_cmd_param(char *str, int* add)
177 force_sysemu_disabled = 1;
178 return 0;
181 __uml_setup("nosysemu", nosysemu_cmd_param,
182 "nosysemu\n"
183 " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
184 " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
185 " behaviour of ptrace() and helps reducing host context switch rate.\n"
186 " To make it working, you need a kernel patch for your host, too.\n"
187 " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
188 " information.\n\n");
190 static void __init check_sysemu(void)
192 unsigned long regs[MAX_REG_NR];
193 int pid, n, status, count=0;
195 non_fatal("Checking syscall emulation patch for ptrace...");
196 sysemu_supported = 0;
197 pid = start_ptraced_child();
199 if (ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
200 goto fail;
202 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
203 if (n < 0)
204 fatal_perror("check_sysemu : wait failed");
205 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
206 fatal("check_sysemu : expected SIGTRAP, got status = %d",
207 status);
209 if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
210 fatal_perror("check_sysemu : PTRACE_GETREGS failed");
211 if (PT_SYSCALL_NR(regs) != __NR_getpid) {
212 non_fatal("check_sysemu got system call number %d, "
213 "expected %d...", PT_SYSCALL_NR(regs), __NR_getpid);
214 goto fail;
217 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET, os_getpid());
218 if (n < 0) {
219 non_fatal("check_sysemu : failed to modify system call "
220 "return");
221 goto fail;
224 if (stop_ptraced_child(pid, 0, 0) < 0)
225 goto fail_stopped;
227 sysemu_supported = 1;
228 non_fatal("OK\n");
229 set_using_sysemu(!force_sysemu_disabled);
231 non_fatal("Checking advanced syscall emulation patch for ptrace...");
232 pid = start_ptraced_child();
234 if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
235 (void *) PTRACE_O_TRACESYSGOOD) < 0))
236 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
238 while (1) {
239 count++;
240 if (ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
241 goto fail;
242 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
243 if (n < 0)
244 fatal_perror("check_ptrace : wait failed");
246 if (WIFSTOPPED(status) &&
247 (WSTOPSIG(status) == (SIGTRAP|0x80))) {
248 if (!count)
249 fatal("check_ptrace : SYSEMU_SINGLESTEP "
250 "doesn't singlestep");
251 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
252 os_getpid());
253 if (n < 0)
254 fatal_perror("check_sysemu : failed to modify "
255 "system call return");
256 break;
258 else if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
259 count++;
260 else
261 fatal("check_ptrace : expected SIGTRAP or "
262 "(SIGTRAP | 0x80), got status = %d", status);
264 if (stop_ptraced_child(pid, 0, 0) < 0)
265 goto fail_stopped;
267 sysemu_supported = 2;
268 non_fatal("OK\n");
270 if (!force_sysemu_disabled)
271 set_using_sysemu(sysemu_supported);
272 return;
274 fail:
275 stop_ptraced_child(pid, 1, 0);
276 fail_stopped:
277 non_fatal("missing\n");
280 static void __init check_ptrace(void)
282 int pid, syscall, n, status;
284 non_fatal("Checking that ptrace can change system call numbers...");
285 pid = start_ptraced_child();
287 if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
288 (void *) PTRACE_O_TRACESYSGOOD) < 0))
289 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
291 while (1) {
292 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
293 fatal_perror("check_ptrace : ptrace failed");
295 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
296 if (n < 0)
297 fatal_perror("check_ptrace : wait failed");
299 if (!WIFSTOPPED(status) ||
300 (WSTOPSIG(status) != (SIGTRAP | 0x80)))
301 fatal("check_ptrace : expected (SIGTRAP|0x80), "
302 "got status = %d", status);
304 syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
306 if (syscall == __NR_getpid) {
307 n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
308 __NR_getppid);
309 if (n < 0)
310 fatal_perror("check_ptrace : failed to modify "
311 "system call");
312 break;
315 stop_ptraced_child(pid, 0, 1);
316 non_fatal("OK\n");
317 check_sysemu();
320 extern void check_tmpexec(void);
322 static void __init check_coredump_limit(void)
324 struct rlimit lim;
325 int err = getrlimit(RLIMIT_CORE, &lim);
327 if (err) {
328 perror("Getting core dump limit");
329 return;
332 printf("Core dump limits :\n\tsoft - ");
333 if (lim.rlim_cur == RLIM_INFINITY)
334 printf("NONE\n");
335 else printf("%lu\n", lim.rlim_cur);
337 printf("\thard - ");
338 if (lim.rlim_max == RLIM_INFINITY)
339 printf("NONE\n");
340 else printf("%lu\n", lim.rlim_max);
343 void __init os_early_checks(void)
345 int pid;
347 /* Print out the core dump limits early */
348 check_coredump_limit();
350 check_ptrace();
352 /* Need to check this early because mmapping happens before the
353 * kernel is running.
355 check_tmpexec();
357 pid = start_ptraced_child();
358 if (init_registers(pid))
359 fatal("Failed to initialize default registers");
360 stop_ptraced_child(pid, 1, 1);
363 static int __init noprocmm_cmd_param(char *str, int* add)
365 proc_mm = 0;
366 return 0;
369 __uml_setup("noprocmm", noprocmm_cmd_param,
370 "noprocmm\n"
371 " Turns off usage of /proc/mm, even if host supports it.\n"
372 " To support /proc/mm, the host needs to be patched using\n"
373 " the current skas3 patch.\n\n");
375 static int __init noptracefaultinfo_cmd_param(char *str, int* add)
377 ptrace_faultinfo = 0;
378 return 0;
381 __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
382 "noptracefaultinfo\n"
383 " Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
384 " it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
385 " using the current skas3 patch.\n\n");
387 static int __init noptraceldt_cmd_param(char *str, int* add)
389 ptrace_ldt = 0;
390 return 0;
393 __uml_setup("noptraceldt", noptraceldt_cmd_param,
394 "noptraceldt\n"
395 " Turns off usage of PTRACE_LDT, even if host supports it.\n"
396 " To support PTRACE_LDT, the host needs to be patched using\n"
397 " the current skas3 patch.\n\n");
399 static inline void check_skas3_ptrace_faultinfo(void)
401 struct ptrace_faultinfo fi;
402 int pid, n;
404 non_fatal(" - PTRACE_FAULTINFO...");
405 pid = start_ptraced_child();
407 n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
408 if (n < 0) {
409 ptrace_faultinfo = 0;
410 if (errno == EIO)
411 non_fatal("not found\n");
412 else
413 perror("not found");
415 else {
416 if (!ptrace_faultinfo)
417 non_fatal("found but disabled on command line\n");
418 else
419 non_fatal("found\n");
422 stop_ptraced_child(pid, 1, 1);
425 static inline void check_skas3_ptrace_ldt(void)
427 #ifdef PTRACE_LDT
428 int pid, n;
429 unsigned char ldtbuf[40];
430 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
431 .func = 2, /* read default ldt */
432 .ptr = ldtbuf,
433 .bytecount = sizeof(ldtbuf)};
435 non_fatal(" - PTRACE_LDT...");
436 pid = start_ptraced_child();
438 n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
439 if (n < 0) {
440 if (errno == EIO)
441 non_fatal("not found\n");
442 else {
443 perror("not found");
445 ptrace_ldt = 0;
447 else {
448 if (ptrace_ldt)
449 non_fatal("found\n");
450 else
451 non_fatal("found, but use is disabled\n");
454 stop_ptraced_child(pid, 1, 1);
455 #else
456 /* PTRACE_LDT might be disabled via cmdline option.
457 * We want to override this, else we might use the stub
458 * without real need
460 ptrace_ldt = 1;
461 #endif
464 static inline void check_skas3_proc_mm(void)
466 non_fatal(" - /proc/mm...");
467 if (access("/proc/mm", W_OK) < 0) {
468 proc_mm = 0;
469 perror("not found");
471 else if (!proc_mm)
472 non_fatal("found but disabled on command line\n");
473 else non_fatal("found\n");
476 void can_do_skas(void)
478 non_fatal("Checking for the skas3 patch in the host:\n");
480 check_skas3_proc_mm();
481 check_skas3_ptrace_faultinfo();
482 check_skas3_ptrace_ldt();
484 if (!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
485 skas_needs_stub = 1;
488 int __init parse_iomem(char *str, int *add)
490 struct iomem_region *new;
491 struct stat64 buf;
492 char *file, *driver;
493 int fd, size;
495 driver = str;
496 file = strchr(str,',');
497 if (file == NULL) {
498 printf("parse_iomem : failed to parse iomem\n");
499 goto out;
501 *file = '\0';
502 file++;
503 fd = open(file, O_RDWR, 0);
504 if (fd < 0) {
505 perror("parse_iomem - Couldn't open io file");
506 goto out;
509 if (fstat64(fd, &buf) < 0) {
510 perror("parse_iomem - cannot stat_fd file");
511 goto out_close;
514 new = malloc(sizeof(*new));
515 if (new == NULL) {
516 perror("Couldn't allocate iomem_region struct");
517 goto out_close;
520 size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
522 *new = ((struct iomem_region) { .next = iomem_regions,
523 .driver = driver,
524 .fd = fd,
525 .size = size,
526 .phys = 0,
527 .virt = 0 });
528 iomem_regions = new;
529 iomem_size += new->size + UM_KERN_PAGE_SIZE;
531 return 0;
532 out_close:
533 close(fd);
534 out:
535 return 1;