| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include <initializer_list> |
| 8 |
|
| 9 |
#include "jit/JitFrames.h" |
| 10 |
#include "jit/JitRuntime.h" |
| 11 |
#include "jit/MacroAssembler.h" |
| 12 |
#include "vm/JitActivation.h" |
| 13 |
#include "vm/JSContext.h" |
| 14 |
|
| 15 |
#include "jit/MacroAssembler-inl.h" |
| 16 |
|
| 17 |
using namespace js; |
| 18 |
using namespace js::jit; |
| 19 |
|
| 20 |
void JitRuntime::generateExceptionTailStub(MacroAssembler& masm, |
| 21 |
Label* profilerExitTail, |
| 22 |
Label* bailoutTail) { |
| 23 |
AutoCreatedBy acb(masm, "JitRuntime::generateExceptionTailStub"); |
| 24 |
|
| 25 |
exceptionTailOffset_ = startTrampolineCode(masm); |
| 26 |
|
| 27 |
uint32_t returnValueCheckOffset = 0; |
| 28 |
masm.bind(masm.failureLabel()); |
| 29 |
masm.handleFailureWithHandlerTail(profilerExitTail, bailoutTail, |
| 30 |
&returnValueCheckOffset); |
| 31 |
|
| 32 |
exceptionTailReturnValueCheckOffset_ = returnValueCheckOffset; |
| 33 |
} |
| 34 |
|
| 35 |
void JitRuntime::generateProfilerExitFrameTailStub(MacroAssembler& masm, |
| 36 |
Label* profilerExitTail) { |
| 37 |
AutoCreatedBy acb(masm, "JitRuntime::generateProfilerExitFrameTailStub"); |
| 38 |
|
| 39 |
profilerExitFrameTailOffset_ = startTrampolineCode(masm); |
| 40 |
masm.bind(profilerExitTail); |
| 41 |
|
| 42 |
static constexpr size_t CallerFPOffset = |
| 43 |
CommonFrameLayout::offsetOfCallerFramePtr(); |
| 44 |
|
| 45 |
// Assert the caller frame's type is one of the types we expect. |
| 46 |
auto emitAssertPrevFrameType = [&masm]( |
| 47 |
Register framePtr, Register scratch, |
| 48 |
std::initializer_list<FrameType> types) { |
| 49 |
#ifdef DEBUG |
| 50 |
masm.loadPtr(Address(framePtr, CommonFrameLayout::offsetOfDescriptor()), |
| 51 |
scratch); |
| 52 |
masm.and32(Imm32(FRAMETYPE_MASK), scratch); |
| 53 |
|
| 54 |
Label checkOk; |
| 55 |
for (FrameType type : types) { |
| 56 |
masm.branch32(Assembler::Equal, scratch, Imm32(type), &checkOk); |
| 57 |
} |
| 58 |
masm.assumeUnreachable("Unexpected previous frame"); |
| 59 |
masm.bind(&checkOk); |
| 60 |
#else |
| 61 |
(void)masm; |
| 62 |
#endif |
| 63 |
}; |
| 64 |
|
| 65 |
AllocatableGeneralRegisterSet regs(GeneralRegisterSet::All()); |
| 66 |
regs.take(JSReturnOperand); |
| 67 |
Register scratch = regs.takeAny(); |
| 68 |
|
| 69 |
// The code generated below expects that the current frame pointer points |
| 70 |
// to an Ion or Baseline frame, at the state it would be immediately before |
| 71 |
// the frame epilogue and ret(). Thus, after this stub's business is done, it |
| 72 |
// restores the frame pointer and stack pointer, then executes a ret() and |
| 73 |
// returns directly to the caller frame, on behalf of the callee script that |
| 74 |
// jumped to this code. |
| 75 |
// |
| 76 |
// Thus the expected state is: |
| 77 |
// |
| 78 |
// [JitFrameLayout] <-- FramePointer |
| 79 |
// [frame contents] <-- StackPointer |
| 80 |
// |
| 81 |
// The generated jitcode is responsible for overwriting the |
| 82 |
// jitActivation->lastProfilingFrame field with a pointer to the previous |
| 83 |
// Ion or Baseline jit-frame that was pushed before this one. It is also |
| 84 |
// responsible for overwriting jitActivation->lastProfilingCallSite with |
| 85 |
// the return address into that frame. |
| 86 |
// |
| 87 |
// So this jitcode is responsible for "walking up" the jit stack, finding |
| 88 |
// the previous Ion or Baseline JS frame, and storing its address and the |
| 89 |
// return address into the appropriate fields on the current jitActivation. |
| 90 |
// |
| 91 |
// There are a fixed number of different path types that can lead to the |
| 92 |
// current frame, which is either a Baseline or Ion frame: |
| 93 |
// |
| 94 |
// <Baseline-Or-Ion> |
| 95 |
// ^ |
| 96 |
// | |
| 97 |
// ^--- Ion (or Baseline JSOp::Resume) |
| 98 |
// | |
| 99 |
// ^--- Baseline Stub <---- Baseline |
| 100 |
// | |
| 101 |
// ^--- IonICCall <---- Ion |
| 102 |
// | |
| 103 |
// ^--- Entry Frame (BaselineInterpreter) (unwrapped) |
| 104 |
// | |
| 105 |
// ^--- Arguments Rectifier (unwrapped) |
| 106 |
// | |
| 107 |
// ^--- Trampoline Native (unwrapped) |
| 108 |
// | |
| 109 |
// ^--- Entry Frame (CppToJSJit or WasmToJSJit) |
| 110 |
// |
| 111 |
// NOTE: Keep this in sync with JSJitProfilingFrameIterator::moveToNextFrame! |
| 112 |
|
| 113 |
Register actReg = regs.takeAny(); |
| 114 |
masm.loadJSContext(actReg); |
| 115 |
masm.loadPtr(Address(actReg, offsetof(JSContext, profilingActivation_)), |
| 116 |
actReg); |
| 117 |
|
| 118 |
Address lastProfilingFrame(actReg, |
| 119 |
JitActivation::offsetOfLastProfilingFrame()); |
| 120 |
Address lastProfilingCallSite(actReg, |
| 121 |
JitActivation::offsetOfLastProfilingCallSite()); |
| 122 |
|
| 123 |
#ifdef DEBUG |
| 124 |
// Ensure that frame we are exiting is current lastProfilingFrame |
| 125 |
{ |
| 126 |
masm.loadPtr(lastProfilingFrame, scratch); |
| 127 |
Label checkOk; |
| 128 |
masm.branchPtr(Assembler::Equal, scratch, ImmWord(0), &checkOk); |
| 129 |
masm.branchPtr(Assembler::Equal, FramePointer, scratch, &checkOk); |
| 130 |
masm.assumeUnreachable( |
| 131 |
"Mismatch between stored lastProfilingFrame and current frame " |
| 132 |
"pointer."); |
| 133 |
masm.bind(&checkOk); |
| 134 |
} |
| 135 |
#endif |
| 136 |
|
| 137 |
// Move FP into a scratch register and use that scratch register below, to |
| 138 |
// allow unwrapping rectifier frames without clobbering FP. |
| 139 |
Register fpScratch = regs.takeAny(); |
| 140 |
masm.mov(FramePointer, fpScratch); |
| 141 |
|
| 142 |
Label again; |
| 143 |
masm.bind(&again); |
| 144 |
|
| 145 |
// Load the frame descriptor into |scratch|, figure out what to do depending |
| 146 |
// on its type. |
| 147 |
masm.loadPtr(Address(fpScratch, JitFrameLayout::offsetOfDescriptor()), |
| 148 |
scratch); |
| 149 |
masm.and32(Imm32(FRAMETYPE_MASK), scratch); |
| 150 |
|
| 151 |
// Handling of each case is dependent on FrameDescriptor.type |
| 152 |
Label handle_BaselineOrIonJS; |
| 153 |
Label handle_BaselineStub; |
| 154 |
Label handle_Rectifier; |
| 155 |
Label handle_TrampolineNative; |
| 156 |
Label handle_BaselineInterpreterEntry; |
| 157 |
Label handle_IonICCall; |
| 158 |
Label handle_Entry; |
| 159 |
|
| 160 |
// We check for IonJS and BaselineStub first because these are the most common |
| 161 |
// types. Calls from Baseline are usually from a BaselineStub frame. |
| 162 |
masm.branch32(Assembler::Equal, scratch, Imm32(FrameType::IonJS), |
| 163 |
&handle_BaselineOrIonJS); |
| 164 |
masm.branch32(Assembler::Equal, scratch, Imm32(FrameType::BaselineStub), |
| 165 |
&handle_BaselineStub); |
| 166 |
masm.branch32(Assembler::Equal, scratch, Imm32(FrameType::Rectifier), |
| 167 |
&handle_Rectifier); |
| 168 |
if (JitOptions.emitInterpreterEntryTrampoline) { |
| 169 |
masm.branch32(Assembler::Equal, scratch, |
| 170 |
Imm32(FrameType::BaselineInterpreterEntry), |
| 171 |
&handle_BaselineInterpreterEntry); |
| 172 |
} |
| 173 |
masm.branch32(Assembler::Equal, scratch, Imm32(FrameType::CppToJSJit), |
| 174 |
&handle_Entry); |
| 175 |
masm.branch32(Assembler::Equal, scratch, Imm32(FrameType::BaselineJS), |
| 176 |
&handle_BaselineOrIonJS); |
| 177 |
masm.branch32(Assembler::Equal, scratch, Imm32(FrameType::IonICCall), |
| 178 |
&handle_IonICCall); |
| 179 |
masm.branch32(Assembler::Equal, scratch, Imm32(FrameType::TrampolineNative), |
| 180 |
&handle_TrampolineNative); |
| 181 |
masm.branch32(Assembler::Equal, scratch, Imm32(FrameType::WasmToJSJit), |
| 182 |
&handle_Entry); |
| 183 |
|
| 184 |
masm.assumeUnreachable( |
| 185 |
"Invalid caller frame type when returning from a JIT frame."); |
| 186 |
|
| 187 |
masm.bind(&handle_BaselineOrIonJS); |
| 188 |
{ |
| 189 |
// Returning directly to a Baseline or Ion frame. |
| 190 |
|
| 191 |
// lastProfilingCallSite := ReturnAddress |
| 192 |
masm.loadPtr(Address(fpScratch, JitFrameLayout::offsetOfReturnAddress()), |
| 193 |
scratch); |
| 194 |
masm.storePtr(scratch, lastProfilingCallSite); |
| 195 |
|
| 196 |
// lastProfilingFrame := CallerFrame |
| 197 |
masm.loadPtr(Address(fpScratch, CallerFPOffset), scratch); |
| 198 |
masm.storePtr(scratch, lastProfilingFrame); |
| 199 |
|
| 200 |
masm.moveToStackPtr(FramePointer); |
| 201 |
masm.pop(FramePointer); |
| 202 |
masm.ret(); |
| 203 |
} |
| 204 |
|
| 205 |
// Shared implementation for BaselineStub and IonICCall frames. |
| 206 |
auto emitHandleStubFrame = [&](FrameType expectedPrevType) { |
| 207 |
// Load pointer to stub frame and assert type of its caller frame. |
| 208 |
masm.loadPtr(Address(fpScratch, CallerFPOffset), fpScratch); |
| 209 |
emitAssertPrevFrameType(fpScratch, scratch, {expectedPrevType}); |
| 210 |
|
| 211 |
// lastProfilingCallSite := StubFrame.ReturnAddress |
| 212 |
masm.loadPtr(Address(fpScratch, CommonFrameLayout::offsetOfReturnAddress()), |
| 213 |
scratch); |
| 214 |
masm.storePtr(scratch, lastProfilingCallSite); |
| 215 |
|
| 216 |
// lastProfilingFrame := StubFrame.CallerFrame |
| 217 |
masm.loadPtr(Address(fpScratch, CallerFPOffset), scratch); |
| 218 |
masm.storePtr(scratch, lastProfilingFrame); |
| 219 |
|
| 220 |
masm.moveToStackPtr(FramePointer); |
| 221 |
masm.pop(FramePointer); |
| 222 |
masm.ret(); |
| 223 |
}; |
| 224 |
|
| 225 |
masm.bind(&handle_BaselineStub); |
| 226 |
{ |
| 227 |
// BaselineJS => BaselineStub frame. |
| 228 |
emitHandleStubFrame(FrameType::BaselineJS); |
| 229 |
} |
| 230 |
|
| 231 |
masm.bind(&handle_IonICCall); |
| 232 |
{ |
| 233 |
// IonJS => IonICCall frame. |
| 234 |
emitHandleStubFrame(FrameType::IonJS); |
| 235 |
} |
| 236 |
|
| 237 |
masm.bind(&handle_Rectifier); |
| 238 |
{ |
| 239 |
// There can be multiple previous frame types so just "unwrap" the arguments |
| 240 |
// rectifier frame and try again. |
| 241 |
masm.loadPtr(Address(fpScratch, CallerFPOffset), fpScratch); |
| 242 |
emitAssertPrevFrameType( |
| 243 |
fpScratch, scratch, |
| 244 |
{FrameType::IonJS, FrameType::BaselineStub, FrameType::TrampolineNative, |
| 245 |
FrameType::CppToJSJit, FrameType::WasmToJSJit}); |
| 246 |
masm.jump(&again); |
| 247 |
} |
| 248 |
|
| 249 |
masm.bind(&handle_TrampolineNative); |
| 250 |
{ |
| 251 |
// Unwrap this frame, similar to arguments rectifier frames. |
| 252 |
masm.loadPtr(Address(fpScratch, CallerFPOffset), fpScratch); |
| 253 |
emitAssertPrevFrameType( |
| 254 |
fpScratch, scratch, |
| 255 |
{FrameType::IonJS, FrameType::BaselineStub, FrameType::Rectifier, |
| 256 |
FrameType::CppToJSJit, FrameType::WasmToJSJit}); |
| 257 |
masm.jump(&again); |
| 258 |
} |
| 259 |
|
| 260 |
if (JitOptions.emitInterpreterEntryTrampoline) { |
| 261 |
masm.bind(&handle_BaselineInterpreterEntry); |
| 262 |
{ |
| 263 |
// Unwrap the baseline interpreter entry frame and try again. |
| 264 |
masm.loadPtr(Address(fpScratch, CallerFPOffset), fpScratch); |
| 265 |
emitAssertPrevFrameType( |
| 266 |
fpScratch, scratch, |
| 267 |
{FrameType::IonJS, FrameType::BaselineJS, FrameType::BaselineStub, |
| 268 |
FrameType::CppToJSJit, FrameType::WasmToJSJit, FrameType::IonICCall, |
| 269 |
FrameType::Rectifier}); |
| 270 |
masm.jump(&again); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
masm.bind(&handle_Entry); |
| 275 |
{ |
| 276 |
// FrameType::CppToJSJit / FrameType::WasmToJSJit |
| 277 |
// |
| 278 |
// A fast-path wasm->jit transition frame is an entry frame from the point |
| 279 |
// of view of the JIT. |
| 280 |
// Store null into both fields. |
| 281 |
masm.movePtr(ImmPtr(nullptr), scratch); |
| 282 |
masm.storePtr(scratch, lastProfilingCallSite); |
| 283 |
masm.storePtr(scratch, lastProfilingFrame); |
| 284 |
|
| 285 |
masm.moveToStackPtr(FramePointer); |
| 286 |
masm.pop(FramePointer); |
| 287 |
masm.ret(); |
| 288 |
} |
| 289 |
} |