Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp913404rwr; Wed, 19 Apr 2023 15:20:55 -0700 (PDT) X-Google-Smtp-Source: AKy350ZD5OO8GUzjJhijWfzvEW1wENmfiUTzwBON7ByXya4PbJ8BI4VZECbA8s43r2XH+/AuSUPv X-Received: by 2002:a17:902:d4d0:b0:1a0:450d:a481 with SMTP id o16-20020a170902d4d000b001a0450da481mr8283385plg.35.1681942854943; Wed, 19 Apr 2023 15:20:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681942854; cv=none; d=google.com; s=arc-20160816; b=iBkCrKccAGRBFgIxd49TLOCvvOAr5JfKI4UV6L4QSOtxyhZw7fYSFclz2yT+U3myJQ 9DYc1voix8NFh1EGb1vq4qCvNEMmJIw37kpRQccRKC26QRFXikzP+zJDsBRrkEkPa4wV eo6fFbx4OT3+RaEv9tCrBMbdqe2RCIkrjVsCuQJKIGuOXrxGUx2t57u4snOll2srp0wI wmxsIgr07KqOBheuVaMSXX5pd1O6cAuWmmcA+yRMkgTxry9oYAU83pWKbUxJAuFWpZeq ZYPrmkWFRIB+WasGQ6q7mKnu1pCmMC3lr+RBvUKTSRv2RDO5/mDtK+HRNyCsRXqb2mzz aOtA== 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 :dkim-signature; bh=cyIj0UBv9pS76jm0gxcpwmAmAKqEq2wUQqZgUQ6cXgk=; b=z81RH3y75gaDBTPS79yGFyEAxrmfwoulmH7xmotnxm1YPMFjtT5NdLA1KX0q9DaQ1k adCvW+DnGgNS5+GtoRnfdYeOfNQrMizX7yA6kX8qk8FXpr5qDloXm6/ZyRnKu5TJ8W7B 3T2MoVLESmO+tuCR0xm49jiM2M6L5ueh+bb0X0r+hbL4ieoLMP0HWnl5/oglP2oufRHq Cyqcl0Jo55wMcLCRvt2d65ug0s5FM0TTveRPdK0R3Rp1zCcSk1IWO+eLBYxnWLOi52pb PF+3XavEqSFxDKN9hDXO/uLjyBVSinOwe9Rp4xlTgSPOLzSeYtaugsZNgP5pX1Gg+VZ+ 9WVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=JUqXodm7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s15-20020a170902ea0f00b001a66a0f12b7si8125275plg.236.2023.04.19.15.20.43; Wed, 19 Apr 2023 15:20:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=JUqXodm7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232163AbjDSWTi (ORCPT + 99 others); Wed, 19 Apr 2023 18:19:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42136 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231331AbjDSWSw (ORCPT ); Wed, 19 Apr 2023 18:18:52 -0400 Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1290293E2 for ; Wed, 19 Apr 2023 15:18:23 -0700 (PDT) Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1a686260adcso5384335ad.0 for ; Wed, 19 Apr 2023 15:18:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942693; x=1684534693; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=cyIj0UBv9pS76jm0gxcpwmAmAKqEq2wUQqZgUQ6cXgk=; b=JUqXodm7yTv0ln6sHn0eyCWrKgQ2lIYWLmeyEDRipWR1vGY9jvhA0yFwbIBRMjhRbb 5feEP/EeoRDxGxyKun0x5G2O5oEBJdDAFRS/RHZ4Ci8b/iv26PTeN1VUZ+s3pIrJBSTf rZjckAXPtWtvH1Kvmtp4JHL0K8GHX72uGWQN4DiIyrkkdwTzznpc2XULSlQqV9+qqsp4 UJ+vBagyFy5rq5HF8Oa6OHLTBhdbA55/ZL15Y605z4/ZrHmgIiKmwYrEl9ntaC9BfLrM ClfqHB6I3YRHPV8jZmE8uYnBE2UGqLOLZNM172UxVHBchm+jEWBkzY2ZxZ7p2UfryNvP OpFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942693; x=1684534693; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cyIj0UBv9pS76jm0gxcpwmAmAKqEq2wUQqZgUQ6cXgk=; b=iCfkzG+o2laaTnC8c4GbM/x4ksCW76tj1ZOaXB8AvGhzMiWGdEw5PQYMIWJh4vnWKb A1ZA6HXlJRlTyp6PEunRu3XiOQ58agnUU99SWYF9hoLB18qMOIhSJ2B4P+BdQufqSlEZ npHRwLZ+4K71iaTxPUIvRdpLk6Ge9nL6f9YOrthOGJspHdArUTFaD+PqIm26lmMWUiIr abbQXk4r4bHxXw1WJquoRLcpFY/bHuFi2rA6dOFV0T/3/rg2uz7yUNk5Kl8YIR6POnlh IjhSIEUPeK5tVRGUkUAmGsHkodj/cVLJg/jtUZBbZIJuiGa5sws3fORUGsIxyjvJE7QO ZTrQ== X-Gm-Message-State: AAQBX9cz/ttT/887Ws48qu1m7aQ+Ohxqt4OPIkc814mKZbkB119cw3aT llHJoUWEgqWnfdJkyztjBAPfyFmZzriJGz7dv2c= X-Received: by 2002:a17:903:2291:b0:19c:dbce:dce8 with SMTP id b17-20020a170903229100b0019cdbcedce8mr8768332plh.15.1681942693408; Wed, 19 Apr 2023 15:18:13 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id jn11-20020a170903050b00b00196807b5189sm11619190plb.292.2023.04.19.15.18.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:13 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Alexandre Ghiti , Andrew Jones , Andrew Morton , Anup Patel , Atish Patra , =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , Suzuki K Poulose , Will Deacon , Marc Zyngier , Sean Christopherson , linux-coco@lists.linux.dev, Dylan Reid , abrestic@rivosinc.com, Samuel Ortiz , Christoph Hellwig , Conor Dooley , Greg Kroah-Hartman , Guo Ren , Heiko Stuebner , Jiri Slaby , kvm-riscv@lists.infradead.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, Mayuresh Chitale , Palmer Dabbelt , Paolo Bonzini , Paul Walmsley , Rajnesh Kanwal , Uladzislau Rezki Subject: [RFC 20/48] RISC-V: KVM: Add gstage mapping for TVMs Date: Wed, 19 Apr 2023 15:16:48 -0700 Message-Id: <20230419221716.3603068-21-atishp@rivosinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230419221716.3603068-1-atishp@rivosinc.com> References: <20230419221716.3603068-1-atishp@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org For TVM, the gstage mapping is managed by the TSM via COVH SBI calls. The host is responsible for allocating page that must be pinned to avoid swapping. The page is converted it to confidential before handing over to the TSM for gstage mapping. Signed-off-by: Atish Patra --- arch/riscv/kvm/cove.c | 63 +++++++++++++++++++++++++++++++++++++- arch/riscv/kvm/vcpu_exit.c | 9 ++++-- 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/arch/riscv/kvm/cove.c b/arch/riscv/kvm/cove.c index 4efcae3..44095f6 100644 --- a/arch/riscv/kvm/cove.c +++ b/arch/riscv/kvm/cove.c @@ -149,8 +149,68 @@ void kvm_riscv_cove_vcpu_put(struct kvm_vcpu *vcpu) int kvm_riscv_cove_gstage_map(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned long hva) { - /* TODO */ + struct kvm_riscv_cove_page *tpage; + struct mm_struct *mm = current->mm; + struct kvm *kvm = vcpu->kvm; + unsigned int flags = FOLL_LONGTERM | FOLL_WRITE | FOLL_HWPOISON; + struct page *page; + int rc; + struct kvm_cove_tvm_context *tvmc = kvm->arch.tvmc; + + tpage = kmalloc(sizeof(*tpage), GFP_KERNEL_ACCOUNT); + if (!tpage) + return -ENOMEM; + + mmap_read_lock(mm); + rc = pin_user_pages(hva, 1, flags, &page, NULL); + mmap_read_unlock(mm); + + if (rc == -EHWPOISON) { + send_sig_mceerr(BUS_MCEERR_AR, (void __user *)hva, + PAGE_SHIFT, current); + rc = 0; + goto free_tpage; + } else if (rc != 1) { + rc = -EFAULT; + goto free_tpage; + } else if (!PageSwapBacked(page)) { + rc = -EIO; + goto free_tpage; + } + + rc = cove_convert_pages(page_to_phys(page), 1, true); + if (rc) + goto unpin_page; + + rc = sbi_covh_add_zero_pages(tvmc->tvm_guest_id, page_to_phys(page), + SBI_COVE_PAGE_4K, 1, gpa); + if (rc) { + pr_err("%s: Adding zero pages failed %d\n", __func__, rc); + goto zero_page_failed; + } + tpage->page = page; + tpage->npages = 1; + tpage->is_mapped = true; + tpage->gpa = gpa; + tpage->hva = hva; + INIT_LIST_HEAD(&tpage->link); + + spin_lock(&kvm->mmu_lock); + list_add(&tpage->link, &kvm->arch.tvmc->zero_pages); + spin_unlock(&kvm->mmu_lock); + return 0; + +zero_page_failed: + //TODO: Do we need to reclaim the page now or VM gets destroyed ? + +unpin_page: + unpin_user_pages(&page, 1); + +free_tpage: + kfree(tpage); + + return rc; } void kvm_riscv_cove_vcpu_switchto(struct kvm_vcpu *vcpu, struct kvm_cpu_trap *trap) @@ -390,6 +450,7 @@ void kvm_riscv_cove_vm_destroy(struct kvm *kvm) cove_delete_page_list(kvm, &tvmc->reclaim_pending_pages, false); cove_delete_page_list(kvm, &tvmc->measured_pages, false); + cove_delete_page_list(kvm, &tvmc->zero_pages, true); /* Reclaim and Free the pages for tvm state management */ rc = sbi_covh_tsm_reclaim_pages(page_to_phys(tvmc->tvm_state.page), tvmc->tvm_state.npages); diff --git a/arch/riscv/kvm/vcpu_exit.c b/arch/riscv/kvm/vcpu_exit.c index 0d0c895..d00b9ee5 100644 --- a/arch/riscv/kvm/vcpu_exit.c +++ b/arch/riscv/kvm/vcpu_exit.c @@ -41,8 +41,13 @@ static int gstage_page_fault(struct kvm_vcpu *vcpu, struct kvm_run *run, }; } - ret = kvm_riscv_gstage_map(vcpu, memslot, fault_addr, hva, - (trap->scause == EXC_STORE_GUEST_PAGE_FAULT) ? true : false); + if (is_cove_vcpu(vcpu)) { + /* CoVE doesn't care about PTE prots now. No need to compute the prots */ + ret = kvm_riscv_cove_gstage_map(vcpu, fault_addr, hva); + } else { + ret = kvm_riscv_gstage_map(vcpu, memslot, fault_addr, hva, + (trap->scause == EXC_STORE_GUEST_PAGE_FAULT) ? true : false); + } if (ret < 0) return ret; -- 2.25.1