x86/vm_event: correctly gather gs_shadow value
PATCH 1/1 x86/vm_event: correctly gather gs_shadow value
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.
--- xen/arch/x86/vm_event.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/vm_event.c +1 -1
@@ -239,7 +239,7 @@ void vm_event_fill_regs(vm_event_request_t *req)vm_event_pack_segment_register (x86_seg_ds, &req->data.regs.x86);vm_event_pack_segment_register (x86_seg_es, &req->data.regs.x86); - req->data.regs.x86.shadow_gs = ctxt.shadow_gs; + req->data.regs.x86.shadow_gs =rdgsshadow (); req->data.regs.x86.dr6 = ctxt.dr6;#endif }
Signature · 7 lines
2.20.1 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 5 lines · Tamas K Lengyel
Currently the
gs_shadowvalue is only cached when the vCPU is being scheduled out by Xen. Reporting this (usually) stale value throughvm_eventis incorrect, since it doesn't represent the actual state of the vCPU at the time the event was recorded. This preventsvm_eventsubscribers from correctly finding kernel structures in the guest when it is trapped while in ring3.
Isn't the VCPU always scheduled out (since it is paused) with sync vm_events? Is this an async fix only?
In any case,
Thanks,
Razvan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Andrew Cooper
Reviewed-byQuoted 8 lines · Razvan Cojocaru
Quoted 5 lines · Tamas K Lengyel
Currently the
gs_shadowvalue is only cached when the vCPU is being scheduled out by Xen. Reporting this (usually) stale value throughvm_eventis incorrect, since it doesn't represent the actual state of the vCPU at the time the event was recorded. This preventsvm_eventsubscribers from correctly finding kernel structures in the guest when it is trapped while in ring3.Isn't the VCPU always scheduled out (since it is paused) with sync
vm_events? Is this an async fix only?
Paused isn't always scheduled out. It can be half-idle with state still in hardware.
Quoted 4 lines
In any case,
Acked-by Razvan Cojocaru <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
xen/arch/x86/vm_event.c
Quoted 20 lines · Tamas K Lengyel
Currently the
gs_shadowvalue is only cached when the vCPU is being scheduled out by Xen. Reporting this (usually) stale value throughvm_eventis incorrect, since it doesn't represent the actual state of the vCPU at the time the event was recorded. This preventsvm_eventsubscribers from correctly finding kernel structures in the guest when it is trapped while in ring3.Signed-off-by Tamas K Lengyel <[email protected]>Cc Razvan Cojocaru <[email protected]>Cc Jan Beulich <[email protected]>Cc Andrew Cooper <[email protected]>Cc Wei Liu <[email protected]>Cc Roger Pau Monne <[email protected]>--- xen/arch/x86/vm_event.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)xen/arch/x86/vm_event.c +0
-0
Actually, come to think of it, the same is true for the SYSENTER details, which by default are read/write to the guest while it is scheduled. As a result, the details reported here will from the last vcpu context switch, and possibly stale.
It might be worth introducing a "sync state from hw" hook which collects all the data we intend to pass to the introspection agent.
~Andrew#
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 18 lines · Andrew Cooper
Quoted 16 lines · Tamas K Lengyel
Currently the
gs_shadowvalue is only cached when the vCPU is being scheduled out by Xen. Reporting this (usually) stale value throughvm_eventis incorrect, since it doesn't represent the actual state of the vCPU at the time the event was recorded. This preventsvm_eventsubscribers from correctly finding kernel structures in the guest when it is trapped while in ring3.Signed-off-by Tamas K Lengyel <[email protected]>Cc Razvan Cojocaru <[email protected]>Cc Jan Beulich <[email protected]>Cc Andrew Cooper <[email protected]>Cc Wei Liu <[email protected]>Cc Roger Pau Monne <[email protected]>--- xen/arch/x86/vm_event.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 9 lines
xen/arch/x86/vm_event.c
Quoted 4 lines
xen/arch/x86/vm_event.c +0
-0Actually, come to think of it, the same is true for the SYSENTER details, which by default are read/write to the guest while it is scheduled. As a result, the details reported here will from the last vcpu context switch, and possibly stale.
I'll look into it.
Quoted 2 lines
It might be worth introducing a "sync state from hw" hook which collects all the data we intend to pass to the introspection agent.
You mean adding another hvm hook?
Thanks,
Tamas
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 4 lines
Quoted 2 lines
It might be worth introducing a "sync state from hw" hook which collects all the data we intend to pass to the introspection agent.
You mean adding another hvm hook?
Actually, instead of another hook I think what would make sense it to just update vmx_save_vmcs_ctxt to automatically refresh the cached register values when it's called with "v == current". Thoughts?
Tamas
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 8 lines · Tamas K Lengyel
Quoted 4 lines
Quoted 2 lines
It might be worth introducing a "sync state from hw" hook which collects all the data we intend to pass to the introspection agent.
You mean adding another hvm hook?
Actually, instead of another hook I think what would make sense it to just update
vmx_save_vmcs_ctxtto automatically refresh the cached register values when it's called with "v == current". Thoughts?
That's probably the better way to go about it, since otherwise the xc_hvm_getcontext_partial() hypercall will suffer from the same problem. (there are two ways of getting guest state: one is via the vm_event cached values, the other is via the aforementioned hypercall).
Thanks,
Razvan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 7 lines · Razvan Cojocaru
Quoted 5 lines · Tamas K Lengyel
Quoted 4 lines
Quoted 2 lines
It might be worth introducing a "sync state from hw" hook which collects all the data we intend to pass to the introspection agent.
You mean adding another hvm hook?
Quoted 8 lines
Quoted 3 lines
Actually, instead of another hook I think what would make sense it to just update
vmx_save_vmcs_ctxtto automatically refresh the cached register values when it's called with "v == current". Thoughts?That's probably the better way to go about it, since otherwise the
xc_hvm_getcontext_partial()hypercall will suffer from the same problem. (there are two ways of getting guest state: one is via thevm_eventcached values, the other is via the aforementioned hypercall).
True, although issuing the hypercall in the vm_event callback is actually fine - that's how I found the issue to begin with, since the vCPU will be scheduled out with the cached registers refreshed and thus be different then what the vm_event itself had. But other callers of the hypercall can run into the problem if the guest/vcpu is not paused.
Tamas
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 19 lines · Tamas K Lengyel
Quoted 7 lines · Razvan Cojocaru
Quoted 5 lines · Tamas K Lengyel
Quoted 4 lines
Quoted 2 lines
It might be worth introducing a "sync state from hw" hook which collects all the data we intend to pass to the introspection agent.
You mean adding another hvm hook?
Quoted 8 lines
Quoted 3 lines
Actually, instead of another hook I think what would make sense it to just update
vmx_save_vmcs_ctxtto automatically refresh the cached register values when it's called with "v == current". Thoughts?That's probably the better way to go about it, since otherwise the
xc_hvm_getcontext_partial()hypercall will suffer from the same problem. (there are two ways of getting guest state: one is via thevm_eventcached values, the other is via the aforementioned hypercall).
Quoted 6 lines
True, although issuing the hypercall in the
vm_eventcallback is actually fine - that's how I found the issue to begin with, since the vCPU will be scheduled out with the cached registers refreshed and thus be different then what thevm_eventitself had. But other callers of the hypercall can run into the problem if the guest/vcpu is not paused.
Actually, doing the "v == current" check wouldn't really do anything for the hypercall since it's not the domain issuing the hypercall on itself. The only way to ensure that hypercall is returning correct values would be to pause the vCPU.
Tamas
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 27 lines · Tamas K Lengyel
Quoted 19 lines · Tamas K Lengyel
Quoted 7 lines · Razvan Cojocaru
Quoted 5 lines · Tamas K Lengyel
Quoted 4 lines
Quoted 2 lines
It might be worth introducing a "sync state from hw" hook which collects all the data we intend to pass to the introspection agent.
You mean adding another hvm hook?
Quoted 8 lines
Quoted 3 lines
Actually, instead of another hook I think what would make sense it to just update
vmx_save_vmcs_ctxtto automatically refresh the cached register values when it's called with "v == current". Thoughts?That's probably the better way to go about it, since otherwise the
xc_hvm_getcontext_partial()hypercall will suffer from the same problem. (there are two ways of getting guest state: one is via thevm_eventcached values, the other is via the aforementioned hypercall).Quoted 6 lines
True, although issuing the hypercall in the
vm_eventcallback is actually fine - that's how I found the issue to begin with, since the vCPU will be scheduled out with the cached registers refreshed and thus be different then what thevm_eventitself had. But other callers of the hypercall can run into the problem if the guest/vcpu is not paused.
Quoted 4 lines
Actually, doing the "v == current" check wouldn't really do anything for the hypercall since it's not the domain issuing the hypercall on itself. The only way to ensure that hypercall is returning correct values would be to pause the vCPU.
I've discussed this with Andrew in the meantime and he's kindly pointed out that for the hypercall we pause from remote context, which forces a de-schedule. So the hypercall _should_ be fine. But we write data to the vm_event ring from the current context, where state might actually be in hardware.
He'll probably chime in with additional suggestions / comments.
Thanks,
Razvan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 34 lines · Razvan Cojocaru
Quoted 27 lines · Tamas K Lengyel
Quoted 19 lines · Tamas K Lengyel
Quoted 7 lines · Razvan Cojocaru
Quoted 5 lines · Tamas K Lengyel
Quoted 4 lines
Quoted 2 lines
It might be worth introducing a "sync state from hw" hook which collects all the data we intend to pass to the introspection agent.
You mean adding another hvm hook?
Quoted 8 lines
Quoted 3 lines
Actually, instead of another hook I think what would make sense it to just update
vmx_save_vmcs_ctxtto automatically refresh the cached register values when it's called with "v == current". Thoughts?That's probably the better way to go about it, since otherwise the
xc_hvm_getcontext_partial()hypercall will suffer from the same problem. (there are two ways of getting guest state: one is via thevm_eventcached values, the other is via the aforementioned hypercall).Quoted 6 lines
True, although issuing the hypercall in the
vm_eventcallback is actually fine - that's how I found the issue to begin with, since the vCPU will be scheduled out with the cached registers refreshed and thus be different then what thevm_eventitself had. But other callers of the hypercall can run into the problem if the guest/vcpu is not paused.Quoted 4 lines
Actually, doing the "v == current" check wouldn't really do anything for the hypercall since it's not the domain issuing the hypercall on itself. The only way to ensure that hypercall is returning correct values would be to pause the vCPU.
Quoted 5 lines
I've discussed this with Andrew in the meantime and he's kindly pointed out that for the hypercall we pause from remote context, which forces a de-schedule. So the hypercall _should_ be fine. But we write data to the
vm_eventring from the current context, where state might actually be in hardware.
Correct, I've went through the hypercall code and it does pause the vCPU. So right now I don't think we need anything else then what's already in this patch.
Tamas
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 30 lines · Tamas K Lengyel
Quoted 18 lines · Andrew Cooper
Quoted 16 lines · Tamas K Lengyel
Currently the
gs_shadowvalue is only cached when the vCPU is being scheduled out by Xen. Reporting this (usually) stale value throughvm_eventis incorrect, since it doesn't represent the actual state of the vCPU at the time the event was recorded. This preventsvm_eventsubscribers from correctly finding kernel structures in the guest when it is trapped while in ring3.Signed-off-by Tamas K Lengyel <[email protected]>Cc Razvan Cojocaru <[email protected]>Cc Jan Beulich <[email protected]>Cc Andrew Cooper <[email protected]>Cc Wei Liu <[email protected]>Cc Roger Pau Monne <[email protected]>--- xen/arch/x86/vm_event.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)Quoted 9 lines
xen/arch/x86/vm_event.c
Quoted 4 lines
xen/arch/x86/vm_event.c +0
-0Actually, come to think of it, the same is true for the SYSENTER details, which by default are read/write to the guest while it is scheduled. As a result, the details reported here will from the last vcpu context switch, and possibly stale.
Quoted 1 line
I'll look into it.
The sysenter values look fine to me because vmx_save_vmcs_ctxt calls vmx_vmcs_save, which refreshes the values from the actual VMCS. It's only shadow_gs that is not refreshed.
Tamas
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 27 lines · Tamas K Lengyel
Quoted 26 lines · Tamas K Lengyel
Quoted 25 lines · Andrew Cooper
xen/arch/x86/vm_event.c
Quoted 20 lines · Tamas K Lengyel
Currently the
gs_shadowvalue is only cached when the vCPU is being scheduled out by Xen. Reporting this (usually) stale value throughvm_eventis incorrect, since it doesn't represent the actual state of the vCPU at the time the event was recorded. This preventsvm_eventsubscribers from correctly finding kernel structures in the guest when it is trapped while in ring3.Signed-off-by Tamas K Lengyel <[email protected]>Cc Razvan Cojocaru <[email protected]>Cc Jan Beulich <[email protected]>Cc Andrew Cooper <[email protected]>Cc Wei Liu <[email protected]>Cc Roger Pau Monne <[email protected]>--- xen/arch/x86/vm_event.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)xen/arch/x86/vm_event.c +0
-0Actually, come to think of it, the same is true for the SYSENTER details, which by default are read/write to the guest while it is scheduled. As a result, the details reported here will from the last vcpu context switch, and possibly stale.
Quoted 4 lines
Quoted 1 line
I'll look into it.
The sysenter values look fine to me because
vmx_save_vmcs_ctxtcallsvmx_vmcs_save, which refreshes the values from the actual VMCS. It's onlyshadow_gsthat is not refreshed.
So, you are correct that we call into vmx_vmcs_save() which causes the SYSENTER details to be correct.
However, the same path also calls vmx_save_cpu_state() which saves shadow_gs, and therefore ought to DTRT for the previous use in vm_event.
The problem is that vmx_{save,load}_cpu_state() only function correctly in remote context, which is why the stale shadow_gs persists.
Contrary to what I said earlier, I now think that a better fix would be to make the above functions safe to use use in current context (at least for the save side - the restore side can probably just ASSERT() atm, as it doesn't seem to have an equivalent use).
That way, the vm_event code doesn't need to contain any context-sensitive code, which is a better overall, IMO.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 43 lines · Andrew Cooper
Quoted 27 lines · Tamas K Lengyel
Quoted 26 lines · Tamas K Lengyel
Quoted 25 lines · Andrew Cooper
xen/arch/x86/vm_event.c
Quoted 20 lines · Tamas K Lengyel
Currently the
gs_shadowvalue is only cached when the vCPU is being scheduled out by Xen. Reporting this (usually) stale value throughvm_eventis incorrect, since it doesn't represent the actual state of the vCPU at the time the event was recorded. This preventsvm_eventsubscribers from correctly finding kernel structures in the guest when it is trapped while in ring3.Signed-off-by Tamas K Lengyel <[email protected]>Cc Razvan Cojocaru <[email protected]>Cc Jan Beulich <[email protected]>Cc Andrew Cooper <[email protected]>Cc Wei Liu <[email protected]>Cc Roger Pau Monne <[email protected]>--- xen/arch/x86/vm_event.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)xen/arch/x86/vm_event.c +0
-0Actually, come to think of it, the same is true for the SYSENTER details, which by default are read/write to the guest while it is scheduled. As a result, the details reported here will from the last vcpu context switch, and possibly stale.
Quoted 4 lines
Quoted 1 line
I'll look into it.
The sysenter values look fine to me because
vmx_save_vmcs_ctxtcallsvmx_vmcs_save, which refreshes the values from the actual VMCS. It's onlyshadow_gsthat is not refreshed.So, you are correct that we call into
vmx_vmcs_save()which causes the SYSENTER details to be correct.However, the same path also calls
vmx_save_cpu_state()which savesshadow_gs, and therefore ought to DTRT for the previous use invm_event.The problem is that vmx_{save,load}_cpu_state() only function correctly in remote context, which is why the stale
shadow_gspersists.
Quoted 7 lines
Contrary to what I said earlier, I now think that a better fix would be to make the above functions safe to use use in current context (at least for the save side - the restore side can probably just
ASSERT()atm, as it doesn't seem to have an equivalent use).That way, the
vm_eventcode doesn't need to contain any context-sensitive code, which is a better overall, IMO.
Sounds good to me. So a "v == current" case to refresh the gs_shadow value is all this will entail, correct?
Thanks,
Tamas
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel