From patchwork Sun May 9 20:45:02 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mohammed Gamal X-Patchwork-Id: 98027 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.3/8.14.3) with ESMTP id o49KjuXT012909 for ; Sun, 9 May 2010 20:45:56 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752155Ab0EIUpX (ORCPT ); Sun, 9 May 2010 16:45:23 -0400 Received: from mail-wy0-f174.google.com ([74.125.82.174]:37120 "EHLO mail-wy0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750824Ab0EIUpT (ORCPT ); Sun, 9 May 2010 16:45:19 -0400 Received: by wyf19 with SMTP id 19so447999wyf.19 for ; Sun, 09 May 2010 13:45:17 -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=/m/sZgSaisrB7XxyWeSfDsiagAIBntFLT9hwK3hvW7I=; b=A6lKR8fCwcLwnEIA7ENVkmvoq8JW4qb+hMIvXKGJds7vu9VO+XNl5Hg3IH6e+gZgtS kmv0hUNNAosCKfmzvYgWC9VUBQGhpu9kMkeLGauDSs9/S3t66kc3QsaUq4pCT6xDhtq+ BOLOH2Fi1Or9Wo91nNMgvY4qpnJw9eq9AMQr0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer; b=QhP6O9LKaBNYN2TAtQ4WTFl5/55/3muevyKIxBakhSjhyX0xVVEj7qQpTOPJI8vLXe 5SL0wo8ojllR3TglFgQ6Yn8Lq5ZnFJ6G3+ZGHgKlfpNNdSZJ8y+bSJPe2fO+YaXQC+2n Gi7QPmljlLhQ5sM6UuQf9b568AlN6wLiI8gLo= Received: by 10.227.141.79 with SMTP id l15mr2891058wbu.57.1273437917248; Sun, 09 May 2010 13:45:17 -0700 (PDT) Received: from localhost ([41.237.151.24]) by mx.google.com with ESMTPS id u5sm1113405wbc.14.2010.05.09.13.45.14 (version=TLSv1/SSLv3 cipher=RC4-MD5); Sun, 09 May 2010 13:45:16 -0700 (PDT) From: Mohammed Gamal To: avi@redhat.com Cc: kvm@vger.kernel.org, Mohammed Gamal Subject: [PATCH] VMX: Invalid guest state detection enhancements and bug fixes Date: Sun, 9 May 2010 23:45:02 +0300 Message-Id: <1273437902-31474-1-git-send-email-m.gamal005@gmail.com> X-Mailer: git-send-email 1.7.0.4 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Sun, 09 May 2010 20:45:56 +0000 (UTC) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index 777e00d..f736008 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -2122,7 +2122,7 @@ static bool stack_segment_valid(struct kvm_vcpu *vcpu) ss_rpl = ss.selector & SELECTOR_RPL_MASK; if (ss.unusable) - return true; + return false; if (ss.type != 3 && ss.type != 7) return false; if (!ss.s) @@ -2144,7 +2144,7 @@ static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) rpl = var.selector & SELECTOR_RPL_MASK; if (var.unusable) - return true; + return false; if (!var.s) return false; if (!var.present) @@ -2185,7 +2185,7 @@ static bool ldtr_valid(struct kvm_vcpu *vcpu) vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); if (ldtr.unusable) - return true; + return false; if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */ return false; if (ldtr.type != 2) @@ -2196,6 +2196,23 @@ static bool ldtr_valid(struct kvm_vcpu *vcpu) return true; } +static bool gdtr_idtr_valid(struct kvm_vcpu *vcpu) +{ + struct desc_ptr gdt; + struct desc_ptr idt; + + vmx_get_gdt(vcpu, &gdt); + vmx_get_idt(vcpu, &idt); + + if (gdt.size & 0xffff0000) + return false; + + if (idt.size & 0xffff0000) + return false; + + return true; +} + static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) { struct kvm_segment cs, ss; @@ -2207,6 +2224,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. @@ -2251,8 +2303,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; } @@ -3559,6 +3614,7 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) } if (err != EMULATE_DONE) { + kvm_report_emulation_failure(vcpu, "invalid guest state handler"); vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; vcpu->run->internal.ndata = 0;