From: Jacky Li <jackyli@google.com>
To: qemu-devel@nongnu.org
Cc: "Paolo Bonzini" <pbonzini@redhat.com>,
"Zhao Liu" <zhao1.liu@intel.com>,
"Richard Henderson" <richard.henderson@linaro.org>,
"Philippe Mathieu-Daudé" <philmd@mailo.com>,
"Peter Xu" <peterx@redhat.com>,
kvm@vger.kernel.org, "James Houghton" <jthoughton@google.com>,
"Mingwei Zhang" <mizhang@google.com>,
"Dave Hansen" <dave.hansen@linux.intel.com>,
"Brendan Jackman" <jackmanb@google.com>,
"Reiji Watanabe" <reijiw@google.com>,
"Jacky Li" <jackyli@google.com>
Subject: [RFC PATCH 5/6] x86: Add xstate parsing and PKRU offset detection
Date: Tue, 18 Aug 2026 20:44:19 +0000 [thread overview]
Message-ID: <20260818-feature-pkey-dev-v1-5-8c0ef96a4da9@google.com> (raw)
In-Reply-To: <20260818-feature-pkey-dev-v1-0-8c0ef96a4da9@google.com>
Add data structures and CPUID detection routines to locate the PKRU
register inside the Linux x86_64 signal frame extended state (`xstate`).
Signed-off-by: Jacky Li <jackyli@google.com>
---
util/pkey.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 53 insertions(+)
diff --git a/util/pkey.c b/util/pkey.c
index 2e42da7d9c..21b41f928e 100644
--- a/util/pkey.c
+++ b/util/pkey.c
@@ -88,6 +88,59 @@ static inline __attribute__((always_inline)) intptr_t local_syscall3(
return ret;
}
+/*
+ * Custom structures to parse FPU xstate and extract the PKRU register.
+ * These match the Linux kernel x86_64 signal frame ABI.
+ */
+struct fpstate_64 {
+ uint16_t cwd;
+ uint16_t swd;
+ uint16_t twd;
+ uint16_t fop;
+ uint64_t rip;
+ uint64_t rdp;
+ uint32_t mxcsr;
+ uint32_t mxcsr_mask;
+ uint32_t st_space[32]; /* 8x FP registers, 16 bytes each */
+ uint32_t xmm_space[64]; /* 16x XMM registers, 16 bytes each */
+ uint32_t reserved1[12];
+ union {
+ uint32_t reserved2[12];
+ struct {
+ uint32_t magic1;
+ uint32_t extended_size;
+ uint64_t xstate_bv;
+ uint32_t xstate_size;
+ uint32_t padding[7];
+ } sw_reserved; /* Extended state is encoded here */
+ };
+};
+
+#define FP_XSTATE_MAGIC1 0x46505853U
+#define FP_XSTATE_MAGIC2 0x46505845U
+#define XFEATURE_PKRU 9
+#define XSTATE_PKRU (1ULL << XFEATURE_PKRU)
+
+static int pkru_offset = -2; /* -2 means uninitialized */
+
+static int __attribute__((unused)) get_pkru_offset(void)
+{
+ uint32_t eax, ebx, ecx, edx;
+ __cpuid_count(0xd, XFEATURE_PKRU, eax, ebx, ecx, edx);
+ if (ebx == 0) {
+ return -1;
+ }
+ return (int)ebx;
+}
+
+static int __attribute__((unused)) qemu_get_pkru_offset(void)
+{
+ if (pkru_offset == -2) {
+ pkru_offset = get_pkru_offset();
+ }
+ return pkru_offset;
+}
+
__attribute__((target("pku"))) void qemu_init_guest_memory_pkey(void)
{
if (guest_memory_pkey != -1) {
--
2.55.0.737.g08866a6d13-goog
next prev parent reply other threads:[~2026-08-18 20:44 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-18 20:44 [RFC PATCH 0/6] Protect VMM from speculative attacks using x86 PKRU Jacky Li
2026-08-18 20:44 ` [RFC PATCH 1/6] x86: Introduce basic PKRU hardware wrappers Jacky Li
2026-08-18 20:44 ` [RFC PATCH 2/6] kvm: Add guest memory Pkey initialization Jacky Li
2026-08-18 22:03 ` Dave Hansen
2026-08-18 20:44 ` [RFC PATCH 3/6] physmem: Tag guest RAMBlocks with Protection Key Jacky Li
2026-08-18 20:44 ` [RFC PATCH 4/6] kvm: Lock guest RAMBlocks via PKRU during host userspace execution Jacky Li
2026-08-18 20:44 ` Jacky Li [this message]
2026-08-18 20:44 ` [RFC PATCH 6/6] kvm: Implement SIGSEGV sentinel for Pkey recovery Jacky Li
2026-08-18 21:55 ` Dave Hansen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260818-feature-pkey-dev-v1-5-8c0ef96a4da9@google.com \
--to=jackyli@google.com \
--cc=dave.hansen@linux.intel.com \
--cc=jackmanb@google.com \
--cc=jthoughton@google.com \
--cc=kvm@vger.kernel.org \
--cc=mizhang@google.com \
--cc=pbonzini@redhat.com \
--cc=peterx@redhat.com \
--cc=philmd@mailo.com \
--cc=qemu-devel@nongnu.org \
--cc=reijiw@google.com \
--cc=richard.henderson@linaro.org \
--cc=zhao1.liu@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox