a7d54a2734ad415bc3c16682a43a4a98b268038e
[oota-llvm.git] / lib / ExecutionEngine / Orc / OrcTargetSupport.cpp
1 //===------- OrcTargetSupport.cpp - Target support utilities for Orc ------===//
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 #include "llvm/ADT/Triple.h"
11 #include "llvm/ExecutionEngine/Orc/OrcTargetSupport.h"
12 #include "llvm/Support/Process.h"
13 #include <array>
14
15 using namespace llvm::orc;
16
17 namespace {
18
19 uint64_t executeCompileCallback(JITCompileCallbackManagerBase *JCBM,
20                                 TargetAddress CallbackID) {
21   return JCBM->executeCompileCallback(CallbackID);
22 }
23
24 }
25
26 namespace llvm {
27 namespace orc {
28
29 const char* OrcX86_64::ResolverBlockName = "orc_resolver_block";
30
31 void OrcX86_64::insertResolverBlock(
32     Module &M, JITCompileCallbackManagerBase &JCBM) {
33
34   // Trampoline code-sequence length, used to get trampoline address from return
35   // address.
36   const unsigned X86_64_TrampolineLength = 6;
37
38   // List of x86-64 GPRs to save. Note - RBP saved separately below.
39   std::array<const char *, 14> GPRs = {{
40       "rax", "rbx", "rcx", "rdx",
41       "rsi", "rdi", "r8", "r9",
42       "r10", "r11", "r12", "r13",
43       "r14", "r15"
44     }};
45
46   // Address of the executeCompileCallback function.
47   uint64_t CallbackAddr =
48       static_cast<uint64_t>(
49         reinterpret_cast<uintptr_t>(executeCompileCallback));
50
51   std::ostringstream AsmStream;
52   Triple TT(M.getTargetTriple());
53
54   // Switch to text section.
55   if (TT.getOS() == Triple::Darwin)
56     AsmStream << ".section __TEXT,__text,regular,pure_instructions\n"
57               << ".align 4, 0x90\n";
58   else
59     AsmStream << ".text\n"
60               << ".align 16, 0x90\n";
61
62   // Bake in a pointer to the callback manager immediately before the
63   // start of the resolver function.
64   AsmStream << "jit_callback_manager_addr:\n"
65             << "  .quad " << &JCBM << "\n";
66
67   // Start the resolver function.
68   AsmStream << ResolverBlockName << ":\n"
69             << "  pushq     %rbp\n"
70             << "  movq      %rsp, %rbp\n";
71
72   // Store the GPRs.
73   for (const auto &GPR : GPRs)
74     AsmStream << "  pushq     %" << GPR << "\n";
75
76   // Store floating-point state with FXSAVE.
77   // Note: We need to keep the stack 16-byte aligned, so if we've emitted an odd
78   //       number of 64-bit pushes so far (GPRs.size() plus 1 for RBP) then add
79   //       an extra 64 bits of padding to the FXSave area.
80   unsigned Padding = (GPRs.size() + 1) % 2 ? 8 : 0;
81   unsigned FXSaveSize = 512 + Padding;
82   AsmStream << "  subq      $" << FXSaveSize << ", %rsp\n"
83             << "  fxsave64  (%rsp)\n"
84
85   // Load callback manager address, compute trampoline address, call JIT.
86             << "  lea       jit_callback_manager_addr(%rip), %rdi\n"
87             << "  movq      (%rdi), %rdi\n"
88             << "  movq      0x8(%rbp), %rsi\n"
89             << "  subq      $" << X86_64_TrampolineLength << ", %rsi\n"
90             << "  movabsq   $" << CallbackAddr << ", %rax\n"
91             << "  callq     *%rax\n"
92
93   // Replace the return to the trampoline with the return address of the
94   // compiled function body.
95             << "  movq      %rax, 0x8(%rbp)\n"
96
97   // Restore the floating point state.
98             << "  fxrstor64 (%rsp)\n"
99             << "  addq      $" << FXSaveSize << ", %rsp\n";
100
101   for (const auto &GPR : make_range(GPRs.rbegin(), GPRs.rend()))
102     AsmStream << "  popq      %" << GPR << "\n";
103
104   // Restore original RBP and return to compiled function body.
105   AsmStream << "  popq      %rbp\n"
106             << "  retq\n";
107
108   M.appendModuleInlineAsm(AsmStream.str());
109 }
110
111 OrcX86_64::LabelNameFtor
112 OrcX86_64::insertCompileCallbackTrampolines(Module &M,
113                                             TargetAddress ResolverBlockAddr,
114                                             unsigned NumCalls,
115                                             unsigned StartIndex) {
116   const char *ResolverBlockPtrName = "Lorc_resolve_block_addr";
117
118   std::ostringstream AsmStream;
119   Triple TT(M.getTargetTriple());
120
121   if (TT.getOS() == Triple::Darwin)
122     AsmStream << ".section __TEXT,__text,regular,pure_instructions\n"
123               << ".align 4, 0x90\n";
124   else
125     AsmStream << ".text\n"
126               << ".align 16, 0x90\n";
127
128   AsmStream << ResolverBlockPtrName << ":\n"
129             << "  .quad " << ResolverBlockAddr << "\n";
130
131   auto GetLabelName =
132     [=](unsigned I) {
133       std::ostringstream LabelStream;
134       LabelStream << "orc_jcc_" << (StartIndex + I);
135       return LabelStream.str();
136   };
137
138   for (unsigned I = 0; I < NumCalls; ++I)
139     AsmStream << GetLabelName(I) << ":\n"
140               << "  callq *" << ResolverBlockPtrName << "(%rip)\n";
141
142   M.appendModuleInlineAsm(AsmStream.str());
143
144   return GetLabelName;
145 }
146
147 OrcX86_64::IndirectStubsInfo::~IndirectStubsInfo() {
148   sys::Memory::releaseMappedMemory(StubsBlock);
149   sys::Memory::releaseMappedMemory(PtrsBlock);
150 }
151
152 std::error_code OrcX86_64::emitIndirectStubsBlock(IndirectStubsInfo &StubsInfo,
153                                                   unsigned MinStubs,
154                                                   void *InitialPtrVal) {
155   // Stub format is:
156   //
157   // .section __orc_stubs
158   // stub1:
159   //                 jmpq    *ptr1(%rip)
160   //                 .byte   0xC4         ; <- Invalid opcode padding.
161   //                 .byte   0xF1
162   // stub2:
163   //                 jmpq    *ptr2(%rip)
164   //
165   // ...
166   //
167   // .section __orc_ptrs
168   // ptr1:
169   //                 .quad 0x0
170   // ptr2:
171   //                 .quad 0x0
172   //
173   // ...
174
175   const unsigned StubSize = IndirectStubsInfo::StubSize;
176
177   // Emit at least MinStubs, rounded up to fill the pages allocated.
178   unsigned PageSize = sys::Process::getPageSize();
179   unsigned NumPages = ((MinStubs * StubSize) + (PageSize - 1)) / PageSize;
180   unsigned NumStubs = (NumPages * PageSize) / StubSize;
181
182   // Allocate memory for stubs and pointers in one call.
183   std::error_code EC;
184   auto InitialBlock = sys::Memory::allocateMappedMemory(2 * NumPages * PageSize,
185                                                         nullptr,
186                                                         sys::Memory::MF_READ |
187                                                         sys::Memory::MF_WRITE,
188                                                         EC);
189
190   if (EC)
191     return EC;
192
193   // Create separate MemoryBlocks representing the stubs and pointers.
194   sys::MemoryBlock StubsBlock(InitialBlock.base(), NumPages * PageSize);
195   sys::MemoryBlock PtrsBlock(static_cast<char*>(InitialBlock.base()) +
196                              NumPages * PageSize,
197                              NumPages * PageSize);
198
199   // Populate the stubs page stubs and mark it executable.
200   uint64_t *Stub = reinterpret_cast<uint64_t*>(StubsBlock.base());
201   uint64_t PtrOffsetField =
202     static_cast<uint64_t>(NumPages * PageSize - 6) << 16;
203   for (unsigned I = 0; I < NumStubs; ++I)
204     Stub[I] = 0xF1C40000000025ff | PtrOffsetField;
205
206   if (auto EC = sys::Memory::protectMappedMemory(StubsBlock,
207                                                  sys::Memory::MF_READ |
208                                                  sys::Memory::MF_EXEC))
209     return EC;
210
211   // Initialize all pointers to point at FailureAddress.
212   void **Ptr = reinterpret_cast<void**>(PtrsBlock.base());
213   for (unsigned I = 0; I < NumStubs; ++I)
214     Ptr[I] = InitialPtrVal;
215
216   StubsInfo.NumStubs = NumStubs;
217   StubsInfo.StubsBlock = std::move(StubsBlock);
218   StubsInfo.PtrsBlock = std::move(PtrsBlock);
219
220   return std::error_code();
221 }
222
223 } // End namespace orc.
224 } // End namespace llvm.