Debian Codegraph @

js/src/jit/arm/Trampoline-arm.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/arm/SharedICHelpers-arm.h"
8 #include "jit/Bailouts.h"
9 #include "jit/BaselineFrame.h"
10 #include "jit/CalleeToken.h"
11 #include "jit/JitFrames.h"
12 #include "jit/JitRuntime.h"
13 #include "jit/JitSpewer.h"
14 #include "jit/PerfSpewer.h"
15 #include "jit/VMFunctions.h"
16 #include "vm/JitActivation.h" // js::jit::JitActivation
17 #include "vm/JSContext.h"
18 #include "vm/Realm.h"
19
20 #include "jit/MacroAssembler-inl.h"
21
22 using namespace js;
23 using namespace js::jit;
24
25 static const FloatRegisterSet NonVolatileFloatRegs = FloatRegisterSet(
26 (1ULL << FloatRegisters::d8) | (1ULL << FloatRegisters::d9) |
27 (1ULL << FloatRegisters::d10) | (1ULL << FloatRegisters::d11) |
28 (1ULL << FloatRegisters::d12) | (1ULL << FloatRegisters::d13) |
29 (1ULL << FloatRegisters::d14) | (1ULL << FloatRegisters::d15));
30
31 static void GenerateReturn(MacroAssembler& masm, int returnCode) {
32 // Restore non-volatile floating point registers.
33 masm.transferMultipleByRuns(NonVolatileFloatRegs, IsLoad, StackPointer, IA);
34
35 // Get rid of padding word.
36 masm.addPtr(Imm32(sizeof(void*)), sp);
37
38 // Set up return value
39 masm.ma_mov(Imm32(returnCode), r0);
40
41 // Pop and return
42 masm.startDataTransferM(IsLoad, sp, IA, WriteBack);
43 masm.transferReg(r4);
44 masm.transferReg(r5);
45 masm.transferReg(r6);
46 masm.transferReg(r7);
47 masm.transferReg(r8);
48 masm.transferReg(r9);
49 masm.transferReg(r10);
50 masm.transferReg(r11);
51 // r12 isn't saved, so it shouldn't be restored.
52 masm.transferReg(pc);
53 masm.finishDataTransfer();
54 masm.flushBuffer();
55 }
56
57 struct EnterJITStack {
58 double d8;
59 double d9;
60 double d10;
61 double d11;
62 double d12;
63 double d13;
64 double d14;
65 double d15;
66
67 // Padding.
68 void* padding;
69
70 // Non-volatile registers.
71 void* r4;
72 void* r5;
73 void* r6;
74 void* r7;
75 void* r8;
76 void* r9;
77 void* r10;
78 void* r11;
79 // The abi does not expect r12 (ip) to be preserved
80 void* lr;
81
82 // Arguments.
83 // code == r0
84 // argc == r1
85 // argv == r2
86 // frame == r3
87 CalleeToken token;
88 JSObject* scopeChain;
89 size_t numStackValues;
90 Value* vp;
91 };
92
93 /*
94 * This method generates a trampoline for a c++ function with the following
95 * signature:
96 * void enter(void* code, int argc, Value* argv, InterpreterFrame* fp,
97 * CalleeToken calleeToken, JSObject* scopeChain, Value* vp)
98 * ...using standard EABI calling convention
99 */
100 void JitRuntime::generateEnterJIT(JSContext* cx, MacroAssembler& masm) {
101 AutoCreatedBy acb(masm, "JitRuntime::generateEnterJIT");
102
103 enterJITOffset_ = startTrampolineCode(masm);
104
105 const Address slot_token(sp, offsetof(EnterJITStack, token));
106 const Address slot_vp(sp, offsetof(EnterJITStack, vp));
107
108 static_assert(OsrFrameReg == r3);
109
110 Assembler* aasm = &masm;
111
112 // Save non-volatile registers. These must be saved by the trampoline,
113 // rather than the JIT'd code, because they are scanned by the conservative
114 // scanner.
115 masm.startDataTransferM(IsStore, sp, DB, WriteBack);
116 masm.transferReg(r4); // [sp,0]
117 masm.transferReg(r5); // [sp,4]
118 masm.transferReg(r6); // [sp,8]
119 masm.transferReg(r7); // [sp,12]
120 masm.transferReg(r8); // [sp,16]
121 masm.transferReg(r9); // [sp,20]
122 masm.transferReg(r10); // [sp,24]
123 masm.transferReg(r11); // [sp,28]
124 // The abi does not expect r12 (ip) to be preserved
125 masm.transferReg(lr); // [sp,32]
126 // The 5th argument is located at [sp, 36]
127 masm.finishDataTransfer();
128
129 // Add padding word.
130 masm.subPtr(Imm32(sizeof(void*)), sp);
131
132 // Push the float registers.
133 masm.transferMultipleByRuns(NonVolatileFloatRegs, IsStore, sp, DB);
134
135 // Load calleeToken into r9.
136 masm.loadPtr(slot_token, r9);
137
138 // Save stack pointer.
139 masm.movePtr(sp, r11);
140
141 // Load the number of actual arguments into r10.
142 masm.loadPtr(slot_vp, r10);
143 masm.unboxInt32(Address(r10, 0), r10);
144
145 {
146 Label noNewTarget;
147 masm.branchTest32(Assembler::Zero, r9,
148 Imm32(CalleeToken_FunctionConstructing), &noNewTarget);
149
150 masm.add32(Imm32(1), r1);
151
152 masm.bind(&noNewTarget);
153 }
154
155 // Guarantee stack alignment of Jit frames.
156 //
157 // This code moves the stack pointer to the location where it should be when
158 // we enter the Jit frame. It moves the stack pointer such that we have
159 // enough space reserved for pushing the arguments, and the JitFrameLayout.
160 // The stack pointer is also aligned on the alignment expected by the Jit
161 // frames.
162 //
163 // At the end the register r4, is a pointer to the stack where the first
164 // argument is expected by the Jit frame.
165 //
166 aasm->as_sub(r4, sp, O2RegImmShift(r1, LSL, 3)); // r4 = sp - argc*8
167 aasm->as_bic(r4, r4, Imm8(JitStackAlignment - 1));
168 // r4 is now the aligned on the bottom of the list of arguments.
169 static_assert(
170 sizeof(JitFrameLayout) % JitStackAlignment == 0,
171 "No need to consider the JitFrameLayout for aligning the stack");
172 // sp' = ~(JitStackAlignment - 1) & (sp - argc * sizeof(Value))
173 masm.movePtr(r4, sp);
174
175 // Get a copy of the number of args to use as a decrement counter, also set
176 // the zero condition code.
177 aasm->as_mov(r5, O2Reg(r1), SetCC);
178
179 // Loop over arguments, copying them from an unknown buffer onto the Ion
180 // stack so they can be accessed from JIT'ed code.
181 {
182 Label header, footer;
183 // If there aren't any arguments, don't do anything.
184 aasm->as_b(&footer, Assembler::Zero);
185 // Get the top of the loop.
186 masm.bind(&header);
187 aasm->as_sub(r5, r5, Imm8(1), SetCC);
188 // We could be more awesome, and unroll this, using a loadm
189 // (particularly since the offset is effectively 0) but that seems more
190 // error prone, and complex.
191 // BIG FAT WARNING: this loads both r6 and r7.
192 aasm->as_extdtr(IsLoad, 64, true, PostIndex, r6,
193 EDtrAddr(r2, EDtrOffImm(8)));
194 aasm->as_extdtr(IsStore, 64, true, PostIndex, r6,
195 EDtrAddr(r4, EDtrOffImm(8)));
196 aasm->as_b(&header, Assembler::NonZero);
197 masm.bind(&footer);
198 }
199
200 // Push the callee token.
201 masm.push(r9);
202
203 // Push the frame descriptor.
204 masm.pushFrameDescriptorForJitCall(FrameType::CppToJSJit, r10, r10);
205
206 Label returnLabel;
207 {
208 // Handle Interpreter -> Baseline OSR.
209 AllocatableGeneralRegisterSet regs(GeneralRegisterSet::All());
210 MOZ_ASSERT(!regs.has(r11));
211 regs.take(OsrFrameReg);
212 regs.take(r0); // jitcode
213 MOZ_ASSERT(!regs.has(ReturnReg), "ReturnReg matches r0");
214
215 const Address slot_numStackValues(r11,
216 offsetof(EnterJITStack, numStackValues));
217
218 Label notOsr;
219 masm.branchTestPtr(Assembler::Zero, OsrFrameReg, OsrFrameReg, &notOsr);
220
221 Register scratch = regs.takeAny();
222
223 Register numStackValues = regs.takeAny();
224 masm.load32(slot_numStackValues, numStackValues);
225
226 // Write return address. On ARM, CodeLabel is only used for tableswitch,
227 // so we can't use it here to get the return address. Instead, we use pc
228 // + a fixed offset to a jump to returnLabel. The pc register holds pc +
229 // 8, so we add the size of 2 instructions to skip the instructions
230 // emitted by push and jump(&skipJump).
231 {
232 AutoForbidPoolsAndNops afp(&masm, 5);
233 Label skipJump;
234 masm.mov(pc, scratch);
235 masm.addPtr(Imm32(2 * sizeof(uint32_t)), scratch);
236 masm.push(scratch);
237 masm.jump(&skipJump);
238 masm.jump(&returnLabel);
239 masm.bind(&skipJump);
240 }
241
242 // Frame prologue.
243 masm.push(FramePointer);
244 masm.mov(sp, FramePointer);
245
246 // Reserve frame.
247 masm.subPtr(Imm32(BaselineFrame::Size()), sp);
248
249 Register framePtrScratch = regs.takeAny();
250 masm.touchFrameValues(numStackValues, scratch, framePtrScratch);
251 masm.mov(sp, framePtrScratch);
252
253 // Reserve space for locals and stack values.
254 masm.ma_lsl(Imm32(3), numStackValues, scratch);
255 masm.ma_sub(sp, scratch, sp);
256
257 // Enter exit frame.
258 masm.pushFrameDescriptor(FrameType::BaselineJS);
259 masm.push(Imm32(0)); // Fake return address.
260 masm.push(FramePointer);
261 // No GC things to mark on the stack, push a bare token.
262 masm.loadJSContext(scratch);
263 masm.enterFakeExitFrame(scratch, scratch, ExitFrameType::Bare);
264
265 masm.push(r0); // jitcode
266
267 using Fn = bool (*)(BaselineFrame* frame, InterpreterFrame* interpFrame,
268 uint32_t numStackValues);
269 masm.setupUnalignedABICall(scratch);
270 masm.passABIArg(framePtrScratch); // BaselineFrame
271 masm.passABIArg(OsrFrameReg); // InterpreterFrame
272 masm.passABIArg(numStackValues);
273 masm.callWithABI<Fn, jit::InitBaselineFrameForOsr>(
274 ABIType::General, CheckUnsafeCallWithABI::DontCheckHasExitFrame);
275
276 Register jitcode = regs.takeAny();
277 masm.pop(jitcode);
278
279 MOZ_ASSERT(jitcode != ReturnReg);
280
281 Label error;
282 masm.addPtr(Imm32(ExitFrameLayout::SizeWithFooter()), sp);
283 masm.branchIfFalseBool(ReturnReg, &error);
284
285 // If OSR-ing, then emit instrumentation for setting lastProfilerFrame
286 // if profiler instrumentation is enabled.
287 {
288 Label skipProfilingInstrumentation;
289 AbsoluteAddress addressOfEnabled(
290 cx->runtime()->geckoProfiler().addressOfEnabled());
291 masm.branch32(Assembler::Equal, addressOfEnabled, Imm32(0),
292 &skipProfilingInstrumentation);
293 masm.profilerEnterFrame(FramePointer, scratch);
294 masm.bind(&skipProfilingInstrumentation);
295 }
296
297 masm.jump(jitcode);
298
299 // OOM: frame epilogue, load error value, discard return address and return.
300 masm.bind(&error);
301 masm.mov(FramePointer, sp);
302 masm.pop(FramePointer);
303 masm.addPtr(Imm32(sizeof(uintptr_t)), sp); // Return address.
304 masm.moveValue(MagicValue(JS_ION_ERROR), JSReturnOperand);
305 masm.jump(&returnLabel);
306
307 masm.bind(&notOsr);
308 // Load the scope chain in R1.
309 MOZ_ASSERT(R1.scratchReg() != r0);
310 masm.loadPtr(Address(r11, offsetof(EnterJITStack, scopeChain)),
311 R1.scratchReg());
312 }
313
314 // The callee will push the return address and frame pointer on the stack,
315 // thus we check that the stack would be aligned once the call is complete.
316 masm.assertStackAlignment(JitStackAlignment, 2 * sizeof(uintptr_t));
317
318 // Call the function.
319 masm.callJitNoProfiler(r0);
320
321 // Interpreter -> Baseline OSR will return here.
322 masm.bind(&returnLabel);
323
324 // Discard arguments and padding. Set sp to the address of the EnterJITStack
325 // on the stack.
326 masm.mov(r11, sp);
327
328 // Store the returned value into the slot_vp
329 masm.loadPtr(slot_vp, r5);
330 masm.storeValue(JSReturnOperand, Address(r5, 0));
331
332 // Restore non-volatile registers and return.
333 GenerateReturn(masm, true);
334 }
335
336 // static
337 mozilla::Maybe<::JS::ProfilingFrameIterator::RegisterState>
338 JitRuntime::getCppEntryRegisters(JitFrameLayout* frameStackAddress) {
339 // Not supported, or not implemented yet.
340 // TODO: Implement along with the corresponding stack-walker changes, in
341 // coordination with the Gecko Profiler, see bug 1635987 and follow-ups.
342 return mozilla::Nothing{};
343 }
344
345 void JitRuntime::generateInvalidator(MacroAssembler& masm, Label* bailoutTail) {
346 // See large comment in x86's JitRuntime::generateInvalidator.
347
348 AutoCreatedBy acb(masm, "JitRuntime::generateInvalidator");
349
350 invalidatorOffset_ = startTrampolineCode(masm);
351
352 // At this point, one of two things has happened:
353 // 1) Execution has just returned from C code, which left the stack aligned
354 // 2) Execution has just returned from Ion code, which left the stack
355 // unaligned. The old return address should not matter, but we still want the
356 // stack to be aligned, and there is no good reason to automatically align it
357 // with a call to setupUnalignedABICall.
358 masm.as_bic(sp, sp, Imm8(7));
359 masm.startDataTransferM(IsStore, sp, DB, WriteBack);
360 // We don't have to push everything, but this is likely easier.
361 // Setting regs_.
362 for (uint32_t i = 0; i < Registers::Total; i++) {
363 masm.transferReg(Register::FromCode(i));
364 }
365 masm.finishDataTransfer();
366
367 // Since our datastructures for stack inspection are compile-time fixed,
368 // if there are only 16 double registers, then we need to reserve
369 // space on the stack for the missing 16.
370 if (FloatRegisters::ActualTotalPhys() != FloatRegisters::TotalPhys) {
371 ScratchRegisterScope scratch(masm);
372 int missingRegs =
373 FloatRegisters::TotalPhys - FloatRegisters::ActualTotalPhys();
374 masm.ma_sub(Imm32(missingRegs * sizeof(double)), sp, scratch);
375 }
376
377 masm.startFloatTransferM(IsStore, sp, DB, WriteBack);
378 for (uint32_t i = 0; i < FloatRegisters::ActualTotalPhys(); i++) {
379 masm.transferFloatReg(FloatRegister(i, FloatRegister::Double));
380 }
381 masm.finishFloatTransfer();
382
383 masm.ma_mov(sp, r0);
384 // Reserve 8 bytes for the outparam to ensure alignment for
385 // setupAlignedABICall.
386 masm.reserveStack(sizeof(void*) * 2);
387 masm.mov(sp, r1);
388 using Fn = bool (*)(InvalidationBailoutStack* sp, BaselineBailoutInfo** info);
389 masm.setupAlignedABICall();
390 masm.passABIArg(r0);
391 masm.passABIArg(r1);
392 masm.callWithABI<Fn, InvalidationBailout>(
393 ABIType::General, CheckUnsafeCallWithABI::DontCheckOther);
394
395 masm.pop(r2); // Get bailoutInfo outparam.
396
397 // Pop the machine state and the dead frame.
398 masm.moveToStackPtr(FramePointer);
399
400 // Jump to shared bailout tail. The BailoutInfo pointer has to be in r2.
401 masm.jump(bailoutTail);
402 }
403
404 void JitRuntime::generateArgumentsRectifier(MacroAssembler& masm,
405 ArgumentsRectifierKind kind) {
406 AutoCreatedBy acb(masm, "JitRuntime::generateArgumentsRectifier");
407
408 switch (kind) {
409 case ArgumentsRectifierKind::Normal:
410 argumentsRectifierOffset_ = startTrampolineCode(masm);
411 break;
412 case ArgumentsRectifierKind::TrialInlining:
413 trialInliningArgumentsRectifierOffset_ = startTrampolineCode(masm);
414 break;
415 }
416 masm.pushReturnAddress();
417
418 // Frame prologue.
419 //
420 // NOTE: if this changes, fix the Baseline bailout code too!
421 // See BaselineStackBuilder::calculatePrevFramePtr and
422 // BaselineStackBuilder::buildRectifierFrame (in BaselineBailouts.cpp).
423 masm.push(FramePointer);
424 masm.mov(StackPointer, FramePointer);
425
426 static_assert(JitStackAlignment == sizeof(Value));
427
428 // Copy number of actual arguments into r0 and r8.
429 masm.loadNumActualArgs(FramePointer, r0);
430 masm.mov(r0, r8);
431
432 // Load the number of |undefined|s to push into r6.
433 masm.loadPtr(
434 Address(FramePointer, RectifierFrameLayout::offsetOfCalleeToken()), r1);
435 {
436 ScratchRegisterScope scratch(masm);
437 masm.ma_and(Imm32(CalleeTokenMask), r1, r6, scratch);
438 }
439 masm.loadFunctionArgCount(r6, r6);
440
441 masm.ma_sub(r6, r8, r2);
442
443 // Get the topmost argument.
444 {
445 ScratchRegisterScope scratch(masm);
446 masm.ma_alu(sp, lsl(r8, 3), r3, OpAdd); // r3 <- sp + nargs * 8
447 masm.ma_add(r3, Imm32(sizeof(RectifierFrameLayout)), r3, scratch);
448 }
449
450 {
451 Label notConstructing;
452
453 masm.branchTest32(Assembler::Zero, r1,
454 Imm32(CalleeToken_FunctionConstructing),
455 &notConstructing);
456
457 // Add sizeof(Value) to overcome |this|
458 masm.as_extdtr(IsLoad, 64, true, Offset, r4, EDtrAddr(r3, EDtrOffImm(8)));
459 masm.as_extdtr(IsStore, 64, true, PreIndex, r4,
460 EDtrAddr(sp, EDtrOffImm(-8)));
461
462 masm.bind(&notConstructing);
463 }
464
465 // Push undefined.
466 masm.moveValue(UndefinedValue(), ValueOperand(r5, r4));
467 {
468 Label undefLoopTop;
469 masm.bind(&undefLoopTop);
470 masm.as_extdtr(IsStore, 64, true, PreIndex, r4,
471 EDtrAddr(sp, EDtrOffImm(-8)));
472 masm.as_sub(r2, r2, Imm8(1), SetCC);
473
474 masm.ma_b(&undefLoopTop, Assembler::NonZero);
475 }
476
477 // Push arguments, |nargs| + 1 times (to include |this|).
478 {
479 Label copyLoopTop;
480 masm.bind(&copyLoopTop);
481 masm.as_extdtr(IsLoad, 64, true, PostIndex, r4,
482 EDtrAddr(r3, EDtrOffImm(-8)));
483 masm.as_extdtr(IsStore, 64, true, PreIndex, r4,
484 EDtrAddr(sp, EDtrOffImm(-8)));
485
486 masm.as_sub(r8, r8, Imm8(1), SetCC);
487 masm.ma_b(&copyLoopTop, Assembler::NotSigned);
488 }
489
490 // Construct JitFrameLayout.
491 masm.ma_push(r1); // callee token
492 masm.pushFrameDescriptorForJitCall(FrameType::Rectifier, r0, r0);
493
494 // Call the target function.
495 masm.andPtr(Imm32(CalleeTokenMask), r1);
496 switch (kind) {
497 case ArgumentsRectifierKind::Normal:
498 masm.loadJitCodeRaw(r1, r3);
499 argumentsRectifierReturnOffset_ = masm.callJitNoProfiler(r3);
500 break;
501 case ArgumentsRectifierKind::TrialInlining:
502 Label noBaselineScript, done;
503 masm.loadBaselineJitCodeRaw(r1, r3, &noBaselineScript);
504 masm.callJitNoProfiler(r3);
505 masm.jump(&done);
506
507 // See BaselineCacheIRCompiler::emitCallInlinedFunction.
508 masm.bind(&noBaselineScript);
509 masm.loadJitCodeRaw(r1, r3);
510 masm.callJitNoProfiler(r3);
511 masm.bind(&done);
512 break;
513 }
514
515 masm.mov(FramePointer, StackPointer);
516 masm.pop(FramePointer);
517 masm.ret();
518 }
519
520 static void PushBailoutFrame(MacroAssembler& masm, Register spArg) {
521 #ifdef ENABLE_WASM_SIMD
522 # error "Needs more careful logic if SIMD is enabled"
523 #endif
524
525 // STEP 1a: Save our register sets to the stack so Bailout() can read
526 // everything.
527 // sp % 8 == 0
528
529 masm.startDataTransferM(IsStore, sp, DB, WriteBack);
530 // We don't have to push everything, but this is likely easier.
531 // Setting regs_.
532 for (uint32_t i = 0; i < Registers::Total; i++) {
533 masm.transferReg(Register::FromCode(i));
534 }
535 masm.finishDataTransfer();
536
537 ScratchRegisterScope scratch(masm);
538
539 // Since our datastructures for stack inspection are compile-time fixed,
540 // if there are only 16 double registers, then we need to reserve
541 // space on the stack for the missing 16.
542 if (FloatRegisters::ActualTotalPhys() != FloatRegisters::TotalPhys) {
543 int missingRegs =
544 FloatRegisters::TotalPhys - FloatRegisters::ActualTotalPhys();
545 masm.ma_sub(Imm32(missingRegs * sizeof(double)), sp, scratch);
546 }
547 masm.startFloatTransferM(IsStore, sp, DB, WriteBack);
548 for (uint32_t i = 0; i < FloatRegisters::ActualTotalPhys(); i++) {
549 masm.transferFloatReg(FloatRegister(i, FloatRegister::Double));
550 }
551 masm.finishFloatTransfer();
552
553 // The current stack pointer is the first argument to jit::Bailout.
554 masm.ma_mov(sp, spArg);
555 }
556
557 static void GenerateBailoutThunk(MacroAssembler& masm, Label* bailoutTail) {
558 PushBailoutFrame(masm, r0);
559
560 // Make space for Bailout's bailoutInfo outparam.
561 masm.reserveStack(sizeof(void*));
562 masm.mov(sp, r1);
563 using Fn = bool (*)(BailoutStack* sp, BaselineBailoutInfo** info);
564 masm.setupAlignedABICall();
565
566 masm.passABIArg(r0);
567 masm.passABIArg(r1);
568
569 masm.callWithABI<Fn, Bailout>(ABIType::General,
570 CheckUnsafeCallWithABI::DontCheckOther);
571 masm.pop(r2); // Get the bailoutInfo outparam.
572
573 // Remove both the bailout frame and the topmost Ion frame's stack.
574 masm.moveToStackPtr(FramePointer);
575
576 // Jump to shared bailout tail. The BailoutInfo pointer has to be in r2.
577 masm.jump(bailoutTail);
578 }
579
580 void JitRuntime::generateBailoutHandler(MacroAssembler& masm,
581 Label* bailoutTail) {
582 AutoCreatedBy acb(masm, "JitRuntime::generateBailoutHandler");
583
584 bailoutHandlerOffset_ = startTrampolineCode(masm);
585
586 GenerateBailoutThunk(masm, bailoutTail);
587 }
588
589 bool JitRuntime::generateVMWrapper(JSContext* cx, MacroAssembler& masm,
590 VMFunctionId id, const VMFunctionData& f,
591 DynFn nativeFun, uint32_t* wrapperOffset) {
592 AutoCreatedBy acb(masm, "JitRuntime::generateVMWrapper");
593
594 *wrapperOffset = startTrampolineCode(masm);
595
596 AllocatableGeneralRegisterSet regs(Register::Codes::WrapperMask);
597
598 static_assert(
599 (Register::Codes::VolatileMask & ~Register::Codes::WrapperMask) == 0,
600 "Wrapper register set must be a superset of Volatile register set.");
601
602 // The context is the first argument; r0 is the first argument register.
603 Register cxreg = r0;
604 regs.take(cxreg);
605
606 // On link-register platforms, it is the responsibility of the VM *callee* to
607 // push the return address, while the caller must ensure that the address
608 // is stored in lr on entry. This allows the VM wrapper to work with both
609 // direct calls and tail calls.
610 masm.pushReturnAddress();
611
612 // Push the frame pointer to finish the exit frame, then link it up.
613 masm.Push(FramePointer);
614 masm.moveStackPtrTo(FramePointer);
615 masm.loadJSContext(cxreg);
616 masm.enterExitFrame(cxreg, regs.getAny(), id);
617
618 // Reserve space for the outparameter.
619 masm.reserveVMFunctionOutParamSpace(f);
620
621 masm.setupUnalignedABICallDontSaveRestoreSP();
622 masm.passABIArg(cxreg);
623
624 size_t argDisp = ExitFrameLayout::Size();
625
626 // Copy any arguments.
627 for (uint32_t explicitArg = 0; explicitArg < f.explicitArgs; explicitArg++) {
628 switch (f.argProperties(explicitArg)) {
629 case VMFunctionData::WordByValue:
630 masm.passABIArg(MoveOperand(FramePointer, argDisp), ABIType::General);
631 argDisp += sizeof(void*);
632 break;
633 case VMFunctionData::DoubleByValue:
634 // Values should be passed by reference, not by value, so we assert
635 // that the argument is a double-precision float.
636 MOZ_ASSERT(f.argPassedInFloatReg(explicitArg));
637 masm.passABIArg(MoveOperand(FramePointer, argDisp), ABIType::Float64);
638 argDisp += sizeof(double);
639 break;
640 case VMFunctionData::WordByRef:
641 masm.passABIArg(MoveOperand(FramePointer, argDisp,
642 MoveOperand::Kind::EffectiveAddress),
643 ABIType::General);
644 argDisp += sizeof(void*);
645 break;
646 case VMFunctionData::DoubleByRef:
647 masm.passABIArg(MoveOperand(FramePointer, argDisp,
648 MoveOperand::Kind::EffectiveAddress),
649 ABIType::General);
650 argDisp += 2 * sizeof(void*);
651 break;
652 }
653 }
654
655 // Copy the implicit outparam, if any.
656 const int32_t outParamOffset =
657 -int32_t(ExitFooterFrame::Size()) - f.sizeOfOutParamStackSlot();
658 if (f.outParam != Type_Void) {
659 masm.passABIArg(MoveOperand(FramePointer, outParamOffset,
660 MoveOperand::Kind::EffectiveAddress),
661 ABIType::General);
662 }
663
664 masm.callWithABI(nativeFun, ABIType::General,
665 CheckUnsafeCallWithABI::DontCheckHasExitFrame);
666
667 // Test for failure.
668 switch (f.failType()) {
669 case Type_Cell:
670 masm.branchTestPtr(Assembler::Zero, r0, r0, masm.failureLabel());
671 break;
672 case Type_Bool:
673 masm.branchIfFalseBool(r0, masm.failureLabel());
674 break;
675 case Type_Void:
676 break;
677 default:
678 MOZ_CRASH("unknown failure kind");
679 }
680
681 // Load the outparam.
682 masm.loadVMFunctionOutParam(f, Address(FramePointer, outParamOffset));
683
684 // Until C++ code is instrumented against Spectre, prevent speculative
685 // execution from returning any private data.
686 if (f.returnsData() && JitOptions.spectreJitToCxxCalls) {
687 masm.speculationBarrier();
688 }
689
690 // Pop frame and restore frame pointer.
691 masm.moveToStackPtr(FramePointer);
692 masm.pop(FramePointer);
693
694 // Return. Subtract sizeof(void*) for the frame pointer.
695 masm.retn(Imm32(sizeof(ExitFrameLayout) - sizeof(void*) +
696 f.explicitStackSlots() * sizeof(void*) +
697 f.extraValuesToPop * sizeof(Value)));
698
699 return true;
700 }
701
702 uint32_t JitRuntime::generatePreBarrier(JSContext* cx, MacroAssembler& masm,
703 MIRType type) {
704 AutoCreatedBy acb(masm, "JitRuntime::generatePreBarrier");
705
706 uint32_t offset = startTrampolineCode(masm);
707
708 masm.pushReturnAddress();
709
710 static_assert(PreBarrierReg == r1);
711 Register temp1 = r2;
712 Register temp2 = r3;
713 Register temp3 = r4;
714 masm.push(temp1);
715 masm.push(temp2);
716 masm.push(temp3);
717
718 Label noBarrier;
719 masm.emitPreBarrierFastPath(cx->runtime(), type, temp1, temp2, temp3,
720 &noBarrier);
721
722 // Call into C++ to mark this GC thing.
723 masm.pop(temp3);
724 masm.pop(temp2);
725 masm.pop(temp1);
726
727 LiveRegisterSet save;
728 save.set() =
729 RegisterSet(GeneralRegisterSet(Registers::VolatileMask),
730 FloatRegisterSet(FloatRegisters::VolatileDoubleMask));
731 masm.PushRegsInMask(save);
732
733 masm.movePtr(ImmPtr(cx->runtime()), r0);
734
735 masm.setupUnalignedABICall(r2);
736 masm.passABIArg(r0);
737 masm.passABIArg(r1);
738 masm.callWithABI(JitPreWriteBarrier(type));
739 masm.PopRegsInMask(save);
740 masm.ret();
741
742 masm.bind(&noBarrier);
743 masm.pop(temp3);
744 masm.pop(temp2);
745 masm.pop(temp1);
746 masm.ret();
747
748 return offset;
749 }
750
751 void JitRuntime::generateBailoutTailStub(MacroAssembler& masm,
752 Label* bailoutTail) {
753 AutoCreatedBy acb(masm, "JitRuntime::generateBailoutTailStub");
754
755 masm.bind(bailoutTail);
756 masm.generateBailoutTail(r1, r2);
757 }