54 lines · c
1//===----------------------- Linux syscalls ---------------------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_H10#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_H11 12#include "src/__support/CPP/bit.h"13#include "src/__support/common.h"14#include "src/__support/macros/config.h"15#include "src/__support/macros/optimization.h"16#include "src/__support/macros/properties/architectures.h"17 18#ifdef LIBC_TARGET_ARCH_IS_X86_3219#include "i386/syscall.h"20#elif defined(LIBC_TARGET_ARCH_IS_X86_64)21#include "x86_64/syscall.h"22#elif defined(LIBC_TARGET_ARCH_IS_AARCH64)23#include "aarch64/syscall.h"24#elif defined(LIBC_TARGET_ARCH_IS_ARM)25#include "arm/syscall.h"26#elif defined(LIBC_TARGET_ARCH_IS_ANY_RISCV)27#include "riscv/syscall.h"28#endif29 30namespace LIBC_NAMESPACE_DECL {31 32template <typename R, typename... Ts>33LIBC_INLINE R syscall_impl(long __number, Ts... ts) {34 static_assert(sizeof...(Ts) <= 6, "Too many arguments for syscall");35 return cpp::bit_or_static_cast<R>(syscall_impl(__number, (long)ts...));36}37 38// Linux-specific function for checking39namespace linux_utils {40LIBC_INLINE_VAR constexpr unsigned long MAX_ERRNO = 4095;41// Ideally, this should be defined using PAGE_OFFSET42// However, that is a configurable parameter. We mimic kernel's behavior43// by checking against MAX_ERRNO.44template <typename PointerLike>45LIBC_INLINE constexpr bool is_valid_mmap(PointerLike ptr) {46 long addr = cpp::bit_cast<long>(ptr);47 return LIBC_LIKELY(addr > 0 || addr < -static_cast<long>(MAX_ERRNO));48}49} // namespace linux_utils50 51} // namespace LIBC_NAMESPACE_DECL52 53#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_H54