2019-04-30 17:56:06 +00:00
|
|
|
// Copyright (c) 2018, Arm Limited and affiliates. All rights reserved.
|
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
|
|
|
|
|
|
|
#include "util/crc32c_arm64.h"
|
|
|
|
|
2020-12-04 23:21:18 +00:00
|
|
|
#if defined(HAVE_ARM64_CRC)
|
2019-04-30 17:56:06 +00:00
|
|
|
|
2020-12-04 23:21:18 +00:00
|
|
|
#if defined(__linux__)
|
2019-04-30 17:56:06 +00:00
|
|
|
#include <asm/hwcap.h>
|
2020-12-04 23:21:18 +00:00
|
|
|
#endif
|
2020-03-04 02:06:56 +00:00
|
|
|
#ifdef ROCKSDB_AUXV_GETAUXVAL_PRESENT
|
2019-04-30 17:56:06 +00:00
|
|
|
#include <sys/auxv.h>
|
2020-03-04 02:06:56 +00:00
|
|
|
#endif
|
2019-04-30 17:56:06 +00:00
|
|
|
#ifndef HWCAP_CRC32
|
|
|
|
#define HWCAP_CRC32 (1 << 7)
|
|
|
|
#endif
|
2020-09-22 17:39:54 +00:00
|
|
|
#ifndef HWCAP_PMULL
|
|
|
|
#define HWCAP_PMULL (1 << 4)
|
|
|
|
#endif
|
2021-03-10 17:04:01 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
2022-11-02 21:35:27 +00:00
|
|
|
#if defined(__OpenBSD__)
|
|
|
|
#include <machine/armreg.h>
|
2023-05-18 02:44:24 +00:00
|
|
|
#include <machine/cpu.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/types.h>
|
2022-11-02 21:35:27 +00:00
|
|
|
#endif
|
2019-08-23 18:02:06 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_ARM64_CRYPTO
|
|
|
|
/* unfolding to compute 8 * 3 = 24 bytes parallelly */
|
2019-09-20 19:00:55 +00:00
|
|
|
#define CRC32C24BYTES(ITR) \
|
|
|
|
crc1 = crc32c_u64(crc1, *(buf64 + BLK_LENGTH + (ITR))); \
|
|
|
|
crc2 = crc32c_u64(crc2, *(buf64 + BLK_LENGTH * 2 + (ITR))); \
|
2019-08-23 18:02:06 +00:00
|
|
|
crc0 = crc32c_u64(crc0, *(buf64 + (ITR)));
|
|
|
|
|
|
|
|
/* unfolding to compute 24 * 7 = 168 bytes parallelly */
|
2019-09-20 19:00:55 +00:00
|
|
|
#define CRC32C7X24BYTES(ITR) \
|
|
|
|
do { \
|
|
|
|
CRC32C24BYTES((ITR)*7 + 0) \
|
|
|
|
CRC32C24BYTES((ITR)*7 + 1) \
|
|
|
|
CRC32C24BYTES((ITR)*7 + 2) \
|
|
|
|
CRC32C24BYTES((ITR)*7 + 3) \
|
|
|
|
CRC32C24BYTES((ITR)*7 + 4) \
|
|
|
|
CRC32C24BYTES((ITR)*7 + 5) \
|
|
|
|
CRC32C24BYTES((ITR)*7 + 6) \
|
|
|
|
} while (0)
|
2019-08-23 18:02:06 +00:00
|
|
|
#endif
|
|
|
|
|
2020-09-22 17:39:54 +00:00
|
|
|
extern bool pmull_runtime_flag;
|
|
|
|
|
2019-04-30 17:56:06 +00:00
|
|
|
uint32_t crc32c_runtime_check(void) {
|
2022-11-02 21:35:27 +00:00
|
|
|
#if defined(ROCKSDB_AUXV_GETAUXVAL_PRESENT) || defined(__FreeBSD__)
|
2020-12-08 21:32:04 +00:00
|
|
|
uint64_t auxv = 0;
|
|
|
|
#if defined(ROCKSDB_AUXV_GETAUXVAL_PRESENT)
|
|
|
|
auxv = getauxval(AT_HWCAP);
|
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
elf_aux_info(AT_HWCAP, &auxv, sizeof(auxv));
|
2020-03-04 02:06:56 +00:00
|
|
|
#endif
|
2020-12-08 21:32:04 +00:00
|
|
|
return (auxv & HWCAP_CRC32) != 0;
|
2022-11-02 21:35:27 +00:00
|
|
|
#elif defined(__APPLE__)
|
2021-03-10 17:04:01 +00:00
|
|
|
int r;
|
|
|
|
size_t l = sizeof(r);
|
|
|
|
if (sysctlbyname("hw.optional.armv8_crc32", &r, &l, NULL, 0) == -1) return 0;
|
|
|
|
return r == 1;
|
2022-11-02 21:35:27 +00:00
|
|
|
#elif defined(__OpenBSD__)
|
|
|
|
int r = 0;
|
2023-05-18 02:44:24 +00:00
|
|
|
const int isar0_mib[] = {CTL_MACHDEP, CPU_ID_AA64ISAR0};
|
2022-11-02 21:35:27 +00:00
|
|
|
uint64_t isar0;
|
|
|
|
size_t len = sizeof(isar0);
|
|
|
|
|
|
|
|
if (sysctl(isar0_mib, 2, &isar0, &len, NULL, 0) != -1) {
|
2023-05-18 02:44:24 +00:00
|
|
|
if (ID_AA64ISAR0_CRC32(isar0) >= ID_AA64ISAR0_CRC32_BASE) r = 1;
|
2022-11-02 21:35:27 +00:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
#else
|
|
|
|
return 0;
|
2021-03-10 17:04:01 +00:00
|
|
|
#endif
|
2019-04-30 17:56:06 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 17:39:54 +00:00
|
|
|
bool crc32c_pmull_runtime_check(void) {
|
2022-11-02 21:35:27 +00:00
|
|
|
#if defined(ROCKSDB_AUXV_GETAUXVAL_PRESENT) || defined(__FreeBSD__)
|
2020-12-08 21:32:04 +00:00
|
|
|
uint64_t auxv = 0;
|
|
|
|
#if defined(ROCKSDB_AUXV_GETAUXVAL_PRESENT)
|
|
|
|
auxv = getauxval(AT_HWCAP);
|
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
elf_aux_info(AT_HWCAP, &auxv, sizeof(auxv));
|
2020-09-22 17:39:54 +00:00
|
|
|
#endif
|
2020-12-08 21:32:04 +00:00
|
|
|
return (auxv & HWCAP_PMULL) != 0;
|
2022-11-02 21:35:27 +00:00
|
|
|
#elif defined(__APPLE__)
|
2021-03-10 17:04:01 +00:00
|
|
|
return true;
|
2022-11-02 21:35:27 +00:00
|
|
|
#elif defined(__OpenBSD__)
|
|
|
|
bool r = false;
|
2023-05-18 02:44:24 +00:00
|
|
|
const int isar0_mib[] = {CTL_MACHDEP, CPU_ID_AA64ISAR0};
|
2022-11-02 21:35:27 +00:00
|
|
|
uint64_t isar0;
|
|
|
|
size_t len = sizeof(isar0);
|
|
|
|
|
|
|
|
if (sysctl(isar0_mib, 2, &isar0, &len, NULL, 0) != -1) {
|
2023-05-18 02:44:24 +00:00
|
|
|
if (ID_AA64ISAR0_AES(isar0) >= ID_AA64ISAR0_AES_PMULL) r = true;
|
2022-11-02 21:35:27 +00:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
#else
|
|
|
|
return false;
|
2021-03-10 17:04:01 +00:00
|
|
|
#endif
|
2020-09-22 17:39:54 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 21:11:03 +00:00
|
|
|
#ifdef ROCKSDB_UBSAN_RUN
|
|
|
|
#if defined(__clang__)
|
|
|
|
__attribute__((__no_sanitize__("alignment")))
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
__attribute__((__no_sanitize_undefined__))
|
|
|
|
#endif
|
|
|
|
#endif
|
2020-12-04 23:21:18 +00:00
|
|
|
uint32_t
|
|
|
|
crc32c_arm64(uint32_t crc, unsigned char const *data, size_t len) {
|
2019-07-17 18:19:06 +00:00
|
|
|
const uint8_t *buf8;
|
|
|
|
const uint64_t *buf64 = (uint64_t *)data;
|
|
|
|
int length = (int)len;
|
|
|
|
crc ^= 0xffffffff;
|
2019-04-30 17:56:06 +00:00
|
|
|
|
2020-09-22 17:39:54 +00:00
|
|
|
/*
|
|
|
|
* Pmull runtime check here.
|
|
|
|
* Raspberry Pi supports crc32 but doesn't support pmull.
|
|
|
|
* Skip Crc32c Parallel computation if no crypto extension available.
|
|
|
|
*/
|
|
|
|
if (pmull_runtime_flag) {
|
|
|
|
/* Macro (HAVE_ARM64_CRYPTO) is used for compiling check */
|
2019-07-17 18:19:06 +00:00
|
|
|
#ifdef HAVE_ARM64_CRYPTO
|
2019-09-20 19:00:55 +00:00
|
|
|
/* Crc32c Parallel computation
|
|
|
|
* Algorithm comes from Intel whitepaper:
|
|
|
|
* crc-iscsi-polynomial-crc32-instruction-paper
|
|
|
|
*
|
|
|
|
* Input data is divided into three equal-sized blocks
|
|
|
|
* Three parallel blocks (crc0, crc1, crc2) for 1024 Bytes
|
|
|
|
* One Block: 42(BLK_LENGTH) * 8(step length: crc32c_u64) bytes
|
|
|
|
*/
|
|
|
|
#define BLK_LENGTH 42
|
2020-09-22 17:39:54 +00:00
|
|
|
while (length >= 1024) {
|
|
|
|
uint64_t t0, t1;
|
|
|
|
uint32_t crc0 = 0, crc1 = 0, crc2 = 0;
|
|
|
|
|
|
|
|
/* Parallel Param:
|
|
|
|
* k0 = CRC32(x ^ (42 * 8 * 8 * 2 - 1));
|
|
|
|
* k1 = CRC32(x ^ (42 * 8 * 8 - 1));
|
|
|
|
*/
|
|
|
|
uint32_t k0 = 0xe417f38a, k1 = 0x8f158014;
|
|
|
|
|
|
|
|
/* Prefetch data for following block to avoid cache miss */
|
|
|
|
PREF1KL1((uint8_t *)buf64, 1024);
|
|
|
|
|
|
|
|
/* First 8 byte for better pipelining */
|
|
|
|
crc0 = crc32c_u64(crc, *buf64++);
|
|
|
|
|
|
|
|
/* 3 blocks crc32c parallel computation
|
|
|
|
* Macro unfolding to compute parallelly
|
|
|
|
* 168 * 6 = 1008 (bytes)
|
|
|
|
*/
|
|
|
|
CRC32C7X24BYTES(0);
|
|
|
|
CRC32C7X24BYTES(1);
|
|
|
|
CRC32C7X24BYTES(2);
|
|
|
|
CRC32C7X24BYTES(3);
|
|
|
|
CRC32C7X24BYTES(4);
|
|
|
|
CRC32C7X24BYTES(5);
|
|
|
|
buf64 += (BLK_LENGTH * 3);
|
|
|
|
|
|
|
|
/* Last 8 bytes */
|
|
|
|
crc = crc32c_u64(crc2, *buf64++);
|
|
|
|
|
|
|
|
t0 = (uint64_t)vmull_p64(crc0, k0);
|
|
|
|
t1 = (uint64_t)vmull_p64(crc1, k1);
|
|
|
|
|
|
|
|
/* Merge (crc0, crc1, crc2) -> crc */
|
|
|
|
crc1 = crc32c_u64(0, t1);
|
|
|
|
crc ^= crc1;
|
|
|
|
crc0 = crc32c_u64(0, t0);
|
|
|
|
crc ^= crc0;
|
|
|
|
|
|
|
|
length -= 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length == 0) return crc ^ (0xffffffffU);
|
2019-07-17 18:19:06 +00:00
|
|
|
#endif
|
2020-09-22 17:39:54 +00:00
|
|
|
} // if Pmull runtime check here
|
|
|
|
|
2019-07-17 18:19:06 +00:00
|
|
|
buf8 = (const uint8_t *)buf64;
|
|
|
|
while (length >= 8) {
|
2019-09-20 19:00:55 +00:00
|
|
|
crc = crc32c_u64(crc, *(const uint64_t *)buf8);
|
2019-07-17 18:19:06 +00:00
|
|
|
buf8 += 8;
|
|
|
|
length -= 8;
|
2019-04-30 17:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The following is more efficient than the straight loop */
|
2019-07-17 18:19:06 +00:00
|
|
|
if (length >= 4) {
|
2019-09-20 19:00:55 +00:00
|
|
|
crc = crc32c_u32(crc, *(const uint32_t *)buf8);
|
2019-07-17 18:19:06 +00:00
|
|
|
buf8 += 4;
|
2019-04-30 17:56:06 +00:00
|
|
|
length -= 4;
|
|
|
|
}
|
|
|
|
|
2019-07-17 18:19:06 +00:00
|
|
|
if (length >= 2) {
|
2019-09-20 19:00:55 +00:00
|
|
|
crc = crc32c_u16(crc, *(const uint16_t *)buf8);
|
2019-07-17 18:19:06 +00:00
|
|
|
buf8 += 2;
|
2019-04-30 17:56:06 +00:00
|
|
|
length -= 2;
|
|
|
|
}
|
|
|
|
|
2019-09-20 19:00:55 +00:00
|
|
|
if (length >= 1) crc = crc32c_u8(crc, *buf8);
|
2019-04-30 17:56:06 +00:00
|
|
|
|
|
|
|
crc ^= 0xffffffff;
|
|
|
|
return crc;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|