github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/arch/syscalls_arm64.go (about)

     1  // Copyright 2020 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 arch
    18  
    19  const restartSyscallNr = uintptr(128)
    20  
    21  // SyscallSaveOrig save the value of the register R0 which is clobbered in
    22  // syscall handler(doSyscall()).
    23  //
    24  // In linux, at the entry of the syscall handler(el0_svc_common()), value of R0
    25  // is saved to the pt_regs.orig_x0 in kernel code. But currently, the orig_x0
    26  // was not accessible to the userspace application, so we have to do the same
    27  // operation in the sentry code to save the R0 value into the App context.
    28  func (c *context64) SyscallSaveOrig() {
    29  	c.OrigR0 = c.Regs.Regs[0]
    30  }
    31  
    32  // SyscallNo returns the syscall number according to the 64-bit convention.
    33  func (c *context64) SyscallNo() uintptr {
    34  	return uintptr(c.Regs.Regs[8])
    35  }
    36  
    37  // SyscallArgs provides syscall arguments according to the 64-bit convention.
    38  //
    39  // Due to the way addresses are mapped for the sentry this binary *must* be
    40  // built in 64-bit mode. So we can just assume the syscall numbers that come
    41  // back match the expected host system call numbers.
    42  // General purpose registers usage on Arm64:
    43  // R0...R7: parameter/result registers.
    44  // R8: indirect result location register.
    45  // R9...R15: temporary registers.
    46  // R16: the first intra-procedure-call scratch register.
    47  // R17: the second intra-procedure-call scratch register.
    48  // R18: the platform register.
    49  // R19...R28: callee-saved registers.
    50  // R29: the frame pointer.
    51  // R30: the link register.
    52  func (c *context64) SyscallArgs() SyscallArguments {
    53  	return SyscallArguments{
    54  		SyscallArgument{Value: uintptr(c.OrigR0)},
    55  		SyscallArgument{Value: uintptr(c.Regs.Regs[1])},
    56  		SyscallArgument{Value: uintptr(c.Regs.Regs[2])},
    57  		SyscallArgument{Value: uintptr(c.Regs.Regs[3])},
    58  		SyscallArgument{Value: uintptr(c.Regs.Regs[4])},
    59  		SyscallArgument{Value: uintptr(c.Regs.Regs[5])},
    60  	}
    61  }
    62  
    63  // RestartSyscall implements Context.RestartSyscall.
    64  // Prepare for system call restart, OrigR0 will be restored to R0.
    65  // Please see the linux code as reference:
    66  // arch/arm64/kernel/signal.c:do_signal()
    67  func (c *context64) RestartSyscall() {
    68  	c.Regs.Pc -= SyscallWidth
    69  	// R0 will be backed up into OrigR0 when entering doSyscall().
    70  	// Please see the linux code as reference:
    71  	// arch/arm64/kernel/syscall.c:el0_svc_common().
    72  	// Here we restore it back.
    73  	c.Regs.Regs[0] = uint64(c.OrigR0)
    74  }
    75  
    76  // RestartSyscallWithRestartBlock implements Context.RestartSyscallWithRestartBlock.
    77  func (c *context64) RestartSyscallWithRestartBlock() {
    78  	c.Regs.Pc -= SyscallWidth
    79  	c.Regs.Regs[0] = uint64(c.OrigR0)
    80  	c.Regs.Regs[8] = uint64(restartSyscallNr)
    81  }