Lang Hames | 70b2406 | 2016-01-11 00:56:15 +0000 | [diff] [blame] | 1 | //===------ OrcArchSupport.cpp - Architecture specific support code -------===// |
Lang Hames | 4df7ba7 | 2015-10-26 06:40:28 +0000 | [diff] [blame] | 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 | |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/Triple.h" |
Lang Hames | 70b2406 | 2016-01-11 00:56:15 +0000 | [diff] [blame] | 11 | #include "llvm/ExecutionEngine/Orc/OrcArchitectureSupport.h" |
Lang Hames | 98c2ac1 | 2015-10-19 17:43:51 +0000 | [diff] [blame] | 12 | #include "llvm/Support/Process.h" |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 13 | #include <array> |
| 14 | |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 15 | namespace llvm { |
Lang Hames | e738061 | 2015-02-21 20:44:36 +0000 | [diff] [blame] | 16 | namespace orc { |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 17 | |
Rafael Espindola | e63e018 | 2015-11-03 16:40:37 +0000 | [diff] [blame] | 18 | void OrcX86_64::writeResolverCode(uint8_t *ResolverMem, JITReentryFn ReentryFn, |
| 19 | void *CallbackMgr) { |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 20 | |
Rafael Espindola | e63e018 | 2015-11-03 16:40:37 +0000 | [diff] [blame] | 21 | const uint8_t ResolverCode[] = { |
| 22 | // resolver_entry: |
| 23 | 0x55, // 0x00: pushq %rbp |
| 24 | 0x48, 0x89, 0xe5, // 0x01: movq %rsp, %rbp |
| 25 | 0x50, // 0x04: pushq %rax |
| 26 | 0x53, // 0x05: pushq %rbx |
| 27 | 0x51, // 0x06: pushq %rcx |
| 28 | 0x52, // 0x07: pushq %rdx |
| 29 | 0x56, // 0x08: pushq %rsi |
| 30 | 0x57, // 0x09: pushq %rdi |
| 31 | 0x41, 0x50, // 0x0a: pushq %r8 |
| 32 | 0x41, 0x51, // 0x0c: pushq %r9 |
| 33 | 0x41, 0x52, // 0x0e: pushq %r10 |
| 34 | 0x41, 0x53, // 0x10: pushq %r11 |
| 35 | 0x41, 0x54, // 0x12: pushq %r12 |
| 36 | 0x41, 0x55, // 0x14: pushq %r13 |
| 37 | 0x41, 0x56, // 0x16: pushq %r14 |
| 38 | 0x41, 0x57, // 0x18: pushq %r15 |
Lang Hames | d677fa8 | 2016-02-05 23:27:48 +0000 | [diff] [blame] | 39 | 0x48, 0x81, 0xec, 0x08, 0x02, 0x00, 0x00, // 0x1a: subq 0x208, %rsp |
Rafael Espindola | e63e018 | 2015-11-03 16:40:37 +0000 | [diff] [blame] | 40 | 0x48, 0x0f, 0xae, 0x04, 0x24, // 0x21: fxsave64 (%rsp) |
Lang Hames | 120a9b4 | 2016-02-06 00:55:08 +0000 | [diff] [blame^] | 41 | 0x48, 0xbf, // 0x26: movabsq <CBMgr>, %rdi |
| 42 | |
| 43 | // 0x28: Callback manager addr. |
| 44 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 45 | |
Rafael Espindola | e63e018 | 2015-11-03 16:40:37 +0000 | [diff] [blame] | 46 | 0x48, 0x8b, 0x75, 0x08, // 0x30: movq 8(%rbp), %rsi |
| 47 | 0x48, 0x83, 0xee, 0x06, // 0x34: subq $6, %rsi |
Lang Hames | 120a9b4 | 2016-02-06 00:55:08 +0000 | [diff] [blame^] | 48 | 0x48, 0xb8, // 0x38: movabsq <REntry>, %rax |
Lang Hames | e51ab6e | 2015-04-06 03:01:29 +0000 | [diff] [blame] | 49 | |
Rafael Espindola | e63e018 | 2015-11-03 16:40:37 +0000 | [diff] [blame] | 50 | // 0x3a: JIT re-entry fn addr: |
| 51 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Lang Hames | e51ab6e | 2015-04-06 03:01:29 +0000 | [diff] [blame] | 52 | |
Rafael Espindola | e63e018 | 2015-11-03 16:40:37 +0000 | [diff] [blame] | 53 | 0xff, 0xd0, // 0x42: callq *%rax |
| 54 | 0x48, 0x89, 0x45, 0x08, // 0x44: movq %rax, 8(%rbp) |
| 55 | 0x48, 0x0f, 0xae, 0x0c, 0x24, // 0x48: fxrstor64 (%rsp) |
Lang Hames | d677fa8 | 2016-02-05 23:27:48 +0000 | [diff] [blame] | 56 | 0x48, 0x81, 0xc4, 0x08, 0x02, 0x00, 0x00, // 0x4d: addq 0x208, %rsp |
Rafael Espindola | e63e018 | 2015-11-03 16:40:37 +0000 | [diff] [blame] | 57 | 0x41, 0x5f, // 0x54: popq %r15 |
| 58 | 0x41, 0x5e, // 0x56: popq %r14 |
| 59 | 0x41, 0x5d, // 0x58: popq %r13 |
| 60 | 0x41, 0x5c, // 0x5a: popq %r12 |
| 61 | 0x41, 0x5b, // 0x5c: popq %r11 |
| 62 | 0x41, 0x5a, // 0x5e: popq %r10 |
| 63 | 0x41, 0x59, // 0x60: popq %r9 |
| 64 | 0x41, 0x58, // 0x62: popq %r8 |
| 65 | 0x5f, // 0x64: popq %rdi |
| 66 | 0x5e, // 0x65: popq %rsi |
| 67 | 0x5a, // 0x66: popq %rdx |
| 68 | 0x59, // 0x67: popq %rcx |
| 69 | 0x5b, // 0x68: popq %rbx |
| 70 | 0x58, // 0x69: popq %rax |
| 71 | 0x5d, // 0x6a: popq %rbp |
| 72 | 0xc3, // 0x6b: retq |
Rafael Espindola | 2f34463 | 2015-11-03 16:25:20 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
Rafael Espindola | e63e018 | 2015-11-03 16:40:37 +0000 | [diff] [blame] | 75 | const unsigned ReentryFnAddrOffset = 0x3a; |
Lang Hames | 120a9b4 | 2016-02-06 00:55:08 +0000 | [diff] [blame^] | 76 | const unsigned CallbackMgrAddrOffset = 0x28; |
Rafael Espindola | e63e018 | 2015-11-03 16:40:37 +0000 | [diff] [blame] | 77 | |
| 78 | memcpy(ResolverMem, ResolverCode, sizeof(ResolverCode)); |
| 79 | memcpy(ResolverMem + ReentryFnAddrOffset, &ReentryFn, sizeof(ReentryFn)); |
| 80 | memcpy(ResolverMem + CallbackMgrAddrOffset, &CallbackMgr, |
| 81 | sizeof(CallbackMgr)); |
| 82 | } |
Rafael Espindola | 2f34463 | 2015-11-03 16:25:20 +0000 | [diff] [blame] | 83 | |
Rafael Espindola | e63e018 | 2015-11-03 16:40:37 +0000 | [diff] [blame] | 84 | void OrcX86_64::writeTrampolines(uint8_t *TrampolineMem, void *ResolverAddr, |
| 85 | unsigned NumTrampolines) { |
Rafael Espindola | 2f34463 | 2015-11-03 16:25:20 +0000 | [diff] [blame] | 86 | |
Rafael Espindola | e63e018 | 2015-11-03 16:40:37 +0000 | [diff] [blame] | 87 | unsigned OffsetToPtr = NumTrampolines * TrampolineSize; |
| 88 | |
| 89 | memcpy(TrampolineMem + OffsetToPtr, &ResolverAddr, sizeof(void*)); |
| 90 | |
| 91 | uint64_t *Trampolines = reinterpret_cast<uint64_t*>(TrampolineMem); |
| 92 | uint64_t CallIndirPCRel = 0xf1c40000000015ff; |
| 93 | |
| 94 | for (unsigned I = 0; I < NumTrampolines; ++I, OffsetToPtr -= TrampolineSize) |
| 95 | Trampolines[I] = CallIndirPCRel | ((OffsetToPtr - 6) << 16); |
Lang Hames | 2754714 | 2015-02-17 01:18:38 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Lang Hames | 98c2ac1 | 2015-10-19 17:43:51 +0000 | [diff] [blame] | 98 | std::error_code OrcX86_64::emitIndirectStubsBlock(IndirectStubsInfo &StubsInfo, |
| 99 | unsigned MinStubs, |
| 100 | void *InitialPtrVal) { |
| 101 | // Stub format is: |
| 102 | // |
| 103 | // .section __orc_stubs |
| 104 | // stub1: |
| 105 | // jmpq *ptr1(%rip) |
| 106 | // .byte 0xC4 ; <- Invalid opcode padding. |
| 107 | // .byte 0xF1 |
| 108 | // stub2: |
| 109 | // jmpq *ptr2(%rip) |
| 110 | // |
| 111 | // ... |
| 112 | // |
| 113 | // .section __orc_ptrs |
| 114 | // ptr1: |
| 115 | // .quad 0x0 |
| 116 | // ptr2: |
| 117 | // .quad 0x0 |
| 118 | // |
| 119 | // ... |
| 120 | |
| 121 | const unsigned StubSize = IndirectStubsInfo::StubSize; |
| 122 | |
| 123 | // Emit at least MinStubs, rounded up to fill the pages allocated. |
| 124 | unsigned PageSize = sys::Process::getPageSize(); |
| 125 | unsigned NumPages = ((MinStubs * StubSize) + (PageSize - 1)) / PageSize; |
| 126 | unsigned NumStubs = (NumPages * PageSize) / StubSize; |
| 127 | |
| 128 | // Allocate memory for stubs and pointers in one call. |
| 129 | std::error_code EC; |
Lang Hames | 5796eb2 | 2015-10-31 00:55:32 +0000 | [diff] [blame] | 130 | auto StubsMem = |
| 131 | sys::OwningMemoryBlock( |
| 132 | sys::Memory::allocateMappedMemory(2 * NumPages * PageSize, nullptr, |
| 133 | sys::Memory::MF_READ | |
| 134 | sys::Memory::MF_WRITE, |
| 135 | EC)); |
Lang Hames | 98c2ac1 | 2015-10-19 17:43:51 +0000 | [diff] [blame] | 136 | |
| 137 | if (EC) |
| 138 | return EC; |
| 139 | |
| 140 | // Create separate MemoryBlocks representing the stubs and pointers. |
Lang Hames | 5796eb2 | 2015-10-31 00:55:32 +0000 | [diff] [blame] | 141 | sys::MemoryBlock StubsBlock(StubsMem.base(), NumPages * PageSize); |
| 142 | sys::MemoryBlock PtrsBlock(static_cast<char*>(StubsMem.base()) + |
| 143 | NumPages * PageSize, |
Lang Hames | 98c2ac1 | 2015-10-19 17:43:51 +0000 | [diff] [blame] | 144 | NumPages * PageSize); |
| 145 | |
| 146 | // Populate the stubs page stubs and mark it executable. |
| 147 | uint64_t *Stub = reinterpret_cast<uint64_t*>(StubsBlock.base()); |
| 148 | uint64_t PtrOffsetField = |
| 149 | static_cast<uint64_t>(NumPages * PageSize - 6) << 16; |
| 150 | for (unsigned I = 0; I < NumStubs; ++I) |
| 151 | Stub[I] = 0xF1C40000000025ff | PtrOffsetField; |
| 152 | |
| 153 | if (auto EC = sys::Memory::protectMappedMemory(StubsBlock, |
| 154 | sys::Memory::MF_READ | |
| 155 | sys::Memory::MF_EXEC)) |
| 156 | return EC; |
| 157 | |
| 158 | // Initialize all pointers to point at FailureAddress. |
| 159 | void **Ptr = reinterpret_cast<void**>(PtrsBlock.base()); |
| 160 | for (unsigned I = 0; I < NumStubs; ++I) |
| 161 | Ptr[I] = InitialPtrVal; |
| 162 | |
Lang Hames | e28b118 | 2016-02-02 19:31:15 +0000 | [diff] [blame] | 163 | StubsInfo = IndirectStubsInfo(NumStubs, std::move(StubsMem)); |
Lang Hames | 98c2ac1 | 2015-10-19 17:43:51 +0000 | [diff] [blame] | 164 | |
| 165 | return std::error_code(); |
| 166 | } |
| 167 | |
Lang Hames | e738061 | 2015-02-21 20:44:36 +0000 | [diff] [blame] | 168 | } // End namespace orc. |
| 169 | } // End namespace llvm. |