From patchwork Mon Nov 18 18:15:09 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 11250097 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6A8FE109A for ; Mon, 18 Nov 2019 18:16:41 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 46935222AC for ; Mon, 18 Nov 2019 18:16:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=citrix.com header.i=@citrix.com header.b="g9Nyz68e" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 46935222AC Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=citrix.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iWlYQ-0004ke-Tj; Mon, 18 Nov 2019 18:15:14 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iWlYP-0004kX-SH for xen-devel@lists.xenproject.org; Mon, 18 Nov 2019 18:15:13 +0000 X-Inumbo-ID: 5ccc0d0e-0a2f-11ea-a2df-12813bfff9fa Received: from esa6.hc3370-68.iphmx.com (unknown [216.71.155.175]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 5ccc0d0e-0a2f-11ea-a2df-12813bfff9fa; Mon, 18 Nov 2019 18:15:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1574100912; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=mkPlKTunlXNe+agMkPgBLYGzKeTsrGcjq7BjfJnrZ5o=; b=g9Nyz68eqiM1WAdIvt3H9V84CA+OitMz+r40oMSPVN0gOthDprW76hWa Eu4jgFHvFuyfBwalhfmxEEI96Th8Z9OoowCmsKSf6wnDXy2YBL/b+acV0 38nXjALbS9v94Suw0dAIReEHB4gckjHLRIXJ2WKEaN/OPQQUG1YGqyzo9 A=; Authentication-Results: esa6.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none; spf=None smtp.pra=andrew.cooper3@citrix.com; spf=Pass smtp.mailfrom=Andrew.Cooper3@citrix.com; spf=None smtp.helo=postmaster@mail.citrix.com Received-SPF: None (esa6.hc3370-68.iphmx.com: no sender authenticity information available from domain of andrew.cooper3@citrix.com) identity=pra; client-ip=162.221.158.21; receiver=esa6.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="andrew.cooper3@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa6.hc3370-68.iphmx.com: domain of Andrew.Cooper3@citrix.com designates 162.221.158.21 as permitted sender) identity=mailfrom; client-ip=162.221.158.21; receiver=esa6.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="Andrew.Cooper3@citrix.com"; x-conformance=sidf_compatible; x-record-type="v=spf1"; x-record-text="v=spf1 ip4:209.167.231.154 ip4:178.63.86.133 ip4:195.66.111.40/30 ip4:85.115.9.32/28 ip4:199.102.83.4 ip4:192.28.146.160 ip4:192.28.146.107 ip4:216.52.6.88 ip4:216.52.6.188 ip4:162.221.158.21 ip4:162.221.156.83 ip4:168.245.78.127 ~all" Received-SPF: None (esa6.hc3370-68.iphmx.com: no sender authenticity information available from domain of postmaster@mail.citrix.com) identity=helo; client-ip=162.221.158.21; receiver=esa6.hc3370-68.iphmx.com; envelope-from="Andrew.Cooper3@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: WQWGQBZQJNbe5EFt3YvowA4zxcxZASSzysr94pFCTMyejBB1gB/bycwmErk9uCpUO8ZzXU1Lnd YUMevPIel3t4zQs8GKHyZw2cjPsAg8NiFdhHBtUDrrGufnUz6JHLh0d5MTgoOnnquDnfgbZ3Fo BJeJtBE92pKLPYiscV372jzYGz6rXSCXKg6HvASPwK0mNlOCUVrSTw1oJtz+NM33amPp5rwIMc DIPdz9DqnN45/p+e5BOt8+3qDjvCcEgSoszl9SHncwO61e3cA5WxFX7XOB1b30LT4u759rQlxC ukk= X-SBRS: 2.7 X-MesageID: 8892082 X-Ironport-Server: esa6.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.68,321,1569297600"; d="scan'208";a="8892082" From: Andrew Cooper To: Xen-devel Date: Mon, 18 Nov 2019 18:15:09 +0000 Message-ID: <20191118181509.10981-1-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 MIME-Version: 1.0 Subject: [Xen-devel] [PATCH] x86/nested-hap: Fix handling of L0_ERROR X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Juergen Gross , Wei Liu , George Dunlap , Andrew Cooper , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" When nestedhvm_hap_nested_page_fault() returns L0_ERROR, hvm_hap_nested_page_fault() operates on the adjusted gpa. However, it operates with the original npfec, which is no longer be correct. In particular, it is possible to get a nested fault where the translation is not present in L12 (and therefore L02), while it is present in L01. When handling an L0_ERROR, adjust npfec as well as gpa. Signed-off-by: Andrew Cooper --- CC: Jan Beulich CC: Wei Liu CC: Roger Pau Monné CC: George Dunlap CC: Juergen Gross This is one part of fixing nested virt in light of XSA-304. Combined with: diff --git a/xen/arch/x86/mm/hap/nested_hap.c b/xen/arch/x86/mm/hap/nested_hap.c index a509a40c93..bd58a86b46 100644 --- a/xen/arch/x86/mm/hap/nested_hap.c +++ b/xen/arch/x86/mm/hap/nested_hap.c @@ -167,7 +167,8 @@ nestedhap_walk_L0_p2m(struct p2m_domain *p2m, paddr_t L1_gpa, paddr_t *L0_gpa, goto out; rc = NESTEDHVM_PAGEFAULT_L0_ERROR; - if ( access_w && p2m_is_readonly(*p2mt) ) + if ( (access_w && p2m_is_readonly(*p2mt)) || + (access_x && *page_order) ) goto out; if ( p2m_is_paging(*p2mt) || p2m_is_shared(*p2mt) || !p2m_is_ram(*p2mt) ) it does resolve the issue. However, the above isn't correct in the general case, and is still under development. --- xen/arch/x86/hvm/hvm.c | 6 ++---- xen/arch/x86/mm/hap/nested_hap.c | 27 +++++++++++++++++++-------- xen/include/asm-x86/hvm/nestedhvm.h | 2 +- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index 818e705fd1..87eed13ee1 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -1729,10 +1729,8 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla, * the same as for shadow paging. */ - rv = nestedhvm_hap_nested_page_fault(curr, &gpa, - npfec.read_access, - npfec.write_access, - npfec.insn_fetch); + rv = nestedhvm_hap_nested_page_fault(curr, &gpa, &npfec); + switch (rv) { case NESTEDHVM_PAGEFAULT_DONE: case NESTEDHVM_PAGEFAULT_RETRY: diff --git a/xen/arch/x86/mm/hap/nested_hap.c b/xen/arch/x86/mm/hap/nested_hap.c index abe5958a52..9eba35f7e8 100644 --- a/xen/arch/x86/mm/hap/nested_hap.c +++ b/xen/arch/x86/mm/hap/nested_hap.c @@ -149,8 +149,7 @@ nestedhap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa, static int nestedhap_walk_L0_p2m(struct p2m_domain *p2m, paddr_t L1_gpa, paddr_t *L0_gpa, p2m_type_t *p2mt, p2m_access_t *p2ma, - unsigned int *page_order, - bool_t access_r, bool_t access_w, bool_t access_x) + unsigned int *page_order, struct npfec *npfec) { mfn_t mfn; int rc; @@ -167,7 +166,7 @@ nestedhap_walk_L0_p2m(struct p2m_domain *p2m, paddr_t L1_gpa, paddr_t *L0_gpa, goto out; rc = NESTEDHVM_PAGEFAULT_L0_ERROR; - if ( access_w && p2m_is_readonly(*p2mt) ) + if ( npfec->write_access && p2m_is_readonly(*p2mt) ) goto out; if ( p2m_is_paging(*p2mt) || p2m_is_shared(*p2mt) || !p2m_is_ram(*p2mt) ) @@ -181,6 +180,18 @@ nestedhap_walk_L0_p2m(struct p2m_domain *p2m, paddr_t L1_gpa, paddr_t *L0_gpa, *L0_gpa = (mfn_x(mfn) << PAGE_SHIFT) + (L1_gpa & ~PAGE_MASK); out: __put_gfn(p2m, L1_gpa >> PAGE_SHIFT); + + /* + * When reporting L0_ERROR, rewrite nfpec to match what would have occured + * if hardware had walked the L0, rather than the combined L02. + */ + if ( rc == NESTEDHVM_PAGEFAULT_L0_ERROR ) + { + npfec->present = !mfn_eq(mfn, INVALID_MFN); + npfec->gla_valid = 0; + npfec->kind = npfec_kind_unknown; + } + return rc; } @@ -191,7 +202,7 @@ nestedhap_walk_L0_p2m(struct p2m_domain *p2m, paddr_t L1_gpa, paddr_t *L0_gpa, */ int nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa, - bool_t access_r, bool_t access_w, bool_t access_x) + struct npfec *npfec) { int rv; paddr_t L1_gpa, L0_gpa; @@ -206,7 +217,8 @@ nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa, /* walk the L1 P2M table */ rv = nestedhap_walk_L1_p2m(v, *L2_gpa, &L1_gpa, &page_order_21, &p2ma_21, - access_r, access_w, access_x); + npfec->read_access, npfec->write_access, + npfec->insn_fetch); /* let caller to handle these two cases */ switch (rv) { @@ -222,9 +234,8 @@ nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa, } /* ==> we have to walk L0 P2M */ - rv = nestedhap_walk_L0_p2m(p2m, L1_gpa, &L0_gpa, - &p2mt_10, &p2ma_10, &page_order_10, - access_r, access_w, access_x); + rv = nestedhap_walk_L0_p2m(p2m, L1_gpa, &L0_gpa, &p2mt_10, &p2ma_10, + &page_order_10, npfec); /* let upper level caller to handle these two cases */ switch (rv) { diff --git a/xen/include/asm-x86/hvm/nestedhvm.h b/xen/include/asm-x86/hvm/nestedhvm.h index 256fed733a..7b53f23e97 100644 --- a/xen/include/asm-x86/hvm/nestedhvm.h +++ b/xen/include/asm-x86/hvm/nestedhvm.h @@ -58,7 +58,7 @@ bool_t nestedhvm_vcpu_in_guestmode(struct vcpu *v); #define NESTEDHVM_PAGEFAULT_RETRY 5 #define NESTEDHVM_PAGEFAULT_DIRECT_MMIO 6 int nestedhvm_hap_nested_page_fault(struct vcpu *v, paddr_t *L2_gpa, - bool_t access_r, bool_t access_w, bool_t access_x); + struct npfec *npfec); int nestedhap_walk_L1_p2m(struct vcpu *v, paddr_t L2_gpa, paddr_t *L1_gpa, unsigned int *page_order, uint8_t *p2m_acc,