blob: 5e89a945b80d109fa7f2bc30829d3d674fd0cb80 [file] [log] [blame]
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +00001//===- SectionMemoryManager.cpp - Memory manager for MCJIT/RtDyld *- C++ -*-==//
Andrew Kaylor5e7d7922012-10-04 20:29:44 +00002//
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 Kaylor3e0f1fb2012-11-27 19:00:17 +000010// This file implements the section-based memory manager used by the MCJIT
11// execution engine and RuntimeDyld
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Config/config.h"
Andrew Kaylorab5ba512012-11-27 19:42:02 +000016#include "llvm/ExecutionEngine/SectionMemoryManager.h"
Amara Emersoneb7fb842012-10-31 17:41:51 +000017#include "llvm/Support/MathExtras.h"
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000018
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000019namespace llvm {
20
21uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size,
Filip Pizlo7aa695e02013-10-02 00:59:25 +000022 unsigned Alignment,
23 unsigned SectionID,
24 StringRef SectionName,
25 bool IsReadOnly) {
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000026 if (IsReadOnly)
27 return allocateSection(RODataMem, Size, Alignment);
28 return allocateSection(RWDataMem, Size, Alignment);
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000029}
30
31uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000032 unsigned Alignment,
Filip Pizlo7aa695e02013-10-02 00:59:25 +000033 unsigned SectionID,
34 StringRef SectionName) {
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000035 return allocateSection(CodeMem, Size, Alignment);
36}
37
38uint8_t *SectionMemoryManager::allocateSection(MemoryGroup &MemGroup,
39 uintptr_t Size,
40 unsigned Alignment) {
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000041 if (!Alignment)
42 Alignment = 16;
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000043
44 assert(!(Alignment & (Alignment - 1)) && "Alignment must be a power of two.");
45
46 uintptr_t RequiredSize = Alignment * ((Size + Alignment - 1)/Alignment + 1);
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000047 uintptr_t Addr = 0;
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000048
49 // Look in the list of free memory regions and use a block there if one
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000050 // is available.
Benjamin Kramerefeddcc2015-08-31 13:39:14 +000051 for (sys::MemoryBlock &MB : MemGroup.FreeMem) {
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000052 if (MB.size() >= RequiredSize) {
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000053 Addr = (uintptr_t)MB.base();
54 uintptr_t EndOfBlock = Addr + MB.size();
55 // Align the address.
56 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
57 // Store cutted free memory block.
Benjamin Kramerefeddcc2015-08-31 13:39:14 +000058 MB = sys::MemoryBlock((void *)(Addr + Size), EndOfBlock - Addr - Size);
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000059 return (uint8_t*)Addr;
60 }
61 }
62
63 // No pre-allocated free block was large enough. Allocate a new memory region.
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000064 // Note that all sections get allocated as read-write. The permissions will
65 // be updated later based on memory group.
66 //
67 // FIXME: It would be useful to define a default allocation size (or add
68 // it as a constructor parameter) to minimize the number of allocations.
Andrew Kaylorab5ba512012-11-27 19:42:02 +000069 //
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000070 // FIXME: Initialize the Near member for each memory group to avoid
71 // interleaving.
Rafael Espindola3acea392014-06-12 21:46:39 +000072 std::error_code ec;
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000073 sys::MemoryBlock MB = sys::Memory::allocateMappedMemory(RequiredSize,
74 &MemGroup.Near,
75 sys::Memory::MF_READ |
76 sys::Memory::MF_WRITE,
77 ec);
78 if (ec) {
Alp Tokercb402912014-01-24 17:20:08 +000079 // FIXME: Add error propagation to the interface.
Craig Topper353eda42014-04-24 06:44:33 +000080 return nullptr;
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000081 }
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000082
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000083 // Save this address as the basis for our next request
84 MemGroup.Near = MB;
85
Keno Fischer17433bd2015-10-01 02:45:07 +000086 MemGroup.PendingMem.push_back(MB);
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000087 Addr = (uintptr_t)MB.base();
88 uintptr_t EndOfBlock = Addr + MB.size();
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000089
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000090 // Align the address.
91 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000092
93 // The allocateMappedMemory may allocate much more memory than we need. In
94 // this case, we store the unused memory as a free memory block.
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000095 unsigned FreeSize = EndOfBlock-Addr-Size;
96 if (FreeSize > 16)
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000097 MemGroup.FreeMem.push_back(sys::MemoryBlock((void*)(Addr + Size), FreeSize));
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000098
99 // Return aligned address
100 return (uint8_t*)Addr;
101}
102
David Tweed2e7efed2013-05-17 10:01:46 +0000103bool SectionMemoryManager::finalizeMemory(std::string *ErrMsg)
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000104{
105 // FIXME: Should in-progress permissions be reverted if an error occurs?
Rafael Espindola3acea392014-06-12 21:46:39 +0000106 std::error_code ec;
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000107
Andrew Kaylorea395922013-10-01 01:47:35 +0000108 // Don't allow free memory blocks to be used after setting protection flags.
109 CodeMem.FreeMem.clear();
110
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000111 // Make code memory executable.
112 ec = applyMemoryGroupPermissions(CodeMem,
113 sys::Memory::MF_READ | sys::Memory::MF_EXEC);
114 if (ec) {
115 if (ErrMsg) {
116 *ErrMsg = ec.message();
117 }
118 return true;
119 }
120
Andrew Kaylorea395922013-10-01 01:47:35 +0000121 // Don't allow free memory blocks to be used after setting protection flags.
122 RODataMem.FreeMem.clear();
123
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000124 // Make read-only data memory read-only.
125 ec = applyMemoryGroupPermissions(RODataMem,
126 sys::Memory::MF_READ | sys::Memory::MF_EXEC);
127 if (ec) {
128 if (ErrMsg) {
129 *ErrMsg = ec.message();
130 }
131 return true;
132 }
133
134 // Read-write data memory already has the correct permissions
135
Andrew Kaylorf91b5ac2013-04-24 22:39:12 +0000136 // Some platforms with separate data cache and instruction cache require
137 // explicit cache flush, otherwise JIT code manipulations (like resolved
138 // relocations) will get to the data cache but not to the instruction cache.
139 invalidateInstructionCache();
140
Keno Fischer17433bd2015-10-01 02:45:07 +0000141 // Now, remember that we have successfully applied the permissions to avoid
142 // having to apply them again.
143 CodeMem.AllocatedMem.append(CodeMem.PendingMem.begin(),CodeMem.PendingMem.end());
144 CodeMem.PendingMem.clear();
145
146 RODataMem.AllocatedMem.append(RODataMem.PendingMem.begin(),RODataMem.PendingMem.end());
147 RODataMem.PendingMem.clear();
148
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000149 return false;
150}
151
Rafael Espindola3acea392014-06-12 21:46:39 +0000152std::error_code
153SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
154 unsigned Permissions) {
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000155
Keno Fischer17433bd2015-10-01 02:45:07 +0000156 for (sys::MemoryBlock &MB : MemGroup.PendingMem)
Benjamin Kramerefeddcc2015-08-31 13:39:14 +0000157 if (std::error_code EC = sys::Memory::protectMappedMemory(MB, Permissions))
158 return EC;
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000159
Rafael Espindola3acea392014-06-12 21:46:39 +0000160 return std::error_code();
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000161}
162
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000163void SectionMemoryManager::invalidateInstructionCache() {
Keno Fischer17433bd2015-10-01 02:45:07 +0000164 for (sys::MemoryBlock &Block : CodeMem.PendingMem)
Benjamin Kramerefeddcc2015-08-31 13:39:14 +0000165 sys::Memory::InvalidateInstructionCache(Block.base(), Block.size());
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000166}
167
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000168SectionMemoryManager::~SectionMemoryManager() {
Keno Fischer17433bd2015-10-01 02:45:07 +0000169 for (MemoryGroup *Group : {&CodeMem, &RWDataMem, &RODataMem}) {
Benjamin Kramerefeddcc2015-08-31 13:39:14 +0000170 for (sys::MemoryBlock &Block : Group->AllocatedMem)
171 sys::Memory::releaseMappedMemory(Block);
Keno Fischer17433bd2015-10-01 02:45:07 +0000172 for (sys::MemoryBlock &Block : Group->PendingMem)
173 sys::Memory::releaseMappedMemory(Block);
174 }
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000175}
176
177} // namespace llvm
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000178