blob: d9ffaeb31d1f685240181bc86f3dd52ff9ed5973 [file] [log] [blame]
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include "mem_map.h"
18
19#include <sys/mman.h>
20
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080021#include "ScopedFd.h"
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070022#include "stringprintf.h"
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080023#include "utils.h"
24
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070025#include <corkscrew/map_info.h>
26
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080027#define USE_ASHMEM 1
28
29#ifdef USE_ASHMEM
30#include <cutils/ashmem.h>
31#endif
32
Brian Carlstrom27ec9612011-09-19 20:20:38 -070033namespace art {
34
Elliott Hughes96970cd2012-03-13 15:46:31 -070035#if !defined(NDEBUG)
36
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070037static std::ostream& operator<<(std::ostream& os, map_info_t* rhs) {
38 for (map_info_t* m = rhs; m != NULL; m = m->next) {
Elliott Hughes7bda1582012-06-07 11:00:54 -070039 os << StringPrintf("0x%08x-0x%08x %c%c %s\n",
40 static_cast<uint32_t>(m->start),
41 static_cast<uint32_t>(m->end),
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070042 m->is_readable ? 'r' : '-', m->is_executable ? 'x' : '-', m->name);
43 }
44 return os;
Brian Carlstrom27ec9612011-09-19 20:20:38 -070045}
46
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070047void CheckMapRequest(byte* addr, size_t byte_count) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -070048 if (addr == NULL) {
49 return;
50 }
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070051
Elliott Hughes96970cd2012-03-13 15:46:31 -070052 uint32_t base = reinterpret_cast<size_t>(addr);
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070053 uint32_t limit = base + byte_count;
Brian Carlstrom27ec9612011-09-19 20:20:38 -070054
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070055 map_info_t* map_info_list = load_map_info_list(getpid());
56 for (map_info_t* m = map_info_list; m != NULL; m = m->next) {
57 CHECK(!(base >= m->start && base < m->end) // start of new within old
58 && !(limit > m->start && limit < m->end) // end of new within old
59 && !(base <= m->start && limit > m->end)) // start/end of new includes all of old
Elliott Hughes7bda1582012-06-07 11:00:54 -070060 << StringPrintf("Requested region 0x%08x-0x%08x overlaps with existing map 0x%08x-0x%08x (%s)\n",
61 base, limit,
62 static_cast<uint32_t>(m->start), static_cast<uint32_t>(m->end), m->name)
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070063 << map_info_list;
Elliott Hughes96970cd2012-03-13 15:46:31 -070064 }
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070065 free_map_info_list(map_info_list);
Brian Carlstrom27ec9612011-09-19 20:20:38 -070066}
67
Elliott Hughes96970cd2012-03-13 15:46:31 -070068#else
Elliott Hughes1bac54f2012-03-16 12:48:31 -070069static void CheckMapRequest(byte*, size_t) { }
Elliott Hughes96970cd2012-03-13 15:46:31 -070070#endif
71
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070072MemMap* MemMap::MapAnonymous(const char* name, byte* addr, size_t byte_count, int prot) {
73 CHECK_NE(0U, byte_count);
Brian Carlstrom27ec9612011-09-19 20:20:38 -070074 CHECK_NE(0, prot);
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070075 size_t page_aligned_byte_count = RoundUp(byte_count, kPageSize);
76 CheckMapRequest(addr, page_aligned_byte_count);
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080077
78#ifdef USE_ASHMEM
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070079 ScopedFd fd(ashmem_create_region(name, page_aligned_byte_count));
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080080 int flags = MAP_PRIVATE;
81 if (fd.get() == -1) {
82 PLOG(ERROR) << "ashmem_create_region failed (" << name << ")";
83 return NULL;
84 }
85#else
86 ScopedFd fd(-1);
87 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
88#endif
89
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070090 byte* actual = reinterpret_cast<byte*>(mmap(addr, page_aligned_byte_count, prot, flags, fd.get(), 0));
Brian Carlstrom27ec9612011-09-19 20:20:38 -070091 if (actual == MAP_FAILED) {
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070092 PLOG(ERROR) << "mmap(" << reinterpret_cast<void*>(addr) << ", " << page_aligned_byte_count
Elliott Hughes5ea8d4b2012-05-30 15:21:36 -070093 << ", " << prot << ", " << flags << ", " << fd.get() << ", 0) failed for " << name;
Brian Carlstrom27ec9612011-09-19 20:20:38 -070094 return NULL;
95 }
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070096 return new MemMap(actual, byte_count, actual, page_aligned_byte_count);
Brian Carlstrom27ec9612011-09-19 20:20:38 -070097}
98
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070099MemMap* MemMap::MapFileAtAddress(byte* addr, size_t byte_count, int prot, int flags, int fd, off_t start) {
100 CHECK_NE(0U, byte_count);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700101 CHECK_NE(0, prot);
102 CHECK_NE(0, flags & (MAP_SHARED | MAP_PRIVATE));
Elliott Hughesecd3a6f2012-06-06 18:16:37 -0700103 // Adjust 'offset' and 'byte_count' to be page-aligned.
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700104 int page_offset = start % kPageSize;
105 off_t page_aligned_offset = start - page_offset;
Elliott Hughesecd3a6f2012-06-06 18:16:37 -0700106 size_t page_aligned_byte_count = RoundUp(byte_count + page_offset, kPageSize);
107 CheckMapRequest(addr, page_aligned_byte_count);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700108 byte* actual = reinterpret_cast<byte*>(mmap(addr,
Elliott Hughesecd3a6f2012-06-06 18:16:37 -0700109 page_aligned_byte_count,
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700110 prot,
111 flags,
112 fd,
113 page_aligned_offset));
114 if (actual == MAP_FAILED) {
115 PLOG(ERROR) << "mmap failed";
116 return NULL;
117 }
Elliott Hughesecd3a6f2012-06-06 18:16:37 -0700118 return new MemMap(actual + page_offset, byte_count, actual, page_aligned_byte_count);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700119}
120
121MemMap::~MemMap() {
Ian Rogers30fab402012-01-23 15:43:46 -0800122 if (base_begin_ == NULL && base_size_ == 0) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700123 return;
124 }
Ian Rogers30fab402012-01-23 15:43:46 -0800125 int result = munmap(base_begin_, base_size_);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700126 if (result == -1) {
127 PLOG(FATAL) << "munmap failed";
128 }
129}
130
Ian Rogers30fab402012-01-23 15:43:46 -0800131MemMap::MemMap(byte* begin, size_t size, void* base_begin, size_t base_size)
132 : begin_(begin), size_(size), base_begin_(base_begin), base_size_(base_size) {
133 CHECK(begin_ != NULL);
134 CHECK_NE(size_, 0U);
135 CHECK(base_begin_ != NULL);
136 CHECK_NE(base_size_, 0U);
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700137};
138
Logan Chiend88fa262012-06-06 15:23:32 +0800139
140bool MemMap::Protect(int prot) {
141 if (base_begin_ == NULL && base_size_ == 0) {
142 return true;
143 }
144
145 if (mprotect(base_begin_, base_size_, prot) == 0) {
146 return true;
147 }
148
Shih-wei Liaoa060ed92012-06-07 09:25:28 -0700149 PLOG(ERROR) << "mprotect(" << reinterpret_cast<void*>(base_begin_) << ", " << base_size_ << ", "
150 << prot << ") failed";
Logan Chiend88fa262012-06-06 15:23:32 +0800151 return false;
152}
153
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700154} // namespace art