Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp913472rwr; Wed, 19 Apr 2023 15:20:59 -0700 (PDT) X-Google-Smtp-Source: AKy350b69SIhwy155wFN9wwrQaBFMO6N7mi3Qui84Y0QVztKVHtlhgxjKduXLxJvZm99pBCeAlN9 X-Received: by 2002:a05:6a00:2355:b0:63d:27a1:d578 with SMTP id j21-20020a056a00235500b0063d27a1d578mr6396342pfj.20.1681942858853; Wed, 19 Apr 2023 15:20:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681942858; cv=none; d=google.com; s=arc-20160816; b=cEkBPT/NA5oXpz7KXL4VlfvM5nWuhWnRKU0Oo6WDDLfXxDZ0qUfW73lLFlJ1nKdRgb vxYm9grKKnnMMe88MQTPUXZagLyafgUe1oSXNBAx2Fy/Lihcf1YDIcXM7xG6Ds/0jbI3 xcoKPyffQX+fkrqFN8+ak4psq4+X0W3MCl3fTK5PoTp1Gle8zVQrw0lJpTsA8dAZ8l7F R81kYydWsoPiFXzCgrLD6qdTiH12grW7Wov2kjCBjAvXOA3grfNegyGESjoIYL4ilFjq d4yy3kSDLKDCH0gLa6pm0NtLy0bbxkwFe4/0bpYhlzqXsKCl0I01kOYTgpTH2/D4s+o7 6LuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=BVskLFhSfykEs4eltGnJpzQVboIqM++EFU/NG1sE5NQ=; b=UxT2ibpdI8PD0o2TBfjf2BbEywtKGLCnln85uWimDd8WCEHwCXxEQeNmeEAx4TeTKG 8iR19yUUqSUx5R6MKePziWL3uSg4MPqM3KGKRzPEEThJ2AlWIYvmGW3LzOWwphYhQQ1/ t31vf6zFKdXk2jVJVJHPtdnECdlXLUoJ5nP3mktChIALaP0gr2ir8euzahFdWzOvWZs3 z3n+o/fltrOfHioZpK+c9CsoS3bWHvKA+qKEsyxpdCOpf99IIj7LV+K02WcjQLMiLx2b TX+IKVQX2gUkRaYGJV9psh0RkbH6XXRa2/XknJ+vifF+LsyZOp2HTX2wCKcWNWu7rY6a 1MzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b="SO8VMn/g"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a19-20020aa79713000000b0063b89234a5asi9921624pfg.371.2023.04.19.15.20.47; Wed, 19 Apr 2023 15:20:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b="SO8VMn/g"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232858AbjDSWTv (ORCPT + 99 others); Wed, 19 Apr 2023 18:19:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42192 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232273AbjDSWTI (ORCPT ); Wed, 19 Apr 2023 18:19:08 -0400 Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BFBDC9748 for ; Wed, 19 Apr 2023 15:18:27 -0700 (PDT) Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1a80d827179so4504245ad.3 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=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=BGbDA7aLZjzolbBZsNIPvIk3tt0I6cYaNorubKmmuD8rbDXAI8cmoC165AjPqqP29g YfoLGS6Vj2xMCzAUYmbJ2hSgEg7z+U8wW7hc8cXNt3wnKqBEZZ6A0liWioD4T4LJvZpa LwB2IPCLaFG854rqWqN94qw1k+sjskdWcEJHYTVgspLHjtEAsa4g137+nOdBdhzfe5pI xeDPlBj3gi/pX7HJ0ueGvQQgvZ3SeFClOywNTVQ5AkREpQjDpBBR3RlLKbPN9HlyKvAS n8/CZLWwSrIXCFfe7YrIj6IsEozBhaMKGWFjs7ZrxScbjwR9RESvLAslzUZ09ol3Hppe FAxw== X-Gm-Message-State: AAQBX9cPzoZ2MdDfA60l4X4JUkBTuF6nVYUGHHCyBrWieziq3h514omm E5+PgmvT0axmM6ezneDIG0E5GhX4+YSP4ialEAM= 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?q?Bj=C3=B6rn=20T=C3=B6pel?= , Suzuki K Poulose , Will Deacon , Marc Zyngier , Sean Christopherson , linux-coco@lists.linux.dev, Dylan Reid , abrestic@rivosinc.com, Samuel Ortiz , Christoph Hellwig , Conor Dooley , Greg Kroah-Hartman , Guo Ren , Heiko Stuebner , Jiri Slaby , kvm-riscv@lists.infradead.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, Mayuresh Chitale , Palmer Dabbelt , Paolo Bonzini , Paul Walmsley , Rajnesh Kanwal , Uladzislau Rezki Subject: [RFC 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 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 -- 2.25.1