• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "src/v8.h"
6 
7 #if V8_TARGET_ARCH_X64
8 
9 #include "src/assembler.h"
10 #include "src/codegen.h"
11 #include "src/debug.h"
12 
13 
14 namespace v8 {
15 namespace internal {
16 
IsDebugBreakAtReturn()17 bool BreakLocationIterator::IsDebugBreakAtReturn()  {
18   return Debug::IsDebugBreakAtReturn(rinfo());
19 }
20 
21 
22 // Patch the JS frame exit code with a debug break call. See
23 // CodeGenerator::VisitReturnStatement and VirtualFrame::Exit in codegen-x64.cc
24 // for the precise return instructions sequence.
SetDebugBreakAtReturn()25 void BreakLocationIterator::SetDebugBreakAtReturn()  {
26   DCHECK(Assembler::kJSReturnSequenceLength >= Assembler::kCallSequenceLength);
27   rinfo()->PatchCodeWithCall(
28       debug_info_->GetIsolate()->builtins()->Return_DebugBreak()->entry(),
29       Assembler::kJSReturnSequenceLength - Assembler::kCallSequenceLength);
30 }
31 
32 
33 // Restore the JS frame exit code.
ClearDebugBreakAtReturn()34 void BreakLocationIterator::ClearDebugBreakAtReturn() {
35   rinfo()->PatchCode(original_rinfo()->pc(),
36                      Assembler::kJSReturnSequenceLength);
37 }
38 
39 
40 // A debug break in the frame exit code is identified by the JS frame exit code
41 // having been patched with a call instruction.
IsDebugBreakAtReturn(v8::internal::RelocInfo * rinfo)42 bool Debug::IsDebugBreakAtReturn(v8::internal::RelocInfo* rinfo) {
43   DCHECK(RelocInfo::IsJSReturn(rinfo->rmode()));
44   return rinfo->IsPatchedReturnSequence();
45 }
46 
47 
IsDebugBreakAtSlot()48 bool BreakLocationIterator::IsDebugBreakAtSlot() {
49   DCHECK(IsDebugBreakSlot());
50   // Check whether the debug break slot instructions have been patched.
51   return rinfo()->IsPatchedDebugBreakSlotSequence();
52 }
53 
54 
SetDebugBreakAtSlot()55 void BreakLocationIterator::SetDebugBreakAtSlot() {
56   DCHECK(IsDebugBreakSlot());
57   rinfo()->PatchCodeWithCall(
58       debug_info_->GetIsolate()->builtins()->Slot_DebugBreak()->entry(),
59       Assembler::kDebugBreakSlotLength - Assembler::kCallSequenceLength);
60 }
61 
62 
ClearDebugBreakAtSlot()63 void BreakLocationIterator::ClearDebugBreakAtSlot() {
64   DCHECK(IsDebugBreakSlot());
65   rinfo()->PatchCode(original_rinfo()->pc(), Assembler::kDebugBreakSlotLength);
66 }
67 
68 
69 #define __ ACCESS_MASM(masm)
70 
71 
Generate_DebugBreakCallHelper(MacroAssembler * masm,RegList object_regs,RegList non_object_regs,bool convert_call_to_jmp)72 static void Generate_DebugBreakCallHelper(MacroAssembler* masm,
73                                           RegList object_regs,
74                                           RegList non_object_regs,
75                                           bool convert_call_to_jmp) {
76   // Enter an internal frame.
77   {
78     FrameScope scope(masm, StackFrame::INTERNAL);
79 
80     // Load padding words on stack.
81     for (int i = 0; i < LiveEdit::kFramePaddingInitialSize; i++) {
82       __ Push(Smi::FromInt(LiveEdit::kFramePaddingValue));
83     }
84     __ Push(Smi::FromInt(LiveEdit::kFramePaddingInitialSize));
85 
86     // Store the registers containing live values on the expression stack to
87     // make sure that these are correctly updated during GC. Non object values
88     // are stored as as two smis causing it to be untouched by GC.
89     DCHECK((object_regs & ~kJSCallerSaved) == 0);
90     DCHECK((non_object_regs & ~kJSCallerSaved) == 0);
91     DCHECK((object_regs & non_object_regs) == 0);
92     for (int i = 0; i < kNumJSCallerSaved; i++) {
93       int r = JSCallerSavedCode(i);
94       Register reg = { r };
95       DCHECK(!reg.is(kScratchRegister));
96       if ((object_regs & (1 << r)) != 0) {
97         __ Push(reg);
98       }
99       if ((non_object_regs & (1 << r)) != 0) {
100         __ PushRegisterAsTwoSmis(reg);
101       }
102     }
103 
104 #ifdef DEBUG
105     __ RecordComment("// Calling from debug break to runtime - come in - over");
106 #endif
107     __ Set(rax, 0);  // No arguments (argc == 0).
108     __ Move(rbx, ExternalReference::debug_break(masm->isolate()));
109 
110     CEntryStub ceb(masm->isolate(), 1);
111     __ CallStub(&ceb);
112 
113     // Restore the register values from the expression stack.
114     for (int i = kNumJSCallerSaved - 1; i >= 0; i--) {
115       int r = JSCallerSavedCode(i);
116       Register reg = { r };
117       if (FLAG_debug_code) {
118         __ Set(reg, kDebugZapValue);
119       }
120       if ((object_regs & (1 << r)) != 0) {
121         __ Pop(reg);
122       }
123       // Reconstruct the 64-bit value from two smis.
124       if ((non_object_regs & (1 << r)) != 0) {
125         __ PopRegisterAsTwoSmis(reg);
126       }
127     }
128 
129     // Read current padding counter and skip corresponding number of words.
130     __ Pop(kScratchRegister);
131     __ SmiToInteger32(kScratchRegister, kScratchRegister);
132     __ leap(rsp, Operand(rsp, kScratchRegister, times_pointer_size, 0));
133 
134     // Get rid of the internal frame.
135   }
136 
137   // If this call did not replace a call but patched other code then there will
138   // be an unwanted return address left on the stack. Here we get rid of that.
139   if (convert_call_to_jmp) {
140     __ addp(rsp, Immediate(kPCOnStackSize));
141   }
142 
143   // Now that the break point has been handled, resume normal execution by
144   // jumping to the target address intended by the caller and that was
145   // overwritten by the address of DebugBreakXXX.
146   ExternalReference after_break_target =
147       ExternalReference::debug_after_break_target_address(masm->isolate());
148   __ Move(kScratchRegister, after_break_target);
149   __ Jump(Operand(kScratchRegister, 0));
150 }
151 
152 
GenerateCallICStubDebugBreak(MacroAssembler * masm)153 void DebugCodegen::GenerateCallICStubDebugBreak(MacroAssembler* masm) {
154   // Register state for CallICStub
155   // ----------- S t a t e -------------
156   //  -- rdx    : type feedback slot (smi)
157   //  -- rdi    : function
158   // -----------------------------------
159   Generate_DebugBreakCallHelper(masm, rdx.bit() | rdi.bit(), 0, false);
160 }
161 
162 
GenerateLoadICDebugBreak(MacroAssembler * masm)163 void DebugCodegen::GenerateLoadICDebugBreak(MacroAssembler* masm) {
164   // Register state for IC load call (from ic-x64.cc).
165   Register receiver = LoadDescriptor::ReceiverRegister();
166   Register name = LoadDescriptor::NameRegister();
167   Generate_DebugBreakCallHelper(masm, receiver.bit() | name.bit(), 0, false);
168 }
169 
170 
GenerateStoreICDebugBreak(MacroAssembler * masm)171 void DebugCodegen::GenerateStoreICDebugBreak(MacroAssembler* masm) {
172   // Register state for IC store call (from ic-x64.cc).
173   Register receiver = StoreDescriptor::ReceiverRegister();
174   Register name = StoreDescriptor::NameRegister();
175   Register value = StoreDescriptor::ValueRegister();
176   Generate_DebugBreakCallHelper(
177       masm, receiver.bit() | name.bit() | value.bit(), 0, false);
178 }
179 
180 
GenerateKeyedLoadICDebugBreak(MacroAssembler * masm)181 void DebugCodegen::GenerateKeyedLoadICDebugBreak(MacroAssembler* masm) {
182   // Register state for keyed IC load call (from ic-x64.cc).
183   GenerateLoadICDebugBreak(masm);
184 }
185 
186 
GenerateKeyedStoreICDebugBreak(MacroAssembler * masm)187 void DebugCodegen::GenerateKeyedStoreICDebugBreak(MacroAssembler* masm) {
188   // Register state for keyed IC store call (from ic-x64.cc).
189   Register receiver = StoreDescriptor::ReceiverRegister();
190   Register name = StoreDescriptor::NameRegister();
191   Register value = StoreDescriptor::ValueRegister();
192   Generate_DebugBreakCallHelper(
193       masm, receiver.bit() | name.bit() | value.bit(), 0, false);
194 }
195 
196 
GenerateCompareNilICDebugBreak(MacroAssembler * masm)197 void DebugCodegen::GenerateCompareNilICDebugBreak(MacroAssembler* masm) {
198   // Register state for CompareNil IC
199   // ----------- S t a t e -------------
200   //  -- rax    : value
201   // -----------------------------------
202   Generate_DebugBreakCallHelper(masm, rax.bit(), 0, false);
203 }
204 
205 
GenerateReturnDebugBreak(MacroAssembler * masm)206 void DebugCodegen::GenerateReturnDebugBreak(MacroAssembler* masm) {
207   // Register state just before return from JS function (from codegen-x64.cc).
208   // ----------- S t a t e -------------
209   //  -- rax: return value
210   // -----------------------------------
211   Generate_DebugBreakCallHelper(masm, rax.bit(), 0, true);
212 }
213 
214 
GenerateCallFunctionStubDebugBreak(MacroAssembler * masm)215 void DebugCodegen::GenerateCallFunctionStubDebugBreak(MacroAssembler* masm) {
216   // Register state for CallFunctionStub (from code-stubs-x64.cc).
217   // ----------- S t a t e -------------
218   //  -- rdi : function
219   // -----------------------------------
220   Generate_DebugBreakCallHelper(masm, rdi.bit(), 0, false);
221 }
222 
223 
GenerateCallConstructStubDebugBreak(MacroAssembler * masm)224 void DebugCodegen::GenerateCallConstructStubDebugBreak(MacroAssembler* masm) {
225   // Register state for CallConstructStub (from code-stubs-x64.cc).
226   // rax is the actual number of arguments not encoded as a smi, see comment
227   // above IC call.
228   // ----------- S t a t e -------------
229   //  -- rax: number of arguments
230   // -----------------------------------
231   // The number of arguments in rax is not smi encoded.
232   Generate_DebugBreakCallHelper(masm, rdi.bit(), rax.bit(), false);
233 }
234 
235 
GenerateCallConstructStubRecordDebugBreak(MacroAssembler * masm)236 void DebugCodegen::GenerateCallConstructStubRecordDebugBreak(
237     MacroAssembler* masm) {
238   // Register state for CallConstructStub (from code-stubs-x64.cc).
239   // rax is the actual number of arguments not encoded as a smi, see comment
240   // above IC call.
241   // ----------- S t a t e -------------
242   //  -- rax: number of arguments
243   //  -- rbx: feedback array
244   //  -- rdx: feedback slot (smi)
245   // -----------------------------------
246   // The number of arguments in rax is not smi encoded.
247   Generate_DebugBreakCallHelper(masm, rbx.bit() | rdx.bit() | rdi.bit(),
248                                 rax.bit(), false);
249 }
250 
251 
GenerateSlot(MacroAssembler * masm)252 void DebugCodegen::GenerateSlot(MacroAssembler* masm) {
253   // Generate enough nop's to make space for a call instruction.
254   Label check_codesize;
255   __ bind(&check_codesize);
256   __ RecordDebugBreakSlot();
257   __ Nop(Assembler::kDebugBreakSlotLength);
258   DCHECK_EQ(Assembler::kDebugBreakSlotLength,
259             masm->SizeOfCodeGeneratedSince(&check_codesize));
260 }
261 
262 
GenerateSlotDebugBreak(MacroAssembler * masm)263 void DebugCodegen::GenerateSlotDebugBreak(MacroAssembler* masm) {
264   // In the places where a debug break slot is inserted no registers can contain
265   // object pointers.
266   Generate_DebugBreakCallHelper(masm, 0, 0, true);
267 }
268 
269 
GeneratePlainReturnLiveEdit(MacroAssembler * masm)270 void DebugCodegen::GeneratePlainReturnLiveEdit(MacroAssembler* masm) {
271   masm->ret(0);
272 }
273 
274 
GenerateFrameDropperLiveEdit(MacroAssembler * masm)275 void DebugCodegen::GenerateFrameDropperLiveEdit(MacroAssembler* masm) {
276   ExternalReference restarter_frame_function_slot =
277       ExternalReference::debug_restarter_frame_function_pointer_address(
278           masm->isolate());
279   __ Move(rax, restarter_frame_function_slot);
280   __ movp(Operand(rax, 0), Immediate(0));
281 
282   // We do not know our frame height, but set rsp based on rbp.
283   __ leap(rsp, Operand(rbp, -1 * kPointerSize));
284 
285   __ Pop(rdi);  // Function.
286   __ popq(rbp);
287 
288   // Load context from the function.
289   __ movp(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
290 
291   // Get function code.
292   __ movp(rdx, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
293   __ movp(rdx, FieldOperand(rdx, SharedFunctionInfo::kCodeOffset));
294   __ leap(rdx, FieldOperand(rdx, Code::kHeaderSize));
295 
296   // Re-run JSFunction, rdi is function, rsi is context.
297   __ jmp(rdx);
298 }
299 
300 const bool LiveEdit::kFrameDropperSupported = true;
301 
302 #undef __
303 
304 } }  // namespace v8::internal
305 
306 #endif  // V8_TARGET_ARCH_X64
307