From patchwork Mon Feb 18 00:57:04 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Krish Sadhukhan X-Patchwork-Id: 10817265 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7E15F14E1 for ; Mon, 18 Feb 2019 01:22:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 61C772A22D for ; Mon, 18 Feb 2019 01:22:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 54FC62A234; Mon, 18 Feb 2019 01:22:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 25DC72A22D for ; Mon, 18 Feb 2019 01:22:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727451AbfBRBWc (ORCPT ); Sun, 17 Feb 2019 20:22:32 -0500 Received: from userp2120.oracle.com ([156.151.31.85]:50340 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726281AbfBRBWc (ORCPT ); Sun, 17 Feb 2019 20:22:32 -0500 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x1I1JF0s049640; Mon, 18 Feb 2019 01:22:26 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type : content-transfer-encoding; s=corp-2018-07-02; bh=YCNW1yI7jYVkcMyFWQYw0jZUcgcgObfNzN7/PdAK1Ac=; b=3hp3HBTK0/nR+N4P0emg0z1/fDfJ3JF0Y1IncD0GmXpOjgDg6VlXS0C1El3HdCDSVeqW 4cX62/CPn7qND5uKTf/5KCl60BEdH5KLxRb7CbO7Pf43ZsxOO/9TcjvpSOHR3E+1WgZ+ U6QnDYlJNazl5VZ4HzFLpFsjlDaQeJgIENdP7TMw2bBZ/3YikPbHBVdoxA+mB1ztHNdJ 6qs1z47UI+bU18Z95JL5RhU/VISOaZdwfjlhfZcHnYr+gRDk+xMQ0aoCi7pC2/msr7Ng UxfxaJs21PXkRVNkKLQvGE5JGyNF2O5pnZwW6wtvD0dvCiaXjFR9cqiUdVZ7G93UHOff hA== Received: from userv0022.oracle.com (userv0022.oracle.com [156.151.31.74]) by userp2120.oracle.com with ESMTP id 2qpb5r3j2v-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 18 Feb 2019 01:22:26 +0000 Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by userv0022.oracle.com (8.14.4/8.14.4) with ESMTP id x1I1MQLe011309 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 18 Feb 2019 01:22:26 GMT Received: from abhmp0018.oracle.com (abhmp0018.oracle.com [141.146.116.24]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id x1I1MQ6K017181; Mon, 18 Feb 2019 01:22:26 GMT Received: from ban25x6uut29.us.oracle.com (/10.153.73.29) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Sun, 17 Feb 2019 17:22:25 -0800 From: Krish Sadhukhan To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, rkrcmar@redhat.com, jmattson@google.com Subject: [PATCH v2][kvm-unit-test nVMX]: Check Host Control Registers on vmentry of L2 guests Date: Sun, 17 Feb 2019 19:57:04 -0500 Message-Id: <20190218005704.17699-2-krish.sadhukhan@oracle.com> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20190218005704.17699-1-krish.sadhukhan@oracle.com> References: <20190218005704.17699-1-krish.sadhukhan@oracle.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9170 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=893 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1902180009 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP According to section "Checks on VMX Controls" in Intel SDM vol 3C, the following checks are performed on vmentry of L2 guests: - The CR0 field must not set any bit to a value not supported in VMX operation. - The CR4 field must not set any bit to a value not supported in VMX operation. - On processors that support Intel 64 architecture, the CR3 field must be such that bits 63:52 and bits in the range 51:32 beyond the processor’s physical-address width must be 0. Signed-off-by: Krish Sadhukhan Reviewed-by: Liam Merwick --- lib/x86/processor.h | 1 + x86/vmx_tests.c | 94 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/lib/x86/processor.h b/lib/x86/processor.h index 15237a5..916e67d 100644 --- a/lib/x86/processor.h +++ b/lib/x86/processor.h @@ -40,6 +40,7 @@ #define X86_CR4_UMIP 0x00000800 #define X86_CR4_VMXE 0x00002000 #define X86_CR4_PCIDE 0x00020000 +#define X86_CR4_SMEP 0x00100000 #define X86_CR4_SMAP 0x00200000 #define X86_CR4_PKE 0x00400000 diff --git a/x86/vmx_tests.c b/x86/vmx_tests.c index 66a87f6..f5324ad 100644 --- a/x86/vmx_tests.c +++ b/x86/vmx_tests.c @@ -4995,6 +4995,98 @@ static void test_sysenter_field(u32 field, const char *name) vmcs_write(field, addr_saved); } +static void test_ctl_reg(const char *cr_name, u64 cr, u64 fixed0, u64 fixed1) +{ + u64 val; + u64 cr_saved = vmcs_read(cr); + int i; + + val = fixed0 & fixed1; + if (cr == HOST_CR4) + vmcs_write(cr, val | X86_CR4_PAE); + else + vmcs_write(cr, val); + report_prefix_pushf("%s %lx", cr_name, val); + if (val == fixed0) + test_vmx_vmlaunch(0, false); + else + test_vmx_vmlaunch(VMXERR_ENTRY_INVALID_HOST_STATE_FIELD, + false); + report_prefix_pop(); + + for (i = 0; i < 64; i++) { + + /* Set a bit when the corresponding bit in fixed1 is 0 */ + if ((fixed1 & (1ull << i)) == 0) { + if (cr == HOST_CR4 && ((1ull << i) & X86_CR4_SMEP || + (1ull << i) & X86_CR4_SMAP)) + continue; + + vmcs_write(cr, cr_saved | (1ull << i)); + report_prefix_pushf("%s %llx", cr_name, + cr_saved | (1ull << i)); + test_vmx_vmlaunch( + VMXERR_ENTRY_INVALID_HOST_STATE_FIELD, + false); + report_prefix_pop(); + } + + /* Unset a bit when the corresponding bit in fixed0 is 1 */ + if (fixed0 & (1ull << i)) { + vmcs_write(cr, cr_saved & ~(1ull << i)); + report_prefix_pushf("%s %llx", cr_name, + cr_saved & ~(1ull << i)); + test_vmx_vmlaunch( + VMXERR_ENTRY_INVALID_HOST_STATE_FIELD, + false); + report_prefix_pop(); + } + } + + vmcs_write(cr, cr_saved); +} + +/* + * 1. The CR0 field must not set any bit to a value not supported in VMX + * operation. + * 2. The CR4 field must not set any bit to a value not supported in VMX + * operation. + * 3. On processors that support Intel 64 architecture, the CR3 field must + * be such that bits 63:52 and bits in the range 51:32 beyond the + * processor’s physical-address width must be 0. + * + * [Intel SDM] + */ +static void test_host_ctl_regs(void) +{ + u64 fixed0, fixed1, cr3, cr3_saved; + int i; + + /* Test CR0 */ + fixed0 = rdmsr(MSR_IA32_VMX_CR0_FIXED0); + fixed1 = rdmsr(MSR_IA32_VMX_CR0_FIXED1); + test_ctl_reg("HOST_CR0", HOST_CR0, fixed0, fixed1); + + /* Test CR4 */ + fixed0 = rdmsr(MSR_IA32_VMX_CR4_FIXED0); + fixed1 = rdmsr(MSR_IA32_VMX_CR4_FIXED1) & + ~(X86_CR4_SMEP | X86_CR4_SMAP); + test_ctl_reg("HOST_CR4", HOST_CR4, fixed0, fixed1); + + /* Test CR3 */ + cr3_saved = vmcs_read(HOST_CR3); + for (i = cpuid_maxphyaddr(); i < 64; i++) { + cr3 = cr3_saved | (1ul << i); + vmcs_write(HOST_CR3, cr3); + report_prefix_pushf("HOST_CR3 %lx", cr3); + test_vmx_vmlaunch(VMXERR_ENTRY_INVALID_HOST_STATE_FIELD, + false); + report_prefix_pop(); + } + + vmcs_write(HOST_CR3, cr3_saved); +} + /* * Check that the virtual CPU checks the VMX Host State Area as * documented in the Intel SDM. @@ -5008,6 +5100,8 @@ static void vmx_host_state_area_test(void) */ vmcs_write(GUEST_RFLAGS, 0); + test_host_ctl_regs(); + test_sysenter_field(HOST_SYSENTER_ESP, "HOST_SYSENTER_ESP"); test_sysenter_field(HOST_SYSENTER_EIP, "HOST_SYSENTER_EIP"); }