github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/ring0/defs_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 ring0
    18  
    19  import (
    20  	"github.com/SagerNet/gvisor/pkg/hostarch"
    21  )
    22  
    23  var (
    24  	// UserspaceSize is the total size of userspace.
    25  	UserspaceSize = uintptr(1) << (VirtualAddressBits())
    26  
    27  	// MaximumUserAddress is the largest possible user address.
    28  	MaximumUserAddress = (UserspaceSize - 1) & ^uintptr(hostarch.PageSize-1)
    29  
    30  	// KernelStartAddress is the starting kernel address.
    31  	KernelStartAddress = ^uintptr(0) - (UserspaceSize - 1)
    32  )
    33  
    34  // KernelArchState contains architecture-specific state.
    35  type KernelArchState struct {
    36  }
    37  
    38  // CPUArchState contains CPU-specific arch state.
    39  type CPUArchState struct {
    40  	// stack is the stack used for interrupts on this CPU.
    41  	stack [128]byte
    42  
    43  	// errorCode is the error code from the last exception.
    44  	errorCode uintptr
    45  
    46  	// errorType indicates the type of error code here, it is always set
    47  	// along with the errorCode value above.
    48  	//
    49  	// It will either by 1, which indicates a user error, or 0 indicating a
    50  	// kernel error. If the error code below returns false (kernel error),
    51  	// then it cannot provide relevant information about the last
    52  	// exception.
    53  	errorType uintptr
    54  
    55  	// faultAddr is the value of far_el1.
    56  	faultAddr uintptr
    57  
    58  	// el0Fp is the address of application's fpstate.
    59  	el0Fp uintptr
    60  
    61  	// ttbr0Kvm is the value of ttbr0_el1 for sentry.
    62  	ttbr0Kvm uintptr
    63  
    64  	// ttbr0App is the value of ttbr0_el1 for applicaton.
    65  	ttbr0App uintptr
    66  
    67  	// exception vector.
    68  	vecCode Vector
    69  
    70  	// application context pointer.
    71  	appAddr uintptr
    72  
    73  	// lazyVFP is the value of cpacr_el1.
    74  	lazyVFP uintptr
    75  
    76  	// appASID is the asid value of guest application.
    77  	appASID uintptr
    78  }
    79  
    80  // ErrorCode returns the last error code.
    81  //
    82  // The returned boolean indicates whether the error code corresponds to the
    83  // last user error or not. If it does not, then fault information must be
    84  // ignored. This is generally the result of a kernel fault while servicing a
    85  // user fault.
    86  //
    87  //go:nosplit
    88  func (c *CPU) ErrorCode() (value uintptr, user bool) {
    89  	return c.errorCode, c.errorType != 0
    90  }
    91  
    92  // ClearErrorCode resets the error code.
    93  //
    94  //go:nosplit
    95  func (c *CPU) ClearErrorCode() {
    96  	c.errorCode = 0 // No code.
    97  	c.errorType = 1 // User mode.
    98  }
    99  
   100  //go:nosplit
   101  func (c *CPU) GetFaultAddr() (value uintptr) {
   102  	return c.faultAddr
   103  }
   104  
   105  //go:nosplit
   106  func (c *CPU) SetTtbr0Kvm(value uintptr) {
   107  	c.ttbr0Kvm = value
   108  }
   109  
   110  //go:nosplit
   111  func (c *CPU) SetTtbr0App(value uintptr) {
   112  	c.ttbr0App = value
   113  }
   114  
   115  //go:nosplit
   116  func (c *CPU) GetVector() (value Vector) {
   117  	return c.vecCode
   118  }
   119  
   120  //go:nosplit
   121  func (c *CPU) SetAppAddr(value uintptr) {
   122  	c.appAddr = value
   123  }
   124  
   125  // GetLazyVFP returns the value of cpacr_el1.
   126  //go:nosplit
   127  func (c *CPU) GetLazyVFP() (value uintptr) {
   128  	return c.lazyVFP
   129  }
   130  
   131  // SwitchArchOpts are embedded in SwitchOpts.
   132  type SwitchArchOpts struct {
   133  	// UserASID indicates that the application ASID to be used on switch,
   134  	UserASID uint16
   135  
   136  	// KernelASID indicates that the kernel ASID to be used on return,
   137  	KernelASID uint16
   138  }
   139  
   140  func init() {
   141  }