Home | History | Annotate | Download | only in x64
      1 // Copyright 2012 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 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 
     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.
     50 void BreakLocationIterator::SetDebugBreakAtReturn()  {
     51   ASSERT(Assembler::kJSReturnSequenceLength >= Assembler::kCallSequenceLength);
     52   rinfo()->PatchCodeWithCall(
     53       Isolate::Current()->debug()->debug_break_return()->entry(),
     54       Assembler::kJSReturnSequenceLength - Assembler::kCallSequenceLength);
     55 }
     56 
     57 
     58 // Restore the JS frame exit code.
     59 void BreakLocationIterator::ClearDebugBreakAtReturn() {
     60   rinfo()->PatchCode(original_rinfo()->pc(),
     61                      Assembler::kJSReturnSequenceLength);
     62 }
     63 
     64 
     65 // A debug break in the frame exit code is identified by the JS frame exit code
     66 // having been patched with a call instruction.
     67 bool Debug::IsDebugBreakAtReturn(v8::internal::RelocInfo* rinfo) {
     68   ASSERT(RelocInfo::IsJSReturn(rinfo->rmode()));
     69   return rinfo->IsPatchedReturnSequence();
     70 }
     71 
     72 
     73 bool BreakLocationIterator::IsDebugBreakAtSlot() {
     74   ASSERT(IsDebugBreakSlot());
     75   // Check whether the debug break slot instructions have been patched.
     76   return !Assembler::IsNop(rinfo()->pc());
     77 }
     78 
     79 
     80 void BreakLocationIterator::SetDebugBreakAtSlot() {
     81   ASSERT(IsDebugBreakSlot());
     82   rinfo()->PatchCodeWithCall(
     83       Isolate::Current()->debug()->debug_break_slot()->entry(),
     84       Assembler::kDebugBreakSlotLength - Assembler::kCallSequenceLength);
     85 }
     86 
     87 
     88 void BreakLocationIterator::ClearDebugBreakAtSlot() {
     89   ASSERT(IsDebugBreakSlot());
     90   rinfo()->PatchCode(original_rinfo()->pc(), Assembler::kDebugBreakSlotLength);
     91 }
     92 
     93 const bool Debug::FramePaddingLayout::kIsSupported = true;
     94 
     95 
     96 #define __ ACCESS_MASM(masm)
     97 
     98 
     99 static void Generate_DebugBreakCallHelper(MacroAssembler* masm,
    100                                           RegList object_regs,
    101                                           RegList non_object_regs,
    102                                           bool convert_call_to_jmp) {
    103   // Enter an internal frame.
    104   {
    105     FrameScope scope(masm, StackFrame::INTERNAL);
    106 
    107     // Load padding words on stack.
    108     for (int i = 0; i < Debug::FramePaddingLayout::kInitialSize; i++) {
    109       __ Push(Smi::FromInt(Debug::FramePaddingLayout::kPaddingValue));
    110     }
    111     __ Push(Smi::FromInt(Debug::FramePaddingLayout::kInitialSize));
    112 
    113     // Store the registers containing live values on the expression stack to
    114     // make sure that these are correctly updated during GC. Non object values
    115     // are stored as as two smis causing it to be untouched by GC.
    116     ASSERT((object_regs & ~kJSCallerSaved) == 0);
    117     ASSERT((non_object_regs & ~kJSCallerSaved) == 0);
    118     ASSERT((object_regs & non_object_regs) == 0);
    119     for (int i = 0; i < kNumJSCallerSaved; i++) {
    120       int r = JSCallerSavedCode(i);
    121       Register reg = { r };
    122       ASSERT(!reg.is(kScratchRegister));
    123       if ((object_regs & (1 << r)) != 0) {
    124         __ push(reg);
    125       }
    126       // Store the 64-bit value as two smis.
    127       if ((non_object_regs & (1 << r)) != 0) {
    128         __ movq(kScratchRegister, reg);
    129         __ Integer32ToSmi(reg, reg);
    130         __ push(reg);
    131         __ sar(kScratchRegister, Immediate(32));
    132         __ Integer32ToSmi(kScratchRegister, kScratchRegister);
    133         __ push(kScratchRegister);
    134       }
    135     }
    136 
    137 #ifdef DEBUG
    138     __ RecordComment("// Calling from debug break to runtime - come in - over");
    139 #endif
    140     __ Set(rax, 0);  // No arguments (argc == 0).
    141     __ movq(rbx, ExternalReference::debug_break(masm->isolate()));
    142 
    143     CEntryStub ceb(1);
    144     __ CallStub(&ceb);
    145 
    146     // Restore the register values from the expression stack.
    147     for (int i = kNumJSCallerSaved - 1; i >= 0; i--) {
    148       int r = JSCallerSavedCode(i);
    149       Register reg = { r };
    150       if (FLAG_debug_code) {
    151         __ Set(reg, kDebugZapValue);
    152       }
    153       if ((object_regs & (1 << r)) != 0) {
    154         __ pop(reg);
    155       }
    156       // Reconstruct the 64-bit value from two smis.
    157       if ((non_object_regs & (1 << r)) != 0) {
    158         __ pop(kScratchRegister);
    159         __ SmiToInteger32(kScratchRegister, kScratchRegister);
    160         __ shl(kScratchRegister, Immediate(32));
    161         __ pop(reg);
    162         __ SmiToInteger32(reg, reg);
    163         __ or_(reg, kScratchRegister);
    164       }
    165     }
    166 
    167     // Read current padding counter and skip corresponding number of words.
    168     __ pop(kScratchRegister);
    169     __ SmiToInteger32(kScratchRegister, kScratchRegister);
    170     __ lea(rsp, Operand(rsp, kScratchRegister, times_pointer_size, 0));
    171 
    172     // Get rid of the internal frame.
    173   }
    174 
    175   // If this call did not replace a call but patched other code then there will
    176   // be an unwanted return address left on the stack. Here we get rid of that.
    177   if (convert_call_to_jmp) {
    178     __ addq(rsp, Immediate(kPointerSize));
    179   }
    180 
    181   // Now that the break point has been handled, resume normal execution by
    182   // jumping to the target address intended by the caller and that was
    183   // overwritten by the address of DebugBreakXXX.
    184   ExternalReference after_break_target =
    185       ExternalReference(Debug_Address::AfterBreakTarget(), masm->isolate());
    186   __ movq(kScratchRegister, after_break_target);
    187   __ jmp(Operand(kScratchRegister, 0));
    188 }
    189 
    190 
    191 void Debug::GenerateLoadICDebugBreak(MacroAssembler* masm) {
    192   // Register state for IC load call (from ic-x64.cc).
    193   // ----------- S t a t e -------------
    194   //  -- rax    : receiver
    195   //  -- rcx    : name
    196   // -----------------------------------
    197   Generate_DebugBreakCallHelper(masm, rax.bit() | rcx.bit(), 0, false);
    198 }
    199 
    200 
    201 void Debug::GenerateStoreICDebugBreak(MacroAssembler* masm) {
    202   // Register state for IC store call (from ic-x64.cc).
    203   // ----------- S t a t e -------------
    204   //  -- rax    : value
    205   //  -- rcx    : name
    206   //  -- rdx    : receiver
    207   // -----------------------------------
    208   Generate_DebugBreakCallHelper(
    209       masm, rax.bit() | rcx.bit() | rdx.bit(), 0, false);
    210 }
    211 
    212 
    213 void Debug::GenerateKeyedLoadICDebugBreak(MacroAssembler* masm) {
    214   // Register state for keyed IC load call (from ic-x64.cc).
    215   // ----------- S t a t e -------------
    216   //  -- rax     : key
    217   //  -- rdx     : receiver
    218   // -----------------------------------
    219   Generate_DebugBreakCallHelper(masm, rax.bit() | rdx.bit(), 0, false);
    220 }
    221 
    222 
    223 void Debug::GenerateKeyedStoreICDebugBreak(MacroAssembler* masm) {
    224   // Register state for keyed IC load call (from ic-x64.cc).
    225   // ----------- S t a t e -------------
    226   //  -- rax    : value
    227   //  -- rcx    : key
    228   //  -- rdx    : receiver
    229   // -----------------------------------
    230   Generate_DebugBreakCallHelper(
    231       masm, rax.bit() | rcx.bit() | rdx.bit(), 0, false);
    232 }
    233 
    234 
    235 void Debug::GenerateCompareNilICDebugBreak(MacroAssembler* masm) {
    236   // Register state for CompareNil IC
    237   // ----------- S t a t e -------------
    238   //  -- rax    : value
    239   // -----------------------------------
    240   Generate_DebugBreakCallHelper(masm, rax.bit(), 0, false);
    241 }
    242 
    243 
    244 void Debug::GenerateCallICDebugBreak(MacroAssembler* masm) {
    245   // Register state for IC call call (from ic-x64.cc)
    246   // ----------- S t a t e -------------
    247   //  -- rcx: function name
    248   // -----------------------------------
    249   Generate_DebugBreakCallHelper(masm, rcx.bit(), 0, false);
    250 }
    251 
    252 
    253 void Debug::GenerateReturnDebugBreak(MacroAssembler* masm) {
    254   // Register state just before return from JS function (from codegen-x64.cc).
    255   // ----------- S t a t e -------------
    256   //  -- rax: return value
    257   // -----------------------------------
    258   Generate_DebugBreakCallHelper(masm, rax.bit(), 0, true);
    259 }
    260 
    261 
    262 void Debug::GenerateCallFunctionStubDebugBreak(MacroAssembler* masm) {
    263   // Register state for CallFunctionStub (from code-stubs-x64.cc).
    264   // ----------- S t a t e -------------
    265   //  -- rdi : function
    266   // -----------------------------------
    267   Generate_DebugBreakCallHelper(masm, rdi.bit(), 0, false);
    268 }
    269 
    270 
    271 void Debug::GenerateCallFunctionStubRecordDebugBreak(MacroAssembler* masm) {
    272   // Register state for CallFunctionStub (from code-stubs-x64.cc).
    273   // ----------- S t a t e -------------
    274   //  -- rdi : function
    275   //  -- rbx: cache cell for call target
    276   // -----------------------------------
    277   Generate_DebugBreakCallHelper(masm, rbx.bit() | rdi.bit(), 0, false);
    278 }
    279 
    280 
    281 void Debug::GenerateCallConstructStubDebugBreak(MacroAssembler* masm) {
    282   // Register state for CallConstructStub (from code-stubs-x64.cc).
    283   // rax is the actual number of arguments not encoded as a smi, see comment
    284   // above IC call.
    285   // ----------- S t a t e -------------
    286   //  -- rax: number of arguments
    287   // -----------------------------------
    288   // The number of arguments in rax is not smi encoded.
    289   Generate_DebugBreakCallHelper(masm, rdi.bit(), rax.bit(), false);
    290 }
    291 
    292 
    293 void Debug::GenerateCallConstructStubRecordDebugBreak(MacroAssembler* masm) {
    294   // Register state for CallConstructStub (from code-stubs-x64.cc).
    295   // rax is the actual number of arguments not encoded as a smi, see comment
    296   // above IC call.
    297   // ----------- S t a t e -------------
    298   //  -- rax: number of arguments
    299   //  -- rbx: cache cell for call target
    300   // -----------------------------------
    301   // The number of arguments in rax is not smi encoded.
    302   Generate_DebugBreakCallHelper(masm, rbx.bit() | rdi.bit(), rax.bit(), false);
    303 }
    304 
    305 
    306 void Debug::GenerateSlot(MacroAssembler* masm) {
    307   // Generate enough nop's to make space for a call instruction.
    308   Label check_codesize;
    309   __ bind(&check_codesize);
    310   __ RecordDebugBreakSlot();
    311   __ Nop(Assembler::kDebugBreakSlotLength);
    312   ASSERT_EQ(Assembler::kDebugBreakSlotLength,
    313             masm->SizeOfCodeGeneratedSince(&check_codesize));
    314 }
    315 
    316 
    317 void Debug::GenerateSlotDebugBreak(MacroAssembler* masm) {
    318   // In the places where a debug break slot is inserted no registers can contain
    319   // object pointers.
    320   Generate_DebugBreakCallHelper(masm, 0, 0, true);
    321 }
    322 
    323 
    324 void Debug::GeneratePlainReturnLiveEdit(MacroAssembler* masm) {
    325   masm->ret(0);
    326 }
    327 
    328 
    329 void Debug::GenerateFrameDropperLiveEdit(MacroAssembler* masm) {
    330   ExternalReference restarter_frame_function_slot =
    331       ExternalReference(Debug_Address::RestarterFrameFunctionPointer(),
    332                         masm->isolate());
    333   __ movq(rax, restarter_frame_function_slot);
    334   __ movq(Operand(rax, 0), Immediate(0));
    335 
    336   // We do not know our frame height, but set rsp based on rbp.
    337   __ lea(rsp, Operand(rbp, -1 * kPointerSize));
    338 
    339   __ pop(rdi);  // Function.
    340   __ pop(rbp);
    341 
    342   // Load context from the function.
    343   __ movq(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
    344 
    345   // Get function code.
    346   __ movq(rdx, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
    347   __ movq(rdx, FieldOperand(rdx, SharedFunctionInfo::kCodeOffset));
    348   __ lea(rdx, FieldOperand(rdx, Code::kHeaderSize));
    349 
    350   // Re-run JSFunction, rdi is function, rsi is context.
    351   __ jmp(rdx);
    352 }
    353 
    354 const bool Debug::kFrameDropperSupported = true;
    355 
    356 #undef __
    357 
    358 #endif  // ENABLE_DEBUGGER_SUPPORT
    359 
    360 } }  // namespace v8::internal
    361 
    362 #endif  // V8_TARGET_ARCH_X64
    363