1293838Sdim//===------ OrcArchSupport.cpp - Architecture specific support code -------===// 2293838Sdim// 3293838Sdim// The LLVM Compiler Infrastructure 4293838Sdim// 5293838Sdim// This file is distributed under the University of Illinois Open Source 6293838Sdim// License. See LICENSE.TXT for details. 7293838Sdim// 8293838Sdim//===----------------------------------------------------------------------===// 9293838Sdim 10293838Sdim#include "llvm/ADT/Triple.h" 11293838Sdim#include "llvm/ExecutionEngine/Orc/OrcArchitectureSupport.h" 12293838Sdim#include "llvm/Support/Process.h" 13293838Sdim#include <array> 14293838Sdim 15293838Sdimnamespace llvm { 16293838Sdimnamespace orc { 17293838Sdim 18293838Sdimvoid OrcX86_64::writeResolverCode(uint8_t *ResolverMem, JITReentryFn ReentryFn, 19293838Sdim void *CallbackMgr) { 20293838Sdim 21293838Sdim const uint8_t ResolverCode[] = { 22293838Sdim // resolver_entry: 23293838Sdim 0x55, // 0x00: pushq %rbp 24293838Sdim 0x48, 0x89, 0xe5, // 0x01: movq %rsp, %rbp 25293838Sdim 0x50, // 0x04: pushq %rax 26293838Sdim 0x53, // 0x05: pushq %rbx 27293838Sdim 0x51, // 0x06: pushq %rcx 28293838Sdim 0x52, // 0x07: pushq %rdx 29293838Sdim 0x56, // 0x08: pushq %rsi 30293838Sdim 0x57, // 0x09: pushq %rdi 31293838Sdim 0x41, 0x50, // 0x0a: pushq %r8 32293838Sdim 0x41, 0x51, // 0x0c: pushq %r9 33293838Sdim 0x41, 0x52, // 0x0e: pushq %r10 34293838Sdim 0x41, 0x53, // 0x10: pushq %r11 35293838Sdim 0x41, 0x54, // 0x12: pushq %r12 36293838Sdim 0x41, 0x55, // 0x14: pushq %r13 37293838Sdim 0x41, 0x56, // 0x16: pushq %r14 38293838Sdim 0x41, 0x57, // 0x18: pushq %r15 39293838Sdim 0x48, 0x81, 0xec, 0x08, 0x02, 0x00, 0x00, // 0x1a: subq 20, %rsp 40293838Sdim 0x48, 0x0f, 0xae, 0x04, 0x24, // 0x21: fxsave64 (%rsp) 41293838Sdim 0x48, 0x8d, 0x3d, 0x43, 0x00, 0x00, 0x00, // 0x26: leaq 67(%rip), %rdi 42293838Sdim 0x48, 0x8b, 0x3f, // 0x2d: movq (%rdi), %rdi 43293838Sdim 0x48, 0x8b, 0x75, 0x08, // 0x30: movq 8(%rbp), %rsi 44293838Sdim 0x48, 0x83, 0xee, 0x06, // 0x34: subq $6, %rsi 45293838Sdim 0x48, 0xb8, // 0x38: movabsq $0, %rax 46293838Sdim 47293838Sdim // 0x3a: JIT re-entry fn addr: 48293838Sdim 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 49293838Sdim 50293838Sdim 0xff, 0xd0, // 0x42: callq *%rax 51293838Sdim 0x48, 0x89, 0x45, 0x08, // 0x44: movq %rax, 8(%rbp) 52293838Sdim 0x48, 0x0f, 0xae, 0x0c, 0x24, // 0x48: fxrstor64 (%rsp) 53293838Sdim 0x48, 0x81, 0xc4, 0x08, 0x02, 0x00, 0x00, // 0x4d: addq 20, %rsp 54293838Sdim 0x41, 0x5f, // 0x54: popq %r15 55293838Sdim 0x41, 0x5e, // 0x56: popq %r14 56293838Sdim 0x41, 0x5d, // 0x58: popq %r13 57293838Sdim 0x41, 0x5c, // 0x5a: popq %r12 58293838Sdim 0x41, 0x5b, // 0x5c: popq %r11 59293838Sdim 0x41, 0x5a, // 0x5e: popq %r10 60293838Sdim 0x41, 0x59, // 0x60: popq %r9 61293838Sdim 0x41, 0x58, // 0x62: popq %r8 62293838Sdim 0x5f, // 0x64: popq %rdi 63293838Sdim 0x5e, // 0x65: popq %rsi 64293838Sdim 0x5a, // 0x66: popq %rdx 65293838Sdim 0x59, // 0x67: popq %rcx 66293838Sdim 0x5b, // 0x68: popq %rbx 67293838Sdim 0x58, // 0x69: popq %rax 68293838Sdim 0x5d, // 0x6a: popq %rbp 69293838Sdim 0xc3, // 0x6b: retq 70293838Sdim 0x00, 0x00, 0x00, 0x00, // 0x6c: <padding> 71293838Sdim 72293838Sdim // 0x70: Callback mgr address. 73293838Sdim 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 74293838Sdim }; 75293838Sdim 76293838Sdim const unsigned ReentryFnAddrOffset = 0x3a; 77293838Sdim const unsigned CallbackMgrAddrOffset = 0x70; 78293838Sdim 79293838Sdim memcpy(ResolverMem, ResolverCode, sizeof(ResolverCode)); 80293838Sdim memcpy(ResolverMem + ReentryFnAddrOffset, &ReentryFn, sizeof(ReentryFn)); 81293838Sdim memcpy(ResolverMem + CallbackMgrAddrOffset, &CallbackMgr, 82293838Sdim sizeof(CallbackMgr)); 83293838Sdim} 84293838Sdim 85293838Sdimvoid OrcX86_64::writeTrampolines(uint8_t *TrampolineMem, void *ResolverAddr, 86293838Sdim unsigned NumTrampolines) { 87293838Sdim 88293838Sdim unsigned OffsetToPtr = NumTrampolines * TrampolineSize; 89293838Sdim 90293838Sdim memcpy(TrampolineMem + OffsetToPtr, &ResolverAddr, sizeof(void*)); 91293838Sdim 92293838Sdim uint64_t *Trampolines = reinterpret_cast<uint64_t*>(TrampolineMem); 93293838Sdim uint64_t CallIndirPCRel = 0xf1c40000000015ff; 94293838Sdim 95293838Sdim for (unsigned I = 0; I < NumTrampolines; ++I, OffsetToPtr -= TrampolineSize) 96293838Sdim Trampolines[I] = CallIndirPCRel | ((OffsetToPtr - 6) << 16); 97293838Sdim} 98293838Sdim 99293838Sdimstd::error_code OrcX86_64::emitIndirectStubsBlock(IndirectStubsInfo &StubsInfo, 100293838Sdim unsigned MinStubs, 101293838Sdim void *InitialPtrVal) { 102293838Sdim // Stub format is: 103293838Sdim // 104293838Sdim // .section __orc_stubs 105293838Sdim // stub1: 106293838Sdim // jmpq *ptr1(%rip) 107293838Sdim // .byte 0xC4 ; <- Invalid opcode padding. 108293838Sdim // .byte 0xF1 109293838Sdim // stub2: 110293838Sdim // jmpq *ptr2(%rip) 111293838Sdim // 112293838Sdim // ... 113293838Sdim // 114293838Sdim // .section __orc_ptrs 115293838Sdim // ptr1: 116293838Sdim // .quad 0x0 117293838Sdim // ptr2: 118293838Sdim // .quad 0x0 119293838Sdim // 120293838Sdim // ... 121293838Sdim 122293838Sdim const unsigned StubSize = IndirectStubsInfo::StubSize; 123293838Sdim 124293838Sdim // Emit at least MinStubs, rounded up to fill the pages allocated. 125293838Sdim unsigned PageSize = sys::Process::getPageSize(); 126293838Sdim unsigned NumPages = ((MinStubs * StubSize) + (PageSize - 1)) / PageSize; 127293838Sdim unsigned NumStubs = (NumPages * PageSize) / StubSize; 128293838Sdim 129293838Sdim // Allocate memory for stubs and pointers in one call. 130293838Sdim std::error_code EC; 131293838Sdim auto StubsMem = 132293838Sdim sys::OwningMemoryBlock( 133293838Sdim sys::Memory::allocateMappedMemory(2 * NumPages * PageSize, nullptr, 134293838Sdim sys::Memory::MF_READ | 135293838Sdim sys::Memory::MF_WRITE, 136293838Sdim EC)); 137293838Sdim 138293838Sdim if (EC) 139293838Sdim return EC; 140293838Sdim 141293838Sdim // Create separate MemoryBlocks representing the stubs and pointers. 142293838Sdim sys::MemoryBlock StubsBlock(StubsMem.base(), NumPages * PageSize); 143293838Sdim sys::MemoryBlock PtrsBlock(static_cast<char*>(StubsMem.base()) + 144293838Sdim NumPages * PageSize, 145293838Sdim NumPages * PageSize); 146293838Sdim 147293838Sdim // Populate the stubs page stubs and mark it executable. 148293838Sdim uint64_t *Stub = reinterpret_cast<uint64_t*>(StubsBlock.base()); 149293838Sdim uint64_t PtrOffsetField = 150293838Sdim static_cast<uint64_t>(NumPages * PageSize - 6) << 16; 151293838Sdim for (unsigned I = 0; I < NumStubs; ++I) 152293838Sdim Stub[I] = 0xF1C40000000025ff | PtrOffsetField; 153293838Sdim 154293838Sdim if (auto EC = sys::Memory::protectMappedMemory(StubsBlock, 155293838Sdim sys::Memory::MF_READ | 156293838Sdim sys::Memory::MF_EXEC)) 157293838Sdim return EC; 158293838Sdim 159293838Sdim // Initialize all pointers to point at FailureAddress. 160293838Sdim void **Ptr = reinterpret_cast<void**>(PtrsBlock.base()); 161293838Sdim for (unsigned I = 0; I < NumStubs; ++I) 162293838Sdim Ptr[I] = InitialPtrVal; 163293838Sdim 164293838Sdim StubsInfo.NumStubs = NumStubs; 165293838Sdim StubsInfo.StubsMem = std::move(StubsMem); 166293838Sdim 167293838Sdim return std::error_code(); 168293838Sdim} 169293838Sdim 170293838Sdim} // End namespace orc. 171293838Sdim} // End namespace llvm. 172