Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(313)

Side by Side Diff: runtime/vm/code_patcher.cc

Issue 1192103004: VM: New calling convention for generated code. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: fixed comments Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/vm/code_patcher.h ('k') | runtime/vm/code_patcher_arm.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/code_patcher.h" 5 #include "vm/code_patcher.h"
6 #include "vm/cpu.h" 6 #include "vm/cpu.h"
7 #include "vm/instructions.h" 7 #include "vm/instructions.h"
8 #include "vm/object.h" 8 #include "vm/object.h"
9 #include "vm/virtual_memory.h" 9 #include "vm/virtual_memory.h"
10 10
(...skipping 17 matching lines...) Expand all
28 WritableInstructionsScope::~WritableInstructionsScope() { 28 WritableInstructionsScope::~WritableInstructionsScope() {
29 if (FLAG_write_protect_code) { 29 if (FLAG_write_protect_code) {
30 bool status = VirtualMemory::Protect(reinterpret_cast<void*>(address_), 30 bool status = VirtualMemory::Protect(reinterpret_cast<void*>(address_),
31 size_, 31 size_,
32 VirtualMemory::kReadExecute); 32 VirtualMemory::kReadExecute);
33 ASSERT(status); 33 ASSERT(status);
34 } 34 }
35 } 35 }
36 36
37 37
38 static void SwapCode(intptr_t num_bytes, char* code, char* buffer) {
39 uword code_address = reinterpret_cast<uword>(code);
40 for (intptr_t i = 0; i < num_bytes; i++) {
41 char tmp = *code;
42 *code = *buffer;
43 *buffer = tmp;
44 code++;
45 buffer++;
46 }
47 CPU::FlushICache(code_address, num_bytes);
48 // The buffer is not executed. No need to flush.
49 }
50
51
52 // The patch code buffer contains the jmp code which will be inserted at 38 // The patch code buffer contains the jmp code which will be inserted at
53 // entry point. 39 // entry point.
54 void CodePatcher::PatchEntry(const Code& code) { 40 void CodePatcher::PatchEntry(const Code& code, const Code& new_code) {
55 ASSERT(!IsEntryPatched(code)); 41 ASSERT(code.instructions() == code.active_instructions());
56 const uword patch_addr = code.GetEntryPatchPc(); 42 code.set_active_instructions(new_code.instructions());
57 ASSERT(patch_addr != 0);
58 JumpPattern jmp_entry(patch_addr, code);
59 ASSERT(!jmp_entry.IsValid());
60 const uword patch_buffer = code.GetPatchCodePc();
61 ASSERT(patch_buffer != 0);
62 JumpPattern jmp_patch(patch_buffer, code);
63 ASSERT(jmp_patch.IsValid());
64 const uword jump_target = jmp_patch.TargetAddress();
65 intptr_t length = jmp_patch.pattern_length_in_bytes();
66 {
67 WritableInstructionsScope writable_code(patch_addr, length);
68 WritableInstructionsScope writable_buffer(patch_buffer, length);
69 SwapCode(jmp_patch.pattern_length_in_bytes(),
70 reinterpret_cast<char*>(patch_addr),
71 reinterpret_cast<char*>(patch_buffer));
72 jmp_entry.SetTargetAddress(jump_target);
73 }
74 } 43 }
75 44
76 45
77 // The entry point is a jmp instruction, the patch code buffer contains 46 // The entry point is a jmp instruction, the patch code buffer contains
78 // original code, the entry point contains the jump instruction. 47 // original code, the entry point contains the jump instruction.
79 void CodePatcher::RestoreEntry(const Code& code) { 48 void CodePatcher::RestoreEntry(const Code& code) {
80 if (!IsEntryPatched(code)) return; 49 if (!IsEntryPatched(code)) return;
81 const uword patch_addr = code.GetEntryPatchPc(); 50 ASSERT(code.instructions() != code.active_instructions());
82 ASSERT(patch_addr != 0); 51 code.set_active_instructions(code.instructions());
83 JumpPattern jmp_entry(patch_addr, code);
84 ASSERT(jmp_entry.IsValid());
85 const uword jump_target = jmp_entry.TargetAddress();
86 const uword patch_buffer = code.GetPatchCodePc();
87 ASSERT(patch_buffer != 0);
88 // 'patch_buffer' contains original entry code.
89 JumpPattern jmp_patch(patch_buffer, code);
90 ASSERT(!jmp_patch.IsValid());
91 intptr_t length = jmp_patch.pattern_length_in_bytes();
92 {
93 WritableInstructionsScope writable_code(patch_addr, length);
94 WritableInstructionsScope writable_buffer(patch_buffer, length);
95 SwapCode(jmp_patch.pattern_length_in_bytes(),
96 reinterpret_cast<char*>(patch_addr),
97 reinterpret_cast<char*>(patch_buffer));
98 ASSERT(jmp_patch.IsValid());
99 jmp_patch.SetTargetAddress(jump_target);
100 }
101 } 52 }
102 53
103 54
104 bool CodePatcher::IsEntryPatched(const Code& code) { 55 bool CodePatcher::IsEntryPatched(const Code& code) {
105 const uword patch_addr = code.GetEntryPatchPc(); 56 return code.instructions() != code.active_instructions();
106 if (patch_addr == 0) {
107 return false;
108 }
109 JumpPattern jmp_entry(patch_addr, code);
110 return jmp_entry.IsValid();
111 }
112
113
114 bool CodePatcher::CodeIsPatchable(const Code& code) {
115 const uword patch_addr = code.GetEntryPatchPc();
116 // Zero means means that the function is not patchable.
117 if (patch_addr == 0) {
118 return false;
119 }
120 JumpPattern jmp_entry(patch_addr, code);
121 if (code.Size() < (jmp_entry.pattern_length_in_bytes() * 2)) {
122 return false;
123 }
124 const uword limit = patch_addr + jmp_entry.pattern_length_in_bytes();
125 // Check no object stored between patch_addr .. limit.
126 for (intptr_t i = 0; i < code.pointer_offsets_length(); i++) {
127 const uword obj_start = code.GetPointerOffsetAt(i) + code.EntryPoint();
128 const uword obj_end = obj_start + kWordSize;
129 if ((obj_start < limit) && (obj_end > patch_addr)) {
130 return false;
131 }
132 }
133 return true;
134 } 57 }
135 58
136 } // namespace dart 59 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/code_patcher.h ('k') | runtime/vm/code_patcher_arm.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698