From mboxrd@z Thu Jan 1 00:00:00 1970 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 88A4BC7EE20 for ; Wed, 19 Apr 2023 22:18:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 15725900019; Wed, 19 Apr 2023 18:18:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 10EF5900003; Wed, 19 Apr 2023 18:18:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E9B4F900019; Wed, 19 Apr 2023 18:18:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id D6E9B900003 for ; Wed, 19 Apr 2023 18:18:25 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 961FB120381 for ; Wed, 19 Apr 2023 22:18:25 +0000 (UTC) X-FDA: 80699555370.29.8A6F6BE Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by imf11.hostedemail.com (Postfix) with ESMTP id CCB9740008 for ; Wed, 19 Apr 2023 22:18:23 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=o8cl3vLF; spf=pass (imf11.hostedemail.com: domain of atishp@rivosinc.com designates 209.85.210.181 as permitted sender) smtp.mailfrom=atishp@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681942703; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=vFCr/CCby/ixIZR1vcNNYtJI+Hn/JpPKkTFrcmf/240=; b=F7JrBptEQHX/I1vqfZek9/UfcqKtZE/e9dcAbsX38Ti0d+VuDOCLfSC2npmyYHbXkxZ1Yi EG16KpEnJGSxy2WUZ8lXZF1ckQdTUV0pDEYETxvvXwVPkX1PhQTW9Q57I6eafifqzF/qnW K1F6/vJV2KyvhEluwaFZrWAb4SExL3g= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=o8cl3vLF; spf=pass (imf11.hostedemail.com: domain of atishp@rivosinc.com designates 209.85.210.181 as permitted sender) smtp.mailfrom=atishp@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681942703; a=rsa-sha256; cv=none; b=SK79rfmIB1jwxBtgJXc86LDGIafA2Ui6VtHk+krJ6xR4YKCOhbx2vm0ZbHK8eC9VTj+Ia5 f7H3nsv+YnOYfyEi/CIGRZxmqartNcfOcQuhkEwiSsGfRnbTlQLmUCLPKoM8lAXI0OwzUX AQg3qhE77w1KekwMN1nirrPzZ7yrUgg= Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-63b50a02bffso366834b3a.2 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=K4g8jMAw0WNFPtqQqIWoK7XUt+da7y9dZNpjwizadpdxoaX2Tdp/FeoSxvz7UQl/0f 3Y4gFHYTFdcXO1kg+mLFmy4VouiyiSsLvXNRlz/N+LjFTirq49ri/Qv8u74kVTYrNaxQ k6zeX4LVWzkuSWRzOyKpuAa1EkaaIrFw4aDtKB3ZHy/UxDlq93KB0fj8j+L0fBVZXN4v y+O7xDrBoun4dAzY0qXzpSifIigFSOO9k50Yoz3g2pF/cziNYyg1d6ZMAQlBovlQrZE6 gqyScRGB55ZHSziL2uTbqvNcj0O2yRs93t6i6h3yoVnuXbp5XcXZqJKyef6jZ57kB/gu 4Q8g== X-Gm-Message-State: AAQBX9e9mM24e9OxoiHnQgaAftHF+y9SCaZhwHZnv9oK00zK04W0ZcdJ hGsGciGpseJRwzodLtj9AXrJCg== 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?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 , 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 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: CCB9740008 X-Rspam-User: X-Stat-Signature: hngb6c5qu6eqx35rc85tcnuiws4hedxt X-HE-Tag: 1681942703-635915 X-HE-Meta: U2FsdGVkX1/s6d5f4jE7vBLuRXcUjhtQaYYEsOAYhjfs51qjjILRP6nL70A2OEhxt9F5yL3ZiVMi113MfFlidcXedJbwYq6oE8orKKzVfjEAuteXFxdjpmUxovi7rTACX3b+i0fcZZRZkfdsZ31YhKjAO8Xaf/AidAVTB2/kNatLDG6uhNkUMZtj25lu4+SZk431wqHi2P3U7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jmrF8fpn2ziHzUBupQVV7FxjsW++TeLUq9VfQRVU8dgAdLqbqDnF+dPoCfG6+/yrQ0k5pxx8pYJ2sJiCGGIRdNWSbkByi81F2qAzlXXip7uQtfQ0QJLoGVFiaqXcKUU0sNJ9mDm+LnkichDGl8Lx74PAtF/s6b4wknmxpjflmP4KvjXs0Gjd1p20XLFIFtBnyMC/5gMJuPE7vNlp/60xBXVo37tDsYsQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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(); + } } -- 2.25.1