Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 1 | /* |
| 2 | * copyright 2010, 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 | #define LOG_TAG "bcc" |
| 18 | #include <cutils/log.h> |
| 19 | |
| 20 | #include "ContextManager.h" |
| 21 | |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 22 | #include <llvm/System/Mutex.h> |
| 23 | #include <llvm/Support/MutexGuard.h> |
| 24 | |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 25 | #include <errno.h> |
| 26 | #include <sys/mman.h> |
Jeff Brown | 937a0bc | 2011-01-26 23:20:14 -0800 | [diff] [blame] | 27 | #include <utils/threads.h> |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 28 | |
| 29 | #include <stddef.h> |
| 30 | #include <string.h> |
| 31 | |
| 32 | |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 33 | namespace bcc { |
| 34 | |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 35 | // Starting address for context slots |
| 36 | char * const ContextManager::ContextFixedAddr = BCC_CONTEXT_FIXED_ADDR_; |
| 37 | |
| 38 | // ContextManager singleton object |
| 39 | ContextManager ContextManager::TheContextManager; |
Jeff Brown | 937a0bc | 2011-01-26 23:20:14 -0800 | [diff] [blame] | 40 | |
Logan | 4259805 | 2011-01-26 22:41:13 +0800 | [diff] [blame] | 41 | |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 42 | ContextManager::ContextManager() { |
| 43 | // Initialize context slot occupation table to false |
| 44 | for (size_t i = 0; i < ContextSlotCount; ++i) { |
| 45 | mContextSlotOccupied[i] = false; |
Logan | 4259805 | 2011-01-26 22:41:13 +0800 | [diff] [blame] | 46 | } |
Logan | 4259805 | 2011-01-26 22:41:13 +0800 | [diff] [blame] | 47 | } |
| 48 | |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 49 | char *ContextManager::allocateContext() { |
| 50 | { |
| 51 | // Acquire mContextSlotOccupiedLock |
| 52 | llvm::MutexGuard Locked(mContextSlotOccupiedLock); |
Logan | 4259805 | 2011-01-26 22:41:13 +0800 | [diff] [blame] | 53 | |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 54 | // Try to allocate context on the managed context slot. |
| 55 | for (size_t i = 0; i < ContextSlotCount; ++i) { |
| 56 | if (mContextSlotOccupied[i]) { |
| 57 | continue; |
| 58 | } |
Jeff Brown | 937a0bc | 2011-01-26 23:20:14 -0800 | [diff] [blame] | 59 | |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 60 | void *addr = ContextFixedAddr + ContextSize * i; |
| 61 | void *result = mmap(addr, ContextSize, |
| 62 | PROT_READ | PROT_WRITE | PROT_EXEC, |
| 63 | MAP_PRIVATE | MAP_ANON, -1, 0); |
| 64 | |
| 65 | if (result == addr) { |
| 66 | LOGI("Allocate bcc context. addr=%p\n", result); |
| 67 | mContextSlotOccupied[i] = true; |
| 68 | return static_cast<char *>(result); |
| 69 | } |
| 70 | |
| 71 | if (result && result != MAP_FAILED) { |
| 72 | LOGE("Unable to allocate. suggested=%p, result=%p\n", addr, result); |
| 73 | munmap(result, ContextSize); |
| 74 | } |
| 75 | |
| 76 | LOGE("Unable to allocate. addr=%p. Retry ...\n", addr); |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 77 | } |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 78 | // Release mContextSlotOccupiedLock |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // No slot available, allocate at arbitary address. |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 82 | void *result = mmap(0, ContextSize, PROT_READ | PROT_WRITE | PROT_EXEC, |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 83 | MAP_PRIVATE | MAP_ANON, -1, 0); |
| 84 | |
| 85 | if (!result || result == MAP_FAILED) { |
Logan | b1feb38 | 2010-12-29 20:37:11 +0800 | [diff] [blame] | 86 | LOGE("Unable to mmap. (reason: %s)\n", strerror(errno)); |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 87 | return NULL; |
| 88 | } |
| 89 | |
Logan | b1feb38 | 2010-12-29 20:37:11 +0800 | [diff] [blame] | 90 | LOGI("Allocate bcc context. addr=%p\n", result); |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 91 | return static_cast<char *>(result); |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 95 | char *ContextManager::allocateContext(char *addr, |
| 96 | int imageFd, off_t imageOffset) { |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 97 | // This function should only allocate context when address is an context |
| 98 | // slot address. And the image offset is aligned to the pagesize. |
| 99 | |
| 100 | if (imageFd < 0) { |
Logan | b1feb38 | 2010-12-29 20:37:11 +0800 | [diff] [blame] | 101 | LOGE("Invalid file descriptor for bcc context image\n"); |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 102 | return NULL; |
| 103 | } |
| 104 | |
| 105 | unsigned long pagesize = (unsigned long)sysconf(_SC_PAGESIZE); |
| 106 | |
| 107 | if (imageOffset % pagesize > 0) { |
Logan | b1feb38 | 2010-12-29 20:37:11 +0800 | [diff] [blame] | 108 | LOGE("BCC context image offset is not aligned to page size\n"); |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 109 | return NULL; |
| 110 | } |
| 111 | |
Jeff Brown | 937a0bc | 2011-01-26 23:20:14 -0800 | [diff] [blame] | 112 | ssize_t slot = getSlotIndexFromAddress(addr); |
| 113 | if (slot < 0) { |
Logan | b1feb38 | 2010-12-29 20:37:11 +0800 | [diff] [blame] | 114 | LOGE("Suggested address is not a bcc context slot address\n"); |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 115 | return NULL; |
| 116 | } |
Logan | b1feb38 | 2010-12-29 20:37:11 +0800 | [diff] [blame] | 117 | |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 118 | llvm::MutexGuard Locked(mContextSlotOccupiedLock); |
| 119 | if (mContextSlotOccupied[slot]) { |
Shih-wei Liao | 39ebe2c | 2011-01-28 11:22:32 -0800 | [diff] [blame] | 120 | LOGW("Suggested bcc context slot has been occupied.\n"); |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 121 | return NULL; |
| 122 | } |
| 123 | |
Shih-wei Liao | 931501a | 2010-12-16 04:43:04 -0800 | [diff] [blame] | 124 | // LOGI("addr=%x, imageFd=%d, imageOffset=%x", addr, imageFd, imageOffset); |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 125 | void *result = mmap(addr, ContextSize, |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 126 | PROT_READ | PROT_WRITE | PROT_EXEC, |
| 127 | MAP_PRIVATE, imageFd, imageOffset); |
| 128 | |
Logan | b1feb38 | 2010-12-29 20:37:11 +0800 | [diff] [blame] | 129 | if (!result || result == MAP_FAILED) { |
| 130 | LOGE("Unable to allocate. addr=%p\n", addr); |
| 131 | return NULL; |
| 132 | } |
| 133 | |
Logan | f15ede3 | 2011-01-01 01:37:22 +0800 | [diff] [blame] | 134 | if (result != addr) { |
| 135 | LOGE("Unable to allocate at suggested=%p, result=%p\n", addr, result); |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 136 | munmap(result, ContextSize); |
Logan | f15ede3 | 2011-01-01 01:37:22 +0800 | [diff] [blame] | 137 | return NULL; |
| 138 | } |
| 139 | |
Logan | b1feb38 | 2010-12-29 20:37:11 +0800 | [diff] [blame] | 140 | LOGI("Allocate bcc context. addr=%p\n", addr); |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 141 | mContextSlotOccupied[slot] = true; |
Logan | b1feb38 | 2010-12-29 20:37:11 +0800 | [diff] [blame] | 142 | return static_cast<char *>(result); |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 146 | void ContextManager::deallocateContext(char *addr) { |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 147 | if (!addr) { |
| 148 | return; |
| 149 | } |
| 150 | |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 151 | llvm::MutexGuard Locked(mContextSlotOccupiedLock); |
Jeff Brown | 937a0bc | 2011-01-26 23:20:14 -0800 | [diff] [blame] | 152 | |
Logan | b1feb38 | 2010-12-29 20:37:11 +0800 | [diff] [blame] | 153 | LOGI("Deallocate bcc context. addr=%p\n", addr); |
Logan | 069b331 | 2010-12-31 18:31:15 +0800 | [diff] [blame] | 154 | |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 155 | // Unmap |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 156 | if (munmap(addr, ContextSize) < 0) { |
Logan | b1feb38 | 2010-12-29 20:37:11 +0800 | [diff] [blame] | 157 | LOGE("Unable to unmap. addr=%p (reason: %s)\n", addr, strerror(errno)); |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | |
| 161 | // If the address is one of the context slot, then mark such slot |
| 162 | // freely available as well. |
Jeff Brown | 937a0bc | 2011-01-26 23:20:14 -0800 | [diff] [blame] | 163 | ssize_t slot = getSlotIndexFromAddress(addr); |
| 164 | if (slot >= 0) { |
| 165 | // Give the context slot back. |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 166 | mContextSlotOccupied[slot] = false; |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 170 | |
Logan | 96d250e | 2011-02-28 01:25:26 +0800 | [diff] [blame] | 171 | bool ContextManager::isManagingContext(char *addr) const { |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 172 | ssize_t slot = getSlotIndexFromAddress(addr); |
| 173 | |
| 174 | if (slot < 0) { |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | llvm::MutexGuard Locked(mContextSlotOccupiedLock); |
| 179 | return mContextSlotOccupied[slot]; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | ssize_t ContextManager::getSlotIndexFromAddress(char *addr) { |
| 184 | if (addr >= ContextFixedAddr) { |
| 185 | size_t offset = (size_t)(addr - ContextFixedAddr); |
| 186 | if (offset % ContextSize == 0) { |
| 187 | size_t slot = offset / ContextSize; |
| 188 | if (slot < ContextSlotCount) { |
| 189 | return slot; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | return -1; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | |
Logan | eb3d12b | 2010-12-16 06:20:18 +0800 | [diff] [blame] | 198 | } // namespace bcc |