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_X64
6 
7 #include "src/assembler.h"
8 #include "src/codegen.h"
9 #include "src/debug/debug.h"
10 
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_codesize;
20   __ bind(&check_codesize);
21   __ Nop(Assembler::kDebugBreakSlotLength);
22   DCHECK_EQ(Assembler::kDebugBreakSlotLength,
23             masm->SizeOfCodeGeneratedSince(&check_codesize));
24 }
25 
26 
GenerateSlot(MacroAssembler * masm,RelocInfo::Mode mode)27 void DebugCodegen::GenerateSlot(MacroAssembler* masm, RelocInfo::Mode mode) {
28   // Generate enough nop's to make space for a call instruction.
29   masm->RecordDebugBreakSlot(mode);
30   EmitDebugBreakSlot(masm);
31 }
32 
33 
ClearDebugBreakSlot(Isolate * isolate,Address pc)34 void DebugCodegen::ClearDebugBreakSlot(Isolate* isolate, Address pc) {
35   CodePatcher patcher(isolate, pc, Assembler::kDebugBreakSlotLength);
36   EmitDebugBreakSlot(patcher.masm());
37 }
38 
39 
PatchDebugBreakSlot(Isolate * isolate,Address pc,Handle<Code> code)40 void DebugCodegen::PatchDebugBreakSlot(Isolate* isolate, Address pc,
41                                        Handle<Code> code) {
42   DCHECK_EQ(Code::BUILTIN, code->kind());
43   static const int kSize = Assembler::kDebugBreakSlotLength;
44   CodePatcher patcher(isolate, pc, kSize);
45   Label check_codesize;
46   patcher.masm()->bind(&check_codesize);
47   patcher.masm()->movp(kScratchRegister, reinterpret_cast<void*>(code->entry()),
48                        Assembler::RelocInfoNone());
49   patcher.masm()->call(kScratchRegister);
50   // Check that the size of the code generated is as expected.
51   DCHECK_EQ(kSize, patcher.masm()->SizeOfCodeGeneratedSince(&check_codesize));
52 }
53 
54 
GenerateDebugBreakStub(MacroAssembler * masm,DebugBreakCallHelperMode mode)55 void DebugCodegen::GenerateDebugBreakStub(MacroAssembler* masm,
56                                           DebugBreakCallHelperMode mode) {
57   __ RecordComment("Debug break");
58 
59   // Enter an internal frame.
60   {
61     FrameScope scope(masm, StackFrame::INTERNAL);
62 
63     // Load padding words on stack.
64     for (int i = 0; i < LiveEdit::kFramePaddingInitialSize; i++) {
65       __ Push(Smi::FromInt(LiveEdit::kFramePaddingValue));
66     }
67     __ Push(Smi::FromInt(LiveEdit::kFramePaddingInitialSize));
68 
69     if (mode == SAVE_RESULT_REGISTER) __ Push(rax);
70 
71     __ Set(rax, 0);  // No arguments (argc == 0).
72     __ Move(rbx, ExternalReference(Runtime::FunctionForId(Runtime::kDebugBreak),
73                                    masm->isolate()));
74 
75     CEntryStub ceb(masm->isolate(), 1);
76     __ CallStub(&ceb);
77 
78     if (FLAG_debug_code) {
79       for (int i = 0; i < kNumJSCallerSaved; ++i) {
80         Register reg = {JSCallerSavedCode(i)};
81         __ Set(reg, kDebugZapValue);
82       }
83     }
84 
85     if (mode == SAVE_RESULT_REGISTER) __ Pop(rax);
86 
87     // Read current padding counter and skip corresponding number of words.
88     __ Pop(kScratchRegister);
89     __ SmiToInteger32(kScratchRegister, kScratchRegister);
90     __ leap(rsp, Operand(rsp, kScratchRegister, times_pointer_size, 0));
91 
92     // Get rid of the internal frame.
93   }
94 
95   // This call did not replace a call , so there will be an unwanted
96   // return address left on the stack. Here we get rid of that.
97   __ addp(rsp, Immediate(kPCOnStackSize));
98 
99   // Now that the break point has been handled, resume normal execution by
100   // jumping to the target address intended by the caller and that was
101   // overwritten by the address of DebugBreakXXX.
102   ExternalReference after_break_target =
103       ExternalReference::debug_after_break_target_address(masm->isolate());
104   __ Move(kScratchRegister, after_break_target);
105   __ Jump(Operand(kScratchRegister, 0));
106 }
107 
108 
GenerateFrameDropperLiveEdit(MacroAssembler * masm)109 void DebugCodegen::GenerateFrameDropperLiveEdit(MacroAssembler* masm) {
110   // We do not know our frame height, but set rsp based on rbp.
111   __ leap(rsp, Operand(rbp, -1 * kPointerSize));
112 
113   __ Pop(rdi);  // Function.
114   __ popq(rbp);
115 
116   ParameterCount dummy(0);
117   __ FloodFunctionIfStepping(rdi, no_reg, dummy, dummy);
118 
119   // Load context from the function.
120   __ movp(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
121 
122   // Clear new.target as a safety measure.
123   __ LoadRoot(rdx, Heap::kUndefinedValueRootIndex);
124 
125   // Get function code.
126   __ movp(rbx, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
127   __ movp(rbx, FieldOperand(rbx, SharedFunctionInfo::kCodeOffset));
128   __ leap(rbx, FieldOperand(rbx, Code::kHeaderSize));
129 
130   // Re-run JSFunction, rdi is function, rsi is context.
131   __ jmp(rbx);
132 }
133 
134 const bool LiveEdit::kFrameDropperSupported = true;
135 
136 #undef __
137 
138 }  // namespace internal
139 }  // namespace v8
140 
141 #endif  // V8_TARGET_ARCH_X64
142