github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/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  	"github.com/MerlinKodo/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  	s.fpLen = uint32(fpLenUint)
    56  	if fs.UseXsaveopt() {
    57  		s.xsaveMode = xsaveopt
    58  	} else if fs.UseXsave() {
    59  		s.xsaveMode = xsave
    60  	} else {
    61  		s.xsaveMode = fxsave
    62  	}
    63  
    64  	if fs.UseFSGSBASE() {
    65  		s.fsgsbase = 1
    66  	}
    67  }
    68  
    69  // FpLen returns the FP state length for AMD64.
    70  func (s *ArchState) FpLen() int {
    71  	return int(s.fpLen)
    72  }
    73  
    74  func (s *ArchState) String() string {
    75  	var b strings.Builder
    76  	fmt.Fprintf(&b, "sysmsg.ArchState{")
    77  	fmt.Fprintf(&b, " xsaveMode %d", s.xsaveMode)
    78  	fmt.Fprintf(&b, " fsgsbase %d", s.fsgsbase)
    79  	fmt.Fprintf(&b, " fpLen %d", s.fpLen)
    80  	b.WriteString(" }")
    81  
    82  	return b.String()
    83  }