Skip to contentSkip to conversation

x86: make cpu virtualization support configurable

PATCH Superseded Sergiy Kibrik 3 participants 41 messages Jun 3, 2024 – Jun 10, 2024

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v3 cover x86: make cpu virtualization support configurable

This is yet another attempt to provide a means to render the cpu virtualization technology support in Xen configurable.
Currently, irrespectively of the target platform, both AMD-V and Intel VT-x drivers are built.
The series adds three new Kconfig controls, ALT2PM, SVM and VMX, that can be used to switch to a finer-grained configuration for a given platform, and reduce dead code.

The code separation is done using the new config guards.

Major changes in this series, comparing to v2, are introduction and adoption of using_vmx & using_svm macros, and turning of arch_vcpu_ioreq_completion() into optional callback. The latter is more rfc-style change, kind of a perspective on how things might look.
More specific changes are provided in per-patch changelog.

v2 series here:
https://lore.kernel.org/xen-devel/[email protected]/

-Sergiy

Sergiy Kibrik (9):
x86/altp2m: check if altp2m active when giving away p2midx x86/p2m: guard altp2m routines
x86: introduce CONFIG_ALTP2M Kconfig option
x86: introduce using_{svm,vmx} macros x86/nestedhvm: switch to using_{svm,vmx} check x86/vmx: guard access to cpu_has_vmx_* in common code x86/vpmu: guard calls to vmx/svm functions
ioreq: make arch_vcpu_ioreq_completion() an optional callback x86/vmx: replace CONFIG_HVM with CONFIG_VMX in vmx.h

Xenia Ragiadakou (7):
x86: introduce AMD-V and Intel VT-x Kconfig options x86/hvm: guard AMD-V and Intel VT-x hvm_function_table initializers x86/p2m: guard EPT functions with using_vmx macro x86/traps: guard vmx specific functions with usinc_vmx macro x86/domain: guard svm specific functions with usinc_svm macro x86/oprofile: guard svm specific symbols with CONFIG_SVM x86/hvm: make AMD-V and Intel VT-x support configurable

 xen/arch/arm/ioreq.c                    |  6 -----
 xen/arch/x86/Kconfig                    | 31 +++++++++++++++++++++++++
 xen/arch/x86/cpu/vpmu_amd.c             |  9 +++----
 xen/arch/x86/cpu/vpmu_intel.c           | 16 +++++++------
 xen/arch/x86/domain.c                   |  8 +++----
 xen/arch/x86/hvm/Makefile               |  4 ++--
 xen/arch/x86/hvm/hvm.c                  |  6 ++---
 xen/arch/x86/hvm/ioreq.c                | 23 ------------------
 xen/arch/x86/hvm/nestedhvm.c            |  4 ++--
 xen/arch/x86/hvm/viridian/viridian.c    |  4 ++--
 xen/arch/x86/hvm/vmx/vmx.c              | 16 +++++++++++++
 xen/arch/x86/include/asm/altp2m.h       | 15 +++++++-----
 xen/arch/x86/include/asm/hvm/hvm.h      |  5 +++-
 xen/arch/x86/include/asm/hvm/vmx/vmcs.h | 12 +++++-----
 xen/arch/x86/include/asm/hvm/vmx/vmx.h  |  2 +-
 xen/arch/x86/include/asm/p2m.h          | 17 +++++++++++++-
 xen/arch/x86/mm/Makefile                |  5 ++--
 xen/arch/x86/mm/hap/Makefile            |  2 +-
 xen/arch/x86/mm/p2m-basic.c             | 13 ++++++-----
 xen/arch/x86/mm/p2m-ept.c               |  2 +-
 xen/arch/x86/oprofile/op_model_athlon.c |  2 +-
 xen/arch/x86/traps.c                    | 13 ++++-------
 xen/common/ioreq.c                      |  5 +++-
 xen/include/xen/ioreq.h                 |  2 +-
 24 files changed, 132 insertions(+), 90 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v3 1/16 x86: introduce AMD-V and Intel VT-x Kconfig options

From: Xenia Ragiadakou <[email protected]>

Introduce two new Kconfig options, SVM and VMX, to allow code specific to each virtualization technology to be separated and, when not required, stripped.
CONFIG_SVM will be used to enable virtual machine extensions on platforms that implement the AMD Virtualization Technology (AMD-V).
CONFIG_VMX will be used to enable virtual machine extensions on platforms that implement the Intel Virtualization Technology (Intel VT-x).

Both features depend on HVM support.

Since, at this point, disabling any of them would cause Xen to not compile, the options are enabled by default if HVM and are not selectable by the user.

No functional change intended.

---
changes in v3:
- tag added
changes in v2:
- simplify kconfig expression to def_bool HVM
- keep file list in Makefile in alphabetical order
changes in v1:
- change kconfig option name AMD_SVM/INTEL_VMX -> SVM/VMX

---
 xen/arch/x86/Kconfig         | 6 ++++++
 xen/arch/x86/hvm/Makefile    | 4 ++--
 xen/arch/x86/mm/Makefile     | 3 ++-
 xen/arch/x86/mm/hap/Makefile | 2 +-
 4 files changed, 11 insertions(+), 4 deletions(-)
xen/arch/x86/Kconfig +6 -0
index 7e03e4bc55..8c9f8431f0 100644
@@ -122,6 +122,12 @@ config HVM
 
 	  If unsure, say Y.
 
+config SVM
+	def_bool HVM
+
+config VMX
+	def_bool HVM
+
 config XEN_SHSTK
 	bool "Supervisor Shadow Stacks"
 	depends on HAS_AS_CET_SS
xen/arch/x86/hvm/Makefile +2 -2
index 3464191544..8434badc64 100644
@@ -1,5 +1,5 @@
-obj-y += svm/
-obj-y += vmx/
+obj-$(CONFIG_SVM) += svm/
+obj-$(CONFIG_VMX) += vmx/
 obj-y += viridian/
 
 obj-y += asid.o
xen/arch/x86/mm/Makefile +2 -1
index 0803ac9297..0128ca7ab6 100644
@@ -10,6 +10,7 @@ obj-$(CONFIG_MEM_SHARING) += mem_sharing.o
 obj-$(CONFIG_HVM) += nested.o
 obj-$(CONFIG_HVM) += p2m.o
 obj-y += p2m-basic.o
-obj-$(CONFIG_HVM) += p2m-ept.o p2m-pod.o p2m-pt.o
+obj-$(CONFIG_VMX) += p2m-ept.o
+obj-$(CONFIG_HVM) += p2m-pod.o p2m-pt.o
 obj-y += paging.o
 obj-y += physmap.o
xen/arch/x86/mm/hap/Makefile +1 -1
index 8ef54b1faa..98c8a87819 100644
@@ -3,4 +3,4 @@ obj-y += guest_walk_2.o
 obj-y += guest_walk_3.o
 obj-y += guest_walk_4.o
 obj-y += nested_hap.o
-obj-y += nested_ept.o
+obj-$(CONFIG_VMX) += nested_ept.o
Signature · 1 line
2.25.1
#2 lore raw

Re: [XEN PATCH v3 01/16] x86: introduce AMD-V and Intel VT-x Kconfig options

Quoted 12 lines · Sergiy Kibrik

From: Xenia Ragiadakou <[email protected]>

Introduce two new Kconfig options, SVM and VMX, to allow code specific to each virtualization technology to be separated and, when not required, stripped.
CONFIG_SVM will be used to enable virtual machine extensions on platforms that implement the AMD Virtualization Technology (AMD-V).
CONFIG_VMX will be used to enable virtual machine extensions on platforms that implement the Intel Virtualization Technology (Intel VT-x).

Both features depend on HVM support.

Quoted 8 lines

Since, at this point, disabling any of them would cause Xen to not compile, the options are enabled by default if HVM and are not selectable by the user.

No functional change intended.

Yet to clarify - my question as to ...

Quoted 2 lines

changes in v1:
- change kconfig option name AMD_SVM/INTEL_VMX -> SVM/VMX

... undoing this still remains.

Jan

#3 lore raw

PATCH v3 2/16 x86/altp2m: check if altp2m active when giving away p2midx

In reply to Sergiy Kibrik

Explicitly check whether altp2m is on for domain when getting altp2m index.

The puspose of that is later to be able to disable altp2m support and exclude its code from the build completely, when not supported by target platform (as of now it's implemented for Intel EPT only).

CC Tamas K Lengyel <[email protected]>
CC Jan Beulich <[email protected]>

---
changes in v3:
- move altp2m_active() check inside altp2m_vcpu_idx()
- drop changes to monitor.c
- changed patch description
changes in v2:
- patch description changed, removed VMX mentioning
- guard by altp2m_active() instead of hvm_altp2m_supported()

---
 xen/arch/x86/include/asm/altp2m.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
xen/arch/x86/include/asm/altp2m.h +5 -5
index e5e59cbd68..2d36c5aa9b 100644
@@ -26,10 +26,6 @@ void altp2m_vcpu_destroy(struct vcpu *v);
 int altp2m_vcpu_enable_ve(struct vcpu *v, gfn_t gfn);
 void altp2m_vcpu_disable_ve(struct vcpu *v);
 
-static inline uint16_t altp2m_vcpu_idx(const struct vcpu *v)
-{
-    return vcpu_altp2m(v).p2midx;
-}
 #else
 
 static inline bool altp2m_active(const struct domain *d)
@@ -38,9 +34,13 @@ static inline bool altp2m_active(const struct domain *d)
 }
 
 /* Only declaration is needed. DCE will optimise it out when linking. */
-uint16_t altp2m_vcpu_idx(const struct vcpu *v);
 void altp2m_vcpu_disable_ve(struct vcpu *v);
 
 #endif
 
+static inline uint16_t altp2m_vcpu_idx(const struct vcpu *v)
+{
+    return altp2m_active(v->domain) ? vcpu_altp2m(v).p2midx : 0;
+}
+
 #endif /* __ASM_X86_ALTP2M_H */
Signature · 1 line
2.25.1
#4 lore raw

Re: [XEN PATCH v3 02/16] x86/altp2m: check if altp2m active when giving away p2midx

xen/arch/x86/include/asm/altp2m.h static inline bool altp2m_active(const struct domain *d) · line 44

Quoted 13 lines · Sergiy Kibrik
xen/arch/x86/include/asm/altp2m.h +4 -1
@@ -38,9 +34,13 @@ static inline bool altp2m_active(const struct domain *d)
 }
 
 /* Only declaration is needed. DCE will optimise it out when linking. */
-uint16_t altp2m_vcpu_idx(const struct vcpu *v);
 void altp2m_vcpu_disable_ve(struct vcpu *v);
 
 #endif
 
+static inline uint16_t altp2m_vcpu_idx(const struct vcpu *v)
+{
+    return altp2m_active(v->domain) ? vcpu_altp2m(v).p2midx : 0;
+}

While perhaps okay this way as a first step, my general expectation would be that with ALTP2M=n there also wouldn't be any p2midx field in the respective struct. Which in turn will mean that this code would need re-doing again, and perhaps again splitting between an inline one and a decl-only one. With that I wonder whether that split wouldn't better be retained right away.

Jan

#5 lore raw

Re: [XEN PATCH v3 02/16] x86/altp2m: check if altp2m active when giving away p2midx

Quoted 8 lines · Jan Beulich
Quoted 7 lines · Sergiy Kibrik
xen/arch/x86/include/asm/altp2m.h +0 -1
@@ -38,9 +34,13 @@ static inline bool altp2m_active(const struct domain *d)
 }
 
 /* Only declaration is needed. DCE will optimise it out when linking. */
-uint16_t altp2m_vcpu_idx(const struct vcpu *v);
 void altp2m_vcpu_disable_ve(struct vcpu *v);
 
Quoted 8 lines

xen/arch/x86/include/asm/altp2m.h static inline bool altp2m_active(const struct domain *d) · line 44

Quoted 6 lines
xen/arch/x86/include/asm/altp2m.h +4 -0
 #endif
 
+static inline uint16_t altp2m_vcpu_idx(const struct vcpu *v)
+{
+    return altp2m_active(v->domain) ? vcpu_altp2m(v).p2midx : 0;
+}

While perhaps okay this way as a first step,

Hmm, or maybe not. 0 is a valid index, and hence could be misleading at call sites.

Jan

Quoted 8 lines

my general expectation
would be that with ALTP2M=n there also wouldn't be any p2midx field in the respective struct. Which in turn will mean that this code would need re-doing again, and perhaps again splitting between an inline one and a decl-only one. With that I wonder whether that split wouldn't better be retained right away.

Jan

#6 lore raw

Re: [XEN PATCH v3 02/16] x86/altp2m: check if altp2m active when giving away p2midx

07.06.24 10:50, Jan Beulich:

Quoted 18 lines · Jan Beulich
Quoted 8 lines · Sergiy Kibrik
Quoted 7 lines
xen/arch/x86/include/asm/altp2m.h +0 -1
@@ -38,9 +34,13 @@ static inline bool altp2m_active(const struct domain *d)
  }
 
  /* Only declaration is needed. DCE will optimise it out when linking. */
-uint16_t altp2m_vcpu_idx(const struct vcpu *v);
  void altp2m_vcpu_disable_ve(struct vcpu *v);
 
Quoted 8 lines

xen/arch/x86/include/asm/altp2m.h static inline bool altp2m_active(const struct domain *d) · line 44

Quoted 6 lines
xen/arch/x86/include/asm/altp2m.h +4 -0
  #endif
 
+static inline uint16_t altp2m_vcpu_idx(const struct vcpu *v)
+{
+    return altp2m_active(v->domain) ? vcpu_altp2m(v).p2midx : 0;
+}

While perhaps okay this way as a first step,

Quoted 2 lines

Hmm, or maybe not. 0 is a valid index, and hence could be misleading at call sites.

I'm returning 0 index here because implementation of p2m_get_mem_access() for x86 & ARM expects it to be 0 when altp2m not active or not implemented.

-Sergiy

#7 lore raw

Re: [XEN PATCH v3 02/16] x86/altp2m: check if altp2m active when giving away p2midx

Quoted 19 lines · Sergiy Kibrik

07.06.24 10:50, Jan Beulich:

Quoted 18 lines · Jan Beulich
Quoted 16 lines · Sergiy Kibrik

xen/arch/x86/include/asm/altp2m.h static inline bool altp2m_active(const struct domain *d) · line 44

Quoted 13 lines
xen/arch/x86/include/asm/altp2m.h +4 -1
@@ -38,9 +34,13 @@ static inline bool altp2m_active(const struct domain *d)
  }
 
  /* Only declaration is needed. DCE will optimise it out when linking. */
-uint16_t altp2m_vcpu_idx(const struct vcpu *v);
  void altp2m_vcpu_disable_ve(struct vcpu *v);
 
  #endif
 
+static inline uint16_t altp2m_vcpu_idx(const struct vcpu *v)
+{
+    return altp2m_active(v->domain) ? vcpu_altp2m(v).p2midx : 0;
+}

While perhaps okay this way as a first step,

Quoted 6 lines
Quoted 2 lines

Hmm, or maybe not. 0 is a valid index, and hence could be misleading at call sites.

I'm returning 0 index here because implementation of p2m_get_mem_access() for x86 & ARM expects it to be 0 when altp2m not active or not implemented.

Tamas, considering the comment in x86'es p2m_get_mem_access(), what purpose are d->arch.altp2m_p2m[0] and d->arch.altp2m_eptp[0] then? In case it indeed is unused, why would p2m_init_altp2m() set up slot 0 in the first place?

Jan

#8 lore raw

PATCH v3 3/16 x86/p2m: guard altp2m routines

In reply to Sergiy Kibrik

Initialize and bring down altp2m only when it is supported by the platform, e.g. VMX. Also guard p2m_altp2m_propagate_change().
The puspose of that is the possiblity to disable altp2m support and exclude its code from the build completely, when it's not supported by the target platform.

---
changes in v3:
- put hvm_altp2m_supported() first
- rewrite changes to p2m_init() with less code
- add tag

---
 xen/arch/x86/mm/p2m-basic.c | 9 +++++----
 xen/arch/x86/mm/p2m-ept.c   | 2 +-
 2 files changed, 6 insertions(+), 5 deletions(-)
xen/arch/x86/mm/p2m-basic.c +5 -4
index 25d27a0a9f..08007a687c 100644
@@ -128,7 +128,7 @@ int p2m_init(struct domain *d)
         return rc;
     }
 
-    rc = p2m_init_altp2m(d);
+    rc = hvm_altp2m_supported() ? p2m_init_altp2m(d) : 0;
     if ( rc )
     {
         p2m_teardown_hostp2m(d);
@@ -197,11 +197,12 @@ void p2m_final_teardown(struct domain *d)
 {
     if ( is_hvm_domain(d) )
     {
+        if ( hvm_altp2m_supported() )
+            p2m_teardown_altp2m(d);
         /*
-         * We must tear down both of them unconditionally because
-         * we initialise them unconditionally.
+         * We must tear down nestedp2m unconditionally because
+         * we initialise it unconditionally.
          */
-        p2m_teardown_altp2m(d);
         p2m_teardown_nestedp2m(d);
     }
 
xen/arch/x86/mm/p2m-ept.c +1 -1
index f83610cb8c..c261ba02db 100644
@@ -986,7 +986,7 @@ out:
     if ( is_epte_present(&old_entry) )
         ept_free_entry(p2m, &old_entry, target);
 
-    if ( entry_written && p2m_is_hostp2m(p2m) )
+    if ( hvm_altp2m_supported() && entry_written && p2m_is_hostp2m(p2m) )
     {
         ret = p2m_altp2m_propagate_change(d, _gfn(gfn), mfn, order, p2mt, p2ma);
         if ( !rc )
Signature · 1 line
2.25.1
Acked-by on v3 3/16
#9 lore raw

Re: [XEN PATCH v3 03/16] x86/p2m: guard altp2m routines

Quoted 7 lines · Sergiy Kibrik

Initialize and bring down altp2m only when it is supported by the platform, e.g. VMX. Also guard p2m_altp2m_propagate_change().
The puspose of that is the possiblity to disable altp2m support and exclude its code from the build completely, when it's not supported by the target platform.

I question though whether Stefano's R-b was valid to retain with ...

Quoted 4 lines

---
changes in v3:
- put hvm_altp2m_supported() first
- rewrite changes to p2m_init() with less code

... this.

Jan

#10 lore raw

Re: [XEN PATCH v3 03/16] x86/p2m: guard altp2m routines

hi Stefano,

07.06.24 10:32, Jan Beulich:

Quoted 11 lines · Sergiy Kibrik
Quoted 7 lines

Initialize and bring down altp2m only when it is supported by the platform, e.g. VMX. Also guard p2m_altp2m_propagate_change().
The puspose of that is the possiblity to disable altp2m support and exclude its code from the build completely, when it's not supported by the target platform.

Quoted 8 lines

I question though whether Stefano's R-b was valid to retain with ...

Quoted 4 lines

---
changes in v3:
- put hvm_altp2m_supported() first
- rewrite changes to p2m_init() with less code

... this.

could you please take a look at this patch once more, after it's been through these modifications? Thank you!

-Sergiy

#11 lore raw

PATCH v3 4/16 x86: introduce CONFIG_ALTP2M Kconfig option

In reply to Sergiy Kibrik

Add new option to make altp2m code inclusion optional.
Currently altp2m implemented for Intel EPT only, so option is dependant on VMX. Also the prompt itself depends on EXPERT=y, so that option is available for fine-tuning, if one want to play around with it.

Use this option instead of more generic CONFIG_HVM option.
That implies the possibility to build hvm code without altp2m support, hence we need to declare altp2m routines for hvm code to compile successfully (altp2m_vcpu_initialise(), altp2m_vcpu_destroy(), altp2m_vcpu_enable_ve())

Also guard altp2m routines, so that they can be disabled completely in the build -- when target platform does not actually support altp2m (AMD-V & ARM as of now).

CC Tamas K Lengyel <[email protected]>
CC Jan Beulich <[email protected]>
CC Stefano Stabellini <[email protected]>

---
changes in v3:
- added help text
- use conditional prompt depending on EXPERT=y
- corrected & extended patch description
- put a blank line before #ifdef CONFIG_ALTP2M
- sqashed in a separate patch for guarding altp2m code with CONFIG_ALTP2M option
changes in v2:
- use separate CONFIG_ALTP2M option instead of CONFIG_VMX

---
 xen/arch/x86/Kconfig               | 11 +++++++++++
 xen/arch/x86/include/asm/altp2m.h  |  5 ++++-
 xen/arch/x86/include/asm/hvm/hvm.h |  2 +-
 xen/arch/x86/include/asm/p2m.h     | 17 ++++++++++++++++-
 xen/arch/x86/mm/Makefile           |  2 +-
 5 files changed, 33 insertions(+), 4 deletions(-)
xen/arch/x86/Kconfig +11 -0
index 8c9f8431f0..4a35c43dc5 100644
@@ -358,6 +358,17 @@ config REQUIRE_NX
 	  was unavailable. However, if enabled, Xen will no longer boot on
 	  any CPU which is lacking NX support.
 
+config ALTP2M
+	bool "Alternate P2M support" if EXPERT
+	default y
+	depends on VMX
+	help
+	  Alternate-p2m allows a guest to manage multiple p2m guest physical
+	  "memory views" (as opposed to a single p2m).
+	  Useful for memory introspection.
+
+	  If unsure, stay with defaults.
+
 endmenu
 
 source "common/Kconfig"
xen/arch/x86/include/asm/altp2m.h +4 -1
index 2d36c5aa9b..effbef51eb 100644
@@ -7,7 +7,7 @@
 #ifndef __ASM_X86_ALTP2M_H
 #define __ASM_X86_ALTP2M_H
 
-#ifdef CONFIG_HVM
+#ifdef CONFIG_ALTP2M
 
 #include <xen/types.h>
 #include <xen/sched.h>         /* for struct vcpu, struct domain */
@@ -34,6 +34,9 @@ static inline bool altp2m_active(const struct domain *d)
 }
 
 /* Only declaration is needed. DCE will optimise it out when linking. */
+void altp2m_vcpu_initialise(struct vcpu *v);
+void altp2m_vcpu_destroy(struct vcpu *v);
+int altp2m_vcpu_enable_ve(struct vcpu *v, gfn_t gfn);
 void altp2m_vcpu_disable_ve(struct vcpu *v);
 
 #endif
xen/arch/x86/include/asm/hvm/hvm.h +1 -1
index 1c01e22c8e..2ebea1a92c 100644
@@ -670,7 +670,7 @@ static inline bool hvm_hap_supported(void)
 /* returns true if hardware supports alternate p2m's */
 static inline bool hvm_altp2m_supported(void)
 {
-    return hvm_funcs.caps.altp2m;
+    return IS_ENABLED(CONFIG_ALTP2M) && hvm_funcs.caps.altp2m;
 }
 
 /* Returns true if we have the minimum hardware requirements for nested virt */
xen/arch/x86/include/asm/p2m.h +16 -1
index c1478ffc36..b247aa4c7d 100644
@@ -577,10 +577,10 @@ static inline gfn_t mfn_to_gfn(const struct domain *d, mfn_t mfn)
         return _gfn(mfn_x(mfn));
 }
 
-#ifdef CONFIG_HVM
 #define AP2MGET_prepopulate true
 #define AP2MGET_query false
 
+#ifdef CONFIG_ALTP2M
 /*
  * Looks up altp2m entry. If the entry is not found it looks up the entry in
  * hostp2m.
@@ -589,6 +589,15 @@ static inline gfn_t mfn_to_gfn(const struct domain *d, mfn_t mfn)
 int altp2m_get_effective_entry(struct p2m_domain *ap2m, gfn_t gfn, mfn_t *mfn,
                                p2m_type_t *t, p2m_access_t *a,
                                bool prepopulate);
+#else
+static inline int altp2m_get_effective_entry(struct p2m_domain *ap2m,
+                                             gfn_t gfn, mfn_t *mfn,
+                                             p2m_type_t *t, p2m_access_t *a,
+                                             bool prepopulate)
+{
+    ASSERT_UNREACHABLE();
+    return -EOPNOTSUPP;
+}
 #endif
 
 /* Init the datastructures for later use by the p2m code */
@@ -914,8 +923,14 @@ static inline bool p2m_set_altp2m(struct vcpu *v, unsigned int idx)
 /* Switch alternate p2m for a single vcpu */
 bool p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, unsigned int idx);
 
+#ifdef CONFIG_ALTP2M
 /* Check to see if vcpu should be switched to a different p2m. */
 void p2m_altp2m_check(struct vcpu *v, uint16_t idx);
+#else
+static inline void p2m_altp2m_check(struct vcpu *v, uint16_t idx)
+{
+}
+#endif
 
 /* Flush all the alternate p2m's for a domain */
 void p2m_flush_altp2m(struct domain *d);
xen/arch/x86/mm/Makefile +1 -1
index 0128ca7ab6..d7d57b8190 100644
@@ -1,7 +1,7 @@
 obj-y += shadow/
 obj-$(CONFIG_HVM) += hap/
 
-obj-$(CONFIG_HVM) += altp2m.o
+obj-$(CONFIG_ALTP2M) += altp2m.o
 obj-$(CONFIG_HVM) += guest_walk_2.o guest_walk_3.o guest_walk_4.o
 obj-$(CONFIG_SHADOW_PAGING) += guest_walk_4.o
 obj-$(CONFIG_MEM_ACCESS) += mem_access.o
Signature · 1 line
2.25.1
#12 lore raw

Re: [XEN PATCH v3 04/16] x86: introduce CONFIG_ALTP2M Kconfig option

Quoted 4 lines · Sergiy Kibrik
xen/arch/x86/include/asm/p2m.h +0 -0
@@ -577,10 +577,10 @@ static inline gfn_t mfn_to_gfn(const struct domain *d, mfn_t mfn)
         return _gfn(mfn_x(mfn));

xen/arch/x86/include/asm/p2m.h static inline gfn_t mfn_to_gfn(const struct domain *d, mfn_t mfn) · line 586

Quoted 10 lines
xen/arch/x86/include/asm/p2m.h +1 -1
 }
 
-#ifdef CONFIG_HVM
 #define AP2MGET_prepopulate true
 #define AP2MGET_query false
 
+#ifdef CONFIG_ALTP2M
 /*
  * Looks up altp2m entry. If the entry is not found it looks up the entry in
  * hostp2m.

In principle this #ifdef shouldn't need moving. It's just that the three use sites need taking care of a little differently. E.g. ...

xen/arch/x86/include/asm/p2m.h static inline gfn_t mfn_to_gfn(const struct domain *d, mfn_t mfn) · line 600

Quoted 13 lines
xen/arch/x86/include/asm/p2m.h +9 -0
@@ -589,6 +589,15 @@ static inline gfn_t mfn_to_gfn(const struct domain *d, mfn_t mfn)
 int altp2m_get_effective_entry(struct p2m_domain *ap2m, gfn_t gfn, mfn_t *mfn,
                                p2m_type_t *t, p2m_access_t *a,
                                bool prepopulate);
+#else
+static inline int altp2m_get_effective_entry(struct p2m_domain *ap2m,
+                                             gfn_t gfn, mfn_t *mfn,
+                                             p2m_type_t *t, p2m_access_t *a,
+                                             bool prepopulate)
+{
+    ASSERT_UNREACHABLE();
+    return -EOPNOTSUPP;
+}

static inline int altp2m_get_effective_entry(struct p2m_domain *ap2m,
gfn_t gfn, mfn_t *mfn, p2m_type_t *t, p2m_access_t *a)

{
    ASSERT_UNREACHABLE();
    return -EOPNOTSUPP;
}

#define altp2m_get_effective_entry(ap2m, gfn, mfn, t, a, prepopulate) \
altp2m_get_effective_entry(ap2m, gfn, mfn, t, a)

Misra doesn't like such shadowing, so the inline function may want naming slightly differently, e.g. _ap2m_get_effective_entry().

xen/arch/x86/include/asm/p2m.h static inline bool p2m_set_altp2m(struct vcpu *v, unsigned int idx) · line 932

Quoted 11 lines
xen/arch/x86/include/asm/p2m.h +5 -0
@@ -914,8 +923,14 @@ static inline bool p2m_set_altp2m(struct vcpu *v, unsigned int idx)
 /* Switch alternate p2m for a single vcpu */
 bool p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, unsigned int idx);
 
+#ifdef CONFIG_ALTP2M
 /* Check to see if vcpu should be switched to a different p2m. */
 void p2m_altp2m_check(struct vcpu *v, uint16_t idx);
+#else
+static inline void p2m_altp2m_check(struct vcpu *v, uint16_t idx)
+{
+}

Btw, no need to put the braces on separate lines for such a stub.

Jan

#13 lore raw

Re: [XEN PATCH v3 04/16] x86: introduce CONFIG_ALTP2M Kconfig option

07.06.24 10:47, Jan Beulich:

Quoted 40 lines · Sergiy Kibrik
Quoted 4 lines
xen/arch/x86/include/asm/p2m.h +0 -0
@@ -577,10 +577,10 @@ static inline gfn_t mfn_to_gfn(const struct domain *d, mfn_t mfn)
          return _gfn(mfn_x(mfn));

xen/arch/x86/include/asm/p2m.h static inline gfn_t mfn_to_gfn(const struct domain *d, mfn_t mfn) · line 586

Quoted 10 lines
xen/arch/x86/include/asm/p2m.h +1 -1
  }
 
-#ifdef CONFIG_HVM
  #define AP2MGET_prepopulate true
  #define AP2MGET_query false
 
+#ifdef CONFIG_ALTP2M
  /*
   * Looks up altp2m entry. If the entry is not found it looks up the entry in
   * hostp2m.

In principle this #ifdef shouldn't need moving. It's just that the three use sites need taking care of a little differently. E.g. ...

xen/arch/x86/include/asm/p2m.h static inline gfn_t mfn_to_gfn(const struct domain *d, mfn_t mfn) · line 600

Quoted 13 lines
xen/arch/x86/include/asm/p2m.h +9 -0
@@ -589,6 +589,15 @@ static inline gfn_t mfn_to_gfn(const struct domain *d, mfn_t mfn)
  int altp2m_get_effective_entry(struct p2m_domain *ap2m, gfn_t gfn, mfn_t *mfn,
                                 p2m_type_t *t, p2m_access_t *a,
                                 bool prepopulate);
+#else
+static inline int altp2m_get_effective_entry(struct p2m_domain *ap2m,
+                                             gfn_t gfn, mfn_t *mfn,
+                                             p2m_type_t *t, p2m_access_t *a,
+                                             bool prepopulate)
+{
+    ASSERT_UNREACHABLE();
+    return -EOPNOTSUPP;
+}

static inline int altp2m_get_effective_entry(struct p2m_domain *ap2m,
gfn_t gfn, mfn_t *mfn, p2m_type_t *t, p2m_access_t *a)

{
     ASSERT_UNREACHABLE();
     return -EOPNOTSUPP;
}
Quoted 5 lines

#define altp2m_get_effective_entry(ap2m, gfn, mfn, t, a, prepopulate) \
altp2m_get_effective_entry(ap2m, gfn, mfn, t, a)

Misra doesn't like such shadowing, so the inline function may want naming slightly differently, e.g. _ap2m_get_effective_entry().

I can do that, sure.
Though here I'm curious what benefits we're getting from little complication of an indirect call to an empty stub -- is avoiding of AP2MGET_* defines worth it?

-Sergiy

#14 lore raw

Re: [XEN PATCH v3 04/16] x86: introduce CONFIG_ALTP2M Kconfig option

Quoted 44 lines · Sergiy Kibrik

07.06.24 10:47, Jan Beulich:

Quoted 43 lines · Sergiy Kibrik
Quoted 4 lines
xen/arch/x86/include/asm/p2m.h +0 -0
@@ -577,10 +577,10 @@ static inline gfn_t mfn_to_gfn(const struct domain *d, mfn_t mfn)
          return _gfn(mfn_x(mfn));

xen/arch/x86/include/asm/p2m.h static inline gfn_t mfn_to_gfn(const struct domain *d, mfn_t mfn) · line 586

Quoted 10 lines
xen/arch/x86/include/asm/p2m.h +1 -1
  }
 
-#ifdef CONFIG_HVM
  #define AP2MGET_prepopulate true
  #define AP2MGET_query false
 
+#ifdef CONFIG_ALTP2M
  /*
   * Looks up altp2m entry. If the entry is not found it looks up the entry in
   * hostp2m.

In principle this #ifdef shouldn't need moving. It's just that the three use sites need taking care of a little differently. E.g. ...

xen/arch/x86/include/asm/p2m.h static inline gfn_t mfn_to_gfn(const struct domain *d, mfn_t mfn) · line 600

Quoted 13 lines
xen/arch/x86/include/asm/p2m.h +9 -0
@@ -589,6 +589,15 @@ static inline gfn_t mfn_to_gfn(const struct domain *d, mfn_t mfn)
  int altp2m_get_effective_entry(struct p2m_domain *ap2m, gfn_t gfn, mfn_t *mfn,
                                 p2m_type_t *t, p2m_access_t *a,
                                 bool prepopulate);
+#else
+static inline int altp2m_get_effective_entry(struct p2m_domain *ap2m,
+                                             gfn_t gfn, mfn_t *mfn,
+                                             p2m_type_t *t, p2m_access_t *a,
+                                             bool prepopulate)
+{
+    ASSERT_UNREACHABLE();
+    return -EOPNOTSUPP;
+}

static inline int altp2m_get_effective_entry(struct p2m_domain *ap2m,
gfn_t gfn, mfn_t *mfn, p2m_type_t *t, p2m_access_t *a)

{
     ASSERT_UNREACHABLE();
     return -EOPNOTSUPP;
}

#define altp2m_get_effective_entry(ap2m, gfn, mfn, t, a, prepopulate) \
altp2m_get_effective_entry(ap2m, gfn, mfn, t, a)

Quoted 8 lines
Quoted 2 lines

Misra doesn't like such shadowing, so the inline function may want naming slightly differently, e.g. _ap2m_get_effective_entry().

I can do that, sure.
Though here I'm curious what benefits we're getting from little complication of an indirect call to an empty stub -- is avoiding of AP2MGET_* defines worth it?

First - how is an indirect call coming into the picture here? We aim at avoiding indirect calls where possible, after all. Yet iirc all calls to altp2m_get_effective_entry() are direct ones.

As to avoiding the (or in fact any such) #define-s - imo it's better to not have items in the name space which can't validly be used, unless this (elsewhere) helps to keep the code tidy overall. This way problems (e.g. from misunderstandings or flaws elsewhere) can be detected early.

Jan

#15 lore raw

PATCH v3 5/16 x86: introduce using_{svm,vmx} macros

In reply to Sergiy Kibrik

As we now have SVM/VMX config options for enabling/disabling these features completely in the build, we need some build-time checks to ensure that vmx/svm code can be used and things compile. Macros cpu_has_{svm,vmx} used to be doing such checks at runtime, however they do not check if SVM/VMX support is enabled in the build.

Also cpu_has_{svm,vmx} can potentially be called from non-{VMX,SVM} build yet running on {VMX,SVM}-enabled CPU, so would correctly indicate that VMX/SVM is indeed supported by CPU, but code to drive it can't be used.

New macros using_{vmx,svm} indicates that both CPU _and_ build provide corresponding technology support, while cpu_has_{vmx,svm} still remains for informational runtime purpose, just as their naming suggests.

---
Here I've followed Jan's suggestion on not touching cpu_has_{vmx,svm} but adding separate macros for solving build problems, and then using these where required.
---
changes in v3:
- introduce separate macros instead of modifying behaviour of cpu_has_{vmx,svm}

---
 xen/arch/x86/include/asm/hvm/hvm.h | 3 +++
 1 file changed, 3 insertions(+)
xen/arch/x86/include/asm/hvm/hvm.h +3 -0
index 2ebea1a92c..778b93df5c 100644
@@ -26,6 +26,9 @@ extern bool opt_hvm_fep;
 #define opt_hvm_fep 0
 #endif
 
+#define using_vmx ( IS_ENABLED(CONFIG_VMX) && cpu_has_vmx )
+#define using_svm ( IS_ENABLED(CONFIG_SVM) && cpu_has_svm )
+
 /* Interrupt acknowledgement sources. */
 enum hvm_intsrc {
     hvm_intsrc_none,
Signature · 1 line
2.25.1
#16 lore raw

Re: [XEN PATCH v3 05/16] x86: introduce using_{svm,vmx} macros

Quoted 14 lines · Sergiy Kibrik

As we now have SVM/VMX config options for enabling/disabling these features completely in the build, we need some build-time checks to ensure that vmx/svm code can be used and things compile. Macros cpu_has_{svm,vmx} used to be doing such checks at runtime, however they do not check if SVM/VMX support is enabled in the build.

Also cpu_has_{svm,vmx} can potentially be called from non-{VMX,SVM} build yet running on {VMX,SVM}-enabled CPU, so would correctly indicate that VMX/SVM is indeed supported by CPU, but code to drive it can't be used.

New macros using_{vmx,svm} indicates that both CPU _and_ build provide corresponding technology support, while cpu_has_{vmx,svm} still remains for informational runtime purpose, just as their naming suggests.

Quoted 6 lines

---
Here I've followed Jan's suggestion on not touching cpu_has_{vmx,svm} but adding separate macros for solving build problems, and then using these where required.

As an isolated change this then may want expressing via Suggested-by:. However, I question whether these wouldn't better be introduced together with their (first) uses (and then perhaps no such tag).

xen/arch/x86/include/asm/hvm/hvm.h extern bool opt_hvm_fep; · line 30

Quoted 8 lines
xen/arch/x86/include/asm/hvm/hvm.h +2 -0
@@ -26,6 +26,9 @@ extern bool opt_hvm_fep;
 #define opt_hvm_fep 0
 #endif
 
+#define using_vmx ( IS_ENABLED(CONFIG_VMX) && cpu_has_vmx )
+#define using_svm ( IS_ENABLED(CONFIG_SVM) && cpu_has_svm )

Nit: Stray blanks immediately next to the parentheses.

Jan

#17 lore raw

Re: [XEN PATCH v3 05/16] x86: introduce using_{svm,vmx} macros

In reply to Sergiy Kibrik

xen/arch/x86/include/asm/hvm/hvm.h extern bool opt_hvm_fep; · line 30

Quoted 8 lines · Sergiy Kibrik
xen/arch/x86/include/asm/hvm/hvm.h +2 -0
@@ -26,6 +26,9 @@ extern bool opt_hvm_fep;
 #define opt_hvm_fep 0
 #endif
 
+#define using_vmx ( IS_ENABLED(CONFIG_VMX) && cpu_has_vmx )
+#define using_svm ( IS_ENABLED(CONFIG_SVM) && cpu_has_svm )

Btw, having seen a number of uses by now: Can we consider making these at least function-like macros (with no arguments), if not inline functions? Imo that'll make use sites look more "natural".

Jan

#18 lore raw

PATCH v3 6/16 x86/nestedhvm: switch to using_{svm,vmx} check

In reply to Sergiy Kibrik

Use using_{svm,vmx} instead of cpu_has_{svm,vmx} that not only checks if CPU supports corresponding virtialization technology, but also if it is supported by build configuration.

This fixes build when VMX=n or SVM=n, because then start_nested_{svm,vmx} routine(s) not available.

---
 xen/arch/x86/hvm/nestedhvm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/nestedhvm.c +2 -2
index 451c4da6d4..008dddf801 100644
@@ -155,9 +155,9 @@ static int __init cf_check nestedhvm_setup(void)
      * done, so that if (for example) HAP is disabled, nested virt is
      * disabled as well.
      */
-    if ( cpu_has_vmx )
+    if ( using_vmx )
         start_nested_vmx(&hvm_funcs);
-    else if ( cpu_has_svm )
+    else if ( using_svm )
         start_nested_svm(&hvm_funcs);
 
     return 0;
Signature · 1 line
2.25.1
Acked-by on v3 6/16
#19 lore raw

Re: [XEN PATCH v3 06/16] x86/nestedhvm: switch to using_{svm,vmx} check

Quoted 8 lines · Sergiy Kibrik

Use using_{svm,vmx} instead of cpu_has_{svm,vmx} that not only checks if CPU supports corresponding virtialization technology, but also if it is supported by build configuration.

This fixes build when VMX=n or SVM=n, because then start_nested_{svm,vmx} routine(s) not available.

preferably, as said there, with the introduction of those macros moved here, and title/description adjusted accordingly.

Jan

#20 lore raw

PATCH v3 7/16 x86/hvm: guard AMD-V and Intel VT-x hvm_function_table initializers

In reply to Sergiy Kibrik

From: Xenia Ragiadakou <[email protected]>

Since start_svm() is AMD-V specific while start_vmx() is Intel VT-x specific, any of them can potentially be excluded from build completely with CONFIG_SVM or CONFIG_VMX options respectively, hence we have to explicitly check if they're available and use specific macros using_{svm,vmx} for that.

---
changes in v3:
- use using_{svm,vmx} macro instead of IS_ENABLED(CONFIG_{SVM,VMX})
- updated description

---
 xen/arch/x86/hvm/hvm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/hvm.c +2 -2
index 8334ab1711..7b8679bcd8 100644
@@ -155,9 +155,9 @@ static int __init cf_check hvm_enable(void)
 {
     const struct hvm_function_table *fns = NULL;
 
-    if ( cpu_has_vmx )
+    if ( using_vmx )
         fns = start_vmx();
-    else if ( cpu_has_svm )
+    else if ( using_svm )
         fns = start_svm();
 
     if ( fns == NULL )
Signature · 1 line
2.25.1
Acked-by on v3 7/16
#21 lore raw

Re: [XEN PATCH v3 07/16] x86/hvm: guard AMD-V and Intel VT-x hvm_function_table initializers

Quoted 9 lines · Sergiy Kibrik

From: Xenia Ragiadakou <[email protected]>

Since start_svm() is AMD-V specific while start_vmx() is Intel VT-x specific, any of them can potentially be excluded from build completely with CONFIG_SVM or CONFIG_VMX options respectively, hence we have to explicitly check if they're available and use specific macros using_{svm,vmx} for that.

yet again I think this could sensibly be folded with the earlier two patches.

Jan

#22 lore raw

Re: [XEN PATCH v3 07/16] x86/hvm: guard AMD-V and Intel VT-x hvm_function_table initializers

07.06.24 11:01, Jan Beulich:

Quoted 8 lines · Sergiy Kibrik
Quoted 7 lines

From: Xenia Ragiadakou <[email protected]>

Since start_svm() is AMD-V specific while start_vmx() is Intel VT-x specific, any of them can potentially be excluded from build completely with CONFIG_SVM or CONFIG_VMX options respectively, hence we have to explicitly check if they're available and use specific macros using_{svm,vmx} for that.

Quoted 7 lines
Quoted 2 lines

yet again I think this could sensibly be folded with the earlier two patches.

sure, I'll squash together patches 5,6,7

-Sergiy

#23 lore raw

PATCH v3 8/16 x86/p2m: guard EPT functions with using_vmx macro

In reply to Sergiy Kibrik

From: Xenia Ragiadakou <[email protected]>

Replace cpu_has_vmx check with using_vmx, so that presence of functions ept_p2m_init() and ept_p2m_uninit() in the build gets checked. Since currently Intel EPT implementation depends on CONFIG_VMX config option, when VMX is off these functions are unavailable.

---
changes in v3:
- using_vmx instead of IS_ENABLED(CONFIG_VMX)
- updated description
-

---
 xen/arch/x86/mm/p2m-basic.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/mm/p2m-basic.c +2 -2
index 08007a687c..442284fb40 100644
@@ -40,7 +40,7 @@ static int p2m_initialise(struct domain *d, struct p2m_domain *p2m)
     p2m_pod_init(p2m);
     p2m_nestedp2m_init(p2m);
 
-    if ( hap_enabled(d) && cpu_has_vmx )
+    if ( hap_enabled(d) && using_vmx )
         ret = ept_p2m_init(p2m);
     else
         p2m_pt_init(p2m);
@@ -72,7 +72,7 @@ struct p2m_domain *p2m_init_one(struct domain *d)
 void p2m_free_one(struct p2m_domain *p2m)
 {
     p2m_free_logdirty(p2m);
-    if ( hap_enabled(p2m->domain) && cpu_has_vmx )
+    if ( hap_enabled(p2m->domain) && using_vmx )
         ept_p2m_uninit(p2m);
     free_cpumask_var(p2m->dirty_cpumask);
     xfree(p2m);
Signature · 1 line
2.25.1
Acked-by on v3 8/16
#24 lore raw

Re: [XEN PATCH v3 08/16] x86/p2m: guard EPT functions with using_vmx macro

Quoted 9 lines · Sergiy Kibrik

From: Xenia Ragiadakou <[email protected]>

Replace cpu_has_vmx check with using_vmx, so that presence of functions ept_p2m_init() and ept_p2m_uninit() in the build gets checked. Since currently Intel EPT implementation depends on CONFIG_VMX config option, when VMX is off these functions are unavailable.

The actual code change is okay, so

I don't, however, understand what "gets checked" in the description is meant to mean. Elsewhere the wording was more towards the actual goal, I think.

Jan

Quoted 20 lines
xen/arch/x86/mm/p2m-basic.c +2 -2
@@ -40,7 +40,7 @@ static int p2m_initialise(struct domain *d, struct p2m_domain *p2m)
     p2m_pod_init(p2m);
     p2m_nestedp2m_init(p2m);
 
-    if ( hap_enabled(d) && cpu_has_vmx )
+    if ( hap_enabled(d) && using_vmx )
         ret = ept_p2m_init(p2m);
     else
         p2m_pt_init(p2m);
@@ -72,7 +72,7 @@ struct p2m_domain *p2m_init_one(struct domain *d)
 void p2m_free_one(struct p2m_domain *p2m)
 {
     p2m_free_logdirty(p2m);
-    if ( hap_enabled(p2m->domain) && cpu_has_vmx )
+    if ( hap_enabled(p2m->domain) && using_vmx )
         ept_p2m_uninit(p2m);
     free_cpumask_var(p2m->dirty_cpumask);
     xfree(p2m);
#25 lore raw

PATCH v3 9/16 x86/traps: guard vmx specific functions with usinc_vmx macro

In reply to Sergiy Kibrik

From: Xenia Ragiadakou <[email protected]>

Replace cpu_has_vmx check with using_vmx, so that not only VMX support in CPU gets checked, but also presence of functions vmx_vmcs_enter() & vmx_vmcs_exit() in the build checked as well.

Also since CONFIG_VMX is checked in using_vmx and it depends on CONFIG_HVM, we can drop #ifdef CONFIG_HVM lines around using_vmx.

---
changes in v3:
- using_vmx instead of IS_ENABLED(CONFIG_VMX)
- updated description

---
 xen/arch/x86/traps.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
xen/arch/x86/traps.c +2 -6
index 9906e874d5..a81f3cf57c 100644
@@ -676,7 +676,6 @@ void vcpu_show_execution_state(struct vcpu *v)
 
     vcpu_pause(v); /* acceptably dangerous */
 
-#ifdef CONFIG_HVM
     /*
      * For VMX special care is needed: Reading some of the register state will
      * require VMCS accesses. Engaging foreign VMCSes involves acquiring of a
@@ -684,12 +683,11 @@ void vcpu_show_execution_state(struct vcpu *v)
      * region. Despite this being a layering violation, engage the VMCS right
      * here. This then also avoids doing so several times in close succession.
      */
-    if ( cpu_has_vmx && is_hvm_vcpu(v) )
+    if ( using_vmx && is_hvm_vcpu(v) )
     {
         ASSERT(!in_irq());
         vmx_vmcs_enter(v);
     }
-#endif
 
     /* Prevent interleaving of output. */
     flags = console_lock_recursive_irqsave();
@@ -714,10 +712,8 @@ void vcpu_show_execution_state(struct vcpu *v)
         console_unlock_recursive_irqrestore(flags);
     }
 
-#ifdef CONFIG_HVM
-    if ( cpu_has_vmx && is_hvm_vcpu(v) )
+    if ( using_vmx && is_hvm_vcpu(v) )
         vmx_vmcs_exit(v);
-#endif
 
     vcpu_unpause(v);
 }
Signature · 1 line
2.25.1
Acked-by on v3 9/16
#26 lore raw

Re: [XEN PATCH v3 09/16] x86/traps: guard vmx specific functions with usinc_vmx macro

Quoted 11 lines · Sergiy Kibrik

From: Xenia Ragiadakou <[email protected]>

Replace cpu_has_vmx check with using_vmx, so that not only VMX support in CPU gets checked, but also presence of functions vmx_vmcs_enter() & vmx_vmcs_exit() in the build checked as well.

Also since CONFIG_VMX is checked in using_vmx and it depends on CONFIG_HVM, we can drop #ifdef CONFIG_HVM lines around using_vmx.

#27 lore raw

PATCH v3 10/16 x86/domain: guard svm specific functions with usinc_svm macro

In reply to Sergiy Kibrik

From: Xenia Ragiadakou <[email protected]>

Replace cpu_has_svm check with using_svm, so that not only SVM support in CPU gets checked, but also presence of functions svm_load_segs() and svm_load_segs_prefetch() in the build checked as well.

Since SVM depends on HVM, it can be used alone.

---
changes in v3:
- using_svm instead of IS_ENABLED(CONFIG_SVM)
- updated description

---
 xen/arch/x86/domain.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
xen/arch/x86/domain.c +3 -5
index 536542841e..a2f19f8b46 100644
@@ -1731,11 +1731,9 @@ static void load_segments(struct vcpu *n)
         if ( !(n->arch.flags & TF_kernel_mode) )
             SWAP(gsb, gss);
 
-#ifdef CONFIG_HVM
-        if ( cpu_has_svm && (uregs->fs | uregs->gs) <= 3 )
+        if ( using_svm && (uregs->fs | uregs->gs) <= 3 )
             fs_gs_done = svm_load_segs(n->arch.pv.ldt_ents, LDT_VIRT_START(n),
                                        n->arch.pv.fs_base, gsb, gss);
-#endif
     }
 
     if ( !fs_gs_done )
@@ -2048,9 +2046,9 @@ static void __context_switch(void)
 
     write_ptbase(n);
 
-#if defined(CONFIG_PV) && defined(CONFIG_HVM)
+#if defined(CONFIG_PV)
     /* Prefetch the VMCB if we expect to use it later in the context switch */
-    if ( cpu_has_svm && is_pv_64bit_domain(nd) && !is_idle_domain(nd) )
+    if ( using_svm && is_pv_64bit_domain(nd) && !is_idle_domain(nd) )
         svm_load_segs_prefetch();
 #endif
 
Signature · 1 line
2.25.1
Acked-by on v3 10/16
#28 lore raw

Re: [XEN PATCH v3 10/16] x86/domain: guard svm specific functions with usinc_svm macro

Quoted 10 lines · Sergiy Kibrik

From: Xenia Ragiadakou <[email protected]>

Replace cpu_has_svm check with using_svm, so that not only SVM support in CPU gets checked, but also presence of functions svm_load_segs() and svm_load_segs_prefetch() in the build checked as well.

Since SVM depends on HVM, it can be used alone.

The code you're touching is solely for PV, even if it's interacting with HVM code. Therefore "x86/PV:" may be the better subject prefix.

Quoted 16 lines
xen/arch/x86/domain.c +1 -3
@@ -1731,11 +1731,9 @@ static void load_segments(struct vcpu *n)
         if ( !(n->arch.flags & TF_kernel_mode) )
             SWAP(gsb, gss);
 
-#ifdef CONFIG_HVM
-        if ( cpu_has_svm && (uregs->fs | uregs->gs) <= 3 )
+        if ( using_svm && (uregs->fs | uregs->gs) <= 3 )
             fs_gs_done = svm_load_segs(n->arch.pv.ldt_ents, LDT_VIRT_START(n),
                                        n->arch.pv.fs_base, gsb, gss);
-#endif
     }
 
     if ( !fs_gs_done )

xen/arch/x86/domain.c static void __context_switch(void) · line 2049

Quoted 5 lines
xen/arch/x86/domain.c +1 -1
@@ -2048,9 +2046,9 @@ static void __context_switch(void)
 
     write_ptbase(n);
 
-#if defined(CONFIG_PV) && defined(CONFIG_HVM)
+#if defined(CONFIG_PV)

In such a case, would you mind switching (back) to the shorter "#ifdef" form? Then

Jan

Quoted 6 lines
xen/arch/x86/domain.c +1 -1
     /* Prefetch the VMCB if we expect to use it later in the context switch */
-    if ( cpu_has_svm && is_pv_64bit_domain(nd) && !is_idle_domain(nd) )
+    if ( using_svm && is_pv_64bit_domain(nd) && !is_idle_domain(nd) )
         svm_load_segs_prefetch();
 #endif
 
#29 lore raw

PATCH v3 11/16 x86/oprofile: guard svm specific symbols with CONFIG_SVM

In reply to Sergiy Kibrik

From: Xenia Ragiadakou <[email protected]>

The symbol svm_stgi_label is AMD-V specific so guard its usage in common code with CONFIG_SVM.

Since SVM depends on HVM, it can be used alone. Also, use #ifdef instead of #if.

No functional change intended.

---
 xen/arch/x86/oprofile/op_model_athlon.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/oprofile/op_model_athlon.c +1 -1
index 69fd3fcc86..a9c7b87d67 100644
@@ -320,7 +320,7 @@ static int cf_check athlon_check_ctrs(
 	struct vcpu *v = current;
 	unsigned int const nr_ctrs = model->num_counters;
 
-#if CONFIG_HVM
+#ifdef CONFIG_SVM
 	struct cpu_user_regs *guest_regs = guest_cpu_user_regs();
 
 	if (!guest_mode(regs) &&
Signature · 1 line
2.25.1
#30 lore raw

PATCH v3 12/16 x86/vmx: guard access to cpu_has_vmx_* in common code

In reply to Sergiy Kibrik

There're several places in common code, outside of arch/x86/hvm/vmx, where cpu_has_vmx_* get accessed without checking whether VMX supported first. These macros rely on global variables defined in vmx code, so when VMX support gets disabled accesses to these variables turn into build failures.

To overcome these failures, build-time check is done before accessing global variables, so that DCE would remove these variables.

CC Andrew Cooper <[email protected]>
CC Jan Beulich <[email protected]>

---
changes in v3:
- using_vmx instead of cpu_has_vmx
- clarify description on why this change needed
changes in v2:
- do not touch SVM code and macros
- drop vmx_ctrl_has_feature()
- guard cpu_has_vmx_* macros in common code instead
changes in v1:
- introduced helper routine vmx_ctrl_has_feature() and used it for all cpu_has_vmx_* macros

---
 xen/arch/x86/hvm/hvm.c                  |  2 +-
 xen/arch/x86/hvm/viridian/viridian.c    |  4 ++--
 xen/arch/x86/include/asm/hvm/vmx/vmcs.h | 12 ++++++------
 xen/arch/x86/traps.c                    |  5 +++--
 4 files changed, 12 insertions(+), 11 deletions(-)
xen/arch/x86/hvm/hvm.c +1 -1
index 7b8679bcd8..af45c5ed8c 100644
@@ -5197,7 +5197,7 @@ int hvm_debug_op(struct vcpu *v, int32_t op)
     {
         case XEN_DOMCTL_DEBUG_OP_SINGLE_STEP_ON:
         case XEN_DOMCTL_DEBUG_OP_SINGLE_STEP_OFF:
-            if ( !cpu_has_monitor_trap_flag )
+            if ( !using_vmx || !cpu_has_monitor_trap_flag )
                 return -EOPNOTSUPP;
             break;
         default:
xen/arch/x86/hvm/viridian/viridian.c +2 -2
index 0496c52ed5..59e7e7955a 100644
@@ -196,7 +196,7 @@ void cpuid_viridian_leaves(const struct vcpu *v, uint32_t leaf,
         res->a = CPUID4A_RELAX_TIMER_INT;
         if ( viridian_feature_mask(d) & HVMPV_hcall_remote_tlb_flush )
             res->a |= CPUID4A_HCALL_REMOTE_TLB_FLUSH;
-        if ( !cpu_has_vmx_apic_reg_virt )
+        if ( !using_vmx || !cpu_has_vmx_apic_reg_virt )
             res->a |= CPUID4A_MSR_BASED_APIC;
         if ( viridian_feature_mask(d) & HVMPV_hcall_ipi )
             res->a |= CPUID4A_SYNTHETIC_CLUSTER_IPI;
@@ -236,7 +236,7 @@ void cpuid_viridian_leaves(const struct vcpu *v, uint32_t leaf,
 
     case 6:
         /* Detected and in use hardware features. */
-        if ( cpu_has_vmx_virtualize_apic_accesses )
+        if ( using_vmx && cpu_has_vmx_virtualize_apic_accesses )
             res->a |= CPUID6A_APIC_OVERLAY;
         if ( cpu_has_vmx_msr_bitmap || (read_efer() & EFER_SVME) )
             res->a |= CPUID6A_MSR_BITMAPS;
xen/arch/x86/include/asm/hvm/vmx/vmcs.h +6 -6
index 58140af691..713088b8d3 100644
@@ -306,7 +306,7 @@ extern u64 vmx_ept_vpid_cap;
 #define cpu_has_vmx_vnmi \
     (vmx_pin_based_exec_control & PIN_BASED_VIRTUAL_NMIS)
 #define cpu_has_vmx_msr_bitmap \
-    (vmx_cpu_based_exec_control & CPU_BASED_ACTIVATE_MSR_BITMAP)
+    (using_vmx && vmx_cpu_based_exec_control & CPU_BASED_ACTIVATE_MSR_BITMAP)
 #define cpu_has_vmx_secondary_exec_control \
     (vmx_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)
 #define cpu_has_vmx_tertiary_exec_control \
@@ -316,7 +316,7 @@ extern u64 vmx_ept_vpid_cap;
 #define cpu_has_vmx_dt_exiting \
     (vmx_secondary_exec_control & SECONDARY_EXEC_DESCRIPTOR_TABLE_EXITING)
 #define cpu_has_vmx_rdtscp \
-    (vmx_secondary_exec_control & SECONDARY_EXEC_ENABLE_RDTSCP)
+    (using_vmx && vmx_secondary_exec_control & SECONDARY_EXEC_ENABLE_RDTSCP)
 #define cpu_has_vmx_vpid \
     (vmx_secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
 #define cpu_has_monitor_trap_flag \
@@ -333,7 +333,7 @@ extern u64 vmx_ept_vpid_cap;
 #define cpu_has_vmx_ple \
     (vmx_secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
 #define cpu_has_vmx_invpcid \
-    (vmx_secondary_exec_control & SECONDARY_EXEC_ENABLE_INVPCID)
+    (using_vmx && vmx_secondary_exec_control & SECONDARY_EXEC_ENABLE_INVPCID)
 #define cpu_has_vmx_apic_reg_virt \
     (vmx_secondary_exec_control & SECONDARY_EXEC_APIC_REGISTER_VIRT)
 #define cpu_has_vmx_virtual_intr_delivery \
@@ -347,14 +347,14 @@ extern u64 vmx_ept_vpid_cap;
 #define cpu_has_vmx_vmfunc \
     (vmx_secondary_exec_control & SECONDARY_EXEC_ENABLE_VM_FUNCTIONS)
 #define cpu_has_vmx_virt_exceptions \
-    (vmx_secondary_exec_control & SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS)
+    (using_vmx && vmx_secondary_exec_control & SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS)
 #define cpu_has_vmx_pml \
     (vmx_secondary_exec_control & SECONDARY_EXEC_ENABLE_PML)
 #define cpu_has_vmx_mpx \
-    ((vmx_vmexit_control & VM_EXIT_CLEAR_BNDCFGS) && \
+    (using_vmx && (vmx_vmexit_control & VM_EXIT_CLEAR_BNDCFGS) && \
      (vmx_vmentry_control & VM_ENTRY_LOAD_BNDCFGS))
 #define cpu_has_vmx_xsaves \
-    (vmx_secondary_exec_control & SECONDARY_EXEC_XSAVES)
+    (using_vmx && vmx_secondary_exec_control & SECONDARY_EXEC_XSAVES)
 #define cpu_has_vmx_tsc_scaling \
     (vmx_secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
 #define cpu_has_vmx_bus_lock_detection \
xen/arch/x86/traps.c +3 -2
index a81f3cf57c..c2f29fc9a4 100644
@@ -1130,7 +1130,7 @@ void cpuid_hypervisor_leaves(const struct vcpu *v, uint32_t leaf,
         if ( !is_hvm_domain(d) || subleaf != 0 )
             break;
 
-        if ( cpu_has_vmx_apic_reg_virt )
+        if ( using_vmx && cpu_has_vmx_apic_reg_virt )
             res->a |= XEN_HVM_CPUID_APIC_ACCESS_VIRT;
 
         /*
@@ -1139,7 +1139,8 @@ void cpuid_hypervisor_leaves(const struct vcpu *v, uint32_t leaf,
          * and wrmsr in the guest will run without VMEXITs (see
          * vmx_vlapic_msr_changed()).
          */
-        if ( cpu_has_vmx_virtualize_x2apic_mode &&
+        if ( using_vmx &&
+             cpu_has_vmx_virtualize_x2apic_mode &&
              cpu_has_vmx_apic_reg_virt &&
              cpu_has_vmx_virtual_intr_delivery )
             res->a |= XEN_HVM_CPUID_X2APIC_VIRT;
Signature · 1 line
2.25.1
#31 lore raw

Re: [XEN PATCH v3 12/16] x86/vmx: guard access to cpu_has_vmx_* in common code

xen/arch/x86/hvm/hvm.c int hvm_debug_op(struct vcpu *v, int32_t op) · line 5202

Quoted 10 lines · Sergiy Kibrik
xen/arch/x86/hvm/hvm.c +1 -1
@@ -5197,7 +5197,7 @@ int hvm_debug_op(struct vcpu *v, int32_t op)
     {
         case XEN_DOMCTL_DEBUG_OP_SINGLE_STEP_ON:
         case XEN_DOMCTL_DEBUG_OP_SINGLE_STEP_OFF:
-            if ( !cpu_has_monitor_trap_flag )
+            if ( !using_vmx || !cpu_has_monitor_trap_flag )
                 return -EOPNOTSUPP;
             break;

Here and elsewhere you're adding a redundant check of cpu_has_vmx, even if that's not visible without looking at using_vmx. I'm inclined to think that adding IS_ENABLED() to the various cpu_has_* that you care about might end up better (for being a little cheaper at runtime).

Jan

#32 lore raw

PATCH v3 13/16 x86/vpmu: guard calls to vmx/svm functions

In reply to Sergiy Kibrik

If VMX/SVM disabled in the build, we may still want to have vPMU drivers for PV guests. Yet in such case before using VMX/SVM features and functions we have to explicitly check if they're available in the build. For this puspose (and also not to complicate conditionals) two helpers introduced -- is_{vmx,svm}_vcpu(v) that check both HVM & VMX/SVM conditions at the same time, and they replace is_hvm_vcpu(v) macro where needed.

---
changes in v3:
- introduced macro is_{vmx,svm}_vcpu(v)
- changed description
- reordered patch, do not modify conditionals w/ cpu_has_vmx_msr_bitmap check

---
 xen/arch/x86/cpu/vpmu_amd.c   |  9 +++++----
 xen/arch/x86/cpu/vpmu_intel.c | 16 +++++++++-------
 2 files changed, 14 insertions(+), 11 deletions(-)
xen/arch/x86/cpu/vpmu_amd.c +5 -4
index 97e6315bd9..217501ecd3 100644
@@ -27,6 +27,7 @@
 #define is_pmu_enabled(msr) ((msr) & (1ULL << MSR_F10H_EVNTSEL_EN_SHIFT))
 #define set_guest_mode(msr) ((msr) |= (1ULL << MSR_F10H_EVNTSEL_GO_SHIFT))
 #define is_overflowed(msr) (!((msr) & (1ULL << (MSR_F10H_COUNTER_LENGTH - 1))))
+#define is_svm_vcpu(v) (using_svm && is_hvm_vcpu(v))
 
 static unsigned int __read_mostly num_counters;
 static const u32 __read_mostly *counters;
@@ -289,7 +290,7 @@ static int cf_check amd_vpmu_save(struct vcpu *v,  bool to_guest)
 
     context_save(v);
 
-    if ( !vpmu_is_set(vpmu, VPMU_RUNNING) && is_hvm_vcpu(v) &&
+    if ( !vpmu_is_set(vpmu, VPMU_RUNNING) && is_svm_vcpu(v) &&
          is_msr_bitmap_on(vpmu) )
         amd_vpmu_unset_msr_bitmap(v);
 
@@ -363,7 +364,7 @@ static int cf_check amd_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
             return 0;
         vpmu_set(vpmu, VPMU_RUNNING);
 
-        if ( is_hvm_vcpu(v) && is_msr_bitmap_on(vpmu) )
+        if ( is_svm_vcpu(v) && is_msr_bitmap_on(vpmu) )
              amd_vpmu_set_msr_bitmap(v);
     }
 
@@ -372,7 +373,7 @@ static int cf_check amd_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
         (is_pmu_enabled(msr_content) == 0) && vpmu_is_set(vpmu, VPMU_RUNNING) )
     {
         vpmu_reset(vpmu, VPMU_RUNNING);
-        if ( is_hvm_vcpu(v) && is_msr_bitmap_on(vpmu) )
+        if ( is_svm_vcpu(v) && is_msr_bitmap_on(vpmu) )
              amd_vpmu_unset_msr_bitmap(v);
         release_pmu_ownership(PMU_OWNER_HVM);
     }
@@ -415,7 +416,7 @@ static void cf_check amd_vpmu_destroy(struct vcpu *v)
 {
     struct vpmu_struct *vpmu = vcpu_vpmu(v);
 
-    if ( is_hvm_vcpu(v) && is_msr_bitmap_on(vpmu) )
+    if ( is_svm_vcpu(v) && is_msr_bitmap_on(vpmu) )
         amd_vpmu_unset_msr_bitmap(v);
 
     xfree(vpmu->context);
xen/arch/x86/cpu/vpmu_intel.c +9 -7
index cd414165df..f95a9b058d 100644
@@ -54,6 +54,8 @@
 #define MSR_PMC_ALIAS_MASK       (~(MSR_IA32_PERFCTR0 ^ MSR_IA32_A_PERFCTR0))
 static bool __read_mostly full_width_write;
 
+#define is_vmx_vcpu(v) ( using_vmx && is_hvm_vcpu(v) )
+
 /*
  * MSR_CORE_PERF_FIXED_CTR_CTRL contains the configuration of all fixed
  * counters. 4 bits for every counter.
@@ -269,7 +271,7 @@ static inline void __core2_vpmu_save(struct vcpu *v)
     if ( !is_hvm_vcpu(v) )
         rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, core2_vpmu_cxt->global_status);
     /* Save MSR to private context to make it fork-friendly */
-    else if ( mem_sharing_enabled(v->domain) )
+    else if ( is_vmx_vcpu(v) && mem_sharing_enabled(v->domain) )
         vmx_read_guest_msr(v, MSR_CORE_PERF_GLOBAL_CTRL,
                            &core2_vpmu_cxt->global_ctrl);
 }
@@ -333,7 +335,7 @@ static inline void __core2_vpmu_load(struct vcpu *v)
         wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, core2_vpmu_cxt->global_ctrl);
     }
     /* Restore MSR from context when used with a fork */
-    else if ( mem_sharing_is_fork(v->domain) )
+    else if ( is_vmx_vcpu(v) && mem_sharing_is_fork(v->domain) )
         vmx_write_guest_msr(v, MSR_CORE_PERF_GLOBAL_CTRL,
                             core2_vpmu_cxt->global_ctrl);
 }
@@ -442,7 +444,7 @@ static int cf_check core2_vpmu_alloc_resource(struct vcpu *v)
     if ( !acquire_pmu_ownership(PMU_OWNER_HVM) )
         return 0;
 
-    if ( is_hvm_vcpu(v) )
+    if ( is_vmx_vcpu(v) )
     {
         if ( vmx_add_host_load_msr(v, MSR_CORE_PERF_GLOBAL_CTRL, 0) )
             goto out_err;
@@ -584,7 +586,7 @@ static int cf_check core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
         if ( msr_content & fixed_ctrl_mask )
             return -EINVAL;
 
-        if ( is_hvm_vcpu(v) )
+        if ( is_vmx_vcpu(v) )
             vmx_read_guest_msr(v, MSR_CORE_PERF_GLOBAL_CTRL,
                                &core2_vpmu_cxt->global_ctrl);
         else
@@ -653,7 +655,7 @@ static int cf_check core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
             if ( blocked )
                 return -EINVAL;
 
-            if ( is_hvm_vcpu(v) )
+            if ( is_vmx_vcpu(v) )
                 vmx_read_guest_msr(v, MSR_CORE_PERF_GLOBAL_CTRL,
                                    &core2_vpmu_cxt->global_ctrl);
             else
@@ -672,7 +674,7 @@ static int cf_check core2_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content)
         wrmsrl(msr, msr_content);
     else
     {
-        if ( is_hvm_vcpu(v) )
+        if ( is_vmx_vcpu(v) )
             vmx_write_guest_msr(v, MSR_CORE_PERF_GLOBAL_CTRL, msr_content);
         else
             wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, msr_content);
@@ -706,7 +708,7 @@ static int cf_check core2_vpmu_do_rdmsr(unsigned int msr, uint64_t *msr_content)
             *msr_content = core2_vpmu_cxt->global_status;
             break;
         case MSR_CORE_PERF_GLOBAL_CTRL:
-            if ( is_hvm_vcpu(v) )
+            if ( is_vmx_vcpu(v) )
                 vmx_read_guest_msr(v, MSR_CORE_PERF_GLOBAL_CTRL, msr_content);
             else
                 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, *msr_content);
Signature · 1 line
2.25.1
#33 lore raw

Re: [XEN PATCH v3 13/16] x86/vpmu: guard calls to vmx/svm functions

Quoted 3 lines · Sergiy Kibrik

If VMX/SVM disabled in the build, we may still want to have vPMU drivers for PV guests. Yet in such case before using VMX/SVM features and functions we have to explicitly check if they're available in the build. For this puspose

Nit: It's not the first time that I see this apparent typo - I assume here and elsewhere "purpose" is meant?

xen/arch/x86/cpu/vpmu_amd.c line 30

Quoted 7 lines
xen/arch/x86/cpu/vpmu_amd.c +1 -0
@@ -27,6 +27,7 @@
 #define is_pmu_enabled(msr) ((msr) & (1ULL << MSR_F10H_EVNTSEL_EN_SHIFT))
 #define set_guest_mode(msr) ((msr) |= (1ULL << MSR_F10H_EVNTSEL_GO_SHIFT))
 #define is_overflowed(msr) (!((msr) & (1ULL << (MSR_F10H_COUNTER_LENGTH - 1))))
+#define is_svm_vcpu(v) (using_svm && is_hvm_vcpu(v))

Like for the earlier patch the implicit cpu_has_svm check you add here is redundant with us knowing we're dealing with AMD hardware here. Please consider switching to IS_ENABLED().

xen/arch/x86/cpu/vpmu_intel.c line 57

Quoted 7 lines
xen/arch/x86/cpu/vpmu_intel.c +1 -0
@@ -54,6 +54,8 @@
 #define MSR_PMC_ALIAS_MASK       (~(MSR_IA32_PERFCTR0 ^ MSR_IA32_A_PERFCTR0))
 static bool __read_mostly full_width_write;
 
+#define is_vmx_vcpu(v) ( using_vmx && is_hvm_vcpu(v) )

Nit (style): Unlike above, but like iirc you had it elsewhere, you have stray blanks here immediately inside the parentheses. Beyond that the comment above applies here, too (with s/AMD/Intel).

Jan

#34 lore raw

PATCH v3 14/16 ioreq: make arch_vcpu_ioreq_completion() an optional callback

In reply to Sergiy Kibrik

For the most cases arch_vcpu_ioreq_completion() routine is just an empty stub, except when handling VIO_realmode_completion, which only happens on HVM domains running on VT-x machine. When VT-x is disabled in build configuration, both x86 & arm version of routine become empty stubs.
To dispose of these useless stubs we can do optional call to arch-specific ioreq completion handler, if it's present, and drop arm and generic x86 handlers. Actual handling of VIO_realmore_completion can be done by VMX code then.

---
 xen/arch/arm/ioreq.c       |  6 ------
 xen/arch/x86/hvm/ioreq.c   | 23 -----------------------
 xen/arch/x86/hvm/vmx/vmx.c | 16 ++++++++++++++++
 xen/common/ioreq.c         |  5 ++++-
 xen/include/xen/ioreq.h    |  2 +-
 5 files changed, 21 insertions(+), 31 deletions(-)
xen/arch/arm/ioreq.c +0 -6
index 5df755b48b..2e829d2e7f 100644
@@ -135,12 +135,6 @@ bool arch_ioreq_complete_mmio(void)
     return false;
 }
 
-bool arch_vcpu_ioreq_completion(enum vio_completion completion)
-{
-    ASSERT_UNREACHABLE();
-    return true;
-}
-
 /*
  * The "legacy" mechanism of mapping magic pages for the IOREQ servers
  * is x86 specific, so the following hooks don't need to be implemented on Arm:
xen/arch/x86/hvm/ioreq.c +0 -23
index 4eb7a70182..088650e007 100644
@@ -29,29 +29,6 @@ bool arch_ioreq_complete_mmio(void)
     return handle_mmio();
 }
 
-bool arch_vcpu_ioreq_completion(enum vio_completion completion)
-{
-    switch ( completion )
-    {
-    case VIO_realmode_completion:
-    {
-        struct hvm_emulate_ctxt ctxt;
-
-        hvm_emulate_init_once(&ctxt, NULL, guest_cpu_user_regs());
-        vmx_realmode_emulate_one(&ctxt);
-        hvm_emulate_writeback(&ctxt);
-
-        break;
-    }
-
-    default:
-        ASSERT_UNREACHABLE();
-        break;
-    }
-
-    return true;
-}
-
 static gfn_t hvm_alloc_legacy_ioreq_gfn(struct ioreq_server *s)
 {
     struct domain *d = s->target;
xen/arch/x86/hvm/vmx/vmx.c +16 -0
index f16faa6a61..7187d1819c 100644
@@ -10,6 +10,7 @@
 #include <xen/param.h>
 #include <xen/trace.h>
 #include <xen/sched.h>
+#include <xen/ioreq.h>
 #include <xen/irq.h>
 #include <xen/softirq.h>
 #include <xen/domain_page.h>
@@ -2749,6 +2750,20 @@ static void cf_check vmx_set_reg(struct vcpu *v, unsigned int reg, uint64_t val)
     vmx_vmcs_exit(v);
 }
 
+bool realmode_vcpu_ioreq_completion(enum vio_completion completion)
+{
+    struct hvm_emulate_ctxt ctxt;
+
+    if ( completion != VIO_realmode_completion )
+        ASSERT_UNREACHABLE();
+
+    hvm_emulate_init_once(&ctxt, NULL, guest_cpu_user_regs());
+    vmx_realmode_emulate_one(&ctxt);
+    hvm_emulate_writeback(&ctxt);
+
+    return true;
+}
+
 static struct hvm_function_table __initdata_cf_clobber vmx_function_table = {
     .name                 = "VMX",
     .cpu_up_prepare       = vmx_cpu_up_prepare,
@@ -3070,6 +3085,7 @@ const struct hvm_function_table * __init start_vmx(void)
     lbr_tsx_fixup_check();
     ler_to_fixup_check();
 
+    arch_vcpu_ioreq_completion = realmode_vcpu_ioreq_completion;
     return &vmx_function_table;
 }
 
xen/common/ioreq.c +4 -1
index 1257a3d972..94fde97ece 100644
@@ -33,6 +33,8 @@
 #include <public/hvm/ioreq.h>
 #include <public/hvm/params.h>
 
+bool (*arch_vcpu_ioreq_completion)(enum vio_completion completion) = NULL;
+
 void ioreq_request_mapcache_invalidate(const struct domain *d)
 {
     struct vcpu *v = current;
@@ -244,7 +246,8 @@ bool vcpu_ioreq_handle_completion(struct vcpu *v)
         break;
 
     default:
-        res = arch_vcpu_ioreq_completion(completion);
+        if ( arch_vcpu_ioreq_completion )
+            res = arch_vcpu_ioreq_completion(completion);
         break;
     }
 
xen/include/xen/ioreq.h +1 -1
index cd399adf17..880214ec41 100644
@@ -111,7 +111,7 @@ void ioreq_domain_init(struct domain *d);
 int ioreq_server_dm_op(struct xen_dm_op *op, struct domain *d, bool *const_op);
 
 bool arch_ioreq_complete_mmio(void);
-bool arch_vcpu_ioreq_completion(enum vio_completion completion);
+extern bool (*arch_vcpu_ioreq_completion)(enum vio_completion completion);
 int arch_ioreq_server_map_pages(struct ioreq_server *s);
 void arch_ioreq_server_unmap_pages(struct ioreq_server *s);
 void arch_ioreq_server_enable(struct ioreq_server *s);
Signature · 1 line
2.25.1
#35 lore raw

Re: [XEN PATCH v3 14/16] ioreq: make arch_vcpu_ioreq_completion() an optional callback

Hi Sergiy,

Quoted 81 lines · Sergiy Kibrik

For the most cases arch_vcpu_ioreq_completion() routine is just an empty stub, except when handling VIO_realmode_completion, which only happens on HVM domains running on VT-x machine. When VT-x is disabled in build configuration, both x86 & arm version of routine become empty stubs.
To dispose of these useless stubs we can do optional call to arch-specific ioreq completion handler, if it's present, and drop arm and generic x86 handlers. Actual handling of VIO_realmore_completion can be done by VMX code then.

---
  xen/arch/arm/ioreq.c       |  6 ------
  xen/arch/x86/hvm/ioreq.c   | 23 -----------------------
  xen/arch/x86/hvm/vmx/vmx.c | 16 ++++++++++++++++
  xen/common/ioreq.c         |  5 ++++-
  xen/include/xen/ioreq.h    |  2 +-
  5 files changed, 21 insertions(+), 31 deletions(-)
xen/arch/arm/ioreq.c +0 -6
index 5df755b48b..2e829d2e7f 100644
@@ -135,12 +135,6 @@ bool arch_ioreq_complete_mmio(void)
      return false;
  }
 
-bool arch_vcpu_ioreq_completion(enum vio_completion completion)
-{
-    ASSERT_UNREACHABLE();
-    return true;
-}
-
  /*
   * The "legacy" mechanism of mapping magic pages for the IOREQ servers
   * is x86 specific, so the following hooks don't need to be implemented on Arm:
xen/arch/x86/hvm/ioreq.c +0 -23
index 4eb7a70182..088650e007 100644
@@ -29,29 +29,6 @@ bool arch_ioreq_complete_mmio(void)
      return handle_mmio();
  }
 
-bool arch_vcpu_ioreq_completion(enum vio_completion completion)
-{
-    switch ( completion )
-    {
-    case VIO_realmode_completion:
-    {
-        struct hvm_emulate_ctxt ctxt;
-
-        hvm_emulate_init_once(&ctxt, NULL, guest_cpu_user_regs());
-        vmx_realmode_emulate_one(&ctxt);
-        hvm_emulate_writeback(&ctxt);
-
-        break;
-    }
-
-    default:
-        ASSERT_UNREACHABLE();
-        break;
-    }
-
-    return true;
-}
-
  static gfn_t hvm_alloc_legacy_ioreq_gfn(struct ioreq_server *s)
  {
      struct domain *d = s->target;
xen/arch/x86/hvm/vmx/vmx.c +1 -0
index f16faa6a61..7187d1819c 100644
@@ -10,6 +10,7 @@
  #include <xen/param.h>
  #include <xen/trace.h>
  #include <xen/sched.h>
+#include <xen/ioreq.h>
  #include <xen/irq.h>
  #include <xen/softirq.h>
  #include <xen/domain_page.h>

xen/arch/x86/hvm/vmx/vmx.c static void cf_check vmx_set_reg(struct vcpu *v, unsigned int reg, uint64_t val) · line 2753

Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c +1 -0
@@ -2749,6 +2750,20 @@ static void cf_check vmx_set_reg(struct vcpu *v, unsigned int reg, uint64_t val)
      vmx_vmcs_exit(v);
  }
 
+bool realmode_vcpu_ioreq_completion(enum vio_completion completion)

No one seems to call this function outside of vmx.c. So can it be 'static'?

Quoted 29 lines
xen/arch/x86/hvm/vmx/vmx.c +14 -0
+{
+    struct hvm_emulate_ctxt ctxt;
+
+    if ( completion != VIO_realmode_completion )
+        ASSERT_UNREACHABLE();
+
+    hvm_emulate_init_once(&ctxt, NULL, guest_cpu_user_regs());
+    vmx_realmode_emulate_one(&ctxt);
+    hvm_emulate_writeback(&ctxt);
+
+    return true;
+}
+
  static struct hvm_function_table __initdata_cf_clobber vmx_function_table = {
      .name                 = "VMX",
      .cpu_up_prepare       = vmx_cpu_up_prepare,
@@ -3070,6 +3085,7 @@ const struct hvm_function_table * __init start_vmx(void)
      lbr_tsx_fixup_check();
      ler_to_fixup_check();
 
+    arch_vcpu_ioreq_completion = realmode_vcpu_ioreq_completion;
      return &vmx_function_table;
  }
 
xen/common/ioreq.c +0 -0
index 1257a3d972..94fde97ece 100644

xen/common/ioreq.c line 36

Quoted 4 lines
xen/common/ioreq.c +1 -0
@@ -33,6 +33,8 @@
  #include <public/hvm/ioreq.h>
  #include <public/hvm/params.h>
 
+bool (*arch_vcpu_ioreq_completion)(enum vio_completion completion) = NULL;

I don't think this should be allowed to be modified after boot. So I woudl add __ro_after_init.

xen/common/ioreq.c bool vcpu_ioreq_handle_completion(struct vcpu *v) · line 250

Quoted 11 lines
xen/common/ioreq.c +3 -1
+
  void ioreq_request_mapcache_invalidate(const struct domain *d)
  {
      struct vcpu *v = current;
@@ -244,7 +246,8 @@ bool vcpu_ioreq_handle_completion(struct vcpu *v)
          break;
 
      default:
-        res = arch_vcpu_ioreq_completion(completion);
+        if ( arch_vcpu_ioreq_completion )
+            res = arch_vcpu_ioreq_completion(completion);

I think this wants an:

else {
ASSERT_UNREACHABLE();
}

So this match the existing code. But I am not fully convinced that this is the right approach. Arch_vcpu_ioreq_completion is not meant to change across boot (or even at compile time for Arm).

Reading the previous thread, I think something like below would work:

static arch_vcpu_ioreq_completion(...)
{
#ifdef CONFIG_VMX
/* Existing code */
#else
ASSERT_UNREACHABLE();
return true;
#endif
}

If we want to avoid stub, then I think it would be better to use

#ifdef CONFIG_VMX
static arch_vcpu_ioreq...
{
}
#endif CONFIG_VMX

Then in the x86 header.

#ifdef CONFIG_VMX
static arch_vcpu_ioreq..();
#define arch_vcpu_ioreq...
#endif

And then in common/ioreq.c

#ifdef arch_vcpu_ioreq
res = arch_vcpu_ioreq(...)
#else
ASSERT_UNREACHABLE();
#endif

Quoted 16 lines
xen/common/ioreq.c +0 -0
          break;
      }
 
xen/include/xen/ioreq.h +1 -1
index cd399adf17..880214ec41 100644
@@ -111,7 +111,7 @@ void ioreq_domain_init(struct domain *d);
  int ioreq_server_dm_op(struct xen_dm_op *op, struct domain *d, bool *const_op);
 
  bool arch_ioreq_complete_mmio(void);
-bool arch_vcpu_ioreq_completion(enum vio_completion completion);
+extern bool (*arch_vcpu_ioreq_completion)(enum vio_completion completion);
  int arch_ioreq_server_map_pages(struct ioreq_server *s);
  void arch_ioreq_server_unmap_pages(struct ioreq_server *s);
  void arch_ioreq_server_enable(struct ioreq_server *s);
Signature · 1 line
Julien Grall
#36 lore raw

Re: [XEN PATCH v3 14/16] ioreq: make arch_vcpu_ioreq_completion() an optional callback

Quoted 8 lines · Julien Grall

xen/arch/x86/hvm/vmx/vmx.c static void cf_check vmx_set_reg(struct vcpu *v, unsigned int reg, uint64_t val) · line 2753

Quoted 5 lines · Sergiy Kibrik
xen/arch/x86/hvm/vmx/vmx.c +1 -0
@@ -2749,6 +2750,20 @@ static void cf_check vmx_set_reg(struct vcpu *v, unsigned int reg, uint64_t val)
      vmx_vmcs_exit(v);
  }
 
+bool realmode_vcpu_ioreq_completion(enum vio_completion completion)

No one seems to call this function outside of vmx.c. So can it be 'static'?

Plus it absolutely needs to be cf_check. If it is to stay, which it looks like it isn't, as per further comments from Julien.

Jan

#37 lore raw

Re: [XEN PATCH v3 14/16] ioreq: make arch_vcpu_ioreq_completion() an optional callback

In reply to Julien Grall

hi Julien,

04.06.24 14:07, Julien Grall:

Quoted 29 lines

So this match the existing code. But I am not fully convinced that this is the right approach. Arch_vcpu_ioreq_completion is not meant to change across boot (or even at compile time for Arm).

Reading the previous thread, I think something like below would work:

static arch_vcpu_ioreq_completion(...)
{
#ifdef CONFIG_VMX
/* Existing code */
#else
ASSERT_UNREACHABLE();
return true;
#endif
}

If we want to avoid stub, then I think it would be better to use

#ifdef CONFIG_VMX
static  arch_vcpu_ioreq...
{
}
#endif CONFIG_VMX

Then in the x86 header.

#ifdef CONFIG_VMX
static arch_vcpu_ioreq..();
#define arch_vcpu_ioreq...
#endif

Quoted 7 lines

And then in common/ioreq.c

#ifdef arch_vcpu_ioreq
res = arch_vcpu_ioreq(...)
#else
ASSERT_UNREACHABLE();
#endif

Thank you for taking a look and a suggestion. I agree that it's all related to compile time configuration and not a runtime.

I'll rewrite the patch and let's see how it looks like.

-Sergiy

#38 lore raw

PATCH v3 15/16 x86/vmx: replace CONFIG_HVM with CONFIG_VMX in vmx.h

In reply to Sergiy Kibrik

As now we got a separate config option for VMX which itself depends on CONFIG_HVM, we need to use it to provide vmx_pi_hooks_{assign,deassign} stubs for case when VMX is disabled while HVM is enabled.

---
changes in v3:
- use CONFIG_VMX instead of CONFIG_HVM to provide stubs, instead of guarding calls to vmx_pi_hooks_{assign,deassign} in iommu/vt-d code

---
 xen/arch/x86/include/asm/hvm/vmx/vmx.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/include/asm/hvm/vmx/vmx.h +1 -1
index 1489dd05c2..025bec2321 100644
@@ -599,7 +599,7 @@ void vmx_pi_desc_fixup(unsigned int cpu);
 
 void vmx_sync_exit_bitmap(struct vcpu *v);
 
-#ifdef CONFIG_HVM
+#ifdef CONFIG_VMX
 void vmx_pi_hooks_assign(struct domain *d);
 void vmx_pi_hooks_deassign(struct domain *d);
 #else
Signature · 1 line
2.25.1
Acked-by on v3 15/16
#39 lore raw

Re: [XEN PATCH v3 15/16] x86/vmx: replace CONFIG_HVM with CONFIG_VMX in vmx.h

Quoted 5 lines · Sergiy Kibrik

As now we got a separate config option for VMX which itself depends on CONFIG_HVM, we need to use it to provide vmx_pi_hooks_{assign,deassign} stubs for case when VMX is disabled while HVM is enabled.

#40 lore raw

PATCH v3 16/16 x86/hvm: make AMD-V and Intel VT-x support configurable

In reply to Sergiy Kibrik

From: Xenia Ragiadakou <[email protected]>

Provide the user with configuration control over the cpu virtualization support in Xen by making SVM and VMX options user selectable.

To preserve the current default behavior, both options depend on HVM and default to value of HVM.

To prevent users from unknowingly disabling virtualization support, make the controls user selectable only if EXPERT is enabled.

No functional change intended.

---
changes in v3:
- only tags added
changes in v2:
- remove dependency of build options IOMMU/AMD_IOMMU on VMX/SVM options

---
 xen/arch/x86/Kconfig | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
xen/arch/x86/Kconfig +16 -2
index 4a35c43dc5..dbee7c2efb 100644
@@ -123,10 +123,24 @@ config HVM
 	  If unsure, say Y.
 
 config SVM
-	def_bool HVM
+	bool "AMD-V" if EXPERT
+	depends on HVM
+	default HVM
+	help
+	  Enables virtual machine extensions on platforms that implement the
+	  AMD Virtualization Technology (AMD-V).
+	  If your system includes a processor with AMD-V support, say Y.
+	  If in doubt, say Y.
 
 config VMX
-	def_bool HVM
+	bool "Intel VT-x" if EXPERT
+	depends on HVM
+	default HVM
+	help
+	  Enables virtual machine extensions on platforms that implement the
+	  Intel Virtualization Technology (Intel VT-x).
+	  If your system includes a processor with Intel VT-x support, say Y.
+	  If in doubt, say Y.
 
 config XEN_SHSTK
 	bool "Supervisor Shadow Stacks"
Signature · 1 line
2.25.1