[libc-commits] [libc] [llvm] Implement pkey_alloc/free/get/set/mprotect for x86_64 linux (PR #162362)
Schrodinger ZHU Yifan via libc-commits
libc-commits at lists.llvm.org
Tue Oct 7 13:48:26 PDT 2025
================
@@ -0,0 +1,71 @@
+#ifndef LLVM_SYS_MMAN_LINUX_X86_64_PKEY_COMMON_H_
+#define LLVM_SYS_MMAN_LINUX_X86_64_PKEY_COMMON_H_
+
+#include "hdr/errno_macros.h" // For ENOSYS
+#include "hdr/stdint_proxy.h"
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+
+#if !defined(LIBC_TARGET_ARCH_IS_X86_64)
+#error "Invalid include"
+#endif
+
+namespace LIBC_NAMESPACE_DECL {
+namespace pkey_common {
+namespace internal {
+
+constexpr int MAX_KEY = 15;
+constexpr int KEY_MASK = 0x3;
+constexpr int BITS_PER_KEY = 2;
+
+// This will SIGILL on CPUs that don't support PKU / OSPKE,
+// but this case should never be reached as a prior pkey_alloc invocation
+// would have failed more gracefully.
+LIBC_INLINE uint32_t read_prku() {
+ uint32_t pkru = 0;
+ uint32_t edx = 0;
+ LIBC_INLINE_ASM("rdpkru" : "=a"(pkru), "=d"(edx) : "c"(0));
----------------
SchrodingerZhu wrote:
Are we migrating back to the macro approach for ASM?
https://github.com/llvm/llvm-project/pull/162362
More information about the libc-commits
mailing list