github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/platform/kvm/testutil/testutil_arm64.go (about)

     1  // Copyright 2019 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 arm64
    16  
    17  package testutil
    18  
    19  import (
    20  	"fmt"
    21  	"reflect"
    22  
    23  	"github.com/SagerNet/gvisor/pkg/sentry/arch"
    24  )
    25  
    26  // TLSWorks is a tls test.
    27  //
    28  // It returns true or false.
    29  func TLSWorks() bool
    30  
    31  // SetTestTarget sets the rip appropriately.
    32  func SetTestTarget(regs *arch.Registers, fn func()) {
    33  	regs.Pc = uint64(reflect.ValueOf(fn).Pointer())
    34  }
    35  
    36  // SetTouchTarget sets rax appropriately.
    37  func SetTouchTarget(regs *arch.Registers, target *uintptr) {
    38  	if target != nil {
    39  		regs.Regs[8] = uint64(reflect.ValueOf(target).Pointer())
    40  	} else {
    41  		regs.Regs[8] = 0
    42  	}
    43  }
    44  
    45  // RewindSyscall rewinds a syscall RIP.
    46  func RewindSyscall(regs *arch.Registers) {
    47  	regs.Pc -= 4
    48  }
    49  
    50  // SetTestRegs initializes registers to known values.
    51  func SetTestRegs(regs *arch.Registers) {
    52  	for i := 0; i <= 30; i++ {
    53  		regs.Regs[i] = uint64(i) + 1
    54  	}
    55  }
    56  
    57  // CheckTestRegs checks that registers were twiddled per TwiddleRegs.
    58  func CheckTestRegs(regs *arch.Registers, full bool) (err error) {
    59  	for i := 0; i <= 30; i++ {
    60  		if need := ^uint64(i + 1); regs.Regs[i] != need {
    61  			err = addRegisterMismatch(err, fmt.Sprintf("R%d", i), regs.Regs[i], need)
    62  		}
    63  	}
    64  	// Check tls.
    65  	if need := ^uint64(11); regs.TPIDR_EL0 != need {
    66  		err = addRegisterMismatch(err, "tpdir_el0", regs.TPIDR_EL0, need)
    67  	}
    68  	return
    69  }