github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/rseq/syscalls.h (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  #ifndef GVISOR_TEST_SYSCALLS_LINUX_RSEQ_SYSCALLS_H_
    16  #define GVISOR_TEST_SYSCALLS_LINUX_RSEQ_SYSCALLS_H_
    17  
    18  #include "test/syscalls/linux/rseq/types.h"
    19  
    20  // Syscall numbers.
    21  #if defined(__x86_64__)
    22  constexpr int kGetpid = 39;
    23  constexpr int kExitGroup = 231;
    24  #elif defined(__aarch64__)
    25  constexpr int kGetpid = 172;
    26  constexpr int kExitGroup = 94;
    27  #else
    28  #error "Unknown architecture"
    29  #endif
    30  
    31  namespace gvisor {
    32  namespace testing {
    33  
    34  // Standalone system call interfaces.
    35  // Note that these are all "raw" system call interfaces which encode
    36  // errors by setting the return value to a small negative number.
    37  // Use sys_errno() to check system call return values for errors.
    38  
    39  // Maximum Linux error number.
    40  constexpr int kMaxErrno = 4095;
    41  
    42  // Errno values.
    43  #define EPERM 1
    44  #define EFAULT 14
    45  #define EBUSY 16
    46  #define EINVAL 22
    47  
    48  // Get the error number from a raw system call return value.
    49  // Returns a positive error number or 0 if there was no error.
    50  static inline int sys_errno(uintptr_t rval) {
    51    if (rval >= static_cast<uintptr_t>(-kMaxErrno)) {
    52      return -static_cast<int>(rval);
    53    }
    54    return 0;
    55  }
    56  
    57  extern "C" uintptr_t raw_syscall(int number, ...);
    58  
    59  static inline void sys_exit_group(int status) {
    60    raw_syscall(kExitGroup, status);
    61  }
    62  static inline int sys_getpid() {
    63    return static_cast<int>(raw_syscall(kGetpid));
    64  }
    65  
    66  }  // namespace testing
    67  }  // namespace gvisor
    68  
    69  #endif  // GVISOR_TEST_SYSCALLS_LINUX_RSEQ_SYSCALLS_H_