From patchwork Tue Mar 19 01:46:24 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Krish Sadhukhan X-Patchwork-Id: 10858697 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 034DA1575 for ; Tue, 19 Mar 2019 02:12:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DEBEF294FE for ; Tue, 19 Mar 2019 02:12:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D24C5294FA; Tue, 19 Mar 2019 02:12:06 +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 59BE7294FA for ; Tue, 19 Mar 2019 02:12:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727393AbfCSCMF (ORCPT ); Mon, 18 Mar 2019 22:12:05 -0400 Received: from userp2120.oracle.com ([156.151.31.85]:51966 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726884AbfCSCME (ORCPT ); Mon, 18 Mar 2019 22:12:04 -0400 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 x2J24eXE027323; Tue, 19 Mar 2019 02:11:59 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=7fe/0qyEOFwxOKnFCN2nR8QNSsQuruUeB5yaBs8pMpc=; b=rFhFx94IdWsgfMXpEMKchWezJsNy/3PJiMGB6Q/GOVGQUfgpnpNNLjpV0k6tDo2CKYYp Q/TNO2fhkar8vQAHp4T7ULl6NOzes+8xsoKFyJ1DGXdgq8k13KkBXK2tFVYIKIzLofvQ zb0J+IQ5pxpMX7v+6FMLf7msAYBbOI8oPD2aq/u8oPVvG6wMUryb3jUiRslcycjqzCTM O1slP2ECZbCJ7uLFqBi7Wsuf423hp/tp5xkIvLUYXbarKIxq2DVA4Ezo5Ijr3aZm23U2 +s5GFuY+iCUNiIhVPgFBGvlUf+C25qUit3UPY4fh4qf2WJcOQuhnzEiMTH48Pud0Wrt9 ug== Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by userp2120.oracle.com with ESMTP id 2r8ssr9s9f-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 19 Mar 2019 02:11:58 +0000 Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by aserv0022.oracle.com (8.14.4/8.14.4) with ESMTP id x2J2BvJR001693 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 19 Mar 2019 02:11:58 GMT Received: from abhmp0005.oracle.com (abhmp0005.oracle.com [141.146.116.11]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id x2J2BvjJ013398; Tue, 19 Mar 2019 02:11:57 GMT Received: from ban25x6uut29.us.oracle.com (/10.153.73.29) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 18 Mar 2019 19:11:57 -0700 From: Krish Sadhukhan To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, rkrcmar@redhat.com, jmattson@google.com Subject: [PATCH 3/3][kvm-unit-test nVMX]: Check "load IA32_PAT" on vmentry of L2 guests Date: Mon, 18 Mar 2019 21:46:24 -0400 Message-Id: <20190319014624.31399-4-krish.sadhukhan@oracle.com> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20190319014624.31399-1-krish.sadhukhan@oracle.com> References: <20190319014624.31399-1-krish.sadhukhan@oracle.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9199 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-1903190014 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 1. According to section "Checks on Host Control Registers and MSRs" in Intel SDM vol 3C, the following check is performed on vmentry of L2 guests: 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-). 2. According to section "CHECKING AND LOADING GUEST STATE" in Intel SDM vol 3C, the following check is performed on vmentry of L2 guests: If the "load IA32_PAT" VM-entry 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-). Signed-off-by: Krish Sadhukhan Reviewed-by: Karl Heubaum --- x86/vmx_tests.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/x86/vmx_tests.c b/x86/vmx_tests.c index 66a87f6..7dae0f0 100644 --- a/x86/vmx_tests.c +++ b/x86/vmx_tests.c @@ -4995,6 +4995,90 @@ static void test_sysenter_field(u32 field, const char *name) vmcs_write(field, addr_saved); } +static void test_pat_fields(u64 vmcs_fld, const char * vmcs_fld_name, + u64 ctrl, u64 ctrl_fld) +{ + u32 ctrl_saved = vmcs_read(ctrl); + u64 vmcs_fld_saved = vmcs_read(vmcs_fld); + int i, j, error = 0; + u64 val; + + vmcs_write(ctrl, ctrl_saved & ~ctrl_fld); + for (i = 0; i < 256; i++) { + /* Test PAT0..PAT7 fields */ + for (j = 0; j < 8; j++) { + val = i << j * 8; + vmcs_write(vmcs_fld, val); + report_prefix_pushf("%s %lx", vmcs_fld_name, val); + test_vmx_vmlaunch(error, false); + report_prefix_pop(); + } + } + + vmcs_write(ctrl, ctrl_saved | ctrl_fld); + for (i = 0; i < 256; i++) { + /* Test PAT0..PAT7 fields */ + for (j = 0; j < 8; j++) { + val = i << j * 8; + vmcs_write(vmcs_fld, val); + report_prefix_pushf("%s %lx", vmcs_fld_name, val); + if (i == 0x2 || i == 0x3 || i > 0x7) { + if (vmcs_fld == HOST_PAT) + error = + VMXERR_ENTRY_INVALID_HOST_STATE_FIELD; + else + error = + VMXERR_ENTRY_INVALID_CONTROL_FIELD; + } else { + error = 0; + } + test_vmx_vmlaunch(error, false); + report_prefix_pop(); + } + } + + vmcs_write(ctrl, ctrl_saved); + vmcs_write(vmcs_fld, vmcs_fld_saved); +} + +/* + * 1. 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-). + * + * 2. If the "load IA32_PAT" VM-entry 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_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_fields(HOST_PAT, "HOST_PAT", EXI_CONTROLS, EXI_LOAD_PAT); + + /* + * "load IA32_PAT" VM-entry control + */ + if (!(ctrl_enter_rev.clr & ENT_LOAD_PAT)) { + printf("\"Load-IA32-PAT\" entry control not supported\n"); + return; + } + + test_pat_fields(GUEST_PAT, "GUEST_PAT", ENT_CONTROLS, ENT_LOAD_PAT); +} + /* * Check that the virtual CPU checks the VMX Host State Area as * documented in the Intel SDM. @@ -5010,6 +5094,9 @@ 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_pat(); + } static bool valid_vmcs_for_vmentry(void)