Skip to contentSkip to conversation

x86/svm: Untangle the remaining global svm/ headers

PATCH In review Andrew Cooper 2 participants 7 messages Dec 12, 2025 – Dec 15, 2025

lore
Share & tools
lore
#0 lore raw

cover x86/svm: Untangle the remaining global svm/ headers

This turned out to be a little easier than I was expecting.

https://gitlab.com/xen-project/hardware/xen-staging/-/pipelines/2212088192

Andrew Cooper (5):
x86/svm: Move asm/hvm/svm/svm.h up one directory x86/svm: Move svm_intercept_msr() into svm.h x86/svm: Introduce svm-types.h
x86/svm: Merge nestedsvm.h into svm-types.h x86/svm: Merge vmcb.h into svm-types.h

 xen/arch/x86/cpu-policy.c                     |  2 +-
 xen/arch/x86/cpu/vpmu_amd.c                   |  5 +-
 xen/arch/x86/domain.c                         |  2 +-
 xen/arch/x86/hvm/svm/asid.c                   |  2 +-
 xen/arch/x86/hvm/svm/emulate.c                |  3 +-
 xen/arch/x86/hvm/svm/intr.c                   |  1 -
 xen/arch/x86/hvm/svm/nestedsvm.c              |  3 +-
 xen/arch/x86/hvm/svm/svm.c                    |  3 +-
 xen/arch/x86/hvm/svm/vmcb.c                   |  3 +-
 xen/arch/x86/include/asm/hvm/domain.h         |  2 +-
 .../asm/hvm/{svm/nestedsvm.h => svm-types.h}  | 76 +++++++++++++------
 xen/arch/x86/include/asm/hvm/{svm => }/svm.h  |  8 ++
 xen/arch/x86/include/asm/hvm/svm/vmcb.h       | 63 ---------------
 xen/arch/x86/include/asm/hvm/vcpu.h           |  3 +-
 xen/arch/x86/spec_ctrl.c                      |  2 +-
 15 files changed, 73 insertions(+), 105 deletions(-)
 rename xen/arch/x86/include/asm/hvm/{svm/nestedsvm.h => svm-types.h} (52%)
 rename xen/arch/x86/include/asm/hvm/{svm => }/svm.h (86%)
 delete mode 100644 xen/arch/x86/include/asm/hvm/svm/vmcb.h

base-commit: e6836f213f0f9d3aeaf7c89ce1e20716795b37bd

Signature · 1 line
2.39.5
#1 lore raw

PATCH 1/5 x86/svm: Move asm/hvm/svm/svm.h up one directory

... in preparation to remove the svm/ directory entirely.

intr.c doesn't need to include svm.h at all.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/cpu-policy.c                    | 2 +-
 xen/arch/x86/domain.c                        | 2 +-
 xen/arch/x86/hvm/svm/asid.c                  | 2 +-
 xen/arch/x86/hvm/svm/emulate.c               | 2 +-
 xen/arch/x86/hvm/svm/intr.c                  | 1 -
 xen/arch/x86/hvm/svm/nestedsvm.c             | 2 +-
 xen/arch/x86/hvm/svm/svm.c                   | 2 +-
 xen/arch/x86/hvm/svm/vmcb.c                  | 2 +-
 xen/arch/x86/include/asm/hvm/{svm => }/svm.h | 0
 xen/arch/x86/spec_ctrl.c                     | 2 +-
 10 files changed, 8 insertions(+), 9 deletions(-)
 rename xen/arch/x86/include/asm/hvm/{svm => }/svm.h (100%)
xen/arch/x86/cpu-policy.c +1 -1
index 0a7ef15f7210..372d11f2ff20 100644
@@ -9,7 +9,7 @@
 #include <asm/amd.h>
 #include <asm/cpu-policy.h>
 #include <asm/hvm/nestedhvm.h>
-#include <asm/hvm/svm/svm.h>
+#include <asm/hvm/svm.h>
 #include <asm/intel-family.h>
 #include <asm/msr-index.h>
 #include <asm/paging.h>
xen/arch/x86/domain.c +1 -1
index b15120180993..7632d5e2d62d 100644
@@ -48,7 +48,7 @@
 #include <asm/guest-msr.h>
 #include <asm/hvm/hvm.h>
 #include <asm/hvm/nestedhvm.h>
-#include <asm/hvm/svm/svm.h>
+#include <asm/hvm/svm.h>
 #include <asm/hvm/viridian.h>
 #include <asm/i387.h>
 #include <asm/idt.h>
xen/arch/x86/hvm/svm/asid.c +1 -1
index 0e115243a3af..53aa5d05125f 100644
@@ -6,7 +6,7 @@
 
 #include <asm/amd.h>
 #include <asm/hvm/nestedhvm.h>
-#include <asm/hvm/svm/svm.h>
+#include <asm/hvm/svm.h>
 
 #include "svm.h"
 #include "vmcb.h"
xen/arch/x86/hvm/svm/emulate.c +1 -1
index d8aa45590885..be3a710fa53d 100644
@@ -11,7 +11,7 @@
 #include <asm/msr.h>
 #include <asm/hvm/emulate.h>
 #include <asm/hvm/hvm.h>
-#include <asm/hvm/svm/svm.h>
+#include <asm/hvm/svm.h>
 #include <asm/hvm/svm/vmcb.h>
 
 #include "svm.h"
xen/arch/x86/hvm/svm/intr.c +0 -1
index 931973a9fad4..702c071e8962 100644
@@ -17,7 +17,6 @@
 #include <asm/hvm/hvm.h>
 #include <asm/hvm/io.h>
 #include <asm/hvm/vlapic.h>
-#include <asm/hvm/svm/svm.h>
 #include <asm/hvm/nestedhvm.h> /* for nestedhvm_vcpu_in_guestmode */
 #include <asm/vm_event.h>
 #include <xen/event.h>
xen/arch/x86/hvm/svm/nestedsvm.c +1 -1
index 63ed6c86b775..5718ca7f7f0f 100644
@@ -6,7 +6,7 @@
  */
 
 #include <asm/hvm/support.h>
-#include <asm/hvm/svm/svm.h>
+#include <asm/hvm/svm.h>
 #include <asm/hvm/svm/vmcb.h>
 #include <asm/hvm/nestedhvm.h>
 #include <asm/paging.h> /* paging_mode_hap */
xen/arch/x86/hvm/svm/svm.c +1 -1
index 15d45cbb57c5..86a030b02c27 100644
@@ -26,7 +26,7 @@
 #include <asm/hvm/monitor.h>
 #include <asm/hvm/nestedhvm.h>
 #include <asm/hvm/support.h>
-#include <asm/hvm/svm/svm.h>
+#include <asm/hvm/svm.h>
 #include <asm/hvm/svm/vmcb.h>
 #include <asm/i387.h>
 #include <asm/idt.h>
xen/arch/x86/hvm/svm/vmcb.c +1 -1
index 7bde6e98ce03..463e527a7d98 100644
@@ -15,7 +15,7 @@
 #include <xen/softirq.h>
 
 #include <asm/guest-msr.h>
-#include <asm/hvm/svm/svm.h>
+#include <asm/hvm/svm.h>
 #include <asm/hvm/svm/vmcb.h>
 #include <asm/msr-index.h>
 #include <asm/p2m.h>
xen/arch/x86/include/asm/hvm/svm.h renamed from xen/arch/x86/include/asm/hvm/svm/svm.h +0 -0
similarity index 100%
rename from xen/arch/x86/include/asm/hvm/svm/svm.h
rename to xen/arch/x86/include/asm/hvm/svm.h
xen/arch/x86/spec_ctrl.c +1 -1
index e71f62c60186..dd0413e1fc13 100644
@@ -11,7 +11,7 @@
 #include <xen/warning.h>
 
 #include <asm/amd.h>
-#include <asm/hvm/svm/svm.h>
+#include <asm/hvm/svm.h>
 #include <asm/intel-family.h>
 #include <asm/microcode.h>
 #include <asm/msr.h>
Signature · 1 line
2.39.5
#2 lore raw

PATCH 2/5 x86/svm: Move svm_intercept_msr() into svm.h

In reply to Andrew Cooper

The single user of svm_intercept_msr() outside of the main svm logic is in vpmu_amd.c. Currently the declaration is picked up transitively through xen/sched.h, so include asm/hvm/svm.h explicitly.

Sort the headers while doing so.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/cpu/vpmu_amd.c             | 5 ++++-
 xen/arch/x86/include/asm/hvm/svm.h      | 8 ++++++++
 xen/arch/x86/include/asm/hvm/svm/vmcb.h | 8 --------
 3 files changed, 12 insertions(+), 9 deletions(-)
xen/arch/x86/cpu/vpmu_amd.c +4 -1
index a6117dfebf2a..fa157d45dd01 100644
@@ -13,10 +13,13 @@
 #include <xen/err.h>
 #include <xen/sched.h>
 #include <xen/xenoprof.h>
+
 #include <asm/apic.h>
-#include <asm/vpmu.h>
 #include <asm/hvm/save.h>
+#include <asm/hvm/svm.h>
 #include <asm/hvm/vlapic.h>
+#include <asm/vpmu.h>
+
 #include <public/pmu.h>
 
 #define MSR_F10H_EVNTSEL_GO_SHIFT   40
xen/arch/x86/include/asm/hvm/svm.h +8 -0
index 4eeeb25da90c..a6d7e4aed3b1 100644
@@ -57,4 +57,12 @@ static inline bool cpu_has_svm_feature(unsigned int feat)
 #define cpu_has_svm_sss       cpu_has_svm_feature(SVM_FEATURE_SSS)
 #define cpu_has_svm_spec_ctrl cpu_has_svm_feature(SVM_FEATURE_SPEC_CTRL)
 
+#define MSR_INTERCEPT_NONE    0
+#define MSR_INTERCEPT_READ    1
+#define MSR_INTERCEPT_WRITE   2
+#define MSR_INTERCEPT_RW      (MSR_INTERCEPT_WRITE | MSR_INTERCEPT_READ)
+void svm_intercept_msr(struct vcpu *v, uint32_t msr, int flags);
+#define svm_disable_intercept_for_msr(v, msr) svm_intercept_msr((v), (msr), MSR_INTERCEPT_NONE)
+#define svm_enable_intercept_for_msr(v, msr) svm_intercept_msr((v), (msr), MSR_INTERCEPT_RW)
+
 #endif /* __ASM_X86_HVM_SVM_H__ */
xen/arch/x86/include/asm/hvm/svm/vmcb.h +0 -8
index 41bcc9f0d862..d1e200df564c 100644
@@ -42,14 +42,6 @@ struct svm_vcpu {
     uint64_t guest_sysenter_eip;
 };
 
-#define MSR_INTERCEPT_NONE    0
-#define MSR_INTERCEPT_READ    1
-#define MSR_INTERCEPT_WRITE   2
-#define MSR_INTERCEPT_RW      (MSR_INTERCEPT_WRITE | MSR_INTERCEPT_READ)
-void svm_intercept_msr(struct vcpu *v, uint32_t msr, int flags);
-#define svm_disable_intercept_for_msr(v, msr) svm_intercept_msr((v), (msr), MSR_INTERCEPT_NONE)
-#define svm_enable_intercept_for_msr(v, msr) svm_intercept_msr((v), (msr), MSR_INTERCEPT_RW)
-
 #endif /* ASM_X86_HVM_SVM_VMCS_H__ */
 
 /*
Signature · 1 line
2.39.5
#3 lore raw

PATCH 3/5 x86/svm: Introduce svm-types.h

In reply to Andrew Cooper

In order to reduce the header tangle around xen/sched.h, we need to split types away from other declarations.

Introduce a new svm/svm-types.h to do this, including it from hvm/domain.h and hvm/vcpu.h, and dropping all other inclusions of svm/vmcb.h and svm/nestedhvm.h

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/hvm/svm/emulate.c           |  1 -
 xen/arch/x86/hvm/svm/nestedsvm.c         |  1 -
 xen/arch/x86/hvm/svm/svm.c               |  1 -
 xen/arch/x86/hvm/svm/vmcb.c              |  1 -
 xen/arch/x86/include/asm/hvm/domain.h    |  2 +-
 xen/arch/x86/include/asm/hvm/svm-types.h | 11 +++++++++++
 xen/arch/x86/include/asm/hvm/vcpu.h      |  3 +--
 7 files changed, 13 insertions(+), 7 deletions(-)
 create mode 100644 xen/arch/x86/include/asm/hvm/svm-types.h
xen/arch/x86/hvm/svm/emulate.c +0 -1
index be3a710fa53d..1f5436c8ea5d 100644
@@ -12,7 +12,6 @@
 #include <asm/hvm/emulate.h>
 #include <asm/hvm/hvm.h>
 #include <asm/hvm/svm.h>
-#include <asm/hvm/svm/vmcb.h>
 
 #include "svm.h"
 #include "vmcb.h"
xen/arch/x86/hvm/svm/nestedsvm.c +0 -1
index 5718ca7f7f0f..a63ec613465f 100644
@@ -7,7 +7,6 @@
 
 #include <asm/hvm/support.h>
 #include <asm/hvm/svm.h>
-#include <asm/hvm/svm/vmcb.h>
 #include <asm/hvm/nestedhvm.h>
 #include <asm/paging.h> /* paging_mode_hap */
 #include <asm/event.h> /* for local_event_delivery_(en|dis)able */
xen/arch/x86/hvm/svm/svm.c +0 -1
index 86a030b02c27..1208999454d3 100644
@@ -27,7 +27,6 @@
 #include <asm/hvm/nestedhvm.h>
 #include <asm/hvm/support.h>
 #include <asm/hvm/svm.h>
-#include <asm/hvm/svm/vmcb.h>
 #include <asm/i387.h>
 #include <asm/idt.h>
 #include <asm/iocap.h>
xen/arch/x86/hvm/svm/vmcb.c +0 -1
index 463e527a7d98..cbee10d0463d 100644
@@ -16,7 +16,6 @@
 
 #include <asm/guest-msr.h>
 #include <asm/hvm/svm.h>
-#include <asm/hvm/svm/vmcb.h>
 #include <asm/msr-index.h>
 #include <asm/p2m.h>
 #include <asm/spec_ctrl.h>
xen/arch/x86/include/asm/hvm/domain.h +1 -1
index 83be2bd1c29c..be1a0d8c5b9f 100644
@@ -15,7 +15,7 @@
 
 #include <asm/hvm/io.h>
 #include <asm/hvm/vmx/vmcs.h>
-#include <asm/hvm/svm/vmcb.h>
+#include <asm/hvm/svm-types.h>
 
 #ifdef CONFIG_MEM_SHARING
 struct mem_sharing_domain
xen/arch/x86/include/asm/hvm/svm-types.h new file +11 -0
new file mode 100644
index 000000000000..1c26daa981a7
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * SVM types needed to form struct domain/vcpu.
+ */
+#ifndef X86_SVM_TYPES_H
+#define X86_SVM_TYPES_H
+
+#include <asm/hvm/svm/vmcb.h>
+#include <asm/hvm/svm/nestedsvm.h>
+
+#endif /* X86_SVM_TYPES_H */
xen/arch/x86/include/asm/hvm/vcpu.h +1 -2
index eae9ac53767b..836138a4a677 100644
@@ -12,8 +12,7 @@
 #include <asm/hvm/vlapic.h>
 #include <asm/hvm/vmx/vmcs.h>
 #include <asm/hvm/vmx/vvmx.h>
-#include <asm/hvm/svm/vmcb.h>
-#include <asm/hvm/svm/nestedsvm.h>
+#include <asm/hvm/svm-types.h>
 #include <asm/mtrr.h>
 #include <public/hvm/ioreq.h>
 
Signature · 1 line
2.39.5
#4 lore raw

PATCH 4/5 x86/svm: Merge nestedsvm.h into svm-types.h

In reply to Andrew Cooper

While doing so, move the ns_gif field to beside ns_hap_enabled to remove 7 padding bytes, and make some style fixes to comments/etc.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/include/asm/hvm/svm-types.h     | 66 +++++++++++++++-
 xen/arch/x86/include/asm/hvm/svm/nestedsvm.h | 79 --------------------
 2 files changed, 65 insertions(+), 80 deletions(-)
 delete mode 100644 xen/arch/x86/include/asm/hvm/svm/nestedsvm.h
xen/arch/x86/include/asm/hvm/svm-types.h +65 -1
index 1c26daa981a7..637b94fa37da 100644
@@ -5,7 +5,71 @@
 #ifndef X86_SVM_TYPES_H
 #define X86_SVM_TYPES_H
 
+#include <xen/types.h>
+
 #include <asm/hvm/svm/vmcb.h>
-#include <asm/hvm/svm/nestedsvm.h>
+
+struct nestedsvm {
+    uint64_t ns_msr_hsavepa; /* MSR HSAVE_PA value */
+
+    /*
+     * l1 guest physical address of virtual vmcb used by prior VMRUN.  Needed
+     * for VMCB Cleanbit emulation.
+     */
+    uint64_t ns_ovvmcb_pa;
+
+    /* Cached real intercepts of the l2 guest */
+    uint32_t ns_cr_intercepts;
+    uint32_t ns_dr_intercepts;
+    uint32_t ns_exception_intercepts;
+    uint32_t ns_general1_intercepts;
+    uint32_t ns_general2_intercepts;
+
+    /* Cached real MSR permission bitmaps of the l2 guest */
+    unsigned long *ns_cached_msrpm;
+    /* Merged MSR permission bitmap */
+    unsigned long *ns_merged_msrpm;
+
+    /* guest physical address of virtual io permission map */
+    paddr_t ns_iomap_pa, ns_oiomap_pa;
+    /* Shadow io permission map */
+    unsigned long *ns_iomap;
+
+     /*
+      * Cached guest_cr[0] of l1 guest while l2 guest runs.  Needed to handle
+      * FPU context switching.
+      */
+    uint64_t ns_cr0;
+
+    /*
+     * Cache guest cr3/host cr3 the guest sets up for the l2 guest.
+     * Used by Shadow-on-Shadow and Nested-on-Nested.
+     * ns_vmcb_guestcr3: in l2 guest physical address space and points to
+     *     the l2 guest page table
+     * ns_vmcb_hostcr3: in l1 guest physical address space and points to
+     *     the l1 guest nested page table
+     */
+    uint64_t ns_vmcb_guestcr3, ns_vmcb_hostcr3;
+    uint32_t ns_asid;
+
+    bool ns_gif;
+    bool ns_hap_enabled;
+
+    /* Only meaningful when vmexit_pending flag is set */
+    struct {
+        uint64_t exitcode;  /* native exitcode to inject into l1 guest */
+        uint64_t exitinfo1; /* additional information to the exitcode */
+        uint64_t exitinfo2; /* additional information to the exitcode */
+    } ns_vmexit;
+
+    union {
+        uint32_t bytes;
+        struct {
+            uint32_t rflagsif:1;
+            uint32_t vintrmask:1;
+            uint32_t reserved:30;
+        } fields;
+    } ns_hostflags;
+};
 
 #endif /* X86_SVM_TYPES_H */
xen/arch/x86/include/asm/hvm/svm/nestedsvm.h deleted +0 -79

79 lines not on this page · raw message

Signature · 1 line
2.39.5
#5 lore raw

PATCH 5/5 x86/svm: Merge vmcb.h into svm-types.h

In reply to Andrew Cooper

While doing so, make some style fixes to comments/etc.

This removes the include/asm/hvm/svm/ subdirectory.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/include/asm/hvm/svm-types.h | 32 +++++++++++++-
 xen/arch/x86/include/asm/hvm/svm/vmcb.h  | 55 ------------------------
 2 files changed, 31 insertions(+), 56 deletions(-)
 delete mode 100644 xen/arch/x86/include/asm/hvm/svm/vmcb.h
xen/arch/x86/include/asm/hvm/svm-types.h +31 -1
index 637b94fa37da..051b235d8f69 100644
@@ -7,7 +7,37 @@
 
 #include <xen/types.h>
 
-#include <asm/hvm/svm/vmcb.h>
+struct svm_domain {
+    union {
+        uint64_t raw[2];
+        struct {
+            uint64_t length;
+            uint64_t status;
+        };
+    } osvw;
+};
+
+struct svm_vcpu {
+    struct vmcb_struct *vmcb;
+    unsigned long *msrpm;
+    uint64_t vmcb_pa;
+    int     launch_core;
+
+    uint8_t vmcb_sync_state; /* enum vmcb_sync_state */
+
+    /* VMCB has a cached instruction from #PF/#NPF Decode Assist? */
+    uint8_t cached_insn_len; /* Zero if no cached instruction. */
+
+    /*
+     * Upper four bytes are undefined in the VMCB, therefore we can't use the
+     * fields in the VMCB. Write a 64bit value and then read a 64bit value is
+     * fine unless there's a VMRUN/VMEXIT in between which clears the upper
+     * four bytes.
+     */
+    uint64_t guest_sysenter_cs;
+    uint64_t guest_sysenter_esp;
+    uint64_t guest_sysenter_eip;
+};
 
 struct nestedsvm {
     uint64_t ns_msr_hsavepa; /* MSR HSAVE_PA value */
xen/arch/x86/include/asm/hvm/svm/vmcb.h deleted +0 -55

55 lines not on this page · raw message

Signature · 1 line
2.39.5
Acked-by on the v1 series
#6 lore raw
In reply to Andrew Cooper
Quoted 10 lines · Andrew Cooper

This turned out to be a little easier than I was expecting.

https://gitlab.com/xen-project/hardware/xen-staging/-/pipelines/2212088192

Andrew Cooper (5):
x86/svm: Move asm/hvm/svm/svm.h up one directory x86/svm: Move svm_intercept_msr() into svm.h x86/svm: Introduce svm-types.h
x86/svm: Merge nestedsvm.h into svm-types.h x86/svm: Merge vmcb.h into svm-types.h

ideally with svm/svm-types.h in patch 3's description corrected to hvm/svm-types.h.

Jan