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