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