2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
28 #ifdef HAVE_POSIX_FILEMAP
36 * Having trouble finding a portable way to get this. sysconf(_SC_PAGE_SIZE)
37 * seems appropriate, but we don't have that on the device. Some systems
38 * have getpagesize(2), though the linux man page has some odd cautions.
40 #define DEFAULT_PAGE_SIZE 4096
44 * Create an anonymous shared memory segment large enough to hold "length"
45 * bytes. The actual segment may be larger because mmap() operates on
46 * page boundaries (usually 4K).
48 static void* sysCreateAnonShmem(size_t length)
50 #ifdef HAVE_POSIX_FILEMAP
53 ptr = mmap(NULL, length, PROT_READ | PROT_WRITE,
54 MAP_SHARED | MAP_ANON, -1, 0);
55 if (ptr == MAP_FAILED) {
56 LOGW("mmap(%d, RW, SHARED|ANON) failed: %s\n", (int) length,
63 LOGE("sysCreateAnonShmem not implemented.\n");
69 * Create a private anonymous storage area.
71 int sysCreatePrivateMap(size_t length, MemMapping* pMap)
75 memPtr = sysCreateAnonShmem(length);
79 pMap->addr = pMap->baseAddr = memPtr;
80 pMap->length = pMap->baseLength = length;
85 * Determine the current offset and remaining length of the open file.
87 static int getFileStartAndLength(int fd, off_t *start_, size_t *length_)
92 assert(start_ != NULL);
93 assert(length_ != NULL);
95 start = lseek(fd, 0L, SEEK_CUR);
96 end = lseek(fd, 0L, SEEK_END);
97 (void) lseek(fd, start, SEEK_SET);
99 if (start == (off_t) -1 || end == (off_t) -1) {
100 LOGE("could not determine length of file\n");
104 length = end - start;
106 LOGE("file is empty\n");
117 * Pull the contents of a file into an new shared memory segment. We grab
118 * everything from fd's current offset on.
120 * We need to know the length ahead of time so we can allocate a segment
121 * of sufficient size.
123 int sysLoadFileInShmem(int fd, MemMapping* pMap)
125 #ifdef HAVE_POSIX_FILEMAP
127 size_t length, actual;
130 assert(pMap != NULL);
132 if (getFileStartAndLength(fd, &start, &length) < 0)
135 memPtr = sysCreateAnonShmem(length);
139 actual = read(fd, memPtr, length);
140 if (actual != length) {
141 LOGE("only read %d of %d bytes\n", (int) actual, (int) length);
142 sysReleaseShmem(pMap);
146 pMap->baseAddr = pMap->addr = memPtr;
147 pMap->baseLength = pMap->length = length;
151 LOGE("sysLoadFileInShmem not implemented.\n");
156 #ifndef HAVE_POSIX_FILEMAP
157 int sysFakeMapFile(int fd, MemMapping* pMap)
159 /* No MMAP, just fake it by copying the bits.
160 For Win32 we could use MapViewOfFile if really necessary
161 (see libs/utils/FileMap.cpp).
167 assert(pMap != NULL);
169 if (getFileStartAndLength(fd, &start, &length) < 0)
172 memPtr = malloc(length);
173 if (read(fd, memPtr, length) < 0) {
174 LOGW("read(fd=%d, start=%d, length=%d) failed: %s\n", (int) length,
175 fd, (int) start, strerror(errno));
179 pMap->baseAddr = pMap->addr = memPtr;
180 pMap->baseLength = pMap->length = length;
187 * Map a file (from fd's current offset) into a shared, read-only memory
188 * segment. The file offset must be a multiple of the system page size.
190 * On success, returns 0 and fills out "pMap". On failure, returns a nonzero
191 * value and does not disturb "pMap".
193 int sysMapFileInShmemReadOnly(int fd, MemMapping* pMap)
195 #ifdef HAVE_POSIX_FILEMAP
200 assert(pMap != NULL);
202 if (getFileStartAndLength(fd, &start, &length) < 0)
205 memPtr = mmap(NULL, length, PROT_READ, MAP_FILE | MAP_SHARED, fd, start);
206 if (memPtr == MAP_FAILED) {
207 LOGW("mmap(%d, RO, FILE|SHARED, %d, %d) failed: %s\n", (int) length,
208 fd, (int) start, strerror(errno));
212 pMap->baseAddr = pMap->addr = memPtr;
213 pMap->baseLength = pMap->length = length;
217 return sysFakeMapFile(fd, pMap);
222 * Map a file (from fd's current offset) into a private, read-write memory
223 * segment that will be marked read-only (a/k/a "writable read-only"). The
224 * file offset must be a multiple of the system page size.
226 * In some cases the mapping will be fully writable (e.g. for files on
229 * On success, returns 0 and fills out "pMap". On failure, returns a nonzero
230 * value and does not disturb "pMap".
232 int sysMapFileInShmemWritableReadOnly(int fd, MemMapping* pMap)
234 #ifdef HAVE_POSIX_FILEMAP
239 assert(pMap != NULL);
241 if (getFileStartAndLength(fd, &start, &length) < 0)
244 memPtr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_FILE | MAP_PRIVATE,
246 if (memPtr == MAP_FAILED) {
247 LOGW("mmap(%d, R/W, FILE|PRIVATE, %d, %d) failed: %s\n", (int) length,
248 fd, (int) start, strerror(errno));
251 if (mprotect(memPtr, length, PROT_READ) < 0) {
252 /* this fails with EACCESS on FAT filesystems, e.g. /sdcard */
254 LOGV("mprotect(%p, %d, PROT_READ) failed: %s\n",
255 memPtr, length, strerror(err));
256 LOGD("mprotect(RO) failed (%d), file will remain read-write\n", err);
259 pMap->baseAddr = pMap->addr = memPtr;
260 pMap->baseLength = pMap->length = length;
264 return sysFakeMapFile(fd, pMap);
269 * Map part of a file (from fd's current offset) into a shared, read-only
272 * On success, returns 0 and fills out "pMap". On failure, returns a nonzero
273 * value and does not disturb "pMap".
275 int sysMapFileSegmentInShmem(int fd, off_t start, long length,
278 #ifdef HAVE_POSIX_FILEMAP
280 size_t fileLength, actualLength;
285 assert(pMap != NULL);
287 if (getFileStartAndLength(fd, &dummy, &fileLength) < 0)
290 if (start + length > (long)fileLength) {
291 LOGW("bad segment: st=%d len=%ld flen=%d\n",
292 (int) start, length, (int) fileLength);
296 /* adjust to be page-aligned */
297 adjust = start % DEFAULT_PAGE_SIZE;
298 actualStart = start - adjust;
299 actualLength = length + adjust;
301 memPtr = mmap(NULL, actualLength, PROT_READ, MAP_FILE | MAP_SHARED,
303 if (memPtr == MAP_FAILED) {
304 LOGW("mmap(%d, R, FILE|SHARED, %d, %d) failed: %s\n",
305 (int) actualLength, fd, (int) actualStart, strerror(errno));
309 pMap->baseAddr = memPtr;
310 pMap->baseLength = actualLength;
311 pMap->addr = (char*)memPtr + adjust;
312 pMap->length = length;
314 LOGVV("mmap seg (st=%d ln=%d): bp=%p bl=%d ad=%p ln=%d\n",
315 (int) start, (int) length,
316 pMap->baseAddr, (int) pMap->baseLength,
317 pMap->addr, (int) pMap->length);
321 LOGE("sysMapFileSegmentInShmem not implemented.\n");
327 * Change the access rights on one or more pages to read-only or read-write.
329 * Returns 0 on success.
331 int sysChangeMapAccess(void* addr, size_t length, int wantReadWrite,
334 #ifdef HAVE_POSIX_FILEMAP
336 * Verify that "addr" is part of this mapping file.
338 if (addr < pMap->baseAddr ||
339 (u1*)addr >= (u1*)pMap->baseAddr + pMap->baseLength)
341 LOGE("Attempted to change %p; map is %p - %p\n",
342 addr, pMap->baseAddr, (u1*)pMap->baseAddr + pMap->baseLength);
347 * Align "addr" to a page boundary and adjust "length" appropriately.
348 * (The address must be page-aligned, the length doesn't need to be,
349 * but we do need to ensure we cover the same range.)
351 u1* alignAddr = (u1*) ((int) addr & ~(SYSTEM_PAGE_SIZE-1));
352 size_t alignLength = length + ((u1*) addr - alignAddr);
354 //LOGI("%p/%zd --> %p/%zd\n", addr, length, alignAddr, alignLength);
355 int prot = wantReadWrite ? (PROT_READ|PROT_WRITE) : (PROT_READ);
356 if (mprotect(alignAddr, alignLength, prot) != 0) {
358 LOGV("mprotect (%p,%zd,%d) failed: %s\n",
359 alignAddr, alignLength, prot, strerror(errno));
360 return (errno != 0) ? errno : -1;
364 /* for "fake" mapping, no need to do anything */
369 * Release a memory mapping.
371 void sysReleaseShmem(MemMapping* pMap)
373 #ifdef HAVE_POSIX_FILEMAP
374 if (pMap->baseAddr == NULL && pMap->baseLength == 0)
377 if (munmap(pMap->baseAddr, pMap->baseLength) < 0) {
378 LOGW("munmap(%p, %d) failed: %s\n",
379 pMap->baseAddr, (int)pMap->baseLength, strerror(errno));
381 LOGV("munmap(%p, %d) succeeded\n", pMap->baseAddr, pMap->baseLength);
382 pMap->baseAddr = NULL;
383 pMap->baseLength = 0;
386 /* Free the bits allocated by sysMapFileInShmem. */
387 if (pMap->baseAddr != NULL) {
388 free(pMap->baseAddr);
389 pMap->baseAddr = NULL;
391 pMap->baseLength = 0;
396 * Make a copy of a MemMapping.
398 void sysCopyMap(MemMapping* dst, const MemMapping* src)
400 memcpy(dst, src, sizeof(MemMapping));