diff --git a/cpu.cpp b/cpu.cpp index ae6c9455..0343f4ec 100644 --- a/cpu.cpp +++ b/cpu.cpp @@ -458,16 +458,16 @@ void DetectX86Features() CpuId(0xC0000005, 0, cpuid2); g_cacheLineSize = GETBYTE(cpuid2[2] /*ECX*/, 0); } -#if defined(_SC_LEVEL1_DCACHE_LINESIZE) - else - { - int cacheLineSize = sysconf(_SC_LEVEL1_DCACHE_LINESIZE); - if (cacheLineSize > 0) - g_cacheLineSize = cacheLineSize; - } -#endif } +#if defined(_SC_LEVEL1_DCACHE_LINESIZE) + // Glibc does not implement on some platforms. The runtime returns 0 instead of error. + // https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/posix/sysconf.c + int cacheLineSize = sysconf(_SC_LEVEL1_DCACHE_LINESIZE); + if (cacheLineSize > 0) + g_cacheLineSize = cacheLineSize; +#endif + if (g_cacheLineSize == 0) g_cacheLineSize = CRYPTOPP_L1_CACHE_LINE_SIZE; @@ -849,7 +849,7 @@ void DetectArmFeatures() g_hasSM3 = CPU_QuerySM3(); // || CPU_ProbeSM3(); g_hasSM4 = CPU_QuerySM4(); // || CPU_ProbeSM4(); -#if defined(__linux__) && defined(_SC_LEVEL1_DCACHE_LINESIZE) +#if defined(_SC_LEVEL1_DCACHE_LINESIZE) // Glibc does not implement on some platforms. The runtime returns 0 instead of error. // https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/posix/sysconf.c int cacheLineSize = sysconf(_SC_LEVEL1_DCACHE_LINESIZE); @@ -857,6 +857,9 @@ void DetectArmFeatures() g_cacheLineSize = cacheLineSize; #endif + if (g_cacheLineSize == 0) + g_cacheLineSize = CRYPTOPP_L1_CACHE_LINE_SIZE; + *const_cast(&g_ArmDetectionDone) = true; } @@ -1066,7 +1069,7 @@ void DetectPowerpcFeatures() int cacheLineSize = getsystemcfg(SC_L1C_DLS); if (cacheLineSize > 0) g_cacheLineSize = cacheLineSize; -#elif defined(__linux__) && defined(_SC_LEVEL1_DCACHE_LINESIZE) +#elif defined(_SC_LEVEL1_DCACHE_LINESIZE) // Glibc does not implement on some platforms. The runtime returns 0 instead of error. // https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/posix/sysconf.c int cacheLineSize = sysconf(_SC_LEVEL1_DCACHE_LINESIZE); @@ -1074,6 +1077,9 @@ void DetectPowerpcFeatures() g_cacheLineSize = cacheLineSize; #endif + if (g_cacheLineSize == 0) + g_cacheLineSize = CRYPTOPP_L1_CACHE_LINE_SIZE; + *const_cast(&g_PowerpcDetectionDone) = true; }