From patchwork Mon Apr 3 09:33:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anup Patel X-Patchwork-Id: 78453 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2178135vqo; Mon, 3 Apr 2023 02:49:56 -0700 (PDT) X-Google-Smtp-Source: AKy350b3moqYIBxyrX59/r5RnH/t2NgnfU/ZF3ykgq96Gucd+aPUFZnmMZupDqLq2NjT16ayGd72 X-Received: by 2002:aa7:cb01:0:b0:4af:81fb:4c72 with SMTP id s1-20020aa7cb01000000b004af81fb4c72mr28900078edt.34.1680515395692; Mon, 03 Apr 2023 02:49:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680515395; cv=none; d=google.com; s=arc-20160816; b=ge15pT2jf4BQeTbttb3EDeJnzbc4d2ah82TKzWS+SUzda7POlNAnMpncBvBjgSFnP3 hXz0O1+et8vyHw6RqWx5nT/dFpud0z3gqeXnBa/HHmKCjWVCRXGX222utpwB1q5OroCR xP53h5KETHATrxCRNOJS/XPzKD8zWIaq64ckQXbb0/gdmnCZcqh9PmiCAYff3/aSJTnz wgmf0GUGfyFnLerXOl0x0DEgLCy7H2OFbR1xSNY8LbEWbpWwwCC7NSTgf+hDyuyTi+mA LO+01Earo4Zz9TAgw0MiuzDkxtWXnU1la+OqI27GPdH2ohUN9q8TQpaN0N+f0Z75w8vH HwLw== 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=G+FKlGY9+fsxmXa+E2ygJ8SqSZPYKfuMuKdW0nBwESI=; b=ICCfRYTZ0DMTVCUNnroBqhHlpe0oWScfy5fsHnaLEphLQ80cIn0VmKrWymoBeibc9K Q76OBhGXxiKvsQy5KcQuLJwmZ0tIbvfUmFkJNMRW7cA80rxwX8u1g+pDKPk01duVC8Rb vkPGO7QFnKuvkl3XuI4XN87wBida7HNDGZxqnr5TBUjQkEoTlraVtlg9FMW6CFtsqOl3 dYlJuYXWt5cogSOdesXT/caZRqJCf32nLthiSOYdXXzjP6iqe+Frbi+0UjPu9bbP3LO/ Lcqe4Jyi0pKLrxgSZnI57zhsXI9gK5cvHwCRuwfISwz8i24a4rXGflmS0sC1TaN1YZLh wfKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ventanamicro.com header.s=google header.b=YeO8AtHt; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h8-20020aa7c608000000b004c698b50187si6742309edq.335.2023.04.03.02.49.31; Mon, 03 Apr 2023 02:49:55 -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=YeO8AtHt; 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 S229698AbjDCJeh (ORCPT + 99 others); Mon, 3 Apr 2023 05:34:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34726 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232119AbjDCJeL (ORCPT ); Mon, 3 Apr 2023 05:34:11 -0400 Received: from mail-ot1-x330.google.com (mail-ot1-x330.google.com [IPv6:2607:f8b0:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE629CC1B for ; Mon, 3 Apr 2023 02:33:53 -0700 (PDT) Received: by mail-ot1-x330.google.com with SMTP id r40-20020a05683044a800b006a14270bc7eso11930232otv.6 for ; Mon, 03 Apr 2023 02:33:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1680514433; x=1683106433; 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=G+FKlGY9+fsxmXa+E2ygJ8SqSZPYKfuMuKdW0nBwESI=; b=YeO8AtHtNrxFd09O0Kv4XtU6gmmvhqa+32++wYbcO3Sn/g9LxebMLwQypOxMg/6HQa yWFYsJL6Fp2RRvTbWgJIaBJYcwB8HDfn2SltCxWI4THa5jjyKaARqZVDUoJYAw8y91rV vJ1ww+xXy2iYXmg2RqQ5nkIlUOk5lhbrfvS1F+7LEAa8RjJl2EI+JqCjjozOeOhu8h7d Y/EFPrErjSV8Pn/f7I+wNkpuRvQh/jYpEOQprcMVV305V9w+C4M4fQtBpIR5Ngpn8M3B OscG6i3382bUuuPHVeCYw6h5DwFafwLLr8FRDbm/0oNi6fU16Q4SIvgU+8FT5Rb/yTTH gvDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680514433; x=1683106433; 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=G+FKlGY9+fsxmXa+E2ygJ8SqSZPYKfuMuKdW0nBwESI=; b=BzY5RHof3kitnJjbJQL1z/bUb0WjyO2ZaheWCW8+odcPFVFQwppOX3F7ZHCFoZ3mq4 hnYpMrs1KIJ0c6jCjjaz/WG1Zo5oeOBOWGMwg1HSJDGZChHHd5o1WAe9vrVZ6DtBMyFy yTLTQKtAnnGA0+WoeDzioGub90MzP9E1ONmDwbfpWDJC7UFoVgIqz4BNQ0UKm2lHCdp0 ifxAAqxmEdtvGtBNDOK32yLOL88J2fhlC+iTgR8jh+jGyObYNliYVMyEmtdNb05s1Zm5 iqE7q9P8zF4N1ZSOc4qJbLLGYb/05cHC8utmyQApOCzHwzIzsXQozBWUoi4mj3JTGsjo bT8A== X-Gm-Message-State: AAQBX9cI6sMNgRnULc6nz0nf+85fjTRn2BJst8uaS79A2y6ERlwS0B2O vSvXjM6t0JElXQCkA3B+FB/3jA== X-Received: by 2002:a05:6830:1503:b0:6a3:2513:c452 with SMTP id k3-20020a056830150300b006a32513c452mr2917922otp.23.1680514433022; Mon, 03 Apr 2023 02:33:53 -0700 (PDT) Received: from anup-ubuntu-vm.localdomain ([103.97.165.210]) by smtp.gmail.com with ESMTPSA id f5-20020a9d6c05000000b006a154373578sm3953953otq.39.2023.04.03.02.33.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Apr 2023 02:33:52 -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 Subject: [PATCH v3 5/8] RISC-V: KVM: Implement subtype for CSR ONE_REG interface Date: Mon, 3 Apr 2023 15:03:07 +0530 Message-Id: <20230403093310.2271142-6-apatel@ventanamicro.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230403093310.2271142-1-apatel@ventanamicro.com> References: <20230403093310.2271142-1-apatel@ventanamicro.com> MIME-Version: 1.0 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762148111658878370?= X-GMAIL-MSGID: =?utf-8?q?1762148111658878370?= 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 1fd54ec15622..aca6b4fb7519 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; }