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