github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/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/ttpreport/gvisor-ligolo/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  const (
    42  	fxsave = iota
    43  	xsave
    44  	xsaveopt
    45  	xsavec
    46  )
    47  
    48  // Init initializes the arch specific state.
    49  func (s *ArchState) Init() {
    50  	fs := cpuid.HostFeatureSet()
    51  
    52  	fpLenUint, _ := fs.ExtendedStateSize()
    53  	s.fpLen = uint32(fpLenUint)
    54  	// TODO(b/268366549): Fix use of xsavec/xsaveopt.
    55  	if fs.UseXsave() {
    56  		s.xsaveMode = xsave
    57  	} else {
    58  		s.xsaveMode = fxsave
    59  	}
    60  
    61  	if fs.UseFSGSBASE() {
    62  		s.fsgsbase = 1
    63  	}
    64  }
    65  
    66  // FpLen returns the FP state length for AMD64.
    67  func (s *ArchState) FpLen() int {
    68  	return int(s.fpLen)
    69  }
    70  
    71  func (s *ArchState) String() string {
    72  	var b strings.Builder
    73  	fmt.Fprintf(&b, "sysmsg.ArchState{")
    74  	fmt.Fprintf(&b, " xsaveMode %d", s.xsaveMode)
    75  	fmt.Fprintf(&b, " fsgsbase %d", s.fsgsbase)
    76  	fmt.Fprintf(&b, " fpLen %d", s.fpLen)
    77  	b.WriteString(" }")
    78  
    79  	return b.String()
    80  }