59 lines · cpp
1//===---------- Linux implementation of the Linux pkey_mprotect function --===//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#include "src/sys/mman/pkey_mprotect.h"10 11#include "hdr/errno_macros.h" // For ENOSYS12#include "hdr/types/size_t.h"13#include "src/__support/OSUtil/syscall.h" // For internal syscall function.14#include "src/__support/common.h"15#include "src/__support/error_or.h"16#include "src/__support/libc_errno.h"17#include "src/__support/macros/config.h"18#include "src/sys/mman/linux/mprotect_common.h"19 20#include <sys/syscall.h> // For syscall numbers.21 22namespace LIBC_NAMESPACE_DECL {23namespace internal {24 25LIBC_INLINE ErrorOr<int> pkey_mprotect_impl(void *addr, size_t len, int prot,26 int pkey) {27 // Fall back to mprotect if pkey is -128 // to maintain compatibility with kernel versions that don't support pkey.29 if (pkey == -1) {30 return LIBC_NAMESPACE::mprotect_common::mprotect_impl(addr, len, prot);31 }32 33#if !defined(SYS_pkey_mprotect)34 return Error(ENOSYS);35#else36 int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_pkey_mprotect, addr, len,37 prot, pkey);38 if (ret < 0) {39 return Error(-ret);40 }41 return 0;42#endif43}44 45} // namespace internal46 47LLVM_LIBC_FUNCTION(int, pkey_mprotect,48 (void *addr, size_t len, int prot, int pkey)) {49 ErrorOr<int> ret =50 LIBC_NAMESPACE::internal::pkey_mprotect_impl(addr, len, prot, pkey);51 if (!ret.has_value()) {52 libc_errno = ret.error();53 return -1;54 }55 return ret.value();56}57 58} // namespace LIBC_NAMESPACE_DECL59