From patchwork Wed Nov 16 17:03:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Perret X-Patchwork-Id: 13045602 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 72002C433FE for ; Wed, 16 Nov 2022 17:08:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=7RKWiA+ae2epJHmYi17xL/j5l/7Wi74E4rXoM6ScEvo=; b=TTrnKo4cOvqBaeOHG/UBtZlOP7 Bjv0Lt0n2IAW4Khu5lNhNYsl13W/rEjsfYZAH9usRq6xfCSErzZPyLZM2rs+gfegiWA5seCbBGN0R z8ShWKRF7/wn6SNRtp2+qAIY4c5ahUZGqL81/F1mech0VNvMyV5lCg4HLxSyYLAIHUCg8uI593uAR 6rj5qKZ15+5+oza1bAaDIzHo2awgtmBOP1iATcuX3UZL0TYi1FIOkWa3is8tKo7FrUWoz3MgpKEcb CpvGM6A8WvAKaeBFWDOuqhdawMDaPv8hf/CXqxzjR86uO6w4qM/fftK8kDd8PQOoGk/8rXTVR+z4s /Mc+trrw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ovLsX-006GQZ-Dp; Wed, 16 Nov 2022 17:07:13 +0000 Received: from mail-ed1-x549.google.com ([2a00:1450:4864:20::549]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ovLpU-006E5L-J0 for linux-arm-kernel@lists.infradead.org; Wed, 16 Nov 2022 17:04:06 +0000 Received: by mail-ed1-x549.google.com with SMTP id y20-20020a056402271400b004630f3a32c3so12879504edd.15 for ; Wed, 16 Nov 2022 09:04:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=WEPKdueo5FNQNF9/fei3NEE3Fz8J5QrFmaxaGbrgW5I=; b=Ab4Emp8kcmWx1L3Px0dWAkM+bY8plvLmVdh7hgZZxbj6/F6TH6+43o8rGornSbrVIO ebkbJBZte8MqtfyIXQZBx7wj3hHV8ksl9YgKmL74XQ+KpJujBKO6fHIcHjg0IqV4MnKj 5cfdyJyZJhgkzyHXh6y96fsIcMwEykmkIDhaultNtYPGW7lrfyDNDOTqM/Ln4gmmLAtR /HesmOGg8kCXIL/YfZepRrSUSW4QEg707ma+KwwParMrtUW8WvU0uFF+AbwnwvlOmGqM uMOhB42D32xrN6r73gzU1HAEb0363s0jkY5bPYQ3NV0hOex9Bwf+SWIgcl9sd0siuZqX VzpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=WEPKdueo5FNQNF9/fei3NEE3Fz8J5QrFmaxaGbrgW5I=; b=7HOzzugluhe+aClmVygrVWgIOZI/Y2oR6hMOYokAhbp07QH2VcUy5KvMe3d8gBwIcD tVQx9OCHbxBSd84v0sIf1K+KAA1aY4OCAorKbdKKz72m0I2tXlIrRety+TIR0UMQfA20 oqP6n33gPufQvJHwHJFA7XLJ8Fg7lRBjbdDtA6Ccb6INZVVe6bigKLNZMcuAVtgPX4EJ s7aUj78Ivmi6pWdeoEo2c7RIn792wWGb7s8lcg3FbnajCBMrPZprRk5aKvRrvFatQZ3H h41McKgOwtD13nDejvVG8ODT3iEQ03HEW0FEFt4TZWiLMb1q95zQQtmx/t0glJ8LvAuB xWjg== X-Gm-Message-State: ANoB5pl3zGhLISZ+52wdLRIvJdwBz8KP+qT2chC/Kkq0rGC0WdE5kMg+ ffBFWQHBCljiJgLXyzuHilsHwIwYTnpy X-Google-Smtp-Source: AA0mqf7VNeZ3KzM1nTjxZISVikiNvvcNnJ0KNs4YdhCtWf+uPc3PxJ392kWTSIoZW/R5ANucY0mbzWDRNhqa X-Received: from big-boi.c.googlers.com ([fda3:e722:ac3:cc00:31:98fb:c0a8:129]) (user=qperret job=sendgmr) by 2002:a17:907:76b8:b0:7ae:6cdd:9bc9 with SMTP id jw24-20020a17090776b800b007ae6cdd9bc9mr17629578ejc.619.1668618242780; Wed, 16 Nov 2022 09:04:02 -0800 (PST) Date: Wed, 16 Nov 2022 17:03:33 +0000 In-Reply-To: <20221116170335.2341003-1-qperret@google.com> Mime-Version: 1.0 References: <20221116170335.2341003-1-qperret@google.com> X-Mailer: git-send-email 2.38.1.431.g37b22c650d-goog Message-ID: <20221116170335.2341003-11-qperret@google.com> Subject: [PATCH 10/12] KVM: arm64: Handle FFA_MEM_RECLAIM calls from the host From: Quentin Perret To: Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , Oliver Upton , Catalin Marinas , Will Deacon , Sudeep Holla , Andrew Walbran Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, kernel-team@android.com, qperret@google.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221116_090404_669798_1EA3B0C6 X-CRM114-Status: GOOD ( 16.43 ) 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 From: Will Deacon Intecept FFA_MEM_RECLAIM calls from the host and transition the host stage-2 page-table entries from the SHARED_OWNED state back to the OWNED state once EL3 has confirmed that the secure mapping has been reclaimed. Signed-off-by: Will Deacon Signed-off-by: Quentin Perret --- arch/arm64/kvm/hyp/nvhe/ffa.c | 80 ++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/hyp/nvhe/ffa.c b/arch/arm64/kvm/hyp/nvhe/ffa.c index e6d85849c617..8f690b80af60 100644 --- a/arch/arm64/kvm/hyp/nvhe/ffa.c +++ b/arch/arm64/kvm/hyp/nvhe/ffa.c @@ -130,6 +130,23 @@ static void spmd_mem_share(struct arm_smccc_res *res, u32 len, u32 fraglen) res); } +static void spmd_mem_reclaim(struct arm_smccc_res *res, u32 handle_lo, + u32 handle_hi, u32 flags) +{ + arm_smccc_1_1_smc(FFA_MEM_RECLAIM, + handle_lo, handle_hi, flags, + 0, 0, 0, 0, + res); +} + +static void spmd_retrieve_req(struct arm_smccc_res *res, u32 len) +{ + arm_smccc_1_1_smc(FFA_FN64_MEM_RETRIEVE_REQ, + len, len, + 0, 0, 0, 0, 0, + res); +} + static void do_ffa_rxtx_map(struct arm_smccc_res *res, struct kvm_cpu_context *ctxt) { @@ -381,6 +398,65 @@ static void do_ffa_mem_share(struct arm_smccc_res *res, return; } +static void do_ffa_mem_reclaim(struct arm_smccc_res *res, + struct kvm_cpu_context *ctxt) +{ + DECLARE_REG(u32, handle_lo, ctxt, 1); + DECLARE_REG(u32, handle_hi, ctxt, 2); + DECLARE_REG(u32, flags, ctxt, 3); + struct ffa_composite_mem_region *reg; + struct ffa_mem_region *buf; + int ret = 0; + u32 offset; + u64 handle; + + handle = PACK_HANDLE(handle_lo, handle_hi); + + hyp_spin_lock(&host_buffers.lock); + + buf = hyp_buffers.tx; + *buf = (struct ffa_mem_region) { + .sender_id = HOST_FFA_ID, + .handle = handle, + }; + + spmd_retrieve_req(res, sizeof(*buf)); + buf = hyp_buffers.rx; + if (res->a0 != FFA_MEM_RETRIEVE_RESP) + goto out_unlock; + + /* Check for fragmentation */ + if (res->a1 != res->a2) { + ret = FFA_RET_ABORTED; + goto out_unlock; + } + + offset = buf->ep_mem_access[0].composite_off; + /* + * We can trust the SPMD to get this right, but let's at least + * check that we end up with something that doesn't look _completely_ + * bogus. + */ + if (WARN_ON(offset > KVM_FFA_MBOX_NR_PAGES * PAGE_SIZE)) { + ret = FFA_RET_ABORTED; + goto out_unlock; + } + + reg = (void *)buf + offset; + spmd_mem_reclaim(res, handle_lo, handle_hi, flags); + if (res->a0 != FFA_SUCCESS) + goto out_unlock; + + /* If the SPMD was happy, then we should be too. */ + WARN_ON(ffa_host_unshare_ranges(reg->constituents, + reg->addr_range_cnt)); +out_unlock: + hyp_spin_unlock(&host_buffers.lock); + + if (ret) + ffa_to_smccc_res(res, ret); +} + static bool ffa_call_unsupported(u64 func_id) { switch (func_id) { @@ -461,9 +537,11 @@ bool kvm_host_ffa_handler(struct kvm_cpu_context *host_ctxt) case FFA_FN64_MEM_SHARE: do_ffa_mem_share(&res, host_ctxt); goto out_handled; + case FFA_MEM_RECLAIM: + do_ffa_mem_reclaim(&res, host_ctxt); + goto out_handled; case FFA_MEM_LEND: case FFA_FN64_MEM_LEND: - case FFA_MEM_RECLAIM: case FFA_MEM_FRAG_TX: break; }