Path: blob/main/contrib/llvm-project/compiler-rt/lib/scudo/standalone/checksum.cpp
35292 views
//===-- checksum.cpp --------------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "checksum.h"9#include "atomic_helpers.h"10#include "chunk.h"1112#if defined(__x86_64__) || defined(__i386__)13#include <cpuid.h>14#elif defined(__arm__) || defined(__aarch64__)15#if SCUDO_FUCHSIA16#include <zircon/features.h>17#include <zircon/syscalls.h>18#else19#include <sys/auxv.h>20#endif21#elif defined(__loongarch__)22#include <sys/auxv.h>23#endif2425namespace scudo {2627Checksum HashAlgorithm = {Checksum::BSD};2829#if defined(__x86_64__) || defined(__i386__)30// i386 and x86_64 specific code to detect CRC32 hardware support via CPUID.31// CRC32 requires the SSE 4.2 instruction set.32#ifndef bit_SSE4_233#define bit_SSE4_2 bit_SSE42 // clang and gcc have different defines.34#endif3536#ifndef signature_HYGON_ebx // They are not defined in gcc.37// HYGON: "HygonGenuine".38#define signature_HYGON_ebx 0x6f67794839#define signature_HYGON_edx 0x6e65476e40#define signature_HYGON_ecx 0x656e697541#endif4243bool hasHardwareCRC32() {44u32 Eax, Ebx = 0, Ecx = 0, Edx = 0;45__get_cpuid(0, &Eax, &Ebx, &Ecx, &Edx);46const bool IsIntel = (Ebx == signature_INTEL_ebx) &&47(Edx == signature_INTEL_edx) &&48(Ecx == signature_INTEL_ecx);49const bool IsAMD = (Ebx == signature_AMD_ebx) && (Edx == signature_AMD_edx) &&50(Ecx == signature_AMD_ecx);51const bool IsHygon = (Ebx == signature_HYGON_ebx) &&52(Edx == signature_HYGON_edx) &&53(Ecx == signature_HYGON_ecx);54if (!IsIntel && !IsAMD && !IsHygon)55return false;56__get_cpuid(1, &Eax, &Ebx, &Ecx, &Edx);57return !!(Ecx & bit_SSE4_2);58}59#elif defined(__arm__) || defined(__aarch64__)60#ifndef AT_HWCAP61#define AT_HWCAP 1662#endif63#ifndef HWCAP_CRC3264#define HWCAP_CRC32 (1U << 7) // HWCAP_CRC32 is missing on older platforms.65#endif6667bool hasHardwareCRC32() {68#if SCUDO_FUCHSIA69u32 HWCap;70const zx_status_t Status =71zx_system_get_features(ZX_FEATURE_KIND_CPU, &HWCap);72if (Status != ZX_OK)73return false;74return !!(HWCap & ZX_ARM64_FEATURE_ISA_CRC32);75#else76return !!(getauxval(AT_HWCAP) & HWCAP_CRC32);77#endif // SCUDO_FUCHSIA78}79#elif defined(__loongarch__)80// The definition is only pulled in by <sys/auxv.h> since glibc 2.38, so81// supply it if missing.82#ifndef HWCAP_LOONGARCH_CRC3283#define HWCAP_LOONGARCH_CRC32 (1 << 6)84#endif85// Query HWCAP for platform capability, according to *Software Development and86// Build Convention for LoongArch Architectures* v0.1, Section 9.1.87//88// Link:89// https://github.com/loongson/la-softdev-convention/blob/v0.1/la-softdev-convention.adoc#kernel-development90bool hasHardwareCRC32() {91return !!(getauxval(AT_HWCAP) & HWCAP_LOONGARCH_CRC32);92}93#else94// No hardware CRC32 implemented in Scudo for other architectures.95bool hasHardwareCRC32() { return false; }96#endif // defined(__x86_64__) || defined(__i386__)9798} // namespace scudo99100101