github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/platform/kvm/kvm_amd64_test.go (about) 1 // Copyright 2020 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 // +build amd64 16 17 package kvm 18 19 import ( 20 "testing" 21 22 "github.com/SagerNet/gvisor/pkg/abi/linux" 23 "github.com/SagerNet/gvisor/pkg/ring0" 24 "github.com/SagerNet/gvisor/pkg/ring0/pagetables" 25 "github.com/SagerNet/gvisor/pkg/sentry/arch" 26 "github.com/SagerNet/gvisor/pkg/sentry/platform" 27 "github.com/SagerNet/gvisor/pkg/sentry/platform/kvm/testutil" 28 ) 29 30 func TestSegments(t *testing.T) { 31 applicationTest(t, true, testutil.TwiddleSegments, func(c *vCPU, regs *arch.Registers, pt *pagetables.PageTables) bool { 32 testutil.SetTestSegments(regs) 33 for { 34 var si linux.SignalInfo 35 if _, err := c.SwitchToUser(ring0.SwitchOpts{ 36 Registers: regs, 37 FloatingPointState: &dummyFPState, 38 PageTables: pt, 39 FullRestore: true, 40 }, &si); err == platform.ErrContextInterrupt { 41 continue // Retry. 42 } else if err != nil { 43 t.Errorf("application segment check with full restore got unexpected error: %v", err) 44 } 45 if err := testutil.CheckTestSegments(regs); err != nil { 46 t.Errorf("application segment check with full restore failed: %v", err) 47 } 48 break // Done. 49 } 50 return false 51 }) 52 } 53 54 // stmxcsr reads the MXCSR control and status register. 55 func stmxcsr(addr *uint32) 56 57 func TestMXCSR(t *testing.T) { 58 applicationTest(t, true, testutil.SyscallLoop, func(c *vCPU, regs *arch.Registers, pt *pagetables.PageTables) bool { 59 var si linux.SignalInfo 60 switchOpts := ring0.SwitchOpts{ 61 Registers: regs, 62 FloatingPointState: &dummyFPState, 63 PageTables: pt, 64 FullRestore: true, 65 } 66 67 const mxcsrControllMask = uint32(0x1f80) 68 mxcsrBefore := uint32(0) 69 mxcsrAfter := uint32(0) 70 stmxcsr(&mxcsrBefore) 71 if mxcsrBefore == 0 { 72 // goruntime sets mxcsr to 0x1f80 and it never changes 73 // the control configuration. 74 panic("mxcsr is zero") 75 } 76 switchOpts.FloatingPointState.SetMXCSR(0) 77 if _, err := c.SwitchToUser( 78 switchOpts, &si); err == platform.ErrContextInterrupt { 79 return true // Retry. 80 } else if err != nil { 81 t.Errorf("application syscall failed: %v", err) 82 } 83 stmxcsr(&mxcsrAfter) 84 if mxcsrAfter&mxcsrControllMask != mxcsrBefore&mxcsrControllMask { 85 t.Errorf("mxcsr = %x (expected %x)", mxcsrBefore, mxcsrAfter) 86 } 87 return false 88 }) 89 }