VM introspection
Subsystems (17)
RFC PATCH v6 63/92 kvm: introspection: add KVMI_EVENT_DESCRIPTOR
In reply to Adalbert LazărFrom: Nicușor Cîțu <[email protected]>
This event is sent when IDTR, GDTR, LDTR or TR are accessed.
These could be used to implement a tiny agent which runs in the context of an introspected guest and uses virtualized exceptions (#VE) and alternate EPT views (VMFUNC #0) to filter converted VMEXITS. The events of interested will be suppressed (after some appropriate guest-side handling) while the rest will be sent to the introspector via a VMCALL.
--- Documentation/virtual/kvm/kvmi.rst | 38 +++++++++++++++ arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/uapi/asm/kvmi.h | 11 +++++ arch/x86/kvm/kvmi.c | 70 ++++++++++++++++++++++++++++ arch/x86/kvm/svm.c | 74 ++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 59 +++++++++++++++++++++++- arch/x86/kvm/vmx/vmx.h | 2 + arch/x86/kvm/x86.c | 6 +++ include/linux/kvm_host.h | 1 + include/linux/kvmi.h | 4 ++ virt/kvm/kvmi.c | 2 +- virt/kvm/kvmi_int.h | 3 ++ virt/kvm/kvmi_msg.c | 17 +++++++ 13 files changed, 285 insertions(+), 3 deletions(-)
Documentation/virtual/kvm/kvmi.rst +38 -0
@@ -1536,3 +1536,41 @@ It is used by the code residing inside the introspected guest to call the introspection tool and to report certain details about its operation. For example, a classic antimalware remediation tool can report what it has found during a scan. + +11. KVMI_EVENT_DESCRIPTOR +------------------------- + +:Architecture: x86 +:Versions: >= 1 +:Actions: CONTINUE, RETRY, CRASH +:Parameters: + +:: + + struct kvmi_event; + struct kvmi_event_descriptor { + __u8 descriptor; + __u8 write; + __u8 padding[6]; + }; + +:Returns: + +:: + + struct kvmi_vcpu_hdr; + struct kvmi_event_reply; + +This event is sent when a descriptor table register is accessed and the +introspection has been enabled for this event (see **KVMI_CONTROL_EVENTS**). + +``kvmi_event`` and ``kvmi_event_descriptor`` are sent to the introspector. + +``descriptor`` can be one of:: + + KVMI_DESC_IDTR + KVMI_DESC_GDTR + KVMI_DESC_LDTR + KVMI_DESC_TR + +``write`` is 1 if the descriptor was written, 0 otherwise.
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1015,6 +1015,7 @@ struct kvm_x86_ops {void (*msr_intercept)(struct kvm_vcpu *vcpu,unsigned int msr,bool enable); +bool (*desc_intercept)(struct kvm_vcpu *vcpu,bool enable);void (*cr3_write_exiting)(struct kvm_vcpu *vcpu,bool enable);bool (*nested_pagefault)(struct kvm_vcpu *vcpu);bool (*spt_fault)(struct kvm_vcpu *vcpu);
arch/x86/include/uapi/asm/kvmi.h +11 -0
@@ -110,4 +110,15 @@ struct kvmi_get_mtrr_type_reply { __u8 padding[7]; }; +#define KVMI_DESC_IDTR 1 +#define KVMI_DESC_GDTR 2 +#define KVMI_DESC_LDTR 3 +#define KVMI_DESC_TR 4 + +struct kvmi_event_descriptor { +__u8 descriptor; +__u8 write; +__u8 padding[6 ]; +}; +#endif /* _UAPI_ASM_X86_KVMI_H */
arch/x86/kvm/kvmi.c +70 -0
82 lines not on this page · raw message
arch/x86/kvm/svm.c +74 -0
104 lines not on this page · raw message
arch/x86/kvm/vmx/vmx.c +57 -2
90 lines not on this page · raw message
arch/x86/kvm/vmx/vmx.h +2 -0
@@ -268,6 +268,8 @@ struct vcpu_vmx {u64 msr_ia32_feature_control_valid_bits;u64 ept_pointer; +bool tracking_desc; +struct pt_desc pt_desc; };
arch/x86/kvm/x86.c +6 -0
@@ -10140,6 +10140,12 @@ bool kvm_spt_fault(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL (kvm_spt_fault); +bool kvm_arch_vcpu_intercept_desc (struct kvm_vcpu *vcpu,bool enable) +{ +return kvm_x86_ops->desc_intercept (vcpu, enable); +} +EXPORT_SYMBOL (kvm_arch_vcpu_intercept_desc); +EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_exit);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_fast_mmio);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_inj_virq);
include/linux/kvm_host.h +1 -0
@@ -806,6 +806,7 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg);int kvm_arch_vcpu_set_guest_debug (struct kvm_vcpu *vcpu,struct kvm_guest_debug *dbg); +bool kvm_arch_vcpu_intercept_desc (struct kvm_vcpu *vcpu,bool enable);int kvm_arch_vcpu_ioctl_run (struct kvm_vcpu *vcpu,struct kvm_run *kvm_run);void kvm_vcpu_ioctl_x86_get_xsave (struct kvm_vcpu *vcpu,struct kvm_xsave *guest_xsave);
include/linux/kvmi.h +4 -0
@@ -20,6 +20,7 @@ bool kvmi_breakpoint_event(struct kvm_vcpu *vcpu, u64 gva, u8 insn_len);bool kvmi_hypercall_event (struct kvm_vcpu *vcpu);bool kvmi_queue_exception (struct kvm_vcpu *vcpu);void kvmi_trap_event (struct kvm_vcpu *vcpu); +bool kvmi_descriptor_event (struct kvm_vcpu *vcpu,u8 descriptor,u8 write);void kvmi_handle_requests (struct kvm_vcpu *vcpu);void kvmi_init_emulate (struct kvm_vcpu *vcpu);void kvmi_activate_rep_complete (struct kvm_vcpu *vcpu);
@@ -35,6 +36,9 @@ static inline int kvmi_vcpu_init(struct kvm_vcpu *vcpu) { return 0; }static inline bool kvmi_breakpoint_event (struct kvm_vcpu *vcpu,u64 gva,u8 insn_len) {return true; } +static inline bool kvmi_descriptor_event (struct kvm_vcpu *vcpu,u8 descriptor, +u8 write) + {return true; }static inline void kvmi_vcpu_uninit (struct kvm_vcpu *vcpu) { }static inline void kvmi_handle_requests (struct kvm_vcpu *vcpu) { }static inline bool kvmi_hypercall_event (struct kvm_vcpu *vcpu) {return false; }
virt/kvm/kvmi.c +1 -1
@@ -1529,7 +1529,7 @@ int kvmi_cmd_control_events(struct kvm_vcpu *vcpu, unsigned int event_id, err =kvmi_control_event_breakpoint (vcpu, enable);break ;default : - err =0 ; + err =kvmi_arch_cmd_control_event (vcpu, event_id, enable);break ; }
virt/kvm/kvmi_int.h +3 -0
@@ -189,6 +189,7 @@ u32 kvmi_msg_send_hypercall(struct kvm_vcpu *vcpu);u32 kvmi_msg_send_pf (struct kvm_vcpu *vcpu,u64 gpa,u64 gva,u8 access,bool *singlestep,bool *rep_complete,u64 *ctx_addr,u8 *ctx,u32 *ctx_size); +u32 kvmi_msg_send_descriptor (struct kvm_vcpu *vcpu,u8 descriptor,u8 write);u32 kvmi_msg_send_create_vcpu (struct kvm_vcpu *vcpu);u32 kvmi_msg_send_pause_vcpu (struct kvm_vcpu *vcpu);int kvmi_msg_send_unhook (struct kvmi *ikvm);
@@ -228,6 +229,8 @@ void kvmi_handle_common_event_actions(struct kvm_vcpu *vcpu, u32 action,void kvmi_arch_update_page_tracking (struct kvm *kvm,struct kvm_memory_slot *slot,struct kvmi_mem_access *m); +int kvmi_arch_cmd_control_event (struct kvm_vcpu *vcpu,unsigned int event_id, +bool enable); int kvmi_arch_cmd_get_registers(struct kvm_vcpu *vcpu, const struct kvmi_msg_hdr *msg, const struct kvmi_get_registers *req,
virt/kvm/kvmi_msg.c +17 -0
@@ -1163,6 +1163,23 @@ u32 kvmi_msg_send_pf(struct kvm_vcpu *vcpu, u64 gpa, u64 gva, u8 access,return action; } +u32 kvmi_msg_send_descriptor (struct kvm_vcpu *vcpu,u8 descriptor,u8 write) +{ +struct kvmi_event_descriptor e; +int err, action; + +memset (&e,0 ,sizeof (e)); + e.descriptor = descriptor; + e.write = write; + + err =kvmi_send_event (vcpu,KVMI_EVENT_DESCRIPTOR , &e,sizeof (e), +NULL ,0 , &action); +if (err) +return KVMI_EVENT_ACTION_CONTINUE ; + +return action; +} +u32 kvmi_msg_send_create_vcpu (struct kvm_vcpu *vcpu) {int err, action;
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 64/92 kvm: introspection: add single-stepping
In reply to Adalbert LazărFrom: Nicușor Cîțu <[email protected]>
This would be used either if the introspection tool request it as a reply to a KVMI_EVENT_PF event or to cope with instructions that cannot be handled by the x86 emulator during the handling of a VMEXIT. In these situations, all other vCPU-s are kicked and held, the EPT-based protection is removed and the guest is single stepped by the vCPU that triggered the initial VMEXIT. Upon completion the EPT-base protection is reinstalled and all vCPU-s all allowed to return to the guest.
This is a rather slow workaround that kicks in occasionally. In the future, the most frequently single-stepped instructions should be added to the emulator (usually, stores to and from memory - SSE/AVX).
For the moment it works only on Intel.
--- arch/x86/include/asm/kvm_host.h | 3 + arch/x86/kvm/kvmi.c | 47 ++++++++++- arch/x86/kvm/svm.c | 5 ++ arch/x86/kvm/vmx/vmx.c | 17 ++++ arch/x86/kvm/x86.c | 19 +++++ include/linux/kvmi.h | 4 + virt/kvm/kvmi.c | 145 +++++++++++++++++++++++++++++++- virt/kvm/kvmi_int.h | 16 ++++ 8 files changed, 253 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -0
@@ -1016,6 +1016,7 @@ struct kvm_x86_ops {void (*msr_intercept)(struct kvm_vcpu *vcpu,unsigned int msr,bool enable);bool (*desc_intercept)(struct kvm_vcpu *vcpu,bool enable); +void (*set_mtf)(struct kvm_vcpu *vcpu,bool enable);void (*cr3_write_exiting)(struct kvm_vcpu *vcpu,bool enable);bool (*nested_pagefault)(struct kvm_vcpu *vcpu);bool (*spt_fault)(struct kvm_vcpu *vcpu);
@@ -1628,6 +1629,8 @@ void kvm_arch_msr_intercept(struct kvm_vcpu *vcpu, unsigned int msr, bool enable);bool kvm_mmu_nested_pagefault (struct kvm_vcpu *vcpu);bool kvm_spt_fault (struct kvm_vcpu *vcpu); +void kvm_set_mtf (struct kvm_vcpu *vcpu,bool enable); +void kvm_set_interrupt_shadow (struct kvm_vcpu *vcpu,int mask);void kvm_control_cr3_write_exiting (struct kvm_vcpu *vcpu,bool enable);#endif /* _ASM_X86_KVM_HOST_H */
arch/x86/kvm/kvmi.c +45 -2
71 lines not on this page · raw message
arch/x86/kvm/svm.c +5 -0
@@ -7183,6 +7183,10 @@ static bool svm_spt_fault(struct kvm_vcpu *vcpu)return (svm->vmcb->control.exit_code ==SVM_EXIT_NPF ); } +static void svm_set_mtf (struct kvm_vcpu *vcpu,bool enable) +{ +} +static void svm_cr3_write_exiting (struct kvm_vcpu *vcpu,bool enable) { }
@@ -7225,6 +7229,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, .has_emulated_msr = svm_has_emulated_msr, + .set_mtf = svm_set_mtf, .cr3_write_exiting = svm_cr3_write_exiting, .msr_intercept = svm_msr_intercept, .desc_intercept = svm_desc_intercept,
arch/x86/kvm/vmx/vmx.c +17 -0
@@ -5384,6 +5384,7 @@ static int handle_invalid_op(struct kvm_vcpu *vcpu)static int handle_monitor_trap (struct kvm_vcpu *vcpu) { +kvmi_stop_ss (vcpu);return 1 ; }
@@ -5992,6 +5993,11 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu) } } +if (kvmi_vcpu_enabled_ss (vcpu) + && exit_reason !=EXIT_REASON_EPT_VIOLATION + && exit_reason !=EXIT_REASON_MONITOR_TRAP_FLAG ) +kvmi_stop_ss (vcpu); +if (exit_reason < kvm_vmx_max_exit_handlers && kvm_vmx_exit_handlers[exit_reason])return kvm_vmx_exit_handlers[exit_reason](vcpu);
@@ -7842,6 +7848,16 @@ static __exit void hardware_unsetup(void)free_kvm_area (); } +static void vmx_set_mtf (struct kvm_vcpu *vcpu,bool enable) +{ +if (enable) +vmcs_set_bits (CPU_BASED_VM_EXEC_CONTROL , +CPU_BASED_MONITOR_TRAP_FLAG ); +else +vmcs_clear_bits (CPU_BASED_VM_EXEC_CONTROL , +CPU_BASED_MONITOR_TRAP_FLAG ); +} +static void vmx_msr_intercept (struct kvm_vcpu *vcpu,unsigned int msr,bool enable) {
@@ -7927,6 +7943,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .cpu_has_accelerated_tpr = report_flexpriority, .has_emulated_msr = vmx_has_emulated_msr, + .set_mtf = vmx_set_mtf, .msr_intercept = vmx_msr_intercept, .cr3_write_exiting = vmx_cr3_write_exiting, .desc_intercept = vmx_desc_intercept,
arch/x86/kvm/x86.c +19 -0
@@ -7358,6 +7358,13 @@ static int inject_pending_event(struct kvm_vcpu *vcpu, bool req_int_win) {int r; +if (kvmi_vcpu_enabled_ss (vcpu)) +/* +* We cannot inject events during single-stepping. +* Try again later. +*/ +return -1 ; +/* try to reinject previous events if any */ if (vcpu->arch.exception.injected)
@@ -10134,6 +10141,18 @@ void kvm_control_cr3_write_exiting(struct kvm_vcpu *vcpu, bool enable) }EXPORT_SYMBOL (kvm_control_cr3_write_exiting); +void kvm_set_mtf (struct kvm_vcpu *vcpu,bool enable) +{ + kvm_x86_ops->set_mtf (vcpu, enable); +} +EXPORT_SYMBOL (kvm_set_mtf); + +void kvm_set_interrupt_shadow (struct kvm_vcpu *vcpu,int mask) +{ + kvm_x86_ops->set_interrupt_shadow (vcpu, mask); +} +EXPORT_SYMBOL (kvm_set_interrupt_shadow); +bool kvm_spt_fault (struct kvm_vcpu *vcpu) {return kvm_x86_ops->spt_fault (vcpu);
include/linux/kvmi.h +4 -0
@@ -22,6 +22,8 @@ bool kvmi_queue_exception(struct kvm_vcpu *vcpu);void kvmi_trap_event (struct kvm_vcpu *vcpu);bool kvmi_descriptor_event (struct kvm_vcpu *vcpu,u8 descriptor,u8 write);void kvmi_handle_requests (struct kvm_vcpu *vcpu); +void kvmi_stop_ss (struct kvm_vcpu *vcpu); +bool kvmi_vcpu_enabled_ss (struct kvm_vcpu *vcpu);void kvmi_init_emulate (struct kvm_vcpu *vcpu);void kvmi_activate_rep_complete (struct kvm_vcpu *vcpu);bool kvmi_bp_intercepted (struct kvm_vcpu *vcpu,u32 dbg);
@@ -44,6 +46,8 @@ static inline void kvmi_handle_requests(struct kvm_vcpu *vcpu) { }static inline bool kvmi_hypercall_event (struct kvm_vcpu *vcpu) {return false; }static inline bool kvmi_queue_exception (struct kvm_vcpu *vcpu) {return true; }static inline void kvmi_trap_event (struct kvm_vcpu *vcpu) { } +static inline void kvmi_stop_ss (struct kvm_vcpu *vcpu) { } +static inline bool kvmi_vcpu_enabled_ss (struct kvm_vcpu *vcpu) {return false; }static inline void kvmi_init_emulate (struct kvm_vcpu *vcpu) { }static inline void kvmi_activate_rep_complete (struct kvm_vcpu *vcpu) { }static inline bool kvmi_bp_intercepted (struct kvm_vcpu *vcpu,u32 dbg)
virt/kvm/kvmi.c +144 -1
161 lines not on this page · raw message
virt/kvm/kvmi_int.h +16 -0
@@ -126,6 +126,9 @@ struct kvmi_vcpu {DECLARE_BITMAP (high,KVMI_NUM_MSR ); } msr_mask; +bool ss_owner; +bool ss_requested; +struct list_head job_list;spinlock_t job_lock;
@@ -151,6 +154,15 @@ struct kvmi {DECLARE_BITMAP (event_allow_mask,KVMI_NUM_EVENTS );DECLARE_BITMAP (vm_ev_mask,KVMI_NUM_EVENTS ); +#define SINGLE_STEP_MAX_DEPTH 8 +struct { +gfn_t gfn; +u8 old_access; +u32 old_write_bitmap; + } ss_context[SINGLE_STEP_MAX_DEPTH ]; +u8 ss_level; +atomic_t ss_active; +struct {bool initialized;atomic_t enabled;
@@ -224,6 +236,7 @@ int kvmi_add_job(struct kvm_vcpu *vcpu, void *ctx, void (*free_fct)(void *ctx));void kvmi_handle_common_event_actions (struct kvm_vcpu *vcpu,u32 action,const char *str); +bool kvmi_start_ss (struct kvm_vcpu *vcpu,gpa_t gpa,u8 access);/* arch */ void kvmi_arch_update_page_tracking(struct kvm *kvm,
@@ -274,6 +287,9 @@ int kvmi_arch_cmd_inject_exception(struct kvm_vcpu *vcpu, u8 vector, u64 address);int kvmi_arch_cmd_control_cr (struct kvm_vcpu *vcpu,const struct kvmi_control_cr *req); +void kvmi_arch_start_single_step (struct kvm_vcpu *vcpu); +void kvmi_arch_stop_single_step (struct kvm_vcpu *vcpu); +u8 kvmi_arch_relax_page_access (u8 old,u8 new);int kvmi_arch_cmd_control_msr (struct kvm_vcpu *vcpu,const struct kvmi_control_msr *req);int kvmi_arch_cmd_get_mtrr_type (struct kvm_vcpu *vcpu,u64 gpa,u8 *type);
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [RFC PATCH v6 64/92] kvm: introspection: add single-stepping
Quoted 40 lines · Adalbert Lazăr
From: Nicușor Cîțu <[email protected]>
This would be used either if the introspection tool request it as a reply to a
KVMI_EVENT_PFevent or to cope with instructions that cannot be handled by the x86 emulator during the handling of a VMEXIT. In these situations, all other vCPU-s are kicked and held, the EPT-based protection is removed and the guest is single stepped by the vCPU that triggered the initial VMEXIT. Upon completion the EPT-base protection is reinstalled and all vCPU-s all allowed to return to the guest.This is a rather slow workaround that kicks in occasionally. In the future, the most frequently single-stepped instructions should be added to the emulator (usually, stores to and from memory - SSE/AVX).
For the moment it works only on Intel.
CC Jim Mattson <[email protected]>CC Sean Christopherson <[email protected]>CC Joerg Roedel <[email protected]>Signed-off-by Nicușor Cîțu <[email protected]>Co-developed-by Mihai Donțu <[email protected]>Signed-off-by Mihai Donțu <[email protected]>Co-developed-by Adalbert Lazăr <[email protected]>Signed-off-by Adalbert Lazăr <[email protected]>--- arch/x86/include/asm/kvm_host.h | 3 + arch/x86/kvm/kvmi.c | 47 ++++++++++- arch/x86/kvm/svm.c | 5 ++ arch/x86/kvm/vmx/vmx.c | 17 ++++ arch/x86/kvm/x86.c | 19 +++++ include/linux/kvmi.h | 4 + virt/kvm/kvmi.c | 145 +++++++++++++++++++++++++++++++- virt/kvm/kvmi_int.h | 16 ++++ 8 files changed, 253 insertions(+), 3 deletions(-)arch/x86/include/asm/kvm_host.h +0
-0
arch/x86/include/asm/kvm_host.h struct kvm_x86_ops { · line 1019
Quoted 4 lines
arch/x86/include/asm/kvm_host.h +1
-0@@ -1016,6 +1016,7 @@ struct kvm_x86_ops {void (*msr_intercept)(struct kvm_vcpu *vcpu,unsigned int msr,bool enable);bool (*desc_intercept)(struct kvm_vcpu *vcpu,bool enable); +void (*set_mtf)(struct kvm_vcpu *vcpu,bool enable);
MTF is a VMX specific implementation of single-stepping, this should be enable_single_step() or something along those lines. For example, I assume SVM could implement something that is mostly functional via RFLAGS.TF.
Quoted 46 lines
arch/x86/include/asm/kvm_host.h +2
-0void (*cr3_write_exiting)(struct kvm_vcpu *vcpu,bool enable);bool (*nested_pagefault)(struct kvm_vcpu *vcpu);bool (*spt_fault)(struct kvm_vcpu *vcpu);@@ -1628,6 +1629,8 @@ void kvm_arch_msr_intercept(struct kvm_vcpu *vcpu, unsigned int msr, bool enable);bool kvm_mmu_nested_pagefault (struct kvm_vcpu *vcpu);bool kvm_spt_fault (struct kvm_vcpu *vcpu); +void kvm_set_mtf (struct kvm_vcpu *vcpu,bool enable); +void kvm_set_interrupt_shadow (struct kvm_vcpu *vcpu,int mask);void kvm_control_cr3_write_exiting (struct kvm_vcpu *vcpu,bool enable);#endif /* _ASM_X86_KVM_HOST_H */ arch/x86/kvm/kvmi.c +3
-2@@ -520,7 +520,6 @@ bool kvmi_arch_pf_event(struct kvm_vcpu *vcpu, gpa_t gpa, gva_t gva,u32 ctx_size;u64 ctx_addr;u32 action; -bool singlestep_ignored;bool ret = false;if (!kvm_spt_fault (vcpu))@@ -533,7 +532,7 @@ bool kvmi_arch_pf_event(struct kvm_vcpu *vcpu, gpa_t gpa, gva_t gva,if (ivcpu->effective_rep_complete)return true; - action =kvmi_msg_send_pf (vcpu, gpa, gva, access, &singlestep_ignored, + action =kvmi_msg_send_pf (vcpu, gpa, gva, access, &ivcpu->ss_requested, &ivcpu->rep_complete, &ctx_addr, ivcpu->ctx_data, &ctx_size);@@ -547,6 +546,8 @@ bool kvmi_arch_pf_event(struct kvm_vcpu *vcpu, gpa_t gpa, gva_t gva, ret = true;break ;case KVMI_EVENT_ACTION_RETRY : +if (ivcpu->ss_requested && !kvmi_start_ss (vcpu, gpa, access)) + ret = true;break ;default :kvmi_handle_common_event_actions (vcpu, action,"PF" );@@ -758,6 +759,48 @@ int kvmi_arch_cmd_control_cr(struct kvm_vcpu *vcpu,return 0 ; }
arch/x86/kvm/kvmi.c int kvmi_arch_cmd_control_cr(struct kvm_vcpu *vcpu, · line 771
Quoted 10 lines
arch/x86/kvm/kvmi.c +10
-0+void kvmi_arch_start_single_step (struct kvm_vcpu *vcpu) +{ +kvm_set_mtf (vcpu, true); + +/* +* Set block by STI only if the RFLAGS.IF = 1. +* Blocking by both STI and MOV/POP SS is not possible. +*/ +if (kvm_arch_interrupt_allowed (vcpu)) +kvm_set_interrupt_shadow (vcpu,KVM_X86_SHADOW_INT_STI );
This is wrong, the STI shadow only exists if interrupts were unblocked prior to STI. I'm guessing this is a hack to workaround kvmi_arch_stop_single_step() not properly handling the clearing case.
arch/x86/kvm/kvmi.c int kvmi_arch_cmd_control_cr(struct kvm_vcpu *vcpu, · line 784
Quoted 13 lines
arch/x86/kvm/kvmi.c +13
-0+ +} + +void kvmi_arch_stop_single_step (struct kvm_vcpu *vcpu) +{ +kvm_set_mtf (vcpu, false); +/* +* The blocking by STI is cleared after the guest +* executes one instruction or incurs an exception. +* However we migh stop the SS before entering to guest, +* so be sure we are clearing the STI blocking. +*/ +kvm_set_interrupt_shadow (vcpu,0 );
There are only three callers of kvmi_stop_ss(), it should be possible to accurately update interruptibility:
- kvmi_run_ss() fail, do nothing
- VM-Exit that wasn't a single-step - clear interruptibility if the guest executed an instruction (including faulted on an instr).
- MTF VM-Exit - do nothing (VMCS should already be up-to-date).
Quoted 58 lines
arch/x86/kvm/kvmi.c +19
-0+} + +u8 kvmi_arch_relax_page_access (u8 old,u8 new) +{ +u8 ret = old | new; + +/* +* An SPTE entry with just the -wx bits set can trigger a +* misconfiguration error from the hardware, as it's the case +* for x86 where this access mode is used to mark I/O memory. +* Thus, we make sure that -wx accesses are translated to rwx. +*/ +if ((ret & (KVMI_PAGE_ACCESS_W |KVMI_PAGE_ACCESS_X )) == + (KVMI_PAGE_ACCESS_W |KVMI_PAGE_ACCESS_X )) + ret |=KVMI_PAGE_ACCESS_R ; + +return ret; +} + static const struct { unsigned int allow_bit; enum kvm_page_track_mode track_mode;arch/x86/kvm/svm.c +5
-0@@ -7183,6 +7183,10 @@ static bool svm_spt_fault(struct kvm_vcpu *vcpu)return (svm->vmcb->control.exit_code ==SVM_EXIT_NPF ); } +static void svm_set_mtf (struct kvm_vcpu *vcpu,bool enable) +{ +} +static void svm_cr3_write_exiting (struct kvm_vcpu *vcpu,bool enable) { }@@ -7225,6 +7229,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, .has_emulated_msr = svm_has_emulated_msr, + .set_mtf = svm_set_mtf, .cr3_write_exiting = svm_cr3_write_exiting, .msr_intercept = svm_msr_intercept, .desc_intercept = svm_desc_intercept,arch/x86/kvm/vmx/vmx.c +1
-0@@ -5384,6 +5384,7 @@ static int handle_invalid_op(struct kvm_vcpu *vcpu)static int handle_monitor_trap (struct kvm_vcpu *vcpu) { +kvmi_stop_ss (vcpu);return 1 ; }
arch/x86/kvm/vmx/vmx.c static int vmx_handle_exit(struct kvm_vcpu *vcpu) · line 5998
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +3
-0@@ -5992,6 +5993,11 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu) } } +if (kvmi_vcpu_enabled_ss (vcpu) + && exit_reason !=EXIT_REASON_EPT_VIOLATION + && exit_reason !=EXIT_REASON_MONITOR_TRAP_FLAG )
Bad indentation. This is prevelant through the series.
Quoted 53 lines
arch/x86/kvm/vmx/vmx.c +13
-0+kvmi_stop_ss (vcpu); +if (exit_reason < kvm_vmx_max_exit_handlers && kvm_vmx_exit_handlers[exit_reason])return kvm_vmx_exit_handlers[exit_reason](vcpu);@@ -7842,6 +7848,16 @@ static __exit void hardware_unsetup(void)free_kvm_area (); } +static void vmx_set_mtf (struct kvm_vcpu *vcpu,bool enable) +{ +if (enable) +vmcs_set_bits (CPU_BASED_VM_EXEC_CONTROL , +CPU_BASED_MONITOR_TRAP_FLAG ); +else +vmcs_clear_bits (CPU_BASED_VM_EXEC_CONTROL , +CPU_BASED_MONITOR_TRAP_FLAG ); +} +static void vmx_msr_intercept (struct kvm_vcpu *vcpu,unsigned int msr,bool enable) {@@ -7927,6 +7943,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .cpu_has_accelerated_tpr = report_flexpriority, .has_emulated_msr = vmx_has_emulated_msr, + .set_mtf = vmx_set_mtf, .msr_intercept = vmx_msr_intercept, .cr3_write_exiting = vmx_cr3_write_exiting, .desc_intercept = vmx_desc_intercept,arch/x86/kvm/x86.c +8
-0@@ -7358,6 +7358,13 @@ static int inject_pending_event(struct kvm_vcpu *vcpu, bool req_int_win) {int r; +if (kvmi_vcpu_enabled_ss (vcpu)) +/* +* We cannot inject events during single-stepping. +* Try again later. +*/ +return -1 ; +/* try to reinject previous events if any */ if (vcpu->arch.exception.injected)@@ -10134,6 +10141,18 @@ void kvm_control_cr3_write_exiting(struct kvm_vcpu *vcpu, bool enable) }EXPORT_SYMBOL (kvm_control_cr3_write_exiting); +void kvm_set_mtf (struct kvm_vcpu *vcpu,bool enable)
arch/x86/kvm/x86.c void kvm_control_cr3_write_exiting(struct kvm_vcpu *vcpu, bool enable) · line 10154
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+{ + kvm_x86_ops->set_mtf (vcpu, enable); +} +EXPORT_SYMBOL (kvm_set_mtf); + +void kvm_set_interrupt_shadow (struct kvm_vcpu *vcpu,int mask) +{ + kvm_x86_ops->set_interrupt_shadow (vcpu, mask); +} +EXPORT_SYMBOL (kvm_set_interrupt_shadow);
Why do these wrappers exist, and why are they exported? Introspection is built into kvm, any reason not to use kvm_x86_ops directly? The most definitely don't need to be exported.
Quoted 9 lines
arch/x86/kvm/x86.c +1
-0+bool kvm_spt_fault (struct kvm_vcpu *vcpu) {return kvm_x86_ops->spt_fault (vcpu);include/linux/kvmi.h +0
-0
include/linux/kvmi.h bool kvmi_queue_exception(struct kvm_vcpu *vcpu); · line 26
Quoted 5 lines
include/linux/kvmi.h +2
-0@@ -22,6 +22,8 @@ bool kvmi_queue_exception(struct kvm_vcpu *vcpu);void kvmi_trap_event (struct kvm_vcpu *vcpu);bool kvmi_descriptor_event (struct kvm_vcpu *vcpu,u8 descriptor,u8 write);void kvmi_handle_requests (struct kvm_vcpu *vcpu); +void kvmi_stop_ss (struct kvm_vcpu *vcpu); +bool kvmi_vcpu_enabled_ss (struct kvm_vcpu *vcpu);
Spell out single step, and be consistent between single_step and singlestep. That applies to pretty much every variable and function unless doing so really makes the verbosity obnoxious.
Quoted 184 lines
include/linux/kvmi.h +2
-0void kvmi_init_emulate (struct kvm_vcpu *vcpu);void kvmi_activate_rep_complete (struct kvm_vcpu *vcpu);bool kvmi_bp_intercepted (struct kvm_vcpu *vcpu,u32 dbg);@@ -44,6 +46,8 @@ static inline void kvmi_handle_requests(struct kvm_vcpu *vcpu) { }static inline bool kvmi_hypercall_event (struct kvm_vcpu *vcpu) {return false; }static inline bool kvmi_queue_exception (struct kvm_vcpu *vcpu) {return true; }static inline void kvmi_trap_event (struct kvm_vcpu *vcpu) { } +static inline void kvmi_stop_ss (struct kvm_vcpu *vcpu) { } +static inline bool kvmi_vcpu_enabled_ss (struct kvm_vcpu *vcpu) {return false; }static inline void kvmi_init_emulate (struct kvm_vcpu *vcpu) { }static inline void kvmi_activate_rep_complete (struct kvm_vcpu *vcpu) { }static inline bool kvmi_bp_intercepted (struct kvm_vcpu *vcpu,u32 dbg)virt/kvm/kvmi.c +144
-1@@ -1260,11 +1260,19 @@ void kvmi_run_jobs(struct kvm_vcpu *vcpu) } } +static bool need_to_wait_for_ss (struct kvm_vcpu *vcpu) +{ +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); +struct kvmi *ikvm =IKVM (vcpu->kvm); + +return atomic_read (&ikvm->ss_active) && !ivcpu->ss_owner; +} +static bool need_to_wait (struct kvm_vcpu *vcpu) {struct kvmi_vcpu *ivcpu =IVCPU (vcpu); -return ivcpu->reply_waiting; +return ivcpu->reply_waiting ||need_to_wait_for_ss (vcpu); }static bool done_waiting (struct kvm_vcpu *vcpu)@@ -1572,6 +1580,141 @@ int kvmi_cmd_pause_vcpu(struct kvm_vcpu *vcpu, bool wait)return 0 ; } +void kvmi_stop_ss (struct kvm_vcpu *vcpu) +{ +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); +struct kvm *kvm = vcpu->kvm; +struct kvmi *ikvm; +int i; + + ikvm =kvmi_get (kvm); +if (!ikvm) +return ; + +if (unlikely (!ivcpu->ss_owner)) { +kvmi_warn (ikvm,"%s\n" , __func__); + goto out; + } + +for (i = ikvm->ss_level; i--;) +kvmi_set_gfn_access (kvm, + ikvm->ss_context[i].gfn, + ikvm->ss_context[i].old_access, + ikvm->ss_context[i].old_write_bitmap); + + ikvm->ss_level =0 ; + +kvmi_arch_stop_single_step (vcpu); + +atomic_set (&ikvm->ss_active, false); +/* +* Make ss_active update visible +* before resuming all the other vCPUs. +*/ +smp_mb__after_atomic (); +kvm_make_all_cpus_request (kvm,0 ); + + ivcpu->ss_owner = false; + +out: +kvmi_put (kvm); +} +EXPORT_SYMBOL (kvmi_stop_ss); + +static bool kvmi_acquire_ss (struct kvm_vcpu *vcpu) +{ +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); +struct kvmi *ikvm =IKVM (vcpu->kvm); + +if (ivcpu->ss_owner) +return true; + +if (atomic_cmpxchg (&ikvm->ss_active, false, true) != false) +return false; + +kvm_make_all_cpus_request (vcpu->kvm,KVM_REQ_INTROSPECTION | +KVM_REQUEST_WAIT ); + + ivcpu->ss_owner = true; + +return true; +} + +static bool kvmi_run_ss (struct kvm_vcpu *vcpu,gpa_t gpa,u8 access) +{ +struct kvmi *ikvm =IKVM (vcpu->kvm); +u8 old_access, new_access; +u32 old_write_bitmap; +gfn_t gfn =gpa_to_gfn (gpa); +int err; + +kvmi_arch_start_single_step (vcpu); + + err =kvmi_get_gfn_access (ikvm, gfn, &old_access, &old_write_bitmap); +/* likely was removed from radix tree due to rwx */ +if (err) { +kvmi_warn (ikvm,"%s: gfn 0x%llx not found in the radix tree\n" , + __func__, gfn); +return true; + } + +if (ikvm->ss_level ==SINGLE_STEP_MAX_DEPTH -1 ) { +kvmi_err (ikvm,"single step limit reached\n" ); +return false; + } + + ikvm->ss_context[ikvm->ss_level].gfn = gfn; + ikvm->ss_context[ikvm->ss_level].old_access = old_access; + ikvm->ss_context[ikvm->ss_level].old_write_bitmap = old_write_bitmap; + ikvm->ss_level++; + + new_access =kvmi_arch_relax_page_access (old_access, access); + +kvmi_set_gfn_access (vcpu->kvm, gfn, new_access, old_write_bitmap); + +return true; +} + +bool kvmi_start_ss (struct kvm_vcpu *vcpu,gpa_t gpa,u8 access) +{ +bool ret = false; + +while (!kvmi_acquire_ss (vcpu)) { +int err =kvmi_run_jobs_and_wait (vcpu); + +if (err) { +kvmi_err (IKVM (vcpu->kvm),"kvmi_acquire_ss() has failed\n" ); + goto out; + } + } + +if (kvmi_run_ss (vcpu, gpa, access)) + ret = true; +else +kvmi_stop_ss (vcpu); + +out: +return ret; +} + +bool kvmi_vcpu_enabled_ss (struct kvm_vcpu *vcpu) +{ +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); +struct kvmi *ikvm; +bool ret; + + ikvm =kvmi_get (vcpu->kvm); +if (!ikvm) +return false; + + ret = ivcpu->ss_owner; + +kvmi_put (vcpu->kvm); + +return ret; +} +EXPORT_SYMBOL (kvmi_vcpu_enabled_ss); +static void kvmi_job_abort (struct kvm_vcpu *vcpu,void *ctx) {struct kvmi_vcpu *ivcpu =IVCPU (vcpu);virt/kvm/kvmi_int.h +0
-0
virt/kvm/kvmi_int.h struct kvmi_vcpu { · line 129
Quoted 4 lines
virt/kvm/kvmi_int.h +1
-0@@ -126,6 +126,9 @@ struct kvmi_vcpu {DECLARE_BITMAP (high,KVMI_NUM_MSR ); } msr_mask; +bool ss_owner;
Why is single-stepping mutually exclusive across all vCPUs? Does that always have to be the case?
Quoted 7 lines
virt/kvm/kvmi_int.h +2
-0+bool ss_requested; +struct list_head job_list;spinlock_t job_lock;@@ -151,6 +154,15 @@ struct kvmi {DECLARE_BITMAP (event_allow_mask,KVMI_NUM_EVENTS );
virt/kvm/kvmi_int.h struct kvmi { · line 164
Quoted 10 lines
virt/kvm/kvmi_int.h +8
-0DECLARE_BITMAP (vm_ev_mask,KVMI_NUM_EVENTS ); +#define SINGLE_STEP_MAX_DEPTH 8 +struct { +gfn_t gfn; +u8 old_access; +u32 old_write_bitmap; + } ss_context[SINGLE_STEP_MAX_DEPTH ]; +u8 ss_level; +atomic_t ss_active;
Good opportunity for an unnamed struct, e.g.
struct {
struct single_step_context[...];
bool owner;
bool requested;
u8 level
atomic_t active;
} single_step;Quoted 13 lines
virt/kvm/kvmi_int.h +2
-0+struct {bool initialized;atomic_t enabled;@@ -224,6 +236,7 @@ int kvmi_add_job(struct kvm_vcpu *vcpu, void *ctx, void (*free_fct)(void *ctx));void kvmi_handle_common_event_actions (struct kvm_vcpu *vcpu,u32 action,const char *str); +bool kvmi_start_ss (struct kvm_vcpu *vcpu,gpa_t gpa,u8 access);/* arch */ void kvmi_arch_update_page_tracking(struct kvm *kvm,
virt/kvm/kvmi_int.h int kvmi_arch_cmd_inject_exception(struct kvm_vcpu *vcpu, u8 vector, · line 295
Quoted 9 lines
virt/kvm/kvmi_int.h +3
-0@@ -274,6 +287,9 @@ int kvmi_arch_cmd_inject_exception(struct kvm_vcpu *vcpu, u8 vector, u64 address);int kvmi_arch_cmd_control_cr (struct kvm_vcpu *vcpu,const struct kvmi_control_cr *req); +void kvmi_arch_start_single_step (struct kvm_vcpu *vcpu); +void kvmi_arch_stop_single_step (struct kvm_vcpu *vcpu); +u8 kvmi_arch_relax_page_access (u8 old,u8 new);int kvmi_arch_cmd_control_msr (struct kvm_vcpu *vcpu,const struct kvmi_control_msr *req);int kvmi_arch_cmd_get_mtrr_type (struct kvm_vcpu *vcpu,u64 gpa,u8 *type);
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [RFC PATCH v6 64/92] kvm: introspection: add single-stepping
Quoted 28 lines · Sean Christopherson
Quoted 27 lines · Adalbert Lazăr
From: Nicușor Cîțu <[email protected]>
This would be used either if the introspection tool request it as a reply to a
KVMI_EVENT_PFevent or to cope with instructions that cannot be handled by the x86 emulator during the handling of a VMEXIT. In these situations, all other vCPU-s are kicked and held, the EPT-based protection is removed and the guest is single stepped by the vCPU that triggered the initial VMEXIT. Upon completion the EPT-base protection is reinstalled and all vCPU-s all allowed to return to the guest.This is a rather slow workaround that kicks in occasionally. In the future, the most frequently single-stepped instructions should be added to the emulator (usually, stores to and from memory - SSE/AVX).
For the moment it works only on Intel.
CC Jim Mattson <[email protected]>CC Sean Christopherson <[email protected]>CC Joerg Roedel <[email protected]>Signed-off-by Nicușor Cîțu <[email protected]>Co-developed-by Mihai Donțu <[email protected]>Signed-off-by Mihai Donțu <[email protected]>Co-developed-by Adalbert Lazăr <[email protected]>Signed-off-by Adalbert Lazăr <[email protected]>--- arch/x86/include/asm/kvm_host.h | 3 + arch/x86/kvm/kvmi.c | 47 ++++++++++-
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/svm.c | 5 ++ arch/x86/kvm/vmx/vmx.c | 17 ++++ arch/x86/kvm/x86.c | 19 +++++ include/linux/kvmi.h | 4 + virt/kvm/kvmi.c | 145 +++++++++++++++++++++++++++++++- virt/kvm/kvmi_int.h | 16 ++++ 8 files changed, 253 insertions(+), 3 deletions(-)
[...] We'll do.
Quoted 8 lines
Quoted 8 lines
virt/kvm/kvmi_int.h +0
-0@@ -126,6 +126,9 @@ struct kvmi_vcpu {DECLARE_BITMAP (high,KVMI_NUM_MSR ); } msr_mask;
Quoted 4 lines
virt/kvm/kvmi_int.h struct kvmi_vcpu { · line 129
Quoted 1 line
virt/kvm/kvmi_int.h +1
-0+bool ss_owner;Why is single-stepping mutually exclusive across all vCPUs? Does that always have to be the case?
I never thought to single-step multiple vCPUs in the same time.
If one vCPU will relax the access to a guest page while a second one, finishing single-stepping, restores the 'r--' flags, the first one will get another page fault and relax the page access again. It might be doable, but before starting single-stepping a vCPU we might replace guest memory (as requested by the introspection tool) and we will have to use a lock for this.
However, we would like to use alternate EPT views with single-step. So, we might replace this patch.
Quoted 17 lines
Quoted 17 lines
virt/kvm/kvmi_int.h +10
-0+bool ss_requested; +struct list_head job_list;spinlock_t job_lock;@@ -151,6 +154,15 @@ struct kvmi {DECLARE_BITMAP (event_allow_mask,KVMI_NUM_EVENTS );DECLARE_BITMAP (vm_ev_mask,KVMI_NUM_EVENTS ); +#define SINGLE_STEP_MAX_DEPTH 8 +struct { +gfn_t gfn; +u8 old_access; +u32 old_write_bitmap; + } ss_context[SINGLE_STEP_MAX_DEPTH ]; +u8 ss_level; +atomic_t ss_active;
Re: [RFC PATCH v6 64/92] kvm: introspection: add single-stepping
In reply to Sean ChristophersonQuoted 28 lines · Sean Christopherson
Quoted 27 lines · Adalbert Lazăr
From: Nicușor Cîțu <[email protected]>
This would be used either if the introspection tool request it as a reply to a
KVMI_EVENT_PFevent or to cope with instructions that cannot be handled by the x86 emulator during the handling of a VMEXIT. In these situations, all other vCPU-s are kicked and held, the EPT-based protection is removed and the guest is single stepped by the vCPU that triggered the initial VMEXIT. Upon completion the EPT-base protection is reinstalled and all vCPU-s all allowed to return to the guest.This is a rather slow workaround that kicks in occasionally. In the future, the most frequently single-stepped instructions should be added to the emulator (usually, stores to and from memory - SSE/AVX).
For the moment it works only on Intel.
CC Jim Mattson <[email protected]>CC Sean Christopherson <[email protected]>CC Joerg Roedel <[email protected]>Signed-off-by Nicușor Cîțu <[email protected]>Co-developed-by Mihai Donțu <[email protected]>Signed-off-by Mihai Donțu <[email protected]>Co-developed-by Adalbert Lazăr <[email protected]>Signed-off-by Adalbert Lazăr <[email protected]>--- arch/x86/include/asm/kvm_host.h | 3 + arch/x86/kvm/kvmi.c | 47 ++++++++++-
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/svm.c | 5 ++ arch/x86/kvm/vmx/vmx.c | 17 ++++ arch/x86/kvm/x86.c | 19 +++++ include/linux/kvmi.h | 4 + virt/kvm/kvmi.c | 145 +++++++++++++++++++++++++++++++- virt/kvm/kvmi_int.h | 16 ++++ 8 files changed, 253 insertions(+), 3 deletions(-)
[...] We'll do.
Quoted 8 lines
Quoted 8 lines
virt/kvm/kvmi_int.h +0
-0@@ -126,6 +126,9 @@ struct kvmi_vcpu {DECLARE_BITMAP (high,KVMI_NUM_MSR ); } msr_mask;
Quoted 4 lines
virt/kvm/kvmi_int.h struct kvmi_vcpu { · line 129
Quoted 1 line
virt/kvm/kvmi_int.h +1
-0+bool ss_owner;Why is single-stepping mutually exclusive across all vCPUs? Does that always have to be the case?
I never thought to single-step multiple vCPUs in the same time.
If one vCPU will relax the access to a guest page while a second one, finishing single-stepping, restores the 'r--' flags, the first one will get another page fault and relax the page access again. It might be doable, but before starting single-stepping a vCPU we might replace guest memory (as requested by the introspection tool) and we will have to use a lock for this.
However, we would like to use alternate EPT views with single-step. So, we might replace this patch.
Quoted 9 lines
Quoted 9 lines
virt/kvm/kvmi_int.h +2
-0+bool ss_requested; +struct list_head job_list;spinlock_t job_lock;@@ -151,6 +154,15 @@ struct kvmi {DECLARE_BITMAP (event_allow_mask,KVMI_NUM_EVENTS );DECLARE_BITMAP (vm_ev_mask,KVMI_NUM_EVENTS );
Quoted 8 lines
virt/kvm/kvmi_int.h struct kvmi { · line 164
Quoted 8 lines
virt/kvm/kvmi_int.h +8
-0+#define SINGLE_STEP_MAX_DEPTH 8 +struct { +gfn_t gfn; +u8 old_access; +u32 old_write_bitmap; + } ss_context[SINGLE_STEP_MAX_DEPTH ]; +u8 ss_level; +atomic_t ss_active;
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [RFC PATCH v6 64/92] kvm: introspection: add single-stepping
In reply to Sean ChristophersonQuoted 115 lines · Sean Christopherson
Quoted 47 lines · Adalbert Lazăr
From: Nicușor Cîțu <[email protected]>
This would be used either if the introspection tool request it as a reply to a
KVMI_EVENT_PFevent or to cope with instructions that cannot
be handled by the x86 emulator during the handling of a VMEXIT. In these situations, all other vCPU-s are kicked and held, the EPT- based
protection is removed and the guest is single stepped by the vCPU that
triggered the initial VMEXIT. Upon completion the EPT-base protection
is reinstalled and all vCPU-s all allowed to return to the guest.This is a rather slow workaround that kicks in occasionally. In the future, the most frequently single-stepped instructions should be added
to the emulator (usually, stores to and from memory - SSE/AVX).For the moment it works only on Intel.
CC Jim Mattson <[email protected]>CC Sean Christopherson <[email protected]>CC Joerg Roedel <[email protected]>Signed-off-by Nicușor Cîțu <[email protected]>Co-developed-by Mihai Donțu <[email protected]>Signed-off-by Mihai Donțu <[email protected]>Co-developed-by Adalbert Lazăr <[email protected]>Signed-off-by Adalbert Lazăr <[email protected]>--- arch/x86/include/asm/kvm_host.h | 3 + arch/x86/kvm/kvmi.c | 47 ++++++++++- arch/x86/kvm/svm.c | 5 ++ arch/x86/kvm/vmx/vmx.c | 17 ++++ arch/x86/kvm/x86.c | 19 +++++ include/linux/kvmi.h | 4 + virt/kvm/kvmi.c | 145 +++++++++++++++++++++++++++++++- virt/kvm/kvmi_int.h | 16 ++++ 8 files changed, 253 insertions(+), 3 deletions(-)arch/x86/include/asm/kvm_host.h +0
-0arch/x86/include/asm/kvm_host.h struct kvm_x86_ops { · line 1019
Quoted 4 lines
arch/x86/include/asm/kvm_host.h +1
-0@@ -1016,6 +1016,7 @@ struct kvm_x86_ops {void (*msr_intercept)(struct kvm_vcpu *vcpu,unsigned int msr,bool enable);bool (*desc_intercept)(struct kvm_vcpu *vcpu,bool enable); +void (*set_mtf)(struct kvm_vcpu *vcpu,bool enable);MTF is a VMX specific implementation of single-stepping, this should be
enable_single_step()or something along those lines. For example, I assume
SVM could implement something that is mostly functional via RFLAGS.TF.Quoted 55 lines
arch/x86/include/asm/kvm_host.h +2
-0void (*cr3_write_exiting)(struct kvm_vcpu *vcpu,bool enable);bool (*nested_pagefault)(struct kvm_vcpu *vcpu);bool (*spt_fault)(struct kvm_vcpu *vcpu);@@ -1628,6 +1629,8 @@ void kvm_arch_msr_intercept(struct kvm_vcpu *vcpu, unsigned int msr, bool enable);bool kvm_mmu_nested_pagefault (struct kvm_vcpu *vcpu);bool kvm_spt_fault (struct kvm_vcpu *vcpu); +void kvm_set_mtf (struct kvm_vcpu *vcpu,bool enable); +void kvm_set_interrupt_shadow (struct kvm_vcpu *vcpu,int mask);void kvm_control_cr3_write_exiting (struct kvm_vcpu *vcpu,bool enable);#endif /*
_ASM_X86_KVM_HOST_H*/arch/x86/kvm/kvmi.c +0
-1@@ -520,7 +520,6 @@ bool kvmi_arch_pf_event(struct kvm_vcpu *vcpu, gpa_t gpa, gva_t gva, u32 ctx_size;u64 ctx_addr;u32 action; -bool singlestep_ignored;bool ret = false;arch/x86/kvm/kvmi.c +1
-1if (!kvm_spt_fault (vcpu))@@ -533,7 +532,7 @@ bool kvmi_arch_pf_event(struct kvm_vcpu *vcpu, gpa_t gpa, gva_t gva, if (ivcpu->effective_rep_complete) return true; - action = kvmi_msg_send_pf(vcpu, gpa, gva, access, &singlestep_ignored, + action = kvmi_msg_send_pf(vcpu, gpa, gva, access, &ivcpu- >ss_requested,arch/x86/kvm/kvmi.c +2
-0&ivcpu->rep_complete, &ctx_addr, ivcpu->ctx_data, &ctx_size);@@ -547,6 +546,8 @@ bool kvmi_arch_pf_event(struct kvm_vcpu *vcpu, gpa_t gpa, gva_t gva, ret = true;break ;case KVMI_EVENT_ACTION_RETRY : +if (ivcpu->ss_requested && !kvmi_start_ss (vcpu, gpa, access)) + ret = true;break ;arch/x86/kvm/kvmi.c +0
-0default :kvmi_handle_common_event_actions (vcpu, action,"PF" );@@ -758,6 +759,48 @@ int kvmi_arch_cmd_control_cr(struct kvm_vcpu *vcpu, return 0; }
Quoted 16 lines
arch/x86/kvm/kvmi.c int kvmi_arch_cmd_control_cr(struct kvm_vcpu *vcpu, · line 771
Quoted 10 lines
arch/x86/kvm/kvmi.c +10
-0+void kvmi_arch_start_single_step (struct kvm_vcpu *vcpu) +{ +kvm_set_mtf (vcpu, true); + +/* +* Set block by STI only if the RFLAGS.IF = 1. +* Blocking by both STI and MOV/POP SS is not possible. +*/ +if (kvm_arch_interrupt_allowed (vcpu)) +kvm_set_interrupt_shadow (vcpu,KVM_X86_SHADOW_INT_STI );This is wrong, the STI shadow only exists if interrupts were unblocked
prior to STI. I'm guessing this is a hack to workaroundkvmi_arch_stop_single_step()not properly handling the clearing case.
Thank you for signaling this. This piece of code is leftover from the initial attempt to make single step running.
Based on latest results, we do not actually need to change interruptibility during the singlestep. It is enough to enable the MTF and just suppress any interrupt injection (if any) before leaving the vcpu entering in guest.
Quoted 452 lines
arch/x86/kvm/kvmi.c int kvmi_arch_cmd_control_cr(struct kvm_vcpu *vcpu, · line 784
Quoted 12 lines
arch/x86/kvm/kvmi.c +12
-0++} + +void kvmi_arch_stop_single_step (struct kvm_vcpu *vcpu) +{ +kvm_set_mtf (vcpu, false); +/* +* The blocking by STI is cleared after the guest +* executes one instruction or incurs an exception. +* However we migh stop the SS before entering to guest, +* so be sure we are clearing the STI blocking. +*/ +kvm_set_interrupt_shadow (vcpu,0 );There are only three callers of
kvmi_stop_ss(), it should be possible to accurately update interruptibility:-
kvmi_run_ss()fail, do nothing
- VM-Exit that wasn't a single-step - clear interruptibility if the guest executed an instruction (including faulted on an instr).
- MTF VM-Exit - do nothing (VMCS should already be up-to-date).Quoted 62 lines
arch/x86/kvm/kvmi.c +19
-0+} + +u8 kvmi_arch_relax_page_access (u8 old,u8 new) +{ +u8 ret = old | new; + +/* +* An SPTE entry with just the -wx bits set can trigger a +* misconfiguration error from the hardware, as it's the case +* for x86 where this access mode is used to mark I/O memory. +* Thus, we make sure that -wx accesses are translated to rwx. +*/ +if ((ret & (KVMI_PAGE_ACCESS_W |KVMI_PAGE_ACCESS_X )) == + (KVMI_PAGE_ACCESS_W |KVMI_PAGE_ACCESS_X )) + ret |=KVMI_PAGE_ACCESS_R ; + +return ret; +} + static const struct { unsigned int allow_bit; enum kvm_page_track_mode track_mode;arch/x86/kvm/svm.c +4
-0@@ -7183,6 +7183,10 @@ static bool svm_spt_fault(struct kvm_vcpu *vcpu)return (svm->vmcb->control.exit_code ==SVM_EXIT_NPF ); } +static void svm_set_mtf (struct kvm_vcpu *vcpu,bool enable) +{ +} +static void svm_cr3_write_exiting (struct kvm_vcpu *vcpu,bool enable){
}arch/x86/kvm/svm.c +1
-0@@ -7225,6 +7229,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, .has_emulated_msr = svm_has_emulated_msr, + .set_mtf = svm_set_mtf, .cr3_write_exiting = svm_cr3_write_exiting, .msr_intercept = svm_msr_intercept,arch/x86/kvm/svm.c +0
-0.desc_intercept = svm_desc_intercept,arch/x86/kvm/vmx/vmx.c +1
-0@@ -5384,6 +5384,7 @@ static int handle_invalid_op(struct kvm_vcpu *vcpu)static int handle_monitor_trap (struct kvm_vcpu *vcpu) { +kvmi_stop_ss (vcpu);return 1 ; }arch/x86/kvm/vmx/vmx.c static int vmx_handle_exit(struct kvm_vcpu · line 6000
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +3
-0@@ -5992,6 +5993,11 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu) } } +if (kvmi_vcpu_enabled_ss (vcpu) + && exit_reason !=EXIT_REASON_EPT_VIOLATION + && exit_reason !=EXIT_REASON_MONITOR_TRAP_FLAG )Bad indentation. This is prevelant through the series.
Quoted 57 lines
arch/x86/kvm/vmx/vmx.c +2
-0+kvmi_stop_ss (vcpu); + if (exit_reason < kvm_vmx_max_exit_handlersarch/x86/kvm/vmx/vmx.c +10
-0&& kvm_vmx_exit_handlers[exit_reason])return kvm_vmx_exit_handlers[exit_reason](vcpu);@@ -7842,6 +7848,16 @@ static __exit void hardware_unsetup(void)free_kvm_area (); } +static void vmx_set_mtf (struct kvm_vcpu *vcpu,bool enable) +{ +if (enable) +vmcs_set_bits (CPU_BASED_VM_EXEC_CONTROL , +CPU_BASED_MONITOR_TRAP_FLAG ); +else +vmcs_clear_bits (CPU_BASED_VM_EXEC_CONTROL , +CPU_BASED_MONITOR_TRAP_FLAG ); +} +static void vmx_msr_intercept (struct kvm_vcpu *vcpu,unsigned int msr,bool enable){
arch/x86/kvm/vmx/vmx.c +1
-0@@ -7927,6 +7943,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .cpu_has_accelerated_tpr = report_flexpriority, .has_emulated_msr = vmx_has_emulated_msr, + .set_mtf = vmx_set_mtf, .msr_intercept = vmx_msr_intercept, .cr3_write_exiting = vmx_cr3_write_exiting,arch/x86/kvm/vmx/vmx.c +0
-0.desc_intercept = vmx_desc_intercept,arch/x86/kvm/x86.c +7
-0@@ -7358,6 +7358,13 @@ static int inject_pending_event(struct kvm_vcpu *vcpu, bool req_int_win) {int r; +if (kvmi_vcpu_enabled_ss (vcpu)) +/* +* We cannot inject events during single-stepping. +* Try again later. +*/ +return -1 ; +/* try to reinject previous events if any */ arch/x86/kvm/x86.c +1
-0if (vcpu->arch.exception.injected)@@ -10134,6 +10141,18 @@ void kvm_control_cr3_write_exiting(struct kvm_vcpu *vcpu, bool enable) }EXPORT_SYMBOL (kvm_control_cr3_write_exiting ); +void kvm_set_mtf (struct kvm_vcpu *vcpu,bool enable)arch/x86/kvm/x86.c void kvm_control_cr3_write_exiting(struct · line 10155
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+{ + kvm_x86_ops->set_mtf (vcpu, enable); +} +EXPORT_SYMBOL (kvm_set_mtf); + +void kvm_set_interrupt_shadow (struct kvm_vcpu *vcpu,int mask) +{ + kvm_x86_ops->set_interrupt_shadow (vcpu, mask); +} +EXPORT_SYMBOL (kvm_set_interrupt_shadow);Why do these wrappers exist, and why are they
exported? Introspection is
built into kvm, any reason not to usekvm_x86_opsdirectly? The most definitely don't need to be exported.Quoted 9 lines
arch/x86/kvm/x86.c +1
-0+bool kvm_spt_fault (struct kvm_vcpu *vcpu) {arch/x86/kvm/x86.c +0
-0return kvm_x86_ops->spt_fault (vcpu);include/linux/kvmi.h +0
-0include/linux/kvmi.h bool kvmi_queue_exception(struct kvm_vcpu *vcpu); · line 27
Quoted 6 lines
include/linux/kvmi.h +2
-0@@ -22,6 +22,8 @@ bool kvmi_queue_exception(struct kvm_vcpu *vcpu);void kvmi_trap_event (struct kvm_vcpu *vcpu);bool kvmi_descriptor_event (struct kvm_vcpu *vcpu,u8 descriptor,u8 write);void kvmi_handle_requests (struct kvm_vcpu *vcpu); +void kvmi_stop_ss (struct kvm_vcpu *vcpu); +bool kvmi_vcpu_enabled_ss (struct kvm_vcpu *vcpu);Spell out single step, and be consistent between
single_stepand singlestep.
That applies to pretty much every variable and function unless doing so
really makes the verbosity obnoxious.Quoted 198 lines
include/linux/kvmi.h +0
-0void kvmi_init_emulate (struct kvm_vcpu *vcpu);void kvmi_activate_rep_complete (struct kvm_vcpu *vcpu);bool
kvmi_bp_intercepted(struct kvm_vcpu *vcpu, u32 dbg);include/linux/kvmi.h +2
-0@@ -44,6 +46,8 @@ static inline void kvmi_handle_requests(struct kvm_vcpu *vcpu) { }static inline bool kvmi_hypercall_event (struct kvm_vcpu *vcpu) {return false; }static inline bool kvmi_queue_exception (struct kvm_vcpu *vcpu) {return true; }static inline void kvmi_trap_event (struct kvm_vcpu *vcpu) { } +static inline void kvmi_stop_ss (struct kvm_vcpu *vcpu) { } +static inline bool kvmi_vcpu_enabled_ss (struct kvm_vcpu *vcpu) {return false; }
static inline voidkvmi_init_emulate(struct kvm_vcpu *vcpu){ } static inline void kvmi_activate_rep_complete(structkvm_vcpu
*vcpu) { }
static inline bool kvmi_bp_intercepted(structkvm_vcpu*vcpu, u32
dbg)virt/kvm/kvmi.c +80
-1@@ -1260,11 +1260,19 @@ void kvmi_run_jobs(struct kvm_vcpu *vcpu) } } +static bool need_to_wait_for_ss (struct kvm_vcpu *vcpu) +{ +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); +struct kvmi *ikvm =IKVM (vcpu->kvm); + +return atomic_read (&ikvm->ss_active) && !ivcpu->ss_owner; +} +static bool need_to_wait (struct kvm_vcpu *vcpu) {struct kvmi_vcpu *ivcpu =IVCPU (vcpu); -return ivcpu->reply_waiting; +return ivcpu->reply_waiting ||need_to_wait_for_ss (vcpu); }static bool done_waiting (struct kvm_vcpu *vcpu)@@ -1572,6 +1580,141 @@ int kvmi_cmd_pause_vcpu(struct kvm_vcpu *vcpu, bool wait)return 0 ; } +void kvmi_stop_ss (struct kvm_vcpu *vcpu) +{ +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); +struct kvm *kvm = vcpu->kvm; +struct kvmi *ikvm; +int i; + + ikvm =kvmi_get (kvm); +if (!ikvm) +return ; + +if (unlikely (!ivcpu->ss_owner)) { +kvmi_warn (ikvm,"%s\n" , __func__); + goto out; + } + +for (i = ikvm->ss_level; i--;) +kvmi_set_gfn_access (kvm, + ikvm->ss_context[i].gfn, + ikvm->ss_context[i].old_access, + ikvm- >ss_context[i].old_write_bitmap); + + ikvm->ss_level =0 ; + +kvmi_arch_stop_single_step (vcpu); + +atomic_set (&ikvm->ss_active, false); +/* +* Make ss_active update visible +* before resuming all the other vCPUs. +*/ +smp_mb__after_atomic (); +kvm_make_all_cpus_request (kvm,0 ); + + ivcpu->ss_owner = false; + +out: +kvmi_put (kvm); +} +EXPORT_SYMBOL (kvmi_stop_ss); + +static bool kvmi_acquire_ss (struct kvm_vcpu *vcpu) +{ +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); +struct kvmi *ikvm =IKVM (vcpu->kvm); + +if (ivcpu->ss_owner) +return true; + +if (atomic_cmpxchg (&ikvm->ss_active, false, true) != false) +return false; + +kvm_make_all_cpus_request (vcpu->kvm,KVM_REQ_INTROSPECTION | +KVM_REQUEST_WAIT ); + + ivcpu->ss_owner = true; + +return true; +} + +static bool kvmi_run_ss (struct kvm_vcpu *vcpu,gpa_t gpa,u8 access) +{ +struct kvmi *ikvm =IKVM (vcpu->kvm); +u8 old_access, new_access; +u32 old_write_bitmap; +gfn_t gfn =gpa_to_gfn (gpa); +int err; + +kvmi_arch_start_single_step (vcpu); + + err = kvmi_get_gfn_access(ikvm, gfn, &old_access,
&old_write_bitmap);virt/kvm/kvmi.c +3
-0+/* likely was removed from radix tree due to rwx */ +if (err) { + kvmi_warn(ikvm, "%s: gfn 0x%llx not found in the radixtree\n",
virt/kvm/kvmi.c +12
-0+ __func__, gfn); +return true; + } + +if (ikvm->ss_level ==SINGLE_STEP_MAX_DEPTH -1 ) { +kvmi_err (ikvm,"single step limit reached\n" ); +return false; + } + + ikvm->ss_context[ikvm->ss_level].gfn = gfn; + ikvm->ss_context[ikvm->ss_level].old_access = old_access; + ikvm->ss_context[ikvm->ss_level].old_write_bitmap =
old_write_bitmap;virt/kvm/kvmi.c +5
-0+ ikvm->ss_level++; + + new_access =kvmi_arch_relax_page_access (old_access, access); + + kvmi_set_gfn_access(vcpu->kvm, gfn, new_access,
old_write_bitmap);virt/kvm/kvmi.c +13
-0+ +return true; +} + +bool kvmi_start_ss (struct kvm_vcpu *vcpu,gpa_t gpa,u8 access) +{ +bool ret = false; + +while (!kvmi_acquire_ss (vcpu)) { +int err =kvmi_run_jobs_and_wait (vcpu); + +if (err) { + kvmi_err(IKVM(vcpu->kvm), "kvmi_acquire_ss()has failed\n");
virt/kvm/kvmi.c +31
-0+ goto out; + } + } + +if (kvmi_run_ss (vcpu, gpa, access)) + ret = true; +else +kvmi_stop_ss (vcpu); + +out: +return ret; +} + +bool kvmi_vcpu_enabled_ss (struct kvm_vcpu *vcpu) +{ +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); +struct kvmi *ikvm; +bool ret; + + ikvm =kvmi_get (vcpu->kvm); +if (!ikvm) +return false; + + ret = ivcpu->ss_owner; + +kvmi_put (vcpu->kvm); + +return ret; +} +EXPORT_SYMBOL (kvmi_vcpu_enabled_ss); +static void kvmi_job_abort (struct kvm_vcpu *vcpu,void *ctx) {struct kvmi_vcpu *ivcpu =IVCPU (vcpu);virt/kvm/kvmi_int.h +0
-0virt/kvm/kvmi_int.h struct kvmi_vcpu { · line 129
Quoted 4 lines
virt/kvm/kvmi_int.h +1
-0@@ -126,6 +126,9 @@ struct kvmi_vcpu {DECLARE_BITMAP (high,KVMI_NUM_MSR ); } msr_mask; +bool ss_owner;Why is single-stepping mutually exclusive across all vCPUs? Does that
always have to be the case?Quoted 7 lines
virt/kvm/kvmi_int.h +2
-0+bool ss_requested; +struct list_head job_list;spinlock_t job_lock;@@ -151,6 +154,15 @@ struct kvmi {DECLARE_BITMAP (event_allow_mask,KVMI_NUM_EVENTS );virt/kvm/kvmi_int.h struct kvmi { · line 164
Quoted 10 lines
virt/kvm/kvmi_int.h +8
-0DECLARE_BITMAP (vm_ev_mask,KVMI_NUM_EVENTS ); +#define SINGLE_STEP_MAX_DEPTH 8 +struct { +gfn_t gfn; +u8 old_access; +u32 old_write_bitmap; + } ss_context[SINGLE_STEP_MAX_DEPTH ]; +u8 ss_level; +atomic_t ss_active;Good opportunity for an unnamed struct, e.g.
struct { struct single_step_context[...]; bool owner; bool requested; u8 level atomic_t active; } single_step;Quoted 23 lines
virt/kvm/kvmi_int.h +2
-0+struct {bool initialized;atomic_t enabled;@@ -224,6 +236,7 @@ int kvmi_add_job(struct kvm_vcpu *vcpu, void *ctx, void (*free_fct)(void *ctx));void kvmi_handle_common_event_actions (struct kvm_vcpu *vcpu,u32 action,const char *str); +bool kvmi_start_ss (struct kvm_vcpu *vcpu,gpa_t gpa,u8 access);/* arch */ void kvmi_arch_update_page_tracking(struct kvm *kvm,
virt/kvm/kvmi_int.h +3
-0@@ -274,6 +287,9 @@ int kvmi_arch_cmd_inject_exception(struct kvm_vcpu *vcpu, u8 vector, u64 address);int kvmi_arch_cmd_control_cr (struct kvm_vcpu *vcpu,const struct kvmi_control_cr *req); +void kvmi_arch_start_single_step (struct kvm_vcpu *vcpu); +void kvmi_arch_stop_single_step (struct kvm_vcpu *vcpu); +u8 kvmi_arch_relax_page_access (u8 old,u8 new);int kvmi_arch_cmd_control_msr (struct kvm_vcpu *vcpu,const struct kvmi_control_msr *req);Quoted 2 lines
int kvmi_arch_cmd_get_mtrr_type(struct
kvm_vcpu*vcpu, u64 gpa, u8
*type);________________________
This email was scanned by Bitdefender
Paolo Bonzini
maintainerRe: [RFC PATCH v6 64/92] kvm: introspection: add single-stepping
Quoted 7 lines · Nicusor CITU
Thank you for signaling this. This piece of code is leftover from the initial attempt to make single step running.
Based on latest results, we do not actually need to change interruptibility during the singlestep. It is enough to enable the MTF and just suppress any interrupt injection (if any) before leaving the vcpu entering in guest.
This is exactly what testcases are for...
Paolo
RFC PATCH v6 65/92 kvm: introspection: add KVMI_EVENT_SINGLESTEP
In reply to Adalbert LazărFrom: Nicușor Cîțu <[email protected]>
This event is sent when the current instruction has been single stepped as a result of a KVMI_EVENT_PF event to which the introspection tool set the singlestep field and responded with CONTINUE.
--- Documentation/virtual/kvm/kvmi.rst | 25 +++++++++++++++++++ virt/kvm/kvmi.c | 40 ++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+)
Documentation/virtual/kvm/kvmi.rst +25 -0
@@ -1574,3 +1574,28 @@ introspection has been enabled for this event (see **KVMI_CONTROL_EVENTS**). KVMI_DESC_TR ``write`` is 1 if the descriptor was written, 0 otherwise. + +12. KVMI_EVENT_SINGLESTEP +------------------------- + +:Architectures: x86 +:Versions: >= 1 +:Actions: CONTINUE, CRASH +:Parameters: + +:: + + struct kvmi_event; + +:Returns: + +:: + + struct kvmi_vcpu_hdr; + struct kvmi_event_reply; + +This event is sent when the current instruction has been executed +(as a result of a *KVMI_EVENT_PF* event to which the introspection +tool set the ``singlestep`` field and responded with *CONTINUE*) +and the introspection has been enabled for this event +(see **KVMI_CONTROL_EVENTS**).
virt/kvm/kvmi.c +40 -0
@@ -1182,6 +1182,44 @@ void kvmi_trap_event(struct kvm_vcpu *vcpu)kvmi_put (vcpu->kvm); } +static u32 kvmi_send_singlestep (struct kvm_vcpu *vcpu) +{ +int err, action; + + err =kvmi_send_event (vcpu,KVMI_EVENT_SINGLESTEP ,NULL ,0 , +NULL ,0 , &action); +if (err) +return KVMI_EVENT_ACTION_CONTINUE ; + +return action; +} + +static void __kvmi_singlestep_event (struct kvm_vcpu *vcpu) +{ +u32 action; + + action =kvmi_send_singlestep (vcpu); +switch (action) { +case KVMI_EVENT_ACTION_CONTINUE : +break ; +default : +kvmi_handle_common_event_actions (vcpu, action,"SINGLESTEP" ); + } +} + +static void kvmi_singlestep_event (struct kvm_vcpu *vcpu) +{ +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); + +if (!ivcpu->ss_requested) +return ; + +if (is_event_enabled (vcpu,KVMI_EVENT_SINGLESTEP )) +__kvmi_singlestep_event (vcpu); + + ivcpu->ss_requested = false; +} +static bool __kvmi_create_vcpu_event (struct kvm_vcpu *vcpu) {u32 action;
@@ -1616,6 +1654,8 @@ void kvmi_stop_ss(struct kvm_vcpu *vcpu) ivcpu->ss_owner = false; +kvmi_singlestep_event (vcpu); + out:kvmi_put (kvm); }
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 66/92 kvm: introspection: add custom input when single-stepping a vCPU
In reply to Adalbert LazărThe introspection tool can respond to a KVMI_EVENT_PF event with custom input for the current instruction. This input is used to trick the guest software into believing it has read certain data, in order to hide the content of certain memory areas (eg. hide injected code from integrity checkers). There are cases when this can happen while the vCPU has to be single stepped, Either the current instruction is not supported by the KVM emulator or the introspection tool requested single-stepping.
This patch saves the old data, write the custom input, start the single stepping and restore the old data.
--- virt/kvm/kvmi.c | 119 ++++++++++++++++++++++++++++++++++++++++++++ virt/kvm/kvmi_int.h | 3 ++ 2 files changed, 122 insertions(+)
virt/kvm/kvmi.c +119 -0
@@ -1618,6 +1618,116 @@ int kvmi_cmd_pause_vcpu(struct kvm_vcpu *vcpu, bool wait)return 0 ; } +static int write_custom_data_to_page (struct kvm_vcpu *vcpu,gva_t gva, +u8 *backup,size_t bytes) +{ +u8 *ptr_page, *ptr; +struct page *page; +gpa_t gpa; + + gpa =kvm_mmu_gva_to_gpa_system (vcpu, gva,NULL ); +if (gpa ==UNMAPPED_GVA ) +return -KVM_EINVAL ; + + ptr_page =get_page_ptr (vcpu->kvm, gpa, &page, true); +if (!ptr_page) +return -KVM_EINVAL ; + + ptr = ptr_page + (gpa & ~PAGE_MASK ); + +memcpy (backup, ptr, bytes); +use_custom_input (vcpu, gva, ptr, bytes); + +put_page_ptr (ptr_page, page); + +return 0 ; +} + +static int write_custom_data (struct kvm_vcpu *vcpu) +{ +struct kvmi *ikvm =IKVM (vcpu->kvm); +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); +size_t bytes = ivcpu->ctx_size; +gva_t gva = ivcpu->ctx_addr; +u8 *backup; + +if (ikvm->ss_custom_size) +return 0 ; + +if (!bytes) +return 0 ; + + backup = ikvm->ss_custom_data; + +while (bytes) { +size_t offset = gva & ~PAGE_MASK ; +size_t chunk =min (bytes,PAGE_SIZE - offset); + +if (write_custom_data_to_page (vcpu, gva, backup, chunk)) +return -KVM_EINVAL ; + + bytes -= chunk; + backup += chunk; + gva += chunk; + ikvm->ss_custom_size += chunk; + } + +return 0 ; +} + +static int restore_backup_data_to_page (struct kvm_vcpu *vcpu,gva_t gva, +u8 *src,size_t bytes) +{ +u8 *ptr_page, *ptr; +struct page *page; +gpa_t gpa; + + gpa =kvm_mmu_gva_to_gpa_system (vcpu, gva,NULL ); +if (gpa ==UNMAPPED_GVA ) +return -KVM_EINVAL ; + + ptr_page =get_page_ptr (vcpu->kvm, gpa, &page, true); +if (!ptr_page) +return -KVM_EINVAL ; + + ptr = ptr_page + (gpa & ~PAGE_MASK ); + +memcpy (ptr, src, bytes); + +put_page_ptr (ptr_page, page); + +return 0 ; +} + +static void restore_backup_data (struct kvm_vcpu *vcpu) +{ +struct kvmi *ikvm =IKVM (vcpu->kvm); +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); +size_t bytes = ikvm->ss_custom_size; +gva_t gva = ivcpu->ctx_addr; +u8 *backup; + +if (!bytes) +return ; + + backup = ikvm->ss_custom_data; + +while (bytes) { +size_t offset = gva & ~PAGE_MASK ; +size_t chunk =min (bytes,PAGE_SIZE - offset); + +if (restore_backup_data_to_page (vcpu, gva, backup, chunk)) + goto out; + + bytes -= chunk; + backup += chunk; + gva += chunk; + } + +out: + ikvm->ss_custom_size =0 ; +} +void kvmi_stop_ss (struct kvm_vcpu *vcpu) {struct kvmi_vcpu *ivcpu =IVCPU (vcpu);
@@ -1642,6 +1752,8 @@ void kvmi_stop_ss(struct kvm_vcpu *vcpu) ikvm->ss_level =0 ; +restore_backup_data (vcpu); +kvmi_arch_stop_single_step (vcpu);atomic_set (&ikvm->ss_active, false);
@@ -1676,6 +1788,7 @@ static bool kvmi_acquire_ss(struct kvm_vcpu *vcpu) KVM_REQUEST_WAIT); ivcpu->ss_owner = true; + ikvm->ss_custom_size =0 ;return true; }
@@ -1690,6 +1803,12 @@ static bool kvmi_run_ss(struct kvm_vcpu *vcpu, gpa_t gpa, u8 access)kvmi_arch_start_single_step (vcpu); + err =write_custom_data (vcpu); +if (err) { +kvmi_err (ikvm,"writing custom data failed, err %d\n" , err); +return false; + } + err =kvmi_get_gfn_access (ikvm, gfn, &old_access, &old_write_bitmap);/* likely was removed from radix tree due to rwx */ if (err) {
virt/kvm/kvmi_int.h +3 -0
@@ -160,6 +160,9 @@ struct kvmi {u8 old_access;u32 old_write_bitmap; } ss_context[SINGLE_STEP_MAX_DEPTH ]; +u8 ss_custom_data[KVMI_CTX_DATA_SIZE ]; +size_t ss_custom_size; +gpa_t ss_custom_addr;u8 ss_level;atomic_t ss_active;
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 67/92 kvm: introspection: use single stepping on unimplemented instructions
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
On emulation failures, we notify the introspection tool for read/write operations if needed. Unless it responds with RETRY (to re-enter guest), we continue single stepping the vCPU.
--- arch/x86/include/asm/kvm_host.h | 5 +++ arch/x86/include/asm/vmx.h | 2 ++ arch/x86/kvm/kvmi.c | 21 ++++++++++++ arch/x86/kvm/mmu.c | 5 +++ arch/x86/kvm/svm.c | 8 +++++ arch/x86/kvm/vmx/vmx.c | 13 ++++++-- arch/x86/kvm/x86.c | 57 ++++++++++++++++++++++++++++++++- include/linux/kvmi.h | 4 +++ virt/kvm/kvmi.c | 56 ++++++++++++++++++++++++++++++++ virt/kvm/kvmi_int.h | 1 + 10 files changed, 169 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm_host.h +5 -0
@@ -770,6 +770,9 @@ struct kvm_vcpu_arch {/* set at EPT violation at this point */ unsigned long exit_qualification; +/* #PF translated error code from EPT/NPT exit reason */ +u64 error_code; +/* pv related host specific info */ struct {bool pv_unhalted;
@@ -1016,6 +1019,7 @@ struct kvm_x86_ops {void (*msr_intercept)(struct kvm_vcpu *vcpu,unsigned int msr,bool enable);bool (*desc_intercept)(struct kvm_vcpu *vcpu,bool enable); +u64 (*fault_gla)(struct kvm_vcpu *vcpu);void (*set_mtf)(struct kvm_vcpu *vcpu,bool enable);void (*cr3_write_exiting)(struct kvm_vcpu *vcpu,bool enable);bool (*nested_pagefault)(struct kvm_vcpu *vcpu);
@@ -1627,6 +1631,7 @@ static inline int kvm_cpu_get_apicid(int mps_cpu)void kvm_arch_msr_intercept (struct kvm_vcpu *vcpu,unsigned int msr,bool enable); +u64 kvm_mmu_fault_gla (struct kvm_vcpu *vcpu);bool kvm_mmu_nested_pagefault (struct kvm_vcpu *vcpu);bool kvm_spt_fault (struct kvm_vcpu *vcpu);void kvm_set_mtf (struct kvm_vcpu *vcpu,bool enable);
arch/x86/include/asm/vmx.h +2 -0
@@ -538,6 +538,7 @@ struct vmx_msr_entry {#define EPT_VIOLATION_READABLE_BIT 3#define EPT_VIOLATION_WRITABLE_BIT 4#define EPT_VIOLATION_EXECUTABLE_BIT 5 +#define EPT_VIOLATION_GLA_VALID_BIT 7#define EPT_VIOLATION_GVA_TRANSLATED_BIT 8#define EPT_VIOLATION_ACC_READ (1 << EPT_VIOLATION_ACC_READ_BIT)#define EPT_VIOLATION_ACC_WRITE (1 << EPT_VIOLATION_ACC_WRITE_BIT)
@@ -545,6 +546,7 @@ struct vmx_msr_entry {#define EPT_VIOLATION_READABLE (1 << EPT_VIOLATION_READABLE_BIT)#define EPT_VIOLATION_WRITABLE (1 << EPT_VIOLATION_WRITABLE_BIT)#define EPT_VIOLATION_EXECUTABLE (1 << EPT_VIOLATION_EXECUTABLE_BIT) +#define EPT_VIOLATION_GLA_VALID (1 << EPT_VIOLATION_GLA_VALID_BIT)#define EPT_VIOLATION_GVA_TRANSLATED (1 << EPT_VIOLATION_GVA_TRANSLATED_BIT)/*
arch/x86/kvm/kvmi.c +21 -0
@@ -759,6 +759,27 @@ int kvmi_arch_cmd_control_cr(struct kvm_vcpu *vcpu,return 0 ; } +bool is_ud2_instruction (struct kvm_vcpu *vcpu,int *emulation_type) +{ +u8 ud2[] = {0x0F ,0x0B }; +u8 insn_len = vcpu->arch.emulate_ctxt.fetch.ptr - + vcpu->arch.emulate_ctxt.fetch.data; + +if (insn_len !=sizeof (ud2)) +return false; + +if (memcmp (vcpu->arch.emulate_ctxt.fetch.data, ud2, insn_len)) +return false; + +/* Do not reexecute the UD2 instruction, else we might enter to an +* endless emulation loop. Let the emulator fall down through the +* handle_emulation_failure() which shall inject the #UD exception. +*/ + *emulation_type &= ~EMULTYPE_ALLOW_RETRY ; + +return true; +} +void kvmi_arch_start_single_step (struct kvm_vcpu *vcpu) {kvm_set_mtf (vcpu, true);
arch/x86/kvm/mmu.c +5 -0
@@ -6667,6 +6667,11 @@ void kvm_mmu_module_exit(void)mmu_audit_disable (); } +u64 kvm_mmu_fault_gla (struct kvm_vcpu *vcpu) +{ +return kvm_x86_ops->fault_gla (vcpu); +} +bool kvm_mmu_nested_pagefault (struct kvm_vcpu *vcpu) {return kvm_x86_ops->nested_pagefault (vcpu);
arch/x86/kvm/svm.c +8 -0
@@ -2675,6 +2675,8 @@ static int pf_interception(struct vcpu_svm *svm)u64 fault_address =__sme_clr (svm->vmcb->control.exit_info_2);u64 error_code = svm->vmcb->control.exit_info_1; + svm->vcpu.arch.error_code = error_code; + return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address, static_cpu_has(X86_FEATURE_DECODEASSISTS) ? svm->vmcb->control.insn_bytes : NULL,
@@ -7171,6 +7173,11 @@ static void svm_msr_intercept(struct kvm_vcpu *vcpu, unsigned int msr,set_msr_interception (svm, msrpm, msr, enable, enable); } +static u64 svm_fault_gla (struct kvm_vcpu *vcpu) +{ +return ~0ull ; +} +static bool svm_nested_pagefault (struct kvm_vcpu *vcpu) {return false;
@@ -7233,6 +7240,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .cr3_write_exiting = svm_cr3_write_exiting, .msr_intercept = svm_msr_intercept, .desc_intercept = svm_desc_intercept, + .fault_gla = svm_fault_gla, .nested_pagefault = svm_nested_pagefault, .spt_fault = svm_spt_fault,
arch/x86/kvm/vmx/vmx.c +11 -2
@@ -5171,10 +5171,11 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu) EPT_VIOLATION_EXECUTABLE)) ?PFERR_PRESENT_MASK :0 ; - error_code |= (exit_qualification &0x100 ) !=0 ? -PFERR_GUEST_FINAL_MASK :PFERR_GUEST_PAGE_MASK ; + error_code |= (exit_qualification &EPT_VIOLATION_GVA_TRANSLATED ) + ?PFERR_GUEST_FINAL_MASK :PFERR_GUEST_PAGE_MASK ; vcpu->arch.exit_qualification = exit_qualification; + vcpu->arch.error_code = error_code;return kvm_mmu_page_fault (vcpu, gpa, error_code,NULL ,0 ); }
@@ -7880,6 +7881,13 @@ static void vmx_cr3_write_exiting(struct kvm_vcpu *vcpu,/* TODO: nested ? vmcs12->cpu_based_vm_exec_control */ } +static u64 vmx_fault_gla (struct kvm_vcpu *vcpu) +{ +if (vcpu->arch.exit_qualification &EPT_VIOLATION_GLA_VALID ) +return vmcs_readl (GUEST_LINEAR_ADDRESS ); +return ~0ull ; +} +static bool vmx_nested_pagefault (struct kvm_vcpu *vcpu) {if (vcpu->arch.exit_qualification &EPT_VIOLATION_GVA_TRANSLATED )
@@ -7947,6 +7955,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .msr_intercept = vmx_msr_intercept, .cr3_write_exiting = vmx_cr3_write_exiting, .desc_intercept = vmx_desc_intercept, + .fault_gla = vmx_fault_gla, .nested_pagefault = vmx_nested_pagefault, .spt_fault = vmx_spt_fault,
arch/x86/kvm/x86.c +56 -1
77 lines not on this page · raw message
include/linux/kvmi.h +4 -0
@@ -21,6 +21,7 @@ bool kvmi_hypercall_event(struct kvm_vcpu *vcpu);bool kvmi_queue_exception (struct kvm_vcpu *vcpu);void kvmi_trap_event (struct kvm_vcpu *vcpu);bool kvmi_descriptor_event (struct kvm_vcpu *vcpu,u8 descriptor,u8 write); +bool kvmi_single_step (struct kvm_vcpu *vcpu,gpa_t gpa,int *emulation_type);void kvmi_handle_requests (struct kvm_vcpu *vcpu);void kvmi_stop_ss (struct kvm_vcpu *vcpu);bool kvmi_vcpu_enabled_ss (struct kvm_vcpu *vcpu);
@@ -41,6 +42,9 @@ static inline bool kvmi_breakpoint_event(struct kvm_vcpu *vcpu, u64 gva,static inline bool kvmi_descriptor_event (struct kvm_vcpu *vcpu,u8 descriptor,u8 write) {return true; } +static inline bool kvmi_single_step (struct kvm_vcpu *vcpu,gpa_t gpa, +int *emulation_type) + {return false; }static inline void kvmi_vcpu_uninit (struct kvm_vcpu *vcpu) { }static inline void kvmi_handle_requests (struct kvm_vcpu *vcpu) { }static inline bool kvmi_hypercall_event (struct kvm_vcpu *vcpu) {return false; }
virt/kvm/kvmi.c +56 -0
62 lines not on this page · raw message
virt/kvm/kvmi_int.h +1 -0
@@ -290,6 +290,7 @@ int kvmi_arch_cmd_inject_exception(struct kvm_vcpu *vcpu, u8 vector, u64 address);int kvmi_arch_cmd_control_cr (struct kvm_vcpu *vcpu,const struct kvmi_control_cr *req); +bool is_ud2_instruction (struct kvm_vcpu *vcpu,int *emulation_type);void kvmi_arch_start_single_step (struct kvm_vcpu *vcpu);void kvmi_arch_stop_single_step (struct kvm_vcpu *vcpu);u8 kvmi_arch_relax_page_access (u8 old,u8 new);
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 68/92 kvm: x86: emulate a guest page table walk on SPT violations due to A/D bit updates
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
On SPT page faults caused by guest page table walks, use the existing guest page table walk code to make the necessary adjustments to the A/D bits and return to guest. This effectively bypasses the x86 emulator who was making the wrong modifications leading one OS (Windows 8.1 x64) to triple-fault very early in the boot process with the introspection enabled.
With introspection disabled, these faults are handled by simply removing the protection from the affected guest page and returning to guest.
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/include/asm/kvmi_host.h | 6 ++++++ arch/x86/kvm/kvmi.c | 34 +++++++++++++++++++++++++++++++- arch/x86/kvm/mmu.c | 11 +++++++++-- arch/x86/kvm/x86.c | 6 +++--- include/linux/kvmi.h | 3 +++ virt/kvm/kvmi.c | 31 +++++++++++++++++++++++++++-- 7 files changed, 84 insertions(+), 9 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -1425,7 +1425,7 @@ gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,gpa_t kvm_mmu_gva_to_gpa_write (struct kvm_vcpu *vcpu,gva_t gva,struct x86_exception *exception);gpa_t kvm_mmu_gva_to_gpa_system (struct kvm_vcpu *vcpu,gva_t gva, -struct x86_exception *exception); +u32 access,struct x86_exception *exception);void kvm_vcpu_deactivate_apicv (struct kvm_vcpu *vcpu);
arch/x86/include/asm/kvmi_host.h +6 -0
@@ -16,6 +16,7 @@ bool kvmi_monitored_msr(struct kvm_vcpu *vcpu, u32 msr);bool kvmi_cr_event (struct kvm_vcpu *vcpu,unsigned int cr,unsigned long old_value,unsigned long *new_value);void kvmi_xsetbv_event (struct kvm_vcpu *vcpu); +bool kvmi_update_ad_flags (struct kvm_vcpu *vcpu);#else /* CONFIG_KVM_INTROSPECTION */
@@ -40,6 +41,11 @@ static inline void kvmi_xsetbv_event(struct kvm_vcpu *vcpu) { } +static inline bool kvmi_update_ad_flags (struct kvm_vcpu *vcpu) +{ +return false; +} +#endif /* CONFIG_KVM_INTROSPECTION */ #endif /* _ASM_X86_KVMI_HOST_H */
arch/x86/kvm/kvmi.c +33 -1
@@ -465,7 +465,7 @@ void kvmi_arch_breakpoint_event(struct kvm_vcpu *vcpu, u64 gva, u8 insn_len)u32 action;u64 gpa; - gpa =kvm_mmu_gva_to_gpa_system (vcpu, gva,NULL ); + gpa =kvm_mmu_gva_to_gpa_system (vcpu, gva,0 ,NULL ); action =kvmi_msg_send_bp (vcpu, gpa, insn_len);switch (action) {
@@ -822,6 +822,38 @@ u8 kvmi_arch_relax_page_access(u8 old, u8 new)return ret; } +bool kvmi_update_ad_flags (struct kvm_vcpu *vcpu) +{ +struct x86_exception exception = { }; +struct kvmi *ikvm; +bool ret = false; +gva_t gva; +gpa_t gpa; + + ikvm =kvmi_get (vcpu->kvm); +if (!ikvm) +return false; + + gva =kvm_mmu_fault_gla (vcpu); + +if (gva == ~0ull ) { +kvmi_warn_once (ikvm,"%s: cannot perform translation\n" , + __func__); + goto out; + } + + gpa =kvm_mmu_gva_to_gpa_system (vcpu, gva,PFERR_WRITE_MASK ,NULL ); +if (gpa ==UNMAPPED_GVA ) + gpa =kvm_mmu_gva_to_gpa_system (vcpu, gva,0 , &exception); + + ret = (gpa !=UNMAPPED_GVA ); + +out: +kvmi_put (vcpu->kvm); + +return ret; +} + static const struct { unsigned int allow_bit; enum kvm_page_track_mode track_mode;
arch/x86/kvm/mmu.c +9 -2
@@ -40,7 +40,9 @@#include <linux/uaccess.h> #include <linux/hash.h> #include <linux/kern_levels.h> +#include <linux/kvmi.h> +#include <asm/kvmi_host.h> #include <asm/page.h> #include <asm/pat.h> #include <asm/cmpxchg.h>
@@ -5960,8 +5962,13 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u64 error_code,*/ if (vcpu->arch.mmu->direct_map && (error_code &PFERR_NESTED_GUEST_PAGE ) ==PFERR_NESTED_GUEST_PAGE ) { -kvm_mmu_unprotect_page (vcpu->kvm,gpa_to_gfn (cr2)); -return 1 ; +if (kvmi_tracked_gfn (vcpu,gpa_to_gfn (cr2))) { +if (kvmi_update_ad_flags (vcpu)) +return 1 ; + }else { +kvm_mmu_unprotect_page (vcpu->kvm,gpa_to_gfn (cr2)); +return 1 ; + } }/*
arch/x86/kvm/x86.c +3 -3
@@ -5175,9 +5175,9 @@ gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,/* uses this to access any guest's mapped memory without checking CPL */ gpa_t kvm_mmu_gva_to_gpa_system (struct kvm_vcpu *vcpu,gva_t gva, -struct x86_exception *exception) +u32 access,struct x86_exception *exception) { -return vcpu->arch.walk_mmu->gva_to_gpa (vcpu, gva,0 , exception); +return vcpu->arch.walk_mmu->gva_to_gpa (vcpu, gva, access, exception); } static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
@@ -8904,7 +8904,7 @@ int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,vcpu_load (vcpu); idx =srcu_read_lock (&vcpu->kvm->srcu); - gpa =kvm_mmu_gva_to_gpa_system (vcpu, vaddr,NULL ); + gpa =kvm_mmu_gva_to_gpa_system (vcpu, vaddr,0 ,NULL );srcu_read_unlock (&vcpu->kvm->srcu, idx); tr->physical_address = gpa; tr->valid = gpa !=UNMAPPED_GVA ;
include/linux/kvmi.h +3 -0
@@ -21,6 +21,7 @@ bool kvmi_hypercall_event(struct kvm_vcpu *vcpu);bool kvmi_queue_exception (struct kvm_vcpu *vcpu);void kvmi_trap_event (struct kvm_vcpu *vcpu);bool kvmi_descriptor_event (struct kvm_vcpu *vcpu,u8 descriptor,u8 write); +bool kvmi_tracked_gfn (struct kvm_vcpu *vcpu,gfn_t gfn);bool kvmi_single_step (struct kvm_vcpu *vcpu,gpa_t gpa,int *emulation_type);void kvmi_handle_requests (struct kvm_vcpu *vcpu);void kvmi_stop_ss (struct kvm_vcpu *vcpu);
@@ -36,6 +37,8 @@ static inline void kvmi_uninit(void) { }static inline void kvmi_create_vm (struct kvm *kvm) { }static inline void kvmi_destroy_vm (struct kvm *kvm) { }static inline int kvmi_vcpu_init (struct kvm_vcpu *vcpu) {return 0 ; } +static inline bool kvmi_tracked_gfn (struct kvm_vcpu *vcpu,gfn_t gfn) + {return false; }static inline bool kvmi_breakpoint_event (struct kvm_vcpu *vcpu,u64 gva,u8 insn_len) {return true; }
virt/kvm/kvmi.c +29 -2
@@ -193,6 +193,33 @@ static bool kvmi_restricted_access(struct kvmi *ikvm, gpa_t gpa, u8 access)return false; } +bool is_tracked_gfn (struct kvmi *ikvm,gfn_t gfn) +{ +struct kvmi_mem_access *m; + +read_lock (&ikvm->access_tree_lock); + m =__kvmi_get_gfn_access (ikvm, gfn); +read_unlock (&ikvm->access_tree_lock); + +return !!m; +} + +bool kvmi_tracked_gfn (struct kvm_vcpu *vcpu,gfn_t gfn) +{ +struct kvmi *ikvm; +bool ret; + + ikvm =kvmi_get (vcpu->kvm); +if (!ikvm) +return false; + + ret =is_tracked_gfn (ikvm, gfn); + +kvmi_put (vcpu->kvm); + +return ret; +} +static void kvmi_clear_mem_access (struct kvm *kvm) {void **slot;
@@ -1681,7 +1708,7 @@ static int write_custom_data_to_page(struct kvm_vcpu *vcpu, gva_t gva,struct page *page;gpa_t gpa; - gpa =kvm_mmu_gva_to_gpa_system (vcpu, gva,NULL ); + gpa =kvm_mmu_gva_to_gpa_system (vcpu, gva,0 ,NULL );if (gpa ==UNMAPPED_GVA )return -KVM_EINVAL ;
@@ -1738,7 +1765,7 @@ static int restore_backup_data_to_page(struct kvm_vcpu *vcpu, gva_t gva,struct page *page;gpa_t gpa; - gpa =kvm_mmu_gva_to_gpa_system (vcpu, gva,NULL ); + gpa =kvm_mmu_gva_to_gpa_system (vcpu, gva,0 ,NULL );if (gpa ==UNMAPPED_GVA )return -KVM_EINVAL ;
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 69/92 kvm: x86: keep the page protected if tracked by the introspection tool
In reply to Adalbert LazărThis patch might be obsolete thanks to single-stepping.
--- arch/x86/kvm/x86.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +7 -2
@@ -6311,7 +6311,8 @@ static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t cr2, indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;spin_unlock (&vcpu->kvm->mmu_lock); -if (indirect_shadow_pages) +if (indirect_shadow_pages + && !kvmi_tracked_gfn (vcpu,gpa_to_gfn (gpa)))kvm_mmu_unprotect_page (vcpu->kvm,gpa_to_gfn (gpa));return true;
@@ -6322,7 +6323,8 @@ static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t cr2,* and it failed try to unshadow page and re-enter the * guest to let CPU execute the instruction. */ -kvm_mmu_unprotect_page (vcpu->kvm,gpa_to_gfn (gpa)); +if (!kvmi_tracked_gfn (vcpu,gpa_to_gfn (gpa))) +kvm_mmu_unprotect_page (vcpu->kvm,gpa_to_gfn (gpa));/* * If the access faults on its page table, it can not
@@ -6374,6 +6376,9 @@ static bool retry_instruction(struct x86_emulate_ctxt *ctxt,if (!vcpu->arch.mmu->direct_map) gpa =kvm_mmu_gva_to_gpa_write (vcpu, cr2,NULL ); +if (kvmi_tracked_gfn (vcpu,gpa_to_gfn (gpa))) +return false; +kvm_mmu_unprotect_page (vcpu->kvm,gpa_to_gfn (gpa));return true;
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [RFC PATCH v6 69/92] kvm: x86: keep the page protected if tracked by the introspection tool
Quoted 1 line · Adalbert Lazăr
This patch might be obsolete thanks to single-stepping.
sooo should it be skipped from this large patchset to easy review?
Quoted 40 lines
Signed-off-by Adalbert Lazăr <[email protected]>--- arch/x86/kvm/x86.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)arch/x86/kvm/x86.c +7
-2@@ -6311,7 +6311,8 @@ static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t cr2, indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;spin_unlock (&vcpu->kvm->mmu_lock); -if (indirect_shadow_pages) +if (indirect_shadow_pages + && !kvmi_tracked_gfn (vcpu,gpa_to_gfn (gpa)))kvm_mmu_unprotect_page (vcpu->kvm,gpa_to_gfn (gpa));return true;@@ -6322,7 +6323,8 @@ static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t cr2,* and it failed try to unshadow page and re-enter the * guest to let CPU execute the instruction. */ -kvm_mmu_unprotect_page (vcpu->kvm,gpa_to_gfn (gpa)); +if (!kvmi_tracked_gfn (vcpu,gpa_to_gfn (gpa))) +kvm_mmu_unprotect_page (vcpu->kvm,gpa_to_gfn (gpa));/* * If the access faults on its page table, it can not @@ -6374,6 +6376,9 @@ static bool retry_instruction(struct x86_emulate_ctxt *ctxt,if (!vcpu->arch.mmu->direct_map) gpa =kvm_mmu_gva_to_gpa_write (vcpu, cr2,NULL ); +if (kvmi_tracked_gfn (vcpu,gpa_to_gfn (gpa))) +return false; +kvm_mmu_unprotect_page (vcpu->kvm,gpa_to_gfn (gpa));return true;
Re: [RFC PATCH v6 69/92] kvm: x86: keep the page protected if tracked by the introspection tool
Quoted 5 lines · Konrad Rzeszutek Wilk
Quoted 1 line · Adalbert Lazăr
This patch might be obsolete thanks to single-stepping.
sooo should it be skipped from this large patchset to easy review?
I'll add a couple of warning messages to check if this patch is still needed, in order to skip it from the next submission (which will be smaller:)
However, on AMD, single-stepping is not an option.
Thanks,
Adalbert
Quoted 41 lines
Quoted 41 lines
Signed-off-by Adalbert Lazăr <[email protected]>--- arch/x86/kvm/x86.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)arch/x86/kvm/x86.c +7
-2@@ -6311,7 +6311,8 @@ static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t cr2, indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;spin_unlock (&vcpu->kvm->mmu_lock); -if (indirect_shadow_pages) +if (indirect_shadow_pages + && !kvmi_tracked_gfn (vcpu,gpa_to_gfn (gpa)))kvm_mmu_unprotect_page (vcpu->kvm,gpa_to_gfn (gpa));return true;@@ -6322,7 +6323,8 @@ static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t cr2,* and it failed try to unshadow page and re-enter the * guest to let CPU execute the instruction. */ -kvm_mmu_unprotect_page (vcpu->kvm,gpa_to_gfn (gpa)); +if (!kvmi_tracked_gfn (vcpu,gpa_to_gfn (gpa))) +kvm_mmu_unprotect_page (vcpu->kvm,gpa_to_gfn (gpa));/* * If the access faults on its page table, it can not @@ -6374,6 +6376,9 @@ static bool retry_instruction(struct x86_emulate_ctxt *ctxt,if (!vcpu->arch.mmu->direct_map) gpa =kvm_mmu_gva_to_gpa_write (vcpu, cr2,NULL ); +if (kvmi_tracked_gfn (vcpu,gpa_to_gfn (gpa))) +return false; +kvm_mmu_unprotect_page (vcpu->kvm,gpa_to_gfn (gpa));return true;
RFC PATCH v6 70/92 kvm: x86: filter out access rights only when tracked by the introspection tool
In reply to Adalbert LazărIt should complete the commit fd34a9518173 ("kvm: x86: consult the page tracking from kvm_mmu_get_page() and __direct_map()")
--- arch/x86/kvm/mmu.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kvm/mmu.c +3 -0
@@ -2660,6 +2660,9 @@ static void clear_sp_write_flooding_count(u64 *spte)static unsigned int kvm_mmu_page_track_acc (struct kvm_vcpu *vcpu,gfn_t gfn,unsigned int acc) { +if (!kvmi_tracked_gfn (vcpu, gfn)) +return acc; +if (kvm_page_track_is_active (vcpu, gfn,KVM_PAGE_TRACK_PREREAD )) acc &= ~ACC_USER_MASK ; if (kvm_page_track_is_active(vcpu, gfn, KVM_PAGE_TRACK_PREWRITE) ||
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Paolo Bonzini
maintainerRe: [RFC PATCH v6 70/92] kvm: x86: filter out access rights only when tracked by the introspection tool
Quoted 13 lines · Adalbert Lazăr
It should complete the commit
fd34a9518173("kvm: x86: consult the page tracking fromkvm_mmu_get_page()and__direct_map()")Signed-off-by Adalbert Lazăr <[email protected]>--- arch/x86/kvm/mmu.c | 3 +++ 1 file changed, 3 insertions(+)arch/x86/kvm/mmu.c +0
-0
arch/x86/kvm/mmu.c static void clear_sp_write_flooding_count(u64 *spte) · line 2668
Quoted 9 lines
arch/x86/kvm/mmu.c +3
-0@@ -2660,6 +2660,9 @@ static void clear_sp_write_flooding_count(u64 *spte)static unsigned int kvm_mmu_page_track_acc (struct kvm_vcpu *vcpu,gfn_t gfn,unsigned int acc) { +if (!kvmi_tracked_gfn (vcpu, gfn)) +return acc; +if (kvm_page_track_is_active (vcpu, gfn,KVM_PAGE_TRACK_PREREAD )) acc &= ~ACC_USER_MASK ; if (kvm_page_track_is_active(vcpu, gfn, KVM_PAGE_TRACK_PREWRITE) ||
If this patch is always needed, then the function should be named something like kvm_mmu_apply_introspection_access and kvmi_tracked_gfn should be tested from the moment it is introduced.
But the commit message says nothing about _why_ it is needed, so I cannot guess. I would very much avoid it however. Is it just an optimization?
Paolo
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [RFC PATCH v6 70/92] kvm: x86: filter out access rights only when tracked by the introspection tool
Quoted 28 lines · Paolo Bonzini
Quoted 13 lines · Adalbert Lazăr
It should complete the commit
fd34a9518173("kvm: x86: consult the page tracking fromkvm_mmu_get_page()and__direct_map()")Signed-off-by Adalbert Lazăr <[email protected]>--- arch/x86/kvm/mmu.c | 3 +++ 1 file changed, 3 insertions(+)arch/x86/kvm/mmu.c +0
-0arch/x86/kvm/mmu.c static void clear_sp_write_flooding_count(u64 *spte) · line 2668
Quoted 9 lines
arch/x86/kvm/mmu.c +3
-0@@ -2660,6 +2660,9 @@ static void clear_sp_write_flooding_count(u64 *spte)static unsigned int kvm_mmu_page_track_acc (struct kvm_vcpu *vcpu,gfn_t gfn,unsigned int acc) { +if (!kvmi_tracked_gfn (vcpu, gfn)) +return acc; +if (kvm_page_track_is_active (vcpu, gfn,KVM_PAGE_TRACK_PREREAD )) acc &= ~ACC_USER_MASK ; if (kvm_page_track_is_active(vcpu, gfn, KVM_PAGE_TRACK_PREWRITE) ||If this patch is always needed, then the function should be named something like
kvm_mmu_apply_introspection_accessandkvmi_tracked_gfnshould be tested from the moment it is introduced.
Quoted 5 lines
But the commit message says nothing about _why_ it is needed, so I cannot guess. I would very much avoid it however. Is it just an optimization?
Paolo
We'll retest to see if we still need kvm_mmu_page_track_acc(). The kvmi_tracked_gfn() check was used to keep the KVM code flow "unchanged" as much as possible. Probably, we can get ride of it.
Re: [RFC PATCH v6 70/92] kvm: x86: filter out access rights only when tracked by the introspection tool
In reply to Paolo BonziniQuoted 28 lines · Paolo Bonzini
Quoted 13 lines · Adalbert Lazăr
It should complete the commit
fd34a9518173("kvm: x86: consult the page tracking fromkvm_mmu_get_page()and__direct_map()")Signed-off-by Adalbert Lazăr <[email protected]>--- arch/x86/kvm/mmu.c | 3 +++ 1 file changed, 3 insertions(+)arch/x86/kvm/mmu.c +0
-0arch/x86/kvm/mmu.c static void clear_sp_write_flooding_count(u64 *spte) · line 2668
Quoted 9 lines
arch/x86/kvm/mmu.c +3
-0@@ -2660,6 +2660,9 @@ static void clear_sp_write_flooding_count(u64 *spte)static unsigned int kvm_mmu_page_track_acc (struct kvm_vcpu *vcpu,gfn_t gfn,unsigned int acc) { +if (!kvmi_tracked_gfn (vcpu, gfn)) +return acc; +if (kvm_page_track_is_active (vcpu, gfn,KVM_PAGE_TRACK_PREREAD )) acc &= ~ACC_USER_MASK ; if (kvm_page_track_is_active(vcpu, gfn, KVM_PAGE_TRACK_PREWRITE) ||If this patch is always needed, then the function should be named something like
kvm_mmu_apply_introspection_accessandkvmi_tracked_gfnshould be tested from the moment it is introduced.
Quoted 5 lines
But the commit message says nothing about _why_ it is needed, so I cannot guess. I would very much avoid it however. Is it just an optimization?
Paolo
We'll retest to see if we still need kvm_mmu_page_track_acc(). The kvmi_tracked_gfn() check was used to keep the KVM code flow "unchanged" as much as possible. Probably, we can get ride of it.
_______________________________________________
Virtualization mailing list [email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 71/92 mm: add support for remote mapping
In reply to Adalbert LazărFrom: Mircea Cîrjaliu <[email protected]>
The following two new mm exports are introduced:
* mm_remote_map(struct mm_struct *req_mm, unsigned long req_hva, unsigned long map_hva)
* mm_remote_unmap(unsigned long map_hva)
* mm_remote_reset(void)
* rmap_walk_remote(struct page *page, struct rmap_walk_control *rwc)
This patch allows one process to map into its address space a page from another process. The previous page (if it exists) is dropped. There is no corresponding pair of system calls as this API is meant to be used by the kernel itself only.
The targeted user is the upcoming KVM VM introspection subsystem (KVMI), where an introspector running in its own VM will map pages from the introspected guest in order to eliminate round trips to the host kernel (read/write guest pages).
The flow is as follows: the introspector identifies a guest physical address where some information of interest is located. It creates a one page anonymous mapping with MAP_LOCKED | MAP_POPULATE and calls the kernel via an IOCTL on /dev/kvmmem giving the map virtual address and the guest physical address as arguments. The kernel converts the map va into a physical page (gpa in KVM-speak) and passes it to the host kernel via a hypercall, along with the introspected guest gpa. The host kernel converts the two gpa-s into their appropriate hva-s (host virtual addresses) and makes sure the vma backing up the page belonging to the VM in which the introspector runs, points to the indicated page into the introspected guest. I have not included here the use of the mapping token described in the KVMI documentation.
--- include/linux/page-flags.h | 9 +- include/linux/remote_mapping.h | 167 +++ include/uapi/linux/remote_mapping.h | 18 + mm/Kconfig | 8 + mm/Makefile | 1 + mm/memory-failure.c | 69 +- mm/migrate.c | 9 +- mm/remote_mapping.c | 1834 +++++++++++++++++++++++++++ mm/rmap.c | 13 +- mm/vmscan.c | 3 +- 10 files changed, 2108 insertions(+), 23 deletions(-) create mode 100644 include/linux/remote_mapping.h create mode 100644 include/uapi/linux/remote_mapping.h create mode 100644 mm/remote_mapping.c
include/linux/page-flags.h +8 -1
@@ -417,8 +417,10 @@ PAGEFLAG(Idle, idle, PF_ANY)*/ #define PAGE_MAPPING_ANON 0x1#define PAGE_MAPPING_MOVABLE 0x2 +#define PAGE_MAPPING_REMOTE 0x4#define PAGE_MAPPING_KSM (PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE) -#define PAGE_MAPPING_FLAGS (PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE) +#define PAGE_MAPPING_FLAGS (PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE | \ + PAGE_MAPPING_REMOTE) static __always_inline int PageMappingFlags(struct page *page) {
@@ -431,6 +433,11 @@ static __always_inline int PageAnon(struct page *page)return ((unsigned long )page->mapping &PAGE_MAPPING_ANON ) !=0 ; } +static __always_inline int PageRemote(struct page *page) +{ +return ((unsigned long )page->mapping &PAGE_MAPPING_REMOTE ) !=0 ; +} + static __always_inline int __PageMovable(struct page *page) { return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) ==
include/linux/remote_mapping.h new file +167 -0
167 lines not on this page · raw message
include/uapi/linux/remote_mapping.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ + +#ifndef __UAPI_REMOTE_MAPPING_H__ +#define __UAPI_REMOTE_MAPPING_H__ + +#include <linux/types.h> +#include <linux/ioctl.h> + +struct remote_map_request { +__u32 req_pid; +__u64 req_hva; +__u64 map_hva; +}; + +#define REMOTE_MAP _IOW('r', 0x01, struct remote_map_request) +#define REMOTE_UNMAP _IOW('r', 0x02, unsigned long) + +#endif /* __UAPI_REMOTE_MAPPING_H__ */
mm/Kconfig +8 -0
@@ -758,4 +758,12 @@ config GUP_BENCHMARK config ARCH_HAS_PTE_SPECIAL bool +config REMOTE_MAPPING + bool "Remote memory mapping" + depends on MMU && !KSM + default n + help + Allows a given application to map pages of another application in its own + address space. + endmenu
mm/Makefile +1 -0
@@ -99,3 +99,4 @@ obj-$(CONFIG_HARDENED_USERCOPY) += usercopy.o obj-$(CONFIG_PERCPU_STATS) += percpu-stats.o obj-$(CONFIG_HMM) += hmm.o obj-$(CONFIG_MEMFD_CREATE) += memfd.o +obj-$(CONFIG_REMOTE_MAPPING) += remote_mapping.o
mm/memory-failure.c +56 -13
105 lines not on this page · raw message
mm/migrate.c +5 -4
@@ -47,6 +47,7 @@#include <linux/page_owner.h> #include <linux/sched/mm.h> #include <linux/ptrace.h> +#include <linux/remote_mapping.h> #include <asm/tlbflush.h>
@@ -215,7 +216,7 @@ static bool remove_migration_pte(struct page *page, struct vm_area_struct *vma,VM_BUG_ON_PAGE (PageTail (page), page);while (page_vma_mapped_walk (&pvmw)) { -if (PageKsm (page)) +if (PageKsm (page) ||PageRemote (page)) new = page;else new = page - pvmw.page->index +
@@ -1065,7 +1066,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,* because that implies that the anon page is no longer mapped * (and cannot be remapped so long as we hold the page lock). */ -if (PageAnon (page) && !PageKsm (page)) +if (PageAnon (page) && !PageKsm (page) && !PageRemote (page)) anon_vma =page_get_anon_vma (page);/*
@@ -1104,8 +1105,8 @@ static int __unmap_and_move(struct page *page, struct page *newpage, } }else if (page_mapped (page)) {/* Establish migration ptes */ -VM_BUG_ON_PAGE (PageAnon (page) && !PageKsm (page) && !anon_vma, - page); +VM_BUG_ON_PAGE (PageAnon (page) && !PageKsm (page) && + !PageRemote (page) && !anon_vma, page);try_to_unmap (page,TTU_MIGRATION |TTU_IGNORE_MLOCK |TTU_IGNORE_ACCESS ); page_was_mapped =1 ;
mm/remote_mapping.c new file +1834 -0
1,834 lines not on this page · raw message
mm/rmap.c +9 -4
@@ -65,6 +65,7 @@#include <linux/page_idle.h> #include <linux/memremap.h> #include <linux/userfaultfd_k.h> +#include <linux/remote_mapping.h> #include <asm/tlbflush.h>
@@ -856,7 +857,7 @@ int page_referenced(struct page *page,if (!page_rmapping (page))return 0 ; -if (!is_locked && (!PageAnon (page) ||PageKsm (page))) { +if (!is_locked && (!PageAnon (page) ||PageKsm (page) ||PageRemote (page))) { we_locked =trylock_page (page);if (!we_locked)return 1 ;
@@ -1021,7 +1022,7 @@ void page_move_anon_rmap(struct page *page, struct vm_area_struct *vma)* __page_set_anon_rmap - set up new anonymous rmap * @page: Page or Hugepage to add to rmap * @vma: VM area to add page to. -* @address: User virtual address of the mapping +* @address: User virtual address of the mapping * @exclusive: the page is exclusively owned by the current process */ static void __page_set_anon_rmap(struct page *page,
@@ -1125,7 +1126,8 @@ void do_page_add_anon_rmap(struct page *page,__inc_node_page_state (page,NR_ANON_THPS );__mod_node_page_state (page_pgdat (page),NR_ANON_MAPPED , nr); } -if (unlikely (PageKsm (page))) + +if (unlikely (PageKsm (page) ||PageRemote (page)))return ;VM_BUG_ON_PAGE (!PageLocked (page), page);
@@ -1897,6 +1899,8 @@ void rmap_walk(struct page *page, struct rmap_walk_control *rwc) {if (unlikely (PageKsm (page)))rmap_walk_ksm (page, rwc); +else if (unlikely (PageRemote (page))) +rmap_walk_remote (page, rwc);else if (PageAnon (page))rmap_walk_anon (page, rwc, false);else
@@ -1906,8 +1910,9 @@ void rmap_walk(struct page *page, struct rmap_walk_control *rwc)/* Like rmap_walk, but caller holds relevant rmap lock */ void rmap_walk_locked (struct page *page,struct rmap_walk_control *rwc) { -/* no ksm support for now */ +/* no ksm/remote support for now */ VM_BUG_ON_PAGE (PageKsm (page), page); +VM_BUG_ON_PAGE (PageRemote (page), page);if (PageAnon (page))rmap_walk_anon (page, rwc, true);else
mm/vmscan.c +2 -1
@@ -4277,7 +4277,8 @@ int page_evictable(struct page *page)/* Prevent address_space of inode and swap cache from being freed */ rcu_read_lock (); - ret = !mapping_unevictable (page_mapping (page)) && !PageMlocked (page); + ret = !mapping_unevictable (page_mapping (page)) && + !PageMlocked (page) && !PageRemote (page);rcu_read_unlock ();return ret; }
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
DANGER WILL ROBINSON, DANGER
include/linux/page-flags.h PAGEFLAG(Idle, idle, PF_ANY) · line 420
Quoted 6 lines · Adalbert Lazăr
+++
b/include/linux/page-flags.hinclude/linux/page-flags.h +1
-0@@ -417,8 +417,10 @@ PAGEFLAG(Idle, idle, PF_ANY)*/ #define PAGE_MAPPING_ANON 0x1#define PAGE_MAPPING_MOVABLE 0x2 +#define PAGE_MAPPING_REMOTE 0x4
Uh. How do you know page->mapping would otherwise have bit 2 clear? Who's guaranteeing that?
This is an awfully big patch to the memory management code, buried in the middle of a gigantic series which almost guarantees nobody would look at it. I call shenanigans.
mm/rmap.c void page_move_anon_rmap(struct page *page, struct vm_area_struct *vma) · line 1025
Quoted 6 lines
mm/rmap.c +1
-1@@ -1021,7 +1022,7 @@ void page_move_anon_rmap(struct page *page, struct vm_area_struct *vma)* __page_set_anon_rmap - set up new anonymous rmap * @page: Page or Hugepage to add to rmap * @vma: VM area to add page to. -* @address: User virtual address of the mapping +* @address: User virtual address of the mapping
And mixing in fluff changes like this is a real no-no. Try again.
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Paolo Bonzini
maintainerRe: DANGER WILL ROBINSON, DANGER
Quoted 10 lines · Matthew Wilcox
include/linux/page-flags.h PAGEFLAG(Idle, idle, PF_ANY) · line 420
Quoted 6 lines · Adalbert Lazăr
+++
b/include/linux/page-flags.hinclude/linux/page-flags.h +1
-0@@ -417,8 +417,10 @@ PAGEFLAG(Idle, idle, PF_ANY)*/ #define PAGE_MAPPING_ANON 0x1#define PAGE_MAPPING_MOVABLE 0x2 +#define PAGE_MAPPING_REMOTE 0x4Uh. How do you know
page->mappingwould otherwise have bit 2 clear? Who's guaranteeing that?
Quoted 3 lines
This is an awfully big patch to the memory management code, buried in the middle of a gigantic series which almost guarantees nobody would look at it. I call shenanigans.
Are you calling shenanigans on the patch submitter (which is gratuitous) or on the KVM maintainers/reviewers?
It's not true that nobody would look at it. Of course no one from linux-mm is going to look at it, but the maintainer that looks at the gigantic series is very much expected to look at it and explain to the submitter that this patch is unacceptable as is.
In fact I shouldn't have to to explain this to you; you know better than believing that I would try to sneak it past the mm folks. I am puzzled.
Paolo
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: DANGER WILL ROBINSON, DANGER
Quoted 11 lines · Paolo Bonzini
Quoted 10 lines · Matthew Wilcox
include/linux/page-flags.h PAGEFLAG(Idle, idle, PF_ANY) · line 420
Quoted 6 lines · Adalbert Lazăr
+++
b/include/linux/page-flags.hinclude/linux/page-flags.h +1
-0@@ -417,8 +417,10 @@ PAGEFLAG(Idle, idle, PF_ANY)*/ #define PAGE_MAPPING_ANON 0x1#define PAGE_MAPPING_MOVABLE 0x2 +#define PAGE_MAPPING_REMOTE 0x4Uh. How do you know
page->mappingwould otherwise have bit 2 clear? Who's guaranteeing that?
Quoted 6 lines
Quoted 3 lines
This is an awfully big patch to the memory management code, buried in the middle of a gigantic series which almost guarantees nobody would look at it. I call shenanigans.
Are you calling shenanigans on the patch submitter (which is gratuitous) or on the KVM maintainers/reviewers?
On the patch submitter, of course. How can I possibly be criticising you for something you didn't do?
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Paolo Bonzini
maintainerRe: DANGER WILL ROBINSON, DANGER
Quoted 9 lines · Matthew Wilcox
Quoted 6 lines
Quoted 4 lines
This is an awfully big patch to the memory management code, buried in the middle of a gigantic series which almost guarantees nobody would look at it. I call shenanigans.
Are you calling shenanigans on the patch submitter (which is gratuitous) or on the KVM maintainers/reviewers?
On the patch submitter, of course. How can I possibly be criticising you for something you didn't do?
No idea. "Nobody would look at it" definitely includes me though.
In any case, water under the bridge. The submitter did duly mark the series as RFC, I don't see anything wrong in what he did apart from not having testcases. :)
Paolo
Re: DANGER WILL ROBINSON, DANGER
In reply to Matthew WilcoxQuoted 15 lines · Matthew Wilcox
include/linux/page-flags.h PAGEFLAG(Idle, idle, PF_ANY) · line 420
Quoted 6 lines · Adalbert Lazăr
+++
b/include/linux/page-flags.hinclude/linux/page-flags.h +1
-0@@ -417,8 +417,10 @@ PAGEFLAG(Idle, idle, PF_ANY)*/ #define PAGE_MAPPING_ANON 0x1#define PAGE_MAPPING_MOVABLE 0x2 +#define PAGE_MAPPING_REMOTE 0x4Uh. How do you know
page->mappingwould otherwise have bit 2 clear? Who's guaranteeing that?This is an awfully big patch to the memory management code, buried in the middle of a gigantic series which almost guarantees nobody would look at it. I call shenanigans.
Quoted 9 lines
mm/rmap.c void page_move_anon_rmap(struct page *page, struct vm_area_struct *vma) · line 1025
Quoted 6 lines
mm/rmap.c +1
-1@@ -1021,7 +1022,7 @@ void page_move_anon_rmap(struct page *page, struct vm_area_struct *vma)* __page_set_anon_rmap - set up new anonymous rmap * @page: Page or Hugepage to add to rmap * @vma: VM area to add page to. -* @address: User virtual address of the mapping +* @address: User virtual address of the mapping And mixing in fluff changes like this is a real no-no. Try again.
No bad intentions, just overzealous.
I didn't want to hide anything from our patches.
Once we advance with the introspection patches related to KVM we'll be back with the remote mapping patch, split and cleaned.
Thanks
Re: DANGER WILL ROBINSON, DANGER
Quoted 26 lines · Adalbert Lazăr
Quoted 15 lines · Matthew Wilcox
include/linux/page-flags.h PAGEFLAG(Idle, idle, PF_ANY) · line 420
Quoted 6 lines · Adalbert Lazăr
+++
b/include/linux/page-flags.hinclude/linux/page-flags.h +1
-0@@ -417,8 +417,10 @@ PAGEFLAG(Idle, idle, PF_ANY)*/ #define PAGE_MAPPING_ANON 0x1#define PAGE_MAPPING_MOVABLE 0x2 +#define PAGE_MAPPING_REMOTE 0x4Uh. How do you know
page->mappingwould otherwise have bit 2 clear? Who's guaranteeing that?This is an awfully big patch to the memory management code, buried in the middle of a gigantic series which almost guarantees nobody would look at it. I call shenanigans.
Quoted 9 lines
mm/rmap.c void page_move_anon_rmap(struct page *page, struct vm_area_struct *vma) · line 1025
Quoted 6 lines
mm/rmap.c +1
-1@@ -1021,7 +1022,7 @@ void page_move_anon_rmap(struct page *page, struct vm_area_struct *vma)* __page_set_anon_rmap - set up new anonymous rmap * @page: Page or Hugepage to add to rmap * @vma: VM area to add page to. -* @address: User virtual address of the mapping +* @address: User virtual address of the mapping And mixing in fluff changes like this is a real no-no. Try again.
Quoted 4 lines
No bad intentions, just overzealous.
I didn't want to hide anything from our patches.
Once we advance with the introspection patches related to KVM we'll be back with the remote mapping patch, split and cleaned.
They are not bit left in struct page ! Looking at the patch it seems you want to have your own pin count just for KVM. This is bad, we are already trying to solve the GUP thing (see all various patchset about GUP posted recently).
You need to rethink how you want to achieve this. Why not simply a remote read()/write() into the process memory ie KVMI would call an ioctl that allow to read or write into a remote process memory like ptrace() but on steroid ...
Adding this whole big complex infrastructure without justification of why we need to avoid round trip is just too much really.
Cheers,
Jérôme
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: DANGER WILL ROBINSON, DANGER
Quoted 37 lines · Jerome Glisse
Quoted 26 lines · Adalbert Lazăr
Quoted 15 lines · Matthew Wilcox
include/linux/page-flags.h PAGEFLAG(Idle, idle, PF_ANY) · line 420
Quoted 6 lines · Adalbert Lazăr
+++
b/include/linux/page-flags.hinclude/linux/page-flags.h +1
-0@@ -417,8 +417,10 @@ PAGEFLAG(Idle, idle, PF_ANY)*/ #define PAGE_MAPPING_ANON 0x1#define PAGE_MAPPING_MOVABLE 0x2 +#define PAGE_MAPPING_REMOTE 0x4Uh. How do you know
page->mappingwould otherwise have bit 2 clear? Who's guaranteeing that?This is an awfully big patch to the memory management code, buried in the middle of a gigantic series which almost guarantees nobody would look at it. I call shenanigans.
Quoted 9 lines
mm/rmap.c void page_move_anon_rmap(struct page *page, struct vm_area_struct *vma) · line 1025
Quoted 6 lines
mm/rmap.c +1
-1@@ -1021,7 +1022,7 @@ void page_move_anon_rmap(struct page *page, struct vm_area_struct *vma)* __page_set_anon_rmap - set up new anonymous rmap * @page: Page or Hugepage to add to rmap * @vma: VM area to add page to. -* @address: User virtual address of the mapping +* @address: User virtual address of the mapping And mixing in fluff changes like this is a real no-no. Try again.
Quoted 4 lines
No bad intentions, just overzealous.
I didn't want to hide anything from our patches.
Once we advance with the introspection patches related to KVM we'll be back with the remote mapping patch, split and cleaned.They are not bit left in struct page ! Looking at the patch it seems you want to have your own pin count just for KVM. This is bad, we are already trying to solve the GUP thing (see all various patchset about GUP posted recently).
Quoted 7 lines
You need to rethink how you want to achieve this. Why not simply a remote read()/write() into the process memory ie KVMI would call an ioctl that allow to read or write into a remote process memory like
ptrace()but on steroid ...Adding this whole big complex infrastructure without justification of why we need to avoid round trip is just too much really.
Thinking a bit more about this, you can achieve the same thing without adding a single line to any mm code. Instead of having mmap with PROT_NONE | MAP_LOCKED you have userspace mmap some kvm device file (i am assuming this is something you already have and can control the mmap callback).
So now kernel side you have a vma with a vm_operations_struct under your control this means that everything you want to block mm wise from within the inspector process can be block through those call- backs (find_special_page() specificaly for which you have to return NULL all the time).
To mirror target process memory you can use hmm_mirror, when you populate the inspector process page table you use insert_pfn() (mmap of the kvm device file must mark this vma as PFNMAP).
By following the hmm_mirror API, anytime the target process has a change in its page table (ie virtual address -> page) you will get a callback and all you have to do is clear the page table within the inspector process and flush tlb (use zap_page_range).
On page fault within the inspector process the fault callback of vm_ops will get call and from there you call hmm_mirror following its API.
Oh also mark the vma with VM_WIPEONFORK to avoid any issue if the inspector process use fork() (you could support fork but then you would need to mark the vma as SHARED and use unmap_mapping_pages instead of zap_page_range).
There everything you want to do with already upstream mm code.
Cheers,
Jérôme
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: DANGER WILL ROBINSON, DANGER
Quoted 52 lines · Jerome Glisse
Quoted 37 lines · Jerome Glisse
Quoted 26 lines · Adalbert Lazăr
Quoted 15 lines · Matthew Wilcox
include/linux/page-flags.h PAGEFLAG(Idle, idle, PF_ANY) · line 420
Quoted 6 lines · Adalbert Lazăr
+++
b/include/linux/page-flags.hinclude/linux/page-flags.h +1
-0@@ -417,8 +417,10 @@ PAGEFLAG(Idle, idle, PF_ANY)*/ #define PAGE_MAPPING_ANON 0x1#define PAGE_MAPPING_MOVABLE 0x2 +#define PAGE_MAPPING_REMOTE 0x4Uh. How do you know
page->mappingwould otherwise have bit 2 clear? Who's guaranteeing that?This is an awfully big patch to the memory management code, buried in the middle of a gigantic series which almost guarantees nobody would look at it. I call shenanigans.
Quoted 9 lines
mm/rmap.c void page_move_anon_rmap(struct page *page, struct vm_area_struct *vma) · line 1025
Quoted 6 lines
mm/rmap.c +1
-1@@ -1021,7 +1022,7 @@ void page_move_anon_rmap(struct page *page, struct vm_area_struct *vma)* __page_set_anon_rmap - set up new anonymous rmap * @page: Page or Hugepage to add to rmap * @vma: VM area to add page to. -* @address: User virtual address of the mapping +* @address: User virtual address of the mapping And mixing in fluff changes like this is a real no-no. Try again.
Quoted 4 lines
No bad intentions, just overzealous.
I didn't want to hide anything from our patches.
Once we advance with the introspection patches related to KVM we'll be back with the remote mapping patch, split and cleaned.They are not bit left in struct page ! Looking at the patch it seems you want to have your own pin count just for KVM. This is bad, we are already trying to solve the GUP thing (see all various patchset about GUP posted recently).
Quoted 7 lines
You need to rethink how you want to achieve this. Why not simply a remote read()/write() into the process memory ie KVMI would call an ioctl that allow to read or write into a remote process memory like
ptrace()but on steroid ...Adding this whole big complex infrastructure without justification of why we need to avoid round trip is just too much really.
Thinking a bit more about this, you can achieve the same thing without adding a single line to any mm code. Instead of having mmap with
PROT_NONE|MAP_LOCKEDyou have userspace mmap some kvm device file (i am assuming this is something you already have and can control the mmap callback).
Quoted 5 lines
So now kernel side you have a vma with a
vm_operations_structunder your control this means that everything you want to block mm wise from within the inspector process can be block through those call- backs (find_special_page()specificaly for which you have to return NULL all the time).
I'm actually aware of a couple of use cases that would like to mirror the VA of one process into another. One big one in the HPC world is the out of tree 'xpmem' still in wide use today. xpmem is basically what Jerome described above.
If you do an approach like Jerome describes it would be nice if was a general facility and not buried in kvm.
I know past xpmem adventures ran into trouble with locking/etc - ie getting the mm_struct of the victim seemed a bit hard for some reason, but maybe that could be done with a FD pass 'ioctl(I AM THE VICITM)' ?
Jason
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RE: DANGER WILL ROBINSON, DANGER
In reply to Jerome GlisseQuoted 73 lines · Jerome Glisse
Quoted 2 lines · Adalbert Lazăr
Quoted 1 line
On Fri, 9 Aug 2019 09:24:44 -0700, Matthew Wilcox <[email protected]>
Quoted 9 lines · wrote:
Quoted 9 lines
Quoted 9 lines · Adalbert Lazăr
include/linux/page-flags.h PAGEFLAG(Idle, idle, PF_ANY) · line 420
Quoted 6 lines
+++
b/include/linux/page-flags.hinclude/linux/page-flags.h +1
-0@@ -417,8 +417,10 @@ PAGEFLAG(Idle, idle, PF_ANY)*/ #define PAGE_MAPPING_ANON 0x1#define PAGE_MAPPING_MOVABLE 0x2 +#define PAGE_MAPPING_REMOTE 0x4Uh. How do you know
page->mappingwould otherwise have bit 2clear?
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Who's guaranteeing that?
This is an awfully big patch to the memory management code, buried in the middle of a gigantic series which almost guarantees nobody would look at it. I call shenanigans.
Review void page_move_anon_rmap(struct page
Quoted 1 line
(quoted diff) +0
-0@@ -1021,7 +1022,7 @@ void page_move_anon_rmap(struct page*page, struct
vm_area_struct*vma)Quoted 19 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
mm/rmap.c +1
-1* __page_set_anon_rmap - set up new anonymous rmap * @page: Page or Hugepage to add to rmap * @vma: VM area to add page to. -* @address: User virtual address of the mapping +* @address: User virtual address of the mapping Quoted 7 lines
Quoted 2 lines
And mixing in fluff changes like this is a real no-no. Try again.
No bad intentions, just overzealous.
I didn't want to hide anything from our patches.
Once we advance with the introspection patches related to KVM we'll be back with the remote mapping patch, split and cleaned.They are not bit left in struct page ! Looking at the patch it seems you want to have your own pin count just for KVM. This is bad, we are already trying to solve the GUP thing (see all various patchset about GUP posted recently).
Quoted 7 lines
You need to rethink how you want to achieve this. Why not simply a remote read()/write() into the process memory ie KVMI would call an ioctl that allow to read or write into a remote process memory like
ptrace()but on steroid ...Adding this whole big complex infrastructure without justification of why we need to avoid round trip is just too much really.
Thinking a bit more about this, you can achieve the same thing without adding a single line to any mm code. Instead of having mmap with
PROT_NONE|MAP_LOCKEDyou have userspace mmap some kvm device file (i am assuming this is something you already have and can control the mmap callback).So now kernel side you have a vma with a
vm_operations_structunder your control this means that everything you want to block mm wise from within the inspector process can be block through those call- backs (find_special_page()specificaly for which you have to return NULL all the time).To mirror target process memory you can use
hmm_mirror, when you populate the inspector process page table you useinsert_pfn()(mmap of the kvm device file must mark this vma as PFNMAP).By following the
hmm_mirrorAPI, anytime the target process has a change in its page table (ie virtual address -> page) you will get a callback and all you have to do is clear the page table within the inspector process and flush tlb (usezap_page_range).On page fault within the inspector process the fault callback of
vm_opswill get call and from there you callhmm_mirrorfollowing its API.
Quoted 7 lines
Oh also mark the vma with
VM_WIPEONFORKto avoid any issue if the inspector process usefork()(you could support fork but then you would need to mark the vma as SHARED and useunmap_mapping_pagesinstead ofzap_page_range).There everything you want to do with already upstream mm code.
I'm the author of remote mapping, so I owe everybody some explanations. My requirement was to map pages from one QEMU process to another QEMU process (our inspector process works in a virtual machine of its own). So I had to implement a KSM-like page sharing between processes, where an anon page from the target QEMU's working memory is promoted to a remote page and mapped in the inspector QEMU's working memory (both anon VMAs). The extra page flag is for differentiating the page for rmap walking.
The mapping requests come at PAGE_SIZE granularity for random addresses within the target/inspector QEMUs, so I couldn't do any linear mapping that would keep things simpler.
I have an extra patch that does remote mapping by mirroring an entire VMA from the target process by way of a device file. This thing creates a separate mirror VMA in my inspector process (at the moment a QEMU), but then I bumped into the KVM hva->gpa mapping, which makes it hard to override mappings with addresses outside memslot associated VMAs.
Mircea
Re: DANGER WILL ROBINSON, DANGER
Quoted 93 lines · Mircea CIRJALIU - MELIU
Quoted 73 lines · Jerome Glisse
Quoted 2 lines · Adalbert Lazăr
Quoted 1 line
On Fri, 9 Aug 2019 09:24:44 -0700, Matthew Wilcox <[email protected]>
Quoted 9 lines · wrote:
Quoted 9 lines
Quoted 9 lines · Adalbert Lazăr
include/linux/page-flags.h PAGEFLAG(Idle, idle, PF_ANY) · line 420
Quoted 6 lines
+++
b/include/linux/page-flags.hinclude/linux/page-flags.h +1
-0@@ -417,8 +417,10 @@ PAGEFLAG(Idle, idle, PF_ANY)*/ #define PAGE_MAPPING_ANON 0x1#define PAGE_MAPPING_MOVABLE 0x2 +#define PAGE_MAPPING_REMOTE 0x4Uh. How do you know
page->mappingwould otherwise have bit 2clear?
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Who's guaranteeing that?
This is an awfully big patch to the memory management code, buried in the middle of a gigantic series which almost guarantees nobody would look at it. I call shenanigans.
Review void page_move_anon_rmap(struct page
Quoted 1 line
(quoted diff) +0
-0@@ -1021,7 +1022,7 @@ void page_move_anon_rmap(struct page*page, struct
vm_area_struct*vma)Quoted 19 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
mm/rmap.c +1
-1* __page_set_anon_rmap - set up new anonymous rmap * @page: Page or Hugepage to add to rmap * @vma: VM area to add page to. -* @address: User virtual address of the mapping +* @address: User virtual address of the mapping Quoted 7 lines
Quoted 2 lines
And mixing in fluff changes like this is a real no-no. Try again.
No bad intentions, just overzealous.
I didn't want to hide anything from our patches.
Once we advance with the introspection patches related to KVM we'll be back with the remote mapping patch, split and cleaned.They are not bit left in struct page ! Looking at the patch it seems you want to have your own pin count just for KVM. This is bad, we are already trying to solve the GUP thing (see all various patchset about GUP posted recently).
Quoted 7 lines
You need to rethink how you want to achieve this. Why not simply a remote read()/write() into the process memory ie KVMI would call an ioctl that allow to read or write into a remote process memory like
ptrace()but on steroid ...Adding this whole big complex infrastructure without justification of why we need to avoid round trip is just too much really.
Thinking a bit more about this, you can achieve the same thing without adding a single line to any mm code. Instead of having mmap with
PROT_NONE|MAP_LOCKEDyou have userspace mmap some kvm device file (i am assuming this is something you already have and can control the mmap callback).So now kernel side you have a vma with a
vm_operations_structunder your control this means that everything you want to block mm wise from within the inspector process can be block through those call- backs (find_special_page()specificaly for which you have to return NULL all the time).To mirror target process memory you can use
hmm_mirror, when you populate the inspector process page table you useinsert_pfn()(mmap of the kvm device file must mark this vma as PFNMAP).By following the
hmm_mirrorAPI, anytime the target process has a change in its page table (ie virtual address -> page) you will get a callback and all you have to do is clear the page table within the inspector process and flush tlb (usezap_page_range).On page fault within the inspector process the fault callback of
vm_opswill get call and from there you callhmm_mirrorfollowing its API.Quoted 7 lines
Oh also mark the vma with
VM_WIPEONFORKto avoid any issue if the inspector process usefork()(you could support fork but then you would need to mark the vma as SHARED and useunmap_mapping_pagesinstead ofzap_page_range).There everything you want to do with already upstream mm code.
I'm the author of remote mapping, so I owe everybody some explanations. My requirement was to map pages from one QEMU process to another QEMU process (our inspector process works in a virtual machine of its own). So I had to implement a KSM-like page sharing between processes, where an anon page from the target QEMU's working memory is promoted to a remote page and mapped in the inspector QEMU's working memory (both anon VMAs). The extra page flag is for differentiating the page for rmap walking.
The mapping requests come at
PAGE_SIZEgranularity for random addresses within the target/inspector QEMUs, so I couldn't do any linear mapping that would keep things simpler.
Quoted 5 lines
I have an extra patch that does remote mapping by mirroring an entire VMA from the target process by way of a device file. This thing creates a separate mirror VMA in my inspector process (at the moment a QEMU), but then I bumped into the KVM
hva->gpamapping, which makes it hard to override mappings with addresses outside memslot associated VMAs.
Not sure i understand, you are saying that the solution i outline above does not work ? If so then i think you are wrong, in the above solution the importing process mmap a device file and the resulting vma is then populated using insert_pfn() and constantly keep synchronize with the target process through mirroring which means that you never have to look at the struct page ... you can mirror any kind of memory from the remote process.
Am i miss-understanding something here ?
Cheers,
Jérôme
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Paolo Bonzini
maintainerRe: DANGER WILL ROBINSON, DANGER
Quoted 7 lines · Jerome Glisse
Not sure i understand, you are saying that the solution i outline above does not work ? If so then i think you are wrong, in the above solution the importing process mmap a device file and the resulting vma is then populated using
insert_pfn()and constantly keep synchronize with the target process through mirroring which means that you never have to look at the struct page ... you can mirror any kind of memory from the remote process.
If insert_pfn in turn calls MMU notifiers for the target VMA (which would be the KVM MMU notifier), then that would work. Though I guess it would be possible to call MMU notifier update callbacks around the call to insert_pfn.
Paolo
RE: DANGER WILL ROBINSON, DANGER
Quoted 9 lines · Jerome Glisse
Quoted 7 lines
Not sure i understand, you are saying that the solution i outline above does not work ? If so then i think you are wrong, in the above solution the importing process mmap a device file and the resulting vma is then populated using
insert_pfn()and constantly keep synchronize with the target process through mirroring which means that you never have to look at the struct page ... you can mirror any kind of memory from the remote process.
Quoted 3 lines
If
insert_pfnin turn calls MMU notifiers for the target VMA (which would be the KVM MMU notifier), then that would work. Though I guess it would be possible to call MMU notifier update callbacks around the call toinsert_pfn.
Can't do that.
First, insert_pfn() uses set_pte_at() which won't trigger the MMU notifier on the target VMA. It's also static, so I'll have to access it thru vmf_insert_pfn() or vmf_insert_mixed().
Our model (the importing process is encapsulated in another VM) forces us to mirror certain pages from the anon VMA backing one VM's system RAM to the other VM's anon VMA.
Using the functions above means setting VM_PFNMAP|VM_MIXEDMAP on the target anon VMA, but I guess this breaks the VMA. Is this recommended?
Then, mapping anon pages from one VMA to another without fixing the refcount and the mapcount breaks the daemons that think they're working on a pure anon VMA (kcompactd, khugepaged).
Mircea
Re: DANGER WILL ROBINSON, DANGER
Quoted 9 lines · Mircea CIRJALIU - MELIU
Quoted 9 lines · Jerome Glisse
Quoted 7 lines
Not sure i understand, you are saying that the solution i outline above does not work ? If so then i think you are wrong, in the above solution the importing process mmap a device file and the resulting vma is then populated using
insert_pfn()and constantly keep synchronize with the target process through mirroring which means that you never have to look at the struct page ... you can mirror any kind of memory from the remote process.
Quoted 8 lines
Quoted 3 lines
If
insert_pfnin turn calls MMU notifiers for the target VMA (which would be the KVM MMU notifier), then that would work. Though I guess it would be possible to call MMU notifier update callbacks around the call toinsert_pfn.Can't do that.
First,insert_pfn()usesset_pte_at()which won't trigger the MMU notifier on the target VMA. It's also static, so I'll have to access it thruvmf_insert_pfn()orvmf_insert_mixed().
Why would you need to target mmu notifier on target vma ? You do not need that. The workflow is:
userspace:
ptr = mmap(/dev/kvm-mirroring-device, virtual_addresse_of_target)
Then when the mirroring process access ptr it triggers page fault that endup in the vm_operation_struct->fault() which is just doing:
kernel-kvm-mirroring-function:
kvm_mirror_page_fault(struct vm_fault *vmf) {
struct kvm_mirror_struct *kvmms;
kvmms = kvm_mirror_struct_from_file(vmf->vma->vm_file);
...
again:
hmm_range_register(&range);
hmm_range_snapshot(&range);
take_lock(kvmms->update);
if (!hmm_range_valid(&range)) {
vm_insert_pfn();
drop_lock(kvmms->update);
hmm_range_unregister(&range);
return VM_FAULT_NOPAGE;
}
drop_lock(kvmms->update);
goto again;
}The notifier callback:
kvmms_notifier_start() {
take_lock(kvmms->update);
clear_pte(start, end);
drop_lock(kvmms->update);
}Quoted 4 lines
Our model (the importing process is encapsulated in another VM) forces us to mirror certain pages from the anon VMA backing one VM's system RAM to the other VM's anon VMA.
The mirror does not have to be an anon vma it can very well be a device vma ie mmap of a device file. I do not see any reasons why the mirror need to be an anon vma. Please explain why.
Quoted 3 lines
Using the functions above means setting VM_PFNMAP|VM_MIXEDMAP on the target anon VMA, but I guess this breaks the VMA. Is this recommended?
The mirror vma should not be an anon vma.
Quoted 4 lines
Then, mapping anon pages from one VMA to another without fixing the refcount and the mapcount breaks the daemons that think they're working on a pure anon VMA (kcompactd, khugepaged).
Note here the target vma ie the mirroring one is a mmap of device file and thus is skip by all of the above (kcompactd, khugepaged, ...) it is fully ignore by core mm.
Thus you do not need to fix the refcount in any way. If any of the core mm try to reclaim memory from the original vma then you will get mmu notifier callbacks and all you have to do is clear the page table of your device vma.
I did exactly that as a tools in the past and it works just fine with no change to core mm whatsoever.
Cheers,
Jérôme
Paolo Bonzini
maintainerRe: DANGER WILL ROBINSON, DANGER
Quoted 14 lines · Jerome Glisse
Quoted 13 lines · Mircea CIRJALIU - MELIU
Quoted 12 lines · Jerome Glisse
Quoted 7 lines
Not sure i understand, you are saying that the solution i outline above does not work ? If so then i think you are wrong, in the above solution the importing process mmap a device file and the resulting vma is then populated using
insert_pfn()and constantly keep synchronize with the target process through mirroring which means that you never have to look at the struct page ... you can mirror any kind of memory from the remote process.If
insert_pfnin turn calls MMU notifiers for the target VMA (which would be the KVM MMU notifier), then that would work. Though I guess it would be possible to call MMU notifier update callbacks around the call toinsert_pfn.
Quoted 6 lines
Quoted 4 lines
Can't do that.
First,insert_pfn()usesset_pte_at()which won't trigger the MMU notifier on the target VMA. It's also static, so I'll have to access it thruvmf_insert_pfn()orvmf_insert_mixed().Why would you need to target mmu notifier on target vma ?
If the mapping of the source VMA changes, mirroring can update the target VMA via insert_pfn. But what ensures that KVM's MMU notifier dismantles its own existing page tables (so that they can be recreated with the new mapping from the source VMA)?
Thanks,
Paolo
Quoted 65 lines
You do not need
that. The workflow is:userspace:
ptr =mmap(/dev/kvm-mirroring-device, virtual_addresse_of_target)Then when the mirroring process access ptr it triggers page fault that endup in the
vm_operation_struct->fault()which is just doing:kernel-kvm-mirroring-function: kvm_mirror_page_fault(struct vm_fault *vmf) { struct kvm_mirror_struct *kvmms; kvmms = kvm_mirror_struct_from_file(vmf->vma->vm_file); ... again: hmm_range_register(&range); hmm_range_snapshot(&range); take_lock(kvmms->update); if (!hmm_range_valid(&range)) { vm_insert_pfn(); drop_lock(kvmms->update); hmm_range_unregister(&range); return VM_FAULT_NOPAGE; } drop_lock(kvmms->update); goto again; }The notifier callback:
kvmms_notifier_start() { take_lock(kvmms->update); clear_pte(start, end); drop_lock(kvmms->update); }Quoted 4 lines
Our model (the importing process is encapsulated in another VM) forces us to mirror certain pages from the anon VMA backing one VM's system RAM to the other VM's anon VMA.
The mirror does not have to be an anon vma it can very well be a device vma ie mmap of a device file. I do not see any reasons why the mirror need to be an anon vma. Please explain why.
Quoted 3 lines
Using the functions above means setting VM_PFNMAP|VM_MIXEDMAP on the target anon VMA, but I guess this breaks the VMA. Is this recommended?
The mirror vma should not be an anon vma.
Quoted 4 lines
Then, mapping anon pages from one VMA to another without fixing the refcount and the mapcount breaks the daemons that think they're working on a pure anon VMA (kcompactd, khugepaged).
Note here the target vma ie the mirroring one is a mmap of device file and thus is skip by all of the above (kcompactd, khugepaged, ...) it is fully ignore by core mm.
Thus you do not need to fix the refcount in any way. If any of the core mm try to reclaim memory from the original vma then you will get mmu notifier callbacks and all you have to do is clear the page table of your device vma.
Quoted 6 lines
I did exactly that as a tools in the past and it works just fine with no change to core mm whatsoever.
Cheers,
Jérôme
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: DANGER WILL ROBINSON, DANGER
Quoted 20 lines · Paolo Bonzini
Quoted 19 lines · Jerome Glisse
Quoted 17 lines · Mircea CIRJALIU - MELIU
Quoted 9 lines · Jerome Glisse
Quoted 7 lines
Not sure i understand, you are saying that the solution i outline above does not work ? If so then i think you are wrong, in the above solution the importing process mmap a device file and the resulting vma is then populated using
insert_pfn()and constantly keep synchronize with the target process through mirroring which means that you never have to look at the struct page ... you can mirror any kind of memory from the remote process.Quoted 3 lines
If
insert_pfnin turn calls MMU notifiers for the target VMA (which would be the KVM MMU notifier), then that would work. Though I guess it would be possible to call MMU notifier update callbacks around the call toinsert_pfn.Can't do that.
First,insert_pfn()usesset_pte_at()which won't trigger the MMU notifier on the target VMA. It's also static, so I'll have to access it thruvmf_insert_pfn()orvmf_insert_mixed().
Quoted 7 lines
Quoted 1 line
Why would you need to target mmu notifier on target vma ?
If the mapping of the source VMA changes, mirroring can update the target VMA via
insert_pfn. But what ensures that KVM's MMU notifier dismantles its own existing page tables (so that they can be recreated with the new mapping from the source VMA)?
So just to make sure i follow we have:
- qemu process on host with anonymous vma
-> host cpu page table
- kvm which maps host anonymous vma to guest
-> kvm guest page table
- kvm inspector process which mirror vma from qemu process -> inspector process page table
AFAIK the KVM notifier's will clear the kvm guest page table whenever necessary (through kvm_mmu_notifier_invalidate_range_start). This is what ensure that KVM's dismatles its own mapping, it abides to mmu- notifier callbacks. If you did not you would have bugs (at least i expect so). Am i wrong here ?
The mirroring kernel driver would also register the notifier against the quemu process and would also abide to notifier callbacks.
What you want to maintain at all times is that none of the actors above ever look at different page for the same virtual address (ie one looking at older page while another look at new page).
This is where you have helper like HMM that make sure that you can not populate the mirroring vma while a notifier is on going. Which means that everything is serialize on the notifier.
Cheers,
Jérôme
Paolo Bonzini
maintainerRe: DANGER WILL ROBINSON, DANGER
Quoted 14 lines · Jerome Glisse
Quoted 6 lines
Quoted 1 line
Why would you need to target mmu notifier on target vma ?
If the mapping of the source VMA changes, mirroring can update the target VMA via
insert_pfn. But what ensures that KVM's MMU notifier dismantles its own existing page tables (so that they can be recreated with the new mapping from the source VMA)?So just to make sure i follow we have:
- qemu process on host with anonymous vma
-> host cpu page table
- kvm which maps host anonymous vma to guest
-> kvm guest page table
- kvm inspector process which mirror vma from qemu process -> inspector process page table
Quoted 5 lines
AFAIK the KVM notifier's will clear the kvm guest page table whenever necessary (through
kvm_mmu_notifier_invalidate_range_start). This is what ensure that KVM's dismatles its own mapping, it abides to mmu- notifier callbacks. If you did not you would have bugs (at least i expect so). Am i wrong here ?
The KVM inspector process is also (or can be) a QEMU that will have to create its own KVM guest page table.
So if a page in the source VMA is unmapped we want:
- the source KVM to invalidate its guest page table (done by the KVM MMU notifier)
- the target VMA to be invalidated (easy using mirroring)
- the target KVM to invalidate its guest page table, as a result of invalidation of the target VMA
Paolo
Re: DANGER WILL ROBINSON, DANGER
Quoted 15 lines · Paolo Bonzini
Quoted 14 lines · Jerome Glisse
Quoted 6 lines
Quoted 1 line
Why would you need to target mmu notifier on target vma ?
If the mapping of the source VMA changes, mirroring can update the target VMA via
insert_pfn. But what ensures that KVM's MMU notifier dismantles its own existing page tables (so that they can be recreated with the new mapping from the source VMA)?So just to make sure i follow we have:
- qemu process on host with anonymous vma
-> host cpu page table
- kvm which maps host anonymous vma to guest
-> kvm guest page table
- kvm inspector process which mirror vma from qemu process -> inspector process page table
Quoted 8 lines
Quoted 5 lines
AFAIK the KVM notifier's will clear the kvm guest page table whenever necessary (through
kvm_mmu_notifier_invalidate_range_start). This is what ensure that KVM's dismatles its own mapping, it abides to mmu- notifier callbacks. If you did not you would have bugs (at least i expect so). Am i wrong here ?The KVM inspector process is also (or can be) a QEMU that will have to create its own KVM guest page table.
Ok missed that part, thank you for explaining
Quoted 10 lines
So if a page in the source VMA is unmapped we want:
- the source KVM to invalidate its guest page table (done by the KVM MMU notifier)
- the target VMA to be invalidated (easy using mirroring)
- the target KVM to invalidate its guest page table, as a result of invalidation of the target VMA
You can do the target KVM invalidation inside the mirroring invalidation code.
Cheers,
Jérôme
Paolo Bonzini
maintainerRe: DANGER WILL ROBINSON, DANGER
Quoted 13 lines · Jerome Glisse
Quoted 12 lines · Paolo Bonzini
Quoted 4 lines
Quoted 4 lines
If the mapping of the source VMA changes, mirroring can update the target VMA via
insert_pfn. But what ensures that KVM's MMU notifier dismantles its own existing page tables (so that they can be recreated with the new mapping from the source VMA)?The KVM inspector process is also (or can be) a QEMU that will have to create its own KVM guest page table. So if a page in the source VMA is unmapped we want:
- the source KVM to invalidate its guest page table (done by the KVM MMU notifier)
Quoted 7 lines
Quoted 4 lines
- the target VMA to be invalidated (easy using mirroring)
- the target KVM to invalidate its guest page table, as a result of invalidation of the target VMA
You can do the target KVM invalidation inside the mirroring invalidation code.
Why should the source and target KVMs behave differently? If the source invalidates its guest page table via MMU notifiers, so should the target.
The KVM MMU notifier exists so that nothing (including mirroring) needs to know that there is KVM on the other side. Any interaction between KVM page tables and VMAs must be mediated by MMU notifiers, anything else is unacceptable.
If it is possible to invoke the MMU notifiers around the calls to insert_pfn, that of course would be perfect.
Thanks,
Paolo
Re: DANGER WILL ROBINSON, DANGER
Quoted 25 lines · Paolo Bonzini
Quoted 13 lines · Jerome Glisse
Quoted 12 lines · Paolo Bonzini
Quoted 4 lines
Quoted 4 lines
If the mapping of the source VMA changes, mirroring can update the target VMA via
insert_pfn. But what ensures that KVM's MMU notifier dismantles its own existing page tables (so that they can be recreated with the new mapping from the source VMA)?The KVM inspector process is also (or can be) a QEMU that will have to create its own KVM guest page table. So if a page in the source VMA is unmapped we want:
- the source KVM to invalidate its guest page table (done by the KVM MMU notifier)
Quoted 7 lines
Quoted 4 lines
- the target VMA to be invalidated (easy using mirroring)
- the target KVM to invalidate its guest page table, as a result of invalidation of the target VMA
You can do the target KVM invalidation inside the mirroring invalidation code.
Why should the source and target KVMs behave differently? If the source invalidates its guest page table via MMU notifiers, so should the target.
Quoted 7 lines
The KVM MMU notifier exists so that nothing (including mirroring) needs to know that there is KVM on the other side. Any interaction between KVM page tables and VMAs must be mediated by MMU notifiers, anything else is unacceptable.
If it is possible to invoke the MMU notifiers around the calls to
insert_pfn, that of course would be perfect.
Ok and yes you can do that exactly ie inside the mmu notifier callback from the target. For instance it is as easy as:
target_mirror_notifier_start_callback(start, end) {
struct kvm_mirror_struct *kvmms = from_mmun(...);
unsigned long target_foff, size;
size = end - start;
target_foff = kvmms_convert_mirror_address(start);
take_lock(kvmms->mirror_fault_exclusion_lock);
unmap_mapping_range(kvmms->address_space, target_foff, size, 1);
drop_lock(kvmms->mirror_fault_exclusion_lock);
}All that is needed is to make sure that vm_normal_page() will see those pte (inside the process that is mirroring the other process) as special which is the case either because insert_pfn() mark the pte as special or the kvm device driver which control the vm_operation struct set a find_special_page() callback that always return NULL, or the vma has either VM_PFNMAP or VM_MIXEDMAP set (which is the case with insert_pfn).
So you can keep the existing kvm code unmodified.
Cheers,
Jérôme
Paolo Bonzini
maintainerRe: DANGER WILL ROBINSON, DANGER
Quoted 8 lines · Jerome Glisse
All that is needed is to make sure that
vm_normal_page()will see those pte (inside the process that is mirroring the other process) as special which is the case either becauseinsert_pfn()mark the pte as special or the kvm device driver which control thevm_operationstruct set afind_special_page()callback that always return NULL, or the vma has eitherVM_PFNMAPorVM_MIXEDMAPset (which is the case withinsert_pfn).So you can keep the existing kvm code unmodified.
Great, thanks. And KVM is already able to handle VM_PFNMAP/VM_MIXEDMAP, so that should work.
Paolo
RE: DANGER WILL ROBINSON, DANGER
Quoted 8 lines · Jerome Glisse
Quoted 7 lines
All that is needed is to make sure that
vm_normal_page()will see those pte (inside the process that is mirroring the other process) as special which is the case either becauseinsert_pfn()mark the pte as special or the kvm device driver which control thevm_operationstruct set afind_special_page()callback that always return NULL, or the vma has eitherVM_PFNMAPorVM_MIXEDMAPset (which is the case with
Quoted 6 lines
insert_pfn).Quoted 2 lines
So you can keep the existing kvm code unmodified.
Great, thanks. And KVM is already able to handle
VM_PFNMAP/VM_MIXEDMAP, so that should work.
This means setting VM_PFNMAP/VM_MIXEDMAP on the anon VMA that acts as the VM's system RAM. Will it have any side effects?
Re: DANGER WILL ROBINSON, DANGER
Quoted 10 lines · Mircea CIRJALIU - MELIU
Quoted 10 lines · Jerome Glisse
Quoted 7 lines
All that is needed is to make sure that
vm_normal_page()will see those pte (inside the process that is mirroring the other process) as special which is the case either becauseinsert_pfn()mark the pte as special or the kvm device driver which control thevm_operationstruct set afind_special_page()callback that always return NULL, or the vma has eitherVM_PFNMAPorVM_MIXEDMAPset (which is the case with
insert_pfn).Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
So you can keep the existing kvm code unmodified.
Great, thanks. And KVM is already able to handle
VM_PFNMAP/VM_MIXEDMAP, so that should work.This means setting VM_PFNMAP/VM_MIXEDMAP on the anon VMA that acts as the VM's system RAM. Will it have any side effects?
You do not set it up on the anonymous vma but on the mmap of the kvm device file, the resulting vma is under the control of the kvm device file and is not an anonymous vma but a "device" special vma.
So in summary, the source qemu process has anonymous vma (regular libc malloc for instance). The introspector qemu process which mirror the the source qemu use mmap on /dev/kvm (assuming you can reuse the kvm device file for this otherwise you can introduce a new kvm device file). The resulting mmap inside the introspector qemu process is a vma which has vma->vm_file pointing to the kvm device file and has VM_PFNMAP or VM_MIXEDMAP (i think you want the former). On architecture with ARCH_SPECIAL_PTE the pte will be mark as special when using insert_pfn() on other architecture you can either rely on VM_PFNMAP/VM_MIXEDMAP flag or set a specific find_special_page() callbacks in vm_ops.
I am at a conference right now but i will put an example of what i mean next week.
Cheers,
Jérôme
Paolo Bonzini
maintainerRe: DANGER WILL ROBINSON, DANGER
Quoted 5 lines · Jerome Glisse
So in summary, the source qemu process has anonymous vma (regular libc malloc for instance). The introspector qemu process which mirror the the source qemu use mmap on
/dev/kvm(assuming you can reuse the kvm device file for this otherwise you can introduce a new kvm device file).
It should be a new device, something like /dev/kvmmem. BitDefender's RFC patches already have the right userspace API, that was not an issue.
Paolo
RE: DANGER WILL ROBINSON, DANGER
Quoted 10 lines · Jerome Glisse
Quoted 3 lines
So in summary, the source qemu process has anonymous vma (regular libc malloc for instance). The introspector qemu process which mirror the the source qemu use mmap on
/dev/kvm(assuming you can reuse thekvm
Quoted 2 lines
device file for this otherwise you can introduce a new kvm device file).
It should be a new device, something like
/dev/kvmmem. BitDefender's RFC patches already have the right userspace API, that was not an issue.
I get it so far. I have a patch that does mirroring in a separate VMA. We create an extra VMA with VM_PFNMAP/VM_MIXEDMAP that mirrors the source VMA in the other QEMU and is refreshed by the device MMU notifier.
This is a simple choice for an introspector process that runs on the same host as the source QEMU. But how do I make the new VMA accessible as memory to the guest VM inside the introspector QEMU? I was thinking of 2 solutions:
Create a new memslot based on the mirror VMA, hotplug it into the guest as new memory device (is this possible?) and have a guest-side driver allocate pages from that area.
or
Redirect (some) GFN->HVA translations into the new VMA based on a table of addresses required by the introspector process.
Mircea
Paolo Bonzini
maintainerRe: DANGER WILL ROBINSON, DANGER
Quoted 3 lines · Mircea CIRJALIU - MELIU
I get it so far. I have a patch that does mirroring in a separate VMA. We create an extra VMA with VM_PFNMAP/VM_MIXEDMAP that mirrors the source VMA in the other QEMU and is refreshed by the device MMU notifier.
So for example on the host you'd have a new ioctl on the kvm file descriptor. You pass a size and you get back a file descriptor for that guest's physical memory, which is mmap-able up to the size you specified in the ioctl.
In turn, the file descriptor would have ioctls to map/unmap ranges of the guest memory into its mmap-able range. Accessing an unmapped range produces a SIGSEGV.
When asked via the QEMU monitor, QEMU will create the file descriptor and pass it back via SCM_RIGHTS. The management application can then use it to hotplug memory into the destination...
Quoted 3 lines
Create a new memslot based on the mirror VMA, hotplug it into the guest as new memory device (is this possible?) and have a guest-side driver allocate pages from that area.
... using the existing ivshmem device, whose BAR can be accessed and mmap-ed from the guest via sysfs. In other words, the hotplugging will use the file descriptor returned by QEMU when creating the ivshmem device.
We then need an additional mechanism to invoke the map/unmap ioctls from the guest. Without writing a guest-side driver it is possible to:
- pass a socket into the "create guest physical memory view" ioctl above. KVM will then associate that KVMI socket with the newly created file descriptor.
- use KVMI messages to that socket to map/unmap sections of memory
Quoted 2 lines
Redirect (some)
GFN->HVAtranslations into the new VMA based on a table of addresses required by the introspector process.
That would be tricky because there are multiple paths (gfn_to_page, gfn_to_pfn, etc.).
There is some complication in this because the new device has to be plumbed at multiple levels (KVM, QEMU, libvirt). But it seems like a very easily separated piece of code (except for the KVMI socket part, which can be added later), so I suggest that you contribute the KVM parts first.
Paolo
RE: DANGER WILL ROBINSON, DANGER
In reply to Paolo BonziniQuoted 7 lines
The KVM MMU notifier exists so that nothing (including mirroring) needs to know that there is KVM on the other side. Any interaction between KVM page tables and VMAs must be mediated by MMU notifiers, anything else is unacceptable.
If it is possible to invoke the MMU notifiers around the calls to
insert_pfn, that of course would be perfect.
Looks to me like a work-around.
Any reason why insert_pfn() can't do set_pte_at_notify() so it triggers the KVM MMU notifier instead?
Re: DANGER WILL ROBINSON, DANGER
In reply to Matthew WilcoxQuoted 15 lines · Matthew Wilcox
include/linux/page-flags.h PAGEFLAG(Idle, idle, PF_ANY) · line 420
Quoted 6 lines · Adalbert Lazăr
+++
b/include/linux/page-flags.hinclude/linux/page-flags.h +1
-0@@ -417,8 +417,10 @@ PAGEFLAG(Idle, idle, PF_ANY)*/ #define PAGE_MAPPING_ANON 0x1#define PAGE_MAPPING_MOVABLE 0x2 +#define PAGE_MAPPING_REMOTE 0x4Uh. How do you know
page->mappingwould otherwise have bit 2 clear? Who's guaranteeing that?This is an awfully big patch to the memory management code, buried in the middle of a gigantic series which almost guarantees nobody would look at it. I call shenanigans.
Quoted 9 lines
mm/rmap.c void page_move_anon_rmap(struct page *page, struct vm_area_struct *vma) · line 1025
Quoted 6 lines
mm/rmap.c +1
-1@@ -1021,7 +1022,7 @@ void page_move_anon_rmap(struct page *page, struct vm_area_struct *vma)* __page_set_anon_rmap - set up new anonymous rmap * @page: Page or Hugepage to add to rmap * @vma: VM area to add page to. -* @address: User virtual address of the mapping +* @address: User virtual address of the mapping And mixing in fluff changes like this is a real no-no. Try again.
No bad intentions, just overzealous.
I didn't want to hide anything from our patches.
Once we advance with the introspection patches related to KVM we'll be back with the remote mapping patch, split and cleaned.
Thanks
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 72/92 kvm: introspection: add memory map/unmap support on the guest side
In reply to Adalbert LazărFrom: Mircea Cîrjaliu <[email protected]>
An introspection tool running in a dedicated VM can use the new device (/dev/kvmmem) to map memory from other introspected VM-s.
Two ioctl operations are supported:
- KVM_HC_MEM_MAP/struct kvmi_mem_map
- KVM_HC_MEM_UNMAP/unsigned long
In order to map an introspected gpa to the local gva, the process using this device needs to obtain a token from the host KVMI subsystem (see Documentation/virtual/kvm/kvmi.rst - KVMI_GET_MAP_TOKEN).
Both operations use hypercalls (KVM_HC_MEM_MAP, KVM_HC_MEM_UNMAP) to pass the requests to the host kernel/KVMi (see hypercalls.txt).
--- Documentation/virtual/kvm/hypercalls.txt | 34 ++ arch/x86/Kconfig | 9 + arch/x86/include/asm/kvmi_guest.h | 10 + arch/x86/kernel/Makefile | 1 + arch/x86/kernel/kvmi_mem_guest.c | 26 + include/uapi/linux/kvm_para.h | 2 + include/uapi/linux/kvmi.h | 21 + virt/kvm/kvmi_mem_guest.c | 651 +++++++++++++++++++++++ 8 files changed, 754 insertions(+) create mode 100644 arch/x86/include/asm/kvmi_guest.h create mode 100644 arch/x86/kernel/kvmi_mem_guest.c create mode 100644 virt/kvm/kvmi_mem_guest.c
Documentation/virtual/kvm/hypercalls.txt +34 -0
@@ -173,3 +173,37 @@ The following registers are clobbered: In particular, for KVM_HC_XEN_HVM_OP_GUEST_REQUEST_VM_EVENT, the last two registers can be poisoned deliberately and cannot be used for passing information. + +9. KVM_HC_MEM_MAP +----------------- + +Architecture: x86 +Status: active +Purpose: Map a guest physical page to another VM (the introspector). +Usage: + +a0: pointer to a token obtained with a KVMI_GET_MAP_TOKEN command (see kvmi.rst) + struct kvmi_map_mem_token { + __u64 token[4]; + }; + +a1: guest physical address to be mapped + +a2: guest physical address from introspector that will be replaced + +Both guest physical addresses will end up poiting to the same physical page. + +Returns any error that the memory manager can return. + +10. KVM_HC_MEM_UNMAP +------------------- + +Architecture: x86 +Status: active +Purpose: Unmap a previously mapped page. +Usage: + +a0: guest physical address from introspector + +The address will stop pointing to the introspected page and a new physical +page is allocated for this gpa.
arch/x86/Kconfig +9 -0
@@ -820,6 +820,15 @@ config KVM_DEBUG_FS Statistics are displayed in debugfs filesystem. Enabling this option may incur significant overhead. +config KVM_INTROSPECTION_GUEST + bool "KVM Memory Introspection support on Guest" + depends on KVM_GUEST + default n + help + This option enables functions and hypercalls for security applications + running in a separate VM to control the execution of other VM-s, query + the state of the vCPU-s (GPR-s, MSR-s etc.). + config PARAVIRT_TIME_ACCOUNTING bool "Paravirtual steal time accounting" depends on PARAVIRT
arch/x86/include/asm/kvmi_guest.h new file +10 -0
@@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __KVMI_GUEST_H__ +#define __KVMI_GUEST_H__ + +long kvmi_arch_map_hc (struct kvmi_map_mem_token *tknp, +gpa_t req_gpa,gpa_t map_gpa); +long kvmi_arch_unmap_hc (gpa_t map_gpa); + + +#endif
arch/x86/kernel/Makefile +1 -0
@@ -116,6 +116,7 @@ obj-$(CONFIG_PARAVIRT) += paravirt.o paravirt_patch_$(BITS).o obj-$(CONFIG_PARAVIRT_SPINLOCKS)+= paravirt-spinlocks.o obj-$(CONFIG_PARAVIRT_CLOCK) += pvclock.o obj-$(CONFIG_X86_PMEM_LEGACY_DEVICE) += pmem.o +obj-$(CONFIG_KVM_INTROSPECTION_GUEST) += kvmi_mem_guest.o ../../../virt/kvm/kvmi_mem_guest.o obj-$(CONFIG_JAILHOUSE_GUEST) += jailhouse.o
arch/x86/kernel/kvmi_mem_guest.c new file +26 -0
@@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* KVM introspection guest implementation +* +* Copyright (C) 2017 Bitdefender S.R.L. +* +* Author: +* Mircea Cirjaliu <[email protected]> +*/ + +#include <uapi/linux/kvmi.h> +#include <uapi/linux/kvm_para.h> +#include <linux/kvm_types.h> +#include <asm/kvm_para.h> + +long kvmi_arch_map_hc (struct kvmi_map_mem_token *tknp, +gpa_t req_gpa,gpa_t map_gpa) +{ +return kvm_hypercall3 (KVM_HC_MEM_MAP , (unsigned long )tknp, + req_gpa, map_gpa); +} + +long kvmi_arch_unmap_hc (gpa_t map_gpa) +{ +return kvm_hypercall1 (KVM_HC_MEM_UNMAP , map_gpa); +}
include/uapi/linux/kvm_para.h +2 -0
@@ -33,6 +33,8 @@#define KVM_HC_CLOCK_PAIRING 9#define KVM_HC_SEND_IPI 10 +#define KVM_HC_MEM_MAP 32 +#define KVM_HC_MEM_UNMAP 33#define KVM_HC_XEN_HVM_OP 34/* Xen's __HYPERVISOR_hvm_op */ /*
include/uapi/linux/kvmi.h +21 -0
@@ -262,4 +262,25 @@ struct kvmi_event_breakpoint { __u8 padding[7]; }; +struct kvmi_map_mem_token { +__u64 token[4 ]; +}; + +struct kvmi_get_map_token_reply { +struct kvmi_map_mem_token token; +}; + +/* Map other guest's gpa to local gva */ +struct kvmi_mem_map { +struct kvmi_map_mem_token token; +__u64 gpa; +__u64 gva; +}; + +/* +* ioctls for /dev/kvmmem +*/ +#define KVM_INTRO_MEM_MAP _IOW('i', 0x01, struct kvmi_mem_map) +#define KVM_INTRO_MEM_UNMAP _IOW('i', 0x02, unsigned long) +#endif /* _UAPI__LINUX_KVMI_H */
virt/kvm/kvmi_mem_guest.c new file +651 -0
651 lines not on this page · raw message
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 73/92 kvm: introspection: use remote mapping
In reply to Adalbert LazărFrom: Mircea Cîrjaliu <[email protected]>
This commit adds the missing KVMI_GET_MAP_TOKEN command and handle the hypercalls used to map/unmap guest pages.
--- Documentation/virtual/kvm/kvmi.rst | 39 ++++ arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/x86.c | 6 + include/linux/kvmi.h | 3 + virt/kvm/kvmi.c | 12 +- virt/kvm/kvmi_int.h | 10 + virt/kvm/kvmi_mem.c | 319 +++++++++++++++++++++++++++++ virt/kvm/kvmi_msg.c | 15 ++ 8 files changed, 404 insertions(+), 2 deletions(-) create mode 100644 virt/kvm/kvmi_mem.c
Documentation/virtual/kvm/kvmi.rst +39 -0
@@ -1144,6 +1144,45 @@ Returns the guest memory type for a specific physical address. * -KVM_EINVAL - padding is not zero * -KVM_EAGAIN - the selected vCPU can't be introspected yet +25. KVMI_GET_MAP_TOKEN +---------------------- + +:Architecture: all +:Versions: >= 1 +:Parameters: none +:Returns: + +:: + + struct kvmi_error_code; + struct kvmi_get_map_token_reply { + struct kvmi_map_mem_token token; + }; + +Where:: + + struct kvmi_map_mem_token { + __u64 token[4]; + }; + +Requests a token for a memory map operation. + +On this command, the host generates a random token to be used (once) +to map a physical page from the introspected guest. The introspector +could use the token with the KVM_INTRO_MEM_MAP ioctl (on /dev/kvmmem) +to map a guest physical page to one of its memory pages. The ioctl, +in turn, will use the KVM_HC_MEM_MAP hypercall (see hypercalls.txt). + +The guest kernel exposing /dev/kvmmem keeps a list with all the mappings +(to all the guests introspected by the tool) in order to unmap them +(using the KVM_HC_MEM_UNMAP hypercall) when /dev/kvmmem is closed or on +demand (using the KVM_INTRO_MEM_UNMAP ioctl). + +:Errors: + +* -KVM_EAGAIN - too many tokens have accumulated +* -KVM_ENOMEM - not enough memory to allocate a new token + Events ======
arch/x86/kvm/Makefile +1 -1
@@ -7,7 +7,7 @@ KVM := ../../../virt/kvm kvm-y += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o \ $(KVM)/eventfd.o $(KVM)/irqchip.o $(KVM)/vfio.o kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o -kvm-$(CONFIG_KVM_INTROSPECTION) += $(KVM)/kvmi.o $(KVM)/kvmi_msg.o kvmi.o +kvm-$(CONFIG_KVM_INTROSPECTION) += $(KVM)/kvmi.o $(KVM)/kvmi_msg.o $(KVM)/kvmi_mem.o kvmi.o kvm-y += x86.o mmu.o emulate.o i8259.o irq.o lapic.o \ i8254.o ioapic.o irq_comm.o cpuid.o pmu.o mtrr.o \
arch/x86/kvm/x86.c +6 -0
@@ -7337,6 +7337,12 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) ret =kvm_pv_send_ipi (vcpu->kvm, a0, a1, a2, a3, op_64_bit);break ;#ifdef CONFIG_KVM_INTROSPECTION +case KVM_HC_MEM_MAP : + ret =kvmi_host_mem_map (vcpu, (gva_t )a0, (gpa_t )a1, (gpa_t )a2); +break ; +case KVM_HC_MEM_UNMAP : + ret =kvmi_host_mem_unmap (vcpu, (gpa_t )a0); +break ;case KVM_HC_XEN_HVM_OP : ret =0 ;if (!kvmi_hypercall_event (vcpu))
include/linux/kvmi.h +3 -0
@@ -24,6 +24,9 @@ bool kvmi_descriptor_event(struct kvm_vcpu *vcpu, u8 descriptor, u8 write);bool kvmi_tracked_gfn (struct kvm_vcpu *vcpu,gfn_t gfn);bool kvmi_single_step (struct kvm_vcpu *vcpu,gpa_t gpa,int *emulation_type);void kvmi_handle_requests (struct kvm_vcpu *vcpu); +int kvmi_host_mem_map (struct kvm_vcpu *vcpu,gva_t tkn_gva, +gpa_t req_gpa,gpa_t map_gpa); +int kvmi_host_mem_unmap (struct kvm_vcpu *vcpu,gpa_t map_gpa);void kvmi_stop_ss (struct kvm_vcpu *vcpu);bool kvmi_vcpu_enabled_ss (struct kvm_vcpu *vcpu);void kvmi_init_emulate (struct kvm_vcpu *vcpu);
virt/kvm/kvmi.c +11 -1
@@ -10,6 +10,7 @@#include "kvmi_int.h" #include <linux/kthread.h> #include <linux/bitmap.h> +#include <linux/remote_mapping.h> #define MAX_PAUSE_REQUESTS 1001
@@ -320,11 +321,13 @@ static int kvmi_cache_create(void)int kvmi_init (void ) { +kvmi_mem_init ();return kvmi_cache_create (); }void kvmi_uninit (void ) { +kvmi_mem_exit ();kvmi_cache_destroy (); }
@@ -1647,6 +1650,11 @@ int kvmi_cmd_write_physical(struct kvm *kvm, u64 gpa, u64 size, const void *buf)return 0 ; } +int kvmi_cmd_alloc_token (struct kvm *kvm,struct kvmi_map_mem_token *token) +{ +return kvmi_mem_generate_token (kvm, token); +} +int kvmi_cmd_control_events (struct kvm_vcpu *vcpu,unsigned int event_id,bool enable) {
@@ -2015,7 +2023,9 @@ int kvmi_ioctl_unhook(struct kvm *kvm, bool force_reset)if (!ikvm)return -EFAULT ; -if (!force_reset && !kvmi_unhook_event (kvm)) +if (force_reset) +mm_remote_reset (); +else if (!kvmi_unhook_event (kvm)) err = -ENOENT ;kvmi_put (kvm);
virt/kvm/kvmi_int.h +10 -0
@@ -148,6 +148,8 @@ struct kvmi {struct task_struct *recv;atomic_t ev_seq; +atomic_t num_tokens; +uuid_t uuid;DECLARE_BITMAP (cmd_allow_mask,KVMI_NUM_COMMANDS );
@@ -229,7 +231,9 @@ int kvmi_cmd_control_events(struct kvm_vcpu *vcpu, unsigned int event_id, bool enable);int kvmi_cmd_control_vm_events (struct kvmi *ikvm,unsigned int event_id,bool enable); +int kvmi_cmd_alloc_token (struct kvm *kvm,struct kvmi_map_mem_token *token);int kvmi_cmd_pause_vcpu (struct kvm_vcpu *vcpu,bool wait); +unsigned long gfn_to_hva_safe (struct kvm *kvm,gfn_t gfn);struct kvmi *__must_check kvmi_get (struct kvm *kvm);void kvmi_put (struct kvm *kvm);int kvmi_run_jobs_and_wait (struct kvm_vcpu *vcpu);
@@ -298,4 +302,10 @@ int kvmi_arch_cmd_control_msr(struct kvm_vcpu *vcpu, const struct kvmi_control_msr *req);int kvmi_arch_cmd_get_mtrr_type (struct kvm_vcpu *vcpu,u64 gpa,u8 *type); +/* kvmi_mem.c */ +void kvmi_mem_init (void ); +void kvmi_mem_exit (void ); +int kvmi_mem_generate_token (struct kvm *kvm,struct kvmi_map_mem_token *token); +void kvmi_clear_vm_tokens (struct kvm *kvm); +#endif
virt/kvm/kvmi_mem.c new file +319 -0
319 lines not on this page · raw message
virt/kvm/kvmi_msg.c +15 -0
@@ -33,6 +33,7 @@ static const char *const msg_IDs[] = { [KVMI_EVENT_REPLY] = "KVMI_EVENT_REPLY", [KVMI_GET_CPUID] = "KVMI_GET_CPUID", [KVMI_GET_GUEST_INFO] = "KVMI_GET_GUEST_INFO", + [KVMI_GET_MAP_TOKEN] = "KVMI_GET_MAP_TOKEN", [KVMI_GET_MTRR_TYPE] = "KVMI_GET_MTRR_TYPE", [KVMI_GET_PAGE_ACCESS] = "KVMI_GET_PAGE_ACCESS", [KVMI_GET_PAGE_WRITE_BITMAP] = "KVMI_GET_PAGE_WRITE_BITMAP",
@@ -352,6 +353,19 @@ static int handle_write_physical(struct kvmi *ikvm,return kvmi_msg_vm_maybe_reply (ikvm, msg, ec,NULL ,0 ); } +static int handle_get_map_token (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg, +const void *_req) +{ +struct kvmi_get_map_token_reply rpl; +int ec; + +memset (&rpl,0 ,sizeof (rpl)); + ec =kvmi_cmd_alloc_token (ikvm->kvm, &rpl.token); + +return kvmi_msg_vm_maybe_reply (ikvm, msg, ec, &rpl,sizeof (rpl)); +} +static bool enable_spp (struct kvmi *ikvm) {if (!ikvm->spp.initialized) {
@@ -524,6 +538,7 @@ static int(*const msg_vm[])(struct kvmi *, const struct kvmi_msg_hdr *, [KVMI_CONTROL_SPP] = handle_control_spp, [KVMI_CONTROL_VM_EVENTS] = handle_control_vm_events, [KVMI_GET_GUEST_INFO] = handle_get_guest_info, + [KVMI_GET_MAP_TOKEN] = handle_get_map_token, [KVMI_GET_PAGE_ACCESS] = handle_get_page_access, [KVMI_GET_PAGE_WRITE_BITMAP] = handle_get_page_write_bitmap, [KVMI_GET_VERSION] = handle_get_version,
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 74/92 kvm: x86: do not unconditionally patch the hypercall instruction during emulation
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
It can happened for us to end up emulating the VMCALL instruction as a result of the handling of an EPT write fault. In this situation, the emulator will try to unconditionally patch the correct hypercall opcode bytes using emulator_write_emulated(). However, this last call uses the fault GPA (if available) or walks the guest page tables at RIP, otherwise. The trouble begins when using KVMI, when we forbid the use of the fault GPA and fallback to the guest pt walk: in Windows (8.1 and newer) the page that we try to write into is marked read-execute and as such emulator_write_emulated() fails and we inject a write #PF, leading to a guest crash.
The fix is rather simple: check the existing instruction bytes before doing the patching. This does not change the normal KVM behaviour, but does help when using KVMI as we no longer inject a write #PF.
--- arch/x86/kvm/x86.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +20 -3
@@ -7363,16 +7363,33 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL_GPL (kvm_emulate_hypercall); +#define KVM_HYPERCALL_INSN_LEN 3 +static int emulator_fix_hypercall (struct x86_emulate_ctxt *ctxt) { +int err;struct kvm_vcpu *vcpu =emul_to_vcpu (ctxt); -char instruction[3 ]; +char buf[KVM_HYPERCALL_INSN_LEN ]; +char instruction[KVM_HYPERCALL_INSN_LEN ];unsigned long rip =kvm_rip_read (vcpu); + err =emulator_read_emulated (ctxt, rip, buf,sizeof (buf), + &ctxt->exception); +if (err !=X86EMUL_CONTINUE ) +return err; + kvm_x86_ops->patch_hypercall (vcpu, instruction); +if (!memcmp (instruction, buf,sizeof (instruction))) +/* +* The hypercall instruction is the correct one. Retry +* its execution maybe we got here as a result of an +* event other than #UD which has been resolved in the +* mean time. +*/ +return X86EMUL_CONTINUE ; -return emulator_write_emulated (ctxt, rip, instruction,3 , - &ctxt->exception); +return emulator_write_emulated (ctxt, rip, instruction, +sizeof (instruction), &ctxt->exception); }static int dm_request_for_irq_injection (struct kvm_vcpu *vcpu)
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Paolo Bonzini
maintainerRe: [RFC PATCH v6 74/92] kvm: x86: do not unconditionally patch the hypercall instruction during emulation
Quoted 13 lines · Adalbert Lazăr
From: Mihai Donțu <[email protected]>
It can happened for us to end up emulating the VMCALL instruction as a result of the handling of an EPT write fault. In this situation, the emulator will try to unconditionally patch the correct hypercall opcode bytes using
emulator_write_emulated(). However, this last call uses the fault GPA (if available) or walks the guest page tables at RIP, otherwise. The trouble begins when using KVMI, when we forbid the use of the fault GPA and fallback to the guest pt walk: in Windows (8.1 and newer) the page that we try to write into is marked read-execute and as suchemulator_write_emulated()fails and we inject a write #PF, leading to a guest crash.
Quoted 3 lines
The fix is rather simple: check the existing instruction bytes before doing the patching. This does not change the normal KVM behaviour, but does help when using KVMI as we no longer inject a write #PF.
Fixing the hypercall is just an optimization. Can we just hush and return to the guest if emulator_write_emulated returns X86EMUL_PROPAGATE_FAULT?
Paolo
Quoted 39 lines
CC Joerg Roedel <[email protected]>Signed-off-by Mihai Donțu <[email protected]>Signed-off-by Adalbert Lazăr <[email protected]>--- arch/x86/kvm/x86.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-)arch/x86/kvm/x86.c +16
-1@@ -7363,16 +7363,33 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL_GPL (kvm_emulate_hypercall); +#define KVM_HYPERCALL_INSN_LEN 3 +static int emulator_fix_hypercall (struct x86_emulate_ctxt *ctxt) { +int err;struct kvm_vcpu *vcpu =emul_to_vcpu (ctxt); -char instruction[3 ]; +char buf[KVM_HYPERCALL_INSN_LEN ]; +char instruction[KVM_HYPERCALL_INSN_LEN ];unsigned long rip =kvm_rip_read (vcpu); + err =emulator_read_emulated (ctxt, rip, buf,sizeof (buf), + &ctxt->exception); +if (err !=X86EMUL_CONTINUE ) +return err; + kvm_x86_ops->patch_hypercall (vcpu, instruction); +if (!memcmp (instruction, buf,sizeof (instruction))) +/* +* The hypercall instruction is the correct one. Retry +* its execution maybe we got here as a result of an +* event other than #UD which has been resolved in the +* mean time.
arch/x86/kvm/x86.c int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) · line 7395
Quoted 10 lines
arch/x86/kvm/x86.c +4
-2+*/ +return X86EMUL_CONTINUE ; -return emulator_write_emulated (ctxt, rip, instruction,3 , - &ctxt->exception); +return emulator_write_emulated (ctxt, rip, instruction, +sizeof (instruction), &ctxt->exception); }static int dm_request_for_irq_injection (struct kvm_vcpu *vcpu)
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [RFC PATCH v6 74/92] kvm: x86: do not unconditionally patch the hypercall instruction during emulation
Quoted 18 lines · Paolo Bonzini
Quoted 13 lines · Adalbert Lazăr
From: Mihai Donțu <[email protected]>
It can happened for us to end up emulating the VMCALL instruction as a result of the handling of an EPT write fault. In this situation, the emulator will try to unconditionally patch the correct hypercall opcode bytes using
emulator_write_emulated(). However, this last call uses the fault GPA (if available) or walks the guest page tables at RIP, otherwise. The trouble begins when using KVMI, when we forbid the use of the fault GPA and fallback to the guest pt walk: in Windows (8.1 and newer) the page that we try to write into is marked read-execute and as suchemulator_write_emulated()fails and we inject a write #PF, leading to a guest crash.Quoted 3 lines
The fix is rather simple: check the existing instruction bytes before doing the patching. This does not change the normal KVM behaviour, but does help when using KVMI as we no longer inject a write #PF.
Quoted 5 lines
Fixing the hypercall is just an optimization. Can we just hush and return to the guest if
emulator_write_emulatedreturnsX86EMUL_PROPAGATE_FAULT?Paolo
Something like this?
err = emulator_write_emulated(...); if (err == X86EMUL_PROPAGATE_FAULT) err = X86EMUL_CONTINUE; return err;
Quoted 39 lines
Quoted 39 lines
arch/x86/kvm/x86.c +20
-3@@ -7363,16 +7363,33 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL_GPL (kvm_emulate_hypercall); +#define KVM_HYPERCALL_INSN_LEN 3 +static int emulator_fix_hypercall (struct x86_emulate_ctxt *ctxt) { +int err;struct kvm_vcpu *vcpu =emul_to_vcpu (ctxt); -char instruction[3 ]; +char buf[KVM_HYPERCALL_INSN_LEN ]; +char instruction[KVM_HYPERCALL_INSN_LEN ];unsigned long rip =kvm_rip_read (vcpu); + err =emulator_read_emulated (ctxt, rip, buf,sizeof (buf), + &ctxt->exception); +if (err !=X86EMUL_CONTINUE ) +return err; + kvm_x86_ops->patch_hypercall (vcpu, instruction); +if (!memcmp (instruction, buf,sizeof (instruction))) +/* +* The hypercall instruction is the correct one. Retry +* its execution maybe we got here as a result of an +* event other than #UD which has been resolved in the +* mean time. +*/ +return X86EMUL_CONTINUE ; -return emulator_write_emulated (ctxt, rip, instruction,3 , - &ctxt->exception); +return emulator_write_emulated (ctxt, rip, instruction, +sizeof (instruction), &ctxt->exception); }
Re: [RFC PATCH v6 74/92] kvm: x86: do not unconditionally patch the hypercall instruction during emulation
In reply to Paolo BonziniQuoted 18 lines · Paolo Bonzini
Quoted 13 lines · Adalbert Lazăr
From: Mihai Donțu <[email protected]>
It can happened for us to end up emulating the VMCALL instruction as a result of the handling of an EPT write fault. In this situation, the emulator will try to unconditionally patch the correct hypercall opcode bytes using
emulator_write_emulated(). However, this last call uses the fault GPA (if available) or walks the guest page tables at RIP, otherwise. The trouble begins when using KVMI, when we forbid the use of the fault GPA and fallback to the guest pt walk: in Windows (8.1 and newer) the page that we try to write into is marked read-execute and as suchemulator_write_emulated()fails and we inject a write #PF, leading to a guest crash.Quoted 3 lines
The fix is rather simple: check the existing instruction bytes before doing the patching. This does not change the normal KVM behaviour, but does help when using KVMI as we no longer inject a write #PF.
Quoted 5 lines
Fixing the hypercall is just an optimization. Can we just hush and return to the guest if
emulator_write_emulatedreturnsX86EMUL_PROPAGATE_FAULT?Paolo
Something like this?
err = emulator_write_emulated(...); if (err == X86EMUL_PROPAGATE_FAULT) err = X86EMUL_CONTINUE; return err;
Quoted 34 lines
Quoted 34 lines
arch/x86/kvm/x86.c +18
-1@@ -7363,16 +7363,33 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL_GPL (kvm_emulate_hypercall); +#define KVM_HYPERCALL_INSN_LEN 3 +static int emulator_fix_hypercall (struct x86_emulate_ctxt *ctxt) { +int err;struct kvm_vcpu *vcpu =emul_to_vcpu (ctxt); -char instruction[3 ]; +char buf[KVM_HYPERCALL_INSN_LEN ]; +char instruction[KVM_HYPERCALL_INSN_LEN ];unsigned long rip =kvm_rip_read (vcpu); + err =emulator_read_emulated (ctxt, rip, buf,sizeof (buf), + &ctxt->exception); +if (err !=X86EMUL_CONTINUE ) +return err; + kvm_x86_ops->patch_hypercall (vcpu, instruction); +if (!memcmp (instruction, buf,sizeof (instruction))) +/* +* The hypercall instruction is the correct one. Retry +* its execution maybe we got here as a result of an +* event other than #UD which has been resolved in the +* mean time. +*/ +return X86EMUL_CONTINUE ;
Quoted 5 lines
arch/x86/kvm/x86.c int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) · line 7393
Quoted 5 lines
arch/x86/kvm/x86.c +2
-2-return emulator_write_emulated (ctxt, rip, instruction,3 , - &ctxt->exception); +return emulator_write_emulated (ctxt, rip, instruction, +sizeof (instruction), &ctxt->exception); }
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
A message that is not in the archive (sent off-list or not yet received): [email protected]
Paolo Bonzini
maintainerRe: [RFC PATCH v6 74/92] kvm: x86: do not unconditionally patch the hypercall instruction during emulation
Quoted 23 lines · Adalbert Lazăr
Quoted 22 lines · Paolo Bonzini
Quoted 13 lines · Adalbert Lazăr
From: Mihai Donțu <[email protected]>
It can happened for us to end up emulating the VMCALL instruction as a result of the handling of an EPT write fault. In this situation, the emulator will try to unconditionally patch the correct hypercall opcode bytes using
emulator_write_emulated(). However, this last call uses the fault GPA (if available) or walks the guest page tables at RIP, otherwise. The trouble begins when using KVMI, when we forbid the use of the fault GPA and fallback to the guest pt walk: in Windows (8.1 and newer) the page that we try to write into is marked read-execute and as suchemulator_write_emulated()fails and we inject a write #PF, leading to a guest crash.Quoted 3 lines
The fix is rather simple: check the existing instruction bytes before doing the patching. This does not change the normal KVM behaviour, but does help when using KVMI as we no longer inject a write #PF.
Fixing the hypercall is just an optimization. Can we just hush and return to the guest if
emulator_write_emulatedreturnsX86EMUL_PROPAGATE_FAULT?
Quoted 8 lines
Quoted 1 line
Paolo
Something like this?
err = emulator_write_emulated(...); if (err == X86EMUL_PROPAGATE_FAULT) err = X86EMUL_CONTINUE; return err;
Yes. The only difference will be that you'll keep getting #UD vmexits instead of hypercall vmexits. It's also safer, we want to obey those r-x permissions because PatchGuard would crash the system if it noticed the rewriting for whatever reason.
Paolo
Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
arch/x86/kvm/x86.c +18
-1@@ -7363,16 +7363,33 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL_GPL (kvm_emulate_hypercall); +#define KVM_HYPERCALL_INSN_LEN 3 +static int emulator_fix_hypercall (struct x86_emulate_ctxt *ctxt) { +int err;struct kvm_vcpu *vcpu =emul_to_vcpu (ctxt); -char instruction[3 ]; +char buf[KVM_HYPERCALL_INSN_LEN ]; +char instruction[KVM_HYPERCALL_INSN_LEN ];unsigned long rip =kvm_rip_read (vcpu); + err =emulator_read_emulated (ctxt, rip, buf,sizeof (buf), + &ctxt->exception); +if (err !=X86EMUL_CONTINUE ) +return err; + kvm_x86_ops->patch_hypercall (vcpu, instruction); +if (!memcmp (instruction, buf,sizeof (instruction))) +/* +* The hypercall instruction is the correct one. Retry +* its execution maybe we got here as a result of an +* event other than #UD which has been resolved in the +* mean time. +*/ +return X86EMUL_CONTINUE ;
Quoted 5 lines
Quoted 5 lines
arch/x86/kvm/x86.c int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) · line 7393
Quoted 5 lines
arch/x86/kvm/x86.c +2
-2-return emulator_write_emulated (ctxt, rip, instruction,3 , - &ctxt->exception); +return emulator_write_emulated (ctxt, rip, instruction, +sizeof (instruction), &ctxt->exception); }
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 75/92 kvm: x86: disable gpa_available optimization in emulator_read_write_onepage()
In reply to Adalbert LazărIf the EPT violation was caused by an execute restriction imposed by the introspection tool, gpa_available will point to the instruction pointer, not the to the read/write location that has to be used to emulate the current instruction.
This optimization should be disabled only when the VM is introspected, not just because the introspection subsystem is present.
--- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/x86.c +1 -1
@@ -5532,7 +5532,7 @@ static int emulator_read_write_onepage(unsigned long addr, void *val,* operation using rep will only have the initial GPA from the NPF * occurred. */ -if (vcpu->arch.gpa_available && +if (vcpu->arch.gpa_available && !kvmi_is_present () &&emulator_can_use_gpa (ctxt) && (addr & ~PAGE_MASK ) == (vcpu->arch.gpa_val & ~PAGE_MASK )) { gpa = vcpu->arch.gpa_val;
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Paolo Bonzini
maintainerRe: [RFC PATCH v6 75/92] kvm: x86: disable gpa_available optimization in emulator_read_write_onepage()
Quoted 9 lines · Adalbert Lazăr
If the EPT violation was caused by an execute restriction imposed by the introspection tool,
gpa_availablewill point to the instruction pointer, not the to the read/write location that has to be used to emulate the current instruction.This optimization should be disabled only when the VM is introspected, not just because the introspection subsystem is present.
Signed-off-by Adalbert Lazăr <[email protected]>
The right thing to do is to not set gpa_available for fetch failures in kvm_mmu_page_fault instead:
arch/x86/kvm/mmu.c +6 -2
@@ -5364,8 +5364,12 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u64 error_code,enum emulation_result er;bool direct = vcpu->arch.mmu->direct_map; -/* With shadow page tables, fault_address contains a GVA or nGPA. */ -if (vcpu->arch.mmu->direct_map) { +/* +* With shadow page tables, fault_address contains a GVA or nGPA. +* On a fetch fault, fault_address contains the instruction pointer. +*/ +if (vcpu->arch.mmu->direct_map && + likely(!(error_code & PFERR_FETCH_MASK)) { vcpu->arch.gpa_available = true; vcpu->arch.gpa_val = cr2; }
Paolo
Quoted 10 lines
--- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c static int emulator_read_write_onepage(unsigned long addr, void *val, · line 5538
Quoted 8 lines
arch/x86/kvm/x86.c +1
-1@@ -5532,7 +5532,7 @@ static int emulator_read_write_onepage(unsigned long addr, void *val,* operation using rep will only have the initial GPA from the NPF * occurred. */ -if (vcpu->arch.gpa_available && +if (vcpu->arch.gpa_available && !kvmi_is_present () &&emulator_can_use_gpa (ctxt) && (addr & ~PAGE_MASK ) == (vcpu->arch.gpa_val & ~PAGE_MASK )) { gpa = vcpu->arch.gpa_val;
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [RFC PATCH v6 75/92] kvm: x86: disable gpa_available optimization in emulator_read_write_onepage()
Quoted 41 lines · Paolo Bonzini
Quoted 9 lines · Adalbert Lazăr
If the EPT violation was caused by an execute restriction imposed by the introspection tool,
gpa_availablewill point to the instruction pointer, not the to the read/write location that has to be used to emulate the current instruction.This optimization should be disabled only when the VM is introspected, not just because the introspection subsystem is present.
Signed-off-by Adalbert Lazăr <[email protected]>The right thing to do is to not set
gpa_availablefor fetch failures inkvm_mmu_page_faultinstead:arch/x86/kvm/mmu.c +6
-2@@ -5364,8 +5364,12 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u64 error_code,enum emulation_result er;bool direct = vcpu->arch.mmu->direct_map; -/* With shadow page tables, fault_address contains a GVA or nGPA. */ -if (vcpu->arch.mmu->direct_map) { +/* +* With shadow page tables, fault_address contains a GVA or nGPA. +* On a fetch fault, fault_address contains the instruction pointer. +*/ +if (vcpu->arch.mmu->direct_map && + likely(!(error_code & PFERR_FETCH_MASK)) { vcpu->arch.gpa_available = true; vcpu->arch.gpa_val = cr2; }Paolo
Quoted 4 lines
--- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines
Quoted 6 lines
arch/x86/kvm/x86.c +0
-0arch/x86/kvm/x86.c static int emulator_read_write_onepage(unsigned long addr, void *val, · line 5538
Quoted 8 lines
arch/x86/kvm/x86.c +1
-1@@ -5532,7 +5532,7 @@ static int emulator_read_write_onepage(unsigned long addr, void *val,* operation using rep will only have the initial GPA from the NPF * occurred. */ -if (vcpu->arch.gpa_available && +if (vcpu->arch.gpa_available && !kvmi_is_present () &&emulator_can_use_gpa (ctxt) && (addr & ~PAGE_MASK ) == (vcpu->arch.gpa_val & ~PAGE_MASK )) { gpa = vcpu->arch.gpa_val;
Sure, but I think we'll have to extend the check.
Searching the logs I've found:
kvm/x86: re-translate broken translation that caused EPT violation
arch/x86/kvm/x86.c | 1 + 1 file changed, 1 insertion(+)
/home/b/kvmi@9cad844~1/arch/x86/kvm/x86.c:4757,4762 - /home/b/kvmi@9cad844/arch/x86/kvm/x86.c:4757,4763
*/
if (vcpu->arch.gpa_available &&
emulator_can_use_gpa(ctxt) &&
+ (vcpu->arch.error_code & PFERR_GUEST_FINAL_MASK) &&
(addr & ~PAGE_MASK) == (vcpu->arch.gpa_val & ~PAGE_MASK)) {
gpa = vcpu->arch.gpa_val;
ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
Re: [RFC PATCH v6 75/92] kvm: x86: disable gpa_available optimization in emulator_read_write_onepage()
In reply to Paolo BonziniQuoted 41 lines · Paolo Bonzini
Quoted 9 lines · Adalbert Lazăr
If the EPT violation was caused by an execute restriction imposed by the introspection tool,
gpa_availablewill point to the instruction pointer, not the to the read/write location that has to be used to emulate the current instruction.This optimization should be disabled only when the VM is introspected, not just because the introspection subsystem is present.
Signed-off-by Adalbert Lazăr <[email protected]>The right thing to do is to not set
gpa_availablefor fetch failures inkvm_mmu_page_faultinstead:arch/x86/kvm/mmu.c +6
-2@@ -5364,8 +5364,12 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u64 error_code,enum emulation_result er;bool direct = vcpu->arch.mmu->direct_map; -/* With shadow page tables, fault_address contains a GVA or nGPA. */ -if (vcpu->arch.mmu->direct_map) { +/* +* With shadow page tables, fault_address contains a GVA or nGPA. +* On a fetch fault, fault_address contains the instruction pointer. +*/ +if (vcpu->arch.mmu->direct_map && + likely(!(error_code & PFERR_FETCH_MASK)) { vcpu->arch.gpa_available = true; vcpu->arch.gpa_val = cr2; }Paolo
Quoted 4 lines
--- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines
Quoted 6 lines
arch/x86/kvm/x86.c +0
-0arch/x86/kvm/x86.c static int emulator_read_write_onepage(unsigned long addr, void *val, · line 5538
Quoted 8 lines
arch/x86/kvm/x86.c +1
-1@@ -5532,7 +5532,7 @@ static int emulator_read_write_onepage(unsigned long addr, void *val,* operation using rep will only have the initial GPA from the NPF * occurred. */ -if (vcpu->arch.gpa_available && +if (vcpu->arch.gpa_available && !kvmi_is_present () &&emulator_can_use_gpa (ctxt) && (addr & ~PAGE_MASK ) == (vcpu->arch.gpa_val & ~PAGE_MASK )) { gpa = vcpu->arch.gpa_val;
Sure, but I think we'll have to extend the check.
Searching the logs I've found:
kvm/x86: re-translate broken translation that caused EPT violation
arch/x86/kvm/x86.c | 1 + 1 file changed, 1 insertion(+)
/home/b/kvmi@9cad844~1/arch/x86/kvm/x86.c:4757,4762 - /home/b/kvmi@9cad844/arch/x86/kvm/x86.c:4757,4763
*/
if (vcpu->arch.gpa_available &&
emulator_can_use_gpa(ctxt) &&
+ (vcpu->arch.error_code & PFERR_GUEST_FINAL_MASK) &&
(addr & ~PAGE_MASK) == (vcpu->arch.gpa_val & ~PAGE_MASK)) {
gpa = vcpu->arch.gpa_val;
ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
A message that is not in the archive (sent off-list or not yet received): [email protected]
Paolo Bonzini
maintainerRe: [RFC PATCH v6 75/92] kvm: x86: disable gpa_available optimization in emulator_read_write_onepage()
Quoted 51 lines · Adalbert Lazăr
Quoted 23 lines · Paolo Bonzini
Quoted 9 lines · Adalbert Lazăr
If the EPT violation was caused by an execute restriction imposed by the introspection tool,
gpa_availablewill point to the instruction pointer, not the to the read/write location that has to be used to emulate the current instruction.This optimization should be disabled only when the VM is introspected, not just because the introspection subsystem is present.
Signed-off-by Adalbert Lazăr <[email protected]>The right thing to do is to not set
gpa_availablefor fetch failures inkvm_mmu_page_faultinstead:arch/x86/kvm/mmu.c +0
-1@@ -5364,8 +5364,12 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u64 error_code,enum emulation_result er;bool direct = vcpu->arch.mmu->direct_map; -/* With shadow page tables, fault_address contains a GVA or nGPA. */ arch/x86/kvm/mmu.c int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u64 error_code, · line 5375
Quoted 10 lines
arch/x86/kvm/mmu.c +6
-1-if (vcpu->arch.mmu->direct_map) { +/* +* With shadow page tables, fault_address contains a GVA or nGPA. +* On a fetch fault, fault_address contains the instruction pointer. +*/ +if (vcpu->arch.mmu->direct_map && + likely(!(error_code & PFERR_FETCH_MASK)) { vcpu->arch.gpa_available = true; vcpu->arch.gpa_val = cr2; }Sure, but I think we'll have to extend the check.
Searching the logs I've found:
kvm/x86: re-translate broken translation that caused EPT violation
Signed-off-by Mircea Cirjaliu <[email protected]>arch/x86/kvm/x86.c | 1 + 1 file changed, 1 insertion(+)/home/b/kvmi@9cad844~1/arch/x86/kvm/x86.c:4757,4762 - /home/b/kvmi@9cad844/arch/x86/kvm/x86.c:4757,4763
*/
if (vcpu->arch.gpa_available&&
emulator_can_use_gpa(ctxt)&&
Review
Quoted 4 lines
(quoted diff) +1
-0+ (vcpu->arch.error_code & PFERR_GUEST_FINAL_MASK) && (addr & ~PAGE_MASK) == (vcpu->arch.gpa_val & ~PAGE_MASK)) { gpa = vcpu->arch.gpa_val; ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
Yes, adding that check makes sense as well (still in kvm_mmu_page_fault).
Paolo
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 76/92 kvm: x86: disable EPT A/D bits if introspection is present
In reply to Adalbert Lazăr--- arch/x86/kvm/vmx/vmx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -7718,7 +7718,7 @@ static __init int hardware_setup(void) !cpu_has_vmx_invept_global()) enable_ept =0 ; -if (!cpu_has_vmx_ept_ad_bits () || !enable_ept) +if (!cpu_has_vmx_ept_ad_bits () || !enable_ept ||kvmi_is_present ()) enable_ept_ad_bits =0 ;if (!cpu_has_vmx_unrestricted_guest () || !enable_ept)
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Paolo Bonzini
maintainerRe: [RFC PATCH v6 76/92] kvm: x86: disable EPT A/D bits if introspection is present
Quoted 11 lines · Adalbert Lazăr
Signed-off-by Adalbert Lazăr <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/vmx/vmx.c +0
-0
arch/x86/kvm/vmx/vmx.c static __init int hardware_setup(void) · line 7724
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +1
-1@@ -7718,7 +7718,7 @@ static __init int hardware_setup(void) !cpu_has_vmx_invept_global()) enable_ept =0 ; -if (!cpu_has_vmx_ept_ad_bits () || !enable_ept) +if (!cpu_has_vmx_ept_ad_bits () || !enable_ept ||kvmi_is_present ()) enable_ept_ad_bits =0 ;if (!cpu_has_vmx_unrestricted_guest () || !enable_ept)
Why?
Paolo
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
A message that is not in the archive (sent off-list or not yet received): [email protected]
Paolo Bonzini
maintainerRe: [RFC PATCH v6 76/92] kvm: x86: disable EPT A/D bits if introspection is present
Quoted 9 lines · Mihai Donțu
Quoted 1 line
Why?
When EPT A/D is enabled, all guest page table walks are treated as writes (like AMD's NPT). Thus, an introspection tool hooking the guest page tables would trigger a flood of VMEXITs (EPT write violations) that will get the introspected VM into an unusable state.
Our implementation of such an introspection tool builds a cache of {cr3, gva} -> gpa, which is why it needs to monitor all guest PTs by hooking them for write.
Please include the kvm list too.
One issue here is that it changes the nested VMX ABI. Can you leave EPT A/D in place for the shadow EPT MMU, but not for "regular" EPT pages?
Also, what is the state of introspection support on AMD?
Paolo
Re: [RFC PATCH v6 76/92] kvm: x86: disable EPT A/D bits if introspection is present
Quoted 7 lines · Paolo Bonzini
Quoted 6 lines · Mihai Donțu
Quoted 1 line
Why?
When EPT A/D is enabled, all guest page table walks are treated as writes (like AMD's NPT). Thus, an introspection tool hooking the guest page tables would trigger a flood of VMEXITs (EPT write violations) that will get the introspected VM into an unusable state.
Quoted 5 lines
Quoted 3 lines
Our implementation of such an introspection tool builds a cache of {cr3, gva} -> gpa, which is why it needs to monitor all guest PTs by hooking them for write.
Please include the kvm list too.
I apologize. I did not notice I trimmed the CC list.
Quoted 2 lines
One issue here is that it changes the nested VMX ABI. Can you leave EPT A/D in place for the shadow EPT MMU, but not for "regular" EPT pages?
I'm not sure I follow. Something like this?
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -2853,7 +2853,7 @@ u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa) eptp |= (get_ept_level (vcpu) ==5 ) ?VMX_EPTP_PWL_5 :VMX_EPTP_PWL_4 ;if (enable_ept_ad_bits && - (!is_guest_mode (vcpu) ||nested_ept_ad_enabled (vcpu))) + ((!is_guest_mode (vcpu) && !kvmi_is_present ()) ||nested_ept_ad_enabled (vcpu))) eptp |=VMX_EPTP_AD_ENABLE_BIT ; eptp |= (root_hpa &PAGE_MASK );
Quoted 1 line
Also, what is the state of introspection support on AMD?
The way we'd like to do introspection is not currently possible on AMD CPUs. The reasons being:
* the NPT has the behavior I talked above (guest PT walks translate to writes)
* it is not possible to mark a guest page as execute-only
* there is no equivalent to Intel's MTF, though it can _probably_ be emulated using a creative trick involving the debug registers
If, however, none of the above are of importance for other users, everything else should work. I have to admit, though, we have not done any tests.
Regards,
Signature · 1 line
Mihai Donțu
Paolo Bonzini
maintainerRe: [RFC PATCH v6 76/92] kvm: x86: disable EPT A/D bits if introspection is present
Quoted 26 lines · Mihai Donțu
Quoted 7 lines · Paolo Bonzini
Quoted 6 lines · Mihai Donțu
Quoted 1 line
Why?
When EPT A/D is enabled, all guest page table walks are treated as writes (like AMD's NPT). Thus, an introspection tool hooking the guest page tables would trigger a flood of VMEXITs (EPT write violations) that will get the introspected VM into an unusable state.
Quoted 5 lines
Quoted 3 lines
Our implementation of such an introspection tool builds a cache of {cr3, gva} -> gpa, which is why it needs to monitor all guest PTs by hooking them for write.
Please include the kvm list too.
I apologize. I did not notice I trimmed the CC list.
Quoted 2 lines
One issue here is that it changes the nested VMX ABI. Can you leave EPT A/D in place for the shadow EPT MMU, but not for "regular" EPT pages?
I'm not sure I follow. Something like this?
arch/x86/kvm/vmx/vmx.c +0
-0
arch/x86/kvm/vmx/vmx.c u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa) · line 2858
Quoted 7 lines
arch/x86/kvm/vmx/vmx.c +1
-1@@ -2853,7 +2853,7 @@ u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa) eptp |= (get_ept_level (vcpu) ==5 ) ?VMX_EPTP_PWL_5 :VMX_EPTP_PWL_4 ;if (enable_ept_ad_bits && - (!is_guest_mode (vcpu) ||nested_ept_ad_enabled (vcpu))) + ((!is_guest_mode (vcpu) && !kvmi_is_present ()) ||nested_ept_ad_enabled (vcpu))) eptp |=VMX_EPTP_AD_ENABLE_BIT ; eptp |= (root_hpa &PAGE_MASK );
Almost, because you also have to change the accessed/dirty bits for mmu.c. Probably by moving the call to kvm_mmu_set_mask_ptes from vmx_enable_tdp to vmx_set_cr3.
Paolo
Quoted 16 lines
Quoted 1 line
Also, what is the state of introspection support on AMD?
The way we'd like to do introspection is not currently possible on AMD CPUs. The reasons being:
* the NPT has the behavior I talked above (guest PT walks translate to writes)
* it is not possible to mark a guest page as execute-only
* there is no equivalent to Intel's MTF, though it can _probably_ be emulated using a creative trick involving the debug registersIf, however, none of the above are of importance for other users, everything else should work. I have to admit, though, we have not done any tests.
Regards,
RFC PATCH v6 77/92 kvm: introspection: add trace functions
In reply to Adalbert Lazăr--- arch/x86/kvm/kvmi.c | 63 ++++ include/trace/events/kvmi.h | 680 ++++++++++++++++++++++++++++++++++++ virt/kvm/kvmi.c | 20 ++ virt/kvm/kvmi_mem.c | 5 + virt/kvm/kvmi_msg.c | 16 + 5 files changed, 784 insertions(+) create mode 100644 include/trace/events/kvmi.h
arch/x86/kvm/kvmi.c +63 -0
@@ -9,6 +9,8 @@#include <asm/vmx.h> #include "../../../virt/kvm/kvmi_int.h" +#include <trace/events/kvmi.h> +static unsigned long *msr_mask (struct kvm_vcpu *vcpu,unsigned int *msr) {switch (*msr) {
@@ -102,6 +104,9 @@ static bool __kvmi_msr_event(struct kvm_vcpu *vcpu, struct msr_data *msr)if (old_msr.data == msr->data)return true; +trace_kvmi_event_msr_send (vcpu->vcpu_id, msr->index, old_msr.data, + msr->data); + action =kvmi_send_msr (vcpu, msr->index, old_msr.data, msr->data, &ret_value);switch (action) {
@@ -113,6 +118,8 @@ static bool __kvmi_msr_event(struct kvm_vcpu *vcpu, struct msr_data *msr)kvmi_handle_common_event_actions (vcpu, action,"MSR" ); } +trace_kvmi_event_msr_recv (vcpu->vcpu_id, action, ret_value); +return ret; }
@@ -387,6 +394,8 @@ static bool __kvmi_cr_event(struct kvm_vcpu *vcpu, unsigned int cr,if (!test_bit (cr,IVCPU (vcpu)->cr_mask))return true; +trace_kvmi_event_cr_send (vcpu->vcpu_id, cr, old_value, *new_value); + action =kvmi_send_cr (vcpu, cr, old_value, *new_value, &ret_value);switch (action) {case KVMI_EVENT_ACTION_CONTINUE :
@@ -397,6 +406,8 @@ static bool __kvmi_cr_event(struct kvm_vcpu *vcpu, unsigned int cr,kvmi_handle_common_event_actions (vcpu, action,"CR" ); } +trace_kvmi_event_cr_recv (vcpu->vcpu_id, action, ret_value); +return ret; }
@@ -437,6 +448,8 @@ static void __kvmi_xsetbv_event(struct kvm_vcpu *vcpu) {u32 action; +trace_kvmi_event_xsetbv_send (vcpu->vcpu_id); + action =kvmi_send_xsetbv (vcpu);switch (action) {case KVMI_EVENT_ACTION_CONTINUE :
@@ -444,6 +457,8 @@ static void __kvmi_xsetbv_event(struct kvm_vcpu *vcpu)default :kvmi_handle_common_event_actions (vcpu, action,"XSETBV" ); } + +trace_kvmi_event_xsetbv_recv (vcpu->vcpu_id, action); }void kvmi_xsetbv_event (struct kvm_vcpu *vcpu)
@@ -460,12 +475,26 @@ void kvmi_xsetbv_event(struct kvm_vcpu *vcpu)kvmi_put (vcpu->kvm); } +static u64 get_next_rip (struct kvm_vcpu *vcpu) +{ +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); + +if (ivcpu->have_delayed_regs) +return ivcpu->delayed_regs.rip; +else +return kvm_rip_read (vcpu); +} +void kvmi_arch_breakpoint_event (struct kvm_vcpu *vcpu,u64 gva,u8 insn_len) {u32 action;u64 gpa; +u64 old_rip; gpa =kvm_mmu_gva_to_gpa_system (vcpu, gva,0 ,NULL ); + old_rip =kvm_rip_read (vcpu); + +trace_kvmi_event_bp_send (vcpu->vcpu_id, gpa, old_rip); action =kvmi_msg_send_bp (vcpu, gpa, insn_len);switch (action) {
@@ -478,6 +507,8 @@ void kvmi_arch_breakpoint_event(struct kvm_vcpu *vcpu, u64 gva, u8 insn_len)default :kvmi_handle_common_event_actions (vcpu, action,"BP" ); } + +trace_kvmi_event_bp_recv (vcpu->vcpu_id, action,get_next_rip (vcpu)); }#define KVM_HC_XEN_HVM_OP_GUEST_REQUEST_VM_EVENT 24
@@ -504,6 +535,8 @@ void kvmi_arch_hypercall_event(struct kvm_vcpu *vcpu) {u32 action; +trace_kvmi_event_hc_send (vcpu->vcpu_id); + action =kvmi_msg_send_hypercall (vcpu);switch (action) {case KVMI_EVENT_ACTION_CONTINUE :
@@ -511,6 +544,8 @@ void kvmi_arch_hypercall_event(struct kvm_vcpu *vcpu)default :kvmi_handle_common_event_actions (vcpu, action,"HYPERCALL" ); } + +trace_kvmi_event_hc_recv (vcpu->vcpu_id, action); } bool kvmi_arch_pf_event(struct kvm_vcpu *vcpu, gpa_t gpa, gva_t gva,
@@ -532,6 +567,9 @@ bool kvmi_arch_pf_event(struct kvm_vcpu *vcpu, gpa_t gpa, gva_t gva,if (ivcpu->effective_rep_complete)return true; +trace_kvmi_event_pf_send (vcpu->vcpu_id, gpa, gva, access, +kvm_rip_read (vcpu)); + action =kvmi_msg_send_pf (vcpu, gpa, gva, access, &ivcpu->ss_requested, &ivcpu->rep_complete, &ctx_addr, ivcpu->ctx_data, &ctx_size);
@@ -553,6 +591,9 @@ bool kvmi_arch_pf_event(struct kvm_vcpu *vcpu, gpa_t gpa, gva_t gva,kvmi_handle_common_event_actions (vcpu, action,"PF" ); } +trace_kvmi_event_pf_recv (vcpu->vcpu_id, action,get_next_rip (vcpu), + ctx_size, ivcpu->ss_requested, ret); +return ret; }
@@ -628,6 +669,11 @@ void kvmi_arch_trap_event(struct kvm_vcpu *vcpu) err =0 ; } +trace_kvmi_event_trap_send (vcpu->vcpu_id, vector, +IVCPU (vcpu)->exception.nr, + err,IVCPU (vcpu)->exception.error_code, + vcpu->arch.cr2); + action =kvmi_send_trap (vcpu, vector, type, err, vcpu->arch.cr2);switch (action) {case KVMI_EVENT_ACTION_CONTINUE :
@@ -635,6 +681,8 @@ void kvmi_arch_trap_event(struct kvm_vcpu *vcpu)default :kvmi_handle_common_event_actions (vcpu, action,"TRAP" ); } + +trace_kvmi_event_trap_recv (vcpu->vcpu_id, action); } static bool __kvmi_descriptor_event(struct kvm_vcpu *vcpu, u8 descriptor,
@@ -643,6 +691,8 @@ static bool __kvmi_descriptor_event(struct kvm_vcpu *vcpu, u8 descriptor,u32 action;bool ret = false; +trace_kvmi_event_desc_send (vcpu->vcpu_id, descriptor, write); + action =kvmi_msg_send_descriptor (vcpu, descriptor, write);switch (action) {case KVMI_EVENT_ACTION_CONTINUE :
@@ -654,6 +704,8 @@ static bool __kvmi_descriptor_event(struct kvm_vcpu *vcpu, u8 descriptor,kvmi_handle_common_event_actions (vcpu, action,"DESC" ); } +trace_kvmi_event_desc_recv (vcpu->vcpu_id, action); +return ret; }
@@ -718,6 +770,15 @@ int kvmi_arch_cmd_inject_exception(struct kvm_vcpu *vcpu, u8 vector,bool error_code_valid, u32 error_code, u64 address) { + struct x86_exception e = { + .error_code_valid = error_code_valid, + .error_code = error_code, + .address = address, + .vector = vector, + }; + +trace_kvmi_cmd_inject_exception (vcpu, &e); +if (!(is_vector_valid (vector) &&is_gva_valid (vcpu, address)))return -KVM_EINVAL ;
@@ -876,6 +937,8 @@ void kvmi_arch_update_page_tracking(struct kvm *kvm,return ; } +trace_kvmi_set_gfn_access (m->gfn, m->access, m->write_bitmap, slot->id); +for (i =0 ; i <ARRAY_SIZE (track_modes); i++) {unsigned int allow_bit = track_modes[i].allow_bit;enum kvm_page_track_mode mode = track_modes[i].track_mode;
include/trace/events/kvmi.h new file +680 -0
680 lines not on this page · raw message
virt/kvm/kvmi.c +20 -0
@@ -12,6 +12,9 @@#include <linux/bitmap.h> #include <linux/remote_mapping.h> +#define CREATE_TRACE_POINTS +#include <trace/events/kvmi.h> +#define MAX_PAUSE_REQUESTS 1001static struct kmem_cache *msg_cache;
@@ -1284,6 +1287,8 @@ static void __kvmi_singlestep_event(struct kvm_vcpu *vcpu) {u32 action; +trace_kvmi_event_singlestep_send (vcpu->vcpu_id); + action =kvmi_send_singlestep (vcpu);switch (action) {case KVMI_EVENT_ACTION_CONTINUE :
@@ -1291,6 +1296,8 @@ static void __kvmi_singlestep_event(struct kvm_vcpu *vcpu)default :kvmi_handle_common_event_actions (vcpu, action,"SINGLESTEP" ); } + +trace_kvmi_event_singlestep_recv (vcpu->vcpu_id, action); }static void kvmi_singlestep_event (struct kvm_vcpu *vcpu)
@@ -1311,6 +1318,8 @@ static bool __kvmi_create_vcpu_event(struct kvm_vcpu *vcpu)u32 action;bool ret = false; +trace_kvmi_event_create_vcpu_send (vcpu->vcpu_id); + action =kvmi_msg_send_create_vcpu (vcpu);switch (action) {case KVMI_EVENT_ACTION_CONTINUE :
@@ -1320,6 +1329,8 @@ static bool __kvmi_create_vcpu_event(struct kvm_vcpu *vcpu)kvmi_handle_common_event_actions (vcpu, action,"CREATE" ); } +trace_kvmi_event_create_vcpu_recv (vcpu->vcpu_id, action); +return ret; }
@@ -1345,6 +1356,8 @@ static bool __kvmi_pause_vcpu_event(struct kvm_vcpu *vcpu)u32 action;bool ret = false; +trace_kvmi_event_pause_vcpu_send (vcpu->vcpu_id); + action =kvmi_msg_send_pause_vcpu (vcpu);switch (action) {case KVMI_EVENT_ACTION_CONTINUE :
@@ -1354,6 +1367,8 @@ static bool __kvmi_pause_vcpu_event(struct kvm_vcpu *vcpu)kvmi_handle_common_event_actions (vcpu, action,"PAUSE" ); } +trace_kvmi_event_pause_vcpu_recv (vcpu->vcpu_id, action); +return ret; }
@@ -1857,6 +1872,8 @@ void kvmi_stop_ss(struct kvm_vcpu *vcpu) ivcpu->ss_owner = false; +trace_kvmi_stop_singlestep (vcpu->vcpu_id); +kvmi_singlestep_event (vcpu); out:
@@ -1892,6 +1909,9 @@ static bool kvmi_run_ss(struct kvm_vcpu *vcpu, gpa_t gpa, u8 access)gfn_t gfn =gpa_to_gfn (gpa);int err; +trace_kvmi_run_singlestep (vcpu, gpa, access, ikvm->ss_level, +IVCPU (vcpu)->ctx_size); +kvmi_arch_start_single_step (vcpu); err =write_custom_data (vcpu);
virt/kvm/kvmi_mem.c +5 -0
@@ -23,6 +23,7 @@#include <linux/remote_mapping.h> #include <uapi/linux/kvmi.h> +#include <trace/events/kvmi.h> #include "kvmi_int.h"
@@ -221,6 +222,8 @@ int kvmi_host_mem_map(struct kvm_vcpu *vcpu, gva_t tkn_gva, } req_mm = target_kvm->mm; +trace_kvmi_mem_map (target_kvm, req_gpa, map_gpa); +/* translate source addresses */ req_gfn =gpa_to_gfn (req_gpa); req_hva =gfn_to_hva_safe (target_kvm, req_gfn);
@@ -274,6 +277,8 @@ int kvmi_host_mem_unmap(struct kvm_vcpu *vcpu, gpa_t map_gpa)kvm_debug ("kvmi: unmapping request for map_gpa %016llx\n" , map_gpa); +trace_kvmi_mem_unmap (map_gpa); +/* convert GPA -> HVA */ map_gfn =gpa_to_gfn (map_gpa); map_hva =gfn_to_hva_safe (vcpu->kvm, map_gfn);
virt/kvm/kvmi_msg.c +16 -0
@@ -8,6 +8,8 @@#include <linux/net.h> #include "kvmi_int.h" +#include <trace/events/kvmi.h> +typedef int (*vcpu_reply_fct )(struct kvm_vcpu *vcpu,const struct kvmi_msg_hdr *msg,int err,const void *rpl,size_t rpl_size);
@@ -165,6 +167,8 @@ static int kvmi_msg_vm_reply(struct kvmi *ikvm, const struct kvmi_msg_hdr *msg, int err, const void *rpl, size_t rpl_size) { + trace_kvmi_vm_reply(msg->id, msg->seq, err); +return kvmi_msg_reply (ikvm, msg, err, rpl, rpl_size); }
@@ -202,6 +206,8 @@ int kvmi_msg_vcpu_reply(struct kvm_vcpu *vcpu, const struct kvmi_msg_hdr *msg, int err, const void *rpl, size_t rpl_size) { + trace_kvmi_vcpu_reply(vcpu->vcpu_id, msg->id, msg->seq, err); +return kvmi_msg_reply (IKVM (vcpu->kvm), msg, err, rpl, rpl_size); }
@@ -559,6 +565,8 @@ static int handle_event_reply(struct kvm_vcpu *vcpu,struct kvmi_vcpu_reply *expected = &ivcpu->reply;size_t useful, received, common; +trace_kvmi_event_reply (reply->event, msg->seq); +if (unlikely (msg->seq != expected->seq)) goto out;
@@ -883,6 +891,8 @@ static struct kvmi_msg_hdr *kvmi_msg_recv(struct kvmi *ikvm, bool *unsupported)static int kvmi_msg_dispatch_vm_cmd (struct kvmi *ikvm,const struct kvmi_msg_hdr *msg) { +trace_kvmi_vm_command (msg->id, msg->seq); +return msg_vm[msg->id](ikvm, msg, msg +1 ); }
@@ -895,6 +905,8 @@ static int kvmi_msg_dispatch_vcpu_job(struct kvmi *ikvm,struct kvm_vcpu *vcpu =NULL ;int err; +trace_kvmi_vcpu_command (cmd->vcpu, hdr->id, hdr->seq); +if (invalid_vcpu_hdr (cmd))return -KVM_EINVAL ;
@@ -1051,6 +1063,8 @@ int kvmi_send_event(struct kvm_vcpu *vcpu, u32 ev_id, ivcpu->reply.size = rpl_size; ivcpu->reply.error = -EINTR ; +trace_kvmi_event (vcpu->vcpu_id, common.event, hdr.seq); + err =kvmi_sock_write (ikvm, vec, n, msg_size);if (err) goto out;
@@ -1091,6 +1105,8 @@ int kvmi_msg_send_unhook(struct kvmi *ikvm)kvmi_setup_event_common (&common,KVMI_EVENT_UNHOOK ,0 ); +trace_kvmi_event (0 , common.event, hdr.seq); +return kvmi_sock_write (ikvm, vec, n, msg_size); }
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 78/92 kvm: x86: add tracepoints for interrupt and exception injections
In reply to Adalbert LazărFrom: Nicușor Cîțu <[email protected]>
This patch introduces additional tracepoints that are meant to help in following the flow of interrupts and exceptions queued to a guest VM. At the same time the kvm_exit tracepoint is enhanced with the vCPU ID.
One scenario in which these help is debugging lost interrupts due to a buggy VMEXIT handler.
--- arch/x86/kvm/svm.c | 9 +++- arch/x86/kvm/trace.h | 118 ++++++++++++++++++++++++++++++++--------- arch/x86/kvm/vmx/vmx.c | 8 ++- arch/x86/kvm/x86.c | 12 +++-- 4 files changed, 116 insertions(+), 31 deletions(-)
arch/x86/kvm/svm.c +8 -1
@@ -799,6 +799,8 @@ static void svm_queue_exception(struct kvm_vcpu *vcpu)bool reinject = vcpu->arch.exception.injected;u32 error_code = vcpu->arch.exception.error_code; +trace_kvm_inj_exception (vcpu); +/* * If we are within a nested VM we'd better #VMEXIT and let the guest * handle the exception
@@ -5108,6 +5110,8 @@ static void svm_inject_nmi(struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); +trace_kvm_inj_nmi (vcpu); + svm->vmcb->control.event_inj =SVM_EVTINJ_VALID |SVM_EVTINJ_TYPE_NMI ; vcpu->arch.hflags |=HF_NMI_MASK ;set_intercept (svm,INTERCEPT_IRET );
@@ -5133,7 +5137,8 @@ static void svm_set_irq(struct kvm_vcpu *vcpu)BUG_ON (!(gif_set (svm))); -trace_kvm_inj_virq (vcpu->arch.interrupt.nr); +trace_kvm_inj_interrupt (vcpu); + ++vcpu->stat.irq_injections; svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
@@ -5637,6 +5642,8 @@ static void svm_cancel_injection(struct kvm_vcpu *vcpu)struct vcpu_svm *svm =to_svm (vcpu);struct vmcb_control_area *control = &svm->vmcb->control; +trace_kvm_cancel_inj (vcpu); + control->exit_int_info = control->event_inj; control->exit_int_info_err = control->event_inj_err; control->event_inj =0 ;
arch/x86/kvm/trace.h +94 -24
160 lines not on this page · raw message
arch/x86/kvm/vmx/vmx.c +7 -1
@@ -1494,6 +1494,8 @@ static void vmx_queue_exception(struct kvm_vcpu *vcpu)u32 error_code = vcpu->arch.exception.error_code;u32 intr_info = nr |INTR_INFO_VALID_MASK ; +trace_kvm_inj_exception (vcpu); +kvm_deliver_exception_payload (vcpu);if (has_error_code) {
@@ -4266,7 +4268,7 @@ static void vmx_inject_irq(struct kvm_vcpu *vcpu)uint32_t intr;int irq = vcpu->arch.interrupt.nr; -trace_kvm_inj_virq (irq); +trace_kvm_inj_interrupt (vcpu); ++vcpu->stat.irq_injections;if (vmx->rmode.vm86_active) {
@@ -4293,6 +4295,8 @@ static void vmx_inject_nmi(struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu); +trace_kvm_inj_nmi (vcpu); +if (!enable_vnmi) {/* * Tracking the NMI-blocked state in software is built upon
@@ -6452,6 +6456,8 @@ static void vmx_complete_interrupts(struct vcpu_vmx *vmx)static void vmx_cancel_injection (struct kvm_vcpu *vcpu) { +trace_kvm_cancel_inj (vcpu); + __vmx_complete_interrupts(vcpu, vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), VM_ENTRY_INSTRUCTION_LEN,
arch/x86/kvm/x86.c +7 -5
@@ -6178,6 +6178,9 @@ static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)static bool inject_emulated_exception (struct kvm_vcpu *vcpu) {struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; + +trace_kvm_inj_emul_exception (vcpu, &ctxt->exception); +if (ctxt->exception.vector ==PF_VECTOR )return kvm_propagate_fault (vcpu, &ctxt->exception);
@@ -7487,10 +7490,6 @@ static int inject_pending_event(struct kvm_vcpu *vcpu, bool req_int_win)/* try to inject new event if pending */ if (vcpu->arch.exception.pending) { -trace_kvm_inj_exception (vcpu->arch.exception.nr, - vcpu->arch.exception.has_error_code, - vcpu->arch.exception.error_code); -WARN_ON_ONCE (vcpu->arch.exception.injected); vcpu->arch.exception.pending = false; vcpu->arch.exception.injected = true;
@@ -10250,7 +10249,10 @@ EXPORT_SYMBOL(kvm_arch_vcpu_intercept_desc);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_exit);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_fast_mmio); -EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_inj_virq); +EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_inj_interrupt); +EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_inj_nmi); +EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_inj_exception); +EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_cancel_inj);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_page_fault);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_msr);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_cr);
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 79/92 kvm: x86: emulate movsd xmm, m64
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This is needed in order to be able to support guest code that uses movsd to write into pages that are marked for write tracking.
--- arch/x86/kvm/emulate.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-)
arch/x86/kvm/emulate.c +27 -5
@@ -1177,6 +1177,27 @@ static int em_fnstsw(struct x86_emulate_ctxt *ctxt)return X86EMUL_CONTINUE ; } +static u8 simd_prefix_to_bytes (const struct x86_emulate_ctxt *ctxt, +int simd_prefix) +{ +u8 bytes; + +switch (ctxt->b) { +case 0x11 : +/* movsd xmm, m64 */ +/* movups xmm, m128 */ +if (simd_prefix ==0xf2 ) { + bytes =8 ; +break ; + } +/* fallthrough */ +default : + bytes =16 ; +break ; + } +return bytes; +} +static void decode_register_operand (struct x86_emulate_ctxt *ctxt,struct operand *op) {
@@ -1187,7 +1208,7 @@ static void decode_register_operand(struct x86_emulate_ctxt *ctxt,if (ctxt->d & Sse) { op->type =OP_XMM ; - op->bytes =16 ; + op->bytes = ctxt->op_bytes; op->addr.xmm = reg;read_sse_reg (ctxt, &op->vec_val, reg);return ;
@@ -1238,7 +1259,7 @@ static int decode_modrm(struct x86_emulate_ctxt *ctxt, ctxt->d & ByteOp);if (ctxt->d & Sse) { op->type =OP_XMM ; - op->bytes =16 ; + op->bytes = ctxt->op_bytes; op->addr.xmm = ctxt->modrm_rm;read_sse_reg (ctxt, &op->vec_val, ctxt->modrm_rm);return rc;
@@ -4529,7 +4550,7 @@ static const struct gprefix pfx_0f_2b = { };static const struct gprefix pfx_0f_10_0f_11 = { -I (Unaligned, em_mov),I (Unaligned, em_mov),N ,N , +I (Unaligned, em_mov),I (Unaligned, em_mov),I (Unaligned, em_mov),N , };static const struct gprefix pfx_0f_28_0f_29 = {
@@ -5097,7 +5118,7 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) {int rc =X86EMUL_CONTINUE ;int mode = ctxt->mode; -int def_op_bytes, def_ad_bytes, goffset, simd_prefix; +int def_op_bytes, def_ad_bytes, goffset, simd_prefix =0 ;bool op_prefix = false;bool has_seg_override = false;struct opcode opcode;
@@ -5320,7 +5341,8 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) ctxt->op_bytes =4 ;if (ctxt->d & Sse) - ctxt->op_bytes =16 ; + ctxt->op_bytes =simd_prefix_to_bytes (ctxt, + simd_prefix);else if (ctxt->d & Mmx) ctxt->op_bytes =8 ; }
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Paolo Bonzini
maintainerRe: [RFC PATCH v6 79/92] kvm: x86: emulate movsd xmm, m64
Quoted 81 lines · Adalbert Lazăr
From: Mihai Donțu <[email protected]>
This is needed in order to be able to support guest code that uses movsd to write into pages that are marked for write tracking.
Signed-off-by Mihai Donțu <[email protected]>Signed-off-by Adalbert Lazăr <[email protected]>--- arch/x86/kvm/emulate.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-)arch/x86/kvm/emulate.c +25
-4@@ -1177,6 +1177,27 @@ static int em_fnstsw(struct x86_emulate_ctxt *ctxt)return X86EMUL_CONTINUE ; } +static u8 simd_prefix_to_bytes (const struct x86_emulate_ctxt *ctxt, +int simd_prefix) +{ +u8 bytes; + +switch (ctxt->b) { +case 0x11 : +/* movsd xmm, m64 */ +/* movups xmm, m128 */ +if (simd_prefix ==0xf2 ) { + bytes =8 ; +break ; + } +/* fallthrough */ +default : + bytes =16 ; +break ; + } +return bytes; +} +static void decode_register_operand (struct x86_emulate_ctxt *ctxt,struct operand *op) {@@ -1187,7 +1208,7 @@ static void decode_register_operand(struct x86_emulate_ctxt *ctxt,if (ctxt->d & Sse) { op->type =OP_XMM ; - op->bytes =16 ; + op->bytes = ctxt->op_bytes; op->addr.xmm = reg;read_sse_reg (ctxt, &op->vec_val, reg);return ;@@ -1238,7 +1259,7 @@ static int decode_modrm(struct x86_emulate_ctxt *ctxt, ctxt->d & ByteOp);if (ctxt->d & Sse) { op->type =OP_XMM ; - op->bytes =16 ; + op->bytes = ctxt->op_bytes; op->addr.xmm = ctxt->modrm_rm;read_sse_reg (ctxt, &op->vec_val, ctxt->modrm_rm);return rc;@@ -4529,7 +4550,7 @@ static const struct gprefix pfx_0f_2b = { };static const struct gprefix pfx_0f_10_0f_11 = { -I (Unaligned, em_mov),I (Unaligned, em_mov),N ,N , +I (Unaligned, em_mov),I (Unaligned, em_mov),I (Unaligned, em_mov),N , };static const struct gprefix pfx_0f_28_0f_29 = {@@ -5097,7 +5118,7 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) {int rc =X86EMUL_CONTINUE ;int mode = ctxt->mode; -int def_op_bytes, def_ad_bytes, goffset, simd_prefix; +int def_op_bytes, def_ad_bytes, goffset, simd_prefix =0 ;bool op_prefix = false;bool has_seg_override = false;struct opcode opcode;
arch/x86/kvm/emulate.c int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) · line 5348
Quoted 9 lines
arch/x86/kvm/emulate.c +2
-1@@ -5320,7 +5341,8 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) ctxt->op_bytes =4 ;if (ctxt->d & Sse) - ctxt->op_bytes =16 ; + ctxt->op_bytes =simd_prefix_to_bytes (ctxt, + simd_prefix);else if (ctxt->d & Mmx) ctxt->op_bytes =8 ; }
Please submit all these emulator patches as a separate series, complete with testcases for kvm-unit-tests.
Paolo
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 80/92 kvm: x86: emulate movss xmm, m32
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This is needed in order to be able to support guest code that uses movss to write into pages that are marked for write tracking.
--- arch/x86/kvm/emulate.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/kvm/emulate.c +6 -2
@@ -1184,9 +1184,13 @@ static u8 simd_prefix_to_bytes(const struct x86_emulate_ctxt *ctxt,switch (ctxt->b) {case 0x11 : +/* movss xmm, m32 */ /* movsd xmm, m64 */ /* movups xmm, m128 */ -if (simd_prefix ==0xf2 ) { +if (simd_prefix ==0xf3 ) { + bytes =4 ; +break ; + }else if (simd_prefix ==0xf2 ) { bytes =8 ;break ; }
@@ -4550,7 +4554,7 @@ static const struct gprefix pfx_0f_2b = { };static const struct gprefix pfx_0f_10_0f_11 = { -I (Unaligned, em_mov),I (Unaligned, em_mov),I (Unaligned, em_mov),N , +I (Unaligned, em_mov),I (Unaligned, em_mov),I (Unaligned, em_mov),I (Unaligned, em_mov), };static const struct gprefix pfx_0f_28_0f_29 = {
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 81/92 kvm: x86: emulate movq xmm, m64
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This is needed in order to be able to support guest code that uses movq to write into pages that are marked for write tracking.
--- arch/x86/kvm/emulate.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)
arch/x86/kvm/emulate.c +15 -9
@@ -1180,23 +1180,24 @@ static int em_fnstsw(struct x86_emulate_ctxt *ctxt)static u8 simd_prefix_to_bytes (const struct x86_emulate_ctxt *ctxt,int simd_prefix) { -u8 bytes; +u8 bytes =16 ;switch (ctxt->b) {case 0x11 :/* movss xmm, m32 */ /* movsd xmm, m64 */ /* movups xmm, m128 */ -if (simd_prefix ==0xf3 ) { +if (simd_prefix ==0xf3 ) bytes =4 ; -break ; - }else if (simd_prefix ==0xf2 ) { +else if (simd_prefix ==0xf2 ) bytes =8 ; -break ; - } -/* fallthrough */ +break ; +case 0xd6 : +/* movq xmm, m64 */ +if (simd_prefix ==0x66 ) + bytes =8 ; +break ;default : - bytes =16 ;break ; }return bytes;
@@ -4549,6 +4550,10 @@ static const struct instr_dual instr_dual_0f_2b = { I(0, em_mov), N }; +static const struct gprefix pfx_0f_d6 = { +N ,I (0 , em_mov),N ,N , +}; +static const struct gprefix pfx_0f_2b = {ID (0 , &instr_dual_0f_2b),ID (0 , &instr_dual_0f_2b),N ,N , };
@@ -4846,7 +4851,8 @@ static const struct opcode twobyte_table[256] = {/* 0xC8 - 0xCF */ X8 (I (DstReg, em_bswap)),/* 0xD0 - 0xDF */ -N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N , +N ,N ,N ,N ,N ,N ,GP (ModRM | SrcReg | DstMem | Mov | Sse, &pfx_0f_d6), +N ,N ,N ,N ,N ,N ,N ,N ,N ,/* 0xE0 - 0xEF */ N ,N ,N ,N ,N ,N ,N ,GP (SrcReg | DstMem | ModRM | Mov, &pfx_0f_e7), N, N, N, N, N, N, N, N,
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 82/92 kvm: x86: emulate movq r, xmm
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This adds support for movq r, xmm. It introduces a new flag (GPRModRM) to indicate decode_modrm() that the encoded register is a general purpose one.
--- arch/x86/kvm/emulate.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)
arch/x86/kvm/emulate.c +13 -2
@@ -172,6 +172,7 @@#define NoMod ((u64)1 << 47)/* Mod field is ignored */ #define Intercept ((u64)1 << 48)/* Has valid intercept field */ #define CheckPerm ((u64)1 << 49)/* Has valid check_perm field */ +#define GPRModRM ((u64)1 << 50)/* The ModRM encoded register is a GP one */ #define PrivUD ((u64)1 << 51)/* #UD instead of #GP on CPL > 0 */ #define NearBranch ((u64)1 << 52)/* Near branches */ #define No16 ((u64)1 << 53)/* No 16 bit operand */
@@ -1197,6 +1198,11 @@ static u8 simd_prefix_to_bytes(const struct x86_emulate_ctxt *ctxt,if (simd_prefix ==0x66 ) bytes =8 ;break ; +case 0x6e : +/* movq r/m64, xmm */ +if (simd_prefix ==0x66 ) + bytes =8 ; +break ;default :break ; }
@@ -1262,7 +1268,7 @@ static int decode_modrm(struct x86_emulate_ctxt *ctxt, op->bytes = (ctxt->d & ByteOp) ?1 : ctxt->op_bytes; op->addr.reg =decode_register (ctxt, ctxt->modrm_rm, ctxt->d & ByteOp); -if (ctxt->d & Sse) { +if ((ctxt->d & Sse) && !(ctxt->d & GPRModRM)) { op->type =OP_XMM ; op->bytes = ctxt->op_bytes; op->addr.xmm = ctxt->modrm_rm;
@@ -4546,6 +4552,10 @@ static const struct gprefix pfx_0f_6f_0f_7f = { I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov), }; +static const struct gprefix pfx_0f_6e_0f_7e = { +N ,I (Sse, em_mov),N ,N +}; +static const struct instr_dual instr_dual_0f_2b = {I (0 , em_mov),N };
@@ -4807,7 +4817,8 @@ static const struct opcode twobyte_table[256] = {N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N , -N ,N ,N ,GP (SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f), +N ,N ,GP (SrcMem | DstReg | ModRM | GPRModRM | Mov, &pfx_0f_6e_0f_7e), +GP (SrcMem | DstReg | ModRM | Mov, &pfx_0f_6f_0f_7f),/* 0x70 - 0x7F */ N ,N ,N ,N , N, N, N, N,
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 83/92 kvm: x86: emulate movd xmm, m32
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This is needed in order to be able to support guest code that uses movd to write into pages that are marked for write tracking.
--- arch/x86/kvm/emulate.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
arch/x86/kvm/emulate.c +11 -1
@@ -1203,6 +1203,11 @@ static u8 simd_prefix_to_bytes(const struct x86_emulate_ctxt *ctxt,if (simd_prefix ==0x66 ) bytes =8 ;break ; +case 0x7e : +/* movd xmm, m32 */ +if (simd_prefix ==0x66 ) + bytes =4 ; +break ;default :break ; }
@@ -4564,6 +4569,10 @@ static const struct gprefix pfx_0f_d6 = { N, I(0, em_mov), N, N, }; +static const struct gprefix pfx_0f_7e = { +N ,I (0 , em_mov),N ,N , +}; +static const struct gprefix pfx_0f_2b = {ID (0 , &instr_dual_0f_2b),ID (0 , &instr_dual_0f_2b),N ,N , };
@@ -4823,7 +4832,8 @@ static const struct opcode twobyte_table[256] = {N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N , -N ,N ,N ,GP (SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f), +N ,N ,GP (ModRM | SrcReg | DstMem | GPRModRM | Mov | Sse, &pfx_0f_7e), +GP (SrcReg | DstMem | ModRM | Mov, &pfx_0f_6f_0f_7f),/* 0x80 - 0x8F */ X16 (D (SrcImm | NearBranch)), /* 0x90 - 0x9F */
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 84/92 kvm: x86: enable the half part of movss, movsd, movups
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
A previous patch added emulation support for these instructions with a register source and memory destination. This patch adds the variants with a memory source and a register destination.
--- arch/x86/kvm/emulate.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/x86/kvm/emulate.c +4 -0
@@ -1184,6 +1184,10 @@ static u8 simd_prefix_to_bytes(const struct x86_emulate_ctxt *ctxt,u8 bytes =16 ;switch (ctxt->b) { +case 0x10 : +/* movss m32, xmm */ +/* movsd m64, xmm */ +/* movups m128, xmm */ case 0x11 :/* movss xmm, m32 */ /* movsd xmm, m64 */
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 85/92 kvm: x86: emulate lfence
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This adds support for all encoding variants of lfence (0x0f 0xae 0xe[8-f]).
I did not use rmb() in case it will be made to use a different instruction on future architectures.
--- arch/x86/kvm/emulate.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
arch/x86/kvm/emulate.c +7 -1
@@ -4168,6 +4168,12 @@ static int em_fxrstor(struct x86_emulate_ctxt *ctxt)return rc; } +static int em_lfence (struct x86_emulate_ctxt *ctxt) +{ + asmvolatile ("lfence" :::"memory" ); +return X86EMUL_CONTINUE ; +} +static bool valid_cr (int nr) {switch (nr) {
@@ -4554,7 +4560,7 @@ static const struct group_dual group15 = { {I (ModRM | Aligned16, em_fxrstor),N ,N ,N ,N ,N ,GP (0 , &pfx_0f_ae_7), }, { - N, N, N, N, N, N, N, N, + N, N, N, N, N, I(ModRM | Sse, em_lfence), N, N, } }; static const struct gprefix pfx_0f_6f_0f_7f = {
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 86/92 kvm: x86: emulate xorpd xmm2/m128, xmm1
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This adds support for xorpd xmm2/m128, xmm1.
--- arch/x86/kvm/emulate.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)
arch/x86/kvm/emulate.c +18 -1
@@ -1178,6 +1178,22 @@ static int em_fnstsw(struct x86_emulate_ctxt *ctxt)return X86EMUL_CONTINUE ; } +static int em_xorpd (struct x86_emulate_ctxt *ctxt) +{ +const sse128_t *src = &ctxt->src.vec_val; +sse128_t *dst = &ctxt->dst.vec_val; +sse128_t xmm0; + + asmvolatile ("movdqu %%xmm0, %0\n" +"movdqu %1, %%xmm0\n" +"xorpd %2, %%xmm0\n" +"movdqu %%xmm0, %1\n" +"movdqu %0, %%xmm0" + :"+m" (xmm0),"+m" (*dst) :"m" (*src)); + +return X86EMUL_CONTINUE ; +} +static u8 simd_prefix_to_bytes (const struct x86_emulate_ctxt *ctxt,int simd_prefix) {
@@ -4831,7 +4847,8 @@ static const struct opcode twobyte_table[256] = {/* 0x40 - 0x4F */ X16 (D (DstReg | SrcMem | ModRM)),/* 0x50 - 0x5F */ -N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N ,N , +N ,N ,N ,N ,N ,N ,N ,I (SrcMem | DstReg | ModRM | Unaligned | Sse, em_xorpd), +N ,N ,N ,N ,N ,N ,N ,N ,/* 0x60 - 0x6F */ N ,N ,N ,N , N, N, N, N,
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 87/92 kvm: x86: emulate xorps xmm/m128, xmm
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This extends the previous xorpd by creating a dedicated group, something I should have done since the very beginning.
--- arch/x86/kvm/emulate.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)
arch/x86/kvm/emulate.c +21 -1
@@ -1178,6 +1178,22 @@ static int em_fnstsw(struct x86_emulate_ctxt *ctxt)return X86EMUL_CONTINUE ; } +static int em_xorps (struct x86_emulate_ctxt *ctxt) +{ +const sse128_t *src = &ctxt->src.vec_val; +sse128_t *dst = &ctxt->dst.vec_val; +sse128_t xmm0; + + asmvolatile ("movdqu %%xmm0, %0\n" +"movdqu %1, %%xmm0\n" +"xorps %2, %%xmm0\n" +"movdqu %%xmm0, %1\n" +"movdqu %0, %%xmm0" + :"+m" (xmm0),"+m" (*dst) :"m" (*src)); + +return X86EMUL_CONTINUE ; +} +static int em_xorpd (struct x86_emulate_ctxt *ctxt) {const sse128_t *src = &ctxt->src.vec_val;
@@ -4615,6 +4631,10 @@ static const struct gprefix pfx_0f_e7 = { N, I(Sse, em_mov), N, N, }; +static const struct gprefix pfx_0f_57 = { +I (Unaligned, em_xorps),I (Unaligned, em_xorpd),N ,N +}; +static const struct escape escape_d9 = { {N ,N ,N ,N ,N ,N ,N ,I (DstMem16 | Mov, em_fnstcw), }, {
@@ -4847,7 +4867,7 @@ static const struct opcode twobyte_table[256] = {/* 0x40 - 0x4F */ X16 (D (DstReg | SrcMem | ModRM)),/* 0x50 - 0x5F */ -N ,N ,N ,N ,N ,N ,N ,I (SrcMem | DstReg | ModRM | Unaligned | Sse, em_xorpd), +N ,N ,N ,N ,N ,N ,N ,GP (SrcMem | DstReg | ModRM | Sse, &pfx_0f_57),N ,N ,N ,N ,N ,N ,N ,N ,/* 0x60 - 0x6F */ N, N, N, N,
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 88/92 kvm: x86: emulate fst/fstp m64fp
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This adds support for fst m64fp and fstp m64fp.
--- arch/x86/kvm/emulate.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)
arch/x86/kvm/emulate.c +22 -1
@@ -1178,6 +1178,26 @@ static int em_fnstsw(struct x86_emulate_ctxt *ctxt)return X86EMUL_CONTINUE ; } +static int em_fstp (struct x86_emulate_ctxt *ctxt) +{ +if (ctxt->ops->get_cr (ctxt,0 ) & (X86_CR0_TS |X86_CR0_EM )) +return emulate_nm (ctxt); + + asmvolatile ("fstpl %0" :"=m" (ctxt->dst.val)); + +return X86EMUL_CONTINUE ; +} + +static int em_fst (struct x86_emulate_ctxt *ctxt) +{ +if (ctxt->ops->get_cr (ctxt,0 ) & (X86_CR0_TS |X86_CR0_EM )) +return emulate_nm (ctxt); + + asmvolatile ("fstl %0" :"=m" (ctxt->dst.val)); + +return X86EMUL_CONTINUE ; +} +static int em_xorps (struct x86_emulate_ctxt *ctxt) {const sse128_t *src = &ctxt->src.vec_val;
@@ -4678,7 +4698,8 @@ static const struct escape escape_db = { { } };static const struct escape escape_dd = { { -N ,N ,N ,N ,N ,N ,N ,I (DstMem16 | Mov, em_fnstsw), +N ,N ,I (DstMem64 | Mov, em_fst),I (DstMem64 | Mov, em_fstp), +N ,N ,N ,I (DstMem16 | Mov, em_fnstsw), }, {/* 0xC0 - 0xC7 */ N ,N ,N ,N ,N ,N ,N ,N ,
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 89/92 kvm: x86: make lock cmpxchg r, r/m atomic
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
The current emulation takes place in two steps: the first does all the actions that an cmpxchg would do, sets ZF and saves all results in a temporary storage (the emulation context). It's the second step that does the actual atomic operation (actually uses cmpxchg). The problem with this approach is that steps one and two can observe different values in memory and when that happens RAX and RFLAGS will have invalid values when returning to the guest as emulator_cmpxchg_emulated() does not set these.
This patch modifies the prototype of emulator_cmpxchg_emulated() so that when cmpxchg fails, it returns in *old the current value. We also modify em_cmpxchg() so that if the LOCK prefix is present we invoke emulator_cmpxchg_emulated() directly and set RAX and RFLAGS. Note that we also disable writeback as it is no longer needed.
--- arch/x86/include/asm/kvm_emulate.h | 2 +- arch/x86/kvm/emulate.c | 57 +++++++++++++++++++++++++++--- arch/x86/kvm/x86.c | 48 ++++++++++++++++++------- 3 files changed, 89 insertions(+), 18 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -1
@@ -178,7 +178,7 @@ struct x86_emulate_ops {*/ int (*cmpxchg_emulated)(struct x86_emulate_ctxt *ctxt,unsigned long addr, -const void *old, +void *old,const void *new,unsigned int bytes,struct x86_exception *fault);
arch/x86/kvm/emulate.c +52 -5
85 lines not on this page · raw message
arch/x86/kvm/x86.c +36 -12
79 lines not on this page · raw message
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 90/92 kvm: x86: emulate lock cmpxchg8b atomically
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
As it was the case for lock cmpxchg, lock cmpxchg8b was emulated in two steps the first one setting/clearing the zero flag and the last one making the actual atomic operation.
This patch fixes that by combining the two, ie. the writeback step is no longer necessary as the first step made the changes directly in memory.
--- arch/x86/kvm/emulate.c | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-)
arch/x86/kvm/emulate.c +41 -1
@@ -2320,7 +2320,47 @@ static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)static int em_cmpxchg8b (struct x86_emulate_ctxt *ctxt) { -u64 old = ctxt->dst.orig_val64; +u64 old; + +if (ctxt->lock_prefix) { +int rc; +ulong linear; +u64 new = (reg_read (ctxt,VCPU_REGS_RBX ) & (u32)-1 ) | + ((reg_read (ctxt,VCPU_REGS_RCX ) & (u32)-1 ) <<32 ); + + old = (reg_read (ctxt,VCPU_REGS_RAX ) & (u32)-1 ) | + ((reg_read (ctxt,VCPU_REGS_RDX ) & (u32)-1 ) <<32 ); + +/* disable writeback altogether */ + ctxt->d &= ~SrcWrite; + ctxt->d |= NoWrite; + + rc =linearize (ctxt, ctxt->dst.addr.mem,8 , true, &linear); +if (rc !=X86EMUL_CONTINUE ) +return rc; + + rc = ctxt->ops->cmpxchg_emulated (ctxt, linear, &old, &new, + ctxt->dst.bytes, + &ctxt->exception); + +switch (rc) { +case X86EMUL_CONTINUE : + ctxt->eflags |=X86_EFLAGS_ZF ; +break ; +case X86EMUL_CMPXCHG_FAILED : + *reg_write (ctxt,VCPU_REGS_RAX ) = old & (u32)-1 ; + *reg_write (ctxt,VCPU_REGS_RDX ) = (old >>32 ) & (u32)-1 ; + + ctxt->eflags &= ~X86_EFLAGS_ZF ; + + rc =X86EMUL_CONTINUE ; +break ; + } + +return rc; + } + + old = ctxt->dst.orig_val64;if (ctxt->dst.bytes ==16 )return X86EMUL_UNHANDLEABLE ;
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 91/92 kvm: x86: emulate lock cmpxchg16b m128
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This patch adds support for lock cmpxchg16b m128 by extending the existent emulation for lock cmpxchg8b m64.
For implementing the atomic operation, we use an explicit assembler statement, as cmpxchg_double() does not provide the contents of the memory on failure. As before, writeback is completely disabled as the operation is executed directly on guest memory, unless the architecture does not advertise CMPXCHG16B in CPUID.
--- arch/x86/kvm/emulate.c | 117 ++++++++++++++++++++++++++++++----------- arch/x86/kvm/x86.c | 37 ++++++++++++- 2 files changed, 122 insertions(+), 32 deletions(-)
arch/x86/kvm/emulate.c +87 -30
@@ -2318,46 +2318,103 @@ static int em_call_near_abs(struct x86_emulate_ctxt *ctxt)return rc; } -static int em_cmpxchg8b (struct x86_emulate_ctxt *ctxt) +static int em_cmpxchg8b_locked (struct x86_emulate_ctxt *ctxt) { -u64 old; +int rc; +ulong linear; +u64 new = (reg_read (ctxt,VCPU_REGS_RBX ) & (u32)-1 ) | + ((reg_read (ctxt,VCPU_REGS_RCX ) & (u32)-1 ) <<32 ); +u64 old = (reg_read (ctxt,VCPU_REGS_RAX ) & (u32)-1 ) | + ((reg_read (ctxt,VCPU_REGS_RDX ) & (u32)-1 ) <<32 ); -if (ctxt->lock_prefix) { -int rc; -ulong linear; -u64 new = (reg_read (ctxt,VCPU_REGS_RBX ) & (u32)-1 ) | - ((reg_read (ctxt,VCPU_REGS_RCX ) & (u32)-1 ) <<32 ); +/* disable writeback altogether */ + ctxt->d |= NoWrite; - old = (reg_read (ctxt,VCPU_REGS_RAX ) & (u32)-1 ) | - ((reg_read (ctxt,VCPU_REGS_RDX ) & (u32)-1 ) <<32 ); + rc =linearize (ctxt, ctxt->dst.addr.mem,8 , true, &linear); +if (rc !=X86EMUL_CONTINUE ) +return rc; -/* disable writeback altogether */ - ctxt->d &= ~SrcWrite; - ctxt->d |= NoWrite; + rc = ctxt->ops->cmpxchg_emulated (ctxt, linear, &old, &new, +8 , &ctxt->exception); - rc =linearize (ctxt, ctxt->dst.addr.mem,8 , true, &linear); -if (rc !=X86EMUL_CONTINUE ) -return rc; - rc = ctxt->ops->cmpxchg_emulated (ctxt, linear, &old, &new, - ctxt->dst.bytes, - &ctxt->exception); +switch (rc) { +case X86EMUL_CONTINUE : + ctxt->eflags |=X86_EFLAGS_ZF ; +break ; +case X86EMUL_CMPXCHG_FAILED : + *reg_write (ctxt,VCPU_REGS_RAX ) = old & (u32)-1 ; + *reg_write (ctxt,VCPU_REGS_RDX ) = (old >>32 ) & (u32)-1 ; -switch (rc) { -case X86EMUL_CONTINUE : - ctxt->eflags |=X86_EFLAGS_ZF ; -break ; -case X86EMUL_CMPXCHG_FAILED : - *reg_write (ctxt,VCPU_REGS_RAX ) = old & (u32)-1 ; - *reg_write (ctxt,VCPU_REGS_RDX ) = (old >>32 ) & (u32)-1 ; + ctxt->eflags &= ~X86_EFLAGS_ZF ; - ctxt->eflags &= ~X86_EFLAGS_ZF ; + rc =X86EMUL_CONTINUE ; +break ; + } - rc =X86EMUL_CONTINUE ; -break ; - } +return rc; +} + +#ifdef CONFIG_X86_64 +static int em_cmpxchg16b_locked (struct x86_emulate_ctxt *ctxt) +{ +int rc; +ulong linear; +u64 new[2 ] = { +reg_read (ctxt,VCPU_REGS_RBX ), +reg_read (ctxt,VCPU_REGS_RCX ) + }; +u64 old[2 ] = { +reg_read (ctxt,VCPU_REGS_RAX ), +reg_read (ctxt,VCPU_REGS_RDX ) + }; +/* disable writeback altogether */ + ctxt->d |= NoWrite; + + rc =linearize (ctxt, ctxt->dst.addr.mem,16 , true, &linear); +if (rc !=X86EMUL_CONTINUE )return rc; + +if (linear %16 ) +return emulate_gp (ctxt,0 ); + + rc = ctxt->ops->cmpxchg_emulated (ctxt, linear, old, new, +16 , &ctxt->exception); + +switch (rc) { +case X86EMUL_CONTINUE : + ctxt->eflags |=X86_EFLAGS_ZF ; +break ; +case X86EMUL_CMPXCHG_FAILED : + *reg_write (ctxt,VCPU_REGS_RAX ) = old[0 ]; + *reg_write (ctxt,VCPU_REGS_RDX ) = old[1 ]; + + ctxt->eflags &= ~X86_EFLAGS_ZF ; + + rc =X86EMUL_CONTINUE ; +break ; + } + +return rc; +} +#else +static int em_cmpxchg16b_locked (struct x86_emulate_ctxt *ctxt) +{ +return X86EMUL_UNHANDLEABLE ; +} +#endif + +static int em_cmpxchg8_16b (struct x86_emulate_ctxt *ctxt) +{ +u64 old; + +if (ctxt->lock_prefix) { +if (ctxt->dst.bytes ==8 ) +return em_cmpxchg8b_locked (ctxt); +else if (ctxt->dst.bytes ==16 ) +return em_cmpxchg16b_locked (ctxt); +return X86EMUL_UNHANDLEABLE ; } old = ctxt->dst.orig_val64;
@@ -4679,7 +4736,7 @@ static const struct gprefix pfx_0f_c7_7 = {static const struct group_dual group9 = { { -N ,I (DstMem64 | Lock | PageTable, em_cmpxchg8b),N ,N ,N ,N ,N ,N , +N ,I (DstMem64 | Lock | PageTable, em_cmpxchg8_16b),N ,N ,N ,N ,N ,N , }, {N ,N ,N ,N ,N ,N ,N ,GP (0 , &pfx_0f_c7_7),
arch/x86/kvm/x86.c +35 -2
@@ -5665,8 +5665,17 @@ static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,char *kaddr;bool exchanged = false; -/* guests cmpxchg8b have to be emulated atomically */ -if (bytes >8 || (bytes & (bytes -1 ))) +#ifdef CONFIG_X86_64 +#define CMPXCHG_MAX_BYTES 16 +#else +#define CMPXCHG_MAX_BYTES 8 +#endif + +/* guests cmpxchg{8,16}b have to be emulated atomically */ +if (bytes >CMPXCHG_MAX_BYTES || (bytes & (bytes -1 ))) + goto emul_write; + +if (bytes ==16 && !system_has_cmpxchg_double ()) goto emul_write; gpa =kvm_mmu_gva_to_gpa_write (vcpu, addr,NULL );
@@ -5724,6 +5733,30 @@ static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt, *((u64 *)old) = val;break ; } +#ifdef CONFIG_X86_64 +case 16 : { +u64 *p1 = (u64 *)kaddr; +u64 *p2 = p1 +1 ; +u64 *o1 = old; +u64 *o2 = o1 +1 ; +const u64 *n1 = new; +const u64 *n2 = n1 +1 ; +const u64 __o1 = *o1; +const u64 __o2 = *o2; + +/* +* We use an explicit asm statement because cmpxchg_double() +* does not return the previous memory contents on failure +*/ + asmvolatile ("lock cmpxchg16b %2\n" + :"+a" (*o1),"+d" (*o2),"+m" (*p1),"+m" (*p2) + :"b" (*n1),"c" (*n2) :"memory" ); + +if (__o1 == *o1 && __o2 == *o2) + exchanged = true; +break ; + } +#endif default :BUG (); }
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 92/92 kvm: x86: fallback to the single-step on multipage CMPXCHG emulation
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
There are cases where we need to emulate a CMPXCHG that touches two pages (4 in one and another 4 in the next, for example). Because it is not easy to map two pages in the kernel so that we can directly execute the exchange instruction, we fallback to single-stepping. Luckly, this is an uncommon occurrence making the overhead of the single-step mechanism acceptable.
--- arch/x86/kvm/x86.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
arch/x86/kvm/x86.c +9 -6
@@ -5671,6 +5671,12 @@ static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,#define CMPXCHG_MAX_BYTES 8#endif + gpa =kvm_mmu_gva_to_gpa_write (vcpu, addr,NULL ); + +if (gpa ==UNMAPPED_GVA || + (gpa &PAGE_MASK ) ==APIC_DEFAULT_PHYS_BASE ) + goto emul_write; +/* guests cmpxchg{8,16}b have to be emulated atomically */ if (bytes >CMPXCHG_MAX_BYTES || (bytes & (bytes -1 ))) goto emul_write;
@@ -5678,12 +5684,6 @@ static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,if (bytes ==16 && !system_has_cmpxchg_double ()) goto emul_write; - gpa =kvm_mmu_gva_to_gpa_write (vcpu, addr,NULL ); - -if (gpa ==UNMAPPED_GVA || - (gpa &PAGE_MASK ) ==APIC_DEFAULT_PHYS_BASE ) - goto emul_write; -if (((gpa + bytes -1 ) &PAGE_MASK ) != (gpa &PAGE_MASK )) goto emul_write;
@@ -5772,6 +5772,9 @@ static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,return X86EMUL_CONTINUE ; emul_write: +if (kvmi_tracked_gfn (vcpu, gpa >>PAGE_SHIFT )) +return X86EMUL_UNHANDLEABLE ; +printk_once (KERN_WARNING "kvm: emulating exchange as write\n" );return emulator_write_emulated (ctxt, addr, new, bytes, exception);
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 6 lines · Adalbert Lazăr
virt/kvm/kvm_main.c | 70 +- virt/kvm/kvmi.c | 2054 ++++++++++++++++++++++ virt/kvm/kvmi_int.h | 311 ++++ virt/kvm/kvmi_mem.c | 324 ++++ virt/kvm/kvmi_mem_guest.c | 651 +++++++ virt/kvm/kvmi_msg.c | 1236 +++++++++++++
That's a lot of code. An 'introspection' sub-directory might be warranted.
_______________________________________________
Virtualization mailing list [email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 1 line · Adalbert Lazăr
55 files changed, 13485 insertions(+), 225 deletions(-)
The size of this series is overwhelming, to say the least. The remote pages concept and SPP patches on their own would be hefty series to review.
It would be very helpful to reviewers to reorder the patches and only send the bits that are absolutely mandatory for initial support. For example, AFAICT the SPP support and remote pages concept are largely performance related and not functionally required.
Note, this wouldn't prevent you from carrying the series in its entirety in your own branches.
Possible reordering:
- Bug fixes (if any patches qualify as such)
- Emulator changes
- KVM preparatory patches
- Basic instrospection functionality
------>8-------- cut the series here
- Optional introspection functionality (if there is any)
- SPP and introspection integration
- Remote pages and introspection integration
_______________________________________________
Virtualization mailing list [email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Paolo Bonzini
maintainerQuoted 30 lines · Adalbert Lazăr
Patches 1-20: unroll a big part of the KVM introspection subsystem, sent in one patch in the previous versions.
Patches 21-24: extend the current page tracking code.
Patches 25-33: make use of page tracking to support the
KVMI_SET_PAGE_ACCESSintrospection command and theKVMI_EVENT_PFevent (on EPT violations caused by the tracking settings).Patches 34-42: include the SPP feature (Enable Sub-page Write Protection Support), already sent to KVM list:
https://lore.kernel.org/lkml/[email protected]/Patches 43-46: add the commands needed to use SPP.
Patches 47-63: unroll almost all the rest of the introspection code.
Patches 64-67: add single-stepping, mostly as a way to overcome the unimplemented instructions, but also as a feature for the introspection tool.
Patches 68-70: cover more cases related to EPT violations.
Patches 71-73: add the remote mapping feature, allowing the introspection tool to map into its address space a page from guest memory.
Patches 74: add a fix to hypercall emulation.
Quoted 8 lines
Patches 75-76: disable some features/optimizations when the introspection code is present.
Patches 77-78: add trace functions for the introspection code and change some related to interrupts/exceptions injection.
Patches 79-92: new instruction for the x86 emulator, including cmpxchg fixes.
Thanks for the very good explanation. Apart from the complicated flow of KVM request handling and KVM reply, the main issue is the complete lack of testcases. There should be a kvmi_test in tools/testing/selftests/kvm, and each patch adding a new ioctl or event should add a new testcase.
Paolo
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization