Skip to contentSkip to conversation

x86/hvm: move hvm_shadow_handle_cd() under CONFIG_INTEL_VMX ifdef

PATCH In review Grygorii Strashko 5 participants 6 messages Oct 23, 2025 – Oct 29, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/hvm: move hvm_shadow_handle_cd() under CONFIG_INTEL_VMX ifdef

From: Grygorii Strashko <[email protected]>

Functions:
hvm_shadow_handle_cd()
hvm_set_uc_mode()
domain_exit_uc_mode()
are used only by Intel VMX code, so move them under CONFIG_INTEL_VMX ifdef.

---
 xen/arch/x86/hvm/hvm.c | 50 ++++++++++++++++++++++--------------------
 1 file changed, 26 insertions(+), 24 deletions(-)
xen/arch/x86/hvm/hvm.c +26 -24
index f1035fc9f645..3a30404d9940 100644
@@ -2168,30 +2168,6 @@ int hvm_set_efer(uint64_t value)
     return X86EMUL_OKAY;
 }
 
-/* Exit UC mode only if all VCPUs agree on MTRR/PAT and are not in no_fill. */
-static bool domain_exit_uc_mode(struct vcpu *v)
-{
-    struct domain *d = v->domain;
-    struct vcpu *vs;
-
-    for_each_vcpu ( d, vs )
-    {
-        if ( (vs == v) || !vs->is_initialised )
-            continue;
-        if ( (vs->arch.hvm.cache_mode == NO_FILL_CACHE_MODE) ||
-             mtrr_pat_not_equal(vs, v) )
-            return 0;
-    }
-
-    return 1;
-}
-
-static void hvm_set_uc_mode(struct vcpu *v, bool is_in_uc_mode)
-{
-    v->domain->arch.hvm.is_in_uc_mode = is_in_uc_mode;
-    shadow_blow_tables_per_domain(v->domain);
-}
-
 int hvm_mov_to_cr(unsigned int cr, unsigned int gpr)
 {
     struct vcpu *curr = current;
@@ -2273,6 +2249,31 @@ int hvm_mov_from_cr(unsigned int cr, unsigned int gpr)
     return X86EMUL_UNHANDLEABLE;
 }
 
+#ifdef CONFIG_INTEL_VMX
+/* Exit UC mode only if all VCPUs agree on MTRR/PAT and are not in no_fill. */
+static bool domain_exit_uc_mode(struct vcpu *v)
+{
+    struct domain *d = v->domain;
+    struct vcpu *vs;
+
+    for_each_vcpu ( d, vs )
+    {
+        if ( (vs == v) || !vs->is_initialised )
+            continue;
+        if ( (vs->arch.hvm.cache_mode == NO_FILL_CACHE_MODE) ||
+             mtrr_pat_not_equal(vs, v) )
+            return 0;
+    }
+
+    return 1;
+}
+
+static void hvm_set_uc_mode(struct vcpu *v, bool is_in_uc_mode)
+{
+    v->domain->arch.hvm.is_in_uc_mode = is_in_uc_mode;
+    shadow_blow_tables_per_domain(v->domain);
+}
+
 void hvm_shadow_handle_cd(struct vcpu *v, unsigned long value)
 {
     if ( value & X86_CR0_CD )
@@ -2306,6 +2307,7 @@ void hvm_shadow_handle_cd(struct vcpu *v, unsigned long value)
         spin_unlock(&v->domain->arch.hvm.uc_lock);
     }
 }
+#endif
 
 static void hvm_update_cr(struct vcpu *v, unsigned int cr, unsigned long value)
 {
Signature · 1 line
2.34.1
#2 lore raw
In reply to Grygorii Strashko

Le 23/10/2025 à 17:22, Grygorii Strashko a écrit :

Quoted 8 lines

From: Grygorii Strashko <[email protected]>

Functions:
hvm_shadow_handle_cd()
hvm_set_uc_mode()
domain_exit_uc_mode()
are used only by Intel VMX code, so move them under CONFIG_INTEL_VMX ifdef.

If they are actually Intel VMX specific, they should rather be moved to VMX code (and named appropriately) rather than if-defed in shared hvm code. If AMD code happens to need these functions in the future, it would make things break pretty confusingly (as headers are not updated consistently).

Quoted 73 lines
---
  xen/arch/x86/hvm/hvm.c | 50 ++++++++++++++++++++++--------------------
  1 file changed, 26 insertions(+), 24 deletions(-)
xen/arch/x86/hvm/hvm.c +25 -24
index f1035fc9f645..3a30404d9940 100644
@@ -2168,30 +2168,6 @@ int hvm_set_efer(uint64_t value)
      return X86EMUL_OKAY;
  }
 
-/* Exit UC mode only if all VCPUs agree on MTRR/PAT and are not in no_fill. */
-static bool domain_exit_uc_mode(struct vcpu *v)
-{
-    struct domain *d = v->domain;
-    struct vcpu *vs;
-
-    for_each_vcpu ( d, vs )
-    {
-        if ( (vs == v) || !vs->is_initialised )
-            continue;
-        if ( (vs->arch.hvm.cache_mode == NO_FILL_CACHE_MODE) ||
-             mtrr_pat_not_equal(vs, v) )
-            return 0;
-    }
-
-    return 1;
-}
-
-static void hvm_set_uc_mode(struct vcpu *v, bool is_in_uc_mode)
-{
-    v->domain->arch.hvm.is_in_uc_mode = is_in_uc_mode;
-    shadow_blow_tables_per_domain(v->domain);
-}
-
  int hvm_mov_to_cr(unsigned int cr, unsigned int gpr)
  {
      struct vcpu *curr = current;
@@ -2273,6 +2249,31 @@ int hvm_mov_from_cr(unsigned int cr, unsigned int gpr)
      return X86EMUL_UNHANDLEABLE;
  }
 
+#ifdef CONFIG_INTEL_VMX
+/* Exit UC mode only if all VCPUs agree on MTRR/PAT and are not in no_fill. */
+static bool domain_exit_uc_mode(struct vcpu *v)
+{
+    struct domain *d = v->domain;
+    struct vcpu *vs;
+
+    for_each_vcpu ( d, vs )
+    {
+        if ( (vs == v) || !vs->is_initialised )
+            continue;
+        if ( (vs->arch.hvm.cache_mode == NO_FILL_CACHE_MODE) ||
+             mtrr_pat_not_equal(vs, v) )
+            return 0;
+    }
+
+    return 1;
+}
+
+static void hvm_set_uc_mode(struct vcpu *v, bool is_in_uc_mode)
+{
+    v->domain->arch.hvm.is_in_uc_mode = is_in_uc_mode;
+    shadow_blow_tables_per_domain(v->domain);
+}
+
  void hvm_shadow_handle_cd(struct vcpu *v, unsigned long value)
  {
      if ( value & X86_CR0_CD )

xen/arch/x86/hvm/hvm.c void hvm_shadow_handle_cd(struct vcpu *v, unsigned long value) · line 2313

Quoted 7 lines
xen/arch/x86/hvm/hvm.c +1 -0
@@ -2306,6 +2307,7 @@ void hvm_shadow_handle_cd(struct vcpu *v, unsigned long value)
          spin_unlock(&v->domain->arch.hvm.uc_lock);
      }
  }
+#endif
 
  static void hvm_update_cr(struct vcpu *v, unsigned int cr, unsigned long value)
  {

Teddy

--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#3 lore raw
Quoted 10 lines · Teddy Astie

Le 23/10/2025 à 17:22, Grygorii Strashko a écrit :

Quoted 8 lines

From: Grygorii Strashko <[email protected]>

Functions:

   hvm_shadow_handle_cd()
   hvm_set_uc_mode()
   domain_exit_uc_mode()

are used only by Intel VMX code, so move them under CONFIG_INTEL_VMX ifdef.

Quoted 5 lines

If they are actually Intel VMX specific, they should rather be moved to VMX code (and named appropriately) rather than if-defed in shared hvm code. If AMD code happens to need these functions in the future, it would make things break pretty confusingly (as headers are not updated consistently).

I agree and like it even better. Can try if there is no objections?

There is hvm_prepare_vm86_tss() also which is also used by VMX code only.

Quoted 81 lines
Quoted 81 lines
---
   xen/arch/x86/hvm/hvm.c | 50 ++++++++++++++++++++++--------------------
   1 file changed, 26 insertions(+), 24 deletions(-)
xen/arch/x86/hvm/hvm.c +26 -24
index f1035fc9f645..3a30404d9940 100644
@@ -2168,30 +2168,6 @@ int hvm_set_efer(uint64_t value)
       return X86EMUL_OKAY;
   }
 
-/* Exit UC mode only if all VCPUs agree on MTRR/PAT and are not in no_fill. */
-static bool domain_exit_uc_mode(struct vcpu *v)
-{
-    struct domain *d = v->domain;
-    struct vcpu *vs;
-
-    for_each_vcpu ( d, vs )
-    {
-        if ( (vs == v) || !vs->is_initialised )
-            continue;
-        if ( (vs->arch.hvm.cache_mode == NO_FILL_CACHE_MODE) ||
-             mtrr_pat_not_equal(vs, v) )
-            return 0;
-    }
-
-    return 1;
-}
-
-static void hvm_set_uc_mode(struct vcpu *v, bool is_in_uc_mode)
-{
-    v->domain->arch.hvm.is_in_uc_mode = is_in_uc_mode;
-    shadow_blow_tables_per_domain(v->domain);
-}
-
   int hvm_mov_to_cr(unsigned int cr, unsigned int gpr)
   {
       struct vcpu *curr = current;
@@ -2273,6 +2249,31 @@ int hvm_mov_from_cr(unsigned int cr, unsigned int gpr)
       return X86EMUL_UNHANDLEABLE;
   }
 
+#ifdef CONFIG_INTEL_VMX
+/* Exit UC mode only if all VCPUs agree on MTRR/PAT and are not in no_fill. */
+static bool domain_exit_uc_mode(struct vcpu *v)
+{
+    struct domain *d = v->domain;
+    struct vcpu *vs;
+
+    for_each_vcpu ( d, vs )
+    {
+        if ( (vs == v) || !vs->is_initialised )
+            continue;
+        if ( (vs->arch.hvm.cache_mode == NO_FILL_CACHE_MODE) ||
+             mtrr_pat_not_equal(vs, v) )
+            return 0;
+    }
+
+    return 1;
+}
+
+static void hvm_set_uc_mode(struct vcpu *v, bool is_in_uc_mode)
+{
+    v->domain->arch.hvm.is_in_uc_mode = is_in_uc_mode;
+    shadow_blow_tables_per_domain(v->domain);
+}
+
   void hvm_shadow_handle_cd(struct vcpu *v, unsigned long value)
   {
       if ( value & X86_CR0_CD )
@@ -2306,6 +2307,7 @@ void hvm_shadow_handle_cd(struct vcpu *v, unsigned long value)
           spin_unlock(&v->domain->arch.hvm.uc_lock);
       }
   }
+#endif
 
   static void hvm_update_cr(struct vcpu *v, unsigned int cr, unsigned long value)
   {
Signature · 2 lines
Best regards,
-grygorii
#4 lore raw
Quoted 18 lines · Grygorii Strashko
Quoted 11 lines · Teddy Astie

Le 23/10/2025 à 17:22, Grygorii Strashko a écrit :

Quoted 9 lines

From: Grygorii Strashko <[email protected]>

Functions:
   hvm_shadow_handle_cd()
   hvm_set_uc_mode()
   domain_exit_uc_mode()
are used only by Intel VMX code, so move them under CONFIG_INTEL_VMX ifdef.

Quoted 5 lines

If they are actually Intel VMX specific, they should rather be moved to VMX code (and named appropriately) rather than if-defed in shared hvm code. If AMD code happens to need these functions in the future, it would make things break pretty confusingly (as headers are not updated consistently).

Quoted 3 lines

I agree and like it even better. Can try if there is no objections?

There is hvm_prepare_vm86_tss() also which is also used by VMX code only.

There is some horrible complexity here.

With SVM, we can run guests perfectly well in Cache Disable mode (i.e. gCR0.CD=1).  Nothing extra is needed.

With VT-x, for unknown reasons, entering a VM explicitly leaves CR0.{CD,NW} unmodified, i.e. always the Xen settings, which will be neither bit set.  As a result, when the guest wishes to set gCR0.{CD,NW}, we have to jump through hoops to make this happen.

Without going into the details, the upshot is that this "handle uc mode" logic is VT-x specific, and oughtn't to be in hvm.c.  Moving it seems fine, but the data, d->arch.hvm.uc_*, wants to move into the vmx union too.

hvm_prepare_vm86_tss() is rather different.  It, ultimately, is because of limitations in early VT-x and it's probably fine to move too, although the infrastructure surrounding it has bigger changes needed.

~Andrew

#5 lore raw
In reply to Grygorii Strashko
Quoted 53 lines · Grygorii Strashko
xen/arch/x86/hvm/hvm.c +16 -24
@@ -2168,30 +2168,6 @@ int hvm_set_efer(uint64_t value)
     return X86EMUL_OKAY;
 }
 
-/* Exit UC mode only if all VCPUs agree on MTRR/PAT and are not in no_fill. */
-static bool domain_exit_uc_mode(struct vcpu *v)
-{
-    struct domain *d = v->domain;
-    struct vcpu *vs;
-
-    for_each_vcpu ( d, vs )
-    {
-        if ( (vs == v) || !vs->is_initialised )
-            continue;
-        if ( (vs->arch.hvm.cache_mode == NO_FILL_CACHE_MODE) ||
-             mtrr_pat_not_equal(vs, v) )
-            return 0;
-    }
-
-    return 1;
-}
-
-static void hvm_set_uc_mode(struct vcpu *v, bool is_in_uc_mode)
-{
-    v->domain->arch.hvm.is_in_uc_mode = is_in_uc_mode;
-    shadow_blow_tables_per_domain(v->domain);
-}
-
 int hvm_mov_to_cr(unsigned int cr, unsigned int gpr)
 {
     struct vcpu *curr = current;
@@ -2273,6 +2249,31 @@ int hvm_mov_from_cr(unsigned int cr, unsigned int gpr)
     return X86EMUL_UNHANDLEABLE;
 }
 
+#ifdef CONFIG_INTEL_VMX
+/* Exit UC mode only if all VCPUs agree on MTRR/PAT and are not in no_fill. */
+static bool domain_exit_uc_mode(struct vcpu *v)
+{
+    struct domain *d = v->domain;
+    struct vcpu *vs;
+
+    for_each_vcpu ( d, vs )
+    {
+        if ( (vs == v) || !vs->is_initialised )
+            continue;
+        if ( (vs->arch.hvm.cache_mode == NO_FILL_CACHE_MODE) ||
+             mtrr_pat_not_equal(vs, v) )
+            return 0;
+    }
+

xen/arch/x86/hvm/hvm.c int hvm_mov_from_cr(unsigned int cr, unsigned int gpr) · line 2277

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +9 -0
+    return 1;
+}
+
+static void hvm_set_uc_mode(struct vcpu *v, bool is_in_uc_mode)
+{
+    v->domain->arch.hvm.is_in_uc_mode = is_in_uc_mode;
+    shadow_blow_tables_per_domain(v->domain);
+}
+
 void hvm_shadow_handle_cd(struct vcpu *v, unsigned long value)

In addition to what others have said, as we're talking of unreachable code
here: This function also is unreachable when SHADOW_PAGING=n. If already you make adjustments, I think you want to cover this other aspect at the same time.

Jan