From af9dc0a51fe3010b0400158e5b45eebaea871adf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Tue, 20 Oct 2026 01:05:09 +0800 Subject: [PATCH] Enable ARM64 CPU feature detection in Windows builds --- .github/go_windows_arm64_cpu_features.patch | 94 +++++++++++++++++++++ .github/patch_go_for_windows_arm64.sh | 31 +++++++ .github/workflows/build.yml | 8 ++ 3 files changed, 133 insertions(+) create mode 100644 .github/go_windows_arm64_cpu_features.patch create mode 100755 .github/patch_go_for_windows_arm64.sh diff --git a/.github/go_windows_arm64_cpu_features.patch b/.github/go_windows_arm64_cpu_features.patch new file mode 100644 index 00000000..7c62dca2 --- /dev/null +++ b/.github/go_windows_arm64_cpu_features.patch @@ -0,0 +1,94 @@ +--- a/src/internal/cpu/cpu_arm64_other.go ++++ b/src/internal/cpu/cpu_arm64_other.go +@@ -2,7 +2,7 @@ + // Use of this source code is governed by a BSD-style + // license that can be found in the LICENSE file. + +-//go:build arm64 && !linux && !freebsd && !android && (!darwin || ios) && !openbsd ++//go:build arm64 && !linux && !freebsd && !android && (!darwin || ios) && !openbsd && !windows + + package cpu + +--- a/src/internal/cpu/cpu_arm64_windows.go ++++ b/src/internal/cpu/cpu_arm64_windows.go +@@ -0,0 +1,26 @@ ++// Copyright 2026 The Go Authors. All rights reserved. ++// Use of this source code is governed by a BSD-style ++// license that can be found in the LICENSE file. ++ ++//go:build arm64 ++ ++package cpu ++ ++func osInit() { ++ // TODO(brainman): we don't know how to retrieve those bits form user space - leaving them as false for now ++ // https://github.com/golang/go/issues/76791#issuecomment-3877873959 ++ ARM64.HasCPUID = false ++ ARM64.HasDIT = false ++ ARM64.IsNeoverse = false ++ ++ if isProcessorFeaturePresent(_PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) { ++ ARM64.HasAES = true ++ ARM64.HasPMULL = true ++ ARM64.HasSHA1 = true ++ ARM64.HasSHA2 = true ++ } ++ ARM64.HasSHA3 = isProcessorFeaturePresent(_PF_ARM_SHA3_INSTRUCTIONS_AVAILABLE) ++ ARM64.HasCRC32 = isProcessorFeaturePresent(_PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE) ++ ARM64.HasSHA512 = isProcessorFeaturePresent(_PF_ARM_SHA512_INSTRUCTIONS_AVAILABLE) ++ ARM64.HasATOMICS = isProcessorFeaturePresent(_PF_ARM_V81_ATOMIC_INSTRUCTIONS_AVAILABLE) ++} +--- a/src/internal/cpu/cpu_windows.go ++++ b/src/internal/cpu/cpu_windows.go +@@ -0,0 +1,20 @@ ++// Copyright 2026 The Go Authors. All rights reserved. ++// Use of this source code is governed by a BSD-style ++// license that can be found in the LICENSE file. ++ ++package cpu ++ ++import _ "unsafe" // for linkname ++ ++const ( ++ _PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE = 30 ++ _PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE = 31 ++ _PF_ARM_V81_ATOMIC_INSTRUCTIONS_AVAILABLE = 34 ++ _PF_ARM_SHA3_INSTRUCTIONS_AVAILABLE = 64 ++ _PF_ARM_SHA512_INSTRUCTIONS_AVAILABLE = 65 ++) ++ ++// isProcessorFeaturePresent calls windows IsProcessorFeaturePresent API. ++// ++//go:linkname isProcessorFeaturePresent ++func isProcessorFeaturePresent(processorFeature uint32) bool // Implemented in runtime package. +--- a/src/runtime/os_windows.go ++++ b/src/runtime/os_windows.go +@@ -39,6 +39,7 @@ const ( + //go:cgo_import_dynamic runtime._GetSystemDirectoryA GetSystemDirectoryA%2 "kernel32.dll" + //go:cgo_import_dynamic runtime._GetSystemInfo GetSystemInfo%1 "kernel32.dll" + //go:cgo_import_dynamic runtime._GetThreadContext GetThreadContext%2 "kernel32.dll" ++//go:cgo_import_dynamic runtime._IsProcessorFeaturePresent IsProcessorFeaturePresent%1 "kernel32.dll" + //go:cgo_import_dynamic runtime._SetThreadContext SetThreadContext%2 "kernel32.dll" + //go:cgo_import_dynamic runtime._LoadLibraryExW LoadLibraryExW%3 "kernel32.dll" + //go:cgo_import_dynamic runtime._PostQueuedCompletionStatus PostQueuedCompletionStatus%4 "kernel32.dll" +@@ -96,6 +97,7 @@ var ( + _GetSystemDirectoryA, + _GetSystemInfo, + _GetThreadContext, ++ _IsProcessorFeaturePresent, + _SetThreadContext, + _LoadLibraryExW, + _PostQueuedCompletionStatus, +@@ -261,6 +263,12 @@ func windows_QueryPerformanceFrequency() int64 { + return frequency + } + ++//go:linkname cpu_isProcessorFeaturePresent internal/cpu.isProcessorFeaturePresent ++func cpu_isProcessorFeaturePresent(processorFeature uint32) bool { ++ ret := stdcall(_IsProcessorFeaturePresent, uintptr(processorFeature)) ++ return ret != 0 ++} ++ + func loadOptionalSyscalls() { + bcryptPrimitives := windowsLoadSystemLib(bcryptprimitivesdll[:]) + if bcryptPrimitives == 0 { diff --git a/.github/patch_go_for_windows_arm64.sh b/.github/patch_go_for_windows_arm64.sh new file mode 100755 index 00000000..dfb5f073 --- /dev/null +++ b/.github/patch_go_for_windows_arm64.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash + +set -euo pipefail + +# Go's internal/cpu never detects ARM64 features on windows/arm64 before Go 1.27, +# so crypto/aes, AES-GCM and crypto/sha256 use their generic implementations. +# Backport of https://go.dev/cl/745560 (golang/go#76791). +# +# Remove once the Go release used by the Windows builds includes the fix. + +export GOTOOLCHAIN=local + +GOROOT="$(go env GOROOT)" +PATCH_FILE="$(cd "$(dirname "$0")" && pwd)/go_windows_arm64_cpu_features.patch" + +cd "$GOROOT" +if [[ -f src/internal/cpu/cpu_arm64_windows.go ]]; then + echo "already patched" +else + patch --verbose -p1 < "$PATCH_FILE" +fi + +CPU_FILES="$(GOOS=windows GOARCH=arm64 CGO_ENABLED=0 go list -f '{{.GoFiles}}' internal/cpu)" +echo "internal/cpu files for windows/arm64: $CPU_FILES" +case "$CPU_FILES" in +*cpu_arm64_windows.go*) ;; +*) + echo "patch is not effective" >&2 + exit 1 + ;; +esac diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1d5a9d64..44ee3b38 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -591,6 +591,10 @@ jobs: with: go-version: 1.26.8 cache: false + - name: Patch Go for Windows ARM64 + if: matrix.arch == 'arm64' + shell: bash + run: .github/patch_go_for_windows_arm64.sh - name: Cache Go modules uses: actions/cache@v4 with: @@ -979,6 +983,10 @@ jobs: with: go-version: 1.26.8 cache: false + - name: Patch Go for Windows ARM64 + if: matrix.desktop_arch == 'arm64' + shell: bash + run: .github/patch_go_for_windows_arm64.sh - name: Cache Go modules uses: actions/cache@v4 with: