From patchwork Fri May 8 08:27:28 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Dong, Eddie" X-Patchwork-Id: 22545 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n488TO71024540 for ; Fri, 8 May 2009 08:29:24 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757424AbZEHI3T (ORCPT ); Fri, 8 May 2009 04:29:19 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1757267AbZEHI3T (ORCPT ); Fri, 8 May 2009 04:29:19 -0400 Received: from mga14.intel.com ([143.182.124.37]:45538 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756824AbZEHI3Q (ORCPT ); Fri, 8 May 2009 04:29:16 -0400 Received: from azsmga001.ch.intel.com ([10.2.17.19]) by azsmga102.ch.intel.com with ESMTP; 08 May 2009 01:29:14 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="4.40,316,1239001200"; d="scan'208";a="140616111" Received: from pgsmsx603.gar.corp.intel.com ([10.221.43.87]) by azsmga001.ch.intel.com with ESMTP; 08 May 2009 01:29:13 -0700 Received: from pgsmsx601.gar.corp.intel.com (10.221.43.69) by pgsmsx603.gar.corp.intel.com (10.221.43.87) with Microsoft SMTP Server (TLS) id 8.1.340.0; Fri, 8 May 2009 16:28:18 +0800 Received: from pdsmsx601.ccr.corp.intel.com (172.16.12.94) by pgsmsx601.gar.corp.intel.com (10.221.43.69) with Microsoft SMTP Server (TLS) id 8.1.340.0; Fri, 8 May 2009 16:28:18 +0800 Received: from pdsmsx503.ccr.corp.intel.com ([172.16.12.95]) by pdsmsx601.ccr.corp.intel.com ([172.16.12.94]) with mapi; Fri, 8 May 2009 16:27:37 +0800 From: "Dong, Eddie" To: Gleb Natapov CC: "kvm@vger.kernel.org" , Avi Kivity , "Dong, Eddie" Date: Fri, 8 May 2009 16:27:28 +0800 Subject: RE: Implement generic double fault generation mechanism Thread-Topic: Implement generic double fault generation mechanism Thread-Index: AcnL3WhbpXMgMiNvRWGUyI26/a2tHAD2F6sw Message-ID: <9832F13BD22FB94A829F798DA4A8280501A81A8E83@pdsmsx503.ccr.corp.intel.com> References: <9832F13BD22FB94A829F798DA4A8280501A80F02A3@pdsmsx503.ccr.corp.intel.com> <20090503105330.GL9795@redhat.com> In-Reply-To: <20090503105330.GL9795@redhat.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: yes X-MS-TNEF-Correlator: acceptlanguage: en-US MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Gleb Natapov wrote: >> + >> +static int exception_class(int vector) >> +{ >> + if (vector == 14) >> + return EXCPT_PF; >> + else if (vector == 0 || (vector >= 10 && vector <= 13)) + return >> EXCPT_CONTRIBUTORY; + else >> + return EXCPT_BENIGN; >> +} >> + > This makes double fault (8) benign exception. Surely not what you > want. double fault fall into neither of above class per SDM. But it should be checked earlier than generating DB fault. See new updated. >> + /* to check exception */ >> + prev_nr = vcpu->arch.exception.nr; >> + class2 = exception_class(nr); >> + class1 = exception_class(prev_nr); >> + if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY) >> + || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) { >> + /* generate double fault per SDM Table 5-5 */ >> + printk(KERN_DEBUG "kvm: double fault 0x%x on 0x%x\n", >> + prev_nr, nr); + vcpu->arch.exception.pending = true; >> + vcpu->arch.exception.has_error_code = 1; >> + vcpu->arch.exception.nr = DF_VECTOR; >> + vcpu->arch.exception.error_code = 0; >> + if (prev_nr == DF_VECTOR) { >> + /* triple fault -> shutdown */ >> + set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests); + } >> + } else >> + printk(KERN_ERR "Exception 0x%x on 0x%x happens serially\n", >> + prev_nr, nr); +} > When two exceptions happens serially is is better to replace pending > exception with a new one. This way the first exception (that is lost) > will be regenerated when instruction will be re-executed. Do you want it to be covered for now? For exception, it is easy but for IRQ, it needs to be pushed back. Thx, eddie Move Double-Fault generation logic out of page fault exception generating function to cover more generic case. Signed-off-by: Eddie Dong diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index ab1fdac..d0e75a2 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -162,12 +162,60 @@ void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data) } EXPORT_SYMBOL_GPL(kvm_set_apic_base); +#define EXCPT_BENIGN 0 +#define EXCPT_CONTRIBUTORY 1 +#define EXCPT_PF 2 + +static int exception_class(int vector) +{ + if (vector == 14) + return EXCPT_PF; + else if (vector == 0 || (vector >= 10 && vector <= 13)) + return EXCPT_CONTRIBUTORY; + else + return EXCPT_BENIGN; +} + +static void kvm_multiple_exception(struct kvm_vcpu *vcpu, + unsigned nr, bool has_error, u32 error_code) +{ + u32 prev_nr; + int class1, class2; + + if (!vcpu->arch.exception.pending) { + vcpu->arch.exception.pending = true; + vcpu->arch.exception.has_error_code = has_error; + vcpu->arch.exception.nr = nr; + vcpu->arch.exception.error_code = error_code; + return; + } + + /* to check exception */ + prev_nr = vcpu->arch.exception.nr; + if (prev_nr == DF_VECTOR) { + /* triple fault -> shutdown */ + set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests); + return; + } + class1 = exception_class(prev_nr); + class2 = exception_class(nr); + if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY) + || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) { + /* generate double fault per SDM Table 5-5 */ + printk(KERN_DEBUG "kvm: double fault 0x%x on 0x%x\n", + prev_nr, nr); + vcpu->arch.exception.pending = true; + vcpu->arch.exception.has_error_code = true; + vcpu->arch.exception.nr = DF_VECTOR; + vcpu->arch.exception.error_code = 0; + } else + printk(KERN_ERR "Exception 0x%x on 0x%x happens serially\n", + prev_nr, nr); +} + void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr) { - WARN_ON(vcpu->arch.exception.pending); - vcpu->arch.exception.pending = true; - vcpu->arch.exception.has_error_code = false; - vcpu->arch.exception.nr = nr; + kvm_multiple_exception(vcpu, nr, false, 0); } EXPORT_SYMBOL_GPL(kvm_queue_exception); @@ -176,18 +224,6 @@ void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr, { ++vcpu->stat.pf_guest; - if (vcpu->arch.exception.pending) { - if (vcpu->arch.exception.nr == PF_VECTOR) { - printk(KERN_DEBUG "kvm: inject_page_fault:" - " double fault 0x%lx\n", addr); - vcpu->arch.exception.nr = DF_VECTOR; - vcpu->arch.exception.error_code = 0; - } else if (vcpu->arch.exception.nr == DF_VECTOR) { - /* triple fault -> shutdown */ - set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests); - } - return; - } vcpu->arch.cr2 = addr; kvm_queue_exception_e(vcpu, PF_VECTOR, error_code); } @@ -200,11 +236,7 @@ EXPORT_SYMBOL_GPL(kvm_inject_nmi); void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) { - WARN_ON(vcpu->arch.exception.pending); - vcpu->arch.exception.pending = true; - vcpu->arch.exception.has_error_code = true; - vcpu->arch.exception.nr = nr; - vcpu->arch.exception.error_code = error_code; + kvm_multiple_exception(vcpu, nr, true, error_code); } EXPORT_SYMBOL_GPL(kvm_queue_exception_e);