From patchwork Fri Jun 29 19:07:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Krish Sadhukhan X-Patchwork-Id: 10497339 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 97CA360325 for ; Fri, 29 Jun 2018 19:07:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8591229912 for ; Fri, 29 Jun 2018 19:07:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7886529942; Fri, 29 Jun 2018 19:07:50 +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 03B4E29912 for ; Fri, 29 Jun 2018 19:07:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S936645AbeF2THp (ORCPT ); Fri, 29 Jun 2018 15:07:45 -0400 Received: from userp2120.oracle.com ([156.151.31.85]:59694 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S936626AbeF2THm (ORCPT ); Fri, 29 Jun 2018 15:07:42 -0400 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w5TJ3kFQ123805; Fri, 29 Jun 2018 19:07:28 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=subject : references : to : cc : from : message-id : date : mime-version : in-reply-to : content-type : content-transfer-encoding; s=corp-2017-10-26; bh=iy0czTK8CT8JO7XdaMDbph7bsTxbieaOXVc2V5VDa2M=; b=VuQshFPZJhCely2gk11j4zpV4vmCY6cJxYsmOSvTcLkdeUXw+PE1Xhb8fUzTbZ1Y2GeI E72f4f1XXQgj8pZNRzFbRPMZ/W807cnXnlY69gwevJ6kuLnHCajnKIf+/RWBZg9Bdmp1 XK0hsGanQUEeRDoIVwI96g2OXoD6GmOUUCXsYT9x4wlP1kMFSjDF2gupO7WJBoWHpKT+ pvr4n0iZapX1RkgDLPsxGCrpcBLuGA6NrTAwfJvMqHjy0cNgiZDPD7Iyyw5Z+eU3uTeq MiE2UIu62GSoRjBOV90lmuKpSxOyjNmSbAu/RRr0NmS2ziizj9ureexlfjtOeRzKbdxw 6g== Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by userp2120.oracle.com with ESMTP id 2jum0afmxq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 29 Jun 2018 19:07:28 +0000 Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by aserv0021.oracle.com (8.14.4/8.14.4) with ESMTP id w5TJ7R9V001207 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 29 Jun 2018 19:07:27 GMT Received: from abhmp0012.oracle.com (abhmp0012.oracle.com [141.146.116.18]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id w5TJ7QhH014208; Fri, 29 Jun 2018 19:07:27 GMT Received: from localhost.localdomain (/10.39.243.129) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 29 Jun 2018 12:07:26 -0700 Subject: [kvm-unit-test 1/2] nVMX x86: APIC virtual controls must be unset if "Use TPR shadow" is unset References: <20180615213908.1698-2-krish.sadhukhan@oracle.com> To: kvm Cc: Paolo Bonzini , Radim Krcmar , Jim Mattson From: Krish Sadhukhan X-Forwarded-Message-Id: <20180615213908.1698-2-krish.sadhukhan@oracle.com> Message-ID: Date: Fri, 29 Jun 2018 12:07:24 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.7.0 MIME-Version: 1.0 In-Reply-To: <20180615213908.1698-2-krish.sadhukhan@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8939 signatures=668703 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=973 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1806210000 definitions=main-1806290203 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 check needs to be enforced on vmentry of L2 guests: If the "use TPR shadow" VM-execution control is 0, the following VM-execution controls must also be 0: "virtualize x2APIC mode", "APIC-register virtualization" and "virtual-interrupt delivery". This unit-test validates the above vmentry check. Signed-off-by: Krish Sadhukhan Reviewed-by: Karl Heubaum Reviewed-by: Jim Mattson --- x86/vmx_tests.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 106 insertions(+), 2 deletions(-) diff --git a/x86/vmx_tests.c b/x86/vmx_tests.c index 38f10f4..64be48f 100644 --- a/x86/vmx_tests.c +++ b/x86/vmx_tests.c @@ -3580,6 +3580,111 @@ static void test_apic_access_addr(void) "virtualize APIC-accesses", true, false); } +/* + * If the "use TPR shadow" VM-execution control is 0, the following + * VM-execution controls must also be 0: + * - virtualize x2APIC mode + * - APIC-register virtualization + * - virtual-interrupt delivery + */ +static void test_apic_virtual_ctls(void) +{ + u32 saved_primary = vmcs_read(CPU_EXEC_CTRL0); + u32 saved_secondary = vmcs_read(CPU_EXEC_CTRL1); + u32 primary = saved_primary; + u32 secondary = saved_secondary; + bool cpu_has_virt_x2apic = 0; + bool cpu_has_apic_reg_virt = 0; + bool cpu_has_vintd = 0; + + if (!((ctrl_cpu_rev[0].clr & (CPU_SECONDARY | CPU_TPR_SHADOW)) == + (CPU_SECONDARY | CPU_TPR_SHADOW))) + return; + + if ((ctrl_cpu_rev[1].clr & CPU_VIRT_X2APIC) == CPU_VIRT_X2APIC) + cpu_has_virt_x2apic = 1; + if ((ctrl_cpu_rev[1].clr & CPU_APIC_REG_VIRT) == CPU_APIC_REG_VIRT) + cpu_has_apic_reg_virt = 1; + if ((ctrl_cpu_rev[1].clr & CPU_VINTD) == CPU_VINTD) + cpu_has_vintd = 1; + + primary |= CPU_SECONDARY; + vmcs_write(CPU_EXEC_CTRL0, primary & ~CPU_TPR_SHADOW); + + if (cpu_has_virt_x2apic) { + secondary &= ~(CPU_APIC_REG_VIRT | CPU_VINTD); + vmcs_write(CPU_EXEC_CTRL1, secondary | CPU_VIRT_X2APIC); + report_prefix_pushf("Use TPR shadow disabled; virtualize x2APIC mode enabled"); + test_vmx_controls(false, false); + report_prefix_pop(); + + if (cpu_has_apic_reg_virt) { + vmcs_write(CPU_EXEC_CTRL1, secondary | + CPU_APIC_REG_VIRT); + report_prefix_pushf("Use TPR shadow disabled; virtualize x2APIC mode enabled; APIC-register virtualization enabled"); + test_vmx_controls(false, false); + report_prefix_pop(); + } + + if (cpu_has_vintd) { + secondary &= ~CPU_APIC_REG_VIRT; + vmcs_write(CPU_EXEC_CTRL1, secondary | CPU_VINTD); + report_prefix_pushf("Use TPR shadow disabled; virtualize x2APIC mode enabled; virtual-interrupt delivery enabled"); + test_vmx_controls(false, false); + report_prefix_pop(); + } + } + + if (cpu_has_apic_reg_virt) { + secondary &= ~(~CPU_VIRT_X2APIC | CPU_VINTD); + vmcs_write(CPU_EXEC_CTRL1, secondary | CPU_APIC_REG_VIRT); + report_prefix_pushf("Use TPR shadow disabled; APIC-register virtualization enabled"); + test_vmx_controls(false, false); + report_prefix_pop(); + + if (cpu_has_vintd) { + secondary &= ~CPU_VIRT_X2APIC; + vmcs_write(CPU_EXEC_CTRL1, secondary | CPU_VINTD); + report_prefix_pushf("Use TPR shadow disabled; APIC-register virtualization enabled; virtual-interrupt delivery enabled"); + test_vmx_controls(false, false); + report_prefix_pop(); + } + } + + if (cpu_has_vintd) { + secondary &= ~(~CPU_VIRT_X2APIC | CPU_APIC_REG_VIRT); + vmcs_write(CPU_EXEC_CTRL1, secondary | CPU_VINTD); + report_prefix_pushf("Use TPR shadow disabled; virtual-interrupt delivery enabled"); + test_vmx_controls(false, false); + report_prefix_pop(); + } + + if (cpu_has_virt_x2apic && cpu_has_apic_reg_virt && + cpu_has_vintd) { + + vmcs_write(CPU_EXEC_CTRL1, secondary | (CPU_VIRT_X2APIC | + CPU_APIC_REG_VIRT | CPU_VINTD)); + report_prefix_pushf("Use TPR shadow disabled; virtualize x2APIC mode enabled; APIC-register virtualization enabled; virtual-interrupt delivery enabled"); + test_vmx_controls(false, false); + report_prefix_pop(); + + vmcs_write(CPU_EXEC_CTRL0, primary | CPU_TPR_SHADOW); + report_prefix_pushf("Use TPR shadow enabled; virtualize x2APIC mode enabled; APIC-register virtualization enabled; virtual-interrupt delivery enabled"); + test_vmx_controls(true, false); + report_prefix_pop(); + } + + vmcs_write(CPU_EXEC_CTRL0, saved_primary); + vmcs_write(CPU_EXEC_CTRL1, saved_secondary); +} + +static void test_apic_ctls(void) +{ + test_apic_virt_addr(); + test_apic_access_addr(); + test_apic_virtual_ctls(); +} + static void set_vtpr(unsigned vtpr) { *(u32 *)phys_to_virt(vmcs_read(APIC_VIRT_ADDR) + APIC_TASKPRI) = vtpr; @@ -3846,8 +3951,7 @@ static void vmx_controls_test(void) test_cr3_targets(); test_io_bitmaps(); test_msr_bitmap(); - test_apic_virt_addr(); - test_apic_access_addr(); + test_apic_ctls(); test_tpr_threshold(); test_nmi_ctrls(); }