1/*
2 * Copyright (C) 2013-2019 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "FTLThunks.h"
28
29#if ENABLE(FTL_JIT)
30
31#include "AssemblyHelpers.h"
32#include "DFGOSRExitCompilerCommon.h"
33#include "FPRInfo.h"
34#include "FTLOSRExitCompiler.h"
35#include "FTLOperations.h"
36#include "FTLSaveRestore.h"
37#include "GPRInfo.h"
38#include "LinkBuffer.h"
39
40namespace JSC { namespace FTL {
41
42using namespace DFG;
43
44enum class FrameAndStackAdjustmentRequirement {
45 Needed,
46 NotNeeded
47};
48
49static MacroAssemblerCodeRef<JITThunkPtrTag> genericGenerationThunkGenerator(
50 VM* vm, FunctionPtr<CFunctionPtrTag> generationFunction, PtrTag resultTag, const char* name, unsigned extraPopsToRestore, FrameAndStackAdjustmentRequirement frameAndStackAdjustmentRequirement)
51{
52 AssemblyHelpers jit(nullptr);
53
54 if (frameAndStackAdjustmentRequirement == FrameAndStackAdjustmentRequirement::Needed) {
55 // This needs to happen before we use the scratch buffer because this function also uses the scratch buffer.
56 adjustFrameAndStackInOSRExitCompilerThunk<FTL::JITCode>(jit, vm, JITType::FTLJIT);
57 }
58
59 // Note that the "return address" will be the ID that we pass to the generation function.
60
61 ptrdiff_t stackMisalignment = MacroAssembler::pushToSaveByteOffset();
62
63 // Pretend that we're a C call frame.
64 jit.pushToSave(MacroAssembler::framePointerRegister);
65 jit.move(MacroAssembler::stackPointerRegister, MacroAssembler::framePointerRegister);
66 stackMisalignment += MacroAssembler::pushToSaveByteOffset();
67
68 // Now create ourselves enough stack space to give saveAllRegisters() a scratch slot.
69 unsigned numberOfRequiredPops = 0;
70 do {
71 jit.pushToSave(GPRInfo::regT0);
72 stackMisalignment += MacroAssembler::pushToSaveByteOffset();
73 numberOfRequiredPops++;
74 } while (stackMisalignment % stackAlignmentBytes());
75
76 ScratchBuffer* scratchBuffer = vm->scratchBufferForSize(requiredScratchMemorySizeInBytes());
77 char* buffer = static_cast<char*>(scratchBuffer->dataBuffer());
78
79 saveAllRegisters(jit, buffer);
80
81 // Tell GC mark phase how much of the scratch buffer is active during call.
82 jit.move(MacroAssembler::TrustedImmPtr(scratchBuffer->addressOfActiveLength()), GPRInfo::nonArgGPR0);
83 jit.storePtr(MacroAssembler::TrustedImmPtr(requiredScratchMemorySizeInBytes()), GPRInfo::nonArgGPR0);
84
85 jit.loadPtr(GPRInfo::callFrameRegister, GPRInfo::argumentGPR0);
86 jit.peek(
87 GPRInfo::argumentGPR1,
88 (stackMisalignment - MacroAssembler::pushToSaveByteOffset()) / sizeof(void*));
89 MacroAssembler::Call functionCall = jit.call(OperationPtrTag);
90
91 // At this point we want to make a tail call to what was returned to us in the
92 // returnValueGPR. But at the same time as we do this, we must restore all registers.
93 // The way we will accomplish this is by arranging to have the tail call target in the
94 // return address "slot" (be it a register or the stack).
95
96 jit.move(GPRInfo::returnValueGPR, GPRInfo::regT0);
97
98 // Make sure we tell the GC that we're not using the scratch buffer anymore.
99 jit.move(MacroAssembler::TrustedImmPtr(scratchBuffer->addressOfActiveLength()), GPRInfo::regT1);
100 jit.storePtr(MacroAssembler::TrustedImmPtr(nullptr), GPRInfo::regT1);
101
102 // Prepare for tail call.
103 while (numberOfRequiredPops--)
104 jit.popToRestore(GPRInfo::regT1);
105 jit.popToRestore(MacroAssembler::framePointerRegister);
106
107 // When we came in here, there was an additional thing pushed to the stack. Some clients want it
108 // popped before proceeding.
109 while (extraPopsToRestore--)
110 jit.popToRestore(GPRInfo::regT1);
111
112 // Put the return address wherever the return instruction wants it. On all platforms, this
113 // ensures that the return address is out of the way of register restoration.
114 jit.restoreReturnAddressBeforeReturn(GPRInfo::regT0);
115
116 restoreAllRegisters(jit, buffer);
117
118#if CPU(ARM64E)
119 jit.untagPtr(resultTag, AssemblyHelpers::linkRegister);
120 jit.tagReturnAddress();
121#else
122 UNUSED_PARAM(resultTag);
123#endif
124 jit.ret();
125
126 LinkBuffer patchBuffer(jit, GLOBAL_THUNK_ID);
127 patchBuffer.link(functionCall, generationFunction.retagged<OperationPtrTag>());
128 return FINALIZE_CODE(patchBuffer, JITThunkPtrTag, "%s", name);
129}
130
131MacroAssemblerCodeRef<JITThunkPtrTag> osrExitGenerationThunkGenerator(VM* vm)
132{
133 unsigned extraPopsToRestore = 0;
134 return genericGenerationThunkGenerator(
135 vm, compileFTLOSRExit, OSRExitPtrTag, "FTL OSR exit generation thunk", extraPopsToRestore, FrameAndStackAdjustmentRequirement::Needed);
136}
137
138MacroAssemblerCodeRef<JITThunkPtrTag> lazySlowPathGenerationThunkGenerator(VM* vm)
139{
140 unsigned extraPopsToRestore = 1;
141 return genericGenerationThunkGenerator(
142 vm, compileFTLLazySlowPath, JITStubRoutinePtrTag, "FTL lazy slow path generation thunk", extraPopsToRestore, FrameAndStackAdjustmentRequirement::NotNeeded);
143}
144
145static void registerClobberCheck(AssemblyHelpers& jit, RegisterSet dontClobber)
146{
147 if (!Options::clobberAllRegsInFTLICSlowPath())
148 return;
149
150 RegisterSet clobber = RegisterSet::allRegisters();
151 clobber.exclude(RegisterSet::reservedHardwareRegisters());
152 clobber.exclude(RegisterSet::stackRegisters());
153 clobber.exclude(RegisterSet::calleeSaveRegisters());
154 clobber.exclude(dontClobber);
155
156 GPRReg someGPR;
157 for (Reg reg = Reg::first(); reg <= Reg::last(); reg = reg.next()) {
158 if (!clobber.get(reg) || !reg.isGPR())
159 continue;
160
161 jit.move(AssemblyHelpers::TrustedImm32(0x1337beef), reg.gpr());
162 someGPR = reg.gpr();
163 }
164
165 for (Reg reg = Reg::first(); reg <= Reg::last(); reg = reg.next()) {
166 if (!clobber.get(reg) || !reg.isFPR())
167 continue;
168
169 jit.move64ToDouble(someGPR, reg.fpr());
170 }
171}
172
173MacroAssemblerCodeRef<JITThunkPtrTag> slowPathCallThunkGenerator(const SlowPathCallKey& key)
174{
175 AssemblyHelpers jit(nullptr);
176 jit.tagReturnAddress();
177
178 // We want to save the given registers at the given offset, then we want to save the
179 // old return address somewhere past that offset, and then finally we want to make the
180 // call.
181
182 size_t currentOffset = key.offset() + sizeof(void*);
183
184#if CPU(X86) || CPU(X86_64)
185 currentOffset += sizeof(void*);
186#endif
187
188 for (MacroAssembler::RegisterID reg = MacroAssembler::firstRegister(); reg <= MacroAssembler::lastRegister(); reg = static_cast<MacroAssembler::RegisterID>(reg + 1)) {
189 if (!key.usedRegisters().get(reg))
190 continue;
191 jit.storePtr(reg, AssemblyHelpers::Address(MacroAssembler::stackPointerRegister, currentOffset));
192 currentOffset += sizeof(void*);
193 }
194
195 for (MacroAssembler::FPRegisterID reg = MacroAssembler::firstFPRegister(); reg <= MacroAssembler::lastFPRegister(); reg = static_cast<MacroAssembler::FPRegisterID>(reg + 1)) {
196 if (!key.usedRegisters().get(reg))
197 continue;
198 jit.storeDouble(reg, AssemblyHelpers::Address(MacroAssembler::stackPointerRegister, currentOffset));
199 currentOffset += sizeof(double);
200 }
201
202 jit.preserveReturnAddressAfterCall(GPRInfo::nonArgGPR0);
203 jit.storePtr(GPRInfo::nonArgGPR0, AssemblyHelpers::Address(MacroAssembler::stackPointerRegister, key.offset()));
204
205 registerClobberCheck(jit, key.argumentRegisters());
206
207 AssemblyHelpers::Call call = jit.call(OperationPtrTag);
208
209 jit.loadPtr(AssemblyHelpers::Address(MacroAssembler::stackPointerRegister, key.offset()), GPRInfo::nonPreservedNonReturnGPR);
210 jit.restoreReturnAddressBeforeReturn(GPRInfo::nonPreservedNonReturnGPR);
211
212 for (MacroAssembler::FPRegisterID reg = MacroAssembler::lastFPRegister(); ; reg = static_cast<MacroAssembler::FPRegisterID>(reg - 1)) {
213 if (key.usedRegisters().get(reg)) {
214 currentOffset -= sizeof(double);
215 jit.loadDouble(AssemblyHelpers::Address(MacroAssembler::stackPointerRegister, currentOffset), reg);
216 }
217 if (reg == MacroAssembler::firstFPRegister())
218 break;
219 }
220
221 for (MacroAssembler::RegisterID reg = MacroAssembler::lastRegister(); ; reg = static_cast<MacroAssembler::RegisterID>(reg - 1)) {
222 if (key.usedRegisters().get(reg)) {
223 currentOffset -= sizeof(void*);
224 jit.loadPtr(AssemblyHelpers::Address(MacroAssembler::stackPointerRegister, currentOffset), reg);
225 }
226 if (reg == MacroAssembler::firstRegister())
227 break;
228 }
229
230 jit.ret();
231
232 LinkBuffer patchBuffer(jit, GLOBAL_THUNK_ID);
233 patchBuffer.link(call, key.callTarget());
234 return FINALIZE_CODE(patchBuffer, JITThunkPtrTag, "FTL slow path call thunk for %s", toCString(key).data());
235}
236
237} } // namespace JSC::FTL
238
239#endif // ENABLE(FTL_JIT)
240
241