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