From patchwork Wed Apr 3 21:59:05 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Krish Sadhukhan X-Patchwork-Id: 10884609 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 F1F53184E for ; Wed, 3 Apr 2019 22:25:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D948F288BF for ; Wed, 3 Apr 2019 22:25:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CD58A2875F; Wed, 3 Apr 2019 22:25:08 +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 588542875F for ; Wed, 3 Apr 2019 22:25:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726372AbfDCWZH (ORCPT ); Wed, 3 Apr 2019 18:25:07 -0400 Received: from aserp2130.oracle.com ([141.146.126.79]:34450 "EHLO aserp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726206AbfDCWZG (ORCPT ); Wed, 3 Apr 2019 18:25:06 -0400 Received: from pps.filterd (aserp2130.oracle.com [127.0.0.1]) by aserp2130.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x33MOPmR031160; Wed, 3 Apr 2019 22:25:00 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=Lz+V9RLcVtcsap5/4cDtp6hY6/qB/tpHKNdHpxeWVy4=; b=dQh+USOg6N6sLmzqtrQP1J9+YREr7eBT0S2jnwIZ466mPrC+vutWkN6OBVKlljK30GRZ 9/cIs9iz1/vhihN8iDYDdvMdHg7p7vqmPlxkCVc1FUCoQBN3DS7AK4ZydowrcG5YVcXG eWi6nmJct/xv61YDlvCiGsOHoTjhIitcb+pTF33Cvkjq+blJOZCaf8jusiT7TsgerDyu 5AY2ZXs+Yw6TCfvsx1tw3n7pbWYQLusCj6Gl2XscqazQ1w3fWvZmTZbM4jB7zUkM95E7 Y1eIU+DJYHYHFenrOfi8F2A29yir5NQiil6zyMzH/mMOusHML1m7I5rcz4P47OVLRq3h 3A== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by aserp2130.oracle.com with ESMTP id 2rhwydc05n-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 03 Apr 2019 22:25:00 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x33MNLaX044456; Wed, 3 Apr 2019 22:25:00 GMT Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserp3020.oracle.com with ESMTP id 2rm9mjakwx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 03 Apr 2019 22:25:00 +0000 Received: from abhmp0012.oracle.com (abhmp0012.oracle.com [141.146.116.18]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id x33MOxhQ013930; Wed, 3 Apr 2019 22:24:59 GMT Received: from ban25x6uut29.us.oracle.com (/10.153.73.29) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 03 Apr 2019 15:24:59 -0700 From: Krish Sadhukhan To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, rkrcmar@redhat.com, jmattson@google.com Subject: [PATCH 4/4 v3][kvm-unit-test nVMX]: Check "load IA32_PAT" on vmentry of L2 guests Date: Wed, 3 Apr 2019 17:59:05 -0400 Message-Id: <20190403215905.2474-5-krish.sadhukhan@oracle.com> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20190403215905.2474-1-krish.sadhukhan@oracle.com> References: <20190403215905.2474-1-krish.sadhukhan@oracle.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9216 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=1 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1904030148 X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9216 signatures=668685 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=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1904030148 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP .to verify KVM performs the appropriate consistency checks for loading IA32_PAT as part of running a nested guest. According to section "Checks on Host Control Registers and MSRs" in Intel SDM vol 3C, the following check is performed on vmentry: If the “load IA32_PAT” VM-exit control is 1, the value of the field for the IA32_PAT MSR must be one that could be written by WRMSR without fault at CPL 0. Specifically, each of the 8 bytes in the field must have one of the values 0 (UC), 1 (WC), 4 (WT), 5 (WP), 6 (WB), or 7 (UC-). Since a PAT value higher than 8 will yield the same test result as that of 8, we want to confine our tests only up to 8 in order to reduce redundancy of tests and to avoid too many vmentries. Signed-off-by: Krish Sadhukhan Reviewed-by: Karl Heubaum --- x86/vmx_tests.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/x86/vmx_tests.c b/x86/vmx_tests.c index 66a87f6..04b1aee 100644 --- a/x86/vmx_tests.c +++ b/x86/vmx_tests.c @@ -4995,6 +4995,75 @@ static void test_sysenter_field(u32 field, const char *name) vmcs_write(field, addr_saved); } +/* + * Since a PAT value higher than 8 will yield the same test result as that + * of 8, we want to confine our tests only up to 8 in order to reduce + * redundancy of tests and to avoid too many vmentries. + */ +#define PAT_VAL_LIMIT 8 + +static void test_pat(u32 fld, const char * fld_name, u32 ctrl_fld, u64 ctrl_bit) +{ + u32 ctrl_saved = vmcs_read(ctrl_fld); + u64 pat_saved = vmcs_read(fld); + u64 i, val; + u32 j; + int error; + + vmcs_write(ctrl_fld, ctrl_saved & ~ctrl_bit); + for (i = 0; i <= PAT_VAL_LIMIT; i++) { + /* Test PAT0..PAT7 fields */ + for (j = 0; j < 8; j++) { + val = i << j * 8; + vmcs_write(fld, val); + report_prefix_pushf("%s %lx", fld_name, val); + test_vmx_vmlaunch(0, false); + report_prefix_pop(); + } + } + + vmcs_write(ctrl_fld, ctrl_saved | ctrl_bit); + for (i = 0; i <= PAT_VAL_LIMIT; i++) { + /* Test PAT0..PAT7 fields */ + for (j = 0; j < 8; j++) { + val = i << j * 8; + vmcs_write(fld, val); + report_prefix_pushf("%s %lx", fld_name, val); + if (i == 0x2 || i == 0x3 || i >= 0x8) + error = VMXERR_ENTRY_INVALID_HOST_STATE_FIELD; + else + error = 0; + test_vmx_vmlaunch(error, false); + report_prefix_pop(); + } + } + + vmcs_write(ctrl_fld, ctrl_saved); + vmcs_write(fld, pat_saved); +} + +/* + * If the "load IA32_PAT" VM-exit control is 1, the value of the field + * for the IA32_PAT MSR must be one that could be written by WRMSR + * without fault at CPL 0. Specifically, each of the 8 bytes in the + * field must have one of the values 0 (UC), 1 (WC), 4 (WT), 5 (WP), + * 6 (WB), or 7 (UC-). + * + * [Intel SDM] + */ +static void test_load_host_pat(void) +{ + /* + * "load IA32_PAT" VM-exit control + */ + if (!(ctrl_exit_rev.clr & EXI_LOAD_PAT)) { + printf("\"Load-IA32-PAT\" exit control not supported\n"); + return; + } + + test_pat(HOST_PAT, "HOST_PAT", EXI_CONTROLS, EXI_LOAD_PAT); +} + /* * Check that the virtual CPU checks the VMX Host State Area as * documented in the Intel SDM. @@ -5010,6 +5079,8 @@ static void vmx_host_state_area_test(void) test_sysenter_field(HOST_SYSENTER_ESP, "HOST_SYSENTER_ESP"); test_sysenter_field(HOST_SYSENTER_EIP, "HOST_SYSENTER_EIP"); + + test_load_host_pat(); } static bool valid_vmcs_for_vmentry(void)