Adding multiple module support for MCJIT.
[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 namespace llvm {
20
21 uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size,
22                                                     unsigned Alignment,
23                                                     unsigned SectionID,
24                                                     bool IsReadOnly) {
25   if (IsReadOnly)
26     return allocateSection(RODataMem, Size, Alignment);
27   return allocateSection(RWDataMem, Size, Alignment);
28 }
29
30 uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
31                                                    unsigned Alignment,
32                                                    unsigned SectionID) {
33   return allocateSection(CodeMem, Size, Alignment);
34 }
35
36 uint8_t *SectionMemoryManager::allocateSection(MemoryGroup &MemGroup,
37                                                uintptr_t Size,
38                                                unsigned Alignment) {
39   if (!Alignment)
40     Alignment = 16;
41
42   assert(!(Alignment & (Alignment - 1)) && "Alignment must be a power of two.");
43
44   uintptr_t RequiredSize = Alignment * ((Size + Alignment - 1)/Alignment + 1);
45   uintptr_t Addr = 0;
46
47   // Look in the list of free memory regions and use a block there if one
48   // is available.
49   for (int i = 0, e = MemGroup.FreeMem.size(); i != e; ++i) {
50     sys::MemoryBlock &MB = MemGroup.FreeMem[i];
51     if (MB.size() >= RequiredSize) {
52       Addr = (uintptr_t)MB.base();
53       uintptr_t EndOfBlock = Addr + MB.size();
54       // Align the address.
55       Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
56       // Store cutted free memory block.
57       MemGroup.FreeMem[i] = sys::MemoryBlock((void*)(Addr + Size),
58                                              EndOfBlock - Addr - Size);
59       return (uint8_t*)Addr;
60     }
61   }
62
63   // No pre-allocated free block was large enough. Allocate a new memory region.
64   // 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.
69   //
70   // FIXME: Initialize the Near member for each memory group to avoid
71   // interleaving.
72   error_code ec;
73   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) {
79     // FIXME: Add error propogation to the interface.
80     return NULL;
81   }
82
83   // Save this address as the basis for our next request
84   MemGroup.Near = MB;
85
86   MemGroup.AllocatedMem.push_back(MB);
87   Addr = (uintptr_t)MB.base();
88   uintptr_t EndOfBlock = Addr + MB.size();
89
90   // Align the address.
91   Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
92
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.
95   unsigned FreeSize = EndOfBlock-Addr-Size;
96   if (FreeSize > 16)
97     MemGroup.FreeMem.push_back(sys::MemoryBlock((void*)(Addr + Size), FreeSize));
98
99   // Return aligned address
100   return (uint8_t*)Addr;
101 }
102
103 bool SectionMemoryManager::finalizeMemory(std::string *ErrMsg)
104 {
105   // FIXME: Should in-progress permissions be reverted if an error occurs?
106   error_code ec;
107
108   // Don't allow free memory blocks to be used after setting protection flags.
109   CodeMem.FreeMem.clear();
110
111   // 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
121   // Don't allow free memory blocks to be used after setting protection flags.
122   RODataMem.FreeMem.clear();
123
124   // 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
136   // 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
141   return false;
142 }
143
144 error_code SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
145                                                              unsigned Permissions) {
146
147   for (int i = 0, e = MemGroup.AllocatedMem.size(); i != e; ++i) {
148       error_code ec;
149       ec = sys::Memory::protectMappedMemory(MemGroup.AllocatedMem[i],
150                                             Permissions);
151       if (ec) {
152         return ec;
153       }
154   }
155
156   return error_code::success();
157 }
158
159 void SectionMemoryManager::invalidateInstructionCache() {
160   for (int i = 0, e = CodeMem.AllocatedMem.size(); i != e; ++i)
161     sys::Memory::InvalidateInstructionCache(CodeMem.AllocatedMem[i].base(),
162                                             CodeMem.AllocatedMem[i].size());
163 }
164
165 SectionMemoryManager::~SectionMemoryManager() {
166   for (unsigned i = 0, e = CodeMem.AllocatedMem.size(); i != e; ++i)
167     sys::Memory::releaseMappedMemory(CodeMem.AllocatedMem[i]);
168   for (unsigned i = 0, e = RWDataMem.AllocatedMem.size(); i != e; ++i)
169     sys::Memory::releaseMappedMemory(RWDataMem.AllocatedMem[i]);
170   for (unsigned i = 0, e = RODataMem.AllocatedMem.size(); i != e; ++i)
171     sys::Memory::releaseMappedMemory(RODataMem.AllocatedMem[i]);
172 }
173
174 } // namespace llvm
175