From patchwork Fri Jun 22 23:35:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liran Alon X-Patchwork-Id: 10483315 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 ED78260388 for ; Fri, 22 Jun 2018 23:36:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DBDAA28FAF for ; Fri, 22 Jun 2018 23:36:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D064629065; Fri, 22 Jun 2018 23:36:36 +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 5E58A28FAF for ; Fri, 22 Jun 2018 23:36:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934222AbeFVXgd (ORCPT ); Fri, 22 Jun 2018 19:36:33 -0400 Received: from aserp2120.oracle.com ([141.146.126.78]:52632 "EHLO aserp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934348AbeFVXg2 (ORCPT ); Fri, 22 Jun 2018 19:36:28 -0400 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w5MNaD13157167; Fri, 22 Jun 2018 23:36:13 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; s=corp-2017-10-26; bh=vzs4oOKpDiCs8iyX5KvIJdbkIguwtG5KtrWfoVITrmc=; b=QyAc1g9p9wlV8doBvGPZ9glKVn4hzVUTKwjH/csZfPCg2lFlx58nusAe1kb6LXYUiV1h QCtVUm9wjIi44paF8xSvKN/hzIQIFCMP3SKPLUzOeaRs0V5u470wRkGe7c3WkPiMqLoY ejtu9j3L4/0p+7hYo9B4FhhtQXCkMj80VQeeXM9ts8Yw2cSLrd+65lpAbCYT5tHIDMt9 yMBMihnU6WX3p5/GZXF34k4jVmKZ7X/EZgmNSFYPj/i/jxMxab1k8Fy0oPIeA5uPBrnU iO5cfPTRQSBs8Ofc8MfQ/v5uvVq53B4AkONsfGeq2OcGqxyzH4y5jZKjRDC1RNJKm5da dA== Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by aserp2120.oracle.com with ESMTP id 2jrp8huqcs-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 22 Jun 2018 23:36:13 +0000 Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserv0021.oracle.com (8.14.4/8.14.4) with ESMTP id w5MNaD5i025965 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 22 Jun 2018 23:36:13 GMT Received: from abhmp0008.oracle.com (abhmp0008.oracle.com [141.146.116.14]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id w5MNaDPb010346; Fri, 22 Jun 2018 23:36:13 GMT Received: from liran-pc.mynet (/79.183.72.72) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 22 Jun 2018 16:36:12 -0700 From: Liran Alon To: pbonzini@redhat.com, rkrcmar@redhat.com, kvm@vger.kernel.org Cc: jmattson@google.com, idan.brown@oracle.com, Liran Alon Subject: [PATCH 20/22] KVM: nVMX: Add util to create vmcs02 vmread/vmwrite bitmaps Date: Sat, 23 Jun 2018 02:35:20 +0300 Message-Id: <1529710522-28315-21-git-send-email-liran.alon@oracle.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1529710522-28315-1-git-send-email-liran.alon@oracle.com> References: <1529710522-28315-1-git-send-email-liran.alon@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8932 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=888 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1806210000 definitions=main-1806220260 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP These bitmaps are based on vmcs12 vmread/vmwrite bitmaps and which VMCS fields are supported by vmcs12 & physical CPU. This is done as a preparation for VMCS shadowing virtualization. Signed-off-by: Liran Alon Signed-off-by: Jim Mattson --- arch/x86/kvm/vmx.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index e3f3bb5102f0..94922adf6f47 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -11162,6 +11162,103 @@ static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, return true; } +/* + * For all valid VMCS fields in a group (having the same width and + * type), clear the permission bit in the vmcs02 bitmap if it is clear + * in the vmcs12 bitmap and the field is supported by kvm and the + * physical CPU. All other permission bits are set. + */ +static void nested_vmx_setup_shadow_bitmap_group(struct kvm_vcpu *vcpu, + unsigned long *vmcs02_bitmap, + unsigned long *vmcs12_bitmap, + unsigned long base) +{ + unsigned long offset = base / BITS_PER_LONG; + unsigned long val = ~0l; + unsigned long bit; + struct vcpu_vmx *vmx = to_vmx(vcpu); + + for_each_clear_bit(bit, vmcs12_bitmap + offset, + nested_vmcs_fields_per_group(vmx)) { + unsigned long field = base + bit; + + if (vmcs_field_to_offset(field) >= 0 && + cpu_has_vmcs_field(field)) + val &= ~(1ul << bit); + } + + vmcs02_bitmap[offset] = val; +} + +/* + * Set up a vmcs02 vmread/vmwrite bitmap based on a vmcs12 + * vmread/vmwrite bitmap. Only fields supported by kvm and the + * physical CPU may be shadowed (i.e. have a clear permission bit). + */ +static bool nested_vmx_setup_shadow_bitmap(struct kvm_vcpu *vcpu, + unsigned long *vmcs02_bitmap, + gpa_t vmcs12_bitmap_gpa, + bool is_write_bitmap) +{ + struct page *page; + unsigned long *vmcs12_bitmap; + enum vmcs_field_width width; + enum vmcs_field_type type; + bool intercept_read_only_fields; + + page = kvm_vcpu_gpa_to_page(vcpu, vmcs12_bitmap_gpa); + if (is_error_page(page)) + return false; + + /* + * If L1 cannot VMWRITE into read-only VMCS fields, + * then L0 needs to intercept VMWRITEs to these fields + * in order to simulate VMWRITE failure for L2. + */ + intercept_read_only_fields = + is_write_bitmap && + !nested_cpu_has_vmwrite_any_field(vcpu); + + vmcs12_bitmap = (unsigned long *)kmap(page); + for (width = 0; width < VMCS_FIELD_WIDTHS; width++) { + for (type = 0; type < VMCS_FIELD_TYPES; type++) { + unsigned long base = encode_vmcs_field(width, type, 0, false); + if (intercept_read_only_fields && + (type == VMCS_FIELD_TYPE_READ_ONLY_DATA)) + vmcs02_bitmap[base / BITS_PER_LONG] = ~0l; + else + nested_vmx_setup_shadow_bitmap_group(vcpu, + vmcs02_bitmap, + vmcs12_bitmap, + base); + } + } + kunmap(page); + + kvm_release_page_clean(page); + + return true; +} + +/* + * Set up the vmcs02 vmread and vmwrite permission bitmaps based on + * the corresponding vmcs12 permission bitmaps. + */ +static inline bool nested_vmx_setup_shadow_bitmaps(struct kvm_vcpu *vcpu, + struct vmcs12 *vmcs12) +{ + struct vcpu_vmx *vmx = to_vmx(vcpu); + + return nested_vmx_setup_shadow_bitmap(vcpu, + vmx->nested.vmread_bitmap, + vmcs12->vmread_bitmap, + false) && + nested_vmx_setup_shadow_bitmap(vcpu, + vmx->nested.vmwrite_bitmap, + vmcs12->vmwrite_bitmap, + true); +} + static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) {