[Orc] Teach IndirectStubsManager to manage an expandable pool of stubs, rather
[oota-llvm.git] / lib / ExecutionEngine / Orc / OrcTargetSupport.cpp
index 258868aa64f63e9f6138222c50572e1c6e97f46c..1e2d58cd5622459540af2db55797e8c07812ea43 100644 (file)
@@ -1,5 +1,15 @@
+//===------- OrcTargetSupport.cpp - Target support utilities for Orc ------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
 #include "llvm/ADT/Triple.h"
 #include "llvm/ExecutionEngine/Orc/OrcTargetSupport.h"
+#include "llvm/Support/Process.h"
 #include <array>
 
 using namespace llvm::orc;
@@ -134,5 +144,97 @@ OrcX86_64::insertCompileCallbackTrampolines(Module &M,
   return GetLabelName;
 }
 
+OrcX86_64::IndirectStubsInfo::IndirectStubsInfo(IndirectStubsInfo &&Other) {
+  StubsBlock = std::move(Other.StubsBlock);
+  PtrsBlock = std::move(Other.PtrsBlock);
+  Other.StubsBlock = sys::MemoryBlock();
+  Other.PtrsBlock = sys::MemoryBlock();
+}
+
+OrcX86_64::IndirectStubsInfo&
+OrcX86_64::IndirectStubsInfo::operator=(IndirectStubsInfo &&Other) {
+  StubsBlock = std::move(Other.StubsBlock);
+  PtrsBlock = std::move(Other.PtrsBlock);
+  Other.StubsBlock = sys::MemoryBlock();
+  Other.PtrsBlock = sys::MemoryBlock();
+  return *this;
+}
+
+OrcX86_64::IndirectStubsInfo::~IndirectStubsInfo() {
+  sys::Memory::releaseMappedMemory(StubsBlock);
+  sys::Memory::releaseMappedMemory(PtrsBlock);
+}
+
+std::error_code OrcX86_64::emitIndirectStubsBlock(IndirectStubsInfo &StubsInfo,
+                                                  unsigned MinStubs,
+                                                  void *InitialPtrVal) {
+  // Stub format is:
+  //
+  // .section __orc_stubs
+  // stub1:
+  //                 jmpq    *ptr1(%rip)
+  //                 .byte   0xC4         ; <- Invalid opcode padding.
+  //                 .byte   0xF1
+  // stub2:
+  //                 jmpq    *ptr2(%rip)
+  //
+  // ...
+  //
+  // .section __orc_ptrs
+  // ptr1:
+  //                 .quad 0x0
+  // ptr2:
+  //                 .quad 0x0
+  //
+  // ...
+
+  const unsigned StubSize = IndirectStubsInfo::StubSize;
+
+  // Emit at least MinStubs, rounded up to fill the pages allocated.
+  unsigned PageSize = sys::Process::getPageSize();
+  unsigned NumPages = ((MinStubs * StubSize) + (PageSize - 1)) / PageSize;
+  unsigned NumStubs = (NumPages * PageSize) / StubSize;
+
+  // Allocate memory for stubs and pointers in one call.
+  std::error_code EC;
+  auto InitialBlock = sys::Memory::allocateMappedMemory(2 * NumPages * PageSize,
+                                                        nullptr,
+                                                        sys::Memory::MF_READ |
+                                                        sys::Memory::MF_WRITE,
+                                                        EC);
+
+  if (EC)
+    return EC;
+
+  // Create separate MemoryBlocks representing the stubs and pointers.
+  sys::MemoryBlock StubsBlock(InitialBlock.base(), NumPages * PageSize);
+  sys::MemoryBlock PtrsBlock(static_cast<char*>(InitialBlock.base()) +
+                             NumPages * PageSize,
+                             NumPages * PageSize);
+
+  // Populate the stubs page stubs and mark it executable.
+  uint64_t *Stub = reinterpret_cast<uint64_t*>(StubsBlock.base());
+  uint64_t PtrOffsetField =
+    static_cast<uint64_t>(NumPages * PageSize - 6) << 16;
+  for (unsigned I = 0; I < NumStubs; ++I)
+    Stub[I] = 0xF1C40000000025ff | PtrOffsetField;
+
+  if (auto EC = sys::Memory::protectMappedMemory(StubsBlock,
+                                                 sys::Memory::MF_READ |
+                                                 sys::Memory::MF_EXEC))
+    return EC;
+
+  // Initialize all pointers to point at FailureAddress.
+  void **Ptr = reinterpret_cast<void**>(PtrsBlock.base());
+  for (unsigned I = 0; I < NumStubs; ++I)
+    Ptr[I] = InitialPtrVal;
+
+  StubsInfo.NumStubs = NumStubs;
+  StubsInfo.StubsBlock = std::move(StubsBlock);
+  StubsInfo.PtrsBlock = std::move(PtrsBlock);
+
+  return std::error_code();
+}
+
 } // End namespace orc.
 } // End namespace llvm.