github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/arch/fpu/fpu.go (about) 1 // Copyright 2021 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 // Package fpu provides basic floating point helpers. 16 package fpu 17 18 import ( 19 "fmt" 20 "reflect" 21 ) 22 23 // State represents floating point state. 24 // 25 // This is a simple byte slice, but may have architecture-specific methods 26 // attached to it. 27 type State []byte 28 29 // ErrLoadingState indicates a failed restore due to unusable floating point 30 // state. 31 type ErrLoadingState struct { 32 // supported is the supported floating point state. 33 supportedFeatures uint64 34 35 // saved is the saved floating point state. 36 savedFeatures uint64 37 } 38 39 // Error returns a sensible description of the restore error. 40 func (e ErrLoadingState) Error() string { 41 return fmt.Sprintf("floating point state contains unsupported features; supported: %#x saved: %#x", e.supportedFeatures, e.savedFeatures) 42 } 43 44 // alignedBytes returns a slice of size bytes, aligned in memory to the given 45 // alignment. This is used because we require certain structures to be aligned 46 // in a specific way (for example, the X86 floating point data). 47 func alignedBytes(size, alignment uint) []byte { 48 data := make([]byte, size+alignment-1) 49 offset := uint(reflect.ValueOf(data).Index(0).Addr().Pointer() % uintptr(alignment)) 50 if offset == 0 { 51 return data[:size:size] 52 } 53 return data[alignment-offset:][:size:size] 54 }