From cb497ae8f18e3c55f81bc9f3876c8f4c3d8a2813 Mon Sep 17 00:00:00 2001 From: Lion Yang Date: Thu, 5 Jan 2017 07:06:24 +0800 Subject: [PATCH] chacha20poly1305: fix detection of BMI on amd64 This change detects BMI2 usability as an additional condition to examine the usability of AVX2 version algorithm, fixes the crash on the platfrom which supports AVX2 but not support BMI2. Change-Id: I5438d4ec84265c79a51c1439265a33b1be04878a Reviewed-on: https://go-review.googlesource.com/34852 Reviewed-by: Adam Langley Reviewed-by: Ian Lance Taylor Reviewed-by: Brad Fitzpatrick Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot --- chacha20poly1305/chacha20poly1305_amd64.s | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/chacha20poly1305/chacha20poly1305_amd64.s b/chacha20poly1305/chacha20poly1305_amd64.s index 1c895d8..39c58b4 100644 --- a/chacha20poly1305/chacha20poly1305_amd64.s +++ b/chacha20poly1305/chacha20poly1305_amd64.s @@ -278,8 +278,15 @@ TEXT ·chacha20Poly1305Open(SB), 0, $288-97 MOVQ ad+72(FP), adp // Check for AVX2 support - CMPB runtime·support_avx2(SB), $1 - JE chacha20Poly1305Open_AVX2 + CMPB runtime·support_avx2(SB), $0 + JE noavx2bmi2Open + + // Check BMI2 bit for MULXQ. + // runtime·cpuid_ebx7 is always available here + // because it passed avx2 check + TESTL $(1<<8), runtime·cpuid_ebx7(SB) + JNE chacha20Poly1305Open_AVX2 +noavx2bmi2Open: // Special optimization, for very short buffers CMPQ inl, $128 @@ -1485,8 +1492,15 @@ TEXT ·chacha20Poly1305Seal(SB), 0, $288-96 MOVQ ad+72(FP), adp // Check for AVX2 support - CMPB runtime·support_avx2(SB), $1 - JE chacha20Poly1305Seal_AVX2 + CMPB runtime·support_avx2(SB), $0 + JE noavx2bmi2Seal + + // Check BMI2 bit for MULXQ. + // runtime·cpuid_ebx7 is always available here + // because it passed avx2 check + TESTL $(1<<8), runtime·cpuid_ebx7(SB) + JNE chacha20Poly1305Seal_AVX2 +noavx2bmi2Seal: // Special optimization, for very short buffers CMPQ inl, $128