github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/ring0/kernel.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  package ring0
    16  
    17  // Init initializes a new kernel.
    18  //
    19  //go:nosplit
    20  func (k *Kernel) Init(maxCPUs int) {
    21  	k.init(maxCPUs)
    22  }
    23  
    24  // Halt halts execution.
    25  func Halt()
    26  
    27  // defaultHooks implements hooks.
    28  type defaultHooks struct{}
    29  
    30  // KernelSyscall implements Hooks.KernelSyscall.
    31  //
    32  // +checkescape:all
    33  //
    34  //go:nosplit
    35  func (defaultHooks) KernelSyscall() {
    36  	Halt()
    37  }
    38  
    39  // KernelException implements Hooks.KernelException.
    40  //
    41  // +checkescape:all
    42  //
    43  //go:nosplit
    44  func (defaultHooks) KernelException(Vector) {
    45  	Halt()
    46  }
    47  
    48  // kernelSyscall is a trampoline.
    49  //
    50  // When in amd64, it is called with %rip on the upper half, so it can
    51  // NOT access to any global data which is not mapped on upper and must
    52  // call to function pointers or interfaces to switch to the lower half
    53  // so that callee can access to global data.
    54  //
    55  // +checkescape:hard,stack
    56  //
    57  //go:nosplit
    58  func kernelSyscall(c *CPU) {
    59  	c.hooks.KernelSyscall()
    60  }
    61  
    62  // kernelException is a trampoline.
    63  //
    64  // When in amd64, it is called with %rip on the upper half, so it can
    65  // NOT access to any global data which is not mapped on upper and must
    66  // call to function pointers or interfaces to switch to the lower half
    67  // so that callee can access to global data.
    68  //
    69  // +checkescape:hard,stack
    70  //
    71  //go:nosplit
    72  func kernelException(c *CPU, vector Vector) {
    73  	c.hooks.KernelException(vector)
    74  }
    75  
    76  // Init initializes a new CPU.
    77  //
    78  // Init allows embedding in other objects.
    79  func (c *CPU) Init(k *Kernel, cpuID int, hooks Hooks) {
    80  	c.self = c    // Set self reference.
    81  	c.kernel = k  // Set kernel reference.
    82  	c.init(cpuID) // Perform architectural init.
    83  
    84  	// Require hooks.
    85  	if hooks != nil {
    86  		c.hooks = hooks
    87  	} else {
    88  		c.hooks = defaultHooks{}
    89  	}
    90  }