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
19namespace llvm {
20
21uint8_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
30uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
31                                                   unsigned Alignment,
32                                                   unsigned SectionID) {
33  return allocateSection(CodeMem, Size, Alignment);
34}
35
36uint8_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
103bool SectionMemoryManager::finalizeMemory(std::string *ErrMsg)
104{
105  // FIXME: Should in-progress permissions be reverted if an error occurs?
106  error_code ec;
107
108  // Make code memory executable.
109  ec = applyMemoryGroupPermissions(CodeMem,
110                                   sys::Memory::MF_READ | sys::Memory::MF_EXEC);
111  if (ec) {
112    if (ErrMsg) {
113      *ErrMsg = ec.message();
114    }
115    return true;
116  }
117
118  // Make read-only data memory read-only.
119  ec = applyMemoryGroupPermissions(RODataMem,
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  // Read-write data memory already has the correct permissions
129
130  // Some platforms with separate data cache and instruction cache require
131  // explicit cache flush, otherwise JIT code manipulations (like resolved
132  // relocations) will get to the data cache but not to the instruction cache.
133  invalidateInstructionCache();
134
135  return false;
136}
137
138error_code SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
139                                                             unsigned Permissions) {
140
141  for (int i = 0, e = MemGroup.AllocatedMem.size(); i != e; ++i) {
142      error_code ec;
143      ec = sys::Memory::protectMappedMemory(MemGroup.AllocatedMem[i],
144                                            Permissions);
145      if (ec) {
146        return ec;
147      }
148  }
149
150  return error_code::success();
151}
152
153void SectionMemoryManager::invalidateInstructionCache() {
154  for (int i = 0, e = CodeMem.AllocatedMem.size(); i != e; ++i)
155    sys::Memory::InvalidateInstructionCache(CodeMem.AllocatedMem[i].base(),
156                                            CodeMem.AllocatedMem[i].size());
157}
158
159SectionMemoryManager::~SectionMemoryManager() {
160  for (unsigned i = 0, e = CodeMem.AllocatedMem.size(); i != e; ++i)
161    sys::Memory::releaseMappedMemory(CodeMem.AllocatedMem[i]);
162  for (unsigned i = 0, e = RWDataMem.AllocatedMem.size(); i != e; ++i)
163    sys::Memory::releaseMappedMemory(RWDataMem.AllocatedMem[i]);
164  for (unsigned i = 0, e = RODataMem.AllocatedMem.size(); i != e; ++i)
165    sys::Memory::releaseMappedMemory(RODataMem.AllocatedMem[i]);
166}
167
168} // namespace llvm
169
170