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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <asm/unistd.h> |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 19 | #include <init.h> |
| 20 | #include <os.h> |
| 21 | #include <mem_user.h> |
| 22 | #include <ptrace_user.h> |
| 23 | #include <registers.h> |
| 24 | #include <skas.h> |
| 25 | #include <skas_ptrace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
WANG Cong | 626c59f | 2008-04-28 02:13:53 -0700 | [diff] [blame] | 27 | static void ptrace_child(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | { |
| 29 | int ret; |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 30 | /* Calling os_getpid because some libcs cached getpid incorrectly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | int pid = os_getpid(), ppid = getppid(); |
| 32 | int sc_result; |
| 33 | |
WANG Cong | 626c59f | 2008-04-28 02:13:53 -0700 | [diff] [blame] | 34 | if (change_sig(SIGWINCH, 0) < 0 || |
| 35 | ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | perror("ptrace"); |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 37 | kill(pid, SIGKILL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | } |
Jeff Dike | 73c8f444 | 2007-02-10 01:44:20 -0800 | [diff] [blame] | 39 | kill(pid, SIGSTOP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 41 | /* |
| 42 | * This syscall will be intercepted by the parent. Don't call more than |
| 43 | * once, please. |
| 44 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | sc_result = os_getpid(); |
| 46 | |
| 47 | if (sc_result == pid) |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 48 | /* Nothing modified by the parent, we are running normally. */ |
| 49 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | else if (sc_result == ppid) |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 51 | /* |
| 52 | * Expected in check_ptrace and check_sysemu when they succeed |
| 53 | * in modifying the stack frame |
| 54 | */ |
| 55 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | else |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 57 | /* Serious trouble! This could be caused by a bug in host 2.6 |
| 58 | * SKAS3/2.6 patch before release -V6, together with a bug in |
| 59 | * the UML code itself. |
| 60 | */ |
| 61 | ret = 2; |
Jeff Dike | bf8fde7 | 2008-02-04 22:31:04 -0800 | [diff] [blame] | 62 | |
| 63 | exit(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | } |
| 65 | |
WANG Cong | c9a3072 | 2008-02-04 22:30:35 -0800 | [diff] [blame] | 66 | static void fatal_perror(const char *str) |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 67 | { |
| 68 | perror(str); |
| 69 | exit(1); |
| 70 | } |
| 71 | |
| 72 | static void fatal(char *fmt, ...) |
| 73 | { |
| 74 | va_list list; |
| 75 | |
| 76 | va_start(list, fmt); |
WANG Cong | 626c59f | 2008-04-28 02:13:53 -0700 | [diff] [blame] | 77 | vfprintf(stderr, fmt, list); |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 78 | va_end(list); |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 79 | |
| 80 | exit(1); |
| 81 | } |
| 82 | |
| 83 | static void non_fatal(char *fmt, ...) |
| 84 | { |
| 85 | va_list list; |
| 86 | |
| 87 | va_start(list, fmt); |
WANG Cong | 626c59f | 2008-04-28 02:13:53 -0700 | [diff] [blame] | 88 | vfprintf(stderr, fmt, list); |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 89 | va_end(list); |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 92 | static int start_ptraced_child(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | int pid, n, status; |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 95 | |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 96 | pid = fork(); |
| 97 | if (pid == 0) |
| 98 | ptrace_child(); |
| 99 | else if (pid < 0) |
| 100 | fatal_perror("start_ptraced_child : fork failed"); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 103 | if (n < 0) |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 104 | fatal_perror("check_ptrace : waitpid failed"); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 105 | if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 106 | fatal("check_ptrace : expected SIGSTOP, got status = %d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | status); |
| 108 | |
Jeff Dike | 9eae9b1 | 2007-02-10 01:44:20 -0800 | [diff] [blame] | 109 | return pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 112 | /* When testing for SYSEMU support, if it is one of the broken versions, we |
| 113 | * must just avoid using sysemu, not panic, but only if SYSEMU features are |
| 114 | * broken. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | * So only for SYSEMU features we test mustpanic, while normal host features |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 116 | * must work anyway! |
| 117 | */ |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 118 | static int stop_ptraced_child(int pid, int exitcode, int mustexit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
| 120 | int status, n, ret = 0; |
| 121 | |
Jeff Dike | f1ef916 | 2008-06-12 15:21:41 -0700 | [diff] [blame] | 122 | if (ptrace(PTRACE_CONT, pid, 0, 0) < 0) { |
| 123 | perror("stop_ptraced_child : ptrace failed"); |
| 124 | return -1; |
| 125 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | CATCH_EINTR(n = waitpid(pid, &status, 0)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 127 | if (!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | int exit_with = WEXITSTATUS(status); |
| 129 | if (exit_with == 2) |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 130 | non_fatal("check_ptrace : child exited with status 2. " |
Jeff Dike | cf6aced | 2007-05-23 13:57:40 -0700 | [diff] [blame] | 131 | "\nDisabling SYSEMU support.\n"); |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 132 | non_fatal("check_ptrace : child exited with exitcode %d, while " |
| 133 | "expecting %d; status 0x%x\n", exit_with, |
| 134 | exitcode, status); |
| 135 | if (mustexit) |
| 136 | exit(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | ret = -1; |
| 138 | } |
| 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | return ret; |
| 141 | } |
| 142 | |
Jeff Dike | 7242a40 | 2007-02-10 01:44:19 -0800 | [diff] [blame] | 143 | /* Changed only during early boot */ |
Jeff Dike | 53c2587 | 2008-05-12 14:01:47 -0700 | [diff] [blame] | 144 | int ptrace_faultinfo; |
| 145 | static int disable_ptrace_faultinfo; |
| 146 | |
| 147 | int ptrace_ldt; |
| 148 | static int disable_ptrace_ldt; |
| 149 | |
| 150 | int proc_mm; |
| 151 | static int disable_proc_mm; |
| 152 | |
| 153 | int have_switch_mm; |
| 154 | static int disable_switch_mm; |
| 155 | |
| 156 | int skas_needs_stub; |
Paolo 'Blaisorblade' Giarrusso | cb66504 | 2005-07-27 11:43:31 -0700 | [diff] [blame] | 157 | |
| 158 | static int __init skas0_cmd_param(char *str, int* add) |
| 159 | { |
Jeff Dike | 53c2587 | 2008-05-12 14:01:47 -0700 | [diff] [blame] | 160 | disable_ptrace_faultinfo = 1; |
| 161 | disable_ptrace_ldt = 1; |
| 162 | disable_proc_mm = 1; |
| 163 | disable_switch_mm = 1; |
| 164 | |
Paolo 'Blaisorblade' Giarrusso | cb66504 | 2005-07-27 11:43:31 -0700 | [diff] [blame] | 165 | return 0; |
| 166 | } |
| 167 | |
Paolo 'Blaisorblade' Giarrusso | 9e3d862 | 2005-10-09 21:37:18 +0200 | [diff] [blame] | 168 | /* The two __uml_setup would conflict, without this stupid alias. */ |
| 169 | |
| 170 | static int __init mode_skas0_cmd_param(char *str, int* add) |
| 171 | __attribute__((alias("skas0_cmd_param"))); |
| 172 | |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 173 | __uml_setup("skas0", skas0_cmd_param, |
Jeff Dike | 53c2587 | 2008-05-12 14:01:47 -0700 | [diff] [blame] | 174 | "skas0\n" |
| 175 | " Disables SKAS3 and SKAS4 usage, so that SKAS0 is used\n\n"); |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 176 | |
Paolo 'Blaisorblade' Giarrusso | 9e3d862 | 2005-10-09 21:37:18 +0200 | [diff] [blame] | 177 | __uml_setup("mode=skas0", mode_skas0_cmd_param, |
Jeff Dike | 53c2587 | 2008-05-12 14:01:47 -0700 | [diff] [blame] | 178 | "mode=skas0\n" |
| 179 | " Disables SKAS3 and SKAS4 usage, so that SKAS0 is used.\n\n"); |
Paolo 'Blaisorblade' Giarrusso | 9e3d862 | 2005-10-09 21:37:18 +0200 | [diff] [blame] | 180 | |
Jeff Dike | 7242a40 | 2007-02-10 01:44:19 -0800 | [diff] [blame] | 181 | /* Changed only during early boot */ |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 182 | static int force_sysemu_disabled = 0; |
| 183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | static int __init nosysemu_cmd_param(char *str, int* add) |
| 185 | { |
| 186 | force_sysemu_disabled = 1; |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | __uml_setup("nosysemu", nosysemu_cmd_param, |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 191 | "nosysemu\n" |
| 192 | " Turns off syscall emulation patch for ptrace (SYSEMU) on.\n" |
| 193 | " SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n" |
| 194 | " behaviour of ptrace() and helps reducing host context switch rate.\n" |
| 195 | " To make it working, you need a kernel patch for your host, too.\n" |
| 196 | " See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n" |
| 197 | " information.\n\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
| 199 | static void __init check_sysemu(void) |
| 200 | { |
Jeff Dike | cf6aced | 2007-05-23 13:57:40 -0700 | [diff] [blame] | 201 | unsigned long regs[MAX_REG_NR]; |
Jeff Dike | 9eae9b1 | 2007-02-10 01:44:20 -0800 | [diff] [blame] | 202 | int pid, n, status, count=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 204 | non_fatal("Checking syscall emulation patch for ptrace..."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | sysemu_supported = 0; |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 206 | pid = start_ptraced_child(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 208 | if (ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | goto fail; |
| 210 | |
| 211 | CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED)); |
| 212 | if (n < 0) |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 213 | fatal_perror("check_sysemu : wait failed"); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 214 | if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP)) |
Jeff Dike | f1ef916 | 2008-06-12 15:21:41 -0700 | [diff] [blame] | 215 | fatal("check_sysemu : expected SIGTRAP, got status = %d\n", |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 216 | status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 218 | if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0) |
Jeff Dike | cf6aced | 2007-05-23 13:57:40 -0700 | [diff] [blame] | 219 | fatal_perror("check_sysemu : PTRACE_GETREGS failed"); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 220 | if (PT_SYSCALL_NR(regs) != __NR_getpid) { |
Jeff Dike | cf6aced | 2007-05-23 13:57:40 -0700 | [diff] [blame] | 221 | non_fatal("check_sysemu got system call number %d, " |
| 222 | "expected %d...", PT_SYSCALL_NR(regs), __NR_getpid); |
| 223 | goto fail; |
| 224 | } |
| 225 | |
Al Viro | 966e803 | 2011-08-18 20:12:19 +0100 | [diff] [blame] | 226 | n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_RET_OFFSET, os_getpid()); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 227 | if (n < 0) { |
Jeff Dike | cf6aced | 2007-05-23 13:57:40 -0700 | [diff] [blame] | 228 | non_fatal("check_sysemu : failed to modify system call " |
| 229 | "return"); |
| 230 | goto fail; |
| 231 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 233 | if (stop_ptraced_child(pid, 0, 0) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | goto fail_stopped; |
| 235 | |
| 236 | sysemu_supported = 1; |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 237 | non_fatal("OK\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | set_using_sysemu(!force_sysemu_disabled); |
| 239 | |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 240 | non_fatal("Checking advanced syscall emulation patch for ptrace..."); |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 241 | pid = start_ptraced_child(); |
Bodo Stroesser | f9dfefe | 2005-09-03 15:57:51 -0700 | [diff] [blame] | 242 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 243 | if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0, |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 244 | (void *) PTRACE_O_TRACESYSGOOD) < 0)) |
WANG Cong | 5062910 | 2009-03-31 15:23:41 -0700 | [diff] [blame] | 245 | fatal_perror("check_sysemu: PTRACE_OLDSETOPTIONS failed"); |
Bodo Stroesser | f9dfefe | 2005-09-03 15:57:51 -0700 | [diff] [blame] | 246 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 247 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | count++; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 249 | if (ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | goto fail; |
| 251 | CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 252 | if (n < 0) |
WANG Cong | 5062910 | 2009-03-31 15:23:41 -0700 | [diff] [blame] | 253 | fatal_perror("check_sysemu: wait failed"); |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 254 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 255 | if (WIFSTOPPED(status) && |
| 256 | (WSTOPSIG(status) == (SIGTRAP|0x80))) { |
Jeff Dike | f1ef916 | 2008-06-12 15:21:41 -0700 | [diff] [blame] | 257 | if (!count) { |
WANG Cong | 5062910 | 2009-03-31 15:23:41 -0700 | [diff] [blame] | 258 | non_fatal("check_sysemu: SYSEMU_SINGLESTEP " |
Jeff Dike | f1ef916 | 2008-06-12 15:21:41 -0700 | [diff] [blame] | 259 | "doesn't singlestep"); |
| 260 | goto fail; |
| 261 | } |
Al Viro | 966e803 | 2011-08-18 20:12:19 +0100 | [diff] [blame] | 262 | n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_RET_OFFSET, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | os_getpid()); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 264 | if (n < 0) |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 265 | fatal_perror("check_sysemu : failed to modify " |
| 266 | "system call return"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | break; |
| 268 | } |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 269 | else if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP)) |
Bodo Stroesser | f9dfefe | 2005-09-03 15:57:51 -0700 | [diff] [blame] | 270 | count++; |
Jeff Dike | f1ef916 | 2008-06-12 15:21:41 -0700 | [diff] [blame] | 271 | else { |
WANG Cong | 5062910 | 2009-03-31 15:23:41 -0700 | [diff] [blame] | 272 | non_fatal("check_sysemu: expected SIGTRAP or " |
Jeff Dike | f1ef916 | 2008-06-12 15:21:41 -0700 | [diff] [blame] | 273 | "(SIGTRAP | 0x80), got status = %d\n", |
| 274 | status); |
| 275 | goto fail; |
| 276 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | } |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 278 | if (stop_ptraced_child(pid, 0, 0) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | goto fail_stopped; |
| 280 | |
| 281 | sysemu_supported = 2; |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 282 | non_fatal("OK\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 284 | if (!force_sysemu_disabled) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | set_using_sysemu(sysemu_supported); |
| 286 | return; |
| 287 | |
| 288 | fail: |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 289 | stop_ptraced_child(pid, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | fail_stopped: |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 291 | non_fatal("missing\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 294 | static void __init check_ptrace(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | int pid, syscall, n, status; |
| 297 | |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 298 | non_fatal("Checking that ptrace can change system call numbers..."); |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 299 | pid = start_ptraced_child(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 301 | if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0, |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 302 | (void *) PTRACE_O_TRACESYSGOOD) < 0)) |
| 303 | fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 305 | while (1) { |
| 306 | if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 307 | fatal_perror("check_ptrace : ptrace failed"); |
| 308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 310 | if (n < 0) |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 311 | fatal_perror("check_ptrace : wait failed"); |
| 312 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 313 | if (!WIFSTOPPED(status) || |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 314 | (WSTOPSIG(status) != (SIGTRAP | 0x80))) |
| 315 | fatal("check_ptrace : expected (SIGTRAP|0x80), " |
| 316 | "got status = %d", status); |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 317 | |
Al Viro | 966e803 | 2011-08-18 20:12:19 +0100 | [diff] [blame] | 318 | syscall = ptrace(PTRACE_PEEKUSER, pid, PT_SYSCALL_NR_OFFSET, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | 0); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 320 | if (syscall == __NR_getpid) { |
Al Viro | 966e803 | 2011-08-18 20:12:19 +0100 | [diff] [blame] | 321 | n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | __NR_getppid); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 323 | if (n < 0) |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 324 | fatal_perror("check_ptrace : failed to modify " |
| 325 | "system call"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | break; |
| 327 | } |
| 328 | } |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 329 | stop_ptraced_child(pid, 0, 1); |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 330 | non_fatal("OK\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | check_sysemu(); |
| 332 | } |
| 333 | |
Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 334 | extern void check_tmpexec(void); |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 335 | |
Jeff Dike | 36e4546 | 2007-05-06 14:51:11 -0700 | [diff] [blame] | 336 | static void __init check_coredump_limit(void) |
Jeff Dike | 1d94cda | 2007-05-06 14:51:00 -0700 | [diff] [blame] | 337 | { |
| 338 | struct rlimit lim; |
| 339 | int err = getrlimit(RLIMIT_CORE, &lim); |
| 340 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 341 | if (err) { |
Jeff Dike | 1d94cda | 2007-05-06 14:51:00 -0700 | [diff] [blame] | 342 | perror("Getting core dump limit"); |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | printf("Core dump limits :\n\tsoft - "); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 347 | if (lim.rlim_cur == RLIM_INFINITY) |
Jeff Dike | 1d94cda | 2007-05-06 14:51:00 -0700 | [diff] [blame] | 348 | printf("NONE\n"); |
| 349 | else printf("%lu\n", lim.rlim_cur); |
| 350 | |
| 351 | printf("\thard - "); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 352 | if (lim.rlim_max == RLIM_INFINITY) |
Jeff Dike | 1d94cda | 2007-05-06 14:51:00 -0700 | [diff] [blame] | 353 | printf("NONE\n"); |
| 354 | else printf("%lu\n", lim.rlim_max); |
| 355 | } |
| 356 | |
Jeff Dike | 36e4546 | 2007-05-06 14:51:11 -0700 | [diff] [blame] | 357 | void __init os_early_checks(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | { |
Jeff Dike | 576c013 | 2008-02-04 22:31:22 -0800 | [diff] [blame] | 359 | int pid; |
| 360 | |
Jeff Dike | 1d94cda | 2007-05-06 14:51:00 -0700 | [diff] [blame] | 361 | /* Print out the core dump limits early */ |
| 362 | check_coredump_limit(); |
| 363 | |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 364 | check_ptrace(); |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 365 | |
| 366 | /* Need to check this early because mmapping happens before the |
| 367 | * kernel is running. |
| 368 | */ |
| 369 | check_tmpexec(); |
Jeff Dike | 576c013 | 2008-02-04 22:31:22 -0800 | [diff] [blame] | 370 | |
| 371 | pid = start_ptraced_child(); |
| 372 | if (init_registers(pid)) |
| 373 | fatal("Failed to initialize default registers"); |
| 374 | stop_ptraced_child(pid, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Bodo Stroesser | d9838d8 | 2005-09-03 15:57:51 -0700 | [diff] [blame] | 377 | static int __init noprocmm_cmd_param(char *str, int* add) |
| 378 | { |
Jeff Dike | 53c2587 | 2008-05-12 14:01:47 -0700 | [diff] [blame] | 379 | disable_proc_mm = 1; |
Bodo Stroesser | d9838d8 | 2005-09-03 15:57:51 -0700 | [diff] [blame] | 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | __uml_setup("noprocmm", noprocmm_cmd_param, |
| 384 | "noprocmm\n" |
| 385 | " Turns off usage of /proc/mm, even if host supports it.\n" |
| 386 | " To support /proc/mm, the host needs to be patched using\n" |
| 387 | " the current skas3 patch.\n\n"); |
| 388 | |
| 389 | static int __init noptracefaultinfo_cmd_param(char *str, int* add) |
| 390 | { |
Jeff Dike | 53c2587 | 2008-05-12 14:01:47 -0700 | [diff] [blame] | 391 | disable_ptrace_faultinfo = 1; |
Bodo Stroesser | d9838d8 | 2005-09-03 15:57:51 -0700 | [diff] [blame] | 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | __uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param, |
| 396 | "noptracefaultinfo\n" |
| 397 | " Turns off usage of PTRACE_FAULTINFO, even if host supports\n" |
| 398 | " it. To support PTRACE_FAULTINFO, the host needs to be patched\n" |
| 399 | " using the current skas3 patch.\n\n"); |
| 400 | |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 401 | static int __init noptraceldt_cmd_param(char *str, int* add) |
| 402 | { |
Jeff Dike | 53c2587 | 2008-05-12 14:01:47 -0700 | [diff] [blame] | 403 | disable_ptrace_ldt = 1; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | __uml_setup("noptraceldt", noptraceldt_cmd_param, |
| 408 | "noptraceldt\n" |
| 409 | " Turns off usage of PTRACE_LDT, even if host supports it.\n" |
| 410 | " To support PTRACE_LDT, the host needs to be patched using\n" |
| 411 | " the current skas3 patch.\n\n"); |
| 412 | |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 413 | static inline void check_skas3_ptrace_faultinfo(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | { |
| 415 | struct ptrace_faultinfo fi; |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 416 | int pid, n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 418 | non_fatal(" - PTRACE_FAULTINFO..."); |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 419 | pid = start_ptraced_child(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
| 421 | n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi); |
| 422 | if (n < 0) { |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 423 | if (errno == EIO) |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 424 | non_fatal("not found\n"); |
Gennady Sharapov | 60d339f6 | 2005-09-03 15:57:47 -0700 | [diff] [blame] | 425 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | perror("not found"); |
Jeff Dike | 53c2587 | 2008-05-12 14:01:47 -0700 | [diff] [blame] | 427 | } else if (disable_ptrace_faultinfo) |
| 428 | non_fatal("found but disabled on command line\n"); |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 429 | else { |
Jeff Dike | 53c2587 | 2008-05-12 14:01:47 -0700 | [diff] [blame] | 430 | ptrace_faultinfo = 1; |
| 431 | non_fatal("found\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 434 | stop_ptraced_child(pid, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 437 | static inline void check_skas3_ptrace_ldt(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | { |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 439 | #ifdef PTRACE_LDT |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 440 | int pid, n; |
| 441 | unsigned char ldtbuf[40]; |
| 442 | struct ptrace_ldt ldt_op = (struct ptrace_ldt) { |
| 443 | .func = 2, /* read default ldt */ |
| 444 | .ptr = ldtbuf, |
| 445 | .bytecount = sizeof(ldtbuf)}; |
| 446 | |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 447 | non_fatal(" - PTRACE_LDT..."); |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 448 | pid = start_ptraced_child(); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 449 | |
| 450 | n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op); |
| 451 | if (n < 0) { |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 452 | if (errno == EIO) |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 453 | non_fatal("not found\n"); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 454 | else |
Jeff Dike | 53c2587 | 2008-05-12 14:01:47 -0700 | [diff] [blame] | 455 | perror("not found"); |
| 456 | } else if (disable_ptrace_ldt) |
| 457 | non_fatal("found, but use is disabled\n"); |
| 458 | else { |
| 459 | ptrace_ldt = 1; |
| 460 | non_fatal("found\n"); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Jeff Dike | 3cdaf45 | 2007-10-16 01:27:09 -0700 | [diff] [blame] | 463 | stop_ptraced_child(pid, 1, 1); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 464 | #endif |
| 465 | } |
| 466 | |
| 467 | static inline void check_skas3_proc_mm(void) |
| 468 | { |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 469 | non_fatal(" - /proc/mm..."); |
Jeff Dike | 53c2587 | 2008-05-12 14:01:47 -0700 | [diff] [blame] | 470 | if (access("/proc/mm", W_OK) < 0) |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 471 | perror("not found"); |
Jeff Dike | 53c2587 | 2008-05-12 14:01:47 -0700 | [diff] [blame] | 472 | else if (disable_proc_mm) |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 473 | non_fatal("found but disabled on command line\n"); |
Jeff Dike | 53c2587 | 2008-05-12 14:01:47 -0700 | [diff] [blame] | 474 | else { |
| 475 | proc_mm = 1; |
| 476 | non_fatal("found\n"); |
| 477 | } |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 478 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
Karol Swietlicki | 6b7e967 | 2008-02-04 22:30:50 -0800 | [diff] [blame] | 480 | void can_do_skas(void) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 481 | { |
Jeff Dike | 3a150e1 | 2007-02-10 01:44:28 -0800 | [diff] [blame] | 482 | non_fatal("Checking for the skas3 patch in the host:\n"); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 483 | |
| 484 | check_skas3_proc_mm(); |
| 485 | check_skas3_ptrace_faultinfo(); |
| 486 | check_skas3_ptrace_ldt(); |
| 487 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 488 | if (!proc_mm || !ptrace_faultinfo || !ptrace_ldt) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 489 | skas_needs_stub = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | } |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 491 | |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 492 | int __init parse_iomem(char *str, int *add) |
| 493 | { |
| 494 | struct iomem_region *new; |
Jeff Dike | 73c8f444 | 2007-02-10 01:44:20 -0800 | [diff] [blame] | 495 | struct stat64 buf; |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 496 | char *file, *driver; |
Jeff Dike | 73c8f444 | 2007-02-10 01:44:20 -0800 | [diff] [blame] | 497 | int fd, size; |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 498 | |
| 499 | driver = str; |
| 500 | file = strchr(str,','); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 501 | if (file == NULL) { |
WANG Cong | 626c59f | 2008-04-28 02:13:53 -0700 | [diff] [blame] | 502 | fprintf(stderr, "parse_iomem : failed to parse iomem\n"); |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 503 | goto out; |
| 504 | } |
| 505 | *file = '\0'; |
| 506 | file++; |
Jeff Dike | 73c8f444 | 2007-02-10 01:44:20 -0800 | [diff] [blame] | 507 | fd = open(file, O_RDWR, 0); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 508 | if (fd < 0) { |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 509 | perror("parse_iomem - Couldn't open io file"); |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 510 | goto out; |
| 511 | } |
| 512 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 513 | if (fstat64(fd, &buf) < 0) { |
Jeff Dike | 73c8f444 | 2007-02-10 01:44:20 -0800 | [diff] [blame] | 514 | perror("parse_iomem - cannot stat_fd file"); |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 515 | goto out_close; |
| 516 | } |
| 517 | |
| 518 | new = malloc(sizeof(*new)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 519 | if (new == NULL) { |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 520 | perror("Couldn't allocate iomem_region struct"); |
| 521 | goto out_close; |
| 522 | } |
| 523 | |
Jeff Dike | 73c8f444 | 2007-02-10 01:44:20 -0800 | [diff] [blame] | 524 | 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] | 525 | |
| 526 | *new = ((struct iomem_region) { .next = iomem_regions, |
| 527 | .driver = driver, |
| 528 | .fd = fd, |
| 529 | .size = size, |
| 530 | .phys = 0, |
| 531 | .virt = 0 }); |
| 532 | iomem_regions = new; |
| 533 | iomem_size += new->size + UM_KERN_PAGE_SIZE; |
| 534 | |
Jeff Dike | 9eae9b1 | 2007-02-10 01:44:20 -0800 | [diff] [blame] | 535 | return 0; |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 536 | out_close: |
Jeff Dike | 73c8f444 | 2007-02-10 01:44:20 -0800 | [diff] [blame] | 537 | close(fd); |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 538 | out: |
Jeff Dike | 9eae9b1 | 2007-02-10 01:44:20 -0800 | [diff] [blame] | 539 | return 1; |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 540 | } |