Skip to contentSkip to conversation

x86emul: allow ->write_msr() to distinguish origins of writes

PATCH In review Jan Beulich 3 participants 5 messages Jan 27 – Jan 30

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86emul: allow ->write_msr() to distinguish origins of writes

Only explicit writes are subject to e.g. the checking of the MSR intercept bitmap, which extends to VM-event's hvm_monitor_msr(). Indicate, by way of a new boolean parameter, to the hook functions which variant it is.

Fixes 6eb43fcf8a0b ("x86emul: support SWAPGS")

---
Later, in particular for nested, ->read_msr() may need to gain a similar parameter.

Prior to nested making use of it, the new parameter is effectively dead code with VM_EVENT=n. If we accepted Misra rule 2.2, something would likely need doing about this.

I've suitably re-based "x86emul: misc additions" on top of this, but I don't think I'll re-post it just for that.

tools/fuzz/x86_instruction_emulator/fuzz-emul.c +2 -1
@@ -569,7 +569,8 @@ static int fuzz_read_msr(
 static int fuzz_write_msr(
     unsigned int reg,
     uint64_t val,
-    struct x86_emulate_ctxt *ctxt)
+    struct x86_emulate_ctxt *ctxt,
+    bool explicit)
 {
     struct fuzz_state *s = ctxt->data;
     struct fuzz_corpus *c = s->corpus;
xen/arch/x86/hvm/emulate.c +5 -3
@@ -1705,7 +1705,8 @@ static int cf_check hvmemul_write_io_dis
 static int cf_check hvmemul_write_msr_discard(
     unsigned int reg,
     uint64_t val,
-    struct x86_emulate_ctxt *ctxt)
+    struct x86_emulate_ctxt *ctxt,
+    bool explicit)
 {
     return X86EMUL_OKAY;
 }
@@ -2427,9 +2428,10 @@ static int cf_check hvmemul_read_msr(
 static int cf_check hvmemul_write_msr(
     unsigned int reg,
     uint64_t val,
-    struct x86_emulate_ctxt *ctxt)
+    struct x86_emulate_ctxt *ctxt,
+    bool explicit)
 {
-    int rc = hvm_msr_write_intercept(reg, val, true);
+    int rc = hvm_msr_write_intercept(reg, val, explicit);
 
     if ( rc == X86EMUL_EXCEPTION )
         x86_emul_hw_exception(X86_EXC_GP, 0, ctxt);
xen/arch/x86/pv/emul-priv-op.c +2 -1
@@ -1038,7 +1038,8 @@ static int cf_check read_msr(
 }
 
 static int cf_check write_msr(
-    unsigned int reg, uint64_t val, struct x86_emulate_ctxt *ctxt)
+    unsigned int reg, uint64_t val, struct x86_emulate_ctxt *ctxt,
+    bool explicit)
 {
     struct vcpu *curr = current;
     const struct domain *currd = curr->domain;
xen/arch/x86/x86_emulate/0f01.c +3 -3
@@ -40,7 +40,7 @@ int x86emul_0f01(struct x86_emulate_stat
             fail_if(!ops->write_msr);
             rc = ops->write_msr(regs->ecx,
                                 ((uint64_t)regs->r(dx) << 32) | regs->eax,
-                                ctxt);
+                                ctxt, true);
             goto done;
         }
         generate_exception(X86_EXC_UD);
@@ -194,7 +194,7 @@ int x86emul_0f01(struct x86_emulate_stat
              (rc = ops->read_msr(MSR_SHADOW_GS_BASE, &msr_val,
                                  ctxt)) != X86EMUL_OKAY ||
              (rc = ops->write_msr(MSR_SHADOW_GS_BASE, sreg.base,
-                                  ctxt)) != X86EMUL_OKAY )
+                                  ctxt, false)) != X86EMUL_OKAY )
             goto done;
         sreg.base = msr_val;
         if ( (rc = ops->write_segment(x86_seg_gs, &sreg,
@@ -202,7 +202,7 @@ int x86emul_0f01(struct x86_emulate_stat
         {
             /* Best effort unwind (i.e. no real error checking). */
             if ( ops->write_msr(MSR_SHADOW_GS_BASE, msr_val,
-                                ctxt) == X86EMUL_EXCEPTION )
+                                ctxt, false) == X86EMUL_EXCEPTION )
                 x86_emul_reset_event(ctxt);
             goto done;
         }
xen/arch/x86/x86_emulate/x86_emulate.c +1 -1
@@ -3814,7 +3814,7 @@ x86_emulate(
         fail_if(ops->write_msr == NULL);
         if ( (rc = ops->write_msr(_regs.ecx,
                                   ((uint64_t)_regs.r(dx) << 32) | _regs.eax,
-                                  ctxt)) != 0 )
+                                  ctxt, true)) != 0 )
             goto done;
         break;
 
xen/arch/x86/x86_emulate/x86_emulate.h +5 -3
@@ -470,13 +470,15 @@ struct x86_emulate_ops
         struct x86_emulate_ctxt *ctxt);
 
     /*
-     * write_dr: Write to model-specific register.
-     *  @reg:   [IN ] Register to write.
+     * write_msr: Write to model-specific register.
+     *  @reg:      [IN ] Register to write.
+     *  @explicit: [IN ] Whether this is an explicit WRMSR or alike.
      */
     int (*write_msr)(
         unsigned int reg,
         uint64_t val,
-        struct x86_emulate_ctxt *ctxt);
+        struct x86_emulate_ctxt *ctxt,
+        bool explicit);
 
     /*
      * cache_op: Write-back and/or invalidate cache contents.
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Jan Beulich

Only explicit writes are subject to e.g. the checking of the MSR intercept bitmap, which extends to VM-event's hvm_monitor_msr(). Indicate, by way of a new boolean parameter, to the hook functions which variant it is.

Fixes 6eb43fcf8a0b ("x86emul: support SWAPGS")
Acked-by on v1 1/1
#2 lore raw
In reply to Jan Beulich
Quoted 7 lines · Jan Beulich

Only explicit writes are subject to e.g. the checking of the MSR intercept bitmap, which extends to VM-event's hvm_monitor_msr(). Indicate, by way of a new boolean parameter, to the hook functions which variant it is.

Fixes 6eb43fcf8a0b ("x86emul: support SWAPGS")
Quoted 7 lines

---
Later, in particular for nested, ->read_msr() may need to gain a similar parameter.

Prior to nested making use of it, the new parameter is effectively dead code with VM_EVENT=n. If we accepted Misra rule 2.2, something would likely need doing about this.

Hm, yes, it propagates into hvm_msr_write_intercept() which then turns into if ( may_defer && false ) in the VM_EVENT=n. But then you could say the same about the code inside the if block above for the VM_EVENT=n, it's also effectively unreachable code.

Quoted 28 lines

I've suitably re-based "x86emul: misc additions" on top of this, but I don't think I'll re-post it just for that.

tools/fuzz/x86_instruction_emulator/fuzz-emul.c +2 -1
@@ -569,7 +569,8 @@ static int fuzz_read_msr(
 static int fuzz_write_msr(
     unsigned int reg,
     uint64_t val,
-    struct x86_emulate_ctxt *ctxt)
+    struct x86_emulate_ctxt *ctxt,
+    bool explicit)
 {
     struct fuzz_state *s = ctxt->data;
     struct fuzz_corpus *c = s->corpus;
xen/arch/x86/hvm/emulate.c +2 -1
@@ -1705,7 +1705,8 @@ static int cf_check hvmemul_write_io_dis
 static int cf_check hvmemul_write_msr_discard(
     unsigned int reg,
     uint64_t val,
-    struct x86_emulate_ctxt *ctxt)
+    struct x86_emulate_ctxt *ctxt,
+    bool explicit)
 {
     return X86EMUL_OKAY;
 }

xen/arch/x86/hvm/emulate.c static int cf_check hvmemul_read_msr( · line 2434

Quoted 9 lines
xen/arch/x86/hvm/emulate.c +3 -2
@@ -2427,9 +2428,10 @@ static int cf_check hvmemul_read_msr(
 static int cf_check hvmemul_write_msr(
     unsigned int reg,
     uint64_t val,
-    struct x86_emulate_ctxt *ctxt)
+    struct x86_emulate_ctxt *ctxt,
+    bool explicit)
 {
-    int rc = hvm_msr_write_intercept(reg, val, true);
+    int rc = hvm_msr_write_intercept(reg, val, explicit);

I've wondered whether we also want to rename the parameter of hvm_msr_write_intercept() into something different than may_defer. It feels weird to translate a parameter that denotes an explicit MSR access into one that signals whether it's fine to defer the operation or not.

Thanks, Roger.

#3 lore raw
Quoted 10 lines · Roger Pau Monné
Quoted 7 lines · Jan Beulich

Only explicit writes are subject to e.g. the checking of the MSR intercept bitmap, which extends to VM-event's hvm_monitor_msr(). Indicate, by way of a new boolean parameter, to the hook functions which variant it is.

Fixes 6eb43fcf8a0b ("x86emul: support SWAPGS")

Thanks.

Quoted 4 lines
Quoted 4 lines

---
Later, in particular for nested, ->read_msr() may need to gain a similar parameter.

Quoted 8 lines
Quoted 3 lines

Prior to nested making use of it, the new parameter is effectively dead code with VM_EVENT=n. If we accepted Misra rule 2.2, something would likely need doing about this.

Hm, yes, it propagates into hvm_msr_write_intercept() which then turns into if ( may_defer && false ) in the VM_EVENT=n. But then you could say the same about the code inside the if block above for the VM_EVENT=n, it's also effectively unreachable code.

Unreachable and dead code are different things to Misra, though. It is my understanding that we deliberately permit constructs reducing to "if (0)" in certain configurations, relying on DCE: There's then no generated code for the construct, and hence nothing that cannot be reached. The situation is different for a parameter that has no use: Its removal (in the specific configuration) wouldn't alter the behavior. Hence the parameter and all arguments passed for it are "dead".

Quoted 8 lines
Quoted 8 lines
xen/arch/x86/hvm/emulate.c +2 -1
@@ -2427,9 +2428,10 @@ static int cf_check hvmemul_read_msr(
 static int cf_check hvmemul_write_msr(
     unsigned int reg,
     uint64_t val,
-    struct x86_emulate_ctxt *ctxt)
+    struct x86_emulate_ctxt *ctxt,
+    bool explicit)
 {
Quoted 8 lines

xen/arch/x86/hvm/emulate.c static int cf_check hvmemul_read_msr( · line 2434

Quoted 2 lines
xen/arch/x86/hvm/emulate.c +1 -1
-    int rc = hvm_msr_write_intercept(reg, val, true);
+    int rc = hvm_msr_write_intercept(reg, val, explicit);

I've wondered whether we also want to rename the parameter of hvm_msr_write_intercept() into something different than may_defer. It feels weird to translate a parameter that denotes an explicit MSR access into one that signals whether it's fine to defer the operation or not.

I did think the same, just that - considering all use sites - I couldn't even come close to thinking of some sensible new name.

Jan

#4 lore raw
Quoted 27 lines · Jan Beulich
Quoted 10 lines · Roger Pau Monné
Quoted 7 lines · Jan Beulich

Only explicit writes are subject to e.g. the checking of the MSR intercept bitmap, which extends to VM-event's hvm_monitor_msr(). Indicate, by way of a new boolean parameter, to the hook functions which variant it is.

Fixes 6eb43fcf8a0b ("x86emul: support SWAPGS")

Thanks.

Quoted 4 lines
Quoted 4 lines

---
Later, in particular for nested, ->read_msr() may need to gain a similar parameter.

Quoted 8 lines
Quoted 3 lines

Prior to nested making use of it, the new parameter is effectively dead code with VM_EVENT=n. If we accepted Misra rule 2.2, something would likely need doing about this.

Hm, yes, it propagates into hvm_msr_write_intercept() which then turns into if ( may_defer && false ) in the VM_EVENT=n. But then you could say the same about the code inside the if block above for the VM_EVENT=n, it's also effectively unreachable code.

Quoted 7 lines

Unreachable and dead code are different things to Misra, though. It is my understanding that we deliberately permit constructs reducing to "if (0)" in certain configurations, relying on DCE: There's then no generated code for the construct, and hence nothing that cannot be reached. The situation is different for a parameter that has no use: Its removal (in the specific configuration) wouldn't alter the behavior. Hence the parameter and all arguments passed for it are "dead".

Yeah, I think dead is a good definition, the variable is possible evaluated, but it's value doesn't change the flow of execution in the VM_EVENT=n case.

Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
xen/arch/x86/hvm/emulate.c +2 -1
@@ -2427,9 +2428,10 @@ static int cf_check hvmemul_read_msr(
 static int cf_check hvmemul_write_msr(
     unsigned int reg,
     uint64_t val,
-    struct x86_emulate_ctxt *ctxt)
+    struct x86_emulate_ctxt *ctxt,
+    bool explicit)
 {
Quoted 8 lines

xen/arch/x86/hvm/emulate.c static int cf_check hvmemul_read_msr( · line 2434

Quoted 2 lines
xen/arch/x86/hvm/emulate.c +1 -1
-    int rc = hvm_msr_write_intercept(reg, val, true);
+    int rc = hvm_msr_write_intercept(reg, val, explicit);

I've wondered whether we also want to rename the parameter of hvm_msr_write_intercept() into something different than may_defer. It feels weird to translate a parameter that denotes an explicit MSR access into one that signals whether it's fine to defer the operation or not.

Quoted 2 lines

I did think the same, just that - considering all use sites - I couldn't even come close to thinking of some sensible new name.

Let's leave as-is then. Since maybe_defer is tied to the monitor logic it might be helpful to give it that meaning, but I'm having a hard time coming with a proper way to name it that's not too verbose. Let's leave as-is for now then.

Thanks, Roger.