From patchwork Mon Sep 21 15:33:55 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mohammed Gamal X-Patchwork-Id: 49068 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 n8LFdiLx014882 for ; Mon, 21 Sep 2009 15:39:44 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751039AbZIUPji (ORCPT ); Mon, 21 Sep 2009 11:39:38 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1750988AbZIUPji (ORCPT ); Mon, 21 Sep 2009 11:39:38 -0400 Received: from mail-fx0-f218.google.com ([209.85.220.218]:33001 "EHLO mail-fx0-f218.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750822AbZIUPji (ORCPT ); Mon, 21 Sep 2009 11:39:38 -0400 X-Greylist: delayed 334 seconds by postgrey-1.27 at vger.kernel.org; Mon, 21 Sep 2009 11:39:37 EDT Received: by fxm18 with SMTP id 18so134843fxm.17 for ; Mon, 21 Sep 2009 08:39:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:from:to:cc:subject:date :message-id:x-mailer; bh=RspkT8RPQ9UQKI4Azfd80vmUBvm5eTAcTFmZZsXuN+Q=; b=P0ERhVCfuMjDNcAdFl5rNntzUER5rNPEsKYGDvphPExNBRf1KO34wm5VQcrIisG8uj iOOsaGtq03h//hxj0HhlaMdpJRT5as0iqe9RFunqzIgSnnqnhu22tCfu2TxW2ejuQG1P XdI6iSGXdQGViDKaByUO8J/HU39CFUz6MfNQ4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer; b=abp9b8LyEWWudd7Tz6UcNG8DNBF9aibnKvJAA+rarwMLM7WLKVvMUIY7+VqAeaOLT3 nO716GhHm3aRkvjSS9c43Vmb3BRheQws2BlJuAJMx1P3vYfRNLmq5XGhyFQOsjNOWQmP gnoQRiz0X9cUH77R1xXLBYnIoljgeDC8IGPn0= Received: by 10.204.26.134 with SMTP id e6mr4384829bkc.87.1253547246802; Mon, 21 Sep 2009 08:34:06 -0700 (PDT) Received: from localhost ([41.238.122.8]) by mx.google.com with ESMTPS id b17sm247537fka.58.2009.09.21.08.34.03 (version=TLSv1/SSLv3 cipher=RC4-MD5); Mon, 21 Sep 2009 08:34:05 -0700 (PDT) From: Mohammed Gamal To: avi@redhat.com Cc: mtosatti@redhat.com, kvm@vger.kernel.org, Mohammed Gamal Subject: [PATCH] Add GDT, IDT and RFLAGS guest state validity checks Date: Mon, 21 Sep 2009 17:33:55 +0200 Message-Id: <1253547235-25348-1-git-send-email-m.gamal005@gmail.com> X-Mailer: git-send-email 1.6.0.4 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org With emulate_invalid_guest_state=1 Windows XP exits with an invalid guest state due to rflags not being in a VMX-compliant state. This patch fixes this issue, although Windows XP doesn't boot yet with invalid state emulation on. Also added GDT and IDT checks while we're at it. Signed-off-by: Mohammed Gamal --- arch/x86/kvm/vmx.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 56 insertions(+), 1 deletions(-) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index 3fe0d42..eaec4a5 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -2022,6 +2022,23 @@ static bool ldtr_valid(struct kvm_vcpu *vcpu) return true; } +static bool gdtr_idtr_valid(struct kvm_vcpu *vcpu) +{ + struct descriptor_table gdt; + struct descriptor_table idt; + + vmx_get_gdt(vcpu, &gdt); + vmx_get_idt(vcpu, &idt); + + if (gdt.limit & 0xffff0000) + return false; + + if (idt.limit & 0xffff0000) + return false; + + return true; +} + static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) { struct kvm_segment cs, ss; @@ -2033,6 +2050,41 @@ static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) (ss.selector & SELECTOR_RPL_MASK)); } +static bool rflags_valid(struct kvm_vcpu *vcpu) +{ + unsigned long rflags; + u32 entry_intr_info; + + rflags = vmcs_readl(GUEST_RFLAGS); + entry_intr_info = vmcs_read32(VM_ENTRY_INTR_INFO_FIELD); +#ifdef CONFIG_X86_64 + if (rflags & 0xffffffffffc00000) + return false; + if (is_long_mode(vcpu)) + if (rflags & X86_EFLAGS_VM) + return false; +#else + if (rflags & 0xffc00000) + return false; +#endif + if (rflags & 0x8000) + return false; + if (rflags & 0x20) + return false; + if (rflags & 0x8) + return false; + if (!(rflags & 0x2)) + return false; + + if ((entry_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_EXT_INTR + && (entry_intr_info & INTR_INFO_VALID_MASK)) { + if (!(rflags & X86_EFLAGS_IF)) + return false; + } + + return true; +} + /* * Check if guest state is valid. Returns true if valid, false if * not. @@ -2077,8 +2129,11 @@ static bool guest_state_valid(struct kvm_vcpu *vcpu) } /* TODO: * - Add checks on RIP - * - Add checks on RFLAGS */ + if (!rflags_valid(vcpu)) + return false; + if (!gdtr_idtr_valid(vcpu)) + return false; return true; }