1*9880d681SAndroid Build Coastguard Worker //===- SectionMemoryManager.cpp - Memory manager for MCJIT/RtDyld *- C++ -*-==//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the section-based memory manager used by the MCJIT
11*9880d681SAndroid Build Coastguard Worker // execution engine and RuntimeDyld
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard Worker #include "llvm/Config/config.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/ExecutionEngine/SectionMemoryManager.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/MathExtras.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Process.h"
19*9880d681SAndroid Build Coastguard Worker
20*9880d681SAndroid Build Coastguard Worker namespace llvm {
21*9880d681SAndroid Build Coastguard Worker
allocateDataSection(uintptr_t Size,unsigned Alignment,unsigned SectionID,StringRef SectionName,bool IsReadOnly)22*9880d681SAndroid Build Coastguard Worker uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size,
23*9880d681SAndroid Build Coastguard Worker unsigned Alignment,
24*9880d681SAndroid Build Coastguard Worker unsigned SectionID,
25*9880d681SAndroid Build Coastguard Worker StringRef SectionName,
26*9880d681SAndroid Build Coastguard Worker bool IsReadOnly) {
27*9880d681SAndroid Build Coastguard Worker if (IsReadOnly)
28*9880d681SAndroid Build Coastguard Worker return allocateSection(RODataMem, Size, Alignment);
29*9880d681SAndroid Build Coastguard Worker return allocateSection(RWDataMem, Size, Alignment);
30*9880d681SAndroid Build Coastguard Worker }
31*9880d681SAndroid Build Coastguard Worker
allocateCodeSection(uintptr_t Size,unsigned Alignment,unsigned SectionID,StringRef SectionName)32*9880d681SAndroid Build Coastguard Worker uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
33*9880d681SAndroid Build Coastguard Worker unsigned Alignment,
34*9880d681SAndroid Build Coastguard Worker unsigned SectionID,
35*9880d681SAndroid Build Coastguard Worker StringRef SectionName) {
36*9880d681SAndroid Build Coastguard Worker return allocateSection(CodeMem, Size, Alignment);
37*9880d681SAndroid Build Coastguard Worker }
38*9880d681SAndroid Build Coastguard Worker
allocateSection(MemoryGroup & MemGroup,uintptr_t Size,unsigned Alignment)39*9880d681SAndroid Build Coastguard Worker uint8_t *SectionMemoryManager::allocateSection(MemoryGroup &MemGroup,
40*9880d681SAndroid Build Coastguard Worker uintptr_t Size,
41*9880d681SAndroid Build Coastguard Worker unsigned Alignment) {
42*9880d681SAndroid Build Coastguard Worker if (!Alignment)
43*9880d681SAndroid Build Coastguard Worker Alignment = 16;
44*9880d681SAndroid Build Coastguard Worker
45*9880d681SAndroid Build Coastguard Worker assert(!(Alignment & (Alignment - 1)) && "Alignment must be a power of two.");
46*9880d681SAndroid Build Coastguard Worker
47*9880d681SAndroid Build Coastguard Worker uintptr_t RequiredSize = Alignment * ((Size + Alignment - 1)/Alignment + 1);
48*9880d681SAndroid Build Coastguard Worker uintptr_t Addr = 0;
49*9880d681SAndroid Build Coastguard Worker
50*9880d681SAndroid Build Coastguard Worker // Look in the list of free memory regions and use a block there if one
51*9880d681SAndroid Build Coastguard Worker // is available.
52*9880d681SAndroid Build Coastguard Worker for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
53*9880d681SAndroid Build Coastguard Worker if (FreeMB.Free.size() >= RequiredSize) {
54*9880d681SAndroid Build Coastguard Worker Addr = (uintptr_t)FreeMB.Free.base();
55*9880d681SAndroid Build Coastguard Worker uintptr_t EndOfBlock = Addr + FreeMB.Free.size();
56*9880d681SAndroid Build Coastguard Worker // Align the address.
57*9880d681SAndroid Build Coastguard Worker Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard Worker if (FreeMB.PendingPrefixIndex == (unsigned)-1) {
60*9880d681SAndroid Build Coastguard Worker // The part of the block we're giving out to the user is now pending
61*9880d681SAndroid Build Coastguard Worker MemGroup.PendingMem.push_back(sys::MemoryBlock((void *)Addr, Size));
62*9880d681SAndroid Build Coastguard Worker
63*9880d681SAndroid Build Coastguard Worker // Remember this pending block, such that future allocations can just
64*9880d681SAndroid Build Coastguard Worker // modify it rather than creating a new one
65*9880d681SAndroid Build Coastguard Worker FreeMB.PendingPrefixIndex = MemGroup.PendingMem.size() - 1;
66*9880d681SAndroid Build Coastguard Worker } else {
67*9880d681SAndroid Build Coastguard Worker sys::MemoryBlock &PendingMB = MemGroup.PendingMem[FreeMB.PendingPrefixIndex];
68*9880d681SAndroid Build Coastguard Worker PendingMB = sys::MemoryBlock(PendingMB.base(), Addr + Size - (uintptr_t)PendingMB.base());
69*9880d681SAndroid Build Coastguard Worker }
70*9880d681SAndroid Build Coastguard Worker
71*9880d681SAndroid Build Coastguard Worker // Remember how much free space is now left in this block
72*9880d681SAndroid Build Coastguard Worker FreeMB.Free = sys::MemoryBlock((void *)(Addr + Size), EndOfBlock - Addr - Size);
73*9880d681SAndroid Build Coastguard Worker return (uint8_t*)Addr;
74*9880d681SAndroid Build Coastguard Worker }
75*9880d681SAndroid Build Coastguard Worker }
76*9880d681SAndroid Build Coastguard Worker
77*9880d681SAndroid Build Coastguard Worker // No pre-allocated free block was large enough. Allocate a new memory region.
78*9880d681SAndroid Build Coastguard Worker // Note that all sections get allocated as read-write. The permissions will
79*9880d681SAndroid Build Coastguard Worker // be updated later based on memory group.
80*9880d681SAndroid Build Coastguard Worker //
81*9880d681SAndroid Build Coastguard Worker // FIXME: It would be useful to define a default allocation size (or add
82*9880d681SAndroid Build Coastguard Worker // it as a constructor parameter) to minimize the number of allocations.
83*9880d681SAndroid Build Coastguard Worker //
84*9880d681SAndroid Build Coastguard Worker // FIXME: Initialize the Near member for each memory group to avoid
85*9880d681SAndroid Build Coastguard Worker // interleaving.
86*9880d681SAndroid Build Coastguard Worker std::error_code ec;
87*9880d681SAndroid Build Coastguard Worker sys::MemoryBlock MB = sys::Memory::allocateMappedMemory(RequiredSize,
88*9880d681SAndroid Build Coastguard Worker &MemGroup.Near,
89*9880d681SAndroid Build Coastguard Worker sys::Memory::MF_READ |
90*9880d681SAndroid Build Coastguard Worker sys::Memory::MF_WRITE,
91*9880d681SAndroid Build Coastguard Worker ec);
92*9880d681SAndroid Build Coastguard Worker if (ec) {
93*9880d681SAndroid Build Coastguard Worker // FIXME: Add error propagation to the interface.
94*9880d681SAndroid Build Coastguard Worker return nullptr;
95*9880d681SAndroid Build Coastguard Worker }
96*9880d681SAndroid Build Coastguard Worker
97*9880d681SAndroid Build Coastguard Worker // Save this address as the basis for our next request
98*9880d681SAndroid Build Coastguard Worker MemGroup.Near = MB;
99*9880d681SAndroid Build Coastguard Worker
100*9880d681SAndroid Build Coastguard Worker // Remember that we allocated this memory
101*9880d681SAndroid Build Coastguard Worker MemGroup.AllocatedMem.push_back(MB);
102*9880d681SAndroid Build Coastguard Worker Addr = (uintptr_t)MB.base();
103*9880d681SAndroid Build Coastguard Worker uintptr_t EndOfBlock = Addr + MB.size();
104*9880d681SAndroid Build Coastguard Worker
105*9880d681SAndroid Build Coastguard Worker // Align the address.
106*9880d681SAndroid Build Coastguard Worker Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
107*9880d681SAndroid Build Coastguard Worker
108*9880d681SAndroid Build Coastguard Worker // The part of the block we're giving out to the user is now pending
109*9880d681SAndroid Build Coastguard Worker MemGroup.PendingMem.push_back(sys::MemoryBlock((void *)Addr, Size));
110*9880d681SAndroid Build Coastguard Worker
111*9880d681SAndroid Build Coastguard Worker // The allocateMappedMemory may allocate much more memory than we need. In
112*9880d681SAndroid Build Coastguard Worker // this case, we store the unused memory as a free memory block.
113*9880d681SAndroid Build Coastguard Worker unsigned FreeSize = EndOfBlock-Addr-Size;
114*9880d681SAndroid Build Coastguard Worker if (FreeSize > 16) {
115*9880d681SAndroid Build Coastguard Worker FreeMemBlock FreeMB;
116*9880d681SAndroid Build Coastguard Worker FreeMB.Free = sys::MemoryBlock((void*)(Addr + Size), FreeSize);
117*9880d681SAndroid Build Coastguard Worker FreeMB.PendingPrefixIndex = (unsigned)-1;
118*9880d681SAndroid Build Coastguard Worker MemGroup.FreeMem.push_back(FreeMB);
119*9880d681SAndroid Build Coastguard Worker }
120*9880d681SAndroid Build Coastguard Worker
121*9880d681SAndroid Build Coastguard Worker // Return aligned address
122*9880d681SAndroid Build Coastguard Worker return (uint8_t*)Addr;
123*9880d681SAndroid Build Coastguard Worker }
124*9880d681SAndroid Build Coastguard Worker
finalizeMemory(std::string * ErrMsg)125*9880d681SAndroid Build Coastguard Worker bool SectionMemoryManager::finalizeMemory(std::string *ErrMsg)
126*9880d681SAndroid Build Coastguard Worker {
127*9880d681SAndroid Build Coastguard Worker // FIXME: Should in-progress permissions be reverted if an error occurs?
128*9880d681SAndroid Build Coastguard Worker std::error_code ec;
129*9880d681SAndroid Build Coastguard Worker
130*9880d681SAndroid Build Coastguard Worker // Make code memory executable.
131*9880d681SAndroid Build Coastguard Worker ec = applyMemoryGroupPermissions(CodeMem,
132*9880d681SAndroid Build Coastguard Worker sys::Memory::MF_READ | sys::Memory::MF_EXEC);
133*9880d681SAndroid Build Coastguard Worker if (ec) {
134*9880d681SAndroid Build Coastguard Worker if (ErrMsg) {
135*9880d681SAndroid Build Coastguard Worker *ErrMsg = ec.message();
136*9880d681SAndroid Build Coastguard Worker }
137*9880d681SAndroid Build Coastguard Worker return true;
138*9880d681SAndroid Build Coastguard Worker }
139*9880d681SAndroid Build Coastguard Worker
140*9880d681SAndroid Build Coastguard Worker // Make read-only data memory read-only.
141*9880d681SAndroid Build Coastguard Worker ec = applyMemoryGroupPermissions(RODataMem,
142*9880d681SAndroid Build Coastguard Worker sys::Memory::MF_READ | sys::Memory::MF_EXEC);
143*9880d681SAndroid Build Coastguard Worker if (ec) {
144*9880d681SAndroid Build Coastguard Worker if (ErrMsg) {
145*9880d681SAndroid Build Coastguard Worker *ErrMsg = ec.message();
146*9880d681SAndroid Build Coastguard Worker }
147*9880d681SAndroid Build Coastguard Worker return true;
148*9880d681SAndroid Build Coastguard Worker }
149*9880d681SAndroid Build Coastguard Worker
150*9880d681SAndroid Build Coastguard Worker // Read-write data memory already has the correct permissions
151*9880d681SAndroid Build Coastguard Worker
152*9880d681SAndroid Build Coastguard Worker // Some platforms with separate data cache and instruction cache require
153*9880d681SAndroid Build Coastguard Worker // explicit cache flush, otherwise JIT code manipulations (like resolved
154*9880d681SAndroid Build Coastguard Worker // relocations) will get to the data cache but not to the instruction cache.
155*9880d681SAndroid Build Coastguard Worker invalidateInstructionCache();
156*9880d681SAndroid Build Coastguard Worker
157*9880d681SAndroid Build Coastguard Worker return false;
158*9880d681SAndroid Build Coastguard Worker }
159*9880d681SAndroid Build Coastguard Worker
trimBlockToPageSize(sys::MemoryBlock M)160*9880d681SAndroid Build Coastguard Worker static sys::MemoryBlock trimBlockToPageSize(sys::MemoryBlock M) {
161*9880d681SAndroid Build Coastguard Worker static const size_t PageSize = sys::Process::getPageSize();
162*9880d681SAndroid Build Coastguard Worker
163*9880d681SAndroid Build Coastguard Worker size_t StartOverlap =
164*9880d681SAndroid Build Coastguard Worker (PageSize - ((uintptr_t)M.base() % PageSize)) % PageSize;
165*9880d681SAndroid Build Coastguard Worker
166*9880d681SAndroid Build Coastguard Worker size_t TrimmedSize = M.size();
167*9880d681SAndroid Build Coastguard Worker TrimmedSize -= StartOverlap;
168*9880d681SAndroid Build Coastguard Worker TrimmedSize -= TrimmedSize % PageSize;
169*9880d681SAndroid Build Coastguard Worker
170*9880d681SAndroid Build Coastguard Worker sys::MemoryBlock Trimmed((void *)((uintptr_t)M.base() + StartOverlap), TrimmedSize);
171*9880d681SAndroid Build Coastguard Worker
172*9880d681SAndroid Build Coastguard Worker assert(((uintptr_t)Trimmed.base() % PageSize) == 0);
173*9880d681SAndroid Build Coastguard Worker assert((Trimmed.size() % PageSize) == 0);
174*9880d681SAndroid Build Coastguard Worker assert(M.base() <= Trimmed.base() && Trimmed.size() <= M.size());
175*9880d681SAndroid Build Coastguard Worker
176*9880d681SAndroid Build Coastguard Worker return Trimmed;
177*9880d681SAndroid Build Coastguard Worker }
178*9880d681SAndroid Build Coastguard Worker
179*9880d681SAndroid Build Coastguard Worker
180*9880d681SAndroid Build Coastguard Worker std::error_code
applyMemoryGroupPermissions(MemoryGroup & MemGroup,unsigned Permissions)181*9880d681SAndroid Build Coastguard Worker SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
182*9880d681SAndroid Build Coastguard Worker unsigned Permissions) {
183*9880d681SAndroid Build Coastguard Worker for (sys::MemoryBlock &MB : MemGroup.PendingMem)
184*9880d681SAndroid Build Coastguard Worker if (std::error_code EC = sys::Memory::protectMappedMemory(MB, Permissions))
185*9880d681SAndroid Build Coastguard Worker return EC;
186*9880d681SAndroid Build Coastguard Worker
187*9880d681SAndroid Build Coastguard Worker MemGroup.PendingMem.clear();
188*9880d681SAndroid Build Coastguard Worker
189*9880d681SAndroid Build Coastguard Worker // Now go through free blocks and trim any of them that don't span the entire
190*9880d681SAndroid Build Coastguard Worker // page because one of the pending blocks may have overlapped it.
191*9880d681SAndroid Build Coastguard Worker for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
192*9880d681SAndroid Build Coastguard Worker FreeMB.Free = trimBlockToPageSize(FreeMB.Free);
193*9880d681SAndroid Build Coastguard Worker // We cleared the PendingMem list, so all these pointers are now invalid
194*9880d681SAndroid Build Coastguard Worker FreeMB.PendingPrefixIndex = (unsigned)-1;
195*9880d681SAndroid Build Coastguard Worker }
196*9880d681SAndroid Build Coastguard Worker
197*9880d681SAndroid Build Coastguard Worker // Remove all blocks which are now empty
198*9880d681SAndroid Build Coastguard Worker MemGroup.FreeMem.erase(
199*9880d681SAndroid Build Coastguard Worker std::remove_if(MemGroup.FreeMem.begin(), MemGroup.FreeMem.end(),
200*9880d681SAndroid Build Coastguard Worker [](FreeMemBlock &FreeMB) { return FreeMB.Free.size() == 0; }),
201*9880d681SAndroid Build Coastguard Worker MemGroup.FreeMem.end());
202*9880d681SAndroid Build Coastguard Worker
203*9880d681SAndroid Build Coastguard Worker return std::error_code();
204*9880d681SAndroid Build Coastguard Worker }
205*9880d681SAndroid Build Coastguard Worker
invalidateInstructionCache()206*9880d681SAndroid Build Coastguard Worker void SectionMemoryManager::invalidateInstructionCache() {
207*9880d681SAndroid Build Coastguard Worker for (sys::MemoryBlock &Block : CodeMem.PendingMem)
208*9880d681SAndroid Build Coastguard Worker sys::Memory::InvalidateInstructionCache(Block.base(), Block.size());
209*9880d681SAndroid Build Coastguard Worker }
210*9880d681SAndroid Build Coastguard Worker
~SectionMemoryManager()211*9880d681SAndroid Build Coastguard Worker SectionMemoryManager::~SectionMemoryManager() {
212*9880d681SAndroid Build Coastguard Worker for (MemoryGroup *Group : {&CodeMem, &RWDataMem, &RODataMem}) {
213*9880d681SAndroid Build Coastguard Worker for (sys::MemoryBlock &Block : Group->AllocatedMem)
214*9880d681SAndroid Build Coastguard Worker sys::Memory::releaseMappedMemory(Block);
215*9880d681SAndroid Build Coastguard Worker }
216*9880d681SAndroid Build Coastguard Worker }
217*9880d681SAndroid Build Coastguard Worker
218*9880d681SAndroid Build Coastguard Worker } // namespace llvm
219