Message ID | 20210811122927.900604-4-mlevitsk@redhat.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | KVM: my debug patch queue | expand |
On Wed, 2021-08-11 at 15:29 +0300, Maxim Levitsky wrote: > Currently #TS interception is only done once. > Also exception interception is not enabled for SEV guests. > > Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com> > --- > arch/x86/include/asm/kvm_host.h | 2 + > arch/x86/kvm/svm/svm.c | 70 +++++++++++++++++++++++++++++++++ > arch/x86/kvm/svm/svm.h | 6 ++- > arch/x86/kvm/x86.c | 5 ++- > 4 files changed, 80 insertions(+), 3 deletions(-) > > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h > index 20daaf67a5bf..72fe03506018 100644 > --- a/arch/x86/include/asm/kvm_host.h > +++ b/arch/x86/include/asm/kvm_host.h > @@ -1690,6 +1690,8 @@ int kvm_emulate_rdpmc(struct kvm_vcpu *vcpu); > void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr); > void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code); > void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr, unsigned long payload); > +void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr, > + u32 error_code, unsigned long payload); > void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr); > void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code); > void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault); > diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c > index e45259177009..19f54b07161a 100644 > --- a/arch/x86/kvm/svm/svm.c > +++ b/arch/x86/kvm/svm/svm.c > @@ -233,6 +233,8 @@ static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000}; > #define MSRS_RANGE_SIZE 2048 > #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2) > > +static int svm_handle_invalid_exit(struct kvm_vcpu *vcpu, u64 exit_code); > + > u32 svm_msrpm_offset(u32 msr) > { > u32 offset; > @@ -1153,6 +1155,22 @@ static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu, > } > } > > +static void svm_init_force_exceptions_intercepts(struct vcpu_svm *svm) > +{ > + int exc; > + > + svm->force_intercept_exceptions_mask = force_intercept_exceptions_mask; > + for (exc = 0 ; exc < 32 ; exc++) { > + if (!(svm->force_intercept_exceptions_mask & (1 << exc))) > + continue; > + > + /* Those are defined to have undefined behavior in the SVM spec */ > + if (exc != 2 && exc != 9) > + continue; > + set_exception_intercept(svm, exc); I made a mistake here, during one of the refactoring I think, after I finished testing this througfully, and I noticed it now while looking again at the code. I attached a fix for this, and I also tested more carefully that the feature works with selftests, kvm unit tests and by booting few VMs. Best regards, Maxim Levitsky > + } > +} > + > static void init_vmcb(struct kvm_vcpu *vcpu) > { > struct vcpu_svm *svm = to_svm(vcpu); > @@ -1304,6 +1322,9 @@ static void init_vmcb(struct kvm_vcpu *vcpu) > > enable_gif(svm); > > + if (!sev_es_guest(vcpu->kvm)) > + svm_init_force_exceptions_intercepts(svm); > + > } > > static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) > @@ -1892,6 +1913,17 @@ static int pf_interception(struct kvm_vcpu *vcpu) > u64 fault_address = svm->vmcb->control.exit_info_2; > u64 error_code = svm->vmcb->control.exit_info_1; > > + if ((svm->force_intercept_exceptions_mask & (1 << PF_VECTOR))) > + if (npt_enabled && !vcpu->arch.apf.host_apf_flags) { > + /* If the #PF was only intercepted for debug, inject > + * it directly to the guest, since the kvm's mmu code > + * is not ready to deal with such page faults. > + */ > + kvm_queue_exception_e_p(vcpu, PF_VECTOR, > + error_code, fault_address); > + return 1; > + } > + > return kvm_handle_page_fault(vcpu, error_code, fault_address, > static_cpu_has(X86_FEATURE_DECODEASSISTS) ? > svm->vmcb->control.insn_bytes : NULL, > @@ -1967,6 +1999,40 @@ static int ac_interception(struct kvm_vcpu *vcpu) > return 1; > } > > +static int gen_exc_interception(struct kvm_vcpu *vcpu) > +{ > + /* > + * Generic exception intercept handler which forwards a guest exception > + * as-is to the guest. > + * For exceptions that don't have a special intercept handler. > + * > + * Used only for 'force_intercept_exceptions_mask' KVM debug feature. > + */ > + struct vcpu_svm *svm = to_svm(vcpu); > + int exc = svm->vmcb->control.exit_code - SVM_EXIT_EXCP_BASE; > + > + /* SVM doesn't provide us with an error code for the #DF */ > + u32 err_code = exc == DF_VECTOR ? 0 : svm->vmcb->control.exit_info_1; > + > + if (!(svm->force_intercept_exceptions_mask & (1 << exc))) > + return svm_handle_invalid_exit(vcpu, svm->vmcb->control.exit_code); > + > + if (exc == TS_VECTOR) { > + /* > + * SVM doesn't provide us with an error code to be able to > + * re-inject the #TS exception, so just disable its > + * intercept, and let the guest re-execute the instruction. > + */ > + vmcb_clr_intercept(&svm->vmcb01.ptr->control, > + INTERCEPT_EXCEPTION_OFFSET + TS_VECTOR); > + recalc_intercepts(svm); > + } else if (x86_exception_has_error_code(exc)) > + kvm_queue_exception_e(vcpu, exc, err_code); > + else > + kvm_queue_exception(vcpu, exc); > + return 1; > +} > + > static bool is_erratum_383(void) > { > int err, i; > @@ -3065,6 +3131,10 @@ static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = { > [SVM_EXIT_WRITE_DR5] = dr_interception, > [SVM_EXIT_WRITE_DR6] = dr_interception, > [SVM_EXIT_WRITE_DR7] = dr_interception, > + > + [SVM_EXIT_EXCP_BASE ... > + SVM_EXIT_EXCP_BASE + 31] = gen_exc_interception, > + > [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception, > [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception, > [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception, > diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h > index 524d943f3efc..187ada7c5b03 100644 > --- a/arch/x86/kvm/svm/svm.h > +++ b/arch/x86/kvm/svm/svm.h > @@ -196,6 +196,7 @@ struct vcpu_svm { > bool ghcb_sa_free; > > bool guest_state_loaded; > + u32 force_intercept_exceptions_mask; > }; > > struct svm_cpu_data { > @@ -351,8 +352,11 @@ static inline void clr_exception_intercept(struct vcpu_svm *svm, u32 bit) > struct vmcb *vmcb = svm->vmcb01.ptr; > > WARN_ON_ONCE(bit >= 32); > - vmcb_clr_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit); > > + if ((1 << bit) & svm->force_intercept_exceptions_mask) > + return; > + > + vmcb_clr_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit); > recalc_intercepts(svm); > } > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index 092e2fad3c0d..e5c7b8fa1f7f 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -695,12 +695,13 @@ void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr, > } > EXPORT_SYMBOL_GPL(kvm_queue_exception_p); > > -static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr, > - u32 error_code, unsigned long payload) > +void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr, > + u32 error_code, unsigned long payload) > { > kvm_multiple_exception(vcpu, nr, true, error_code, > true, payload, false); > } > +EXPORT_SYMBOL_GPL(kvm_queue_exception_e_p); > > int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err) > {
On Wed, Aug 11, 2021, Maxim Levitsky wrote: > On Wed, 2021-08-11 at 15:29 +0300, Maxim Levitsky wrote: > > diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c > > index e45259177009..19f54b07161a 100644 > > --- a/arch/x86/kvm/svm/svm.c > > +++ b/arch/x86/kvm/svm/svm.c > > @@ -233,6 +233,8 @@ static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000}; > > #define MSRS_RANGE_SIZE 2048 > > #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2) > > > > +static int svm_handle_invalid_exit(struct kvm_vcpu *vcpu, u64 exit_code); > > + > > u32 svm_msrpm_offset(u32 msr) > > { > > u32 offset; > > @@ -1153,6 +1155,22 @@ static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu, > > } > > } > > > > +static void svm_init_force_exceptions_intercepts(struct vcpu_svm *svm) > > +{ > > + int exc; > > + > > + svm->force_intercept_exceptions_mask = force_intercept_exceptions_mask; Ah, the param is being snapshotted on vCPU creation, hence the writable module param. That works, though it'd be better to snapshot it on a per-VM basic, not per-vCPU, and do so in common x86 code so that the param doesn't need to be exported. > > + for (exc = 0 ; exc < 32 ; exc++) { for_each_set_bit() > > + if (!(svm->force_intercept_exceptions_mask & (1 << exc))) > > + continue; > > + > > + /* Those are defined to have undefined behavior in the SVM spec */ > > + if (exc != 2 && exc != 9) Maybe add a pr_warn_once() to let the user know they done messed up? And given that there are already intercepts with undefined behavior, it's probably best to disallow intercepting anything we aren't 100% postive will be handled correctly, e.g. intercepting vector 31 is nonsensical at this time. > > + continue; > > + set_exception_intercept(svm, exc); ... > > +static int gen_exc_interception(struct kvm_vcpu *vcpu) > > +{ > > + /* > > + * Generic exception intercept handler which forwards a guest exception > > + * as-is to the guest. > > + * For exceptions that don't have a special intercept handler. > > + * > > + * Used only for 'force_intercept_exceptions_mask' KVM debug feature. > > + */ > > + struct vcpu_svm *svm = to_svm(vcpu); > > + int exc = svm->vmcb->control.exit_code - SVM_EXIT_EXCP_BASE; > > + > > + /* SVM doesn't provide us with an error code for the #DF */ > > + u32 err_code = exc == DF_VECTOR ? 0 : svm->vmcb->control.exit_info_1; Might be better to handle this in the x86_exception_has_error_code() path to avoid confusing readers with respect to exceptions that don't have an error code, e.g. else if (x86_exception_has_error_code(exc)) { /* SVM doesn't provide the error code on #DF :-( */ if (exc == DF_VECTOR) kvm_queue_exception_e(vcpu, exc, 0); else kvm_queue_exception_e(vcpu, exc, svm->vmcb->control.exit_info_1); } else { ... } Alternatively, can we zero svm->vmcb->control.exit_info_1 on #DF to make it more obvious that SVM leaves stale data in exit_info_1 (assuming that's true)? E.g. ... if (exc == TS_VECTOR) { ... } else if (x86_exception_has_error_code(exc)) { /* SVM doesn't provide the error code on #DF :-( */ if (exc == DF_VECTOR) svm->vmcb->control.exit_info_1 = 0; kvm_queue_exception_e(vcpu, exc, svm->vmcb->control.exit_info_1); } else { ... } > > + > > + if (!(svm->force_intercept_exceptions_mask & (1 << exc))) BIT(exc) > > + return svm_handle_invalid_exit(vcpu, svm->vmcb->control.exit_code); > > + > > + if (exc == TS_VECTOR) { > > + /* > > + * SVM doesn't provide us with an error code to be able to > > + * re-inject the #TS exception, so just disable its > > + * intercept, and let the guest re-execute the instruction. > > + */ > > + vmcb_clr_intercept(&svm->vmcb01.ptr->control, > > + INTERCEPT_EXCEPTION_OFFSET + TS_VECTOR); Maybe just disallow intercepting #TS altogether? Or does this fall into your Win98 use case? :-) > > + recalc_intercepts(svm); > > + } else if (x86_exception_has_error_code(exc)) > > + kvm_queue_exception_e(vcpu, exc, err_code); > > + else > > + kvm_queue_exception(vcpu, exc); > > + return 1; > > +} > > + > > static bool is_erratum_383(void) > > { > > int err, i; > > @@ -3065,6 +3131,10 @@ static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = { > > [SVM_EXIT_WRITE_DR5] = dr_interception, > > [SVM_EXIT_WRITE_DR6] = dr_interception, > > [SVM_EXIT_WRITE_DR7] = dr_interception, > > + > > + [SVM_EXIT_EXCP_BASE ... > > + SVM_EXIT_EXCP_BASE + 31] = gen_exc_interception, This generates a Sparse warning due to the duplicate initializer. IMO that's a very good warning as I have zero idea how the compiler actually handles this particular scenario, e.g. do later entries take priority, is it technically "undefined" behavior, etc... arch/x86/kvm/svm/svm.c:3065:10: warning: Initializer entry defined twice arch/x86/kvm/svm/svm.c:3067:29: also defined here I don't have a clever solution though :-( > > + > > [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception, > > [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception, > > [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception, > > diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h > > index 524d943f3efc..187ada7c5b03 100644 > > --- a/arch/x86/kvm/svm/svm.h > > +++ b/arch/x86/kvm/svm/svm.h > > @@ -196,6 +196,7 @@ struct vcpu_svm { > > bool ghcb_sa_free; > > > > bool guest_state_loaded; > > + u32 force_intercept_exceptions_mask; > > }; > > > > struct svm_cpu_data { > > @@ -351,8 +352,11 @@ static inline void clr_exception_intercept(struct vcpu_svm *svm, u32 bit) > > struct vmcb *vmcb = svm->vmcb01.ptr; > > > > WARN_ON_ONCE(bit >= 32); > > - vmcb_clr_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit); > > > > + if ((1 << bit) & svm->force_intercept_exceptions_mask) BIT(bit) > > + return; > > + > > + vmcb_clr_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit); > > recalc_intercepts(svm); > > }
On Thu, 2021-09-02 at 17:34 +0000, Sean Christopherson wrote: > On Wed, Aug 11, 2021, Maxim Levitsky wrote: > > On Wed, 2021-08-11 at 15:29 +0300, Maxim Levitsky wrote: > > > diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c > > > index e45259177009..19f54b07161a 100644 > > > --- a/arch/x86/kvm/svm/svm.c > > > +++ b/arch/x86/kvm/svm/svm.c > > > @@ -233,6 +233,8 @@ static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000}; > > > #define MSRS_RANGE_SIZE 2048 > > > #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2) > > > > > > +static int svm_handle_invalid_exit(struct kvm_vcpu *vcpu, u64 exit_code); > > > + > > > u32 svm_msrpm_offset(u32 msr) > > > { > > > u32 offset; > > > @@ -1153,6 +1155,22 @@ static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu, > > > } > > > } > > > > > > +static void svm_init_force_exceptions_intercepts(struct vcpu_svm *svm) > > > +{ > > > + int exc; > > > + > > > + svm->force_intercept_exceptions_mask = force_intercept_exceptions_mask; > > Ah, the param is being snapshotted on vCPU creation, hence the writable module > param. That works, though it'd be better to snapshot it on a per-VM basic, not > per-vCPU, and do so in common x86 code so that the param doesn't need to be > exported. I have nothing against that. > > > > + for (exc = 0 ; exc < 32 ; exc++) { > > for_each_set_bit() I used a helper function instead, IMHO a bit cleaner. > > > > + if (!(svm->force_intercept_exceptions_mask & (1 << exc))) > > > + continue; > > > + > > > + /* Those are defined to have undefined behavior in the SVM spec */ > > > + if (exc != 2 && exc != 9) > > Maybe add a pr_warn_once() to let the user know they done messed up? > > And given that there are already intercepts with undefined behavior, it's probably > best to disallow intercepting anything we aren't 100% postive will be handled > correctly, e.g. intercepting vector 31 is nonsensical at this time. Or I think I'll just drop this check altogether - this is a debug feature anyway. > > > > + continue; > > > + set_exception_intercept(svm, exc); > > ... > > > > +static int gen_exc_interception(struct kvm_vcpu *vcpu) > > > +{ > > > + /* > > > + * Generic exception intercept handler which forwards a guest exception > > > + * as-is to the guest. > > > + * For exceptions that don't have a special intercept handler. > > > + * > > > + * Used only for 'force_intercept_exceptions_mask' KVM debug feature. > > > + */ > > > + struct vcpu_svm *svm = to_svm(vcpu); > > > + int exc = svm->vmcb->control.exit_code - SVM_EXIT_EXCP_BASE; > > > + > > > + /* SVM doesn't provide us with an error code for the #DF */ > > > + u32 err_code = exc == DF_VECTOR ? 0 : svm->vmcb->control.exit_info_1; > > Might be better to handle this in the x86_exception_has_error_code() path to > avoid confusing readers with respect to exceptions that don't have an error code, > e.g. > > else if (x86_exception_has_error_code(exc)) { > /* SVM doesn't provide the error code on #DF :-( */ > if (exc == DF_VECTOR) > kvm_queue_exception_e(vcpu, exc, 0); > else > kvm_queue_exception_e(vcpu, exc, svm->vmcb->control.exit_info_1); > } else { > ... > } > > Alternatively, can we zero svm->vmcb->control.exit_info_1 on #DF to make it more > obvious that SVM leaves stale data in exit_info_1 (assuming that's true)? E.g. > > ... > > if (exc == TS_VECTOR) { > ... > } else if (x86_exception_has_error_code(exc)) { > /* SVM doesn't provide the error code on #DF :-( */ > if (exc == DF_VECTOR) > svm->vmcb->control.exit_info_1 = 0; > > kvm_queue_exception_e(vcpu, exc, svm->vmcb->control.exit_info_1); > } else { > ... > } Makes sense. > > > > > + > > > + if (!(svm->force_intercept_exceptions_mask & (1 << exc))) > > BIT(exc) I added a helper function in common x86 code for this. > > > > + return svm_handle_invalid_exit(vcpu, svm->vmcb->control.exit_code); > > > + > > > + if (exc == TS_VECTOR) { > > > + /* > > > + * SVM doesn't provide us with an error code to be able to > > > + * re-inject the #TS exception, so just disable its > > > + * intercept, and let the guest re-execute the instruction. > > > + */ > > > + vmcb_clr_intercept(&svm->vmcb01.ptr->control, > > > + INTERCEPT_EXCEPTION_OFFSET + TS_VECTOR); > > Maybe just disallow intercepting #TS altogether? Or does this fall into your > Win98 use case? :-) Win98 does indeed generate few #TS exceptions but so far I haven't noticed any issues related to task switches. Anyway I would like to intercept as much as possible since this is a debug feature. A single interception is still better that nothing. > > > > + recalc_intercepts(svm); > > > + } else if (x86_exception_has_error_code(exc)) > > > + kvm_queue_exception_e(vcpu, exc, err_code); > > > + else > > > + kvm_queue_exception(vcpu, exc); > > > + return 1; > > > +} > > > + > > > static bool is_erratum_383(void) > > > { > > > int err, i; > > > @@ -3065,6 +3131,10 @@ static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = { > > > [SVM_EXIT_WRITE_DR5] = dr_interception, > > > [SVM_EXIT_WRITE_DR6] = dr_interception, > > > [SVM_EXIT_WRITE_DR7] = dr_interception, > > > + > > > + [SVM_EXIT_EXCP_BASE ... > > > + SVM_EXIT_EXCP_BASE + 31] = gen_exc_interception, > > This generates a Sparse warning due to the duplicate initializer. IMO that's a > very good warning as I have zero idea how the compiler actually handles this > particular scenario, e.g. do later entries take priority, is it technically > "undefined" behavior, etc... > > arch/x86/kvm/svm/svm.c:3065:10: warning: Initializer entry defined twice > arch/x86/kvm/svm/svm.c:3067:29: also defined here > > I don't have a clever solution though :-(' Good catch. I thought that this would make sense but standards never make sense. I'll do this manually. > > > > + > > > [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception, > > > [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception, > > > [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception, > > > diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h > > > index 524d943f3efc..187ada7c5b03 100644 > > > --- a/arch/x86/kvm/svm/svm.h > > > +++ b/arch/x86/kvm/svm/svm.h > > > @@ -196,6 +196,7 @@ struct vcpu_svm { > > > bool ghcb_sa_free; > > > > > > bool guest_state_loaded; > > > + u32 force_intercept_exceptions_mask; > > > }; > > > > > > struct svm_cpu_data { > > > @@ -351,8 +352,11 @@ static inline void clr_exception_intercept(struct vcpu_svm *svm, u32 bit) > > > struct vmcb *vmcb = svm->vmcb01.ptr; > > > > > > WARN_ON_ONCE(bit >= 32); > > > - vmcb_clr_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit); > > > > > > + if ((1 << bit) & svm->force_intercept_exceptions_mask) > > BIT(bit) Fixed with helper function as well. > > > > + return; > > > + > > > + vmcb_clr_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit); > > > recalc_intercepts(svm); > > > } Thanks for the review! Best regards, Maxim Levitsky
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 20daaf67a5bf..72fe03506018 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1690,6 +1690,8 @@ int kvm_emulate_rdpmc(struct kvm_vcpu *vcpu); void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr); void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code); void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr, unsigned long payload); +void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr, + u32 error_code, unsigned long payload); void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr); void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code); void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index e45259177009..19f54b07161a 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -233,6 +233,8 @@ static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000}; #define MSRS_RANGE_SIZE 2048 #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2) +static int svm_handle_invalid_exit(struct kvm_vcpu *vcpu, u64 exit_code); + u32 svm_msrpm_offset(u32 msr) { u32 offset; @@ -1153,6 +1155,22 @@ static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu, } } +static void svm_init_force_exceptions_intercepts(struct vcpu_svm *svm) +{ + int exc; + + svm->force_intercept_exceptions_mask = force_intercept_exceptions_mask; + for (exc = 0 ; exc < 32 ; exc++) { + if (!(svm->force_intercept_exceptions_mask & (1 << exc))) + continue; + + /* Those are defined to have undefined behavior in the SVM spec */ + if (exc != 2 && exc != 9) + continue; + set_exception_intercept(svm, exc); + } +} + static void init_vmcb(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); @@ -1304,6 +1322,9 @@ static void init_vmcb(struct kvm_vcpu *vcpu) enable_gif(svm); + if (!sev_es_guest(vcpu->kvm)) + svm_init_force_exceptions_intercepts(svm); + } static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) @@ -1892,6 +1913,17 @@ static int pf_interception(struct kvm_vcpu *vcpu) u64 fault_address = svm->vmcb->control.exit_info_2; u64 error_code = svm->vmcb->control.exit_info_1; + if ((svm->force_intercept_exceptions_mask & (1 << PF_VECTOR))) + if (npt_enabled && !vcpu->arch.apf.host_apf_flags) { + /* If the #PF was only intercepted for debug, inject + * it directly to the guest, since the kvm's mmu code + * is not ready to deal with such page faults. + */ + kvm_queue_exception_e_p(vcpu, PF_VECTOR, + error_code, fault_address); + return 1; + } + return kvm_handle_page_fault(vcpu, error_code, fault_address, static_cpu_has(X86_FEATURE_DECODEASSISTS) ? svm->vmcb->control.insn_bytes : NULL, @@ -1967,6 +1999,40 @@ static int ac_interception(struct kvm_vcpu *vcpu) return 1; } +static int gen_exc_interception(struct kvm_vcpu *vcpu) +{ + /* + * Generic exception intercept handler which forwards a guest exception + * as-is to the guest. + * For exceptions that don't have a special intercept handler. + * + * Used only for 'force_intercept_exceptions_mask' KVM debug feature. + */ + struct vcpu_svm *svm = to_svm(vcpu); + int exc = svm->vmcb->control.exit_code - SVM_EXIT_EXCP_BASE; + + /* SVM doesn't provide us with an error code for the #DF */ + u32 err_code = exc == DF_VECTOR ? 0 : svm->vmcb->control.exit_info_1; + + if (!(svm->force_intercept_exceptions_mask & (1 << exc))) + return svm_handle_invalid_exit(vcpu, svm->vmcb->control.exit_code); + + if (exc == TS_VECTOR) { + /* + * SVM doesn't provide us with an error code to be able to + * re-inject the #TS exception, so just disable its + * intercept, and let the guest re-execute the instruction. + */ + vmcb_clr_intercept(&svm->vmcb01.ptr->control, + INTERCEPT_EXCEPTION_OFFSET + TS_VECTOR); + recalc_intercepts(svm); + } else if (x86_exception_has_error_code(exc)) + kvm_queue_exception_e(vcpu, exc, err_code); + else + kvm_queue_exception(vcpu, exc); + return 1; +} + static bool is_erratum_383(void) { int err, i; @@ -3065,6 +3131,10 @@ static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = { [SVM_EXIT_WRITE_DR5] = dr_interception, [SVM_EXIT_WRITE_DR6] = dr_interception, [SVM_EXIT_WRITE_DR7] = dr_interception, + + [SVM_EXIT_EXCP_BASE ... + SVM_EXIT_EXCP_BASE + 31] = gen_exc_interception, + [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception, [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception, [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception, diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 524d943f3efc..187ada7c5b03 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -196,6 +196,7 @@ struct vcpu_svm { bool ghcb_sa_free; bool guest_state_loaded; + u32 force_intercept_exceptions_mask; }; struct svm_cpu_data { @@ -351,8 +352,11 @@ static inline void clr_exception_intercept(struct vcpu_svm *svm, u32 bit) struct vmcb *vmcb = svm->vmcb01.ptr; WARN_ON_ONCE(bit >= 32); - vmcb_clr_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit); + if ((1 << bit) & svm->force_intercept_exceptions_mask) + return; + + vmcb_clr_intercept(&vmcb->control, INTERCEPT_EXCEPTION_OFFSET + bit); recalc_intercepts(svm); } diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 092e2fad3c0d..e5c7b8fa1f7f 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -695,12 +695,13 @@ void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr, } EXPORT_SYMBOL_GPL(kvm_queue_exception_p); -static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr, - u32 error_code, unsigned long payload) +void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr, + u32 error_code, unsigned long payload) { kvm_multiple_exception(vcpu, nr, true, error_code, true, payload, false); } +EXPORT_SYMBOL_GPL(kvm_queue_exception_e_p); int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err) {
Currently #TS interception is only done once. Also exception interception is not enabled for SEV guests. Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com> --- arch/x86/include/asm/kvm_host.h | 2 + arch/x86/kvm/svm/svm.c | 70 +++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 6 ++- arch/x86/kvm/x86.c | 5 ++- 4 files changed, 80 insertions(+), 3 deletions(-)