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