Put RTDyldMemoryManager into its own file, and make it linked into
[oota-llvm.git] / lib / ExecutionEngine / MCJIT / SectionMemoryManager.cpp
1 //===- SectionMemoryManager.cpp - Memory manager for MCJIT/RtDyld *- C++ -*-==//
2 //
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 //
10 // This file implements the section-based memory manager used by the MCJIT
11 // execution engine and RuntimeDyld
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Config/config.h"
16 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
17 #include "llvm/Support/MathExtras.h"
18
19 #ifdef __linux__
20   // These includes used by SectionMemoryManager::getPointerToNamedFunction()
21   // for Glibc trickery. See comments in this function for more information.
22   #ifdef HAVE_SYS_STAT_H
23     #include <sys/stat.h>
24   #endif
25   #include <fcntl.h>
26   #include <unistd.h>
27 #endif
28
29 namespace llvm {
30
31 uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size,
32                                                     unsigned Alignment,
33                                                     unsigned SectionID,
34                                                     bool IsReadOnly) {
35   if (IsReadOnly)
36     return allocateSection(RODataMem, Size, Alignment);
37   return allocateSection(RWDataMem, Size, Alignment);
38 }
39
40 uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
41                                                    unsigned Alignment,
42                                                    unsigned SectionID) {
43   return allocateSection(CodeMem, Size, Alignment);
44 }
45
46 uint8_t *SectionMemoryManager::allocateSection(MemoryGroup &MemGroup,
47                                                uintptr_t Size,
48                                                unsigned Alignment) {
49   if (!Alignment)
50     Alignment = 16;
51
52   assert(!(Alignment & (Alignment - 1)) && "Alignment must be a power of two.");
53
54   uintptr_t RequiredSize = Alignment * ((Size + Alignment - 1)/Alignment + 1);
55   uintptr_t Addr = 0;
56
57   // Look in the list of free memory regions and use a block there if one
58   // is available.
59   for (int i = 0, e = MemGroup.FreeMem.size(); i != e; ++i) {
60     sys::MemoryBlock &MB = MemGroup.FreeMem[i];
61     if (MB.size() >= RequiredSize) {
62       Addr = (uintptr_t)MB.base();
63       uintptr_t EndOfBlock = Addr + MB.size();
64       // Align the address.
65       Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
66       // Store cutted free memory block.
67       MemGroup.FreeMem[i] = sys::MemoryBlock((void*)(Addr + Size),
68                                              EndOfBlock - Addr - Size);
69       return (uint8_t*)Addr;
70     }
71   }
72
73   // No pre-allocated free block was large enough. Allocate a new memory region.
74   // Note that all sections get allocated as read-write.  The permissions will
75   // be updated later based on memory group.
76   //
77   // FIXME: It would be useful to define a default allocation size (or add
78   // it as a constructor parameter) to minimize the number of allocations.
79   //
80   // FIXME: Initialize the Near member for each memory group to avoid
81   // interleaving.
82   error_code ec;
83   sys::MemoryBlock MB = sys::Memory::allocateMappedMemory(RequiredSize,
84                                                           &MemGroup.Near,
85                                                           sys::Memory::MF_READ |
86                                                             sys::Memory::MF_WRITE,
87                                                           ec);
88   if (ec) {
89     // FIXME: Add error propogation to the interface.
90     return NULL;
91   }
92
93   // Save this address as the basis for our next request
94   MemGroup.Near = MB;
95
96   MemGroup.AllocatedMem.push_back(MB);
97   Addr = (uintptr_t)MB.base();
98   uintptr_t EndOfBlock = Addr + MB.size();
99
100   // Align the address.
101   Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
102
103   // The allocateMappedMemory may allocate much more memory than we need. In
104   // this case, we store the unused memory as a free memory block.
105   unsigned FreeSize = EndOfBlock-Addr-Size;
106   if (FreeSize > 16)
107     MemGroup.FreeMem.push_back(sys::MemoryBlock((void*)(Addr + Size), FreeSize));
108
109   // Return aligned address
110   return (uint8_t*)Addr;
111 }
112
113 bool SectionMemoryManager::finalizeMemory(std::string *ErrMsg)
114 {
115   // FIXME: Should in-progress permissions be reverted if an error occurs?
116   error_code ec;
117
118   // Make code memory executable.
119   ec = applyMemoryGroupPermissions(CodeMem,
120                                    sys::Memory::MF_READ | sys::Memory::MF_EXEC);
121   if (ec) {
122     if (ErrMsg) {
123       *ErrMsg = ec.message();
124     }
125     return true;
126   }
127
128   // Make read-only data memory read-only.
129   ec = applyMemoryGroupPermissions(RODataMem,
130                                    sys::Memory::MF_READ | sys::Memory::MF_EXEC);
131   if (ec) {
132     if (ErrMsg) {
133       *ErrMsg = ec.message();
134     }
135     return true;
136   }
137
138   // Read-write data memory already has the correct permissions
139
140   // Some platforms with separate data cache and instruction cache require
141   // explicit cache flush, otherwise JIT code manipulations (like resolved
142   // relocations) will get to the data cache but not to the instruction cache.
143   invalidateInstructionCache();
144
145   return false;
146 }
147
148 error_code SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
149                                                              unsigned Permissions) {
150
151   for (int i = 0, e = MemGroup.AllocatedMem.size(); i != e; ++i) {
152       error_code ec;
153       ec = sys::Memory::protectMappedMemory(MemGroup.AllocatedMem[i],
154                                             Permissions);
155       if (ec) {
156         return ec;
157       }
158   }
159
160   return error_code::success();
161 }
162
163 void SectionMemoryManager::invalidateInstructionCache() {
164   for (int i = 0, e = CodeMem.AllocatedMem.size(); i != e; ++i)
165     sys::Memory::InvalidateInstructionCache(CodeMem.AllocatedMem[i].base(),
166                                             CodeMem.AllocatedMem[i].size());
167 }
168
169 SectionMemoryManager::~SectionMemoryManager() {
170   for (unsigned i = 0, e = CodeMem.AllocatedMem.size(); i != e; ++i)
171     sys::Memory::releaseMappedMemory(CodeMem.AllocatedMem[i]);
172   for (unsigned i = 0, e = RWDataMem.AllocatedMem.size(); i != e; ++i)
173     sys::Memory::releaseMappedMemory(RWDataMem.AllocatedMem[i]);
174   for (unsigned i = 0, e = RODataMem.AllocatedMem.size(); i != e; ++i)
175     sys::Memory::releaseMappedMemory(RODataMem.AllocatedMem[i]);
176 }
177
178 } // namespace llvm
179