Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.sys.mman.munlock
libc.src.sys.mman.munlockall
libc.src.sys.mman.munmap
libc.src.sys.mman.pkey_alloc
libc.src.sys.mman.pkey_free
libc.src.sys.mman.pkey_get
libc.src.sys.mman.pkey_mprotect
libc.src.sys.mman.pkey_set
libc.src.sys.mman.remap_file_pages
libc.src.sys.mman.posix_madvise
libc.src.sys.mman.shm_open
Expand Down
35 changes: 35 additions & 0 deletions libc/include/sys/mman.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,41 @@ functions:
arguments:
- type: void *
- type: size_t
- name: pkey_alloc
standards:
- Linux
return_type: int
arguments:
- type: unsigned int
- type: unsigned int
- name: pkey_free
standards:
- Linux
return_type: int
arguments:
- type: int
- name: pkey_get
standards:
- GNUExtensions
return_type: int
arguments:
- type: int
- name: pkey_mprotect
standards:
- Linux
return_type: int
arguments:
- type: void *
- type: size_t
- type: int
- type: int
- name: pkey_set
standards:
- GNUExtensions
return_type: int
arguments:
- type: int
- type: unsigned int
- name: posix_madvise
standards:
- POSIX
Expand Down
35 changes: 35 additions & 0 deletions libc/src/sys/mman/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,41 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.msync
)

add_entrypoint_object(
pkey_alloc
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.pkey_alloc
)

add_entrypoint_object(
pkey_free
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.pkey_free
)

add_entrypoint_object(
pkey_get
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.pkey_get
)

add_entrypoint_object(
pkey_mprotect
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.pkey_mprotect
)

add_entrypoint_object(
pkey_set
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.pkey_set
)

add_entrypoint_object(
remap_file_pages
ALIAS
Expand Down
75 changes: 75 additions & 0 deletions libc/src/sys/mman/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
add_subdirectory(generic)
set(ARCH_SUBDIRECTORY generic)
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_ARCHITECTURE})
add_subdirectory(${LIBC_TARGET_ARCHITECTURE})
set(ARCH_SUBDIRECTORY ${LIBC_TARGET_ARCHITECTURE})
endif()

add_entrypoint_object(
madvise
SRCS
Expand Down Expand Up @@ -166,6 +173,74 @@ add_entrypoint_object(
libc.src.errno.errno
)

add_entrypoint_object(
pkey_alloc
SRCS
pkey_alloc.cpp
HDRS
../pkey_alloc.h
DEPENDS
libc.include.sys_mman
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)

add_entrypoint_object(
pkey_free
SRCS
pkey_free.cpp
HDRS
../pkey_free.h
DEPENDS
libc.include.sys_mman
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)

add_entrypoint_object(
pkey_get
SRCS
pkey_get.cpp
HDRS
../pkey_get.h
DEPENDS
libc.include.sys_mman
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
.${ARCH_SUBDIRECTORY}.pkey_common
)

add_entrypoint_object(
pkey_mprotect
SRCS
pkey_mprotect.cpp
HDRS
../pkey_mprotect.h
DEPENDS
libc.include.sys_mman
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.sys.mman.mprotect
libc.src.errno.errno
)

add_entrypoint_object(
pkey_set
SRCS
pkey_set.cpp
HDRS
../pkey_set.h
DEPENDS
libc.include.sys_mman
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
.${ARCH_SUBDIRECTORY}.pkey_common
)

add_entrypoint_object(
remap_file_pages
SRCS
Expand Down
9 changes: 9 additions & 0 deletions libc/src/sys/mman/linux/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
add_header_library(
pkey_common
HDRS
pkey_common.h
DEPENDS
libc.hdr.errno_macros
libc.src.__support.common
libc.src.__support.error_or
)
33 changes: 33 additions & 0 deletions libc/src/sys/mman/linux/generic/pkey_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===---------- Generic stub implementations for pkey functionality. ------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SYS_MMAN_LINUX_GENERIC_PKEY_COMMON_H_
#define LLVM_SYS_MMAN_LINUX_GENERIC_PKEY_COMMON_H_

#include "hdr/errno_macros.h" // For ENOSYS
#include "src/__support/common.h"
#include "src/__support/error_or.h"

namespace LIBC_NAMESPACE_DECL {
namespace pkey_common {

LIBC_INLINE ErrorOr<int> pkey_get(int pkey) {
(void)pkey;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use [[maybe_unused]] on pkey declaration instead.

return Error(ENOSYS);
}

LIBC_INLINE ErrorOr<int> pkey_set(int pkey, unsigned int access_rights) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use [[maybe_unused]] instead of (void)

(void)pkey;
(void)access_rights;
return Error(ENOSYS);
}

} // namespace pkey_common
} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_SYS_MMAN_LINUX_GENERIC_PKEY_COMMON_H_
37 changes: 37 additions & 0 deletions libc/src/sys/mman/linux/pkey_alloc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===---------- Linux implementation of the Linux pkey_alloc 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_alloc.h"

#include "hdr/errno_macros.h" // For ENOSYS
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"

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

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, pkey_alloc,
(unsigned int flags, unsigned int access_rights)) {
#if !defined(SYS_pkey_alloc)
libc_errno = ENOSYS;
return -1;
#else
int ret =
LIBC_NAMESPACE::syscall_impl<int>(SYS_pkey_alloc, flags, access_rights);
if (ret < 0) {
libc_errno = static_cast<int>(-ret);
return -1;
}
return static_cast<int>(ret);
#endif
}

} // namespace LIBC_NAMESPACE_DECL
35 changes: 35 additions & 0 deletions libc/src/sys/mman/linux/pkey_free.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===---------- Linux implementation of the Linux pkey_free 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_free.h"

#include "hdr/errno_macros.h" // For ENOSYS
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"

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

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, pkey_free, (int pkey)) {
#if !defined(SYS_pkey_free)
libc_errno = ENOSYS;
return -1;
#else
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_pkey_free, pkey);
if (ret < 0) {
libc_errno = static_cast<int>(-ret);
return -1;
}
return 0;
#endif
}

} // namespace LIBC_NAMESPACE_DECL
35 changes: 35 additions & 0 deletions libc/src/sys/mman/linux/pkey_get.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===---------- 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_get.h"

#include "hdr/errno_macros.h" // For ENOSYS
#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/__support/macros/properties/architectures.h"

#if defined(LIBC_TARGET_ARCH_IS_X86_64)
#include "src/sys/mman/linux/x86_64/pkey_common.h"
#else
#include "src/sys/mman/linux/generic/pkey_common.h"
#endif

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, pkey_get, (int pkey)) {
ErrorOr<int> ret = LIBC_NAMESPACE::pkey_common::pkey_get(pkey);
if (!ret.has_value()) {
libc_errno = ret.error();
return -1;
}
return ret.value();
}

} // namespace LIBC_NAMESPACE_DECL
45 changes: 45 additions & 0 deletions libc/src/sys/mman/linux/pkey_mprotect.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//===---------- 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/libc_errno.h"
#include "src/__support/macros/config.h"
#include "src/sys/mman/mprotect.h"

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

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, pkey_mprotect,
(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(addr, len, prot);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this entrypoint dependency be broken up?

}

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

} // namespace LIBC_NAMESPACE_DECL
Loading
Loading