1 //===- SectionMemoryManager.cpp - Memory manager for MCJIT/RtDyld *- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the section-based memory manager used by the MCJIT
11 // execution engine and RuntimeDyld
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Config/config.h"
16 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
17 #include "llvm/Support/MathExtras.h"
18 #include "llvm/Support/Process.h"
22 uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size,
25 StringRef SectionName,
28 return allocateSection(RODataMem, Size, Alignment);
29 return allocateSection(RWDataMem, Size, Alignment);
32 uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
35 StringRef SectionName) {
36 return allocateSection(CodeMem, Size, Alignment);
39 uint8_t *SectionMemoryManager::allocateSection(MemoryGroup &MemGroup,
45 assert(!(Alignment & (Alignment - 1)) && "Alignment must be a power of two.");
47 uintptr_t RequiredSize = Alignment * ((Size + Alignment - 1)/Alignment + 1);
50 // Look in the list of free memory regions and use a block there if one
52 for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
53 if (FreeMB.Free.size() >= RequiredSize) {
54 Addr = (uintptr_t)FreeMB.Free.base();
55 uintptr_t EndOfBlock = Addr + FreeMB.Free.size();
57 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
59 if (FreeMB.PendingPrefixIndex == (unsigned)-1) {
60 // The part of the block we're giving out to the user is now pending
61 MemGroup.PendingMem.push_back(sys::MemoryBlock((void *)Addr, Size));
63 // Remember this pending block, such that future allocations can just
64 // modify it rather than creating a new one
65 FreeMB.PendingPrefixIndex = MemGroup.PendingMem.size() - 1;
67 sys::MemoryBlock &PendingMB = MemGroup.PendingMem[FreeMB.PendingPrefixIndex];
68 PendingMB = sys::MemoryBlock(PendingMB.base(), Addr + Size - (uintptr_t)PendingMB.base());
71 // Remember how much free space is now left in this block
72 FreeMB.Free = sys::MemoryBlock((void *)(Addr + Size), EndOfBlock - Addr - Size);
73 return (uint8_t*)Addr;
77 // No pre-allocated free block was large enough. Allocate a new memory region.
78 // Note that all sections get allocated as read-write. The permissions will
79 // be updated later based on memory group.
81 // FIXME: It would be useful to define a default allocation size (or add
82 // it as a constructor parameter) to minimize the number of allocations.
84 // FIXME: Initialize the Near member for each memory group to avoid
87 sys::MemoryBlock MB = sys::Memory::allocateMappedMemory(RequiredSize,
89 sys::Memory::MF_READ |
90 sys::Memory::MF_WRITE,
93 // FIXME: Add error propagation to the interface.
97 // Save this address as the basis for our next request
100 // Remember that we allocated this memory
101 MemGroup.AllocatedMem.push_back(MB);
102 Addr = (uintptr_t)MB.base();
103 uintptr_t EndOfBlock = Addr + MB.size();
105 // Align the address.
106 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
108 // The part of the block we're giving out to the user is now pending
109 MemGroup.PendingMem.push_back(sys::MemoryBlock((void *)Addr, Size));
111 // The allocateMappedMemory may allocate much more memory than we need. In
112 // this case, we store the unused memory as a free memory block.
113 unsigned FreeSize = EndOfBlock-Addr-Size;
116 FreeMB.Free = sys::MemoryBlock((void*)(Addr + Size), FreeSize);
117 FreeMB.PendingPrefixIndex = (unsigned)-1;
118 MemGroup.FreeMem.push_back(FreeMB);
121 // Return aligned address
122 return (uint8_t*)Addr;
125 bool SectionMemoryManager::finalizeMemory(std::string *ErrMsg)
127 // FIXME: Should in-progress permissions be reverted if an error occurs?
130 // Make code memory executable.
131 ec = applyMemoryGroupPermissions(CodeMem,
132 sys::Memory::MF_READ | sys::Memory::MF_EXEC);
135 *ErrMsg = ec.message();
140 // Make read-only data memory read-only.
141 ec = applyMemoryGroupPermissions(RODataMem,
142 sys::Memory::MF_READ | sys::Memory::MF_EXEC);
145 *ErrMsg = ec.message();
150 // Read-write data memory already has the correct permissions
152 // Some platforms with separate data cache and instruction cache require
153 // explicit cache flush, otherwise JIT code manipulations (like resolved
154 // relocations) will get to the data cache but not to the instruction cache.
155 invalidateInstructionCache();
160 static sys::MemoryBlock trimBlockToPageSize(sys::MemoryBlock M) {
161 static const size_t PageSize = sys::Process::getPageSize();
163 size_t StartOverlap =
164 (PageSize - ((uintptr_t)M.base() % PageSize)) % PageSize;
166 size_t TrimmedSize = M.size();
167 TrimmedSize -= StartOverlap;
168 TrimmedSize -= TrimmedSize % PageSize;
170 sys::MemoryBlock Trimmed((void *)((uintptr_t)M.base() + StartOverlap), TrimmedSize);
172 assert(((uintptr_t)Trimmed.base() % PageSize) == 0);
173 assert((Trimmed.size() % PageSize) == 0);
174 assert(M.base() <= Trimmed.base() && Trimmed.size() <= M.size());
181 SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
182 unsigned Permissions) {
183 for (sys::MemoryBlock &MB : MemGroup.PendingMem)
184 if (std::error_code EC = sys::Memory::protectMappedMemory(MB, Permissions))
187 MemGroup.PendingMem.clear();
189 // Now go through free blocks and trim any of them that don't span the entire
190 // page because one of the pending blocks may have overlapped it.
191 for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
192 FreeMB.Free = trimBlockToPageSize(FreeMB.Free);
193 // We cleared the PendingMem list, so all these pointers are now invalid
194 FreeMB.PendingPrefixIndex = (unsigned)-1;
197 // Remove all blocks which are now empty
198 MemGroup.FreeMem.erase(
199 std::remove_if(MemGroup.FreeMem.begin(), MemGroup.FreeMem.end(),
200 [](FreeMemBlock &FreeMB) { return FreeMB.Free.size() == 0; }),
201 MemGroup.FreeMem.end());
203 return std::error_code();
206 void SectionMemoryManager::invalidateInstructionCache() {
207 for (sys::MemoryBlock &Block : CodeMem.PendingMem)
208 sys::Memory::InvalidateInstructionCache(Block.base(), Block.size());
211 SectionMemoryManager::~SectionMemoryManager() {
212 for (MemoryGroup *Group : {&CodeMem, &RWDataMem, &RODataMem}) {
213 for (sys::MemoryBlock &Block : Group->AllocatedMem)
214 sys::Memory::releaseMappedMemory(Block);