gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/platform/kvm/testutil/testutil_amd64.go (about)

     1  // Copyright 2018 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 testutil
    19  
    20  import (
    21  	"reflect"
    22  
    23  	"gvisor.dev/gvisor/pkg/sentry/arch"
    24  )
    25  
    26  // AddrOfTwiddleSegments return the address of a function that reads segments
    27  // into known registers.
    28  func AddrOfTwiddleSegments() uintptr
    29  func twiddleSegments()
    30  
    31  // SetTestTarget sets the rip appropriately.
    32  func SetTestTarget(regs *arch.Registers, fn uintptr) {
    33  	regs.Rip = uint64(fn)
    34  }
    35  
    36  // SetTouchTarget sets rax appropriately.
    37  func SetTouchTarget(regs *arch.Registers, target *uintptr) {
    38  	if target != nil {
    39  		regs.Rax = uint64(reflect.ValueOf(target).Pointer())
    40  	} else {
    41  		regs.Rax = 0
    42  	}
    43  }
    44  
    45  // RewindSyscall rewinds a syscall RIP.
    46  func RewindSyscall(regs *arch.Registers) {
    47  	regs.Rip -= 2
    48  }
    49  
    50  // SetTestRegs initializes registers to known values.
    51  func SetTestRegs(regs *arch.Registers) {
    52  	regs.R15 = 0x15
    53  	regs.R14 = 0x14
    54  	regs.R13 = 0x13
    55  	regs.R12 = 0x12
    56  	regs.Rbp = 0xb9
    57  	regs.Rbx = 0xb4
    58  	regs.R11 = 0x11
    59  	regs.R10 = 0x10
    60  	regs.R9 = 0x09
    61  	regs.R8 = 0x08
    62  	regs.Rax = 0x44
    63  	regs.Rcx = 0xc4
    64  	regs.Rdx = 0xd4
    65  	regs.Rsi = 0x51
    66  	regs.Rdi = 0xd1
    67  	regs.Rsp = 0x59
    68  }
    69  
    70  // CheckTestRegs checks that registers were twiddled per TwiddleRegs.
    71  func CheckTestRegs(regs *arch.Registers, full bool) (err error) {
    72  	if need := ^uint64(0x15); regs.R15 != need {
    73  		err = addRegisterMismatch(err, "R15", regs.R15, need)
    74  	}
    75  	if need := ^uint64(0x14); regs.R14 != need {
    76  		err = addRegisterMismatch(err, "R14", regs.R14, need)
    77  	}
    78  	if need := ^uint64(0x13); regs.R13 != need {
    79  		err = addRegisterMismatch(err, "R13", regs.R13, need)
    80  	}
    81  	if need := ^uint64(0x12); regs.R12 != need {
    82  		err = addRegisterMismatch(err, "R12", regs.R12, need)
    83  	}
    84  	if need := ^uint64(0xb9); regs.Rbp != need {
    85  		err = addRegisterMismatch(err, "Rbp", regs.Rbp, need)
    86  	}
    87  	if need := ^uint64(0xb4); regs.Rbx != need {
    88  		err = addRegisterMismatch(err, "Rbx", regs.Rbx, need)
    89  	}
    90  	if need := ^uint64(0x10); regs.R10 != need {
    91  		err = addRegisterMismatch(err, "R10", regs.R10, need)
    92  	}
    93  	if need := ^uint64(0x09); regs.R9 != need {
    94  		err = addRegisterMismatch(err, "R9", regs.R9, need)
    95  	}
    96  	if need := ^uint64(0x08); regs.R8 != need {
    97  		err = addRegisterMismatch(err, "R8", regs.R8, need)
    98  	}
    99  	if need := ^uint64(0x44); regs.Rax != need {
   100  		err = addRegisterMismatch(err, "Rax", regs.Rax, need)
   101  	}
   102  	if need := ^uint64(0xd4); regs.Rdx != need {
   103  		err = addRegisterMismatch(err, "Rdx", regs.Rdx, need)
   104  	}
   105  	if need := ^uint64(0x51); regs.Rsi != need {
   106  		err = addRegisterMismatch(err, "Rsi", regs.Rsi, need)
   107  	}
   108  	if need := ^uint64(0xd1); regs.Rdi != need {
   109  		err = addRegisterMismatch(err, "Rdi", regs.Rdi, need)
   110  	}
   111  	if need := ^uint64(0x59); regs.Rsp != need {
   112  		err = addRegisterMismatch(err, "Rsp", regs.Rsp, need)
   113  	}
   114  	// Rcx & R11 are ignored if !full is set.
   115  	if need := ^uint64(0x11); full && regs.R11 != need {
   116  		err = addRegisterMismatch(err, "R11", regs.R11, need)
   117  	}
   118  	if need := ^uint64(0xc4); full && regs.Rcx != need {
   119  		err = addRegisterMismatch(err, "Rcx", regs.Rcx, need)
   120  	}
   121  	return
   122  }
   123  
   124  var fsData uint64 = 0x55
   125  var gsData uint64 = 0x85
   126  
   127  // SetTestSegments initializes segments to known values.
   128  func SetTestSegments(regs *arch.Registers) {
   129  	regs.Fs_base = uint64(reflect.ValueOf(&fsData).Pointer())
   130  	regs.Gs_base = uint64(reflect.ValueOf(&gsData).Pointer())
   131  }
   132  
   133  // CheckTestSegments checks that registers were twiddled per TwiddleSegments.
   134  func CheckTestSegments(regs *arch.Registers) (err error) {
   135  	if regs.Rax != fsData {
   136  		err = addRegisterMismatch(err, "Rax", regs.Rax, fsData)
   137  	}
   138  	if regs.Rbx != gsData {
   139  		err = addRegisterMismatch(err, "Rbx", regs.Rcx, gsData)
   140  	}
   141  	return
   142  }