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