From: Paolo Bonzini <pbonzini@redhat.com>
To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org
Cc: Binbin Wu <binbin.wu@linux.intel.com>,
Uros Bizjak <ubizjak@gmail.com>,
"Chang S. Bae" <chang.seok.bae@intel.com>,
Sean Christopherson <seanjc@google.com>
Subject: [PATCH v2 5/7] KVM: SVM: extract RESTORE_*_SPEC_CTRL_BODY out of svm/vmenter.S
Date: Mon, 27 Apr 2026 06:58:46 -0400 [thread overview]
Message-ID: <20260427105848.44865-6-pbonzini@redhat.com> (raw)
In-Reply-To: <20260427105848.44865-1-pbonzini@redhat.com>
Pure code movement, in preparation for using these macros for
VMX as well.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
arch/x86/kvm/svm/vmenter.S | 62 ----------------------------------
arch/x86/kvm/vmenter.h | 68 +++++++++++++++++++++++++++++++++++++-
2 files changed, 67 insertions(+), 63 deletions(-)
diff --git a/arch/x86/kvm/svm/vmenter.S b/arch/x86/kvm/svm/vmenter.S
index 2bc7ecb338a9..f523d9e49839 100644
--- a/arch/x86/kvm/svm/vmenter.S
+++ b/arch/x86/kvm/svm/vmenter.S
@@ -41,35 +41,6 @@
"jmp 800f", X86_FEATURE_MSR_SPEC_CTRL, \
"", X86_FEATURE_V_SPEC_CTRL
.endm
-.macro RESTORE_GUEST_SPEC_CTRL_BODY guest_spec_ctrl:req, label:req
- /*
- * SPEC_CTRL handling: if the guest's SPEC_CTRL value differs from the
- * host's, write the MSR. This is kept out-of-line so that the common
- * case does not have to jump.
- *
- * IMPORTANT: To avoid RSB underflow attacks and any other nastiness,
- * there must not be any returns or indirect branches between this code
- * and vmentry.
- */
-#ifdef CONFIG_X86_64
- mov \guest_spec_ctrl, %rdx
- cmp PER_CPU_VAR(x86_spec_ctrl_current), %rdx
- je \label
- movl %edx, %eax
- shr $32, %rdx
-#else
- mov \guest_spec_ctrl, %eax
- mov PER_CPU_VAR(x86_spec_ctrl_current), %ecx
- xor %eax, %ecx
- mov 4 + \guest_spec_ctrl, %edx
- mov PER_CPU_VAR(x86_spec_ctrl_current + 4), %esi
- xor %edx, %esi
- or %esi, %ecx
- je \label
-#endif
- mov $MSR_IA32_SPEC_CTRL, %ecx
- wrmsr
-.endm
.macro RESTORE_HOST_SPEC_CTRL
/* No need to do anything if SPEC_CTRL is unset or V_SPEC_CTRL is set */
@@ -77,39 +48,6 @@
"jmp 900f", X86_FEATURE_MSR_SPEC_CTRL, \
"", X86_FEATURE_V_SPEC_CTRL
.endm
-.macro RESTORE_HOST_SPEC_CTRL_BODY guest_spec_ctrl:req, enter_flags:req, label:req
- /* Same for after vmexit. */
- mov $MSR_IA32_SPEC_CTRL, %ecx
-
- /*
- * Load the value that the guest had written into MSR_IA32_SPEC_CTRL,
- * if it was not intercepted during guest execution.
- */
- testl $KVM_ENTER_SAVE_SPEC_CTRL, \enter_flags
- jz 998f
- rdmsr
- movl %eax, \guest_spec_ctrl
- movl %edx, 4 + \guest_spec_ctrl
-998:
- /* Now restore the host value of the MSR if different from the guest's. */
-#ifdef CONFIG_X86_64
- mov PER_CPU_VAR(x86_spec_ctrl_current), %rdx
- cmp \guest_spec_ctrl, %rdx
- je \label
- movl %edx, %eax
- shr $32, %rdx
-#else
- mov PER_CPU_VAR(x86_spec_ctrl_current), %eax
- mov \guest_spec_ctrl, %esi
- xor %eax, %esi
- mov PER_CPU_VAR(x86_spec_ctrl_current + 4), %edx
- mov 4 + \guest_spec_ctrl, %edi
- xor %edx, %edi
- or %edi, %esi
- je \label
-#endif
- wrmsr
-.endm
#define SVM_CLEAR_CPU_BUFFERS \
ALTERNATIVE "", __CLEAR_CPU_BUFFERS, X86_FEATURE_CLEAR_CPU_BUF_VM
diff --git a/arch/x86/kvm/vmenter.h b/arch/x86/kvm/vmenter.h
index 29cdae650069..84951e186859 100644
--- a/arch/x86/kvm/vmenter.h
+++ b/arch/x86/kvm/vmenter.h
@@ -6,4 +6,70 @@
#define KVM_ENTER_SAVE_SPEC_CTRL BIT(1)
#define KVM_ENTER_CLEAR_CPU_BUFFERS_FOR_MMIO BIT(2)
-#endif /* __KVM_X86_VMENTER_H */
+#ifdef __ASSEMBLER__
+.macro RESTORE_GUEST_SPEC_CTRL_BODY guest_spec_ctrl:req, label:req
+ /*
+ * SPEC_CTRL handling: if the guest's SPEC_CTRL value differs from the
+ * host's, write the MSR. This is kept out-of-line so that the common
+ * case does not have to jump.
+ *
+ * IMPORTANT: To avoid RSB underflow attacks and any other nastiness,
+ * there must not be any returns or indirect branches between this code
+ * and vmentry.
+ */
+#ifdef CONFIG_X86_64
+ mov \guest_spec_ctrl, %rdx
+ cmp PER_CPU_VAR(x86_spec_ctrl_current), %rdx
+ je \label
+ movl %edx, %eax
+ shr $32, %rdx
+#else
+ mov \guest_spec_ctrl, %eax
+ mov PER_CPU_VAR(x86_spec_ctrl_current), %ecx
+ xor %eax, %ecx
+ mov 4 + \guest_spec_ctrl, %edx
+ mov PER_CPU_VAR(x86_spec_ctrl_current + 4), %esi
+ xor %edx, %esi
+ or %esi, %ecx
+ je \label
+#endif
+ mov $MSR_IA32_SPEC_CTRL, %ecx
+ wrmsr
+.endm
+
+.macro RESTORE_HOST_SPEC_CTRL_BODY guest_spec_ctrl:req, enter_flags:req, label:req
+ /* Same for after vmexit. */
+ mov $MSR_IA32_SPEC_CTRL, %ecx
+
+ /*
+ * Load the value that the guest had written into MSR_IA32_SPEC_CTRL,
+ * if it was not intercepted during guest execution.
+ */
+ testl $KVM_ENTER_SAVE_SPEC_CTRL, \enter_flags
+ jz 998f
+ rdmsr
+ movl %eax, \guest_spec_ctrl
+ movl %edx, 4 + \guest_spec_ctrl
+998:
+ /* Now restore the host value of the MSR if different from the guest's. */
+#ifdef CONFIG_X86_64
+ mov PER_CPU_VAR(x86_spec_ctrl_current), %rdx
+ cmp \guest_spec_ctrl, %rdx
+ je \label
+ movl %edx, %eax
+ shr $32, %rdx
+#else
+ mov PER_CPU_VAR(x86_spec_ctrl_current), %eax
+ mov \guest_spec_ctrl, %esi
+ xor %eax, %esi
+ mov PER_CPU_VAR(x86_spec_ctrl_current + 4), %edx
+ mov 4 + \guest_spec_ctrl, %edi
+ xor %edx, %edi
+ or %edi, %esi
+ je \label
+#endif
+ wrmsr
+.endm
+
+#endif /* __ASSEMBLER__ */
+#endif /* __KVM_X86_ENTER_FLAGS_H */
--
2.52.0
next prev parent reply other threads:[~2026-04-27 10:59 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-27 10:58 [PATCH v2 0/7] KVM: VMX/SVM: use the same SPEC_CTRL assembly code Paolo Bonzini
2026-04-27 10:58 ` [PATCH v2 1/7] KVM: VMX: remove regs argument of __vmx_vcpu_run Paolo Bonzini
2026-04-27 10:58 ` [PATCH v2 2/7] KVM: VMX: more cleanups to __vmx_vcpu_run Paolo Bonzini
2026-04-27 10:58 ` [PATCH v2 3/7] KVM: SVM: prepare for making SPEC_CTRL switch common with VMX Paolo Bonzini
2026-04-27 10:58 ` [PATCH v2 4/7] KVM: SVM: adopt the same VMX_RUN_* flags as VMX Paolo Bonzini
2026-04-27 17:51 ` Chang S. Bae
2026-04-27 18:01 ` Paolo Bonzini
2026-04-27 10:58 ` Paolo Bonzini [this message]
2026-04-27 10:58 ` [PATCH v2 6/7] KVM: VMX: switch to RESTORE_GUEST_SPEC_CTRL_BODY Paolo Bonzini
2026-04-27 10:58 ` [PATCH v2 7/7] KVM: VMX: replace vmx_spec_ctrl_restore_host with RESTORE_HOST_SPEC_CTRL_BODY Paolo Bonzini
2026-04-27 17:51 ` Chang S. Bae
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=20260427105848.44865-6-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=binbin.wu@linux.intel.com \
--cc=chang.seok.bae@intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=seanjc@google.com \
--cc=ubizjak@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox