Debian Codegraph @

js/src/jit/x64/Trampoline-x64.cpp

raw
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 "jit/Bailouts.h"
8 #include "jit/BaselineFrame.h"
9 #include "jit/CalleeToken.h"
10 #include "jit/JitFrames.h"
11 #include "jit/JitRuntime.h"
12 #include "jit/PerfSpewer.h"
13 #include "jit/VMFunctions.h"
14 #include "jit/x64/SharedICRegisters-x64.h"
15 #include "vm/JitActivation.h" // js::jit::JitActivation
16 #include "vm/JSContext.h"
17
18 #include "jit/MacroAssembler-inl.h"
19
20 using namespace js;
21 using namespace js::jit;
22
23 using mozilla::IsPowerOfTwo;
24
25 // This struct reflects the contents of the stack entry.
26 // Given a `CommonFrameLayout* frame`:
27 // - `frame->prevType()` should be `FrameType::CppToJSJit`.
28 // - Then EnterJITStackEntry starts at:
29 // frame->callerFramePtr() + EnterJITStackEntry::offsetFromFP()
30 // (the offset is negative, so this subtracts from the frame pointer)
31 struct EnterJITStackEntry {
32 // Offset from frame pointer to EnterJITStackEntry*.
33 static constexpr int32_t offsetFromFP() {
34 return -int32_t(offsetof(EnterJITStackEntry, rbp));
35 }
36
37 void* result;
38
39 #if defined(_WIN64)
40 struct XMM {
41 using XMM128 = char[16];
42 XMM128 xmm6;
43 XMM128 xmm7;
44 XMM128 xmm8;
45 XMM128 xmm9;
46 XMM128 xmm10;
47 XMM128 xmm11;
48 XMM128 xmm12;
49 XMM128 xmm13;
50 XMM128 xmm14;
51 XMM128 xmm15;
52 } xmm;
53
54 // 16-byte aligment for xmm registers above.
55 uint64_t xmmPadding;
56
57 void* rsi;
58 void* rdi;
59 #endif
60
61 void* r15;
62 void* r14;
63 void* r13;
64 void* r12;
65 void* rbx;
66 void* rbp;
67
68 // Pushed by CALL.
69 void* rip;
70 };
71
72 // All registers to save and restore. This includes the stack pointer, since we
73 // use the ability to reference register values on the stack by index.
74 static const LiveRegisterSet AllRegs =
75 LiveRegisterSet(GeneralRegisterSet(Registers::AllMask),
76 FloatRegisterSet(FloatRegisters::AllMask));
77
78 // Generates a trampoline for calling Jit compiled code from a C++ function.
79 // The trampoline use the EnterJitCode signature, with the standard x64 fastcall
80 // calling convention.
81 void JitRuntime::generateEnterJIT(JSContext* cx, MacroAssembler& masm) {
82 AutoCreatedBy acb(masm, "JitRuntime::generateEnterJIT");
83
84 enterJITOffset_ = startTrampolineCode(masm);
85
86 masm.assertStackAlignment(ABIStackAlignment,
87 -int32_t(sizeof(uintptr_t)) /* return address */);
88
89 const Register reg_code = IntArgReg0;
90 const Register reg_argc = IntArgReg1;
91 const Register reg_argv = IntArgReg2;
92 static_assert(OsrFrameReg == IntArgReg3);
93
94 #if defined(_WIN64)
95 const Address token = Address(rbp, 16 + ShadowStackSpace);
96 const Operand scopeChain = Operand(rbp, 24 + ShadowStackSpace);
97 const Operand numStackValuesAddr = Operand(rbp, 32 + ShadowStackSpace);
98 const Operand result = Operand(rbp, 40 + ShadowStackSpace);
99 #else
100 const Register token = IntArgReg4;
101 const Register scopeChain = IntArgReg5;
102 const Operand numStackValuesAddr = Operand(rbp, 16 + ShadowStackSpace);
103 const Operand result = Operand(rbp, 24 + ShadowStackSpace);
104 #endif
105
106 // Note: the stack pushes below must match the fields in EnterJITStackEntry.
107
108 // Save old stack frame pointer, set new stack frame pointer.
109 masm.push(rbp);
110 masm.mov(rsp, rbp);
111
112 // Save non-volatile registers. These must be saved by the trampoline, rather
113 // than by the JIT'd code, because they are scanned by the conservative
114 // scanner.
115 masm.push(rbx);
116 masm.push(r12);
117 masm.push(r13);
118 masm.push(r14);
119 masm.push(r15);
120 #if defined(_WIN64)
121 masm.push(rdi);
122 masm.push(rsi);
123
124 // 16-byte aligment for vmovdqa
125 masm.subq(Imm32(sizeof(EnterJITStackEntry::XMM) + 8), rsp);
126
127 masm.vmovdqa(xmm6, Operand(rsp, offsetof(EnterJITStackEntry::XMM, xmm6)));
128 masm.vmovdqa(xmm7, Operand(rsp, offsetof(EnterJITStackEntry::XMM, xmm7)));
129 masm.vmovdqa(xmm8, Operand(rsp, offsetof(EnterJITStackEntry::XMM, xmm8)));
130 masm.vmovdqa(xmm9, Operand(rsp, offsetof(EnterJITStackEntry::XMM, xmm9)));
131 masm.vmovdqa(xmm10, Operand(rsp, offsetof(EnterJITStackEntry::XMM, xmm10)));
132 masm.vmovdqa(xmm11, Operand(rsp, offsetof(EnterJITStackEntry::XMM, xmm11)));
133 masm.vmovdqa(xmm12, Operand(rsp, offsetof(EnterJITStackEntry::XMM, xmm12)));
134 masm.vmovdqa(xmm13, Operand(rsp, offsetof(EnterJITStackEntry::XMM, xmm13)));
135 masm.vmovdqa(xmm14, Operand(rsp, offsetof(EnterJITStackEntry::XMM, xmm14)));
136 masm.vmovdqa(xmm15, Operand(rsp, offsetof(EnterJITStackEntry::XMM, xmm15)));
137 #endif
138
139 // Save arguments passed in registers needed after function call.
140 masm.push(result);
141
142 // End of pushes reflected in EnterJITStackEntry, i.e. EnterJITStackEntry
143 // starts at this rsp.
144
145 // Remember number of bytes occupied by argument vector
146 masm.mov(reg_argc, r13);
147
148 // if we are constructing, that also needs to include newTarget
149 {
150 Label noNewTarget;
151 masm.branchTest32(Assembler::Zero, token,
152 Imm32(CalleeToken_FunctionConstructing), &noNewTarget);
153
154 masm.addq(Imm32(1), r13);
155
156 masm.bind(&noNewTarget);
157 }
158
159 masm.shll(Imm32(3), r13); // r13 = argc * sizeof(Value)
160 static_assert(sizeof(Value) == 1 << 3, "Constant is baked in assembly code");
161
162 // Guarantee stack alignment of Jit frames.
163 //
164 // This code compensates for the offset created by the copy of the vector of
165 // arguments, such that the jit frame will be aligned once the return
166 // address is pushed on the stack.
167 //
168 // In the computation of the offset, we omit the size of the JitFrameLayout
169 // which is pushed on the stack, as the JitFrameLayout size is a multiple of
170 // the JitStackAlignment.
171 masm.mov(rsp, r12);
172 masm.subq(r13, r12);
173 static_assert(
174 sizeof(JitFrameLayout) % JitStackAlignment == 0,
175 "No need to consider the JitFrameLayout for aligning the stack");
176 masm.andl(Imm32(JitStackAlignment - 1), r12);
177 masm.subq(r12, rsp);
178
179 /***************************************************************
180 Loop over argv vector, push arguments onto stack in reverse order
181 ***************************************************************/
182
183 // r13 still stores the number of bytes in the argument vector.
184 masm.addq(reg_argv, r13); // r13 points above last argument or newTarget
185
186 // while r13 > rdx, push arguments.
187 {
188 Label header, footer;
189 masm.bind(&header);
190
191 masm.cmpPtr(r13, reg_argv);
192 masm.j(AssemblerX86Shared::BelowOrEqual, &footer);
193
194 masm.subq(Imm32(8), r13);
195 masm.push(Operand(r13, 0));
196 masm.jmp(&header);
197
198 masm.bind(&footer);
199 }
200
201 // Load the number of actual arguments. |result| is used to store the
202 // actual number of arguments without adding an extra argument to the enter
203 // JIT.
204 masm.movq(result, reg_argc);
205 masm.unboxInt32(Operand(reg_argc, 0), reg_argc);
206
207 // Push the callee token.
208 masm.push(token);
209
210 // Push the descriptor.
211 masm.pushFrameDescriptorForJitCall(FrameType::CppToJSJit, reg_argc, reg_argc);
212
213 CodeLabel returnLabel;
214 Label oomReturnLabel;
215 {
216 // Handle Interpreter -> Baseline OSR.
217 AllocatableGeneralRegisterSet regs(GeneralRegisterSet::All());
218 MOZ_ASSERT(!regs.has(rbp));
219 regs.take(OsrFrameReg);
220 regs.take(reg_code);
221
222 Register scratch = regs.takeAny();
223
224 Label notOsr;
225 masm.branchTestPtr(Assembler::Zero, OsrFrameReg, OsrFrameReg, &notOsr);
226
227 Register numStackValues = regs.takeAny();
228 masm.movq(numStackValuesAddr, numStackValues);
229
230 // Push return address
231 masm.mov(&returnLabel, scratch);
232 masm.push(scratch);
233
234 // Frame prologue.
235 masm.push(rbp);
236 masm.mov(rsp, rbp);
237
238 // Reserve frame.
239 masm.subPtr(Imm32(BaselineFrame::Size()), rsp);
240
241 Register framePtrScratch = regs.takeAny();
242 masm.touchFrameValues(numStackValues, scratch, framePtrScratch);
243 masm.mov(rsp, framePtrScratch);
244
245 // Reserve space for locals and stack values.
246 Register valuesSize = regs.takeAny();
247 masm.mov(numStackValues, valuesSize);
248 masm.shll(Imm32(3), valuesSize);
249 masm.subPtr(valuesSize, rsp);
250
251 // Enter exit frame.
252 masm.pushFrameDescriptor(FrameType::BaselineJS);
253 masm.push(Imm32(0)); // Fake return address.
254 masm.push(FramePointer);
255 // No GC things to mark, push a bare token.
256 masm.loadJSContext(scratch);
257 masm.enterFakeExitFrame(scratch, scratch, ExitFrameType::Bare);
258
259 regs.add(valuesSize);
260
261 masm.push(reg_code);
262
263 using Fn = bool (*)(BaselineFrame* frame, InterpreterFrame* interpFrame,
264 uint32_t numStackValues);
265 masm.setupUnalignedABICall(scratch);
266 masm.passABIArg(framePtrScratch); // BaselineFrame
267 masm.passABIArg(OsrFrameReg); // InterpreterFrame
268 masm.passABIArg(numStackValues);
269 masm.callWithABI<Fn, jit::InitBaselineFrameForOsr>(
270 ABIType::General, CheckUnsafeCallWithABI::DontCheckHasExitFrame);
271
272 masm.pop(reg_code);
273
274 MOZ_ASSERT(reg_code != ReturnReg);
275
276 Label error;
277 masm.addPtr(Imm32(ExitFrameLayout::SizeWithFooter()), rsp);
278 masm.branchIfFalseBool(ReturnReg, &error);
279
280 // If OSR-ing, then emit instrumentation for setting lastProfilerFrame
281 // if profiler instrumentation is enabled.
282 {
283 Label skipProfilingInstrumentation;
284 AbsoluteAddress addressOfEnabled(
285 cx->runtime()->geckoProfiler().addressOfEnabled());
286 masm.branch32(Assembler::Equal, addressOfEnabled, Imm32(0),
287 &skipProfilingInstrumentation);
288 masm.profilerEnterFrame(rbp, scratch);
289 masm.bind(&skipProfilingInstrumentation);
290 }
291
292 masm.jump(reg_code);
293
294 // OOM: frame epilogue, load error value, discard return address and return.
295 masm.bind(&error);
296 masm.mov(rbp, rsp);
297 masm.pop(rbp);
298 masm.addPtr(Imm32(sizeof(uintptr_t)), rsp); // Return address.
299 masm.moveValue(MagicValue(JS_ION_ERROR), JSReturnOperand);
300 masm.jump(&oomReturnLabel);
301
302 masm.bind(&notOsr);
303 masm.movq(scopeChain, R1.scratchReg());
304 }
305
306 // The call will push the return address and frame pointer on the stack, thus
307 // we check that the stack would be aligned once the call is complete.
308 masm.assertStackAlignment(JitStackAlignment, 2 * sizeof(uintptr_t));
309
310 // Call function.
311 masm.callJitNoProfiler(reg_code);
312
313 {
314 // Interpreter -> Baseline OSR will return here.
315 masm.bind(&returnLabel);
316 masm.addCodeLabel(returnLabel);
317 masm.bind(&oomReturnLabel);
318 }
319
320 // Discard arguments and padding. Set rsp to the address of the
321 // EnterJITStackEntry on the stack.
322 masm.lea(Operand(rbp, EnterJITStackEntry::offsetFromFP()), rsp);
323
324 /*****************************************************************
325 Place return value where it belongs, pop all saved registers
326 *****************************************************************/
327 masm.pop(r12); // vp
328 masm.storeValue(JSReturnOperand, Operand(r12, 0));
329
330 // Restore non-volatile registers.
331 #if defined(_WIN64)
332 masm.vmovdqa(Operand(rsp, offsetof(EnterJITStackEntry::XMM, xmm6)), xmm6);
333 masm.vmovdqa(Operand(rsp, offsetof(EnterJITStackEntry::XMM, xmm7)), xmm7);
334 masm.vmovdqa(Operand(rsp, offsetof(EnterJITStackEntry::XMM, xmm8)), xmm8);
335 masm.vmovdqa(Operand(rsp, offsetof(EnterJITStackEntry::XMM, xmm9)), xmm9);
336 masm.vmovdqa(Operand(rsp, offsetof(EnterJITStackEntry::XMM, xmm10)), xmm10);
337 masm.vmovdqa(Operand(rsp, offsetof(EnterJITStackEntry::XMM, xmm11)), xmm11);
338 masm.vmovdqa(Operand(rsp, offsetof(EnterJITStackEntry::XMM, xmm12)), xmm12);
339 masm.vmovdqa(Operand(rsp, offsetof(EnterJITStackEntry::XMM, xmm13)), xmm13);
340 masm.vmovdqa(Operand(rsp, offsetof(EnterJITStackEntry::XMM, xmm14)), xmm14);
341 masm.vmovdqa(Operand(rsp, offsetof(EnterJITStackEntry::XMM, xmm15)), xmm15);
342
343 masm.addq(Imm32(sizeof(EnterJITStackEntry::XMM) + 8), rsp);
344
345 masm.pop(rsi);
346 masm.pop(rdi);
347 #endif
348 masm.pop(r15);
349 masm.pop(r14);
350 masm.pop(r13);
351 masm.pop(r12);
352 masm.pop(rbx);
353
354 // Restore frame pointer and return.
355 masm.pop(rbp);
356 masm.ret();
357 }
358
359 // static
360 mozilla::Maybe<::JS::ProfilingFrameIterator::RegisterState>
361 JitRuntime::getCppEntryRegisters(JitFrameLayout* frameStackAddress) {
362 if (frameStackAddress->prevType() != FrameType::CppToJSJit) {
363 // This is not a CppToJSJit frame, there are no C++ registers here.
364 return mozilla::Nothing{};
365 }
366
367 // Compute pointer to start of EnterJITStackEntry on the stack.
368 uint8_t* fp = frameStackAddress->callerFramePtr();
369 auto* enterJITStackEntry = reinterpret_cast<EnterJITStackEntry*>(
370 fp + EnterJITStackEntry::offsetFromFP());
371
372 // Extract native function call registers.
373 ::JS::ProfilingFrameIterator::RegisterState registerState;
374 registerState.fp = enterJITStackEntry->rbp;
375 registerState.pc = enterJITStackEntry->rip;
376 // sp should be inside the caller's frame, so set sp to the value of the stack
377 // pointer before the call to the EnterJit trampoline.
378 registerState.sp = &enterJITStackEntry->rip + 1;
379 // No lr in this world.
380 registerState.lr = nullptr;
381 return mozilla::Some(registerState);
382 }
383
384 // Push AllRegs in a way that is compatible with RegisterDump, regardless of
385 // what PushRegsInMask might do to reduce the set size.
386 static void DumpAllRegs(MacroAssembler& masm) {
387 #ifdef ENABLE_WASM_SIMD
388 masm.PushRegsInMask(AllRegs);
389 #else
390 // When SIMD isn't supported, PushRegsInMask reduces the set of float
391 // registers to be double-sized, while the RegisterDump expects each of
392 // the float registers to have the maximal possible size
393 // (Simd128DataSize). To work around this, we just spill the double
394 // registers by hand here, using the register dump offset directly.
395 for (GeneralRegisterBackwardIterator iter(AllRegs.gprs()); iter.more();
396 ++iter) {
397 masm.Push(*iter);
398 }
399
400 masm.reserveStack(sizeof(RegisterDump::FPUArray));
401 for (FloatRegisterBackwardIterator iter(AllRegs.fpus()); iter.more();
402 ++iter) {
403 FloatRegister reg = *iter;
404 Address spillAddress(StackPointer, reg.getRegisterDumpOffsetInBytes());
405 masm.storeDouble(reg, spillAddress);
406 }
407 #endif
408 }
409
410 void JitRuntime::generateInvalidator(MacroAssembler& masm, Label* bailoutTail) {
411 AutoCreatedBy acb(masm, "JitRuntime::generateInvalidator");
412
413 // See explanatory comment in x86's JitRuntime::generateInvalidator.
414
415 invalidatorOffset_ = startTrampolineCode(masm);
416
417 // Push registers such that we can access them from [base + code].
418 DumpAllRegs(masm);
419
420 masm.movq(rsp, rax); // Argument to jit::InvalidationBailout.
421
422 // Make space for InvalidationBailout's bailoutInfo outparam.
423 masm.reserveStack(sizeof(void*));
424 masm.movq(rsp, rbx);
425
426 using Fn = bool (*)(InvalidationBailoutStack* sp, BaselineBailoutInfo** info);
427 masm.setupUnalignedABICall(rdx);
428 masm.passABIArg(rax);
429 masm.passABIArg(rbx);
430 masm.callWithABI<Fn, InvalidationBailout>(
431 ABIType::General, CheckUnsafeCallWithABI::DontCheckOther);
432
433 masm.pop(r9); // Get the bailoutInfo outparam.
434
435 // Pop the machine state and the dead frame.
436 masm.moveToStackPtr(FramePointer);
437
438 // Jump to shared bailout tail. The BailoutInfo pointer has to be in r9.
439 masm.jmp(bailoutTail);
440 }
441
442 void JitRuntime::generateArgumentsRectifier(MacroAssembler& masm,
443 ArgumentsRectifierKind kind) {
444 // Do not erase the frame pointer in this function.
445
446 AutoCreatedBy acb(masm, "JitRuntime::generateArgumentsRectifier");
447
448 switch (kind) {
449 case ArgumentsRectifierKind::Normal:
450 argumentsRectifierOffset_ = startTrampolineCode(masm);
451 break;
452 case ArgumentsRectifierKind::TrialInlining:
453 trialInliningArgumentsRectifierOffset_ = startTrampolineCode(masm);
454 break;
455 }
456
457 // Caller:
458 // [arg2] [arg1] [this] [[argc] [callee] [descr] [raddr]] <- rsp
459
460 // Frame prologue.
461 //
462 // NOTE: if this changes, fix the Baseline bailout code too!
463 // See BaselineStackBuilder::calculatePrevFramePtr and
464 // BaselineStackBuilder::buildRectifierFrame (in BaselineBailouts.cpp).
465 masm.push(FramePointer);
466 masm.movq(rsp, FramePointer);
467
468 // Load argc.
469 masm.loadNumActualArgs(FramePointer, r8);
470
471 // Load |nformals| into %rcx.
472 masm.loadPtr(Address(rbp, RectifierFrameLayout::offsetOfCalleeToken()), rax);
473 masm.mov(rax, rcx);
474 masm.andq(Imm32(uint32_t(CalleeTokenMask)), rcx);
475 masm.loadFunctionArgCount(rcx, rcx);
476
477 // Stash another copy in r11, since we are going to do destructive operations
478 // on rcx
479 masm.mov(rcx, r11);
480
481 static_assert(
482 CalleeToken_FunctionConstructing == 1,
483 "Ensure that we can use the constructing bit to count the value");
484 masm.mov(rax, rdx);
485 masm.andq(Imm32(uint32_t(CalleeToken_FunctionConstructing)), rdx);
486
487 // Including |this|, and |new.target|, there are (|nformals| + 1 +
488 // isConstructing) arguments to push to the stack. Then we push a
489 // JitFrameLayout. We compute the padding expressed in the number of extra
490 // |undefined| values to push on the stack.
491 static_assert(
492 sizeof(JitFrameLayout) % JitStackAlignment == 0,
493 "No need to consider the JitFrameLayout for aligning the stack");
494 static_assert(
495 JitStackAlignment % sizeof(Value) == 0,
496 "Ensure that we can pad the stack by pushing extra UndefinedValue");
497 static_assert(IsPowerOfTwo(JitStackValueAlignment),
498 "must have power of two for masm.andl to do its job");
499
500 masm.addl(
501 Imm32(JitStackValueAlignment - 1 /* for padding */ + 1 /* for |this| */),
502 rcx);
503 masm.addl(rdx, rcx);
504 masm.andl(Imm32(~(JitStackValueAlignment - 1)), rcx);
505
506 // Load the number of |undefined|s to push into %rcx. Subtract 1 for |this|.
507 masm.subl(r8, rcx);
508 masm.subl(Imm32(1), rcx);
509
510 // Caller:
511 // [arg2] [arg1] [this] [ [argc] [callee] [descr] [raddr] ] <- rsp
512 // '--- #r8 ---'
513 //
514 // Rectifier frame:
515 // [rbp'] [undef] [undef] [undef] [arg2] [arg1] [this] [ [argc] [callee]
516 // [descr] [raddr] ]
517 // '------- #rcx --------' '--- #r8 ---'
518
519 // Copy the number of actual arguments into rdx.
520 masm.mov(r8, rdx);
521
522 masm.moveValue(UndefinedValue(), ValueOperand(r10));
523
524 // Push undefined. (including the padding)
525 {
526 Label undefLoopTop;
527 masm.bind(&undefLoopTop);
528
529 masm.push(r10);
530 masm.subl(Imm32(1), rcx);
531 masm.j(Assembler::NonZero, &undefLoopTop);
532 }
533
534 // Get the topmost argument.
535 static_assert(sizeof(Value) == 8, "TimesEight is used to skip arguments");
536
537 // Get the topmost argument.
538 BaseIndex b(FramePointer, r8, TimesEight, sizeof(RectifierFrameLayout));
539 masm.lea(Operand(b), rcx);
540
541 // Push arguments, |nargs| + 1 times (to include |this|).
542 masm.addl(Imm32(1), r8);
543 {
544 Label copyLoopTop;
545
546 masm.bind(&copyLoopTop);
547 masm.push(Operand(rcx, 0x0));
548 masm.subq(Imm32(sizeof(Value)), rcx);
549 masm.subl(Imm32(1), r8);
550 masm.j(Assembler::NonZero, &copyLoopTop);
551 }
552
553 // if constructing, copy newTarget
554 {
555 Label notConstructing;
556
557 masm.branchTest32(Assembler::Zero, rax,
558 Imm32(CalleeToken_FunctionConstructing),
559 &notConstructing);
560
561 // thisFrame[numFormals] = prevFrame[argc]
562 ValueOperand newTarget(r10);
563
564 // Load vp[argc]. Add sizeof(Value) for |this|.
565 BaseIndex newTargetSrc(FramePointer, rdx, TimesEight,
566 sizeof(RectifierFrameLayout) + sizeof(Value));
567 masm.loadValue(newTargetSrc, newTarget);
568
569 // Again, 1 for |this|
570 BaseIndex newTargetDest(rsp, r11, TimesEight, sizeof(Value));
571 masm.storeValue(newTarget, newTargetDest);
572
573 masm.bind(&notConstructing);
574 }
575
576 // Caller:
577 // [arg2] [arg1] [this] [ [argc] [callee] [descr] [raddr] ]
578 //
579 //
580 // Rectifier frame:
581 // [rbp'] <- rbp [undef] [undef] [undef] [arg2] [arg1] [this] <- rsp [ [argc]
582 // [callee] [descr] [raddr] ]
583 //
584
585 // Construct JitFrameLayout.
586 masm.push(rax); // callee token
587 masm.pushFrameDescriptorForJitCall(FrameType::Rectifier, rdx, rdx);
588
589 // Call the target function.
590 masm.andq(Imm32(uint32_t(CalleeTokenMask)), rax);
591 switch (kind) {
592 case ArgumentsRectifierKind::Normal:
593 masm.loadJitCodeRaw(rax, rax);
594 argumentsRectifierReturnOffset_ = masm.callJitNoProfiler(rax);
595 break;
596 case ArgumentsRectifierKind::TrialInlining:
597 Label noBaselineScript, done;
598 masm.loadBaselineJitCodeRaw(rax, rbx, &noBaselineScript);
599 masm.callJitNoProfiler(rbx);
600 masm.jump(&done);
601
602 // See BaselineCacheIRCompiler::emitCallInlinedFunction.
603 masm.bind(&noBaselineScript);
604 masm.loadJitCodeRaw(rax, rax);
605 masm.callJitNoProfiler(rax);
606 masm.bind(&done);
607 break;
608 }
609
610 masm.mov(FramePointer, StackPointer);
611 masm.pop(FramePointer);
612 masm.ret();
613 }
614
615 static void PushBailoutFrame(MacroAssembler& masm, Register spArg) {
616 // Push registers such that we can access them from [base + code].
617 DumpAllRegs(masm);
618
619 // Get the stack pointer into a register, pre-alignment.
620 masm.movq(rsp, spArg);
621 }
622
623 static void GenerateBailoutThunk(MacroAssembler& masm, Label* bailoutTail) {
624 PushBailoutFrame(masm, r8);
625
626 // Make space for Bailout's bailoutInfo outparam.
627 masm.reserveStack(sizeof(void*));
628 masm.movq(rsp, r9);
629
630 // Call the bailout function.
631 using Fn = bool (*)(BailoutStack* sp, BaselineBailoutInfo** info);
632 masm.setupUnalignedABICall(rax);
633 masm.passABIArg(r8);
634 masm.passABIArg(r9);
635 masm.callWithABI<Fn, Bailout>(ABIType::General,
636 CheckUnsafeCallWithABI::DontCheckOther);
637
638 masm.pop(r9); // Get the bailoutInfo outparam.
639
640 // Remove both the bailout frame and the topmost Ion frame's stack.
641 masm.moveToStackPtr(FramePointer);
642
643 // Jump to shared bailout tail. The BailoutInfo pointer has to be in r9.
644 masm.jmp(bailoutTail);
645 }
646
647 void JitRuntime::generateBailoutHandler(MacroAssembler& masm,
648 Label* bailoutTail) {
649 AutoCreatedBy acb(masm, "JitRuntime::generateBailoutHandler");
650
651 bailoutHandlerOffset_ = startTrampolineCode(masm);
652
653 GenerateBailoutThunk(masm, bailoutTail);
654 }
655
656 bool JitRuntime::generateVMWrapper(JSContext* cx, MacroAssembler& masm,
657 VMFunctionId id, const VMFunctionData& f,
658 DynFn nativeFun, uint32_t* wrapperOffset) {
659 AutoCreatedBy acb(masm, "JitRuntime::generateVMWrapper");
660
661 *wrapperOffset = startTrampolineCode(masm);
662
663 // Avoid conflicts with argument registers while discarding the result after
664 // the function call.
665 AllocatableGeneralRegisterSet regs(Register::Codes::WrapperMask);
666
667 static_assert(
668 (Register::Codes::VolatileMask & ~Register::Codes::WrapperMask) == 0,
669 "Wrapper register set must be a superset of Volatile register set");
670
671 // The context is the first argument.
672 Register cxreg = IntArgReg0;
673 regs.take(cxreg);
674
675 // Stack is:
676 // ... frame ...
677 // +12 [args]
678 // +8 descriptor
679 // +0 returnAddress
680 //
681 // Push the frame pointer to finish the exit frame, then link it up.
682 masm.Push(FramePointer);
683 masm.moveStackPtrTo(FramePointer);
684 masm.loadJSContext(cxreg);
685 masm.enterExitFrame(cxreg, regs.getAny(), id);
686
687 // Reserve space for the outparameter.
688 masm.reserveVMFunctionOutParamSpace(f);
689
690 masm.setupUnalignedABICallDontSaveRestoreSP();
691 masm.passABIArg(cxreg);
692
693 size_t argDisp = ExitFrameLayout::Size();
694
695 // Copy arguments.
696 for (uint32_t explicitArg = 0; explicitArg < f.explicitArgs; explicitArg++) {
697 switch (f.argProperties(explicitArg)) {
698 case VMFunctionData::WordByValue:
699 if (f.argPassedInFloatReg(explicitArg)) {
700 masm.passABIArg(MoveOperand(FramePointer, argDisp), ABIType::Float64);
701 } else {
702 masm.passABIArg(MoveOperand(FramePointer, argDisp), ABIType::General);
703 }
704 argDisp += sizeof(void*);
705 break;
706 case VMFunctionData::WordByRef:
707 masm.passABIArg(MoveOperand(FramePointer, argDisp,
708 MoveOperand::Kind::EffectiveAddress),
709 ABIType::General);
710 argDisp += sizeof(void*);
711 break;
712 case VMFunctionData::DoubleByValue:
713 case VMFunctionData::DoubleByRef:
714 MOZ_CRASH("NYI: x64 callVM should not be used with 128bits values.");
715 }
716 }
717
718 // Copy the implicit outparam, if any.
719 const int32_t outParamOffset =
720 -int32_t(ExitFooterFrame::Size()) - f.sizeOfOutParamStackSlot();
721 if (f.outParam != Type_Void) {
722 masm.passABIArg(MoveOperand(FramePointer, outParamOffset,
723 MoveOperand::Kind::EffectiveAddress),
724 ABIType::General);
725 }
726
727 masm.callWithABI(nativeFun, ABIType::General,
728 CheckUnsafeCallWithABI::DontCheckHasExitFrame);
729
730 // Test for failure.
731 switch (f.failType()) {
732 case Type_Cell:
733 masm.branchTestPtr(Assembler::Zero, rax, rax, masm.failureLabel());
734 break;
735 case Type_Bool:
736 masm.testb(rax, rax);
737 masm.j(Assembler::Zero, masm.failureLabel());
738 break;
739 case Type_Void:
740 break;
741 default:
742 MOZ_CRASH("unknown failure kind");
743 }
744
745 // Load the outparam.
746 masm.loadVMFunctionOutParam(f, Address(FramePointer, outParamOffset));
747
748 // Until C++ code is instrumented against Spectre, prevent speculative
749 // execution from returning any private data.
750 if (f.returnsData() && JitOptions.spectreJitToCxxCalls) {
751 masm.speculationBarrier();
752 }
753
754 // Pop frame and restore frame pointer.
755 masm.moveToStackPtr(FramePointer);
756 masm.pop(FramePointer);
757
758 // Return. Subtract sizeof(void*) for the frame pointer.
759 masm.retn(Imm32(sizeof(ExitFrameLayout) - sizeof(void*) +
760 f.explicitStackSlots() * sizeof(void*) +
761 f.extraValuesToPop * sizeof(Value)));
762
763 return true;
764 }
765
766 uint32_t JitRuntime::generatePreBarrier(JSContext* cx, MacroAssembler& masm,
767 MIRType type) {
768 AutoCreatedBy acb(masm, "JitRuntime::generatePreBarrier");
769
770 uint32_t offset = startTrampolineCode(masm);
771
772 static_assert(PreBarrierReg == rdx);
773 Register temp1 = rax;
774 Register temp2 = rbx;
775 Register temp3 = rcx;
776 masm.push(temp1);
777 masm.push(temp2);
778 masm.push(temp3);
779
780 Label noBarrier;
781 masm.emitPreBarrierFastPath(cx->runtime(), type, temp1, temp2, temp3,
782 &noBarrier);
783
784 // Call into C++ to mark this GC thing.
785 masm.pop(temp3);
786 masm.pop(temp2);
787 masm.pop(temp1);
788
789 LiveRegisterSet regs =
790 LiveRegisterSet(GeneralRegisterSet(Registers::VolatileMask),
791 FloatRegisterSet(FloatRegisters::VolatileMask));
792 masm.PushRegsInMask(regs);
793
794 masm.mov(ImmPtr(cx->runtime()), rcx);
795
796 masm.setupUnalignedABICall(rax);
797 masm.passABIArg(rcx);
798 masm.passABIArg(rdx);
799 masm.callWithABI(JitPreWriteBarrier(type));
800
801 masm.PopRegsInMask(regs);
802 masm.ret();
803
804 masm.bind(&noBarrier);
805 masm.pop(temp3);
806 masm.pop(temp2);
807 masm.pop(temp1);
808 masm.ret();
809
810 return offset;
811 }
812
813 void JitRuntime::generateBailoutTailStub(MacroAssembler& masm,
814 Label* bailoutTail) {
815 AutoCreatedBy acb(masm, "JitRuntime::generateBailoutTailStub");
816
817 masm.bind(bailoutTail);
818 masm.generateBailoutTail(rdx, r9);
819 }