Skip to contentSkip to conversation

x86/vmx: Rework __vmread()/vmread_safe()/vmr()

PATCH In review [email protected] 3 participants 5 messages Apr 8, 2025 – Apr 17, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/vmx: Rework __vmread()/vmread_safe()/vmr()

From: Denis Mukhin <[email protected]>

Use asm goto() in vmread_safe() to simplify the error handling logic.

Update __vmread() to return unsigned long as per suggestion in [1]. Rename __vmread() to vmread_unsafe() to match the behavior. Update all call sites everywhere. Drop UNLIKELY_*().

Group all vmread*() calls close to each other in vmx.h

Rename internal vmr*() to vmread*().

[1] https://lore.kernel.org/xen-devel/[email protected]/

---
Link to CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/1756781092

---
 xen/arch/x86/cpu/vpmu_intel.c          |   3 +-
 xen/arch/x86/hvm/vmx/intr.c            |  26 +--
 xen/arch/x86/hvm/vmx/realmode.c        |   2 +-
 xen/arch/x86/hvm/vmx/vmcs.c            | 160 ++++++++++---------
 xen/arch/x86/hvm/vmx/vmx.c             | 209 +++++++++++--------------
 xen/arch/x86/hvm/vmx/vvmx.c            |  43 +++--
 xen/arch/x86/include/asm/domain.h      |   2 +-
 xen/arch/x86/include/asm/hvm/vmx/vmx.h |  69 ++++----
 8 files changed, 235 insertions(+), 279 deletions(-)
xen/arch/x86/cpu/vpmu_intel.c +1 -2
index 7ce98ee42e..9c93d1f28c 100644
@@ -796,8 +796,7 @@ static int cf_check core2_vpmu_do_interrupt(void)
     else
     {
         /* No PMC overflow but perhaps a Trace Message interrupt. */
-        __vmread(GUEST_IA32_DEBUGCTL, &msr_content);
-        if ( !(msr_content & IA32_DEBUGCTLMSR_TR) )
+        if ( !(vmread_unsafe(GUEST_IA32_DEBUGCTL) & IA32_DEBUGCTLMSR_TR) )
             return 0;
     }
 
xen/arch/x86/hvm/vmx/intr.c +7 -19
index 91b407e6bc..63330615cd 100644
@@ -63,9 +63,8 @@ static void vmx_enable_intr_window(struct vcpu *v, struct hvm_intack intack)
 
     if ( unlikely(tb_init_done) )
     {
-        unsigned long intr;
+        unsigned long intr = vmread_unsafe(VM_ENTRY_INTR_INFO);
 
-        __vmread(VM_ENTRY_INTR_INFO, &intr);
         TRACE(TRC_HVM_INTR_WINDOW, intack.vector, intack.source,
               (intr & INTR_INFO_VALID_MASK) ? intr & 0xff : -1);
     }
@@ -81,9 +80,8 @@ static void vmx_enable_intr_window(struct vcpu *v, struct hvm_intack intack)
          * we may immediately vmexit and hance make no progress!
          * (see SDM 3B 21.3, "Other Causes of VM Exits").
          */
-        unsigned long intr_shadow;
+        unsigned long intr_shadow = vmread_unsafe(GUEST_INTERRUPTIBILITY_INFO);
 
-        __vmread(GUEST_INTERRUPTIBILITY_INFO, &intr_shadow);
         if ( intr_shadow & VMX_INTR_SHADOW_STI )
         {
             /* Having both STI-blocking and MOV-SS-blocking fails vmentry. */
@@ -144,14 +142,8 @@ enum hvm_intblk cf_check nvmx_intr_blocked(struct vcpu *v)
         if ( nvcpu->nv_vmexit_pending ||
              nvcpu->nv_vmswitch_in_progress )
             r = hvm_intblk_rflags_ie;
-        else
-        {
-            unsigned long intr_info;
-
-            __vmread(VM_ENTRY_INTR_INFO, &intr_info);
-            if ( intr_info & INTR_INFO_VALID_MASK )
-                r = hvm_intblk_rflags_ie;
-        }
+        else if ( vmread_unsafe(VM_ENTRY_INTR_INFO) & INTR_INFO_VALID_MASK )
+            r = hvm_intblk_rflags_ie;
     }
     else if ( nvcpu->nv_vmentry_pending )
         r = hvm_intblk_rflags_ie;
@@ -253,8 +245,6 @@ void asmlinkage vmx_intr_assist(void)
     pt_vector = pt_update_irq(v);
 
     do {
-        unsigned long intr_info;
-
         intack = hvm_vcpu_has_pending_irq(v);
         if ( likely(intack.source == hvm_intsrc_none) )
             goto out;
@@ -275,8 +265,7 @@ void asmlinkage vmx_intr_assist(void)
                 goto out;
             }
 
-            __vmread(VM_ENTRY_INTR_INFO, &intr_info);
-            if ( intr_info & INTR_INFO_VALID_MASK )
+            if ( vmread_unsafe(VM_ENTRY_INTR_INFO) & INTR_INFO_VALID_MASK )
             {
                 if ( (intack.source == hvm_intsrc_pic) ||
                      (intack.source == hvm_intsrc_nmi) ||
@@ -299,8 +288,7 @@ void asmlinkage vmx_intr_assist(void)
         }
         else
         {
-            __vmread(VM_ENTRY_INTR_INFO, &intr_info);
-            if ( intr_info & INTR_INFO_VALID_MASK )
+            if ( vmread_unsafe(VM_ENTRY_INTR_INFO) & INTR_INFO_VALID_MASK )
             {
                 vmx_enable_intr_window(v, intack);
                 goto out;
@@ -377,7 +365,7 @@ void asmlinkage vmx_intr_assist(void)
         }
 
         /* we need update the RVI field */
-        __vmread(GUEST_INTR_STATUS, &status);
+        status = vmread_unsafe(GUEST_INTR_STATUS);
         status &= ~VMX_GUEST_INTR_STATUS_SUBFIELD_BITMASK;
         status |= VMX_GUEST_INTR_STATUS_SUBFIELD_BITMASK &
                     intack.vector;
xen/arch/x86/hvm/vmx/realmode.c +1 -1
index ff44ddcfa6..e8eb9773ee 100644
@@ -159,7 +159,7 @@ void vmx_realmode(struct cpu_user_regs *regs)
     unsigned int emulations = 0;
 
     /* Get-and-clear VM_ENTRY_INTR_INFO. */
-    __vmread(VM_ENTRY_INTR_INFO, &intr_info);
+    intr_info = vmread_unsafe(VM_ENTRY_INTR_INFO);
     if ( intr_info & INTR_INFO_VALID_MASK )
         __vmwrite(VM_ENTRY_INTR_INFO, 0);
 
xen/arch/x86/hvm/vmx/vmcs.c +82 -78

296 lines not on this page · raw message

xen/arch/x86/hvm/vmx/vmx.c +90 -119

597 lines not on this page · raw message

xen/arch/x86/hvm/vmx/vvmx.c +19 -24

169 lines not on this page · raw message

xen/arch/x86/include/asm/domain.h +1 -1
index 6b877e33a1..ffe9acd75d 100644
@@ -595,7 +595,7 @@ struct arch_vcpu
 
     /* Debug registers. */
     unsigned long dr[4];
-    unsigned long dr7; /* Ideally int, but __vmread() needs long. */
+    unsigned long dr7; /* Ideally int, but vmread_unsafe() needs unsigned long. */
     unsigned int dr6;
 
     /* other state */
xen/arch/x86/include/asm/hvm/vmx/vmx.h +34 -35

91 lines not on this page · raw message

Signature · 1 line
2.34.1
#1 lore raw
Quoted 3 lines · On 08.04.2025 03:15, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Use asm goto() in vmread_safe() to simplify the error handling logic.

This can't be quite right, considering we need to avoid outputs there.

Quoted 2 lines

Update __vmread() to return unsigned long as per suggestion in [1]. Rename __vmread() to vmread_unsafe() to match the behavior.

I disagree with this renaming: See e.g. rdmsr() and rdmsr_safe() that we have. The common case function wants to not have unnecessary verbosity in its name. And there's nothing unsafe about it in the general case. Plus if there was anything unsafe, many of the call sites would require some form of error handling.

Quoted 11 lines
xen/arch/x86/hvm/vmx/vmcs.c +1 -2
@@ -1957,38 +1955,44 @@ void cf_check vmx_do_resume(void)
     hvm_do_resume(v);
 
     /* Sync host CR4 in case its value has changed. */
-    __vmread(HOST_CR4, &host_cr4);
-    if ( host_cr4 != read_cr4() )
+    if ( vmread_unsafe(HOST_CR4) != read_cr4() )
         __vmwrite(HOST_CR4, read_cr4());
 
     reset_stack_and_jump(vmx_asm_do_vmentry);
 }

xen/arch/x86/hvm/vmx/vmcs.c void cf_check vmx_do_resume(void) · line 1969

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmcs.c +4 -3
 
-static inline unsigned long vmr(unsigned long field)
+static inline unsigned long vmread(unsigned long field)
 {
-    unsigned long val;
+    unsigned long value = 0;
 
-    return vmread_safe(field, &val) ? 0 : val;
+    asm goto ( "vmread %[field], %[value]\n\t"
+               "jmp %l[out]"

Why's the JMP needed here? With it dropped, why's open-coding of vmread_unsafe() necessary here? And why's the "safe" variant being replaced by the "unsafe" one?

xen/arch/x86/hvm/vmx/vmcs.c void cf_check vmx_do_resume(void) · line 1971

Quoted 2 lines
xen/arch/x86/hvm/vmx/vmcs.c +2 -0
+               :
+               : [field] "r" (field), [value] "m" (value)

"value" is an output and hence cannot be just "m" (and hence be an input"). The only option to make such work correctly would be to ...

Review void cf_check vmx_do_resume(void) · line 1972

Quoted 1 line
(quoted diff) +1 -0
+               :

... add a "memory" clobber here. Which may have other unwanted side effects.

xen/arch/x86/hvm/vmx/vmcs.c void cf_check vmx_do_resume(void) · line 1974

Quoted 2 lines
xen/arch/x86/hvm/vmx/vmcs.c +2 -0
+               : out );
+out:

Nit (here and elsewhere): Labels indented by at least one blank please. See ./CODING_STYLE.

Quoted 19 lines
xen/arch/x86/hvm/vmx/vmcs.c +5 -4
+    return value;
 }
 
-#define vmr16(fld) ({             \
+#define vmread16(fld) ({          \
     BUILD_BUG_ON((fld) & 0x6001); \
-    (uint16_t)vmr(fld);           \
+    (uint16_t)vmread(fld);        \
 })
 
-#define vmr32(fld) ({                         \
+#define vmread32(fld) ({                      \
     BUILD_BUG_ON(((fld) & 0x6001) != 0x4000); \
-    (uint32_t)vmr(fld);                       \
+    (uint32_t)vmread(fld);                    \
 })
 
 static void vmx_dump_sel(const char *name, uint32_t selector)
 {

xen/arch/x86/hvm/vmx/vmcs.c void cf_check vmx_do_resume(void) · line 1995

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmcs.c +4 -4
     uint32_t sel, attr, limit;
     uint64_t base;
-    sel = vmr(selector);
-    attr = vmr(selector + (GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR));
-    limit = vmr(selector + (GUEST_ES_LIMIT - GUEST_ES_SELECTOR));
-    base = vmr(selector + (GUEST_ES_BASE - GUEST_ES_SELECTOR));
+    sel = vmread(selector);
+    attr = vmread(selector + (GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR));
+    limit = vmread(selector + (GUEST_ES_LIMIT - GUEST_ES_SELECTOR));
+    base = vmread(selector + (GUEST_ES_BASE - GUEST_ES_SELECTOR));

The renaming causes entirely unnecessary extra churn here (and of course elsewhere). The patch is already big enough without this.

xen/arch/x86/include/asm/domain.h struct arch_vcpu · line 599

Quoted 9 lines
xen/arch/x86/include/asm/domain.h +1 -1
@@ -595,7 +595,7 @@ struct arch_vcpu
 
     /* Debug registers. */
     unsigned long dr[4];
-    unsigned long dr7; /* Ideally int, but __vmread() needs long. */
+    unsigned long dr7; /* Ideally int, but vmread_unsafe() needs unsigned long. */
     unsigned int dr6;

If you left this comment alone, all would be (largely) fine - this particular aspect could then be tidied in a follow-on path. But vmread_unsafe() specifically does not need "unsigned long" anymore. The issue was with __vmread() taking a pointer argument.

Quoted 12 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +1 -4
@@ -320,16 +320,40 @@ static always_inline void __vmpclear(u64 addr)
     BUG();
 }
 
-static always_inline void __vmread(unsigned long field, unsigned long *value)
+static always_inline unsigned long vmread_unsafe(unsigned long field)
 {
-    asm volatile ( "vmread %1, %0\n\t"
-                   /* CF==1 or ZF==1 --> BUG() */
-                   UNLIKELY_START(be, vmread)

xen/arch/x86/include/asm/hvm/vmx/vmx.h static always_inline void __vmpclear(u64 addr) · line 329

Quoted 10 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +5 -5
-                   _ASM_BUGFRAME_TEXT(0)
-                   UNLIKELY_END_SECTION
-                   : "=rm" (*value)
-                   : "r" (field),
-                     _ASM_BUGFRAME_INFO(BUGFRAME_bug, __LINE__, __FILE__, 0) );
+    unsigned long value;
+
+    asm volatile ( "vmread %[field], %[value]\n\t"
+                   "jc 1f\n\t"
+                   "jz 1f\n\t"

Why not JBE as it was before?

xen/arch/x86/include/asm/hvm/vmx/vmx.h static always_inline void __vmpclear(u64 addr) · line 333

Quoted 4 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +4 -0
+                   "jmp 2f\n\t"
+                   "1:\n\t"
+                   "    ud2\n\t"
+                   "2:"

This is specifically why we used UNLIKELY_*() before. There's no justification whatsoever in the description for the dropping of its use here.

Plus - where did _ASM_BUGFRAME_TEXT(0) go? A bare UD2 isn't acceptable, as it won't be possible to associate it back with the respective source line.

Quoted 4 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +4 -0
+                   : [value] "=rm" (value)
+                   : [field] "r" (field) );
+
+    return value;

xen/arch/x86/include/asm/hvm/vmx/vmx.h static always_inline void __vmpclear(u64 addr) · line 347

Quoted 10 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +10 -0
+}
+
+static inline enum vmx_insn_errno vmread_safe(unsigned long field,
+                                              unsigned long *value)
+{
+    asm goto ( "vmread %[field], %[value]\n\t"
+               "jc %l[vmfail_invalid]\n\t"
+               "jz %l[vmfail_error]"
+               :
+               : [field] "r" (field), [value] "m" (*value)

See comments on the vmr() adjustments you're making.

Jan

#2 lore raw
Quoted 11 lines · Jan Beulich

xen/arch/x86/include/asm/hvm/vmx/vmx.h static always_inline void __vmpclear(u64 addr) · line 347

Quoted 8 lines · On 08.04.2025 03:15, [email protected] wrote:
xen/arch/x86/include/asm/hvm/vmx/vmx.h +8 -0
+static inline enum vmx_insn_errno vmread_safe(unsigned long field,
+                                              unsigned long *value)
+{
+    asm goto ( "vmread %[field], %[value]\n\t"
+               "jc %l[vmfail_invalid]\n\t"
+               "jz %l[vmfail_error]"
+               :
+               : [field] "r" (field), [value] "m" (*value)

See comments on the vmr() adjustments you're making.

Oh, and - why is "+rm" lost here and there? We shouldn't be taking away from the compiler the option of not going through memory here. Aiui that's solely because you figured that you can't use "rm" (i.e. an input). But then you drew the wrong conclusion.

Jan

#3 lore raw
In reply to Jan Beulich
Quoted 10 lines · Jan Beulich
Quoted 3 lines · On 08.04.2025 03:15, [email protected] wrote:

From: Denis Mukhin [email protected]

Use asm goto() in vmread_safe() to simplify the error handling logic.

This can't be quite right, considering we need to avoid outputs there.

I should have experiment a bit more :-/

Thanks for reviewing this.

Quoted 10 lines
Quoted 3 lines

Update __vmread() to return unsigned long as per suggestion in [1]. Rename __vmread() to vmread_unsafe() to match the behavior.

I disagree with this renaming: See e.g. rdmsr() and rdmsr_safe() that we have. The common case function wants to not have unnecessary verbosity in its name. And there's nothing unsafe about it in the general case. Plus if there was anything unsafe, many of the call sites would require some form of error handling.

I will revert all the naming changes and limit the scope only to __vmread() signature change.

Quoted 37 lines
Quoted 12 lines
xen/arch/x86/hvm/vmx/vmcs.c +1 -2
@@ -1957,38 +1955,44 @@ void cf_check vmx_do_resume(void)
 hvm_do_resume(v);
 
 /* Sync host CR4 in case its value has changed. */
- __vmread(HOST_CR4, &host_cr4);
- if ( host_cr4 != read_cr4() )
+ if ( vmread_unsafe(HOST_CR4) != read_cr4() )
 __vmwrite(HOST_CR4, read_cr4());
 
 reset_stack_and_jump(vmx_asm_do_vmentry);
 }

xen/arch/x86/hvm/vmx/vmcs.c void cf_check vmx_do_resume(void) · line 1969

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmcs.c +4 -3
 
-static inline unsigned long vmr(unsigned long field)
+static inline unsigned long vmread(unsigned long field)
 {
- unsigned long val;
+ unsigned long value = 0;
 
- return vmread_safe(field, &val) ? 0 : val;
+ asm goto ( "vmread %[field], %[value]\n\t"
+ "jmp %l[out]"

Why's the JMP needed here? With it dropped, why's open-coding of vmread_unsafe() necessary here? And why's the "safe" variant being replaced by the "unsafe" one?

xen/arch/x86/hvm/vmx/vmcs.c void cf_check vmx_do_resume(void) · line 1971

Quoted 2 lines
xen/arch/x86/hvm/vmx/vmcs.c +2 -0
+ :
+ : [field] "r" (field), [value] "m" (value)

"value" is an output and hence cannot be just "m" (and hence be an input"). The only option to make such work correctly would be to ...

Review void cf_check vmx_do_resume(void) · line 1972

Quoted 1 line
(quoted diff) +1 -0
+ :
Quoted 8 lines

... add a "memory" clobber here. Which may have other unwanted side effects.

xen/arch/x86/hvm/vmx/vmcs.c void cf_check vmx_do_resume(void) · line 1974

Quoted 2 lines
xen/arch/x86/hvm/vmx/vmcs.c +2 -0
+ : out );
+out:

Nit (here and elsewhere): Labels indented by at least one blank please. See ./CODING_STYLE.

Overlooked formatting change.

Quoted 108 lines
Quoted 20 lines
xen/arch/x86/hvm/vmx/vmcs.c +5 -4
+ return value;
 }
 
-#define vmr16(fld) ({ \
+#define vmread16(fld) ({ \
 BUILD_BUG_ON((fld) & 0x6001); \
- (uint16_t)vmr(fld); \
+ (uint16_t)vmread(fld); \
 })
 
-#define vmr32(fld) ({ \
+#define vmread32(fld) ({ \
 BUILD_BUG_ON(((fld) & 0x6001) != 0x4000); \
- (uint32_t)vmr(fld); \
+ (uint32_t)vmread(fld); \
 })
 
 static void vmx_dump_sel(const char *name, uint32_t selector)
 {

xen/arch/x86/hvm/vmx/vmcs.c void cf_check vmx_do_resume(void) · line 1995

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmcs.c +4 -4
 uint32_t sel, attr, limit;
 uint64_t base;
- sel = vmr(selector);
- attr = vmr(selector + (GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR));
- limit = vmr(selector + (GUEST_ES_LIMIT - GUEST_ES_SELECTOR));
- base = vmr(selector + (GUEST_ES_BASE - GUEST_ES_SELECTOR));
+ sel = vmread(selector);
+ attr = vmread(selector + (GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR));
+ limit = vmread(selector + (GUEST_ES_LIMIT - GUEST_ES_SELECTOR));
+ base = vmread(selector + (GUEST_ES_BASE - GUEST_ES_SELECTOR));

The renaming causes entirely unnecessary extra churn here (and of course elsewhere). The patch is already big enough without this.

xen/arch/x86/include/asm/domain.h struct arch_vcpu · line 599

Quoted 9 lines
xen/arch/x86/include/asm/domain.h +1 -1
@@ -595,7 +595,7 @@ struct arch_vcpu
 
 /* Debug registers. /
 unsigned long dr[4];
- unsigned long dr7; / Ideally int, but __vmread() needs long. /
+ unsigned long dr7; / Ideally int, but vmread_unsafe() needs unsigned long. */
 unsigned int dr6;

If you left this comment alone, all would be (largely) fine - this particular aspect could then be tidied in a follow-on path. But vmread_unsafe() specifically does not need "unsigned long" anymore. The issue was with __vmread() taking a pointer argument.

Quoted 12 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +1 -4
@@ -320,16 +320,40 @@ static always_inline void __vmpclear(u64 addr)
 BUG();
 }
 
-static always_inline void __vmread(unsigned long field, unsigned long value)
+static always_inline unsigned long vmread_unsafe(unsigned long field)
 {
- asm volatile ( "vmread %1, %0\n\t"
- / CF==1 or ZF==1 --> BUG() */
- UNLIKELY_START(be, vmread)

xen/arch/x86/include/asm/hvm/vmx/vmx.h static always_inline void __vmpclear(u64 addr) · line 329

Quoted 10 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +5 -5
- _ASM_BUGFRAME_TEXT(0)
- UNLIKELY_END_SECTION
- : "=rm" (*value)
- : "r" (field),
- _ASM_BUGFRAME_INFO(BUGFRAME_bug, LINE, FILE, 0) );
+ unsigned long value;
+
+ asm volatile ( "vmread %[field], %[value]\n\t"
+ "jc 1f\n\t"
+ "jz 1f\n\t"

Why not JBE as it was before?

xen/arch/x86/include/asm/hvm/vmx/vmx.h static always_inline void __vmpclear(u64 addr) · line 333

Quoted 4 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +4 -0
+ "jmp 2f\n\t"
+ "1:\n\t"
+ " ud2\n\t"
+ "2:"

This is specifically why we used UNLIKELY_*() before. There's no justification whatsoever in the description for the dropping of its use here.

Plus - where did _ASM_BUGFRAME_TEXT(0) go? A bare UD2 isn't acceptable, as it won't be possible to associate it back with the respective source line.

Quoted 4 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +4 -0
+ : [value] "=rm" (value)
+ : [field] "r" (field) );
+
+ return value;

xen/arch/x86/include/asm/hvm/vmx/vmx.h static always_inline void __vmpclear(u64 addr) · line 347

Quoted 10 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +10 -0
+}
+
+static inline enum vmx_insn_errno vmread_safe(unsigned long field,
+ unsigned long *value)
+{
+ asm goto ( "vmread %[field], %[value]\n\t"
+ "jc %l[vmfail_invalid]\n\t"
+ "jz %l[vmfail_error]"
+ :
+ : [field] "r" (field), [value] "m" (*value)

See comments on the vmr() adjustments you're making.

Jan

#4 lore raw
In reply to [email protected]
Quoted 17 lines · On 08/04/2025 2:15 am, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Use asm goto() in vmread_safe() to simplify the error handling logic.

Update __vmread() to return unsigned long as per suggestion in [1]. Rename __vmread() to vmread_unsafe() to match the behavior. Update all call sites everywhere. Drop UNLIKELY_*().

Group all vmread*() calls close to each other in vmx.h

Rename internal vmr*() to vmread*().

[1] https://lore.kernel.org/xen-devel/[email protected]/

---
Link to CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/1756781092

Quoted 10 lines
---
 xen/arch/x86/cpu/vpmu_intel.c          |   3 +-
 xen/arch/x86/hvm/vmx/intr.c            |  26 +--
 xen/arch/x86/hvm/vmx/realmode.c        |   2 +-
 xen/arch/x86/hvm/vmx/vmcs.c            | 160 ++++++++++---------
 xen/arch/x86/hvm/vmx/vmx.c             | 209 +++++++++++--------------
 xen/arch/x86/hvm/vmx/vvmx.c            |  43 +++--
 xen/arch/x86/include/asm/domain.h      |   2 +-
 xen/arch/x86/include/asm/hvm/vmx/vmx.h |  69 ++++----
 8 files changed, 235 insertions(+), 279 deletions(-)

This is why I suggested not to convert everything in one go.  It's now a patch doing multiple complicated things, and is proportionally harder to review.

For everyone in public, it is especially daft that we have __vmread() which is void and (if it doesn't BUG()) will pass it's return value by pointer.  It leads to very unergonomic logic.

Start by just implementing vmread(), and updating __vmread() and vmwrite_safe() to use it.  You cannot use asm goto() for vmread() because of the no-outputs constraint that we still need to follow.

Then, in a separate patch, you can do simple conversion such as ...

Quoted 6 lines
xen/arch/x86/cpu/vpmu_intel.c +0 -0
index 7ce98ee42e..9c93d1f28c 100644

xen/arch/x86/cpu/vpmu_intel.c static int cf_check core2_vpmu_do_interrupt(void) · line 802

Quoted 9 lines
xen/arch/x86/cpu/vpmu_intel.c +1 -2
@@ -796,8 +796,7 @@ static int cf_check core2_vpmu_do_interrupt(void)
     else
     {
         /* No PMC overflow but perhaps a Trace Message interrupt. */
-        __vmread(GUEST_IA32_DEBUGCTL, &msr_content);
-        if ( !(msr_content & IA32_DEBUGCTLMSR_TR) )
+        if ( !(vmread_unsafe(GUEST_IA32_DEBUGCTL) & IA32_DEBUGCTLMSR_TR) )
             return 0;
     }
 

... this to vmread().

Splitting the patch makes a substantial difference to review-ability, because patch 1 is "is this new helper implemented correctly?", and patch 2 is "is this boilerplate rearrangement no overall change?".

For vmr(), I'd start by just wrapping vmread().  It's debugging logic where brevity is important.

xen/arch/x86/include/asm/domain.h struct arch_vcpu · line 599

Quoted 11 lines
xen/arch/x86/include/asm/domain.h +1 -1
index 6b877e33a1..ffe9acd75d 100644
@@ -595,7 +595,7 @@ struct arch_vcpu
 
     /* Debug registers. */
     unsigned long dr[4];
-    unsigned long dr7; /* Ideally int, but __vmread() needs long. */
+    unsigned long dr7; /* Ideally int, but vmread_unsafe() needs unsigned long. */
     unsigned int dr6;

This comment was left as a hint, and you've just addressed the problem forcing it to stay unsigned long.

Changing dr7 should be in a separate patch too.

~Andrew