Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp6161134rwl; Tue, 4 Apr 2023 08:37:48 -0700 (PDT) X-Google-Smtp-Source: AKy350ZLlAwtacw+dn6cZLlGgmhdcZVibmTv17eAC11wDJF67fImyxjfLJozVt9pnycnpWtWVWLy X-Received: by 2002:a17:90a:414c:b0:23f:7e48:4f20 with SMTP id m12-20020a17090a414c00b0023f7e484f20mr3346572pjg.41.1680622667770; Tue, 04 Apr 2023 08:37:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680622667; cv=none; d=google.com; s=arc-20160816; b=q6rXHLS/yrl6U/BiGY3HLa1P5N7OfZ2ObBB/0V06kACF/Wwpa+ntAXvvCdhRNa0vZG TuSvyqD7MXkjAU4+YE0g/K25sJcAsh+NDCGuqBayH6APZz5Qsw/2vHOSGJ4qH2u90MpH THiv1bl0pWnkd8UiW4RSYbIfv2BMOek94+L6XghjGVrlsPNPEwp7TVfEAWu4p9o0oZge rBhLLRxnkOGCCzUo9UFhQuym8Mji4qHHUM5h+h3cMXSzTfzs9DucBtlAwm4l96NBalG6 UOc5v6BNyvZ2+tBenh5vlIa4e5LJaOJ+NWzNk4qvNvqJYYEdKphpu1K2aDW5gKJSyS85 qTBg== 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=8FseYZBc2vbVsgmp58SjYhTfgYw9HMD9OT7D84agdy4=; b=IE2/QUj3grycQbk2T0QpldVaF3UToTYOP96Ovevj3f7zIaGnk4K4I8m/F7pC4khQnp ax1STiNAMu4WER95s8Tx1FbvbGJyxekyZ1qf5dg2Ot0KUtLyCEfDBvXwNbERnZvkElJo ZI8/+Uc/iPSk2BXP9C0nYXoULCJudvexebWC/be3iV/TCLbTDM15HyQQhMg1BPE9a6/R 8AywSWm3gNkeWFJ/EnJDd1di+VpJpXH86VB2pd4YOTTHxIVf049k/8N0bVYMvd5P35Zo TvwihIL2DcclCTWQ7RoVqJzd1RVdaAWMwbWxZUM4KQQySRIK+lmzvp6ufntcCE/GJvHO kWIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ventanamicro.com header.s=google header.b=CcF6k9bs; 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 o5-20020a635a05000000b0050b3064cd60si10735423pgb.416.2023.04.04.08.37.16; Tue, 04 Apr 2023 08:37:47 -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=@ventanamicro.com header.s=google header.b=CcF6k9bs; 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 S235856AbjDDPf7 (ORCPT + 99 others); Tue, 4 Apr 2023 11:35:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235756AbjDDPft (ORCPT ); Tue, 4 Apr 2023 11:35:49 -0400 Received: from mail-oi1-x22c.google.com (mail-oi1-x22c.google.com [IPv6:2607:f8b0:4864:20::22c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D5344C3D for ; Tue, 4 Apr 2023 08:35:40 -0700 (PDT) Received: by mail-oi1-x22c.google.com with SMTP id y184so24494942oiy.8 for ; Tue, 04 Apr 2023 08:35:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1680622540; x=1683214540; 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=8FseYZBc2vbVsgmp58SjYhTfgYw9HMD9OT7D84agdy4=; b=CcF6k9bsPL04Yie0m1mJOB4RDrQYhoe5kS8g/vI84FcXM0a0q0WO8xNae0/WThsPD1 0DrENTVc6MES/bNbzu3cV9uLX3wLV/IFu/bQdq8YFc86uOm1eUQ1PXasE1laJJ7UBkE1 yzpxGCG6oDAWwApG0gSPsJYf5VA/D7HkFS/Y15svZ56Tts4LKJCd6rIDh6ccXwyewE1o bt44EhKsdZsHXdE/nnalutYhtiP1umMpYPElpmqwEdLQm87f4Mu6lEHQPsLrAudXPE0p +BtK5sFbDWtya3CXewFrKEu+ZUVD6ZJblWacnQAu8RMI2N/lYcBhwYZUsmpOTZ02v3Yl 5PkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680622540; x=1683214540; 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=8FseYZBc2vbVsgmp58SjYhTfgYw9HMD9OT7D84agdy4=; b=hiKTn6tMrkqfVSDqz7wj2DkVu8xBjX+DMZTNfc5pXHJqY8eD9NESY/hYCip21LO9fB 5Djg+/f+FUvAr8IftKkflceb5huDTxYm5+B+xxbeoBFMvO+KBv0WcjyX99MeYIbBC+Ra yi0n9q5kF0ARnyvWdHlEmvuFA9EN41IYThwTIX2UpL76r3g7In0Gm9xooIlA49eMenhJ LRPVn2cB3rvBVdmXZWddQSwaYzCAdVZQi2dv220kF9lipVHdxnNX5bzBMcDjXc43M1sj k49duMdd8KkvGcOSPEottln+whxoEnIUyY4+5VjsUO7uz5brniRvuryMn4g5KqOQPnvX 1EjQ== X-Gm-Message-State: AAQBX9eCwGgPSokUmvHJ/2gy+Dy5CSvldpCDroJ6IxybuTismk2n7eHA 3N6qqFrkPNuQmubsgtMoBt2CCg== X-Received: by 2002:aca:1a09:0:b0:384:2486:95cc with SMTP id a9-20020aca1a09000000b00384248695ccmr1446080oia.37.1680622539921; Tue, 04 Apr 2023 08:35:39 -0700 (PDT) Received: from anup-ubuntu-vm.localdomain ([103.97.165.210]) by smtp.gmail.com with ESMTPSA id w124-20020acadf82000000b00387384dc768sm5325803oig.9.2023.04.04.08.35.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 08:35:39 -0700 (PDT) From: Anup Patel To: Paolo Bonzini , Atish Patra Cc: Palmer Dabbelt , Paul Walmsley , Andrew Jones , Anup Patel , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Anup Patel , Atish Patra Subject: [PATCH v4 5/9] RISC-V: KVM: Implement subtype for CSR ONE_REG interface Date: Tue, 4 Apr 2023 21:04:48 +0530 Message-Id: <20230404153452.2405681-6-apatel@ventanamicro.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230404153452.2405681-1-apatel@ventanamicro.com> References: <20230404153452.2405681-1-apatel@ventanamicro.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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 To make the CSR ONE_REG interface extensible, we implement subtype for the CSR ONE_REG IDs. The existing CSR ONE_REG IDs are treated as subtype = 0 (aka General CSRs). Signed-off-by: Anup Patel Reviewed-by: Andrew Jones Reviewed-by: Atish Patra --- arch/riscv/include/uapi/asm/kvm.h | 3 +- arch/riscv/kvm/vcpu.c | 88 +++++++++++++++++++++++-------- 2 files changed, 69 insertions(+), 22 deletions(-) diff --git a/arch/riscv/include/uapi/asm/kvm.h b/arch/riscv/include/uapi/asm/kvm.h index 47a7c3958229..182023dc9a51 100644 --- a/arch/riscv/include/uapi/asm/kvm.h +++ b/arch/riscv/include/uapi/asm/kvm.h @@ -65,7 +65,7 @@ struct kvm_riscv_core { #define KVM_RISCV_MODE_S 1 #define KVM_RISCV_MODE_U 0 -/* CSR registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */ +/* General CSR registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */ struct kvm_riscv_csr { unsigned long sstatus; unsigned long sie; @@ -152,6 +152,7 @@ enum KVM_RISCV_SBI_EXT_ID { /* Control and status registers are mapped as type 3 */ #define KVM_REG_RISCV_CSR (0x03 << KVM_REG_RISCV_TYPE_SHIFT) +#define KVM_REG_RISCV_CSR_GENERAL (0x0 << KVM_REG_RISCV_SUBTYPE_SHIFT) #define KVM_REG_RISCV_CSR_REG(name) \ (offsetof(struct kvm_riscv_csr, name) / sizeof(unsigned long)) diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index b46e9cc92938..3394859c5f85 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -460,27 +460,72 @@ static int kvm_riscv_vcpu_set_reg_core(struct kvm_vcpu *vcpu, return 0; } +static int kvm_riscv_vcpu_general_get_csr(struct kvm_vcpu *vcpu, + unsigned long reg_num, + unsigned long *out_val) +{ + struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr; + + if (reg_num >= sizeof(struct kvm_riscv_csr) / sizeof(unsigned long)) + return -EINVAL; + + if (reg_num == KVM_REG_RISCV_CSR_REG(sip)) { + kvm_riscv_vcpu_flush_interrupts(vcpu); + *out_val = (csr->hvip >> VSIP_TO_HVIP_SHIFT) & VSIP_VALID_MASK; + } else + *out_val = ((unsigned long *)csr)[reg_num]; + + return 0; +} + +static inline int kvm_riscv_vcpu_general_set_csr(struct kvm_vcpu *vcpu, + unsigned long reg_num, + unsigned long reg_val) +{ + struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr; + + if (reg_num >= sizeof(struct kvm_riscv_csr) / sizeof(unsigned long)) + return -EINVAL; + + if (reg_num == KVM_REG_RISCV_CSR_REG(sip)) { + reg_val &= VSIP_VALID_MASK; + reg_val <<= VSIP_TO_HVIP_SHIFT; + } + + ((unsigned long *)csr)[reg_num] = reg_val; + + if (reg_num == KVM_REG_RISCV_CSR_REG(sip)) + WRITE_ONCE(vcpu->arch.irqs_pending_mask, 0); + + return 0; +} + static int kvm_riscv_vcpu_get_reg_csr(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) { - struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr; + int rc; unsigned long __user *uaddr = (unsigned long __user *)(unsigned long)reg->addr; unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_RISCV_CSR); - unsigned long reg_val; + unsigned long reg_val, reg_subtype; if (KVM_REG_SIZE(reg->id) != sizeof(unsigned long)) return -EINVAL; - if (reg_num >= sizeof(struct kvm_riscv_csr) / sizeof(unsigned long)) - return -EINVAL; - if (reg_num == KVM_REG_RISCV_CSR_REG(sip)) { - kvm_riscv_vcpu_flush_interrupts(vcpu); - reg_val = (csr->hvip >> VSIP_TO_HVIP_SHIFT) & VSIP_VALID_MASK; - } else - reg_val = ((unsigned long *)csr)[reg_num]; + reg_subtype = reg_num & KVM_REG_RISCV_SUBTYPE_MASK; + reg_num &= ~KVM_REG_RISCV_SUBTYPE_MASK; + switch (reg_subtype) { + case KVM_REG_RISCV_CSR_GENERAL: + rc = kvm_riscv_vcpu_general_get_csr(vcpu, reg_num, ®_val); + break; + default: + rc = -EINVAL; + break; + } + if (rc) + return rc; if (copy_to_user(uaddr, ®_val, KVM_REG_SIZE(reg->id))) return -EFAULT; @@ -491,31 +536,32 @@ static int kvm_riscv_vcpu_get_reg_csr(struct kvm_vcpu *vcpu, static int kvm_riscv_vcpu_set_reg_csr(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) { - struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr; + int rc; unsigned long __user *uaddr = (unsigned long __user *)(unsigned long)reg->addr; unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_RISCV_CSR); - unsigned long reg_val; + unsigned long reg_val, reg_subtype; if (KVM_REG_SIZE(reg->id) != sizeof(unsigned long)) return -EINVAL; - if (reg_num >= sizeof(struct kvm_riscv_csr) / sizeof(unsigned long)) - return -EINVAL; if (copy_from_user(®_val, uaddr, KVM_REG_SIZE(reg->id))) return -EFAULT; - if (reg_num == KVM_REG_RISCV_CSR_REG(sip)) { - reg_val &= VSIP_VALID_MASK; - reg_val <<= VSIP_TO_HVIP_SHIFT; + reg_subtype = reg_num & KVM_REG_RISCV_SUBTYPE_MASK; + reg_num &= ~KVM_REG_RISCV_SUBTYPE_MASK; + switch (reg_subtype) { + case KVM_REG_RISCV_CSR_GENERAL: + rc = kvm_riscv_vcpu_general_set_csr(vcpu, reg_num, reg_val); + break; + default: + rc = -EINVAL; + break; } - - ((unsigned long *)csr)[reg_num] = reg_val; - - if (reg_num == KVM_REG_RISCV_CSR_REG(sip)) - WRITE_ONCE(vcpu->arch.irqs_pending_mask, 0); + if (rc) + return rc; return 0; } -- 2.34.1