gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/cpuid/cpuid_amd64_test.go (about)

     1  // Copyright 2019 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  //go:build amd64
    16  // +build amd64
    17  
    18  package cpuid
    19  
    20  import (
    21  	"testing"
    22  )
    23  
    24  // makeFatureSet creates a new FeatureSet.
    25  func makeFeatureSet(features ...Feature) FeatureSet {
    26  	s := make(Static)
    27  	for _, f := range features {
    28  		s.Add(f)
    29  	}
    30  	return FeatureSet{
    31  		Function: s,
    32  	}
    33  }
    34  
    35  var (
    36  	justFPU       = makeFeatureSet(X86FeatureFPU)
    37  	justFPUandPAE = makeFeatureSet(X86FeatureFPU, X86FeaturePAE)
    38  )
    39  
    40  func TestSubtract(t *testing.T) {
    41  	if left := justFPU.Subtract(justFPUandPAE); len(left) > 0 {
    42  		t.Errorf("Got %q is not subset of %q, want left (%v) to be non-empty", justFPU.FlagString(), justFPUandPAE.FlagString(), left)
    43  	}
    44  	if left := justFPUandPAE.Subtract(justFPU); len(left) == 0 {
    45  		t.Errorf("Got %q is a subset of %q, want left (%v) to be empty", justFPU.FlagString(), justFPUandPAE.FlagString(), left)
    46  	}
    47  }
    48  
    49  // TODO(b/73346484): Run this test on a very old platform, and make sure more
    50  // bits are enabled than just FPU and PAE. This test currently may not detect
    51  // if HostFeatureSet gives back junk bits.
    52  func TestHostFeatureSet(t *testing.T) {
    53  	hostFeatures := HostFeatureSet()
    54  	if justFPUandPAE.Subtract(hostFeatures) != nil {
    55  		t.Errorf("Got invalid feature set %v from HostFeatureSet()", hostFeatures)
    56  	}
    57  }
    58  
    59  func TestFixedExtendedState(t *testing.T) {
    60  	hostFeatures := HostFeatureSet()
    61  	fixedFeatures := hostFeatures.Fixed()
    62  	for i, allowed := range allowedBasicFunctions {
    63  		if !allowed {
    64  			continue
    65  		}
    66  		in := In{Eax: uint32(i) + uint32(extendedStart)}
    67  		h := hostFeatures.Query(in)
    68  		f := fixedFeatures.Query(in)
    69  		if h != f {
    70  			t.Errorf("native: %x fixed: %x", h, f)
    71  		}
    72  	}
    73  
    74  }
    75  
    76  func TestHasFeature(t *testing.T) {
    77  	if !justFPU.HasFeature(X86FeatureFPU) {
    78  		t.Errorf("HasFeature failed, %q should contain %v", justFPU.FlagString(), X86FeatureFPU)
    79  	}
    80  	if justFPU.HasFeature(X86FeatureAVX) {
    81  		t.Errorf("HasFeature failed, %q should not contain %v", justFPU.FlagString(), X86FeatureAVX)
    82  	}
    83  }
    84  
    85  func TestAdd(t *testing.T) {
    86  	// Test a basic insertion into the FeatureSet.
    87  	testFeatures := makeFeatureSet(X86FeatureCLFSH)
    88  	if !testFeatures.HasFeature(X86FeatureCLFSH) {
    89  		t.Errorf("Add failed, got %v want set with %v", testFeatures, X86FeatureCLFSH)
    90  	}
    91  
    92  	// Test that duplicates are ignored.
    93  	testFeatures.Function.(Static).Add(X86FeatureCLFSH)
    94  	if !testFeatures.HasFeature(X86FeatureCLFSH) {
    95  		t.Errorf("Duplicate add removed entry, got %v want set with %v", testFeatures, X86FeatureCLFSH)
    96  	}
    97  }
    98  
    99  func TestRemove(t *testing.T) {
   100  	// Try removing the last feature.
   101  	testFeatures := makeFeatureSet(X86FeatureFPU, X86FeaturePAE)
   102  	testFeatures.Function.(Static).Remove(X86FeaturePAE)
   103  	if !testFeatures.HasFeature(X86FeatureFPU) || testFeatures.HasFeature(X86FeaturePAE) {
   104  		t.Errorf("Remove failed, got %q want %q", testFeatures.FlagString(), justFPU.FlagString())
   105  	}
   106  
   107  	// Try removing a feature not in the set.
   108  	testFeatures.Function.(Static).Remove(X86FeatureRDRAND)
   109  	if !testFeatures.HasFeature(X86FeatureFPU) {
   110  		t.Errorf("Remove failed, got %q want %q", testFeatures.FlagString(), justFPU.FlagString())
   111  	}
   112  }