checksum.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //===-- checksum.cpp --------------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "checksum.h"
  9. #include "atomic_helpers.h"
  10. #include "chunk.h"
  11. #if defined(__x86_64__) || defined(__i386__)
  12. #include <cpuid.h>
  13. #elif defined(__arm__) || defined(__aarch64__)
  14. #if SCUDO_FUCHSIA
  15. #error #include <zircon/features.h>
  16. #error #include <zircon/syscalls.h>
  17. #else
  18. #include <sys/auxv.h>
  19. #endif
  20. #endif
  21. namespace scudo {
  22. Checksum HashAlgorithm = {Checksum::BSD};
  23. #if defined(__x86_64__) || defined(__i386__)
  24. // i386 and x86_64 specific code to detect CRC32 hardware support via CPUID.
  25. // CRC32 requires the SSE 4.2 instruction set.
  26. #ifndef bit_SSE4_2
  27. #define bit_SSE4_2 bit_SSE42 // clang and gcc have different defines.
  28. #endif
  29. #ifndef signature_HYGON_ebx // They are not defined in gcc.
  30. // HYGON: "HygonGenuine".
  31. #define signature_HYGON_ebx 0x6f677948
  32. #define signature_HYGON_edx 0x6e65476e
  33. #define signature_HYGON_ecx 0x656e6975
  34. #endif
  35. bool hasHardwareCRC32() {
  36. u32 Eax, Ebx = 0, Ecx = 0, Edx = 0;
  37. __get_cpuid(0, &Eax, &Ebx, &Ecx, &Edx);
  38. const bool IsIntel = (Ebx == signature_INTEL_ebx) &&
  39. (Edx == signature_INTEL_edx) &&
  40. (Ecx == signature_INTEL_ecx);
  41. const bool IsAMD = (Ebx == signature_AMD_ebx) && (Edx == signature_AMD_edx) &&
  42. (Ecx == signature_AMD_ecx);
  43. const bool IsHygon = (Ebx == signature_HYGON_ebx) &&
  44. (Edx == signature_HYGON_edx) &&
  45. (Ecx == signature_HYGON_ecx);
  46. if (!IsIntel && !IsAMD && !IsHygon)
  47. return false;
  48. __get_cpuid(1, &Eax, &Ebx, &Ecx, &Edx);
  49. return !!(Ecx & bit_SSE4_2);
  50. }
  51. #elif defined(__arm__) || defined(__aarch64__)
  52. #ifndef AT_HWCAP
  53. #define AT_HWCAP 16
  54. #endif
  55. #ifndef HWCAP_CRC32
  56. #define HWCAP_CRC32 (1U << 7) // HWCAP_CRC32 is missing on older platforms.
  57. #endif
  58. bool hasHardwareCRC32() {
  59. #if SCUDO_FUCHSIA
  60. u32 HWCap;
  61. const zx_status_t Status =
  62. zx_system_get_features(ZX_FEATURE_KIND_CPU, &HWCap);
  63. if (Status != ZX_OK)
  64. return false;
  65. return !!(HWCap & ZX_ARM64_FEATURE_ISA_CRC32);
  66. #else
  67. return !!(getauxval(AT_HWCAP) & HWCAP_CRC32);
  68. #endif // SCUDO_FUCHSIA
  69. }
  70. #else
  71. // No hardware CRC32 implemented in Scudo for other architectures.
  72. bool hasHardwareCRC32() { return false; }
  73. #endif // defined(__x86_64__) || defined(__i386__)
  74. } // namespace scudo