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 | 4f8194e | 2016-02-10 01:02:33 +0000 | [diff] [blame] | 168 | void OrcI386::writeResolverCode(uint8_t *ResolverMem, JITReentryFn ReentryFn, |
| 169 | void *CallbackMgr) { |
| 170 | |
| 171 | const uint8_t ResolverCode[] = { |
| 172 | // resolver_entry: |
| 173 | 0x55, // 0x00: pushl %ebp |
| 174 | 0x89, 0xe5, // 0x01: movl %esp, %ebp |
Lang Hames | e1fd99c | 2016-02-21 22:50:26 +0000 | [diff] [blame^] | 175 | 0x54, // 0x03: pushl %esp |
| 176 | 0x83, 0xe4, 0xf0, // 0x04: andl $-0x10, %esp |
| 177 | 0x50, // 0x07: pushl %eax |
| 178 | 0x53, // 0x08: pushl %ebx |
| 179 | 0x51, // 0x09: pushl %ecx |
| 180 | 0x52, // 0x0a: pushl %edx |
| 181 | 0x56, // 0x0b: pushl %esi |
| 182 | 0x57, // 0x0c: pushl %edi |
| 183 | 0x81, 0xec, 0x18, 0x02, 0x00, 0x00, // 0x0d: subl $0x218, %esp |
| 184 | 0x0f, 0xae, 0x44, 0x24, 0x10, // 0x13: fxsave 0x10(%esp) |
| 185 | 0x8b, 0x75, 0x04, // 0x18: movl 0x4(%ebp), %esi |
| 186 | 0x83, 0xee, 0x05, // 0x1b: subl $0x5, %esi |
| 187 | 0x89, 0x74, 0x24, 0x04, // 0x1e: movl %esi, 0x4(%esp) |
| 188 | 0xc7, 0x04, 0x24, 0x00, 0x00, 0x00, 0x00, // 0x22: movl <cbmgr>, (%esp) |
| 189 | 0xb8, 0x00, 0x00, 0x00, 0x00, // 0x29: movl <reentry>, %eax |
| 190 | 0xff, 0xd0, // 0x2e: calll *%eax |
| 191 | 0x89, 0x45, 0x04, // 0x30: movl %eax, 0x4(%ebp) |
| 192 | 0x0f, 0xae, 0x4c, 0x24, 0x10, // 0x33: fxrstor 0x10(%esp) |
| 193 | 0x81, 0xc4, 0x18, 0x02, 0x00, 0x00, // 0x38: addl $0x218, %esp |
| 194 | 0x5f, // 0x3e: popl %edi |
| 195 | 0x5e, // 0x3f: popl %esi |
| 196 | 0x5a, // 0x40: popl %edx |
| 197 | 0x59, // 0x41: popl %ecx |
| 198 | 0x5b, // 0x42: popl %ebx |
| 199 | 0x58, // 0x43: popl %eax |
| 200 | 0x8b, 0x65, 0xfc, // 0x44: movl -0x4(%ebp), %esp |
| 201 | 0x5d, // 0x48: popl %ebp |
| 202 | 0xc3 // 0x49: retl |
Lang Hames | 4f8194e | 2016-02-10 01:02:33 +0000 | [diff] [blame] | 203 | }; |
| 204 | |
Lang Hames | e1fd99c | 2016-02-21 22:50:26 +0000 | [diff] [blame^] | 205 | const unsigned ReentryFnAddrOffset = 0x2a; |
| 206 | const unsigned CallbackMgrAddrOffset = 0x25; |
Lang Hames | 4f8194e | 2016-02-10 01:02:33 +0000 | [diff] [blame] | 207 | |
| 208 | memcpy(ResolverMem, ResolverCode, sizeof(ResolverCode)); |
| 209 | memcpy(ResolverMem + ReentryFnAddrOffset, &ReentryFn, sizeof(ReentryFn)); |
| 210 | memcpy(ResolverMem + CallbackMgrAddrOffset, &CallbackMgr, |
| 211 | sizeof(CallbackMgr)); |
| 212 | } |
| 213 | |
| 214 | void OrcI386::writeTrampolines(uint8_t *TrampolineMem, void *ResolverAddr, |
| 215 | unsigned NumTrampolines) { |
| 216 | |
| 217 | uint64_t CallRelImm = 0xF1C4C400000000e8; |
| 218 | uint64_t Resolver = reinterpret_cast<uint64_t>(ResolverAddr); |
| 219 | uint64_t ResolverRel = |
| 220 | Resolver - reinterpret_cast<uint64_t>(TrampolineMem) - 5; |
| 221 | |
| 222 | uint64_t *Trampolines = reinterpret_cast<uint64_t*>(TrampolineMem); |
| 223 | for (unsigned I = 0; I < NumTrampolines; ++I, ResolverRel -= TrampolineSize) |
| 224 | Trampolines[I] = CallRelImm | (ResolverRel << 8); |
| 225 | } |
| 226 | |
| 227 | std::error_code OrcI386::emitIndirectStubsBlock(IndirectStubsInfo &StubsInfo, |
| 228 | unsigned MinStubs, |
| 229 | void *InitialPtrVal) { |
| 230 | // Stub format is: |
| 231 | // |
| 232 | // .section __orc_stubs |
| 233 | // stub1: |
| 234 | // jmpq *ptr1 |
| 235 | // .byte 0xC4 ; <- Invalid opcode padding. |
| 236 | // .byte 0xF1 |
| 237 | // stub2: |
| 238 | // jmpq *ptr2 |
| 239 | // |
| 240 | // ... |
| 241 | // |
| 242 | // .section __orc_ptrs |
| 243 | // ptr1: |
| 244 | // .quad 0x0 |
| 245 | // ptr2: |
| 246 | // .quad 0x0 |
| 247 | // |
| 248 | // ... |
| 249 | |
| 250 | const unsigned StubSize = IndirectStubsInfo::StubSize; |
| 251 | |
| 252 | // Emit at least MinStubs, rounded up to fill the pages allocated. |
| 253 | unsigned PageSize = sys::Process::getPageSize(); |
| 254 | unsigned NumPages = ((MinStubs * StubSize) + (PageSize - 1)) / PageSize; |
| 255 | unsigned NumStubs = (NumPages * PageSize) / StubSize; |
| 256 | |
| 257 | // Allocate memory for stubs and pointers in one call. |
| 258 | std::error_code EC; |
| 259 | auto StubsMem = |
| 260 | sys::OwningMemoryBlock( |
| 261 | sys::Memory::allocateMappedMemory(2 * NumPages * PageSize, nullptr, |
| 262 | sys::Memory::MF_READ | |
| 263 | sys::Memory::MF_WRITE, |
| 264 | EC)); |
| 265 | |
| 266 | if (EC) |
| 267 | return EC; |
| 268 | |
| 269 | // Create separate MemoryBlocks representing the stubs and pointers. |
| 270 | sys::MemoryBlock StubsBlock(StubsMem.base(), NumPages * PageSize); |
| 271 | sys::MemoryBlock PtrsBlock(static_cast<char*>(StubsMem.base()) + |
| 272 | NumPages * PageSize, |
| 273 | NumPages * PageSize); |
| 274 | |
| 275 | // Populate the stubs page stubs and mark it executable. |
| 276 | uint64_t *Stub = reinterpret_cast<uint64_t*>(StubsBlock.base()); |
| 277 | uint64_t PtrAddr = reinterpret_cast<uint64_t>(PtrsBlock.base()); |
| 278 | for (unsigned I = 0; I < NumStubs; ++I, PtrAddr += 4) |
| 279 | Stub[I] = 0xF1C40000000025ff | (PtrAddr << 16); |
| 280 | |
| 281 | if (auto EC = sys::Memory::protectMappedMemory(StubsBlock, |
| 282 | sys::Memory::MF_READ | |
| 283 | sys::Memory::MF_EXEC)) |
| 284 | return EC; |
| 285 | |
| 286 | // Initialize all pointers to point at FailureAddress. |
| 287 | void **Ptr = reinterpret_cast<void**>(PtrsBlock.base()); |
| 288 | for (unsigned I = 0; I < NumStubs; ++I) |
| 289 | Ptr[I] = InitialPtrVal; |
| 290 | |
| 291 | StubsInfo = IndirectStubsInfo(NumStubs, std::move(StubsMem)); |
| 292 | |
| 293 | return std::error_code(); |
| 294 | } |
| 295 | |
Lang Hames | e738061 | 2015-02-21 20:44:36 +0000 | [diff] [blame] | 296 | } // End namespace orc. |
| 297 | } // End namespace llvm. |