aboutsummaryrefslogtreecommitdiff
path: root/libc/src/sys/mman/linux/pkey_mprotect.cpp
blob: daa12fa927f87a392872727a93f09baa3589e06b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//===---------- Linux implementation of the Linux pkey_mprotect function --===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/sys/mman/pkey_mprotect.h"

#include "hdr/errno_macros.h" // For ENOSYS
#include "hdr/types/size_t.h"
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/error_or.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
#include "src/sys/mman/linux/mprotect_common.h"

#include <sys/syscall.h> // For syscall numbers.

namespace LIBC_NAMESPACE_DECL {
namespace internal {

LIBC_INLINE ErrorOr<int> pkey_mprotect_impl(void *addr, size_t len, int prot,
                                            int pkey) {
  // Fall back to mprotect if pkey is -1
  // to maintain compatibility with kernel versions that don't support pkey.
  if (pkey == -1) {
    return LIBC_NAMESPACE::mprotect_common::mprotect_impl(addr, len, prot);
  }

#if !defined(SYS_pkey_mprotect)
  return Error(ENOSYS);
#else
  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_pkey_mprotect, addr, len,
                                              prot, pkey);
  if (ret < 0) {
    return Error(-ret);
  }
  return 0;
#endif
}

} // namespace internal

LLVM_LIBC_FUNCTION(int, pkey_mprotect,
                   (void *addr, size_t len, int prot, int pkey)) {
  ErrorOr<int> ret =
      LIBC_NAMESPACE::internal::pkey_mprotect_impl(addr, len, prot, pkey);
  if (!ret.has_value()) {
    libc_errno = ret.error();
    return -1;
  }
  return ret.value();
}

} // namespace LIBC_NAMESPACE_DECL