dart-sdk/runtime/vm/cpu_arm.cc
なつき c50940b56b Fallback to uname for detecting ARM CPU architecture
Currently running official dart `linux/arm/v7` container image on x86_64 with QEMU results in `Unrecognized ARM CPU architecture`.

This is due to QEMU reporting host `/proc/cpuinfo` instead of the emulated architecture.

Checking `uname` in addition to `/proc/cpuinfo` would make `linux/arm/v7` build work consistently regardless of whether `--use-qemu` is supplied during SDK build time.

TEST=running the qemu bot
TEST=ran the ffi/hardfp_test.dart locally both in AOT and JIT mode.

Closes https://github.com/dart-lang/sdk/pull/48665
https://github.com/dart-lang/sdk/pull/48665

GitOrigin-RevId: d1414d747152d048f3ecaadfd0cd3356ce7a2205
Change-Id: If8b42a700d55e83fea0ee0035848308a3211ec43
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/238841
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
2022-04-13 07:54:02 +00:00

268 lines
8.8 KiB
C++

// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "vm/globals.h"
#if defined(TARGET_ARCH_ARM)
#include "vm/cpu.h"
#include "vm/cpu_arm.h"
#include "vm/cpuinfo.h"
#include "vm/heap/heap.h"
#include "vm/isolate.h"
#include "vm/object.h"
#include "vm/simulator.h"
#if !defined(TARGET_HOST_MISMATCH)
#if defined(DART_HOST_OS_MACOS) || defined(DART_HOST_OS_IOS)
#include <libkern/OSCacheControl.h>
#elif defined(DART_HOST_OS_WINDOWS)
#include <processthreadsapi.h>
#endif
#if !defined(DART_HOST_OS_WINDOWS)
#include <string.h> /* NOLINT */
#include <sys/utsname.h> /* NOLINT */
#endif
#endif
// ARM version differences.
// We support only ARMv7 and variants. We detect the presence of vfp,
// neon, and integer division instructions. Considering ARMv5TE as the baseline,
// later versions add the following features/instructions that we use:
//
// ARMv6:
// - PC read offset in store instructions is 8 rather than 12, matching the
// offset in read instructions,
// - strex, ldrex, and clrex load/store/clear exclusive instructions,
// - umaal multiplication instruction,
// ARMv7:
// - movw, movt 16-bit immediate load instructions,
// - mls multiplication instruction,
// - vmovs, vmovd floating point immediate load instructions.
//
// If an aarch64 CPU is detected, we generate ARMv7 code.
//
// Where we are missing vfp, we do not unbox doubles, or generate intrinsics for
// floating point operations. Where we are missing neon, we do not unbox SIMD
// values, or inline operations on SIMD values. Where we are missing integer
// division, we do not inline division operations, and we do not generate
// intrinsics that do division. See the feature tests in flow_graph_optimizer.cc
// for details.
namespace dart {
DEFINE_FLAG(bool, use_neon, true, "Use neon instructions if supported");
DEFINE_FLAG(bool,
use_integer_division,
true,
"Use integer division instruction if supported");
#if defined(TARGET_HOST_MISMATCH)
#if defined(DART_TARGET_OS_ANDROID) || defined(DART_TARGET_OS_MACOS_IOS)
DEFINE_FLAG(bool, sim_use_hardfp, false, "Use the hardfp ABI.");
#else
DEFINE_FLAG(bool, sim_use_hardfp, true, "Use the hardfp ABI.");
#endif
#endif
void CPU::FlushICache(uword start, uword size) {
#if defined(DART_PRECOMPILED_RUNTIME)
UNREACHABLE();
#elif !defined(TARGET_HOST_MISMATCH) && HOST_ARCH_ARM
// Nothing to do. Flushing no instructions.
if (size == 0) {
return;
}
// ARM recommends using the gcc intrinsic __clear_cache on Linux, and the
// library call cacheflush from unistd.h on Android:
//
// https://community.arm.com/developer/ip-products/processors/b/processors-ip-blog/posts/caches-and-self-modifying-code
//
// On iOS we use sys_icache_invalidate from Darwin. See:
//
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/sys_icache_invalidate.3.html
#if defined(DART_HOST_OS_IOS)
sys_icache_invalidate(reinterpret_cast<void*>(start), size);
#elif defined(DART_HOST_OS_LINUX)
char* beg = reinterpret_cast<char*>(start);
char* end = reinterpret_cast<char*>(start + size);
__builtin___clear_cache(beg, end);
#elif defined(DART_HOST_OS_ANDROID)
cacheflush(start, start + size, 0);
#elif defined(DART_HOST_OS_WINDOWS)
BOOL result = FlushInstructionCache(
GetCurrentProcess(), reinterpret_cast<const void*>(start), size);
ASSERT(result != 0);
#else
#error FlushICache only tested/supported on Linux, Android and iOS
#endif
#endif
}
const char* CPU::Id() {
return
#if defined(TARGET_HOST_MISMATCH)
"sim"
#endif // defined(TARGET_HOST_MISMATCH)
"arm";
}
bool HostCPUFeatures::integer_division_supported_ = false;
bool HostCPUFeatures::neon_supported_ = false;
bool HostCPUFeatures::hardfp_supported_ = false;
const char* HostCPUFeatures::hardware_ = NULL;
intptr_t HostCPUFeatures::store_pc_read_offset_ = 8;
#if defined(DEBUG)
bool HostCPUFeatures::initialized_ = false;
#endif
#if !defined(TARGET_HOST_MISMATCH)
#if DART_HOST_OS_IOS
void HostCPUFeatures::Init() {
// TODO(24743): Actually check the CPU features and fail if we're missing
// something assumed in a precompiled snapshot.
hardware_ = "";
// When the VM is targetted to ARMv7, pretend that the CPU is ARMv7 even if
// the CPU is actually AArch64.
integer_division_supported_ = FLAG_use_integer_division;
neon_supported_ = FLAG_use_neon;
hardfp_supported_ = false;
#if defined(DEBUG)
initialized_ = true;
#endif
}
#elif DART_HOST_OS_WINDOWS
void HostCPUFeatures::Init() {
hardware_ = "";
integer_division_supported_ = true;
neon_supported_ = true;
hardfp_supported_ = true;
#if defined(DEBUG)
initialized_ = true;
#endif
}
#else // DART_HOST_OS_IOS
void HostCPUFeatures::Init() {
bool is_arm64 = false;
CpuInfo::Init();
hardware_ = CpuInfo::GetCpuModel();
// QEMU may report host cpuinfo instead of emulated cpuinfo, use uname as a
// fallback for checking if CPU is AArch64 or ARMv7.
struct utsname uname_;
int ret_ = uname(&uname_);
// Check for ARMv7, or aarch64.
// It can be in either the Processor or Model information fields.
if (CpuInfo::FieldContains(kCpuInfoProcessor, "aarch64") ||
CpuInfo::FieldContains(kCpuInfoModel, "aarch64") ||
CpuInfo::FieldContains(kCpuInfoArchitecture, "8") ||
CpuInfo::FieldContains(kCpuInfoArchitecture, "AArch64") ||
(ret_ == 0 && (strstr(uname_.machine, "aarch64") != NULL ||
strstr(uname_.machine, "arm64") != NULL ||
strstr(uname_.machine, "armv8") != NULL))) {
// pretend that this arm64 cpu is really an ARMv7
is_arm64 = true;
} else if (!CpuInfo::FieldContains(kCpuInfoProcessor, "ARMv7") &&
!CpuInfo::FieldContains(kCpuInfoModel, "ARMv7") &&
!CpuInfo::FieldContains(kCpuInfoArchitecture, "7") &&
!(ret_ == 0 && strstr(uname_.machine, "armv7") != NULL)) {
FATAL("Unrecognized ARM CPU architecture.");
}
// Has integer division.
// Special cases:
// - Qualcomm Krait CPUs (QCT APQ8064) in Nexus 4 and 7 incorrectly report
// that they lack integer division.
// - Marvell Armada 370/XP incorrectly reports that it has integer division.
bool is_krait = CpuInfo::FieldContains(kCpuInfoHardware, "QCT APQ8064");
bool is_armada_370xp =
CpuInfo::FieldContains(kCpuInfoHardware, "Marvell Armada 370/XP");
bool is_virtual_machine =
CpuInfo::FieldContains(kCpuInfoHardware, "Dummy Virtual Machine");
#if defined(DART_HOST_OS_ANDROID)
bool is_android = true;
#else
bool is_android = false;
#endif
if (is_krait) {
integer_division_supported_ = FLAG_use_integer_division;
} else if (is_android && is_arm64) {
// Various Android ARM64 devices, including the Qualcomm Snapdragon 820/821
// CPUs (MSM 8996 and MSM8996pro) in Xiaomi MI5 and Pixel lack integer
// division even though ARMv8 requires it in A32. Instead of attempting to
// track all of these devices, we conservatively disable use of integer
// division on Android ARM64 devices.
// TODO(29270): /proc/self/auxv might be more reliable here.
integer_division_supported_ = false;
} else if (is_armada_370xp) {
integer_division_supported_ = false;
} else if (is_android && !is_arm64 && is_virtual_machine) {
// Some Android ARM emulators claim support for integer division in
// /proc/cpuinfo but do not actually support it.
integer_division_supported_ = false;
} else {
integer_division_supported_ =
(CpuInfo::FieldContains(kCpuInfoFeatures, "idiva") || is_arm64) &&
FLAG_use_integer_division;
}
neon_supported_ =
(CpuInfo::FieldContains(kCpuInfoFeatures, "neon") || is_arm64) &&
FLAG_use_neon;
// Use the cross-compiler's predefined macros to determine whether we should
// use the hard or soft float ABI.
#if defined(__ARM_PCS_VFP)
hardfp_supported_ = true;
#else
hardfp_supported_ = false;
#endif
#if defined(DEBUG)
initialized_ = true;
#endif
}
#endif // DART_HOST_OS_IOS
void HostCPUFeatures::Cleanup() {
DEBUG_ASSERT(initialized_);
#if defined(DEBUG)
initialized_ = false;
#endif
ASSERT(hardware_ != NULL);
free(const_cast<char*>(hardware_));
hardware_ = NULL;
CpuInfo::Cleanup();
}
#else
void HostCPUFeatures::Init() {
CpuInfo::Init();
hardware_ = CpuInfo::GetCpuModel();
integer_division_supported_ = FLAG_use_integer_division;
neon_supported_ = FLAG_use_neon;
hardfp_supported_ = FLAG_sim_use_hardfp;
#if defined(DEBUG)
initialized_ = true;
#endif
}
void HostCPUFeatures::Cleanup() {
DEBUG_ASSERT(initialized_);
#if defined(DEBUG)
initialized_ = false;
#endif
ASSERT(hardware_ != NULL);
free(const_cast<char*>(hardware_));
hardware_ = NULL;
CpuInfo::Cleanup();
}
#endif // !defined(TARGET_HOST_MISMATCH)
} // namespace dart
#endif // defined TARGET_ARCH_ARM