| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/globals.h" // Needed here to get TARGET_ARCH_X64. | 5 #include "vm/globals.h" // Needed here to get TARGET_ARCH_X64. |
| 6 #if defined(TARGET_ARCH_X64) | 6 #if defined(TARGET_ARCH_X64) |
| 7 | 7 |
| 8 #include "vm/assembler.h" | 8 #include "vm/assembler.h" |
| 9 #include "vm/code_patcher.h" | 9 #include "vm/code_patcher.h" |
| 10 #include "vm/cpu.h" | 10 #include "vm/cpu.h" |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 call.set_target(new_target); | 137 call.set_target(new_target); |
| 138 } | 138 } |
| 139 | 139 |
| 140 | 140 |
| 141 void CodePatcher::PatchInstanceCallAt(uword return_address, uword new_target) { | 141 void CodePatcher::PatchInstanceCallAt(uword return_address, uword new_target) { |
| 142 InstanceCall call(return_address); | 142 InstanceCall call(return_address); |
| 143 call.set_target(new_target); | 143 call.set_target(new_target); |
| 144 } | 144 } |
| 145 | 145 |
| 146 | 146 |
| 147 static void SwapCode(intptr_t num_bytes, char* a, char* b) { | |
| 148 for (intptr_t i = 0; i < num_bytes; i++) { | |
| 149 char tmp = *a; | |
| 150 *a = *b; | |
| 151 *b = tmp; | |
| 152 a++; | |
| 153 b++; | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 | |
| 158 // The patch code buffer contains the jump code sequence which will be inserted | |
| 159 // at entry point. | |
| 160 void CodePatcher::PatchEntry(const Code& code) { | |
| 161 JumpPattern jmp_entry(code.EntryPoint()); | |
| 162 ASSERT(!jmp_entry.IsValid()); | |
| 163 const uword patch_buffer = code.GetPatchCodePc(); | |
| 164 ASSERT(patch_buffer != 0); | |
| 165 JumpPattern jmp_patch(patch_buffer); | |
| 166 ASSERT(jmp_patch.IsValid()); | |
| 167 const uword jump_target = jmp_patch.TargetAddress(); | |
| 168 SwapCode(jmp_patch.pattern_length_in_bytes(), | |
| 169 reinterpret_cast<char*>(code.EntryPoint()), | |
| 170 reinterpret_cast<char*>(patch_buffer)); | |
| 171 jmp_entry.SetTargetAddress(jump_target); | |
| 172 } | |
| 173 | |
| 174 | |
| 175 // The entry point is a jump code sequence, the patch code buffer contains | |
| 176 // original code, the entry point contains the jump code sequence. | |
| 177 void CodePatcher::RestoreEntry(const Code& code) { | |
| 178 JumpPattern jmp_entry(code.EntryPoint()); | |
| 179 ASSERT(jmp_entry.IsValid()); | |
| 180 const uword jump_target = jmp_entry.TargetAddress(); | |
| 181 const uword patch_buffer = code.GetPatchCodePc(); | |
| 182 ASSERT(patch_buffer != 0); | |
| 183 // 'patch_buffer' contains original entry code. | |
| 184 JumpPattern jmp_patch(patch_buffer); | |
| 185 ASSERT(!jmp_patch.IsValid()); | |
| 186 SwapCode(jmp_patch.pattern_length_in_bytes(), | |
| 187 reinterpret_cast<char*>(code.EntryPoint()), | |
| 188 reinterpret_cast<char*>(patch_buffer)); | |
| 189 ASSERT(jmp_patch.IsValid()); | |
| 190 jmp_patch.SetTargetAddress(jump_target); | |
| 191 } | |
| 192 | |
| 193 | |
| 194 bool CodePatcher::CodeIsPatchable(const Code& code) { | |
| 195 JumpPattern jmp_entry(code.EntryPoint()); | |
| 196 if (code.Size() < (jmp_entry.pattern_length_in_bytes() * 2)) { | |
| 197 return false; | |
| 198 } | |
| 199 uword limit = code.EntryPoint() + jmp_entry.pattern_length_in_bytes(); | |
| 200 for (intptr_t i = 0; i < code.pointer_offsets_length(); i++) { | |
| 201 const uword addr = code.GetPointerOffsetAt(i) + code.EntryPoint(); | |
| 202 if (addr < limit) { | |
| 203 return false; | |
| 204 } | |
| 205 } | |
| 206 return true; | |
| 207 } | |
| 208 | |
| 209 | |
| 210 bool CodePatcher::IsDartCall(uword return_address) { | 147 bool CodePatcher::IsDartCall(uword return_address) { |
| 211 return DartCallPattern::IsValid(return_address); | 148 return DartCallPattern::IsValid(return_address); |
| 212 } | 149 } |
| 213 | 150 |
| 214 | 151 |
| 215 uword CodePatcher::GetInstanceCallAt(uword return_address, | 152 uword CodePatcher::GetInstanceCallAt(uword return_address, |
| 216 ICData* ic_data, | 153 ICData* ic_data, |
| 217 Array* arguments_descriptor) { | 154 Array* arguments_descriptor) { |
| 218 InstanceCall call(return_address); | 155 InstanceCall call(return_address); |
| 219 if (ic_data != NULL) { | 156 if (ic_data != NULL) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 235 *reinterpret_cast<uint8_t*>(start) = 0xE8; | 172 *reinterpret_cast<uint8_t*>(start) = 0xE8; |
| 236 ShortCallPattern call(start); | 173 ShortCallPattern call(start); |
| 237 call.SetTargetAddress(target); | 174 call.SetTargetAddress(target); |
| 238 CPU::FlushICache(start, ShortCallPattern::InstructionLength()); | 175 CPU::FlushICache(start, ShortCallPattern::InstructionLength()); |
| 239 } | 176 } |
| 240 | 177 |
| 241 | 178 |
| 242 } // namespace dart | 179 } // namespace dart |
| 243 | 180 |
| 244 #endif // defined TARGET_ARCH_X64 | 181 #endif // defined TARGET_ARCH_X64 |
| OLD | NEW |