|
| 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-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "src/sys/mman/pkey_mprotect.h" |
| 10 | + |
| 11 | +#include "hdr/errno_macros.h" // For ENOSYS |
| 12 | +#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/libc_errno.h" |
| 16 | +#include "src/__support/macros/config.h" |
| 17 | +#include "src/sys/mman/mprotect.h" |
| 18 | + |
| 19 | +#include <sys/syscall.h> // For syscall numbers. |
| 20 | + |
| 21 | +namespace LIBC_NAMESPACE_DECL { |
| 22 | + |
| 23 | +LLVM_LIBC_FUNCTION(int, pkey_mprotect, |
| 24 | + (void *addr, size_t len, int prot, int pkey)) { |
| 25 | + // Fall back to mprotect if pkey is -1 |
| 26 | + // to maintain compatibility with kernel versions that don't support pkey. |
| 27 | + if (pkey == -1) { |
| 28 | + return LIBC_NAMESPACE::mprotect(addr, len, prot); |
| 29 | + } |
| 30 | + |
| 31 | +#if !defined(SYS_pkey_mprotect) |
| 32 | + libc_errno = ENOSYS; |
| 33 | + return -1; |
| 34 | +#else |
| 35 | + int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_pkey_mprotect, addr, len, |
| 36 | + prot, pkey); |
| 37 | + if (ret < 0) { |
| 38 | + libc_errno = -ret; |
| 39 | + return -1; |
| 40 | + } |
| 41 | + return 0; |
| 42 | +#endif |
| 43 | +} |
| 44 | + |
| 45 | +} // namespace LIBC_NAMESPACE_DECL |
0 commit comments