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