Stephen Hines | b53c8a5 | 2013-04-05 22:17:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011, 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 "MemChunk.h" |
| 18 | |
| 19 | #include "utils/flush_cpu_cache.h" |
| 20 | #include "utils/helper.h" |
| 21 | |
| 22 | #include <llvm/Support/raw_ostream.h> |
| 23 | |
Raphael Moll | 1033b59 | 2013-07-23 19:03:29 -0700 | [diff] [blame] | 24 | #ifndef USE_MINGW /* TODO create a proper HAVE_MMAN_H */ |
Stephen Hines | b53c8a5 | 2013-04-05 22:17:36 -0700 | [diff] [blame] | 25 | #include <sys/mman.h> |
Raphael Moll | 1033b59 | 2013-07-23 19:03:29 -0700 | [diff] [blame] | 26 | #else |
| 27 | #include "mmanWindows.h" |
| 28 | #endif |
Stephen Hines | b53c8a5 | 2013-04-05 22:17:36 -0700 | [diff] [blame] | 29 | |
| 30 | #include <stdlib.h> |
| 31 | |
| 32 | #ifndef MAP_32BIT |
| 33 | #define MAP_32BIT 0 |
| 34 | // Note: If the <sys/mman.h> does not come with MAP_32BIT, then we |
| 35 | // define it as zero, so that it won't manipulate the flags. |
| 36 | #endif |
| 37 | |
| 38 | //#define USE_FIXED_ADDR_MEM_CHUNK 1 |
| 39 | |
| 40 | #if USE_FIXED_ADDR_MEM_CHUNK |
| 41 | static uintptr_t StartAddr = 0x7e000000UL; |
| 42 | #endif |
| 43 | |
Stephen Hines | a221f56 | 2013-07-10 17:59:47 -0700 | [diff] [blame] | 44 | AllocFunc MemChunk::VendorAlloc = NULL; |
| 45 | FreeFunc MemChunk::VendorFree = NULL; |
| 46 | |
| 47 | MemChunk::MemChunk() : buf(NULL), buf_size(0), bVendorBuf(true) { |
Stephen Hines | b53c8a5 | 2013-04-05 22:17:36 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | MemChunk::~MemChunk() { |
Stephen Hines | a221f56 | 2013-07-10 17:59:47 -0700 | [diff] [blame] | 51 | if (!invalidBuf() && bVendorBuf && VendorFree) { |
| 52 | (*VendorFree)(buf); |
| 53 | return; |
| 54 | } |
| 55 | if (!invalidBuf()) { |
Stephen Hines | b53c8a5 | 2013-04-05 22:17:36 -0700 | [diff] [blame] | 56 | munmap(buf, buf_size); |
| 57 | } |
| 58 | } |
| 59 | |
Stephen Hines | a221f56 | 2013-07-10 17:59:47 -0700 | [diff] [blame] | 60 | bool MemChunk::invalidBuf() const { |
| 61 | return (buf == 0 || buf == (unsigned char *)MAP_FAILED); |
| 62 | } |
| 63 | |
Stephen Hines | b53c8a5 | 2013-04-05 22:17:36 -0700 | [diff] [blame] | 64 | bool MemChunk::allocate(size_t size) { |
| 65 | if (size == 0) { |
| 66 | return true; |
| 67 | } |
Stephen Hines | a221f56 | 2013-07-10 17:59:47 -0700 | [diff] [blame] | 68 | if (VendorAlloc) { |
| 69 | buf = (unsigned char*)(*VendorAlloc)(size, 0); |
| 70 | } |
| 71 | if (invalidBuf()) { |
| 72 | bVendorBuf = false; |
Stephen Hines | b53c8a5 | 2013-04-05 22:17:36 -0700 | [diff] [blame] | 73 | #if USE_FIXED_ADDR_MEM_CHUNK |
Stephen Hines | a221f56 | 2013-07-10 17:59:47 -0700 | [diff] [blame] | 74 | buf = (unsigned char *)mmap((void *)StartAddr, size, |
| 75 | PROT_READ | PROT_WRITE, |
| 76 | MAP_PRIVATE | MAP_ANON | MAP_32BIT, |
| 77 | -1, 0); |
Stephen Hines | b53c8a5 | 2013-04-05 22:17:36 -0700 | [diff] [blame] | 78 | #else |
Stephen Hines | a221f56 | 2013-07-10 17:59:47 -0700 | [diff] [blame] | 79 | buf = (unsigned char *)mmap(0, size, |
| 80 | PROT_READ | PROT_WRITE, |
| 81 | MAP_PRIVATE | MAP_ANON | MAP_32BIT, |
| 82 | -1, 0); |
Stephen Hines | b53c8a5 | 2013-04-05 22:17:36 -0700 | [diff] [blame] | 83 | #endif |
Stephen Hines | a221f56 | 2013-07-10 17:59:47 -0700 | [diff] [blame] | 84 | } |
Stephen Hines | b53c8a5 | 2013-04-05 22:17:36 -0700 | [diff] [blame] | 85 | |
Stephen Hines | a221f56 | 2013-07-10 17:59:47 -0700 | [diff] [blame] | 86 | if (invalidBuf()) { |
Stephen Hines | b53c8a5 | 2013-04-05 22:17:36 -0700 | [diff] [blame] | 87 | return false; |
| 88 | } |
| 89 | |
| 90 | #if USE_FIXED_ADDR_MEM_CHUNK |
| 91 | StartAddr += (size + 4095) / 4096 * 4096; |
| 92 | #endif |
| 93 | |
| 94 | buf_size = size; |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | void MemChunk::print() const { |
Stephen Hines | a221f56 | 2013-07-10 17:59:47 -0700 | [diff] [blame] | 99 | if (!invalidBuf()) { |
Stephen Hines | b53c8a5 | 2013-04-05 22:17:36 -0700 | [diff] [blame] | 100 | dump_hex(buf, buf_size, 0, buf_size); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | bool MemChunk::protect(int prot) { |
| 105 | if (buf_size > 0) { |
| 106 | int ret = mprotect((void *)buf, buf_size, prot); |
| 107 | if (ret == -1) { |
| 108 | llvm::errs() << "Error: Can't mprotect.\n"; |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | if (prot & PROT_EXEC) { |
| 113 | FLUSH_CPU_CACHE(buf, buf + buf_size); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return true; |
| 118 | } |