From 1ccd69e6e39d2c710a92e878dc1a6e61a95c64e6 Mon Sep 17 00:00:00 2001 From: Bin Lu Date: Tue, 4 Dec 2018 10:41:10 +0800 Subject: [PATCH] Add ARM64 support to cpuid Signed-off-by: Bin Lu --- source/cpuid/cpuid.go | 15 ----- source/cpuid/cpuid_amd64.go | 32 +++++++++++ source/cpuid/cpuid_arm64.go | 110 ++++++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+), 15 deletions(-) create mode 100644 source/cpuid/cpuid_amd64.go create mode 100644 source/cpuid/cpuid_arm64.go diff --git a/source/cpuid/cpuid.go b/source/cpuid/cpuid.go index f0721ffa8..43cfc6c3e 100644 --- a/source/cpuid/cpuid.go +++ b/source/cpuid/cpuid.go @@ -16,23 +16,8 @@ limitations under the License. package cpuid -import ( - "github.com/klauspost/cpuid" - "sigs.k8s.io/node-feature-discovery/source" -) - // Source implements FeatureSource. type Source struct{} // Name returns an identifier string for this feature source. func (s Source) Name() string { return "cpuid" } - -// Discover returns feature names for all the supported CPU features. -func (s Source) Discover() (source.Features, error) { - // Get the cpu features as strings - features := source.Features{} - for _, f := range cpuid.CPU.Features.Strings() { - features[f] = true - } - return features, nil -} diff --git a/source/cpuid/cpuid_amd64.go b/source/cpuid/cpuid_amd64.go new file mode 100644 index 000000000..81e6d8ad9 --- /dev/null +++ b/source/cpuid/cpuid_amd64.go @@ -0,0 +1,32 @@ +/* +Copyright 2017 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cpuid + +import ( + "github.com/klauspost/cpuid" + "sigs.k8s.io/node-feature-discovery/source" +) + +// Discover returns feature names for all the supported CPU features. +func (s Source) Discover() (source.Features, error) { + // Get the cpu features as strings + features := source.Features{} + for _, f := range cpuid.CPU.Features.Strings() { + features[f] = true + } + return features, nil +} diff --git a/source/cpuid/cpuid_arm64.go b/source/cpuid/cpuid_arm64.go new file mode 100644 index 000000000..3e252201b --- /dev/null +++ b/source/cpuid/cpuid_arm64.go @@ -0,0 +1,110 @@ +/* +Copyright 2017 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cpuid + +/* +#include +#define HWCAP_CPUID (1 << 11) + +unsigned long gethwcap() { + return getauxval(AT_HWCAP); +} +*/ +import "C" + +import ( + "sigs.k8s.io/node-feature-discovery/source" +) + +/* all special features for arm64 should be defined here */ +const ( + /* extension instructions */ + CPU_ARM64_FEATURE_FP = 1 << iota + CPU_ARM64_FEATURE_ASIMD + CPU_ARM64_FEATURE_EVTSTRM + CPU_ARM64_FEATURE_AES + CPU_ARM64_FEATURE_PMULL + CPU_ARM64_FEATURE_SHA1 + CPU_ARM64_FEATURE_SHA2 + CPU_ARM64_FEATURE_CRC32 + CPU_ARM64_FEATURE_ATOMICS + CPU_ARM64_FEATURE_FPHP + CPU_ARM64_FEATURE_ASIMDHP + CPU_ARM64_FEATURE_CPUID + CPU_ARM64_FEATURE_ASIMDRDM + CPU_ARM64_FEATURE_JSCVT + CPU_ARM64_FEATURE_FCMA + CPU_ARM64_FEATURE_LRCPC + CPU_ARM64_FEATURE_DCPOP + CPU_ARM64_FEATURE_SHA3 + CPU_ARM64_FEATURE_SM3 + CPU_ARM64_FEATURE_SM4 + CPU_ARM64_FEATURE_ASIMDDP + CPU_ARM64_FEATURE_SHA512 + CPU_ARM64_FEATURE_SVE +) + +var flagNames_arm64 = map[uint64]string{ + CPU_ARM64_FEATURE_FP: "FP", + CPU_ARM64_FEATURE_ASIMD: "ASIMD", + CPU_ARM64_FEATURE_EVTSTRM: "EVTSTRM", + CPU_ARM64_FEATURE_AES: "AES", + CPU_ARM64_FEATURE_PMULL: "PMULL", + CPU_ARM64_FEATURE_SHA1: "SHA1", + CPU_ARM64_FEATURE_SHA2: "SHA2", + CPU_ARM64_FEATURE_CRC32: "CRC32", + CPU_ARM64_FEATURE_ATOMICS: "ATOMICS", + CPU_ARM64_FEATURE_FPHP: "FPHP", + CPU_ARM64_FEATURE_ASIMDHP: "ASIMDHP", + CPU_ARM64_FEATURE_CPUID: "CPUID", + CPU_ARM64_FEATURE_ASIMDRDM: "ASIMDRDM", + CPU_ARM64_FEATURE_JSCVT: "JSCVT", + CPU_ARM64_FEATURE_FCMA: "FCMA", + CPU_ARM64_FEATURE_LRCPC: "LRCPC", + CPU_ARM64_FEATURE_DCPOP: "DCPOP", + CPU_ARM64_FEATURE_SHA3: "SHA3", + CPU_ARM64_FEATURE_SM3: "SM3", + CPU_ARM64_FEATURE_SM4: "SM4", + CPU_ARM64_FEATURE_ASIMDDP: "ASIMDDP", + CPU_ARM64_FEATURE_SHA512: "SHA512", + CPU_ARM64_FEATURE_SVE: "SVE", +} + +func getFeaturesFromHWCAP() []string { + r := make([]string, 0, 20) + hwcap := uint64(C.gethwcap()) + for i := uint(0); i < 64; i++ { + key := uint64(1 << i) + val := flagNames_arm64[key] + if hwcap&key != 0 { + r = append(r, val) + } + } + return r +} + +// Discover returns feature names for all the supported CPU features. +func (s Source) Discover() (source.Features, error) { + // Get the cpu features as strings + features := source.Features{} + + for _, f := range getFeaturesFromHWCAP() { + features[f] = true + } + + return features, nil +}