blob: 8904475f084f13033bbaa678f7bdcca78b53980c [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
Andrew Kaylorab5ba512012-11-27 19:42:02 +000015#include "llvm/ExecutionEngine/SectionMemoryManager.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "llvm/Config/config.h"
Amara Emersoneb7fb842012-10-31 17:41:51 +000017#include "llvm/Support/MathExtras.h"
Keno Fischer94f181a2015-12-16 11:13:23 +000018#include "llvm/Support/Process.h"
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000019
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000020namespace llvm {
21
22uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size,
Filip Pizlo7aa695e02013-10-02 00:59:25 +000023 unsigned Alignment,
24 unsigned SectionID,
25 StringRef SectionName,
26 bool IsReadOnly) {
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000027 if (IsReadOnly)
28 return allocateSection(RODataMem, Size, Alignment);
29 return allocateSection(RWDataMem, Size, Alignment);
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000030}
31
32uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000033 unsigned Alignment,
Filip Pizlo7aa695e02013-10-02 00:59:25 +000034 unsigned SectionID,
35 StringRef SectionName) {
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000036 return allocateSection(CodeMem, Size, Alignment);
37}
38
39uint8_t *SectionMemoryManager::allocateSection(MemoryGroup &MemGroup,
40 uintptr_t Size,
41 unsigned Alignment) {
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000042 if (!Alignment)
43 Alignment = 16;
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000044
45 assert(!(Alignment & (Alignment - 1)) && "Alignment must be a power of two.");
46
47 uintptr_t RequiredSize = Alignment * ((Size + Alignment - 1)/Alignment + 1);
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000048 uintptr_t Addr = 0;
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000049
50 // Look in the list of free memory regions and use a block there if one
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000051 // is available.
Keno Fischer94f181a2015-12-16 11:13:23 +000052 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();
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000056 // Align the address.
57 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
Keno Fischer94f181a2015-12-16 11:13:23 +000058
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));
62
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;
66 } else {
67 sys::MemoryBlock &PendingMB = MemGroup.PendingMem[FreeMB.PendingPrefixIndex];
68 PendingMB = sys::MemoryBlock(PendingMB.base(), Addr + Size - (uintptr_t)PendingMB.base());
69 }
70
71 // Remember how much free space is now left in this block
72 FreeMB.Free = sys::MemoryBlock((void *)(Addr + Size), EndOfBlock - Addr - Size);
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000073 return (uint8_t*)Addr;
74 }
75 }
76
77 // No pre-allocated free block was large enough. Allocate a new memory region.
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000078 // Note that all sections get allocated as read-write. The permissions will
79 // be updated later based on memory group.
80 //
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.
Andrew Kaylorab5ba512012-11-27 19:42:02 +000083 //
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000084 // FIXME: Initialize the Near member for each memory group to avoid
85 // interleaving.
Rafael Espindola3acea392014-06-12 21:46:39 +000086 std::error_code ec;
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000087 sys::MemoryBlock MB = sys::Memory::allocateMappedMemory(RequiredSize,
88 &MemGroup.Near,
89 sys::Memory::MF_READ |
90 sys::Memory::MF_WRITE,
91 ec);
92 if (ec) {
Alp Tokercb402912014-01-24 17:20:08 +000093 // FIXME: Add error propagation to the interface.
Craig Topper353eda42014-04-24 06:44:33 +000094 return nullptr;
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000095 }
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000096
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000097 // Save this address as the basis for our next request
98 MemGroup.Near = MB;
99
Keno Fischer94f181a2015-12-16 11:13:23 +0000100 // Remember that we allocated this memory
101 MemGroup.AllocatedMem.push_back(MB);
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000102 Addr = (uintptr_t)MB.base();
103 uintptr_t EndOfBlock = Addr + MB.size();
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000104
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000105 // Align the address.
106 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000107
Keno Fischer94f181a2015-12-16 11:13:23 +0000108 // 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));
110
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000111 // The allocateMappedMemory may allocate much more memory than we need. In
112 // this case, we store the unused memory as a free memory block.
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000113 unsigned FreeSize = EndOfBlock-Addr-Size;
Keno Fischer94f181a2015-12-16 11:13:23 +0000114 if (FreeSize > 16) {
115 FreeMemBlock FreeMB;
116 FreeMB.Free = sys::MemoryBlock((void*)(Addr + Size), FreeSize);
117 FreeMB.PendingPrefixIndex = (unsigned)-1;
118 MemGroup.FreeMem.push_back(FreeMB);
119 }
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000120
121 // Return aligned address
122 return (uint8_t*)Addr;
123}
124
David Tweed2e7efed2013-05-17 10:01:46 +0000125bool SectionMemoryManager::finalizeMemory(std::string *ErrMsg)
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000126{
127 // FIXME: Should in-progress permissions be reverted if an error occurs?
Rafael Espindola3acea392014-06-12 21:46:39 +0000128 std::error_code ec;
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000129
130 // Make code memory executable.
131 ec = applyMemoryGroupPermissions(CodeMem,
132 sys::Memory::MF_READ | sys::Memory::MF_EXEC);
133 if (ec) {
134 if (ErrMsg) {
135 *ErrMsg = ec.message();
136 }
137 return true;
138 }
139
140 // Make read-only data memory read-only.
141 ec = applyMemoryGroupPermissions(RODataMem,
142 sys::Memory::MF_READ | sys::Memory::MF_EXEC);
143 if (ec) {
144 if (ErrMsg) {
145 *ErrMsg = ec.message();
146 }
147 return true;
148 }
149
150 // Read-write data memory already has the correct permissions
151
Andrew Kaylorf91b5ac2013-04-24 22:39:12 +0000152 // 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();
156
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000157 return false;
158}
159
Keno Fischer94f181a2015-12-16 11:13:23 +0000160static sys::MemoryBlock trimBlockToPageSize(sys::MemoryBlock M) {
161 static const size_t PageSize = sys::Process::getPageSize();
162
163 size_t StartOverlap =
164 (PageSize - ((uintptr_t)M.base() % PageSize)) % PageSize;
165
166 size_t TrimmedSize = M.size();
167 TrimmedSize -= StartOverlap;
168 TrimmedSize -= TrimmedSize % PageSize;
169
170 sys::MemoryBlock Trimmed((void *)((uintptr_t)M.base() + StartOverlap), TrimmedSize);
171
172 assert(((uintptr_t)Trimmed.base() % PageSize) == 0);
173 assert((Trimmed.size() % PageSize) == 0);
174 assert(M.base() <= Trimmed.base() && Trimmed.size() <= M.size());
175
176 return Trimmed;
177}
178
179
Rafael Espindola3acea392014-06-12 21:46:39 +0000180std::error_code
181SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
182 unsigned Permissions) {
Keno Fischer17433bd2015-10-01 02:45:07 +0000183 for (sys::MemoryBlock &MB : MemGroup.PendingMem)
Benjamin Kramerefeddcc2015-08-31 13:39:14 +0000184 if (std::error_code EC = sys::Memory::protectMappedMemory(MB, Permissions))
185 return EC;
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000186
Keno Fischer94f181a2015-12-16 11:13:23 +0000187 MemGroup.PendingMem.clear();
188
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;
195 }
196
197 // Remove all blocks which are now empty
198 MemGroup.FreeMem.erase(
David Majnemerc7004902016-08-12 04:32:37 +0000199 remove_if(MemGroup.FreeMem,
200 [](FreeMemBlock &FreeMB) { return FreeMB.Free.size() == 0; }),
Keno Fischer94f181a2015-12-16 11:13:23 +0000201 MemGroup.FreeMem.end());
202
Rafael Espindola3acea392014-06-12 21:46:39 +0000203 return std::error_code();
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000204}
205
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000206void SectionMemoryManager::invalidateInstructionCache() {
Keno Fischer17433bd2015-10-01 02:45:07 +0000207 for (sys::MemoryBlock &Block : CodeMem.PendingMem)
Benjamin Kramerefeddcc2015-08-31 13:39:14 +0000208 sys::Memory::InvalidateInstructionCache(Block.base(), Block.size());
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000209}
210
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000211SectionMemoryManager::~SectionMemoryManager() {
Keno Fischer17433bd2015-10-01 02:45:07 +0000212 for (MemoryGroup *Group : {&CodeMem, &RWDataMem, &RODataMem}) {
Benjamin Kramerefeddcc2015-08-31 13:39:14 +0000213 for (sys::MemoryBlock &Block : Group->AllocatedMem)
214 sys::Memory::releaseMappedMemory(Block);
Keno Fischer17433bd2015-10-01 02:45:07 +0000215 }
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000216}
217
218} // namespace llvm