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"
|
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.
|
2012-11-28 03:00:17 +08:00
|
|
|
for (int i = 0, e = MemGroup.FreeMem.size(); i != e; ++i) {
|
|
|
|
sys::MemoryBlock &MB = MemGroup.FreeMem[i];
|
|
|
|
if (MB.size() >= RequiredSize) {
|
2012-10-05 04:29:44 +08:00
|
|
|
Addr = (uintptr_t)MB.base();
|
|
|
|
uintptr_t EndOfBlock = Addr + MB.size();
|
|
|
|
// Align the address.
|
|
|
|
Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
|
|
|
|
// Store cutted free memory block.
|
2012-11-28 03:00:17 +08:00
|
|
|
MemGroup.FreeMem[i] = 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.
|
|
|
|
error_code ec;
|
|
|
|
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;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// 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;
|
|
|
|
if (FreeSize > 16)
|
2012-11-28 03:00:17 +08:00
|
|
|
MemGroup.FreeMem.push_back(sys::MemoryBlock((void*)(Addr + Size), FreeSize));
|
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?
|
|
|
|
error_code ec;
|
|
|
|
|
2013-10-01 09:47:35 +08:00
|
|
|
// Don't allow free memory blocks to be used after setting protection flags.
|
|
|
|
CodeMem.FreeMem.clear();
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-10-01 09:47:35 +08:00
|
|
|
// Don't allow free memory blocks to be used after setting protection flags.
|
|
|
|
RODataMem.FreeMem.clear();
|
|
|
|
|
2012-11-28 03:00:17 +08:00
|
|
|
// 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();
|
|
|
|
|
2012-11-28 03:00:17 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
error_code SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
|
|
|
|
unsigned Permissions) {
|
|
|
|
|
|
|
|
for (int i = 0, e = MemGroup.AllocatedMem.size(); i != e; ++i) {
|
|
|
|
error_code ec;
|
|
|
|
ec = sys::Memory::protectMappedMemory(MemGroup.AllocatedMem[i],
|
|
|
|
Permissions);
|
|
|
|
if (ec) {
|
|
|
|
return ec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return error_code::success();
|
|
|
|
}
|
|
|
|
|
2012-10-05 04:29:44 +08:00
|
|
|
void SectionMemoryManager::invalidateInstructionCache() {
|
2012-11-28 03:00:17 +08:00
|
|
|
for (int i = 0, e = CodeMem.AllocatedMem.size(); i != e; ++i)
|
|
|
|
sys::Memory::InvalidateInstructionCache(CodeMem.AllocatedMem[i].base(),
|
|
|
|
CodeMem.AllocatedMem[i].size());
|
|
|
|
}
|
|
|
|
|
2012-10-05 04:29:44 +08:00
|
|
|
SectionMemoryManager::~SectionMemoryManager() {
|
2012-11-28 03:00:17 +08:00
|
|
|
for (unsigned i = 0, e = CodeMem.AllocatedMem.size(); i != e; ++i)
|
|
|
|
sys::Memory::releaseMappedMemory(CodeMem.AllocatedMem[i]);
|
|
|
|
for (unsigned i = 0, e = RWDataMem.AllocatedMem.size(); i != e; ++i)
|
|
|
|
sys::Memory::releaseMappedMemory(RWDataMem.AllocatedMem[i]);
|
|
|
|
for (unsigned i = 0, e = RODataMem.AllocatedMem.size(); i != e; ++i)
|
|
|
|
sys::Memory::releaseMappedMemory(RODataMem.AllocatedMem[i]);
|
2012-10-05 04:29:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llvm
|
2012-11-28 03:00:17 +08:00
|
|
|
|