1/*
2 * Copyright (C) 2012-2018 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 "LLIntThunks.h"
28
29#include "CallData.h"
30#include "ExceptionHelpers.h"
31#include "Interpreter.h"
32#include "JSCJSValueInlines.h"
33#include "JSInterfaceJIT.h"
34#include "JSObject.h"
35#include "LLIntCLoop.h"
36#include "LLIntData.h"
37#include "LinkBuffer.h"
38#include "LowLevelInterpreter.h"
39#include "ProtoCallFrame.h"
40#include "StackAlignment.h"
41#include "VM.h"
42#include "WasmCallingConvention.h"
43#include "WasmContextInlines.h"
44#include <wtf/NeverDestroyed.h>
45
46namespace JSC {
47
48#if ENABLE(JIT)
49
50namespace LLInt {
51
52// These thunks are necessary because of nearCall used on JITed code.
53// It requires that the distance from nearCall address to the destination address
54// fits on 32-bits, and that's not the case of getCodeRef(llint_function_for_call_prologue)
55// and others LLIntEntrypoints.
56
57static MacroAssemblerCodeRef<JITThunkPtrTag> generateThunkWithJumpTo(OpcodeID opcodeID, const char *thunkKind)
58{
59 JSInterfaceJIT jit;
60
61 // FIXME: there's probably a better way to do it on X86, but I'm not sure I care.
62 LLIntCode target = LLInt::getCodeFunctionPtr<JSEntryPtrTag>(opcodeID);
63 assertIsTaggedWith(target, JSEntryPtrTag);
64
65#if ENABLE(WEBASSEMBLY)
66 CCallHelpers::RegisterID scratch = Wasm::wasmCallingConvention().prologueScratchGPRs[0];
67#else
68 CCallHelpers::RegisterID scratch = JSInterfaceJIT::regT0;
69#endif
70 jit.move(JSInterfaceJIT::TrustedImmPtr(target), scratch);
71 jit.farJump(scratch, JSEntryPtrTag);
72
73 LinkBuffer patchBuffer(jit, GLOBAL_THUNK_ID);
74 return FINALIZE_CODE(patchBuffer, JITThunkPtrTag, "LLInt %s prologue thunk", thunkKind);
75}
76
77MacroAssemblerCodeRef<JITThunkPtrTag> functionForCallEntryThunk()
78{
79 static LazyNeverDestroyed<MacroAssemblerCodeRef<JITThunkPtrTag>> codeRef;
80 static std::once_flag onceKey;
81 std::call_once(onceKey, [&] {
82 codeRef.construct(generateThunkWithJumpTo(llint_function_for_call_prologue, "function for call"));
83 });
84 return codeRef;
85}
86
87MacroAssemblerCodeRef<JITThunkPtrTag> functionForConstructEntryThunk()
88{
89 static LazyNeverDestroyed<MacroAssemblerCodeRef<JITThunkPtrTag>> codeRef;
90 static std::once_flag onceKey;
91 std::call_once(onceKey, [&] {
92 codeRef.construct(generateThunkWithJumpTo(llint_function_for_construct_prologue, "function for construct"));
93 });
94 return codeRef;
95}
96
97MacroAssemblerCodeRef<JITThunkPtrTag> functionForCallArityCheckThunk()
98{
99 static LazyNeverDestroyed<MacroAssemblerCodeRef<JITThunkPtrTag>> codeRef;
100 static std::once_flag onceKey;
101 std::call_once(onceKey, [&] {
102 codeRef.construct(generateThunkWithJumpTo(llint_function_for_call_arity_check, "function for call with arity check"));
103 });
104 return codeRef;
105}
106
107MacroAssemblerCodeRef<JITThunkPtrTag> functionForConstructArityCheckThunk()
108{
109 static LazyNeverDestroyed<MacroAssemblerCodeRef<JITThunkPtrTag>> codeRef;
110 static std::once_flag onceKey;
111 std::call_once(onceKey, [&] {
112 codeRef.construct(generateThunkWithJumpTo(llint_function_for_construct_arity_check, "function for construct with arity check"));
113 });
114 return codeRef;
115}
116
117MacroAssemblerCodeRef<JITThunkPtrTag> evalEntryThunk()
118{
119 static LazyNeverDestroyed<MacroAssemblerCodeRef<JITThunkPtrTag>> codeRef;
120 static std::once_flag onceKey;
121 std::call_once(onceKey, [&] {
122 codeRef.construct(generateThunkWithJumpTo(llint_eval_prologue, "eval"));
123 });
124 return codeRef;
125}
126
127MacroAssemblerCodeRef<JITThunkPtrTag> programEntryThunk()
128{
129 static LazyNeverDestroyed<MacroAssemblerCodeRef<JITThunkPtrTag>> codeRef;
130 static std::once_flag onceKey;
131 std::call_once(onceKey, [&] {
132 codeRef.construct(generateThunkWithJumpTo(llint_program_prologue, "program"));
133 });
134 return codeRef;
135}
136
137MacroAssemblerCodeRef<JITThunkPtrTag> moduleProgramEntryThunk()
138{
139 static LazyNeverDestroyed<MacroAssemblerCodeRef<JITThunkPtrTag>> codeRef;
140 static std::once_flag onceKey;
141 std::call_once(onceKey, [&] {
142 codeRef.construct(generateThunkWithJumpTo(llint_module_program_prologue, "module_program"));
143 });
144 return codeRef;
145}
146
147#if ENABLE(WEBASSEMBLY)
148MacroAssemblerCodeRef<JITThunkPtrTag> wasmFunctionEntryThunk()
149{
150 static LazyNeverDestroyed<MacroAssemblerCodeRef<JITThunkPtrTag>> codeRef;
151 static std::once_flag onceKey;
152 std::call_once(onceKey, [&] {
153 if (Wasm::Context::useFastTLS())
154 codeRef.construct(generateThunkWithJumpTo(wasm_function_prologue, "function for call"));
155 else
156 codeRef.construct(generateThunkWithJumpTo(wasm_function_prologue_no_tls, "function for call"));
157 });
158 return codeRef;
159}
160#endif // ENABLE(WEBASSEMBLY)
161
162} // namespace LLInt
163
164#endif
165
166#if ENABLE(C_LOOP)
167// Non-JIT (i.e. C Loop LLINT) case:
168
169EncodedJSValue vmEntryToJavaScript(void* executableAddress, VM* vm, ProtoCallFrame* protoCallFrame)
170{
171 JSValue result = CLoop::execute(llint_vm_entry_to_javascript, executableAddress, vm, protoCallFrame);
172 return JSValue::encode(result);
173}
174
175EncodedJSValue vmEntryToNative(void* executableAddress, VM* vm, ProtoCallFrame* protoCallFrame)
176{
177 JSValue result = CLoop::execute(llint_vm_entry_to_native, executableAddress, vm, protoCallFrame);
178 return JSValue::encode(result);
179}
180
181extern "C" VMEntryRecord* vmEntryRecord(EntryFrame* entryFrame)
182{
183 // The C Loop doesn't have any callee save registers, so the VMEntryRecord is allocated at the base of the frame.
184 intptr_t stackAlignment = stackAlignmentBytes();
185 intptr_t VMEntryTotalFrameSize = (sizeof(VMEntryRecord) + (stackAlignment - 1)) & ~(stackAlignment - 1);
186 return reinterpret_cast<VMEntryRecord*>(reinterpret_cast<char*>(entryFrame) - VMEntryTotalFrameSize);
187}
188
189#endif // ENABLE(C_LOOP)
190
191} // namespace JSC
192