gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/platform/systrap/sysmsg/sysmsg_amd64.go (about)

     1  // Copyright 2023 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 sysmsg
    19  
    20  import (
    21  	_ "embed"
    22  	"fmt"
    23  	"strings"
    24  
    25  	"gvisor.dev/gvisor/pkg/cpuid"
    26  )
    27  
    28  // SighandlerBlob contains the compiled code of the sysmsg signal handler.
    29  //
    30  //go:embed sighandler.built-in.amd64.bin
    31  var SighandlerBlob []byte
    32  
    33  // ArchState defines variables specific to the architecture being
    34  // used.
    35  type ArchState struct {
    36  	xsaveMode uint32
    37  	fpLen     uint32
    38  	fsgsbase  uint32
    39  }
    40  
    41  // The linux kernel does not allow using xsavec from userspace, so we are limited
    42  // to xsaveopt.
    43  // See arch/x86/kernel/fpu/xstate.c:validate_user_xstate_header for details.
    44  const (
    45  	fxsave = iota
    46  	xsave
    47  	xsaveopt
    48  )
    49  
    50  // Init initializes the arch specific state.
    51  func (s *ArchState) Init() {
    52  	fs := cpuid.HostFeatureSet()
    53  
    54  	fpLenUint, _ := fs.ExtendedStateSize()
    55  	// TODO(gvisor.dev/issues/9896): Implement AMX Support.
    56  	s.fpLen = uint32(fpLenUint - fs.AMXExtendedStateSize())
    57  	if fs.UseXsaveopt() {
    58  		s.xsaveMode = xsaveopt
    59  	} else if fs.UseXsave() {
    60  		s.xsaveMode = xsave
    61  	} else {
    62  		s.xsaveMode = fxsave
    63  	}
    64  
    65  	if fs.UseFSGSBASE() {
    66  		s.fsgsbase = 1
    67  	}
    68  }
    69  
    70  // FpLen returns the FP state length for AMD64.
    71  func (s *ArchState) FpLen() int {
    72  	return int(s.fpLen)
    73  }
    74  
    75  func (s *ArchState) String() string {
    76  	var b strings.Builder
    77  	fmt.Fprintf(&b, "sysmsg.ArchState{")
    78  	fmt.Fprintf(&b, " xsaveMode %d", s.xsaveMode)
    79  	fmt.Fprintf(&b, " fsgsbase %d", s.fsgsbase)
    80  	fmt.Fprintf(&b, " fpLen %d", s.fpLen)
    81  	b.WriteString(" }")
    82  
    83  	return b.String()
    84  }