Skip to contentSkip to conversation

x86/vmx: correctly gather gs_shadow value for current vCPU

PATCH In review Tamas K Lengyel 5 participants 9 messages May 1, 2019 – May 11, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/vmx: correctly gather gs_shadow value for current vCPU

Currently the gs_shadow value is only cached when the vCPU is being scheduled out by Xen. Reporting this (usually) stale value through vm_event is incorrect, since it doesn't represent the actual state of the vCPU at the time the event was recorded. This prevents vm_event subscribers from correctly finding kernel structures in the guest when it is trapped while in ring3.

Refresh shadow_gs value when the context being saved is for the current vCPU.

Cc Razvan Cojocaru <[email protected]>
Cc Jun Nakajima <[email protected]>
Cc Kevin Tian <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
Cc Wei Liu <[email protected]>
Cc Roger Pau Monne <[email protected]>

---
v2: move fix to hvm so vm_event doesn't have to know specifics

---
 xen/arch/x86/hvm/vmx/vmx.c | 5 +++++
 1 file changed, 5 insertions(+)
xen/arch/x86/hvm/vmx/vmx.c +5 -0
index 283eb7b34d..5154ecc2a8 100644
@@ -779,12 +779,17 @@ static void vmx_load_cpu_state(struct vcpu *v, struct hvm_hw_cpu *data)
 
 static void vmx_save_vmcs_ctxt(struct vcpu *v, struct hvm_hw_cpu *ctxt)
 {
+    if ( v == current )
+        vmx_save_guest_msrs(v);
+
     vmx_save_cpu_state(v, ctxt);
     vmx_vmcs_save(v, ctxt);
 }
 
 static int vmx_load_vmcs_ctxt(struct vcpu *v, struct hvm_hw_cpu *ctxt)
 {
+    ASSERT(v != current);
+
     vmx_load_cpu_state(v, ctxt);
 
     if ( vmx_vmcs_restore(v, ctxt) )
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v2 1/1
#1 lore raw
Quoted 27 lines · Tamas K Lengyel

Currently the gs_shadow value is only cached when the vCPU is being scheduled out by Xen. Reporting this (usually) stale value through vm_event is incorrect, since it doesn't represent the actual state of the vCPU at the time the event was recorded. This prevents vm_event subscribers from correctly finding kernel structures in the guest when it is trapped while in ring3.

Refresh shadow_gs value when the context being saved is for the current vCPU.

Cc Razvan Cojocaru <[email protected]>
Cc Jun Nakajima <[email protected]>
Cc Kevin Tian <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
Cc Wei Liu <[email protected]>
Cc Roger Pau Monne <[email protected]>

---
v2: move fix to hvm so vm_event doesn't have to know specifics

---
 xen/arch/x86/hvm/vmx/vmx.c | 5 +++++
 1 file changed, 5 insertions(+)
xen/arch/x86/hvm/vmx/vmx.c +0 -0
index 283eb7b34d..5154ecc2a8 100644

xen/arch/x86/hvm/vmx/vmx.c static void vmx_load_cpu_state(struct vcpu *v, struct hvm_hw_cpu *data) · line 783

Quoted 5 lines
xen/arch/x86/hvm/vmx/vmx.c +2 -0
@@ -779,12 +779,17 @@ static void vmx_load_cpu_state(struct vcpu *v, struct hvm_hw_cpu *data)
 
 static void vmx_save_vmcs_ctxt(struct vcpu *v, struct hvm_hw_cpu *ctxt)
 {
+    if ( v == current )
+        vmx_save_guest_msrs(v);

vmx_save_guest_msrs() is simple enough that the if is probably not necessary here (we can just call the function directly, regardless of what v holds).

But that's not technically an issue, so if nobody else minds:

Thanks,
Razvan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 02.05.19 at 08:20, <[email protected]> wrote:
Quoted 4 lines · Tamas K Lengyel
xen/arch/x86/hvm/vmx/vmx.c +0 -0
@@ -779,12 +779,17 @@ static void vmx_load_cpu_state(struct vcpu *v, struct hvm_hw_cpu *data)
 
Quoted 8 lines

xen/arch/x86/hvm/vmx/vmx.c static void vmx_load_cpu_state(struct vcpu *v, struct hvm_hw_cpu *data) · line 783

Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c +2 -0
 static void vmx_save_vmcs_ctxt(struct vcpu *v, struct hvm_hw_cpu *ctxt)
 {
+    if ( v == current )
+        vmx_save_guest_msrs(v);

vmx_save_guest_msrs() is simple enough that the if is probably not necessary here (we can just call the function directly, regardless of what v holds).

Avoiding an MSR access is perhaps worth the conditional.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 15 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 02.05.19 at 08:20, <[email protected]> wrote:
Quoted 4 lines · Tamas K Lengyel
xen/arch/x86/hvm/vmx/vmx.c +0 -0
@@ -779,12 +779,17 @@ static void vmx_load_cpu_state(struct vcpu *v, struct hvm_hw_cpu *data)
 
Quoted 8 lines

xen/arch/x86/hvm/vmx/vmx.c static void vmx_load_cpu_state(struct vcpu *v, struct hvm_hw_cpu *data) · line 783

Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c +2 -0
  static void vmx_save_vmcs_ctxt(struct vcpu *v, struct hvm_hw_cpu *ctxt)
  {
+    if ( v == current )
+        vmx_save_guest_msrs(v);

vmx_save_guest_msrs() is simple enough that the if is probably not necessary here (we can just call the function directly, regardless of what v holds).

Quoted 1 line

Avoiding an MSR access is perhaps worth the conditional.

Fair enough.

Thanks,
Razvan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
In reply to Razvan Cojocaru
Quoted 29 lines · Razvan Cojocaru
Quoted 28 lines · Tamas K Lengyel

Currently the gs_shadow value is only cached when the vCPU is being scheduled out by Xen. Reporting this (usually) stale value through vm_event is incorrect, since it doesn't represent the actual state of the vCPU at the time the event was recorded. This prevents vm_event subscribers from correctly finding kernel structures in the guest when it is trapped while in ring3.

Refresh shadow_gs value when the context being saved is for the current vCPU.

Cc Razvan Cojocaru <[email protected]>
Cc Jun Nakajima <[email protected]>
Cc Kevin Tian <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
Cc Wei Liu <[email protected]>
Cc Roger Pau Monne <[email protected]>

---
v2: move fix to hvm so vm_event doesn't have to know specifics

---
 xen/arch/x86/hvm/vmx/vmx.c | 5 +++++
 1 file changed, 5 insertions(+)
xen/arch/x86/hvm/vmx/vmx.c +0 -0
index 283eb7b34d..5154ecc2a8 100644
@@ -779,12 +779,17 @@ static void vmx_load_cpu_state(struct vcpu *v, struct hvm_hw_cpu *data)
 
Quoted 7 lines

xen/arch/x86/hvm/vmx/vmx.c static void vmx_load_cpu_state(struct vcpu *v, struct hvm_hw_cpu *data) · line 783

Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c +2 -0
 static void vmx_save_vmcs_ctxt(struct vcpu *v, struct hvm_hw_cpu *ctxt)
 {
+    if ( v == current )
+        vmx_save_guest_msrs(v);

vmx_save_guest_msrs() is simple enough that the if is probably not necessary here (we can just call the function directly, regardless of what v holds).

Why?  Doing that would fully corrupt v's state when called in remote context, as you'd clobber v's gs_shadow which whatever the value was from the current vcpu.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
Quoted 38 lines · Andrew Cooper
Quoted 29 lines · Razvan Cojocaru
Quoted 28 lines · Tamas K Lengyel

Currently the gs_shadow value is only cached when the vCPU is being scheduled out by Xen. Reporting this (usually) stale value through vm_event is incorrect, since it doesn't represent the actual state of the vCPU at the time the event was recorded. This prevents vm_event subscribers from correctly finding kernel structures in the guest when it is trapped while in ring3.

Refresh shadow_gs value when the context being saved is for the current vCPU.

Cc Razvan Cojocaru <[email protected]>
Cc Jun Nakajima <[email protected]>
Cc Kevin Tian <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
Cc Wei Liu <[email protected]>
Cc Roger Pau Monne <[email protected]>

---
v2: move fix to hvm so vm_event doesn't have to know specifics

---
  xen/arch/x86/hvm/vmx/vmx.c | 5 +++++
  1 file changed, 5 insertions(+)
xen/arch/x86/hvm/vmx/vmx.c +0 -0
index 283eb7b34d..5154ecc2a8 100644
@@ -779,12 +779,17 @@ static void vmx_load_cpu_state(struct vcpu *v, struct hvm_hw_cpu *data)
 
Quoted 7 lines

xen/arch/x86/hvm/vmx/vmx.c static void vmx_load_cpu_state(struct vcpu *v, struct hvm_hw_cpu *data) · line 783

Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c +2 -0
  static void vmx_save_vmcs_ctxt(struct vcpu *v, struct hvm_hw_cpu *ctxt)
  {
+    if ( v == current )
+        vmx_save_guest_msrs(v);

vmx_save_guest_msrs() is simple enough that the if is probably not necessary here (we can just call the function directly, regardless of what v holds).

Quoted 3 lines

Why?  Doing that would fully corrupt v's state when called in remote context, as you'd clobber v's gs_shadow which whatever the value was from the current vcpu.

Good point, I've missed that.

Thanks,
Razvan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw
In reply to Tamas K Lengyel
Quoted 8 lines · Tamas K Lengyel
xen/arch/x86/hvm/vmx/vmx.c +0 -0
index 283eb7b34d..5154ecc2a8 100644
@@ -779,12 +779,17 @@ static void vmx_load_cpu_state(struct vcpu *v, struct hvm_hw_cpu *data)
 
 static void vmx_save_vmcs_ctxt(struct vcpu *v, struct hvm_hw_cpu *ctxt)
 {

xen/arch/x86/hvm/vmx/vmx.c static void vmx_load_cpu_state(struct vcpu *v, struct hvm_hw_cpu *data) · line 791

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +4 -0
+    if ( v == current )
+        vmx_save_guest_msrs(v);
+
     vmx_save_cpu_state(v, ctxt);
     vmx_vmcs_save(v, ctxt);
 }
 
 static int vmx_load_vmcs_ctxt(struct vcpu *v, struct hvm_hw_cpu *ctxt)
 {
+    ASSERT(v != current);

I'd leave a comment along the lines of /* Not currently safe to use in current context. */

Can be fixed up on commit.

This version is much cleaner, architecturally speaking, so Reviewed-by:
Andrew Cooper <[email protected]>

I'll drop the previous version out of x86-next.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw
Quoted 24 lines · Andrew Cooper
Quoted 8 lines · Tamas K Lengyel
xen/arch/x86/hvm/vmx/vmx.c +0 -0
index 283eb7b34d..5154ecc2a8 100644
@@ -779,12 +779,17 @@ static void vmx_load_cpu_state(struct vcpu *v, struct hvm_hw_cpu *data)
 
 static void vmx_save_vmcs_ctxt(struct vcpu *v, struct hvm_hw_cpu *ctxt)
 {

xen/arch/x86/hvm/vmx/vmx.c static void vmx_load_cpu_state(struct vcpu *v, struct hvm_hw_cpu *data) · line 791

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +4 -0
+    if ( v == current )
+        vmx_save_guest_msrs(v);
+
     vmx_save_cpu_state(v, ctxt);
     vmx_vmcs_save(v, ctxt);
 }
 
 static int vmx_load_vmcs_ctxt(struct vcpu *v, struct hvm_hw_cpu *ctxt)
 {
+    ASSERT(v != current);

I'd leave a comment along the lines of /* Not currently safe to use in current context. */

Quoted 6 lines

Can be fixed up on commit.

This version is much cleaner, architecturally speaking, so Reviewed-by:
Andrew Cooper <[email protected]>

I'll drop the previous version out of x86-next.

Thanks,
Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Acked-by on v2 1/1
#8 lore raw
In reply to Tamas K Lengyel
Quoted 11 lines

From: Tamas K Lengyel [mailto:[email protected]]
Sent: Thursday, May 2, 2019 7:52 AM

Currently the gs_shadow value is only cached when the vCPU is being scheduled
out by Xen. Reporting this (usually) stale value through vm_event is incorrect, since it doesn't represent the actual state of the vCPU at the time the event was recorded. This prevents vm_event subscribers from correctly finding kernel
structures in the guest when it is trapped while in ring3.

Quoted 4 lines

Refresh shadow_gs value when the context being saved is for the current vCPU.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel