From patchwork Wed Apr 19 22:16:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217673 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 EB7A7C77B73 for ; Wed, 19 Apr 2023 23:30:58 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=QAxDHh/6yF+jQHRAEUu11YNdXXyW0DjhlbKMWkN7af8=; b=oOEfMNI4U7vqkw 1oPqlo4wyeGoUSO0ruht5Sr3cjqcHOnzgOEKhp+TteSfhlNR6gLrBjrT73v0NELt6G3kNbsUq118Q JjWNPgV0EcTMMhSP9gTfX7HJ/vBA2NETBrTFWBJ9JHdwKIackXyqbIUzdLEUFdp/oWql6Wn7bxM7K It6707unp6LOdkhSD98jlE+iSPUrbHoAZ7wVqYldVHa4HhrYEHRqtes1TQvTxd48NQcM4Vxa4gvHh 2eiRUHKUoPmEWC/JpRCDDjG2NtgWzNkAAzjliX4LGF34f/M64m2f53BgC7G1RwYsR+usBXMmLWcjP H2AsBDBX15+TVdcd8SNQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppHG2-006eSi-2Z; Wed, 19 Apr 2023 23:30:38 +0000 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG7K-006SnQ-2M for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:17:38 +0000 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1a5197f00e9so4654945ad.1 for ; Wed, 19 Apr 2023 15:17:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942652; x=1684534652; 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=mljZsiO5QRb1GQDkLUDPFjVfkuRVaSDyhp9y+byMdss=; b=rSiuLSw6DyvgYFbph9bmFmqDG3DBHXwiC4IjLHTdoeoNqI3Zj5mvjcgftruD3sRQcL WxDYcG6ScHPYH/tsYrRzbCoTKzHkW8KNXh7K15sX/3vd0JxNdmgwDLmJ5Gbl1ptnc8kV B413qUWKvROFulz1VG3quMom7gr4KDz5SG6hA7i1NB7suVliV8jg80Ei4eKibGK25dVw IUKYu3sVmnUrAoFcDhoqyEJFPDZqPR+RsU7ZbMNTfRONc8CY2sotnhibyQDIiJL6WP4d uPsZMJmzwj1MOrHzXnjW7uCJa6/KKt6iVnYO2QkJX8Ox8I1aJyUyI4qshbT0yxib1Rsh ovBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942652; x=1684534652; 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=mljZsiO5QRb1GQDkLUDPFjVfkuRVaSDyhp9y+byMdss=; b=clwRyi2lSO34ErpWNwgeKM71+rgmwdlFcBivWCC3WaEv3aNIFcQvs+uxR6i7oOxwTB PfaOD4kiKs4Or9yhm5VAGqjvFQu5eXTyHeP67o+a6dGFPwbkupj1ZUhhSktcvYfJx7dh Ln3lUEKVAd3K972h7zOeGEJIakfP3/NdoRLE1QGIvuFBU0uCbqxDY7U+udp0bg7IXgRJ tHTQxqrUtbaPuNiI8vhX0sSEeR6FWzBTp23mUd5STwd2lx5Z5UyITSkTw7uilNg56lf4 9IGs9mL+uliGMg7OE5Lnc1plknULnh5RAg9CTmJjQNN2sOswHtzyqnt5DHF12gtk7r1W UdQA== X-Gm-Message-State: AAQBX9dxr7RM3eXJaGpza8Q/bax7dsC9HfycZjh34GchuAxWAxurLtOT ZhL+xXVC5uMav5RskUbnvy+t4Q== X-Google-Smtp-Source: AKy350YAUHKVoqLB+lNg1GyvGR/YhY1dexLjtDy2z84R1FBjB72vycA1EeypPas5l/++BhN5lcYzyg== X-Received: by 2002:a17:902:e74c:b0:1a6:4200:bef4 with SMTP id p12-20020a170902e74c00b001a64200bef4mr7961111plf.56.1681942651818; Wed, 19 Apr 2023 15:17:31 -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.17.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:17:31 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Rajnesh Kanwal , Atish Patra , Alexandre Ghiti , Andrew Jones , Andrew Morton , Anup Patel , Atish Patra , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , 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 , Uladzislau Rezki Subject: [RFC 01/48] mm/vmalloc: Introduce arch hooks to notify ioremap/unmap changes Date: Wed, 19 Apr 2023 15:16:29 -0700 Message-Id: <20230419221716.3603068-2-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151734_779482_E9A120ED X-CRM114-Status: GOOD ( 13.15 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Rajnesh Kanwal In virtualization, the guest may need notify the host about the ioremap regions. This is a common usecase in confidential computing where the host only provides MMIO emulation for the regions specified by the guest. Add a pair if arch specific callbacks to track the ioremapped regions. This patch is based on pkvm patches. A generic arch config can be added similar to pkvm if this is going to be the final solution. The device authorization/filtering approach is very different from this and we may prefer that one as it provides more flexibility in terms of which devices are allowed for the confidential guests. Signed-off-by: Rajnesh Kanwal Signed-off-by: Atish Patra --- mm/vmalloc.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index bef6cf2..023630e 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -304,6 +304,14 @@ static int vmap_range_noflush(unsigned long addr, unsigned long end, return err; } +__weak void ioremap_phys_range_hook(phys_addr_t phys_addr, size_t size, pgprot_t prot) +{ +} + +__weak void iounmap_phys_range_hook(phys_addr_t phys_addr, size_t size) +{ +} + int ioremap_page_range(unsigned long addr, unsigned long end, phys_addr_t phys_addr, pgprot_t prot) { @@ -315,6 +323,10 @@ int ioremap_page_range(unsigned long addr, unsigned long end, if (!err) kmsan_ioremap_page_range(addr, end, phys_addr, prot, ioremap_max_page_shift); + + if (!err) + ioremap_phys_range_hook(phys_addr, end - addr, prot); + return err; } @@ -2772,6 +2784,10 @@ void vunmap(const void *addr) addr); return; } + + if (vm->flags & VM_IOREMAP) + iounmap_phys_range_hook(vm->phys_addr, get_vm_area_size(vm)); + kfree(vm); } EXPORT_SYMBOL(vunmap); From patchwork Wed Apr 19 22:16:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217676 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 06645C77B75 for ; Wed, 19 Apr 2023 23:31:01 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=fkqc50KTMOKsKBZFu+bvlqGVWqFEICqNJSr5Cpzj9wQ=; b=ZZsSLDGbngaF2c ulMpf2ZSkU/ly5q1nGTOBFHN+N3Lrwi97qpSToA/imVOqMraWgBk4SqeKn4LVUUB4vpdDFmhVAgkD 1CRaOKzLUE6IEjfsVwUaOkBoyXO1VUbsoDvj6WddFwgBcM+hXX+P/MrIRAuEutK2TzOHRNJw05Ldq qMcyGfdX1ZODYYJbrOWCsqlsYlwyLpeS/+ilXaDz2mPBntIQKXvnuznB6ksabhtFKOvM0LpQmCY4+ rYs9PU7pOrXYHSsQWxQNepfEVooHPW8s6l/mcij8/L9M1V/+eJYxoXhSxwoVs9dU/2Zbqko6rH3KB yoAzFu8dUxTCrorCfkuQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppHG4-006eTe-1w; Wed, 19 Apr 2023 23:30:40 +0000 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG7M-006So6-2y for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:17:41 +0000 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1a516fb6523so5305055ad.3 for ; Wed, 19 Apr 2023 15:17:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942654; x=1684534654; 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=TviUiXCC6v39lMS4rfPLGE9Pfx0aaxYAF0PLkJZJ+44=; b=fw3FXTdDZVAQaqNcpqxhJ5jCbeJxZbbRCeUP7dUA0xboMzGhByaEInJPZwYw9bXQXk ARHWS0qP1pZrTXclentFqb6h5U2ua9sUZm6vZcXmwvDFczjrVihV2Nra1Jc2/WF5XyU2 0JadbkEFEI0//guHLR6qxF3JNYLlwaQIZD991cfubV5PenBiFCNtb6hgKaPgRSq66vCg e+VFqjLE6civs190GuIRI3K+ZsW+0KEkHzSOhGggkx8qfJ2wZO5KlkmScX+zwalNjqk6 RuMOo25mdsFietap8z25HKARP1geAiwoigenSaj60oBOp8w+6PxBWZ/NPdxHu1rx5LuB U5bA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942654; x=1684534654; 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=TviUiXCC6v39lMS4rfPLGE9Pfx0aaxYAF0PLkJZJ+44=; b=leBLlvvX3eXBJF2wl5SPtWuNz1yghx4kW+klsQQKKLylK3JEklkURBj215pNaX/hMD 8fFCQA+NgxNyU3/y4rIuYB6iVWCFmW5c7VZHadIaXKI5WhpewuZ6/5npUa1zEfrPZDqL tKa0+oslR35X38j2JFJX31eEF9sJD8uo9ObT/9k/2zi0APzeOdN/bQTGyQUseVXtIMsi yljU9L5RwWOF6DVUmMmbLo+vJOfbI/jbwXw8W0kU0j7EYaZnCECcFqOuyospFjU60GWM 6jZAx3eZOu3tULTei4p2l4VFFg2pTQSItUFskH4v5gSJXGQo4MJsbT5u1XGPsfoqAF6m xpJQ== X-Gm-Message-State: AAQBX9ddBNuoeewE4PEq/loc1+6fM32p7ZxTeDxtLiCw2nUFCP6XOkz4 Dpr+SscS2tn3ry/9bNjd4rhTzw== X-Google-Smtp-Source: AKy350bx72vsD2gx/qhvw+4gm1lk9ECFZlAAwd4+tDv5bxVSjJh5w7Co6h0FkmAoPJamfBpYhAs0yQ== X-Received: by 2002:a17:902:e80a:b0:1a1:cc5a:b04 with SMTP id u10-20020a170902e80a00b001a1cc5a0b04mr8122719plg.3.1681942654045; Wed, 19 Apr 2023 15:17:34 -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.17.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:17:33 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 02/48] RISC-V: KVM: Improve KVM error reporting to the user space Date: Wed, 19 Apr 2023 15:16:30 -0700 Message-Id: <20230419221716.3603068-3-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151737_035707_B1B3E0D3 X-CRM114-Status: GOOD ( 12.54 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org This patch adds RISC-V specific cause for ioctl run failure. For now, it will be used for the below two cases: 1. Insufficient IMSIC files if VM is configured to run in HWACCEL mode 2. TSM is unable to run run_vcpu SBI call for TVMs KVM also uses a custom scause bit(48) to distinguish this case from regular vcpu exit causes. Signed-off-by: Atish Patra --- arch/riscv/include/asm/csr.h | 2 ++ arch/riscv/include/uapi/asm/kvm.h | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h index 3176355..e78503a 100644 --- a/arch/riscv/include/asm/csr.h +++ b/arch/riscv/include/asm/csr.h @@ -96,6 +96,8 @@ #define EXC_VIRTUAL_INST_FAULT 22 #define EXC_STORE_GUEST_PAGE_FAULT 23 +#define EXC_CUSTOM_KVM_COVE_RUN_FAIL 48 + /* PMP configuration */ #define PMP_R 0x01 #define PMP_W 0x02 diff --git a/arch/riscv/include/uapi/asm/kvm.h b/arch/riscv/include/uapi/asm/kvm.h index b41d0e7..11440df 100644 --- a/arch/riscv/include/uapi/asm/kvm.h +++ b/arch/riscv/include/uapi/asm/kvm.h @@ -245,6 +245,10 @@ enum KVM_RISCV_SBI_EXT_ID { /* One single KVM irqchip, ie. the AIA */ #define KVM_NR_IRQCHIPS 1 +/* run->fail_entry.hardware_entry_failure_reason codes. */ +#define KVM_EXIT_FAIL_ENTRY_IMSIC_FILE_UNAVAILABLE (1ULL << 0) +#define KVM_EXIT_FAIL_ENTRY_COVE_RUN_VCPU (1ULL << 1) + #endif #endif /* __LINUX_KVM_RISCV_H */ From patchwork Wed Apr 19 22:16:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217678 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 537ECC77B7E for ; Wed, 19 Apr 2023 23:31:01 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=0VbwfL4JXDrp5CHlkZTSfcysNU/Q8iMSIju7eW3gTeI=; b=YOzpDlB4XFnUAP IfRF7td/zuPGYt532lwu9i0VhmNyYumgJC0vVhMkBtm6EIFvHG03D0UDz9sfBs7RPkq25ptZypiL+ wBua5uI2q8Gu0Fi4P7OjGA7+owGn9EapvXvyy9IoudWvS+LGIv+61LzcVT6S61dWyTA6w5Z9nfik5 Y21lbQZG+RYZoZDkJBdVrVavCUKqBk3JvD0t0hDOEi7kAOxnL7WSFNUODNmF7z3oWYiXgBQauDKc3 UGPooHk01xbB9KtFG3VvyR693ZvO991JbR9V5yxfa1URm5KY9kIwGhTzYbcJkHfwB7JkSkJqK3h0N rQnuiFbQApdtxIAtgmnQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppHG3-006eTF-2f; Wed, 19 Apr 2023 23:30:39 +0000 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG7M-006Sp1-25 for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:17:40 +0000 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1a6862e47b1so5504105ad.0 for ; Wed, 19 Apr 2023 15:17:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942656; x=1684534656; 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=QQ+Q1EB8U9VN9DkQE/7ZRwGYKH2CHxTB2A2ZNISqIdY=; b=M7pMYKMh2fzPlQxiN+Er+1mRXb5xsvVBR/kCf0Xc6I4eRmeu17ZWxN3F+8Uh4bOfYt pLsryNkeitFt3/g2LwdIO4gRgf1eK0d0zd3qJJmJH0/Ytd9PXb2f4ZIv2vw9qF7rcmT+ +/5fNVmr7py0CepFxvid91vQee8LTDJ1trxwVh1r+SZa6D1ybCiNK5WtQpwcXBYzwzon 14Dk5NjS3zvhDHWUiYkgfDVG/0CawxYwCekaOcRjbz+KKATicit8f2wKNBE+FUv2M2pL VFRS7BwOkXZ7Z6aFPV2U5r0uWlcE+ptOAW8XtsMKVeJj+vK+/KtMoB1U0hUKE2I995ZX 1ZPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942656; x=1684534656; 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=QQ+Q1EB8U9VN9DkQE/7ZRwGYKH2CHxTB2A2ZNISqIdY=; b=Og8s4o7DjQIySgTmzS7bF7gtj751ZxnETypH6OtWbDxKpFUaexSUK8zQXx9cOahnSO aR6uJzA0iu4JH3hGwTqoxws60X8sPcURep3pj08Du/fnldi2jMU2IRxpdzWL0N36RxNv bTcuUDoc/dt2kWGn/B/B/9XRGnzAZ3Tpi/zASB/HJbA/CmYTmMcGdu3OcWNaVFsXVCtu AnlilCuaxUju4g57a/W2ugWK2xq925g5U6pOITwWPar1j0srgh835viLw6VCwMpBcywA s+ymbL4v8Y77Ku8sPtuumw9om/FJH/VMHTCWsOq8iQXQpB6cKKYh/8KMlhKBhXflwskd TwDQ== X-Gm-Message-State: AAQBX9c7r7WkSThZcbHDTk9m96MjO+QHe630eSfWp8yPVpy+L7lUtB78 T3av84p41P6wlrxA5v8Y6Itimg== X-Google-Smtp-Source: AKy350ZJUiSSYrDjl7yi3RGe7KkMi+TjKSL1H9rjEJIsWoSyzpH0Qljb3g4DlHLMrKbT1BFQ8UiTyA== X-Received: by 2002:a17:902:6bc1:b0:1a6:81fc:b585 with SMTP id m1-20020a1709026bc100b001a681fcb585mr6201339plt.41.1681942656169; Wed, 19 Apr 2023 15:17:36 -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.17.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:17:35 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 03/48] RISC-V: KVM: Invoke aia_update with preempt disabled/irq enabled Date: Wed, 19 Apr 2023 15:16:31 -0700 Message-Id: <20230419221716.3603068-4-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151736_682795_C017E692 X-CRM114-Status: GOOD ( 10.81 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Some of the aia_update operations required to invoke IPIs that needs interrupts to be enabled. Currently, entire aia_update is being called from irqs disabled context while only preemption disable is necessary. Signed-off-by: Atish Patra --- arch/riscv/kvm/vcpu.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index e65852d..c53bf98 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -1247,15 +1247,16 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) kvm_riscv_check_vcpu_requests(vcpu); - local_irq_disable(); - /* Update AIA HW state before entering guest */ + preempt_disable(); ret = kvm_riscv_vcpu_aia_update(vcpu); if (ret <= 0) { - local_irq_enable(); + preempt_enable(); continue; } + preempt_enable(); + local_irq_disable(); /* * Ensure we set mode to IN_GUEST_MODE after we disable * interrupts and before the final VCPU requests check. From patchwork Wed Apr 19 22:16:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217666 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 E9175C77B75 for ; Wed, 19 Apr 2023 23:12:45 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=CTrwhO1OQfbyWaZCPzAJDqomrbN/cxSHct/FTnzENuM=; b=m9XWnRpxZki8qj 5+8KUYkRMUqt/wkXciZjUwycjGtu2NfX471oZXLuIRis0g8peg231gVAryhvZ2KfE81yC/Yf/qnIL rdG9SJamx/Sz5/dzumYG4eEt5ekx1bkucm2oCmiQ1DGC848TEbHX7NMa9Lv9AFwdlFkAjFWyO1TCI MLfb71iAEwPc9rYMpdz0S8IaYMp11DQhVwRG/gXgi6MfZRQgoN+mpO7QSLaLPw6KMFQLCgYeoFtb1 CyB5/yU8OnxxKTsAXCUIIYZn0waEV9taLuKiEK6VfYIa9EMiRqkapVs2dEUDClKcDJP9+JcU3VQZk UG4jVo0k/t17ZIK98/oQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGyd-006ckf-2h; Wed, 19 Apr 2023 23:12:39 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGyc-006ckG-1C for linux-riscv@bombadil.infradead.org; Wed, 19 Apr 2023 23:12:38 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:MIME-Version :References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=ur77tHINwX+qbUdlSGnRNMwUnwDaAVVcewhgSBhFUx0=; b=lA0hdhT4lCZbEncpRNFlLQUG2n OXeLj61Y5Cby0LS+17cVm9/R+BmnNg1z7Omcj7DoVCwcg8k7SYUB/T/fp/a8IU+Ik0UM0dr1dH5PO RMVi8xT+agxCMWHjwVOtEqUQ13ChStKkglMBHOt2Y0wF/Bwl7RjO2t84XX0uSvIw5z4iFCpeDpxKQ d4YsTbSGUKb6diasGBhxLHziLxmPoDJvglZeTeM1kfBCxeoKBnB0LGwMh8vfW4a4uAXATL026AkxX D4sEt53Hjm0wl7hTxT8xtBwiP+PPhhtkKaKKn2veVlr56aYls0lhw3JG8oATjcTeB56Z3KpGPw3zO 0m+xeZKw==; Received: from mail-pg1-x532.google.com ([2607:f8b0:4864:20::532]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG7T-0021Gy-1d for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:19:53 +0000 Received: by mail-pg1-x532.google.com with SMTP id 41be03b00d2f7-517c840f181so182430a12.3 for ; Wed, 19 Apr 2023 15:17:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942658; x=1684534658; 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=ur77tHINwX+qbUdlSGnRNMwUnwDaAVVcewhgSBhFUx0=; b=c18b6HjhB6xZTijYQX2nimbdoBlIXr9qBNm2sHraX79sumJ2mnHyyAF4iUwGRyqfEL BhG5ZneVkTF9bRo5tCCiGvsNVcpWUKCNmPy0vd1ZqJvuNoQxq1qR6f9cnB7vi+C19Hpt 5xUpfFR1k+6B8FbGHFbFXaZtvW/H2sqgRRevhtG9hluShqAQzpot/WQd/qsXuDNgOnpx pk3tmGjn1iUOEgqZ0Ek3rQdbQ5Ky98ttJ9TAom0YVfPe7HTavUoUAmMw/LDuEdRosyc4 CHRnQ9rCEYyXJCmVNUHBEGkpIQ2ljog3inmdpJcdTAOUNGgtwJP32aGziItUDdOFTV+8 kKew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942658; x=1684534658; 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=ur77tHINwX+qbUdlSGnRNMwUnwDaAVVcewhgSBhFUx0=; b=AHU+42AFqdaTaypWAbiszJbzJV6H1lOPfo/pLO4kG3dnTUFhc05bTau+yBC3R3XuAn GovutDJUYi+TkoMhytatgzmdj1xr7q6JCxRMGdo0P+lDjGjDEONeIyNk8sVoHnEDwLhj aozRlOw61EJ6JV/cFBDTY10FhUi5ZC55oFyOVKAkAAbBo0csHnca3yLQXJRhJ8wBfS9K e5PksOlSatUobYOgjanQ+H2tleFIjx9LyuQ5i7JsxbHUIRloP8S+kZ//BndgVwLGY1CT U6X0fijr6X5Kt9LYMEkYUMKJssQvY2QZptNst50PNSX8pdyS6Fj92nIKqC4pPiRbAEHL i9Bw== X-Gm-Message-State: AAQBX9dd98ouK7Y8hnb71+nLwcKlGkvrvLiM1d5FyS35/G4o7KxwNoB4 tpu06YeBFlAaiBIKB0/qDCk1TA== X-Google-Smtp-Source: AKy350ZqgGYrfhLdHBkYHvd+cLv1LSY5KY8FQdwstau4aKvHx0NnJWKbJ8fq3mAaGG3s8B2dSOXS2Q== X-Received: by 2002:a17:903:1210:b0:1a6:4a64:4d27 with SMTP id l16-20020a170903121000b001a64a644d27mr7782580plh.40.1681942658444; Wed, 19 Apr 2023 15:17:38 -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.17.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:17:38 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 04/48] RISC-V: KVM: Add a helper function to get pgd size Date: Wed, 19 Apr 2023 15:16:32 -0700 Message-Id: <20230419221716.3603068-5-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_231947_719069_BDC2D128 X-CRM114-Status: GOOD ( 11.72 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org The cove support will require to find out the pgd size to be used for the gstage page table directory. Export the value via an additional helper. Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_host.h | 1 + arch/riscv/kvm/mmu.c | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h index 8714325..63c46af 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -313,6 +313,7 @@ void kvm_riscv_gstage_update_hgatp(struct kvm_vcpu *vcpu); void __init kvm_riscv_gstage_mode_detect(void); unsigned long __init kvm_riscv_gstage_mode(void); int kvm_riscv_gstage_gpa_bits(void); +unsigned long kvm_riscv_gstage_pgd_size(void); void __init kvm_riscv_gstage_vmid_detect(void); unsigned long kvm_riscv_gstage_vmid_bits(void); diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c index f0fff56..6b037f7 100644 --- a/arch/riscv/kvm/mmu.c +++ b/arch/riscv/kvm/mmu.c @@ -797,3 +797,8 @@ int kvm_riscv_gstage_gpa_bits(void) { return gstage_gpa_bits; } + +unsigned long kvm_riscv_gstage_pgd_size(void) +{ + return gstage_pgd_size; +} From patchwork Wed Apr 19 22:16:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217665 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 E697EC77B73 for ; Wed, 19 Apr 2023 23:12:42 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=pdprnsNgGWENRw5O5N2f2nBpsq66Qg5wY690PJJkVkQ=; b=hpzYtkRgsSpKH8 CxAlgq97NNjnD4A1DCrYmBzwwTWOTA9seqDv9VDGg39pg4RKzgTuSPnrovC/lv7alf6sY7pCvnlN8 Mr3LC7A0B2jiC56wEbi0npQFj9YiKfcgjCn4kZy2LvIAkHyL+Vdg4zZ/FYlkBtKU3W2DZGlnpjS/A Mc0HTy/l2iwarrl6Dv+cPuBTBirefWUIcSklMsk08uUoInEIvo53zpbgcbGhZJO75U+a9Afh5LPsJ xe3T7eI0Ng1jtYmKVDdOyAxX1Fr32lYNRAkD9YvDdFQ/nAA1EUvaBRbyusQZtzyLhA8JseaHwyPxf C+iaSsqYVS8lAcQQ/www==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGyb-006ck1-0q; Wed, 19 Apr 2023 23:12:37 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGyZ-006cjg-1C for linux-riscv@bombadil.infradead.org; Wed, 19 Apr 2023 23:12:35 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:MIME-Version :References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=YhmpWINWx68bxRl5iJymJMR8koMWZ3k0vMo650bJZTU=; b=WHnX7sHlsVqi8VMckMDhRra9vO EI+BD+ALcmEcin/JwbXg7GvwVjnQX3c2QUQDahioTnboQf5v14aMsjD9DT1sJkeRfAAWb9hoQDZ3X vZLMdLvFGli6LtLCfoPGNrWUVSeSoMoqP+vUDfsftujdw4PLg0NpJGw5tknaiSJjnCYKVZtLA117k i6ZmY70gzRmi3CsZFHqdnmiKeWvIQQ+aYpmdyhXmj0tN0qQs/XniXBV6rirL59AUurARDN1BjNBrd qCZ5LhbueLS6/s0V14DZ7utOVsX1T6FIvkhc7FO4NS8ZtWekCEKTXVsGb4uTeUNo/oO5K/I1NUVLx gd1aRmxA==; Received: from mail-pg1-x530.google.com ([2607:f8b0:4864:20::530]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG7T-0021HF-1e for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:19:51 +0000 Received: by mail-pg1-x530.google.com with SMTP id 41be03b00d2f7-51b5490c6f0so264570a12.0 for ; Wed, 19 Apr 2023 15:17:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942660; x=1684534660; 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=YhmpWINWx68bxRl5iJymJMR8koMWZ3k0vMo650bJZTU=; b=ub984HbNYnZCS4cYMVeGAOsRXSkOK2FPD4icQUHZlHix4aKpIqyXeuVfQOuD1xHj9v vsKnnlhxdpBXKMy0/ROqMjpZOfEnWXQbDuHp5T383/Vm/+FQmuzlPZpnUGhvlMFFe7rt 8HkCX+VPliU1MmnP1Ss2H9IInj7b1S/ZGGfNasP3k5lYx2EUDdBQbfa+gCsPCMfJr4EM doY/zh1GbMfgfCGSFTcIGG6F7Q7zICkSwwBAXaUPKIzJ8MoIZKG1DSX+I00o1o0F5UZi WiH33xlJMuVTKRgbJ22ofZQJq8VWsCC+jXY14LMcf4NAwgljQxOy08LcxeWTlGXumKfv zGJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942660; x=1684534660; 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=YhmpWINWx68bxRl5iJymJMR8koMWZ3k0vMo650bJZTU=; b=Z06qaVox+4rc2+8r8Uv4+lx2OfZNqdiRuchLNivLeI7fsu92Ejnqouxopugiji8gFG FuWtJNlYd7CWV5L1Pms9Q17EMcvnfrljhTGjX+q3TEnm+HaytPhEC7syLLZCq6BUize2 I5UozFuT6sMes4SiScuWumvkHriqssbcjmBWZBI/ZicRmIj1nhUnMu8pNK/2tNE0T28/ KlrkhafUXMQQTBuwtnDVaLxCyiq08NXiaC8XzB7vZ1FxmW5pFFrJTJPcGQo1RREa4htu rdBVKIo5siHFOae19ikt1dTcywj3T+o//xRNORpMOxiXzwRs/d2aRheKB+pckfZzjeAn ly7w== X-Gm-Message-State: AAQBX9fGw6HhOI8uhN1xWW8i7xyAht//mWd0xEAZo7wegmYBtO2lP0AT eJ51zBhcbNFsLRmQ4QJ93PMrLg== X-Google-Smtp-Source: AKy350b9J0w/WoMpaq+FxHZfAsQ/ItbJjVOrBVDU6LQmeWRXpZ0GrubHeMgcOdfazZVEwzQcIEKGsA== X-Received: by 2002:a17:90a:f696:b0:246:a782:d94 with SMTP id cl22-20020a17090af69600b00246a7820d94mr4315223pjb.7.1681942660612; Wed, 19 Apr 2023 15:17:40 -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.17.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:17:40 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 05/48] RISC-V: Add COVH SBI extensions definitions Date: Wed, 19 Apr 2023 15:16:33 -0700 Message-Id: <20230419221716.3603068-6-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_231947_705176_0D8EA0D7 X-CRM114-Status: GOOD ( 12.77 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org RISC-V Confidential Virtualization Extension(COVE) specification defines following 3 SBI extensions. COVH (Host side interface) COVG (Guest side interface) COVI (Interrupt management interface) Few acronyms introduced in this patch: TSM - TEE Security Manager TVM - TEE VM This patch adds the definitions for COVH extension only. Signed-off-by: Atish Patra --- arch/riscv/include/asm/sbi.h | 61 ++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/arch/riscv/include/asm/sbi.h b/arch/riscv/include/asm/sbi.h index 62d00c7..c5a5526 100644 --- a/arch/riscv/include/asm/sbi.h +++ b/arch/riscv/include/asm/sbi.h @@ -32,6 +32,7 @@ enum sbi_ext_id { SBI_EXT_PMU = 0x504D55, SBI_EXT_DBCN = 0x4442434E, SBI_EXT_NACL = 0x4E41434C, + SBI_EXT_COVH = 0x434F5648, /* Experimentals extensions must lie within this range */ SBI_EXT_EXPERIMENTAL_START = 0x08000000, @@ -348,6 +349,66 @@ enum sbi_ext_nacl_feature { #define SBI_NACL_SHMEM_SRET_X(__i) ((__riscv_xlen / 8) * (__i)) #define SBI_NACL_SHMEM_SRET_X_LAST 31 +/* SBI COVH extension data structures */ +enum sbi_ext_covh_fid { + SBI_EXT_COVH_TSM_GET_INFO = 0, + SBI_EXT_COVH_TSM_CONVERT_PAGES, + SBI_EXT_COVH_TSM_RECLAIM_PAGES, + SBI_EXT_COVH_TSM_INITIATE_FENCE, + SBI_EXT_COVH_TSM_LOCAL_FENCE, + SBI_EXT_COVH_CREATE_TVM, + SBI_EXT_COVH_FINALIZE_TVM, + SBI_EXT_COVH_DESTROY_TVM, + SBI_EXT_COVH_TVM_ADD_MEMORY_REGION, + SBI_EXT_COVH_TVM_ADD_PGT_PAGES, + SBI_EXT_COVH_TVM_ADD_MEASURED_PAGES, + SBI_EXT_COVH_TVM_ADD_ZERO_PAGES, + SBI_EXT_COVH_TVM_ADD_SHARED_PAGES, + SBI_EXT_COVH_TVM_CREATE_VCPU, + SBI_EXT_COVH_TVM_VCPU_RUN, + SBI_EXT_COVH_TVM_INITIATE_FENCE, +}; + +enum sbi_cove_page_type { + SBI_COVE_PAGE_4K, + SBI_COVE_PAGE_2MB, + SBI_COVE_PAGE_1GB, + SBI_COVE_PAGE_512GB, +}; + +enum sbi_cove_tsm_state { + /* TSM has not been loaded yet */ + TSM_NOT_LOADED, + /* TSM has been loaded but not initialized yet */ + TSM_LOADED, + /* TSM has been initialized and ready to run */ + TSM_READY, +}; + +struct sbi_cove_tsm_info { + /* Current state of the TSM */ + enum sbi_cove_tsm_state tstate; + + /* Version of the loaded TSM */ + uint32_t version; + + /* Number of 4K pages required per TVM */ + unsigned long tvm_pages_needed; + + /* Maximum VCPUs supported per TVM */ + unsigned long tvm_max_vcpus; + + /* Number of 4K pages each vcpu per TVM */ + unsigned long tvcpu_pages_needed; +}; + +struct sbi_cove_tvm_create_params { + /* Root page directory for TVM's page table management */ + unsigned long tvm_page_directory_addr; + /* Confidential memory address used to store TVM state information. Must be page aligned */ + unsigned long tvm_state_addr; +}; + #define SBI_SPEC_VERSION_DEFAULT 0x1 #define SBI_SPEC_VERSION_MAJOR_SHIFT 24 #define SBI_SPEC_VERSION_MAJOR_MASK 0x7f From patchwork Wed Apr 19 22:16:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217679 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 C5D07C77B7C for ; Wed, 19 Apr 2023 23:31:01 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Ase9LrzmRvu0zuQGy3xVq6UvXQOL36xj10FLU6A/Jbo=; b=Fcab0qhWLJ6WOG Cg6iYdW26vrcKPyqsM6vw3E6YB3dlHUKG0UQqxxQfkZ7RXA5Mx0MzBTrhl+hFcdWA9CU6nV+MmwPs mqutDlbfMEbaiKvxjuT8uHcCLtymNsJxvEoUi5g3gvwnXwUFuAn/lujBKf9FKc8/+m/nKkJHQJgch 8ssX4KtdTDc8nMjWrA1M4up3urr2sWeOi3Pfls/d5oYJ7B/DU/HKOW2UbYFTJYCyJA56LlR4Zjzt1 bluG5pgeo7w/ZaEXqSUMxdssZ6A7y96NOIkMRVGGeVRmPRdyHXaHde+DaHnb8Op7HYCpT8PxrKe/1 SDbHQwhPFSQjKuqyoCZA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppHG6-006eV0-1U; Wed, 19 Apr 2023 23:30:42 +0000 Received: from mail-pj1-x102c.google.com ([2607:f8b0:4864:20::102c]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG7W-006SuK-2K for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:17:50 +0000 Received: by mail-pj1-x102c.google.com with SMTP id 98e67ed59e1d1-2474e09fdcfso226993a91.0 for ; Wed, 19 Apr 2023 15:17:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942663; x=1684534663; 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=MC6vnkFRr+LS0VzfoACyhzPionL0+vOgt4l/y7HAvLQ=; b=DRAvwidPC7IdYDxLvMZPx6rD17zG/oS72Z/5NPbNssSMeEjGZ/qFXxKzxUoDWrBvaq DZ0TInEL8TrdR76cTRhzAJ5vG3gQXDbbiRYVKe5TFYSlezfrU3NDmW+/vfPxscuEdQxg jXWWGJNbmSYg4oA+SEn/VeITfEYeL5F6eAjq1+89vsTHHHYZkIRXRhIdgP4FIkoXVHnV bl38T3t+0HkBGPH61JvdtrFKDQGGEV6QVexZiROWg23DV082ptqUszDkbJ0kgnR2cFa0 h+Xapq4PsL1WId18x50ElV1iwBPVNVRSYCKwHvchYfyD9yud1mQMh17Z1wEgoBq7zr/0 kh+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942663; x=1684534663; 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=MC6vnkFRr+LS0VzfoACyhzPionL0+vOgt4l/y7HAvLQ=; b=Iv8kI1uPltJDW/CO0R06/zXy5SHYNCi/QtNOsIBaN76V52Xg3FvGCXjUhWAvT+7ZYZ aEOYzrbE4DSKoWGz9V/RyLzPHdzikCtw1DspSZ9Kf/vzXCcdO8dcw/v2lZSZ/9yr/s8+ lp33z2USdC+FEC9OTh1DZ4TUugzUTADU3No7fswThn77lH5m8pifj2nWF24FhTcl+6yD tShSx3KxMEKvwgDIy8fWOjPKFlA0cEagyBuafGqpoZz1DhrZfVAxfdzurVjz/sUZmxjt aEvlLpGfimurQldvYpJsewKy3x94sHIpDDlw2lkl8Ts165xjCWC0bjC/zaiAvE6nGHfq 0zxw== X-Gm-Message-State: AAQBX9dhQ2fg71Xn7z8PldnMQ5UM7reIFlX552PgtMf7O1XWw3ARNfG9 p+ezj1mc29zSA+0tr87aemNcZw== X-Google-Smtp-Source: AKy350YM0J4iT9dthj2PQFzxPsPzjGXv4OgXVXSGvlPqAIkruWzwbcoYuc98tklz6iIJwKCz8HSgyw== X-Received: by 2002:a17:90a:f3c4:b0:247:6364:b8d8 with SMTP id ha4-20020a17090af3c400b002476364b8d8mr4026019pjb.6.1681942662828; Wed, 19 Apr 2023 15:17:42 -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.17.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:17:42 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 06/48] RISC-V: KVM: Implement COVH SBI extension Date: Wed, 19 Apr 2023 15:16:34 -0700 Message-Id: <20230419221716.3603068-7-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151746_758682_82C106EA X-CRM114-Status: GOOD ( 20.26 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org COVH SBI extension defines the SBI functions that the host will invoke to configure/create/destroy a TEE VM (TVM). Implement all the COVH SBI extension functions. Signed-off-by: Atish Patra --- arch/riscv/Kconfig | 13 ++ arch/riscv/include/asm/kvm_cove_sbi.h | 46 +++++ arch/riscv/kvm/Makefile | 1 + arch/riscv/kvm/cove_sbi.c | 245 ++++++++++++++++++++++++++ 4 files changed, 305 insertions(+) create mode 100644 arch/riscv/include/asm/kvm_cove_sbi.h create mode 100644 arch/riscv/kvm/cove_sbi.c diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 4044080..8462941 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -501,6 +501,19 @@ config FPU If you don't know what to do here, say Y. +menu "Confidential VM Extension(CoVE) Support" + +config RISCV_COVE_HOST + bool "Host(KVM) support for Confidential VM Extension(CoVE)" + depends on KVM + default n + help + Enable this if the platform supports confidential vm extension. + That means the platform should be capable of running TEE VM (TVM) + using KVM and TEE Security Manager (TSM). + +endmenu # "Confidential VM Extension(CoVE) Support" + endmenu # "Platform type" menu "Kernel features" diff --git a/arch/riscv/include/asm/kvm_cove_sbi.h b/arch/riscv/include/asm/kvm_cove_sbi.h new file mode 100644 index 0000000..24562df --- /dev/null +++ b/arch/riscv/include/asm/kvm_cove_sbi.h @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * COVE SBI extension related header file. + * + * Copyright (c) 2023 RivosInc + * + * Authors: + * Atish Patra + */ + +#ifndef __KVM_COVE_SBI_H +#define __KVM_COVE_SBI_H + +#include +#include +#include +#include +#include + +int sbi_covh_tsm_get_info(struct sbi_cove_tsm_info *tinfo_addr); +int sbi_covh_tvm_initiate_fence(unsigned long tvmid); +int sbi_covh_tsm_initiate_fence(void); +int sbi_covh_tsm_local_fence(void); +int sbi_covh_tsm_create_tvm(struct sbi_cove_tvm_create_params *tparam, unsigned long *tvmid); +int sbi_covh_tsm_finalize_tvm(unsigned long tvmid, unsigned long sepc, unsigned long entry_arg); +int sbi_covh_tsm_destroy_tvm(unsigned long tvmid); +int sbi_covh_add_memory_region(unsigned long tvmid, unsigned long tgpadr, unsigned long rlen); + +int sbi_covh_tsm_reclaim_pages(unsigned long phys_addr, unsigned long npages); +int sbi_covh_tsm_convert_pages(unsigned long phys_addr, unsigned long npages); +int sbi_covh_tsm_reclaim_page(unsigned long page_addr_phys); +int sbi_covh_add_pgt_pages(unsigned long tvmid, unsigned long page_addr_phys, unsigned long npages); + +int sbi_covh_add_measured_pages(unsigned long tvmid, unsigned long src_addr, + unsigned long dest_addr, enum sbi_cove_page_type ptype, + unsigned long npages, unsigned long tgpa); +int sbi_covh_add_zero_pages(unsigned long tvmid, unsigned long page_addr_phys, + enum sbi_cove_page_type ptype, unsigned long npages, + unsigned long tvm_base_page_addr); + +int sbi_covh_create_tvm_vcpu(unsigned long tvmid, unsigned long tvm_vcpuid, + unsigned long vpus_page_addr); + +int sbi_covh_run_tvm_vcpu(unsigned long tvmid, unsigned long tvm_vcpuid); + +#endif diff --git a/arch/riscv/kvm/Makefile b/arch/riscv/kvm/Makefile index 6986d3c..40dee04 100644 --- a/arch/riscv/kvm/Makefile +++ b/arch/riscv/kvm/Makefile @@ -31,3 +31,4 @@ kvm-y += aia.o kvm-y += aia_device.o kvm-y += aia_aplic.o kvm-y += aia_imsic.o +kvm-$(CONFIG_RISCV_COVE_HOST) += cove_sbi.o diff --git a/arch/riscv/kvm/cove_sbi.c b/arch/riscv/kvm/cove_sbi.c new file mode 100644 index 0000000..c8c63fe --- /dev/null +++ b/arch/riscv/kvm/cove_sbi.c @@ -0,0 +1,245 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * COVE SBI extensions related helper functions. + * + * Copyright (c) 2023 RivosInc + * + * Authors: + * Atish Patra + */ + +#include +#include +#include +#include +#include +#include +#include + +#define RISCV_COVE_ALIGN_4KB (1UL << 12) + +int sbi_covh_tsm_get_info(struct sbi_cove_tsm_info *tinfo_addr) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVH, SBI_EXT_COVH_TSM_GET_INFO, __pa(tinfo_addr), + sizeof(*tinfo_addr), 0, 0, 0, 0); + + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covh_tvm_initiate_fence(unsigned long tvmid) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVH, SBI_EXT_COVH_TVM_INITIATE_FENCE, tvmid, 0, 0, 0, 0, 0); + + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covh_tsm_initiate_fence(void) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVH, SBI_EXT_COVH_TSM_INITIATE_FENCE, 0, 0, 0, 0, 0, 0); + + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covh_tsm_local_fence(void) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVH, SBI_EXT_COVH_TSM_LOCAL_FENCE, 0, 0, 0, 0, 0, 0); + + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covh_tsm_create_tvm(struct sbi_cove_tvm_create_params *tparam, unsigned long *tvmid) +{ + struct sbiret ret; + int rc = 0; + + ret = sbi_ecall(SBI_EXT_COVH, SBI_EXT_COVH_CREATE_TVM, __pa(tparam), + sizeof(*tparam), 0, 0, 0, 0); + + if (ret.error) { + rc = sbi_err_map_linux_errno(ret.error); + if (rc == -EFAULT) + kvm_err("Invalid phsyical address for tvm params structure\n"); + goto done; + } + + kvm_info("%s: create_tvm tvmid %lx\n", __func__, ret.value); + *tvmid = ret.value; + +done: + return rc; +} + +int sbi_covh_tsm_finalize_tvm(unsigned long tvmid, unsigned long sepc, unsigned long entry_arg) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVH, SBI_EXT_COVH_FINALIZE_TVM, tvmid, + sepc, entry_arg, 0, 0, 0); + + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covh_tsm_destroy_tvm(unsigned long tvmid) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVH, SBI_EXT_COVH_DESTROY_TVM, tvmid, + 0, 0, 0, 0, 0); + + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covh_add_memory_region(unsigned long tvmid, unsigned long tgpaddr, unsigned long rlen) +{ + struct sbiret ret; + + if (!IS_ALIGNED(tgpaddr, RISCV_COVE_ALIGN_4KB) || !IS_ALIGNED(rlen, RISCV_COVE_ALIGN_4KB)) + return -EINVAL; + + ret = sbi_ecall(SBI_EXT_COVH, SBI_EXT_COVH_TVM_ADD_MEMORY_REGION, tvmid, + tgpaddr, rlen, 0, 0, 0); + if (ret.error) { + kvm_err("Add memory region failed with sbi error code %ld\n", ret.error); + return sbi_err_map_linux_errno(ret.error); + } + + return 0; +} + +int sbi_covh_tsm_convert_pages(unsigned long phys_addr, unsigned long npages) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVH, SBI_EXT_COVH_TSM_CONVERT_PAGES, phys_addr, + npages, 0, 0, 0, 0); + if (ret.error) { + kvm_err("Convert pages failed ret %ld\n", ret.error); + return sbi_err_map_linux_errno(ret.error); + } + return 0; +} + +int sbi_covh_tsm_reclaim_page(unsigned long page_addr_phys) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVH, SBI_EXT_COVH_TSM_RECLAIM_PAGES, page_addr_phys, + 1, 0, 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covh_tsm_reclaim_pages(unsigned long phys_addr, unsigned long npages) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVH, SBI_EXT_COVH_TSM_RECLAIM_PAGES, phys_addr, + npages, 0, 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covh_add_pgt_pages(unsigned long tvmid, unsigned long page_addr_phys, unsigned long npages) +{ + struct sbiret ret; + + if (!PAGE_ALIGNED(page_addr_phys)) + return -EINVAL; + + ret = sbi_ecall(SBI_EXT_COVH, SBI_EXT_COVH_TVM_ADD_PGT_PAGES, tvmid, page_addr_phys, + npages, 0, 0, 0); + if (ret.error) { + kvm_err("Adding page table pages at %lx failed %ld\n", page_addr_phys, ret.error); + return sbi_err_map_linux_errno(ret.error); + } + + return 0; +} + +int sbi_covh_add_measured_pages(unsigned long tvmid, unsigned long src_addr, + unsigned long dest_addr, enum sbi_cove_page_type ptype, + unsigned long npages, unsigned long tgpa) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVH, SBI_EXT_COVH_TVM_ADD_MEASURED_PAGES, tvmid, src_addr, + dest_addr, ptype, npages, tgpa); + if (ret.error) { + kvm_err("Adding measued pages failed ret %ld\n", ret.error); + return sbi_err_map_linux_errno(ret.error); + } + + return 0; +} + +int sbi_covh_add_zero_pages(unsigned long tvmid, unsigned long page_addr_phys, + enum sbi_cove_page_type ptype, unsigned long npages, + unsigned long tvm_base_page_addr) +{ + struct sbiret ret; + + if (!PAGE_ALIGNED(page_addr_phys)) + return -EINVAL; + ret = sbi_ecall(SBI_EXT_COVH, SBI_EXT_COVH_TVM_ADD_ZERO_PAGES, tvmid, page_addr_phys, + ptype, npages, tvm_base_page_addr, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covh_create_tvm_vcpu(unsigned long tvmid, unsigned long vcpuid, + unsigned long vcpu_state_paddr) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVH, SBI_EXT_COVH_TVM_CREATE_VCPU, tvmid, vcpuid, + vcpu_state_paddr, 0, 0, 0); + if (ret.error) { + kvm_err("create vcpu failed ret %ld\n", ret.error); + return sbi_err_map_linux_errno(ret.error); + } + return 0; +} + +int sbi_covh_run_tvm_vcpu(unsigned long tvmid, unsigned long vcpuid) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVH, SBI_EXT_COVH_TVM_VCPU_RUN, tvmid, vcpuid, 0, 0, 0, 0); + /* Non-zero return value indicate the vcpu is already terminated */ + if (ret.error || !ret.value) + return ret.error ? sbi_err_map_linux_errno(ret.error) : ret.value; + + return 0; +} From patchwork Wed Apr 19 22:16:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217661 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 64512C6FD18 for ; Wed, 19 Apr 2023 23:06:49 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=m1EjcOJgGKCO8Ug0OS3hXNQz3b8VOei8B+60UvsN40w=; b=sIMAVamTsDfXFy 9xCaBjIMN5uF+qs9kCnoidaUTwmkh6bgtj/2N9J+1wVbhYIQ6gBN2vbgTQ4OkDEbMcZbKVBWtKnA1 Ewm4hBAr0WW5bv2Q5KaySGB2w5pAD1d9OTL6FDtb3ztbm380u4jMr/R7cw5BY1lnLM2vHkLhhFvYh nm1N0zfJZNvi+QlwOg2pc7Wo7A13giyaiyj8Lsd9tZk8DNHbcMK0Vj51UqDXV+3mEkoeD93lcjOR6 +LjGatcoodKVale1xZDItGC89mx812yTRrGwWTgMFUkXwnIUfJxXl/3KWQyo9x1hW5ljUwl9yW7A2 SuWQCAsPvH298io1xUuw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGst-006byU-00; Wed, 19 Apr 2023 23:06:43 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGsq-006byE-2s for linux-riscv@bombadil.infradead.org; Wed, 19 Apr 2023 23:06:41 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:MIME-Version :References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=ozf7CgfDu6QfbrVSJ88D8CUhAzN4Z1ozQ6Paui1PG2Q=; b=eT5Xd7SMyjH4QnFT1DTGKYRh8Q rRHE77fF7XOcMC5xPf2ulx9XHYL5P7BuPox74/10lrIpEJkH/3omjqh5enfotrSUcBc3Ad+LK6i2d qC6MB0y9jtQsu6svpPzfzsXOZq7F6UqRfgtfxV7Jl7v7SIIMysjGxbBhEH0GuIgxr9llq3WzlopkI sD6GVCC90hif1PbClefoYSe0dpfzvGr88n3tKLktNH/uNRYs6MAMb+qomdNo7vgkPmbPTADxTD9W2 qzobK1OH7AukNfsg8M8xPEcxfx6u9Y7j5SOmveaBMCg6V/EBMNAD3VW0RxH7LbDbrY4Z/7/RJk/2b m5ElyzrA==; Received: from mail-pf1-x431.google.com ([2607:f8b0:4864:20::431]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG7a-0021HP-21 for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:20:58 +0000 Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-63b87d23729so355933b3a.0 for ; Wed, 19 Apr 2023 15:17:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942665; x=1684534665; 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=ozf7CgfDu6QfbrVSJ88D8CUhAzN4Z1ozQ6Paui1PG2Q=; b=UiEEqgIiFGjQrlRc05xi8M/8WHKF79aVIaVCTtJDmslx64Vz9QBOTfjDBhPpfGvCZM hD6Trwo85wezLe+LkfwiK3xPiE6mOt9Shkgc2tP3Y/1nUYrDuHN5QdD+FGshzmUzMxq4 mejPehx72O0SUsimaMCZdOowioZ7cAJ9dbR/w4XBmGXhQPqlCzQ4x0w5/UsyzImcsr+6 0jU+qDjHWtK8/W2MYoBsF8SijK/drU0MOEOFRuhfuuakyHvRuiRniJi5FDWwOeJwfZL2 B/tTxcEapPl4p4WTn0L3HBdaLB5N3pSCGJN12RxBZo6KQjdkqGjifVeYXsUH3bU3gjvu nEXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942665; x=1684534665; 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=ozf7CgfDu6QfbrVSJ88D8CUhAzN4Z1ozQ6Paui1PG2Q=; b=VkHGX3cYTRWIc/6D5A/tVe70k531JfNZyqcigI6WdtGvmyzb4h8GRCUcaXq6+5VY85 UEwdlZXRjgn115bFJY28iq6KqL6TWsMPV4voMsg3TYhltOkQSKe8AeT+4tn4tmjkMlU8 AnQ2bN7/acuxc2Ke7ZXQSTVKyuhmJ/BlT29oRY4Sk+7H1t5VjAtZKLWZ2J2mRmhr8w3T HQy1tvazW6Hl6fHh31grXi8kOnxs3WzXXW5lpLB2mR+xSIn6ePU1V002jURJJ54CLwIt m7Ebsl0qmyYKd2kHOJCT+EFQNo01b5xB9eXASteiAvhK189A9f4zGjCq16a1wGPaG9kv Vhnw== X-Gm-Message-State: AAQBX9cFY6lR2k2/nUa1ihaw7cZZ3J5J1Wp+tMmNaeDXF3GAoRSXHhxI Z+3c0ARrY7+Y1Fe8CuFDL58lag== X-Google-Smtp-Source: AKy350blkLhMRfe6KzTyuHMpWHXcb6B+q1BXTSTdLN78RbOAC8KzxAc9EsSMEhzo4VgDG1Ea+hcCQw== X-Received: by 2002:a17:902:d48c:b0:1a6:e58f:8aee with SMTP id c12-20020a170902d48c00b001a6e58f8aeemr7335979plg.65.1681942665252; Wed, 19 Apr 2023 15:17:45 -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.17.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:17:44 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 07/48] RISC-V: KVM: Add a barebone CoVE implementation Date: Wed, 19 Apr 2023 15:16:35 -0700 Message-Id: <20230419221716.3603068-8-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_232042_858615_813F313F X-CRM114-Status: GOOD ( 29.16 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org This patch just adds a barebone implementation of CoVE functionality that exercises the COVH functions to create/manage pages for various boot time operations such as page directory, page table management, vcpu/vm state management etc. Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_cove.h | 154 ++++++++++++ arch/riscv/include/asm/kvm_host.h | 7 + arch/riscv/kvm/Makefile | 2 +- arch/riscv/kvm/cove.c | 401 ++++++++++++++++++++++++++++++ arch/riscv/kvm/cove_sbi.c | 2 - include/uapi/linux/kvm.h | 6 + 6 files changed, 569 insertions(+), 3 deletions(-) create mode 100644 arch/riscv/include/asm/kvm_cove.h create mode 100644 arch/riscv/kvm/cove.c diff --git a/arch/riscv/include/asm/kvm_cove.h b/arch/riscv/include/asm/kvm_cove.h new file mode 100644 index 0000000..3bf1bcd --- /dev/null +++ b/arch/riscv/include/asm/kvm_cove.h @@ -0,0 +1,154 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * COVE related header file. + * + * Copyright (c) 2023 RivosInc + * + * Authors: + * Atish Patra + */ + +#ifndef __KVM_RISCV_COVE_H +#define __KVM_RISCV_COVE_H + +#include +#include +#include +#include +#include +#include +#include + +#define KVM_COVE_PAGE_SIZE_4K (1UL << 12) +#define KVM_COVE_PAGE_SIZE_2MB (1UL << 21) +#define KVM_COVE_PAGE_SIZE_1GB (1UL << 30) +#define KVM_COVE_PAGE_SIZE_512GB (1UL << 39) + +#define bytes_to_pages(n) ((n + PAGE_SIZE - 1) >> PAGE_SHIFT) + +/* Allocate 2MB(i.e. 512 pages) for the page table pool */ +#define KVM_COVE_PGTABLE_SIZE_MAX ((1UL << 10) * PAGE_SIZE) + +#define get_order_num_pages(n) (get_order(n << PAGE_SHIFT)) + +/* Describe a confidential or shared memory region */ +struct kvm_riscv_cove_mem_region { + unsigned long hva; + unsigned long gpa; + unsigned long npages; +}; + +/* Page management structure for the host */ +struct kvm_riscv_cove_page { + struct list_head link; + + /* Pointer to page allocated */ + struct page *page; + + /* number of pages allocated for page */ + unsigned long npages; + + /* Described the page type */ + unsigned long ptype; + + /* set if the page is mapped in guest physical address */ + bool is_mapped; + + /* The below two fileds are only valid if is_mapped is true */ + /* host virtual address for the mapping */ + unsigned long hva; + /* guest physical address for the mapping */ + unsigned long gpa; +}; + +struct kvm_cove_tvm_vcpu_context { + struct kvm_vcpu *vcpu; + /* Pages storing each vcpu state of the TVM in TSM */ + struct kvm_riscv_cove_page vcpu_state; +}; + +struct kvm_cove_tvm_context { + struct kvm *kvm; + + /* TODO: This is not really a VMID as TSM returns the page owner ID instead of VMID */ + unsigned long tvm_guest_id; + + /* Pages where TVM page table is stored */ + struct kvm_riscv_cove_page pgtable; + + /* Pages storing the TVM state in TSM */ + struct kvm_riscv_cove_page tvm_state; + + /* Keep track of zero pages */ + struct list_head zero_pages; + + /* Pages where TVM image is measured & loaded */ + struct list_head measured_pages; + + /* keep track of shared pages */ + struct list_head shared_pages; + + /* keep track of pending reclaim confidential pages */ + struct list_head reclaim_pending_pages; + + struct kvm_riscv_cove_mem_region shared_region; + struct kvm_riscv_cove_mem_region confidential_region; + + /* spinlock to protect the tvm fence sequence */ + spinlock_t tvm_fence_lock; + + /* Track TVM state */ + bool finalized_done; +}; + +static inline bool is_cove_vm(struct kvm *kvm) +{ + return kvm->arch.vm_type == KVM_VM_TYPE_RISCV_COVE; +} + +static inline bool is_cove_vcpu(struct kvm_vcpu *vcpu) +{ + return is_cove_vm(vcpu->kvm); +} + +#ifdef CONFIG_RISCV_COVE_HOST + +bool kvm_riscv_cove_enabled(void); +int kvm_riscv_cove_init(void); + +/* TVM related functions */ +void kvm_riscv_cove_vm_destroy(struct kvm *kvm); +int kvm_riscv_cove_vm_init(struct kvm *kvm); + +/* TVM VCPU related functions */ +void kvm_riscv_cove_vcpu_destroy(struct kvm_vcpu *vcpu); +int kvm_riscv_cove_vcpu_init(struct kvm_vcpu *vcpu); +void kvm_riscv_cove_vcpu_load(struct kvm_vcpu *vcpu); +void kvm_riscv_cove_vcpu_put(struct kvm_vcpu *vcpu); +void kvm_riscv_cove_vcpu_switchto(struct kvm_vcpu *vcpu, struct kvm_cpu_trap *trap); + +int kvm_riscv_cove_vm_add_memreg(struct kvm *kvm, unsigned long gpa, unsigned long size); +int kvm_riscv_cove_gstage_map(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned long hva); +#else +static inline bool kvm_riscv_cove_enabled(void) {return false; }; +static inline int kvm_riscv_cove_init(void) { return -1; } +static inline void kvm_riscv_cove_hardware_disable(void) {} +static inline int kvm_riscv_cove_hardware_enable(void) {return 0; } + +/* TVM related functions */ +static inline void kvm_riscv_cove_vm_destroy(struct kvm *kvm) {} +static inline int kvm_riscv_cove_vm_init(struct kvm *kvm) {return -1; } + +/* TVM VCPU related functions */ +static inline void kvm_riscv_cove_vcpu_destroy(struct kvm_vcpu *vcpu) {} +static inline int kvm_riscv_cove_vcpu_init(struct kvm_vcpu *vcpu) {return -1; } +static inline void kvm_riscv_cove_vcpu_load(struct kvm_vcpu *vcpu) {} +static inline void kvm_riscv_cove_vcpu_put(struct kvm_vcpu *vcpu) {} +static inline void kvm_riscv_cove_vcpu_switchto(struct kvm_vcpu *vcpu, struct kvm_cpu_trap *trap) {} +static inline int kvm_riscv_cove_vm_add_memreg(struct kvm *kvm, unsigned long gpa, + unsigned long size) {return -1; } +static inline int kvm_riscv_cove_gstage_map(struct kvm_vcpu *vcpu, + gpa_t gpa, unsigned long hva) {return -1; } +#endif /* CONFIG_RISCV_COVE_HOST */ + +#endif /* __KVM_RISCV_COVE_H */ diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h index 63c46af..ca2ebe3 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -88,6 +88,8 @@ struct kvm_vmid { }; struct kvm_arch { + unsigned long vm_type; + /* G-stage vmid */ struct kvm_vmid vmid; @@ -100,6 +102,9 @@ struct kvm_arch { /* AIA Guest/VM context */ struct kvm_aia aia; + + /* COVE guest/VM context */ + struct kvm_cove_tvm_context *tvmc; }; struct kvm_cpu_trap { @@ -242,6 +247,8 @@ struct kvm_vcpu_arch { /* Performance monitoring context */ struct kvm_pmu pmu_context; + + struct kvm_cove_tvm_vcpu_context *tc; }; static inline void kvm_arch_sync_events(struct kvm *kvm) {} diff --git a/arch/riscv/kvm/Makefile b/arch/riscv/kvm/Makefile index 40dee04..8c91551 100644 --- a/arch/riscv/kvm/Makefile +++ b/arch/riscv/kvm/Makefile @@ -31,4 +31,4 @@ kvm-y += aia.o kvm-y += aia_device.o kvm-y += aia_aplic.o kvm-y += aia_imsic.o -kvm-$(CONFIG_RISCV_COVE_HOST) += cove_sbi.o +kvm-$(CONFIG_RISCV_COVE_HOST) += cove_sbi.o cove.o diff --git a/arch/riscv/kvm/cove.c b/arch/riscv/kvm/cove.c new file mode 100644 index 0000000..d001e36 --- /dev/null +++ b/arch/riscv/kvm/cove.c @@ -0,0 +1,401 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * COVE related helper functions. + * + * Copyright (c) 2023 RivosInc + * + * Authors: + * Atish Patra + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static struct sbi_cove_tsm_info tinfo; +struct sbi_cove_tvm_create_params params; + +/* We need a global lock as initiate fence can be invoked once per host */ +static DEFINE_SPINLOCK(cove_fence_lock); + +static bool riscv_cove_enabled; + +static void kvm_cove_local_fence(void *info) +{ + int rc; + + rc = sbi_covh_tsm_local_fence(); + + if (rc) + kvm_err("local fence for TSM failed %d on cpu %d\n", rc, smp_processor_id()); +} + +static void cove_delete_page_list(struct kvm *kvm, struct list_head *tpages, bool unpin) +{ + struct kvm_riscv_cove_page *tpage, *temp; + int rc; + + list_for_each_entry_safe(tpage, temp, tpages, link) { + rc = sbi_covh_tsm_reclaim_pages(page_to_phys(tpage->page), tpage->npages); + if (rc) + kvm_err("Reclaiming page %llx failed\n", page_to_phys(tpage->page)); + if (unpin) + unpin_user_pages_dirty_lock(&tpage->page, 1, true); + list_del(&tpage->link); + kfree(tpage); + } +} + +static int kvm_riscv_cove_fence(void) +{ + int rc; + + spin_lock(&cove_fence_lock); + + rc = sbi_covh_tsm_initiate_fence(); + if (rc) { + kvm_err("initiate fence for tsm failed %d\n", rc); + goto done; + } + + /* initiate local fence on each online hart */ + on_each_cpu(kvm_cove_local_fence, NULL, 1); +done: + spin_unlock(&cove_fence_lock); + return rc; +} + +static int cove_convert_pages(unsigned long phys_addr, unsigned long npages, bool fence) +{ + int rc; + + if (!IS_ALIGNED(phys_addr, PAGE_SIZE)) + return -EINVAL; + + rc = sbi_covh_tsm_convert_pages(phys_addr, npages); + if (rc) + return rc; + + /* Conversion was successful. Flush the TLB if caller requested */ + if (fence) + rc = kvm_riscv_cove_fence(); + + return rc; +} + +__always_inline bool kvm_riscv_cove_enabled(void) +{ + return riscv_cove_enabled; +} + +void kvm_riscv_cove_vcpu_load(struct kvm_vcpu *vcpu) +{ + /* TODO */ +} + +void kvm_riscv_cove_vcpu_put(struct kvm_vcpu *vcpu) +{ + /* TODO */ +} + +int kvm_riscv_cove_gstage_map(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned long hva) +{ + /* TODO */ + return 0; +} + +void kvm_riscv_cove_vcpu_switchto(struct kvm_vcpu *vcpu, struct kvm_cpu_trap *trap) +{ + /* TODO */ +} + +void kvm_riscv_cove_vcpu_destroy(struct kvm_vcpu *vcpu) +{ + struct kvm_cove_tvm_vcpu_context *tvcpuc = vcpu->arch.tc; + struct kvm *kvm = vcpu->kvm; + + /* + * Just add the vcpu state pages to a list at this point as these can not + * be claimed until tvm is destroyed. * + */ + list_add(&tvcpuc->vcpu_state.link, &kvm->arch.tvmc->reclaim_pending_pages); +} + +int kvm_riscv_cove_vcpu_init(struct kvm_vcpu *vcpu) +{ + int rc; + struct kvm *kvm; + struct kvm_cove_tvm_vcpu_context *tvcpuc; + struct kvm_cove_tvm_context *tvmc; + struct page *vcpus_page; + unsigned long vcpus_phys_addr; + + if (!vcpu) + return -EINVAL; + + kvm = vcpu->kvm; + + if (!kvm->arch.tvmc) + return -EINVAL; + + tvmc = kvm->arch.tvmc; + + if (tvmc->finalized_done) { + kvm_err("vcpu init must not happen after finalize\n"); + return -EINVAL; + } + + tvcpuc = kzalloc(sizeof(*tvcpuc), GFP_KERNEL); + if (!tvcpuc) + return -ENOMEM; + + vcpus_page = alloc_pages(GFP_KERNEL | __GFP_ZERO, + get_order_num_pages(tinfo.tvcpu_pages_needed)); + if (!vcpus_page) { + rc = -ENOMEM; + goto alloc_page_failed; + } + + tvcpuc->vcpu = vcpu; + tvcpuc->vcpu_state.npages = tinfo.tvcpu_pages_needed; + tvcpuc->vcpu_state.page = vcpus_page; + vcpus_phys_addr = page_to_phys(vcpus_page); + + rc = cove_convert_pages(vcpus_phys_addr, tvcpuc->vcpu_state.npages, true); + if (rc) + goto convert_failed; + + rc = sbi_covh_create_tvm_vcpu(tvmc->tvm_guest_id, vcpu->vcpu_idx, vcpus_phys_addr); + if (rc) + goto vcpu_create_failed; + + vcpu->arch.tc = tvcpuc; + + return 0; + +vcpu_create_failed: + /* Reclaim all the pages or return to the confidential page pool */ + sbi_covh_tsm_reclaim_pages(vcpus_phys_addr, tvcpuc->vcpu_state.npages); + +convert_failed: + __free_pages(vcpus_page, get_order_num_pages(tinfo.tvcpu_pages_needed)); + +alloc_page_failed: + kfree(tvcpuc); + return rc; +} + +int kvm_riscv_cove_vm_add_memreg(struct kvm *kvm, unsigned long gpa, unsigned long size) +{ + int rc; + struct kvm_cove_tvm_context *tvmc = kvm->arch.tvmc; + + if (!tvmc) + return -EFAULT; + + if (tvmc->finalized_done) { + kvm_err("Memory region can not be added after finalize\n"); + return -EINVAL; + } + + tvmc->confidential_region.gpa = gpa; + tvmc->confidential_region.npages = bytes_to_pages(size); + + rc = sbi_covh_add_memory_region(tvmc->tvm_guest_id, gpa, size); + if (rc) { + kvm_err("Registering confidential memory region failed with rc %d\n", rc); + return rc; + } + + kvm_info("%s: Success with gpa %lx size %lx\n", __func__, gpa, size); + + return 0; +} + +/* + * Destroying A TVM is expensive because we need to reclaim all the pages by iterating over it. + * Few ideas to improve: + * 1. At least do the reclaim part in a worker thread in the background + * 2. Define a page pool which can contain a pre-allocated/converted pages. + * In this step, we just return to the confidential page pool. Thus, some other TVM + * can use it. + */ +void kvm_riscv_cove_vm_destroy(struct kvm *kvm) +{ + int rc; + struct kvm_cove_tvm_context *tvmc = kvm->arch.tvmc; + unsigned long pgd_npages; + + if (!tvmc) + return; + + /* Release all the confidential pages using COVH SBI call */ + rc = sbi_covh_tsm_destroy_tvm(tvmc->tvm_guest_id); + if (rc) { + kvm_err("TVM %ld destruction failed with rc = %d\n", tvmc->tvm_guest_id, rc); + return; + } + + cove_delete_page_list(kvm, &tvmc->reclaim_pending_pages, false); + + /* 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); + if (rc) + goto reclaim_failed; + + __free_pages(tvmc->tvm_state.page, get_order_num_pages(tvmc->tvm_state.npages)); + + /* Reclaim and Free the pages for gstage page table management */ + rc = sbi_covh_tsm_reclaim_pages(page_to_phys(tvmc->pgtable.page), tvmc->pgtable.npages); + if (rc) + goto reclaim_failed; + + __free_pages(tvmc->pgtable.page, get_order_num_pages(tvmc->pgtable.npages)); + + /* Reclaim the confidential page for pgd */ + pgd_npages = kvm_riscv_gstage_pgd_size() >> PAGE_SHIFT; + rc = sbi_covh_tsm_reclaim_pages(kvm->arch.pgd_phys, pgd_npages); + if (rc) + goto reclaim_failed; + + kfree(tvmc); + + return; + +reclaim_failed: + kvm_err("Memory reclaim failed with rc %d\n", rc); +} + +int kvm_riscv_cove_vm_init(struct kvm *kvm) +{ + struct kvm_cove_tvm_context *tvmc; + struct page *tvms_page, *pgt_page; + unsigned long tvm_gid, pgt_phys_addr, tvms_phys_addr; + unsigned long gstage_pgd_size = kvm_riscv_gstage_pgd_size(); + int rc = 0; + + tvmc = kzalloc(sizeof(*tvmc), GFP_KERNEL); + if (!tvmc) + return -ENOMEM; + + /* Allocate the pages required for gstage page table management */ + /* TODO: Just give enough pages for page table pool for now */ + pgt_page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(KVM_COVE_PGTABLE_SIZE_MAX)); + if (!pgt_page) + return -ENOMEM; + + /* pgd is always 16KB aligned */ + rc = cove_convert_pages(kvm->arch.pgd_phys, gstage_pgd_size >> PAGE_SHIFT, false); + if (rc) + goto done; + + /* Convert the gstage page table pages */ + tvmc->pgtable.page = pgt_page; + tvmc->pgtable.npages = KVM_COVE_PGTABLE_SIZE_MAX >> PAGE_SHIFT; + pgt_phys_addr = page_to_phys(pgt_page); + + rc = cove_convert_pages(pgt_phys_addr, tvmc->pgtable.npages, false); + if (rc) { + kvm_err("%s: page table pool conversion failed rc %d\n", __func__, rc); + goto pgt_convert_failed; + } + + /* Allocate and convert the pages required for TVM state management */ + tvms_page = alloc_pages(GFP_KERNEL | __GFP_ZERO, + get_order_num_pages(tinfo.tvm_pages_needed)); + if (!tvms_page) { + rc = -ENOMEM; + goto tvms_alloc_failed; + } + + tvmc->tvm_state.page = tvms_page; + tvmc->tvm_state.npages = tinfo.tvm_pages_needed; + tvms_phys_addr = page_to_phys(tvms_page); + + rc = cove_convert_pages(tvms_phys_addr, tinfo.tvm_pages_needed, false); + if (rc) { + kvm_err("%s: tvm state page conversion failed rc %d\n", __func__, rc); + goto tvms_convert_failed; + } + + rc = kvm_riscv_cove_fence(); + if (rc) + goto tvm_init_failed; + + INIT_LIST_HEAD(&tvmc->measured_pages); + INIT_LIST_HEAD(&tvmc->zero_pages); + INIT_LIST_HEAD(&tvmc->shared_pages); + INIT_LIST_HEAD(&tvmc->reclaim_pending_pages); + + /* The required pages have been converted to confidential memory. Create the TVM now */ + params.tvm_page_directory_addr = kvm->arch.pgd_phys; + params.tvm_state_addr = tvms_phys_addr; + + rc = sbi_covh_tsm_create_tvm(¶ms, &tvm_gid); + if (rc) + goto tvm_init_failed; + + tvmc->tvm_guest_id = tvm_gid; + spin_lock_init(&tvmc->tvm_fence_lock); + kvm->arch.tvmc = tvmc; + + rc = sbi_covh_add_pgt_pages(tvm_gid, pgt_phys_addr, tvmc->pgtable.npages); + if (rc) + goto tvm_init_failed; + + tvmc->kvm = kvm; + kvm_info("Guest VM creation successful with guest id %lx\n", tvm_gid); + + return 0; + +tvm_init_failed: + /* Reclaim tvm state pages */ + sbi_covh_tsm_reclaim_pages(tvms_phys_addr, tvmc->tvm_state.npages); + +tvms_convert_failed: + __free_pages(tvms_page, get_order_num_pages(tinfo.tvm_pages_needed)); + +tvms_alloc_failed: + /* Reclaim pgtable pages */ + sbi_covh_tsm_reclaim_pages(pgt_phys_addr, tvmc->pgtable.npages); + +pgt_convert_failed: + __free_pages(pgt_page, get_order(KVM_COVE_PGTABLE_SIZE_MAX)); + /* Reclaim pgd pages */ + sbi_covh_tsm_reclaim_pages(kvm->arch.pgd_phys, gstage_pgd_size >> PAGE_SHIFT); + +done: + kfree(tvmc); + return rc; +} + +int kvm_riscv_cove_init(void) +{ + int rc; + + /* We currently support host in VS mode. Thus, NACL is mandatory */ + if (sbi_probe_extension(SBI_EXT_COVH) <= 0 || !kvm_riscv_nacl_available()) + return -EOPNOTSUPP; + + rc = sbi_covh_tsm_get_info(&tinfo); + if (rc < 0) + return -EINVAL; + + if (tinfo.tstate != TSM_READY) { + kvm_err("TSM is not ready yet. Can't run TVMs\n"); + return -EAGAIN; + } + + riscv_cove_enabled = true; + kvm_info("The platform has confidential computing feature enabled\n"); + kvm_info("TSM version %d is loaded and ready to run\n", tinfo.version); + + return 0; +} diff --git a/arch/riscv/kvm/cove_sbi.c b/arch/riscv/kvm/cove_sbi.c index c8c63fe..bf037f6 100644 --- a/arch/riscv/kvm/cove_sbi.c +++ b/arch/riscv/kvm/cove_sbi.c @@ -82,9 +82,7 @@ int sbi_covh_tsm_create_tvm(struct sbi_cove_tvm_create_params *tparam, unsigned goto done; } - kvm_info("%s: create_tvm tvmid %lx\n", __func__, ret.value); *tvmid = ret.value; - done: return rc; } diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 8923319..a55a6a5 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -914,6 +914,12 @@ struct kvm_ppc_resize_hpt { #define KVM_VM_TYPE_ARM_IPA_SIZE_MASK 0xffULL #define KVM_VM_TYPE_ARM_IPA_SIZE(x) \ ((x) & KVM_VM_TYPE_ARM_IPA_SIZE_MASK) + +/* + * RISCV-V Confidential VM type. The large bit shift is chosen on purpose + * to allow other architectures to have their specific VM types if required. + */ +#define KVM_VM_TYPE_RISCV_COVE (1UL << 9) /* * ioctls for /dev/kvm fds: */ From patchwork Wed Apr 19 22:16:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217669 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 695EDC77B73 for ; Wed, 19 Apr 2023 23:30:55 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=fGkJAXY+5f7sf3taYd1/tUxQFQTC42a9UWz2d1X3MKY=; b=OEp6Ey4bH0W4ye DIXhZ/sU9D30EkpX0P2JWELKAlMUfz0qbBHhAOFn8Hjstx2iFIX+t2xrXqKkFMKWkGNihPMvx9Fpf 1Zuk6qkaXf/L0ltghcgewY4QdsVxvZRgeAqLhbDGoYfXZxzFbm1Sw9CenWS2l+r2wvrPO0FlbIu3C OuMOg3yffgUXosMIv1Ejrx34lSu6tOFW6nqo+biWnqGv0ny1CrOfn+LWrOu9pgeM4ec/Iw9UJgcMd TRsyiGGuL+2Z7CvG1bOjzsaIJAN6Qp83XuOGxnhMef1eHmm5zJe37xa1Jiz3AB0rzKvjJQ1FQqmQ/ LkEUHlg3bRRVnnQLfwGw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppHG5-006eUW-30; Wed, 19 Apr 2023 23:30:41 +0000 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG7X-006Svq-32 for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:17:49 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1a526aa3dd5so5173815ad.3 for ; Wed, 19 Apr 2023 15:17:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942667; x=1684534667; 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=xru1T6bsi1+3ZKp8XjmLeeEUO6sc9QurHQCwhhM84HA=; b=fB9b4YMtcQOYrcKj9/GWzeONjcxKP2oeywQTvv9jgtHPhKMG6piGSirEiNQPiRUSw/ TNDD98EsCQRHJb8o4KR/40iwkDz2iCL9O1Q5D4OApUqfzyyueF2pWWXu9oUBTr1sBd7r qN8+st+530NNWaQglZ+vx32ZFpdd6jtoI22iSuSF0a+tcpVy60XJq7vb5hms8XM51miU EUekIXnr0Cbflr87WbR+EkVxvu0imyYB9o1bwvrkRw5vQJ9T+Gvh04UNMDx3e/rdtmef Rhm+A3oBDjRklFUpsePBjaVMWeVQWCFlRT5JvPLYOIPW/X1HEUctXf38yhKlTJ/Aw1FH TvXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942667; x=1684534667; 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=xru1T6bsi1+3ZKp8XjmLeeEUO6sc9QurHQCwhhM84HA=; b=T/h0Wl4ULgTrEQkMp2i0eTQk3uk1NR0G658JHbf8Lt746cwPxytu3u55nZoyTGPMKB U4OpVZtJVsPpDYXsWm3RZ2Vr0VCM3ztvn7fbtqte4eudb6cnQY9IuNfzTIsYgE7dy4uB UTdytJ9xcDeLJMUWB7Y8wieSK6nv6CKqWQNosCHg4n78deixi5OqJ+/avhw6hC+lm88t l9oZS7Rede13JtNNDRKJJC4BSDIiFtc/fmocN7jklX8oB0Gw1tGfZN0ycEzvuNVuoLMs CocDuEjc829mK+EIZdJR8Wo8H3bu0t8pYuk6FgHDn9g3PePCJvpGMoPwBbratDQsrJX5 hiIw== X-Gm-Message-State: AAQBX9coBgH9sIwdQna3POhSB9R7At4vXh9VhxirEXwG8CRQg3oLF11u gMzk1b0rMsp93JPYIeb2IBc+Ww== X-Google-Smtp-Source: AKy350Ze6w+VITNJKc37wiOWND61z06Oiijk/jlDr4gywczSxyZvsezIoz1E2Y/uHqG7LrTSWYdlBg== X-Received: by 2002:a17:902:f682:b0:1a1:f5dd:2dce with SMTP id l2-20020a170902f68200b001a1f5dd2dcemr8068605plg.6.1681942667389; Wed, 19 Apr 2023 15:17:47 -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.17.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:17:47 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 08/48] RISC-V: KVM: Add UABI to support static memory region attestation Date: Wed, 19 Apr 2023 15:16:36 -0700 Message-Id: <20230419221716.3603068-9-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151747_979928_9242FD7B X-CRM114-Status: GOOD ( 15.48 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org To initialize a TVM, a TSM must ensure that all the static memory regions that contain the device tree, the kernel image or initrd for the TVM attested. Some of these information is not usually present with the host and only VMM is aware of these. Introduce an new ioctl which is part of the uABI to support this. Signed-off-by: Atish Patra --- arch/riscv/include/uapi/asm/kvm.h | 12 ++++++++++++ include/uapi/linux/kvm.h | 2 ++ 2 files changed, 14 insertions(+) diff --git a/arch/riscv/include/uapi/asm/kvm.h b/arch/riscv/include/uapi/asm/kvm.h index 11440df..ac3def0 100644 --- a/arch/riscv/include/uapi/asm/kvm.h +++ b/arch/riscv/include/uapi/asm/kvm.h @@ -98,6 +98,18 @@ struct kvm_riscv_timer { __u64 state; }; +/* Memory region details of a CoVE guest that is measured at boot time */ +struct kvm_riscv_cove_measure_region { + /* Address of the user space where the VM code/data resides */ + unsigned long userspace_addr; + + /* The guest physical address where VM code/data should be mapped */ + unsigned long gpa; + + /* Size of the region */ + unsigned long size; +}; + /* * ISA extension IDs specific to KVM. This is not the same as the host ISA * extension IDs as that is internal to the host and should not be exposed diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index a55a6a5..84a73b5 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1552,6 +1552,8 @@ struct kvm_s390_ucas_mapping { #define KVM_PPC_SVM_OFF _IO(KVMIO, 0xb3) #define KVM_ARM_MTE_COPY_TAGS _IOR(KVMIO, 0xb4, struct kvm_arm_copy_mte_tags) +#define KVM_RISCV_COVE_MEASURE_REGION _IOR(KVMIO, 0xb5, struct kvm_riscv_cove_measure_region) + /* ioctl for vm fd */ #define KVM_CREATE_DEVICE _IOWR(KVMIO, 0xe0, struct kvm_create_device) From patchwork Wed Apr 19 22:16:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217608 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 BCB2CC77B75 for ; Wed, 19 Apr 2023 22:37:59 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=84OZJ5WvzBxwqS7xDP9hYy+NkBhtoW7QHUAdc3Kv80k=; b=v7phJJxK6pP7qR PpCblpfg/7EERt9pGAF1d8ACbxCPLsAyp/TfBxVYLQyTMDoKMaMoamjQL1MKB9foa/5/HPdrqvOMP ZP5xSPgFb8mtjwqyxCFW0HLA7SlPjf/S7sg1tajFbVWrBe/X2BLobNwBRVzNXDkxgfA0o6+mAX3gi g1oiKQPp/QQa2vTiYZsEwnt6fCYVePJzz8C7Fk/8T5UtRlt6g/w3fiiWQoFfr5MFy7iB2Bg47un74 F+fjm52cxzWoutpOZXQGvAiNz4yTSDUSLQ1kVAnNSGragOEfs6YzoYqQhM8xGmKwVCtBCUOSPVdDL v2ZPtI7xCh5whF0DNmBg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQz-006X3Z-1F; Wed, 19 Apr 2023 22:37:53 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQr-006WXV-2d for linux-riscv@bombadil.infradead.org; Wed, 19 Apr 2023 22:37:45 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=T59m5JAZKpRDJgvlPNnQtLbeFsduDf6TBzeyZHAxXV0=; b=p7/SroNSPZUHlUNu7L2CpNV4+y WjMIsoeUtnkb1bIPcTplVMTg/s45jVUP1j/9ATfSD5sVjI36/XoJ/1XgI6X0jLA1CKtYh2pySXflJ wpkuH08892lqhPQTyOVvqOJ4/L3ENcPgvVhzWDr33Fx81OP+6X4aLOIpexRr66QOvccvd78VLNi40 wNTK/O33iWukly7Xyb14bXITi3ntATUJ+nDLXESxO3o6130hPTe90Cx454sA0JShLpOyI2UpNxGCm NDOTnCMTYjpK70fw1GYeqrCWwX8wo6HGxpKP5PzZiY68PvqWXn2pDs9KJF2mUhEwZcB6PqfIxp/Jc oeSThqXw==; Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by casper.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ppG7c-00De6w-Me for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:17:56 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1a6762fd23cso4482175ad.3 for ; Wed, 19 Apr 2023 15:17:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942669; x=1684534669; 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=T59m5JAZKpRDJgvlPNnQtLbeFsduDf6TBzeyZHAxXV0=; b=qjxNMIrl8HnhfIp/ecw72oYKFII9UTAwql/uMAHKk/FXlEBEiMQ66OH7eiwjlGKtCq fOjlVW3CuH2HqfOiNP9XRTFCzDI4+lJmQdsac4GSSp3r+OkcygCfPaztbk/heRGDIhKM pnYeif4EQ7Prrd5kCwoWu5m8+1XOIOkSJ78OzpuHzmCbajgIzTGS3QZn2FEzSLBwhiSb 6w7a2QMSx0oX83P6+RuntF3wYcXCE4rbF3duzd0QwdejxuKey4lt6WjYMDej+sLNVh9T 6z0R8DizcoQpeT/MVd47bfnzAc938aOKta+516VE8cUvwIwwgAey2BJajvvIriSfLlPU 8bjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942669; x=1684534669; 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=T59m5JAZKpRDJgvlPNnQtLbeFsduDf6TBzeyZHAxXV0=; b=C6Zu2EWgRC5wN/3UkICGWE8V9TWCpmYdknkM5pAaZFwsXcN/wVfAe8aX3TRjWfslpS 06reEdV+Yo6mvr/3NSKFiW0mVoCBLDJgJowDXtJP4iLImQtZ1NAw7qlnPzlczutX8j70 SPWi5E8AlDexKu+KTpvLNd7J+Xb8REAMjOydZPx2Dw6KWU9B8RAkACpPN8ZD30a/bMy3 N3OZnkziV0w21BNK1k12SKLMvG1DrReTwZZD1mJFgK/Vkx5ToZZrqaAS27tvHKcbzt3k 9P/AnNBTqx8nchVzKbjEJ6dWlDbPe41WeJpHSGMmatIelvrmUKcnSU0SC364g13QHXw2 AYhQ== X-Gm-Message-State: AAQBX9cQ2KZcU6IYLj8zD8CzkrxN/A92ZO50l3NBMSjcR5dzwaNTQjyC hW6/7L+OSLbgTbZQ8QkIM4nRlw== X-Google-Smtp-Source: AKy350a2O0VHJD6AXYzabb4g8786HYDihr3e7/POpprE3Ii6IVd/H8nE9VOR2CTMJgAkkR7C+O0Rjg== X-Received: by 2002:a17:902:aa4a:b0:1a1:f0cb:1055 with SMTP id c10-20020a170902aa4a00b001a1f0cb1055mr6242462plr.28.1681942669632; Wed, 19 Apr 2023 15:17:49 -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.17.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:17:49 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 09/48] RISC-V: KVM: Add CoVE related nacl helpers Date: Wed, 19 Apr 2023 15:16:37 -0700 Message-Id: <20230419221716.3603068-10-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_231752_749930_64E4AED7 X-CRM114-Status: UNSURE ( 9.82 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org The NACL SBI extension allows the scratch area to be customizable per SBI extension. As per the COVH SBI extension, the scratch area stores the guest gpr state. Add some helpers to read/write gprs easily. Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_cove_sbi.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/arch/riscv/include/asm/kvm_cove_sbi.h b/arch/riscv/include/asm/kvm_cove_sbi.h index 24562df..df7d88c 100644 --- a/arch/riscv/include/asm/kvm_cove_sbi.h +++ b/arch/riscv/include/asm/kvm_cove_sbi.h @@ -17,6 +17,21 @@ #include #include +#include + +/** + * CoVE SBI extensions defines the NACL scratch memory. + * uint64_t gprs[32] + * uint64_t reserved[224] + */ +#define get_scratch_gpr_offset(goffset) (goffset - KVM_ARCH_GUEST_ZERO) + +#define nacl_shmem_gpr_write_cove(__s, __g, __o) \ + nacl_shmem_scratch_write_long(__s, get_scratch_gpr_offset(__g), __o) + +#define nacl_shmem_gpr_read_cove(__s, __g) \ + nacl_shmem_scratch_read_long(__s, get_scratch_gpr_offset(__g)) + int sbi_covh_tsm_get_info(struct sbi_cove_tsm_info *tinfo_addr); int sbi_covh_tvm_initiate_fence(unsigned long tvmid); int sbi_covh_tsm_initiate_fence(void); From patchwork Wed Apr 19 22:16:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217605 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 66814C6FD18 for ; Wed, 19 Apr 2023 22:37:56 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=6F7J2jQuklGt6pf5S8H/BCFvjGpGiv0A+Z3RVTIxnP0=; b=07XboFwxTVA22R lFpyHbTnErOljx/Z04Q7UEEg3g02Fn0WoLbZPc0qbgDtlavRAgx+NHtuSCrz/OvuUSZwLJFd8j8SY xuXEIRi4Yr+wmPmVKqzgD6WGScM2V+I0z5FOgs6ajmExOAKzCwGl2rvWPVnOp2rHYmuBzjlDOLsaG ubCgpeYOK/uQqP0R9EfmCAuqx7H1X7dogF9RATRjvSjgRywU+qcc86wYD0T5Or0jLL2c00bqlqiMU MA+bo1qEkbI7EZN4bsY8XunY85VvzHCN2gPxEuHUi8Eult+o5szDBBM7ymcBFxpAKvJgEzQ8xAVr1 ZWUvDiyl5bBHe5bdE3og==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQw-006Wzh-00; Wed, 19 Apr 2023 22:37:50 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQo-006WXV-0W for linux-riscv@bombadil.infradead.org; Wed, 19 Apr 2023 22:37:42 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=qOzd/WhvlpjJ956p72hZNImrff40bav5pGpgVf1jDCE=; b=QeyTykNf4sc+5fpBcoYaTa6P+R y6MlgO3KH/Qhb5EXhYArTT6lOV2f3e8VrIU1u81L4XTDygBMaZwOgDZeQC53GpxfUqGMZVyEkpOKX OEOsyD1Wq8JReNbr3HpM628s1M3GhuJhPm1qD5LxBQRgtbcZtDY/PJIZ0xdaR/ns99r78QPcN5uQQ 3FMqpAnBXiMHo/MXuvTKFij7jDkDu+VTThHfKfUaXigGHQvJKshxjn2t9jcNoPnHwKRY1e/yur18H GLWLcktfmLz2gPUwl2cKCb6/nI2N6RZIGhkflXQFFy3i15UWg0cJMo/j/rpMdR/wRGLA+gDTGno49 +gjTxAbQ==; Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by casper.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ppG7e-00De7n-Mv for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:05 +0000 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-1a682eee3baso4401165ad.0 for ; Wed, 19 Apr 2023 15:17:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942672; x=1684534672; 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=qOzd/WhvlpjJ956p72hZNImrff40bav5pGpgVf1jDCE=; b=nK5/1WGSlykOrMrz6zLE7ByOwHsNX7ku+LU2VBvTADuPSy3LTEvEYN/5idyeMAH9v6 lDrCh9vx2uQPGrR2xDDmRNujTPNDyyzCxzk4mY1KxzUuZQGhhSyvWuaw1bMwsQMloPyl 7AP1+xv6gUZEztUQTM1GZJONZkurrJQmSw8oo0Vlacs0NCnY8W0HIEeaXUNTT9FXPkDs BnQUvSJIxGIi32I9gl3+IkxfURT26aMcR2YDE4SZvKgasSWBzWeQlJS8HIQa8AHGBoBy oAuh9qhu2Ya7h80HYOyhHTVyAo19NM+J4mD7HNvy+vl3b7n5pBV/07agfGWnPCRxQI9a znow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942672; x=1684534672; 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=qOzd/WhvlpjJ956p72hZNImrff40bav5pGpgVf1jDCE=; b=hktU/E1u9Gki/thp+uyp+s7z6ZwnHfL3TxzEOX2kZhLjqflq5KGda+Afo34NuWZsdm e09A6/uDFf08Dgy6UATSKsz886o/UTQnnFiVsAkVXmA4sK0MkirtQNy6VYs5sIDJUrD1 ozkEYpZDY2BqNsoVSk5Zw+hDwPGE4EPMTChFUQJ/UOtrFPRXjFjCN9vr69AYCvG9LhSY mlta2bzIk2xT0LqgqGV/jnt86vRNANsBFq2A+KHqjgwNGNBdhZPqahtmYa+Vpj9TRXOO Bgg9yTFUdazhS4eukiywsC/FtcIpISGATq1v10/Z0Z/ozsDqYzUInjma05ufU/kkDf6v T/5A== X-Gm-Message-State: AAQBX9ddJkgI3kYFP4pJH2BieKVRe6rf1/zxDez1/bFp28w3tacb7Zh0 2ZT6AUhv/h+4Lh4bvRkSty5Bew== X-Google-Smtp-Source: AKy350YH91pAUPM41j7LqluKiXzQ7Ynx9OMjbtDndSOQdeDLatwEyjhoPFWqpUQdaJ4OsOf4sEQSuA== X-Received: by 2002:a17:902:9004:b0:1a5:2621:34cd with SMTP id a4-20020a170902900400b001a5262134cdmr5755075plp.39.1681942671881; Wed, 19 Apr 2023 15:17:51 -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.17.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:17:51 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 10/48] RISC-V: KVM: Implement static memory region measurement Date: Wed, 19 Apr 2023 15:16:38 -0700 Message-Id: <20230419221716.3603068-11-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_231754_899004_F1B8F4B6 X-CRM114-Status: GOOD ( 18.85 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org To support attestation of any images loaded by the VMM, the COVH allows measuring these memory regions. Currently, it will be used for the kernel image, device tree and initrd images. Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_cove.h | 6 ++ arch/riscv/kvm/cove.c | 110 ++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/arch/riscv/include/asm/kvm_cove.h b/arch/riscv/include/asm/kvm_cove.h index 3bf1bcd..4ea1df1 100644 --- a/arch/riscv/include/asm/kvm_cove.h +++ b/arch/riscv/include/asm/kvm_cove.h @@ -127,6 +127,7 @@ void kvm_riscv_cove_vcpu_load(struct kvm_vcpu *vcpu); void kvm_riscv_cove_vcpu_put(struct kvm_vcpu *vcpu); void kvm_riscv_cove_vcpu_switchto(struct kvm_vcpu *vcpu, struct kvm_cpu_trap *trap); +int kvm_riscv_cove_vm_measure_pages(struct kvm *kvm, struct kvm_riscv_cove_measure_region *mr); int kvm_riscv_cove_vm_add_memreg(struct kvm *kvm, unsigned long gpa, unsigned long size); int kvm_riscv_cove_gstage_map(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned long hva); #else @@ -147,6 +148,11 @@ static inline void kvm_riscv_cove_vcpu_put(struct kvm_vcpu *vcpu) {} static inline void kvm_riscv_cove_vcpu_switchto(struct kvm_vcpu *vcpu, struct kvm_cpu_trap *trap) {} static inline int kvm_riscv_cove_vm_add_memreg(struct kvm *kvm, unsigned long gpa, unsigned long size) {return -1; } +static inline int kvm_riscv_cove_vm_measure_pages(struct kvm *kvm, + struct kvm_riscv_cove_measure_region *mr) +{ + return -1; +} static inline int kvm_riscv_cove_gstage_map(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned long hva) {return -1; } #endif /* CONFIG_RISCV_COVE_HOST */ diff --git a/arch/riscv/kvm/cove.c b/arch/riscv/kvm/cove.c index d001e36..5b4d9ba 100644 --- a/arch/riscv/kvm/cove.c +++ b/arch/riscv/kvm/cove.c @@ -27,6 +27,12 @@ static DEFINE_SPINLOCK(cove_fence_lock); static bool riscv_cove_enabled; +static inline bool cove_is_within_region(unsigned long addr1, unsigned long size1, + unsigned long addr2, unsigned long size2) +{ + return ((addr1 <= addr2) && ((addr1 + size1) >= (addr2 + size2))); +} + static void kvm_cove_local_fence(void *info) { int rc; @@ -192,6 +198,109 @@ int kvm_riscv_cove_vcpu_init(struct kvm_vcpu *vcpu) return rc; } +int kvm_riscv_cove_vm_measure_pages(struct kvm *kvm, struct kvm_riscv_cove_measure_region *mr) +{ + struct kvm_cove_tvm_context *tvmc = kvm->arch.tvmc; + int rc = 0, idx, num_pages; + struct kvm_riscv_cove_mem_region *conf; + struct page *pinned_page, *conf_page; + struct kvm_riscv_cove_page *cpage; + + if (!tvmc) + return -EFAULT; + + if (tvmc->finalized_done) { + kvm_err("measured_mr pages can not be added after finalize\n"); + return -EINVAL; + } + + num_pages = bytes_to_pages(mr->size); + conf = &tvmc->confidential_region; + + if (!IS_ALIGNED(mr->userspace_addr, PAGE_SIZE) || + !IS_ALIGNED(mr->gpa, PAGE_SIZE) || !mr->size || + !cove_is_within_region(conf->gpa, conf->npages << PAGE_SHIFT, mr->gpa, mr->size)) + return -EINVAL; + + idx = srcu_read_lock(&kvm->srcu); + + /*TODO: Iterate one page at a time as pinning multiple pages fail with unmapped panic + * with a virtual address range belonging to vmalloc region for some reason. + */ + while (num_pages) { + if (signal_pending(current)) { + rc = -ERESTARTSYS; + break; + } + + if (need_resched()) + cond_resched(); + + rc = get_user_pages_fast(mr->userspace_addr, 1, 0, &pinned_page); + if (rc < 0) { + kvm_err("Pinning the userpsace addr %lx failed\n", mr->userspace_addr); + break; + } + + /* Enough pages are not available to be pinned */ + if (rc != 1) { + rc = -ENOMEM; + break; + } + conf_page = alloc_page(GFP_KERNEL | __GFP_ZERO); + if (!conf_page) { + rc = -ENOMEM; + break; + } + + rc = cove_convert_pages(page_to_phys(conf_page), 1, true); + if (rc) + break; + + /*TODO: Support other pages sizes */ + rc = sbi_covh_add_measured_pages(tvmc->tvm_guest_id, page_to_phys(pinned_page), + page_to_phys(conf_page), SBI_COVE_PAGE_4K, + 1, mr->gpa); + if (rc) + break; + + /* Unpin the page now */ + put_page(pinned_page); + + cpage = kmalloc(sizeof(*cpage), GFP_KERNEL_ACCOUNT); + if (!cpage) { + rc = -ENOMEM; + break; + } + + cpage->page = conf_page; + cpage->npages = 1; + cpage->gpa = mr->gpa; + cpage->hva = mr->userspace_addr; + cpage->is_mapped = true; + INIT_LIST_HEAD(&cpage->link); + list_add(&cpage->link, &tvmc->measured_pages); + + mr->userspace_addr += PAGE_SIZE; + mr->gpa += PAGE_SIZE; + num_pages--; + conf_page = NULL; + + continue; + } + srcu_read_unlock(&kvm->srcu, idx); + + if (rc < 0) { + /* We don't to need unpin pages as it is allocated by the hypervisor itself */ + cove_delete_page_list(kvm, &tvmc->measured_pages, false); + /* Free the last allocated page for which conversion/measurement failed */ + kfree(conf_page); + kvm_err("Adding/Converting measured pages failed %d\n", num_pages); + } + + return rc; +} + int kvm_riscv_cove_vm_add_memreg(struct kvm *kvm, unsigned long gpa, unsigned long size) { int rc; @@ -244,6 +353,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); /* 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); From patchwork Wed Apr 19 22:16:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217668 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 3ED6AC6FD18 for ; Wed, 19 Apr 2023 23:30:55 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=8vYmpoEzObstFNZBx4vWKtWimNtrgSYq9CjVQiUW7PA=; b=nkSowq2TI8YqhX RHiQ1qkQ++OldrQElMVtzS4+oLrhlXTr/6vGvIelKVVZKJ+nBQvvdPRsIz9NpSMgf2zuKLpspS609 9ryUWAwlzwC8n7vU/cG0lqejP8JWygj4/9xCtuy19z3deoB0Msv6zBhxWEM6tvcW0dLJuCuPAG+1N gNdiSzYXY0TLbLlsIqjsurMhpR/2P3/feOnO3uAMDVnz9dEgwUhf8wDSnFRGfSWCmO3XNew/VeAB+ 1pEWvlvGWhIBbvDF3uNK0/8F4Hd6kz4hZQ9rFAlF3j+pHC41/uXcEo4EyeTs7ThYkBhJLBYmxyTwx mtSdYmiVcmuMahVoW/lw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppHG7-006eVx-0p; Wed, 19 Apr 2023 23:30:43 +0000 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG7e-006SnP-12 for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:17:55 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1a69f686345so4525435ad.2 for ; Wed, 19 Apr 2023 15:17:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942674; x=1684534674; 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=uwIdkHCarryXV50I6knz0ZklcYwS0nTZYHahvAAioGA=; b=JAfyJwlLZTLpeO+0a9mP+Qz17gHyrei/u3hvU+Y1oEWg9aDIafnfffPw2WLQocmCE5 thEwftksAEQCxymE79XbmXlY3CV86W6kKDPWDlsoieUM1U98liDXsdwKh0TtzMRMco/6 2qgFOkWb8NmcTouc/FLGToAZLWlsyjEWM3W1S9eitYQ6PZtGjRV1E0Bh4dYyak7MqlLF NqImRVNGkomVidir55QeblFMKk81mgA2duY/+3llfMenErvbAqjZMXeQVvmExrFa536W fZATF/TIP+lPFweeamhhj205CiSSkghmiFgf4j0xA47+WlMqYFC57GJCbBbKkv5f7DR4 f0PA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942674; x=1684534674; 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=uwIdkHCarryXV50I6knz0ZklcYwS0nTZYHahvAAioGA=; b=fGzpNa0MfLe62LY2L3Gn3cSi8KU8UTZE3KGWfOF/sqLnmj9P6r4TGpdP3cri8lXGaL X9IOGMrJlB3SFVl4juNXM+5STOTots/Pmb6TFTJVvHcA0GjurUhZOZEygYhgqgvvBWxI 40WkImZC7R55uk3iEO/hpiNQOq5s/GdOM6XBuJ190wCBeMJI/+7+JPAbj/857mxeg3d/ RuncZQNlKu6qCnTWZmouD6JtsJvpkJ23HL4TEyEhBCnds5GjlLXV6m7FUMEOAC0hj5fQ haS7pRHjOs/VGzISsnydihI0DhI4bmfCuemZ3SCBVvTIeqSyZ8fQe/cV/e4Xtpgm+Chm FGEQ== X-Gm-Message-State: AAQBX9ecS0Sfx60e5zfBEqwZiI06QTKD6OesQZNA/rSmapC5e5XhLNAb TN1kWp7Oh5CyzAkgdLoMlVU3cw== X-Google-Smtp-Source: AKy350aOeQnqsiOOnUYsijZU0I/0P7RvEF6TJy/X0/H7ROAFo8Dof2s5JoE26B/8+FVZEZPKo3gc/w== X-Received: by 2002:a17:902:ce88:b0:1a1:f5dd:2dd5 with SMTP id f8-20020a170902ce8800b001a1f5dd2dd5mr8081414plg.13.1681942674035; Wed, 19 Apr 2023 15:17:54 -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.17.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:17:53 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 11/48] RISC-V: KVM: Use the new VM IOCTL for measuring pages Date: Wed, 19 Apr 2023 15:16:39 -0700 Message-Id: <20230419221716.3603068-12-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151754_358798_8AF52F4E X-CRM114-Status: GOOD ( 13.90 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org The newly introduced VM IOCTL allow the VMM to measure the pages used to load the blobs. Hookup the VM ioctl. Signed-off-by: Atish Patra --- arch/riscv/kvm/vm.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/arch/riscv/kvm/vm.c b/arch/riscv/kvm/vm.c index 29d3221..1b59a8f 100644 --- a/arch/riscv/kvm/vm.c +++ b/arch/riscv/kvm/vm.c @@ -11,6 +11,7 @@ #include #include #include +#include const struct _kvm_stats_desc kvm_vm_stats_desc[] = { KVM_GENERIC_VM_STATS() @@ -209,5 +210,20 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) { - return -EINVAL; + struct kvm *kvm = filp->private_data; + void __user *argp = (void __user *)arg; + struct kvm_riscv_cove_measure_region mr; + + switch (ioctl) { + case KVM_RISCV_COVE_MEASURE_REGION: + if (!is_cove_vm(kvm)) + return -EINVAL; + if (copy_from_user(&mr, argp, sizeof(mr))) + return -EFAULT; + + return kvm_riscv_cove_vm_measure_pages(kvm, &mr); + default: + return -EINVAL; + } + } From patchwork Wed Apr 19 22:16:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217607 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 EF621C77B73 for ; Wed, 19 Apr 2023 22:37:57 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=P4vkuhqVHZTzAFo2ZCaO7oNFf19H5RHqwCXpvzAz9FU=; b=ghqaCSeMLf6g+p ADH3iJ79TyN76hb6vMZoUDpmf8w/dNPKgmc3+GNTvURmsSj2xnQg3qcM99Hh90cyU2lrJ1BkXbZsj dWwGsqtF3Q1fVrP/RxekJdEZlhsdeOfu/PRcO07qSHpNqJ6IUe+qoVANKuBaGDxCSUa3CshUb0sQe sjFBtFMIwMAKJqjSlP9iVd/hJ93aysXMAOwblVlOTvz0Kt3sAy4pmTCSM97ogq83s2pfSli3QN21L T2K5ZdLvykW0FTGZ8twvneaR9GQfRSOjvtrY2tq4ip/uydMBQadWbY9ZQXTeDyU+o0sITCR413Ku4 okqnxbw4UvlY1mc6waWg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQy-006X2C-0h; Wed, 19 Apr 2023 22:37:52 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQp-006WXV-14 for linux-riscv@bombadil.infradead.org; Wed, 19 Apr 2023 22:37:43 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=aZA9SkX62fIMzNEcuGtwzVe8Up8R4eolVQO1cYHyNXI=; b=JXF5pukAj9BQ7nR+nmNYFjWrxk 1fnHPDpvrgI7pFR8zfliqsZxD9a7aB0PcS0PEOVv+dzpf9M9v6HP6x8q71bFkd32IGjMJ6unYdVU2 JtLWVk3o8QINUU8SSV6xwDCoCpP5a49cOH0GBa8qfSCGdJdZjufuj3r3ZcQ9VT5LI78zp/Mz6yRd9 gMNcyIVegKUOiLAMmxT26AJgFAOw1XIT7ON6kmXNsqiZOjzA8VMMnAHwLMbE5QjKy9lWR4f9ot59K 7Wg3ZIQsgrh0DM2Z6I5nCdBc1IbCuUuGmat/tzJkxGpzen7/UvA9vbOsrGTLQR4TdnWcHroieW/pJ rX41lr8w==; Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by casper.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ppG7k-00De9N-J7 for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:03 +0000 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1a80d827179so4502365ad.3 for ; Wed, 19 Apr 2023 15:17:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942676; x=1684534676; 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=aZA9SkX62fIMzNEcuGtwzVe8Up8R4eolVQO1cYHyNXI=; b=pr/kZUHrkhLnktVNRdfz4ij02MTZd+B9h2TaIJSMbt3NVujPvaiFp7Ik4rbkspQ4Mw yuGTwtHLYNepi572aUMJZXGKBGxqqLOoQQAVLkJQE8EutQeaNnEXjp7ozMrwLpK3F/bd Ea/tWnKPdLgS76oaBzGp+E0BB9Obb3YUpFKxXwNOfadBL+8Y1NryGBq3tL1grWdeANk4 WZM2Z6J10fv3mT8jXclG/Jc0tJeziAmqwuD529CbKYuuh+zFcMDe14NqalRMWLaTiCrI zVE7Fohp3aDpbBaOaQnfhoMTTf4olrAvxRsbHG36Vnwm4rXJhTcr8zeKFydq/GZ4e/40 +9lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942676; x=1684534676; 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=aZA9SkX62fIMzNEcuGtwzVe8Up8R4eolVQO1cYHyNXI=; b=lW998tAkTDPMSQtKYPuqN3XLzcHQnf2W0E2FtATzYFFtrX+z62fr/wojcnNCxC5Yw4 QvisULnO8MTG4Ma98pu4cCCBoqzWcbfo8rHEAVNMkLiv1N3Ir+Wb3W+8yKAS1CGWMSAt 4cBZJAbR3ZMrES2ifW7QGcnxaGBxtzcpWp0LcdGvwXgYft38qOI2WWIXnPqd//dyN0PR KEDhzsjJ25qD47N4gVVCxIdTB710y/zn7es6o49BnH6KyqM+W40mXuAiQdzwb6gaZB4K aBQG74Pn65zYurSluXD/oef6pDlRU3jnSOh8rd2drGPuoA5OFkvHgvFtql6yrKzYRfuq H+1g== X-Gm-Message-State: AAQBX9d0mlV+pJt7ozTcIhUypQlGRhO02f9vbVpf9fKbZAwKRtldqQ0/ 8wC98580uS6vSyH/LOtmf4XCxg== X-Google-Smtp-Source: AKy350aluUR1rPQXEDjya7qc0cousjv7HzRiR+wEHDbTSJe3tj7Btum5nXicmgptUoZ7hSCP4qqeQg== X-Received: by 2002:a17:902:c40d:b0:1a8:1cbb:4f76 with SMTP id k13-20020a170902c40d00b001a81cbb4f76mr4908149plk.28.1681942676188; Wed, 19 Apr 2023 15:17:56 -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.17.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:17:55 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 12/48] RISC-V: KVM: Exit to the user space for trap redirection Date: Wed, 19 Apr 2023 15:16:40 -0700 Message-Id: <20230419221716.3603068-13-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_231800_732043_A8F1B741 X-CRM114-Status: GOOD ( 17.36 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Currently, the trap redirection to the guest happens in the following cases. 1. Illegal instruction trap 2. Virtual instruction trap 3. Unsuccesfull unpriv read Allowing host to cause traps in the TVM directly is problematic. TSM doesn't support trap redirection yet. Ideally, the host should not end up in one of these situations where it has to redirect the trap. If it happens, exit to the userspace with error as it can't forward the trap to the TVM. If there is any usecasse arises in the future, it has to be co-ordinated through TSM. Signed-off-by: Atish Patra --- arch/riscv/kvm/vcpu_exit.c | 9 ++++++++- arch/riscv/kvm/vcpu_insn.c | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/arch/riscv/kvm/vcpu_exit.c b/arch/riscv/kvm/vcpu_exit.c index 4ea101a..0d0c895 100644 --- a/arch/riscv/kvm/vcpu_exit.c +++ b/arch/riscv/kvm/vcpu_exit.c @@ -9,6 +9,7 @@ #include #include #include +#include static int gstage_page_fault(struct kvm_vcpu *vcpu, struct kvm_run *run, struct kvm_cpu_trap *trap) @@ -135,8 +136,14 @@ unsigned long kvm_riscv_vcpu_unpriv_read(struct kvm_vcpu *vcpu, void kvm_riscv_vcpu_trap_redirect(struct kvm_vcpu *vcpu, struct kvm_cpu_trap *trap) { - unsigned long vsstatus = csr_read(CSR_VSSTATUS); + unsigned long vsstatus; + if (is_cove_vcpu(vcpu)) { + kvm_err("RISC-V KVM do not support redirect to CoVE guest yet\n"); + return; + } + + vsstatus = csr_read(CSR_VSSTATUS); /* Change Guest SSTATUS.SPP bit */ vsstatus &= ~SR_SPP; if (vcpu->arch.guest_context.sstatus & SR_SPP) diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c index 7a6abed..331489f 100644 --- a/arch/riscv/kvm/vcpu_insn.c +++ b/arch/riscv/kvm/vcpu_insn.c @@ -6,6 +6,7 @@ #include #include +#include #define INSN_OPCODE_MASK 0x007c #define INSN_OPCODE_SHIFT 2 @@ -153,6 +154,10 @@ static int truly_illegal_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, { struct kvm_cpu_trap utrap = { 0 }; + /* The host can not redirect any illegal instruction trap to TVM */ + if (unlikely(is_cove_vcpu(vcpu))) + return -EPERM; + /* Redirect trap to Guest VCPU */ utrap.sepc = vcpu->arch.guest_context.sepc; utrap.scause = EXC_INST_ILLEGAL; @@ -169,6 +174,10 @@ static int truly_virtual_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, { struct kvm_cpu_trap utrap = { 0 }; + /* The host can not redirect any virtual instruction trap to TVM */ + if (unlikely(is_cove_vcpu(vcpu))) + return -EPERM; + /* Redirect trap to Guest VCPU */ utrap.sepc = vcpu->arch.guest_context.sepc; utrap.scause = EXC_VIRTUAL_INST_FAULT; @@ -417,6 +426,10 @@ int kvm_riscv_vcpu_virtual_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, if (unlikely(INSN_IS_16BIT(insn))) { if (insn == 0) { ct = &vcpu->arch.guest_context; + + if (unlikely(is_cove_vcpu(vcpu))) + return -EPERM; + insn = kvm_riscv_vcpu_unpriv_read(vcpu, true, ct->sepc, &utrap); @@ -469,6 +482,8 @@ int kvm_riscv_vcpu_mmio_load(struct kvm_vcpu *vcpu, struct kvm_run *run, insn = htinst | INSN_16BIT_MASK; insn_len = (htinst & BIT(1)) ? INSN_LEN(insn) : 2; } else { + if (unlikely(is_cove_vcpu(vcpu))) + return -EFAULT; /* * Bit[0] == 0 implies trapped instruction value is * zero or special value. @@ -595,6 +610,8 @@ int kvm_riscv_vcpu_mmio_store(struct kvm_vcpu *vcpu, struct kvm_run *run, insn = htinst | INSN_16BIT_MASK; insn_len = (htinst & BIT(1)) ? INSN_LEN(insn) : 2; } else { + if (unlikely(is_cove_vcpu(vcpu))) + return -EFAULT; /* * Bit[0] == 0 implies trapped instruction value is * zero or special value. From patchwork Wed Apr 19 22:16:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217606 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 CBD73C77B75 for ; Wed, 19 Apr 2023 22:37:56 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Ug8mUdNFHlvl32MBqviGEAxIIcxYqG/mNFLsAR4uTZY=; b=Lt9xe3/annbOuw oGjMv+SaPaK/1nBcz6FlZXggBLDocDIB8fFAlaLS8U8KO3rBNIUlNtcdlLMJtzGKZDhSDGJJCs4r8 rp+rCCU60vPQ6K+2Baw55vIgspG+SVf/3X05H3IcRXFyLHqqJNPa+XY3FQ6m5YsUelB1muqh9UcJW 5zyl0FRshulseltyL+9h1ryCkUIR8AeCEB5nbiBaPDEr84XwcrwvHpJV5Hb1jVxsVQg0dUtXUVnTC 7ySrGtD6JLHFQzZJc65ROfxHili88MEAQW4G+2tz5FvivV+r5sZGCEeZOUKoe6nZynTBx+loWCjvS gJn4DU+zvFGMMOBIUG0g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQw-006X0Q-29; Wed, 19 Apr 2023 22:37:50 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQn-006WXV-0B for linux-riscv@bombadil.infradead.org; Wed, 19 Apr 2023 22:37:41 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=LUld/G/1WeH25lUdXRYxnjPYZXeYYgEcmI7A3/bRIcw=; b=eJXo7HjH5vYPqWNF4d729XqpRB UCZbYawxPRX+ceQ0V9OlXPpPeOOhKV2ELt8nL0q3PZyKVZEC9ZLEXIoN1RgDaBwBedWniXqydIeMs 71YSpYfOFZQ/nO0qO/gwA7eEVaIIosL26RenbXL/reVe0aXlwOX6MudiFfpdjLuFZGuEYgq+UL6DY OQZHjTsR84uzSckPQzJBDCIdwazHb/SqBqZuoFE32AtlwAmSCNOueBmLTZ3cBdhbZ1N5HqDj8em7j 3cCpGnGP853xpCboE7ZLwD4kjSEShg6ZfbTKPZmBDo7mPTkUDB3FVSN+Y1mg7bm0M+HaN11zTiTjv VkygLfoA==; Received: from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435]) by casper.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ppG7l-00De9m-2M for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:07 +0000 Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-63b4dfead1bso375904b3a.3 for ; Wed, 19 Apr 2023 15:18:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942678; x=1684534678; 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=LUld/G/1WeH25lUdXRYxnjPYZXeYYgEcmI7A3/bRIcw=; b=3BXkkvcr1OqHpZ/1lavYXP7tve2GxDnWf4O2fWPOiSdbOWRF8chSO/YIyqpEuxWDXh BJ7QFDeoj+Jn0Arzj7+Z5YjQU574gbvT6cAETsRRSqDeVkx/Ylx7RmGaHK6RFXNh6Eit fV0M97Hugr5QTbW1u6+18YRRaKIRIagsziaBLExN4bCT6bnocXCkvHc2Ew+80dXvoOZo BF8+iOJKnVBNIx5dtVYx6vnMu5+ZMBbkouCan/31RqRplh6dabd1rARUBLlSsQGaKiSb 4trlbYIIc+EnwzxQuThX6PKBOpT5C2rO+n2a+7R9hS1L0V6qAuL49rS0jBoNV5zJv4h3 Joww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942678; x=1684534678; 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=LUld/G/1WeH25lUdXRYxnjPYZXeYYgEcmI7A3/bRIcw=; b=if/i+A2iH3bnzB2FQB/aB20QMEIZa1ZQSw6FCPL1VVwDEoqcJppwTiWkikmiD55oM6 vvGpOkUCzeviTr9eK3KEN7T/82pmtFKhfRtx8fbvpECKn9DmSqMS+xREF195ox/GyVUW RGOOuVg4GcASwABol4t15e0UwFthVfehUNRf1vbQC6Jc0GZiVDi5VthgL0CJcYDgULHB tws07RoDHaj/4hG7TeUkYpMrdcvUdyWZ5MdxdNkgLxxSCHx1BToeWwJr4eUD80HjkjgR qxrMvO8DJftONFmju8DyL11lxPN5LU79Lrvc+3O59JikMUzUSW8lwvKwgHEG/TCOJ7Fc xbFw== X-Gm-Message-State: AAQBX9dQacdaxAo4NzGwiUaWLtCLg3+fX7XaxZ/bhchfd5j+kC+Bz2Zy kN1oxsABmXjR650mzP0QZMA50w== X-Google-Smtp-Source: AKy350bb7O7vRNJqV0slEkpJiEEitj66qfL0H4rvbzo6EDHZOM2g1chblrb/iCkgTowCPCRsrQSCkw== X-Received: by 2002:a17:902:eb8d:b0:19c:f476:4793 with SMTP id q13-20020a170902eb8d00b0019cf4764793mr5940941plg.51.1681942678346; Wed, 19 Apr 2023 15:17:58 -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.17.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:17:58 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 13/48] RISC-V: KVM: Return early for gstage modifications Date: Wed, 19 Apr 2023 15:16:41 -0700 Message-Id: <20230419221716.3603068-14-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_231801_165286_F66EB8BB X-CRM114-Status: GOOD ( 13.61 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org The gstage entries for CoVE VM is managed by the TSM. Return early for any gstage pte modification operations. Signed-off-by: Atish Patra --- arch/riscv/kvm/mmu.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c index 6b037f7..9693897 100644 --- a/arch/riscv/kvm/mmu.c +++ b/arch/riscv/kvm/mmu.c @@ -16,6 +16,9 @@ #include #include #include +#include +#include +#include #include #include @@ -356,6 +359,11 @@ int kvm_riscv_gstage_ioremap(struct kvm *kvm, gpa_t gpa, .gfp_zero = __GFP_ZERO, }; + if (is_cove_vm(kvm)) { + kvm_debug("%s: KVM doesn't support ioremap for TVM io regions\n", __func__); + return -EPERM; + } + end = (gpa + size + PAGE_SIZE - 1) & PAGE_MASK; pfn = __phys_to_pfn(hpa); @@ -385,6 +393,10 @@ int kvm_riscv_gstage_ioremap(struct kvm *kvm, gpa_t gpa, void kvm_riscv_gstage_iounmap(struct kvm *kvm, gpa_t gpa, unsigned long size) { + /* KVM doesn't map any IO region in gstage for TVM */ + if (is_cove_vm(kvm)) + return; + spin_lock(&kvm->mmu_lock); gstage_unmap_range(kvm, gpa, size, false); spin_unlock(&kvm->mmu_lock); @@ -431,6 +443,10 @@ void kvm_arch_flush_shadow_memslot(struct kvm *kvm, gpa_t gpa = slot->base_gfn << PAGE_SHIFT; phys_addr_t size = slot->npages << PAGE_SHIFT; + /* No need to unmap gstage as it is managed by TSM */ + if (is_cove_vm(kvm)) + return; + spin_lock(&kvm->mmu_lock); gstage_unmap_range(kvm, gpa, size, false); spin_unlock(&kvm->mmu_lock); @@ -547,7 +563,7 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range) { - if (!kvm->arch.pgd) + if (!kvm->arch.pgd || is_cove_vm(kvm)) return false; gstage_unmap_range(kvm, range->start << PAGE_SHIFT, @@ -561,7 +577,7 @@ bool kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range) int ret; kvm_pfn_t pfn = pte_pfn(range->pte); - if (!kvm->arch.pgd) + if (!kvm->arch.pgd || is_cove_vm(kvm)) return false; WARN_ON(range->end - range->start != 1); @@ -582,7 +598,7 @@ bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) u32 ptep_level = 0; u64 size = (range->end - range->start) << PAGE_SHIFT; - if (!kvm->arch.pgd) + if (!kvm->arch.pgd || is_cove_vm(kvm)) return false; WARN_ON(size != PAGE_SIZE && size != PMD_SIZE && size != PUD_SIZE); @@ -600,7 +616,7 @@ bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) u32 ptep_level = 0; u64 size = (range->end - range->start) << PAGE_SHIFT; - if (!kvm->arch.pgd) + if (!kvm->arch.pgd || is_cove_vm(kvm)) return false; WARN_ON(size != PAGE_SIZE && size != PMD_SIZE && size != PUD_SIZE); @@ -737,6 +753,10 @@ void kvm_riscv_gstage_free_pgd(struct kvm *kvm) { void *pgd = NULL; + /* PGD is mapped in TSM */ + if (is_cove_vm(kvm)) + return; + spin_lock(&kvm->mmu_lock); if (kvm->arch.pgd) { gstage_unmap_range(kvm, 0UL, gstage_gpa_size, false); From patchwork Wed Apr 19 22:16:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217603 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 F065BC77B73 for ; Wed, 19 Apr 2023 22:37:54 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=GTzfFdfFo8jW2vBQtPkitP4lPIvk0zR40JmFZNauHjg=; b=haQ2NGiMmGV/Ex HGCoUyLam9GFO15sU6LCK1+offxOdtUKhJOGVoJPVphlcVXqOSFH/dYfF73kznLPUHhOOo3T9eYpQ 9VKSMO85tg5kGdLL3ivBUuQoHzGvw5UO4nSkxTPzQ9RhAgnb+ESJeb+c+FxWpVShyC37IkF1oxXmJ 8o1BYX8SnDoAOFPNls5H2Z0ywJRLFJEWpeobekt3LzuUqcy+5Nu09A8CBjLTs9VExVkho8p9tA3mn LZfNhxh/94KfZttr/2G3oMuRJPNESH0XCMlGrF0sxc4xPk0alMHnTFuiJSdPO3V313Vh1xjeyX+1D zcajn7isCVW8CFiPyu8A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQu-006Wy0-12; Wed, 19 Apr 2023 22:37:48 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQl-006WXV-0C for linux-riscv@bombadil.infradead.org; Wed, 19 Apr 2023 22:37:39 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=UvoLTFH11lm+jG4OLKr6/L5S3PiYUonU+A7E1tEIRKo=; b=YKQMBzDjUjPkbmaaLbtU3qwKtK 760MjI8RqrBi0zzXnUVYBpG9OxyhB4aDBygqnXQQL1VZZmRc44Bbmd7iw7baSnwh/QXE/aqr2BQjw hOLCgtpY7loyP9T4ZibSo+KLRClersGrZ7JZ8h6c0xCrN5/MmE+h91L5e1KZicuBq/+OiO1dVHHaH JjIWdlSiHmXGmeDmjYrJMYSSVur5zl0STImcWgcaUokcTHAJRmgBk+ASkHaqGCYVgc7b40hiRfFij zg2rE2umctxip3uHvLuiOaIRtPQWQIVecq3oWjbhZw1uNfa2+MGTTQ92/bEk8Cb6X7TT8B92FYeIR vGujcUQw==; Received: from mail-pf1-x434.google.com ([2607:f8b0:4864:20::434]) by casper.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ppG7n-00DeBE-9N for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:09 +0000 Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-63d2ba63dddso346415b3a.2 for ; Wed, 19 Apr 2023 15:18:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942680; x=1684534680; 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=UvoLTFH11lm+jG4OLKr6/L5S3PiYUonU+A7E1tEIRKo=; b=N4XtdIUPBSqsJPJi86uCyaF35SsvQqKIOljuRP/nM3GeL5PnC+sHwcgW+t5y7DrxbF 5TpNpwBssHKlVMD0+rR0V581oyy3Tas3sY/32xO2PA8Waej/gLWBYShGPmuxBhpf6re5 DdBOslqLXNOPSWtzW2KMHDPcIh6b83lTPIBfFraWIh9OIpnFYzRRhBid7+FKOFDAgg76 TSEZFYh5jipl2xgrex0+4e/qWmzQSgGTsJyLQHLHYVjRsdnpfnSpQ74r0gthMKcWw1bC bNIhm8IsywbFNoO4sfJHPMRDxwvnuXKEz2kBcBAbp8YppTYmN0JgS+/GNdHTRqrvAGAB SHNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942680; x=1684534680; 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=UvoLTFH11lm+jG4OLKr6/L5S3PiYUonU+A7E1tEIRKo=; b=UZ2fGudJGf4pLKEANlZkUQwZ/fX91fswACQuAAXByFQcoJFM2GnMPeJa/96VVt/Ufa zpgjRZ+JAXm+4mhwj7BrV5pC4yuiLEi5RvA9O0t6WpdbOPA03WfkcSU1wVd4kbQ+ZTTw hBUu59b77SBDUhcer/EyJyUsq0NhzLBgswJBTcmZJrnV1w5jJ6oc2zmAc9gVa28ncxkA Pxlx5GCFzZGkOrO6yrLy/tdbreoL5R0b/Lkemji5YkDGo3bq87IYpmjhCQ+c+RL2N+2A gHktahAt3DyQZqXDWs+PuSPUmlVEECrbL/wJIzxxiFivWWL/ujRyDLd1fPm4hqGDAFm0 xXSg== X-Gm-Message-State: AAQBX9dHh1B4TOHSme9qKr/QnIwMGBCj4u4QWt/tqNYb89RXCX7rywNE FwvJtPAabm7xFhBsrsA2bLZc/Q== X-Google-Smtp-Source: AKy350Z88ZumXUVLTXNRcg1zbdln/2qU8CAI5Xlijd5PgR7U+e9fUdXEuzGKHFP/DBsSEjOZB75l1A== X-Received: by 2002:a17:903:2287:b0:1a9:2951:7753 with SMTP id b7-20020a170903228700b001a929517753mr1272548plh.45.1681942680495; Wed, 19 Apr 2023 15:18:00 -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.17.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:00 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 14/48] RISC-V: KVM: Skip dirty logging updates for TVM Date: Wed, 19 Apr 2023 15:16:42 -0700 Message-Id: <20230419221716.3603068-15-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_231803_393718_AFC99B50 X-CRM114-Status: GOOD ( 11.19 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org The CoVE doesn't support dirty logging for TVMs yet. Skip for now. Signed-off-by: Atish Patra --- arch/riscv/kvm/mmu.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c index 9693897..1d5e4ed 100644 --- a/arch/riscv/kvm/mmu.c +++ b/arch/riscv/kvm/mmu.c @@ -457,6 +457,9 @@ void kvm_arch_commit_memory_region(struct kvm *kvm, const struct kvm_memory_slot *new, enum kvm_mr_change change) { + /* We don't support dirty logging for CoVE guests yet */ + if (is_cove_vm(kvm)) + return; /* * At this point memslot has been committed and there is an * allocated dirty_bitmap[], dirty pages will be tracked while From patchwork Wed Apr 19 22:16:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217602 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 E5749C77B75 for ; Wed, 19 Apr 2023 22:37:53 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=0MnGhUfWqjknEQ8s4nhsl/eVfna39ujMRVZxh5R3Wgw=; b=vTR5b8Lg+psD6W KKdxxFAeoK9ICAY/uMbU4KLLjFm4FJVH1miTvmu22sJ0je7kN6WYotNT3Qq5JaZc93JqqZL5dlxsb 7EsGg48KawE4SF6+0e8BTpyvQxAxRryR4/JSXvdy0PttiKfa21DIC6tKZUOrSAvD+ZN0SH6Hppzao X7uHU5wkYMwGDsmWr4BDm+TSzXNGAxHfj3HngXq6taUN+sHnoJiSxe5SKAPvE42X2RXOn5H0gR9Kp NTD9DQ3foF2Lw8o1cLShGq61IvCCglhB+2whiA0Yf2mvb+4+O+4aDGtzjZ7hbUmxHd+YUowD2j5yH DtqQpUcSMvHlNJGL2wuQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQt-006Wx2-1t; Wed, 19 Apr 2023 22:37:47 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQk-006WXV-2F for linux-riscv@bombadil.infradead.org; Wed, 19 Apr 2023 22:37:38 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=gRHTy1c+Pl7OVrg4l834g5VjRWcSWQwGhgEvJAPkg+w=; b=HKRCdjqxkUofja+gDpkfK5KD44 xqYsmkcRlGmck/YCxlTHpmmCHW29ILlJtak7vy2W2LRONr/2beTgRvutOP9v/5AHFPodturYkwoft nWreSAEUjMguWYF1DswYZ5oZ5fBL5d2NleS+X0yNTqgmsTja7eAD3xuU0c7pk5YdyiV1mv8bqdnGj 2OKjT1MRimyVcru/rRIqkyyQ6kck/X5PTFsH0PB9eX3CBiLxvaSKZFw0qHY6z14Ad757pZKdl5f7U zaUuk9vg5kDAPpHWFZxVuEI39rMgw7bjOQRMU2CiroyxaYnXACzr/ibKjxv7uA79UC01Jtbg8tUGD 1ShNq5ZA==; Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by casper.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ppG7p-00DeCA-8X for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:09 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1a66888cb89so4502975ad.3 for ; Wed, 19 Apr 2023 15:18:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942682; x=1684534682; 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=gRHTy1c+Pl7OVrg4l834g5VjRWcSWQwGhgEvJAPkg+w=; b=kmEcD4YfOAFXGG17QX3KsyUFIzwyUfrsKZ1wYGSvZgqODlY6mzST437Sn9HGEmF2e4 2a2a47FJD8fRdSNkrzKWkWk+t1hYBGWo5RKv/QKbDEChEI7DPntySLvvhVENeLXEvAzZ Yi9hf+mn4OFq02nwK3TFnSe2tJbzQbrAKkpWS2zkyUDSsMGzPeY61HsFbh3a25B058l+ uUA1pt3+Fbza1RQ2lPxN0ghz2+DhS82u09mOfYSZt3cX82Z63iV2DNRddUax6jZ/SlYZ 5R7VI8q/CylWQy7dmPwpqmG5M0g3aSf6g2QGsKHX2ECmbls1SK5jLS9FX181LfzlvClk L9rA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942682; x=1684534682; 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=gRHTy1c+Pl7OVrg4l834g5VjRWcSWQwGhgEvJAPkg+w=; b=SN+F8MD82/wqdt9EbxWPGTliiGFR5ZkTdFHQLeOjjHg1XsJz/84plPpHj7ifnEdUAd gp+VFziqk288TJnMlGipSpXCB9YzB6HaBWafZRqr2dqjN9FzCvg18nRppPOyU4wJ4JwP U4yOmjwAfUFlQdgoqlcBvdq8YJvJHBqBil5DxU6vU03vNP9saq91akNoizXZmcQ0N3Ie GRNIF6YuTnhp2X/XimEnlJ7lJ8cB/JdnVchcnSeR8boeoEYzV0sh3B9MyPz50n82hZHA 49xPG+u5XkVHOrQ+FMhmz743CkuX7P6u3lOap98sHysxlCrHk8KCCMLIAcJnGHU+72qK 0faA== X-Gm-Message-State: AAQBX9ef5o9unW0NBjjPsfGpLtpS/kXm/b1WwSGKJxFkJyyqVl4X9bnr wrnJ4hduEqgkSn9WbbiXhVJjGw== X-Google-Smtp-Source: AKy350YWY4zn6DitmSoPXPvjQSlrg5vxaeh3mQBYR6TvOk3ni0Mxh3KE27JFZrBKZBdZgegUV17R1Q== X-Received: by 2002:a17:902:ec8b:b0:1a9:23b7:9182 with SMTP id x11-20020a170902ec8b00b001a923b79182mr2673903plg.27.1681942682652; Wed, 19 Apr 2023 15:18:02 -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.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:02 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 15/48] RISC-V: KVM: Add a helper function to trigger fence ops Date: Wed, 19 Apr 2023 15:16:43 -0700 Message-Id: <20230419221716.3603068-16-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_231805_388629_4A94850A X-CRM114-Status: GOOD ( 16.82 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org When Cove is enabled in RISC-V, the TLB shootdown happens in co-ordination with TSM. The host must not issue hfence directly. It relies on TSM to do that instead. It just needs to initiate the process and make sure that all the running vcpus exit the guest mode. As a result, it traps to TSM and TSM issues hfence on behalf of the host. Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_cove.h | 2 ++ arch/riscv/kvm/cove.c | 36 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/arch/riscv/include/asm/kvm_cove.h b/arch/riscv/include/asm/kvm_cove.h index 4ea1df1..fc8633d 100644 --- a/arch/riscv/include/asm/kvm_cove.h +++ b/arch/riscv/include/asm/kvm_cove.h @@ -130,6 +130,8 @@ void kvm_riscv_cove_vcpu_switchto(struct kvm_vcpu *vcpu, struct kvm_cpu_trap *tr int kvm_riscv_cove_vm_measure_pages(struct kvm *kvm, struct kvm_riscv_cove_measure_region *mr); int kvm_riscv_cove_vm_add_memreg(struct kvm *kvm, unsigned long gpa, unsigned long size); int kvm_riscv_cove_gstage_map(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned long hva); +/* Fence related function */ +int kvm_riscv_cove_tvm_fence(struct kvm_vcpu *vcpu); #else static inline bool kvm_riscv_cove_enabled(void) {return false; }; static inline int kvm_riscv_cove_init(void) { return -1; } diff --git a/arch/riscv/kvm/cove.c b/arch/riscv/kvm/cove.c index 5b4d9ba..4efcae3 100644 --- a/arch/riscv/kvm/cove.c +++ b/arch/riscv/kvm/cove.c @@ -78,6 +78,42 @@ static int kvm_riscv_cove_fence(void) return rc; } +int kvm_riscv_cove_tvm_fence(struct kvm_vcpu *vcpu) +{ + struct kvm *kvm = vcpu->kvm; + struct kvm_cove_tvm_context *tvmc = kvm->arch.tvmc; + DECLARE_BITMAP(vcpu_mask, KVM_MAX_VCPUS); + unsigned long i; + struct kvm_vcpu *temp_vcpu; + int ret; + + if (!tvmc) + return -EINVAL; + + spin_lock(&tvmc->tvm_fence_lock); + ret = sbi_covh_tvm_initiate_fence(tvmc->tvm_guest_id); + if (ret) { + spin_unlock(&tvmc->tvm_fence_lock); + return ret; + } + + bitmap_clear(vcpu_mask, 0, KVM_MAX_VCPUS); + kvm_for_each_vcpu(i, temp_vcpu, kvm) { + if (temp_vcpu != vcpu) + bitmap_set(vcpu_mask, i, 1); + } + + /* + * The host just needs to make sure that the running vcpus exit the + * guest mode and traps into TSM so that it can issue hfence. + */ + kvm_make_vcpus_request_mask(kvm, KVM_REQ_OUTSIDE_GUEST_MODE, vcpu_mask); + spin_unlock(&tvmc->tvm_fence_lock); + + return 0; +} + + static int cove_convert_pages(unsigned long phys_addr, unsigned long npages, bool fence) { int rc; From patchwork Wed Apr 19 22:16:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217580 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 32093C6FD18 for ; Wed, 19 Apr 2023 22:25:45 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=PezAG/1NFSiGHgoxeIgl1Yr95JlpIVTNnfaKn8Ky0kA=; b=D+2Vb585cvxmvm zLt0ua0Cmbi5OIBMN7pUPvaK396fkzN0o7fhFqvwcgOIHNd/qbpx8i/vPrqcjugJz6CgzhaBZNmrA Mn18NDQRklDAkNO8s+JVe3LYAw6oQAV0z7AVEKdkTtxLpP51NWscMT4lJAbHrKV3+Km/6JchLPhd/ 77z15d0XEuO8L3Dk9z3L4GBKL493dtB2D6txLWnYSBRJiW5lA8iXBb0KDMlyQqkSXQCyDH8iVbHV7 nKt9zqT2mstPRJSxbiSM3NXGLf6ILPvOTUpdrjSbMDOlxgpG3p7lkdmX/7hviFuhVgz7cpovDP3eI qy7ceU4m0K+sNI9lUrzw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGF9-006V8i-0q; Wed, 19 Apr 2023 22:25:39 +0000 Received: from mail-yw1-x1135.google.com ([2607:f8b0:4864:20::1135]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGF5-006V6w-0N for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:25:37 +0000 Received: by mail-yw1-x1135.google.com with SMTP id 00721157ae682-54fe25c2765so3754127b3.3 for ; Wed, 19 Apr 2023 15:25:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681943132; x=1684535132; 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=ojiAvkB2QjgGX0WuePfTk5AH+Ftof3ihRnPYCRhXfys=; b=nFNsj5scFi/GRqkwMsKxCwS1liPDLGmDdauVHvD6dFHHZzCgrY/5HEG29GXiDyavv9 kRd/WBDjtO0uulVis3EeAvDE5qT5rfJIvJ2gEKENIjOLjGP0Z8tKKdHdHwQImxPQFMRB t8kYP/O06d6/pAygirZs6WL8SIH4R8uMfl9QqMQ447z9nfC8pamfDqG7z+90q58oYMfR pkUGNBEiPX4jRWJAzizbaIj+DkeiIr+LBqMXRMXDcO5agbkdB+ky6vjY+GOzqsWBD0jX ZwZbZ8FU/+jgMr+MZiW7QgF6l4PO0ZVCmko6Gx0TciTUWkKH+Oh2F5Zf+vMea7cEtRjF lzpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681943132; x=1684535132; 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=ojiAvkB2QjgGX0WuePfTk5AH+Ftof3ihRnPYCRhXfys=; b=kXhWUchIEO4yB++GxkEShLSR1MIGkxVJZg8c4ap3nbX+bs3gdWvMt+Os+PEJwyaTkL LfxgeYYTm97/J2noW3Ol3W4626E2RD7DbN/ezACHFwZxuGQYcYKzHKwBAG9Pw9sEazfQ IGdksNSeCLOWQG8ss3OSm8e1FQV436r16RB3mviF7qqKJrg8kAN1icDI+LTl/ioLVgUC CVTE4tToZ5yg9NbMdiqo4VhfF/BqCAl8phwsJijXVrUvX06I+9KZLKznvBSrqRpVqf8a PCkY2vXRIu+eLdVurvhjahpFQf7feFGqPhgLcUQgCRKZ2rePzjISOk22xoEQ1DMpS83C ASHA== X-Gm-Message-State: AAQBX9fj2z1XP2fZ3r5LJ0Ox9oDmU9CA/Po9c1l0CtrThXh+DUs66hDO +QtZ4SRnP4Yagns4YMuvKgviz52T1IOYMr2A0HI= X-Google-Smtp-Source: AKy350aUmxK/wzp1xvJIpKWslK3gwGr/UxzJX5snN5ZgZlPl8MyALzkERpVzHjZpOuQ6botaKIIVqg== X-Received: by 2002:a17:903:22c1:b0:1a1:c3eb:afd with SMTP id y1-20020a17090322c100b001a1c3eb0afdmr7315223plg.65.1681942684832; Wed, 19 Apr 2023 15:18:04 -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.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:04 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 16/48] RISC-V: KVM: Skip most VCPU requests for TVMs Date: Wed, 19 Apr 2023 15:16:44 -0700 Message-Id: <20230419221716.3603068-17-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_152535_157880_9ABDEFAE X-CRM114-Status: GOOD ( 13.78 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Currently, KVM manages TLB shootdown, hgatp updates and fence.i through vcpu requests. TLB shootdown for the TVMs happens in co-ordination with TSM. The fence.i & hgatp updates are directly managed by the TSM. There is no need to issue these requests directly for TVMs. Signed-off-by: Atish Patra --- arch/riscv/kvm/vcpu.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index c53bf98..3b600c6 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -22,6 +22,7 @@ #include #include #include +#include const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = { KVM_GENERIC_VCPU_STATS(), @@ -1078,6 +1079,15 @@ static void kvm_riscv_check_vcpu_requests(struct kvm_vcpu *vcpu) if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu)) kvm_riscv_reset_vcpu(vcpu); + if (is_cove_vcpu(vcpu)) { + /* + * KVM doesn't need to do anything special here + * as the TSM is expected track the tlb version and issue + * hfence when vcpu is scheduled again. + */ + return; + } + if (kvm_check_request(KVM_REQ_UPDATE_HGATP, vcpu)) kvm_riscv_gstage_update_hgatp(vcpu); From patchwork Wed Apr 19 22:16:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217506 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 30EA4C6FD18 for ; Wed, 19 Apr 2023 22:18:17 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=D2lb017B0/uycnd3eww/P7ELMGbM2lZkIImNC5I+Kgw=; b=Cx2vQHeQkContK HOsTIRZTI0OTvnNb6epJQCnofS6uHPOWr8dOk6/HDq38bShfLdipnMbl40TjNSvfLnaR8VX2TT2S0 7ssZebVJ+94Oj6ESjSsbgYPXYnGKUyPu4UxNMn4+DEdOIjG7+Wfph/O6rc5EkQljV2dKC4XCboUcp bGsyzC/3aoYSXZ8EQfCumpfGyP5Knsoc9zirTNcf7mAe9N4j/q7KzxtYPOdVj/FRu78g5MotAVLDs eGKyRo3ibo4MkU9fXYqcmuxugatkeojqRYIgyxo8u8pQuyK/EdbgDttNUTQ2HTycS2AtMGysTD+n8 wTL5bf8pl5mdpDrnqeWg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppG7u-006T1n-23; Wed, 19 Apr 2023 22:18:10 +0000 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG7r-006SnP-0y for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:08 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1a69f686345so4526955ad.2 for ; Wed, 19 Apr 2023 15:18:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942687; x=1684534687; 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=IHi27AH8EgCkCQBErjcLInO9Ogd99Uht/0Y2I/rPxr0=; b=YSIfA3fBHehSI9sfNw8kNY3SjYw3MKpDND587n51K3eoVf8RJTCG1iSaqqG9yfNjCx jwqPpoi/ITD0shW0NUnRyZli8JT8h1t3/2gNbp76WuPUO0CZ5s8zSvoyiHQFyjfIMwOE 1ZldJnGMaXcDrIoP6ErfdR2UHM3fNFztr08TXf0xVGRFo5tk0moUvzoYm/r0hkRFoy0K cWLNwCIHBfTQ0bCQR3c38U1nAB1slt1q42rqYoamEUYp5QQ79+wtILJqFzbQlYA06O6P OEHwUmVOCGHeSnw6bqeEZSZ4KjU2xfgwWJGo24N5J96VrnqV0nME7zdNFrLcXHLPuPgD RkSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942687; x=1684534687; 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=IHi27AH8EgCkCQBErjcLInO9Ogd99Uht/0Y2I/rPxr0=; b=GIA1EKzZDTqYlaCiQYXu/xQHiqdGXaVTWbGee0v0cUhlhPJHgOsCRCmF9dB14KRejf ErUfXYcB24lrWngm0cfuiUUsazXgBVrJ+7ZtYQT+CRBWxwmcJHHCsxrbCf1o246RnRBK CT3EDpWGr9EumIvmZh1MypOHdM+vc0i9ytZT6pa9+/8zUfm76eAxT01tRR8fcta8eSu2 MIWJZoHhcBXo6V9YB+tlX9YmMRqggISa8fgIFzNETFylPZcDFL+YdIdvFamDSGBoCjT3 CfZO26yDQopf4v2thrSPvkamvMcnUK3QlxTu9pjTecrruxwrN31pIoRwRKHZLZ2t33qk 25DQ== X-Gm-Message-State: AAQBX9eMHEKhlVdHULBih1euSjxYK5a2bZ2xA+cwA13UV1GRGbzIoJk5 mvGDm4uQYLSqP9Z8+Fe80g2cEg== X-Google-Smtp-Source: AKy350a7uqnn10B5RYMJKbmqBxpZkXHpV8hWE2JbxUvi5COFiIeLm7AFkPV91DbiIrXf3nFN1rwkeg== X-Received: by 2002:a17:902:ec8b:b0:1a9:23b7:9182 with SMTP id x11-20020a170902ec8b00b001a923b79182mr2674160plg.27.1681942686991; Wed, 19 Apr 2023 15:18:06 -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.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:06 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 17/48] RISC-V : KVM: Skip vmid/hgatp management for TVMs Date: Wed, 19 Apr 2023 15:16:45 -0700 Message-Id: <20230419221716.3603068-18-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151807_356764_1816C4D5 X-CRM114-Status: GOOD ( 18.92 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org The TSM manages the vmid for the guests running in CoVE. The host doesn't need to update vmid at all. As a result, the host doesn't need to update the hgatp as well. Return early for vmid/hgatp management functions for confidential guests. Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_host.h | 2 +- arch/riscv/kvm/mmu.c | 4 ++++ arch/riscv/kvm/vcpu.c | 2 +- arch/riscv/kvm/vmid.c | 17 ++++++++++++----- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h index ca2ebe3..047e046 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -325,7 +325,7 @@ unsigned long kvm_riscv_gstage_pgd_size(void); void __init kvm_riscv_gstage_vmid_detect(void); unsigned long kvm_riscv_gstage_vmid_bits(void); int kvm_riscv_gstage_vmid_init(struct kvm *kvm); -bool kvm_riscv_gstage_vmid_ver_changed(struct kvm_vmid *vmid); +bool kvm_riscv_gstage_vmid_ver_changed(struct kvm *kvm); void kvm_riscv_gstage_vmid_update(struct kvm_vcpu *vcpu); int kvm_riscv_setup_default_irq_routing(struct kvm *kvm, u32 lines); diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c index 1d5e4ed..4b0f09e 100644 --- a/arch/riscv/kvm/mmu.c +++ b/arch/riscv/kvm/mmu.c @@ -778,6 +778,10 @@ void kvm_riscv_gstage_update_hgatp(struct kvm_vcpu *vcpu) unsigned long hgatp = gstage_mode; struct kvm_arch *k = &vcpu->kvm->arch; + /* COVE VCPU hgatp is managed by TSM. */ + if (is_cove_vcpu(vcpu)) + return; + hgatp |= (READ_ONCE(k->vmid.vmid) << HGATP_VMID_SHIFT) & HGATP_VMID; hgatp |= (k->pgd_phys >> PAGE_SHIFT) & HGATP_PPN; diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index 3b600c6..8cf462c 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -1288,7 +1288,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) kvm_riscv_update_hvip(vcpu); if (ret <= 0 || - kvm_riscv_gstage_vmid_ver_changed(&vcpu->kvm->arch.vmid) || + kvm_riscv_gstage_vmid_ver_changed(vcpu->kvm) || kvm_request_pending(vcpu) || xfer_to_guest_mode_work_pending()) { vcpu->mode = OUTSIDE_GUEST_MODE; diff --git a/arch/riscv/kvm/vmid.c b/arch/riscv/kvm/vmid.c index ddc9871..dc03601 100644 --- a/arch/riscv/kvm/vmid.c +++ b/arch/riscv/kvm/vmid.c @@ -14,6 +14,7 @@ #include #include #include +#include static unsigned long vmid_version = 1; static unsigned long vmid_next; @@ -54,12 +55,13 @@ int kvm_riscv_gstage_vmid_init(struct kvm *kvm) return 0; } -bool kvm_riscv_gstage_vmid_ver_changed(struct kvm_vmid *vmid) +bool kvm_riscv_gstage_vmid_ver_changed(struct kvm *kvm) { - if (!vmid_bits) + /* VMID version can't be changed by the host for TVMs */ + if (!vmid_bits || is_cove_vm(kvm)) return false; - return unlikely(READ_ONCE(vmid->vmid_version) != + return unlikely(READ_ONCE(kvm->arch.vmid.vmid_version) != READ_ONCE(vmid_version)); } @@ -72,9 +74,14 @@ void kvm_riscv_gstage_vmid_update(struct kvm_vcpu *vcpu) { unsigned long i; struct kvm_vcpu *v; + struct kvm *kvm = vcpu->kvm; struct kvm_vmid *vmid = &vcpu->kvm->arch.vmid; - if (!kvm_riscv_gstage_vmid_ver_changed(vmid)) + /* No VMID management for TVMs by the host */ + if (is_cove_vcpu(vcpu)) + return; + + if (!kvm_riscv_gstage_vmid_ver_changed(kvm)) return; spin_lock(&vmid_lock); @@ -83,7 +90,7 @@ void kvm_riscv_gstage_vmid_update(struct kvm_vcpu *vcpu) * We need to re-check the vmid_version here to ensure that if * another vcpu already allocated a valid vmid for this vm. */ - if (!kvm_riscv_gstage_vmid_ver_changed(vmid)) { + if (!kvm_riscv_gstage_vmid_ver_changed(kvm)) { spin_unlock(&vmid_lock); return; } From patchwork Wed Apr 19 22:16:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217507 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 9D71BC77B75 for ; Wed, 19 Apr 2023 22:18:19 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=d2e3yKCmmXyJVMLmHisDMBSAbwu0P1wv8JD2X6/XQ2M=; b=YEfPujtNi/jDmK tKYIjPnwoDTiG34kOBRrbZr9SWricHEuRXgvwC3XQpTFpCXgebG3HSdOVhHMluMgtPrz3hgzAd4+x zt/rbHEbb7ct6jj2f9K7DoZN3wfFef2p48/eSpkYEfPvD85LCM4oT9+lrhkp0ZcEH6Ng9PBd5Ua3Q ARi9CwOKImxKuSogYBIyLpKcMaP9Zdo8mTX7llJBPocnZzMHyM+ysSDmd5478ojgJtxBBw+SKa8+H Eggzdi8Jlo50zB3Go1OkpHjcaYgdUO3Lk1GBkqHilxa2Awcpug5w+OYWz/KoTkvH7V+w9pQ2wRSk8 ieyIqx6bONXPTNVUkf+g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppG7w-006T3T-1x; Wed, 19 Apr 2023 22:18:12 +0000 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG7t-006SnP-1I for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:10 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1a69f686345so4527095ad.2 for ; Wed, 19 Apr 2023 15:18:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942689; x=1684534689; 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=EbUa6ryrerMYndbR89JSXBoBIycYIOI5MWjMv8QTenY=; b=EpHc5rfijGiS9cvhe3gxGYJxsSuAlVbG/pIJOuFJNosZx/VFQeVfm0eSrlDMuReSFj 7ypZdw3QjuxdW55Zh8SHxl8sc3yXVSNROJW2fjaa+26b7equVpWpnxFDYreqHSbXDwun vUfn3kQvN844BJ2Z55UTjnK33zctXOKmEJb38uE3yoQBIsr74T/OcqMWb3KweGfTU4Yi yYPd+HV7CeU/AvlBhTKuqFAlbX3z2KwharbhJqAcrRnAlg1XHGRRtMmnLgMuCDk8ejGu HvdE2AEcRzhomKSg5akQJMWerijQ4tiFWsNpofhgVRA19F6Cu15/PaXEiC7xDxP0NCRj VY9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942689; x=1684534689; 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=EbUa6ryrerMYndbR89JSXBoBIycYIOI5MWjMv8QTenY=; b=e6VeLPyOFjRUzknzf5PQMk5rpxLhwHljNt0FcFLfvPdZzYGu5t2SRqzyq/KC1TIoLP mcjjnw1LYtWZDcJ1YN9vvgcDGFfnivh8XAwB6R9cWUbdQ2exK2UuRkryG53ylbFbKew8 pnjTp4ziAEw/RaxynBsCPsMGOjLhvnNBdozu9mkCVFSU8fEixjx1EliqR6KmdyvScWXN exI1YGqK53FSXR8fj9K6Nv+apK5GVR3d658ZE7sOB/j/5tdN5g3L6UetZpl8zjwNJLb6 vxpesDHO8GL26Vb2EG6Tu+Vqw1jR6ArjwXytqGUdYO9YMYJA81LO/zjDl7bAszBfH+fi qPcg== X-Gm-Message-State: AAQBX9e0HXNuQdF4qd06vEW332lPj49u8/uHHHaEYFXy8jkOgiqy8Zc0 y+kXPaN+4ZQogIVaotzQwtPllQ== X-Google-Smtp-Source: AKy350bOrY/ltn4aOC/irJe3I2c3DHWQXEGnacE0hKIRD44viSqKcVds6P+5HuawRV2gn0DMpSc7nw== X-Received: by 2002:a17:902:e80c:b0:1a8:32e:3256 with SMTP id u12-20020a170902e80c00b001a8032e3256mr6684314plg.35.1681942689090; Wed, 19 Apr 2023 15:18:09 -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.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:08 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 18/48] RISC-V: KVM: Skip TLB management for TVMs Date: Wed, 19 Apr 2023 15:16:46 -0700 Message-Id: <20230419221716.3603068-19-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151809_437911_F3272319 X-CRM114-Status: GOOD ( 13.76 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org TSM manages the tlb entries for the TVMs. Thus, host can ignore all the hfence requests or tlb updates for confidential guests. Most of the hfence requests happen through vcpu requests which are skipped for TVMs. Thus, we just need to take care of the invocation from tlb management here. Signed-off-by: Atish Patra --- arch/riscv/kvm/tlb.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/riscv/kvm/tlb.c b/arch/riscv/kvm/tlb.c index dff37b57..b007c02 100644 --- a/arch/riscv/kvm/tlb.c +++ b/arch/riscv/kvm/tlb.c @@ -15,6 +15,7 @@ #include #include #include +#include #define has_svinval() riscv_has_extension_unlikely(RISCV_ISA_EXT_SVINVAL) @@ -72,6 +73,14 @@ void kvm_riscv_local_hfence_gvma_gpa(gpa_t gpa, gpa_t gpsz, void kvm_riscv_local_hfence_gvma_all(void) { + /* For TVMs, TSM will take care of hfence. + * TODO: We can't skip unconditionally if cove is enabled + * as the host may be running in HS-mode and need to issue hfence + * for legacy VMs. + */ + if (kvm_riscv_cove_enabled()) + return; + asm volatile(HFENCE_GVMA(zero, zero) : : : "memory"); } @@ -160,7 +169,7 @@ void kvm_riscv_local_tlb_sanitize(struct kvm_vcpu *vcpu) { unsigned long vmid; - if (!kvm_riscv_gstage_vmid_bits() || + if (is_cove_vcpu(vcpu) || !kvm_riscv_gstage_vmid_bits() || vcpu->arch.last_exit_cpu == vcpu->cpu) return; From patchwork Wed Apr 19 22:16:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217508 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 84357C77B7C for ; Wed, 19 Apr 2023 22:18:22 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=j/lpSN6LVnENVOmYwqzbcCOTk8gm4iY2IbMAiBLwh0w=; b=X+oJ9aRcow2erQ 8tWrRyD4GEP6zU+Jv+8IbIGmEPh7NUCTxVwaFVqO1Q+imdDxm/neBxKX5M88xogKa25Q6amVGBni9 9yb11mo6Tq0GUZeZqg9ENCWZ+hN5KJ4y9OKNMqGXlDE1HQ6UGgNILKpOJVWX/SYeDDGHWSVyNfGqj YOWsYttrkm76mLMGOOmw6Xg72YYs1tij0aFXLQWEoawH3IXgftHmtpKcJF/MjiQBoFNvy9rA0O3ur SzZ8iLKGFqVX+73aFmOnm1W7MnaCbWotb/tUNfb2FIUVirngEu28AsSg/vJEsNko0DWsCLj95XZeY sfkRZIyNVBJNmNV9o46g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppG7z-006T5s-3D; Wed, 19 Apr 2023 22:18:16 +0000 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG7v-006T2d-2M for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:14 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1a6715ee82fso5264415ad.1 for ; Wed, 19 Apr 2023 15:18:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942691; x=1684534691; 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=EN7cfqRTuUxE4NYFTrE6RcyXhfZTP+XO506Z7P5jnSo=; b=2HntuY5n1tLM4pNJa7nz4osjLmrd+gHhTujKcUSE8JGJeCF47AW9Dyj0rRPbdS8CKR ec8mdHxJp3zhFL+VPoHjKpwqqItbmuGrX1ePOLL/H/CjhizNW48AJx/+m0SEc6hw+lrg 4TAnXTUfT+76XBgFTJYmo+eDlVergYm7g4UDIIG8q8towz6RIKhd0qUN5gI1M3MPJ6Yf v6+fwgTgJxyGyevBN2wreiHgOozsBMp0FBa2pY9PJ2gXBMvj4I9WYLUgQzPavzY4XGMn BfwkMktMwYiFO0NHEmqBJ8uwsXt7UjR2yMhk7oglZtm60MHD5OqGzZFoQLQcULwN1VOW 9p+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942691; x=1684534691; 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=EN7cfqRTuUxE4NYFTrE6RcyXhfZTP+XO506Z7P5jnSo=; b=Jg/gYYBVNwaLQ9ZJnITWtTD7cPkUGhRkCxD69+CHBcQZeGr4yNmen9Nucy4zdBDSGd GSf8AaZxQ4OKSRX3DQ/EHy7gpjtrrjTyKs6uV77TeOKvaIbjoDLbvi0Dx0EcY3Epruxy xKA3C8BbdIPjXDscsBlE0Hhd6lZq4Y3M4cAESs2pvAvC8kzQrrYwgNobZC9J8DWrvwKQ UeZVmHtGnlcLrut8WOfOHrr1LnM4ay6MQMqhFAvYVM8GZVTTgJSNCgvZpffZ3kg5L/Xl s71P2qNp1AH5929F4yERcGSLqv+n9ZL4R6RRu7g4srlhh46bj8emJ3o60jMPxncwO8Vn e3DQ== X-Gm-Message-State: AAQBX9fYWbjmNNO3aG4IFe6FMnXVx3qBPIJbSzR6PwwVkSqDXIg+h9Ij zFehS6XZHJYxjwUpKQqOYaq/mg== X-Google-Smtp-Source: AKy350ZYsA8lzJG7NSbCoTlmBQanAcDJyPETNQetpvDDQv9McJwUAM0nHCIZ5qbS6IwUoaXgRnH5YQ== X-Received: by 2002:a17:902:8b8a:b0:1a6:dfb3:5f4b with SMTP id ay10-20020a1709028b8a00b001a6dfb35f4bmr5466195plb.55.1681942691242; Wed, 19 Apr 2023 15:18:11 -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.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:11 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 19/48] RISC-V: KVM: Register memory regions as confidential for TVMs Date: Wed, 19 Apr 2023 15:16:47 -0700 Message-Id: <20230419221716.3603068-20-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151811_770268_A370F0E9 X-CRM114-Status: GOOD ( 10.47 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org The entire DRAM region of a TVM running in CoVE must be confidential by default. If a TVM wishes to share any sub-region, the TVM has to request it explicitly with memory share APIs. Mark the memory region as confidential during vm create itself. Signed-off-by: Atish Patra --- arch/riscv/kvm/mmu.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c index 4b0f09e..63889d9 100644 --- a/arch/riscv/kvm/mmu.c +++ b/arch/riscv/kvm/mmu.c @@ -499,6 +499,11 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, mmap_read_lock(current->mm); + if (is_cove_vm(kvm)) { + ret = kvm_riscv_cove_vm_add_memreg(kvm, base_gpa, size); + if (ret) + return ret; + } /* * A memory region could potentially cover multiple VMAs, and * any holes between them, so iterate over all of them to find From patchwork Wed Apr 19 22:16:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217509 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 1F40FC77B7F for ; Wed, 19 Apr 2023 22:18:24 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=DumRLR18v7k/JN9hawYmiOPFVGtUkOFYOik2n4rsgm4=; b=jIPfBm8/gLIRYZ gNmC8ECpMsM+JdkohuCOX2PxKcrkJCCkFe62Cxhf0/Vzbhd0+tzzoGdBw0FJYIUhXkZMepyPQ6BEm tOOeq/2o1kQLOIlYJm/KLSu0y9zvbm8a+U1eRhk1DcKUZw5/1ZrcvijDJbF/uDEdd67bH00QgRT98 SnIavw7ypgL+t2j5KlcBZekn5/A4re7JISU0clouyuOSYZROHwIaQTvVvsbKuHjgRdpAzbFfpjpom nenPUliUIMEDW4sfK2Yl5YrlwRkm5wFqELcXkS4g722wx8G3rD483xN74AMJJB/HSSoURRRwXPJIa nO/v806ttxtaUhDKnNPQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppG80-006T6q-2C; Wed, 19 Apr 2023 22:18:16 +0000 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG7x-006T2c-2M for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:15 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1a686260adcso5384305ad.0 for ; Wed, 19 Apr 2023 15:18:13 -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=Msmy0uV9wqlDWAh9PZLs8uQnoY1Zmu+BoidP3zddmQkbfuQjS6VIfDab5v4jx/7kHE ydlBAto4Nv9p+6rejpvzLOiRRLzvMmZjrbPyoun8Cd7jzPacekj+alZBJem/pBMYAOrV b8mAzyyufj3E+pKwMNJrXOgquuwq2SIs4CX2FREYe6VXC8aVhW9lAy/xSMj/JxYRl8pz Npi4T1erWYrlZ4HFE8UsV6+0uIdM0JoMk004mumRY4VqiIf2aQ6Wtz9/d+DxLZHldBlr A8/D574TlwvE8TUDmIH2vYdn61XPsZ83BPH1rj9guOUh+jLKnl9fHi/2o3zcE25l0AkN JXsw== X-Gm-Message-State: AAQBX9fhO5vh1/QCtxoR3k2sIq8/LtoxNeweH0y00EZ03wKv7WpmwEdG Qf0RnD7B3bHpFxcV1vE1SCW3zw== X-Google-Smtp-Source: AKy350Y8OMGVUtVmKbRjYFU/NfkRlor4BXVO9AnvAi3r9stoJW7W2hRl3eELkUdIf0o2cuCG7q0qBA== 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?b?QmrDtnJuIFTDtnBlbA==?= , 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151813_767273_9838A5FD X-CRM114-Status: GOOD ( 17.97 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.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; From patchwork Wed Apr 19 22:16:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217510 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 A2405C77B75 for ; Wed, 19 Apr 2023 22:18:29 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=EdDnhe3FyyU06MXSVLSkF0+evRlHV3uGualW/k2e7Cw=; b=nnozy+dH67G2IV SBIfpXwio4s31XtrVZJSCLF1/qFYYKiXIIpDNUzIGQPz3KoNvu3QLbCYM+KC7ogiKdPnVHHfHgaem hhE1o8sHVfYZ1hg4qRDUR4YJ4tN3cuatAHYhKiG7x9oK4EhJLzfPba3CFhEUh8dvomrXXkG86sdos k3cKAXACTD4qiSVXtxO/c3fqqyJW7iSePtZtkDHPY3EVWu7PuyBQ+AqeF3v0qxb0a3n6DELPbi8Oo 9DXq5EgkJ5Ap/LZpELxNeScesoFrvyT/BdMrek3wCoJ5sCEXuqGMijwHTf37kkPsARp4LYRpiTDkq WLyqu8zt4Jn02/2BH6cg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppG86-006T9r-1d; Wed, 19 Apr 2023 22:18:22 +0000 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG80-006T5q-0S for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:21 +0000 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-1a66911f5faso4694715ad.0 for ; Wed, 19 Apr 2023 15:18:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942695; x=1684534695; 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=BVskLFhSfykEs4eltGnJpzQVboIqM++EFU/NG1sE5NQ=; b=SO8VMn/gX3e5vrOk29Jpp+EW3CH0aS36Ddf+WvlYfYdV2qV/sEQkL249EYwnIbGQPP IbrgDiUX6yusY5G6N+gv2DqO2gsWL/6qr3Vjo5vnbGXNSTABlKc+Z5vspap8SEcOKeLU So7lcKbcoaSAhkenmIqQER+vNAwXBWV2IvUOGT4NehU2jVE3eYLDxu3zsV9yLUqbsLR7 v7+u/wK5FHf0rE8XEaPscHq9ay3O7yJVyu0alqyYfsf0oZDQ04UuLg/LxXgaBLNutCRN t5gGEdgbWRMkTDfoaMJlc0lkYq+/3m6Gj+9+DtkEoQY+7pVpM5Wn8qS8u2Y8SjuI+hhP lcIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942695; x=1684534695; 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=BVskLFhSfykEs4eltGnJpzQVboIqM++EFU/NG1sE5NQ=; b=VsxjVOwvgBDpCFhaaeUaoirqd6BhAOpf7b995n1vd7M0CthYinGdQhpxVIBDhmC/WT aPHp3jwTSyqRmHaN34W8WxSp3Zti6E9VG8WXOCdgKOQqMZbsfZKl0Z0QBWrvUDFO3fmu kJcDTiK9zBIH6FvZP8VAg4ca/akPv7anVtdciQPdxDqRvak9Lhzbp33OczePoVG0eHM/ xgy1AwsbZLBTOSRuL0vBIzsfXsHli4fvdCuJBwftY6UvZiSrqhlP2ZJCJOss9g1XZ802 AVaobbvVOgHzU+iXKALkGXhfUIo9hoeXt1Wjl5+0WGxQhjkb2zzJddW8a1K9vzPRpLdf txsQ== X-Gm-Message-State: AAQBX9fskSoxkswcSQlRpH/gaSE2xGZXs3tcbwvzZVKBBouIEZw3/h+e tMP3gtR1fBxDyogT78084kaN2g== X-Google-Smtp-Source: AKy350YJdtE76oL4JZlI/rCsVg2p4hVlt1BCJyTIcyC6OmWTTvtKZe676vtO5k8no3TozALhiDYlgQ== X-Received: by 2002:a17:902:b489:b0:19d:297:f30b with SMTP id y9-20020a170902b48900b0019d0297f30bmr6180769plr.19.1681942695581; Wed, 19 Apr 2023 15:18:15 -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.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:15 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 21/48] RISC-V: KVM: Handle SBI call forward from the TSM Date: Wed, 19 Apr 2023 15:16:49 -0700 Message-Id: <20230419221716.3603068-22-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151816_181799_BF10E0D3 X-CRM114-Status: GOOD ( 21.03 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org TSM may forward the some SBI calls to the host as the host is the best place to handle these calls. Any calls related to hart state management or console or guest side interface (COVG) falls under this category. Add a cove specific ecall handler to take appropriate actions upon receiving these SBI calls. Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_cove.h | 5 +++ arch/riscv/kvm/cove.c | 54 +++++++++++++++++++++++++++++++ arch/riscv/kvm/vcpu_exit.c | 6 +++- arch/riscv/kvm/vcpu_sbi.c | 2 ++ 4 files changed, 66 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/kvm_cove.h b/arch/riscv/include/asm/kvm_cove.h index fc8633d..b63682f 100644 --- a/arch/riscv/include/asm/kvm_cove.h +++ b/arch/riscv/include/asm/kvm_cove.h @@ -126,6 +126,7 @@ int kvm_riscv_cove_vcpu_init(struct kvm_vcpu *vcpu); void kvm_riscv_cove_vcpu_load(struct kvm_vcpu *vcpu); void kvm_riscv_cove_vcpu_put(struct kvm_vcpu *vcpu); void kvm_riscv_cove_vcpu_switchto(struct kvm_vcpu *vcpu, struct kvm_cpu_trap *trap); +int kvm_riscv_cove_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run); int kvm_riscv_cove_vm_measure_pages(struct kvm *kvm, struct kvm_riscv_cove_measure_region *mr); int kvm_riscv_cove_vm_add_memreg(struct kvm *kvm, unsigned long gpa, unsigned long size); @@ -148,6 +149,10 @@ static inline int kvm_riscv_cove_vcpu_init(struct kvm_vcpu *vcpu) {return -1; } static inline void kvm_riscv_cove_vcpu_load(struct kvm_vcpu *vcpu) {} static inline void kvm_riscv_cove_vcpu_put(struct kvm_vcpu *vcpu) {} static inline void kvm_riscv_cove_vcpu_switchto(struct kvm_vcpu *vcpu, struct kvm_cpu_trap *trap) {} +static inline int kvm_riscv_cove_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run) +{ + return -1; +} static inline int kvm_riscv_cove_vm_add_memreg(struct kvm *kvm, unsigned long gpa, unsigned long size) {return -1; } static inline int kvm_riscv_cove_vm_measure_pages(struct kvm *kvm, diff --git a/arch/riscv/kvm/cove.c b/arch/riscv/kvm/cove.c index 44095f6..87fa04b 100644 --- a/arch/riscv/kvm/cove.c +++ b/arch/riscv/kvm/cove.c @@ -147,6 +147,60 @@ void kvm_riscv_cove_vcpu_put(struct kvm_vcpu *vcpu) /* TODO */ } +int kvm_riscv_cove_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run) +{ + void *nshmem; + const struct kvm_vcpu_sbi_extension *sbi_ext; + struct kvm_cpu_context *cp = &vcpu->arch.guest_context; + struct kvm_cpu_trap utrap = { 0 }; + struct kvm_vcpu_sbi_return sbi_ret = { + .out_val = 0, + .err_val = 0, + .utrap = &utrap, + }; + bool ext_is_01 = false; + int ret = 1; + + nshmem = nacl_shmem(); + cp->a0 = nacl_shmem_gpr_read_cove(nshmem, KVM_ARCH_GUEST_A0); + cp->a1 = nacl_shmem_gpr_read_cove(nshmem, KVM_ARCH_GUEST_A1); + cp->a6 = nacl_shmem_gpr_read_cove(nshmem, KVM_ARCH_GUEST_A6); + cp->a7 = nacl_shmem_gpr_read_cove(nshmem, KVM_ARCH_GUEST_A7); + + /* TSM will only forward legacy console to the host */ +#ifdef CONFIG_RISCV_SBI_V01 + if (cp->a7 == SBI_EXT_0_1_CONSOLE_PUTCHAR) + ext_is_01 = true; +#endif + + sbi_ext = kvm_vcpu_sbi_find_ext(vcpu, cp->a7); + if ((sbi_ext && sbi_ext->handler) && ((cp->a7 == SBI_EXT_DBCN) || + (cp->a7 == SBI_EXT_HSM) || (cp->a7 == SBI_EXT_SRST) || ext_is_01)) { + ret = sbi_ext->handler(vcpu, run, &sbi_ret); + } else { + kvm_err("%s: SBI EXT %lx not supported for TVM\n", __func__, cp->a7); + /* Return error for unsupported SBI calls */ + sbi_ret.err_val = SBI_ERR_NOT_SUPPORTED; + goto ecall_done; + } + + if (ret < 0) + goto ecall_done; + + ret = (sbi_ret.uexit) ? 0 : 1; + +ecall_done: + /* + * No need to update the sepc as TSM will take care of SEPC increment + * for ECALLS that won't be forwarded to the user space (e.g. console) + */ + nacl_shmem_gpr_write_cove(nshmem, KVM_ARCH_GUEST_A0, sbi_ret.err_val); + if (!ext_is_01) + nacl_shmem_gpr_write_cove(nshmem, KVM_ARCH_GUEST_A1, sbi_ret.out_val); + + return ret; +} + int kvm_riscv_cove_gstage_map(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned long hva) { struct kvm_riscv_cove_page *tpage; diff --git a/arch/riscv/kvm/vcpu_exit.c b/arch/riscv/kvm/vcpu_exit.c index d00b9ee5..8944e29 100644 --- a/arch/riscv/kvm/vcpu_exit.c +++ b/arch/riscv/kvm/vcpu_exit.c @@ -207,11 +207,15 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, case EXC_INST_GUEST_PAGE_FAULT: case EXC_LOAD_GUEST_PAGE_FAULT: case EXC_STORE_GUEST_PAGE_FAULT: + //TODO: If the host runs in HS mode, this won't work as we don't + //read hstatus from the shared memory yet if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) ret = gstage_page_fault(vcpu, run, trap); break; case EXC_SUPERVISOR_SYSCALL: - if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) + if (is_cove_vcpu(vcpu)) + ret = kvm_riscv_cove_vcpu_sbi_ecall(vcpu, run); + else if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) ret = kvm_riscv_vcpu_sbi_ecall(vcpu, run); break; default: diff --git a/arch/riscv/kvm/vcpu_sbi.c b/arch/riscv/kvm/vcpu_sbi.c index 047ba10..d2f43bc 100644 --- a/arch/riscv/kvm/vcpu_sbi.c +++ b/arch/riscv/kvm/vcpu_sbi.c @@ -10,6 +10,8 @@ #include #include #include +#include +#include #include #ifndef CONFIG_RISCV_SBI_V01 From patchwork Wed Apr 19 22:16:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217604 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 3E7A8C77B7C for ; Wed, 19 Apr 2023 22:37:54 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=0CeMuih/jSl8oion930i5ThrVw6AZTWEs84l+15tdg4=; b=NrmIVVZghpAXdE h61HOPXPBRn7VDlMD5QNY5sEXce0Mxsj3pax/zrbNbyCjdO+nEwhDuc+PQW+u15m6c5AIS6Ps9crQ zmggeFVo3PkjbstIdQrA5ex+tlo/Xk5xQTjSOiVDtkkl98PP46C9xnlvj56BMhCyWHw8n8W+J+scZ Z8UPlKYHS0fLNfpza6Yz8hC7KJV1/IyMynrZAxYxrn0VTd/+t/g+gRt7it3F8t8DCLyQPdNBSgzxj EmpAxVz82jHn3NqaMRqCgJJxxzXl9AzKYL0KzjSe/YIJWlGUFCaZlsWJwY1jGt4+jJc34/PH+Ytv5 /6IyrwbAy6sD5VUvXJtw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQr-006WuR-11; Wed, 19 Apr 2023 22:37:45 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQi-006WXV-2e for linux-riscv@bombadil.infradead.org; Wed, 19 Apr 2023 22:37:36 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=Pex4ariWnXDdKnos4XH43DasA78+EqfUbH0Wh8CR5w4=; b=u9suVPhA7xhAu3RyYw05osEdde JfrgsBnyVat1wBHWYuPdhGKl8poMnYs2JezbG2fWcPhrMq7O7p2M63rEYU/o6xfB14FiQzO+yJsuZ ++LOZjTf6gBq1VwtuKq2jrOLxeiksvhGil8KTHCNj9VDzc80UgidS1ErTNlCRQSuNMJ/Vsex3Ct8f Zaa46dn4elcDtgwB189LDVolJ7eJe6Rj2ysaJQAB+9o3gsLSzqgy/Qn92+dGXYdopom+FW+xurceg HFr9QM4KmLlyiwz3MX49M0BLwe07zstlFN/dIM40lkHeThQNqvmmhDkpYJ3jRCkK26hq4SP2Irz1x 4rb6oeSw==; Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by casper.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ppG82-00De9l-Ji for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:20 +0000 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-1a66b9bd893so4666055ad.1 for ; Wed, 19 Apr 2023 15:18:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942698; x=1684534698; 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=Pex4ariWnXDdKnos4XH43DasA78+EqfUbH0Wh8CR5w4=; b=di7chbqLiC3SvYTY8G+fGI2cGV6/NplXfCh5v5XyNgDlngesP2y27MIvOHwrp1X4yx 4JsJle4v6NILzYjrBdQfclQHMQ1y+XkUvrnKfQUj0Ry5xsz9RSAaP7xk6bNe/vd52w3q C3nd14GGW5Qj94glYyk3NSDPRmLJqvozvF8lGdcOkQJDY2HZ2XGm6lNAbX6/JmUwtYhH cJH7ZmbN28BevOjXzCzDy6LpP/+8QR1qPGxigpkkPQjHRv6qA67df2bzJD5EJMK27xPT x2yJWlQYdkRTzgVvhswp/3TbWfU5x7nor5CBTVJeRFUg+I3fFtPgIMfjV7YwcmwCLtW3 tRuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942698; x=1684534698; 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=Pex4ariWnXDdKnos4XH43DasA78+EqfUbH0Wh8CR5w4=; b=OJA5qzgOOimxEOdTiRaU5zDGn/lCKP08L9pQ+O66DZzXg7n4r7cmZpgXciUQhYOnm3 3wFbu/KAKPhdVWz4vXbkX8lKnf/8vAf54yl0ZaWQA0qpGduMvmCJ3BSyAin80iryONiK Azc1cnbRcsOU8QMD/cDIswgA+3RzWUv2UxXof7P+B0V6duimSjKgZ+ClhOHbSvQ2g7uQ +IzQQSzoAJy5RDOTZx26crlTA5wobzVAyUSmlzcSEsxSymY0gHE49Edgoksj1ZWqJD/7 YdXjHF1F/ggCfaOxf6WVsbWM965HfGlBcJqiektW7BQcOm5UwOny/TBVvgxpMwW55P5m ccjg== X-Gm-Message-State: AAQBX9cNHDU4x3Xp1rjiSD0/oysbe+mGWfr4077z8PsfvfIwMaf5iL9G 6zTkxpKhKQy0xuRiYov5jkcUcA== X-Google-Smtp-Source: AKy350bAGBT2wDySHdJsoaBivRKAhhoN6FGmg4/i0kk3x7CE/96pMKWgvriPwXlMHgJNkHUROr/6AQ== X-Received: by 2002:a17:903:1210:b0:1a6:4a64:4d27 with SMTP id l16-20020a170903121000b001a64a644d27mr7784281plh.40.1681942697893; Wed, 19 Apr 2023 15:18:17 -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.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:17 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Rajnesh Kanwal , Alexandre Ghiti , Andrew Jones , Andrew Morton , Anup Patel , Atish Patra , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , 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 , Uladzislau Rezki Subject: [RFC 22/48] RISC-V: KVM: Implement vcpu load/put functions for CoVE guests Date: Wed, 19 Apr 2023 15:16:50 -0700 Message-Id: <20230419221716.3603068-23-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_231818_674321_26A65EDF X-CRM114-Status: GOOD ( 12.62 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org The TSM takes care of most of the H extension CSR/fp save/restore for any guest running in CoVE. It may choose to do the fp save/restore lazily as well. The host has to do minimal operations such timer save/restore and interrupt state restore during vcpu load/put. Signed-off-by: Rajnesh Kanwal Signed-off-by: Atish Patra --- arch/riscv/kvm/cove.c | 12 ++++++++++-- arch/riscv/kvm/vcpu.c | 12 +++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/arch/riscv/kvm/cove.c b/arch/riscv/kvm/cove.c index 87fa04b..c93de9b 100644 --- a/arch/riscv/kvm/cove.c +++ b/arch/riscv/kvm/cove.c @@ -139,12 +139,20 @@ __always_inline bool kvm_riscv_cove_enabled(void) void kvm_riscv_cove_vcpu_load(struct kvm_vcpu *vcpu) { - /* TODO */ + kvm_riscv_vcpu_timer_restore(vcpu); } void kvm_riscv_cove_vcpu_put(struct kvm_vcpu *vcpu) { - /* TODO */ + void *nshmem; + struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr; + + kvm_riscv_vcpu_timer_save(vcpu); + /* NACL is mandatory for CoVE */ + nshmem = nacl_shmem(); + + /* Only VSIE needs to be read to manage the interrupt stuff */ + csr->vsie = nacl_shmem_csr_read(nshmem, CSR_VSIE); } int kvm_riscv_cove_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run) diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index 8cf462c..3e04b78 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -972,6 +972,11 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) u64 henvcfg = kvm_riscv_vcpu_get_henvcfg(vcpu->arch.isa); struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr; + if (is_cove_vcpu(vcpu)) { + kvm_riscv_cove_vcpu_load(vcpu); + goto skip_load; + } + if (kvm_riscv_nacl_sync_csr_available()) { nshmem = nacl_shmem(); nacl_shmem_csr_write(nshmem, CSR_VSSTATUS, csr->vsstatus); @@ -1010,9 +1015,9 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) kvm_riscv_vcpu_host_fp_save(&vcpu->arch.host_context); kvm_riscv_vcpu_guest_fp_restore(&vcpu->arch.guest_context, vcpu->arch.isa); - kvm_riscv_vcpu_aia_load(vcpu, cpu); +skip_load: vcpu->cpu = cpu; } @@ -1023,6 +1028,11 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) vcpu->cpu = -1; + if (is_cove_vcpu(vcpu)) { + kvm_riscv_cove_vcpu_put(vcpu); + return; + } + kvm_riscv_vcpu_aia_put(vcpu); kvm_riscv_vcpu_guest_fp_save(&vcpu->arch.guest_context, From patchwork Wed Apr 19 22:16:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217556 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 0BC67C6FD18 for ; Wed, 19 Apr 2023 22:23:47 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=LJgtHAulwFn7C0M3Pmpxc1aGCmYOmYezhYWmS0A5lYo=; b=XAUoNDNxjOuEjV YeKDHERUdOeDyZ1dYWhU3CheB8bdKvw+BtCuuNOc2yzL2OvEQRBhQ2rrA51IrIVRHzvgEacA0BSm+ dDgbJbOA5aExeJLsUIeDIo/6qDM0djnqWrgKE2OyMiipl+PslZADwfaoIauo5cN83msvSD7PTMON4 NE+CVU1a8kHix5HPaUNNqcdx9dQRu7pMfVojMViM51a4WM6kIrfzg+PVTERxzASb1nA0clMIzb/PK 35E3+W7/z3LZT1nJgfIqhga6Rt9fSiStqBocE4fBtQRdR6okU4Ob9YpHH2l2QAOp9bj19DCC0zh7b f37Jw6K9jr+n0cgmqigQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGDE-006UUj-02; Wed, 19 Apr 2023 22:23:40 +0000 Received: from mail-il1-x133.google.com ([2607:f8b0:4864:20::133]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGDC-006UT7-0V for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:23:39 +0000 Received: by mail-il1-x133.google.com with SMTP id e9e14a558f8ab-32942f4b670so2114025ab.0 for ; Wed, 19 Apr 2023 15:23:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681943015; x=1684535015; 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=Nt9qr+Ng8JBl/RErKCxn2q3deom0MBnPuCU/gT00P20=; b=ot9ZGgjcXMaxAV0r1VOO+BvehMSySZNIsVcDPBEFwlJAhM2M5m+gwfrB780olzD6hR Hdrq9yXgl+sn4/wrpA4PaTrrtnnFy9Say+EnWPhRztcDse00ZGPmHquLuK3Ybnm7wJWA ynzhzpWOzs18V7RsuuGPFfvie8ep260dMB4F3F4+em6ryITqN7ugXLqLmHk+GAoN7Fmf OQtf58b1FXhM9LfuvN4kb6ax1lLXAD0ES3eChU4pExsoMv+YvW59AxrBX1OEBU8pI/52 BPBRljloH/NRMWTqp6Eb9GcWkOIf5Jl1lSkwFq1AdstC6QGzsUCBs48503R1c8/SLqih fkIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681943015; x=1684535015; 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=Nt9qr+Ng8JBl/RErKCxn2q3deom0MBnPuCU/gT00P20=; b=U2E9vA8a+J14v3eStdCXgh1wzpGwYQzMv7BSteKA4aEQRgK7fb0Cm9e2nllu1IJlLd 3lXzc92VWgy4oepkGAoE9lz7ysh+5EKrJDjd7DSQKm+wiX8r0CJaJY2HWnUCAyycK9k4 9HYMEdEf4CNsg7R7eNHwedIsXgPAw2W5q4wBqRUp+9d96sr2HbOqSB4Ten/hZcOCwPhK gRmorz6/+8zpOtonQy4N0LTWW6k+rivFllTbXThwulmh0xrQfwTDziOmEDTEKjy6LnO5 MmdxevyT/+H74b/YHzQxKm+wCcDiju0zxF/RF7mprmZHIPUTAYvMflFeyFVBBihbPOFf WIBQ== X-Gm-Message-State: AAQBX9flhJqSWXrs88HuBjN4ZJ3m/v20tGzkd4RCReD8/V34UX0D+PBh skupx8vJ6pjU/efh43rbTirVIBnGhh3OQdbKgRY= X-Google-Smtp-Source: AKy350bclXeDaJm5NGd+baLQjNm1etLX+lLDzeTXzfOcBiZB99+UVCqjhPXny+Rg6qaFMwVObWbhvw== X-Received: by 2002:a17:903:110e:b0:1a6:6fe3:df91 with SMTP id n14-20020a170903110e00b001a66fe3df91mr7814057plh.50.1681942700098; Wed, 19 Apr 2023 15:18:20 -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.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:19 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 23/48] RISC-V: KVM: Wireup TVM world switch Date: Wed, 19 Apr 2023 15:16:51 -0700 Message-Id: <20230419221716.3603068-24-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_152338_198348_935D45A6 X-CRM114-Status: GOOD ( 18.82 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org TVM worlds switch takes a different path from regular VM world switch as it needs to make an ecall to TSM and TSM actually does the world switch. The host doesn't need to save/restore any context as TSM is expected to do that on behalf of the host. The TSM updatess the trap information in the shared memory which host uses to figure out the cause of the guest exit. Signed-off-by: Atish Patra --- arch/riscv/kvm/cove.c | 31 +++++++++++++++++++++++++++++-- arch/riscv/kvm/vcpu.c | 11 +++++++++++ arch/riscv/kvm/vcpu_exit.c | 10 ++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/arch/riscv/kvm/cove.c b/arch/riscv/kvm/cove.c index c93de9b..c11db7a 100644 --- a/arch/riscv/kvm/cove.c +++ b/arch/riscv/kvm/cove.c @@ -275,9 +275,36 @@ int kvm_riscv_cove_gstage_map(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned long hv return rc; } -void kvm_riscv_cove_vcpu_switchto(struct kvm_vcpu *vcpu, struct kvm_cpu_trap *trap) +void noinstr kvm_riscv_cove_vcpu_switchto(struct kvm_vcpu *vcpu, struct kvm_cpu_trap *trap) { - /* TODO */ + int rc; + struct kvm *kvm = vcpu->kvm; + struct kvm_cove_tvm_context *tvmc; + struct kvm_cpu_context *cntx = &vcpu->arch.guest_context; + void *nshmem; + + if (!kvm->arch.tvmc) + return; + + tvmc = kvm->arch.tvmc; + + nshmem = nacl_shmem(); + /* Invoke finalize to mark TVM is ready run for the first time */ + if (unlikely(!tvmc->finalized_done)) { + + rc = sbi_covh_tsm_finalize_tvm(tvmc->tvm_guest_id, cntx->sepc, cntx->a1); + if (rc) { + kvm_err("TVM Finalized failed with %d\n", rc); + return; + } + tvmc->finalized_done = true; + } + + rc = sbi_covh_run_tvm_vcpu(tvmc->tvm_guest_id, vcpu->vcpu_idx); + if (rc) { + trap->scause = EXC_CUSTOM_KVM_COVE_RUN_FAIL; + return; + } } void kvm_riscv_cove_vcpu_destroy(struct kvm_vcpu *vcpu) diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index 3e04b78..43a0b8c 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -1042,6 +1042,11 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) kvm_riscv_vcpu_timer_save(vcpu); if (kvm_riscv_nacl_available()) { + /** + * For TVMs, we don't need a separate case as TSM only updates + * the required CSRs during the world switch. All other CSR + * value should be zeroed out by TSM anyways. + */ nshmem = nacl_shmem(); csr->vsstatus = nacl_shmem_csr_read(nshmem, CSR_VSSTATUS); csr->vsie = nacl_shmem_csr_read(nshmem, CSR_VSIE); @@ -1191,6 +1196,12 @@ static void noinstr kvm_riscv_vcpu_enter_exit(struct kvm_vcpu *vcpu, gcntx->hstatus = csr_swap(CSR_HSTATUS, hcntx->hstatus); } + trap->htval = nacl_shmem_csr_read(nshmem, CSR_HTVAL); + trap->htinst = nacl_shmem_csr_read(nshmem, CSR_HTINST); + } else if (is_cove_vcpu(vcpu)) { + nshmem = nacl_shmem(); + kvm_riscv_cove_vcpu_switchto(vcpu, trap); + trap->htval = nacl_shmem_csr_read(nshmem, CSR_HTVAL); trap->htinst = nacl_shmem_csr_read(nshmem, CSR_HTINST); } else { diff --git a/arch/riscv/kvm/vcpu_exit.c b/arch/riscv/kvm/vcpu_exit.c index 8944e29..c46e7f2 100644 --- a/arch/riscv/kvm/vcpu_exit.c +++ b/arch/riscv/kvm/vcpu_exit.c @@ -218,6 +218,15 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, else if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) ret = kvm_riscv_vcpu_sbi_ecall(vcpu, run); break; + case EXC_CUSTOM_KVM_COVE_RUN_FAIL: + if (likely(is_cove_vcpu(vcpu))) { + ret = -EACCES; + run->fail_entry.hardware_entry_failure_reason = + KVM_EXIT_FAIL_ENTRY_COVE_RUN_VCPU; + run->fail_entry.cpu = vcpu->cpu; + run->exit_reason = KVM_EXIT_FAIL_ENTRY; + } + break; default: break; } @@ -225,6 +234,7 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, /* Print details in-case of error */ if (ret < 0) { kvm_err("VCPU exit error %d\n", ret); + //TODO: These values are bogus/stale for a TVM. Improve it kvm_err("SEPC=0x%lx SSTATUS=0x%lx HSTATUS=0x%lx\n", vcpu->arch.guest_context.sepc, vcpu->arch.guest_context.sstatus, From patchwork Wed Apr 19 22:16:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217511 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 52143C77B7F for ; Wed, 19 Apr 2023 22:18:36 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=r6dlfil32VVoYTnbRXip6TNGVQ7DV3Ou9DRWySFkuqI=; b=eQY25eu9pT8++5 rvdC63OkNFvypi5Bo5zXuytDp8Uv1p1DBwC1vXbGtDd0GgxBYiF0ryKkfayDwAMYLYhWkAEkXsIp2 ZFstEjPFecxh30CjKIswSb3Bq4t6WowIqRzutL/0TEg0in4NjKjAaystK0m10dAQdYoq3m6kmvxXQ iObx39Ti/yz387L14KgF5bErKSNu79UsXBTHWyNzDQU17J0PR8xD3f1ucqTzYFi08X+WsLvElbDGk TtzhH3Xj6h1t1NlUGk1mcwQhs/BHepD9EJgxVwOjibaFXjscPcGUUGvnkqYDVwAqhG3dyi1HioR0x ZPzBVio3pWcdItaiCeDw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppG8D-006TFj-23; Wed, 19 Apr 2023 22:18:29 +0000 Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG87-006TAN-16 for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:28 +0000 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-63b5c48ea09so373981b3a.1 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=1681942703; x=1684534703; 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=vFCr/CCby/ixIZR1vcNNYtJI+Hn/JpPKkTFrcmf/240=; b=o8cl3vLFW0T/UgdJpNmsgqy/trLWjUKDtYhD18vtn6zFmKJnhCVb3UEfS7g11Yxo6v 70pgYAJ/oiEoiEaIzaXTTARUDuEe0wemxcSCjeDS0me1n4QkzpJpeXfeqrgbXd5cjW+8 mPLL2Zixb1wKKpwdABmEKJb6AdfFo9aF/CARiWba1a9ZO3iyrr6WtwnggyEbtMKiFH3H d5ALpqRY6NcLK/kvZ5gIdFQLtEIiUaW/W6h18t9SJVK+nkmzEtEqwMweSnvDyUAtFakX ItHgUeBEX6SFRd5as2TzksX5YG1nm8yDIWTd3Rsk2EmExawcoSXVI008NBflAu//NIVj OPxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942703; x=1684534703; 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=vFCr/CCby/ixIZR1vcNNYtJI+Hn/JpPKkTFrcmf/240=; b=PLitYa/Y5kyJWEgUcIDj4TfKEsoOz/NaXvEEbBsAYZ0caTXPYtgMgqoKM0V34yaIWn Q/sF08/sdg4b6NszKc3xaCDSQNJ8e+dtFvn26EnFPgHkQCViGRutlH7s6u45nM7tG/Jt FKiEiE13rj5YM2kQ+qM1HzRMUavU9eZm6/4HQBQC8bUMNGlGtusUdUHFzOspArQzJx4+ QzOdMo2OvL1o7f3RJZgA+mpxlhG6CdxfuTNhct1ht5GPpQZrqv4GZSzEwuP5SsyxMfBT eaNi+iuWYxCMBs/udMFtLyWqZubcQY3o0y3JatYyq9OIvj1CWVDv95apUrvuV6RoKHHd O+fw== X-Gm-Message-State: AAQBX9f4Nn5pMEbuzNiZ18vQKJuCq1LMgBJdHnQ7T77YhpcQBNku9Pkl 4jlJm2AaFx7AH+WwZgQpndBwEA== X-Google-Smtp-Source: AKy350YqF/4+akV7tQXSVqBJeeYYqMrxklqGFMYEM4KYqKrP5PB2sjQSY1CZVqN7qH3odtWcRYYeog== X-Received: by 2002:a17:903:290:b0:19d:b02:cca5 with SMTP id j16-20020a170903029000b0019d0b02cca5mr6589575plr.12.1681942702695; Wed, 19 Apr 2023 15:18:22 -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.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:22 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Rajnesh Kanwal , Atish Patra , Alexandre Ghiti , Andrew Jones , Andrew Morton , Anup Patel , Atish Patra , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , 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 , Uladzislau Rezki Subject: [RFC 24/48] RISC-V: KVM: Update timer functionality for TVMs. Date: Wed, 19 Apr 2023 15:16:52 -0700 Message-Id: <20230419221716.3603068-25-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151823_393149_7675ABD2 X-CRM114-Status: GOOD ( 21.75 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Rajnesh Kanwal TSM manages the htimedelta/vstimecmp for the TVM and shares it with the host to properly schedule hrtimer to keep timer interrupt ticking. TSM only sets htimedetla when first VCPU is run to make sure host is not able to control the start time of the VM. TSM updates vstimemcp at every vmexit and ignores any write to vstimecmp from the host. Signed-off-by: Rajnesh Kanwal Signed-off-by: Atish Patra --- arch/riscv/kvm/cove.c | 8 ++++++++ arch/riscv/kvm/vcpu_timer.c | 26 +++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/arch/riscv/kvm/cove.c b/arch/riscv/kvm/cove.c index c11db7a..4a8a8db 100644 --- a/arch/riscv/kvm/cove.c +++ b/arch/riscv/kvm/cove.c @@ -282,6 +282,7 @@ void noinstr kvm_riscv_cove_vcpu_switchto(struct kvm_vcpu *vcpu, struct kvm_cpu_ struct kvm_cove_tvm_context *tvmc; struct kvm_cpu_context *cntx = &vcpu->arch.guest_context; void *nshmem; + struct kvm_guest_timer *gt = &kvm->arch.timer; if (!kvm->arch.tvmc) return; @@ -305,6 +306,13 @@ void noinstr kvm_riscv_cove_vcpu_switchto(struct kvm_vcpu *vcpu, struct kvm_cpu_ trap->scause = EXC_CUSTOM_KVM_COVE_RUN_FAIL; return; } + + /* Read htimedelta from shmem. Given it's written by TSM only when we + * run first VCPU, we need to update this here rather than in timer + * init. + */ + if (unlikely(!gt->time_delta)) + gt->time_delta = nacl_shmem_csr_read(nshmem, CSR_HTIMEDELTA); } void kvm_riscv_cove_vcpu_destroy(struct kvm_vcpu *vcpu) diff --git a/arch/riscv/kvm/vcpu_timer.c b/arch/riscv/kvm/vcpu_timer.c index 71a4560..f059e14 100644 --- a/arch/riscv/kvm/vcpu_timer.c +++ b/arch/riscv/kvm/vcpu_timer.c @@ -14,6 +14,7 @@ #include #include #include +#include static u64 kvm_riscv_current_cycles(struct kvm_guest_timer *gt) { @@ -71,6 +72,10 @@ static int kvm_riscv_vcpu_timer_cancel(struct kvm_vcpu_timer *t) static int kvm_riscv_vcpu_update_vstimecmp(struct kvm_vcpu *vcpu, u64 ncycles) { + /* Host is not allowed to update the vstimecmp for the TVM */ + if (is_cove_vcpu(vcpu)) + return 0; + #if defined(CONFIG_32BIT) nacl_csr_write(CSR_VSTIMECMP, ncycles & 0xFFFFFFFF); nacl_csr_write(CSR_VSTIMECMPH, ncycles >> 32); @@ -221,6 +226,11 @@ int kvm_riscv_vcpu_set_reg_timer(struct kvm_vcpu *vcpu, ret = -EOPNOTSUPP; break; case KVM_REG_RISCV_TIMER_REG(time): + /* For trusted VMs we can not update htimedelta. We can just + * read it from shared memory. + */ + if (is_cove_vcpu(vcpu)) + return -EOPNOTSUPP; gt->time_delta = reg_val - get_cycles64(); break; case KVM_REG_RISCV_TIMER_REG(compare): @@ -287,6 +297,7 @@ static void kvm_riscv_vcpu_update_timedelta(struct kvm_vcpu *vcpu) { struct kvm_guest_timer *gt = &vcpu->kvm->arch.timer; + #if defined(CONFIG_32BIT) nacl_csr_write(CSR_HTIMEDELTA, (u32)(gt->time_delta)); nacl_csr_write(CSR_HTIMEDELTAH, (u32)(gt->time_delta >> 32)); @@ -299,6 +310,10 @@ void kvm_riscv_vcpu_timer_restore(struct kvm_vcpu *vcpu) { struct kvm_vcpu_timer *t = &vcpu->arch.timer; + /* While in CoVE, HOST must not manage HTIMEDELTA or VSTIMECMP for TVM */ + if (is_cove_vcpu(vcpu)) + goto skip_hcsr_update; + kvm_riscv_vcpu_update_timedelta(vcpu); if (!t->sstc_enabled) @@ -311,6 +326,7 @@ void kvm_riscv_vcpu_timer_restore(struct kvm_vcpu *vcpu) nacl_csr_write(CSR_VSTIMECMP, t->next_cycles); #endif +skip_hcsr_update: /* timer should be enabled for the remaining operations */ if (unlikely(!t->init_done)) return; @@ -358,5 +374,13 @@ void kvm_riscv_guest_timer_init(struct kvm *kvm) struct kvm_guest_timer *gt = &kvm->arch.timer; riscv_cs_get_mult_shift(>->nsec_mult, >->nsec_shift); - gt->time_delta = -get_cycles64(); + if (is_cove_vm(kvm)) { + /* For TVMs htimedelta is managed by TSM and it's communicated using + * NACL shmem interface when first time VCPU is run. so we read it in + * kvm_riscv_cove_vcpu_switchto() where we enter VCPUs. + */ + gt->time_delta = 0; + } else { + gt->time_delta = -get_cycles64(); + } } From patchwork Wed Apr 19 22:16:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217513 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 9E208C77B7E for ; Wed, 19 Apr 2023 22:18:39 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=OAICPAbWJIaCediYLz9gNl/5qWFEyzDYawF/SPkycBg=; b=tQHoLOJUUXET1F Y6kuGLpcK5PKcl38GqZa2hMiZWfGZG+rbAUQKTGegX9MU1ivZ7R7Lkrzan0z3miY1MKfmhWO658bS pDpi4l5nbsyahqrF+059MiNRQxzeI3yVCPfDQgtec6j6nNj+rY6zm1wUp3s61ItyiAzA98om+RVaG HwIUWtHlYDhygag+uakXkN4mxlj0ekSb0VBXcH+l4FDbAhpHmZ/ZyNxkIA5j6tot607a8S1IFNJAW W9i9dSXYKn3ByihxYeIoIJt2WJAH0j3tmHeKCaZe4umKPsftSo7xNoKi0lUHFwIN9JkTe98M1mJpP /EEWxUY4PA+pAZS6yNRQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppG8H-006TJO-1c; Wed, 19 Apr 2023 22:18:33 +0000 Received: from mail-pj1-x1032.google.com ([2607:f8b0:4864:20::1032]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG8B-006TCR-0t for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:31 +0000 Received: by mail-pj1-x1032.google.com with SMTP id 98e67ed59e1d1-2466f65d7e0so177972a91.2 for ; Wed, 19 Apr 2023 15:18:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942705; x=1684534705; 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=UJcz91lEoSJqtalpSfBSHZNF30nj7YibjBRer+eosEg=; b=qMbw2bfkgktxDwIJo8enIEL5pLtnbolY3+JaaISTkIKsa3/Pbsco4zcJlI15aLKelR WFKrKF2Hz7v5krpEvYpYwDTqJPbGu3BuJcQtCe9xizDdAggE5jW1G4X8qqMenNOBqXz9 5gqd6GiYE1ZkgW2qNIjSboTRdUgFAKPx3ibrEbYtA8McCRZRHbdZdMvH67xn6xctz3io Yx4YAKRBSdhdHTVV7HAXUHddiREJjeIsjlNB/MFPQtt0zqpmQorHvJ8xpPjM9lkMYa3A PJWfAzX50ZJIGmvwtxGZ3PpJ/kZeMQBLQ0BYf7N4KMtYq6ZlC++uPwgnbCGRBd8idM/u /cyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942705; x=1684534705; 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=UJcz91lEoSJqtalpSfBSHZNF30nj7YibjBRer+eosEg=; b=Sq4ZYLcllf4b8jQxUdiQVKxZKjXjjK7ceFdYxOaY9/fsnZnPlrazsYAXzwUWeZnniB UFXxTFV4qjk9olnbQgvVTpEgmXqei+osekKGmLm4MpGNOURKHRrnj2nKJb/i/87yzhHd Yif79bQ3eQzTekV1Pmode+SJKenHoJ9LNZ33Hwi62Ul5NJc3L7LWek1n4wMaXE4zU3AA 1C2T87L7XBv4xGIeAgi6stfkT45HeP0scuHYtdkeUojdiMl1R0BHdSKbRwqPpzRhHnXO CZdSRla+eLoZep6rtG9VuRUebGA0Azd/fEhBXVBZyuZ9ShxXhB88aHCY8uWQAXE8fRZT Psdg== X-Gm-Message-State: AAQBX9cb86gGJh+aA8Sq7m/Ay8t3h+/AxqYpAn9ZQD7dtNcXQCzFom3D XQZAnkD3H/i1cDXUtYCo7bgSVg== X-Google-Smtp-Source: AKy350Z8cKy3RK/3X5ej3IZFoGxmT2xiFm/BExphbF3ahTLRykDnPstTsJrfduCst6vY0llDQCAqqw== X-Received: by 2002:a17:90b:890:b0:23a:333c:6bab with SMTP id bj16-20020a17090b089000b0023a333c6babmr4018734pjb.23.1681942704931; Wed, 19 Apr 2023 15:18:24 -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.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:24 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 25/48] RISC-V: KVM: Skip HVIP update for TVMs Date: Wed, 19 Apr 2023 15:16:53 -0700 Message-Id: <20230419221716.3603068-26-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151827_320183_24EA0BAF X-CRM114-Status: GOOD ( 13.32 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Skip HVIP update as the Host shouldn't be able to inject interrupt directly to a TVM. Signed-off-by: Atish Patra --- arch/riscv/kvm/vcpu.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index 43a0b8c..20d4800 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -822,7 +822,10 @@ void kvm_riscv_vcpu_sync_interrupts(struct kvm_vcpu *vcpu) /* Read current HVIP and VSIE CSRs */ csr->vsie = nacl_csr_read(CSR_VSIE); - /* Sync-up HVIP.VSSIP bit changes does by Guest */ + /* + * Sync-up HVIP.VSSIP bit changes does by Guest. For TVMs, + * the HVIP is not updated by the TSM. Expect it to be zero. + */ hvip = nacl_csr_read(CSR_HVIP); if ((csr->hvip ^ hvip) & (1UL << IRQ_VS_SOFT)) { if (hvip & (1UL << IRQ_VS_SOFT)) { @@ -1305,8 +1308,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) */ kvm_riscv_vcpu_flush_interrupts(vcpu); - /* Update HVIP CSR for current CPU */ - kvm_riscv_update_hvip(vcpu); + /* Update HVIP CSR for current CPU only for non TVMs */ + if (!is_cove_vcpu(vcpu)) + kvm_riscv_update_hvip(vcpu); if (ret <= 0 || kvm_riscv_gstage_vmid_ver_changed(vcpu->kvm) || From patchwork Wed Apr 19 22:16:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217512 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 34F3AC77B73 for ; Wed, 19 Apr 2023 22:18: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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=XVoBp1b2d4zb1rP/iVL4MBdSmEZROZZ1oRjWMNJs/gg=; b=Qowne3Rnbr82ec 9wGYALjbOvdd4RnURxGC0mdcl5W1StP2YQLIYDDaXVXMSH8qHkjbkmfDH7t9D3gcU8m1MVuWS//KN aDTU/dDBXMo+n+sWX9hQDbDmT+YtyTcn6qYROPfIYP/vcKt3YStFLSwDZuFaY0Hzmcj0SS4MhbxUI xPwwfF3M9WHIoM1+DSCu08SKGm4U8tKQCCkpgVgoL+n+77mYy32TWkMfASzjSFjrqRDDgaL5fgnfO Wugj5Yf/psOQm5dWyZI3PWKdBoLH/aobgbD/JaXrfEDn3SSyO6FoBbBgYZog/UBeBIxyUQXqWT8IL UrVGA2OU5LaXYRSaTx8Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppG8F-006THq-1o; Wed, 19 Apr 2023 22:18:31 +0000 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG8B-006T2c-1T for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:28 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1a686260adcso5385745ad.0 for ; Wed, 19 Apr 2023 15:18:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942707; x=1684534707; 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=WztMTyUU5RnTeRMzQjXOG7tTAHmGJecGpWKfqq8b0LY=; b=JM/4VdTUHWchd0agtBcZMVxlzWz+xlNHvW3+YRV1BgFp8SzvOalMG8gKSVRQwN23lF /HaZUXiHN5WGp4Li2ns1uHUPekHEfh3C+xw1tqVd+IJGR2EtpE3yOA/YQtJ1ZYeUVJ/l 7gjUeS7TILHwQHaL9l5Fnqsci5dyPD6ZQhESjL0jYTR+yEk6eIszpIH5SlasnHwxJ7Ky wlJDpXRoqS0jyG2/jEwfH6PNHHxv4wzD8drzVC/Q8h9ghskwb1QRJA40nqZUrBtk1qK0 ooViUN4xyDeEoBWE1iz1aXlci22pk9Oc7YzeXphdmUjkGiDah9tsQrhCdZR8NGP6os1O 4yYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942707; x=1684534707; 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=WztMTyUU5RnTeRMzQjXOG7tTAHmGJecGpWKfqq8b0LY=; b=lu0kzxhD+/y7ykLSdjddFaLRaIg4x4t2frn+vbor/2JYZwJ7lxJsmoUlvtEmSF0D2C BAz1zYLoZq1lRn7mAQC+WuhCjuTVlZQqjq7ETqy5cj46kq0hjijTP+0f4KVwO2vS9h69 dQVvAEULZ32Edg7C/+V5v+5dd/U7XCAox5S2Q9JW1hqzoSJEEB3lmA+f7TOo9/LA+z+u 9S9ObIghifEQDVKvALGxES/KoJyOWs/nYDzxspoYnbC0LgmBr60e5OEddloOTyCCnpGC lmSS1dSMzSXvOU/b8Z0nXkQiANG+s6pCb4p8LKSQWQ/YaDzNPj4k0v62HgWFcDkStdiO CU/w== X-Gm-Message-State: AAQBX9d+VuchfL2nZWXIPjcwGinple28a8Jm+YK2iYpZK5PHFrExchXL pm2kYLDr83RHB8pVS6geIHBGpg== X-Google-Smtp-Source: AKy350am1y3CAd0kzsPnqccpnVuquygqNjOFE27i75PzlAcwaRULYItnkTY6+2qQuafZtBMoWXv5YA== X-Received: by 2002:a17:902:b087:b0:1a6:9363:1632 with SMTP id p7-20020a170902b08700b001a693631632mr6436484plr.25.1681942707075; Wed, 19 Apr 2023 15:18:27 -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.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:26 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Rajnesh Kanwal , Atish Patra , Alexandre Ghiti , Andrew Jones , Andrew Morton , Anup Patel , Atish Patra , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , 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 , Uladzislau Rezki Subject: [RFC 26/48] RISC-V: Add COVI extension definitions Date: Wed, 19 Apr 2023 15:16:54 -0700 Message-Id: <20230419221716.3603068-27-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151827_514530_03C4802B X-CRM114-Status: GOOD ( 13.26 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Rajnesh Kanwal This patch adds the CoVE interrupt management extension(COVI) details to the sbi header file. Signed-off-by: Atish Patra Signed-off-by: Rajnesh Kanwal --- arch/riscv/include/asm/sbi.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/arch/riscv/include/asm/sbi.h b/arch/riscv/include/asm/sbi.h index c5a5526..bbea922 100644 --- a/arch/riscv/include/asm/sbi.h +++ b/arch/riscv/include/asm/sbi.h @@ -33,6 +33,7 @@ enum sbi_ext_id { SBI_EXT_DBCN = 0x4442434E, SBI_EXT_NACL = 0x4E41434C, SBI_EXT_COVH = 0x434F5648, + SBI_EXT_COVI = 0x434F5649, /* Experimentals extensions must lie within this range */ SBI_EXT_EXPERIMENTAL_START = 0x08000000, @@ -369,6 +370,20 @@ enum sbi_ext_covh_fid { SBI_EXT_COVH_TVM_INITIATE_FENCE, }; +enum sbi_ext_covi_fid { + SBI_EXT_COVI_TVM_AIA_INIT, + SBI_EXT_COVI_TVM_CPU_SET_IMSIC_ADDR, + SBI_EXT_COVI_TVM_CONVERT_IMSIC, + SBI_EXT_COVI_TVM_RECLAIM_IMSIC, + SBI_EXT_COVI_TVM_CPU_BIND_IMSIC, + SBI_EXT_COVI_TVM_CPU_UNBIND_IMSIC_BEGIN, + SBI_EXT_COVI_TVM_CPU_UNBIND_IMSIC_END, + SBI_EXT_COVI_TVM_CPU_INJECT_EXT_INTERRUPT, + SBI_EXT_COVI_TVM_REBIND_IMSIC_BEGIN, + SBI_EXT_COVI_TVM_REBIND_IMSIC_CLONE, + SBI_EXT_COVI_TVM_REBIND_IMSIC_END, +}; + enum sbi_cove_page_type { SBI_COVE_PAGE_4K, SBI_COVE_PAGE_2MB, @@ -409,6 +424,21 @@ struct sbi_cove_tvm_create_params { unsigned long tvm_state_addr; }; +struct sbi_cove_tvm_aia_params { + /* The base address is the address of the IMSIC with group ID, hart ID, and guest ID of 0 */ + uint64_t imsic_base_addr; + /* The number of group index bits in an IMSIC address */ + uint32_t group_index_bits; + /* The location of the group index in an IMSIC address. Must be >= 24i. */ + uint32_t group_index_shift; + /* The number of hart index bits in an IMSIC address */ + uint32_t hart_index_bits; + /* The number of guest index bits in an IMSIC address. Must be >= log2(guests/hart + 1) */ + uint32_t guest_index_bits; + /* The number of guest interrupt files to be implemented per vCPU */ + uint32_t guests_per_hart; +}; + #define SBI_SPEC_VERSION_DEFAULT 0x1 #define SBI_SPEC_VERSION_MAJOR_SHIFT 24 #define SBI_SPEC_VERSION_MAJOR_MASK 0x7f From patchwork Wed Apr 19 22:16:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217677 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 10A65C77B73 for ; Wed, 19 Apr 2023 23:31:01 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=4t6s5+2nLulK3myzQiOYwYeLvgDD5kxi+t+Zn4oCKzE=; b=LlPXVbseTOEAcz xRmzELiUt6BIuLTobgSULAIi6c27G4v8OfsNDJC/Pzao7gKgIcVC/xYkvedqTakJQ2P2G3jufnFCO DUaDGJ4JSM9FAzpdBIh7ZkotNFJH7IPOTu0wx0OZzh2nH+ZSi5UrIGbj2c4qldax6Z+nmg3KusfA0 j8rlcfnoVC0EAnVGWnQQ4ybemSUy/TVEgsbtPKbQRYqjv+gcefrw+zqKi6e/mIR2JzyIgXan0MO33 +3lQp/5g8UsgFEJfl5Re3qdsvHxQ8zBdR9Qwrw3KJq2XYQ0H/PZmR4A0MftB94eEJ+TLlUJKbjg7L 0DsVo7r6zCyFuGijmPAw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppHG8-006eXP-1Q; Wed, 19 Apr 2023 23:30:44 +0000 Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG8D-006TAN-1p for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:32 +0000 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-63b5c48ea09so374046b3a.1 for ; Wed, 19 Apr 2023 15:18:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942709; x=1684534709; 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=e0F6tHm2quQwLEvcCl5FI5ypn8ePvTg03kiPuCNoeNw=; b=B0z1LKxRqo0N/+B1Zq8uNd+jORsnBmdASeazIE6ba9jOD7NNAm6Y6Ydluna+vSisdS Ns9+QkyzZkHLKpqekwlR8LOXx0SmdbLDH70zYIU2tu7Ppm7uTfxUiNTzbORQk6jDkpwT wou9JJMc8eyykdjw9eDblXGrXRv3fHzY2QDwRPmwO1RqTVCF9ZjGycORqBMV7BBhXY2c 4MNxDLzeWD+U/vLAXIW8MKmvELAjO/f9B7KULqHL5O/X2CES/rwKUq+7tYosSOcZcHDe RrEGZ8I2t0oPTTiBYWoZFiRl6ej+YFgKxG/VdR1RjRUQssQeFPlAP7g8lmfAqRhNq39Y qB8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942709; x=1684534709; 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=e0F6tHm2quQwLEvcCl5FI5ypn8ePvTg03kiPuCNoeNw=; b=DF85g2WvwJHTSQPBSbo9XayzHmKgewIdzlIXWUvpTMBcYgVDjDhXLTZ7ylpUZH8dyH GeKHBc9KZn29SCl7CfuLWG+3zzVRASIjN0odP5uYfujNyUC6GVTWmHJLsP7muTYlRfxO hVWQ8zQmR4veGMqQiJRbfNJMDzcw5q3hb2l8dFPb0OJoDN3HZalPE89R6giWRnziiIc1 3Am4Yf+fngR1iwhYn6Es2azbJhE5QXIsvRzZBWyIiklWD4mQ8BrjqmZSyo6BERJPHgaN P9h3hIZ0eDT+v4n/yOQ+3ssj4Lt4mEv8UdD8VTDVwJWRcRF4yeqSqsOsZXMdGp6pTZmz 7M2g== X-Gm-Message-State: AAQBX9e80pH/7GyIR4H8ekRBHH8P8BaHOXGuzE3MrhYHcGh80/Jg0EQm lMMfWKVsReqlrmXlt9eyayrvVg== X-Google-Smtp-Source: AKy350aSSLZmcJ6EGd9ViVcCgExeh8NfbY776YNFamx7qI4QWXSDqSo7VY1vzDz6CmaJhnexr4tP0w== X-Received: by 2002:a17:902:be02:b0:1a3:c8c2:c322 with SMTP id r2-20020a170902be0200b001a3c8c2c322mr6340185pls.29.1681942709189; Wed, 19 Apr 2023 15:18:29 -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.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:28 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Rajnesh Kanwal , Alexandre Ghiti , Andrew Jones , Andrew Morton , Anup Patel , Atish Patra , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , 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 , Uladzislau Rezki Subject: [RFC 27/48] RISC-V: KVM: Implement COVI SBI extension Date: Wed, 19 Apr 2023 15:16:55 -0700 Message-Id: <20230419221716.3603068-28-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151829_640385_3A5EDC7C X-CRM114-Status: GOOD ( 14.71 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org CoVE specification defines a separate SBI extension to manage interrupts in TVM. This extension is known as COVI as both host & guest interface access these functions. This patch implements the functions defined by COVI. Co-developed-by: Rajnesh Kanwal Signed-off-by: Rajnesh Kanwal Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_cove_sbi.h | 20 ++++ arch/riscv/kvm/cove_sbi.c | 164 ++++++++++++++++++++++++++ 2 files changed, 184 insertions(+) diff --git a/arch/riscv/include/asm/kvm_cove_sbi.h b/arch/riscv/include/asm/kvm_cove_sbi.h index df7d88c..0759f70 100644 --- a/arch/riscv/include/asm/kvm_cove_sbi.h +++ b/arch/riscv/include/asm/kvm_cove_sbi.h @@ -32,6 +32,7 @@ #define nacl_shmem_gpr_read_cove(__s, __g) \ nacl_shmem_scratch_read_long(__s, get_scratch_gpr_offset(__g)) +/* Functions related to CoVE Host Interface (COVH) Extension */ int sbi_covh_tsm_get_info(struct sbi_cove_tsm_info *tinfo_addr); int sbi_covh_tvm_initiate_fence(unsigned long tvmid); int sbi_covh_tsm_initiate_fence(void); @@ -58,4 +59,23 @@ int sbi_covh_create_tvm_vcpu(unsigned long tvmid, unsigned long tvm_vcpuid, int sbi_covh_run_tvm_vcpu(unsigned long tvmid, unsigned long tvm_vcpuid); +/* Functions related to CoVE Interrupt Management(COVI) Extension */ +int sbi_covi_tvm_aia_init(unsigned long tvm_gid, struct sbi_cove_tvm_aia_params *tvm_aia_params); +int sbi_covi_set_vcpu_imsic_addr(unsigned long tvm_gid, unsigned long vcpu_id, + unsigned long imsic_addr); +int sbi_covi_convert_imsic(unsigned long imsic_addr); +int sbi_covi_reclaim_imsic(unsigned long imsic_addr); +int sbi_covi_bind_vcpu_imsic(unsigned long tvm_gid, unsigned long vcpu_id, + unsigned long imsic_mask); +int sbi_covi_unbind_vcpu_imsic_begin(unsigned long tvm_gid, unsigned long vcpu_id); +int sbi_covi_unbind_vcpu_imsic_end(unsigned long tvm_gid, unsigned long vcpu_id); +int sbi_covi_inject_external_interrupt(unsigned long tvm_gid, unsigned long vcpu_id, + unsigned long interrupt_id); +int sbi_covi_rebind_vcpu_imsic_begin(unsigned long tvm_gid, unsigned long vcpu_id, + unsigned long imsic_mask); +int sbi_covi_rebind_vcpu_imsic_clone(unsigned long tvm_gid, unsigned long vcpu_id); +int sbi_covi_rebind_vcpu_imsic_end(unsigned long tvm_gid, unsigned long vcpu_id); + + + #endif diff --git a/arch/riscv/kvm/cove_sbi.c b/arch/riscv/kvm/cove_sbi.c index bf037f6..a8901ac 100644 --- a/arch/riscv/kvm/cove_sbi.c +++ b/arch/riscv/kvm/cove_sbi.c @@ -18,6 +18,170 @@ #define RISCV_COVE_ALIGN_4KB (1UL << 12) +int sbi_covi_tvm_aia_init(unsigned long tvm_gid, + struct sbi_cove_tvm_aia_params *tvm_aia_params) +{ + struct sbiret ret; + + unsigned long pa = __pa(tvm_aia_params); + + ret = sbi_ecall(SBI_EXT_COVI, SBI_EXT_COVI_TVM_AIA_INIT, tvm_gid, pa, + sizeof(*tvm_aia_params), 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covi_set_vcpu_imsic_addr(unsigned long tvm_gid, unsigned long vcpu_id, + unsigned long imsic_addr) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVI, SBI_EXT_COVI_TVM_CPU_SET_IMSIC_ADDR, + tvm_gid, vcpu_id, imsic_addr, 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +/* + * Converts the guest interrupt file at `imsic_addr` for use with a TVM. + * The guest interrupt file must not be used by the caller until reclaim. + */ +int sbi_covi_convert_imsic(unsigned long imsic_addr) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVI, SBI_EXT_COVI_TVM_CONVERT_IMSIC, + imsic_addr, 0, 0, 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covi_reclaim_imsic(unsigned long imsic_addr) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVI, SBI_EXT_COVI_TVM_RECLAIM_IMSIC, + imsic_addr, 0, 0, 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +/* + * Binds a vCPU to this physical CPU and the specified set of confidential guest + * interrupt files. + */ +int sbi_covi_bind_vcpu_imsic(unsigned long tvm_gid, unsigned long vcpu_id, + unsigned long imsic_mask) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVI, SBI_EXT_COVI_TVM_CPU_BIND_IMSIC, tvm_gid, + vcpu_id, imsic_mask, 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +/* + * Begins the unbind process for the specified vCPU from this physical CPU and its guest + * interrupt files. The host must complete a TLB invalidation sequence for the TVM before + * completing the unbind with `unbind_vcpu_imsic_end()`. + */ +int sbi_covi_unbind_vcpu_imsic_begin(unsigned long tvm_gid, + unsigned long vcpu_id) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVI, SBI_EXT_COVI_TVM_CPU_UNBIND_IMSIC_BEGIN, + tvm_gid, vcpu_id, 0, 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +/* + * Completes the unbind process for the specified vCPU from this physical CPU and its guest + * interrupt files. + */ +int sbi_covi_unbind_vcpu_imsic_end(unsigned long tvm_gid, unsigned long vcpu_id) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVI, SBI_EXT_COVI_TVM_CPU_UNBIND_IMSIC_END, + tvm_gid, vcpu_id, 0, 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +/* + * Injects an external interrupt into the specified vCPU. The interrupt ID must + * have been allowed with `allow_external_interrupt()` by the guest. + */ +int sbi_covi_inject_external_interrupt(unsigned long tvm_gid, + unsigned long vcpu_id, + unsigned long interrupt_id) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVI, SBI_EXT_COVI_TVM_CPU_INJECT_EXT_INTERRUPT, + tvm_gid, vcpu_id, interrupt_id, 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covi_rebind_vcpu_imsic_begin(unsigned long tvm_gid, + unsigned long vcpu_id, + unsigned long imsic_mask) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVI, SBI_EXT_COVI_TVM_REBIND_IMSIC_BEGIN, + tvm_gid, vcpu_id, imsic_mask, 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covi_rebind_vcpu_imsic_clone(unsigned long tvm_gid, + unsigned long vcpu_id) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVI, SBI_EXT_COVI_TVM_REBIND_IMSIC_CLONE, + tvm_gid, vcpu_id, 0, 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covi_rebind_vcpu_imsic_end(unsigned long tvm_gid, unsigned long vcpu_id) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVI, SBI_EXT_COVI_TVM_REBIND_IMSIC_END, + tvm_gid, vcpu_id, 0, 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + int sbi_covh_tsm_get_info(struct sbi_cove_tsm_info *tinfo_addr) { struct sbiret ret; From patchwork Wed Apr 19 22:16:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217601 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 09F8FC77B75 for ; Wed, 19 Apr 2023 22:37:46 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=a9xUgF7pUNXoYbZsp+oZ6o2nv5KRwW13MdGWBAPCxyY=; b=jedvPobbCwLQhO VGNvsbykTQBZXf6qFG4M7bFO8pNEXCIrHr0L7AfOXHBVJsQPDElEr/NpuO7Kj3dVg5wGffiQMeqO9 ENg6K1eYw5kiIhD6juSSX8XtBcSWx1q+iGAahmXPLZHQ05WoUbE8MnYXVNjzRhHbarwugCiN4oQZw 1WPQwMxnZjOyp+sN3ueeOc7ghWGJ/Tb/jx74VopCxVrLTSteNzv8oY6AYTqR2HsnVu+oadk8qm+/S xdsD0s3CDbItMjoWcNlzDKc+dktr2UxK34dbg+tV1Vj8fS9KJkPh5ISbFzQZca8Q6cR8nlcfKrtZq NIQmZ123lm5PyJKmRNqQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQk-006WnM-2G; Wed, 19 Apr 2023 22:37:38 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQg-006WXV-0d for linux-riscv@bombadil.infradead.org; Wed, 19 Apr 2023 22:37:34 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=PU8iFueolrZCmcbo4gYTi+eKDv6f+PtpXTvRFNX2NZA=; b=DCIUkHCAO6LjgQqsEpU1VOVQPf NS8dP7W6hJjjh5QpqPVq0B4dfXrWned1Ls2iA8vYTUOZ+LUlH9Zvol0D39bEmSnB/+EZebNpvPH95 ucIFGneDi/Cf84GtBgt/jauN+uMkoJbUUKI/GhUBWhgoZOcreQUgm82kmd4PnTDF3/kr8PyvShkMq Ua6F1K/xAzk1gEAiLOMwDtF5hQ6OcJVhIGNFv2KhpGxdXcubuHlM2C7RYDQMSYGcEMut9kCYA5wEQ y4F73AUD29li8ds2KZa2R8qGJ4ZV8V0rcMiJXDc8G0UI6SvSoo3qIedTgANqnlhf4wpHVFjWkELZE yXQOAEXw==; Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]) by casper.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ppG8H-00DeGj-Ve for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:36 +0000 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-63b73203e0aso2785627b3a.1 for ; Wed, 19 Apr 2023 15:18:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942711; x=1684534711; 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=PU8iFueolrZCmcbo4gYTi+eKDv6f+PtpXTvRFNX2NZA=; b=SCmbMjD55QloUYOgXX6MGUNEjAyAnMm/HfGRAGM1vS7vADDmD//a3bJ19IoxEPbsjR M2cj4sRqNKbEknJUmqcRzzuGvuAWBBnIZmhseVA9z0HrnfGvz/BvmurAl+ZaqdFV9z0+ XE+By4GvDamj1V0iHkImOVtuMlZpTJ8+WBz54nyEAGyf404LButJI5R1r1M9kSp/2kE/ u0yMzOB8MvU04pFuFPh8V3hwD0+VVdFu6iRbztwNwUe0wOuPOvAaUzMpoheX119q8fkx HByPgmjTnZTij1BszzopQyeoxfkgy1UkfAJB+AO4WFsTrtiA+xJhP2Ikh+DJT3BJUL+Y xQ7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942711; x=1684534711; 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=PU8iFueolrZCmcbo4gYTi+eKDv6f+PtpXTvRFNX2NZA=; b=mAdjioR9VXDZ9XBNihI+dYA4Mw1KyH//KvJlVYl9lRe1cHsPZvgXNi1T/lkjGPv8xf 2HTPzuct5QFbNoHFhlpNHQwninbffF5eLbEqaAZnevlNC1bjZbDwqO+NcBJEv7QKnu8x 1KDDfscwgTrzGlcD6t04LGdhVbBD08fBw80rJmTmX1qKC59+gHN2WBBT+uBuPT6xzRwJ O4bSY7Fkov71FA4Wx/g5lqZKd9UzFCXEVJiGa1x1wgTLo2FIO/RrT7mo/sS9Vka14z84 QxTCAhGe0UMLpyv43TgwpVypV7sL8F29FO+OnuxUCvfrtBfTZWIQeq86p0bZVQ1NJgOa 8E8g== X-Gm-Message-State: AAQBX9fzJhUUHDTUNmVjazwi1xucXpHu6PEmHBTE4CEYd0qtTsE5JajO /tgg6dd2I/m7Di3XL3WFxMUxTA== X-Google-Smtp-Source: AKy350YxZk7F6dEeajji4ZjqoV6O38ZOLVkf4vnq1EOHfqdcV069hDfO+BMQgpUlX71NUn0DfXxZlw== X-Received: by 2002:a17:90a:9e5:b0:246:aeee:e61c with SMTP id 92-20020a17090a09e500b00246aeeee61cmr4072001pjo.11.1681942711371; Wed, 19 Apr 2023 15:18:31 -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.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:31 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 28/48] RISC-V: KVM: Add interrupt management functions for TVM Date: Wed, 19 Apr 2023 15:16:56 -0700 Message-Id: <20230419221716.3603068-29-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_231834_093564_B5D9BF0D X-CRM114-Status: GOOD ( 22.18 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org The COVI SBI extension defines the functions related to interrupt management for TVMs. These functions are the glue logic between AIA code and the actually CoVE Interrupt SBI extension(COVI). Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_cove.h | 34 ++++ arch/riscv/kvm/cove.c | 256 ++++++++++++++++++++++++++++++ 2 files changed, 290 insertions(+) diff --git a/arch/riscv/include/asm/kvm_cove.h b/arch/riscv/include/asm/kvm_cove.h index b63682f..74bad2f 100644 --- a/arch/riscv/include/asm/kvm_cove.h +++ b/arch/riscv/include/asm/kvm_cove.h @@ -61,10 +61,19 @@ struct kvm_riscv_cove_page { unsigned long gpa; }; +struct imsic_tee_state { + bool bind_required; + bool bound; + int vsfile_hgei; +}; + struct kvm_cove_tvm_vcpu_context { struct kvm_vcpu *vcpu; /* Pages storing each vcpu state of the TVM in TSM */ struct kvm_riscv_cove_page vcpu_state; + + /* Per VCPU imsic state */ + struct imsic_tee_state imsic; }; struct kvm_cove_tvm_context { @@ -133,6 +142,16 @@ int kvm_riscv_cove_vm_add_memreg(struct kvm *kvm, unsigned long gpa, unsigned lo int kvm_riscv_cove_gstage_map(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned long hva); /* Fence related function */ int kvm_riscv_cove_tvm_fence(struct kvm_vcpu *vcpu); + +/* AIA related CoVE functions */ +int kvm_riscv_cove_aia_init(struct kvm *kvm); +int kvm_riscv_cove_vcpu_inject_interrupt(struct kvm_vcpu *vcpu, unsigned long iid); +int kvm_riscv_cove_vcpu_imsic_unbind(struct kvm_vcpu *vcpu, int old_cpu); +int kvm_riscv_cove_vcpu_imsic_bind(struct kvm_vcpu *vcpu, unsigned long imsic_mask); +int kvm_riscv_cove_vcpu_imsic_rebind(struct kvm_vcpu *vcpu, int old_pcpu); +int kvm_riscv_cove_aia_claim_imsic(struct kvm_vcpu *vcpu, phys_addr_t imsic_pa); +int kvm_riscv_cove_aia_convert_imsic(struct kvm_vcpu *vcpu, phys_addr_t imsic_pa); +int kvm_riscv_cove_vcpu_imsic_addr(struct kvm_vcpu *vcpu); #else static inline bool kvm_riscv_cove_enabled(void) {return false; }; static inline int kvm_riscv_cove_init(void) { return -1; } @@ -162,6 +181,21 @@ static inline int kvm_riscv_cove_vm_measure_pages(struct kvm *kvm, } static inline int kvm_riscv_cove_gstage_map(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned long hva) {return -1; } +/* AIA related TEE functions */ +static inline int kvm_riscv_cove_aia_init(struct kvm *kvm) { return -1; } +static inline int kvm_riscv_cove_vcpu_inject_interrupt(struct kvm_vcpu *vcpu, + unsigned long iid) { return -1; } +static inline int kvm_riscv_cove_vcpu_imsic_unbind(struct kvm_vcpu *vcpu, + int old_cpu) { return -1; } +static inline int kvm_riscv_cove_vcpu_imsic_bind(struct kvm_vcpu *vcpu, + unsigned long imsic_mask) { return -1; } +static inline int kvm_riscv_cove_aia_claim_imsic(struct kvm_vcpu *vcpu, + phys_addr_t imsic_pa) { return -1; } +static inline int kvm_riscv_cove_aia_convert_imsic(struct kvm_vcpu *vcpu, + phys_addr_t imsic_pa) { return -1; } +static inline int kvm_riscv_cove_vcpu_imsic_addr(struct kvm_vcpu *vcpu) { return -1; } +static inline int kvm_riscv_cove_vcpu_imsic_rebind(struct kvm_vcpu *vcpu, + int old_pcpu) { return -1; } #endif /* CONFIG_RISCV_COVE_HOST */ #endif /* __KVM_RISCV_COVE_H */ diff --git a/arch/riscv/kvm/cove.c b/arch/riscv/kvm/cove.c index 4a8a8db..154b01a 100644 --- a/arch/riscv/kvm/cove.c +++ b/arch/riscv/kvm/cove.c @@ -8,6 +8,7 @@ * Atish Patra */ +#include #include #include #include @@ -137,6 +138,247 @@ __always_inline bool kvm_riscv_cove_enabled(void) return riscv_cove_enabled; } +static void kvm_cove_imsic_clone(void *info) +{ + int rc; + struct kvm_vcpu *vcpu = info; + struct kvm *kvm = vcpu->kvm; + + rc = sbi_covi_rebind_vcpu_imsic_clone(kvm->arch.tvmc->tvm_guest_id, vcpu->vcpu_idx); + if (rc) + kvm_err("Imsic clone failed guest %ld vcpu %d pcpu %d\n", + kvm->arch.tvmc->tvm_guest_id, vcpu->vcpu_idx, smp_processor_id()); +} + +static void kvm_cove_imsic_unbind(void *info) +{ + struct kvm_vcpu *vcpu = info; + struct kvm_cove_tvm_context *tvmc = vcpu->kvm->arch.tvmc; + + /*TODO: We probably want to return but the remote function call doesn't allow any return */ + if (sbi_covi_unbind_vcpu_imsic_begin(tvmc->tvm_guest_id, vcpu->vcpu_idx)) + return; + + /* This may issue IPIs to running vcpus. */ + if (kvm_riscv_cove_tvm_fence(vcpu)) + return; + + if (sbi_covi_unbind_vcpu_imsic_end(tvmc->tvm_guest_id, vcpu->vcpu_idx)) + return; + + kvm_info("Unbind success for guest %ld vcpu %d pcpu %d\n", + tvmc->tvm_guest_id, smp_processor_id(), vcpu->vcpu_idx); +} + +int kvm_riscv_cove_vcpu_imsic_addr(struct kvm_vcpu *vcpu) +{ + struct kvm_cove_tvm_context *tvmc; + struct kvm *kvm = vcpu->kvm; + struct kvm_vcpu_aia *vaia = &vcpu->arch.aia_context; + int ret; + + if (!kvm->arch.tvmc) + return -EINVAL; + + tvmc = kvm->arch.tvmc; + + ret = sbi_covi_set_vcpu_imsic_addr(tvmc->tvm_guest_id, vcpu->vcpu_idx, vaia->imsic_addr); + if (ret) + return -EPERM; + + return 0; +} + +int kvm_riscv_cove_aia_convert_imsic(struct kvm_vcpu *vcpu, phys_addr_t imsic_pa) +{ + struct kvm *kvm = vcpu->kvm; + int ret; + + if (!kvm->arch.tvmc) + return -EINVAL; + + ret = sbi_covi_convert_imsic(imsic_pa); + if (ret) + return -EPERM; + + ret = kvm_riscv_cove_fence(); + if (ret) + return ret; + + return 0; +} + +int kvm_riscv_cove_aia_claim_imsic(struct kvm_vcpu *vcpu, phys_addr_t imsic_pa) +{ + int ret; + struct kvm *kvm = vcpu->kvm; + + if (!kvm->arch.tvmc) + return -EINVAL; + + ret = sbi_covi_reclaim_imsic(imsic_pa); + if (ret) + return -EPERM; + + return 0; +} + +int kvm_riscv_cove_vcpu_imsic_rebind(struct kvm_vcpu *vcpu, int old_pcpu) +{ + struct kvm_cove_tvm_context *tvmc; + struct kvm *kvm = vcpu->kvm; + struct kvm_cove_tvm_vcpu_context *tvcpu = vcpu->arch.tc; + int ret; + cpumask_t tmpmask; + + if (!kvm->arch.tvmc) + return -EINVAL; + + tvmc = kvm->arch.tvmc; + + ret = sbi_covi_rebind_vcpu_imsic_begin(tvmc->tvm_guest_id, vcpu->vcpu_idx, + BIT(tvcpu->imsic.vsfile_hgei)); + if (ret) { + kvm_err("Imsic rebind begin failed guest %ld vcpu %d pcpu %d\n", + tvmc->tvm_guest_id, vcpu->vcpu_idx, smp_processor_id()); + return ret; + } + + ret = kvm_riscv_cove_tvm_fence(vcpu); + if (ret) + return ret; + + cpumask_clear(&tmpmask); + cpumask_set_cpu(old_pcpu, &tmpmask); + on_each_cpu_mask(&tmpmask, kvm_cove_imsic_clone, vcpu, 1); + + ret = sbi_covi_rebind_vcpu_imsic_end(tvmc->tvm_guest_id, vcpu->vcpu_idx); + if (ret) { + kvm_err("Imsic rebind end failed guest %ld vcpu %d pcpu %d\n", + tvmc->tvm_guest_id, vcpu->vcpu_idx, smp_processor_id()); + return ret; + } + + tvcpu->imsic.bound = true; + + return 0; +} + +int kvm_riscv_cove_vcpu_imsic_bind(struct kvm_vcpu *vcpu, unsigned long imsic_mask) +{ + struct kvm_cove_tvm_context *tvmc; + struct kvm *kvm = vcpu->kvm; + struct kvm_cove_tvm_vcpu_context *tvcpu = vcpu->arch.tc; + int ret; + + if (!kvm->arch.tvmc) + return -EINVAL; + + tvmc = kvm->arch.tvmc; + + ret = sbi_covi_bind_vcpu_imsic(tvmc->tvm_guest_id, vcpu->vcpu_idx, imsic_mask); + if (ret) { + kvm_err("Imsic bind failed for imsic %lx guest %ld vcpu %d pcpu %d\n", + imsic_mask, tvmc->tvm_guest_id, vcpu->vcpu_idx, smp_processor_id()); + return ret; + } + tvcpu->imsic.bound = true; + pr_err("%s: rebind success vcpu %d hgei %d pcpu %d\n", __func__, + vcpu->vcpu_idx, tvcpu->imsic.vsfile_hgei, smp_processor_id()); + + return 0; +} + +int kvm_riscv_cove_vcpu_imsic_unbind(struct kvm_vcpu *vcpu, int old_pcpu) +{ + struct kvm *kvm = vcpu->kvm; + struct kvm_cove_tvm_vcpu_context *tvcpu = vcpu->arch.tc; + cpumask_t tmpmask; + + if (!kvm->arch.tvmc) + return -EINVAL; + + /* No need to unbind if it is not bound already */ + if (!tvcpu->imsic.bound) + return 0; + + /* Do it first even if there is failure to prevent it to try again */ + tvcpu->imsic.bound = false; + + if (smp_processor_id() == old_pcpu) { + kvm_cove_imsic_unbind(vcpu); + } else { + /* Unbind can be invoked from a different physical cpu */ + cpumask_clear(&tmpmask); + cpumask_set_cpu(old_pcpu, &tmpmask); + on_each_cpu_mask(&tmpmask, kvm_cove_imsic_unbind, vcpu, 1); + } + + return 0; +} + +int kvm_riscv_cove_vcpu_inject_interrupt(struct kvm_vcpu *vcpu, unsigned long iid) +{ + struct kvm_cove_tvm_context *tvmc; + struct kvm *kvm = vcpu->kvm; + int ret; + + if (!kvm->arch.tvmc) + return -EINVAL; + + tvmc = kvm->arch.tvmc; + + ret = sbi_covi_inject_external_interrupt(tvmc->tvm_guest_id, vcpu->vcpu_idx, iid); + if (ret) + return ret; + + return 0; +} + +int kvm_riscv_cove_aia_init(struct kvm *kvm) +{ + struct kvm_aia *aia = &kvm->arch.aia; + struct sbi_cove_tvm_aia_params *tvm_aia; + struct kvm_vcpu *vcpu; + struct kvm_cove_tvm_context *tvmc; + int ret; + + if (!kvm->arch.tvmc) + return -EINVAL; + + tvmc = kvm->arch.tvmc; + + /* Sanity Check */ + if (aia->aplic_addr != KVM_RISCV_AIA_UNDEF_ADDR) + return -EINVAL; + + /* TVMs must have a physical guest interrut file */ + if (aia->mode != KVM_DEV_RISCV_AIA_MODE_HWACCEL) + return -ENODEV; + + tvm_aia = kzalloc(sizeof(*tvm_aia), GFP_KERNEL); + if (!tvm_aia) + return -ENOMEM; + + /* Address of the IMSIC group ID, hart ID & guest ID of 0 */ + vcpu = kvm_get_vcpu_by_id(kvm, 0); + tvm_aia->imsic_base_addr = vcpu->arch.aia_context.imsic_addr; + + tvm_aia->group_index_bits = aia->nr_group_bits; + tvm_aia->group_index_shift = aia->nr_group_shift; + tvm_aia->hart_index_bits = aia->nr_hart_bits; + tvm_aia->guest_index_bits = aia->nr_guest_bits; + /* Nested TVMs are not supported yet */ + tvm_aia->guests_per_hart = 0; + + + ret = sbi_covi_tvm_aia_init(tvmc->tvm_guest_id, tvm_aia); + if (ret) + kvm_err("TVM AIA init failed with rc %d\n", ret); + + return ret; +} + void kvm_riscv_cove_vcpu_load(struct kvm_vcpu *vcpu) { kvm_riscv_vcpu_timer_restore(vcpu); @@ -283,6 +525,7 @@ void noinstr kvm_riscv_cove_vcpu_switchto(struct kvm_vcpu *vcpu, struct kvm_cpu_ struct kvm_cpu_context *cntx = &vcpu->arch.guest_context; void *nshmem; struct kvm_guest_timer *gt = &kvm->arch.timer; + struct kvm_cove_tvm_vcpu_context *tvcpuc = vcpu->arch.tc; if (!kvm->arch.tvmc) return; @@ -301,6 +544,19 @@ void noinstr kvm_riscv_cove_vcpu_switchto(struct kvm_vcpu *vcpu, struct kvm_cpu_ tvmc->finalized_done = true; } + /* + * Bind the vsfile here instead during the new vsfile allocation because + * COVH bind call requires the TVM to be in finalized state. + */ + if (tvcpuc->imsic.bind_required) { + tvcpuc->imsic.bind_required = false; + rc = kvm_riscv_cove_vcpu_imsic_bind(vcpu, BIT(tvcpuc->imsic.vsfile_hgei)); + if (rc) { + kvm_err("bind failed with rc %d\n", rc); + return; + } + } + rc = sbi_covh_run_tvm_vcpu(tvmc->tvm_guest_id, vcpu->vcpu_idx); if (rc) { trap->scause = EXC_CUSTOM_KVM_COVE_RUN_FAIL; From patchwork Wed Apr 19 22:16:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217581 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 D0B5EC77B73 for ; Wed, 19 Apr 2023 22:26:28 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=nEG32vACDefGrj1YBl7j3hUeYlYROZ/B4c7lqElsy3I=; b=PMrplUy04qppUD OHtZqfmvuRl8ISkGH8ih9YUKFmBloD/CwA04hkre0BbyDr5rvLBKmFX4nXz83M/GTZRGkLqWu/Sun 4c7QpLY9W45K/nhgsMmYSnhU4GMxyaMH5AYb9W3Yw+fbkq/5K1r7xnGID9yUWWFXyeFAYvrQszCQf PiL6Qd2/gOtxLaibq+hikbfrZPcONisVnFToCTCOX5DaIj08x6Cj6U8e+J9sqO/xMG7z/HvpKL/U+ gPinbjbgk/NTBLDhvquuSLspYR3bsaU2pb/6RzT3m/+HYz2YSWRz59CQWvblf+r8yiqzT4dlZWhcN In9J96FKDtUXU/0NEFzw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGFq-006VGx-1w; Wed, 19 Apr 2023 22:26:22 +0000 Received: from mail-ua1-x92e.google.com ([2607:f8b0:4864:20::92e]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGFn-006VGJ-2x for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:26:21 +0000 Received: by mail-ua1-x92e.google.com with SMTP id a1e0cc1a2514c-771d9ec5aa5so156683241.0 for ; Wed, 19 Apr 2023 15:26:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681943179; x=1684535179; 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=MzHgRd9E4939KzL4yVjhGODBFacgyWrf3hGucLzcUO8=; b=KQwPki3+o8RoSAlX8lJMO4IjKMjct4qtKGGSedm1Y5NrjE1yY6FE23juxnWdGAF5tF Ff6QhY6Een6c7MTHWr0Xv+sqox3ZFbM8CsoaTjqv+xTv4V1Gsf7MwcsCfEYqnbxAomgt oLCHtdhD2NnWg/5AnkAX5r+ZV4Dkc1YjkJKxW7lMlcwGqAEjIdXE7si5CPtyD/9nZ8NZ hsF48oR7lVZvLvhPQ9qXnMo/UH/MoMqpcBfkzH/2jWAw8M4WaCGgDKuRe2y0g24zujoz tyVcLJGnwIQ6aI2nvzPvOvTO13onlK5b5SHjhfISiWK5GCiZTvN4vw/LB5p6AbUS6sq9 fE4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681943179; x=1684535179; 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=MzHgRd9E4939KzL4yVjhGODBFacgyWrf3hGucLzcUO8=; b=EYDFpGT7/xZkmrXeO12pQs6p/3spnLeWe76EfJKuKyF5MGt712R6abR8BCqpu3iWcb Y2Y3wgVB3yMHP+CUkakFYuEqUA3SuJS2QzExiojt7dQTnI0nsSVvWDFW40T7mc/7ew5R bBLh1To9bgsCQYVSAarhk5HpgPMKdW6qNetF+mbUDBbX7yT9Y7D16cTh4cd+R8JTHOEP sFdamDF1hGQEiiFXerAWbJHGJJYrOuIw7Y9EwwI+WP0xAQjqzTU33NivCMqoGAwrcBn9 ArYoFrxVNkgQMTNOkgBvIYqb1rHxln0tS8rE5Rtwg5i6jVxRz+BcO5MSqRLUhgzSf9wo hG4w== X-Gm-Message-State: AAQBX9d588EthNbIeAAyQugxVEAUmQZWF+db+idQUyKRWv0CORJ3vZQa vTxYw6ItySGzAmbyH2cwrUdVDJseNUv4X8FUnl8= X-Google-Smtp-Source: AKy350Zeg7WcBlbIeS4PGWBrU+nA6D3ZQw2RQUdtMzrRIbcOBcnB4FvzAieagBSVaRSEerSYF/00HQ== X-Received: by 2002:a17:903:25d1:b0:1a6:3e45:8df with SMTP id jc17-20020a17090325d100b001a63e4508dfmr3379688plb.33.1681942713552; Wed, 19 Apr 2023 15:18:33 -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.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:33 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 29/48] RISC-V: KVM: Skip AIA CSR updates for TVMs Date: Wed, 19 Apr 2023 15:16:57 -0700 Message-Id: <20230419221716.3603068-30-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_152619_951037_80870E5E X-CRM114-Status: GOOD ( 15.51 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org For TVMs, the host must not support AIA CSR emulation. In addition to that, during vcpu load/put the CSR updates are not necessary as the CSR state must not be visible to the host. Signed-off-by: Atish Patra --- arch/riscv/kvm/aia.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/arch/riscv/kvm/aia.c b/arch/riscv/kvm/aia.c index 71216e1..e3da661 100644 --- a/arch/riscv/kvm/aia.c +++ b/arch/riscv/kvm/aia.c @@ -15,6 +15,7 @@ #include #include #include +#include struct aia_hgei_control { raw_spinlock_t lock; @@ -134,7 +135,7 @@ void kvm_riscv_vcpu_aia_load(struct kvm_vcpu *vcpu, int cpu) { struct kvm_vcpu_aia_csr *csr = &vcpu->arch.aia_context.guest_csr; - if (!kvm_riscv_aia_available()) + if (!kvm_riscv_aia_available() || is_cove_vcpu(vcpu)) return; csr_write(CSR_VSISELECT, csr->vsiselect); @@ -152,7 +153,7 @@ void kvm_riscv_vcpu_aia_put(struct kvm_vcpu *vcpu) { struct kvm_vcpu_aia_csr *csr = &vcpu->arch.aia_context.guest_csr; - if (!kvm_riscv_aia_available()) + if (!kvm_riscv_aia_available() || is_cove_vcpu(vcpu)) return; csr->vsiselect = csr_read(CSR_VSISELECT); @@ -370,6 +371,10 @@ int kvm_riscv_vcpu_aia_rmw_ireg(struct kvm_vcpu *vcpu, unsigned int csr_num, if (!kvm_riscv_aia_available()) return KVM_INSN_ILLEGAL_TRAP; + /* TVMs do not support AIA emulation */ + if (is_cove_vcpu(vcpu)) + return KVM_INSN_EXIT_TO_USER_SPACE; + /* First try to emulate in kernel space */ isel = csr_read(CSR_VSISELECT) & ISELECT_MASK; if (isel >= ISELECT_IPRIO0 && isel <= ISELECT_IPRIO15) @@ -529,6 +534,9 @@ void kvm_riscv_aia_enable(void) if (!kvm_riscv_aia_available()) return; + if (unlikely(kvm_riscv_cove_enabled())) + goto enable_gext; + aia_set_hvictl(false); csr_write(CSR_HVIPRIO1, 0x0); csr_write(CSR_HVIPRIO2, 0x0); @@ -539,6 +547,7 @@ void kvm_riscv_aia_enable(void) csr_write(CSR_HVIPRIO2H, 0x0); #endif +enable_gext: /* Enable per-CPU SGEI interrupt */ enable_percpu_irq(hgei_parent_irq, irq_get_trigger_type(hgei_parent_irq)); @@ -559,7 +568,9 @@ void kvm_riscv_aia_disable(void) csr_clear(CSR_HIE, BIT(IRQ_S_GEXT)); disable_percpu_irq(hgei_parent_irq); - aia_set_hvictl(false); + /* The host is not allowed modify hvictl for TVMs */ + if (!unlikely(kvm_riscv_cove_enabled())) + aia_set_hvictl(false); raw_spin_lock_irqsave(&hgctrl->lock, flags); From patchwork Wed Apr 19 22:16:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217670 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 996BEC77B75 for ; Wed, 19 Apr 2023 23:30:56 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=5DEGGnAtZ4FcKGGIjBrH7ur91iScjY3HGz88dWc5tyA=; b=dBX6rz7lVyzDNC 7c/dRIk5CAf4aGX0UEAKfmJL6o4h6hVgG5LUMFGnRYjdYaOLDOZHzU3s/oBtPc/SNf9i/J26fldDx VQSQHM74BoQerHkhG1vBPxdaG7U9JCuJxRyWtNx4mixaCuY1kj+q+qo5vv+yOPl7WJv/DXupl57sq rHJsmos200a/5NtnsUzSU3ciDppQjn4JinVsrnkf4wfGeuX5W+4mzptuOyV4OrNvpTT+xaAINs8gc FnwAoBLUvcYOBbdd8wu01nljB3zdUfGXaZTXRFIjxRfsuuZkoNCmXvuwHJ6sWF6UGJ5AB4YvKU0H8 8k9HnKI6CeIDiHfvP+GQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppHG8-006eXv-2G; Wed, 19 Apr 2023 23:30:44 +0000 Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG8N-006TLQ-0z for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:42 +0000 Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-63b7096e2e4so377285b3a.2 for ; Wed, 19 Apr 2023 15:18:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942716; x=1684534716; 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=g1210zPiD8zHFMhzBINzpx5nEW8Eoe5MAD0zhuCIRKc=; b=ANhU+J8nQWq2obESgBEJE0X9NLT82kBUBDzc1BRbSeZ1EgPeiOl0GddoyWaEPDRU8N vnwjEIo0UjaS9xezvAc/UGttMWlJo7ew/iqSZCawFdCpXkbeL6whYMnxnv1UE0Mkzjl9 zxU19X9QWwNQwOpt5WP+CxQUT1ZA3xUcznURR03bnG+WcdbBRPDMOVkCCCi+rkT0f8qL 85oGYhbbNz2xb1HJWq0YwM9QGXFDFKbWMgSuumHQ7xfEhCPnY/6ETL+ra68ruaG7yl2u qOhetkOJdoWKqEsNNxd/aiUVnUUF2SqhhyrAmB8w+eSk+hd8cQeWPbuaUf1XaZLgT9mw WXww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942716; x=1684534716; 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=g1210zPiD8zHFMhzBINzpx5nEW8Eoe5MAD0zhuCIRKc=; b=bZvfW1QfzIjjqH2QtPCrZr64S4o6bQVtk1wYMancBdyJqNceUgR/J3dnDLGZHielSs mlR6rJ7GIbqFsZAyfL6tq73zR/ACI0FhMCd7eLDWdHsm4mFHI1EhhOg1tv/OdgSWzUsF xI24dUBwIikDNpKuwbsFIWw9P3DI6gsB7zBvpyiLO085zR3RiDKllZg3snb4ZnMIPSLJ hundfSZJ6mspSASMJdJsxIKe6UNgjzZCcqSDeQUIrNNupGEneVrhWVFW8gZ7xY+veA7q 9/Au3WmRtLRQakTI0VlRz5Jp/z/t/J3KzjQX19TpVozpMek/rbGJY7Y6n5RWu77Tr5N6 5zMw== X-Gm-Message-State: AAQBX9eK9F66qYb5O75NzOyL05YCifzMGx/nZD0nEWR5UyfxAmRTqGhK w5M0M5Q6S9/o7KtfNDgQChbwVw== X-Google-Smtp-Source: AKy350YnFYoQc/AGMgRzA7K3To1EBVIiHBUS1P/8MWh29k47wvhKo2iefk2kUhAwoVQIoToGy+/bcA== X-Received: by 2002:a17:903:1c4:b0:1a1:ee8c:eeba with SMTP id e4-20020a17090301c400b001a1ee8ceebamr7881066plh.59.1681942715856; Wed, 19 Apr 2023 15:18:35 -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.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:35 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 30/48] RISC-V: KVM: Perform limited operations in hardware enable/disable Date: Wed, 19 Apr 2023 15:16:58 -0700 Message-Id: <20230419221716.3603068-31-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151839_342521_E415E331 X-CRM114-Status: GOOD ( 14.79 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Hardware enable/disable path only need to perform AIA/NACL enable/disable for TVMs. All other operations i.e. interrupt/exception delegation, counter access must be provided by the TSM as host doesn't have control of these operations for a TVM. Signed-off-by: Atish Patra --- arch/riscv/kvm/main.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/arch/riscv/kvm/main.c b/arch/riscv/kvm/main.c index 45ee62d..842b78d 100644 --- a/arch/riscv/kvm/main.c +++ b/arch/riscv/kvm/main.c @@ -13,6 +13,7 @@ #include #include #include +#include long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) @@ -29,6 +30,15 @@ int kvm_arch_hardware_enable(void) if (rc) return rc; + /* + * We just need to invoke aia enable for CoVE if host is in VS mode + * However, if the host is running in HS mode, we need to initialize + * other CSRs as well for legacy VMs. + * TODO: Handle host in HS mode use case. + */ + if (unlikely(kvm_riscv_cove_enabled())) + goto enable_aia; + hedeleg = 0; hedeleg |= (1UL << EXC_INST_MISALIGNED); hedeleg |= (1UL << EXC_BREAKPOINT); @@ -49,6 +59,7 @@ int kvm_arch_hardware_enable(void) csr_write(CSR_HVIP, 0); +enable_aia: kvm_riscv_aia_enable(); return 0; @@ -58,6 +69,8 @@ void kvm_arch_hardware_disable(void) { kvm_riscv_aia_disable(); + if (unlikely(kvm_riscv_cove_enabled())) + goto disable_nacl; /* * After clearing the hideleg CSR, the host kernel will receive * spurious interrupts if hvip CSR has pending interrupts and the @@ -69,6 +82,7 @@ void kvm_arch_hardware_disable(void) csr_write(CSR_HEDELEG, 0); csr_write(CSR_HIDELEG, 0); +disable_nacl: kvm_riscv_nacl_disable(); } From patchwork Wed Apr 19 22:16:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217600 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 76CDDC77B73 for ; Wed, 19 Apr 2023 22:37:44 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=5pDw/rPTC5RJLTp76vciku2Gjf8yhfCXkuhyDEJB3JU=; b=swRvSPqettBKDW XuwpVTJ6LsqObV48AG2n+ZAGYSbwrIuK5rhqQF/rwjqsoQHpqcuipNz67VkcNQS2P645MIjAj0GN1 ZeHb+DVcNmMf7NJ7/d5nq7SrL8oKjok2HyJkRyJ+rErihA8X6S//0CQBN0VUQf20FOj/sG9Dn3YKt TKqZ+jWPljqiRgxzqv1K6f76vF+efCry/7XkI88Jdum4fbeiIcIjG0TJjuqRuruduCnFS7sga/AFE ay/xRtWJloPMffeG2JjM+cqugWvzh97qjChTeG/Qrn2WMsf4Aps5MI1tn3OLTZ3o8o63Kqn7gKEEC ChnPrfdit9KDyLjfAvtg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQi-006Wkk-2j; Wed, 19 Apr 2023 22:37:36 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQd-006WXV-3D for linux-riscv@bombadil.infradead.org; Wed, 19 Apr 2023 22:37:32 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=K2nJhanY2RyrdzmZ5jdtnkafHRmAptwnMC5XRsCTn9I=; b=TNbWOSBVqkHVpbvLG3ZtrEaO4o QaMa77oL45WzjKichcoz/ywNOr30n9pI+0JyxrWabHHYbjvmW1pYCkviXpDOFEHvcVNL1pDsqUPfn REgU0MznW/VcLnC+4KTLFOLkGvqr2SFL8WqSAyAwq8e/BGHC+D+kCcxDshZv9w3UFslcPnw4MJKeA QXFx1Et1VwkmQX7k/h2adb1WfOQxoTKSQvmUHoBhJJPvUOPNUzufP7WJJMJeZ1HIP/rOCbaM36dhj BdpFGv+sdd6ZJrHJz7JF9Bmr89pqFKN4rlri1ZrIqjMnDq6xy4mahBUCXlaZDGAW3taMgiFw2I9y0 +rSWCsdw==; Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]) by casper.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ppG8O-00DeJ0-KH for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:44 +0000 Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-63b4dfead1bso376289b3a.3 for ; Wed, 19 Apr 2023 15:18:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942718; x=1684534718; 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=K2nJhanY2RyrdzmZ5jdtnkafHRmAptwnMC5XRsCTn9I=; b=JS7pXfcL0uZWhUdoeqJLjU0060h+zlTyFU4Xu6hWHx+bZXgxY7ubdU1X9NVau3JlX9 +7CYbRRwlIc3Vqq9APC8BsBuJn4DTncbkzqKnldPV32jiP3HmMEO6Gpz0Jpg3E8ecp/b 7UYaf07kCninwBHQ2YB/t4GOxc2RYZ1XD5frQZDPWtSV2zmt3BBqoNXrrBa8O/2uay1U 9Q5+GXI2CSH6c4nCaey/IdmivSR/XDL19JFooIN8hV4UeXZUf4HW7Tq4P1XLc0glmzvp cJ6OAEWscVYWk70eywtGuEoxadALFsAZYFXB+jt15zlHvhhj3dquE05TC9qY1zNQuLog DNPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942718; x=1684534718; 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=K2nJhanY2RyrdzmZ5jdtnkafHRmAptwnMC5XRsCTn9I=; b=jJcZ3XbROcHulEnswWHhPiItPUDq5wVGf0LjXHfTw7QaBANIF3u0NTJPHKC6f2UCiu 8NDSkB4DioQCeV8mecquF5lCP2iU0z6mLwpaB+wLFftc9XrfpFn6Z4lOJDg7us4660J9 +XICh3ZWjBXVVEVUuhgK5R8PHedUQeFWw69cMQzz7v02GvUH1liS8c3JI88flRZy5fuS ObrR8hmCqHzSK4sm7eGx2Ri10IGWpScVjGZ41te4uUDRLJSfOojq4K5jenvWQlZihgcW ciV4IY2hNka9v5iEbL/EO3y/nxvORGQ7Ku0JbySeHV9oWwCVS16joyMpYr/EfxZ/zzU8 bHZg== X-Gm-Message-State: AAQBX9dtqesfeesIInZeZVZV8ox7L+vgDiS0cUo/wApGC8PoDFhJOUq6 /Q+aej1OxsOJ3loQ/5KXJSVuBA== X-Google-Smtp-Source: AKy350Z7HNgpp/rz6oeri9oovBKao1xAaHX/MXTw46NfVQFmX/yVtIUB1ewCw+YJMzwOiUfVeGRvZQ== X-Received: by 2002:a17:902:e0d3:b0:1a2:8770:bb17 with SMTP id e19-20020a170902e0d300b001a28770bb17mr5706306pla.9.1681942717994; Wed, 19 Apr 2023 15:18:37 -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.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:37 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 31/48] RISC-V: KVM: Indicate no support user space emulated IRQCHIP Date: Wed, 19 Apr 2023 15:16:59 -0700 Message-Id: <20230419221716.3603068-32-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_231840_729973_2C4D4F8C X-CRM114-Status: GOOD ( 11.37 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org The KVM_INTERRUPT IOCTL is used for the userspace emulated IRQCHIP. The TEE use case do not support that yet. Return appropriate error in case any VMM tries to invoke that operation. Signed-off-by: Atish Patra --- arch/riscv/kvm/vcpu.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index 20d4800..65f87e1 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -716,6 +716,9 @@ long kvm_arch_vcpu_async_ioctl(struct file *filp, if (ioctl == KVM_INTERRUPT) { struct kvm_interrupt irq; + /* We do not support user space emulated IRQCHIP for TVMs yet */ + if (is_cove_vcpu(vcpu)) + return -ENXIO; if (copy_from_user(&irq, argp, sizeof(irq))) return -EFAULT; From patchwork Wed Apr 19 22:17:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217593 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 6F1CDC77B73 for ; Wed, 19 Apr 2023 22:37:32 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=FYyvqKjlWX7xps3XjRgMbzJvJgA0ZiizgpisFoWjiC8=; b=YGPPMKlcvLZzTp bd9GMybh9Qo1cwogVgWsbNgC4jQGbgFNN9fPd9B0YeNag+Qi0gABusy8pzXOmwuLBesS7a4G874qM ZhKX5TmFJAGjzOyu3GqDExgXK5k4AX3N+CpNVfymzGytw+iYSiTappT/yq211or2+Iu2+jcDpZsYt 371w2bOHkXrMj9VSOHff5Q1UIkdXAs6Cy1ApAHBItPEsV2F0/DAgiGeYqJYLZDG9pHAaqvfbUssnB HPesVSCypYT/es3WkhVAjxHegZTD1rrUOnDK+uaH0gjLl+BzVGEVIYMf+jZloZiy3pbHI9UlnC4QR bUkodik9WlroV2Z+OwNg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQW-006WYP-1Q; Wed, 19 Apr 2023 22:37:24 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQU-006WXV-1P for linux-riscv@bombadil.infradead.org; Wed, 19 Apr 2023 22:37:22 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=7+zj7OnzXIW11i/huBWKMb2xqgovx7dUnUv6Tzb0tqU=; b=Lx7fD1vuM2h/gXJEBfsZVuRIJZ +P1cilVW+za9e09okgrFea68p+a5Gw9IjAU2y6eahur+jqkpHnmPDivWVvXMQGznYrUVDq9zX/UvK 5f8/l+s+IEb7+utbjZmwchpWebe/hKyYfUddIR63aIXQCd9EA3/hW+DXQM7EOib2UUdksLBftrjz/ 69vF3TlbGKZHD7uCf1X7a/NpWOC8NbIaSgkZfvM6z9hthNNyVquDIokQLwut9DDW0yI0lm9526Gei fgRbudesnJ+imTFm7X/r1bXDQmASczMvteeUJouFAagDVQ9Ea2BZRmQRvvy728SmJ9IcaDPKjdjg6 TMSLT4tg==; Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by casper.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ppG8P-00De9l-Fs for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:46 +0000 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-1a66b9bd893so4667535ad.1 for ; Wed, 19 Apr 2023 15:18:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942721; x=1684534721; 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=7+zj7OnzXIW11i/huBWKMb2xqgovx7dUnUv6Tzb0tqU=; b=kKffrnrLJhT1F2qbpdEGRZ8TTdXO/ezDgOuiwNxMV5iPfOJqI4IDxFqpw9pAeQ5RyL txc+F1a0ERaLPbEf/eIAQ97QF+P0vW9KXEXQJVk4MAG0hZ5UHvM8iR+j9FWY80tzoRv/ i18leGEtafNSkE1v0+pd0Hfx2PDQbn2laJSCgiLJbhWUmlq3l8lS0EIHC495U1w/1Sbo lQtTZAz+qIu3/rAdq8eTemzM8D2U0sbpzmAeqpgkGt5b1+8A50H+erFvF/kq3fNWeSd/ Aaooy2SRdRuS8o6rl9qDP37Xbl9koaQ1p1+GFSXg8xMxaIolOUWU6ET8zb1xeekSFWHq oPvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942721; x=1684534721; 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=7+zj7OnzXIW11i/huBWKMb2xqgovx7dUnUv6Tzb0tqU=; b=j+7Olro2O7V1xrdJKSpqNnLCK3IaLNSoygQlGeytliLUuxnGEeIghPJoQLdV281vLM 2f0XhyLsF8Eis43xcuuIfA/bKPf0OBCsALI7T+A3OPEDWb1POcagbvEmB2+DGBwoyk/a 4FFK5bFM4ZgTL/Onxff+vh6rDvUx6tU4YpjP6EhG5oAtrw2UStbcJ88QBxnB7PXR6/vH 88KxBevpWRTIMTtXF5yyifBytephlDw2dyorCNFGEPbYqiyO3S1REhUSje+3rBsTl/cb Gxz0BpFIXGHNT4m7ruVtZ9CmfPOSKExL+Etf7jPxeIr0hWlr8BFfypTiUh3N86GBpdt5 8cUQ== X-Gm-Message-State: AAQBX9e66ikIwMCKNKzxmUfymqxPJU5ltcEYvftdIZ3ZNLhmQsn8L8n+ U0rzUY31dj2uOxMge7g3xihum07n8wIzXniorik= X-Google-Smtp-Source: AKy350YAhdUYSn6knSbdmrvNKc2nBR1+eLVyT4UOeUtVo2MC/0v8WULeVwQQVk7jPKicbrdK5dLY6w== X-Received: by 2002:a17:902:a58a:b0:1a5:153f:2442 with SMTP id az10-20020a170902a58a00b001a5153f2442mr6421815plb.52.1681942720765; Wed, 19 Apr 2023 15:18:40 -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.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:39 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Rajnesh Kanwal , Alexandre Ghiti , Andrew Jones , Andrew Morton , Anup Patel , Atish Patra , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , 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 , Uladzislau Rezki Subject: [RFC 32/48] RISC-V: KVM: Add AIA support for TVMs Date: Wed, 19 Apr 2023 15:17:00 -0700 Message-Id: <20230419221716.3603068-33-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_231841_612553_15B72614 X-CRM114-Status: GOOD ( 30.36 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org The AIA support for TVMs are split between the host and the TSM. While the host allocates the vsfile, the TSM controls the gstage mapping and any updates to it. The host must not be able to inject interrupt to a TVM. Thus, the interrupt injection has to happen via the TSM only for the interrupts allowed by the guest. The swfile maintained by the host is not useful for the TVMs as well as the TVMs only work for HW_ACCEL mode. The TSM does maintain a swfile for the vcpu internally. The swfile allocation in the host is kept as is to avoid further bifurcation of the code. Co-developed-by: Rajnesh Kanwal Signed-off-by: Rajnesh Kanwal Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_cove.h | 6 +- arch/riscv/kvm/aia.c | 84 +++++++++++++++++--- arch/riscv/kvm/aia_device.c | 41 +++++++--- arch/riscv/kvm/aia_imsic.c | 127 +++++++++++++++++++++--------- 4 files changed, 195 insertions(+), 63 deletions(-) diff --git a/arch/riscv/include/asm/kvm_cove.h b/arch/riscv/include/asm/kvm_cove.h index 74bad2f..4367281 100644 --- a/arch/riscv/include/asm/kvm_cove.h +++ b/arch/riscv/include/asm/kvm_cove.h @@ -61,7 +61,7 @@ struct kvm_riscv_cove_page { unsigned long gpa; }; -struct imsic_tee_state { +struct imsic_cove_state { bool bind_required; bool bound; int vsfile_hgei; @@ -73,7 +73,7 @@ struct kvm_cove_tvm_vcpu_context { struct kvm_riscv_cove_page vcpu_state; /* Per VCPU imsic state */ - struct imsic_tee_state imsic; + struct imsic_cove_state imsic; }; struct kvm_cove_tvm_context { @@ -181,7 +181,7 @@ static inline int kvm_riscv_cove_vm_measure_pages(struct kvm *kvm, } static inline int kvm_riscv_cove_gstage_map(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned long hva) {return -1; } -/* AIA related TEE functions */ +/* TVM interrupt managenet via AIA functions */ static inline int kvm_riscv_cove_aia_init(struct kvm *kvm) { return -1; } static inline int kvm_riscv_cove_vcpu_inject_interrupt(struct kvm_vcpu *vcpu, unsigned long iid) { return -1; } diff --git a/arch/riscv/kvm/aia.c b/arch/riscv/kvm/aia.c index e3da661..88b91b5 100644 --- a/arch/riscv/kvm/aia.c +++ b/arch/riscv/kvm/aia.c @@ -20,6 +20,8 @@ struct aia_hgei_control { raw_spinlock_t lock; unsigned long free_bitmap; + /* Tracks if a hgei is converted to confidential mode */ + unsigned long nconf_bitmap; struct kvm_vcpu *owners[BITS_PER_LONG]; }; static DEFINE_PER_CPU(struct aia_hgei_control, aia_hgei); @@ -391,34 +393,96 @@ int kvm_riscv_vcpu_aia_rmw_ireg(struct kvm_vcpu *vcpu, unsigned int csr_num, int kvm_riscv_aia_alloc_hgei(int cpu, struct kvm_vcpu *owner, void __iomem **hgei_va, phys_addr_t *hgei_pa) { - int ret = -ENOENT; - unsigned long flags; + int ret = -ENOENT, rc; + bool reclaim_needed = false; + unsigned long flags, tmp_bitmap; const struct imsic_local_config *lc; struct aia_hgei_control *hgctrl = per_cpu_ptr(&aia_hgei, cpu); + phys_addr_t imsic_hgei_pa; if (!kvm_riscv_aia_available()) return -ENODEV; if (!hgctrl) return -ENODEV; + lc = imsic_get_local_config(cpu); raw_spin_lock_irqsave(&hgctrl->lock, flags); - if (hgctrl->free_bitmap) { - ret = __ffs(hgctrl->free_bitmap); - hgctrl->free_bitmap &= ~BIT(ret); - hgctrl->owners[ret] = owner; + if (!hgctrl->free_bitmap) { + raw_spin_unlock_irqrestore(&hgctrl->lock, flags); + goto done; + } + + if (!is_cove_vcpu(owner)) { + /* Find a free one that is not converted */ + tmp_bitmap = hgctrl->free_bitmap & hgctrl->nconf_bitmap; + if (tmp_bitmap > 0) + ret = __ffs(tmp_bitmap); + else { + /* All free ones have been converted in the past. Reclaim one now */ + ret = __ffs(hgctrl->free_bitmap); + reclaim_needed = true; + } + } else { + /* First try to find a free one that is already converted */ + tmp_bitmap = hgctrl->free_bitmap & !hgctrl->nconf_bitmap; + if (tmp_bitmap > 0) + ret = __ffs(tmp_bitmap); + else + ret = __ffs(hgctrl->free_bitmap); } + hgctrl->free_bitmap &= ~BIT(ret); + hgctrl->owners[ret] = owner; raw_spin_unlock_irqrestore(&hgctrl->lock, flags); - lc = imsic_get_local_config(cpu); if (lc && ret > 0) { if (hgei_va) *hgei_va = lc->msi_va + (ret * IMSIC_MMIO_PAGE_SZ); - if (hgei_pa) - *hgei_pa = lc->msi_pa + (ret * IMSIC_MMIO_PAGE_SZ); + imsic_hgei_pa = lc->msi_pa + (ret * IMSIC_MMIO_PAGE_SZ); + + if (reclaim_needed) { + rc = kvm_riscv_cove_aia_claim_imsic(owner, imsic_hgei_pa); + if (rc) { + kvm_err("Reclaim of imsic pa %pa failed for vcpu %d pcpu %d ret %d\n", + &imsic_hgei_pa, owner->vcpu_idx, smp_processor_id(), ret); + kvm_riscv_aia_free_hgei(cpu, ret); + return rc; + } + } + + /* + * Clear the free_bitmap here instead in case relcaim was necessary. + * Do it here instead of above because it we should only set the nconf + * bitmap after the claim is successful. + */ + raw_spin_lock_irqsave(&hgctrl->lock, flags); + if (reclaim_needed) + set_bit(ret, &hgctrl->nconf_bitmap); + raw_spin_unlock_irqrestore(&hgctrl->lock, flags); + + if (is_cove_vcpu(owner) && test_bit(ret, &hgctrl->nconf_bitmap)) { + /* + * Convert the address to confidential mode. + * This may need to send IPIs to issue global fence. Hence, + * enable interrupts temporarily for irq processing + */ + rc = kvm_riscv_cove_aia_convert_imsic(owner, imsic_hgei_pa); + + if (rc) { + kvm_riscv_aia_free_hgei(cpu, ret); + ret = rc; + } else { + raw_spin_lock_irqsave(&hgctrl->lock, flags); + clear_bit(ret, &hgctrl->nconf_bitmap); + raw_spin_unlock_irqrestore(&hgctrl->lock, flags); + } + } } + if (hgei_pa) + *hgei_pa = imsic_hgei_pa; +done: return ret; } @@ -495,6 +559,8 @@ static int aia_hgei_init(void) hgctrl->free_bitmap &= ~BIT(0); } else hgctrl->free_bitmap = 0; + /* By default all vsfiles are to be used for non-confidential mode */ + hgctrl->nconf_bitmap = hgctrl->free_bitmap; } /* Find INTC irq domain */ diff --git a/arch/riscv/kvm/aia_device.c b/arch/riscv/kvm/aia_device.c index 3556e82..ecf6734 100644 --- a/arch/riscv/kvm/aia_device.c +++ b/arch/riscv/kvm/aia_device.c @@ -11,6 +11,7 @@ #include #include #include +#include static void unlock_vcpus(struct kvm *kvm, int vcpu_lock_idx) { @@ -103,6 +104,10 @@ static int aia_config(struct kvm *kvm, unsigned long type, default: return -EINVAL; }; + /* TVM must have a physical vs file */ + if (is_cove_vm(kvm) && *nr != KVM_DEV_RISCV_AIA_MODE_HWACCEL) + return -EINVAL; + aia->mode = *nr; } else *nr = aia->mode; @@ -264,18 +269,24 @@ static int aia_init(struct kvm *kvm) if (kvm->created_vcpus != atomic_read(&kvm->online_vcpus)) return -EBUSY; - /* Number of sources should be less than or equals number of IDs */ - if (aia->nr_ids < aia->nr_sources) - return -EINVAL; + if (!is_cove_vm(kvm)) { + /* Number of sources should be less than or equals number of IDs */ + if (aia->nr_ids < aia->nr_sources) + return -EINVAL; + /* APLIC base is required for non-zero number of sources only for non TVMs*/ + if (aia->nr_sources && aia->aplic_addr == KVM_RISCV_AIA_UNDEF_ADDR) + return -EINVAL; - /* APLIC base is required for non-zero number of sources */ - if (aia->nr_sources && aia->aplic_addr == KVM_RISCV_AIA_UNDEF_ADDR) - return -EINVAL; + /* Initialize APLIC */ + ret = kvm_riscv_aia_aplic_init(kvm); + if (ret) + return ret; - /* Initialze APLIC */ - ret = kvm_riscv_aia_aplic_init(kvm); - if (ret) - return ret; + } else { + ret = kvm_riscv_cove_aia_init(kvm); + if (ret) + return ret; + } /* Iterate over each VCPU */ kvm_for_each_vcpu(idx, vcpu, kvm) { @@ -650,8 +661,14 @@ void kvm_riscv_aia_init_vm(struct kvm *kvm) */ /* Initialize default values in AIA global context */ - aia->mode = (kvm_riscv_aia_nr_hgei) ? - KVM_DEV_RISCV_AIA_MODE_AUTO : KVM_DEV_RISCV_AIA_MODE_EMUL; + if (is_cove_vm(kvm)) { + if (!kvm_riscv_aia_nr_hgei) + return; + aia->mode = KVM_DEV_RISCV_AIA_MODE_HWACCEL; + } else { + aia->mode = (kvm_riscv_aia_nr_hgei) ? + KVM_DEV_RISCV_AIA_MODE_AUTO : KVM_DEV_RISCV_AIA_MODE_EMUL; + } aia->nr_ids = kvm_riscv_aia_max_ids - 1; aia->nr_sources = 0; aia->nr_group_bits = 0; diff --git a/arch/riscv/kvm/aia_imsic.c b/arch/riscv/kvm/aia_imsic.c index 419c98d..8db1e29 100644 --- a/arch/riscv/kvm/aia_imsic.c +++ b/arch/riscv/kvm/aia_imsic.c @@ -15,6 +15,7 @@ #include #include #include +#include #define IMSIC_MAX_EIX (IMSIC_MAX_ID / BITS_PER_TYPE(u64)) @@ -583,7 +584,7 @@ static void imsic_vsfile_local_update(int vsfile_hgei, u32 nr_eix, csr_write(CSR_VSISELECT, old_vsiselect); } -static void imsic_vsfile_cleanup(struct imsic *imsic) +static void imsic_vsfile_cleanup(struct kvm_vcpu *vcpu, struct imsic *imsic) { int old_vsfile_hgei, old_vsfile_cpu; unsigned long flags; @@ -604,8 +605,12 @@ static void imsic_vsfile_cleanup(struct imsic *imsic) memset(imsic->swfile, 0, sizeof(*imsic->swfile)); - if (old_vsfile_cpu >= 0) + if (old_vsfile_cpu >= 0) { + if (is_cove_vcpu(vcpu)) + kvm_riscv_cove_vcpu_imsic_unbind(vcpu, old_vsfile_cpu); + kvm_riscv_aia_free_hgei(old_vsfile_cpu, old_vsfile_hgei); + } } static void imsic_swfile_extirq_update(struct kvm_vcpu *vcpu) @@ -688,27 +693,30 @@ void kvm_riscv_vcpu_aia_imsic_release(struct kvm_vcpu *vcpu) * the old IMSIC VS-file so we first re-direct all interrupt * producers. */ + if (!is_cove_vcpu(vcpu)) { + /* Purge the G-stage mapping */ + kvm_riscv_gstage_iounmap(vcpu->kvm, + vcpu->arch.aia_context.imsic_addr, + IMSIC_MMIO_PAGE_SZ); - /* Purge the G-stage mapping */ - kvm_riscv_gstage_iounmap(vcpu->kvm, - vcpu->arch.aia_context.imsic_addr, - IMSIC_MMIO_PAGE_SZ); - - /* TODO: Purge the IOMMU mapping ??? */ + /* TODO: Purge the IOMMU mapping ??? */ - /* - * At this point, all interrupt producers have been re-directed - * to somewhere else so we move register state from the old IMSIC - * VS-file to the IMSIC SW-file. - */ + /* + * At this point, all interrupt producers have been re-directed + * to somewhere else so we move register state from the old IMSIC + * VS-file to the IMSIC SW-file. + */ - /* Read and clear register state from old IMSIC VS-file */ - memset(&tmrif, 0, sizeof(tmrif)); - imsic_vsfile_read(old_vsfile_hgei, old_vsfile_cpu, imsic->nr_hw_eix, - true, &tmrif); + /* Read and clear register state from old IMSIC VS-file */ + memset(&tmrif, 0, sizeof(tmrif)); + imsic_vsfile_read(old_vsfile_hgei, old_vsfile_cpu, imsic->nr_hw_eix, + true, &tmrif); - /* Update register state in IMSIC SW-file */ - imsic_swfile_update(vcpu, &tmrif); + /* Update register state in IMSIC SW-file */ + imsic_swfile_update(vcpu, &tmrif); + } else { + kvm_riscv_cove_vcpu_imsic_unbind(vcpu, old_vsfile_cpu); + } /* Free-up old IMSIC VS-file */ kvm_riscv_aia_free_hgei(old_vsfile_cpu, old_vsfile_hgei); @@ -747,7 +755,7 @@ int kvm_riscv_vcpu_aia_imsic_update(struct kvm_vcpu *vcpu) /* For HW acceleration mode, we can't continue */ if (kvm->arch.aia.mode == KVM_DEV_RISCV_AIA_MODE_HWACCEL) { run->fail_entry.hardware_entry_failure_reason = - CSR_HSTATUS; + KVM_EXIT_FAIL_ENTRY_IMSIC_FILE_UNAVAILABLE; run->fail_entry.cpu = vcpu->cpu; run->exit_reason = KVM_EXIT_FAIL_ENTRY; return 0; @@ -762,22 +770,24 @@ int kvm_riscv_vcpu_aia_imsic_update(struct kvm_vcpu *vcpu) } new_vsfile_hgei = ret; - /* - * At this point, all interrupt producers are still using - * to the old IMSIC VS-file so we first move all interrupt - * producers to the new IMSIC VS-file. - */ - - /* Zero-out new IMSIC VS-file */ - imsic_vsfile_local_clear(new_vsfile_hgei, imsic->nr_hw_eix); - - /* Update G-stage mapping for the new IMSIC VS-file */ - ret = kvm_riscv_gstage_ioremap(kvm, vcpu->arch.aia_context.imsic_addr, - new_vsfile_pa, IMSIC_MMIO_PAGE_SZ, - true, true); - if (ret) - goto fail_free_vsfile_hgei; - + /* TSM only maintains the gstage mapping. Skip vsfile updates & ioremap */ + if (!is_cove_vcpu(vcpu)) { + /* + * At this point, all interrupt producers are still using + * to the old IMSIC VS-file so we first move all interrupt + * producers to the new IMSIC VS-file. + */ + + /* Zero-out new IMSIC VS-file */ + imsic_vsfile_local_clear(new_vsfile_hgei, imsic->nr_hw_eix); + + /* Update G-stage mapping for the new IMSIC VS-file */ + ret = kvm_riscv_gstage_ioremap(kvm, vcpu->arch.aia_context.imsic_addr, + new_vsfile_pa, IMSIC_MMIO_PAGE_SZ, + true, true); + if (ret) + goto fail_free_vsfile_hgei; + } /* TODO: Update the IOMMU mapping ??? */ /* Update new IMSIC VS-file details in IMSIC context */ @@ -788,12 +798,32 @@ int kvm_riscv_vcpu_aia_imsic_update(struct kvm_vcpu *vcpu) imsic->vsfile_pa = new_vsfile_pa; write_unlock_irqrestore(&imsic->vsfile_lock, flags); + /* Now bind the new vsfile for the TVMs */ + if (is_cove_vcpu(vcpu) && vcpu->arch.tc) { + vcpu->arch.tc->imsic.vsfile_hgei = new_vsfile_hgei; + if (old_vsfile_cpu >= 0) { + if (vcpu->arch.tc->imsic.bound) { + ret = kvm_riscv_cove_vcpu_imsic_rebind(vcpu, old_vsfile_cpu); + if (ret) { + kvm_err("imsic rebind failed for vcpu %d ret %d\n", + vcpu->vcpu_idx, ret); + goto fail_free_vsfile_hgei; + } + } + kvm_riscv_aia_free_hgei(old_vsfile_cpu, old_vsfile_hgei); + } else { + /* Bind if it is not a migration case */ + vcpu->arch.tc->imsic.bind_required = true; + } + /* Skip the oldvsfile and swfile update process as it is managed by TSM */ + goto done; + } + /* * At this point, all interrupt producers have been moved * to the new IMSIC VS-file so we move register state from * the old IMSIC VS/SW-file to the new IMSIC VS-file. */ - memset(&tmrif, 0, sizeof(tmrif)); if (old_vsfile_cpu >= 0) { /* Read and clear register state from old IMSIC VS-file */ @@ -946,6 +976,7 @@ int kvm_riscv_vcpu_aia_imsic_inject(struct kvm_vcpu *vcpu, unsigned long flags; struct imsic_mrif_eix *eix; struct imsic *imsic = vcpu->arch.aia_context.imsic_state; + int ret; /* We only emulate one IMSIC MMIO page for each Guest VCPU */ if (!imsic || !iid || guest_index || @@ -960,7 +991,14 @@ int kvm_riscv_vcpu_aia_imsic_inject(struct kvm_vcpu *vcpu, read_lock_irqsave(&imsic->vsfile_lock, flags); if (imsic->vsfile_cpu >= 0) { - writel(iid, imsic->vsfile_va + IMSIC_MMIO_SETIPNUM_LE); + /* TSM can only inject the external interrupt if it is allowed by the guest */ + if (is_cove_vcpu(vcpu)) { + ret = kvm_riscv_cove_vcpu_inject_interrupt(vcpu, iid); + if (ret) + kvm_err("External interrupt %d injection failed\n", iid); + } else { + writel(iid, imsic->vsfile_va + IMSIC_MMIO_SETIPNUM_LE); + } kvm_vcpu_kick(vcpu); } else { eix = &imsic->swfile->eix[iid / BITS_PER_TYPE(u64)]; @@ -1039,6 +1077,17 @@ int kvm_riscv_vcpu_aia_imsic_init(struct kvm_vcpu *vcpu) imsic->swfile = page_to_virt(swfile_page); imsic->swfile_pa = page_to_phys(swfile_page); + /* No need to setup iodev ops for TVMs. Swfile will also not be used for + * TVMs. However, allocate it for now as to avoid different path during + * free. + */ + if (is_cove_vcpu(vcpu)) { + ret = kvm_riscv_cove_vcpu_imsic_addr(vcpu); + if (ret) + goto fail_free_swfile; + return 0; + } + /* Setup IO device */ kvm_iodevice_init(&imsic->iodev, &imsic_iodoev_ops); mutex_lock(&kvm->slots_lock); @@ -1069,7 +1118,7 @@ void kvm_riscv_vcpu_aia_imsic_cleanup(struct kvm_vcpu *vcpu) if (!imsic) return; - imsic_vsfile_cleanup(imsic); + imsic_vsfile_cleanup(vcpu, imsic); mutex_lock(&kvm->slots_lock); kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &imsic->iodev); From patchwork Wed Apr 19 22:17:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217680 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 52EA9C77B7F for ; Wed, 19 Apr 2023 23:31:02 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=YEjE6taHPy8Wr14O02gnbjXmv0/QA7rEP2pBVZlpOIw=; b=wZU2hwWFcn3V9Q I4y/t0zv9F2Htp9h+Dks+M48PULRv+4Vl3v1RX5wAESaolbzdexVwvn/J5NW7ScmGcbUIZp/M+JPw Y5kCMSzpV2sahUmdn41ivLrzKXOVH0hpufX7VGkOp2UmWjifDzDENptOymB8wxSvGSG6J7GGmP3vD t/lhPWKdN0sHtRCrpeys7zkgINHXvwNsbpUZS6UtUxNnfR2I598cE/D8hjYre6mfNqIOfKZRriGmW YtiIXOy9shNi+ZVLuVLjEslDOSVUE3NeBbUkLa/lQawd/eR96r+KehrmYZJzLoY63IpYO0uTvKFBV l4yXCO04wFld6bRy2xLQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppHG9-006eYo-1I; Wed, 19 Apr 2023 23:30:45 +0000 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG8R-006TQD-1G for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:44 +0000 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1a920d4842bso4244125ad.2 for ; Wed, 19 Apr 2023 15:18:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942723; x=1684534723; 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=CQCEa95xde61G0+g1spWAS5gSK9slN4MB0K1b9QrdE8=; b=Ynw03ruOG3PNfgG00HUUd7HJCh15VY3aEZKHBX+JvWIZqAaPdtZJFMNC9sQ+7tdyNf gQij1krIXgqHn6c3FKneR90d+ObOiz01SLEvx89Co8mtqMDidl42AKlK9b8IeRrjhkVj wFMRWmBw0WyiUKSEW2tAX/U5AjDnycCzdvc7/5XQe2CDXdqSpATkNcRDmxBB2iAZSbfO Qe8Etb6nnDF5lWA9UfVwL0EYoHqQHL5jKdNdyJEbe8jXSsaw1PcWzy6qONpwtJLTmV1a iWDDSsLQLNxRbxPSgzzxgbkJX9sxpOs3jDYDbGE18ziPonHZ9Lw5ZRGoMLwBsGOpngcC aTQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942723; x=1684534723; 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=CQCEa95xde61G0+g1spWAS5gSK9slN4MB0K1b9QrdE8=; b=Vi9xwkYG2MjT+IOoZGiaFqeHtFzX7trcImTle00uaCgbHlFHndp1XiAUM5hq14KLWT e30kg//iQkqyOg5WxqKADSt+ROsCGOc2IauySuyQVtEiwaNzMJs/XMc+ZOPrcasFt3BH 5Zo312kqOzUrr/9x14PkyrmdbILeDhvVZ+tUWrlu9DYeeN4AnBy8HRjHfMTz/mjeNi3z g9y8Nx9b7ZdiNahKyeke7FJMaACO9b3r4FLx5cI4PLlg8Pjs007hLIgIpzrO0luiOgyh CRVo3kchVFebPj81S3m6Q2OwcE9rKiA7zeTgPhh75w2yLMhn60d+A8cMUPO6R0DSKIu9 Y1pg== X-Gm-Message-State: AAQBX9cHkGmqWpcE5YadB7xB3Jm3C2RqwZGRM2JH9864LTodT0MvY900 zlGXvLRJ07LO9eCLuwgrjczOmg== X-Google-Smtp-Source: AKy350a8Ym8XHInuF0mFeXBc2+oA8kajjJCNeYyHi0F4HD8cqx1mngs6rpkeTwZ7fiAYwBWCbSvEDg== X-Received: by 2002:a17:902:ba89:b0:1a9:2693:2e29 with SMTP id k9-20020a170902ba8900b001a926932e29mr1536823pls.42.1681942722916; Wed, 19 Apr 2023 15:18:42 -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.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:42 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 33/48] RISC-V: KVM: Hookup TVM VCPU init/destroy Date: Wed, 19 Apr 2023 15:17:01 -0700 Message-Id: <20230419221716.3603068-34-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151843_426885_DB904D03 X-CRM114-Status: GOOD ( 11.85 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org The TVM VCPU create function requires the vcpu id which is generated after the arch_create_vcpu returns. Thus, TVM vcpu init can not be invoked from the arch_create_vcpu. Invoke it in post create for now. However, post create doesn't return any error which is problematic as vcpu creation can fail from TSM side. Signed-off-by: Atish Patra --- arch/riscv/kvm/vcpu.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index 65f87e1..005c7c9 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -218,6 +218,17 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) { + int rc; + /* + * TODO: Ideally it should be invoked in vcpu_create. but vcpu_idx + * is allocated after returning create_vcpu. Find a better place to do it + */ + if (unlikely(is_cove_vcpu(vcpu))) { + rc = kvm_riscv_cove_vcpu_init(vcpu); + if (rc) + pr_err("%s: cove vcpu init failed %d\n", __func__, rc); + } + /** * vcpu with id 0 is the designated boot cpu. * Keep all vcpus with non-zero id in power-off state so that @@ -237,6 +248,9 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) kvm_riscv_vcpu_pmu_deinit(vcpu); + if (unlikely(is_cove_vcpu(vcpu))) + kvm_riscv_cove_vcpu_destroy(vcpu); + /* Free unused pages pre-allocated for G-stage page table mappings */ kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache); } From patchwork Wed Apr 19 22:17:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217671 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 CFECAC77B7E for ; Wed, 19 Apr 2023 23:30:57 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Pvc3oxKfVz3JF63OspYCfSBi2+qeISA7xGUDkHSBOuw=; b=2lDeM0xvNWy3Wy Q2UDdjvpsWcFn2v4o0DECNx9ULHYzVchWDBGS8kzgH+1MTlA3iufhsbR7rJKFmZcoHGWcw7/nEe/Y GWLouYGS6F5q57lEnZFsS6lWlaE2BuMrtmo+6K3iMQ2HM8RO6XTUSE7FWc500kB/a4dPfFaj6Tgl5 qx3dqu3kcauQY1a2oGk2/GQoy32jhILXsjnbN0RqR30G5m4aKKHJVybAoSktBkj6n4u57K1liP7Xj u8SeMlAgCtvCn2AITmTJN6D1tMxbSYGqLLclf37tPymNvDI4K911EI5nZVUwGGshs2R2qszBWJkAq c+STf8JV7w21HRl+yoRw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppHGA-006eaQ-1r; Wed, 19 Apr 2023 23:30:46 +0000 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG8T-006TRq-1z for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:47 +0000 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-1a6ebc66ca4so4213645ad.3 for ; Wed, 19 Apr 2023 15:18:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942725; x=1684534725; 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=V944izAfHfvPXt2SodYy8axfAv3FUhh/YjA49qCII3Y=; b=WToiBYM90+kXSHR4f4lcMC2do7CnNE609AkBJ9ILRsQSAqLQhvGE4fka9RZc7XkmOV nVSgbPgR0g26+R9aOz7prZ0ZYw39dV9KxvmHBGDvRgR8MR74QfL1ZHIt8XSWqAKcLN8r 0f6Tq006zXgK2g4OuuillctnBCZLY1xVWuRZMqYQdfd3ybKvutxZLqu19QqqHmXC8ODS KoYa4jRQnyk9O92MghnQdSKIAU/AWL4fTpgXKATBvPLDFP59BwJ0oeGTqTbRTShwnDj2 1+Ojob0S1CfD+/Tznk4BIbXVZIp9qmP+FcBhkm6vxxe63VSq5U1n2NJ31boDb5/Auf9y qI8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942725; x=1684534725; 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=V944izAfHfvPXt2SodYy8axfAv3FUhh/YjA49qCII3Y=; b=dET+de0cU07gElFVNDtbh+mcefv5RSPiCE3hR2XDpLMoGwj2wsqr3+8IapBx82rGrh lwOTvXlMcJ1JL9zJVFxR5KWqLzzZl6UWEKXxedntNM2fcpeRmLt+JGWeubBBDtrHD9sE o5Q5VlzuCX/G1ESTFLphtZYI/61ZlZ4l5zASaGzoT9osZXaMYaNFmDFeUTQnE/A89N4F Y2c5GFLlvHZgo2A2BDtEReI+A/lE3vNr1wYKcCRksbvrrCyDFSqET1tJX/t6Ai6KLyiP xiRYwfHRAEse9CoqLbnuKxCvrOJjV1fZxXl6czqCwuffi4qQydjPMuwAQ6CWcqQ12qhA D8uQ== X-Gm-Message-State: AAQBX9eZEBEnCnZ9TwPMDmxl/dPupWviBB+uqhdg+pxpxw3tJZLQzhwu 80kXXGaxsDC0mnau8LyTf7DKHg== X-Google-Smtp-Source: AKy350Z1qByMTGJhiqV3zdWqTg/zhHnUn1VfTimSG0DF0as79Rscca2Gz4ZKZ2NL/Pi2zbK61ibEFw== X-Received: by 2002:a17:903:2409:b0:19e:839e:49d8 with SMTP id e9-20020a170903240900b0019e839e49d8mr6224177plo.59.1681942725072; Wed, 19 Apr 2023 15:18:45 -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.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:44 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 34/48] RISC-V: KVM: Initialize CoVE Date: Wed, 19 Apr 2023 15:17:02 -0700 Message-Id: <20230419221716.3603068-35-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151845_656506_96A38FF3 X-CRM114-Status: GOOD ( 14.60 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org CoVE initialization depends on few underlying conditions that differs from normal VMs. 1. RFENCE extension is no longer mandatory as TEEH APIs has its own set of fence APIs. 2. SBI NACL is mandatory for TEE VMs to share memory between the host and the TSM. Signed-off-by: Atish Patra --- arch/riscv/kvm/main.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/arch/riscv/kvm/main.c b/arch/riscv/kvm/main.c index 842b78d..a059414 100644 --- a/arch/riscv/kvm/main.c +++ b/arch/riscv/kvm/main.c @@ -102,15 +102,12 @@ static int __init riscv_kvm_init(void) return -ENODEV; } - if (sbi_probe_extension(SBI_EXT_RFENCE) <= 0) { - kvm_info("require SBI RFENCE extension\n"); - return -ENODEV; - } - rc = kvm_riscv_nacl_init(); if (rc && rc != -ENODEV) return rc; + kvm_riscv_cove_init(); + kvm_riscv_gstage_mode_detect(); kvm_riscv_gstage_vmid_detect(); @@ -121,6 +118,15 @@ static int __init riscv_kvm_init(void) return rc; } + /* TVM don't need RFENCE extension as hardware imsic support is mandatory for TVMs + * TODO: This check should happen later if HW_ACCEL mode is not set as RFENCE + * should only be mandatory in that case. + */ + if (!kvm_riscv_cove_enabled() && sbi_probe_extension(SBI_EXT_RFENCE) <= 0) { + kvm_info("require SBI RFENCE extension\n"); + return -ENODEV; + } + kvm_info("hypervisor extension available\n"); if (kvm_riscv_nacl_available()) { From patchwork Wed Apr 19 22:17:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217599 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 75D6CC6FD18 for ; Wed, 19 Apr 2023 22:37:43 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=3S5twefMnVrL4r7U+J2/SyTAiQ8t5fMcA791sPqMZzU=; b=DSr0+OAzdyFU03 gIehHSxkpc7hPjsx/ltB2Cj0ogBzYNHmZDMhe4UKmcoP2EY6Xqry32ncDvBBr8YSggGAlMzfKyfuP 98dajG2KuSkJOe+uyKUwXwJmoVwiAcY0YCDLrTxmnk7Lwi9IvPBSUhQBOMLHJisrtdBnz8BCftAwi /XFU45TyeGSdodJU92Jeho8f2x3HlSH+mG9hypu9BOgKvmtXdRX7pUztl+tR+5KZT4oGdqE+xmHZz zRkWgZ9g1Wc8ZbMrJCYv8do3ARjYWjy+qoJedbgVJ9tgVKWdVi5EByKvEtne+YXJO6H3X424/MgL8 JpGMNQzh+2VHV+eF5gWQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQi-006Wjs-0i; Wed, 19 Apr 2023 22:37:36 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQc-006WXV-0q for linux-riscv@bombadil.infradead.org; Wed, 19 Apr 2023 22:37:30 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=PF8Qn0Z7zg/7RPDtBuNuOPN3zeJXhr/Jx4YP7Fj6yI4=; b=BN9ZOKvUXlBHpHYxGBzvEyGBnT HrHfiwYSYeQbAD57tatRJwYCojYS7DVwgYytdRQ7k8MNqtdzmtDj3jnoEi2ISgIHmf/K0AIp7O3kT LntGsJUTZl38EOg7fzVG96i5rl7pMurtx1qtImGRO5Z2wVfqYF+WPZGDOIn/qMA/jzdLZZE3DZZb+ dnFQeHUC8i4PKIjU+PRqDBXsfjnw1L7Cu3MdJ9jchy4ZAnGqsUslbuO5+1pdhDB7mrijjiZEaTzyS pnFUEwMhVVY5hj2c/QuZ2X+dcQKdxQHs+kSES3Pd0iGHrAREh/FEIoUumdqqDBCl5Wh8A9hboIaPD CQxWiFjw==; Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]) by casper.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ppG8V-00DeGj-W0 for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:49 +0000 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-63b73203e0aso2786774b3a.1 for ; Wed, 19 Apr 2023 15:18:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942727; x=1684534727; 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=PF8Qn0Z7zg/7RPDtBuNuOPN3zeJXhr/Jx4YP7Fj6yI4=; b=G/Fug0YDh/vT2gLC0JnMu9cK8/lm7JaHRK8wavACXkcusYj2LIYmBRBKMqHOqlZK+w 4X0VmEj6BTotc/ZSwQ/hqCf5XFjDLPfMNoRWYQ9eoQMvSpOlejZpx2S6qdCGkDr0mV1j tz/t8nrgfTSM3p2DcPJ6IP/ULX8p9u8n4bLZ8UppcJfyijITcktTN9QxUA/PYL/GUGR2 g1Dwy3qOR6S1EA5f1HDfH1/Z8AcgmbMd6OoD3Rpin7C3Tw7AepN0itfwNNwkxK4BS08T wI5vk6637oOkFsektud4GizoiS09rEJkFLg8xFSYn4+9QqroF/v5cNomyMNwCRJWPRAY /ruQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942727; x=1684534727; 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=PF8Qn0Z7zg/7RPDtBuNuOPN3zeJXhr/Jx4YP7Fj6yI4=; b=mD1TvfC9/qjwBcG9y0eoS4hMctr1mph3PKuMyjQt07i2L0lxOl3xZDeYGuIuw1Obvu tiwHz65xpvgaJr1vjhj+VOGQdcPsU2EPTXAc7YkdbYbTIU5GAjMBCPWf8+bYXZDniX+P Y+IlCxSrQzPjPECom3nOdtvp+C9uCZT9TV7VFlzGHtH1IS+oRX1W0mfgWrmxApwPOqde pPtPtEnZfDJOOHaqsKOXxNecw2Ms1gkva870ywZcAUXqQrdJDv7jSyUYZ8Tv16GJulf/ LGuwIfmgXK27395A+GbTOll3JIKtE3h30TUP4Btm2eoNZPm+TpyhzaP3P+OtDni0vT9X R+JQ== X-Gm-Message-State: AAQBX9fS26l+IykJUzQFdNAZqLif9FjNIisfjhe/ks6S6bV2eMLTMqGc poOElUV5n/yiNJPkSsBJLtdZpQ== X-Google-Smtp-Source: AKy350Zh1JoIu/w89g7BU4hvCJLzB5n1X2PHzMgJ8jQn7nK1FJDYYuKYpLhfuRupPsetmV7URaqafw== X-Received: by 2002:a17:902:ea03:b0:19b:64bb:d546 with SMTP id s3-20020a170902ea0300b0019b64bbd546mr4360345plg.18.1681942727339; Wed, 19 Apr 2023 15:18:47 -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.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:47 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 35/48] RISC-V: KVM: Add TVM init/destroy calls Date: Wed, 19 Apr 2023 15:17:03 -0700 Message-Id: <20230419221716.3603068-36-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_231848_052597_C94E46C2 X-CRM114-Status: GOOD ( 10.45 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org A TVM can only be created upon explicit request from the VMM via the vm type if CoVE SBI extensions must supported by the TSM. Signed-off-by: Atish Patra --- arch/riscv/kvm/vm.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/arch/riscv/kvm/vm.c b/arch/riscv/kvm/vm.c index 1b59a8f..8a1460d 100644 --- a/arch/riscv/kvm/vm.c +++ b/arch/riscv/kvm/vm.c @@ -42,6 +42,19 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) return r; } + if (unlikely(type == KVM_VM_TYPE_RISCV_COVE)) { + if (!kvm_riscv_cove_enabled()) { + kvm_err("Unable to init CoVE VM because cove is not enabled\n"); + return -EPERM; + } + + r = kvm_riscv_cove_vm_init(kvm); + if (r) + return r; + kvm->arch.vm_type = type; + kvm_info("Trusted VM instance init successful\n"); + } + kvm_riscv_aia_init_vm(kvm); kvm_riscv_guest_timer_init(kvm); @@ -54,6 +67,9 @@ void kvm_arch_destroy_vm(struct kvm *kvm) kvm_destroy_vcpus(kvm); kvm_riscv_aia_destroy_vm(kvm); + + if (unlikely(is_cove_vm(kvm))) + kvm_riscv_cove_vm_destroy(kvm); } int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irql, From patchwork Wed Apr 19 22:17:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217672 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 9C1E6C77B7C for ; Wed, 19 Apr 2023 23:30:57 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=b1eephXtMiVQPa1r6Fo6bSc/CAJhpP8sJG2/T3zZVVs=; b=ULoN4zvdNQRq+w bBIkspl/cxCgwkpV11xmSf+WjE6y4VcYTLivGOehChW0jegaFLlDDN72JgSz7iY95sjkC818yAixq mZg04hn73EKE2rMqcXn+HXEdJLxTzPRst+GQiBItjto567Qsf/tJ5yeimMuzQLQlzDAJlQm4xNFcH mlt1Hynf4UXfv70txAhiiCB87gkjSx4yrd1loU57XUncWS+uA2uiew7F8FpZ+7Fm98dxeEJvbCble Svz/V1AnzbDcYQHKVDcO93M7zLUBjoKi5MTdIayuJz3OqBVNQmxrWVahcjAXKz0k8QqJq3/V+Z5yd 0AHyNgjcrUU5QNAjRCVA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppHGB-006ebV-1S; Wed, 19 Apr 2023 23:30:47 +0000 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG8X-006TQC-2i for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:51 +0000 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-1a645fd0c6dso4319835ad.1 for ; Wed, 19 Apr 2023 15:18:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942729; x=1684534729; 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=XK5xUw/rzaPgA4jO4a/YwH8Byrp1ajuQRNW8VnU2lnI=; b=QfXuULt6GKdZ0yIYOBu58cROq1xtd+AwU5tA9tWSYcP8puXwvEN9RX3lXVuV60tyoW 4Q0CSfUoxv6NDjypNOuOT4ywNhU7IYB+qeWNkXwN91wiD9TQD3HOmit1hlHNqFbWVtNJ ziwh+QBq1N6MeIyCvSv5s6RR13cXyHzSqXBQKCnCZR2rrQanp3QhB3VbRYZ2jQSTPE6d Vpg87adIN6SFou4dfOJ3ZLRcLAQq2ZlEUcDyGi0vckdKvSSc1nPlKdGfKGwb9h5rUo3e rLKhVd1kgYj7FOEo7g+ojtFs7lAxzBaXut+YFsbxt/trii3p8w9AEE1pM3je3JF/e5ut 1yXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942729; x=1684534729; 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=XK5xUw/rzaPgA4jO4a/YwH8Byrp1ajuQRNW8VnU2lnI=; b=ka7r5aAOjm0MmXaxH5BTTfJr9/Sj6TzYoy+rc7tKsgRVkDk9QnAGZEPQGTmT/J0H5I UwSFS6ZVVORZdLNtD28jkNYLEIZzXWuoGzSNVjD8+KwxW88bLfWtRhTm0Qa4q2DxslJD +t0jmHkvILhZkqAJNxsGlKwNLjA6/6XcJGyHGMrB8PYnaIV7DSuBWt2UaV+Gs7ixUpQA z15RlIXIQPNf5g1yNzjdyGCL5SFDBVwZ+zhbtXrM8vpnm2lTjcXMGuWML6NFzOS1z9nr Ev7q5/SeKXoS3R1snl+3LOadryPWp4LvZGL7UyMfeEy/BDpDRI6VR26hOCU4IoJFTN4q Hc7Q== X-Gm-Message-State: AAQBX9eUCwuM6j+bu6aAdZhGTSbkACMeM0wB8CYc2jfXomPZoMNf8z8p HJ/v3pQzcWFlkjKhQGXGVIQTBA== X-Google-Smtp-Source: AKy350baQTNt4Vs4HHjBbXvyObvJ3IXFGZptf7h2H+OASSZVy5+7QV7+crZBWOtRSKw2ldJwBUdMWw== X-Received: by 2002:a17:903:784:b0:1a2:9183:a499 with SMTP id kn4-20020a170903078400b001a29183a499mr5853196plb.34.1681942729515; Wed, 19 Apr 2023 15:18:49 -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.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:49 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Rajnesh Kanwal , Atish Patra , Alexandre Ghiti , Andrew Jones , Andrew Morton , Anup Patel , Atish Patra , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , 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 , Uladzislau Rezki Subject: [RFC 36/48] RISC-V: KVM: Read/write gprs from/to shmem in case of TVM VCPU. Date: Wed, 19 Apr 2023 15:17:04 -0700 Message-Id: <20230419221716.3603068-37-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151849_892766_AACE03B9 X-CRM114-Status: GOOD ( 16.37 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Rajnesh Kanwal For TVM vcpus, TSM uses shared memory to exposes gprs for the trusted VCPU. This change makes sure we use shmem when doing mmio emulation for trusted VMs. Signed-off-by: Rajnesh Kanwal Signed-off-by: Atish Patra --- arch/riscv/kvm/vcpu_insn.c | 98 +++++++++++++++++++++++++++++++++----- 1 file changed, 85 insertions(+), 13 deletions(-) diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c index 331489f..56eeb86 100644 --- a/arch/riscv/kvm/vcpu_insn.c +++ b/arch/riscv/kvm/vcpu_insn.c @@ -7,6 +7,9 @@ #include #include #include +#include +#include +#include #define INSN_OPCODE_MASK 0x007c #define INSN_OPCODE_SHIFT 2 @@ -116,6 +119,10 @@ #define REG_OFFSET(insn, pos) \ (SHIFT_RIGHT((insn), (pos) - LOG_REGBYTES) & REG_MASK) +#define REG_INDEX(insn, pos) \ + ((SHIFT_RIGHT((insn), (pos)-LOG_REGBYTES) & REG_MASK) / \ + (__riscv_xlen / 8)) + #define REG_PTR(insn, pos, regs) \ ((ulong *)((ulong)(regs) + REG_OFFSET(insn, pos))) @@ -600,6 +607,7 @@ int kvm_riscv_vcpu_mmio_store(struct kvm_vcpu *vcpu, struct kvm_run *run, int len = 0, insn_len = 0; struct kvm_cpu_trap utrap = { 0 }; struct kvm_cpu_context *ct = &vcpu->arch.guest_context; + void *nshmem; /* Determine trapped instruction */ if (htinst & 0x1) { @@ -627,7 +635,15 @@ int kvm_riscv_vcpu_mmio_store(struct kvm_vcpu *vcpu, struct kvm_run *run, insn_len = INSN_LEN(insn); } - data = GET_RS2(insn, &vcpu->arch.guest_context); + if (is_cove_vcpu(vcpu)) { + nshmem = nacl_shmem(); + data = nacl_shmem_gpr_read_cove(nshmem, + REG_INDEX(insn, SH_RS2) * 8 + + KVM_ARCH_GUEST_ZERO); + } else { + data = GET_RS2(insn, &vcpu->arch.guest_context); + } + data8 = data16 = data32 = data64 = data; if ((insn & INSN_MASK_SW) == INSN_MATCH_SW) { @@ -643,19 +659,43 @@ int kvm_riscv_vcpu_mmio_store(struct kvm_vcpu *vcpu, struct kvm_run *run, #ifdef CONFIG_64BIT } else if ((insn & INSN_MASK_C_SD) == INSN_MATCH_C_SD) { len = 8; - data64 = GET_RS2S(insn, &vcpu->arch.guest_context); + if (is_cove_vcpu(vcpu)) { + data64 = nacl_shmem_gpr_read_cove( + nshmem, + RVC_RS2S(insn) * 8 + KVM_ARCH_GUEST_ZERO); + } else { + data64 = GET_RS2S(insn, &vcpu->arch.guest_context); + } } else if ((insn & INSN_MASK_C_SDSP) == INSN_MATCH_C_SDSP && ((insn >> SH_RD) & 0x1f)) { len = 8; - data64 = GET_RS2C(insn, &vcpu->arch.guest_context); + if (is_cove_vcpu(vcpu)) { + data64 = nacl_shmem_gpr_read_cove( + nshmem, REG_INDEX(insn, SH_RS2C) * 8 + + KVM_ARCH_GUEST_ZERO); + } else { + data64 = GET_RS2C(insn, &vcpu->arch.guest_context); + } #endif } else if ((insn & INSN_MASK_C_SW) == INSN_MATCH_C_SW) { len = 4; - data32 = GET_RS2S(insn, &vcpu->arch.guest_context); + if (is_cove_vcpu(vcpu)) { + data32 = nacl_shmem_gpr_read_cove( + nshmem, + RVC_RS2S(insn) * 8 + KVM_ARCH_GUEST_ZERO); + } else { + data32 = GET_RS2S(insn, &vcpu->arch.guest_context); + } } else if ((insn & INSN_MASK_C_SWSP) == INSN_MATCH_C_SWSP && ((insn >> SH_RD) & 0x1f)) { len = 4; - data32 = GET_RS2C(insn, &vcpu->arch.guest_context); + if (is_cove_vcpu(vcpu)) { + data32 = nacl_shmem_gpr_read_cove( + nshmem, REG_INDEX(insn, SH_RS2C) * 8 + + KVM_ARCH_GUEST_ZERO); + } else { + data32 = GET_RS2C(insn, &vcpu->arch.guest_context); + } } else { return -EOPNOTSUPP; } @@ -725,6 +765,7 @@ int kvm_riscv_vcpu_mmio_return(struct kvm_vcpu *vcpu, struct kvm_run *run) u64 data64; ulong insn; int len, shift; + void *nshmem; if (vcpu->arch.mmio_decode.return_handled) return 0; @@ -738,26 +779,57 @@ int kvm_riscv_vcpu_mmio_return(struct kvm_vcpu *vcpu, struct kvm_run *run) len = vcpu->arch.mmio_decode.len; shift = vcpu->arch.mmio_decode.shift; + if (is_cove_vcpu(vcpu)) + nshmem = nacl_shmem(); + switch (len) { case 1: data8 = *((u8 *)run->mmio.data); - SET_RD(insn, &vcpu->arch.guest_context, - (ulong)data8 << shift >> shift); + if (is_cove_vcpu(vcpu)) { + nacl_shmem_gpr_write_cove(nshmem, + REG_INDEX(insn, SH_RD) * 8 + + KVM_ARCH_GUEST_ZERO, + (unsigned long)data8); + } else { + SET_RD(insn, &vcpu->arch.guest_context, + (ulong)data8 << shift >> shift); + } break; case 2: data16 = *((u16 *)run->mmio.data); - SET_RD(insn, &vcpu->arch.guest_context, - (ulong)data16 << shift >> shift); + if (is_cove_vcpu(vcpu)) { + nacl_shmem_gpr_write_cove(nshmem, + REG_INDEX(insn, SH_RD) * 8 + + KVM_ARCH_GUEST_ZERO, + (unsigned long)data16); + } else { + SET_RD(insn, &vcpu->arch.guest_context, + (ulong)data16 << shift >> shift); + } break; case 4: data32 = *((u32 *)run->mmio.data); - SET_RD(insn, &vcpu->arch.guest_context, - (ulong)data32 << shift >> shift); + if (is_cove_vcpu(vcpu)) { + nacl_shmem_gpr_write_cove(nshmem, + REG_INDEX(insn, SH_RD) * 8 + + KVM_ARCH_GUEST_ZERO, + (unsigned long)data32); + } else { + SET_RD(insn, &vcpu->arch.guest_context, + (ulong)data32 << shift >> shift); + } break; case 8: data64 = *((u64 *)run->mmio.data); - SET_RD(insn, &vcpu->arch.guest_context, - (ulong)data64 << shift >> shift); + if (is_cove_vcpu(vcpu)) { + nacl_shmem_gpr_write_cove(nshmem, + REG_INDEX(insn, SH_RD) * 8 + + KVM_ARCH_GUEST_ZERO, + (unsigned long)data64); + } else { + SET_RD(insn, &vcpu->arch.guest_context, + (ulong)data64 << shift >> shift); + } break; default: return -EOPNOTSUPP; From patchwork Wed Apr 19 22:17:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217598 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 13EF5C77B73 for ; Wed, 19 Apr 2023 22:37:37 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=tv9ZRV9lin6TEEDiRfqX+Dz+dBEC/tWfpoEUBH27A8s=; b=ltvxofTe9pgw88 YszBOhwyN98GI4XTvXMSIBfv2/de/2qph1Pc6JsbGpB01p41Ds3q72RBYotGYPo5dUsDvmU8fPtYi UrG5jiwyJpp0FecVDESZGX9WMQnRP3Zf7B8gsWSKh6K+I1jEGKwc7ex7jaxqFosciWYqSopBdlrHH BV6RkepSvvukqNmDTTIEAHAXzrUBK+atN35XryzypreW+FMjfaXuqWh05EBFb1A3R0nhzT2Uc9tK0 6wcce+KtRJpG1QvKl3tb9o5g9n93a74ie4jBYBUnkayHVkksAlJf4iuJt/eFyzbBTNQr3X/eSBs3Y 6Sf4x0Q/C5M9tlBoSi/Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQc-006Wdz-2G; Wed, 19 Apr 2023 22:37:30 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQZ-006WXV-2h for linux-riscv@bombadil.infradead.org; Wed, 19 Apr 2023 22:37:27 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=3wZ38NgXR92ZnL3KCz4X1T1orXD86k7gku+jH9LYvlc=; b=sXWU2DYmm/all61rEO6UXVpE0s GG7AjfJe5emteqD8zCdFtXPSHkedkH678w2GV4Um4ykbQa9th4wDF/hQLYjQ9WnP2yZfuvyZ1rcqw p2eQme/HIbyKqyIm7j5sCkHfYT8JjbDRJGqAWmo8068Rki6KyPpqgak0erh8OzUeq5N9Nq6Kb6abw cwU12dtu/G3H9QLFYH592K3Ew+X7FeyehChAY1yPHdLn8/Kt2+9NLMs/HDM5dGejKpnxmUtNeqTKm Buvf9c7goPjRlqdC+JrmHAlODdppJi5h8pqRMUqeTqIz/8JnhliLqN8Y3Va1ZBmlRIYQBtnqi6Gis iZMHEmbQ==; Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by casper.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ppG8b-00DeM3-Tt for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:55 +0000 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1a8097c1ccfso5309395ad.1 for ; Wed, 19 Apr 2023 15:18:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942732; x=1684534732; 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=3wZ38NgXR92ZnL3KCz4X1T1orXD86k7gku+jH9LYvlc=; b=QmuuVYt46KpS/zYYMAaGwSu/4ogHzgebSuHz2Y3KqaXaWYNGDgVkh+TM+9F0z1UKqq yLwfXsEjIj5AyyA1hGl2bhxYnaVMKxeSrJ8kwz/jbSGPSXzXJrYc1cZLmnuKcOHvK/NO W+UYzU5cNYXd7iKLqbolbPZxHkvQvk9pg5qltXG2gdevWPNka1SD3vMUG51IVWwpm1Is pQl00SUeHSzQxwfQfWPPzCjONURk75DuuPNE8FRa7jDR7RGmxRIpfU2bo0VrGWZ8ZwD3 0HsjPnYDT4F6NmAip4Qb5wSlSawzKk38AMTj8xzmlCbhjRiSI7AJ2N04QWgrNDK8/C2A ee7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942732; x=1684534732; 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=3wZ38NgXR92ZnL3KCz4X1T1orXD86k7gku+jH9LYvlc=; b=Qbq5FP1o11HYmUjg5oI0neFrTIQDCZkanFRrWMZRsExUR9L4Ut4L3xG4cAZEJ913xI wAjVRnseYoxWCMuEz2qBs+EfNh8T5/m3iteX44DZDay4j+TMq5bi9fp1M5MsS236y+gv m5JJorebXgjP0Gx7Z6DQbyDKCp5qJgHBUSpibYF0ZqglqPwj/zD8jj4eNRJElM9WPreU +Lnx6nBtRVAcwMU0g5p1t8okypoY/OgKc/LJwQhIojPv06gYvg48U5m2AQpcRStaNdk/ t9zv/DLLAyJS2jf9OYGWQzNvVweKi2ZlHtrS32Mm3WR9WxhKvwp4GLkdtId1n9nnroyK aPHA== X-Gm-Message-State: AAQBX9d05zb8tKoRDuG9Zr6V4CQ2etfdrDagUcoO5+zvz5SwB6k6R+A5 80FpfNgl1yOwelD2+PBSPWyX8w== X-Google-Smtp-Source: AKy350YlbYAHX3czmRaEVnTcNdf891KQxoyoTa0cHJBez0RPMbydsH+Ponf0TcKud2fsWou26F4oLw== X-Received: by 2002:a17:903:2281:b0:1a6:3737:750d with SMTP id b1-20020a170903228100b001a63737750dmr7538499plh.17.1681942731698; Wed, 19 Apr 2023 15:18:51 -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.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:51 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Rajnesh Kanwal , Atish Patra , Alexandre Ghiti , Andrew Jones , Andrew Morton , Anup Patel , Atish Patra , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , 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 , Uladzislau Rezki Subject: [RFC 37/48] RISC-V: Add COVG SBI extension definitions Date: Wed, 19 Apr 2023 15:17:05 -0700 Message-Id: <20230419221716.3603068-38-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_231853_990192_4DCE2616 X-CRM114-Status: GOOD ( 10.66 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Rajnesh Kanwal CoVE specification defines a separate SBI extension known as CoVG for the guest side interface. Add the definitions for that extension. Signed-off-by: Rajnesh Kanwal Signed-off-by: Atish Patra --- arch/riscv/include/asm/sbi.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/arch/riscv/include/asm/sbi.h b/arch/riscv/include/asm/sbi.h index bbea922..e02ee75 100644 --- a/arch/riscv/include/asm/sbi.h +++ b/arch/riscv/include/asm/sbi.h @@ -34,6 +34,7 @@ enum sbi_ext_id { SBI_EXT_NACL = 0x4E41434C, SBI_EXT_COVH = 0x434F5648, SBI_EXT_COVI = 0x434F5649, + SBI_EXT_COVG = 0x434F5647, /* Experimentals extensions must lie within this range */ SBI_EXT_EXPERIMENTAL_START = 0x08000000, @@ -439,6 +440,16 @@ struct sbi_cove_tvm_aia_params { uint32_t guests_per_hart; }; +/* SBI COVG extension data structures */ +enum sbi_ext_covg_fid { + SBI_EXT_COVG_ADD_MMIO_REGION, + SBI_EXT_COVG_REMOVE_MMIO_REGION, + SBI_EXT_COVG_SHARE_MEMORY, + SBI_EXT_COVG_UNSHARE_MEMORY, + SBI_EXT_COVG_ALLOW_EXT_INTERRUPT, + SBI_EXT_COVG_DENY_EXT_INTERRUPT, +}; + #define SBI_SPEC_VERSION_DEFAULT 0x1 #define SBI_SPEC_VERSION_MAJOR_SHIFT 24 #define SBI_SPEC_VERSION_MAJOR_MASK 0x7f From patchwork Wed Apr 19 22:17:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217597 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 4EB43C6FD18 for ; Wed, 19 Apr 2023 22:37:36 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=dkyAp2wiApqpxYSOKdt8if6AOqVtzsutZ0s0c/xiLHc=; b=s6+o2hF3HzjmLg 85sLakNohO+/C40g4bQU8auxGicyh6MZXFfV/hgCiS/uWzDl58Ru9ESnhVbzhHS0PzhbQl55wjJUq Hv5bdOh06wbK2w9KBOzP7zEVxFCdIa+iC9bARoj8NsYzEs23CwR+K+GdHb2EXI4WItdb/7i0ePChD X4V/ZbzWTG8J7JGvnukN0PMuXZMEu8kIlNucfF0iXMLhobQIX0GJiMT/iQiRJyBm8ohrJaikpSRYX UBDHK2Y2Zdz/xWZPjXQUaU4GZ1N+Y63JxMTocjdNsGuNg2OGSX/noNyAk3+ysCpSAEq2cUd4nPXzo mjgTMfancIbSL4RYzsYw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQb-006Wct-1V; Wed, 19 Apr 2023 22:37:29 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQY-006WXV-2G for linux-riscv@bombadil.infradead.org; Wed, 19 Apr 2023 22:37:26 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=wI5JEqEayPS5t0DearmNB32dGieY2Rp85qRqyRU4954=; b=duerpfjtqTK/qclkuoznxgNhsj RsmKl6SLHGrUkRgV8Jf8/6sViV3AQwtnaRXfgJYG6KhYFZZiwAR42kpjXxA3Kk7qhjcClM311Eu14 Uu2J8Cd+oIxdylra4R18vHm91yoCw86adqKM338na30JlHcfChxJRnCCGEWWjiJr6BXXaRimkW8di bOdeaVjSuHucLUxHaRInKEzqP1sDDMDl7wTs13nAtH4WArdwMBpPow720LvAJPN82DEFe891g1a4c n4vGqWNX8NMnUBoTRPwbJoCFNyaiOJEKR+oGCIFT+PWSz8hENhvTW46tFJo3b0n5VnYfhSLoLJ4CJ hLCQ71DQ==; Received: from mail-pf1-x429.google.com ([2607:f8b0:4864:20::429]) by casper.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ppG8c-00DeGl-IK for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:57 +0000 Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-63d4595d60fso2916523b3a.0 for ; Wed, 19 Apr 2023 15:18:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942734; x=1684534734; 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=wI5JEqEayPS5t0DearmNB32dGieY2Rp85qRqyRU4954=; b=c61/p77dAtF7ITFaJtJ2ThSiNokGE55QUnDMhiyOWQfpvPZA4LwEm0dNNepuTh8/vI ETC0NUKHH0B7ioWMNIOCRl7AjZHuiWQCd9YRt/w8gX3X03F0aRKluujbAxJMyKQ218iz HyGwjvHTZ57PibWhtCDK2NRKMiLlH7ybE/Z877P0tsYOxOH43juir+xqANUqGBHB7/WR aQibBdjDGfMJsGQ2duTavnD7648VTHs+6zw4IzdBl5zGyzuHnyd08ri5ZAx79ZyGdHPQ +HVr9NAcGBE4GY7Q3b9IwiUkw1Xx2A9JUqYVhhUubWWiX//8s8ew3EPoW97k6emKoi+z u1mQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942734; x=1684534734; 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=wI5JEqEayPS5t0DearmNB32dGieY2Rp85qRqyRU4954=; b=kLV7O4Lxc+yQqG32CeInYLlnzXSARhXU03mPxr7OrKWbYSQvTUka1cru2C0R9Uj//8 l3zKnDwAaU9EPpgkI+/lTddKhZhJyWHKBVRKmyfaaIJmbnSMBPuzLh64wf9tZ68n5wm3 wRwuBxjq4hVrxRx7kTs8fS9TdQiaPnbB6z01zBB2QbwDLGkcaAx7in23VMOsJSBTiUK/ ITtoK3TfrAP4lH/kohN+Xd2W53ioF0wg50cTaDQEUYHmxfWXSu25Di5TG/ad8Dkwy6zf Jemn5jA+FEHvpwyDUBE7Hx4WPlhmnMdUu5VOdtpxyKzhXAekCSok0XwahgPXQ8ciwU3t /VJQ== X-Gm-Message-State: AAQBX9elILQ8Zp90jU+oPORBHkXjevlagqeQ7T8B2goT4RX1niA8ET1x FckmTQkEvNKN3ag1X4WQe5ydNw== X-Google-Smtp-Source: AKy350bHPqgey3c6g8HqkR9B9xlgHTWq+fR92XQNeVK1lT0G63M1RyCGum2kegw8LaA3eJtSnsnKXQ== X-Received: by 2002:a17:90a:6002:b0:246:865d:419a with SMTP id y2-20020a17090a600200b00246865d419amr3928528pji.6.1681942733866; Wed, 19 Apr 2023 15:18:53 -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.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:53 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Rajnesh Kanwal , Atish Patra , Alexandre Ghiti , Andrew Jones , Andrew Morton , Anup Patel , Atish Patra , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , 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 , Uladzislau Rezki Subject: [RFC 38/48] RISC-V: Add CoVE guest config and helper functions Date: Wed, 19 Apr 2023 15:17:06 -0700 Message-Id: <20230419221716.3603068-39-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_231854_640823_883ABCA7 X-CRM114-Status: GOOD ( 19.30 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Rajnesh Kanwal Introduce a separate config for the guest running in CoVE so that it can be enabled separately if required. However, the default config will enable both CoVE host & guest configs in order to make single image work as both host & guest. Introduce a helper function to detect if a guest is TVM or not at run time. The TSM only enables the CoVE guest SBI extension for TVMs. Signed-off-by: Rajnesh Kanwal Co-developed-by: Atish Patra Signed-off-by: Atish Patra --- arch/riscv/Kbuild | 2 ++ arch/riscv/Kconfig | 6 ++++++ arch/riscv/cove/Makefile | 2 ++ arch/riscv/cove/core.c | 28 ++++++++++++++++++++++++++++ arch/riscv/include/asm/cove.h | 27 +++++++++++++++++++++++++++ arch/riscv/kernel/setup.c | 2 ++ 6 files changed, 67 insertions(+) create mode 100644 arch/riscv/cove/Makefile create mode 100644 arch/riscv/cove/core.c create mode 100644 arch/riscv/include/asm/cove.h diff --git a/arch/riscv/Kbuild b/arch/riscv/Kbuild index afa83e3..ecd661e 100644 --- a/arch/riscv/Kbuild +++ b/arch/riscv/Kbuild @@ -1,5 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only +obj-$(CONFIG_RISCV_COVE_GUEST) += cove/ + obj-y += kernel/ mm/ net/ obj-$(CONFIG_BUILTIN_DTB) += boot/dts/ obj-y += errata/ diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 8462941..49c3006 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -512,6 +512,12 @@ config RISCV_COVE_HOST That means the platform should be capable of running TEE VM (TVM) using KVM and TEE Security Manager (TSM). +config RISCV_COVE_GUEST + bool "Guest Support for Confidential VM Extension(CoVE)" + default n + help + Enables support for running TVMs on platforms supporting CoVE. + endmenu # "Confidential VM Extension(CoVE) Support" endmenu # "Platform type" diff --git a/arch/riscv/cove/Makefile b/arch/riscv/cove/Makefile new file mode 100644 index 0000000..03a0cac --- /dev/null +++ b/arch/riscv/cove/Makefile @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-$(CONFIG_RISCV_COVE_GUEST) += core.o diff --git a/arch/riscv/cove/core.c b/arch/riscv/cove/core.c new file mode 100644 index 0000000..7218fe7 --- /dev/null +++ b/arch/riscv/cove/core.c @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Confidential Computing Platform Capability checks + * + * Copyright (c) 2023 Rivos Inc. + * + * Authors: + * Rajnesh Kanwal + */ + +#include +#include +#include +#include + +static bool is_tvm; + +bool is_cove_guest(void) +{ + return is_tvm; +} +EXPORT_SYMBOL_GPL(is_cove_guest); + +void riscv_cove_sbi_init(void) +{ + if (sbi_probe_extension(SBI_EXT_COVG) > 0) + is_tvm = true; +} diff --git a/arch/riscv/include/asm/cove.h b/arch/riscv/include/asm/cove.h new file mode 100644 index 0000000..c4d609d --- /dev/null +++ b/arch/riscv/include/asm/cove.h @@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * TVM helper functions + * + * Copyright (c) 2023 Rivos Inc. + * + * Authors: + * Rajnesh Kanwal + */ + +#ifndef __RISCV_COVE_H__ +#define __RISCV_COVE_H__ + +#ifdef CONFIG_RISCV_COVE_GUEST +void riscv_cove_sbi_init(void); +bool is_cove_guest(void); +#else /* CONFIG_RISCV_COVE_GUEST */ +static inline bool is_cove_guest(void) +{ + return false; +} +static inline void riscv_cove_sbi_init(void) +{ +} +#endif /* CONFIG_RISCV_COVE_GUEST */ + +#endif /* __RISCV_COVE_H__ */ diff --git a/arch/riscv/kernel/setup.c b/arch/riscv/kernel/setup.c index 7b2b065..20b0280 100644 --- a/arch/riscv/kernel/setup.c +++ b/arch/riscv/kernel/setup.c @@ -35,6 +35,7 @@ #include #include #include +#include #include "head.h" @@ -272,6 +273,7 @@ void __init setup_arch(char **cmdline_p) early_ioremap_setup(); sbi_init(); + riscv_cove_sbi_init(); jump_label_init(); parse_early_param(); From patchwork Wed Apr 19 22:17:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217674 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 DDEA9C77B7F for ; Wed, 19 Apr 2023 23:30:59 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=61e3DKb+3ZsW9OlNwlKQ+ef56OAjWQ1e0aZYXPFPDC8=; b=0zJ7J64+KvUvOY dzFN0NdhStL9hTwov3Emhzu4deVAgk4TVKhnjdgkEoKhiJ/jXFgnqfzMkRBJ9T5OQHJTd2na6VvqX YwjorSBTIqK1jRxTngH076diybQ6xXTgXEdw181FRBPE1P0xHRX0+4mKEPzGQanl8vPXl/kd355+o gKw0dpaCgH+BJzcxDXk5LlyQOynEGGsDGCoBqkVXnuRGPLCFAXpNwLGIi2peeWw7UoRa2Gyx+HxPC QG6uZ2lqNwiePdBcTYFpFTas4+Zxc72bn9fsqRakMzw1WaLlj912IVw7+KkFi7bS/HYhyBqeWBZJq egJq4L2LrbG+QMBhRtAg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppHGC-006ecJ-0G; Wed, 19 Apr 2023 23:30:48 +0000 Received: from mail-pj1-x1035.google.com ([2607:f8b0:4864:20::1035]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG8e-006TXl-2E for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:18:58 +0000 Received: by mail-pj1-x1035.google.com with SMTP id 98e67ed59e1d1-24736790966so213025a91.2 for ; Wed, 19 Apr 2023 15:18:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942736; x=1684534736; 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=HpkP1wnIrltbLZeAb/TZzxnsq9NvqWT39c9WofSyiOk=; b=Nzsbq++D21vwA8dGs2TEhUmfLPFSaig0puRkykWSASC64dxd/vGkpSKeFxQXOJxyon 5Ou4PoViyNfDX1Gfoq/jlBl1G1RhaiR4JLXaKzD7kt+niF6AKE+yOb7DVxkMfV9AiHsx mTPuZ/MAZl6BEVu8xpX5HBzOXWWYp6VQYr3Mp0sTKgkIRgLhxoSPyTdHc4vxQBKRTjC6 nzaETrxZRtdvV7d/S3g19DaIk1Y5S0BgV4Ei7fF0jUB1ujpfDWT04dllTpouA2v7Akvc l3myGSmdOxw+gPB2lOjj2aqYoHDlOR0Rr4q6kW0PPflSvA1d+iZ7docwAXBz+likDSY4 tCOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942736; x=1684534736; 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=HpkP1wnIrltbLZeAb/TZzxnsq9NvqWT39c9WofSyiOk=; b=RfVBc1WUuUNOcgwdT00uMr8L/SiopP7wnODqQh447mx7DqaVRLsdRc3DseiCrLbVUY tojjP6y7EcQ0j5Ip2/1+aVVP0Dd5WyeQ2XuKUXUyfW8GSrr5/FA4xFnQ+5/vrphyLaJ9 orjS5ciIx9XHmKOGaYy2rC+SkRkUMF6T13smPQZna5aEVFPhJ6KqyTSn4A5+34P4Fr5X emNUKIeOhcNfNeni2i+q4m89IR28hALP7vacZyr4qOD6DGz+KJishGs4LDGgbIj1rJWo JpQMP/9AoBa+MrMNUacxkiHLQLXu72X0tqukiDL1y4OWP808G2TKnYOBUrdEdmeYWBow 3b/w== X-Gm-Message-State: AAQBX9c5+bwiOVxFzxo6I3XptSbVdLukId7HR7kxw+/9b9pKGnZP671i yuyDmEgFBQks0F5p7PclK1Iyhg== X-Google-Smtp-Source: AKy350bCoBKIrFcroX21XtHTuZqvJehyqLN918v9CE2Osbf4jkMqvKMUa5z+Sio4RKf3jpe/KHP4Bg== X-Received: by 2002:a17:90a:f415:b0:247:6be7:8cc0 with SMTP id ch21-20020a17090af41500b002476be78cc0mr3832862pjb.35.1681942736133; Wed, 19 Apr 2023 15:18:56 -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.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:55 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Rajnesh Kanwal , Atish Patra , Alexandre Ghiti , Andrew Jones , Andrew Morton , Anup Patel , Atish Patra , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , 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 , Uladzislau Rezki Subject: [RFC 39/48] RISC-V: Implement COVG SBI extension Date: Wed, 19 Apr 2023 15:17:07 -0700 Message-Id: <20230419221716.3603068-40-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151856_731413_0C8C36A5 X-CRM114-Status: GOOD ( 17.10 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Rajnesh Kanwal COVG extension defines the guest side interface for running a guest in CoVE. These functions allow a CoVE guest to share/unshare memory, ask host to trap and emulate MMIO regions and allow/deny injection of interrupts from host. Signed-off-by: Rajnesh Kanwal Signed-off-by: Atish Patra --- arch/riscv/cove/Makefile | 2 +- arch/riscv/cove/cove_guest_sbi.c | 109 ++++++++++++++++++++++++++++++ arch/riscv/include/asm/covg_sbi.h | 38 +++++++++++ 3 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/cove/cove_guest_sbi.c create mode 100644 arch/riscv/include/asm/covg_sbi.h diff --git a/arch/riscv/cove/Makefile b/arch/riscv/cove/Makefile index 03a0cac..a95043b 100644 --- a/arch/riscv/cove/Makefile +++ b/arch/riscv/cove/Makefile @@ -1,2 +1,2 @@ # SPDX-License-Identifier: GPL-2.0 -obj-$(CONFIG_RISCV_COVE_GUEST) += core.o +obj-$(CONFIG_RISCV_COVE_GUEST) += core.o cove_guest_sbi.o diff --git a/arch/riscv/cove/cove_guest_sbi.c b/arch/riscv/cove/cove_guest_sbi.c new file mode 100644 index 0000000..af22d5e --- /dev/null +++ b/arch/riscv/cove/cove_guest_sbi.c @@ -0,0 +1,109 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * COVG SBI extensions related helper functions. + * + * Copyright (c) 2023 Rivos Inc. + * + * Authors: + * Rajnesh Kanwal + */ + +#include +#include +#include + +int sbi_covg_add_mmio_region(unsigned long addr, unsigned long len) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVG, SBI_EXT_COVG_ADD_MMIO_REGION, addr, len, + 0, 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covg_remove_mmio_region(unsigned long addr, unsigned long len) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVG, SBI_EXT_COVG_REMOVE_MMIO_REGION, addr, + len, 0, 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covg_share_memory(unsigned long addr, unsigned long len) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVG, SBI_EXT_COVG_SHARE_MEMORY, addr, len, 0, + 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covg_unshare_memory(unsigned long addr, unsigned long len) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVG, SBI_EXT_COVG_UNSHARE_MEMORY, addr, len, 0, + 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covg_allow_external_interrupt(unsigned long id) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVG, SBI_EXT_COVG_ALLOW_EXT_INTERRUPT, id, 0, + 0, 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covg_allow_all_external_interrupt(void) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVG, SBI_EXT_COVG_ALLOW_EXT_INTERRUPT, -1, 0, + 0, 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covg_deny_external_interrupt(unsigned long id) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVG, SBI_EXT_COVG_DENY_EXT_INTERRUPT, id, 0, 0, + 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covg_deny_all_external_interrupt(void) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_COVG, SBI_EXT_COVG_DENY_EXT_INTERRUPT, -1, 0, 0, + 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} diff --git a/arch/riscv/include/asm/covg_sbi.h b/arch/riscv/include/asm/covg_sbi.h new file mode 100644 index 0000000..31283de --- /dev/null +++ b/arch/riscv/include/asm/covg_sbi.h @@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * COVG SBI extension related header file. + * + * Copyright (c) 2023 Rivos Inc. + * + * Authors: + * Rajnesh Kanwal + */ + +#ifndef __RISCV_COVG_SBI_H__ +#define __RISCV_COVG_SBI_H__ + +#ifdef CONFIG_RISCV_COVE_GUEST + +int sbi_covg_add_mmio_region(unsigned long addr, unsigned long len); +int sbi_covg_remove_mmio_region(unsigned long addr, unsigned long len); +int sbi_covg_share_memory(unsigned long addr, unsigned long len); +int sbi_covg_unshare_memory(unsigned long addr, unsigned long len); +int sbi_covg_allow_external_interrupt(unsigned long id); +int sbi_covg_allow_all_external_interrupt(void); +int sbi_covg_deny_external_interrupt(unsigned long id); +int sbi_covg_deny_all_external_interrupt(void); + +#else + +static inline int sbi_covg_add_mmio_region(unsigned long addr, unsigned long len) { return 0; } +static inline int sbi_covg_remove_mmio_region(unsigned long addr, unsigned long len) { return 0; } +static inline int sbi_covg_share_memory(unsigned long addr, unsigned long len) { return 0; } +static inline int sbi_covg_unshare_memory(unsigned long addr, unsigned long len) { return 0; } +static inline int sbi_covg_allow_external_interrupt(unsigned long id) { return 0; } +static inline int sbi_covg_allow_all_external_interrupt(void) { return 0; } +static inline int sbi_covg_deny_external_interrupt(unsigned long id) { return 0; } +static inline int sbi_covg_deny_all_external_interrupt(void) { return 0; } + +#endif + +#endif /* __RISCV_COVG_SBI_H__ */ From patchwork Wed Apr 19 22:17:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217596 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 BC236C77B75 for ; Wed, 19 Apr 2023 22:37:35 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=XTo8nEktlmNKT0pN6YjKp98FZPlKHVLHH3KGWK37dTw=; b=FuIl6uo/VXyY2v wmVHAWbonlgOYkRm+fQNvY+saJLJh0x+Ht5ODAao4ZdeRJ2WvRzZSugtJ3QwdH/v3sVsjM1JBnsgs lhMaZpLQ0qU/b2I3fILI3yHnrxL1gbVx1G3tMFF6oqoyj1DXDiefe4guqtnaPkqXiFibETD+vxxdd g2729kQbIU8QEFxoE8B/S9CC2bordQ80uP+w4E0Oeqzltt6PclXIhbwfeyugABXV0oUp2SPyXzoOk +Dk5bP+p3/7ixmEgNrvIAxKYkSB40j44/SMfSeiMOInLn/nJ6wsCEQHxKf8ghaKV6exa9jjNmYsMI bzsDypQDko3y+7SfJAyg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQa-006Wc4-2x; Wed, 19 Apr 2023 22:37:28 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQY-006WXV-11 for linux-riscv@bombadil.infradead.org; Wed, 19 Apr 2023 22:37:26 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=LnnJUnDn5Gg0VxA4G2224aJuFesVpwMyXWuJ35Yh1EA=; b=eJNtAiIkcpAQ74xX+zRtztxCc6 7rLhIzs369n7oIt9Qe90y6LdKu/Om0iTSZI/B2Dix9Zup+wVufgH6Bq59zqTBhcIb3QaNlb0s1Su0 sdOb0SRBjA3hPCRxQbUf/9b68Pp0AhY0v4zErAqMFBCiWVfStcV07SL4NbAQb6LGzs2nWmHGhC5sE cVBcavXGfkaLNiTRvSt+dyNfMIxsJlf7lok2pKw2k/JopKV6ycTjDtuyyfvi5wV80u6KtQkfOP1hT tA6a10KVwbPjY1FHO2R2zgy+qap+63/CtTzLWUezkZmkUhT3n0iP1V4dGuLsp26tKeW8AjOkmZTt6 JGkb1bCA==; Received: from mail-pj1-f45.google.com ([209.85.216.45]) by casper.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ppG8j-00DeNc-Hh for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:19:03 +0000 Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-2496863c2c7so216518a91.1 for ; Wed, 19 Apr 2023 15:19:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942738; x=1684534738; 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=LnnJUnDn5Gg0VxA4G2224aJuFesVpwMyXWuJ35Yh1EA=; b=qN2qG27GHv+d9gUwWztkTi8p+g3aaC2rFXOZGwLc8eAMBqzRu7eT8kWoOJLWLTW0Eo Z5lYOlCFWymONdQ75UX7d1w0T/hT7irv6Jb4eLBvu7abb5vVm52aXVMSJ1GMw0Dbzqsr +1z7/IHpZr9TOWWv65z2G7cezkDsdmOF3T3T+GedPiWIaC/Kuy6PLSFoobdSxJzll2MD g1noklYQNjPoDJtmIYCLWo0ww78Wd6QJJJ0qNcx7/a9E59ARcqxtApq0JG/1pIkk/aOb ezm4wy13mbTPlO+a9FFwfpQuhkNR1dmeg9ddTlU2Pq0Tpw9j5c0vXUUJGHEIdY9VbyGW 1hlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942738; x=1684534738; 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=LnnJUnDn5Gg0VxA4G2224aJuFesVpwMyXWuJ35Yh1EA=; b=aWEwYLOH827OekqSSmZZ2dGQlFT89FCe2bi4asZBiPVfzCV4blYNHFDw6dhTFpyPlT dpcXoib6HXUUV99WyxiCcJocgVIIF+xMDUELBFVZx93CSGLUQOa9z0LPMTdK+c2hY4aj nnF49YGYUmpyjeZnXgbY29DYiKhcj3G4oYowwgrE08QyE7gHDmwECpiBKdg6zZ9UgSet 14sWqYdFfcmEiGmta9QZ6yTRZW4TAzITIzXxPDVz6NxJ5/pYk//eeGkisjeRxen2EneT LEN/UjtYuEzfYsV7yaD1nyXrfAsd4E5yQj1uodZhKwCn4xF4r4Uut1CO66y5+LKMHVkL /vBA== X-Gm-Message-State: AAQBX9dNc2GEDVvmvvvk0Ud2+VSQ274Kv5wNnirqoodQZwiEfclpAxHb fULRdNbp9DvpEuOb66QDomcJHg== X-Google-Smtp-Source: AKy350aHmxw5PVAlBIl9T883RKpr61ycrn0Nkrml8nQtl+RbpvzosCcY4lybRtm63StCXT7HS5Gtsg== X-Received: by 2002:a17:90b:38c6:b0:237:c209:5b14 with SMTP id nn6-20020a17090b38c600b00237c2095b14mr3820467pjb.22.1681942738315; Wed, 19 Apr 2023 15:18:58 -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.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:18:58 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Rajnesh Kanwal , Atish Patra , Alexandre Ghiti , Andrew Jones , Andrew Morton , Anup Patel , Atish Patra , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , 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 , Uladzislau Rezki Subject: [RFC 40/48] RISC-V: COVE: Add COVH invalidate, validate, promote, demote and remove APIs. Date: Wed, 19 Apr 2023 15:17:08 -0700 Message-Id: <20230419221716.3603068-41-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_231901_692933_E480EA7C X-CRM114-Status: GOOD ( 13.62 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Rajnesh Kanwal SBI_EXT_COVH_TVM_INVALIDATE_PAGES: Invalidates the pages in the specified range of guest physical address space. The host can now work upon the range without TVM's interaction. Any access from TVM to the range will result in a page-fault which will be reported to the host. SBI_EXT_COVH_TVM_VALIDATE_PAGES: Marks the invalidated pages in the specified range of guest physical address space as present and TVM can now access the pages. SBI_EXT_COVH_TVM_PROMOTE_PAGES: Promotes a set of contiguous mappings to the requested page size. This is mainly to support huge-pages. SBI_EXT_COVH_TVM_DEMOTE_PAGES: Demotes a huge page mapping to a set of contiguous mappings at the target size. SBI_EXT_COVH_TVM_REMOVE_PAGES: Removes mappings from a TVM. The range to be unmapped must already have been invalidated and fenced, and must lie within a removable region of guest physical address space. Signed-off-by: Atish Patra Signed-off-by: Rajnesh Kanwal --- arch/riscv/include/asm/kvm_cove_sbi.h | 16 +++++++ arch/riscv/include/asm/sbi.h | 5 +++ arch/riscv/kvm/cove_sbi.c | 65 +++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/arch/riscv/include/asm/kvm_cove_sbi.h b/arch/riscv/include/asm/kvm_cove_sbi.h index 0759f70..b554a8d 100644 --- a/arch/riscv/include/asm/kvm_cove_sbi.h +++ b/arch/riscv/include/asm/kvm_cove_sbi.h @@ -59,6 +59,22 @@ int sbi_covh_create_tvm_vcpu(unsigned long tvmid, unsigned long tvm_vcpuid, int sbi_covh_run_tvm_vcpu(unsigned long tvmid, unsigned long tvm_vcpuid); +int sbi_covh_tvm_invalidate_pages(unsigned long tvmid, + unsigned long tvm_base_page_addr, + unsigned long len); +int sbi_covh_tvm_validate_pages(unsigned long tvmid, + unsigned long tvm_base_page_addr, + unsigned long len); +int sbi_covh_tvm_promote_page(unsigned long tvmid, + unsigned long tvm_base_page_addr, + enum sbi_cove_page_type ptype); +int sbi_covh_tvm_demote_page(unsigned long tvmid, + unsigned long tvm_base_page_addr, + enum sbi_cove_page_type ptype); +int sbi_covh_tvm_remove_pages(unsigned long tvmid, + unsigned long tvm_base_page_addr, + unsigned long len); + /* Functions related to CoVE Interrupt Management(COVI) Extension */ int sbi_covi_tvm_aia_init(unsigned long tvm_gid, struct sbi_cove_tvm_aia_params *tvm_aia_params); int sbi_covi_set_vcpu_imsic_addr(unsigned long tvm_gid, unsigned long vcpu_id, diff --git a/arch/riscv/include/asm/sbi.h b/arch/riscv/include/asm/sbi.h index e02ee75..03b0cc8 100644 --- a/arch/riscv/include/asm/sbi.h +++ b/arch/riscv/include/asm/sbi.h @@ -369,6 +369,11 @@ enum sbi_ext_covh_fid { SBI_EXT_COVH_TVM_CREATE_VCPU, SBI_EXT_COVH_TVM_VCPU_RUN, SBI_EXT_COVH_TVM_INITIATE_FENCE, + SBI_EXT_COVH_TVM_INVALIDATE_PAGES, + SBI_EXT_COVH_TVM_VALIDATE_PAGES, + SBI_EXT_COVH_TVM_PROMOTE_PAGE, + SBI_EXT_COVH_TVM_DEMOTE_PAGE, + SBI_EXT_COVH_TVM_REMOVE_PAGES, }; enum sbi_ext_covi_fid { diff --git a/arch/riscv/kvm/cove_sbi.c b/arch/riscv/kvm/cove_sbi.c index a8901ac..01dc260 100644 --- a/arch/riscv/kvm/cove_sbi.c +++ b/arch/riscv/kvm/cove_sbi.c @@ -405,3 +405,68 @@ int sbi_covh_run_tvm_vcpu(unsigned long tvmid, unsigned long vcpuid) return 0; } + +int sbi_covh_tvm_invalidate_pages(unsigned long tvmid, + unsigned long tvm_base_page_addr, + unsigned long len) +{ + struct sbiret ret = sbi_ecall(SBI_EXT_COVH, + SBI_EXT_COVH_TVM_INVALIDATE_PAGES, tvmid, + tvm_base_page_addr, len, 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covh_tvm_validate_pages(unsigned long tvmid, + unsigned long tvm_base_page_addr, + unsigned long len) +{ + struct sbiret ret = sbi_ecall(SBI_EXT_COVH, + SBI_EXT_COVH_TVM_VALIDATE_PAGES, tvmid, + tvm_base_page_addr, len, 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covh_tvm_promote_page(unsigned long tvmid, + unsigned long tvm_base_page_addr, + enum sbi_cove_page_type ptype) +{ + struct sbiret ret = sbi_ecall(SBI_EXT_COVH, + SBI_EXT_COVH_TVM_PROMOTE_PAGE, tvmid, + tvm_base_page_addr, ptype, 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covh_tvm_demote_page(unsigned long tvmid, + unsigned long tvm_base_page_addr, + enum sbi_cove_page_type ptype) +{ + struct sbiret ret = sbi_ecall(SBI_EXT_COVH, + SBI_EXT_COVH_TVM_DEMOTE_PAGE, tvmid, + tvm_base_page_addr, ptype, 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + +int sbi_covh_tvm_remove_pages(unsigned long tvmid, + unsigned long tvm_base_page_addr, + unsigned long len) +{ + struct sbiret ret = sbi_ecall(SBI_EXT_COVH, + SBI_EXT_COVH_TVM_REMOVE_PAGES, tvmid, + tvm_base_page_addr, len, 0, 0, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} From patchwork Wed Apr 19 22:17:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217582 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 522CDC77B73 for ; Wed, 19 Apr 2023 22:26:56 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=9dPA+iaiy3EPtGqVWYhiRcLhCnUvVhGoofQsUwBha4U=; b=wrdPaPipap4FOt gwotfcCUbkaEGFcDdCKo8zW9Die7AEVbElqZxCKyJpPAsCpjTpzcYtNIMOuK5xhjCvuCdZQkt5xJT Ws3ErBx4JcXlXsTASVFDVkAdeMpVfHKqsrOqPAuZTvcVOTwjf1SyYZLzq+fYrBiPo1VmBfwW4Ziwv 20PrhQywbVQkPPkrsi6JPl870m+DxQmq9K8mdEYH9hsFi6mrV0ZFfHQNvuHPyKTPXlBx7BzjEEbW5 y45gMl3yu3iYgIDedapoVw2dAcuU5TkWlYrh7Wl9rxIXjIpt4FUdmbBFAfY4RRL9cVnK1KM9Nqvug 0+7hzrdCdtEp9ZJG1bmw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGGH-006VL0-2a; Wed, 19 Apr 2023 22:26:49 +0000 Received: from mail-oi1-x22f.google.com ([2607:f8b0:4864:20::22f]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGGE-006VK0-0y for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:26:48 +0000 Received: by mail-oi1-x22f.google.com with SMTP id 5614622812f47-38e04d1b2b4so202077b6e.3 for ; Wed, 19 Apr 2023 15:26:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681943202; x=1684535202; 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=XoUTXkkdzp66fUQa0SZgxh2I0hcfFcSevl2ozJzsZr8=; b=qetVclOS7C4iQjnwdsJwHYSpf5BepeIZ63R9PczrHts9kU2IgiP/gphzKbCz0+qql5 19l8QI7jihNyy0HMk+f4L/yaHWDP8g/5ISE2pFhKpUQBcrBAN042sfjzfGIOTokJitrX HztJt1jUl+NrWr9azg8Vhd8qGdKD22tT+CP/XfJZw1SOr+UfUFHPcZeHoyN7TOmivrHV rbvB4GVxN/XLTeWxIXq2RzXJuTBZb92Vr07sm+4SAzfad6M4sQ44O0f23y6ABPtzLE+j yLHGQb2FPvcAoZE1/SpRTWnz0qHs9r6eFcREAXPLLw9RsxFEcKQGSMSktH28IOMjUV6w e+Sg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681943202; x=1684535202; 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=XoUTXkkdzp66fUQa0SZgxh2I0hcfFcSevl2ozJzsZr8=; b=ZSkCRHbFLzVkuRrpgB4vxRLQj0WxQ/BUYB9DDkJy0tMI0twboonWyezQ6Wt0Jnws/l 8MRTL8awM17VFFCp+0G4x/NjwNB3t+XSh0nf4Co/HRzpAe+Hk68eQws1mvSPBf7bXAbO EmDvEj3GpOuIr1L3gNUIp+elZsKG2ZasyCPqGf840YF5nYfPnG940T6xhfb4SFJU84Vm gofcFszHZwLodpoAs8D+OuBUzZade/MCp3EuEQSN/VSNrGqCJARMz7WkGhAkYlFIDaDL 8wg9eqzvt5xjNTh+ww278Zecn7x9+FOMr4M1JcKnOThNnsCK1YsweHpwBvmsiT0ba0EE oJ0g== X-Gm-Message-State: AAQBX9cJwd4C7tdi2wVXOt/ciPiUTMfhcWNKQFYB08tBpaCSWYlIcsh2 JWJ7r5hsAVq1z/ou2GMaR3MmO6AxNnGY2WPZdK0= X-Google-Smtp-Source: AKy350Y7aINNAc9YHSFZjS/1v/4hxySiR9RBBeMIRAwJ/RoWtbD0/kdtTdNjVZfjscd6qYzOFWfZ9Q== X-Received: by 2002:a17:902:eb91:b0:1a6:d2a9:3fab with SMTP id q17-20020a170902eb9100b001a6d2a93fabmr5967973plg.47.1681942740547; Wed, 19 Apr 2023 15:19:00 -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.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:19:00 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Rajnesh Kanwal , Atish Patra , Alexandre Ghiti , Andrew Jones , Andrew Morton , Anup Patel , Atish Patra , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , 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 , Uladzislau Rezki Subject: [RFC 41/48] RISC-V: KVM: Add host side support to handle COVG SBI calls. Date: Wed, 19 Apr 2023 15:17:09 -0700 Message-Id: <20230419221716.3603068-42-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_152646_341406_2F2E6799 X-CRM114-Status: GOOD ( 27.69 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Rajnesh Kanwal Adding host side support to allow memory sharing/unsharing. Host needs to check if the page has been already assigned (converted) to a TVM or not. If yes, that page needs to be reclaimed before sharing that page. For the remaining ECALLs host doesn't really need to do anything and we just return in those cases. Signed-off-by: Rajnesh Kanwal Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_cove.h | 11 +- arch/riscv/include/asm/kvm_cove_sbi.h | 4 + arch/riscv/include/asm/kvm_vcpu_sbi.h | 3 + arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/Makefile | 2 +- arch/riscv/kvm/cove.c | 48 +++++- arch/riscv/kvm/cove_sbi.c | 18 ++ arch/riscv/kvm/vcpu_exit.c | 2 +- arch/riscv/kvm/vcpu_sbi.c | 14 ++ arch/riscv/kvm/vcpu_sbi_covg.c | 232 ++++++++++++++++++++++++++ 10 files changed, 328 insertions(+), 7 deletions(-) create mode 100644 arch/riscv/kvm/vcpu_sbi_covg.c diff --git a/arch/riscv/include/asm/kvm_cove.h b/arch/riscv/include/asm/kvm_cove.h index 4367281..afaea7c 100644 --- a/arch/riscv/include/asm/kvm_cove.h +++ b/arch/riscv/include/asm/kvm_cove.h @@ -31,6 +31,9 @@ #define get_order_num_pages(n) (get_order(n << PAGE_SHIFT)) +#define get_gpr_index(goffset) \ + ((goffset - KVM_ARCH_GUEST_ZERO) / (__riscv_xlen / 8)) + /* Describe a confidential or shared memory region */ struct kvm_riscv_cove_mem_region { unsigned long hva; @@ -139,7 +142,8 @@ int kvm_riscv_cove_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run); int kvm_riscv_cove_vm_measure_pages(struct kvm *kvm, struct kvm_riscv_cove_measure_region *mr); int kvm_riscv_cove_vm_add_memreg(struct kvm *kvm, unsigned long gpa, unsigned long size); -int kvm_riscv_cove_gstage_map(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned long hva); +int kvm_riscv_cove_handle_pagefault(struct kvm_vcpu *vcpu, gpa_t gpa, + unsigned long hva); /* Fence related function */ int kvm_riscv_cove_tvm_fence(struct kvm_vcpu *vcpu); @@ -179,8 +183,9 @@ static inline int kvm_riscv_cove_vm_measure_pages(struct kvm *kvm, { return -1; } -static inline int kvm_riscv_cove_gstage_map(struct kvm_vcpu *vcpu, - gpa_t gpa, unsigned long hva) {return -1; } +static inline int kvm_riscv_cove_handle_pagefault(struct kvm_vcpu *vcpu, + gpa_t gpa, unsigned long hva) { return -1; } + /* TVM interrupt managenet via AIA functions */ static inline int kvm_riscv_cove_aia_init(struct kvm *kvm) { return -1; } static inline int kvm_riscv_cove_vcpu_inject_interrupt(struct kvm_vcpu *vcpu, diff --git a/arch/riscv/include/asm/kvm_cove_sbi.h b/arch/riscv/include/asm/kvm_cove_sbi.h index b554a8d..c930265 100644 --- a/arch/riscv/include/asm/kvm_cove_sbi.h +++ b/arch/riscv/include/asm/kvm_cove_sbi.h @@ -59,6 +59,10 @@ int sbi_covh_create_tvm_vcpu(unsigned long tvmid, unsigned long tvm_vcpuid, int sbi_covh_run_tvm_vcpu(unsigned long tvmid, unsigned long tvm_vcpuid); +int sbi_covh_add_shared_pages(unsigned long tvmid, unsigned long page_addr_phys, + enum sbi_cove_page_type ptype, + unsigned long npages, + unsigned long tvm_base_page_addr); int sbi_covh_tvm_invalidate_pages(unsigned long tvmid, unsigned long tvm_base_page_addr, unsigned long len); diff --git a/arch/riscv/include/asm/kvm_vcpu_sbi.h b/arch/riscv/include/asm/kvm_vcpu_sbi.h index b10c896..5b37a12 100644 --- a/arch/riscv/include/asm/kvm_vcpu_sbi.h +++ b/arch/riscv/include/asm/kvm_vcpu_sbi.h @@ -66,5 +66,8 @@ extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_hsm; extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_dbcn; extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_experimental; extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_vendor; +#ifdef CONFIG_RISCV_COVE_HOST +extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_covg; +#endif #endif /* __RISCV_KVM_VCPU_SBI_H__ */ diff --git a/arch/riscv/include/uapi/asm/kvm.h b/arch/riscv/include/uapi/asm/kvm.h index ac3def0..2a24341 100644 --- a/arch/riscv/include/uapi/asm/kvm.h +++ b/arch/riscv/include/uapi/asm/kvm.h @@ -148,6 +148,7 @@ enum KVM_RISCV_SBI_EXT_ID { KVM_RISCV_SBI_EXT_EXPERIMENTAL, KVM_RISCV_SBI_EXT_VENDOR, KVM_RISCV_SBI_EXT_DBCN, + KVM_RISCV_SBI_EXT_COVG, KVM_RISCV_SBI_EXT_MAX, }; diff --git a/arch/riscv/kvm/Makefile b/arch/riscv/kvm/Makefile index 8c91551..31f4dbd 100644 --- a/arch/riscv/kvm/Makefile +++ b/arch/riscv/kvm/Makefile @@ -31,4 +31,4 @@ kvm-y += aia.o kvm-y += aia_device.o kvm-y += aia_aplic.o kvm-y += aia_imsic.o -kvm-$(CONFIG_RISCV_COVE_HOST) += cove_sbi.o cove.o +kvm-$(CONFIG_RISCV_COVE_HOST) += cove_sbi.o cove.o vcpu_sbi_covg.o diff --git a/arch/riscv/kvm/cove.c b/arch/riscv/kvm/cove.c index 154b01a..ba596b7 100644 --- a/arch/riscv/kvm/cove.c +++ b/arch/riscv/kvm/cove.c @@ -44,6 +44,18 @@ static void kvm_cove_local_fence(void *info) kvm_err("local fence for TSM failed %d on cpu %d\n", rc, smp_processor_id()); } +static void cove_delete_shared_pinned_page_list(struct kvm *kvm, + struct list_head *tpages) +{ + struct kvm_riscv_cove_page *tpage, *temp; + + list_for_each_entry_safe(tpage, temp, tpages, link) { + unpin_user_pages_dirty_lock(&tpage->page, 1, true); + list_del(&tpage->link); + kfree(tpage); + } +} + static void cove_delete_page_list(struct kvm *kvm, struct list_head *tpages, bool unpin) { struct kvm_riscv_cove_page *tpage, *temp; @@ -425,7 +437,8 @@ int kvm_riscv_cove_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run) sbi_ext = kvm_vcpu_sbi_find_ext(vcpu, cp->a7); if ((sbi_ext && sbi_ext->handler) && ((cp->a7 == SBI_EXT_DBCN) || - (cp->a7 == SBI_EXT_HSM) || (cp->a7 == SBI_EXT_SRST) || ext_is_01)) { + (cp->a7 == SBI_EXT_HSM) || (cp->a7 == SBI_EXT_SRST) || + (cp->a7 == SBI_EXT_COVG) || ext_is_01)) { ret = sbi_ext->handler(vcpu, run, &sbi_ret); } else { kvm_err("%s: SBI EXT %lx not supported for TVM\n", __func__, cp->a7); @@ -451,7 +464,8 @@ int kvm_riscv_cove_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run) return ret; } -int kvm_riscv_cove_gstage_map(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned long hva) +static int kvm_riscv_cove_gstage_map(struct kvm_vcpu *vcpu, gpa_t gpa, + unsigned long hva) { struct kvm_riscv_cove_page *tpage; struct mm_struct *mm = current->mm; @@ -517,6 +531,35 @@ int kvm_riscv_cove_gstage_map(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned long hv return rc; } +int kvm_riscv_cove_handle_pagefault(struct kvm_vcpu *vcpu, gpa_t gpa, + unsigned long hva) +{ + struct kvm_cove_tvm_context *tvmc = vcpu->kvm->arch.tvmc; + struct kvm_riscv_cove_page *tpage, *next; + bool shared = false; + + /* TODO: Implement a better approach to track regions to avoid + * traversing the whole list on each fault. + */ + spin_lock(&vcpu->kvm->mmu_lock); + list_for_each_entry_safe(tpage, next, &tvmc->shared_pages, link) { + if (tpage->gpa == (gpa & PAGE_MASK)) { + shared = true; + break; + } + } + spin_unlock(&vcpu->kvm->mmu_lock); + + if (shared) { + return sbi_covh_add_shared_pages(tvmc->tvm_guest_id, + page_to_phys(tpage->page), + SBI_COVE_PAGE_4K, 1, + gpa & PAGE_MASK); + } + + return kvm_riscv_cove_gstage_map(vcpu, gpa, hva); +} + void noinstr kvm_riscv_cove_vcpu_switchto(struct kvm_vcpu *vcpu, struct kvm_cpu_trap *trap) { int rc; @@ -804,6 +847,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); + cove_delete_shared_pinned_page_list(kvm, &tvmc->shared_pages); /* 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/cove_sbi.c b/arch/riscv/kvm/cove_sbi.c index 01dc260..4759b49 100644 --- a/arch/riscv/kvm/cove_sbi.c +++ b/arch/riscv/kvm/cove_sbi.c @@ -380,6 +380,24 @@ int sbi_covh_add_zero_pages(unsigned long tvmid, unsigned long page_addr_phys, return 0; } +int sbi_covh_add_shared_pages(unsigned long tvmid, unsigned long page_addr_phys, + enum sbi_cove_page_type ptype, + unsigned long npages, + unsigned long tvm_base_page_addr) +{ + struct sbiret ret; + + if (!PAGE_ALIGNED(page_addr_phys)) + return -EINVAL; + + ret = sbi_ecall(SBI_EXT_COVH, SBI_EXT_COVH_TVM_ADD_SHARED_PAGES, tvmid, + page_addr_phys, ptype, npages, tvm_base_page_addr, 0); + if (ret.error) + return sbi_err_map_linux_errno(ret.error); + + return 0; +} + int sbi_covh_create_tvm_vcpu(unsigned long tvmid, unsigned long vcpuid, unsigned long vcpu_state_paddr) { diff --git a/arch/riscv/kvm/vcpu_exit.c b/arch/riscv/kvm/vcpu_exit.c index c46e7f2..51eb434 100644 --- a/arch/riscv/kvm/vcpu_exit.c +++ b/arch/riscv/kvm/vcpu_exit.c @@ -43,7 +43,7 @@ static int gstage_page_fault(struct kvm_vcpu *vcpu, struct kvm_run *run, 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); + ret = kvm_riscv_cove_handle_pagefault(vcpu, fault_addr, hva); } else { ret = kvm_riscv_gstage_map(vcpu, memslot, fault_addr, hva, (trap->scause == EXC_STORE_GUEST_PAGE_FAULT) ? true : false); diff --git a/arch/riscv/kvm/vcpu_sbi.c b/arch/riscv/kvm/vcpu_sbi.c index d2f43bc..8bc7d73 100644 --- a/arch/riscv/kvm/vcpu_sbi.c +++ b/arch/riscv/kvm/vcpu_sbi.c @@ -13,6 +13,8 @@ #include #include #include +#include +#include #ifndef CONFIG_RISCV_SBI_V01 static const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_v01 = { @@ -32,6 +34,14 @@ static const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_pmu = { }; #endif +#ifndef CONFIG_RISCV_COVE_HOST +static const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_covg = { + .extid_start = -1UL, + .extid_end = -1UL, + .handler = NULL, +}; +#endif + struct kvm_riscv_sbi_extension_entry { enum KVM_RISCV_SBI_EXT_ID dis_idx; const struct kvm_vcpu_sbi_extension *ext_ptr; @@ -82,6 +92,10 @@ static const struct kvm_riscv_sbi_extension_entry sbi_ext[] = { .dis_idx = KVM_RISCV_SBI_EXT_VENDOR, .ext_ptr = &vcpu_sbi_ext_vendor, }, + { + .dis_idx = KVM_RISCV_SBI_EXT_COVG, + .ext_ptr = &vcpu_sbi_ext_covg, + }, }; void kvm_riscv_vcpu_sbi_forward(struct kvm_vcpu *vcpu, struct kvm_run *run) diff --git a/arch/riscv/kvm/vcpu_sbi_covg.c b/arch/riscv/kvm/vcpu_sbi_covg.c new file mode 100644 index 0000000..44a3b06 --- /dev/null +++ b/arch/riscv/kvm/vcpu_sbi_covg.c @@ -0,0 +1,232 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2023 Rivos Inc. + * + * Authors: + * Rajnesh Kanwal + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static int cove_share_converted_page(struct kvm_vcpu *vcpu, gpa_t gpa, + struct kvm_riscv_cove_page *tpage) +{ + struct kvm *kvm = vcpu->kvm; + struct kvm_cove_tvm_context *tvmc = kvm->arch.tvmc; + int rc; + + rc = sbi_covh_tvm_invalidate_pages(tvmc->tvm_guest_id, gpa, PAGE_SIZE); + if (rc) + return rc; + + rc = kvm_riscv_cove_tvm_fence(vcpu); + if (rc) + goto err; + + rc = sbi_covh_tvm_remove_pages(tvmc->tvm_guest_id, gpa, PAGE_SIZE); + if (rc) + goto err; + + rc = sbi_covh_tsm_reclaim_page(page_to_phys(tpage->page)); + if (rc) + return rc; + + spin_lock(&kvm->mmu_lock); + list_del(&tpage->link); + list_add(&tpage->link, &tvmc->shared_pages); + spin_unlock(&kvm->mmu_lock); + + return 0; + +err: + sbi_covh_tvm_validate_pages(tvmc->tvm_guest_id, gpa, PAGE_SIZE); + + return rc; +} + +static int cove_share_page(struct kvm_vcpu *vcpu, gpa_t gpa, + unsigned long *sbi_err) +{ + unsigned long hva = gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT); + struct kvm_cove_tvm_context *tvmc = vcpu->kvm->arch.tvmc; + struct mm_struct *mm = current->mm; + struct kvm_riscv_cove_page *tpage; + struct page *page; + int rc; + + if (kvm_is_error_hva(hva)) { + /* Address is out of the guest ram memory region. */ + *sbi_err = SBI_ERR_INVALID_PARAM; + return 0; + } + + tpage = kmalloc(sizeof(*tpage), GFP_KERNEL_ACCOUNT); + if (!tpage) + return -ENOMEM; + + mmap_read_lock(mm); + rc = pin_user_pages(hva, 1, FOLL_LONGTERM | FOLL_WRITE, &page, NULL); + mmap_read_unlock(mm); + + if (rc != 1) { + rc = -EINVAL; + goto free_tpage; + } else if (!PageSwapBacked(page)) { + rc = -EIO; + goto free_tpage; + } + + tpage->page = page; + tpage->gpa = gpa; + tpage->hva = hva; + INIT_LIST_HEAD(&tpage->link); + + spin_lock(&vcpu->kvm->mmu_lock); + list_add(&tpage->link, &tvmc->shared_pages); + spin_unlock(&vcpu->kvm->mmu_lock); + + return 0; + +free_tpage: + kfree(tpage); + + return rc; +} + +static int kvm_riscv_cove_share_page(struct kvm_vcpu *vcpu, gpa_t gpa, + unsigned long *sbi_err) +{ + struct kvm_cove_tvm_context *tvmc = vcpu->kvm->arch.tvmc; + struct kvm_riscv_cove_page *tpage, *next; + bool converted = false; + + /* + * Check if the shared memory is part of the pages already assigned + * to the TVM. + * + * TODO: Implement a better approach to track regions to avoid + * traversing the whole list. + */ + spin_lock(&vcpu->kvm->mmu_lock); + list_for_each_entry_safe(tpage, next, &tvmc->zero_pages, link) { + if (tpage->gpa == gpa) { + converted = true; + break; + } + } + spin_unlock(&vcpu->kvm->mmu_lock); + + if (converted) + return cove_share_converted_page(vcpu, gpa, tpage); + + return cove_share_page(vcpu, gpa, sbi_err); +} + +static int kvm_riscv_cove_unshare_page(struct kvm_vcpu *vcpu, gpa_t gpa) +{ + struct kvm_riscv_cove_page *tpage, *next; + struct kvm *kvm = vcpu->kvm; + struct kvm_cove_tvm_context *tvmc = kvm->arch.tvmc; + struct page *page = NULL; + int rc; + + spin_lock(&kvm->mmu_lock); + list_for_each_entry_safe(tpage, next, &tvmc->shared_pages, link) { + if (tpage->gpa == gpa) { + page = tpage->page; + break; + } + } + spin_unlock(&kvm->mmu_lock); + + if (unlikely(!page)) + return -EINVAL; + + rc = sbi_covh_tvm_invalidate_pages(tvmc->tvm_guest_id, gpa, PAGE_SIZE); + if (rc) + return rc; + + rc = kvm_riscv_cove_tvm_fence(vcpu); + if (rc) + return rc; + + rc = sbi_covh_tvm_remove_pages(tvmc->tvm_guest_id, gpa, PAGE_SIZE); + if (rc) + return rc; + + unpin_user_pages_dirty_lock(&page, 1, true); + + spin_lock(&kvm->mmu_lock); + list_del(&tpage->link); + spin_unlock(&kvm->mmu_lock); + + kfree(tpage); + + return 0; +} + +static int kvm_sbi_ext_covg_handler(struct kvm_vcpu *vcpu, struct kvm_run *run, + struct kvm_vcpu_sbi_return *retdata) +{ + struct kvm_cpu_context *cp = &vcpu->arch.guest_context; + uint32_t num_pages = cp->a1 / PAGE_SIZE; + unsigned long funcid = cp->a6; + unsigned long *err_val = &retdata->err_val; + uint32_t i; + int ret; + + switch (funcid) { + case SBI_EXT_COVG_SHARE_MEMORY: + for (i = 0; i < num_pages; i++) { + ret = kvm_riscv_cove_share_page( + vcpu, cp->a0 + i * PAGE_SIZE, err_val); + if (ret || *err_val != SBI_SUCCESS) + return ret; + } + return 0; + + case SBI_EXT_COVG_UNSHARE_MEMORY: + for (i = 0; i < num_pages; i++) { + ret = kvm_riscv_cove_unshare_page( + vcpu, cp->a0 + i * PAGE_SIZE); + if (ret) + return ret; + } + return 0; + + case SBI_EXT_COVG_ADD_MMIO_REGION: + case SBI_EXT_COVG_REMOVE_MMIO_REGION: + case SBI_EXT_COVG_ALLOW_EXT_INTERRUPT: + case SBI_EXT_COVG_DENY_EXT_INTERRUPT: + /* We don't really need to do anything here for now. */ + return 0; + + default: + kvm_err("%s: Unsupported guest SBI %ld.\n", __func__, funcid); + retdata->err_val = SBI_ERR_NOT_SUPPORTED; + return -EOPNOTSUPP; + } +} + +unsigned long kvm_sbi_ext_covg_probe(struct kvm_vcpu *vcpu) +{ + /* KVM COVG SBI handler is only meant for handling calls from TSM */ + return 0; +} + +const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_covg = { + .extid_start = SBI_EXT_COVG, + .extid_end = SBI_EXT_COVG, + .handler = kvm_sbi_ext_covg_handler, + .probe = kvm_sbi_ext_covg_probe, +}; From patchwork Wed Apr 19 22:17:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217675 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 4B4E1C6FD18 for ; Wed, 19 Apr 2023 23:30:59 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=m7BUXLoYOITUMK3beG2D1QImyFYrvHJp+OBVe1KCmsA=; b=twqg52XClfi8lc hYgA5J+RGTyXgiyLeCy0PCnhOOXoOe0y6yoyql/+ULrDjW+EzjX+clGgxkD0Kat5fo3Ussv10x7TS NKCdzYkgpNI7LNrJDeHmkIFS03UKOkpx2tlfyJYNtbbsiEM/QIKzSqb+KHalCv4WSWu/YVSR4PVUb 3Am9xm039aeZNncKgp2PjhYZD/ee3hRdVDW/UkqJh+nBpszNuAiAPzduD54gAbT/qXIJnPTzSX0K/ hWUsgx51s9JdJah9QSMRGoXEpnewRONpQjQtDEYOD5Z0XPOz6ctGdGe8yPjAHKcXnPnbBSjrx270/ tZ2Vj16r16SnnZAbECAw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppHGD-006ee2-0r; Wed, 19 Apr 2023 23:30:49 +0000 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG8l-006Tb8-0s for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:19:04 +0000 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-1a69f686345so4530415ad.2 for ; Wed, 19 Apr 2023 15:19:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942743; x=1684534743; 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=a40XLfKnT38y+rtkL3pdG8Ux1IFfCO6rS0FTBqpEPf0=; b=3qxwZ1AI4VGDwq0mJs81MteY/7NGEkj+18YI44D7IRwVFmeOk/RDHZQGN6wE3U+Fah h7gisXIiukRhgYPfWkwf74DwoXRlOc0S4t3u5KNMCW0DvYSt6D9lgBm8X1/OboQx9bjc IzBdkE1rsZq0oHGxqiSUWhGy7JzHKWqkxpvHKq8QGhfZMNoiHDUHp+knL2W2J0PauRX8 Z60xK4xlPZJK9w4CVaJLh1I3bqfv7kssfB1lBki6XAPwuKeXcckA/zvCt73aqzG97NxY H3VYn/a0FuKbmIi+8CNiszb6pRZFNUdKkES3iAmBw3QloUaHccRpDErn+0099zd/wFiN sTyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942743; x=1684534743; 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=a40XLfKnT38y+rtkL3pdG8Ux1IFfCO6rS0FTBqpEPf0=; b=Wvb32a2leW2xcCa5c3SbwhsjZl7BywaHsTroWQB5suDgycHWFTpEX1T3ENB3aCVKX+ dIDBlFt2xDxAhQWXcMkH9kRcC/nkLoMfZpnCDWzFyx/pa/X/kZ0P7HUgZFLsrAo0Lh6f 7//MW+Zxo8Jas/hLhrvAr8U2xiuxLipubc8Gt5XsHICOoS2O1oejNBSqlTJdbl5/a4rm pIuZwwz2mVTDiRtiBbwfC7TGlkMZtjXNT6kO3WcSpWD8kRSbQxWdB7RRv7CY4WDY/Lqj 1Evn/NwU55W8gzwVi120F3IjwST0SDNwvF+h78fuQEBkEKEuXu3WvrPICE2JYMu9mtHB 0arA== X-Gm-Message-State: AAQBX9cb1Cueh3dluABc4uPuf/yjoHoxzZro4o/UnsM/zgL0dBh5S8Yx pfbL8Q3ZGIMh6L31KQcwVUrJIQ== X-Google-Smtp-Source: AKy350YLjqHMzW16SB61ZrKO34D3/TlmDW2rBvoLEyn4qa6+KyWVGCYv7MHLqblqPruYYQP5o2V7iQ== X-Received: by 2002:a17:902:ea0e:b0:1a1:ee8c:eef5 with SMTP id s14-20020a170902ea0e00b001a1ee8ceef5mr7846112plg.7.1681942742761; Wed, 19 Apr 2023 15:19:02 -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.19.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:19:02 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Rajnesh Kanwal , Atish Patra , Alexandre Ghiti , Andrew Jones , Andrew Morton , Anup Patel , Atish Patra , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , 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 , Uladzislau Rezki Subject: [RFC 42/48] RISC-V: Allow host to inject any ext interrupt id to a CoVE guest. Date: Wed, 19 Apr 2023 15:17:10 -0700 Message-Id: <20230419221716.3603068-43-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151903_307033_611D2625 X-CRM114-Status: GOOD ( 13.40 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Rajnesh Kanwal Ideally, host must not inject any external interrupt until explicitly allowed by the guest. This should be done per interrupt id but currently adding allow-all call in init_IRQ. In future, it will be modified to allow specific interrupts only. Signed-off-by: Rajnesh Kanwal Signed-off-by: Atish Patra --- arch/riscv/kernel/irq.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/arch/riscv/kernel/irq.c b/arch/riscv/kernel/irq.c index eb9a68a..b5e0fd8 100644 --- a/arch/riscv/kernel/irq.c +++ b/arch/riscv/kernel/irq.c @@ -11,6 +11,8 @@ #include #include #include +#include +#include static struct fwnode_handle *(*__get_intc_node)(void); @@ -36,8 +38,18 @@ int arch_show_interrupts(struct seq_file *p, int prec) void __init init_IRQ(void) { + int ret; + irqchip_init(); if (!handle_arch_irq) panic("No interrupt controller found."); sbi_ipi_init(); + + if (is_cove_guest()) { + /* FIXME: For now just allow all interrupts. */ + ret = sbi_covg_allow_all_external_interrupt(); + + if (ret) + pr_err("Failed to allow external interrupts.\n"); + } } From patchwork Wed Apr 19 22:17:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217682 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 477C5C6FD18 for ; Wed, 19 Apr 2023 23:31:03 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=QQKiKJ+p/9150huWS56DbLqYvI8oxEHBAIG2y2/XsCk=; b=GXs5XMF1Ss5Rjk uwGkr3WfBe5Kbn+qDFUOeqbnzqt+JcpMRoLm7x3Gx7GpYZdoH9ojMQQG+UgsQOwmib4u/D0ZjRS5r 05ud9JrD78HOuuAO4cgiGWxgfVAxctlsbS8hrbZqVbabX0Q3+TVgiJ67fomH9bNH0JJkydOVQ9vgL XidlSTL76GbWCTg2xuojuviCoFO7bOoGm0GG4xh0FGxc1FlmB2GzTHP0KzWqyVjfXcdgaxuY37gn3 o1RumrlsL32KUoC9rR3KUf5GOW3OFHtRtq6qKOyiHKKGhlFWN7Z30HHrORw18+DoYHOTF2x21zAOP f85MMtA5Bjyw3JPyGHPw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppHGE-006efu-1c; Wed, 19 Apr 2023 23:30:50 +0000 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG8n-006Tcn-27 for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:19:07 +0000 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1a68f2345c5so4229505ad.2 for ; Wed, 19 Apr 2023 15:19:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942745; x=1684534745; 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=ZFcComAa5TlSFZBz7x8OlktBSnuCSnQ4ODeflbYaEGg=; b=0nUEiKekWfmNI3AIrUphLeoG9IshBvYin/IZirKoihkL7Vz1hKxvtZ9S/+qBRCxzGl Z5d0ha3qHQFVtOuYq101NOgtW6elAbMIjX+yLfEnzAp2xs2I4iBDYY5ZkQhxDmkY/7bK cWsa9maVnCVxVpLIasWERRfsvZygnli2tjaAPy0CiHcpn0ucula1hxhQbTtXkquOE/G9 qpketxd8B8maczWCb6ig5SjaDPKhR+D5kNN9pT45ngLGWR38A7D8mV+x7ZjU7uDYBccF cGzTF21j2WAnsV3IO9kxRpdiOoTA+Oi128benfRj77bo/TAAOLLGK42wViYQ/tssBFno STjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942745; x=1684534745; 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=ZFcComAa5TlSFZBz7x8OlktBSnuCSnQ4ODeflbYaEGg=; b=jCEjYJrPZT4jH3vGW/m1TEvlGGtAR0Vzk+YQU69l7g71Gg4a0lfmc0dJcQp1LR7etQ 6kQRO3X23dxWAM3z96RY0eDaoxU2ha76pwnO3Rw4fhUTKKD4KkZ4xAmX5G7JNrgduRXE COAIbTLE3X5Sy3HJepYtgwI3AMr4dFeTMCp4En8QwSNIqAgQ7imnFg+HlsRMyRUWVrGe GdTFC2mdRuNNd3/ZPwEkNNolV2QXx/oPO0BlcXIPJoRnz95EOfXOzhj3yg1GWRjqDMWA S7JM5XX0NIXaSW5MociL3awkXMtFRSvvcq3CtoxIw4liDWwIYE7D1em/XJk0XAmucEHN ew0A== X-Gm-Message-State: AAQBX9cju8jfynxtFX7mCBQX1TkGWgwJsmrqU/5xNidk8MiqQUbYD6AU E7noTbCqM78EBxaJBW1MWz159Q== X-Google-Smtp-Source: AKy350ZdssyQae53kgooCFIYW6ziRRtfOc1jDxPEdCsn1TsvpfXtraJ7uSS11rn4Y1HrD+jtGXUOIw== X-Received: by 2002:a17:902:c792:b0:19e:7a2c:78a7 with SMTP id w18-20020a170902c79200b0019e7a2c78a7mr5521629pla.57.1681942745174; Wed, 19 Apr 2023 15:19:05 -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.19.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:19:04 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Rajnesh Kanwal , Atish Patra , Alexandre Ghiti , Andrew Jones , Andrew Morton , Anup Patel , Atish Patra , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , 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 , Uladzislau Rezki Subject: [RFC 43/48] RISC-V: Add base memory encryption functions. Date: Wed, 19 Apr 2023 15:17:11 -0700 Message-Id: <20230419221716.3603068-44-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151905_692281_37FB6416 X-CRM114-Status: GOOD ( 25.15 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Rajnesh Kanwal Devices like virtio use shared memory buffers to transfer data. These buffers are part of the guest memory region. For CoVE guest this is not possible as host can not access guest memory. This is solved by VIRTIO_F_ACCESS_PLATFORM feature and SWIOTLB bounce buffers. Guest only allow devices with VIRTIO_F_ACCESS_PLATFORM feature which leads to guest using DMA API and from there moving to SWIOTLB bounce buffer due to SWIOTLB_FORCE flag set for TEE VM. set_memory_encrypted and set_memory_decrypted sit in this allocation path. Based on if a buffer is being decrypted we mark it shared and if it's being encrypted we mark it unshared using hypercalls. Signed-off-by: Rajnesh Kanwal Signed-off-by: Atish Patra --- arch/riscv/Kconfig | 7 ++++ arch/riscv/include/asm/mem_encrypt.h | 26 +++++++++++++ arch/riscv/mm/Makefile | 2 + arch/riscv/mm/init.c | 17 ++++++++- arch/riscv/mm/mem_encrypt.c | 57 ++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/include/asm/mem_encrypt.h create mode 100644 arch/riscv/mm/mem_encrypt.c diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 49c3006..414cee1 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -163,6 +163,11 @@ config ARCH_MMAP_RND_BITS_MAX config ARCH_MMAP_RND_COMPAT_BITS_MAX default 17 +config RISCV_MEM_ENCRYPT + select ARCH_HAS_MEM_ENCRYPT + select ARCH_HAS_FORCE_DMA_UNENCRYPTED + def_bool n + # set if we run in machine mode, cleared if we run in supervisor mode config RISCV_M_MODE bool @@ -515,6 +520,8 @@ config RISCV_COVE_HOST config RISCV_COVE_GUEST bool "Guest Support for Confidential VM Extension(CoVE)" default n + select SWIOTLB + select RISCV_MEM_ENCRYPT help Enables support for running TVMs on platforms supporting CoVE. diff --git a/arch/riscv/include/asm/mem_encrypt.h b/arch/riscv/include/asm/mem_encrypt.h new file mode 100644 index 0000000..0dc3fe8 --- /dev/null +++ b/arch/riscv/include/asm/mem_encrypt.h @@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * RISCV Memory Encryption Support. + * + * Copyright (c) 2023 Rivos Inc. + * + * Authors: + * Rajnesh Kanwal + */ + +#ifndef __RISCV_MEM_ENCRYPT_H__ +#define __RISCV_MEM_ENCRYPT_H__ + +#include + +struct device; + +bool force_dma_unencrypted(struct device *dev); + +/* Architecture __weak replacement functions */ +void __init mem_encrypt_init(void); + +int set_memory_encrypted(unsigned long addr, int numpages); +int set_memory_decrypted(unsigned long addr, int numpages); + +#endif /* __RISCV_MEM_ENCRYPT_H__ */ diff --git a/arch/riscv/mm/Makefile b/arch/riscv/mm/Makefile index 2ac177c..1fd9b60 100644 --- a/arch/riscv/mm/Makefile +++ b/arch/riscv/mm/Makefile @@ -33,3 +33,5 @@ endif obj-$(CONFIG_DEBUG_VIRTUAL) += physaddr.o obj-$(CONFIG_RISCV_DMA_NONCOHERENT) += dma-noncoherent.o + +obj-$(CONFIG_RISCV_MEM_ENCRYPT) += mem_encrypt.o diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index 478d676..b5edd8e 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -156,11 +157,25 @@ static void print_vm_layout(void) { } void __init mem_init(void) { + unsigned int flags = SWIOTLB_VERBOSE; + bool swiotlb_en; + + if (is_cove_guest()) { + /* Since the guest memory is inaccessible to the host, devices + * always need to use the SWIOTLB buffer for DMA even if + * dma_capable() says otherwise. + */ + flags |= SWIOTLB_FORCE; + swiotlb_en = true; + } else { + swiotlb_en = !!(max_pfn > PFN_DOWN(dma32_phys_limit)); + } + #ifdef CONFIG_FLATMEM BUG_ON(!mem_map); #endif /* CONFIG_FLATMEM */ - swiotlb_init(max_pfn > PFN_DOWN(dma32_phys_limit), SWIOTLB_VERBOSE); + swiotlb_init(swiotlb_en, flags); memblock_free_all(); print_vm_layout(); diff --git a/arch/riscv/mm/mem_encrypt.c b/arch/riscv/mm/mem_encrypt.c new file mode 100644 index 0000000..8207a5c --- /dev/null +++ b/arch/riscv/mm/mem_encrypt.c @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2023 Rivos Inc. + * + * Authors: + * Rajnesh Kanwal + */ + +#include +#include +#include +#include +#include + +/* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */ +bool force_dma_unencrypted(struct device *dev) +{ + /* + * For authorized devices in trusted guest, all DMA must be to/from + * unencrypted addresses. + */ + return cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT); +} + +int set_memory_encrypted(unsigned long addr, int numpages) +{ + if (!cc_platform_has(CC_ATTR_MEM_ENCRYPT)) + return 0; + + if (!PAGE_ALIGNED(addr)) + return -EINVAL; + + return sbi_covg_unshare_memory(__pa(addr), numpages * PAGE_SIZE); +} +EXPORT_SYMBOL_GPL(set_memory_encrypted); + +int set_memory_decrypted(unsigned long addr, int numpages) +{ + if (!cc_platform_has(CC_ATTR_MEM_ENCRYPT)) + return 0; + + if (!PAGE_ALIGNED(addr)) + return -EINVAL; + + return sbi_covg_share_memory(__pa(addr), numpages * PAGE_SIZE); +} +EXPORT_SYMBOL_GPL(set_memory_decrypted); + +/* Architecture __weak replacement functions */ +void __init mem_encrypt_init(void) +{ + if (!cc_platform_has(CC_ATTR_MEM_ENCRYPT)) + return; + + /* Call into SWIOTLB to update the SWIOTLB DMA buffers */ + swiotlb_update_mem_attributes(); +} From patchwork Wed Apr 19 22:17:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217595 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 A949BC77B7C for ; Wed, 19 Apr 2023 22:37:34 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=3lQ79q1AxbHSZF+8VQReUJvGjz8GC0p/DzzCOQqDgCY=; b=CDdC9LA5YMtV4O vlYZL0ifFcjFO/KzICK4GZlpmbtMPpYntQOliKNGCdbeW6YgMHVyU0i7F031SOdOvq08plQlLm8Ve d8q38J+WkWzzm72u4G3/r+IYIxTS04j3iG8w8BkJeTcIkT+WmjHImBpUcQO8vT1rBg/kOWDapejX6 qNGC3tw8KENrQvB/mHvNGySieYa9AbAdfwoSBTvMhi8HFOaJdaC+7QVb98kAH/+RAVtymhQuycYRj UE+UG2HQeR7hIeRbvCT0umvNHrOmbB3bDQHOBccVkryes3dxX1KfWJFR+atUKD9+VdJjscLYAV8xt tJJuW6f+KVEm9j2fOEgQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQZ-006Wan-23; Wed, 19 Apr 2023 22:37:27 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQX-006WXV-0P for linux-riscv@bombadil.infradead.org; Wed, 19 Apr 2023 22:37:25 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=+bezt4nspa35ZRqzRaZmY422b4QR0Mn7woQKzr9+0N8=; b=gmS+YEvO4UVOxrahcs5hMdHrAM 629b5mpjOyIoh0jUFs7Feu8FB9I8elgIcl3nYixu/mKyKFlLchXNxcWMewjY//rXY2Rm5BghA/d+H dCcwGoUiEmKeKm8/pbadicWmmt6P0b6Nw4caluYJ3kU2ulYz9SFF5wdxD+KfBSis74qEpVwAYfhmn owZTVdtw/SrKVmsBimwIasx7Fx/yhYOD5KD9ROstEfEjFVvqsdvYxjY36oVEpj865trIGHlHH5Mxy x4ViShTfNFM8vwMmdQjYxbx1bzus+MaWXSZ/02nTRT4uY9umYPRrHYsvB5EDhM1OX5Aw1jqL+yeIh tamTuPVg==; Received: from mail-pg1-f173.google.com ([209.85.215.173]) by casper.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ppG8s-00DeOW-Jq for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:19:12 +0000 Received: by mail-pg1-f173.google.com with SMTP id 41be03b00d2f7-5144a9c11c7so274470a12.2 for ; Wed, 19 Apr 2023 15:19:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942747; x=1684534747; 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=+bezt4nspa35ZRqzRaZmY422b4QR0Mn7woQKzr9+0N8=; b=cFqy/7hM+Ny4fP4WIxs4wAIEAjRhQmifvh65+m0WDOJ/jYCumQJdB1BJZNu5ucVQLt vHeOX9JqsZJsPoOkvbrleSPo5GRulHiShRoX7tLw/Bo3NH2vnqKu9FAh7Id3Frv2QuNE +weDjwOUG7PuVaf0Q8I8065eawPZBUZF/IBP3CPxafiXKLMpIPqIxJCnZC8Rka6I13Pf MlvlahxQGVeNZnlElGB4Hwi7P8WFIaVtZscNXzvS3DDLN30pdgEiwWgQCtnYC84qh4Gj EUi0u8wk2hWEG92QVESAvb05CMVa66rE4mAeYWrE7046KCBAGXIObMS7PVHwhD5fW9N7 Ltcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942747; x=1684534747; 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=+bezt4nspa35ZRqzRaZmY422b4QR0Mn7woQKzr9+0N8=; b=I9dseFWM2zbgJAb0mEg6G733W1Cy8kiLJdwSMhWuSFBk4oq9lNTglY+Pg/+YTmcrVY X1ZVkHsNliaHWZYPUbGWKyVqPO7B0eSQdctxov+meLQKAgF39UJBjWHGdykhKQ1+W0HY P3Q2GONdjkG3EZ30X+jfjQ2FlC1MPF/fbEM9STT5GAKO4K0Kwn928svo/KQoN/pD2qbs McFNYInuAjcU6qVRvQVReHrLUTmF5to091L50YAaBZYiQWfwLSxY6ErkmzRwpUYhIzCj pOrTcleXSOzjWHa2WPUQ7ivH8zu4iPyBfNbjy/tls6h5yz5IZLAEhgw89NysGKFC4Rh9 945Q== X-Gm-Message-State: AAQBX9ca7H1VcVwsgpg0AE2VxPCmJUu2MAfyfrJL2dSKm7TrmDewt3/w kRV45TOs9osiSnsqIDpQFGlfaQ== X-Google-Smtp-Source: AKy350bB8ZklTkd1MwNIad8/q9PvIlAh0JulWMyK+P713MLjTnxYs1KPIh7Umu4fPlJwkV2KzfaSZw== X-Received: by 2002:a17:902:e5c7:b0:1a8:1436:c892 with SMTP id u7-20020a170902e5c700b001a81436c892mr5030556plf.14.1681942747426; Wed, 19 Apr 2023 15:19:07 -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.19.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:19:07 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Rajnesh Kanwal , Atish Patra , Alexandre Ghiti , Andrew Jones , Andrew Morton , Anup Patel , Atish Patra , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , 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 , Uladzislau Rezki Subject: [RFC 44/48] RISC-V: Add cc_platform_has() for RISC-V for CoVE Date: Wed, 19 Apr 2023 15:17:12 -0700 Message-Id: <20230419221716.3603068-45-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_231910_864025_5C27DDFD X-CRM114-Status: GOOD ( 11.79 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Rajnesh Kanwal All the confidential computing solutions uses the arch specific cc_platform_has function to enable memory encryption/decryption. Implement the same for RISC-V to support that as well. Signed-off-by: Rajnesh Kanwal Signed-off-by: Atish Patra --- arch/riscv/Kconfig | 1 + arch/riscv/cove/core.c | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 414cee1..2ca9e01 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -522,6 +522,7 @@ config RISCV_COVE_GUEST default n select SWIOTLB select RISCV_MEM_ENCRYPT + select ARCH_HAS_CC_PLATFORM help Enables support for running TVMs on platforms supporting CoVE. diff --git a/arch/riscv/cove/core.c b/arch/riscv/cove/core.c index 7218fe7..582feb1c 100644 --- a/arch/riscv/cove/core.c +++ b/arch/riscv/cove/core.c @@ -21,6 +21,18 @@ bool is_cove_guest(void) } EXPORT_SYMBOL_GPL(is_cove_guest); +bool cc_platform_has(enum cc_attr attr) +{ + switch (attr) { + case CC_ATTR_GUEST_MEM_ENCRYPT: + case CC_ATTR_MEM_ENCRYPT: + return is_cove_guest(); + default: + return false; + } +} +EXPORT_SYMBOL_GPL(cc_platform_has); + void riscv_cove_sbi_init(void) { if (sbi_probe_extension(SBI_EXT_COVG) > 0) From patchwork Wed Apr 19 22:17:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217579 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 89AE7C77B73 for ; Wed, 19 Apr 2023 22:25:06 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Jn/tzlhevHPlKDQf1k9u2FtqwzmEahcNwa2FDWJ3tuE=; b=I65Fx95DeutWJ3 HjQoUyRkCIsJGvIQJyayMxUQKUrWt+dKFexnMzeZrlcrNXmuyINbH3phAeXM7OsSYZm5Q8FBlrz1U jhkaDYSZHgeDTAlGdeZoI5kLZMEMWU9oDSysyDXAx/NttAVIY6SmFbiagwzt1PFGX5f382iLsbKiE tzy9BV3g+Cn2rQhZ3wgpoHxJgM34sa3F9uptK6T4K5VaaEUzVYNykguB5YiS9cwLkJyAw6IMvvcY5 FNd9304layyZzZFDKmEvlfJO4r+P2rVRSyb2lK/5eAUK0HTxqwCygVqsoLhVe3Dlgm9A8iGFFHwPG 4D590GnIixd/pCGM+i6A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGEW-006V0O-1U; Wed, 19 Apr 2023 22:25:00 +0000 Received: from mail-ot1-x330.google.com ([2607:f8b0:4864:20::330]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGET-006UzR-2z for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:24:59 +0000 Received: by mail-ot1-x330.google.com with SMTP id 46e09a7af769-6a60460a23dso307202a34.0 for ; Wed, 19 Apr 2023 15:24:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681943096; x=1684535096; 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=D8vKC+DATLxW+QWgEBtVVKhLwPezg/zgsscm2TPJ1sU=; b=VQdfMegg0sotRUdUBMTnRu/PoUpfepJVSIVszY0v5nY2tBbv0V9zTwYG1M9RQVUUHf wSAAM2OycooUxPKB+ectrhrNclJs+GFx1PaBr2OFw8YbkY5UsJBLwzZMzoteEfTEqd5Y KZzAHqRGBnCCVCNcaR6eLlUbBlIv6w1OatwOXO9/nD8Z5G877MgPmpoV7ieCBMtugPs9 g739nNPT520xRNBH+mCMsniAFe2Okvc+gk+b4mtnfXJnKP2MiTKlSctEliGWXjTPI2pi WNInFt91QRPPcJ5BHrx4MzAcZDYKN/79L75Lgh6r7qkyHqlyTMMOzaBz6tzaY2Ix4ReO QpTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681943096; x=1684535096; 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=D8vKC+DATLxW+QWgEBtVVKhLwPezg/zgsscm2TPJ1sU=; b=X8v0MwrWpyRgaHwTqjt0nUed3gYs4Senh03wlNg3TdRZkaxh78+ry1Ahprl8Uu+Ump WL8EkgG7RCcukj6eACQhARNGsSm/NnFKECAcquttZSyaIMFcNkIfc5wru6bx0D/QXydJ f3E0WjvDQbgFTeqoVYngXoa25Z357GFpCwOkT9uD/ct0Hjd5WzybdJkXVQ+A1Yxf21qW kZkantp8uplUn6EtzF11GvqnDPI9Na9DO0l14FHJLE/BqPZj9gxQKkwdqJm/vyt2Cf5V GqGT94+vToBk5UuZFelGEfk8Tm9re036xPzRlweotsEC5QRRq5+/dae0zH+dw/lCvJNT doxw== X-Gm-Message-State: AAQBX9dLXfvSp7gnbIONvKZuH3qaxLurqcjsaSPXQZfkoJT0JDTlttuM vugdXnMwM/OR/jVJWYOq27xyUcgp4ze5GgvEPtQ= X-Google-Smtp-Source: AKy350YMt1GXaXJLEtI/tcFCT7P2BRkKgLnzDade8rOaHZzTbglX9T86uEwEdM3HEdy8DD35oL3itg== X-Received: by 2002:a17:903:3013:b0:1a2:8940:6da4 with SMTP id o19-20020a170903301300b001a289406da4mr5416617pla.29.1681942749582; Wed, 19 Apr 2023 15:19:09 -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.19.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:19:09 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Rajnesh Kanwal , Atish Patra , Alexandre Ghiti , Andrew Jones , Andrew Morton , Anup Patel , Atish Patra , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , 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 , Uladzislau Rezki Subject: [RFC 45/48] RISC-V: ioremap: Implement for arch specific ioremap hooks Date: Wed, 19 Apr 2023 15:17:13 -0700 Message-Id: <20230419221716.3603068-46-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_152457_963403_1A2CDE31 X-CRM114-Status: GOOD ( 14.50 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Rajnesh Kanwal The guests running in CoVE must notify the host about its mmio regions so that host can enable mmio emulation. Signed-off-by: Rajnesh Kanwal Signed-off-by: Atish Patra --- arch/riscv/mm/Makefile | 1 + arch/riscv/mm/ioremap.c | 45 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 arch/riscv/mm/ioremap.c diff --git a/arch/riscv/mm/Makefile b/arch/riscv/mm/Makefile index 1fd9b60..721b557 100644 --- a/arch/riscv/mm/Makefile +++ b/arch/riscv/mm/Makefile @@ -15,6 +15,7 @@ obj-y += cacheflush.o obj-y += context.o obj-y += pgtable.o obj-y += pmem.o +obj-y += ioremap.o ifeq ($(CONFIG_MMU),y) obj-$(CONFIG_SMP) += tlbflush.o diff --git a/arch/riscv/mm/ioremap.c b/arch/riscv/mm/ioremap.c new file mode 100644 index 0000000..0d4e026 --- /dev/null +++ b/arch/riscv/mm/ioremap.c @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2023 Rivos Inc. + * + * Authors: + * Rajnesh Kanwal + */ + +#include +#include +#include +#include +#include +#include +#include + +void ioremap_phys_range_hook(phys_addr_t addr, size_t size, pgprot_t prot) +{ + unsigned long offset; + + if (!is_cove_guest()) + return; + + /* Page-align address and size. */ + offset = addr & (~PAGE_MASK); + addr -= offset; + size = PAGE_ALIGN(size + offset); + + sbi_covg_add_mmio_region(addr, size); +} + +void iounmap_phys_range_hook(phys_addr_t addr, size_t size) +{ + unsigned long offset; + + if (!is_cove_guest()) + return; + + /* Page-align address and size. */ + offset = addr & (~PAGE_MASK); + addr -= offset; + size = PAGE_ALIGN(size + offset); + + sbi_covg_remove_mmio_region(addr, size); +} From patchwork Wed Apr 19 22:17:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217594 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 B05BAC6FD18 for ; Wed, 19 Apr 2023 22:37:33 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Y+sZdryphwz+qAWHq/kPD+mgA9sK0pEILUNEhk7rFvg=; b=LlgNuP2HS74T6/ tDxJmMG9I9Jn23Y6E/vgXO114+a1xGbVWlA+5ZseWdU8Nruohb8T6coEuow6A0pg1QVJT8NdaWj2I KI5/g813sQ46eHEhtOGiQGQ/TWI37/n8NItG8ucis2LZayxHeTre2q2PBSburDHPr0frV3ogn6Ivz dkqGTToIyxbEVUBtRT9Q8prmfkxMj22mpfG5gWSONq0sGKPspTF3MVi+A15DMtUwVxBoj2ESxcjVX Co9YPbwSrPs+uH2SXiYPaLx8JCtAVK8qHgEErOdApO07rFJgC9qBR8yGL2AyYFwwFMlOMwjpMPMLS WNULITZjsGSa0qli5TLg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQY-006WZf-1K; Wed, 19 Apr 2023 22:37:26 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGQW-006WXV-07 for linux-riscv@bombadil.infradead.org; Wed, 19 Apr 2023 22:37:24 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=ecSGlantlLzvA0G9VPQY9TXgG8EZEn7xksLEzRIVmIY=; b=f/mLc5TvFRoVXRHYGVG+fr1fX6 AtnhydbomwHzBevDQGFtszsn4ZAy2yzwIWn05UNXQNUDgKeJ+TtbRzfASEYHbVSafbA/PFW84aCVm RR0lzEoICIzJ7IeB8n1ubGj+v3n1Z1tZNwKhRSOLtidaE+x9Kwij/oi+yua1742P4Dnfeln6uTeyW 8kA3hEFnKRFy4GolLYqI+xJMJSWyQ7djXXY3/uWzdHgXT9MCl1PFPvBqIOYGcrJP7zb+ju8ZkZjgt Ypg51wN4aQO+Q8mnlnH64hw5QPgGBUOOZDg+4Kqh2e9rSfUiwQEye3Ir2FlvW4F4xuFZFMCDbiyk5 5fGpqFlg==; Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]) by casper.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ppG8u-00DeGj-HS for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:19:15 +0000 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-63b73203e0aso2788962b3a.1 for ; Wed, 19 Apr 2023 15:19:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942752; x=1684534752; 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=ecSGlantlLzvA0G9VPQY9TXgG8EZEn7xksLEzRIVmIY=; b=c21zAYv4W/8efVpj9s/CrAgsnyC7GRoXBj+SQ9uvGvmMRjbXi9C/l4ApXeL/28qOSL J5ZZ+TVmCUP3cqWaEnHlrNG9d5s4uDtI+r8DS5GpmAI6Te5+PPhg0JPesMsJ6QkBBUKM i2T5E7+u0KUNpPzwSPIgIDhWBA0x55J8INy/+9T5mEzV2Ub7rIt7zSh4bB9QChjdbp48 Ndc6bow+xP/wEEVhqSShIWQJsWlE7p19+1r0llK0omkaJDYaHRCgD+bcV/3pj1FMTVTV cqaMAtAzWTd3lG4iAZAgavUNbrYuLSPh85yDNfwJPgtXxhKdv5Whjg+e3B66BtLz82q2 i0eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942752; x=1684534752; 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=ecSGlantlLzvA0G9VPQY9TXgG8EZEn7xksLEzRIVmIY=; b=Fz/dPnM7+ojgw7wc3dkL+Mxu0mKEwdIfxqv1VGaiilCENR3E5DaelYE49aQ8CMTnHE pm3MpkepXcODCqUJV7RA2qlt48pSIKqDsQdbbHDbUOPBO+wqLd+C/8bB/YwcEa62gjrF 32/9OHNGBg283ABNxMpEGp1yIR3LNvJ+nCMjxMV165na5coHGOk0Y37beRhLNr4+Oy6p hvlo+m0/0N3j5Pc5e+kQszGdoyTBWqUCdjBo6ccSIzj+Vg1qiyUY5XOiV2WCIaPfRH3Y SOFQQagS9dRPkdvcc/B5ueWb9c+2aGOJr94Lo0cVXPcagn78Fu62fG7HGrDMCADeMH9e D7Rw== X-Gm-Message-State: AAQBX9erNf0tz6YnMjhaqWqVOnKrICh0TfT4w1AoIQshCzq+/ir0mcbE pmDMZqEipGxUEVehBk1ZL82jsA== X-Google-Smtp-Source: AKy350YKfPHbIymM6qmHQFNTIkUnRhQSV2hEDBlfRu6z0TKA3ibQ947LdlZE55huq5S43nAipp/NMA== X-Received: by 2002:a17:903:22c7:b0:1a6:3737:750c with SMTP id y7-20020a17090322c700b001a63737750cmr4095049plg.21.1681942751934; Wed, 19 Apr 2023 15:19:11 -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.19.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:19:11 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Rajnesh Kanwal , Alexandre Ghiti , Andrew Jones , Andrew Morton , Anup Patel , Atish Patra , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , 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 , Uladzislau Rezki Subject: [RFC 46/48] riscv/virtio: Have CoVE guests enforce restricted virtio memory access. Date: Wed, 19 Apr 2023 15:17:14 -0700 Message-Id: <20230419221716.3603068-47-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_231912_636221_A404B1C4 X-CRM114-Status: GOOD ( 13.06 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Rajnesh Kanwal CoVE guest requires that virtio devices use the DMA API to allow the hypervisor to successfully access guest memory as needed. The VIRTIO_F_VERSION_1 and VIRTIO_F_ACCESS_PLATFORM features tell virtio to use the DMA API. Force to check for these features to fail the device probe if these features have not been set when running as an TEE guest. Signed-off-by: Rajnesh Kanwal --- arch/riscv/mm/mem_encrypt.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/riscv/mm/mem_encrypt.c b/arch/riscv/mm/mem_encrypt.c index 8207a5c..8523c50 100644 --- a/arch/riscv/mm/mem_encrypt.c +++ b/arch/riscv/mm/mem_encrypt.c @@ -10,6 +10,7 @@ #include #include #include +#include #include /* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */ @@ -54,4 +55,7 @@ void __init mem_encrypt_init(void) /* Call into SWIOTLB to update the SWIOTLB DMA buffers */ swiotlb_update_mem_attributes(); + + /* Set restricted memory access for virtio. */ + virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc); } From patchwork Wed Apr 19 22:17:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217583 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 0AD0BC77B73 for ; Wed, 19 Apr 2023 22:27:24 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=HQtoLmHK/P/wqRf2ZKY1j/Xfa9RUdS4RrN/MzKMAT3A=; b=rNFB6IVD8/Fk1v Ydx7VuKrtLx+oE1ZQKZaJPjUpurAj/vACvb/wSJs4FbOrBZ3rfGdi0kqEcqTWnruWhOiuU7Lr8kfP iofQ+MI9k9XsqvnPE0aOVKKwFjuK5lKzjj+7DGyTz83J1pqh1R43JU1eJqrJjPzpetlXf4GEDE7WB 8lxVVQZHxmKcpgP4nv98Bf8Jnf7M74JoncIIJ17Zg9uWw7P566cmA9uWCzPTsQ+XpofZOJyRF25Bv KrNI6oyQi3hbzZZoTuIZyrGdXhr0uEK5xPxSl+TefNF3asOK0x6YWBf3baUJxapyhwa/opqXYvP18 THlqYTjOokZ23S12SvWw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppGGk-006VPZ-0L; Wed, 19 Apr 2023 22:27:18 +0000 Received: from mail-ot1-x330.google.com ([2607:f8b0:4864:20::330]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppGGh-006VOo-1f for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:27:16 +0000 Received: by mail-ot1-x330.google.com with SMTP id 46e09a7af769-6a5f21a0604so274283a34.2 for ; Wed, 19 Apr 2023 15:27:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681943234; x=1684535234; 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=8fg2apWNkO7XMTsfLYU3rTZlWorsx4wHesUuN9ZGrN4=; b=ItoVYcTFBf7zsnYDoxjd87iRY341AJI6ZgLW2Lf5PDSjv8cy6mqUqNZLI1Ulr8gUJ7 q/yAUjrCoP+vd9RPobIMZCejLwg0K3m0Fzcyh/bj/ymObZHv6H25k4zWUQ35MdBGioWB TKpfwDBdCh9/VPa4ozbJY5z9bitXy7OYB/DmEqISYDBPpBu2/8Ji398y2Zxv8jn/QYYE 5aFlsJMlKDhamnuJwxLiGxMRu2NB/gZyA8P8X21MrEKFXYAq/aOX3vMIyo9buRCJQo8X IH54PpjgTxuyrWPAzjqy02aKjtEcSIeEKSVnMZs0ZjYFU6PJblj36f3LE6/NBmKo03Xq 8bLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681943234; x=1684535234; 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=8fg2apWNkO7XMTsfLYU3rTZlWorsx4wHesUuN9ZGrN4=; b=LLlPD7XvC0LFOesgYc0cueoduEbsvdKUl4yFlLlSDNTlA7broM9eSfbWIGQSJOuKmu WOUvQa1xE9s0wnOsCEj3mf+LlEjr2s6bOn1OSh5GVGfMV5B7Fu6MMO08miT3n60D7/3u nt5geOSp68AfJOVHjoTaudm3A0VZC5yltvmrz7RIJCmCl4f9N6nkrwq7KdsmVufjhtb8 7CLyzx8+j/Zhw9aBTkIf2DQVmTcaU0fVjoQbG7im8wvZ5BFf+pipQPzqAKTYG+x1u2YO qw0ph/CdWwXIC0o/QIvi9PunUwaXLnf8nnooRDJ/S6avrHPqhCe/klWXI5jCeqj8jYDQ 9PXw== X-Gm-Message-State: AAQBX9fCIbW5E3nyL4Oh1gfRi2qZrDIQ1Bd1KKFcokPGT4bzhOhlgau5 sp5wqFZhW8TF/U8nn5EzMA3KJv3eBJQr8o+M5Ek= X-Google-Smtp-Source: AKy350bcbgE/EJ3Ej08OUUDHS562dStL1nY59dRLyQva3UBkO8qD2X/slMFNk57ZH7aSQKcUf51qPw== X-Received: by 2002:a17:902:a504:b0:1a1:be45:9857 with SMTP id s4-20020a170902a50400b001a1be459857mr6646673plq.1.1681942754165; Wed, 19 Apr 2023 15:19:14 -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.19.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:19:13 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Rajnesh Kanwal , Atish Patra , Alexandre Ghiti , Andrew Jones , Andrew Morton , Anup Patel , Atish Patra , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , 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 , Uladzislau Rezki Subject: [RFC 47/48] RISC-V: Add shared bounce buffer to support DBCN for CoVE Guest. Date: Wed, 19 Apr 2023 15:17:15 -0700 Message-Id: <20230419221716.3603068-48-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_152715_552952_474EAFC9 X-CRM114-Status: GOOD ( 17.35 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Rajnesh Kanwal Early console buffer needs to be shared with the host for CoVE Guest. Signed-off-by: Rajnesh Kanwal Signed-off-by: Atish Patra --- drivers/tty/serial/earlycon-riscv-sbi.c | 51 ++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/drivers/tty/serial/earlycon-riscv-sbi.c b/drivers/tty/serial/earlycon-riscv-sbi.c index 311a4f8..9033cca 100644 --- a/drivers/tty/serial/earlycon-riscv-sbi.c +++ b/drivers/tty/serial/earlycon-riscv-sbi.c @@ -9,6 +9,14 @@ #include #include #include +#include +#include +#include + +#ifdef CONFIG_RISCV_COVE_GUEST +#define DBCN_BOUNCE_BUF_SIZE (PAGE_SIZE) +static char dbcn_buf[DBCN_BOUNCE_BUF_SIZE] __aligned(PAGE_SIZE); +#endif #ifdef CONFIG_RISCV_SBI_V01 static void sbi_putc(struct uart_port *port, unsigned char c) @@ -24,6 +32,33 @@ static void sbi_0_1_console_write(struct console *con, } #endif +#ifdef CONFIG_RISCV_COVE_GUEST +static void sbi_dbcn_console_write_cove(struct console *con, const char *s, + unsigned int n) +{ + phys_addr_t pa = __pa(dbcn_buf); + unsigned int off = 0; + + while (off < n) { + const unsigned int rem = n - off; + const unsigned int size = + rem > DBCN_BOUNCE_BUF_SIZE ? DBCN_BOUNCE_BUF_SIZE : rem; + + memcpy(dbcn_buf, &s[off], size); + + sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE, +#ifdef CONFIG_32BIT + size, pa, (u64)pa >> 32, +#else + size, pa, 0, +#endif + 0, 0, 0); + + off += size; + } +} +#endif + static void sbi_dbcn_console_write(struct console *con, const char *s, unsigned n) { @@ -45,14 +80,26 @@ static int __init early_sbi_setup(struct earlycon_device *device, /* TODO: Check for SBI debug console (DBCN) extension */ if ((sbi_spec_version >= sbi_mk_version(1, 0)) && - (sbi_probe_extension(SBI_EXT_DBCN) > 0)) + (sbi_probe_extension(SBI_EXT_DBCN) > 0)) { +#ifdef CONFIG_RISCV_COVE_GUEST + if (is_cove_guest()) { + ret = sbi_covg_share_memory(__pa(dbcn_buf), + DBCN_BOUNCE_BUF_SIZE); + if (ret) + return ret; + + device->con->write = sbi_dbcn_console_write_cove; + return 0; + } +#endif device->con->write = sbi_dbcn_console_write; - else + } else { #ifdef CONFIG_RISCV_SBI_V01 device->con->write = sbi_0_1_console_write; #else ret = -ENODEV; #endif + } return ret; } From patchwork Wed Apr 19 22:17:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13217514 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 8DA19C77B73 for ; Wed, 19 Apr 2023 22:19:27 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=6zI1FKlzHnMp/zIiCadgYTexIe6tmr9IjZSjvHFo1vc=; b=ZL37FI8UWIz6j5 zLinFbHLoq16y1QQVxjOulBceL8ltSu7+ucOBp8x5O4FmsCkbopdy0tPUBaiTm8Ric0HBVzTQR9wI xgHBlJR4Atve5sLhw9xSjkNCKzXDzOxLxSugeutiHunYBvxZ+BbpDb8ghomkMqb+YzGLbQdJa8hlO hysjT2Q3moD2OP6377G6pU+Wf8afItvmAcNRuwdhca7F5dIW3k9n2qRsLW/qosQUBvfcEnnUD6k5j pyTzaqpLfxIRYxoUVhsoajabsZ2EWTiUMUzu0x85j8f5b/kFUOoAgNOjHF8vyYjbdWjZ1qrkOhgQ1 X9PKwyj2BHQChZbigGnw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1ppG93-006Tjj-1F; Wed, 19 Apr 2023 22:19:21 +0000 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1ppG90-006ThT-1v for linux-riscv@lists.infradead.org; Wed, 19 Apr 2023 22:19:19 +0000 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-1a9253d4551so4770525ad.0 for ; Wed, 19 Apr 2023 15:19:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1681942756; x=1684534756; 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=P8D5qQaAsWRyihppL8lxgNE7a17gNAwjOwZEXYQRMgE=; b=KY3qeDkuqx7ojUE9edJo91m4yJkLaFWv5WBr0z1XBW37m3Ugju5gvpwIFDQff0jKms +hgKcILxSjrfAbaSn/gsJas5jFwQYwJwvq++pUZKG2L4GsKhyqTPcUu6xp5ypSE2166a M9MbN1bFoVVAdPgWpFhugIMpkrykve+HvKmx+0rzvYuv5b7fjLguHE92nxuCMfaazyT0 EjqjUkf7F7QIDOnYykXPXccd7CL3t2WBS+Rd2ML5ADa2yHIE2iBIctAwVUyHJ5XwQDtq 3t1dyXic09IH7MeVfo82H9l2jXFUHVEBVSeBHFYy8QxMmo1hXkdwnKJUN8t2qaLHF1ux +z5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681942756; x=1684534756; 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=P8D5qQaAsWRyihppL8lxgNE7a17gNAwjOwZEXYQRMgE=; b=JE52ZKvI10MSfOKvZTew1es2NLploNrboFmJZZao7MfcmKPQLBsC0OOUbR5SObFwxE sElBkF+S5SyGQJNFEfrsUdtxq3GMr1CmjH+Zz7qcoBID0mgjtGVdNOEJtaOiApirIEdn 1msVg6hLjYPPyF/8qFxFhlVgNsw9leE1hQjDordyE8SB9lO/MoaDA+K7T7UUR9RDF8sR TYSVjqu3Yv0WX3h+j+97Lb3Tx8vGTVKEQS/hTTjraL0HikDOX5VotaFcjx/EvlWpNyv3 kVkTODTaNr3O7K1tdegfN6CTaEaI+wehoBCrKGTjDKeM2NKLGaex6H7PHBjSyhK5gts/ fFlw== X-Gm-Message-State: AAQBX9eu8sM+qpYw/nrneWiioRa6xhVL0OxYtMlbQvHjp7FB9o5+ncQE FbrI8LuerT4iwtm1/RcUo0kjLQ== X-Google-Smtp-Source: AKy350ZigrT04ztZNfYo7vxPOIAk65YMZhdMrGeFS26zGyCIXgxICUZh5tguDmZpxCxB9++y2y9iww== X-Received: by 2002:a17:903:2288:b0:1a5:2db2:2bb with SMTP id b8-20020a170903228800b001a52db202bbmr8899588plh.15.1681942756356; Wed, 19 Apr 2023 15:19:16 -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.19.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 15:19:16 -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?b?QmrDtnJuIFTDtnBlbA==?= , 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 48/48] drivers/hvc: sbi: Disable HVC console for TVMs Date: Wed, 19 Apr 2023 15:17:16 -0700 Message-Id: <20230419221716.3603068-49-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230419_151918_632767_15E2D8C6 X-CRM114-Status: GOOD ( 16.77 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org If two same type of console is used in command line, kernel picks up the first registered one instead of the preferred one. The fix was proposed and NACK'ed due to a possible regression for other users. https://lore.kernel.org/all/Y+tziG0Uo5ey+Ocy@alley/ HVC sbi console makes it impossible to use virtio console which is preferred anyways. We could have disabled HVC console for TVMs but same kernel image must work on both host and the the guest. There are genuine reasons for requiring the hvc sbi cosnole for the host. Do not initialize the hvc console for the TVMs so that virtio console can be used. Signed-off-by: Atish Patra --- drivers/tty/hvc/hvc_riscv_sbi.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/tty/hvc/hvc_riscv_sbi.c b/drivers/tty/hvc/hvc_riscv_sbi.c index 83cfe00..dee96c5 100644 --- a/drivers/tty/hvc/hvc_riscv_sbi.c +++ b/drivers/tty/hvc/hvc_riscv_sbi.c @@ -11,6 +11,7 @@ #include #include +#include #include #include "hvc_console.h" @@ -103,6 +104,10 @@ static int __init hvc_sbi_init(void) { int err; + /* Prefer virtio console as hvc console for guests */ + if (is_cove_guest()) + return 0; + if ((sbi_spec_version >= sbi_mk_version(1, 0)) && (sbi_probe_extension(SBI_EXT_DBCN) > 0)) { err = PTR_ERR_OR_ZERO(hvc_alloc(0, 0, &hvc_sbi_dbcn_ops, 16));