10b57cec5SDimitry Andric //===------------- OrcABISupport.cpp - ABI specific support code ----------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "llvm/ExecutionEngine/Orc/OrcABISupport.h"
105ffd83dbSDimitry Andric #include "llvm/Support/FormatVariadic.h"
110b57cec5SDimitry Andric #include "llvm/Support/Process.h"
125ffd83dbSDimitry Andric #include "llvm/Support/raw_ostream.h"
135ffd83dbSDimitry Andric
145ffd83dbSDimitry Andric #define DEBUG_TYPE "orc"
155ffd83dbSDimitry Andric
165ffd83dbSDimitry Andric using namespace llvm;
17*06c3fb27SDimitry Andric using namespace llvm::orc;
185ffd83dbSDimitry Andric
195ffd83dbSDimitry Andric template <typename ORCABI>
stubAndPointerRangesOk(ExecutorAddr StubBlockAddr,ExecutorAddr PointerBlockAddr,unsigned NumStubs)20*06c3fb27SDimitry Andric static bool stubAndPointerRangesOk(ExecutorAddr StubBlockAddr,
21*06c3fb27SDimitry Andric ExecutorAddr PointerBlockAddr,
225ffd83dbSDimitry Andric unsigned NumStubs) {
235ffd83dbSDimitry Andric constexpr unsigned MaxDisp = ORCABI::StubToPointerMaxDisplacement;
24*06c3fb27SDimitry Andric ExecutorAddr FirstStub = StubBlockAddr;
25*06c3fb27SDimitry Andric ExecutorAddr LastStub = FirstStub + ((NumStubs - 1) * ORCABI::StubSize);
26*06c3fb27SDimitry Andric ExecutorAddr FirstPointer = PointerBlockAddr;
27*06c3fb27SDimitry Andric ExecutorAddr LastPointer = FirstPointer + ((NumStubs - 1) * ORCABI::StubSize);
285ffd83dbSDimitry Andric
295ffd83dbSDimitry Andric if (FirstStub < FirstPointer) {
305ffd83dbSDimitry Andric if (LastStub >= FirstPointer)
315ffd83dbSDimitry Andric return false; // Ranges overlap.
325ffd83dbSDimitry Andric return (FirstPointer - FirstStub <= MaxDisp) &&
335ffd83dbSDimitry Andric (LastPointer - LastStub <= MaxDisp); // out-of-range.
345ffd83dbSDimitry Andric }
355ffd83dbSDimitry Andric
365ffd83dbSDimitry Andric if (LastPointer >= FirstStub)
375ffd83dbSDimitry Andric return false; // Ranges overlap.
385ffd83dbSDimitry Andric
395ffd83dbSDimitry Andric return (FirstStub - FirstPointer <= MaxDisp) &&
405ffd83dbSDimitry Andric (LastStub - LastPointer <= MaxDisp);
415ffd83dbSDimitry Andric }
420b57cec5SDimitry Andric
430b57cec5SDimitry Andric namespace llvm {
440b57cec5SDimitry Andric namespace orc {
450b57cec5SDimitry Andric
writeResolverCode(char * ResolverWorkingMem,ExecutorAddr ResolverTargetAddress,ExecutorAddr ReentryFnAddr,ExecutorAddr ReentryCtxAddr)465ffd83dbSDimitry Andric void OrcAArch64::writeResolverCode(char *ResolverWorkingMem,
47*06c3fb27SDimitry Andric ExecutorAddr ResolverTargetAddress,
48*06c3fb27SDimitry Andric ExecutorAddr ReentryFnAddr,
49*06c3fb27SDimitry Andric ExecutorAddr ReentryCtxAddr) {
500b57cec5SDimitry Andric
510b57cec5SDimitry Andric const uint32_t ResolverCode[] = {
520b57cec5SDimitry Andric // resolver_entry:
530b57cec5SDimitry Andric 0xa9bf47fd, // 0x000: stp x29, x17, [sp, #-16]!
540b57cec5SDimitry Andric 0x910003fd, // 0x004: mov x29, sp
550b57cec5SDimitry Andric 0xa9bf73fb, // 0x008: stp x27, x28, [sp, #-16]!
560b57cec5SDimitry Andric 0xa9bf6bf9, // 0x00c: stp x25, x26, [sp, #-16]!
570b57cec5SDimitry Andric 0xa9bf63f7, // 0x010: stp x23, x24, [sp, #-16]!
580b57cec5SDimitry Andric 0xa9bf5bf5, // 0x014: stp x21, x22, [sp, #-16]!
590b57cec5SDimitry Andric 0xa9bf53f3, // 0x018: stp x19, x20, [sp, #-16]!
600b57cec5SDimitry Andric 0xa9bf3fee, // 0x01c: stp x14, x15, [sp, #-16]!
610b57cec5SDimitry Andric 0xa9bf37ec, // 0x020: stp x12, x13, [sp, #-16]!
620b57cec5SDimitry Andric 0xa9bf2fea, // 0x024: stp x10, x11, [sp, #-16]!
630b57cec5SDimitry Andric 0xa9bf27e8, // 0x028: stp x8, x9, [sp, #-16]!
640b57cec5SDimitry Andric 0xa9bf1fe6, // 0x02c: stp x6, x7, [sp, #-16]!
650b57cec5SDimitry Andric 0xa9bf17e4, // 0x030: stp x4, x5, [sp, #-16]!
660b57cec5SDimitry Andric 0xa9bf0fe2, // 0x034: stp x2, x3, [sp, #-16]!
670b57cec5SDimitry Andric 0xa9bf07e0, // 0x038: stp x0, x1, [sp, #-16]!
680b57cec5SDimitry Andric 0xadbf7ffe, // 0x03c: stp q30, q31, [sp, #-32]!
690b57cec5SDimitry Andric 0xadbf77fc, // 0x040: stp q28, q29, [sp, #-32]!
700b57cec5SDimitry Andric 0xadbf6ffa, // 0x044: stp q26, q27, [sp, #-32]!
710b57cec5SDimitry Andric 0xadbf67f8, // 0x048: stp q24, q25, [sp, #-32]!
720b57cec5SDimitry Andric 0xadbf5ff6, // 0x04c: stp q22, q23, [sp, #-32]!
730b57cec5SDimitry Andric 0xadbf57f4, // 0x050: stp q20, q21, [sp, #-32]!
740b57cec5SDimitry Andric 0xadbf4ff2, // 0x054: stp q18, q19, [sp, #-32]!
750b57cec5SDimitry Andric 0xadbf47f0, // 0x058: stp q16, q17, [sp, #-32]!
760b57cec5SDimitry Andric 0xadbf3fee, // 0x05c: stp q14, q15, [sp, #-32]!
770b57cec5SDimitry Andric 0xadbf37ec, // 0x060: stp q12, q13, [sp, #-32]!
780b57cec5SDimitry Andric 0xadbf2fea, // 0x064: stp q10, q11, [sp, #-32]!
790b57cec5SDimitry Andric 0xadbf27e8, // 0x068: stp q8, q9, [sp, #-32]!
800b57cec5SDimitry Andric 0xadbf1fe6, // 0x06c: stp q6, q7, [sp, #-32]!
810b57cec5SDimitry Andric 0xadbf17e4, // 0x070: stp q4, q5, [sp, #-32]!
820b57cec5SDimitry Andric 0xadbf0fe2, // 0x074: stp q2, q3, [sp, #-32]!
830b57cec5SDimitry Andric 0xadbf07e0, // 0x078: stp q0, q1, [sp, #-32]!
845ffd83dbSDimitry Andric 0x580004e0, // 0x07c: ldr x0, Lreentry_ctx_ptr
850b57cec5SDimitry Andric 0xaa1e03e1, // 0x080: mov x1, x30
860b57cec5SDimitry Andric 0xd1003021, // 0x084: sub x1, x1, #12
870b57cec5SDimitry Andric 0x58000442, // 0x088: ldr x2, Lreentry_fn_ptr
880b57cec5SDimitry Andric 0xd63f0040, // 0x08c: blr x2
890b57cec5SDimitry Andric 0xaa0003f1, // 0x090: mov x17, x0
900b57cec5SDimitry Andric 0xacc107e0, // 0x094: ldp q0, q1, [sp], #32
910b57cec5SDimitry Andric 0xacc10fe2, // 0x098: ldp q2, q3, [sp], #32
920b57cec5SDimitry Andric 0xacc117e4, // 0x09c: ldp q4, q5, [sp], #32
930b57cec5SDimitry Andric 0xacc11fe6, // 0x0a0: ldp q6, q7, [sp], #32
940b57cec5SDimitry Andric 0xacc127e8, // 0x0a4: ldp q8, q9, [sp], #32
950b57cec5SDimitry Andric 0xacc12fea, // 0x0a8: ldp q10, q11, [sp], #32
960b57cec5SDimitry Andric 0xacc137ec, // 0x0ac: ldp q12, q13, [sp], #32
970b57cec5SDimitry Andric 0xacc13fee, // 0x0b0: ldp q14, q15, [sp], #32
980b57cec5SDimitry Andric 0xacc147f0, // 0x0b4: ldp q16, q17, [sp], #32
990b57cec5SDimitry Andric 0xacc14ff2, // 0x0b8: ldp q18, q19, [sp], #32
1000b57cec5SDimitry Andric 0xacc157f4, // 0x0bc: ldp q20, q21, [sp], #32
1010b57cec5SDimitry Andric 0xacc15ff6, // 0x0c0: ldp q22, q23, [sp], #32
1020b57cec5SDimitry Andric 0xacc167f8, // 0x0c4: ldp q24, q25, [sp], #32
1030b57cec5SDimitry Andric 0xacc16ffa, // 0x0c8: ldp q26, q27, [sp], #32
1040b57cec5SDimitry Andric 0xacc177fc, // 0x0cc: ldp q28, q29, [sp], #32
1050b57cec5SDimitry Andric 0xacc17ffe, // 0x0d0: ldp q30, q31, [sp], #32
1060b57cec5SDimitry Andric 0xa8c107e0, // 0x0d4: ldp x0, x1, [sp], #16
1070b57cec5SDimitry Andric 0xa8c10fe2, // 0x0d8: ldp x2, x3, [sp], #16
1080b57cec5SDimitry Andric 0xa8c117e4, // 0x0dc: ldp x4, x5, [sp], #16
1090b57cec5SDimitry Andric 0xa8c11fe6, // 0x0e0: ldp x6, x7, [sp], #16
1100b57cec5SDimitry Andric 0xa8c127e8, // 0x0e4: ldp x8, x9, [sp], #16
1110b57cec5SDimitry Andric 0xa8c12fea, // 0x0e8: ldp x10, x11, [sp], #16
1120b57cec5SDimitry Andric 0xa8c137ec, // 0x0ec: ldp x12, x13, [sp], #16
1130b57cec5SDimitry Andric 0xa8c13fee, // 0x0f0: ldp x14, x15, [sp], #16
1140b57cec5SDimitry Andric 0xa8c153f3, // 0x0f4: ldp x19, x20, [sp], #16
1150b57cec5SDimitry Andric 0xa8c15bf5, // 0x0f8: ldp x21, x22, [sp], #16
1160b57cec5SDimitry Andric 0xa8c163f7, // 0x0fc: ldp x23, x24, [sp], #16
1170b57cec5SDimitry Andric 0xa8c16bf9, // 0x100: ldp x25, x26, [sp], #16
1180b57cec5SDimitry Andric 0xa8c173fb, // 0x104: ldp x27, x28, [sp], #16
1190b57cec5SDimitry Andric 0xa8c17bfd, // 0x108: ldp x29, x30, [sp], #16
1200b57cec5SDimitry Andric 0xd65f0220, // 0x10c: ret x17
1210b57cec5SDimitry Andric 0x01234567, // 0x110: Lreentry_fn_ptr:
1220b57cec5SDimitry Andric 0xdeadbeef, // 0x114: .quad 0
1235ffd83dbSDimitry Andric 0x98765432, // 0x118: Lreentry_ctx_ptr:
1240b57cec5SDimitry Andric 0xcafef00d // 0x11c: .quad 0
1250b57cec5SDimitry Andric };
1260b57cec5SDimitry Andric
1270b57cec5SDimitry Andric const unsigned ReentryFnAddrOffset = 0x110;
1285ffd83dbSDimitry Andric const unsigned ReentryCtxAddrOffset = 0x118;
1290b57cec5SDimitry Andric
1305ffd83dbSDimitry Andric memcpy(ResolverWorkingMem, ResolverCode, sizeof(ResolverCode));
1315ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + ReentryFnAddrOffset, &ReentryFnAddr,
1325ffd83dbSDimitry Andric sizeof(uint64_t));
1335ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + ReentryCtxAddrOffset, &ReentryCtxAddr,
1345ffd83dbSDimitry Andric sizeof(uint64_t));
1350b57cec5SDimitry Andric }
1360b57cec5SDimitry Andric
writeTrampolines(char * TrampolineBlockWorkingMem,ExecutorAddr TrampolineBlockTargetAddress,ExecutorAddr ResolverAddr,unsigned NumTrampolines)1375ffd83dbSDimitry Andric void OrcAArch64::writeTrampolines(char *TrampolineBlockWorkingMem,
138*06c3fb27SDimitry Andric ExecutorAddr TrampolineBlockTargetAddress,
139*06c3fb27SDimitry Andric ExecutorAddr ResolverAddr,
1400b57cec5SDimitry Andric unsigned NumTrampolines) {
1410b57cec5SDimitry Andric
1420b57cec5SDimitry Andric unsigned OffsetToPtr = alignTo(NumTrampolines * TrampolineSize, 8);
1430b57cec5SDimitry Andric
1445ffd83dbSDimitry Andric memcpy(TrampolineBlockWorkingMem + OffsetToPtr, &ResolverAddr,
1455ffd83dbSDimitry Andric sizeof(uint64_t));
1460b57cec5SDimitry Andric
1470b57cec5SDimitry Andric // OffsetToPtr is actually the offset from the PC for the 2nd instruction, so
1480b57cec5SDimitry Andric // subtract 32-bits.
1490b57cec5SDimitry Andric OffsetToPtr -= 4;
1500b57cec5SDimitry Andric
1515ffd83dbSDimitry Andric uint32_t *Trampolines =
1525ffd83dbSDimitry Andric reinterpret_cast<uint32_t *>(TrampolineBlockWorkingMem);
1530b57cec5SDimitry Andric
1540b57cec5SDimitry Andric for (unsigned I = 0; I < NumTrampolines; ++I, OffsetToPtr -= TrampolineSize) {
1550b57cec5SDimitry Andric Trampolines[3 * I + 0] = 0xaa1e03f1; // mov x17, x30
1560b57cec5SDimitry Andric Trampolines[3 * I + 1] = 0x58000010 | (OffsetToPtr << 3); // adr x16, Lptr
1570b57cec5SDimitry Andric Trampolines[3 * I + 2] = 0xd63f0200; // blr x16
1580b57cec5SDimitry Andric }
1590b57cec5SDimitry Andric }
1600b57cec5SDimitry Andric
writeIndirectStubsBlock(char * StubsBlockWorkingMem,ExecutorAddr StubsBlockTargetAddress,ExecutorAddr PointersBlockTargetAddress,unsigned NumStubs)1615ffd83dbSDimitry Andric void OrcAArch64::writeIndirectStubsBlock(
162*06c3fb27SDimitry Andric char *StubsBlockWorkingMem, ExecutorAddr StubsBlockTargetAddress,
163*06c3fb27SDimitry Andric ExecutorAddr PointersBlockTargetAddress, unsigned NumStubs) {
1640b57cec5SDimitry Andric // Stub format is:
1650b57cec5SDimitry Andric //
1660b57cec5SDimitry Andric // .section __orc_stubs
1670b57cec5SDimitry Andric // stub1:
168*06c3fb27SDimitry Andric // ldr x16, ptr1 ; PC-rel load of ptr1
169*06c3fb27SDimitry Andric // br x16 ; Jump to resolver
1700b57cec5SDimitry Andric // stub2:
171*06c3fb27SDimitry Andric // ldr x16, ptr2 ; PC-rel load of ptr2
172*06c3fb27SDimitry Andric // br x16 ; Jump to resolver
1730b57cec5SDimitry Andric //
1740b57cec5SDimitry Andric // ...
1750b57cec5SDimitry Andric //
1760b57cec5SDimitry Andric // .section __orc_ptrs
1770b57cec5SDimitry Andric // ptr1:
1780b57cec5SDimitry Andric // .quad 0x0
1790b57cec5SDimitry Andric // ptr2:
1800b57cec5SDimitry Andric // .quad 0x0
1810b57cec5SDimitry Andric //
1820b57cec5SDimitry Andric // ...
1830b57cec5SDimitry Andric
1845ffd83dbSDimitry Andric static_assert(StubSize == PointerSize,
1855ffd83dbSDimitry Andric "Pointer and stub size must match for algorithm below");
1865ffd83dbSDimitry Andric assert(stubAndPointerRangesOk<OrcAArch64>(
1875ffd83dbSDimitry Andric StubsBlockTargetAddress, PointersBlockTargetAddress, NumStubs) &&
1885ffd83dbSDimitry Andric "PointersBlock is out of range");
1895ffd83dbSDimitry Andric uint64_t PtrDisplacement =
1905ffd83dbSDimitry Andric PointersBlockTargetAddress - StubsBlockTargetAddress;
191*06c3fb27SDimitry Andric assert((PtrDisplacement % 8 == 0) &&
192*06c3fb27SDimitry Andric "Displacement to pointer is not a multiple of 8");
1935ffd83dbSDimitry Andric uint64_t *Stub = reinterpret_cast<uint64_t *>(StubsBlockWorkingMem);
194*06c3fb27SDimitry Andric uint64_t PtrOffsetField = ((PtrDisplacement >> 2) & 0x7ffff) << 5;
1950b57cec5SDimitry Andric
1960b57cec5SDimitry Andric for (unsigned I = 0; I < NumStubs; ++I)
1970b57cec5SDimitry Andric Stub[I] = 0xd61f020058000010 | PtrOffsetField;
1980b57cec5SDimitry Andric }
1990b57cec5SDimitry Andric
writeTrampolines(char * TrampolineBlockWorkingMem,ExecutorAddr TrampolineBlockTargetAddress,ExecutorAddr ResolverAddr,unsigned NumTrampolines)200*06c3fb27SDimitry Andric void OrcX86_64_Base::writeTrampolines(char *TrampolineBlockWorkingMem,
201*06c3fb27SDimitry Andric ExecutorAddr TrampolineBlockTargetAddress,
202*06c3fb27SDimitry Andric ExecutorAddr ResolverAddr,
203*06c3fb27SDimitry Andric unsigned NumTrampolines) {
2040b57cec5SDimitry Andric
2050b57cec5SDimitry Andric unsigned OffsetToPtr = NumTrampolines * TrampolineSize;
2060b57cec5SDimitry Andric
2075ffd83dbSDimitry Andric memcpy(TrampolineBlockWorkingMem + OffsetToPtr, &ResolverAddr,
2085ffd83dbSDimitry Andric sizeof(uint64_t));
2090b57cec5SDimitry Andric
2105ffd83dbSDimitry Andric uint64_t *Trampolines =
2115ffd83dbSDimitry Andric reinterpret_cast<uint64_t *>(TrampolineBlockWorkingMem);
2120b57cec5SDimitry Andric uint64_t CallIndirPCRel = 0xf1c40000000015ff;
2130b57cec5SDimitry Andric
2140b57cec5SDimitry Andric for (unsigned I = 0; I < NumTrampolines; ++I, OffsetToPtr -= TrampolineSize)
2150b57cec5SDimitry Andric Trampolines[I] = CallIndirPCRel | ((OffsetToPtr - 6) << 16);
2160b57cec5SDimitry Andric }
2170b57cec5SDimitry Andric
writeIndirectStubsBlock(char * StubsBlockWorkingMem,ExecutorAddr StubsBlockTargetAddress,ExecutorAddr PointersBlockTargetAddress,unsigned NumStubs)2185ffd83dbSDimitry Andric void OrcX86_64_Base::writeIndirectStubsBlock(
219*06c3fb27SDimitry Andric char *StubsBlockWorkingMem, ExecutorAddr StubsBlockTargetAddress,
220*06c3fb27SDimitry Andric ExecutorAddr PointersBlockTargetAddress, unsigned NumStubs) {
2210b57cec5SDimitry Andric // Stub format is:
2220b57cec5SDimitry Andric //
2230b57cec5SDimitry Andric // .section __orc_stubs
2240b57cec5SDimitry Andric // stub1:
2250b57cec5SDimitry Andric // jmpq *ptr1(%rip)
2260b57cec5SDimitry Andric // .byte 0xC4 ; <- Invalid opcode padding.
2270b57cec5SDimitry Andric // .byte 0xF1
2280b57cec5SDimitry Andric // stub2:
2290b57cec5SDimitry Andric // jmpq *ptr2(%rip)
2300b57cec5SDimitry Andric //
2310b57cec5SDimitry Andric // ...
2320b57cec5SDimitry Andric //
2330b57cec5SDimitry Andric // .section __orc_ptrs
2340b57cec5SDimitry Andric // ptr1:
2350b57cec5SDimitry Andric // .quad 0x0
2360b57cec5SDimitry Andric // ptr2:
2370b57cec5SDimitry Andric // .quad 0x0
2380b57cec5SDimitry Andric //
2390b57cec5SDimitry Andric // ...
2400b57cec5SDimitry Andric
2410b57cec5SDimitry Andric // Populate the stubs page stubs and mark it executable.
2425ffd83dbSDimitry Andric static_assert(StubSize == PointerSize,
2435ffd83dbSDimitry Andric "Pointer and stub size must match for algorithm below");
2445ffd83dbSDimitry Andric assert(stubAndPointerRangesOk<OrcX86_64_Base>(
2455ffd83dbSDimitry Andric StubsBlockTargetAddress, PointersBlockTargetAddress, NumStubs) &&
2465ffd83dbSDimitry Andric "PointersBlock is out of range");
2475ffd83dbSDimitry Andric uint64_t *Stub = reinterpret_cast<uint64_t *>(StubsBlockWorkingMem);
2485ffd83dbSDimitry Andric uint64_t PtrOffsetField =
2495ffd83dbSDimitry Andric (PointersBlockTargetAddress - StubsBlockTargetAddress - 6) << 16;
2500b57cec5SDimitry Andric for (unsigned I = 0; I < NumStubs; ++I)
2510b57cec5SDimitry Andric Stub[I] = 0xF1C40000000025ff | PtrOffsetField;
2520b57cec5SDimitry Andric }
2530b57cec5SDimitry Andric
writeResolverCode(char * ResolverWorkingMem,ExecutorAddr ResolverTargetAddress,ExecutorAddr ReentryFnAddr,ExecutorAddr ReentryCtxAddr)2545ffd83dbSDimitry Andric void OrcX86_64_SysV::writeResolverCode(char *ResolverWorkingMem,
255*06c3fb27SDimitry Andric ExecutorAddr ResolverTargetAddress,
256*06c3fb27SDimitry Andric ExecutorAddr ReentryFnAddr,
257*06c3fb27SDimitry Andric ExecutorAddr ReentryCtxAddr) {
2585ffd83dbSDimitry Andric
2595ffd83dbSDimitry Andric LLVM_DEBUG({
2605ffd83dbSDimitry Andric dbgs() << "Writing resolver code to "
2615ffd83dbSDimitry Andric << formatv("{0:x16}", ResolverTargetAddress) << "\n";
2625ffd83dbSDimitry Andric });
2630b57cec5SDimitry Andric
2640b57cec5SDimitry Andric const uint8_t ResolverCode[] = {
2650b57cec5SDimitry Andric // resolver_entry:
2660b57cec5SDimitry Andric 0x55, // 0x00: pushq %rbp
2670b57cec5SDimitry Andric 0x48, 0x89, 0xe5, // 0x01: movq %rsp, %rbp
2680b57cec5SDimitry Andric 0x50, // 0x04: pushq %rax
2690b57cec5SDimitry Andric 0x53, // 0x05: pushq %rbx
2700b57cec5SDimitry Andric 0x51, // 0x06: pushq %rcx
2710b57cec5SDimitry Andric 0x52, // 0x07: pushq %rdx
2720b57cec5SDimitry Andric 0x56, // 0x08: pushq %rsi
2730b57cec5SDimitry Andric 0x57, // 0x09: pushq %rdi
2740b57cec5SDimitry Andric 0x41, 0x50, // 0x0a: pushq %r8
2750b57cec5SDimitry Andric 0x41, 0x51, // 0x0c: pushq %r9
2760b57cec5SDimitry Andric 0x41, 0x52, // 0x0e: pushq %r10
2770b57cec5SDimitry Andric 0x41, 0x53, // 0x10: pushq %r11
2780b57cec5SDimitry Andric 0x41, 0x54, // 0x12: pushq %r12
2790b57cec5SDimitry Andric 0x41, 0x55, // 0x14: pushq %r13
2800b57cec5SDimitry Andric 0x41, 0x56, // 0x16: pushq %r14
2810b57cec5SDimitry Andric 0x41, 0x57, // 0x18: pushq %r15
2820b57cec5SDimitry Andric 0x48, 0x81, 0xec, 0x08, 0x02, 0x00, 0x00, // 0x1a: subq 0x208, %rsp
2830b57cec5SDimitry Andric 0x48, 0x0f, 0xae, 0x04, 0x24, // 0x21: fxsave64 (%rsp)
2840b57cec5SDimitry Andric 0x48, 0xbf, // 0x26: movabsq <CBMgr>, %rdi
2850b57cec5SDimitry Andric
2865ffd83dbSDimitry Andric // 0x28: JIT re-entry ctx addr.
2870b57cec5SDimitry Andric 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2880b57cec5SDimitry Andric
2890b57cec5SDimitry Andric 0x48, 0x8b, 0x75, 0x08, // 0x30: movq 8(%rbp), %rsi
2900b57cec5SDimitry Andric 0x48, 0x83, 0xee, 0x06, // 0x34: subq $6, %rsi
2910b57cec5SDimitry Andric 0x48, 0xb8, // 0x38: movabsq <REntry>, %rax
2920b57cec5SDimitry Andric
2930b57cec5SDimitry Andric // 0x3a: JIT re-entry fn addr:
2940b57cec5SDimitry Andric 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2950b57cec5SDimitry Andric
2960b57cec5SDimitry Andric 0xff, 0xd0, // 0x42: callq *%rax
2970b57cec5SDimitry Andric 0x48, 0x89, 0x45, 0x08, // 0x44: movq %rax, 8(%rbp)
2980b57cec5SDimitry Andric 0x48, 0x0f, 0xae, 0x0c, 0x24, // 0x48: fxrstor64 (%rsp)
2990b57cec5SDimitry Andric 0x48, 0x81, 0xc4, 0x08, 0x02, 0x00, 0x00, // 0x4d: addq 0x208, %rsp
3000b57cec5SDimitry Andric 0x41, 0x5f, // 0x54: popq %r15
3010b57cec5SDimitry Andric 0x41, 0x5e, // 0x56: popq %r14
3020b57cec5SDimitry Andric 0x41, 0x5d, // 0x58: popq %r13
3030b57cec5SDimitry Andric 0x41, 0x5c, // 0x5a: popq %r12
3040b57cec5SDimitry Andric 0x41, 0x5b, // 0x5c: popq %r11
3050b57cec5SDimitry Andric 0x41, 0x5a, // 0x5e: popq %r10
3060b57cec5SDimitry Andric 0x41, 0x59, // 0x60: popq %r9
3070b57cec5SDimitry Andric 0x41, 0x58, // 0x62: popq %r8
3080b57cec5SDimitry Andric 0x5f, // 0x64: popq %rdi
3090b57cec5SDimitry Andric 0x5e, // 0x65: popq %rsi
3100b57cec5SDimitry Andric 0x5a, // 0x66: popq %rdx
3110b57cec5SDimitry Andric 0x59, // 0x67: popq %rcx
3120b57cec5SDimitry Andric 0x5b, // 0x68: popq %rbx
3130b57cec5SDimitry Andric 0x58, // 0x69: popq %rax
3140b57cec5SDimitry Andric 0x5d, // 0x6a: popq %rbp
3150b57cec5SDimitry Andric 0xc3, // 0x6b: retq
3160b57cec5SDimitry Andric };
3170b57cec5SDimitry Andric
3180b57cec5SDimitry Andric const unsigned ReentryFnAddrOffset = 0x3a;
3195ffd83dbSDimitry Andric const unsigned ReentryCtxAddrOffset = 0x28;
3200b57cec5SDimitry Andric
3215ffd83dbSDimitry Andric memcpy(ResolverWorkingMem, ResolverCode, sizeof(ResolverCode));
3225ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + ReentryFnAddrOffset, &ReentryFnAddr,
3235ffd83dbSDimitry Andric sizeof(uint64_t));
3245ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + ReentryCtxAddrOffset, &ReentryCtxAddr,
3255ffd83dbSDimitry Andric sizeof(uint64_t));
3260b57cec5SDimitry Andric }
3270b57cec5SDimitry Andric
writeResolverCode(char * ResolverWorkingMem,ExecutorAddr ResolverTargetAddress,ExecutorAddr ReentryFnAddr,ExecutorAddr ReentryCtxAddr)3285ffd83dbSDimitry Andric void OrcX86_64_Win32::writeResolverCode(char *ResolverWorkingMem,
329*06c3fb27SDimitry Andric ExecutorAddr ResolverTargetAddress,
330*06c3fb27SDimitry Andric ExecutorAddr ReentryFnAddr,
331*06c3fb27SDimitry Andric ExecutorAddr ReentryCtxAddr) {
3320b57cec5SDimitry Andric
3335ffd83dbSDimitry Andric // resolverCode is similar to OrcX86_64 with differences specific to windows
3345ffd83dbSDimitry Andric // x64 calling convention: arguments go into rcx, rdx and come in reverse
3355ffd83dbSDimitry Andric // order, shadow space allocation on stack
3360b57cec5SDimitry Andric const uint8_t ResolverCode[] = {
3370b57cec5SDimitry Andric // resolver_entry:
3380b57cec5SDimitry Andric 0x55, // 0x00: pushq %rbp
3390b57cec5SDimitry Andric 0x48, 0x89, 0xe5, // 0x01: movq %rsp, %rbp
3400b57cec5SDimitry Andric 0x50, // 0x04: pushq %rax
3410b57cec5SDimitry Andric 0x53, // 0x05: pushq %rbx
3420b57cec5SDimitry Andric 0x51, // 0x06: pushq %rcx
3430b57cec5SDimitry Andric 0x52, // 0x07: pushq %rdx
3440b57cec5SDimitry Andric 0x56, // 0x08: pushq %rsi
3450b57cec5SDimitry Andric 0x57, // 0x09: pushq %rdi
3460b57cec5SDimitry Andric 0x41, 0x50, // 0x0a: pushq %r8
3470b57cec5SDimitry Andric 0x41, 0x51, // 0x0c: pushq %r9
3480b57cec5SDimitry Andric 0x41, 0x52, // 0x0e: pushq %r10
3490b57cec5SDimitry Andric 0x41, 0x53, // 0x10: pushq %r11
3500b57cec5SDimitry Andric 0x41, 0x54, // 0x12: pushq %r12
3510b57cec5SDimitry Andric 0x41, 0x55, // 0x14: pushq %r13
3520b57cec5SDimitry Andric 0x41, 0x56, // 0x16: pushq %r14
3530b57cec5SDimitry Andric 0x41, 0x57, // 0x18: pushq %r15
3540b57cec5SDimitry Andric 0x48, 0x81, 0xec, 0x08, 0x02, 0x00, 0x00, // 0x1a: subq 0x208, %rsp
3550b57cec5SDimitry Andric 0x48, 0x0f, 0xae, 0x04, 0x24, // 0x21: fxsave64 (%rsp)
3560b57cec5SDimitry Andric
3570b57cec5SDimitry Andric 0x48, 0xb9, // 0x26: movabsq <CBMgr>, %rcx
3585ffd83dbSDimitry Andric // 0x28: JIT re-entry ctx addr.
3590b57cec5SDimitry Andric 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3600b57cec5SDimitry Andric
3610b57cec5SDimitry Andric 0x48, 0x8B, 0x55, 0x08, // 0x30: mov rdx, [rbp+0x8]
3620b57cec5SDimitry Andric 0x48, 0x83, 0xea, 0x06, // 0x34: sub rdx, 0x6
3630b57cec5SDimitry Andric
3640b57cec5SDimitry Andric 0x48, 0xb8, // 0x38: movabsq <REntry>, %rax
3650b57cec5SDimitry Andric // 0x3a: JIT re-entry fn addr:
3660b57cec5SDimitry Andric 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3670b57cec5SDimitry Andric
3680b57cec5SDimitry Andric // 0x42: sub rsp, 0x20 (Allocate shadow space)
3690b57cec5SDimitry Andric 0x48, 0x83, 0xEC, 0x20,
3700b57cec5SDimitry Andric 0xff, 0xd0, // 0x46: callq *%rax
3710b57cec5SDimitry Andric
3720b57cec5SDimitry Andric // 0x48: add rsp, 0x20 (Free shadow space)
3730b57cec5SDimitry Andric 0x48, 0x83, 0xC4, 0x20,
3740b57cec5SDimitry Andric
3750b57cec5SDimitry Andric 0x48, 0x89, 0x45, 0x08, // 0x4C: movq %rax, 8(%rbp)
3760b57cec5SDimitry Andric 0x48, 0x0f, 0xae, 0x0c, 0x24, // 0x50: fxrstor64 (%rsp)
3770b57cec5SDimitry Andric 0x48, 0x81, 0xc4, 0x08, 0x02, 0x00, 0x00, // 0x55: addq 0x208, %rsp
3780b57cec5SDimitry Andric 0x41, 0x5f, // 0x5C: popq %r15
3790b57cec5SDimitry Andric 0x41, 0x5e, // 0x5E: popq %r14
3800b57cec5SDimitry Andric 0x41, 0x5d, // 0x60: popq %r13
3810b57cec5SDimitry Andric 0x41, 0x5c, // 0x62: popq %r12
3820b57cec5SDimitry Andric 0x41, 0x5b, // 0x64: popq %r11
3830b57cec5SDimitry Andric 0x41, 0x5a, // 0x66: popq %r10
3840b57cec5SDimitry Andric 0x41, 0x59, // 0x68: popq %r9
3850b57cec5SDimitry Andric 0x41, 0x58, // 0x6a: popq %r8
3860b57cec5SDimitry Andric 0x5f, // 0x6c: popq %rdi
3870b57cec5SDimitry Andric 0x5e, // 0x6d: popq %rsi
3880b57cec5SDimitry Andric 0x5a, // 0x6e: popq %rdx
3890b57cec5SDimitry Andric 0x59, // 0x6f: popq %rcx
3900b57cec5SDimitry Andric 0x5b, // 0x70: popq %rbx
3910b57cec5SDimitry Andric 0x58, // 0x71: popq %rax
3920b57cec5SDimitry Andric 0x5d, // 0x72: popq %rbp
3930b57cec5SDimitry Andric 0xc3, // 0x73: retq
3940b57cec5SDimitry Andric };
3950b57cec5SDimitry Andric
3960b57cec5SDimitry Andric const unsigned ReentryFnAddrOffset = 0x3a;
3975ffd83dbSDimitry Andric const unsigned ReentryCtxAddrOffset = 0x28;
3980b57cec5SDimitry Andric
3995ffd83dbSDimitry Andric memcpy(ResolverWorkingMem, ResolverCode, sizeof(ResolverCode));
4005ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + ReentryFnAddrOffset, &ReentryFnAddr,
4015ffd83dbSDimitry Andric sizeof(uint64_t));
4025ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + ReentryCtxAddrOffset, &ReentryCtxAddr,
4035ffd83dbSDimitry Andric sizeof(uint64_t));
4040b57cec5SDimitry Andric }
4050b57cec5SDimitry Andric
writeResolverCode(char * ResolverWorkingMem,ExecutorAddr ResolverTargetAddress,ExecutorAddr ReentryFnAddr,ExecutorAddr ReentryCtxAddr)4065ffd83dbSDimitry Andric void OrcI386::writeResolverCode(char *ResolverWorkingMem,
407*06c3fb27SDimitry Andric ExecutorAddr ResolverTargetAddress,
408*06c3fb27SDimitry Andric ExecutorAddr ReentryFnAddr,
409*06c3fb27SDimitry Andric ExecutorAddr ReentryCtxAddr) {
4105ffd83dbSDimitry Andric
411*06c3fb27SDimitry Andric assert((ReentryFnAddr.getValue() >> 32) == 0 && "ReentryFnAddr out of range");
412*06c3fb27SDimitry Andric assert((ReentryCtxAddr.getValue() >> 32) == 0 &&
413*06c3fb27SDimitry Andric "ReentryCtxAddr out of range");
4140b57cec5SDimitry Andric
4150b57cec5SDimitry Andric const uint8_t ResolverCode[] = {
4160b57cec5SDimitry Andric // resolver_entry:
4170b57cec5SDimitry Andric 0x55, // 0x00: pushl %ebp
4180b57cec5SDimitry Andric 0x89, 0xe5, // 0x01: movl %esp, %ebp
4190b57cec5SDimitry Andric 0x54, // 0x03: pushl %esp
4200b57cec5SDimitry Andric 0x83, 0xe4, 0xf0, // 0x04: andl $-0x10, %esp
4210b57cec5SDimitry Andric 0x50, // 0x07: pushl %eax
4220b57cec5SDimitry Andric 0x53, // 0x08: pushl %ebx
4230b57cec5SDimitry Andric 0x51, // 0x09: pushl %ecx
4240b57cec5SDimitry Andric 0x52, // 0x0a: pushl %edx
4250b57cec5SDimitry Andric 0x56, // 0x0b: pushl %esi
4260b57cec5SDimitry Andric 0x57, // 0x0c: pushl %edi
4270b57cec5SDimitry Andric 0x81, 0xec, 0x18, 0x02, 0x00, 0x00, // 0x0d: subl $0x218, %esp
4280b57cec5SDimitry Andric 0x0f, 0xae, 0x44, 0x24, 0x10, // 0x13: fxsave 0x10(%esp)
4290b57cec5SDimitry Andric 0x8b, 0x75, 0x04, // 0x18: movl 0x4(%ebp), %esi
4300b57cec5SDimitry Andric 0x83, 0xee, 0x05, // 0x1b: subl $0x5, %esi
4310b57cec5SDimitry Andric 0x89, 0x74, 0x24, 0x04, // 0x1e: movl %esi, 0x4(%esp)
4320b57cec5SDimitry Andric 0xc7, 0x04, 0x24, 0x00, 0x00, 0x00,
4330b57cec5SDimitry Andric 0x00, // 0x22: movl <cbmgr>, (%esp)
4340b57cec5SDimitry Andric 0xb8, 0x00, 0x00, 0x00, 0x00, // 0x29: movl <reentry>, %eax
4350b57cec5SDimitry Andric 0xff, 0xd0, // 0x2e: calll *%eax
4360b57cec5SDimitry Andric 0x89, 0x45, 0x04, // 0x30: movl %eax, 0x4(%ebp)
4370b57cec5SDimitry Andric 0x0f, 0xae, 0x4c, 0x24, 0x10, // 0x33: fxrstor 0x10(%esp)
4380b57cec5SDimitry Andric 0x81, 0xc4, 0x18, 0x02, 0x00, 0x00, // 0x38: addl $0x218, %esp
4390b57cec5SDimitry Andric 0x5f, // 0x3e: popl %edi
4400b57cec5SDimitry Andric 0x5e, // 0x3f: popl %esi
4410b57cec5SDimitry Andric 0x5a, // 0x40: popl %edx
4420b57cec5SDimitry Andric 0x59, // 0x41: popl %ecx
4430b57cec5SDimitry Andric 0x5b, // 0x42: popl %ebx
4440b57cec5SDimitry Andric 0x58, // 0x43: popl %eax
4450b57cec5SDimitry Andric 0x8b, 0x65, 0xfc, // 0x44: movl -0x4(%ebp), %esp
4460b57cec5SDimitry Andric 0x5d, // 0x48: popl %ebp
4470b57cec5SDimitry Andric 0xc3 // 0x49: retl
4480b57cec5SDimitry Andric };
4490b57cec5SDimitry Andric
4500b57cec5SDimitry Andric const unsigned ReentryFnAddrOffset = 0x2a;
4515ffd83dbSDimitry Andric const unsigned ReentryCtxAddrOffset = 0x25;
4520b57cec5SDimitry Andric
4535ffd83dbSDimitry Andric memcpy(ResolverWorkingMem, ResolverCode, sizeof(ResolverCode));
4545ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + ReentryFnAddrOffset, &ReentryFnAddr,
4555ffd83dbSDimitry Andric sizeof(uint32_t));
4565ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + ReentryCtxAddrOffset, &ReentryCtxAddr,
4575ffd83dbSDimitry Andric sizeof(uint32_t));
4580b57cec5SDimitry Andric }
4590b57cec5SDimitry Andric
writeTrampolines(char * TrampolineWorkingMem,ExecutorAddr TrampolineBlockTargetAddress,ExecutorAddr ResolverAddr,unsigned NumTrampolines)4605ffd83dbSDimitry Andric void OrcI386::writeTrampolines(char *TrampolineWorkingMem,
461*06c3fb27SDimitry Andric ExecutorAddr TrampolineBlockTargetAddress,
462*06c3fb27SDimitry Andric ExecutorAddr ResolverAddr,
4630b57cec5SDimitry Andric unsigned NumTrampolines) {
464*06c3fb27SDimitry Andric assert((ResolverAddr.getValue() >> 32) == 0 && "ResolverAddr out of range");
4650b57cec5SDimitry Andric
4660b57cec5SDimitry Andric uint64_t CallRelImm = 0xF1C4C400000000e8;
4675ffd83dbSDimitry Andric uint64_t ResolverRel = ResolverAddr - TrampolineBlockTargetAddress - 5;
4680b57cec5SDimitry Andric
4695ffd83dbSDimitry Andric uint64_t *Trampolines = reinterpret_cast<uint64_t *>(TrampolineWorkingMem);
4700b57cec5SDimitry Andric for (unsigned I = 0; I < NumTrampolines; ++I, ResolverRel -= TrampolineSize)
4710b57cec5SDimitry Andric Trampolines[I] = CallRelImm | (ResolverRel << 8);
4720b57cec5SDimitry Andric }
4730b57cec5SDimitry Andric
writeIndirectStubsBlock(char * StubsBlockWorkingMem,ExecutorAddr StubsBlockTargetAddress,ExecutorAddr PointersBlockTargetAddress,unsigned NumStubs)474*06c3fb27SDimitry Andric void OrcI386::writeIndirectStubsBlock(char *StubsBlockWorkingMem,
475*06c3fb27SDimitry Andric ExecutorAddr StubsBlockTargetAddress,
476*06c3fb27SDimitry Andric ExecutorAddr PointersBlockTargetAddress,
477*06c3fb27SDimitry Andric unsigned NumStubs) {
478*06c3fb27SDimitry Andric assert((StubsBlockTargetAddress.getValue() >> 32) == 0 &&
4795ffd83dbSDimitry Andric "StubsBlockTargetAddress is out of range");
480*06c3fb27SDimitry Andric assert((PointersBlockTargetAddress.getValue() >> 32) == 0 &&
4815ffd83dbSDimitry Andric "PointersBlockTargetAddress is out of range");
4825ffd83dbSDimitry Andric
4830b57cec5SDimitry Andric // Stub format is:
4840b57cec5SDimitry Andric //
4850b57cec5SDimitry Andric // .section __orc_stubs
4860b57cec5SDimitry Andric // stub1:
4870b57cec5SDimitry Andric // jmpq *ptr1
4880b57cec5SDimitry Andric // .byte 0xC4 ; <- Invalid opcode padding.
4890b57cec5SDimitry Andric // .byte 0xF1
4900b57cec5SDimitry Andric // stub2:
4910b57cec5SDimitry Andric // jmpq *ptr2
4920b57cec5SDimitry Andric //
4930b57cec5SDimitry Andric // ...
4940b57cec5SDimitry Andric //
4950b57cec5SDimitry Andric // .section __orc_ptrs
4960b57cec5SDimitry Andric // ptr1:
4970b57cec5SDimitry Andric // .quad 0x0
4980b57cec5SDimitry Andric // ptr2:
4990b57cec5SDimitry Andric // .quad 0x0
5000b57cec5SDimitry Andric //
5010b57cec5SDimitry Andric // ...
5020b57cec5SDimitry Andric
5035ffd83dbSDimitry Andric assert(stubAndPointerRangesOk<OrcI386>(
5045ffd83dbSDimitry Andric StubsBlockTargetAddress, PointersBlockTargetAddress, NumStubs) &&
5055ffd83dbSDimitry Andric "PointersBlock is out of range");
5060b57cec5SDimitry Andric
5075ffd83dbSDimitry Andric uint64_t *Stub = reinterpret_cast<uint64_t *>(StubsBlockWorkingMem);
508*06c3fb27SDimitry Andric uint64_t PtrAddr = PointersBlockTargetAddress.getValue();
5090b57cec5SDimitry Andric for (unsigned I = 0; I < NumStubs; ++I, PtrAddr += 4)
5100b57cec5SDimitry Andric Stub[I] = 0xF1C40000000025ff | (PtrAddr << 16);
5110b57cec5SDimitry Andric }
5120b57cec5SDimitry Andric
writeResolverCode(char * ResolverWorkingMem,ExecutorAddr ResolverTargetAddress,ExecutorAddr ReentryFnAddr,ExecutorAddr ReentryCtxAddr,bool isBigEndian)5135ffd83dbSDimitry Andric void OrcMips32_Base::writeResolverCode(char *ResolverWorkingMem,
514*06c3fb27SDimitry Andric ExecutorAddr ResolverTargetAddress,
515*06c3fb27SDimitry Andric ExecutorAddr ReentryFnAddr,
516*06c3fb27SDimitry Andric ExecutorAddr ReentryCtxAddr,
5175ffd83dbSDimitry Andric bool isBigEndian) {
5180b57cec5SDimitry Andric
5190b57cec5SDimitry Andric const uint32_t ResolverCode[] = {
5200b57cec5SDimitry Andric // resolver_entry:
5210b57cec5SDimitry Andric 0x27bdff98, // 0x00: addiu $sp,$sp,-104
5220b57cec5SDimitry Andric 0xafa20000, // 0x04: sw $v0,0($sp)
5230b57cec5SDimitry Andric 0xafa30004, // 0x08: sw $v1,4($sp)
5240b57cec5SDimitry Andric 0xafa40008, // 0x0c: sw $a0,8($sp)
5250b57cec5SDimitry Andric 0xafa5000c, // 0x10: sw $a1,12($sp)
5260b57cec5SDimitry Andric 0xafa60010, // 0x14: sw $a2,16($sp)
5270b57cec5SDimitry Andric 0xafa70014, // 0x18: sw $a3,20($sp)
5280b57cec5SDimitry Andric 0xafb00018, // 0x1c: sw $s0,24($sp)
5290b57cec5SDimitry Andric 0xafb1001c, // 0x20: sw $s1,28($sp)
5300b57cec5SDimitry Andric 0xafb20020, // 0x24: sw $s2,32($sp)
5310b57cec5SDimitry Andric 0xafb30024, // 0x28: sw $s3,36($sp)
5320b57cec5SDimitry Andric 0xafb40028, // 0x2c: sw $s4,40($sp)
5330b57cec5SDimitry Andric 0xafb5002c, // 0x30: sw $s5,44($sp)
5340b57cec5SDimitry Andric 0xafb60030, // 0x34: sw $s6,48($sp)
5350b57cec5SDimitry Andric 0xafb70034, // 0x38: sw $s7,52($sp)
5360b57cec5SDimitry Andric 0xafa80038, // 0x3c: sw $t0,56($sp)
5370b57cec5SDimitry Andric 0xafa9003c, // 0x40: sw $t1,60($sp)
5380b57cec5SDimitry Andric 0xafaa0040, // 0x44: sw $t2,64($sp)
5390b57cec5SDimitry Andric 0xafab0044, // 0x48: sw $t3,68($sp)
5400b57cec5SDimitry Andric 0xafac0048, // 0x4c: sw $t4,72($sp)
5410b57cec5SDimitry Andric 0xafad004c, // 0x50: sw $t5,76($sp)
5420b57cec5SDimitry Andric 0xafae0050, // 0x54: sw $t6,80($sp)
5430b57cec5SDimitry Andric 0xafaf0054, // 0x58: sw $t7,84($sp)
5440b57cec5SDimitry Andric 0xafb80058, // 0x5c: sw $t8,88($sp)
5450b57cec5SDimitry Andric 0xafb9005c, // 0x60: sw $t9,92($sp)
5460b57cec5SDimitry Andric 0xafbe0060, // 0x64: sw $fp,96($sp)
5470b57cec5SDimitry Andric 0xafbf0064, // 0x68: sw $ra,100($sp)
5480b57cec5SDimitry Andric
5495ffd83dbSDimitry Andric // JIT re-entry ctx addr.
5505ffd83dbSDimitry Andric 0x00000000, // 0x6c: lui $a0,ctx
5515ffd83dbSDimitry Andric 0x00000000, // 0x70: addiu $a0,$a0,ctx
5520b57cec5SDimitry Andric
5530b57cec5SDimitry Andric 0x03e02825, // 0x74: move $a1, $ra
5540b57cec5SDimitry Andric 0x24a5ffec, // 0x78: addiu $a1,$a1,-20
5550b57cec5SDimitry Andric
5560b57cec5SDimitry Andric // JIT re-entry fn addr:
5570b57cec5SDimitry Andric 0x00000000, // 0x7c: lui $t9,reentry
5580b57cec5SDimitry Andric 0x00000000, // 0x80: addiu $t9,$t9,reentry
5590b57cec5SDimitry Andric
5600b57cec5SDimitry Andric 0x0320f809, // 0x84: jalr $t9
5610b57cec5SDimitry Andric 0x00000000, // 0x88: nop
5620b57cec5SDimitry Andric 0x8fbf0064, // 0x8c: lw $ra,100($sp)
5630b57cec5SDimitry Andric 0x8fbe0060, // 0x90: lw $fp,96($sp)
5640b57cec5SDimitry Andric 0x8fb9005c, // 0x94: lw $t9,92($sp)
5650b57cec5SDimitry Andric 0x8fb80058, // 0x98: lw $t8,88($sp)
5660b57cec5SDimitry Andric 0x8faf0054, // 0x9c: lw $t7,84($sp)
5670b57cec5SDimitry Andric 0x8fae0050, // 0xa0: lw $t6,80($sp)
5680b57cec5SDimitry Andric 0x8fad004c, // 0xa4: lw $t5,76($sp)
5690b57cec5SDimitry Andric 0x8fac0048, // 0xa8: lw $t4,72($sp)
5700b57cec5SDimitry Andric 0x8fab0044, // 0xac: lw $t3,68($sp)
5710b57cec5SDimitry Andric 0x8faa0040, // 0xb0: lw $t2,64($sp)
5720b57cec5SDimitry Andric 0x8fa9003c, // 0xb4: lw $t1,60($sp)
5730b57cec5SDimitry Andric 0x8fa80038, // 0xb8: lw $t0,56($sp)
5740b57cec5SDimitry Andric 0x8fb70034, // 0xbc: lw $s7,52($sp)
5750b57cec5SDimitry Andric 0x8fb60030, // 0xc0: lw $s6,48($sp)
5760b57cec5SDimitry Andric 0x8fb5002c, // 0xc4: lw $s5,44($sp)
5770b57cec5SDimitry Andric 0x8fb40028, // 0xc8: lw $s4,40($sp)
5780b57cec5SDimitry Andric 0x8fb30024, // 0xcc: lw $s3,36($sp)
5790b57cec5SDimitry Andric 0x8fb20020, // 0xd0: lw $s2,32($sp)
5800b57cec5SDimitry Andric 0x8fb1001c, // 0xd4: lw $s1,28($sp)
5810b57cec5SDimitry Andric 0x8fb00018, // 0xd8: lw $s0,24($sp)
5820b57cec5SDimitry Andric 0x8fa70014, // 0xdc: lw $a3,20($sp)
5830b57cec5SDimitry Andric 0x8fa60010, // 0xe0: lw $a2,16($sp)
5840b57cec5SDimitry Andric 0x8fa5000c, // 0xe4: lw $a1,12($sp)
5850b57cec5SDimitry Andric 0x8fa40008, // 0xe8: lw $a0,8($sp)
5860b57cec5SDimitry Andric 0x27bd0068, // 0xec: addiu $sp,$sp,104
5870b57cec5SDimitry Andric 0x0300f825, // 0xf0: move $ra, $t8
5880b57cec5SDimitry Andric 0x03200008, // 0xf4: jr $t9
5890b57cec5SDimitry Andric 0x00000000, // 0xf8: move $t9, $v0/v1
5900b57cec5SDimitry Andric };
5910b57cec5SDimitry Andric
5920b57cec5SDimitry Andric const unsigned ReentryFnAddrOffset = 0x7c; // JIT re-entry fn addr lui
5935ffd83dbSDimitry Andric const unsigned ReentryCtxAddrOffset = 0x6c; // JIT re-entry context addr lui
5940b57cec5SDimitry Andric const unsigned Offsett = 0xf8;
5950b57cec5SDimitry Andric
5965ffd83dbSDimitry Andric memcpy(ResolverWorkingMem, ResolverCode, sizeof(ResolverCode));
5970b57cec5SDimitry Andric
5980b57cec5SDimitry Andric // Depending on endian return value will be in v0 or v1.
5990b57cec5SDimitry Andric uint32_t MoveVxT9 = isBigEndian ? 0x0060c825 : 0x0040c825;
6005ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + Offsett, &MoveVxT9, sizeof(MoveVxT9));
6010b57cec5SDimitry Andric
6025ffd83dbSDimitry Andric uint32_t ReentryCtxLUi =
603*06c3fb27SDimitry Andric 0x3c040000 | (((ReentryCtxAddr.getValue() + 0x8000) >> 16) & 0xFFFF);
604*06c3fb27SDimitry Andric uint32_t ReentryCtxADDiu = 0x24840000 | (ReentryCtxAddr.getValue() & 0xFFFF);
6055ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + ReentryCtxAddrOffset, &ReentryCtxLUi,
6065ffd83dbSDimitry Andric sizeof(ReentryCtxLUi));
6075ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + ReentryCtxAddrOffset + 4, &ReentryCtxADDiu,
6085ffd83dbSDimitry Andric sizeof(ReentryCtxADDiu));
6090b57cec5SDimitry Andric
6105ffd83dbSDimitry Andric uint32_t ReentryFnLUi =
611*06c3fb27SDimitry Andric 0x3c190000 | (((ReentryFnAddr.getValue() + 0x8000) >> 16) & 0xFFFF);
612*06c3fb27SDimitry Andric uint32_t ReentryFnADDiu = 0x27390000 | (ReentryFnAddr.getValue() & 0xFFFF);
6135ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + ReentryFnAddrOffset, &ReentryFnLUi,
6145ffd83dbSDimitry Andric sizeof(ReentryFnLUi));
6155ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + ReentryFnAddrOffset + 4, &ReentryFnADDiu,
6165ffd83dbSDimitry Andric sizeof(ReentryFnADDiu));
6170b57cec5SDimitry Andric }
6180b57cec5SDimitry Andric
writeTrampolines(char * TrampolineBlockWorkingMem,ExecutorAddr TrampolineBlockTargetAddress,ExecutorAddr ResolverAddr,unsigned NumTrampolines)619*06c3fb27SDimitry Andric void OrcMips32_Base::writeTrampolines(char *TrampolineBlockWorkingMem,
620*06c3fb27SDimitry Andric ExecutorAddr TrampolineBlockTargetAddress,
621*06c3fb27SDimitry Andric ExecutorAddr ResolverAddr,
622*06c3fb27SDimitry Andric unsigned NumTrampolines) {
6230b57cec5SDimitry Andric
624*06c3fb27SDimitry Andric assert((ResolverAddr.getValue() >> 32) == 0 && "ResolverAddr out of range");
6255ffd83dbSDimitry Andric
6265ffd83dbSDimitry Andric uint32_t *Trampolines =
6275ffd83dbSDimitry Andric reinterpret_cast<uint32_t *>(TrampolineBlockWorkingMem);
628*06c3fb27SDimitry Andric uint32_t RHiAddr = ((ResolverAddr.getValue() + 0x8000) >> 16);
6290b57cec5SDimitry Andric
6300b57cec5SDimitry Andric for (unsigned I = 0; I < NumTrampolines; ++I) {
6315ffd83dbSDimitry Andric // move $t8,$ra
6325ffd83dbSDimitry Andric // lui $t9,ResolverAddr
6335ffd83dbSDimitry Andric // addiu $t9,$t9,ResolverAddr
6345ffd83dbSDimitry Andric // jalr $t9
6355ffd83dbSDimitry Andric // nop
6365ffd83dbSDimitry Andric Trampolines[5 * I + 0] = 0x03e0c025;
6375ffd83dbSDimitry Andric Trampolines[5 * I + 1] = 0x3c190000 | (RHiAddr & 0xFFFF);
638*06c3fb27SDimitry Andric Trampolines[5 * I + 2] = 0x27390000 | (ResolverAddr.getValue() & 0xFFFF);
6395ffd83dbSDimitry Andric Trampolines[5 * I + 3] = 0x0320f809;
6405ffd83dbSDimitry Andric Trampolines[5 * I + 4] = 0x00000000;
6410b57cec5SDimitry Andric }
6420b57cec5SDimitry Andric }
6430b57cec5SDimitry Andric
writeIndirectStubsBlock(char * StubsBlockWorkingMem,ExecutorAddr StubsBlockTargetAddress,ExecutorAddr PointersBlockTargetAddress,unsigned NumStubs)6445ffd83dbSDimitry Andric void OrcMips32_Base::writeIndirectStubsBlock(
645*06c3fb27SDimitry Andric char *StubsBlockWorkingMem, ExecutorAddr StubsBlockTargetAddress,
646*06c3fb27SDimitry Andric ExecutorAddr PointersBlockTargetAddress, unsigned NumStubs) {
647*06c3fb27SDimitry Andric assert((StubsBlockTargetAddress.getValue() >> 32) == 0 &&
6485ffd83dbSDimitry Andric "InitialPtrVal is out of range");
6495ffd83dbSDimitry Andric
6500b57cec5SDimitry Andric // Stub format is:
6510b57cec5SDimitry Andric //
6520b57cec5SDimitry Andric // .section __orc_stubs
6530b57cec5SDimitry Andric // stub1:
6540b57cec5SDimitry Andric // lui $t9, ptr1
6550b57cec5SDimitry Andric // lw $t9, %lo(ptr1)($t9)
6560b57cec5SDimitry Andric // jr $t9
6570b57cec5SDimitry Andric // stub2:
6580b57cec5SDimitry Andric // lui $t9, ptr2
6590b57cec5SDimitry Andric // lw $t9,%lo(ptr1)($t9)
6600b57cec5SDimitry Andric // jr $t9
6610b57cec5SDimitry Andric //
6620b57cec5SDimitry Andric // ...
6630b57cec5SDimitry Andric //
6640b57cec5SDimitry Andric // .section __orc_ptrs
6650b57cec5SDimitry Andric // ptr1:
6660b57cec5SDimitry Andric // .word 0x0
6670b57cec5SDimitry Andric // ptr2:
6680b57cec5SDimitry Andric // .word 0x0
6690b57cec5SDimitry Andric //
6705ffd83dbSDimitry Andric // i..
6710b57cec5SDimitry Andric
672753f127fSDimitry Andric assert(stubAndPointerRangesOk<OrcMips32_Base>(
6735ffd83dbSDimitry Andric StubsBlockTargetAddress, PointersBlockTargetAddress, NumStubs) &&
6745ffd83dbSDimitry Andric "PointersBlock is out of range");
6750b57cec5SDimitry Andric
6760b57cec5SDimitry Andric // Populate the stubs page stubs and mark it executable.
6775ffd83dbSDimitry Andric uint32_t *Stub = reinterpret_cast<uint32_t *>(StubsBlockWorkingMem);
678*06c3fb27SDimitry Andric uint64_t PtrAddr = PointersBlockTargetAddress.getValue();
6790b57cec5SDimitry Andric
6800b57cec5SDimitry Andric for (unsigned I = 0; I < NumStubs; ++I) {
6810b57cec5SDimitry Andric uint32_t HiAddr = ((PtrAddr + 0x8000) >> 16);
6820b57cec5SDimitry Andric Stub[4 * I + 0] = 0x3c190000 | (HiAddr & 0xFFFF); // lui $t9,ptr1
6830b57cec5SDimitry Andric Stub[4 * I + 1] = 0x8f390000 | (PtrAddr & 0xFFFF); // lw $t9,%lo(ptr1)($t9)
6840b57cec5SDimitry Andric Stub[4 * I + 2] = 0x03200008; // jr $t9
6850b57cec5SDimitry Andric Stub[4 * I + 3] = 0x00000000; // nop
6860b57cec5SDimitry Andric PtrAddr += 4;
6870b57cec5SDimitry Andric }
6880b57cec5SDimitry Andric }
6890b57cec5SDimitry Andric
writeResolverCode(char * ResolverWorkingMem,ExecutorAddr ResolverTargetAddress,ExecutorAddr ReentryFnAddr,ExecutorAddr ReentryCtxAddr)6905ffd83dbSDimitry Andric void OrcMips64::writeResolverCode(char *ResolverWorkingMem,
691*06c3fb27SDimitry Andric ExecutorAddr ResolverTargetAddress,
692*06c3fb27SDimitry Andric ExecutorAddr ReentryFnAddr,
693*06c3fb27SDimitry Andric ExecutorAddr ReentryCtxAddr) {
6940b57cec5SDimitry Andric
6950b57cec5SDimitry Andric const uint32_t ResolverCode[] = {
6960b57cec5SDimitry Andric //resolver_entry:
6970b57cec5SDimitry Andric 0x67bdff30, // 0x00: daddiu $sp,$sp,-208
6980b57cec5SDimitry Andric 0xffa20000, // 0x04: sd v0,0(sp)
6990b57cec5SDimitry Andric 0xffa30008, // 0x08: sd v1,8(sp)
7000b57cec5SDimitry Andric 0xffa40010, // 0x0c: sd a0,16(sp)
7010b57cec5SDimitry Andric 0xffa50018, // 0x10: sd a1,24(sp)
7020b57cec5SDimitry Andric 0xffa60020, // 0x14: sd a2,32(sp)
7030b57cec5SDimitry Andric 0xffa70028, // 0x18: sd a3,40(sp)
7040b57cec5SDimitry Andric 0xffa80030, // 0x1c: sd a4,48(sp)
7050b57cec5SDimitry Andric 0xffa90038, // 0x20: sd a5,56(sp)
7060b57cec5SDimitry Andric 0xffaa0040, // 0x24: sd a6,64(sp)
7070b57cec5SDimitry Andric 0xffab0048, // 0x28: sd a7,72(sp)
7080b57cec5SDimitry Andric 0xffac0050, // 0x2c: sd t0,80(sp)
7090b57cec5SDimitry Andric 0xffad0058, // 0x30: sd t1,88(sp)
7100b57cec5SDimitry Andric 0xffae0060, // 0x34: sd t2,96(sp)
7110b57cec5SDimitry Andric 0xffaf0068, // 0x38: sd t3,104(sp)
7120b57cec5SDimitry Andric 0xffb00070, // 0x3c: sd s0,112(sp)
7130b57cec5SDimitry Andric 0xffb10078, // 0x40: sd s1,120(sp)
7140b57cec5SDimitry Andric 0xffb20080, // 0x44: sd s2,128(sp)
7150b57cec5SDimitry Andric 0xffb30088, // 0x48: sd s3,136(sp)
7160b57cec5SDimitry Andric 0xffb40090, // 0x4c: sd s4,144(sp)
7170b57cec5SDimitry Andric 0xffb50098, // 0x50: sd s5,152(sp)
7180b57cec5SDimitry Andric 0xffb600a0, // 0x54: sd s6,160(sp)
7190b57cec5SDimitry Andric 0xffb700a8, // 0x58: sd s7,168(sp)
7200b57cec5SDimitry Andric 0xffb800b0, // 0x5c: sd t8,176(sp)
7210b57cec5SDimitry Andric 0xffb900b8, // 0x60: sd t9,184(sp)
7220b57cec5SDimitry Andric 0xffbe00c0, // 0x64: sd fp,192(sp)
7230b57cec5SDimitry Andric 0xffbf00c8, // 0x68: sd ra,200(sp)
7240b57cec5SDimitry Andric
7255ffd83dbSDimitry Andric // JIT re-entry ctx addr.
7265ffd83dbSDimitry Andric 0x00000000, // 0x6c: lui $a0,heighest(ctx)
7275ffd83dbSDimitry Andric 0x00000000, // 0x70: daddiu $a0,$a0,heigher(ctx)
7280b57cec5SDimitry Andric 0x00000000, // 0x74: dsll $a0,$a0,16
7295ffd83dbSDimitry Andric 0x00000000, // 0x78: daddiu $a0,$a0,hi(ctx)
7300b57cec5SDimitry Andric 0x00000000, // 0x7c: dsll $a0,$a0,16
7315ffd83dbSDimitry Andric 0x00000000, // 0x80: daddiu $a0,$a0,lo(ctx)
7320b57cec5SDimitry Andric
7330b57cec5SDimitry Andric 0x03e02825, // 0x84: move $a1, $ra
7340b57cec5SDimitry Andric 0x64a5ffdc, // 0x88: daddiu $a1,$a1,-36
7350b57cec5SDimitry Andric
7360b57cec5SDimitry Andric // JIT re-entry fn addr:
7370b57cec5SDimitry Andric 0x00000000, // 0x8c: lui $t9,reentry
7380b57cec5SDimitry Andric 0x00000000, // 0x90: daddiu $t9,$t9,reentry
7390b57cec5SDimitry Andric 0x00000000, // 0x94: dsll $t9,$t9,
7400b57cec5SDimitry Andric 0x00000000, // 0x98: daddiu $t9,$t9,
7410b57cec5SDimitry Andric 0x00000000, // 0x9c: dsll $t9,$t9,
7420b57cec5SDimitry Andric 0x00000000, // 0xa0: daddiu $t9,$t9,
7430b57cec5SDimitry Andric 0x0320f809, // 0xa4: jalr $t9
7440b57cec5SDimitry Andric 0x00000000, // 0xa8: nop
7450b57cec5SDimitry Andric 0xdfbf00c8, // 0xac: ld ra, 200(sp)
7460b57cec5SDimitry Andric 0xdfbe00c0, // 0xb0: ld fp, 192(sp)
7470b57cec5SDimitry Andric 0xdfb900b8, // 0xb4: ld t9, 184(sp)
7480b57cec5SDimitry Andric 0xdfb800b0, // 0xb8: ld t8, 176(sp)
7490b57cec5SDimitry Andric 0xdfb700a8, // 0xbc: ld s7, 168(sp)
7500b57cec5SDimitry Andric 0xdfb600a0, // 0xc0: ld s6, 160(sp)
7510b57cec5SDimitry Andric 0xdfb50098, // 0xc4: ld s5, 152(sp)
7520b57cec5SDimitry Andric 0xdfb40090, // 0xc8: ld s4, 144(sp)
7530b57cec5SDimitry Andric 0xdfb30088, // 0xcc: ld s3, 136(sp)
7540b57cec5SDimitry Andric 0xdfb20080, // 0xd0: ld s2, 128(sp)
7550b57cec5SDimitry Andric 0xdfb10078, // 0xd4: ld s1, 120(sp)
7560b57cec5SDimitry Andric 0xdfb00070, // 0xd8: ld s0, 112(sp)
7570b57cec5SDimitry Andric 0xdfaf0068, // 0xdc: ld t3, 104(sp)
7580b57cec5SDimitry Andric 0xdfae0060, // 0xe0: ld t2, 96(sp)
7590b57cec5SDimitry Andric 0xdfad0058, // 0xe4: ld t1, 88(sp)
7600b57cec5SDimitry Andric 0xdfac0050, // 0xe8: ld t0, 80(sp)
7610b57cec5SDimitry Andric 0xdfab0048, // 0xec: ld a7, 72(sp)
7620b57cec5SDimitry Andric 0xdfaa0040, // 0xf0: ld a6, 64(sp)
7630b57cec5SDimitry Andric 0xdfa90038, // 0xf4: ld a5, 56(sp)
7640b57cec5SDimitry Andric 0xdfa80030, // 0xf8: ld a4, 48(sp)
7650b57cec5SDimitry Andric 0xdfa70028, // 0xfc: ld a3, 40(sp)
7660b57cec5SDimitry Andric 0xdfa60020, // 0x100: ld a2, 32(sp)
7670b57cec5SDimitry Andric 0xdfa50018, // 0x104: ld a1, 24(sp)
7680b57cec5SDimitry Andric 0xdfa40010, // 0x108: ld a0, 16(sp)
7690b57cec5SDimitry Andric 0xdfa30008, // 0x10c: ld v1, 8(sp)
7700b57cec5SDimitry Andric 0x67bd00d0, // 0x110: daddiu $sp,$sp,208
7710b57cec5SDimitry Andric 0x0300f825, // 0x114: move $ra, $t8
7720b57cec5SDimitry Andric 0x03200008, // 0x118: jr $t9
7730b57cec5SDimitry Andric 0x0040c825, // 0x11c: move $t9, $v0
7740b57cec5SDimitry Andric };
7750b57cec5SDimitry Andric
7760b57cec5SDimitry Andric const unsigned ReentryFnAddrOffset = 0x8c; // JIT re-entry fn addr lui
7775ffd83dbSDimitry Andric const unsigned ReentryCtxAddrOffset = 0x6c; // JIT re-entry ctx addr lui
7780b57cec5SDimitry Andric
7795ffd83dbSDimitry Andric memcpy(ResolverWorkingMem, ResolverCode, sizeof(ResolverCode));
7800b57cec5SDimitry Andric
7815ffd83dbSDimitry Andric uint32_t ReentryCtxLUi =
782*06c3fb27SDimitry Andric 0x3c040000 |
783*06c3fb27SDimitry Andric (((ReentryCtxAddr.getValue() + 0x800080008000) >> 48) & 0xFFFF);
7845ffd83dbSDimitry Andric uint32_t ReentryCtxDADDiu =
785*06c3fb27SDimitry Andric 0x64840000 | (((ReentryCtxAddr.getValue() + 0x80008000) >> 32) & 0xFFFF);
7865ffd83dbSDimitry Andric uint32_t ReentryCtxDSLL = 0x00042438;
7875ffd83dbSDimitry Andric uint32_t ReentryCtxDADDiu2 =
788*06c3fb27SDimitry Andric 0x64840000 | ((((ReentryCtxAddr.getValue() + 0x8000) >> 16) & 0xFFFF));
7895ffd83dbSDimitry Andric uint32_t ReentryCtxDSLL2 = 0x00042438;
790*06c3fb27SDimitry Andric uint32_t ReentryCtxDADDiu3 =
791*06c3fb27SDimitry Andric 0x64840000 | (ReentryCtxAddr.getValue() & 0xFFFF);
7920b57cec5SDimitry Andric
7935ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + ReentryCtxAddrOffset, &ReentryCtxLUi,
7945ffd83dbSDimitry Andric sizeof(ReentryCtxLUi));
7955ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + (ReentryCtxAddrOffset + 4), &ReentryCtxDADDiu,
7965ffd83dbSDimitry Andric sizeof(ReentryCtxDADDiu));
7975ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + (ReentryCtxAddrOffset + 8), &ReentryCtxDSLL,
7985ffd83dbSDimitry Andric sizeof(ReentryCtxDSLL));
7995ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + (ReentryCtxAddrOffset + 12), &ReentryCtxDADDiu2,
8005ffd83dbSDimitry Andric sizeof(ReentryCtxDADDiu2));
8015ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + (ReentryCtxAddrOffset + 16), &ReentryCtxDSLL2,
8025ffd83dbSDimitry Andric sizeof(ReentryCtxDSLL2));
8035ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + (ReentryCtxAddrOffset + 20), &ReentryCtxDADDiu3,
8045ffd83dbSDimitry Andric sizeof(ReentryCtxDADDiu3));
8050b57cec5SDimitry Andric
8065ffd83dbSDimitry Andric uint32_t ReentryFnLUi =
807*06c3fb27SDimitry Andric 0x3c190000 |
808*06c3fb27SDimitry Andric (((ReentryFnAddr.getValue() + 0x800080008000) >> 48) & 0xFFFF);
8090b57cec5SDimitry Andric
8105ffd83dbSDimitry Andric uint32_t ReentryFnDADDiu =
811*06c3fb27SDimitry Andric 0x67390000 | (((ReentryFnAddr.getValue() + 0x80008000) >> 32) & 0xFFFF);
8120b57cec5SDimitry Andric
8135ffd83dbSDimitry Andric uint32_t ReentryFnDSLL = 0x0019cc38;
8140b57cec5SDimitry Andric
8155ffd83dbSDimitry Andric uint32_t ReentryFnDADDiu2 =
816*06c3fb27SDimitry Andric 0x67390000 | (((ReentryFnAddr.getValue() + 0x8000) >> 16) & 0xFFFF);
8170b57cec5SDimitry Andric
8185ffd83dbSDimitry Andric uint32_t ReentryFnDSLL2 = 0x0019cc38;
8190b57cec5SDimitry Andric
820*06c3fb27SDimitry Andric uint32_t ReentryFnDADDiu3 = 0x67390000 | (ReentryFnAddr.getValue() & 0xFFFF);
8210b57cec5SDimitry Andric
8225ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + ReentryFnAddrOffset, &ReentryFnLUi,
8235ffd83dbSDimitry Andric sizeof(ReentryFnLUi));
8245ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + (ReentryFnAddrOffset + 4), &ReentryFnDADDiu,
8255ffd83dbSDimitry Andric sizeof(ReentryFnDADDiu));
8265ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + (ReentryFnAddrOffset + 8), &ReentryFnDSLL,
8275ffd83dbSDimitry Andric sizeof(ReentryFnDSLL));
8285ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + (ReentryFnAddrOffset + 12), &ReentryFnDADDiu2,
8295ffd83dbSDimitry Andric sizeof(ReentryFnDADDiu2));
8305ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + (ReentryFnAddrOffset + 16), &ReentryFnDSLL2,
8315ffd83dbSDimitry Andric sizeof(ReentryFnDSLL2));
8325ffd83dbSDimitry Andric memcpy(ResolverWorkingMem + (ReentryFnAddrOffset + 20), &ReentryFnDADDiu3,
8335ffd83dbSDimitry Andric sizeof(ReentryFnDADDiu3));
8340b57cec5SDimitry Andric }
8350b57cec5SDimitry Andric
writeTrampolines(char * TrampolineBlockWorkingMem,ExecutorAddr TrampolineBlockTargetAddress,ExecutorAddr ResolverAddr,unsigned NumTrampolines)8365ffd83dbSDimitry Andric void OrcMips64::writeTrampolines(char *TrampolineBlockWorkingMem,
837*06c3fb27SDimitry Andric ExecutorAddr TrampolineBlockTargetAddress,
838*06c3fb27SDimitry Andric ExecutorAddr ResolverAddr,
8390b57cec5SDimitry Andric unsigned NumTrampolines) {
8400b57cec5SDimitry Andric
8415ffd83dbSDimitry Andric uint32_t *Trampolines =
8425ffd83dbSDimitry Andric reinterpret_cast<uint32_t *>(TrampolineBlockWorkingMem);
8430b57cec5SDimitry Andric
844*06c3fb27SDimitry Andric uint64_t HeighestAddr = ((ResolverAddr.getValue() + 0x800080008000) >> 48);
845*06c3fb27SDimitry Andric uint64_t HeigherAddr = ((ResolverAddr.getValue() + 0x80008000) >> 32);
846*06c3fb27SDimitry Andric uint64_t HiAddr = ((ResolverAddr.getValue() + 0x8000) >> 16);
8470b57cec5SDimitry Andric
8480b57cec5SDimitry Andric for (unsigned I = 0; I < NumTrampolines; ++I) {
8490b57cec5SDimitry Andric Trampolines[10 * I + 0] = 0x03e0c025; // move $t8,$ra
8500b57cec5SDimitry Andric Trampolines[10 * I + 1] = 0x3c190000 | (HeighestAddr & 0xFFFF); // lui $t9,resolveAddr
8510b57cec5SDimitry Andric Trampolines[10 * I + 2] = 0x67390000 | (HeigherAddr & 0xFFFF); // daddiu $t9,$t9,%higher(resolveAddr)
8520b57cec5SDimitry Andric Trampolines[10 * I + 3] = 0x0019cc38; // dsll $t9,$t9,16
8530b57cec5SDimitry Andric Trampolines[10 * I + 4] = 0x67390000 | (HiAddr & 0xFFFF); // daddiu $t9,$t9,%hi(ptr)
8540b57cec5SDimitry Andric Trampolines[10 * I + 5] = 0x0019cc38; // dsll $t9,$t9,16
855*06c3fb27SDimitry Andric Trampolines[10 * I + 6] = 0x67390000 | (ResolverAddr.getValue() &
856*06c3fb27SDimitry Andric 0xFFFF); // daddiu $t9,$t9,%lo(ptr)
8570b57cec5SDimitry Andric Trampolines[10 * I + 7] = 0x0320f809; // jalr $t9
8580b57cec5SDimitry Andric Trampolines[10 * I + 8] = 0x00000000; // nop
8590b57cec5SDimitry Andric Trampolines[10 * I + 9] = 0x00000000; // nop
8600b57cec5SDimitry Andric }
8610b57cec5SDimitry Andric }
8620b57cec5SDimitry Andric
writeIndirectStubsBlock(char * StubsBlockWorkingMem,ExecutorAddr StubsBlockTargetAddress,ExecutorAddr PointersBlockTargetAddress,unsigned NumStubs)863*06c3fb27SDimitry Andric void OrcMips64::writeIndirectStubsBlock(char *StubsBlockWorkingMem,
864*06c3fb27SDimitry Andric ExecutorAddr StubsBlockTargetAddress,
865*06c3fb27SDimitry Andric ExecutorAddr PointersBlockTargetAddress,
866*06c3fb27SDimitry Andric unsigned NumStubs) {
8670b57cec5SDimitry Andric // Stub format is:
8680b57cec5SDimitry Andric //
8690b57cec5SDimitry Andric // .section __orc_stubs
8700b57cec5SDimitry Andric // stub1:
8710b57cec5SDimitry Andric // lui $t9,ptr1
8720b57cec5SDimitry Andric // dsll $t9,$t9,16
8730b57cec5SDimitry Andric // daddiu $t9,$t9,%hi(ptr)
8740b57cec5SDimitry Andric // dsll $t9,$t9,16
8750b57cec5SDimitry Andric // ld $t9,%lo(ptr)
8760b57cec5SDimitry Andric // jr $t9
8770b57cec5SDimitry Andric // stub2:
8780b57cec5SDimitry Andric // lui $t9,ptr1
8790b57cec5SDimitry Andric // dsll $t9,$t9,16
8800b57cec5SDimitry Andric // daddiu $t9,$t9,%hi(ptr)
8810b57cec5SDimitry Andric // dsll $t9,$t9,16
8820b57cec5SDimitry Andric // ld $t9,%lo(ptr)
8830b57cec5SDimitry Andric // jr $t9
8840b57cec5SDimitry Andric //
8850b57cec5SDimitry Andric // ...
8860b57cec5SDimitry Andric //
8870b57cec5SDimitry Andric // .section __orc_ptrs
8880b57cec5SDimitry Andric // ptr1:
8890b57cec5SDimitry Andric // .dword 0x0
8900b57cec5SDimitry Andric // ptr2:
8910b57cec5SDimitry Andric // .dword 0x0
8920b57cec5SDimitry Andric //
8930b57cec5SDimitry Andric // ...
8940b57cec5SDimitry Andric
895753f127fSDimitry Andric assert(stubAndPointerRangesOk<OrcMips64>(
8965ffd83dbSDimitry Andric StubsBlockTargetAddress, PointersBlockTargetAddress, NumStubs) &&
8975ffd83dbSDimitry Andric "PointersBlock is out of range");
8980b57cec5SDimitry Andric
8990b57cec5SDimitry Andric // Populate the stubs page stubs and mark it executable.
9005ffd83dbSDimitry Andric uint32_t *Stub = reinterpret_cast<uint32_t *>(StubsBlockWorkingMem);
901*06c3fb27SDimitry Andric uint64_t PtrAddr = PointersBlockTargetAddress.getValue();
9020b57cec5SDimitry Andric
9030b57cec5SDimitry Andric for (unsigned I = 0; I < NumStubs; ++I, PtrAddr += 8) {
9040b57cec5SDimitry Andric uint64_t HeighestAddr = ((PtrAddr + 0x800080008000) >> 48);
9050b57cec5SDimitry Andric uint64_t HeigherAddr = ((PtrAddr + 0x80008000) >> 32);
9060b57cec5SDimitry Andric uint64_t HiAddr = ((PtrAddr + 0x8000) >> 16);
9070b57cec5SDimitry Andric Stub[8 * I + 0] = 0x3c190000 | (HeighestAddr & 0xFFFF); // lui $t9,ptr1
9080b57cec5SDimitry Andric Stub[8 * I + 1] = 0x67390000 | (HeigherAddr & 0xFFFF); // daddiu $t9,$t9,%higher(ptr)
9090b57cec5SDimitry Andric Stub[8 * I + 2] = 0x0019cc38; // dsll $t9,$t9,16
9100b57cec5SDimitry Andric Stub[8 * I + 3] = 0x67390000 | (HiAddr & 0xFFFF); // daddiu $t9,$t9,%hi(ptr)
9110b57cec5SDimitry Andric Stub[8 * I + 4] = 0x0019cc38; // dsll $t9,$t9,16
9120b57cec5SDimitry Andric Stub[8 * I + 5] = 0xdf390000 | (PtrAddr & 0xFFFF); // ld $t9,%lo(ptr)
9130b57cec5SDimitry Andric Stub[8 * I + 6] = 0x03200008; // jr $t9
9140b57cec5SDimitry Andric Stub[8 * I + 7] = 0x00000000; // nop
9150b57cec5SDimitry Andric }
9160b57cec5SDimitry Andric }
91781ad6265SDimitry Andric
writeResolverCode(char * ResolverWorkingMem,ExecutorAddr ResolverTargetAddress,ExecutorAddr ReentryFnAddr,ExecutorAddr ReentryCtxAddr)91881ad6265SDimitry Andric void OrcRiscv64::writeResolverCode(char *ResolverWorkingMem,
919*06c3fb27SDimitry Andric ExecutorAddr ResolverTargetAddress,
920*06c3fb27SDimitry Andric ExecutorAddr ReentryFnAddr,
921*06c3fb27SDimitry Andric ExecutorAddr ReentryCtxAddr) {
92281ad6265SDimitry Andric
92381ad6265SDimitry Andric const uint32_t ResolverCode[] = {
92481ad6265SDimitry Andric 0xef810113, // 0x00: addi sp,sp,-264
92581ad6265SDimitry Andric 0x00813023, // 0x04: sd s0,0(sp)
92681ad6265SDimitry Andric 0x00913423, // 0x08: sd s1,8(sp)
92781ad6265SDimitry Andric 0x01213823, // 0x0c: sd s2,16(sp)
92881ad6265SDimitry Andric 0x01313c23, // 0x10: sd s3,24(sp)
92981ad6265SDimitry Andric 0x03413023, // 0x14: sd s4,32(sp)
93081ad6265SDimitry Andric 0x03513423, // 0x18: sd s5,40(sp)
93181ad6265SDimitry Andric 0x03613823, // 0x1c: sd s6,48(sp)
93281ad6265SDimitry Andric 0x03713c23, // 0x20: sd s7,56(sp)
93381ad6265SDimitry Andric 0x05813023, // 0x24: sd s8,64(sp)
93481ad6265SDimitry Andric 0x05913423, // 0x28: sd s9,72(sp)
93581ad6265SDimitry Andric 0x05a13823, // 0x2c: sd s10,80(sp)
93681ad6265SDimitry Andric 0x05b13c23, // 0x30: sd s11,88(sp)
93781ad6265SDimitry Andric 0x06113023, // 0x34: sd ra,96(sp)
93881ad6265SDimitry Andric 0x06a13423, // 0x38: sd a0,104(sp)
93981ad6265SDimitry Andric 0x06b13823, // 0x3c: sd a1,112(sp)
94081ad6265SDimitry Andric 0x06c13c23, // 0x40: sd a2,120(sp)
94181ad6265SDimitry Andric 0x08d13023, // 0x44: sd a3,128(sp)
94281ad6265SDimitry Andric 0x08e13423, // 0x48: sd a4,136(sp)
94381ad6265SDimitry Andric 0x08f13823, // 0x4c: sd a5,144(sp)
94481ad6265SDimitry Andric 0x09013c23, // 0x50: sd a6,152(sp)
94581ad6265SDimitry Andric 0x0b113023, // 0x54: sd a7,160(sp)
94681ad6265SDimitry Andric 0x0a813427, // 0x58: fsd fs0,168(sp)
94781ad6265SDimitry Andric 0x0a913827, // 0x5c: fsd fs1,176(sp)
94881ad6265SDimitry Andric 0x0b213c27, // 0x60: fsd fs2,184(sp)
94981ad6265SDimitry Andric 0x0d313027, // 0x64: fsd fs3,192(sp)
95081ad6265SDimitry Andric 0x0d413427, // 0x68: fsd fs4,200(sp)
95181ad6265SDimitry Andric 0x0d513827, // 0x6c: fsd fs5,208(sp)
95281ad6265SDimitry Andric 0x0d613c27, // 0x70: fsd fs6,216(sp)
95381ad6265SDimitry Andric 0x0f713027, // 0x74: fsd fs7,224(sp)
95481ad6265SDimitry Andric 0x0f813427, // 0x78: fsd fs8,232(sp)
95581ad6265SDimitry Andric 0x0f913827, // 0x7c: fsd fs9,240(sp)
95681ad6265SDimitry Andric 0x0fa13c27, // 0x80: fsd fs10,248(sp)
95781ad6265SDimitry Andric 0x11b13027, // 0x84: fsd fs11,256(sp)
95881ad6265SDimitry Andric 0x00000517, // 0x88: auipc a0,0x0
95981ad6265SDimitry Andric 0x0b053503, // 0x8c: ld a0,176(a0) # 0x138
96081ad6265SDimitry Andric 0x00030593, // 0x90: mv a1,t1
96181ad6265SDimitry Andric 0xff458593, // 0x94: addi a1,a1,-12
96281ad6265SDimitry Andric 0x00000617, // 0x98: auipc a2,0x0
96381ad6265SDimitry Andric 0x0a863603, // 0x9c: ld a2,168(a2) # 0x140
96481ad6265SDimitry Andric 0x000600e7, // 0xa0: jalr a2
96581ad6265SDimitry Andric 0x00050293, // 0xa4: mv t0,a0
96681ad6265SDimitry Andric 0x00013403, // 0xa8: ld s0,0(sp)
96781ad6265SDimitry Andric 0x00813483, // 0xac: ld s1,8(sp)
96881ad6265SDimitry Andric 0x01013903, // 0xb0: ld s2,16(sp)
96981ad6265SDimitry Andric 0x01813983, // 0xb4: ld s3,24(sp)
97081ad6265SDimitry Andric 0x02013a03, // 0xb8: ld s4,32(sp)
97181ad6265SDimitry Andric 0x02813a83, // 0xbc: ld s5,40(sp)
97281ad6265SDimitry Andric 0x03013b03, // 0xc0: ld s6,48(sp)
97381ad6265SDimitry Andric 0x03813b83, // 0xc4: ld s7,56(sp)
97481ad6265SDimitry Andric 0x04013c03, // 0xc8: ld s8,64(sp)
97581ad6265SDimitry Andric 0x04813c83, // 0xcc: ld s9,72(sp)
97681ad6265SDimitry Andric 0x05013d03, // 0xd0: ld s10,80(sp)
97781ad6265SDimitry Andric 0x05813d83, // 0xd4: ld s11,88(sp)
97881ad6265SDimitry Andric 0x06013083, // 0xd8: ld ra,96(sp)
97981ad6265SDimitry Andric 0x06813503, // 0xdc: ld a0,104(sp)
98081ad6265SDimitry Andric 0x07013583, // 0xe0: ld a1,112(sp)
98181ad6265SDimitry Andric 0x07813603, // 0xe4: ld a2,120(sp)
98281ad6265SDimitry Andric 0x08013683, // 0xe8: ld a3,128(sp)
98381ad6265SDimitry Andric 0x08813703, // 0xec: ld a4,136(sp)
98481ad6265SDimitry Andric 0x09013783, // 0xf0: ld a5,144(sp)
98581ad6265SDimitry Andric 0x09813803, // 0xf4: ld a6,152(sp)
98681ad6265SDimitry Andric 0x0a013883, // 0xf8: ld a7,160(sp)
98781ad6265SDimitry Andric 0x0a813407, // 0xfc: fld fs0,168(sp)
98881ad6265SDimitry Andric 0x0b013487, // 0x100: fld fs1,176(sp)
98981ad6265SDimitry Andric 0x0b813907, // 0x104: fld fs2,184(sp)
99081ad6265SDimitry Andric 0x0c013987, // 0x108: fld fs3,192(sp)
99181ad6265SDimitry Andric 0x0c813a07, // 0x10c: fld fs4,200(sp)
99281ad6265SDimitry Andric 0x0d013a87, // 0x110: fld fs5,208(sp)
99381ad6265SDimitry Andric 0x0d813b07, // 0x114: fld fs6,216(sp)
99481ad6265SDimitry Andric 0x0e013b87, // 0x118: fld fs7,224(sp)
99581ad6265SDimitry Andric 0x0e813c07, // 0x11c: fld fs8,232(sp)
99681ad6265SDimitry Andric 0x0f013c87, // 0x120: fld fs9,240(sp)
99781ad6265SDimitry Andric 0x0f813d07, // 0x124: fld fs10,248(sp)
99881ad6265SDimitry Andric 0x10013d87, // 0x128: fld fs11,256(sp)
99981ad6265SDimitry Andric 0x10810113, // 0x12c: addi sp,sp,264
100081ad6265SDimitry Andric 0x00028067, // 0x130: jr t0
100181ad6265SDimitry Andric 0x12345678, // 0x134: padding to align at 8 byte
100281ad6265SDimitry Andric 0x12345678, // 0x138: Lreentry_ctx_ptr:
100381ad6265SDimitry Andric 0xdeadbeef, // 0x13c: .quad 0
100481ad6265SDimitry Andric 0x98765432, // 0x140: Lreentry_fn_ptr:
100581ad6265SDimitry Andric 0xcafef00d // 0x144: .quad 0
100681ad6265SDimitry Andric };
100781ad6265SDimitry Andric
100881ad6265SDimitry Andric const unsigned ReentryCtxAddrOffset = 0x138;
100981ad6265SDimitry Andric const unsigned ReentryFnAddrOffset = 0x140;
101081ad6265SDimitry Andric
101181ad6265SDimitry Andric memcpy(ResolverWorkingMem, ResolverCode, sizeof(ResolverCode));
101281ad6265SDimitry Andric memcpy(ResolverWorkingMem + ReentryFnAddrOffset, &ReentryFnAddr,
101381ad6265SDimitry Andric sizeof(uint64_t));
101481ad6265SDimitry Andric memcpy(ResolverWorkingMem + ReentryCtxAddrOffset, &ReentryCtxAddr,
101581ad6265SDimitry Andric sizeof(uint64_t));
101681ad6265SDimitry Andric }
101781ad6265SDimitry Andric
writeTrampolines(char * TrampolineBlockWorkingMem,ExecutorAddr TrampolineBlockTargetAddress,ExecutorAddr ResolverAddr,unsigned NumTrampolines)101881ad6265SDimitry Andric void OrcRiscv64::writeTrampolines(char *TrampolineBlockWorkingMem,
1019*06c3fb27SDimitry Andric ExecutorAddr TrampolineBlockTargetAddress,
1020*06c3fb27SDimitry Andric ExecutorAddr ResolverAddr,
102181ad6265SDimitry Andric unsigned NumTrampolines) {
102281ad6265SDimitry Andric
102381ad6265SDimitry Andric unsigned OffsetToPtr = alignTo(NumTrampolines * TrampolineSize, 8);
102481ad6265SDimitry Andric
102581ad6265SDimitry Andric memcpy(TrampolineBlockWorkingMem + OffsetToPtr, &ResolverAddr,
102681ad6265SDimitry Andric sizeof(uint64_t));
102781ad6265SDimitry Andric
102881ad6265SDimitry Andric uint32_t *Trampolines =
102981ad6265SDimitry Andric reinterpret_cast<uint32_t *>(TrampolineBlockWorkingMem);
103081ad6265SDimitry Andric for (unsigned I = 0; I < NumTrampolines; ++I, OffsetToPtr -= TrampolineSize) {
103181ad6265SDimitry Andric uint32_t Hi20 = (OffsetToPtr + 0x800) & 0xFFFFF000;
103281ad6265SDimitry Andric uint32_t Lo12 = OffsetToPtr - Hi20;
103381ad6265SDimitry Andric Trampolines[4 * I + 0] = 0x00000297 | Hi20; // auipc t0, %hi(Lptr)
103481ad6265SDimitry Andric Trampolines[4 * I + 1] =
103581ad6265SDimitry Andric 0x0002b283 | ((Lo12 & 0xFFF) << 20); // ld t0, %lo(Lptr)
103681ad6265SDimitry Andric Trampolines[4 * I + 2] = 0x00028367; // jalr t1, t0
103781ad6265SDimitry Andric Trampolines[4 * I + 3] = 0xdeadface; // padding
103881ad6265SDimitry Andric }
103981ad6265SDimitry Andric }
104081ad6265SDimitry Andric
writeIndirectStubsBlock(char * StubsBlockWorkingMem,ExecutorAddr StubsBlockTargetAddress,ExecutorAddr PointersBlockTargetAddress,unsigned NumStubs)104181ad6265SDimitry Andric void OrcRiscv64::writeIndirectStubsBlock(
1042*06c3fb27SDimitry Andric char *StubsBlockWorkingMem, ExecutorAddr StubsBlockTargetAddress,
1043*06c3fb27SDimitry Andric ExecutorAddr PointersBlockTargetAddress, unsigned NumStubs) {
104481ad6265SDimitry Andric // Stub format is:
104581ad6265SDimitry Andric //
104681ad6265SDimitry Andric // .section __orc_stubs
104781ad6265SDimitry Andric // stub1:
104881ad6265SDimitry Andric // auipc t0, %hi(ptr1) ; PC-rel load of ptr1
104981ad6265SDimitry Andric // ld t0, %lo(t0)
105081ad6265SDimitry Andric // jr t0 ; Jump to resolver
105181ad6265SDimitry Andric // .quad 0 ; Pad to 16 bytes
105281ad6265SDimitry Andric // stub2:
105381ad6265SDimitry Andric // auipc t0, %hi(ptr1) ; PC-rel load of ptr1
105481ad6265SDimitry Andric // ld t0, %lo(t0)
105581ad6265SDimitry Andric // jr t0 ; Jump to resolver
105681ad6265SDimitry Andric // .quad 0
105781ad6265SDimitry Andric //
105881ad6265SDimitry Andric // ...
105981ad6265SDimitry Andric //
106081ad6265SDimitry Andric // .section __orc_ptrs
106181ad6265SDimitry Andric // ptr1:
106281ad6265SDimitry Andric // .quad 0x0
106381ad6265SDimitry Andric // ptr2:
106481ad6265SDimitry Andric // .quad 0x0
106581ad6265SDimitry Andric //
106681ad6265SDimitry Andric // ...
106781ad6265SDimitry Andric
106881ad6265SDimitry Andric assert(stubAndPointerRangesOk<OrcRiscv64>(
106981ad6265SDimitry Andric StubsBlockTargetAddress, PointersBlockTargetAddress, NumStubs) &&
107081ad6265SDimitry Andric "PointersBlock is out of range");
107181ad6265SDimitry Andric
107281ad6265SDimitry Andric uint32_t *Stub = reinterpret_cast<uint32_t *>(StubsBlockWorkingMem);
107381ad6265SDimitry Andric
107481ad6265SDimitry Andric for (unsigned I = 0; I < NumStubs; ++I) {
107581ad6265SDimitry Andric uint64_t PtrDisplacement =
107681ad6265SDimitry Andric PointersBlockTargetAddress - StubsBlockTargetAddress;
107781ad6265SDimitry Andric uint32_t Hi20 = (PtrDisplacement + 0x800) & 0xFFFFF000;
107881ad6265SDimitry Andric uint32_t Lo12 = PtrDisplacement - Hi20;
107981ad6265SDimitry Andric Stub[4 * I + 0] = 0x00000297 | Hi20; // auipc t0, %hi(Lptr)
108081ad6265SDimitry Andric Stub[4 * I + 1] = 0x0002b283 | ((Lo12 & 0xFFF) << 20); // ld t0, %lo(Lptr)
108181ad6265SDimitry Andric Stub[4 * I + 2] = 0x00028067; // jr t0
108281ad6265SDimitry Andric Stub[4 * I + 3] = 0xfeedbeef; // padding
108381ad6265SDimitry Andric PointersBlockTargetAddress += PointerSize;
108481ad6265SDimitry Andric StubsBlockTargetAddress += StubSize;
108581ad6265SDimitry Andric }
108681ad6265SDimitry Andric }
108781ad6265SDimitry Andric
writeResolverCode(char * ResolverWorkingMem,ExecutorAddr ResolverTargetAddress,ExecutorAddr ReentryFnAddr,ExecutorAddr ReentryCtxAddr)1088bdd1243dSDimitry Andric void OrcLoongArch64::writeResolverCode(char *ResolverWorkingMem,
1089*06c3fb27SDimitry Andric ExecutorAddr ResolverTargetAddress,
1090*06c3fb27SDimitry Andric ExecutorAddr ReentryFnAddr,
1091*06c3fb27SDimitry Andric ExecutorAddr ReentryCtxAddr) {
1092bdd1243dSDimitry Andric
1093bdd1243dSDimitry Andric LLVM_DEBUG({
1094bdd1243dSDimitry Andric dbgs() << "Writing resolver code to "
1095bdd1243dSDimitry Andric << formatv("{0:x16}", ResolverTargetAddress) << "\n";
1096bdd1243dSDimitry Andric });
1097bdd1243dSDimitry Andric
1098bdd1243dSDimitry Andric const uint32_t ResolverCode[] = {
1099bdd1243dSDimitry Andric 0x02fde063, // 0x0: addi.d $sp, $sp, -136(0xf78)
1100bdd1243dSDimitry Andric 0x29c00061, // 0x4: st.d $ra, $sp, 0
1101bdd1243dSDimitry Andric 0x29c02064, // 0x8: st.d $a0, $sp, 8(0x8)
1102bdd1243dSDimitry Andric 0x29c04065, // 0xc: st.d $a1, $sp, 16(0x10)
1103bdd1243dSDimitry Andric 0x29c06066, // 0x10: st.d $a2, $sp, 24(0x18)
1104bdd1243dSDimitry Andric 0x29c08067, // 0x14: st.d $a3, $sp, 32(0x20)
1105bdd1243dSDimitry Andric 0x29c0a068, // 0x18: st.d $a4, $sp, 40(0x28)
1106bdd1243dSDimitry Andric 0x29c0c069, // 0x1c: st.d $a5, $sp, 48(0x30)
1107bdd1243dSDimitry Andric 0x29c0e06a, // 0x20: st.d $a6, $sp, 56(0x38)
1108bdd1243dSDimitry Andric 0x29c1006b, // 0x24: st.d $a7, $sp, 64(0x40)
1109bdd1243dSDimitry Andric 0x2bc12060, // 0x28: fst.d $fa0, $sp, 72(0x48)
1110bdd1243dSDimitry Andric 0x2bc14061, // 0x2c: fst.d $fa1, $sp, 80(0x50)
1111bdd1243dSDimitry Andric 0x2bc16062, // 0x30: fst.d $fa2, $sp, 88(0x58)
1112bdd1243dSDimitry Andric 0x2bc18063, // 0x34: fst.d $fa3, $sp, 96(0x60)
1113bdd1243dSDimitry Andric 0x2bc1a064, // 0x38: fst.d $fa4, $sp, 104(0x68)
1114bdd1243dSDimitry Andric 0x2bc1c065, // 0x3c: fst.d $fa5, $sp, 112(0x70)
1115bdd1243dSDimitry Andric 0x2bc1e066, // 0x40: fst.d $fa6, $sp, 120(0x78)
1116bdd1243dSDimitry Andric 0x2bc20067, // 0x44: fst.d $fa7, $sp, 128(0x80)
1117bdd1243dSDimitry Andric 0x1c000004, // 0x48: pcaddu12i $a0, 0
1118bdd1243dSDimitry Andric 0x28c1c084, // 0x4c: ld.d $a0, $a0, 112(0x70)
1119bdd1243dSDimitry Andric 0x001501a5, // 0x50: move $a1, $t1
1120bdd1243dSDimitry Andric 0x02ffd0a5, // 0x54: addi.d $a1, $a1, -12(0xff4)
1121bdd1243dSDimitry Andric 0x1c000006, // 0x58: pcaddu12i $a2, 0
1122bdd1243dSDimitry Andric 0x28c1a0c6, // 0x5c: ld.d $a2, $a2, 104(0x68)
1123bdd1243dSDimitry Andric 0x4c0000c1, // 0x60: jirl $ra, $a2, 0
1124bdd1243dSDimitry Andric 0x0015008c, // 0x64: move $t0, $a0
1125bdd1243dSDimitry Andric 0x2b820067, // 0x68: fld.d $fa7, $sp, 128(0x80)
1126bdd1243dSDimitry Andric 0x2b81e066, // 0x6c: fld.d $fa6, $sp, 120(0x78)
1127bdd1243dSDimitry Andric 0x2b81c065, // 0x70: fld.d $fa5, $sp, 112(0x70)
1128bdd1243dSDimitry Andric 0x2b81a064, // 0x74: fld.d $fa4, $sp, 104(0x68)
1129bdd1243dSDimitry Andric 0x2b818063, // 0x78: fld.d $fa3, $sp, 96(0x60)
1130bdd1243dSDimitry Andric 0x2b816062, // 0x7c: fld.d $fa2, $sp, 88(0x58)
1131bdd1243dSDimitry Andric 0x2b814061, // 0x80: fld.d $fa1, $sp, 80(0x50)
1132bdd1243dSDimitry Andric 0x2b812060, // 0x84: fld.d $fa0, $sp, 72(0x48)
1133bdd1243dSDimitry Andric 0x28c1006b, // 0x88: ld.d $a7, $sp, 64(0x40)
1134bdd1243dSDimitry Andric 0x28c0e06a, // 0x8c: ld.d $a6, $sp, 56(0x38)
1135bdd1243dSDimitry Andric 0x28c0c069, // 0x90: ld.d $a5, $sp, 48(0x30)
1136bdd1243dSDimitry Andric 0x28c0a068, // 0x94: ld.d $a4, $sp, 40(0x28)
1137bdd1243dSDimitry Andric 0x28c08067, // 0x98: ld.d $a3, $sp, 32(0x20)
1138bdd1243dSDimitry Andric 0x28c06066, // 0x9c: ld.d $a2, $sp, 24(0x18)
1139bdd1243dSDimitry Andric 0x28c04065, // 0xa0: ld.d $a1, $sp, 16(0x10)
1140bdd1243dSDimitry Andric 0x28c02064, // 0xa4: ld.d $a0, $sp, 8(0x8)
1141bdd1243dSDimitry Andric 0x28c00061, // 0xa8: ld.d $ra, $sp, 0
1142bdd1243dSDimitry Andric 0x02c22063, // 0xac: addi.d $sp, $sp, 136(0x88)
1143bdd1243dSDimitry Andric 0x4c000180, // 0xb0: jr $t0
1144bdd1243dSDimitry Andric 0x00000000, // 0xb4: padding to align at 8 bytes
1145bdd1243dSDimitry Andric 0x01234567, // 0xb8: Lreentry_ctx_ptr:
1146bdd1243dSDimitry Andric 0xdeedbeef, // 0xbc: .dword 0
1147bdd1243dSDimitry Andric 0x98765432, // 0xc0: Lreentry_fn_ptr:
1148bdd1243dSDimitry Andric 0xcafef00d, // 0xc4: .dword 0
1149bdd1243dSDimitry Andric };
1150bdd1243dSDimitry Andric
1151bdd1243dSDimitry Andric const unsigned ReentryCtxAddrOffset = 0xb8;
1152bdd1243dSDimitry Andric const unsigned ReentryFnAddrOffset = 0xc0;
1153bdd1243dSDimitry Andric
1154bdd1243dSDimitry Andric memcpy(ResolverWorkingMem, ResolverCode, sizeof(ResolverCode));
1155bdd1243dSDimitry Andric memcpy(ResolverWorkingMem + ReentryFnAddrOffset, &ReentryFnAddr,
1156bdd1243dSDimitry Andric sizeof(uint64_t));
1157bdd1243dSDimitry Andric memcpy(ResolverWorkingMem + ReentryCtxAddrOffset, &ReentryCtxAddr,
1158bdd1243dSDimitry Andric sizeof(uint64_t));
1159bdd1243dSDimitry Andric }
1160bdd1243dSDimitry Andric
writeTrampolines(char * TrampolineBlockWorkingMem,ExecutorAddr TrampolineBlockTargetAddress,ExecutorAddr ResolverAddr,unsigned NumTrampolines)1161*06c3fb27SDimitry Andric void OrcLoongArch64::writeTrampolines(char *TrampolineBlockWorkingMem,
1162*06c3fb27SDimitry Andric ExecutorAddr TrampolineBlockTargetAddress,
1163*06c3fb27SDimitry Andric ExecutorAddr ResolverAddr,
1164*06c3fb27SDimitry Andric unsigned NumTrampolines) {
1165bdd1243dSDimitry Andric
1166bdd1243dSDimitry Andric LLVM_DEBUG({
1167bdd1243dSDimitry Andric dbgs() << "Writing trampoline code to "
1168bdd1243dSDimitry Andric << formatv("{0:x16}", TrampolineBlockTargetAddress) << "\n";
1169bdd1243dSDimitry Andric });
1170bdd1243dSDimitry Andric
1171bdd1243dSDimitry Andric unsigned OffsetToPtr = alignTo(NumTrampolines * TrampolineSize, 8);
1172bdd1243dSDimitry Andric
1173bdd1243dSDimitry Andric memcpy(TrampolineBlockWorkingMem + OffsetToPtr, &ResolverAddr,
1174bdd1243dSDimitry Andric sizeof(uint64_t));
1175bdd1243dSDimitry Andric
1176bdd1243dSDimitry Andric uint32_t *Trampolines =
1177bdd1243dSDimitry Andric reinterpret_cast<uint32_t *>(TrampolineBlockWorkingMem);
1178bdd1243dSDimitry Andric for (unsigned I = 0; I < NumTrampolines; ++I, OffsetToPtr -= TrampolineSize) {
1179bdd1243dSDimitry Andric uint32_t Hi20 = (OffsetToPtr + 0x800) & 0xfffff000;
1180bdd1243dSDimitry Andric uint32_t Lo12 = OffsetToPtr - Hi20;
1181bdd1243dSDimitry Andric Trampolines[4 * I + 0] =
1182bdd1243dSDimitry Andric 0x1c00000c |
1183bdd1243dSDimitry Andric (((Hi20 >> 12) & 0xfffff) << 5); // pcaddu12i $t0, %pc_hi20(Lptr)
1184bdd1243dSDimitry Andric Trampolines[4 * I + 1] =
1185bdd1243dSDimitry Andric 0x28c0018c | ((Lo12 & 0xfff) << 10); // ld.d $t0, $t0, %pc_lo12(Lptr)
1186bdd1243dSDimitry Andric Trampolines[4 * I + 2] = 0x4c00018d; // jirl $t1, $t0, 0
1187bdd1243dSDimitry Andric Trampolines[4 * I + 3] = 0x0; // padding
1188bdd1243dSDimitry Andric }
1189bdd1243dSDimitry Andric }
1190bdd1243dSDimitry Andric
writeIndirectStubsBlock(char * StubsBlockWorkingMem,ExecutorAddr StubsBlockTargetAddress,ExecutorAddr PointersBlockTargetAddress,unsigned NumStubs)1191bdd1243dSDimitry Andric void OrcLoongArch64::writeIndirectStubsBlock(
1192*06c3fb27SDimitry Andric char *StubsBlockWorkingMem, ExecutorAddr StubsBlockTargetAddress,
1193*06c3fb27SDimitry Andric ExecutorAddr PointersBlockTargetAddress, unsigned NumStubs) {
1194bdd1243dSDimitry Andric // Stub format is:
1195bdd1243dSDimitry Andric //
1196bdd1243dSDimitry Andric // .section __orc_stubs
1197bdd1243dSDimitry Andric // stub1:
1198bdd1243dSDimitry Andric // pcaddu12i $t0, %pc_hi20(ptr1) ; PC-rel load of ptr1
1199bdd1243dSDimitry Andric // ld.d $t0, $t0, %pc_lo12(ptr1)
1200bdd1243dSDimitry Andric // jr $t0 ; Jump to resolver
1201bdd1243dSDimitry Andric // .dword 0 ; Pad to 16 bytes
1202bdd1243dSDimitry Andric // stub2:
1203bdd1243dSDimitry Andric // pcaddu12i $t0, %pc_hi20(ptr2) ; PC-rel load of ptr2
1204bdd1243dSDimitry Andric // ld.d $t0, $t0, %pc_lo12(ptr2)
1205bdd1243dSDimitry Andric // jr $t0 ; Jump to resolver
1206bdd1243dSDimitry Andric // .dword 0 ; Pad to 16 bytes
1207bdd1243dSDimitry Andric // ...
1208bdd1243dSDimitry Andric //
1209bdd1243dSDimitry Andric // .section __orc_ptrs
1210bdd1243dSDimitry Andric // ptr1:
1211bdd1243dSDimitry Andric // .dword 0x0
1212bdd1243dSDimitry Andric // ptr2:
1213bdd1243dSDimitry Andric // .dword 0x0
1214bdd1243dSDimitry Andric // ...
1215bdd1243dSDimitry Andric LLVM_DEBUG({
1216bdd1243dSDimitry Andric dbgs() << "Writing stubs code to "
1217bdd1243dSDimitry Andric << formatv("{0:x16}", StubsBlockTargetAddress) << "\n";
1218bdd1243dSDimitry Andric });
1219bdd1243dSDimitry Andric assert(stubAndPointerRangesOk<OrcLoongArch64>(
1220bdd1243dSDimitry Andric StubsBlockTargetAddress, PointersBlockTargetAddress, NumStubs) &&
1221bdd1243dSDimitry Andric "PointersBlock is out of range");
1222bdd1243dSDimitry Andric
1223bdd1243dSDimitry Andric uint32_t *Stub = reinterpret_cast<uint32_t *>(StubsBlockWorkingMem);
1224bdd1243dSDimitry Andric
1225bdd1243dSDimitry Andric for (unsigned I = 0; I < NumStubs; ++I) {
1226bdd1243dSDimitry Andric uint64_t PtrDisplacement =
1227bdd1243dSDimitry Andric PointersBlockTargetAddress - StubsBlockTargetAddress;
1228bdd1243dSDimitry Andric uint32_t Hi20 = (PtrDisplacement + 0x800) & 0xfffff000;
1229bdd1243dSDimitry Andric uint32_t Lo12 = PtrDisplacement - Hi20;
1230bdd1243dSDimitry Andric Stub[4 * I + 0] = 0x1c00000c | (((Hi20 >> 12) & 0xfffff)
1231bdd1243dSDimitry Andric << 5); // pcaddu12i $t0, %pc_hi20(Lptr)
1232bdd1243dSDimitry Andric Stub[4 * I + 1] =
1233bdd1243dSDimitry Andric 0x28c0018c | ((Lo12 & 0xfff) << 10); // ld.d $t0, $t0, %pc_lo12(Lptr)
1234bdd1243dSDimitry Andric Stub[4 * I + 2] = 0x4c000180; // jr $t0
1235bdd1243dSDimitry Andric Stub[4 * I + 3] = 0x0; // padding
1236bdd1243dSDimitry Andric PointersBlockTargetAddress += PointerSize;
1237bdd1243dSDimitry Andric StubsBlockTargetAddress += StubSize;
1238bdd1243dSDimitry Andric }
1239bdd1243dSDimitry Andric }
1240bdd1243dSDimitry Andric
12410b57cec5SDimitry Andric } // End namespace orc.
12420b57cec5SDimitry Andric } // End namespace llvm.
1243