Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 1 | //===- SectionMemoryManager.cpp - Memory manager for MCJIT/RtDyld *- C++ -*-==// |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 10 | // This file implements the section-based memory manager used by the MCJIT |
| 11 | // execution engine and RuntimeDyld |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Config/config.h" |
Andrew Kaylor | 927ba6a | 2012-11-27 19:42:02 +0000 | [diff] [blame] | 16 | #include "llvm/ExecutionEngine/SectionMemoryManager.h" |
Amara Emerson | a25ad19 | 2012-10-31 17:41:51 +0000 | [diff] [blame] | 17 | #include "llvm/Support/MathExtras.h" |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 18 | |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 19 | namespace llvm { |
| 20 | |
| 21 | uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size, |
Filip Pizlo | 6eb43d2 | 2013-10-02 00:59:25 +0000 | [diff] [blame^] | 22 | unsigned Alignment, |
| 23 | unsigned SectionID, |
| 24 | StringRef SectionName, |
| 25 | bool IsReadOnly) { |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 26 | if (IsReadOnly) |
| 27 | return allocateSection(RODataMem, Size, Alignment); |
| 28 | return allocateSection(RWDataMem, Size, Alignment); |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size, |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 32 | unsigned Alignment, |
Filip Pizlo | 6eb43d2 | 2013-10-02 00:59:25 +0000 | [diff] [blame^] | 33 | unsigned SectionID, |
| 34 | StringRef SectionName) { |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 35 | return allocateSection(CodeMem, Size, Alignment); |
| 36 | } |
| 37 | |
| 38 | uint8_t *SectionMemoryManager::allocateSection(MemoryGroup &MemGroup, |
| 39 | uintptr_t Size, |
| 40 | unsigned Alignment) { |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 41 | if (!Alignment) |
| 42 | Alignment = 16; |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 43 | |
| 44 | assert(!(Alignment & (Alignment - 1)) && "Alignment must be a power of two."); |
| 45 | |
| 46 | uintptr_t RequiredSize = Alignment * ((Size + Alignment - 1)/Alignment + 1); |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 47 | uintptr_t Addr = 0; |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 48 | |
| 49 | // Look in the list of free memory regions and use a block there if one |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 50 | // is available. |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 51 | for (int i = 0, e = MemGroup.FreeMem.size(); i != e; ++i) { |
| 52 | sys::MemoryBlock &MB = MemGroup.FreeMem[i]; |
| 53 | if (MB.size() >= RequiredSize) { |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 54 | Addr = (uintptr_t)MB.base(); |
| 55 | uintptr_t EndOfBlock = Addr + MB.size(); |
| 56 | // Align the address. |
| 57 | Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1); |
| 58 | // Store cutted free memory block. |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 59 | MemGroup.FreeMem[i] = sys::MemoryBlock((void*)(Addr + Size), |
| 60 | EndOfBlock - Addr - Size); |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 61 | return (uint8_t*)Addr; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // No pre-allocated free block was large enough. Allocate a new memory region. |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 66 | // Note that all sections get allocated as read-write. The permissions will |
| 67 | // be updated later based on memory group. |
| 68 | // |
| 69 | // FIXME: It would be useful to define a default allocation size (or add |
| 70 | // it as a constructor parameter) to minimize the number of allocations. |
Andrew Kaylor | 927ba6a | 2012-11-27 19:42:02 +0000 | [diff] [blame] | 71 | // |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 72 | // FIXME: Initialize the Near member for each memory group to avoid |
| 73 | // interleaving. |
| 74 | error_code ec; |
| 75 | sys::MemoryBlock MB = sys::Memory::allocateMappedMemory(RequiredSize, |
| 76 | &MemGroup.Near, |
| 77 | sys::Memory::MF_READ | |
| 78 | sys::Memory::MF_WRITE, |
| 79 | ec); |
| 80 | if (ec) { |
| 81 | // FIXME: Add error propogation to the interface. |
| 82 | return NULL; |
| 83 | } |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 84 | |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 85 | // Save this address as the basis for our next request |
| 86 | MemGroup.Near = MB; |
| 87 | |
| 88 | MemGroup.AllocatedMem.push_back(MB); |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 89 | Addr = (uintptr_t)MB.base(); |
| 90 | uintptr_t EndOfBlock = Addr + MB.size(); |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 91 | |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 92 | // Align the address. |
| 93 | Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1); |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 94 | |
| 95 | // The allocateMappedMemory may allocate much more memory than we need. In |
| 96 | // this case, we store the unused memory as a free memory block. |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 97 | unsigned FreeSize = EndOfBlock-Addr-Size; |
| 98 | if (FreeSize > 16) |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 99 | MemGroup.FreeMem.push_back(sys::MemoryBlock((void*)(Addr + Size), FreeSize)); |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 100 | |
| 101 | // Return aligned address |
| 102 | return (uint8_t*)Addr; |
| 103 | } |
| 104 | |
David Tweed | abb38fe | 2013-05-17 10:01:46 +0000 | [diff] [blame] | 105 | bool SectionMemoryManager::finalizeMemory(std::string *ErrMsg) |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 106 | { |
| 107 | // FIXME: Should in-progress permissions be reverted if an error occurs? |
| 108 | error_code ec; |
| 109 | |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 110 | // Don't allow free memory blocks to be used after setting protection flags. |
| 111 | CodeMem.FreeMem.clear(); |
| 112 | |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 113 | // Make code memory executable. |
| 114 | ec = applyMemoryGroupPermissions(CodeMem, |
| 115 | sys::Memory::MF_READ | sys::Memory::MF_EXEC); |
| 116 | if (ec) { |
| 117 | if (ErrMsg) { |
| 118 | *ErrMsg = ec.message(); |
| 119 | } |
| 120 | return true; |
| 121 | } |
| 122 | |
Andrew Kaylor | 8e9ec01 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 123 | // Don't allow free memory blocks to be used after setting protection flags. |
| 124 | RODataMem.FreeMem.clear(); |
| 125 | |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 126 | // Make read-only data memory read-only. |
| 127 | ec = applyMemoryGroupPermissions(RODataMem, |
| 128 | sys::Memory::MF_READ | sys::Memory::MF_EXEC); |
| 129 | if (ec) { |
| 130 | if (ErrMsg) { |
| 131 | *ErrMsg = ec.message(); |
| 132 | } |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | // Read-write data memory already has the correct permissions |
| 137 | |
Andrew Kaylor | 034f4be | 2013-04-24 22:39:12 +0000 | [diff] [blame] | 138 | // Some platforms with separate data cache and instruction cache require |
| 139 | // explicit cache flush, otherwise JIT code manipulations (like resolved |
| 140 | // relocations) will get to the data cache but not to the instruction cache. |
| 141 | invalidateInstructionCache(); |
| 142 | |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 143 | return false; |
| 144 | } |
| 145 | |
| 146 | error_code SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup, |
| 147 | unsigned Permissions) { |
| 148 | |
| 149 | for (int i = 0, e = MemGroup.AllocatedMem.size(); i != e; ++i) { |
| 150 | error_code ec; |
| 151 | ec = sys::Memory::protectMappedMemory(MemGroup.AllocatedMem[i], |
| 152 | Permissions); |
| 153 | if (ec) { |
| 154 | return ec; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return error_code::success(); |
| 159 | } |
| 160 | |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 161 | void SectionMemoryManager::invalidateInstructionCache() { |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 162 | for (int i = 0, e = CodeMem.AllocatedMem.size(); i != e; ++i) |
| 163 | sys::Memory::InvalidateInstructionCache(CodeMem.AllocatedMem[i].base(), |
| 164 | CodeMem.AllocatedMem[i].size()); |
| 165 | } |
| 166 | |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 167 | SectionMemoryManager::~SectionMemoryManager() { |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 168 | for (unsigned i = 0, e = CodeMem.AllocatedMem.size(); i != e; ++i) |
| 169 | sys::Memory::releaseMappedMemory(CodeMem.AllocatedMem[i]); |
| 170 | for (unsigned i = 0, e = RWDataMem.AllocatedMem.size(); i != e; ++i) |
| 171 | sys::Memory::releaseMappedMemory(RWDataMem.AllocatedMem[i]); |
| 172 | for (unsigned i = 0, e = RODataMem.AllocatedMem.size(); i != e; ++i) |
| 173 | sys::Memory::releaseMappedMemory(RODataMem.AllocatedMem[i]); |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | } // namespace llvm |
Andrew Kaylor | cc7773b | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 177 | |