1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #if V8_TARGET_ARCH_MIPS64
6 
7 #include "src/codegen.h"
8 #include "src/debug/debug.h"
9 
10 namespace v8 {
11 namespace internal {
12 
13 #define __ ACCESS_MASM(masm)
14 
EmitDebugBreakSlot(MacroAssembler * masm)15 void EmitDebugBreakSlot(MacroAssembler* masm) {
16   Label check_size;
17   __ bind(&check_size);
18   for (int i = 0; i < Assembler::kDebugBreakSlotInstructions; i++) {
19     __ nop(MacroAssembler::DEBUG_BREAK_NOP);
20   }
21   DCHECK_EQ(Assembler::kDebugBreakSlotInstructions,
22             masm->InstructionsGeneratedSince(&check_size));
23 }
24 
25 
GenerateSlot(MacroAssembler * masm,RelocInfo::Mode mode)26 void DebugCodegen::GenerateSlot(MacroAssembler* masm, RelocInfo::Mode mode) {
27   // Generate enough nop's to make space for a call instruction. Avoid emitting
28   // the trampoline pool in the debug break slot code.
29   Assembler::BlockTrampolinePoolScope block_pool(masm);
30   masm->RecordDebugBreakSlot(mode);
31   EmitDebugBreakSlot(masm);
32 }
33 
34 
ClearDebugBreakSlot(Isolate * isolate,Address pc)35 void DebugCodegen::ClearDebugBreakSlot(Isolate* isolate, Address pc) {
36   CodePatcher patcher(isolate, pc, Assembler::kDebugBreakSlotInstructions);
37   EmitDebugBreakSlot(patcher.masm());
38 }
39 
40 
PatchDebugBreakSlot(Isolate * isolate,Address pc,Handle<Code> code)41 void DebugCodegen::PatchDebugBreakSlot(Isolate* isolate, Address pc,
42                                        Handle<Code> code) {
43   DCHECK_EQ(Code::BUILTIN, code->kind());
44   CodePatcher patcher(isolate, pc, Assembler::kDebugBreakSlotInstructions);
45   // Patch the code changing the debug break slot code from:
46   //   nop(DEBUG_BREAK_NOP) - nop(1) is sll(zero_reg, zero_reg, 1)
47   //   nop(DEBUG_BREAK_NOP)
48   //   nop(DEBUG_BREAK_NOP)
49   //   nop(DEBUG_BREAK_NOP)
50   //   nop(DEBUG_BREAK_NOP)
51   //   nop(DEBUG_BREAK_NOP)
52   // to a call to the debug break slot code.
53   //   li t9, address   (4-instruction sequence on mips64)
54   //   call t9          (jalr t9 / nop instruction pair)
55   patcher.masm()->li(v8::internal::t9,
56                      Operand(reinterpret_cast<int64_t>(code->entry())),
57                      ADDRESS_LOAD);
58   patcher.masm()->Call(v8::internal::t9);
59 }
60 
61 
GenerateDebugBreakStub(MacroAssembler * masm,DebugBreakCallHelperMode mode)62 void DebugCodegen::GenerateDebugBreakStub(MacroAssembler* masm,
63                                           DebugBreakCallHelperMode mode) {
64   __ RecordComment("Debug break");
65   {
66     FrameScope scope(masm, StackFrame::INTERNAL);
67 
68     // Load padding words on stack.
69     __ li(at, Operand(Smi::FromInt(LiveEdit::kFramePaddingValue)));
70     __ Dsubu(sp, sp,
71             Operand(kPointerSize * LiveEdit::kFramePaddingInitialSize));
72     for (int i = LiveEdit::kFramePaddingInitialSize - 1; i >= 0; i--) {
73       __ sd(at, MemOperand(sp, kPointerSize * i));
74     }
75     __ li(at, Operand(Smi::FromInt(LiveEdit::kFramePaddingInitialSize)));
76     __ push(at);
77 
78     if (mode == SAVE_RESULT_REGISTER) __ push(v0);
79 
80     __ PrepareCEntryArgs(0);  // No arguments.
81     __ PrepareCEntryFunction(ExternalReference(
82         Runtime::FunctionForId(Runtime::kDebugBreak), masm->isolate()));
83 
84     CEntryStub ceb(masm->isolate(), 1);
85     __ CallStub(&ceb);
86 
87     if (FLAG_debug_code) {
88       for (int i = 0; i < kNumJSCallerSaved; i++) {
89         Register reg = {JSCallerSavedCode(i)};
90         __ li(reg, kDebugZapValue);
91       }
92     }
93 
94     if (mode == SAVE_RESULT_REGISTER) __ pop(v0);
95 
96     // Don't bother removing padding bytes pushed on the stack
97     // as the frame is going to be restored right away.
98 
99     // Leave the internal frame.
100   }
101 
102   // Now that the break point has been handled, resume normal execution by
103   // jumping to the target address intended by the caller and that was
104   // overwritten by the address of DebugBreakXXX.
105   ExternalReference after_break_target =
106       ExternalReference::debug_after_break_target_address(masm->isolate());
107   __ li(t9, Operand(after_break_target));
108   __ ld(t9, MemOperand(t9));
109   __ Jump(t9);
110 }
111 
112 
GenerateFrameDropperLiveEdit(MacroAssembler * masm)113 void DebugCodegen::GenerateFrameDropperLiveEdit(MacroAssembler* masm) {
114   // We do not know our frame height, but set sp based on fp.
115   __ Dsubu(sp, fp, Operand(kPointerSize));
116 
117   __ Pop(ra, fp, a1);  // Return address, Frame, Function.
118 
119   ParameterCount dummy(0);
120   __ FloodFunctionIfStepping(a1, no_reg, dummy, dummy);
121 
122   // Load context from the function.
123   __ ld(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
124 
125   // Clear new.target as a safety measure.
126   __ LoadRoot(a3, Heap::kUndefinedValueRootIndex);
127 
128   // Get function code.
129   __ ld(at, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
130   __ ld(at, FieldMemOperand(at, SharedFunctionInfo::kCodeOffset));
131   __ Daddu(t9, at, Operand(Code::kHeaderSize - kHeapObjectTag));
132 
133   // Re-run JSFunction, a1 is function, cp is context.
134   __ Jump(t9);
135 }
136 
137 
138 const bool LiveEdit::kFrameDropperSupported = true;
139 
140 #undef __
141 
142 }  // namespace internal
143 }  // namespace v8
144 
145 #endif  // V8_TARGET_ARCH_MIPS64
146