2012-11-28 03:00:17 +08:00
|
|
|
//===- SectionMemoryManager.cpp - Memory manager for MCJIT/RtDyld *- C++ -*-==//
|
2012-10-05 04:29:44 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-11-28 03:00:17 +08:00
|
|
|
// This file implements the section-based memory manager used by the MCJIT
|
|
|
|
// execution engine and RuntimeDyld
|
2012-10-05 04:29:44 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Config/config.h"
|
2012-11-28 03:42:02 +08:00
|
|
|
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
|
2012-11-01 01:41:51 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2015-12-16 19:13:23 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size,
|
2013-10-02 08:59:25 +08:00
|
|
|
unsigned Alignment,
|
|
|
|
unsigned SectionID,
|
|
|
|
StringRef SectionName,
|
|
|
|
bool IsReadOnly) {
|
2012-11-28 03:00:17 +08:00
|
|
|
if (IsReadOnly)
|
|
|
|
return allocateSection(RODataMem, Size, Alignment);
|
|
|
|
return allocateSection(RWDataMem, Size, Alignment);
|
2012-10-05 04:29:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
|
2012-11-28 03:00:17 +08:00
|
|
|
unsigned Alignment,
|
2013-10-02 08:59:25 +08:00
|
|
|
unsigned SectionID,
|
|
|
|
StringRef SectionName) {
|
2012-11-28 03:00:17 +08:00
|
|
|
return allocateSection(CodeMem, Size, Alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *SectionMemoryManager::allocateSection(MemoryGroup &MemGroup,
|
|
|
|
uintptr_t Size,
|
|
|
|
unsigned Alignment) {
|
2012-10-05 04:29:44 +08:00
|
|
|
if (!Alignment)
|
|
|
|
Alignment = 16;
|
2012-11-28 03:00:17 +08:00
|
|
|
|
|
|
|
assert(!(Alignment & (Alignment - 1)) && "Alignment must be a power of two.");
|
|
|
|
|
|
|
|
uintptr_t RequiredSize = Alignment * ((Size + Alignment - 1)/Alignment + 1);
|
2012-10-05 04:29:44 +08:00
|
|
|
uintptr_t Addr = 0;
|
2012-11-28 03:00:17 +08:00
|
|
|
|
|
|
|
// Look in the list of free memory regions and use a block there if one
|
2012-10-05 04:29:44 +08:00
|
|
|
// is available.
|
2015-12-16 19:13:23 +08:00
|
|
|
for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
|
|
|
|
if (FreeMB.Free.size() >= RequiredSize) {
|
|
|
|
Addr = (uintptr_t)FreeMB.Free.base();
|
|
|
|
uintptr_t EndOfBlock = Addr + FreeMB.Free.size();
|
2012-10-05 04:29:44 +08:00
|
|
|
// Align the address.
|
|
|
|
Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
|
2015-12-16 19:13:23 +08:00
|
|
|
|
|
|
|
if (FreeMB.PendingPrefixIndex == (unsigned)-1) {
|
|
|
|
// The part of the block we're giving out to the user is now pending
|
|
|
|
MemGroup.PendingMem.push_back(sys::MemoryBlock((void *)Addr, Size));
|
|
|
|
|
|
|
|
// Remember this pending block, such that future allocations can just
|
|
|
|
// modify it rather than creating a new one
|
|
|
|
FreeMB.PendingPrefixIndex = MemGroup.PendingMem.size() - 1;
|
|
|
|
} else {
|
|
|
|
sys::MemoryBlock &PendingMB = MemGroup.PendingMem[FreeMB.PendingPrefixIndex];
|
|
|
|
PendingMB = sys::MemoryBlock(PendingMB.base(), Addr + Size - (uintptr_t)PendingMB.base());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remember how much free space is now left in this block
|
|
|
|
FreeMB.Free = sys::MemoryBlock((void *)(Addr + Size), EndOfBlock - Addr - Size);
|
2012-10-05 04:29:44 +08:00
|
|
|
return (uint8_t*)Addr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No pre-allocated free block was large enough. Allocate a new memory region.
|
2012-11-28 03:00:17 +08:00
|
|
|
// Note that all sections get allocated as read-write. The permissions will
|
|
|
|
// be updated later based on memory group.
|
|
|
|
//
|
|
|
|
// FIXME: It would be useful to define a default allocation size (or add
|
|
|
|
// it as a constructor parameter) to minimize the number of allocations.
|
2012-11-28 03:42:02 +08:00
|
|
|
//
|
2012-11-28 03:00:17 +08:00
|
|
|
// FIXME: Initialize the Near member for each memory group to avoid
|
|
|
|
// interleaving.
|
2014-06-13 05:46:39 +08:00
|
|
|
std::error_code ec;
|
2012-11-28 03:00:17 +08:00
|
|
|
sys::MemoryBlock MB = sys::Memory::allocateMappedMemory(RequiredSize,
|
|
|
|
&MemGroup.Near,
|
|
|
|
sys::Memory::MF_READ |
|
|
|
|
sys::Memory::MF_WRITE,
|
|
|
|
ec);
|
|
|
|
if (ec) {
|
2014-01-25 01:20:08 +08:00
|
|
|
// FIXME: Add error propagation to the interface.
|
2014-04-24 14:44:33 +08:00
|
|
|
return nullptr;
|
2012-11-28 03:00:17 +08:00
|
|
|
}
|
2012-10-05 04:29:44 +08:00
|
|
|
|
2012-11-28 03:00:17 +08:00
|
|
|
// Save this address as the basis for our next request
|
|
|
|
MemGroup.Near = MB;
|
|
|
|
|
2015-12-16 19:13:23 +08:00
|
|
|
// Remember that we allocated this memory
|
|
|
|
MemGroup.AllocatedMem.push_back(MB);
|
2012-10-05 04:29:44 +08:00
|
|
|
Addr = (uintptr_t)MB.base();
|
|
|
|
uintptr_t EndOfBlock = Addr + MB.size();
|
2012-11-28 03:00:17 +08:00
|
|
|
|
2012-10-05 04:29:44 +08:00
|
|
|
// Align the address.
|
|
|
|
Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
|
2012-11-28 03:00:17 +08:00
|
|
|
|
2015-12-16 19:13:23 +08:00
|
|
|
// The part of the block we're giving out to the user is now pending
|
|
|
|
MemGroup.PendingMem.push_back(sys::MemoryBlock((void *)Addr, Size));
|
|
|
|
|
2012-11-28 03:00:17 +08:00
|
|
|
// The allocateMappedMemory may allocate much more memory than we need. In
|
|
|
|
// this case, we store the unused memory as a free memory block.
|
2012-10-05 04:29:44 +08:00
|
|
|
unsigned FreeSize = EndOfBlock-Addr-Size;
|
2015-12-16 19:13:23 +08:00
|
|
|
if (FreeSize > 16) {
|
|
|
|
FreeMemBlock FreeMB;
|
|
|
|
FreeMB.Free = sys::MemoryBlock((void*)(Addr + Size), FreeSize);
|
|
|
|
FreeMB.PendingPrefixIndex = (unsigned)-1;
|
|
|
|
MemGroup.FreeMem.push_back(FreeMB);
|
|
|
|
}
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
// Return aligned address
|
|
|
|
return (uint8_t*)Addr;
|
|
|
|
}
|
|
|
|
|
2013-05-17 18:01:46 +08:00
|
|
|
bool SectionMemoryManager::finalizeMemory(std::string *ErrMsg)
|
2012-11-28 03:00:17 +08:00
|
|
|
{
|
|
|
|
// FIXME: Should in-progress permissions be reverted if an error occurs?
|
2014-06-13 05:46:39 +08:00
|
|
|
std::error_code ec;
|
2012-11-28 03:00:17 +08:00
|
|
|
|
|
|
|
// Make code memory executable.
|
|
|
|
ec = applyMemoryGroupPermissions(CodeMem,
|
|
|
|
sys::Memory::MF_READ | sys::Memory::MF_EXEC);
|
|
|
|
if (ec) {
|
|
|
|
if (ErrMsg) {
|
|
|
|
*ErrMsg = ec.message();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make read-only data memory read-only.
|
|
|
|
ec = applyMemoryGroupPermissions(RODataMem,
|
|
|
|
sys::Memory::MF_READ | sys::Memory::MF_EXEC);
|
|
|
|
if (ec) {
|
|
|
|
if (ErrMsg) {
|
|
|
|
*ErrMsg = ec.message();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read-write data memory already has the correct permissions
|
|
|
|
|
2013-04-25 06:39:12 +08:00
|
|
|
// Some platforms with separate data cache and instruction cache require
|
|
|
|
// explicit cache flush, otherwise JIT code manipulations (like resolved
|
|
|
|
// relocations) will get to the data cache but not to the instruction cache.
|
|
|
|
invalidateInstructionCache();
|
|
|
|
|
2015-12-16 19:13:23 +08:00
|
|
|
return false;
|
|
|
|
}
|
2015-10-01 10:45:07 +08:00
|
|
|
|
2015-12-16 19:13:23 +08:00
|
|
|
static sys::MemoryBlock trimBlockToPageSize(sys::MemoryBlock M) {
|
|
|
|
static const size_t PageSize = sys::Process::getPageSize();
|
2015-10-01 10:45:07 +08:00
|
|
|
|
2015-12-16 19:13:23 +08:00
|
|
|
size_t StartOverlap =
|
|
|
|
(PageSize - ((uintptr_t)M.base() % PageSize)) % PageSize;
|
|
|
|
|
|
|
|
size_t TrimmedSize = M.size();
|
|
|
|
TrimmedSize -= StartOverlap;
|
|
|
|
TrimmedSize -= TrimmedSize % PageSize;
|
|
|
|
|
|
|
|
sys::MemoryBlock Trimmed((void *)((uintptr_t)M.base() + StartOverlap), TrimmedSize);
|
|
|
|
|
|
|
|
assert(((uintptr_t)Trimmed.base() % PageSize) == 0);
|
|
|
|
assert((Trimmed.size() % PageSize) == 0);
|
|
|
|
assert(M.base() <= Trimmed.base() && Trimmed.size() <= M.size());
|
|
|
|
|
|
|
|
return Trimmed;
|
2012-11-28 03:00:17 +08:00
|
|
|
}
|
|
|
|
|
2015-12-16 19:13:23 +08:00
|
|
|
|
2014-06-13 05:46:39 +08:00
|
|
|
std::error_code
|
|
|
|
SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
|
|
|
|
unsigned Permissions) {
|
2015-10-01 10:45:07 +08:00
|
|
|
for (sys::MemoryBlock &MB : MemGroup.PendingMem)
|
2015-08-31 21:39:14 +08:00
|
|
|
if (std::error_code EC = sys::Memory::protectMappedMemory(MB, Permissions))
|
|
|
|
return EC;
|
2012-11-28 03:00:17 +08:00
|
|
|
|
2015-12-16 19:13:23 +08:00
|
|
|
MemGroup.PendingMem.clear();
|
|
|
|
|
|
|
|
// Now go through free blocks and trim any of them that don't span the entire
|
|
|
|
// page because one of the pending blocks may have overlapped it.
|
|
|
|
for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
|
|
|
|
FreeMB.Free = trimBlockToPageSize(FreeMB.Free);
|
|
|
|
// We cleared the PendingMem list, so all these pointers are now invalid
|
|
|
|
FreeMB.PendingPrefixIndex = (unsigned)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all blocks which are now empty
|
|
|
|
MemGroup.FreeMem.erase(
|
|
|
|
std::remove_if(MemGroup.FreeMem.begin(), MemGroup.FreeMem.end(),
|
|
|
|
[](FreeMemBlock &FreeMB) { return FreeMB.Free.size() == 0; }),
|
|
|
|
MemGroup.FreeMem.end());
|
|
|
|
|
2014-06-13 05:46:39 +08:00
|
|
|
return std::error_code();
|
2012-11-28 03:00:17 +08:00
|
|
|
}
|
|
|
|
|
2012-10-05 04:29:44 +08:00
|
|
|
void SectionMemoryManager::invalidateInstructionCache() {
|
2015-10-01 10:45:07 +08:00
|
|
|
for (sys::MemoryBlock &Block : CodeMem.PendingMem)
|
2015-08-31 21:39:14 +08:00
|
|
|
sys::Memory::InvalidateInstructionCache(Block.base(), Block.size());
|
2012-11-28 03:00:17 +08:00
|
|
|
}
|
|
|
|
|
2012-10-05 04:29:44 +08:00
|
|
|
SectionMemoryManager::~SectionMemoryManager() {
|
2015-10-01 10:45:07 +08:00
|
|
|
for (MemoryGroup *Group : {&CodeMem, &RWDataMem, &RODataMem}) {
|
2015-08-31 21:39:14 +08:00
|
|
|
for (sys::MemoryBlock &Block : Group->AllocatedMem)
|
|
|
|
sys::Memory::releaseMappedMemory(Block);
|
2015-10-01 10:45:07 +08:00
|
|
|
}
|
2012-10-05 04:29:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llvm
|