Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp1885781pxv; Fri, 2 Jul 2021 15:09:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxBpBMP9c1ZoppeVKVVvSNuctxQD+EL0IQ+Knz/R4lv+GZfmiRMaMEo11OM24K4Y46mGTdl X-Received: by 2002:a17:907:9495:: with SMTP id dm21mr1896849ejc.526.1625263765779; Fri, 02 Jul 2021 15:09:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625263765; cv=none; d=google.com; s=arc-20160816; b=sGTFnas+JU5qCJAdIes2XC1e3tBkzOT/ZyN6X9kZ7W08CQuM9VyXrU8yysWs7cMnLe ylWc5BeavRUT5vmQ213hNnCIBcvtjuEDStR/Zd0UPC5KYh+pOPOoUql1i44LfpenfQ/O PLlY50kW23H2u0bfHlqDvojMFdXmPV+yKqBDW2LL4GRum6dzxIeGwZEPry6VLGk/UzNh QZQg1btubo+pFquQpKPjW7kfUL0++EkayvF779oO7ixsxbU+6u8+KvE8pgGWzKkHu6or iX81ZwDevbG03TMyzrXd5SfRTrND997kU4TXOf0xG7jkpeXsenIf7UwZig4NWNNUCZoi xoSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=CsW5DYuZaPMCz3ESiiMnUZpiDQOpmdIYurt1orYLvBw=; b=HWkwJm0xPmu0KGqJp5WKFP3fY7gmQVLggMGQnDAJcO9h26RxQXMXRzk8PHw+qb8a5z JK48/Q218mB9IAaCKzFALs8E1UoUeKoqq9AgYcPNmEZY6t61B69oeyldGe5+647EDWcG Nj+LK/fdAP2FbBvh2FWgmWpu7NUY9kSZjmoL5EL0AEapc8hvkojNyRyfIsXSEi5KB37f c8lpIQJpbZ47LhVBOkwH6gLXT5PsF8nYb/DvLdwZV55Rv6VbQdrP+qOm3g6tB9lEQ+Ej 7nEBp1/n226hPnXfzkDTtCtBCzJqpfTPFfdswT+0FrVxHBNerdatbmfTKnx3zp7+J2bm egog== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s3si4461631ejs.220.2021.07.02.15.09.02; Fri, 02 Jul 2021 15:09:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234420AbhGBWJ7 (ORCPT + 99 others); Fri, 2 Jul 2021 18:09:59 -0400 Received: from mga17.intel.com ([192.55.52.151]:15277 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233843AbhGBWIi (ORCPT ); Fri, 2 Jul 2021 18:08:38 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10033"; a="189168406" X-IronPort-AV: E=Sophos;i="5.83,320,1616482800"; d="scan'208";a="189168406" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Jul 2021 15:05:31 -0700 X-IronPort-AV: E=Sophos;i="5.83,320,1616482800"; d="scan'208";a="642814883" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Jul 2021 15:05:31 -0700 From: isaku.yamahata@intel.com To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H . Peter Anvin" , Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , erdemaktas@google.com, Connor Kuehl , Sean Christopherson , x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Sean Christopherson Subject: [RFC PATCH v2 61/69] KVM: VMX: Move AR_BYTES encoder/decoder helpers to common.h Date: Fri, 2 Jul 2021 15:05:07 -0700 Message-Id: <847069aafe640a360007a4c531930e34945e6417.1625186503.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Christopherson Move the AR_BYTES helpers to common.h so that future patches can reuse them to decode/encode AR for TDX. Signed-off-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/x86/kvm/vmx/common.h | 41 ++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 47 ++++----------------------------------- 2 files changed, 45 insertions(+), 43 deletions(-) diff --git a/arch/x86/kvm/vmx/common.h b/arch/x86/kvm/vmx/common.h index aa6a569b87d1..755aaec85199 100644 --- a/arch/x86/kvm/vmx/common.h +++ b/arch/x86/kvm/vmx/common.h @@ -4,6 +4,7 @@ #include +#include #include #include @@ -119,4 +120,44 @@ static inline int __vmx_handle_ept_violation(struct kvm_vcpu *vcpu, gpa_t gpa, return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); } +static inline u32 vmx_encode_ar_bytes(struct kvm_segment *var) +{ + u32 ar; + + if (var->unusable || !var->present) + ar = 1 << 16; + else { + ar = var->type & 15; + ar |= (var->s & 1) << 4; + ar |= (var->dpl & 3) << 5; + ar |= (var->present & 1) << 7; + ar |= (var->avl & 1) << 12; + ar |= (var->l & 1) << 13; + ar |= (var->db & 1) << 14; + ar |= (var->g & 1) << 15; + } + + return ar; +} + +static inline void vmx_decode_ar_bytes(u32 ar, struct kvm_segment *var) +{ + var->unusable = (ar >> 16) & 1; + var->type = ar & 15; + var->s = (ar >> 4) & 1; + var->dpl = (ar >> 5) & 3; + /* + * Some userspaces do not preserve unusable property. Since usable + * segment has to be present according to VMX spec we can use present + * property to amend userspace bug by making unusable segment always + * nonpresent. vmx_encode_ar_bytes() already marks nonpresent + * segment as unusable. + */ + var->present = !var->unusable; + var->avl = (ar >> 12) & 1; + var->l = (ar >> 13) & 1; + var->db = (ar >> 14) & 1; + var->g = (ar >> 15) & 1; +} + #endif /* __KVM_X86_VMX_COMMON_H */ diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 3c3bfc80d2bb..40843ca2fb33 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -365,8 +365,6 @@ static const struct kernel_param_ops vmentry_l1d_flush_ops = { }; module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644); -static u32 vmx_segment_access_rights(struct kvm_segment *var); - void vmx_vmexit(void); #define vmx_insn_failed(fmt...) \ @@ -2826,7 +2824,7 @@ static void fix_rmode_seg(int seg, struct kvm_segment *save) vmcs_write16(sf->selector, var.selector); vmcs_writel(sf->base, var.base); vmcs_write32(sf->limit, var.limit); - vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); + vmcs_write32(sf->ar_bytes, vmx_encode_ar_bytes(&var)); } static void enter_rmode(struct kvm_vcpu *vcpu) @@ -3217,7 +3215,6 @@ void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) { struct vcpu_vmx *vmx = to_vmx(vcpu); - u32 ar; if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { *var = vmx->rmode.segs[seg]; @@ -3231,23 +3228,7 @@ void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) var->base = vmx_read_guest_seg_base(vmx, seg); var->limit = vmx_read_guest_seg_limit(vmx, seg); var->selector = vmx_read_guest_seg_selector(vmx, seg); - ar = vmx_read_guest_seg_ar(vmx, seg); - var->unusable = (ar >> 16) & 1; - var->type = ar & 15; - var->s = (ar >> 4) & 1; - var->dpl = (ar >> 5) & 3; - /* - * Some userspaces do not preserve unusable property. Since usable - * segment has to be present according to VMX spec we can use present - * property to amend userspace bug by making unusable segment always - * nonpresent. vmx_segment_access_rights() already marks nonpresent - * segment as unusable. - */ - var->present = !var->unusable; - var->avl = (ar >> 12) & 1; - var->l = (ar >> 13) & 1; - var->db = (ar >> 14) & 1; - var->g = (ar >> 15) & 1; + vmx_decode_ar_bytes(vmx_read_guest_seg_ar(vmx, seg), var); } static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) @@ -3273,26 +3254,6 @@ int vmx_get_cpl(struct kvm_vcpu *vcpu) } } -static u32 vmx_segment_access_rights(struct kvm_segment *var) -{ - u32 ar; - - if (var->unusable || !var->present) - ar = 1 << 16; - else { - ar = var->type & 15; - ar |= (var->s & 1) << 4; - ar |= (var->dpl & 3) << 5; - ar |= (var->present & 1) << 7; - ar |= (var->avl & 1) << 12; - ar |= (var->l & 1) << 13; - ar |= (var->db & 1) << 14; - ar |= (var->g & 1) << 15; - } - - return ar; -} - void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) { struct vcpu_vmx *vmx = to_vmx(vcpu); @@ -3327,7 +3288,7 @@ void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) if (is_unrestricted_guest(vcpu) && (seg != VCPU_SREG_LDTR)) var->type |= 0x1; /* Accessed */ - vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); + vmcs_write32(sf->ar_bytes, vmx_encode_ar_bytes(var)); out: vmx->emulation_required = emulation_required(vcpu); @@ -3374,7 +3335,7 @@ static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) var.dpl = 0x3; if (seg == VCPU_SREG_CS) var.type = 0x3; - ar = vmx_segment_access_rights(&var); + ar = vmx_encode_ar_bytes(&var); if (var.base != (var.selector << 4)) return false; -- 2.25.1