Skip to content

Commit f5eaefd

Browse files
rmacnak-googleCommit Queue
authored andcommitted
[vm] Runtime option to not use the simulator for simarm64_arm64.
TEST=ci Cq-Include-Trybots: luci.dart.try:vm-ffi-mac-debug-simarm64_arm64-try,vm-ffi-mac-release-simarm64_arm64-try Change-Id: Id98d337ce16ba4b434b5c69016f01eec9b85f9d2 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/434982 Reviewed-by: Slava Egorov <[email protected]> Commit-Queue: Ryan Macnak <[email protected]>
1 parent 69b9982 commit f5eaefd

File tree

12 files changed

+193
-85
lines changed

12 files changed

+193
-85
lines changed

runtime/vm/compiler/backend/il_arm64.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,8 +1429,10 @@ void FfiCallInstr::EmitNativeCode(FlowGraphCompiler* compiler) {
14291429
__ mov(CSP, SP);
14301430

14311431
#if defined(SIMULATOR_FFI)
1432-
__ Emit(Instr::kSimulatorFfiRedirectInstruction);
1433-
ASSERT(branch == R9);
1432+
if (FLAG_use_simulator) {
1433+
__ Emit(Instr::kSimulatorFfiRedirectInstruction);
1434+
ASSERT(branch == R9);
1435+
}
14341436
#endif
14351437
__ blr(branch);
14361438

runtime/vm/compiler/stub_code_compiler_arm64.cc

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,9 @@ void StubCodeCompiler::GenerateCallNativeThroughSafepointStub() {
417417
#endif
418418

419419
#if defined(SIMULATOR_FFI)
420-
__ Emit(Instr::kSimulatorFfiRedirectInstruction);
420+
if (FLAG_use_simulator) {
421+
__ Emit(Instr::kSimulatorFfiRedirectInstruction);
422+
}
421423
#endif
422424
__ blr(R9);
423425

@@ -469,11 +471,6 @@ void StubCodeCompiler::GenerateLoadFfiCallbackMetadataRuntimeFunction(
469471
}
470472

471473
void StubCodeCompiler::GenerateFfiCallbackTrampolineStub() {
472-
#if defined(USING_SIMULATOR) && !defined(DART_PRECOMPILER)
473-
// TODO(37299): FFI is not supported in SIMARM64.
474-
// See Simulator::DoDirectedFfiCallback.
475-
__ Breakpoint();
476-
#else
477474
Label body;
478475

479476
// R9 is volatile and not used for passing any arguments.
@@ -705,7 +702,6 @@ void StubCodeCompiler::GenerateFfiCallbackTrampolineStub() {
705702
__ Breakpoint();
706703
}
707704
#endif
708-
#endif // !defined(HOST_ARCH_ARM64)
709705
}
710706

711707
void StubCodeCompiler::GenerateDispatchTableNullErrorStub() {

runtime/vm/cpu_arm64.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,26 @@
1010

1111
#include "vm/cpuinfo.h"
1212

13-
#if !defined(USING_SIMULATOR)
1413
#if defined(DART_HOST_OS_FUCHSIA)
1514
#include <zircon/syscalls.h>
1615
#elif defined(DART_HOST_OS_MACOS) || defined(DART_HOST_OS_IOS)
1716
#include <libkern/OSCacheControl.h>
1817
#elif defined(DART_HOST_OS_WINDOWS)
1918
#include <processthreadsapi.h>
2019
#endif
21-
#endif
2220

2321
namespace dart {
2422

2523
void CPU::FlushICache(uword start, uword size) {
2624
#if defined(DART_PRECOMPILED_RUNTIME)
2725
UNREACHABLE();
28-
#elif !defined(USING_SIMULATOR)
26+
#else
27+
#if defined(USING_SIMULATOR)
28+
if (FLAG_use_simulator) {
29+
return;
30+
}
31+
#endif
32+
2933
// Nothing to do. Flushing no instructions.
3034
if (size == 0) {
3135
return;

runtime/vm/dart_entry.cc

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,20 @@ static ObjectPtr InvokeDartCode(uword entry_point,
104104

105105
const uword stub = StubCode::InvokeDartCode().EntryPoint();
106106
#if defined(USING_SIMULATOR)
107-
auto invoke = [&](uword entry_point, uword arguments_descriptor,
108-
uword arguments, Thread* thread) -> uword {
109-
return Simulator::Current()->Call(stub, entry_point, arguments_descriptor,
110-
arguments,
111-
reinterpret_cast<int64_t>(thread));
112-
};
113-
#else
114-
auto invoke = reinterpret_cast<invokestub>(stub);
107+
if (FLAG_use_simulator) {
108+
auto invoke = [&](uword entry_point, uword arguments_descriptor,
109+
uword arguments, Thread* thread) -> uword {
110+
return Simulator::Current()->Call(stub, entry_point, arguments_descriptor,
111+
arguments,
112+
reinterpret_cast<int64_t>(thread));
113+
};
114+
uword result =
115+
invoke(entry_point, static_cast<uword>(arguments_descriptor.ptr()),
116+
static_cast<uword>(arguments.ptr()), thread);
117+
return static_cast<ObjectPtr>(result);
118+
}
115119
#endif
120+
auto invoke = reinterpret_cast<invokestub>(stub);
116121
uword result =
117122
invoke(entry_point, static_cast<uword>(arguments_descriptor.ptr()),
118123
static_cast<uword>(arguments.ptr()), thread);

runtime/vm/exceptions.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,9 +627,12 @@ NO_SANITIZE_SAFE_STACK // This function manipulates the safestack pointer.
627627
// exception object in the kExceptionObjectReg register and the stacktrace
628628
// object (may be raw null) in the kStackTraceObjectReg register.
629629

630-
Simulator::Current()->JumpToFrame(program_counter, stack_pointer,
631-
frame_pointer, thread);
632-
#else
630+
if (FLAG_use_simulator) {
631+
Simulator::Current()->JumpToFrame(program_counter, stack_pointer,
632+
frame_pointer, thread);
633+
UNREACHABLE();
634+
}
635+
#endif
633636

634637
// Zero out HWASAN tags from the current stack pointer to the destination.
635638
//
@@ -677,7 +680,6 @@ NO_SANITIZE_SAFE_STACK // This function manipulates the safestack pointer.
677680
}
678681
func(program_counter, stack_pointer, frame_pointer, thread);
679682

680-
#endif
681683
UNREACHABLE();
682684
}
683685

runtime/vm/ffi_callback_metadata.cc

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,19 @@ void FfiCallbackMetadata::EnsureStubPageLocked() {
2929
ASSERT_LESS_OR_EQUAL(VirtualMemory::PageSize(), kPageSize);
3030

3131
#if defined(SIMULATOR_FFI) && defined(HOST_ARCH_ARM64)
32-
const uword code_start =
33-
reinterpret_cast<uword>(SimulatorFfiCallbackTrampoline);
34-
const uword code_end =
35-
reinterpret_cast<uword>(SimulatorFfiCallbackTrampolineEnd);
36-
const uword page_start = code_start & ~(VirtualMemory::PageSize() - 1);
32+
uword code_start, code_end, page_start;
33+
if (FLAG_use_simulator) {
34+
code_start = reinterpret_cast<uword>(SimulatorFfiCallbackTrampoline);
35+
code_end = reinterpret_cast<uword>(SimulatorFfiCallbackTrampolineEnd);
36+
page_start = code_start & ~(VirtualMemory::PageSize() - 1);
37+
} else {
38+
const Code& trampoline_code = StubCode::FfiCallbackTrampoline();
39+
code_start = trampoline_code.EntryPoint();
40+
code_end = code_start + trampoline_code.Size();
41+
page_start = code_start & ~(VirtualMemory::PageSize() - 1);
42+
ASSERT_LESS_OR_EQUAL((code_start - page_start) + trampoline_code.Size(),
43+
RXMappingSize());
44+
}
3745
#else
3846
const Code& trampoline_code = StubCode::FfiCallbackTrampoline();
3947
const uword code_start = trampoline_code.EntryPoint();
@@ -50,8 +58,7 @@ void FfiCallbackMetadata::EnsureStubPageLocked() {
5058

5159
offset_of_first_trampoline_in_page_ = code_start - page_start;
5260

53-
#if defined(DART_TARGET_OS_FUCHSIA) || \
54-
(defined(SIMULATOR_FFI) && defined(HOST_ARCH_ARM64))
61+
#if defined(DART_TARGET_OS_FUCHSIA)
5562
// On Fuchsia we can't currently duplicate pages, so use the first page of
5663
// trampolines. Store the stub page's metadata in a separately allocated RW
5764
// page.
@@ -64,6 +71,17 @@ void FfiCallbackMetadata::EnsureStubPageLocked() {
6471
for (intptr_t i = 0; i < NumCallbackTrampolinesPerPage(); ++i) {
6572
AddToFreeListLocked(&metadata_entry[i]);
6673
}
74+
#elif defined(SIMULATOR_FFI) && defined(HOST_ARCH_ARM64)
75+
if (FLAG_use_simulator) {
76+
original_metadata_page_ = VirtualMemory::AllocateAligned(
77+
MappingSize(), MappingAlignment(), /*is_executable=*/false,
78+
/*is_compressed=*/false, "FfiCallbackMetadata::TrampolinePage");
79+
MetadataEntry* metadata_entry = reinterpret_cast<MetadataEntry*>(
80+
original_metadata_page_->start() + MetadataOffset());
81+
for (intptr_t i = 0; i < NumCallbackTrampolinesPerPage(); ++i) {
82+
AddToFreeListLocked(&metadata_entry[i]);
83+
}
84+
}
6785
#endif // defined(DART_TARGET_OS_FUCHSIA)
6886
}
6987

@@ -112,12 +130,17 @@ void FfiCallbackMetadata::FillRuntimeFunction(VirtualMemory* page,
112130
}
113131

114132
VirtualMemory* FfiCallbackMetadata::AllocateTrampolinePage() {
115-
#if defined(DART_TARGET_OS_FUCHSIA) || \
116-
(defined(SIMULATOR_FFI) && defined(HOST_ARCH_ARM64))
133+
#if defined(DART_TARGET_OS_FUCHSIA)
117134
// TODO(https://dartbug.com/52579): Remove.
118135
UNREACHABLE();
119136
return nullptr;
120137
#else
138+
#if defined(SIMULATOR_FFI) && defined(HOST_ARCH_ARM64)
139+
if (FLAG_use_simulator) {
140+
UNREACHABLE();
141+
return nullptr;
142+
}
143+
#endif
121144

122145
#if defined(DART_HOST_OS_MACOS) && defined(DART_PRECOMPILED_RUNTIME)
123146
const bool should_remap_stub_page = true;
@@ -413,8 +436,13 @@ FfiCallbackMetadata::Trampoline FfiCallbackMetadata::TrampolineOfMetadataEntry(
413436
reinterpret_cast<MetadataEntry*>(start + MetadataOffset());
414437
const uword index = metadata_entry - metadata_entries;
415438
#if defined(SIMULATOR_FFI) && defined(HOST_ARCH_ARM64)
416-
return reinterpret_cast<uword>(SimulatorFfiCallbackTrampoline) +
417-
index * kNativeCallbackTrampolineSize;
439+
if (FLAG_use_simulator) {
440+
return reinterpret_cast<uword>(SimulatorFfiCallbackTrampoline) +
441+
index * kNativeCallbackTrampolineSize;
442+
} else {
443+
return start + offset_of_first_trampoline_in_page_ +
444+
index * kNativeCallbackTrampolineSize;
445+
}
418446
#elif defined(DART_TARGET_OS_FUCHSIA)
419447
return StubCode::FfiCallbackTrampoline().EntryPoint() +
420448
index * kNativeCallbackTrampolineSize;
@@ -426,8 +454,7 @@ FfiCallbackMetadata::Trampoline FfiCallbackMetadata::TrampolineOfMetadataEntry(
426454

427455
FfiCallbackMetadata::MetadataEntry*
428456
FfiCallbackMetadata::MetadataEntryOfTrampoline(Trampoline trampoline) const {
429-
#if defined(DART_TARGET_OS_FUCHSIA) || \
430-
(defined(SIMULATOR_FFI) && defined(HOST_ARCH_ARM64))
457+
#if defined(DART_TARGET_OS_FUCHSIA)
431458
// On Fuchsia the metadata page is separate to the trampoline page.
432459
// TODO(https://dartbug.com/52579): Remove.
433460
const uword page_start =
@@ -440,6 +467,27 @@ FfiCallbackMetadata::MetadataEntryOfTrampoline(Trampoline trampoline) const {
440467
MetadataEntry* metadata_etnry_table = reinterpret_cast<MetadataEntry*>(
441468
original_metadata_page_->start() + MetadataOffset());
442469
return metadata_etnry_table + index;
470+
#elif defined(SIMULATOR_FFI) && defined(HOST_ARCH_ARM64)
471+
if (FLAG_use_simulator) {
472+
const uword page_start =
473+
Utils::RoundDown(trampoline - offset_of_first_trampoline_in_page_,
474+
VirtualMemory::PageSize());
475+
const uword index =
476+
(trampoline - offset_of_first_trampoline_in_page_ - page_start) /
477+
kNativeCallbackTrampolineSize;
478+
ASSERT(index < NumCallbackTrampolinesPerPage());
479+
MetadataEntry* metadata_etnry_table = reinterpret_cast<MetadataEntry*>(
480+
original_metadata_page_->start() + MetadataOffset());
481+
return metadata_etnry_table + index;
482+
} else {
483+
const uword start = MappingStart(trampoline);
484+
MetadataEntry* metadata_entries =
485+
reinterpret_cast<MetadataEntry*>(start + MetadataOffset());
486+
const uword index =
487+
(trampoline - start - offset_of_first_trampoline_in_page_) /
488+
kNativeCallbackTrampolineSize;
489+
return &metadata_entries[index];
490+
}
443491
#else
444492
const uword start = MappingStart(trampoline);
445493
MetadataEntry* metadata_entries =

runtime/vm/flag_list.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ constexpr bool FLAG_support_il_printer = false;
249249
"Throw API error on invalid member access through native API. See " \
250250
"entry_point_pragma.md") \
251251
C(branch_coverage, false, false, bool, false, "Enable branch coverage") \
252-
C(coverage, false, false, bool, true, "Enable coverage")
252+
C(coverage, false, false, bool, true, "Enable coverage") \
253+
P(use_simulator, bool, true, "Use simulator if available")
253254

254255
#endif // RUNTIME_VM_FLAG_LIST_H_

runtime/vm/native_entry.cc

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,11 @@ extern "C" void DRT_BootstrapNativeCall(Dart_NativeArguments args,
120120
uword NativeEntry::BootstrapNativeCallWrapperEntry() {
121121
uword entry = reinterpret_cast<uword>(DRT_BootstrapNativeCall);
122122
#if defined(USING_SIMULATOR)
123-
entry = Simulator::RedirectExternalReference(
124-
entry, Simulator::kNativeCallWrapper,
125-
NativeEntry::kNumCallWrapperArguments);
123+
if (FLAG_use_simulator) {
124+
entry = Simulator::RedirectExternalReference(
125+
entry, Simulator::kNativeCallWrapper,
126+
NativeEntry::kNumCallWrapperArguments);
127+
}
126128
#endif
127129
return entry;
128130
}
@@ -164,9 +166,11 @@ extern "C" void DRT_NoScopeNativeCall(Dart_NativeArguments args,
164166
uword NativeEntry::NoScopeNativeCallWrapperEntry() {
165167
uword entry = reinterpret_cast<uword>(DRT_NoScopeNativeCall);
166168
#if defined(USING_SIMULATOR)
167-
entry = Simulator::RedirectExternalReference(
168-
entry, Simulator::kNativeCallWrapper,
169-
NativeEntry::kNumCallWrapperArguments);
169+
if (FLAG_use_simulator) {
170+
entry = Simulator::RedirectExternalReference(
171+
entry, Simulator::kNativeCallWrapper,
172+
NativeEntry::kNumCallWrapperArguments);
173+
}
170174
#endif
171175
return entry;
172176
}
@@ -195,9 +199,11 @@ extern "C" void DRT_AutoScopeNativeCall(Dart_NativeArguments args,
195199
uword NativeEntry::AutoScopeNativeCallWrapperEntry() {
196200
uword entry = reinterpret_cast<uword>(DRT_AutoScopeNativeCall);
197201
#if defined(USING_SIMULATOR)
198-
entry = Simulator::RedirectExternalReference(
199-
entry, Simulator::kNativeCallWrapper,
200-
NativeEntry::kNumCallWrapperArguments);
202+
if (FLAG_use_simulator) {
203+
entry = Simulator::RedirectExternalReference(
204+
entry, Simulator::kNativeCallWrapper,
205+
NativeEntry::kNumCallWrapperArguments);
206+
}
201207
#endif
202208
return entry;
203209
}

runtime/vm/profiler.cc

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ static bool GetAndValidateThreadStackBounds(OSThread* os_thread,
365365

366366
#if defined(USING_SIMULATOR)
367367
const bool use_simulator_stack_bounds =
368-
thread != nullptr && thread->IsExecutingDartCode();
368+
FLAG_use_simulator && thread != nullptr && thread->IsExecutingDartCode();
369369
if (use_simulator_stack_bounds) {
370370
Isolate* isolate = thread->isolate();
371371
ASSERT(isolate != nullptr);
@@ -1253,9 +1253,11 @@ static Sample* SetupSample(Thread* thread,
12531253
// When running in the simulator, the runtime entry function address
12541254
// (stored as the vm tag) is the address of a redirect function.
12551255
// Attempt to find the real runtime entry function address and use that.
1256-
uword redirect_vm_tag = Simulator::FunctionForRedirect(vm_tag);
1257-
if (redirect_vm_tag != 0) {
1258-
vm_tag = redirect_vm_tag;
1256+
if (FLAG_use_simulator) {
1257+
uword redirect_vm_tag = Simulator::FunctionForRedirect(vm_tag);
1258+
if (redirect_vm_tag != 0) {
1259+
vm_tag = redirect_vm_tag;
1260+
}
12591261
}
12601262
#endif
12611263
sample->set_vm_tag(vm_tag);
@@ -1388,18 +1390,19 @@ void Profiler::SampleThread(Thread* thread,
13881390
uintptr_t fp = state.fp;
13891391
uintptr_t pc = state.pc;
13901392
uintptr_t lr = state.lr;
1391-
#if defined(USING_SIMULATOR)
1392-
Simulator* simulator = nullptr;
1393-
#endif
13941393

13951394
if (in_dart_code) {
1396-
// If we're in Dart code, use the Dart stack pointer.
1395+
// If we're in Dart code, use the Dart stack pointer.
13971396
#if defined(USING_SIMULATOR)
1398-
simulator = isolate->simulator();
1399-
sp = simulator->get_register(SPREG);
1400-
fp = simulator->get_register(FPREG);
1401-
pc = simulator->get_pc();
1402-
lr = simulator->get_lr();
1397+
if (FLAG_use_simulator) {
1398+
Simulator* simulator = isolate->simulator();
1399+
sp = simulator->get_register(SPREG);
1400+
fp = simulator->get_register(FPREG);
1401+
pc = simulator->get_pc();
1402+
lr = simulator->get_lr();
1403+
} else {
1404+
sp = state.dsp;
1405+
}
14031406
#else
14041407
sp = state.dsp;
14051408
#endif

0 commit comments

Comments
 (0)