From patchwork Thu Jun 13 13:20:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Ene X-Patchwork-Id: 13696794 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AFFF1C27C4F for ; Thu, 13 Jun 2024 13:21:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=sESXJ6lTpXS1Uiyd1DMUTJSwf8gpaqDrOmX2O7P+Tt8=; b=mAY2h8kLLytUswPODFWvxlNSVB +vmucDQk4cSa20+kJcTc5W1Rzvralux2x95TCxTIRB5qMDWQknk9StKAPVFzffbY0/T3SmuiCj5GH raEk4jw25ILYKj9zGSE5L0y78ppwQsoFhjg/vHHuqgjfjr8g2au42XwYy8X/2KO8760p4W6+bQIMP X+zZf0yX8oV0cqXp0ocfa8g1KCoxKvLjuDwlRXfeyM0GeJ5vRSR2UPuaCNCZuKJPazMMjrzag9qSe WE/zMn9KqqN7miiG0dY1MnV6MmkHrSfSVBssUADL3+CtYEWwLXpB03HAlNzgU4OQJV9KPkWZxRghI gFyOaYVg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sHkNz-0000000GgTE-2Oxj; Thu, 13 Jun 2024 13:21:03 +0000 Received: from mail-wr1-x44a.google.com ([2a00:1450:4864:20::44a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sHkNv-0000000GgQg-1tHm for linux-arm-kernel@lists.infradead.org; Thu, 13 Jun 2024 13:21:01 +0000 Received: by mail-wr1-x44a.google.com with SMTP id ffacd0b85a97d-35f06558bc3so703857f8f.1 for ; Thu, 13 Jun 2024 06:20:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1718284855; x=1718889655; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=sESXJ6lTpXS1Uiyd1DMUTJSwf8gpaqDrOmX2O7P+Tt8=; b=mGo6M/4SBuggue8pbB8211o18KzlMxtW2RCx9s4olBp8QpQQjZMMFEOdHsbFTcasS4 Pt2u92sP9Z8RvafL+e6F3HH/H0CjJqaD1zzRJa98VFAOIc58H6yzJjrjkGSx+5Fv0+0J PKPVJ5anhKG/eOTOcUAZt0dgv9p5RI55gOvpBDjtyAYFCrS8vozzuZX1ZDwpVcXmyNfZ RUSvGXd0HWIbS8JsG9mmEqZAmTPoG9BparMQcnZif+Am/VlP+EQUojVinDl/HJmVII6s 8gGEo2SKqzXHDQNDvRsrfpKfkEsyuiCKIV24EFtrJEPM1E/TAjQAJk005jsHASdASz+E 2EOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718284855; x=1718889655; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sESXJ6lTpXS1Uiyd1DMUTJSwf8gpaqDrOmX2O7P+Tt8=; b=v5P2+X1yLGBQs/AZqBpvtMLdg9dhF1t8cBmR7hd9lcMin/B2xs6l2NVGHO9L34TT1w TS/xjKDKotohUrT0JAzw5klT4O9+TOrj3xaSkY/CRNQ5zox5pTpvUB8uyGRqgml+VPuk GGe6SgYEIApx6l5Lqf3AzR+Zyk8nA9CZegLrU+MrhDuNx+gwR7QTTH1oYaggq1lSvRT/ 0113Wqg5S9yVfXF1in48ptcBGpKGlYQyA74GX/tnDEUdE4j+2zDOySxqDzPLwbTHInlv eO9+kMgIfv/dMqawSPS8MFs55PjfCMIDjk920Xx4qtDb7apY0mEDtLxWB4OUnPoBfGe4 RLuA== X-Forwarded-Encrypted: i=1; AJvYcCX8f6pbDyfnBgBrzyJ9LS+V+LOS02oe1G/U8KfQrIBcwB3aMCgQ9seKwnpOab+1G4kuKy1hQgJ1lp0CJalgkbPEZkH3TMssxYJejrWZtkyy+0jqNVs= X-Gm-Message-State: AOJu0YwTeweo3PMQdgcfsCiPqzRpw6GBCEsdSi404oVlq3vh9+Ko1x2v wpsWE1Ltj/Zk8tBO35y5ab5s9es3Us3gR9OnCdO98dp4VCjcjLgcBWQj6oVZ6Qp1/VTiNWbt3c5 af4281noGosnhKij9Ma3SlK4VVw== X-Google-Smtp-Source: AGHT+IEuL/StGDQdidgafs+3ZU4qfQosVRrlPI2JL8IIcAF+ju+qgsK/HapKvJnwS4cSax/FEowSz1v2ebxWWLTYgbE= X-Received: from sebkvm.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:cd5]) (user=sebastianene job=sendgmr) by 2002:a5d:6405:0:b0:35f:2e9c:3295 with SMTP id ffacd0b85a97d-35fdf7aad26mr10867f8f.4.1718284854455; Thu, 13 Jun 2024 06:20:54 -0700 (PDT) Date: Thu, 13 Jun 2024 13:20:32 +0000 In-Reply-To: <20240613132035.1070360-1-sebastianene@google.com> Mime-Version: 1.0 References: <20240613132035.1070360-1-sebastianene@google.com> X-Mailer: git-send-email 2.45.2.505.gda0bf45e8d-goog Message-ID: <20240613132035.1070360-2-sebastianene@google.com> Subject: [PATCH v3 1/4] KVM: arm64: Trap FFA_VERSION host call in pKVM From: Sebastian Ene To: catalin.marinas@arm.com, james.morse@arm.com, jean-philippe@linaro.org, maz@kernel.org, oliver.upton@linux.dev, qperret@google.com, qwandor@google.com, sudeep.holla@arm.com, suzuki.poulose@arm.com, tabba@google.com, will@kernel.org, yuzenghui@huawei.com, lpieralisi@kernel.org Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel-team@android.com, Sebastian Ene X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240613_062059_532581_67F3C33A X-CRM114-Status: GOOD ( 19.72 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The pKVM hypervisor initializes with FF-A version 1.0. The spec requires that no other FF-A calls to be issued before the version negotiation phase is complete. Split the hypervisor proxy initialization code in two parts so that we can move the later one after the host negotiates its version. Without trapping the call, the host drivers can negotiate a higher version number with TEE which can result in a different memory layout described during the memory sharing calls. Signed-off-by: Sebastian Ene Reviewed-by: Sudeep Holla Tested-by: Sudeep Holla --- arch/arm64/kvm/hyp/nvhe/ffa.c | 120 +++++++++++++++++++++++++--------- 1 file changed, 90 insertions(+), 30 deletions(-) diff --git a/arch/arm64/kvm/hyp/nvhe/ffa.c b/arch/arm64/kvm/hyp/nvhe/ffa.c index 02746f9d0980..c8ab51c331f0 100644 --- a/arch/arm64/kvm/hyp/nvhe/ffa.c +++ b/arch/arm64/kvm/hyp/nvhe/ffa.c @@ -67,6 +67,9 @@ struct kvm_ffa_buffers { */ static struct kvm_ffa_buffers hyp_buffers; static struct kvm_ffa_buffers host_buffers; +static u32 hyp_ffa_version; +static bool has_version_negotiated; +static hyp_spinlock_t version_lock; static void ffa_to_smccc_error(struct arm_smccc_res *res, u64 ffa_errno) { @@ -639,6 +642,83 @@ static bool do_ffa_features(struct arm_smccc_res *res, return true; } +static int hyp_ffa_post_init(void) +{ + size_t min_rxtx_sz; + struct arm_smccc_res res; + + arm_smccc_1_1_smc(FFA_ID_GET, 0, 0, 0, 0, 0, 0, 0, &res); + if (res.a0 != FFA_SUCCESS) + return -EOPNOTSUPP; + + if (res.a2 != HOST_FFA_ID) + return -EINVAL; + + arm_smccc_1_1_smc(FFA_FEATURES, FFA_FN64_RXTX_MAP, + 0, 0, 0, 0, 0, 0, &res); + if (res.a0 != FFA_SUCCESS) + return -EOPNOTSUPP; + + switch (res.a2) { + case FFA_FEAT_RXTX_MIN_SZ_4K: + min_rxtx_sz = SZ_4K; + break; + case FFA_FEAT_RXTX_MIN_SZ_16K: + min_rxtx_sz = SZ_16K; + break; + case FFA_FEAT_RXTX_MIN_SZ_64K: + min_rxtx_sz = SZ_64K; + break; + default: + return -EINVAL; + } + + if (min_rxtx_sz > PAGE_SIZE) + return -EOPNOTSUPP; + + return 0; +} + +static void do_ffa_version(struct arm_smccc_res *res, + struct kvm_cpu_context *ctxt) +{ + DECLARE_REG(u32, ffa_req_version, ctxt, 1); + + if (FFA_MAJOR_VERSION(ffa_req_version) != 1) { + res->a0 = FFA_RET_NOT_SUPPORTED; + return; + } + + hyp_spin_lock(&version_lock); + if (has_version_negotiated) { + res->a0 = hyp_ffa_version; + goto unlock; + } + + /* + * If the client driver tries to downgrade the version, we need to ask + * first if TEE supports it. + */ + if (FFA_MINOR_VERSION(ffa_req_version) < FFA_MINOR_VERSION(hyp_ffa_version)) { + arm_smccc_1_1_smc(FFA_VERSION, ffa_req_version, 0, + 0, 0, 0, 0, 0, + res); + if (res->a0 == FFA_RET_NOT_SUPPORTED) + goto unlock; + + hyp_ffa_version = ffa_req_version; + } + + if (hyp_ffa_post_init()) + res->a0 = FFA_RET_NOT_SUPPORTED; + else { + has_version_negotiated = true; + res->a0 = hyp_ffa_version; + } +unlock: + hyp_spin_unlock(&version_lock); +} + bool kvm_host_ffa_handler(struct kvm_cpu_context *host_ctxt, u32 func_id) { struct arm_smccc_res res; @@ -659,6 +739,11 @@ bool kvm_host_ffa_handler(struct kvm_cpu_context *host_ctxt, u32 func_id) if (!is_ffa_call(func_id)) return false; + if (!has_version_negotiated && func_id != FFA_VERSION) { + ffa_to_smccc_error(&res, FFA_RET_INVALID_PARAMETERS); + goto out_handled; + } + switch (func_id) { case FFA_FEATURES: if (!do_ffa_features(&res, host_ctxt)) @@ -685,6 +770,9 @@ bool kvm_host_ffa_handler(struct kvm_cpu_context *host_ctxt, u32 func_id) case FFA_MEM_FRAG_TX: do_ffa_mem_frag_tx(&res, host_ctxt); goto out_handled; + case FFA_VERSION: + do_ffa_version(&res, host_ctxt); + goto out_handled; } if (ffa_call_supported(func_id)) @@ -699,7 +787,6 @@ bool kvm_host_ffa_handler(struct kvm_cpu_context *host_ctxt, u32 func_id) int hyp_ffa_init(void *pages) { struct arm_smccc_res res; - size_t min_rxtx_sz; void *tx, *rx; if (kvm_host_psci_config.smccc_version < ARM_SMCCC_VERSION_1_2) @@ -725,35 +812,7 @@ int hyp_ffa_init(void *pages) if (FFA_MAJOR_VERSION(res.a0) != 1) return -EOPNOTSUPP; - arm_smccc_1_1_smc(FFA_ID_GET, 0, 0, 0, 0, 0, 0, 0, &res); - if (res.a0 != FFA_SUCCESS) - return -EOPNOTSUPP; - - if (res.a2 != HOST_FFA_ID) - return -EINVAL; - - arm_smccc_1_1_smc(FFA_FEATURES, FFA_FN64_RXTX_MAP, - 0, 0, 0, 0, 0, 0, &res); - if (res.a0 != FFA_SUCCESS) - return -EOPNOTSUPP; - - switch (res.a2) { - case FFA_FEAT_RXTX_MIN_SZ_4K: - min_rxtx_sz = SZ_4K; - break; - case FFA_FEAT_RXTX_MIN_SZ_16K: - min_rxtx_sz = SZ_16K; - break; - case FFA_FEAT_RXTX_MIN_SZ_64K: - min_rxtx_sz = SZ_64K; - break; - default: - return -EINVAL; - } - - if (min_rxtx_sz > PAGE_SIZE) - return -EOPNOTSUPP; - + hyp_ffa_version = FFA_VERSION_1_0; tx = pages; pages += KVM_FFA_MBOX_NR_PAGES * PAGE_SIZE; rx = pages; @@ -775,5 +834,6 @@ int hyp_ffa_init(void *pages) .lock = __HYP_SPIN_LOCK_UNLOCKED, }; + version_lock = __HYP_SPIN_LOCK_UNLOCKED; return 0; }