1 //===------------- OrcABISupport.cpp - ABI specific support code ----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ExecutionEngine/Orc/OrcABISupport.h"
10 #include "llvm/Support/FormatVariadic.h"
11 #include "llvm/Support/Process.h"
12 #include "llvm/Support/raw_ostream.h"
14 #define DEBUG_TYPE "orc"
18 template <typename ORCABI
>
19 bool stubAndPointerRangesOk(JITTargetAddress StubBlockAddr
,
20 JITTargetAddress PointerBlockAddr
,
22 constexpr unsigned MaxDisp
= ORCABI::StubToPointerMaxDisplacement
;
23 JITTargetAddress FirstStub
= StubBlockAddr
;
24 JITTargetAddress LastStub
= FirstStub
+ ((NumStubs
- 1) * ORCABI::StubSize
);
25 JITTargetAddress FirstPointer
= PointerBlockAddr
;
26 JITTargetAddress LastPointer
=
27 FirstPointer
+ ((NumStubs
- 1) * ORCABI::StubSize
);
29 if (FirstStub
< FirstPointer
) {
30 if (LastStub
>= FirstPointer
)
31 return false; // Ranges overlap.
32 return (FirstPointer
- FirstStub
<= MaxDisp
) &&
33 (LastPointer
- LastStub
<= MaxDisp
); // out-of-range.
36 if (LastPointer
>= FirstStub
)
37 return false; // Ranges overlap.
39 return (FirstStub
- FirstPointer
<= MaxDisp
) &&
40 (LastStub
- LastPointer
<= MaxDisp
);
46 void OrcAArch64::writeResolverCode(char *ResolverWorkingMem
,
47 JITTargetAddress ResolverTargetAddress
,
48 JITTargetAddress ReentryFnAddr
,
49 JITTargetAddress ReentryCtxAddr
) {
51 const uint32_t ResolverCode
[] = {
53 0xa9bf47fd, // 0x000: stp x29, x17, [sp, #-16]!
54 0x910003fd, // 0x004: mov x29, sp
55 0xa9bf73fb, // 0x008: stp x27, x28, [sp, #-16]!
56 0xa9bf6bf9, // 0x00c: stp x25, x26, [sp, #-16]!
57 0xa9bf63f7, // 0x010: stp x23, x24, [sp, #-16]!
58 0xa9bf5bf5, // 0x014: stp x21, x22, [sp, #-16]!
59 0xa9bf53f3, // 0x018: stp x19, x20, [sp, #-16]!
60 0xa9bf3fee, // 0x01c: stp x14, x15, [sp, #-16]!
61 0xa9bf37ec, // 0x020: stp x12, x13, [sp, #-16]!
62 0xa9bf2fea, // 0x024: stp x10, x11, [sp, #-16]!
63 0xa9bf27e8, // 0x028: stp x8, x9, [sp, #-16]!
64 0xa9bf1fe6, // 0x02c: stp x6, x7, [sp, #-16]!
65 0xa9bf17e4, // 0x030: stp x4, x5, [sp, #-16]!
66 0xa9bf0fe2, // 0x034: stp x2, x3, [sp, #-16]!
67 0xa9bf07e0, // 0x038: stp x0, x1, [sp, #-16]!
68 0xadbf7ffe, // 0x03c: stp q30, q31, [sp, #-32]!
69 0xadbf77fc, // 0x040: stp q28, q29, [sp, #-32]!
70 0xadbf6ffa, // 0x044: stp q26, q27, [sp, #-32]!
71 0xadbf67f8, // 0x048: stp q24, q25, [sp, #-32]!
72 0xadbf5ff6, // 0x04c: stp q22, q23, [sp, #-32]!
73 0xadbf57f4, // 0x050: stp q20, q21, [sp, #-32]!
74 0xadbf4ff2, // 0x054: stp q18, q19, [sp, #-32]!
75 0xadbf47f0, // 0x058: stp q16, q17, [sp, #-32]!
76 0xadbf3fee, // 0x05c: stp q14, q15, [sp, #-32]!
77 0xadbf37ec, // 0x060: stp q12, q13, [sp, #-32]!
78 0xadbf2fea, // 0x064: stp q10, q11, [sp, #-32]!
79 0xadbf27e8, // 0x068: stp q8, q9, [sp, #-32]!
80 0xadbf1fe6, // 0x06c: stp q6, q7, [sp, #-32]!
81 0xadbf17e4, // 0x070: stp q4, q5, [sp, #-32]!
82 0xadbf0fe2, // 0x074: stp q2, q3, [sp, #-32]!
83 0xadbf07e0, // 0x078: stp q0, q1, [sp, #-32]!
84 0x580004e0, // 0x07c: ldr x0, Lreentry_ctx_ptr
85 0xaa1e03e1, // 0x080: mov x1, x30
86 0xd1003021, // 0x084: sub x1, x1, #12
87 0x58000442, // 0x088: ldr x2, Lreentry_fn_ptr
88 0xd63f0040, // 0x08c: blr x2
89 0xaa0003f1, // 0x090: mov x17, x0
90 0xacc107e0, // 0x094: ldp q0, q1, [sp], #32
91 0xacc10fe2, // 0x098: ldp q2, q3, [sp], #32
92 0xacc117e4, // 0x09c: ldp q4, q5, [sp], #32
93 0xacc11fe6, // 0x0a0: ldp q6, q7, [sp], #32
94 0xacc127e8, // 0x0a4: ldp q8, q9, [sp], #32
95 0xacc12fea, // 0x0a8: ldp q10, q11, [sp], #32
96 0xacc137ec, // 0x0ac: ldp q12, q13, [sp], #32
97 0xacc13fee, // 0x0b0: ldp q14, q15, [sp], #32
98 0xacc147f0, // 0x0b4: ldp q16, q17, [sp], #32
99 0xacc14ff2, // 0x0b8: ldp q18, q19, [sp], #32
100 0xacc157f4, // 0x0bc: ldp q20, q21, [sp], #32
101 0xacc15ff6, // 0x0c0: ldp q22, q23, [sp], #32
102 0xacc167f8, // 0x0c4: ldp q24, q25, [sp], #32
103 0xacc16ffa, // 0x0c8: ldp q26, q27, [sp], #32
104 0xacc177fc, // 0x0cc: ldp q28, q29, [sp], #32
105 0xacc17ffe, // 0x0d0: ldp q30, q31, [sp], #32
106 0xa8c107e0, // 0x0d4: ldp x0, x1, [sp], #16
107 0xa8c10fe2, // 0x0d8: ldp x2, x3, [sp], #16
108 0xa8c117e4, // 0x0dc: ldp x4, x5, [sp], #16
109 0xa8c11fe6, // 0x0e0: ldp x6, x7, [sp], #16
110 0xa8c127e8, // 0x0e4: ldp x8, x9, [sp], #16
111 0xa8c12fea, // 0x0e8: ldp x10, x11, [sp], #16
112 0xa8c137ec, // 0x0ec: ldp x12, x13, [sp], #16
113 0xa8c13fee, // 0x0f0: ldp x14, x15, [sp], #16
114 0xa8c153f3, // 0x0f4: ldp x19, x20, [sp], #16
115 0xa8c15bf5, // 0x0f8: ldp x21, x22, [sp], #16
116 0xa8c163f7, // 0x0fc: ldp x23, x24, [sp], #16
117 0xa8c16bf9, // 0x100: ldp x25, x26, [sp], #16
118 0xa8c173fb, // 0x104: ldp x27, x28, [sp], #16
119 0xa8c17bfd, // 0x108: ldp x29, x30, [sp], #16
120 0xd65f0220, // 0x10c: ret x17
121 0x01234567, // 0x110: Lreentry_fn_ptr:
122 0xdeadbeef, // 0x114: .quad 0
123 0x98765432, // 0x118: Lreentry_ctx_ptr:
124 0xcafef00d // 0x11c: .quad 0
127 const unsigned ReentryFnAddrOffset
= 0x110;
128 const unsigned ReentryCtxAddrOffset
= 0x118;
130 memcpy(ResolverWorkingMem
, ResolverCode
, sizeof(ResolverCode
));
131 memcpy(ResolverWorkingMem
+ ReentryFnAddrOffset
, &ReentryFnAddr
,
133 memcpy(ResolverWorkingMem
+ ReentryCtxAddrOffset
, &ReentryCtxAddr
,
137 void OrcAArch64::writeTrampolines(char *TrampolineBlockWorkingMem
,
138 JITTargetAddress TrampolineBlockTargetAddress
,
139 JITTargetAddress ResolverAddr
,
140 unsigned NumTrampolines
) {
142 unsigned OffsetToPtr
= alignTo(NumTrampolines
* TrampolineSize
, 8);
144 memcpy(TrampolineBlockWorkingMem
+ OffsetToPtr
, &ResolverAddr
,
147 // OffsetToPtr is actually the offset from the PC for the 2nd instruction, so
151 uint32_t *Trampolines
=
152 reinterpret_cast<uint32_t *>(TrampolineBlockWorkingMem
);
154 for (unsigned I
= 0; I
< NumTrampolines
; ++I
, OffsetToPtr
-= TrampolineSize
) {
155 Trampolines
[3 * I
+ 0] = 0xaa1e03f1; // mov x17, x30
156 Trampolines
[3 * I
+ 1] = 0x58000010 | (OffsetToPtr
<< 3); // adr x16, Lptr
157 Trampolines
[3 * I
+ 2] = 0xd63f0200; // blr x16
161 void OrcAArch64::writeIndirectStubsBlock(
162 char *StubsBlockWorkingMem
, JITTargetAddress StubsBlockTargetAddress
,
163 JITTargetAddress PointersBlockTargetAddress
, unsigned NumStubs
) {
166 // .section __orc_stubs
168 // ldr x0, ptr1 ; PC-rel load of ptr1
169 // br x0 ; Jump to resolver
171 // ldr x0, ptr2 ; PC-rel load of ptr2
172 // br x0 ; Jump to resolver
176 // .section __orc_ptrs
184 static_assert(StubSize
== PointerSize
,
185 "Pointer and stub size must match for algorithm below");
186 assert(stubAndPointerRangesOk
<OrcAArch64
>(
187 StubsBlockTargetAddress
, PointersBlockTargetAddress
, NumStubs
) &&
188 "PointersBlock is out of range");
189 uint64_t PtrDisplacement
=
190 PointersBlockTargetAddress
- StubsBlockTargetAddress
;
191 uint64_t *Stub
= reinterpret_cast<uint64_t *>(StubsBlockWorkingMem
);
192 uint64_t PtrOffsetField
= PtrDisplacement
<< 3;
194 for (unsigned I
= 0; I
< NumStubs
; ++I
)
195 Stub
[I
] = 0xd61f020058000010 | PtrOffsetField
;
198 void OrcX86_64_Base::writeTrampolines(
199 char *TrampolineBlockWorkingMem
,
200 JITTargetAddress TrampolineBlockTargetAddress
,
201 JITTargetAddress ResolverAddr
, unsigned NumTrampolines
) {
203 unsigned OffsetToPtr
= NumTrampolines
* TrampolineSize
;
205 memcpy(TrampolineBlockWorkingMem
+ OffsetToPtr
, &ResolverAddr
,
208 uint64_t *Trampolines
=
209 reinterpret_cast<uint64_t *>(TrampolineBlockWorkingMem
);
210 uint64_t CallIndirPCRel
= 0xf1c40000000015ff;
212 for (unsigned I
= 0; I
< NumTrampolines
; ++I
, OffsetToPtr
-= TrampolineSize
)
213 Trampolines
[I
] = CallIndirPCRel
| ((OffsetToPtr
- 6) << 16);
216 void OrcX86_64_Base::writeIndirectStubsBlock(
217 char *StubsBlockWorkingMem
, JITTargetAddress StubsBlockTargetAddress
,
218 JITTargetAddress PointersBlockTargetAddress
, unsigned NumStubs
) {
221 // .section __orc_stubs
224 // .byte 0xC4 ; <- Invalid opcode padding.
231 // .section __orc_ptrs
239 // Populate the stubs page stubs and mark it executable.
240 static_assert(StubSize
== PointerSize
,
241 "Pointer and stub size must match for algorithm below");
242 assert(stubAndPointerRangesOk
<OrcX86_64_Base
>(
243 StubsBlockTargetAddress
, PointersBlockTargetAddress
, NumStubs
) &&
244 "PointersBlock is out of range");
245 uint64_t *Stub
= reinterpret_cast<uint64_t *>(StubsBlockWorkingMem
);
246 uint64_t PtrOffsetField
=
247 (PointersBlockTargetAddress
- StubsBlockTargetAddress
- 6) << 16;
248 for (unsigned I
= 0; I
< NumStubs
; ++I
)
249 Stub
[I
] = 0xF1C40000000025ff | PtrOffsetField
;
252 void OrcX86_64_SysV::writeResolverCode(char *ResolverWorkingMem
,
253 JITTargetAddress ResolverTargetAddress
,
254 JITTargetAddress ReentryFnAddr
,
255 JITTargetAddress ReentryCtxAddr
) {
258 dbgs() << "Writing resolver code to "
259 << formatv("{0:x16}", ResolverTargetAddress
) << "\n";
262 const uint8_t ResolverCode
[] = {
264 0x55, // 0x00: pushq %rbp
265 0x48, 0x89, 0xe5, // 0x01: movq %rsp, %rbp
266 0x50, // 0x04: pushq %rax
267 0x53, // 0x05: pushq %rbx
268 0x51, // 0x06: pushq %rcx
269 0x52, // 0x07: pushq %rdx
270 0x56, // 0x08: pushq %rsi
271 0x57, // 0x09: pushq %rdi
272 0x41, 0x50, // 0x0a: pushq %r8
273 0x41, 0x51, // 0x0c: pushq %r9
274 0x41, 0x52, // 0x0e: pushq %r10
275 0x41, 0x53, // 0x10: pushq %r11
276 0x41, 0x54, // 0x12: pushq %r12
277 0x41, 0x55, // 0x14: pushq %r13
278 0x41, 0x56, // 0x16: pushq %r14
279 0x41, 0x57, // 0x18: pushq %r15
280 0x48, 0x81, 0xec, 0x08, 0x02, 0x00, 0x00, // 0x1a: subq 0x208, %rsp
281 0x48, 0x0f, 0xae, 0x04, 0x24, // 0x21: fxsave64 (%rsp)
282 0x48, 0xbf, // 0x26: movabsq <CBMgr>, %rdi
284 // 0x28: JIT re-entry ctx addr.
285 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
287 0x48, 0x8b, 0x75, 0x08, // 0x30: movq 8(%rbp), %rsi
288 0x48, 0x83, 0xee, 0x06, // 0x34: subq $6, %rsi
289 0x48, 0xb8, // 0x38: movabsq <REntry>, %rax
291 // 0x3a: JIT re-entry fn addr:
292 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
294 0xff, 0xd0, // 0x42: callq *%rax
295 0x48, 0x89, 0x45, 0x08, // 0x44: movq %rax, 8(%rbp)
296 0x48, 0x0f, 0xae, 0x0c, 0x24, // 0x48: fxrstor64 (%rsp)
297 0x48, 0x81, 0xc4, 0x08, 0x02, 0x00, 0x00, // 0x4d: addq 0x208, %rsp
298 0x41, 0x5f, // 0x54: popq %r15
299 0x41, 0x5e, // 0x56: popq %r14
300 0x41, 0x5d, // 0x58: popq %r13
301 0x41, 0x5c, // 0x5a: popq %r12
302 0x41, 0x5b, // 0x5c: popq %r11
303 0x41, 0x5a, // 0x5e: popq %r10
304 0x41, 0x59, // 0x60: popq %r9
305 0x41, 0x58, // 0x62: popq %r8
306 0x5f, // 0x64: popq %rdi
307 0x5e, // 0x65: popq %rsi
308 0x5a, // 0x66: popq %rdx
309 0x59, // 0x67: popq %rcx
310 0x5b, // 0x68: popq %rbx
311 0x58, // 0x69: popq %rax
312 0x5d, // 0x6a: popq %rbp
316 const unsigned ReentryFnAddrOffset
= 0x3a;
317 const unsigned ReentryCtxAddrOffset
= 0x28;
319 memcpy(ResolverWorkingMem
, ResolverCode
, sizeof(ResolverCode
));
320 memcpy(ResolverWorkingMem
+ ReentryFnAddrOffset
, &ReentryFnAddr
,
322 memcpy(ResolverWorkingMem
+ ReentryCtxAddrOffset
, &ReentryCtxAddr
,
326 void OrcX86_64_Win32::writeResolverCode(char *ResolverWorkingMem
,
327 JITTargetAddress ResolverTargetAddress
,
328 JITTargetAddress ReentryFnAddr
,
329 JITTargetAddress ReentryCtxAddr
) {
331 // resolverCode is similar to OrcX86_64 with differences specific to windows
332 // x64 calling convention: arguments go into rcx, rdx and come in reverse
333 // order, shadow space allocation on stack
334 const uint8_t ResolverCode
[] = {
336 0x55, // 0x00: pushq %rbp
337 0x48, 0x89, 0xe5, // 0x01: movq %rsp, %rbp
338 0x50, // 0x04: pushq %rax
339 0x53, // 0x05: pushq %rbx
340 0x51, // 0x06: pushq %rcx
341 0x52, // 0x07: pushq %rdx
342 0x56, // 0x08: pushq %rsi
343 0x57, // 0x09: pushq %rdi
344 0x41, 0x50, // 0x0a: pushq %r8
345 0x41, 0x51, // 0x0c: pushq %r9
346 0x41, 0x52, // 0x0e: pushq %r10
347 0x41, 0x53, // 0x10: pushq %r11
348 0x41, 0x54, // 0x12: pushq %r12
349 0x41, 0x55, // 0x14: pushq %r13
350 0x41, 0x56, // 0x16: pushq %r14
351 0x41, 0x57, // 0x18: pushq %r15
352 0x48, 0x81, 0xec, 0x08, 0x02, 0x00, 0x00, // 0x1a: subq 0x208, %rsp
353 0x48, 0x0f, 0xae, 0x04, 0x24, // 0x21: fxsave64 (%rsp)
355 0x48, 0xb9, // 0x26: movabsq <CBMgr>, %rcx
356 // 0x28: JIT re-entry ctx addr.
357 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
359 0x48, 0x8B, 0x55, 0x08, // 0x30: mov rdx, [rbp+0x8]
360 0x48, 0x83, 0xea, 0x06, // 0x34: sub rdx, 0x6
362 0x48, 0xb8, // 0x38: movabsq <REntry>, %rax
363 // 0x3a: JIT re-entry fn addr:
364 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
366 // 0x42: sub rsp, 0x20 (Allocate shadow space)
367 0x48, 0x83, 0xEC, 0x20,
368 0xff, 0xd0, // 0x46: callq *%rax
370 // 0x48: add rsp, 0x20 (Free shadow space)
371 0x48, 0x83, 0xC4, 0x20,
373 0x48, 0x89, 0x45, 0x08, // 0x4C: movq %rax, 8(%rbp)
374 0x48, 0x0f, 0xae, 0x0c, 0x24, // 0x50: fxrstor64 (%rsp)
375 0x48, 0x81, 0xc4, 0x08, 0x02, 0x00, 0x00, // 0x55: addq 0x208, %rsp
376 0x41, 0x5f, // 0x5C: popq %r15
377 0x41, 0x5e, // 0x5E: popq %r14
378 0x41, 0x5d, // 0x60: popq %r13
379 0x41, 0x5c, // 0x62: popq %r12
380 0x41, 0x5b, // 0x64: popq %r11
381 0x41, 0x5a, // 0x66: popq %r10
382 0x41, 0x59, // 0x68: popq %r9
383 0x41, 0x58, // 0x6a: popq %r8
384 0x5f, // 0x6c: popq %rdi
385 0x5e, // 0x6d: popq %rsi
386 0x5a, // 0x6e: popq %rdx
387 0x59, // 0x6f: popq %rcx
388 0x5b, // 0x70: popq %rbx
389 0x58, // 0x71: popq %rax
390 0x5d, // 0x72: popq %rbp
394 const unsigned ReentryFnAddrOffset
= 0x3a;
395 const unsigned ReentryCtxAddrOffset
= 0x28;
397 memcpy(ResolverWorkingMem
, ResolverCode
, sizeof(ResolverCode
));
398 memcpy(ResolverWorkingMem
+ ReentryFnAddrOffset
, &ReentryFnAddr
,
400 memcpy(ResolverWorkingMem
+ ReentryCtxAddrOffset
, &ReentryCtxAddr
,
404 void OrcI386::writeResolverCode(char *ResolverWorkingMem
,
405 JITTargetAddress ResolverTargetAddress
,
406 JITTargetAddress ReentryFnAddr
,
407 JITTargetAddress ReentryCtxAddr
) {
409 assert((ReentryFnAddr
>> 32) == 0 && "ReentryFnAddr out of range");
410 assert((ReentryCtxAddr
>> 32) == 0 && "ReentryCtxAddr out of range");
412 const uint8_t ResolverCode
[] = {
414 0x55, // 0x00: pushl %ebp
415 0x89, 0xe5, // 0x01: movl %esp, %ebp
416 0x54, // 0x03: pushl %esp
417 0x83, 0xe4, 0xf0, // 0x04: andl $-0x10, %esp
418 0x50, // 0x07: pushl %eax
419 0x53, // 0x08: pushl %ebx
420 0x51, // 0x09: pushl %ecx
421 0x52, // 0x0a: pushl %edx
422 0x56, // 0x0b: pushl %esi
423 0x57, // 0x0c: pushl %edi
424 0x81, 0xec, 0x18, 0x02, 0x00, 0x00, // 0x0d: subl $0x218, %esp
425 0x0f, 0xae, 0x44, 0x24, 0x10, // 0x13: fxsave 0x10(%esp)
426 0x8b, 0x75, 0x04, // 0x18: movl 0x4(%ebp), %esi
427 0x83, 0xee, 0x05, // 0x1b: subl $0x5, %esi
428 0x89, 0x74, 0x24, 0x04, // 0x1e: movl %esi, 0x4(%esp)
429 0xc7, 0x04, 0x24, 0x00, 0x00, 0x00,
430 0x00, // 0x22: movl <cbmgr>, (%esp)
431 0xb8, 0x00, 0x00, 0x00, 0x00, // 0x29: movl <reentry>, %eax
432 0xff, 0xd0, // 0x2e: calll *%eax
433 0x89, 0x45, 0x04, // 0x30: movl %eax, 0x4(%ebp)
434 0x0f, 0xae, 0x4c, 0x24, 0x10, // 0x33: fxrstor 0x10(%esp)
435 0x81, 0xc4, 0x18, 0x02, 0x00, 0x00, // 0x38: addl $0x218, %esp
436 0x5f, // 0x3e: popl %edi
437 0x5e, // 0x3f: popl %esi
438 0x5a, // 0x40: popl %edx
439 0x59, // 0x41: popl %ecx
440 0x5b, // 0x42: popl %ebx
441 0x58, // 0x43: popl %eax
442 0x8b, 0x65, 0xfc, // 0x44: movl -0x4(%ebp), %esp
443 0x5d, // 0x48: popl %ebp
447 const unsigned ReentryFnAddrOffset
= 0x2a;
448 const unsigned ReentryCtxAddrOffset
= 0x25;
450 memcpy(ResolverWorkingMem
, ResolverCode
, sizeof(ResolverCode
));
451 memcpy(ResolverWorkingMem
+ ReentryFnAddrOffset
, &ReentryFnAddr
,
453 memcpy(ResolverWorkingMem
+ ReentryCtxAddrOffset
, &ReentryCtxAddr
,
457 void OrcI386::writeTrampolines(char *TrampolineWorkingMem
,
458 JITTargetAddress TrampolineBlockTargetAddress
,
459 JITTargetAddress ResolverAddr
,
460 unsigned NumTrampolines
) {
461 assert((ResolverAddr
>> 32) == 0 && "ResolverAddr out of range");
463 uint64_t CallRelImm
= 0xF1C4C400000000e8;
464 uint64_t ResolverRel
= ResolverAddr
- TrampolineBlockTargetAddress
- 5;
466 uint64_t *Trampolines
= reinterpret_cast<uint64_t *>(TrampolineWorkingMem
);
467 for (unsigned I
= 0; I
< NumTrampolines
; ++I
, ResolverRel
-= TrampolineSize
)
468 Trampolines
[I
] = CallRelImm
| (ResolverRel
<< 8);
471 void OrcI386::writeIndirectStubsBlock(
472 char *StubsBlockWorkingMem
, JITTargetAddress StubsBlockTargetAddress
,
473 JITTargetAddress PointersBlockTargetAddress
, unsigned NumStubs
) {
474 assert((StubsBlockTargetAddress
>> 32) == 0 &&
475 "StubsBlockTargetAddress is out of range");
476 assert((PointersBlockTargetAddress
>> 32) == 0 &&
477 "PointersBlockTargetAddress is out of range");
481 // .section __orc_stubs
484 // .byte 0xC4 ; <- Invalid opcode padding.
491 // .section __orc_ptrs
499 assert(stubAndPointerRangesOk
<OrcI386
>(
500 StubsBlockTargetAddress
, PointersBlockTargetAddress
, NumStubs
) &&
501 "PointersBlock is out of range");
503 uint64_t *Stub
= reinterpret_cast<uint64_t *>(StubsBlockWorkingMem
);
504 uint64_t PtrAddr
= PointersBlockTargetAddress
;
505 for (unsigned I
= 0; I
< NumStubs
; ++I
, PtrAddr
+= 4)
506 Stub
[I
] = 0xF1C40000000025ff | (PtrAddr
<< 16);
509 void OrcMips32_Base::writeResolverCode(char *ResolverWorkingMem
,
510 JITTargetAddress ResolverTargetAddress
,
511 JITTargetAddress ReentryFnAddr
,
512 JITTargetAddress ReentryCtxAddr
,
515 const uint32_t ResolverCode
[] = {
517 0x27bdff98, // 0x00: addiu $sp,$sp,-104
518 0xafa20000, // 0x04: sw $v0,0($sp)
519 0xafa30004, // 0x08: sw $v1,4($sp)
520 0xafa40008, // 0x0c: sw $a0,8($sp)
521 0xafa5000c, // 0x10: sw $a1,12($sp)
522 0xafa60010, // 0x14: sw $a2,16($sp)
523 0xafa70014, // 0x18: sw $a3,20($sp)
524 0xafb00018, // 0x1c: sw $s0,24($sp)
525 0xafb1001c, // 0x20: sw $s1,28($sp)
526 0xafb20020, // 0x24: sw $s2,32($sp)
527 0xafb30024, // 0x28: sw $s3,36($sp)
528 0xafb40028, // 0x2c: sw $s4,40($sp)
529 0xafb5002c, // 0x30: sw $s5,44($sp)
530 0xafb60030, // 0x34: sw $s6,48($sp)
531 0xafb70034, // 0x38: sw $s7,52($sp)
532 0xafa80038, // 0x3c: sw $t0,56($sp)
533 0xafa9003c, // 0x40: sw $t1,60($sp)
534 0xafaa0040, // 0x44: sw $t2,64($sp)
535 0xafab0044, // 0x48: sw $t3,68($sp)
536 0xafac0048, // 0x4c: sw $t4,72($sp)
537 0xafad004c, // 0x50: sw $t5,76($sp)
538 0xafae0050, // 0x54: sw $t6,80($sp)
539 0xafaf0054, // 0x58: sw $t7,84($sp)
540 0xafb80058, // 0x5c: sw $t8,88($sp)
541 0xafb9005c, // 0x60: sw $t9,92($sp)
542 0xafbe0060, // 0x64: sw $fp,96($sp)
543 0xafbf0064, // 0x68: sw $ra,100($sp)
545 // JIT re-entry ctx addr.
546 0x00000000, // 0x6c: lui $a0,ctx
547 0x00000000, // 0x70: addiu $a0,$a0,ctx
549 0x03e02825, // 0x74: move $a1, $ra
550 0x24a5ffec, // 0x78: addiu $a1,$a1,-20
552 // JIT re-entry fn addr:
553 0x00000000, // 0x7c: lui $t9,reentry
554 0x00000000, // 0x80: addiu $t9,$t9,reentry
556 0x0320f809, // 0x84: jalr $t9
557 0x00000000, // 0x88: nop
558 0x8fbf0064, // 0x8c: lw $ra,100($sp)
559 0x8fbe0060, // 0x90: lw $fp,96($sp)
560 0x8fb9005c, // 0x94: lw $t9,92($sp)
561 0x8fb80058, // 0x98: lw $t8,88($sp)
562 0x8faf0054, // 0x9c: lw $t7,84($sp)
563 0x8fae0050, // 0xa0: lw $t6,80($sp)
564 0x8fad004c, // 0xa4: lw $t5,76($sp)
565 0x8fac0048, // 0xa8: lw $t4,72($sp)
566 0x8fab0044, // 0xac: lw $t3,68($sp)
567 0x8faa0040, // 0xb0: lw $t2,64($sp)
568 0x8fa9003c, // 0xb4: lw $t1,60($sp)
569 0x8fa80038, // 0xb8: lw $t0,56($sp)
570 0x8fb70034, // 0xbc: lw $s7,52($sp)
571 0x8fb60030, // 0xc0: lw $s6,48($sp)
572 0x8fb5002c, // 0xc4: lw $s5,44($sp)
573 0x8fb40028, // 0xc8: lw $s4,40($sp)
574 0x8fb30024, // 0xcc: lw $s3,36($sp)
575 0x8fb20020, // 0xd0: lw $s2,32($sp)
576 0x8fb1001c, // 0xd4: lw $s1,28($sp)
577 0x8fb00018, // 0xd8: lw $s0,24($sp)
578 0x8fa70014, // 0xdc: lw $a3,20($sp)
579 0x8fa60010, // 0xe0: lw $a2,16($sp)
580 0x8fa5000c, // 0xe4: lw $a1,12($sp)
581 0x8fa40008, // 0xe8: lw $a0,8($sp)
582 0x27bd0068, // 0xec: addiu $sp,$sp,104
583 0x0300f825, // 0xf0: move $ra, $t8
584 0x03200008, // 0xf4: jr $t9
585 0x00000000, // 0xf8: move $t9, $v0/v1
588 const unsigned ReentryFnAddrOffset
= 0x7c; // JIT re-entry fn addr lui
589 const unsigned ReentryCtxAddrOffset
= 0x6c; // JIT re-entry context addr lui
590 const unsigned Offsett
= 0xf8;
592 memcpy(ResolverWorkingMem
, ResolverCode
, sizeof(ResolverCode
));
594 // Depending on endian return value will be in v0 or v1.
595 uint32_t MoveVxT9
= isBigEndian
? 0x0060c825 : 0x0040c825;
596 memcpy(ResolverWorkingMem
+ Offsett
, &MoveVxT9
, sizeof(MoveVxT9
));
598 uint32_t ReentryCtxLUi
=
599 0x3c040000 | (((ReentryCtxAddr
+ 0x8000) >> 16) & 0xFFFF);
600 uint32_t ReentryCtxADDiu
= 0x24840000 | ((ReentryCtxAddr
)&0xFFFF);
601 memcpy(ResolverWorkingMem
+ ReentryCtxAddrOffset
, &ReentryCtxLUi
,
602 sizeof(ReentryCtxLUi
));
603 memcpy(ResolverWorkingMem
+ ReentryCtxAddrOffset
+ 4, &ReentryCtxADDiu
,
604 sizeof(ReentryCtxADDiu
));
606 uint32_t ReentryFnLUi
=
607 0x3c190000 | (((ReentryFnAddr
+ 0x8000) >> 16) & 0xFFFF);
608 uint32_t ReentryFnADDiu
= 0x27390000 | ((ReentryFnAddr
)&0xFFFF);
609 memcpy(ResolverWorkingMem
+ ReentryFnAddrOffset
, &ReentryFnLUi
,
610 sizeof(ReentryFnLUi
));
611 memcpy(ResolverWorkingMem
+ ReentryFnAddrOffset
+ 4, &ReentryFnADDiu
,
612 sizeof(ReentryFnADDiu
));
615 void OrcMips32_Base::writeTrampolines(
616 char *TrampolineBlockWorkingMem
,
617 JITTargetAddress TrampolineBlockTargetAddress
,
618 JITTargetAddress ResolverAddr
, unsigned NumTrampolines
) {
620 assert((ResolverAddr
>> 32) == 0 && "ResolverAddr out of range");
622 uint32_t *Trampolines
=
623 reinterpret_cast<uint32_t *>(TrampolineBlockWorkingMem
);
624 uint32_t RHiAddr
= ((ResolverAddr
+ 0x8000) >> 16);
626 for (unsigned I
= 0; I
< NumTrampolines
; ++I
) {
628 // lui $t9,ResolverAddr
629 // addiu $t9,$t9,ResolverAddr
632 Trampolines
[5 * I
+ 0] = 0x03e0c025;
633 Trampolines
[5 * I
+ 1] = 0x3c190000 | (RHiAddr
& 0xFFFF);
634 Trampolines
[5 * I
+ 2] = 0x27390000 | (ResolverAddr
& 0xFFFF);
635 Trampolines
[5 * I
+ 3] = 0x0320f809;
636 Trampolines
[5 * I
+ 4] = 0x00000000;
640 void OrcMips32_Base::writeIndirectStubsBlock(
641 char *StubsBlockWorkingMem
, JITTargetAddress StubsBlockTargetAddress
,
642 JITTargetAddress PointersBlockTargetAddress
, unsigned NumStubs
) {
643 assert((StubsBlockTargetAddress
>> 32) == 0 &&
644 "InitialPtrVal is out of range");
648 // .section __orc_stubs
651 // lw $t9, %lo(ptr1)($t9)
655 // lw $t9,%lo(ptr1)($t9)
660 // .section __orc_ptrs
668 assert(stubAndPointerRangesOk
<OrcAArch64
>(
669 StubsBlockTargetAddress
, PointersBlockTargetAddress
, NumStubs
) &&
670 "PointersBlock is out of range");
672 // Populate the stubs page stubs and mark it executable.
673 uint32_t *Stub
= reinterpret_cast<uint32_t *>(StubsBlockWorkingMem
);
674 uint64_t PtrAddr
= PointersBlockTargetAddress
;
676 for (unsigned I
= 0; I
< NumStubs
; ++I
) {
677 uint32_t HiAddr
= ((PtrAddr
+ 0x8000) >> 16);
678 Stub
[4 * I
+ 0] = 0x3c190000 | (HiAddr
& 0xFFFF); // lui $t9,ptr1
679 Stub
[4 * I
+ 1] = 0x8f390000 | (PtrAddr
& 0xFFFF); // lw $t9,%lo(ptr1)($t9)
680 Stub
[4 * I
+ 2] = 0x03200008; // jr $t9
681 Stub
[4 * I
+ 3] = 0x00000000; // nop
686 void OrcMips64::writeResolverCode(char *ResolverWorkingMem
,
687 JITTargetAddress ResolverTargetAddress
,
688 JITTargetAddress ReentryFnAddr
,
689 JITTargetAddress ReentryCtxAddr
) {
691 const uint32_t ResolverCode
[] = {
693 0x67bdff30, // 0x00: daddiu $sp,$sp,-208
694 0xffa20000, // 0x04: sd v0,0(sp)
695 0xffa30008, // 0x08: sd v1,8(sp)
696 0xffa40010, // 0x0c: sd a0,16(sp)
697 0xffa50018, // 0x10: sd a1,24(sp)
698 0xffa60020, // 0x14: sd a2,32(sp)
699 0xffa70028, // 0x18: sd a3,40(sp)
700 0xffa80030, // 0x1c: sd a4,48(sp)
701 0xffa90038, // 0x20: sd a5,56(sp)
702 0xffaa0040, // 0x24: sd a6,64(sp)
703 0xffab0048, // 0x28: sd a7,72(sp)
704 0xffac0050, // 0x2c: sd t0,80(sp)
705 0xffad0058, // 0x30: sd t1,88(sp)
706 0xffae0060, // 0x34: sd t2,96(sp)
707 0xffaf0068, // 0x38: sd t3,104(sp)
708 0xffb00070, // 0x3c: sd s0,112(sp)
709 0xffb10078, // 0x40: sd s1,120(sp)
710 0xffb20080, // 0x44: sd s2,128(sp)
711 0xffb30088, // 0x48: sd s3,136(sp)
712 0xffb40090, // 0x4c: sd s4,144(sp)
713 0xffb50098, // 0x50: sd s5,152(sp)
714 0xffb600a0, // 0x54: sd s6,160(sp)
715 0xffb700a8, // 0x58: sd s7,168(sp)
716 0xffb800b0, // 0x5c: sd t8,176(sp)
717 0xffb900b8, // 0x60: sd t9,184(sp)
718 0xffbe00c0, // 0x64: sd fp,192(sp)
719 0xffbf00c8, // 0x68: sd ra,200(sp)
721 // JIT re-entry ctx addr.
722 0x00000000, // 0x6c: lui $a0,heighest(ctx)
723 0x00000000, // 0x70: daddiu $a0,$a0,heigher(ctx)
724 0x00000000, // 0x74: dsll $a0,$a0,16
725 0x00000000, // 0x78: daddiu $a0,$a0,hi(ctx)
726 0x00000000, // 0x7c: dsll $a0,$a0,16
727 0x00000000, // 0x80: daddiu $a0,$a0,lo(ctx)
729 0x03e02825, // 0x84: move $a1, $ra
730 0x64a5ffdc, // 0x88: daddiu $a1,$a1,-36
732 // JIT re-entry fn addr:
733 0x00000000, // 0x8c: lui $t9,reentry
734 0x00000000, // 0x90: daddiu $t9,$t9,reentry
735 0x00000000, // 0x94: dsll $t9,$t9,
736 0x00000000, // 0x98: daddiu $t9,$t9,
737 0x00000000, // 0x9c: dsll $t9,$t9,
738 0x00000000, // 0xa0: daddiu $t9,$t9,
739 0x0320f809, // 0xa4: jalr $t9
740 0x00000000, // 0xa8: nop
741 0xdfbf00c8, // 0xac: ld ra, 200(sp)
742 0xdfbe00c0, // 0xb0: ld fp, 192(sp)
743 0xdfb900b8, // 0xb4: ld t9, 184(sp)
744 0xdfb800b0, // 0xb8: ld t8, 176(sp)
745 0xdfb700a8, // 0xbc: ld s7, 168(sp)
746 0xdfb600a0, // 0xc0: ld s6, 160(sp)
747 0xdfb50098, // 0xc4: ld s5, 152(sp)
748 0xdfb40090, // 0xc8: ld s4, 144(sp)
749 0xdfb30088, // 0xcc: ld s3, 136(sp)
750 0xdfb20080, // 0xd0: ld s2, 128(sp)
751 0xdfb10078, // 0xd4: ld s1, 120(sp)
752 0xdfb00070, // 0xd8: ld s0, 112(sp)
753 0xdfaf0068, // 0xdc: ld t3, 104(sp)
754 0xdfae0060, // 0xe0: ld t2, 96(sp)
755 0xdfad0058, // 0xe4: ld t1, 88(sp)
756 0xdfac0050, // 0xe8: ld t0, 80(sp)
757 0xdfab0048, // 0xec: ld a7, 72(sp)
758 0xdfaa0040, // 0xf0: ld a6, 64(sp)
759 0xdfa90038, // 0xf4: ld a5, 56(sp)
760 0xdfa80030, // 0xf8: ld a4, 48(sp)
761 0xdfa70028, // 0xfc: ld a3, 40(sp)
762 0xdfa60020, // 0x100: ld a2, 32(sp)
763 0xdfa50018, // 0x104: ld a1, 24(sp)
764 0xdfa40010, // 0x108: ld a0, 16(sp)
765 0xdfa30008, // 0x10c: ld v1, 8(sp)
766 0x67bd00d0, // 0x110: daddiu $sp,$sp,208
767 0x0300f825, // 0x114: move $ra, $t8
768 0x03200008, // 0x118: jr $t9
769 0x0040c825, // 0x11c: move $t9, $v0
772 const unsigned ReentryFnAddrOffset
= 0x8c; // JIT re-entry fn addr lui
773 const unsigned ReentryCtxAddrOffset
= 0x6c; // JIT re-entry ctx addr lui
775 memcpy(ResolverWorkingMem
, ResolverCode
, sizeof(ResolverCode
));
777 uint32_t ReentryCtxLUi
=
778 0x3c040000 | (((ReentryCtxAddr
+ 0x800080008000) >> 48) & 0xFFFF);
779 uint32_t ReentryCtxDADDiu
=
780 0x64840000 | (((ReentryCtxAddr
+ 0x80008000) >> 32) & 0xFFFF);
781 uint32_t ReentryCtxDSLL
= 0x00042438;
782 uint32_t ReentryCtxDADDiu2
=
783 0x64840000 | ((((ReentryCtxAddr
+ 0x8000) >> 16) & 0xFFFF));
784 uint32_t ReentryCtxDSLL2
= 0x00042438;
785 uint32_t ReentryCtxDADDiu3
= 0x64840000 | ((ReentryCtxAddr
)&0xFFFF);
787 memcpy(ResolverWorkingMem
+ ReentryCtxAddrOffset
, &ReentryCtxLUi
,
788 sizeof(ReentryCtxLUi
));
789 memcpy(ResolverWorkingMem
+ (ReentryCtxAddrOffset
+ 4), &ReentryCtxDADDiu
,
790 sizeof(ReentryCtxDADDiu
));
791 memcpy(ResolverWorkingMem
+ (ReentryCtxAddrOffset
+ 8), &ReentryCtxDSLL
,
792 sizeof(ReentryCtxDSLL
));
793 memcpy(ResolverWorkingMem
+ (ReentryCtxAddrOffset
+ 12), &ReentryCtxDADDiu2
,
794 sizeof(ReentryCtxDADDiu2
));
795 memcpy(ResolverWorkingMem
+ (ReentryCtxAddrOffset
+ 16), &ReentryCtxDSLL2
,
796 sizeof(ReentryCtxDSLL2
));
797 memcpy(ResolverWorkingMem
+ (ReentryCtxAddrOffset
+ 20), &ReentryCtxDADDiu3
,
798 sizeof(ReentryCtxDADDiu3
));
800 uint32_t ReentryFnLUi
=
801 0x3c190000 | (((ReentryFnAddr
+ 0x800080008000) >> 48) & 0xFFFF);
803 uint32_t ReentryFnDADDiu
=
804 0x67390000 | (((ReentryFnAddr
+ 0x80008000) >> 32) & 0xFFFF);
806 uint32_t ReentryFnDSLL
= 0x0019cc38;
808 uint32_t ReentryFnDADDiu2
=
809 0x67390000 | (((ReentryFnAddr
+ 0x8000) >> 16) & 0xFFFF);
811 uint32_t ReentryFnDSLL2
= 0x0019cc38;
813 uint32_t ReentryFnDADDiu3
= 0x67390000 | ((ReentryFnAddr
)&0xFFFF);
815 memcpy(ResolverWorkingMem
+ ReentryFnAddrOffset
, &ReentryFnLUi
,
816 sizeof(ReentryFnLUi
));
817 memcpy(ResolverWorkingMem
+ (ReentryFnAddrOffset
+ 4), &ReentryFnDADDiu
,
818 sizeof(ReentryFnDADDiu
));
819 memcpy(ResolverWorkingMem
+ (ReentryFnAddrOffset
+ 8), &ReentryFnDSLL
,
820 sizeof(ReentryFnDSLL
));
821 memcpy(ResolverWorkingMem
+ (ReentryFnAddrOffset
+ 12), &ReentryFnDADDiu2
,
822 sizeof(ReentryFnDADDiu2
));
823 memcpy(ResolverWorkingMem
+ (ReentryFnAddrOffset
+ 16), &ReentryFnDSLL2
,
824 sizeof(ReentryFnDSLL2
));
825 memcpy(ResolverWorkingMem
+ (ReentryFnAddrOffset
+ 20), &ReentryFnDADDiu3
,
826 sizeof(ReentryFnDADDiu3
));
829 void OrcMips64::writeTrampolines(char *TrampolineBlockWorkingMem
,
830 JITTargetAddress TrampolineBlockTargetAddress
,
831 JITTargetAddress ResolverAddr
,
832 unsigned NumTrampolines
) {
834 uint32_t *Trampolines
=
835 reinterpret_cast<uint32_t *>(TrampolineBlockWorkingMem
);
837 uint64_t HeighestAddr
= ((ResolverAddr
+ 0x800080008000) >> 48);
838 uint64_t HeigherAddr
= ((ResolverAddr
+ 0x80008000) >> 32);
839 uint64_t HiAddr
= ((ResolverAddr
+ 0x8000) >> 16);
841 for (unsigned I
= 0; I
< NumTrampolines
; ++I
) {
842 Trampolines
[10 * I
+ 0] = 0x03e0c025; // move $t8,$ra
843 Trampolines
[10 * I
+ 1] = 0x3c190000 | (HeighestAddr
& 0xFFFF); // lui $t9,resolveAddr
844 Trampolines
[10 * I
+ 2] = 0x67390000 | (HeigherAddr
& 0xFFFF); // daddiu $t9,$t9,%higher(resolveAddr)
845 Trampolines
[10 * I
+ 3] = 0x0019cc38; // dsll $t9,$t9,16
846 Trampolines
[10 * I
+ 4] = 0x67390000 | (HiAddr
& 0xFFFF); // daddiu $t9,$t9,%hi(ptr)
847 Trampolines
[10 * I
+ 5] = 0x0019cc38; // dsll $t9,$t9,16
848 Trampolines
[10 * I
+ 6] =
849 0x67390000 | (ResolverAddr
& 0xFFFF); // daddiu $t9,$t9,%lo(ptr)
850 Trampolines
[10 * I
+ 7] = 0x0320f809; // jalr $t9
851 Trampolines
[10 * I
+ 8] = 0x00000000; // nop
852 Trampolines
[10 * I
+ 9] = 0x00000000; // nop
856 void OrcMips64::writeIndirectStubsBlock(
857 char *StubsBlockWorkingMem
, JITTargetAddress StubsBlockTargetAddress
,
858 JITTargetAddress PointersBlockTargetAddress
, unsigned NumStubs
) {
861 // .section __orc_stubs
865 // daddiu $t9,$t9,%hi(ptr)
872 // daddiu $t9,$t9,%hi(ptr)
879 // .section __orc_ptrs
887 assert(stubAndPointerRangesOk
<OrcAArch64
>(
888 StubsBlockTargetAddress
, PointersBlockTargetAddress
, NumStubs
) &&
889 "PointersBlock is out of range");
891 // Populate the stubs page stubs and mark it executable.
892 uint32_t *Stub
= reinterpret_cast<uint32_t *>(StubsBlockWorkingMem
);
893 uint64_t PtrAddr
= PointersBlockTargetAddress
;
895 for (unsigned I
= 0; I
< NumStubs
; ++I
, PtrAddr
+= 8) {
896 uint64_t HeighestAddr
= ((PtrAddr
+ 0x800080008000) >> 48);
897 uint64_t HeigherAddr
= ((PtrAddr
+ 0x80008000) >> 32);
898 uint64_t HiAddr
= ((PtrAddr
+ 0x8000) >> 16);
899 Stub
[8 * I
+ 0] = 0x3c190000 | (HeighestAddr
& 0xFFFF); // lui $t9,ptr1
900 Stub
[8 * I
+ 1] = 0x67390000 | (HeigherAddr
& 0xFFFF); // daddiu $t9,$t9,%higher(ptr)
901 Stub
[8 * I
+ 2] = 0x0019cc38; // dsll $t9,$t9,16
902 Stub
[8 * I
+ 3] = 0x67390000 | (HiAddr
& 0xFFFF); // daddiu $t9,$t9,%hi(ptr)
903 Stub
[8 * I
+ 4] = 0x0019cc38; // dsll $t9,$t9,16
904 Stub
[8 * I
+ 5] = 0xdf390000 | (PtrAddr
& 0xFFFF); // ld $t9,%lo(ptr)
905 Stub
[8 * I
+ 6] = 0x03200008; // jr $t9
906 Stub
[8 * I
+ 7] = 0x00000000; // nop
909 } // End namespace orc.
910 } // End namespace llvm.