blob: ba277796dd53fc7c25ee1b45576ab3e8da718f1f [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Kaylor5e7d7922012-10-04 20:29:44 +00006//
7//===----------------------------------------------------------------------===//
8//
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +00009// This file implements the section-based memory manager used by the MCJIT
10// execution engine and RuntimeDyld
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000011//
12//===----------------------------------------------------------------------===//
13
Andrew Kaylorab5ba512012-11-27 19:42:02 +000014#include "llvm/ExecutionEngine/SectionMemoryManager.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/Config/config.h"
Amara Emersoneb7fb842012-10-31 17:41:51 +000016#include "llvm/Support/MathExtras.h"
Keno Fischer94f181a2015-12-16 11:13:23 +000017#include "llvm/Support/Process.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)
Sanjoy Dase3992c62017-11-09 06:31:33 +000027 return allocateSection(SectionMemoryManager::AllocationPurpose::ROData,
28 Size, Alignment);
29 return allocateSection(SectionMemoryManager::AllocationPurpose::RWData, Size,
30 Alignment);
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000031}
32
33uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000034 unsigned Alignment,
Filip Pizlo7aa695e02013-10-02 00:59:25 +000035 unsigned SectionID,
36 StringRef SectionName) {
Sanjoy Dase3992c62017-11-09 06:31:33 +000037 return allocateSection(SectionMemoryManager::AllocationPurpose::Code, Size,
38 Alignment);
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000039}
40
Sanjoy Dase3992c62017-11-09 06:31:33 +000041uint8_t *SectionMemoryManager::allocateSection(
42 SectionMemoryManager::AllocationPurpose Purpose, uintptr_t Size,
43 unsigned Alignment) {
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000044 if (!Alignment)
45 Alignment = 16;
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000046
47 assert(!(Alignment & (Alignment - 1)) && "Alignment must be a power of two.");
48
Sanjoy Dase3992c62017-11-09 06:31:33 +000049 uintptr_t RequiredSize = Alignment * ((Size + Alignment - 1) / Alignment + 1);
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000050 uintptr_t Addr = 0;
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000051
Sanjoy Dase3992c62017-11-09 06:31:33 +000052 MemoryGroup &MemGroup = [&]() -> MemoryGroup & {
53 switch (Purpose) {
54 case AllocationPurpose::Code:
55 return CodeMem;
56 case AllocationPurpose::ROData:
57 return RODataMem;
58 case AllocationPurpose::RWData:
59 return RWDataMem;
60 }
Simon Pilgrim89d31652017-11-09 14:56:17 +000061 llvm_unreachable("Unknown SectionMemoryManager::AllocationPurpose");
Sanjoy Dase3992c62017-11-09 06:31:33 +000062 }();
63
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000064 // Look in the list of free memory regions and use a block there if one
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000065 // is available.
Keno Fischer94f181a2015-12-16 11:13:23 +000066 for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
67 if (FreeMB.Free.size() >= RequiredSize) {
68 Addr = (uintptr_t)FreeMB.Free.base();
69 uintptr_t EndOfBlock = Addr + FreeMB.Free.size();
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000070 // Align the address.
71 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
Keno Fischer94f181a2015-12-16 11:13:23 +000072
73 if (FreeMB.PendingPrefixIndex == (unsigned)-1) {
74 // The part of the block we're giving out to the user is now pending
75 MemGroup.PendingMem.push_back(sys::MemoryBlock((void *)Addr, Size));
76
77 // Remember this pending block, such that future allocations can just
78 // modify it rather than creating a new one
79 FreeMB.PendingPrefixIndex = MemGroup.PendingMem.size() - 1;
80 } else {
Sanjoy Dase3992c62017-11-09 06:31:33 +000081 sys::MemoryBlock &PendingMB =
82 MemGroup.PendingMem[FreeMB.PendingPrefixIndex];
83 PendingMB = sys::MemoryBlock(PendingMB.base(),
84 Addr + Size - (uintptr_t)PendingMB.base());
Keno Fischer94f181a2015-12-16 11:13:23 +000085 }
86
87 // Remember how much free space is now left in this block
Sanjoy Dase3992c62017-11-09 06:31:33 +000088 FreeMB.Free =
89 sys::MemoryBlock((void *)(Addr + Size), EndOfBlock - Addr - Size);
90 return (uint8_t *)Addr;
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000091 }
92 }
93
94 // No pre-allocated free block was large enough. Allocate a new memory region.
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +000095 // Note that all sections get allocated as read-write. The permissions will
96 // be updated later based on memory group.
97 //
98 // FIXME: It would be useful to define a default allocation size (or add
99 // it as a constructor parameter) to minimize the number of allocations.
Andrew Kaylorab5ba512012-11-27 19:42:02 +0000100 //
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000101 // FIXME: Initialize the Near member for each memory group to avoid
102 // interleaving.
Rafael Espindola3acea392014-06-12 21:46:39 +0000103 std::error_code ec;
Sanjoy Dase3992c62017-11-09 06:31:33 +0000104 sys::MemoryBlock MB = MMapper.allocateMappedMemory(
105 Purpose, RequiredSize, &MemGroup.Near,
106 sys::Memory::MF_READ | sys::Memory::MF_WRITE, ec);
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000107 if (ec) {
Alp Tokercb402912014-01-24 17:20:08 +0000108 // FIXME: Add error propagation to the interface.
Craig Topper353eda42014-04-24 06:44:33 +0000109 return nullptr;
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000110 }
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000111
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000112 // Save this address as the basis for our next request
113 MemGroup.Near = MB;
114
Keno Fischer94f181a2015-12-16 11:13:23 +0000115 // Remember that we allocated this memory
116 MemGroup.AllocatedMem.push_back(MB);
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000117 Addr = (uintptr_t)MB.base();
118 uintptr_t EndOfBlock = Addr + MB.size();
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000119
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000120 // Align the address.
121 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000122
Keno Fischer94f181a2015-12-16 11:13:23 +0000123 // The part of the block we're giving out to the user is now pending
124 MemGroup.PendingMem.push_back(sys::MemoryBlock((void *)Addr, Size));
125
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000126 // The allocateMappedMemory may allocate much more memory than we need. In
127 // this case, we store the unused memory as a free memory block.
Sanjoy Dase3992c62017-11-09 06:31:33 +0000128 unsigned FreeSize = EndOfBlock - Addr - Size;
Keno Fischer94f181a2015-12-16 11:13:23 +0000129 if (FreeSize > 16) {
130 FreeMemBlock FreeMB;
Sanjoy Dase3992c62017-11-09 06:31:33 +0000131 FreeMB.Free = sys::MemoryBlock((void *)(Addr + Size), FreeSize);
Keno Fischer94f181a2015-12-16 11:13:23 +0000132 FreeMB.PendingPrefixIndex = (unsigned)-1;
133 MemGroup.FreeMem.push_back(FreeMB);
134 }
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000135
136 // Return aligned address
Sanjoy Dase3992c62017-11-09 06:31:33 +0000137 return (uint8_t *)Addr;
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000138}
139
Sanjoy Dase3992c62017-11-09 06:31:33 +0000140bool SectionMemoryManager::finalizeMemory(std::string *ErrMsg) {
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000141 // FIXME: Should in-progress permissions be reverted if an error occurs?
Rafael Espindola3acea392014-06-12 21:46:39 +0000142 std::error_code ec;
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000143
144 // Make code memory executable.
145 ec = applyMemoryGroupPermissions(CodeMem,
146 sys::Memory::MF_READ | sys::Memory::MF_EXEC);
147 if (ec) {
148 if (ErrMsg) {
149 *ErrMsg = ec.message();
150 }
151 return true;
152 }
153
154 // Make read-only data memory read-only.
155 ec = applyMemoryGroupPermissions(RODataMem,
156 sys::Memory::MF_READ | sys::Memory::MF_EXEC);
157 if (ec) {
158 if (ErrMsg) {
159 *ErrMsg = ec.message();
160 }
161 return true;
162 }
163
164 // Read-write data memory already has the correct permissions
165
Andrew Kaylorf91b5ac2013-04-24 22:39:12 +0000166 // Some platforms with separate data cache and instruction cache require
167 // explicit cache flush, otherwise JIT code manipulations (like resolved
168 // relocations) will get to the data cache but not to the instruction cache.
169 invalidateInstructionCache();
170
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000171 return false;
172}
173
Keno Fischer94f181a2015-12-16 11:13:23 +0000174static sys::MemoryBlock trimBlockToPageSize(sys::MemoryBlock M) {
175 static const size_t PageSize = sys::Process::getPageSize();
176
177 size_t StartOverlap =
178 (PageSize - ((uintptr_t)M.base() % PageSize)) % PageSize;
179
180 size_t TrimmedSize = M.size();
181 TrimmedSize -= StartOverlap;
182 TrimmedSize -= TrimmedSize % PageSize;
183
Sanjoy Dase3992c62017-11-09 06:31:33 +0000184 sys::MemoryBlock Trimmed((void *)((uintptr_t)M.base() + StartOverlap),
185 TrimmedSize);
Keno Fischer94f181a2015-12-16 11:13:23 +0000186
187 assert(((uintptr_t)Trimmed.base() % PageSize) == 0);
188 assert((Trimmed.size() % PageSize) == 0);
189 assert(M.base() <= Trimmed.base() && Trimmed.size() <= M.size());
190
191 return Trimmed;
192}
193
Rafael Espindola3acea392014-06-12 21:46:39 +0000194std::error_code
195SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
196 unsigned Permissions) {
Keno Fischer17433bd2015-10-01 02:45:07 +0000197 for (sys::MemoryBlock &MB : MemGroup.PendingMem)
Sanjoy Dase3992c62017-11-09 06:31:33 +0000198 if (std::error_code EC = MMapper.protectMappedMemory(MB, Permissions))
Benjamin Kramerefeddcc2015-08-31 13:39:14 +0000199 return EC;
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000200
Keno Fischer94f181a2015-12-16 11:13:23 +0000201 MemGroup.PendingMem.clear();
202
203 // Now go through free blocks and trim any of them that don't span the entire
204 // page because one of the pending blocks may have overlapped it.
205 for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
206 FreeMB.Free = trimBlockToPageSize(FreeMB.Free);
207 // We cleared the PendingMem list, so all these pointers are now invalid
208 FreeMB.PendingPrefixIndex = (unsigned)-1;
209 }
210
211 // Remove all blocks which are now empty
212 MemGroup.FreeMem.erase(
David Majnemerc7004902016-08-12 04:32:37 +0000213 remove_if(MemGroup.FreeMem,
214 [](FreeMemBlock &FreeMB) { return FreeMB.Free.size() == 0; }),
Keno Fischer94f181a2015-12-16 11:13:23 +0000215 MemGroup.FreeMem.end());
216
Rafael Espindola3acea392014-06-12 21:46:39 +0000217 return std::error_code();
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000218}
219
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000220void SectionMemoryManager::invalidateInstructionCache() {
Keno Fischer17433bd2015-10-01 02:45:07 +0000221 for (sys::MemoryBlock &Block : CodeMem.PendingMem)
Benjamin Kramerefeddcc2015-08-31 13:39:14 +0000222 sys::Memory::InvalidateInstructionCache(Block.base(), Block.size());
Andrew Kaylor3e0f1fb2012-11-27 19:00:17 +0000223}
224
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000225SectionMemoryManager::~SectionMemoryManager() {
Keno Fischer17433bd2015-10-01 02:45:07 +0000226 for (MemoryGroup *Group : {&CodeMem, &RWDataMem, &RODataMem}) {
Benjamin Kramerefeddcc2015-08-31 13:39:14 +0000227 for (sys::MemoryBlock &Block : Group->AllocatedMem)
Sanjoy Dase3992c62017-11-09 06:31:33 +0000228 MMapper.releaseMappedMemory(Block);
Keno Fischer17433bd2015-10-01 02:45:07 +0000229 }
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000230}
231
Sanjoy Dase3992c62017-11-09 06:31:33 +0000232SectionMemoryManager::MemoryMapper::~MemoryMapper() {}
233
Weiming Zhao1bd40002018-04-11 23:09:20 +0000234void SectionMemoryManager::anchor() {}
235
Sanjoy Dase3992c62017-11-09 06:31:33 +0000236namespace {
237// Trivial implementation of SectionMemoryManager::MemoryMapper that just calls
238// into sys::Memory.
239class DefaultMMapper final : public SectionMemoryManager::MemoryMapper {
240public:
241 sys::MemoryBlock
242 allocateMappedMemory(SectionMemoryManager::AllocationPurpose Purpose,
243 size_t NumBytes, const sys::MemoryBlock *const NearBlock,
244 unsigned Flags, std::error_code &EC) override {
Rui Ueyama7c8fc812019-01-23 02:03:26 +0000245 // allocateMappedMemory calls mmap(2). We round up a request size
246 // to page size to get extra space for free.
247 static const size_t PageSize = sys::Process::getPageSize();
248 size_t ReqBytes = (NumBytes + PageSize - 1) & ~(PageSize - 1);
249 return sys::Memory::allocateMappedMemory(ReqBytes, NearBlock, Flags, EC);
Sanjoy Dase3992c62017-11-09 06:31:33 +0000250 }
251
252 std::error_code protectMappedMemory(const sys::MemoryBlock &Block,
253 unsigned Flags) override {
254 return sys::Memory::protectMappedMemory(Block, Flags);
255 }
256
257 std::error_code releaseMappedMemory(sys::MemoryBlock &M) override {
258 return sys::Memory::releaseMappedMemory(M);
259 }
260};
261
262DefaultMMapper DefaultMMapperInstance;
263} // namespace
264
265SectionMemoryManager::SectionMemoryManager(MemoryMapper *MM)
266 : MMapper(MM ? *MM : DefaultMMapperInstance) {}
267
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000268} // namespace llvm