blob: 22a358ef1b0cdaf227d5c768bc5670672fa45554 [file] [log] [blame]
Gennady Sharapov60d339f62005-09-03 15:57:47 -07001/*
Jeff Dikeba180fd2007-10-16 01:27:00 -07002 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
6#include <stdio.h>
Jeff Dike0f80bc82005-09-16 19:27:50 -07007#include <stdlib.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -07008#include <stdarg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <errno.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -070011#include <fcntl.h>
12#include <sched.h>
13#include <signal.h>
14#include <string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <sys/mman.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -070016#include <sys/stat.h>
17#include <sys/wait.h>
Sergei Trofimovichfdfa4c92012-12-30 01:37:30 +030018#include <sys/time.h>
19#include <sys/resource.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/unistd.h>
Al Viro37185b32012-10-08 03:27:32 +010021#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 Torvalds1da177e2005-04-16 15:20:36 -070027
WANG Cong626c59f2008-04-28 02:13:53 -070028static void ptrace_child(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
30 int ret;
Jeff Dike512b6fb2007-10-16 01:27:11 -070031 /* Calling os_getpid because some libcs cached getpid incorrectly */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 int pid = os_getpid(), ppid = getppid();
33 int sc_result;
34
WANG Cong626c59f2008-04-28 02:13:53 -070035 if (change_sig(SIGWINCH, 0) < 0 ||
36 ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 perror("ptrace");
Jeff Dike512b6fb2007-10-16 01:27:11 -070038 kill(pid, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 }
Jeff Dike73c8f4442007-02-10 01:44:20 -080040 kill(pid, SIGSTOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Jeff Dikeba180fd2007-10-16 01:27:00 -070042 /*
43 * This syscall will be intercepted by the parent. Don't call more than
44 * once, please.
45 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 sc_result = os_getpid();
47
48 if (sc_result == pid)
Jeff Dikeba180fd2007-10-16 01:27:00 -070049 /* Nothing modified by the parent, we are running normally. */
50 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 else if (sc_result == ppid)
Jeff Dikeba180fd2007-10-16 01:27:00 -070052 /*
53 * Expected in check_ptrace and check_sysemu when they succeed
54 * in modifying the stack frame
55 */
56 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 else
Jeff Dikeba180fd2007-10-16 01:27:00 -070058 /* 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 Dikebf8fde72008-02-04 22:31:04 -080063
64 exit(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
WANG Congc9a30722008-02-04 22:30:35 -080067static void fatal_perror(const char *str)
Jeff Dike3a150e12007-02-10 01:44:28 -080068{
69 perror(str);
70 exit(1);
71}
72
73static void fatal(char *fmt, ...)
74{
75 va_list list;
76
77 va_start(list, fmt);
WANG Cong626c59f2008-04-28 02:13:53 -070078 vfprintf(stderr, fmt, list);
Jeff Dike3a150e12007-02-10 01:44:28 -080079 va_end(list);
Jeff Dike3a150e12007-02-10 01:44:28 -080080
81 exit(1);
82}
83
84static void non_fatal(char *fmt, ...)
85{
86 va_list list;
87
88 va_start(list, fmt);
WANG Cong626c59f2008-04-28 02:13:53 -070089 vfprintf(stderr, fmt, list);
Jeff Dike3a150e12007-02-10 01:44:28 -080090 va_end(list);
Jeff Dike3a150e12007-02-10 01:44:28 -080091}
92
Jeff Dike3cdaf452007-10-16 01:27:09 -070093static int start_ptraced_child(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 int pid, n, status;
Gennady Sharapov60d339f62005-09-03 15:57:47 -070096
Vegard Nossum0754fb22015-12-18 21:28:53 +010097 fflush(stdout);
98
Jeff Dike3cdaf452007-10-16 01:27:09 -070099 pid = fork();
100 if (pid == 0)
101 ptrace_child();
102 else if (pid < 0)
103 fatal_perror("start_ptraced_child : fork failed");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700106 if (n < 0)
Jeff Dike3cdaf452007-10-16 01:27:09 -0700107 fatal_perror("check_ptrace : waitpid failed");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700108 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
Jeff Dike3a150e12007-02-10 01:44:28 -0800109 fatal("check_ptrace : expected SIGSTOP, got status = %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 status);
111
Jeff Dike9eae9b12007-02-10 01:44:20 -0800112 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700115/* 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 Torvalds1da177e2005-04-16 15:20:36 -0700118 * So only for SYSEMU features we test mustpanic, while normal host features
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700119 * must work anyway!
120 */
Jeff Dike3cdaf452007-10-16 01:27:09 -0700121static int stop_ptraced_child(int pid, int exitcode, int mustexit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 int status, n, ret = 0;
124
Jeff Dikef1ef9162008-06-12 15:21:41 -0700125 if (ptrace(PTRACE_CONT, pid, 0, 0) < 0) {
126 perror("stop_ptraced_child : ptrace failed");
127 return -1;
128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 CATCH_EINTR(n = waitpid(pid, &status, 0));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700130 if (!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 int exit_with = WEXITSTATUS(status);
132 if (exit_with == 2)
Jeff Dike3a150e12007-02-10 01:44:28 -0800133 non_fatal("check_ptrace : child exited with status 2. "
Jeff Dikecf6aced2007-05-23 13:57:40 -0700134 "\nDisabling SYSEMU support.\n");
Jeff Dike3a150e12007-02-10 01:44:28 -0800135 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 Torvalds1da177e2005-04-16 15:20:36 -0700140 ret = -1;
141 }
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return ret;
144}
145
Jeff Dike7242a402007-02-10 01:44:19 -0800146/* Changed only during early boot */
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700147static int force_sysemu_disabled = 0;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static 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 Sharapov60d339f62005-09-03 15:57:47 -0700156"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 Torvalds1da177e2005-04-16 15:20:36 -0700163
164static void __init check_sysemu(void)
165{
Jeff Dikecf6aced2007-05-23 13:57:40 -0700166 unsigned long regs[MAX_REG_NR];
Jeff Dike9eae9b12007-02-10 01:44:20 -0800167 int pid, n, status, count=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Jeff Dike3a150e12007-02-10 01:44:28 -0800169 non_fatal("Checking syscall emulation patch for ptrace...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 sysemu_supported = 0;
Jeff Dike3cdaf452007-10-16 01:27:09 -0700171 pid = start_ptraced_child();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Jeff Dikeba180fd2007-10-16 01:27:00 -0700173 if (ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 goto fail;
175
176 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
177 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800178 fatal_perror("check_sysemu : wait failed");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700179 if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
Jeff Dikef1ef9162008-06-12 15:21:41 -0700180 fatal("check_sysemu : expected SIGTRAP, got status = %d\n",
Jeff Dike3a150e12007-02-10 01:44:28 -0800181 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Jeff Dikeba180fd2007-10-16 01:27:00 -0700183 if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
Jeff Dikecf6aced2007-05-23 13:57:40 -0700184 fatal_perror("check_sysemu : PTRACE_GETREGS failed");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700185 if (PT_SYSCALL_NR(regs) != __NR_getpid) {
Jeff Dikecf6aced2007-05-23 13:57:40 -0700186 non_fatal("check_sysemu got system call number %d, "
187 "expected %d...", PT_SYSCALL_NR(regs), __NR_getpid);
188 goto fail;
189 }
190
Al Viro966e8032011-08-18 20:12:19 +0100191 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_RET_OFFSET, os_getpid());
Jeff Dikeba180fd2007-10-16 01:27:00 -0700192 if (n < 0) {
Jeff Dikecf6aced2007-05-23 13:57:40 -0700193 non_fatal("check_sysemu : failed to modify system call "
194 "return");
195 goto fail;
196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Jeff Dike3cdaf452007-10-16 01:27:09 -0700198 if (stop_ptraced_child(pid, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 goto fail_stopped;
200
201 sysemu_supported = 1;
Jeff Dike3a150e12007-02-10 01:44:28 -0800202 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 set_using_sysemu(!force_sysemu_disabled);
204
Jeff Dike3a150e12007-02-10 01:44:28 -0800205 non_fatal("Checking advanced syscall emulation patch for ptrace...");
Jeff Dike3cdaf452007-10-16 01:27:09 -0700206 pid = start_ptraced_child();
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700207
Jeff Dikeba180fd2007-10-16 01:27:00 -0700208 if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
Jeff Dike3a150e12007-02-10 01:44:28 -0800209 (void *) PTRACE_O_TRACESYSGOOD) < 0))
WANG Cong50629102009-03-31 15:23:41 -0700210 fatal_perror("check_sysemu: PTRACE_OLDSETOPTIONS failed");
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700211
Jeff Dikeba180fd2007-10-16 01:27:00 -0700212 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 count++;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700214 if (ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 goto fail;
216 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700217 if (n < 0)
WANG Cong50629102009-03-31 15:23:41 -0700218 fatal_perror("check_sysemu: wait failed");
Jeff Dike3a150e12007-02-10 01:44:28 -0800219
Jeff Dikeba180fd2007-10-16 01:27:00 -0700220 if (WIFSTOPPED(status) &&
221 (WSTOPSIG(status) == (SIGTRAP|0x80))) {
Jeff Dikef1ef9162008-06-12 15:21:41 -0700222 if (!count) {
WANG Cong50629102009-03-31 15:23:41 -0700223 non_fatal("check_sysemu: SYSEMU_SINGLESTEP "
Jeff Dikef1ef9162008-06-12 15:21:41 -0700224 "doesn't singlestep");
225 goto fail;
226 }
Al Viro966e8032011-08-18 20:12:19 +0100227 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_RET_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 os_getpid());
Jeff Dikeba180fd2007-10-16 01:27:00 -0700229 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800230 fatal_perror("check_sysemu : failed to modify "
231 "system call return");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 break;
233 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700234 else if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
Bodo Stroesserf9dfefe2005-09-03 15:57:51 -0700235 count++;
Jeff Dikef1ef9162008-06-12 15:21:41 -0700236 else {
WANG Cong50629102009-03-31 15:23:41 -0700237 non_fatal("check_sysemu: expected SIGTRAP or "
Jeff Dikef1ef9162008-06-12 15:21:41 -0700238 "(SIGTRAP | 0x80), got status = %d\n",
239 status);
240 goto fail;
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
Jeff Dike3cdaf452007-10-16 01:27:09 -0700243 if (stop_ptraced_child(pid, 0, 0) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 goto fail_stopped;
245
246 sysemu_supported = 2;
Jeff Dike3a150e12007-02-10 01:44:28 -0800247 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Jeff Dikeba180fd2007-10-16 01:27:00 -0700249 if (!force_sysemu_disabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 set_using_sysemu(sysemu_supported);
251 return;
252
253fail:
Jeff Dike3cdaf452007-10-16 01:27:09 -0700254 stop_ptraced_child(pid, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255fail_stopped:
Jeff Dike3a150e12007-02-10 01:44:28 -0800256 non_fatal("missing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700259static void __init check_ptrace(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 int pid, syscall, n, status;
262
Jeff Dike3a150e12007-02-10 01:44:28 -0800263 non_fatal("Checking that ptrace can change system call numbers...");
Jeff Dike3cdaf452007-10-16 01:27:09 -0700264 pid = start_ptraced_child();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Jeff Dikeba180fd2007-10-16 01:27:00 -0700266 if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
Jeff Dike3a150e12007-02-10 01:44:28 -0800267 (void *) PTRACE_O_TRACESYSGOOD) < 0))
268 fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Jeff Dikeba180fd2007-10-16 01:27:00 -0700270 while (1) {
271 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800272 fatal_perror("check_ptrace : ptrace failed");
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700275 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800276 fatal_perror("check_ptrace : wait failed");
277
Jeff Dikeba180fd2007-10-16 01:27:00 -0700278 if (!WIFSTOPPED(status) ||
Jeff Dike3a150e12007-02-10 01:44:28 -0800279 (WSTOPSIG(status) != (SIGTRAP | 0x80)))
280 fatal("check_ptrace : expected (SIGTRAP|0x80), "
281 "got status = %d", status);
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700282
Al Viro966e8032011-08-18 20:12:19 +0100283 syscall = ptrace(PTRACE_PEEKUSER, pid, PT_SYSCALL_NR_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700285 if (syscall == __NR_getpid) {
Al Viro966e8032011-08-18 20:12:19 +0100286 n = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 __NR_getppid);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700288 if (n < 0)
Jeff Dike3a150e12007-02-10 01:44:28 -0800289 fatal_perror("check_ptrace : failed to modify "
290 "system call");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 break;
292 }
293 }
Jeff Dike3cdaf452007-10-16 01:27:09 -0700294 stop_ptraced_child(pid, 0, 1);
Jeff Dike3a150e12007-02-10 01:44:28 -0800295 non_fatal("OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 check_sysemu();
297}
298
Rob Landley966a082f2006-04-18 22:21:43 -0700299extern void check_tmpexec(void);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700300
Jeff Dike36e45462007-05-06 14:51:11 -0700301static void __init check_coredump_limit(void)
Jeff Dike1d94cda2007-05-06 14:51:00 -0700302{
303 struct rlimit lim;
304 int err = getrlimit(RLIMIT_CORE, &lim);
305
Jeff Dikeba180fd2007-10-16 01:27:00 -0700306 if (err) {
Jeff Dike1d94cda2007-05-06 14:51:00 -0700307 perror("Getting core dump limit");
308 return;
309 }
310
311 printf("Core dump limits :\n\tsoft - ");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700312 if (lim.rlim_cur == RLIM_INFINITY)
Jeff Dike1d94cda2007-05-06 14:51:00 -0700313 printf("NONE\n");
314 else printf("%lu\n", lim.rlim_cur);
315
316 printf("\thard - ");
Jeff Dikeba180fd2007-10-16 01:27:00 -0700317 if (lim.rlim_max == RLIM_INFINITY)
Jeff Dike1d94cda2007-05-06 14:51:00 -0700318 printf("NONE\n");
319 else printf("%lu\n", lim.rlim_max);
320}
321
Jeff Dike36e45462007-05-06 14:51:11 -0700322void __init os_early_checks(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Jeff Dike576c0132008-02-04 22:31:22 -0800324 int pid;
325
Jeff Dike1d94cda2007-05-06 14:51:00 -0700326 /* Print out the core dump limits early */
327 check_coredump_limit();
328
Gennady Sharapov60d339f62005-09-03 15:57:47 -0700329 check_ptrace();
Jeff Dike0f80bc82005-09-16 19:27:50 -0700330
331 /* Need to check this early because mmapping happens before the
332 * kernel is running.
333 */
334 check_tmpexec();
Jeff Dike576c0132008-02-04 22:31:22 -0800335
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 Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Jeff Dike0f80bc82005-09-16 19:27:50 -0700342int __init parse_iomem(char *str, int *add)
343{
344 struct iomem_region *new;
Jeff Dike73c8f4442007-02-10 01:44:20 -0800345 struct stat64 buf;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700346 char *file, *driver;
Jeff Dike73c8f4442007-02-10 01:44:20 -0800347 int fd, size;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700348
349 driver = str;
350 file = strchr(str,',');
Jeff Dikeba180fd2007-10-16 01:27:00 -0700351 if (file == NULL) {
WANG Cong626c59f2008-04-28 02:13:53 -0700352 fprintf(stderr, "parse_iomem : failed to parse iomem\n");
Jeff Dike0f80bc82005-09-16 19:27:50 -0700353 goto out;
354 }
355 *file = '\0';
356 file++;
Jeff Dike73c8f4442007-02-10 01:44:20 -0800357 fd = open(file, O_RDWR, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700358 if (fd < 0) {
Jeff Dike512b6fb2007-10-16 01:27:11 -0700359 perror("parse_iomem - Couldn't open io file");
Jeff Dike0f80bc82005-09-16 19:27:50 -0700360 goto out;
361 }
362
Jeff Dikeba180fd2007-10-16 01:27:00 -0700363 if (fstat64(fd, &buf) < 0) {
Jeff Dike73c8f4442007-02-10 01:44:20 -0800364 perror("parse_iomem - cannot stat_fd file");
Jeff Dike0f80bc82005-09-16 19:27:50 -0700365 goto out_close;
366 }
367
368 new = malloc(sizeof(*new));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700369 if (new == NULL) {
Jeff Dike0f80bc82005-09-16 19:27:50 -0700370 perror("Couldn't allocate iomem_region struct");
371 goto out_close;
372 }
373
Jeff Dike73c8f4442007-02-10 01:44:20 -0800374 size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700375
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 Dike9eae9b12007-02-10 01:44:20 -0800385 return 0;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700386 out_close:
Jeff Dike73c8f4442007-02-10 01:44:20 -0800387 close(fd);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700388 out:
Jeff Dike9eae9b12007-02-10 01:44:20 -0800389 return 1;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700390}