All of lore.kernel.org
 help / color / mirror / Atom feed
From: vsntk18@gmail.com
To: kvm@vger.kernel.org
Cc: pbonzini@redhat.com, seanjc@google.com, jroedel@suse.de,
	papaluri@amd.com, zxwang42@gmail.com,
	Vasant Karasulli <vkarasulli@suse.de>,
	Varad Gautam <varad.gautam@suse.com>,
	Marc Orr <marcorr@google.com>
Subject: [kvm-unit-tests PATCH v6 07/11] lib/x86: Move xsave helpers to lib/
Date: Thu, 11 Apr 2024 19:29:40 +0200	[thread overview]
Message-ID: <20240411172944.23089-8-vsntk18@gmail.com> (raw)
In-Reply-To: <20240411172944.23089-1-vsntk18@gmail.com>

From: Vasant Karasulli <vkarasulli@suse.de>

Processing CPUID #VC for AMD SEV-ES requires copying xcr0 into GHCB.
Move the xsave read/write helpers used by xsave testcase to lib/x86
to share as common code.

Signed-off-by: Varad Gautam <varad.gautam@suse.com>
Signed-off-by: Vasant Karasulli <vkarasulli@suse.de>
Reviewed-by: Marc Orr <marcorr@google.com>
---
 lib/x86/processor.h | 10 ----------
 lib/x86/xsave.c     | 26 ++++++++++++++++++++++++++
 lib/x86/xsave.h     | 15 +++++++++++++++
 x86/Makefile.common |  1 +
 x86/xsave.c         | 17 +----------------
 5 files changed, 43 insertions(+), 26 deletions(-)
 create mode 100644 lib/x86/xsave.c
 create mode 100644 lib/x86/xsave.h

diff --git a/lib/x86/processor.h b/lib/x86/processor.h
index b324cbf0..d839308f 100644
--- a/lib/x86/processor.h
+++ b/lib/x86/processor.h
@@ -477,16 +477,6 @@ static inline uint64_t rdpmc(uint32_t index)
 	return val;
 }

-static inline int xgetbv_safe(u32 index, u64 *result)
-{
-	return rdreg64_safe(".byte 0x0f,0x01,0xd0", index, result);
-}
-
-static inline int xsetbv_safe(u32 index, u64 value)
-{
-	return wrreg64_safe(".byte 0x0f,0x01,0xd1", index, value);
-}
-
 static inline int write_cr0_safe(ulong val)
 {
 	return asm_safe("mov %0,%%cr0", "r" (val));
diff --git a/lib/x86/xsave.c b/lib/x86/xsave.c
new file mode 100644
index 00000000..85aae78f
--- /dev/null
+++ b/lib/x86/xsave.c
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "libcflat.h"
+#include "xsave.h"
+#include "processor.h"
+
+int xgetbv_safe(u32 index, u64 *result)
+{
+	return rdreg64_safe(".byte 0x0f,0x01,0xd0", index, result);
+}
+
+int xsetbv_safe(u32 index, u64 value)
+{
+	return wrreg64_safe(".byte 0x0f,0x01,0xd1", index, value);
+}
+
+uint64_t get_supported_xcr0(void)
+{
+    struct cpuid r;
+    r = cpuid_indexed(0xd, 0);
+    printf("eax %x, ebx %x, ecx %x, edx %x\n",
+            r.a, r.b, r.c, r.d);
+    return r.a + ((u64)r.d << 32);
+}
+
+
diff --git a/lib/x86/xsave.h b/lib/x86/xsave.h
new file mode 100644
index 00000000..34c1c149
--- /dev/null
+++ b/lib/x86/xsave.h
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _X86_XSAVE_H_
+#define _X86_XSAVE_H_
+
+#define XCR_XFEATURE_ENABLED_MASK       0x00000000
+#define XCR_XFEATURE_ILLEGAL_MASK       0x00000010
+
+#define XSTATE_FP       0x1
+#define XSTATE_SSE      0x2
+#define XSTATE_YMM      0x4
+
+int xgetbv_safe(u32 index, u64 *result);
+int xsetbv_safe(u32 index, u64 value);
+uint64_t get_supported_xcr0(void);
+#endif
diff --git a/x86/Makefile.common b/x86/Makefile.common
index 25ae6f78..c1e90b86 100644
--- a/x86/Makefile.common
+++ b/x86/Makefile.common
@@ -23,6 +23,7 @@ cflatobjs += lib/x86/stack.o
 cflatobjs += lib/x86/fault_test.o
 cflatobjs += lib/x86/delay.o
 cflatobjs += lib/x86/pmu.o
+cflatobjs += lib/x86/xsave.o
 ifeq ($(CONFIG_EFI),y)
 cflatobjs += lib/x86/amd_sev.o
 cflatobjs += lib/x86/amd_sev_vc.o
diff --git a/x86/xsave.c b/x86/xsave.c
index 5d80f245..f3cbfca4 100644
--- a/x86/xsave.c
+++ b/x86/xsave.c
@@ -1,6 +1,7 @@
 #include "libcflat.h"
 #include "desc.h"
 #include "processor.h"
+#include "xsave.h"

 #ifdef __x86_64__
 #define uint64_t unsigned long
@@ -8,22 +9,6 @@
 #define uint64_t unsigned long long
 #endif

-static uint64_t get_supported_xcr0(void)
-{
-    struct cpuid r;
-    r = cpuid_indexed(0xd, 0);
-    printf("eax %x, ebx %x, ecx %x, edx %x\n",
-            r.a, r.b, r.c, r.d);
-    return r.a + ((u64)r.d << 32);
-}
-
-#define XCR_XFEATURE_ENABLED_MASK       0x00000000
-#define XCR_XFEATURE_ILLEGAL_MASK       0x00000010
-
-#define XSTATE_FP       0x1
-#define XSTATE_SSE      0x2
-#define XSTATE_YMM      0x4
-
 static void test_xsave(void)
 {
     unsigned long cr4;
--
2.34.1


  parent reply	other threads:[~2024-04-11 17:29 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-11 17:29 [PATCH v6 00/11] Add #VC exception handling for AMD SEV-ES vsntk18
2024-04-11 17:29 ` [kvm-unit-tests PATCH v6 01/11] x86: AMD SEV-ES: Setup #VC exception handler " vsntk18
2024-04-11 17:29 ` [kvm-unit-tests PATCH v6 02/11] x86: Move svm.h to lib/x86/ vsntk18
2024-04-11 17:29 ` [kvm-unit-tests PATCH v6 03/11] lib: Define unlikely()/likely() macros in libcflat.h vsntk18
2024-04-12  6:31   ` Andrew Jones
2024-04-11 17:29 ` [kvm-unit-tests PATCH v6 04/11] lib: x86: Import insn decoder from Linux vsntk18
2024-04-11 17:29 ` [kvm-unit-tests PATCH v6 05/11] x86: AMD SEV-ES: Pull related GHCB definitions and helpers " vsntk18
2024-04-11 17:29 ` [kvm-unit-tests PATCH v6 06/11] x86: AMD SEV-ES: Prepare for #VC processing vsntk18
2024-04-11 17:29 ` vsntk18 [this message]
2024-04-11 17:29 ` [kvm-unit-tests PATCH v6 08/11] x86: AMD SEV-ES: Handle CPUID #VC vsntk18
2024-04-11 17:29 ` [kvm-unit-tests PATCH v6 09/11] x86: AMD SEV-ES: Handle MSR #VC vsntk18
2024-04-11 17:29 ` [kvm-unit-tests PATCH v6 10/11] x86: AMD SEV-ES: Handle IOIO #VC vsntk18
2024-04-11 17:29 ` [kvm-unit-tests PATCH v6 11/11] x86: AMD SEV-ES: Handle string IO for " vsntk18

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=20240411172944.23089-8-vsntk18@gmail.com \
    --to=vsntk18@gmail.com \
    --cc=jroedel@suse.de \
    --cc=kvm@vger.kernel.org \
    --cc=marcorr@google.com \
    --cc=papaluri@amd.com \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=varad.gautam@suse.com \
    --cc=vkarasulli@suse.de \
    --cc=zxwang42@gmail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.