Andrew Kaylor | 3e0f1fb | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 1 | //===- SectionMemoryManager.cpp - Memory manager for MCJIT/RtDyld *- C++ -*-==// |
Andrew Kaylor | 5e7d792 | 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 | 3e0f1fb | 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 | 5e7d792 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Andrew Kaylor | ab5ba51 | 2012-11-27 19:42:02 +0000 | [diff] [blame] | 15 | #include "llvm/ExecutionEngine/SectionMemoryManager.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 16 | #include "llvm/Config/config.h" |
Amara Emerson | eb7fb84 | 2012-10-31 17:41:51 +0000 | [diff] [blame] | 17 | #include "llvm/Support/MathExtras.h" |
Keno Fischer | 94f181a | 2015-12-16 11:13:23 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Process.h" |
Andrew Kaylor | 5e7d792 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 19 | |
Andrew Kaylor | 5e7d792 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 20 | namespace llvm { |
| 21 | |
| 22 | uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size, |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 23 | unsigned Alignment, |
| 24 | unsigned SectionID, |
| 25 | StringRef SectionName, |
| 26 | bool IsReadOnly) { |
Andrew Kaylor | 3e0f1fb | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 27 | if (IsReadOnly) |
Sanjoy Das | e3992c6 | 2017-11-09 06:31:33 +0000 | [diff] [blame^] | 28 | return allocateSection(SectionMemoryManager::AllocationPurpose::ROData, |
| 29 | Size, Alignment); |
| 30 | return allocateSection(SectionMemoryManager::AllocationPurpose::RWData, Size, |
| 31 | Alignment); |
Andrew Kaylor | 5e7d792 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size, |
Andrew Kaylor | 3e0f1fb | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 35 | unsigned Alignment, |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 36 | unsigned SectionID, |
| 37 | StringRef SectionName) { |
Sanjoy Das | e3992c6 | 2017-11-09 06:31:33 +0000 | [diff] [blame^] | 38 | return allocateSection(SectionMemoryManager::AllocationPurpose::Code, Size, |
| 39 | Alignment); |
Andrew Kaylor | 3e0f1fb | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Sanjoy Das | e3992c6 | 2017-11-09 06:31:33 +0000 | [diff] [blame^] | 42 | uint8_t *SectionMemoryManager::allocateSection( |
| 43 | SectionMemoryManager::AllocationPurpose Purpose, uintptr_t Size, |
| 44 | unsigned Alignment) { |
Andrew Kaylor | 5e7d792 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 45 | if (!Alignment) |
| 46 | Alignment = 16; |
Andrew Kaylor | 3e0f1fb | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 47 | |
| 48 | assert(!(Alignment & (Alignment - 1)) && "Alignment must be a power of two."); |
| 49 | |
Sanjoy Das | e3992c6 | 2017-11-09 06:31:33 +0000 | [diff] [blame^] | 50 | uintptr_t RequiredSize = Alignment * ((Size + Alignment - 1) / Alignment + 1); |
Andrew Kaylor | 5e7d792 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 51 | uintptr_t Addr = 0; |
Andrew Kaylor | 3e0f1fb | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 52 | |
Sanjoy Das | e3992c6 | 2017-11-09 06:31:33 +0000 | [diff] [blame^] | 53 | MemoryGroup &MemGroup = [&]() -> MemoryGroup & { |
| 54 | switch (Purpose) { |
| 55 | case AllocationPurpose::Code: |
| 56 | return CodeMem; |
| 57 | case AllocationPurpose::ROData: |
| 58 | return RODataMem; |
| 59 | case AllocationPurpose::RWData: |
| 60 | return RWDataMem; |
| 61 | } |
| 62 | }(); |
| 63 | |
Andrew Kaylor | 3e0f1fb | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 64 | // Look in the list of free memory regions and use a block there if one |
Andrew Kaylor | 5e7d792 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 65 | // is available. |
Keno Fischer | 94f181a | 2015-12-16 11:13:23 +0000 | [diff] [blame] | 66 | for (FreeMemBlock &FreeMB : MemGroup.FreeMem) { |
| 67 | if (FreeMB.Free.size() >= RequiredSize) { |
| 68 | Addr = (uintptr_t)FreeMB.Free.base(); |
| 69 | uintptr_t EndOfBlock = Addr + FreeMB.Free.size(); |
Andrew Kaylor | 5e7d792 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 70 | // Align the address. |
| 71 | Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1); |
Keno Fischer | 94f181a | 2015-12-16 11:13:23 +0000 | [diff] [blame] | 72 | |
| 73 | if (FreeMB.PendingPrefixIndex == (unsigned)-1) { |
| 74 | // The part of the block we're giving out to the user is now pending |
| 75 | MemGroup.PendingMem.push_back(sys::MemoryBlock((void *)Addr, Size)); |
| 76 | |
| 77 | // Remember this pending block, such that future allocations can just |
| 78 | // modify it rather than creating a new one |
| 79 | FreeMB.PendingPrefixIndex = MemGroup.PendingMem.size() - 1; |
| 80 | } else { |
Sanjoy Das | e3992c6 | 2017-11-09 06:31:33 +0000 | [diff] [blame^] | 81 | sys::MemoryBlock &PendingMB = |
| 82 | MemGroup.PendingMem[FreeMB.PendingPrefixIndex]; |
| 83 | PendingMB = sys::MemoryBlock(PendingMB.base(), |
| 84 | Addr + Size - (uintptr_t)PendingMB.base()); |
Keno Fischer | 94f181a | 2015-12-16 11:13:23 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Remember how much free space is now left in this block |
Sanjoy Das | e3992c6 | 2017-11-09 06:31:33 +0000 | [diff] [blame^] | 88 | FreeMB.Free = |
| 89 | sys::MemoryBlock((void *)(Addr + Size), EndOfBlock - Addr - Size); |
| 90 | return (uint8_t *)Addr; |
Andrew Kaylor | 5e7d792 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | // No pre-allocated free block was large enough. Allocate a new memory region. |
Andrew Kaylor | 3e0f1fb | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 95 | // Note that all sections get allocated as read-write. The permissions will |
| 96 | // be updated later based on memory group. |
| 97 | // |
| 98 | // FIXME: It would be useful to define a default allocation size (or add |
| 99 | // it as a constructor parameter) to minimize the number of allocations. |
Andrew Kaylor | ab5ba51 | 2012-11-27 19:42:02 +0000 | [diff] [blame] | 100 | // |
Andrew Kaylor | 3e0f1fb | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 101 | // FIXME: Initialize the Near member for each memory group to avoid |
| 102 | // interleaving. |
Rafael Espindola | 3acea39 | 2014-06-12 21:46:39 +0000 | [diff] [blame] | 103 | std::error_code ec; |
Sanjoy Das | e3992c6 | 2017-11-09 06:31:33 +0000 | [diff] [blame^] | 104 | sys::MemoryBlock MB = MMapper.allocateMappedMemory( |
| 105 | Purpose, RequiredSize, &MemGroup.Near, |
| 106 | sys::Memory::MF_READ | sys::Memory::MF_WRITE, ec); |
Andrew Kaylor | 3e0f1fb | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 107 | if (ec) { |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 108 | // FIXME: Add error propagation to the interface. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 109 | return nullptr; |
Andrew Kaylor | 3e0f1fb | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 110 | } |
Andrew Kaylor | 5e7d792 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 111 | |
Andrew Kaylor | 3e0f1fb | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 112 | // Save this address as the basis for our next request |
| 113 | MemGroup.Near = MB; |
| 114 | |
Keno Fischer | 94f181a | 2015-12-16 11:13:23 +0000 | [diff] [blame] | 115 | // Remember that we allocated this memory |
| 116 | MemGroup.AllocatedMem.push_back(MB); |
Andrew Kaylor | 5e7d792 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 117 | Addr = (uintptr_t)MB.base(); |
| 118 | uintptr_t EndOfBlock = Addr + MB.size(); |
Andrew Kaylor | 3e0f1fb | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 119 | |
Andrew Kaylor | 5e7d792 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 120 | // Align the address. |
| 121 | Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1); |
Andrew Kaylor | 3e0f1fb | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 122 | |
Keno Fischer | 94f181a | 2015-12-16 11:13:23 +0000 | [diff] [blame] | 123 | // The part of the block we're giving out to the user is now pending |
| 124 | MemGroup.PendingMem.push_back(sys::MemoryBlock((void *)Addr, Size)); |
| 125 | |
Andrew Kaylor | 3e0f1fb | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 126 | // The allocateMappedMemory may allocate much more memory than we need. In |
| 127 | // this case, we store the unused memory as a free memory block. |
Sanjoy Das | e3992c6 | 2017-11-09 06:31:33 +0000 | [diff] [blame^] | 128 | unsigned FreeSize = EndOfBlock - Addr - Size; |
Keno Fischer | 94f181a | 2015-12-16 11:13:23 +0000 | [diff] [blame] | 129 | if (FreeSize > 16) { |
| 130 | FreeMemBlock FreeMB; |
Sanjoy Das | e3992c6 | 2017-11-09 06:31:33 +0000 | [diff] [blame^] | 131 | FreeMB.Free = sys::MemoryBlock((void *)(Addr + Size), FreeSize); |
Keno Fischer | 94f181a | 2015-12-16 11:13:23 +0000 | [diff] [blame] | 132 | FreeMB.PendingPrefixIndex = (unsigned)-1; |
| 133 | MemGroup.FreeMem.push_back(FreeMB); |
| 134 | } |
Andrew Kaylor | 5e7d792 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 135 | |
| 136 | // Return aligned address |
Sanjoy Das | e3992c6 | 2017-11-09 06:31:33 +0000 | [diff] [blame^] | 137 | return (uint8_t *)Addr; |
Andrew Kaylor | 5e7d792 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Sanjoy Das | e3992c6 | 2017-11-09 06:31:33 +0000 | [diff] [blame^] | 140 | bool SectionMemoryManager::finalizeMemory(std::string *ErrMsg) { |
Andrew Kaylor | 3e0f1fb | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 141 | // FIXME: Should in-progress permissions be reverted if an error occurs? |
Rafael Espindola | 3acea39 | 2014-06-12 21:46:39 +0000 | [diff] [blame] | 142 | std::error_code ec; |
Andrew Kaylor | 3e0f1fb | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 143 | |
| 144 | // Make code memory executable. |
| 145 | ec = applyMemoryGroupPermissions(CodeMem, |
| 146 | sys::Memory::MF_READ | sys::Memory::MF_EXEC); |
| 147 | if (ec) { |
| 148 | if (ErrMsg) { |
| 149 | *ErrMsg = ec.message(); |
| 150 | } |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | // Make read-only data memory read-only. |
| 155 | ec = applyMemoryGroupPermissions(RODataMem, |
| 156 | sys::Memory::MF_READ | sys::Memory::MF_EXEC); |
| 157 | if (ec) { |
| 158 | if (ErrMsg) { |
| 159 | *ErrMsg = ec.message(); |
| 160 | } |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | // Read-write data memory already has the correct permissions |
| 165 | |
Andrew Kaylor | f91b5ac | 2013-04-24 22:39:12 +0000 | [diff] [blame] | 166 | // Some platforms with separate data cache and instruction cache require |
| 167 | // explicit cache flush, otherwise JIT code manipulations (like resolved |
| 168 | // relocations) will get to the data cache but not to the instruction cache. |
| 169 | invalidateInstructionCache(); |
| 170 | |
Andrew Kaylor | 3e0f1fb | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 171 | return false; |
| 172 | } |
| 173 | |
Keno Fischer | 94f181a | 2015-12-16 11:13:23 +0000 | [diff] [blame] | 174 | static sys::MemoryBlock trimBlockToPageSize(sys::MemoryBlock M) { |
| 175 | static const size_t PageSize = sys::Process::getPageSize(); |
| 176 | |
| 177 | size_t StartOverlap = |
| 178 | (PageSize - ((uintptr_t)M.base() % PageSize)) % PageSize; |
| 179 | |
| 180 | size_t TrimmedSize = M.size(); |
| 181 | TrimmedSize -= StartOverlap; |
| 182 | TrimmedSize -= TrimmedSize % PageSize; |
| 183 | |
Sanjoy Das | e3992c6 | 2017-11-09 06:31:33 +0000 | [diff] [blame^] | 184 | sys::MemoryBlock Trimmed((void *)((uintptr_t)M.base() + StartOverlap), |
| 185 | TrimmedSize); |
Keno Fischer | 94f181a | 2015-12-16 11:13:23 +0000 | [diff] [blame] | 186 | |
| 187 | assert(((uintptr_t)Trimmed.base() % PageSize) == 0); |
| 188 | assert((Trimmed.size() % PageSize) == 0); |
| 189 | assert(M.base() <= Trimmed.base() && Trimmed.size() <= M.size()); |
| 190 | |
| 191 | return Trimmed; |
| 192 | } |
| 193 | |
Rafael Espindola | 3acea39 | 2014-06-12 21:46:39 +0000 | [diff] [blame] | 194 | std::error_code |
| 195 | SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup, |
| 196 | unsigned Permissions) { |
Keno Fischer | 17433bd | 2015-10-01 02:45:07 +0000 | [diff] [blame] | 197 | for (sys::MemoryBlock &MB : MemGroup.PendingMem) |
Sanjoy Das | e3992c6 | 2017-11-09 06:31:33 +0000 | [diff] [blame^] | 198 | if (std::error_code EC = MMapper.protectMappedMemory(MB, Permissions)) |
Benjamin Kramer | efeddcc | 2015-08-31 13:39:14 +0000 | [diff] [blame] | 199 | return EC; |
Andrew Kaylor | 3e0f1fb | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 200 | |
Keno Fischer | 94f181a | 2015-12-16 11:13:23 +0000 | [diff] [blame] | 201 | MemGroup.PendingMem.clear(); |
| 202 | |
| 203 | // Now go through free blocks and trim any of them that don't span the entire |
| 204 | // page because one of the pending blocks may have overlapped it. |
| 205 | for (FreeMemBlock &FreeMB : MemGroup.FreeMem) { |
| 206 | FreeMB.Free = trimBlockToPageSize(FreeMB.Free); |
| 207 | // We cleared the PendingMem list, so all these pointers are now invalid |
| 208 | FreeMB.PendingPrefixIndex = (unsigned)-1; |
| 209 | } |
| 210 | |
| 211 | // Remove all blocks which are now empty |
| 212 | MemGroup.FreeMem.erase( |
David Majnemer | c700490 | 2016-08-12 04:32:37 +0000 | [diff] [blame] | 213 | remove_if(MemGroup.FreeMem, |
| 214 | [](FreeMemBlock &FreeMB) { return FreeMB.Free.size() == 0; }), |
Keno Fischer | 94f181a | 2015-12-16 11:13:23 +0000 | [diff] [blame] | 215 | MemGroup.FreeMem.end()); |
| 216 | |
Rafael Espindola | 3acea39 | 2014-06-12 21:46:39 +0000 | [diff] [blame] | 217 | return std::error_code(); |
Andrew Kaylor | 3e0f1fb | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Andrew Kaylor | 5e7d792 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 220 | void SectionMemoryManager::invalidateInstructionCache() { |
Keno Fischer | 17433bd | 2015-10-01 02:45:07 +0000 | [diff] [blame] | 221 | for (sys::MemoryBlock &Block : CodeMem.PendingMem) |
Benjamin Kramer | efeddcc | 2015-08-31 13:39:14 +0000 | [diff] [blame] | 222 | sys::Memory::InvalidateInstructionCache(Block.base(), Block.size()); |
Andrew Kaylor | 3e0f1fb | 2012-11-27 19:00:17 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Andrew Kaylor | 5e7d792 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 225 | SectionMemoryManager::~SectionMemoryManager() { |
Keno Fischer | 17433bd | 2015-10-01 02:45:07 +0000 | [diff] [blame] | 226 | for (MemoryGroup *Group : {&CodeMem, &RWDataMem, &RODataMem}) { |
Benjamin Kramer | efeddcc | 2015-08-31 13:39:14 +0000 | [diff] [blame] | 227 | for (sys::MemoryBlock &Block : Group->AllocatedMem) |
Sanjoy Das | e3992c6 | 2017-11-09 06:31:33 +0000 | [diff] [blame^] | 228 | MMapper.releaseMappedMemory(Block); |
Keno Fischer | 17433bd | 2015-10-01 02:45:07 +0000 | [diff] [blame] | 229 | } |
Andrew Kaylor | 5e7d792 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Sanjoy Das | e3992c6 | 2017-11-09 06:31:33 +0000 | [diff] [blame^] | 232 | SectionMemoryManager::MemoryMapper::~MemoryMapper() {} |
| 233 | |
| 234 | namespace { |
| 235 | // Trivial implementation of SectionMemoryManager::MemoryMapper that just calls |
| 236 | // into sys::Memory. |
| 237 | class DefaultMMapper final : public SectionMemoryManager::MemoryMapper { |
| 238 | public: |
| 239 | sys::MemoryBlock |
| 240 | allocateMappedMemory(SectionMemoryManager::AllocationPurpose Purpose, |
| 241 | size_t NumBytes, const sys::MemoryBlock *const NearBlock, |
| 242 | unsigned Flags, std::error_code &EC) override { |
| 243 | return sys::Memory::allocateMappedMemory(NumBytes, NearBlock, Flags, EC); |
| 244 | } |
| 245 | |
| 246 | std::error_code protectMappedMemory(const sys::MemoryBlock &Block, |
| 247 | unsigned Flags) override { |
| 248 | return sys::Memory::protectMappedMemory(Block, Flags); |
| 249 | } |
| 250 | |
| 251 | std::error_code releaseMappedMemory(sys::MemoryBlock &M) override { |
| 252 | return sys::Memory::releaseMappedMemory(M); |
| 253 | } |
| 254 | }; |
| 255 | |
| 256 | DefaultMMapper DefaultMMapperInstance; |
| 257 | } // namespace |
| 258 | |
| 259 | SectionMemoryManager::SectionMemoryManager(MemoryMapper *MM) |
| 260 | : MMapper(MM ? *MM : DefaultMMapperInstance) {} |
| 261 | |
Andrew Kaylor | 5e7d792 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 262 | } // namespace llvm |