blob: cf90e77e38951121ae9940d69c22d9f0ad8a3a91 [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
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000019namespace llvm {
20
21uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size,
Filip Pizlo6eb43d22013-10-02 00:59:25 +000022 unsigned Alignment,
23 unsigned SectionID,
24 StringRef SectionName,
25 bool IsReadOnly) {
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000026 if (IsReadOnly)
27 return allocateSection(RODataMem, Size, Alignment);
28 return allocateSection(RWDataMem, Size, Alignment);
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000029}
30
31uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000032 unsigned Alignment,
Filip Pizlo6eb43d22013-10-02 00:59:25 +000033 unsigned SectionID,
34 StringRef SectionName) {
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000035 return allocateSection(CodeMem, Size, Alignment);
36}
37
38uint8_t *SectionMemoryManager::allocateSection(MemoryGroup &MemGroup,
39 uintptr_t Size,
40 unsigned Alignment) {
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000041 if (!Alignment)
42 Alignment = 16;
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000043
44 assert(!(Alignment & (Alignment - 1)) && "Alignment must be a power of two.");
45
46 uintptr_t RequiredSize = Alignment * ((Size + Alignment - 1)/Alignment + 1);
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000047 uintptr_t Addr = 0;
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000048
49 // Look in the list of free memory regions and use a block there if one
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000050 // is available.
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000051 for (int i = 0, e = MemGroup.FreeMem.size(); i != e; ++i) {
52 sys::MemoryBlock &MB = MemGroup.FreeMem[i];
53 if (MB.size() >= RequiredSize) {
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000054 Addr = (uintptr_t)MB.base();
55 uintptr_t EndOfBlock = Addr + MB.size();
56 // Align the address.
57 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
58 // Store cutted free memory block.
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000059 MemGroup.FreeMem[i] = sys::MemoryBlock((void*)(Addr + Size),
60 EndOfBlock - Addr - Size);
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000061 return (uint8_t*)Addr;
62 }
63 }
64
65 // No pre-allocated free block was large enough. Allocate a new memory region.
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000066 // Note that all sections get allocated as read-write. The permissions will
67 // be updated later based on memory group.
68 //
69 // FIXME: It would be useful to define a default allocation size (or add
70 // it as a constructor parameter) to minimize the number of allocations.
Andrew Kaylor927ba6a2012-11-27 19:42:02 +000071 //
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000072 // FIXME: Initialize the Near member for each memory group to avoid
73 // interleaving.
74 error_code ec;
75 sys::MemoryBlock MB = sys::Memory::allocateMappedMemory(RequiredSize,
76 &MemGroup.Near,
77 sys::Memory::MF_READ |
78 sys::Memory::MF_WRITE,
79 ec);
80 if (ec) {
81 // FIXME: Add error propogation to the interface.
82 return NULL;
83 }
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000084
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000085 // Save this address as the basis for our next request
86 MemGroup.Near = MB;
87
88 MemGroup.AllocatedMem.push_back(MB);
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000089 Addr = (uintptr_t)MB.base();
90 uintptr_t EndOfBlock = Addr + MB.size();
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000091
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000092 // Align the address.
93 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000094
95 // The allocateMappedMemory may allocate much more memory than we need. In
96 // this case, we store the unused memory as a free memory block.
Andrew Kaylor2d6d5852012-10-04 20:29:44 +000097 unsigned FreeSize = EndOfBlock-Addr-Size;
98 if (FreeSize > 16)
Andrew Kaylorcc7773b2012-11-27 19:00:17 +000099 MemGroup.FreeMem.push_back(sys::MemoryBlock((void*)(Addr + Size), FreeSize));
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000100
101 // Return aligned address
102 return (uint8_t*)Addr;
103}
104
David Tweedabb38fe2013-05-17 10:01:46 +0000105bool SectionMemoryManager::finalizeMemory(std::string *ErrMsg)
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000106{
107 // FIXME: Should in-progress permissions be reverted if an error occurs?
108 error_code ec;
109
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000110 // Don't allow free memory blocks to be used after setting protection flags.
111 CodeMem.FreeMem.clear();
112
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000113 // Make code memory executable.
114 ec = applyMemoryGroupPermissions(CodeMem,
115 sys::Memory::MF_READ | sys::Memory::MF_EXEC);
116 if (ec) {
117 if (ErrMsg) {
118 *ErrMsg = ec.message();
119 }
120 return true;
121 }
122
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000123 // Don't allow free memory blocks to be used after setting protection flags.
124 RODataMem.FreeMem.clear();
125
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000126 // Make read-only data memory read-only.
127 ec = applyMemoryGroupPermissions(RODataMem,
128 sys::Memory::MF_READ | sys::Memory::MF_EXEC);
129 if (ec) {
130 if (ErrMsg) {
131 *ErrMsg = ec.message();
132 }
133 return true;
134 }
135
136 // Read-write data memory already has the correct permissions
137
Andrew Kaylor034f4be2013-04-24 22:39:12 +0000138 // Some platforms with separate data cache and instruction cache require
139 // explicit cache flush, otherwise JIT code manipulations (like resolved
140 // relocations) will get to the data cache but not to the instruction cache.
141 invalidateInstructionCache();
142
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000143 return false;
144}
145
146error_code SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
147 unsigned Permissions) {
148
149 for (int i = 0, e = MemGroup.AllocatedMem.size(); i != e; ++i) {
150 error_code ec;
151 ec = sys::Memory::protectMappedMemory(MemGroup.AllocatedMem[i],
152 Permissions);
153 if (ec) {
154 return ec;
155 }
156 }
157
158 return error_code::success();
159}
160
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000161void SectionMemoryManager::invalidateInstructionCache() {
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000162 for (int i = 0, e = CodeMem.AllocatedMem.size(); i != e; ++i)
163 sys::Memory::InvalidateInstructionCache(CodeMem.AllocatedMem[i].base(),
164 CodeMem.AllocatedMem[i].size());
165}
166
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000167SectionMemoryManager::~SectionMemoryManager() {
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000168 for (unsigned i = 0, e = CodeMem.AllocatedMem.size(); i != e; ++i)
169 sys::Memory::releaseMappedMemory(CodeMem.AllocatedMem[i]);
170 for (unsigned i = 0, e = RWDataMem.AllocatedMem.size(); i != e; ++i)
171 sys::Memory::releaseMappedMemory(RWDataMem.AllocatedMem[i]);
172 for (unsigned i = 0, e = RODataMem.AllocatedMem.size(); i != e; ++i)
173 sys::Memory::releaseMappedMemory(RODataMem.AllocatedMem[i]);
Andrew Kaylor2d6d5852012-10-04 20:29:44 +0000174}
175
176} // namespace llvm
Andrew Kaylorcc7773b2012-11-27 19:00:17 +0000177