1 //===------- OrcTargetSupport.cpp - Target support utilities for Orc ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/Triple.h"
11 #include "llvm/ExecutionEngine/Orc/OrcTargetSupport.h"
12 #include "llvm/Support/Process.h"
15 using namespace llvm::orc;
19 uint64_t executeCompileCallback(JITCompileCallbackManagerBase *JCBM,
20 TargetAddress CallbackID) {
21 return JCBM->executeCompileCallback(CallbackID);
29 const char* OrcX86_64::ResolverBlockName = "orc_resolver_block";
31 void OrcX86_64::insertResolverBlock(
32 Module &M, JITCompileCallbackManagerBase &JCBM) {
34 // Trampoline code-sequence length, used to get trampoline address from return
36 const unsigned X86_64_TrampolineLength = 6;
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",
46 // Address of the executeCompileCallback function.
47 uint64_t CallbackAddr =
48 static_cast<uint64_t>(
49 reinterpret_cast<uintptr_t>(executeCompileCallback));
51 std::ostringstream AsmStream;
52 Triple TT(M.getTargetTriple());
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";
59 AsmStream << ".text\n"
60 << ".align 16, 0x90\n";
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";
67 // Start the resolver function.
68 AsmStream << ResolverBlockName << ":\n"
70 << " movq %rsp, %rbp\n";
73 for (const auto &GPR : GPRs)
74 AsmStream << " pushq %" << GPR << "\n";
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"
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"
93 // Replace the return to the trampoline with the return address of the
94 // compiled function body.
95 << " movq %rax, 0x8(%rbp)\n"
97 // Restore the floating point state.
98 << " fxrstor64 (%rsp)\n"
99 << " addq $" << FXSaveSize << ", %rsp\n";
101 for (const auto &GPR : make_range(GPRs.rbegin(), GPRs.rend()))
102 AsmStream << " popq %" << GPR << "\n";
104 // Restore original RBP and return to compiled function body.
105 AsmStream << " popq %rbp\n"
108 M.appendModuleInlineAsm(AsmStream.str());
111 OrcX86_64::LabelNameFtor
112 OrcX86_64::insertCompileCallbackTrampolines(Module &M,
113 TargetAddress ResolverBlockAddr,
115 unsigned StartIndex) {
116 const char *ResolverBlockPtrName = "Lorc_resolve_block_addr";
118 std::ostringstream AsmStream;
119 Triple TT(M.getTargetTriple());
121 if (TT.getOS() == Triple::Darwin)
122 AsmStream << ".section __TEXT,__text,regular,pure_instructions\n"
123 << ".align 4, 0x90\n";
125 AsmStream << ".text\n"
126 << ".align 16, 0x90\n";
128 AsmStream << ResolverBlockPtrName << ":\n"
129 << " .quad " << ResolverBlockAddr << "\n";
133 std::ostringstream LabelStream;
134 LabelStream << "orc_jcc_" << (StartIndex + I);
135 return LabelStream.str();
138 for (unsigned I = 0; I < NumCalls; ++I)
139 AsmStream << GetLabelName(I) << ":\n"
140 << " callq *" << ResolverBlockPtrName << "(%rip)\n";
142 M.appendModuleInlineAsm(AsmStream.str());
147 std::error_code OrcX86_64::emitIndirectStubsBlock(IndirectStubsInfo &StubsInfo,
149 void *InitialPtrVal) {
152 // .section __orc_stubs
155 // .byte 0xC4 ; <- Invalid opcode padding.
162 // .section __orc_ptrs
170 const unsigned StubSize = IndirectStubsInfo::StubSize;
172 // Emit at least MinStubs, rounded up to fill the pages allocated.
173 unsigned PageSize = sys::Process::getPageSize();
174 unsigned NumPages = ((MinStubs * StubSize) + (PageSize - 1)) / PageSize;
175 unsigned NumStubs = (NumPages * PageSize) / StubSize;
177 // Allocate memory for stubs and pointers in one call.
180 sys::OwningMemoryBlock(
181 sys::Memory::allocateMappedMemory(2 * NumPages * PageSize, nullptr,
182 sys::Memory::MF_READ |
183 sys::Memory::MF_WRITE,
189 // Create separate MemoryBlocks representing the stubs and pointers.
190 sys::MemoryBlock StubsBlock(StubsMem.base(), NumPages * PageSize);
191 sys::MemoryBlock PtrsBlock(static_cast<char*>(StubsMem.base()) +
193 NumPages * PageSize);
195 // Populate the stubs page stubs and mark it executable.
196 uint64_t *Stub = reinterpret_cast<uint64_t*>(StubsBlock.base());
197 uint64_t PtrOffsetField =
198 static_cast<uint64_t>(NumPages * PageSize - 6) << 16;
199 for (unsigned I = 0; I < NumStubs; ++I)
200 Stub[I] = 0xF1C40000000025ff | PtrOffsetField;
202 if (auto EC = sys::Memory::protectMappedMemory(StubsBlock,
203 sys::Memory::MF_READ |
204 sys::Memory::MF_EXEC))
207 // Initialize all pointers to point at FailureAddress.
208 void **Ptr = reinterpret_cast<void**>(PtrsBlock.base());
209 for (unsigned I = 0; I < NumStubs; ++I)
210 Ptr[I] = InitialPtrVal;
212 StubsInfo.NumStubs = NumStubs;
213 StubsInfo.StubsMem = std::move(StubsMem);
215 return std::error_code();
218 } // End namespace orc.
219 } // End namespace llvm.