blob: 397426afd48555b51da9367f61ae51ee0de270c1 [file] [log] [blame]
Andrew Kaylorcc7773b2012-11-27 19:00:17 +00001//===- SectionMemoryManager.cpp - Memory manager for MCJIT/RtDyld *- C++ -*-==//
Andrew Kaylor2d6d5852012-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 Kaylorcc7773b2012-11-27 19:00:17 +000010// This file implements the section-based memory manager used by the MCJIT
11// execution engine and RuntimeDyld
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Config/config.h"
Andrew Kaylor927ba6a2012-11-27 19:42:02 +000016#include "llvm/ExecutionEngine/SectionMemoryManager.h"
Amara Emersona25ad192012-10-31 17:41:51 +000017#include "llvm/Support/MathExtras.h"
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000018
19#ifdef __linux__
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000020 // 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>
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000027#endif
28
29namespace llvm {
30
31uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size,
32 unsigned Alignment,
Andrew Kaylor53608a32012-11-15 23:50:01 +000033 unsigned SectionID,
34 bool IsReadOnly) {
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000035 if (IsReadOnly)
36 return allocateSection(RODataMem, Size, Alignment);
37 return allocateSection(RWDataMem, Size, Alignment);
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000038}
39
40uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000041 unsigned Alignment,
42 unsigned SectionID) {
43 return allocateSection(CodeMem, Size, Alignment);
44}
45
46uint8_t *SectionMemoryManager::allocateSection(MemoryGroup &MemGroup,
47 uintptr_t Size,
48 unsigned Alignment) {
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000049 if (!Alignment)
50 Alignment = 16;
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000051
52 assert(!(Alignment & (Alignment - 1)) && "Alignment must be a power of two.");
53
54 uintptr_t RequiredSize = Alignment * ((Size + Alignment - 1)/Alignment + 1);
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000055 uintptr_t Addr = 0;
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000056
57 // Look in the list of free memory regions and use a block there if one
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000058 // is available.
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000059 for (int i = 0, e = MemGroup.FreeMem.size(); i != e; ++i) {
60 sys::MemoryBlock &MB = MemGroup.FreeMem[i];
61 if (MB.size() >= RequiredSize) {
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000062 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.
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000067 MemGroup.FreeMem[i] = sys::MemoryBlock((void*)(Addr + Size),
68 EndOfBlock - Addr - Size);
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000069 return (uint8_t*)Addr;
70 }
71 }
72
73 // No pre-allocated free block was large enough. Allocate a new memory region.
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000074 // 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.
Andrew Kaylor927ba6a2012-11-27 19:42:02 +000079 //
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000080 // 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 }
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000092
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000093 // Save this address as the basis for our next request
94 MemGroup.Near = MB;
95
96 MemGroup.AllocatedMem.push_back(MB);
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000097 Addr = (uintptr_t)MB.base();
98 uintptr_t EndOfBlock = Addr + MB.size();
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000099
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000100 // Align the address.
101 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000102
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.
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000105 unsigned FreeSize = EndOfBlock-Addr-Size;
106 if (FreeSize > 16)
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000107 MemGroup.FreeMem.push_back(sys::MemoryBlock((void*)(Addr + Size), FreeSize));
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000108
109 // Return aligned address
110 return (uint8_t*)Addr;
111}
112
David Tweedabb38fe2013-05-17 10:01:46 +0000113bool SectionMemoryManager::finalizeMemory(std::string *ErrMsg)
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000114{
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
Andrew Kaylor034f4be2013-04-24 22:39:12 +0000140 // 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
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000145 return false;
146}
147
148error_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
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000163void SectionMemoryManager::invalidateInstructionCache() {
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000164 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
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000169SectionMemoryManager::~SectionMemoryManager() {
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000170 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]);
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000176}
177
178} // namespace llvm
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000179