Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 1 | /* |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 2 | * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #include <stdio.h> |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 7 | #include <stdlib.h> |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 8 | #include <stdarg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <unistd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <errno.h> |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 11 | #include <fcntl.h> |
| 12 | #include <sched.h> |
| 13 | #include <signal.h> |
| 14 | #include <string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <sys/mman.h> |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 16 | #include <sys/stat.h> |
| 17 | #include <sys/wait.h> |
Sergei Trofimovich | fdfa4c9 | 2012-12-30 01:37:30 +0300 | [diff] [blame] | 18 | #include <sys/time.h> |
| 19 | #include <sys/resource.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/unistd.h> |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 21 | #include <init.h> |
| 22 | #include <os.h> |
| 23 | #include <mem_user.h> |
| 24 | #include <ptrace_user.h> |
| 25 | #include <registers.h> |
| 26 | #include <skas.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
WANG Cong | 626c59f | 2008-04-28 02:13:53 -0700 | [diff] [blame] | 28 | static void ptrace_child(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | { |
| 30 | int ret; |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 31 | /* Calling os_getpid because some libcs cached getpid incorrectly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | int pid = os_getpid(), ppid = getppid(); |
| 33 | int sc_result; |
| 34 | |
WANG Cong | 626c59f | 2008-04-28 02:13:53 -0700 | [diff] [blame] | 35 | if (change_sig(SIGWINCH, 0) < 0 || |
| 36 | ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | perror("ptrace"); |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 38 | kill(pid, SIGKILL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
Jeff Dike | 73c8f444 | 2007-02-10 01:44:20 -0800 | [diff] [blame] | 40 | kill(pid, SIGSTOP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 42 | /* |
| 43 | * This syscall will be intercepted by the parent. Don't call more than |
| 44 | * once, please. |
| 45 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | sc_result = os_getpid(); |
| 47 | |
| 48 | if (sc_result == pid) |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 49 | /* Nothing modified by the parent, we are running normally. */ |
| 50 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | else if (sc_result == ppid) |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 52 | /* |
| 53 | * Expected in check_ptrace and check_sysemu when they succeed |
| 54 | * in modifying the stack frame |
| 55 | */ |
| 56 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | else |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 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. |
| 61 | */ |
| 62 | ret = 2; |
Jeff Dike | bf8fde7 | 2008-02-04 22:31:04 -0800 | [diff] [blame] | 63 | |
| 64 | exit(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
| 66 | |
WANG Cong | c9a3072 | 2008-02-04 22:30:35 -0800 | [diff] [blame] | 67 | static void fatal_perror(const char *str) |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 68 | { |
| 69 | perror(str); |
| 70 | exit(1); |
| 71 | } |
| 72 | |
| 73 | static void fatal(char *fmt, ...) |
| 74 | { |
| 75 | va_list list; |
| 76 | |
| 77 | va_start(list, fmt); |
WANG Cong | 626c59f | 2008-04-28 02:13:53 -0700 | [diff] [blame] | 78 | vfprintf(stderr, fmt, list); |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 79 | va_end(list); |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 80 | |
| 81 | exit(1); |
| 82 | } |
| 83 | |
| 84 | static void non_fatal(char *fmt, ...) |
| 85 | { |
| 86 | va_list list; |
| 87 | |
| 88 | va_start(list, fmt); |
WANG Cong | 626c59f | 2008-04-28 02:13:53 -0700 | [diff] [blame] | 89 | vfprintf(stderr, fmt, list); |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 90 | va_end(list); |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 93 | static int start_ptraced_child(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | int pid, n, status; |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 96 | |
Vegard Nossum | 0754fb2 | 2015-12-18 21:28:53 +0100 | [diff] [blame] | 97 | fflush(stdout); |
| 98 | |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 99 | pid = fork(); |
| 100 | if (pid == 0) |
| 101 | ptrace_child(); |
| 102 | else if (pid < 0) |
| 103 | fatal_perror("start_ptraced_child : fork failed"); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 106 | if (n < 0) |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 107 | fatal_perror("check_ptrace : waitpid failed"); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 108 | if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 109 | fatal("check_ptrace : expected SIGSTOP, got status = %d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | status); |
| 111 | |
Jeff Dike | 9eae9b1 | 2007-02-10 01:44:20 -0800 | [diff] [blame] | 112 | return pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | * So only for SYSEMU features we test mustpanic, while normal host features |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 119 | * must work anyway! |
| 120 | */ |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 121 | static int stop_ptraced_child(int pid, int exitcode, int mustexit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
| 123 | int status, n, ret = 0; |
| 124 | |
Jeff Dike | f1ef916 | 2008-06-12 15:21:41 -0700 | [diff] [blame] | 125 | if (ptrace(PTRACE_CONT, pid, 0, 0) < 0) { |
| 126 | perror("stop_ptraced_child : ptrace failed"); |
| 127 | return -1; |
| 128 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | CATCH_EINTR(n = waitpid(pid, &status, 0)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 130 | if (!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | int exit_with = WEXITSTATUS(status); |
| 132 | if (exit_with == 2) |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 133 | non_fatal("check_ptrace : child exited with status 2. " |
Jeff Dike | cf6aced | 2007-05-23 13:57:40 -0700 | [diff] [blame] | 134 | "\nDisabling SYSEMU support.\n"); |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 135 | non_fatal("check_ptrace : child exited with exitcode %d, while " |
| 136 | "expecting %d; status 0x%x\n", exit_with, |
| 137 | exitcode, status); |
| 138 | if (mustexit) |
| 139 | exit(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | ret = -1; |
| 141 | } |
| 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | return ret; |
| 144 | } |
| 145 | |
Jeff Dike | 7242a40 | 2007-02-10 01:44:19 -0800 | [diff] [blame] | 146 | /* Changed only during early boot */ |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 147 | static int force_sysemu_disabled = 0; |
| 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | static int __init nosysemu_cmd_param(char *str, int* add) |
| 150 | { |
| 151 | force_sysemu_disabled = 1; |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | __uml_setup("nosysemu", nosysemu_cmd_param, |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 156 | "nosysemu\n" |
| 157 | " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n" |
| 158 | " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n" |
| 159 | " behaviour of ptrace() and helps reducing host context switch rate.\n" |
| 160 | " To make it working, you need a kernel patch for your host, too.\n" |
| 161 | " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n" |
| 162 | " information.\n\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | static void __init check_sysemu(void) |
| 165 | { |
Jeff Dike | cf6aced | 2007-05-23 13:57:40 -0700 | [diff] [blame] | 166 | unsigned long regs[MAX_REG_NR]; |
Jeff Dike | 9eae9b1 | 2007-02-10 01:44:20 -0800 | [diff] [blame] | 167 | int pid, n, status, count=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 169 | non_fatal("Checking syscall emulation patch for ptrace..."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | sysemu_supported = 0; |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 171 | pid = start_ptraced_child(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 173 | if (ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | goto fail; |
| 175 | |
| 176 | CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED)); |
| 177 | if (n < 0) |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 178 | fatal_perror("check_sysemu : wait failed"); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 179 | if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP)) |
Jeff Dike | f1ef916 | 2008-06-12 15:21:41 -0700 | [diff] [blame] | 180 | fatal("check_sysemu : expected SIGTRAP, got status = %d\n", |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 181 | status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 183 | if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0) |
Jeff Dike | cf6aced | 2007-05-23 13:57:40 -0700 | [diff] [blame] | 184 | fatal_perror("check_sysemu : PTRACE_GETREGS failed"); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 185 | if (PT_SYSCALL_NR(regs) != __NR_getpid) { |
Jeff Dike | cf6aced | 2007-05-23 13:57:40 -0700 | [diff] [blame] | 186 | non_fatal("check_sysemu got system call number %d, " |
| 187 | "expected %d...", PT_SYSCALL_NR(regs), __NR_getpid); |
| 188 | goto fail; |
| 189 | } |
| 190 | |
Al Viro | 966e803 | 2011-08-18 20:12:19 +0100 | [diff] [blame] | 191 | n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_RET_OFFSET, os_getpid()); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 192 | if (n < 0) { |
Jeff Dike | cf6aced | 2007-05-23 13:57:40 -0700 | [diff] [blame] | 193 | non_fatal("check_sysemu : failed to modify system call " |
| 194 | "return"); |
| 195 | goto fail; |
| 196 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 198 | if (stop_ptraced_child(pid, 0, 0) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | goto fail_stopped; |
| 200 | |
| 201 | sysemu_supported = 1; |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 202 | non_fatal("OK\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | set_using_sysemu(!force_sysemu_disabled); |
| 204 | |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 205 | non_fatal("Checking advanced syscall emulation patch for ptrace..."); |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 206 | pid = start_ptraced_child(); |
Bodo Stroesser | f9dfefe | 2005-09-03 15:57:51 -0700 | [diff] [blame] | 207 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 208 | if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0, |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 209 | (void *) PTRACE_O_TRACESYSGOOD) < 0)) |
WANG Cong | 5062910 | 2009-03-31 15:23:41 -0700 | [diff] [blame] | 210 | fatal_perror("check_sysemu: PTRACE_OLDSETOPTIONS failed"); |
Bodo Stroesser | f9dfefe | 2005-09-03 15:57:51 -0700 | [diff] [blame] | 211 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 212 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | count++; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 214 | if (ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | goto fail; |
| 216 | CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 217 | if (n < 0) |
WANG Cong | 5062910 | 2009-03-31 15:23:41 -0700 | [diff] [blame] | 218 | fatal_perror("check_sysemu: wait failed"); |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 219 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 220 | if (WIFSTOPPED(status) && |
| 221 | (WSTOPSIG(status) == (SIGTRAP|0x80))) { |
Jeff Dike | f1ef916 | 2008-06-12 15:21:41 -0700 | [diff] [blame] | 222 | if (!count) { |
WANG Cong | 5062910 | 2009-03-31 15:23:41 -0700 | [diff] [blame] | 223 | non_fatal("check_sysemu: SYSEMU_SINGLESTEP " |
Jeff Dike | f1ef916 | 2008-06-12 15:21:41 -0700 | [diff] [blame] | 224 | "doesn't singlestep"); |
| 225 | goto fail; |
| 226 | } |
Al Viro | 966e803 | 2011-08-18 20:12:19 +0100 | [diff] [blame] | 227 | n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_RET_OFFSET, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | os_getpid()); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 229 | if (n < 0) |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 230 | fatal_perror("check_sysemu : failed to modify " |
| 231 | "system call return"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | break; |
| 233 | } |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 234 | else if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP)) |
Bodo Stroesser | f9dfefe | 2005-09-03 15:57:51 -0700 | [diff] [blame] | 235 | count++; |
Jeff Dike | f1ef916 | 2008-06-12 15:21:41 -0700 | [diff] [blame] | 236 | else { |
WANG Cong | 5062910 | 2009-03-31 15:23:41 -0700 | [diff] [blame] | 237 | non_fatal("check_sysemu: expected SIGTRAP or " |
Jeff Dike | f1ef916 | 2008-06-12 15:21:41 -0700 | [diff] [blame] | 238 | "(SIGTRAP | 0x80), got status = %d\n", |
| 239 | status); |
| 240 | goto fail; |
| 241 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 243 | if (stop_ptraced_child(pid, 0, 0) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | goto fail_stopped; |
| 245 | |
| 246 | sysemu_supported = 2; |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 247 | non_fatal("OK\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 249 | if (!force_sysemu_disabled) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | set_using_sysemu(sysemu_supported); |
| 251 | return; |
| 252 | |
| 253 | fail: |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 254 | stop_ptraced_child(pid, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | fail_stopped: |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 256 | non_fatal("missing\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 259 | static void __init check_ptrace(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | int pid, syscall, n, status; |
| 262 | |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 263 | non_fatal("Checking that ptrace can change system call numbers..."); |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 264 | pid = start_ptraced_child(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 266 | if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0, |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 267 | (void *) PTRACE_O_TRACESYSGOOD) < 0)) |
| 268 | fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 270 | while (1) { |
| 271 | if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 272 | fatal_perror("check_ptrace : ptrace failed"); |
| 273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 275 | if (n < 0) |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 276 | fatal_perror("check_ptrace : wait failed"); |
| 277 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 278 | if (!WIFSTOPPED(status) || |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 279 | (WSTOPSIG(status) != (SIGTRAP | 0x80))) |
| 280 | fatal("check_ptrace : expected (SIGTRAP|0x80), " |
| 281 | "got status = %d", status); |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 282 | |
Al Viro | 966e803 | 2011-08-18 20:12:19 +0100 | [diff] [blame] | 283 | syscall = ptrace(PTRACE_PEEKUSER, pid, PT_SYSCALL_NR_OFFSET, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | 0); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 285 | if (syscall == __NR_getpid) { |
Al Viro | 966e803 | 2011-08-18 20:12:19 +0100 | [diff] [blame] | 286 | n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | __NR_getppid); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 288 | if (n < 0) |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 289 | fatal_perror("check_ptrace : failed to modify " |
| 290 | "system call"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | break; |
| 292 | } |
| 293 | } |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 294 | stop_ptraced_child(pid, 0, 1); |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 295 | non_fatal("OK\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | check_sysemu(); |
| 297 | } |
| 298 | |
Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 299 | extern void check_tmpexec(void); |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 300 | |
Jeff Dike | 36e4546 | 2007-05-06 14:51:11 -0700 | [diff] [blame] | 301 | static void __init check_coredump_limit(void) |
Jeff Dike | 1d94cda | 2007-05-06 14:51:00 -0700 | [diff] [blame] | 302 | { |
| 303 | struct rlimit lim; |
| 304 | int err = getrlimit(RLIMIT_CORE, &lim); |
| 305 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 306 | if (err) { |
Jeff Dike | 1d94cda | 2007-05-06 14:51:00 -0700 | [diff] [blame] | 307 | perror("Getting core dump limit"); |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | printf("Core dump limits :\n\tsoft - "); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 312 | if (lim.rlim_cur == RLIM_INFINITY) |
Jeff Dike | 1d94cda | 2007-05-06 14:51:00 -0700 | [diff] [blame] | 313 | printf("NONE\n"); |
| 314 | else printf("%lu\n", lim.rlim_cur); |
| 315 | |
| 316 | printf("\thard - "); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 317 | if (lim.rlim_max == RLIM_INFINITY) |
Jeff Dike | 1d94cda | 2007-05-06 14:51:00 -0700 | [diff] [blame] | 318 | printf("NONE\n"); |
| 319 | else printf("%lu\n", lim.rlim_max); |
| 320 | } |
| 321 | |
Jeff Dike | 36e4546 | 2007-05-06 14:51:11 -0700 | [diff] [blame] | 322 | void __init os_early_checks(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | { |
Jeff Dike | 576c013 | 2008-02-04 22:31:22 -0800 | [diff] [blame] | 324 | int pid; |
| 325 | |
Jeff Dike | 1d94cda | 2007-05-06 14:51:00 -0700 | [diff] [blame] | 326 | /* Print out the core dump limits early */ |
| 327 | check_coredump_limit(); |
| 328 | |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 329 | check_ptrace(); |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 330 | |
| 331 | /* Need to check this early because mmapping happens before the |
| 332 | * kernel is running. |
| 333 | */ |
| 334 | check_tmpexec(); |
Jeff Dike | 576c013 | 2008-02-04 22:31:22 -0800 | [diff] [blame] | 335 | |
| 336 | pid = start_ptraced_child(); |
| 337 | if (init_registers(pid)) |
| 338 | fatal("Failed to initialize default registers"); |
| 339 | stop_ptraced_child(pid, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 342 | int __init parse_iomem(char *str, int *add) |
| 343 | { |
| 344 | struct iomem_region *new; |
Jeff Dike | 73c8f444 | 2007-02-10 01:44:20 -0800 | [diff] [blame] | 345 | struct stat64 buf; |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 346 | char *file, *driver; |
Jeff Dike | 73c8f444 | 2007-02-10 01:44:20 -0800 | [diff] [blame] | 347 | int fd, size; |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 348 | |
| 349 | driver = str; |
| 350 | file = strchr(str,','); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 351 | if (file == NULL) { |
WANG Cong | 626c59f | 2008-04-28 02:13:53 -0700 | [diff] [blame] | 352 | fprintf(stderr, "parse_iomem : failed to parse iomem\n"); |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 353 | goto out; |
| 354 | } |
| 355 | *file = '\0'; |
| 356 | file++; |
Jeff Dike | 73c8f444 | 2007-02-10 01:44:20 -0800 | [diff] [blame] | 357 | fd = open(file, O_RDWR, 0); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 358 | if (fd < 0) { |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 359 | perror("parse_iomem - Couldn't open io file"); |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 360 | goto out; |
| 361 | } |
| 362 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 363 | if (fstat64(fd, &buf) < 0) { |
Jeff Dike | 73c8f444 | 2007-02-10 01:44:20 -0800 | [diff] [blame] | 364 | perror("parse_iomem - cannot stat_fd file"); |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 365 | goto out_close; |
| 366 | } |
| 367 | |
| 368 | new = malloc(sizeof(*new)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 369 | if (new == NULL) { |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 370 | perror("Couldn't allocate iomem_region struct"); |
| 371 | goto out_close; |
| 372 | } |
| 373 | |
Jeff Dike | 73c8f444 | 2007-02-10 01:44:20 -0800 | [diff] [blame] | 374 | size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1); |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 375 | |
| 376 | *new = ((struct iomem_region) { .next = iomem_regions, |
| 377 | .driver = driver, |
| 378 | .fd = fd, |
| 379 | .size = size, |
| 380 | .phys = 0, |
| 381 | .virt = 0 }); |
| 382 | iomem_regions = new; |
| 383 | iomem_size += new->size + UM_KERN_PAGE_SIZE; |
| 384 | |
Jeff Dike | 9eae9b1 | 2007-02-10 01:44:20 -0800 | [diff] [blame] | 385 | return 0; |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 386 | out_close: |
Jeff Dike | 73c8f444 | 2007-02-10 01:44:20 -0800 | [diff] [blame] | 387 | close(fd); |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 388 | out: |
Jeff Dike | 9eae9b1 | 2007-02-10 01:44:20 -0800 | [diff] [blame] | 389 | return 1; |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 390 | } |