1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include "v8.h"
29
30 #if defined(V8_TARGET_ARCH_X64)
31
32 #include "assembler.h"
33 #include "codegen.h"
34 #include "debug.h"
35
36
37 namespace v8 {
38 namespace internal {
39
40 #ifdef ENABLE_DEBUGGER_SUPPORT
41
IsDebugBreakAtReturn()42 bool BreakLocationIterator::IsDebugBreakAtReturn() {
43 return Debug::IsDebugBreakAtReturn(rinfo());
44 }
45
46
47 // Patch the JS frame exit code with a debug break call. See
48 // CodeGenerator::VisitReturnStatement and VirtualFrame::Exit in codegen-x64.cc
49 // for the precise return instructions sequence.
SetDebugBreakAtReturn()50 void BreakLocationIterator::SetDebugBreakAtReturn() {
51 ASSERT(Assembler::kJSReturnSequenceLength >=
52 Assembler::kCallInstructionLength);
53 rinfo()->PatchCodeWithCall(
54 Isolate::Current()->debug()->debug_break_return()->entry(),
55 Assembler::kJSReturnSequenceLength - Assembler::kCallInstructionLength);
56 }
57
58
59 // Restore the JS frame exit code.
ClearDebugBreakAtReturn()60 void BreakLocationIterator::ClearDebugBreakAtReturn() {
61 rinfo()->PatchCode(original_rinfo()->pc(),
62 Assembler::kJSReturnSequenceLength);
63 }
64
65
66 // A debug break in the frame exit code is identified by the JS frame exit code
67 // having been patched with a call instruction.
IsDebugBreakAtReturn(v8::internal::RelocInfo * rinfo)68 bool Debug::IsDebugBreakAtReturn(v8::internal::RelocInfo* rinfo) {
69 ASSERT(RelocInfo::IsJSReturn(rinfo->rmode()));
70 return rinfo->IsPatchedReturnSequence();
71 }
72
73
IsDebugBreakAtSlot()74 bool BreakLocationIterator::IsDebugBreakAtSlot() {
75 ASSERT(IsDebugBreakSlot());
76 // Check whether the debug break slot instructions have been patched.
77 return !Assembler::IsNop(rinfo()->pc());
78 }
79
80
SetDebugBreakAtSlot()81 void BreakLocationIterator::SetDebugBreakAtSlot() {
82 ASSERT(IsDebugBreakSlot());
83 rinfo()->PatchCodeWithCall(
84 Isolate::Current()->debug()->debug_break_slot()->entry(),
85 Assembler::kDebugBreakSlotLength - Assembler::kCallInstructionLength);
86 }
87
88
ClearDebugBreakAtSlot()89 void BreakLocationIterator::ClearDebugBreakAtSlot() {
90 ASSERT(IsDebugBreakSlot());
91 rinfo()->PatchCode(original_rinfo()->pc(), Assembler::kDebugBreakSlotLength);
92 }
93
94
95 #define __ ACCESS_MASM(masm)
96
97
Generate_DebugBreakCallHelper(MacroAssembler * masm,RegList object_regs,RegList non_object_regs,bool convert_call_to_jmp)98 static void Generate_DebugBreakCallHelper(MacroAssembler* masm,
99 RegList object_regs,
100 RegList non_object_regs,
101 bool convert_call_to_jmp) {
102 // Enter an internal frame.
103 __ EnterInternalFrame();
104
105 // Store the registers containing live values on the expression stack to
106 // make sure that these are correctly updated during GC. Non object values
107 // are stored as as two smis causing it to be untouched by GC.
108 ASSERT((object_regs & ~kJSCallerSaved) == 0);
109 ASSERT((non_object_regs & ~kJSCallerSaved) == 0);
110 ASSERT((object_regs & non_object_regs) == 0);
111 for (int i = 0; i < kNumJSCallerSaved; i++) {
112 int r = JSCallerSavedCode(i);
113 Register reg = { r };
114 ASSERT(!reg.is(kScratchRegister));
115 if ((object_regs & (1 << r)) != 0) {
116 __ push(reg);
117 }
118 // Store the 64-bit value as two smis.
119 if ((non_object_regs & (1 << r)) != 0) {
120 __ movq(kScratchRegister, reg);
121 __ Integer32ToSmi(reg, reg);
122 __ push(reg);
123 __ sar(kScratchRegister, Immediate(32));
124 __ Integer32ToSmi(kScratchRegister, kScratchRegister);
125 __ push(kScratchRegister);
126 }
127 }
128
129 #ifdef DEBUG
130 __ RecordComment("// Calling from debug break to runtime - come in - over");
131 #endif
132 __ Set(rax, 0); // No arguments (argc == 0).
133 __ movq(rbx, ExternalReference::debug_break(masm->isolate()));
134
135 CEntryStub ceb(1);
136 __ CallStub(&ceb);
137
138 // Restore the register values from the expression stack.
139 for (int i = kNumJSCallerSaved - 1; i >= 0; i--) {
140 int r = JSCallerSavedCode(i);
141 Register reg = { r };
142 if (FLAG_debug_code) {
143 __ Set(reg, kDebugZapValue);
144 }
145 if ((object_regs & (1 << r)) != 0) {
146 __ pop(reg);
147 }
148 // Reconstruct the 64-bit value from two smis.
149 if ((non_object_regs & (1 << r)) != 0) {
150 __ pop(kScratchRegister);
151 __ SmiToInteger32(kScratchRegister, kScratchRegister);
152 __ shl(kScratchRegister, Immediate(32));
153 __ pop(reg);
154 __ SmiToInteger32(reg, reg);
155 __ or_(reg, kScratchRegister);
156 }
157 }
158
159 // Get rid of the internal frame.
160 __ LeaveInternalFrame();
161
162 // If this call did not replace a call but patched other code then there will
163 // be an unwanted return address left on the stack. Here we get rid of that.
164 if (convert_call_to_jmp) {
165 __ addq(rsp, Immediate(kPointerSize));
166 }
167
168 // Now that the break point has been handled, resume normal execution by
169 // jumping to the target address intended by the caller and that was
170 // overwritten by the address of DebugBreakXXX.
171 ExternalReference after_break_target =
172 ExternalReference(Debug_Address::AfterBreakTarget(), masm->isolate());
173 __ movq(kScratchRegister, after_break_target);
174 __ jmp(Operand(kScratchRegister, 0));
175 }
176
177
GenerateLoadICDebugBreak(MacroAssembler * masm)178 void Debug::GenerateLoadICDebugBreak(MacroAssembler* masm) {
179 // Register state for IC load call (from ic-x64.cc).
180 // ----------- S t a t e -------------
181 // -- rax : receiver
182 // -- rcx : name
183 // -----------------------------------
184 Generate_DebugBreakCallHelper(masm, rax.bit() | rcx.bit(), 0, false);
185 }
186
187
GenerateStoreICDebugBreak(MacroAssembler * masm)188 void Debug::GenerateStoreICDebugBreak(MacroAssembler* masm) {
189 // Register state for IC store call (from ic-x64.cc).
190 // ----------- S t a t e -------------
191 // -- rax : value
192 // -- rcx : name
193 // -- rdx : receiver
194 // -----------------------------------
195 Generate_DebugBreakCallHelper(
196 masm, rax.bit() | rcx.bit() | rdx.bit(), 0, false);
197 }
198
199
GenerateKeyedLoadICDebugBreak(MacroAssembler * masm)200 void Debug::GenerateKeyedLoadICDebugBreak(MacroAssembler* masm) {
201 // Register state for keyed IC load call (from ic-x64.cc).
202 // ----------- S t a t e -------------
203 // -- rax : key
204 // -- rdx : receiver
205 // -----------------------------------
206 Generate_DebugBreakCallHelper(masm, rax.bit() | rdx.bit(), 0, false);
207 }
208
209
GenerateKeyedStoreICDebugBreak(MacroAssembler * masm)210 void Debug::GenerateKeyedStoreICDebugBreak(MacroAssembler* masm) {
211 // Register state for keyed IC load call (from ic-x64.cc).
212 // ----------- S t a t e -------------
213 // -- rax : value
214 // -- rcx : key
215 // -- rdx : receiver
216 // -----------------------------------
217 Generate_DebugBreakCallHelper(
218 masm, rax.bit() | rcx.bit() | rdx.bit(), 0, false);
219 }
220
221
GenerateCallICDebugBreak(MacroAssembler * masm)222 void Debug::GenerateCallICDebugBreak(MacroAssembler* masm) {
223 // Register state for IC call call (from ic-x64.cc)
224 // ----------- S t a t e -------------
225 // -- rcx: function name
226 // -----------------------------------
227 Generate_DebugBreakCallHelper(masm, rcx.bit(), 0, false);
228 }
229
230
GenerateConstructCallDebugBreak(MacroAssembler * masm)231 void Debug::GenerateConstructCallDebugBreak(MacroAssembler* masm) {
232 // Register state just before return from JS function (from codegen-x64.cc).
233 // rax is the actual number of arguments not encoded as a smi, see comment
234 // above IC call.
235 // ----------- S t a t e -------------
236 // -- rax: number of arguments
237 // -----------------------------------
238 // The number of arguments in rax is not smi encoded.
239 Generate_DebugBreakCallHelper(masm, rdi.bit(), rax.bit(), false);
240 }
241
242
GenerateReturnDebugBreak(MacroAssembler * masm)243 void Debug::GenerateReturnDebugBreak(MacroAssembler* masm) {
244 // Register state just before return from JS function (from codegen-x64.cc).
245 // ----------- S t a t e -------------
246 // -- rax: return value
247 // -----------------------------------
248 Generate_DebugBreakCallHelper(masm, rax.bit(), 0, true);
249 }
250
251
GenerateStubNoRegistersDebugBreak(MacroAssembler * masm)252 void Debug::GenerateStubNoRegistersDebugBreak(MacroAssembler* masm) {
253 // Register state for stub CallFunction (from CallFunctionStub in ic-x64.cc).
254 // ----------- S t a t e -------------
255 // No registers used on entry.
256 // -----------------------------------
257 Generate_DebugBreakCallHelper(masm, 0, 0, false);
258 }
259
260
GenerateSlot(MacroAssembler * masm)261 void Debug::GenerateSlot(MacroAssembler* masm) {
262 // Generate enough nop's to make space for a call instruction.
263 Label check_codesize;
264 __ bind(&check_codesize);
265 __ RecordDebugBreakSlot();
266 for (int i = 0; i < Assembler::kDebugBreakSlotLength; i++) {
267 __ nop();
268 }
269 ASSERT_EQ(Assembler::kDebugBreakSlotLength,
270 masm->SizeOfCodeGeneratedSince(&check_codesize));
271 }
272
273
GenerateSlotDebugBreak(MacroAssembler * masm)274 void Debug::GenerateSlotDebugBreak(MacroAssembler* masm) {
275 // In the places where a debug break slot is inserted no registers can contain
276 // object pointers.
277 Generate_DebugBreakCallHelper(masm, 0, 0, true);
278 }
279
280
GeneratePlainReturnLiveEdit(MacroAssembler * masm)281 void Debug::GeneratePlainReturnLiveEdit(MacroAssembler* masm) {
282 masm->ret(0);
283 }
284
285
GenerateFrameDropperLiveEdit(MacroAssembler * masm)286 void Debug::GenerateFrameDropperLiveEdit(MacroAssembler* masm) {
287 ExternalReference restarter_frame_function_slot =
288 ExternalReference(Debug_Address::RestarterFrameFunctionPointer(),
289 masm->isolate());
290 __ movq(rax, restarter_frame_function_slot);
291 __ movq(Operand(rax, 0), Immediate(0));
292
293 // We do not know our frame height, but set rsp based on rbp.
294 __ lea(rsp, Operand(rbp, -1 * kPointerSize));
295
296 __ pop(rdi); // Function.
297 __ pop(rbp);
298
299 // Load context from the function.
300 __ movq(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
301
302 // Get function code.
303 __ movq(rdx, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
304 __ movq(rdx, FieldOperand(rdx, SharedFunctionInfo::kCodeOffset));
305 __ lea(rdx, FieldOperand(rdx, Code::kHeaderSize));
306
307 // Re-run JSFunction, rdi is function, rsi is context.
308 __ jmp(rdx);
309 }
310
311 const bool Debug::kFrameDropperSupported = true;
312
313 #undef __
314
315 #endif // ENABLE_DEBUGGER_SUPPORT
316
317 } } // namespace v8::internal
318
319 #endif // V8_TARGET_ARCH_X64
320