x86/vm_event: add short-circuit for breakpoints (aka, , "fast single step")
PATCH v3 1/1 x86/vm_event: add short-circuit for breakpoints (aka, , "fast single step")
When using DRAKVUF (or another system using altp2m with shadow pages similar to what is described in
https://xenproject.org/2016/04/13/stealthy-monitoring-with-xen-altp2m),
after a breakpoint is hit the system switches to the default unrestricted altp2m view with singlestep enabled. When the singlestep traps to Xen another vm_event is sent to the monitor agent, which then normally disables singlestepping and switches the altp2m view back to the restricted view.
This patch short-circuiting that last part so that it doesn't need to send the vm_event out for the singlestep event and should switch back to the restricted view in Xen automatically.
This optimization gains about 35% speed-up.
Was tested on Debian branch of Xen 4.12. See at:
https://github.com/skvl/xen/tree/debian/knorrie/4.12/fast-singlestep
Rebased on master:
https://github.com/skvl/xen/tree/fast-singlestep
--- xen/arch/x86/hvm/hvm.c | 15 +++++++++++++++ xen/arch/x86/hvm/monitor.c | 9 +++++++++ xen/arch/x86/vm_event.c | 8 ++++++-- xen/include/asm-x86/hvm/hvm.h | 1 + xen/include/asm-x86/hvm/vcpu.h | 4 ++++ xen/include/public/vm_event.h | 10 ++++++++++ 6 files changed, 45 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/hvm.c +15 -0
@@ -5126,6 +5126,21 @@ void hvm_toggle_singlestep(struct vcpu *v) v->arch.hvm.single_step = !v->arch.hvm.single_step; } +void hvm_fast_singlestep (struct vcpu *v,uint16_t p2midx) +{ +ASSERT (atomic_read (&v->pause_count)); + +if ( !hvm_is_singlestep_supported () ) +return ; + +if ( p2midx >=MAX_ALTP2M ) +return ; + + v->arch.hvm.single_step = true; + v->arch.hvm.fast_single_step.enabled = true; + v->arch.hvm.fast_single_step.p2midx = p2midx; +} +/* * Segment caches in VMCB/VMCS are inconsistent about which bits are checked, * important, and preserved across vmentry/exit. Cook the values to make them
xen/arch/x86/hvm/monitor.c +9 -0
@@ -28,6 +28,7 @@#include <asm/hvm/monitor.h> #include <asm/altp2m.h> #include <asm/monitor.h> +#include <asm/p2m.h> #include <asm/paging.h> #include <asm/vm_event.h> #include <public/vm_event.h>
@@ -159,6 +160,14 @@ int hvm_monitor_debug(unsigned long rip, enum hvm_monitor_debug_type type,case HVM_MONITOR_SINGLESTEP_BREAKPOINT :if ( !ad->monitor.singlestep_enabled )return 0 ; +if ( curr->arch.hvm.fast_single_step.enabled ) + { +p2m_altp2m_check (curr, curr->arch.hvm.fast_single_step.p2midx); + curr->arch.hvm.single_step = false; + curr->arch.hvm.fast_single_step.enabled = false; + curr->arch.hvm.fast_single_step.p2midx =0 ; +return 0 ; + } req.reason =VM_EVENT_REASON_SINGLESTEP ; req.u.singlestep.gfn =gfn_of_rip (rip); sync = true;
xen/arch/x86/vm_event.c +6 -2
@@ -61,7 +61,8 @@ void vm_event_cleanup_domain(struct domain *d)void vm_event_toggle_singlestep (struct domain *d,struct vcpu *v,vm_event_response_t *rsp) { -if ( !(rsp->flags &VM_EVENT_FLAG_TOGGLE_SINGLESTEP ) ) +if ( !(rsp->flags &VM_EVENT_FLAG_TOGGLE_SINGLESTEP || + rsp->flags &VM_EVENT_FLAG_FAST_SINGLESTEP ) )return ;if ( !is_hvm_domain (d) )
@@ -69,7 +70,10 @@ void vm_event_toggle_singlestep(struct domain *d, struct vcpu *v,ASSERT (atomic_read (&v->vm_event_pause_count)); -hvm_toggle_singlestep (v); +if ( rsp->flags &VM_EVENT_FLAG_TOGGLE_SINGLESTEP ) +hvm_toggle_singlestep (v); +else +hvm_fast_singlestep (v, rsp->u.fast_singlestep.p2midx); }void vm_event_register_write_resume (struct vcpu *v,vm_event_response_t *rsp)
xen/include/asm-x86/hvm/hvm.h +1 -0
@@ -323,6 +323,7 @@ int hvm_debug_op(struct vcpu *v, int32_t op);/* Caller should pause vcpu before calling this function */ void hvm_toggle_singlestep (struct vcpu *v); +void hvm_fast_singlestep (struct vcpu *v,uint16_t p2midx);int hvm_hap_nested_page_fault (paddr_t gpa,unsigned long gla,struct npfec npfec);
xen/include/asm-x86/hvm/vcpu.h +4 -0
@@ -172,6 +172,10 @@ struct hvm_vcpu {bool flag_dr_dirty;bool debug_state_latch;bool single_step; +struct { +bool enabled; +uint16_t p2midx; + } fast_single_step;struct hvm_vcpu_asid n1asid;
xen/include/public/vm_event.h +10 -0
@@ -110,6 +110,11 @@* interrupt pending after resuming the VCPU. */ #define VM_EVENT_FLAG_GET_NEXT_INTERRUPT (1 << 10) +/* +* Execute fast singlestepping on vm_event response. +* Requires the vCPU to be paused already (synchronous events only). +*/ +#define VM_EVENT_FLAG_FAST_SINGLESTEP (1 << 11)/* * Reasons for the vm event request
@@ -276,6 +281,10 @@ struct vm_event_singlestep {uint64_t gfn; }; +struct vm_event_fast_singlestep { +uint16_t p2midx; +}; +struct vm_event_debug {uint64_t gfn;uint32_t insn_length;
@@ -363,6 +372,7 @@ typedef struct vm_event_st {struct vm_event_mov_to_msr mov_to_msr;struct vm_event_desc_access desc_access;struct vm_event_singlestep singlestep; +struct vm_event_fast_singlestep fast_singlestep;struct vm_event_debug software_breakpoint;struct vm_event_debug debug_exception;struct vm_event_cpuid cpuid;
Signature · 7 lines
2.20.1 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
xen/arch/x86/vm_event.c void vm_event_cleanup_domain(struct domain *d) · line 66
Quoted 10 lines · Sergey Kovalev
xen/arch/x86/vm_event.c +2
-1@@ -61,7 +61,8 @@ void vm_event_cleanup_domain(struct domain *d)void vm_event_toggle_singlestep (struct domain *d,struct vcpu *v,vm_event_response_t *rsp) { -if ( !(rsp->flags &VM_EVENT_FLAG_TOGGLE_SINGLESTEP ) ) +if ( !(rsp->flags &VM_EVENT_FLAG_TOGGLE_SINGLESTEP || + rsp->flags &VM_EVENT_FLAG_FAST_SINGLESTEP ) )return ;
This is still the same as in v2 (could be taken care of while committing if no other need for a v4 arises). Also please allow a little more time for reviews between sending versions. And also please have a brief revision log after a --- separator after the commit message.
Anyway - applicable bits
Jan
Quoted 68 lines
xen/arch/x86/vm_event.c +4
-1@@ -69,7 +70,10 @@ void vm_event_toggle_singlestep(struct domain *d, struct vcpu *v,ASSERT (atomic_read (&v->vm_event_pause_count)); -hvm_toggle_singlestep (v); +if ( rsp->flags &VM_EVENT_FLAG_TOGGLE_SINGLESTEP ) +hvm_toggle_singlestep (v); +else +hvm_fast_singlestep (v, rsp->u.fast_singlestep.p2midx); }void vm_event_register_write_resume (struct vcpu *v,vm_event_response_t *rsp)xen/include/asm-x86/hvm/hvm.h +1
-0@@ -323,6 +323,7 @@ int hvm_debug_op(struct vcpu *v, int32_t op);/* Caller should pause vcpu before calling this function */ void hvm_toggle_singlestep (struct vcpu *v); +void hvm_fast_singlestep (struct vcpu *v,uint16_t p2midx);int hvm_hap_nested_page_fault (paddr_t gpa,unsigned long gla,struct npfec npfec);xen/include/asm-x86/hvm/vcpu.h +4
-0@@ -172,6 +172,10 @@ struct hvm_vcpu {bool flag_dr_dirty;bool debug_state_latch;bool single_step; +struct { +bool enabled; +uint16_t p2midx; + } fast_single_step;struct hvm_vcpu_asid n1asid;xen/include/public/vm_event.h +9
-0@@ -110,6 +110,11 @@* interrupt pending after resuming the VCPU. */ #define VM_EVENT_FLAG_GET_NEXT_INTERRUPT (1 << 10) +/* +* Execute fast singlestepping on vm_event response. +* Requires the vCPU to be paused already (synchronous events only). +*/ +#define VM_EVENT_FLAG_FAST_SINGLESTEP (1 << 11)/* * Reasons for the vm event request @@ -276,6 +281,10 @@ struct vm_event_singlestep {uint64_t gfn; }; +struct vm_event_fast_singlestep { +uint16_t p2midx; +}; +struct vm_event_debug {uint64_t gfn;uint32_t insn_length;
xen/include/public/vm_event.h typedef struct vm_event_st { · line 378
Quoted 7 lines
xen/include/public/vm_event.h +1
-0@@ -363,6 +372,7 @@ typedef struct vm_event_st {struct vm_event_mov_to_msr mov_to_msr;struct vm_event_desc_access desc_access;struct vm_event_singlestep singlestep; +struct vm_event_fast_singlestep fast_singlestep;struct vm_event_debug software_breakpoint;struct vm_event_debug debug_exception;struct vm_event_cpuid cpuid;
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 14 lines · Jan Beulich
xen/arch/x86/vm_event.c void vm_event_cleanup_domain(struct domain *d) · line 66
Quoted 10 lines · Sergey Kovalev
xen/arch/x86/vm_event.c +2
-1@@ -61,7 +61,8 @@ void vm_event_cleanup_domain(struct domain *d)void vm_event_toggle_singlestep (struct domain *d,struct vcpu *v,vm_event_response_t *rsp) { -if ( !(rsp->flags &VM_EVENT_FLAG_TOGGLE_SINGLESTEP ) ) +if ( !(rsp->flags &VM_EVENT_FLAG_TOGGLE_SINGLESTEP || + rsp->flags &VM_EVENT_FLAG_FAST_SINGLESTEP ) )return ;This is still the same as in v2 (could be taken care of while committing if no other need for a v4 arises).
Sorry for that. I didn't stage it...
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
xen/include/public/vm_event.h line 116
Quoted 12 lines
xen/include/public/vm_event.h +4
-0@@ -110,6 +110,11 @@* interrupt pending after resuming the VCPU. */ #define VM_EVENT_FLAG_GET_NEXT_INTERRUPT (1 << 10) +/* +* Execute fast singlestepping on vm_event response. +* Requires the vCPU to be paused already (synchronous events only). +*/
Please expand this description here that it also requires setting the p2midx field of fast_singlestep to which Xen will switch the vCPU to on the occurance of the first singlestep, after which singlestep gets automatically disabled.
Quoted 24 lines
xen/include/public/vm_event.h +6
-1+#define VM_EVENT_FLAG_FAST_SINGLESTEP (1 << 11)/* * Reasons for the vm event request @@ -276,6 +281,10 @@ struct vm_event_singlestep {uint64_t gfn; }; +struct vm_event_fast_singlestep { +uint16_t p2midx; +}; +struct vm_event_debug {uint64_t gfn;uint32_t insn_length;@@ -363,6 +372,7 @@ typedef struct vm_event_st {struct vm_event_mov_to_msr mov_to_msr;struct vm_event_desc_access desc_access;struct vm_event_singlestep singlestep; +struct vm_event_fast_singlestep fast_singlestep;struct vm_event_debug software_breakpoint;struct vm_event_debug debug_exception;struct vm_event_cpuid cpuid; --
Quoted 1 line
2.20.1
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
xen/include/public/vm_event.h line 117
Quoted 13 lines
xen/include/public/vm_event.h +5
-0@@ -110,6 +110,11 @@* interrupt pending after resuming the VCPU. */ #define VM_EVENT_FLAG_GET_NEXT_INTERRUPT (1 << 10) +/* +* Execute fast singlestepping on vm_event response. +* Requires the vCPU to be paused already (synchronous events only). +*/ +#define VM_EVENT_FLAG_FAST_SINGLESTEP (1 << 11)
Just another minor style nitpick: alignment of (1 << 11) is off compared to all of the previous declaration above.
Tamas
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel