Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp5235085iob; Mon, 9 May 2022 11:34:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzDXPd9txb5u2D9s15+cCrD7MbwJSdF2p/bdzAEr0SFGLXTjEBv+l8SYDeOEQipXOZ6yRpV X-Received: by 2002:a05:6830:4187:b0:605:4e15:de30 with SMTP id r7-20020a056830418700b006054e15de30mr6641849otu.182.1652121287911; Mon, 09 May 2022 11:34:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652121287; cv=none; d=google.com; s=arc-20160816; b=PFqpioEj6ZBhDc1f2l5symwKroM3/bzEWCVNn4q0zyEEzGhCO7XFXO8dqdyi4UvrKt uVCv5WfFe0dToWF3n0kWL0fqttjm497UdbPt1L+yK8Tnd1+Z21kynbkBDw8eguhn5oIS MwuSS4wqrTLjVUn38vCoquj7vO16dHshu7DsQHSt1TRqVkGcuBh0om9jPIQuv3LxBZCl a7Ujtu3naa2jXP67eMQBTitvTPGHzIxpZ/dhJ0PyJVpuoy+sZywutfIaecL3E08vIpDV XoNzSI64QY24FG3TShf3+KOfJ7XmQ+DtbkZjIvU2P2HG0GIVBGe1wgFlxjUmPSYaPppx NhUg== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=HT8JD8HOTg70SDyIzNGh8qklo/ybWwxwLyVZIAu4uKc=; b=o+i2W7BOFbOyzK8ZmfRCPAipyyUBMEWc7eMVNvKTT6tQa7xjz7YBQc8o3XgCBeqKfC yn/vs4YbRFCJ++Q9xnMlECDakXvCPOtcZe7/uh58sAIZGJoI7G+7HqK9RSFxTBvV7EM/ +/AsiflrN9qNGO3/rNudZotXQbiSkNMpt7BqlTJywHDBiU+zbvohjGpgJmnbuIrGmL4m ohR7habMmN1PDCCkUjYStD92wYOL5QkVyvTp9yscAsl9JqRn9FzCNu02ZpKSIhTtt3qq D2e3dwgAjUvnHautaHvkbD4IYMks4skjmHoLFaStdXuM2dj0s2tJzmJilDlc3nbolV83 w8sg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=XcvLTWGx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id e17-20020a056870c35100b000e960f9f6f0si12382795oak.269.2022.05.09.11.34.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 May 2022 11:34:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=XcvLTWGx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D28AA2BE52E; Mon, 9 May 2022 11:29:56 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240197AbiEISdl (ORCPT + 99 others); Mon, 9 May 2022 14:33:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38288 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240185AbiEISdj (ORCPT ); Mon, 9 May 2022 14:33:39 -0400 Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B0AC52B09D8 for ; Mon, 9 May 2022 11:29:44 -0700 (PDT) Received: by mail-pf1-x433.google.com with SMTP id y41so8137935pfw.12 for ; Mon, 09 May 2022 11:29:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=HT8JD8HOTg70SDyIzNGh8qklo/ybWwxwLyVZIAu4uKc=; b=XcvLTWGxN4frUHzM4LzCHdAuFx3DpO20/OXKwC2zVshCYLJaALwhqZIRXwaXuun6OA gwtIPYUj8zYlJGRfZpiR/xVyPUYBP58JmN68uPjbdKjbpGg3Th2W16JOjKWNOB3LxWd4 iWp1/IaoAx7+oCYctKVczAa6fFdOvkoVFg2hPG/ks0+DECCoo14DWpdUleauO0QAY/Lb uaTxBtvLt1hevoDrFbYIppb2fVwy+IdqvduULR3vLQn9RJYoI75+KKP/4kbExVvFgkpv n4DvUXLPNOOLQpaVSAgsIGfPY9P7auJjP9FtCLlZMZOI979Eeeres3CyWxbQibIJYuVQ H0+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=HT8JD8HOTg70SDyIzNGh8qklo/ybWwxwLyVZIAu4uKc=; b=OIOddgv+GEJZ6ErKZFNCsGtV3yeuq7c/yfL+kqMCj0d1usdk7Oml8Ls4Vtz2TV42no TcuAeDNgnYG2J/eLP1SppA730Yr/iz6H5t/E+oAE7vfNS1pNxzzHj/O5i+SNZ1k6XBmR MxE/RfbQ+ZwVKJj6S7piDWZlp2B8qHYQxdXUbEydew2qotg4YhZUt1DNfhSeirFPz9EI +PfB8Q+hq3hZrBlKZZ3+TI7KXRrlgvdx1pLpx6uyLJSaXksVoNjXD8N4zKMSOG4h+uh9 RdPZVgoGBySumwa/pXxf7g+qABVd1/BRxtba87z9YgrtLTdHWI72ygWKwokWm0ULcbac fpGA== X-Gm-Message-State: AOAM5328ZG9H9mKsGmlGFdv843pr3rglLo0yBuuFCXel8MbUT8rIOx4L jZcc9DaaHSU17UiAPYO0qvV2Iw== X-Received: by 2002:a05:6a00:16c7:b0:4f7:e497:69b8 with SMTP id l7-20020a056a0016c700b004f7e49769b8mr17070165pfc.6.1652120984019; Mon, 09 May 2022 11:29:44 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id d20-20020a170902aa9400b0015ebbaccc46sm177241plr.159.2022.05.09.11.29.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 May 2022 11:29:43 -0700 (PDT) From: Atish Patra To: kvm@vger.kernel.org Cc: Atish Patra , Atish Patra , Anup Patel , Damien Le Moal , devicetree@vger.kernel.org, Jisheng Zhang , Krzysztof Kozlowski , kvm-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, Palmer Dabbelt , Paul Walmsley , Rob Herring Subject: [PATCH v3] RISC-V: KVM: Introduce ISA extension register Date: Mon, 9 May 2022 11:29:37 -0700 Message-Id: <20220509182937.1881849-1-atishp@rivosinc.com> X-Mailer: git-send-email 2.36.0 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,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 Currently, there is no provision for vmm (qemu-kvm or kvmtool) to query about multiple-letter ISA extensions. The config register is only used for base single letter ISA extensions. A new ISA extension register is added that will allow the vmm to query about any ISA extension one at a time. It is enabled for both single letter or multi-letter ISA extensions. The ISA extension register is useful to if the vmm requires to retrieve/set single extension while the config register should be used if all the base ISA extension required to retrieve or set. For any multi-letter ISA extensions, the new register interface must be used. Signed-off-by: Atish Patra --- Changes from v2->v3: 1. Fixed the comment style. 2. Fixed the kvm_riscv_vcpu_get_reg_isa_ext function. --- arch/riscv/include/uapi/asm/kvm.h | 20 +++++++ arch/riscv/kvm/vcpu.c | 99 +++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) diff --git a/arch/riscv/include/uapi/asm/kvm.h b/arch/riscv/include/uapi/asm/kvm.h index f808ad1ce500..92bd469e2ba6 100644 --- a/arch/riscv/include/uapi/asm/kvm.h +++ b/arch/riscv/include/uapi/asm/kvm.h @@ -82,6 +82,23 @@ struct kvm_riscv_timer { __u64 state; }; +/** + * 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 + * to the guest. This should always be contiguous to keep the mapping simple + * in KVM implementation. + */ +enum KVM_RISCV_ISA_EXT_ID { + KVM_RISCV_ISA_EXT_A = 0, + KVM_RISCV_ISA_EXT_C, + KVM_RISCV_ISA_EXT_D, + KVM_RISCV_ISA_EXT_F, + KVM_RISCV_ISA_EXT_H, + KVM_RISCV_ISA_EXT_I, + KVM_RISCV_ISA_EXT_M, + KVM_RISCV_ISA_EXT_MAX, +}; + /* Possible states for kvm_riscv_timer */ #define KVM_RISCV_TIMER_STATE_OFF 0 #define KVM_RISCV_TIMER_STATE_ON 1 @@ -123,6 +140,9 @@ struct kvm_riscv_timer { #define KVM_REG_RISCV_FP_D_REG(name) \ (offsetof(struct __riscv_d_ext_state, name) / sizeof(__u64)) +/* ISA Extension registers are mapped as type 7 */ +#define KVM_REG_RISCV_ISA_EXT (0x07 << KVM_REG_RISCV_TYPE_SHIFT) + #endif #endif /* __LINUX_KVM_RISCV_H */ diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index 7461f964d20a..0875beaa1973 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -365,6 +365,101 @@ static int kvm_riscv_vcpu_set_reg_csr(struct kvm_vcpu *vcpu, return 0; } +/* Mapping between KVM ISA Extension ID & Host ISA extension ID */ +static unsigned long kvm_isa_ext_arr[] = { + RISCV_ISA_EXT_a, + RISCV_ISA_EXT_c, + RISCV_ISA_EXT_d, + RISCV_ISA_EXT_f, + RISCV_ISA_EXT_h, + RISCV_ISA_EXT_i, + RISCV_ISA_EXT_m, +}; + +static int kvm_riscv_vcpu_get_reg_isa_ext(struct kvm_vcpu *vcpu, + const struct kvm_one_reg *reg) +{ + 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_ISA_EXT); + unsigned long reg_val = 0; + unsigned long host_isa_ext; + + if (KVM_REG_SIZE(reg->id) != sizeof(unsigned long)) + return -EINVAL; + + if (reg_num >= KVM_RISCV_ISA_EXT_MAX || reg_num >= ARRAY_SIZE(kvm_isa_ext_arr)) + return -EINVAL; + + host_isa_ext = kvm_isa_ext_arr[reg_num]; + if (__riscv_isa_extension_available(&vcpu->arch.isa, host_isa_ext)) + reg_val = 1; /* Mark the given extension as available */ + + if (copy_to_user(uaddr, ®_val, KVM_REG_SIZE(reg->id))) + return -EFAULT; + + return 0; +} + +static int kvm_riscv_vcpu_set_reg_isa_ext(struct kvm_vcpu *vcpu, + const struct kvm_one_reg *reg) +{ + 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_ISA_EXT); + unsigned long reg_val; + unsigned long host_isa_ext; + unsigned long host_isa_ext_mask; + + if (KVM_REG_SIZE(reg->id) != sizeof(unsigned long)) + return -EINVAL; + + if (reg_num >= KVM_RISCV_ISA_EXT_MAX || reg_num >= ARRAY_SIZE(kvm_isa_ext_arr)) + return -EINVAL; + + if (copy_from_user(®_val, uaddr, KVM_REG_SIZE(reg->id))) + return -EFAULT; + + host_isa_ext = kvm_isa_ext_arr[reg_num]; + if (!__riscv_isa_extension_available(NULL, host_isa_ext)) + return -EOPNOTSUPP; + + if (host_isa_ext >= RISCV_ISA_EXT_BASE && + host_isa_ext < RISCV_ISA_EXT_MAX) { + /* + * Multi-letter ISA extension. Currently there is no provision + * to enable/disable the multi-letter ISA extensions for guests. + * Return success if the request is to enable any ISA extension + * that is available in the hardware. + * Return -EOPNOTSUPP otherwise. + */ + if (!reg_val) + return -EOPNOTSUPP; + else + return 0; + } + + /* Single letter base ISA extension */ + if (!vcpu->arch.ran_atleast_once) { + host_isa_ext_mask = BIT_MASK(host_isa_ext); + if (!reg_val && (host_isa_ext_mask & KVM_RISCV_ISA_DISABLE_ALLOWED)) + vcpu->arch.isa &= ~host_isa_ext_mask; + else + vcpu->arch.isa |= host_isa_ext_mask; + vcpu->arch.isa &= riscv_isa_extension_base(NULL); + vcpu->arch.isa &= KVM_RISCV_ISA_ALLOWED; + kvm_riscv_vcpu_fp_reset(vcpu); + } else { + return -EOPNOTSUPP; + } + + return 0; +} + static int kvm_riscv_vcpu_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) { @@ -382,6 +477,8 @@ static int kvm_riscv_vcpu_set_reg(struct kvm_vcpu *vcpu, else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_FP_D) return kvm_riscv_vcpu_set_reg_fp(vcpu, reg, KVM_REG_RISCV_FP_D); + else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_ISA_EXT) + return kvm_riscv_vcpu_set_reg_isa_ext(vcpu, reg); return -EINVAL; } @@ -403,6 +500,8 @@ static int kvm_riscv_vcpu_get_reg(struct kvm_vcpu *vcpu, else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_FP_D) return kvm_riscv_vcpu_get_reg_fp(vcpu, reg, KVM_REG_RISCV_FP_D); + else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_ISA_EXT) + return kvm_riscv_vcpu_get_reg_isa_ext(vcpu, reg); return -EINVAL; } -- 2.36.0